Path: blob/main/crypto/krb5/src/lib/rpc/get_myaddress.c
39537 views
/* @(#)get_myaddress.c 2.1 88/07/29 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* get_myaddress.c39*40* Get client's IP address via ioctl. This avoids using the yellowpages.41*/4243#ifdef GSSAPI_KRB544#include <string.h>45#include <gssrpc/types.h>46#include <gssrpc/rpc.h>47#include <gssrpc/pmap_prot.h>48#include <sys/socket.h>49#include <netinet/in.h>50#include <krb5.h>51/*52* don't use gethostbyname, which would invoke yellow pages53*/54int55get_myaddress(struct sockaddr_in *addr)56{57memset(addr, 0, sizeof(*addr));58addr->sin_family = AF_INET;59addr->sin_port = htons(PMAPPORT);60addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);61return (0);62}63#else /* !GSSAPI_KRB5 */64#include <gssrpc/types.h>65#include <gssrpc/pmap_prot.h>66#include <sys/socket.h>67#if defined(sun)68#include <sys/sockio.h>69#endif70#include <stdio.h>71#ifdef OSF172#include <net/route.h>73#include <sys/mbuf.h>74#endif75#include <net/if.h>76#include <sys/ioctl.h>77#include <arpa/inet.h>78#include <netinet/in.h>7980/*81* don't use gethostbyname, which would invoke yellow pages82*/83get_myaddress(struct sockaddr_in *addr)84{85int s;86char buf[256 * sizeof (struct ifreq)];87struct ifconf ifc;88struct ifreq ifreq, *ifr;89int len;9091if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {92perror("get_myaddress: socket");93exit(1);94}95set_cloexec_fd(s);96ifc.ifc_len = sizeof (buf);97ifc.ifc_buf = buf;98if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {99perror("get_myaddress: ioctl (get interface configuration)");100exit(1);101}102ifr = ifc.ifc_req;103for (len = ifc.ifc_len; len; len -= sizeof ifreq) {104ifreq = *ifr;105if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {106perror("get_myaddress: ioctl");107exit(1);108}109if ((ifreq.ifr_flags & IFF_UP) &&110ifr->ifr_addr.sa_family == AF_INET) {111*addr = *((struct sockaddr_in *)&ifr->ifr_addr);112addr->sin_port = htons(PMAPPORT);113break;114}115ifr++;116}117(void) close(s);118}119#endif /* !GSSAPI_KRB5 */120121122