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