Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / TBBT / trace_play / rpc / get_myaddress.c
1 #ifndef lint
2 static char sfs_get_myaddress_id[] = "@(#)get_myaddress.c     2.1     97/10/23";
3 #endif
4 /* @(#)get_myaddress.c  2.1 88/07/29 4.0 RPCSRC */
5 /*
6  *   Copyright (c) 1992-1997,2001 by Standard Performance Evaluation Corporation
7  *      All rights reserved.
8  *              Standard Performance Evaluation Corporation (SPEC)
9  *              6585 Merchant Place, Suite 100
10  *              Warrenton, VA 20187
11  *
12  *      This product contains benchmarks acquired from several sources who
13  *      understand and agree with SPEC's goal of creating fair and objective
14  *      benchmarks to measure computer performance.
15  *
16  *      This copyright notice is placed here only to protect SPEC in the
17  *      event the source is misused in any manner that is contrary to the
18  *      spirit, the goals and the intent of SPEC.
19  *
20  *      The source code is provided to the user or company under the license
21  *      agreement for the SPEC Benchmark Suite for this product.
22  */
23 /*
24  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
25  * unrestricted use provided that this legend is included on all tape
26  * media and as a part of the software program in whole or part.  Users
27  * may copy or modify Sun RPC without charge, but are not authorized
28  * to license or distribute it to anyone else except as part of a product or
29  * program developed by the user.
30  * 
31  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
32  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
34  * 
35  * Sun RPC is provided with no support and without any obligation on the
36  * part of Sun Microsystems, Inc. to assist in its use, correction,
37  * modification or enhancement.
38  * 
39  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
40  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
41  * OR ANY PART THEREOF.
42  * 
43  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
44  * or profits or other special, indirect and consequential damages, even if
45  * Sun has been advised of the possibility of such damages.
46  * 
47  * Sun Microsystems, Inc.
48  * 2550 Garcia Avenue
49  * Mountain View, California  94043
50  */
51 #if !defined(lint) && defined(SCCSIDS)
52 static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro";
53 #endif
54
55 /*
56  * get_myaddress.c
57  *
58  * Get client's IP address via ioctl.  This avoids using the yellowpages.
59  * Copyright (C) 1984, Sun Microsystems, Inc.
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h> 
65 #ifndef FreeBSD
66 #include <stropts.h>    
67 #endif /* ndef FreeBSD */
68 #include <string.h>
69 #include "rpc/rpc.h"
70 #include "rpc/pmap_prot.h"
71 #include "rpc/osdep.h"
72 #include <sys/utsname.h>
73
74 #ifdef FreeBSD
75 #include <netdb.h>      
76 #define MY_NAMELEN 256
77 static char myhostname[MY_NAMELEN];
78 #endif /* def FreeBSD */
79
80 void
81 get_myaddress(
82         struct sockaddr_in *addr)
83 {
84         int s;
85         char buf[BUFSIZ];
86         struct ifconf ifc;
87         struct ifreq ifreq, *ifr;
88         int len;        
89         struct sockaddr_in tmp_addr;
90 #ifdef _AIX
91         char    *cp, *cplim;
92 #endif
93
94 #ifdef FreeBSD
95         static struct in_addr *my_addr;
96         struct hostent *ent;     
97
98         /* In FreeBSD, SIOCGIFCONF provides AF_LINK information, not AF_INET. */
99         gethostname(myhostname, MY_NAMELEN);
100         ent = gethostbyname(myhostname);
101         if (ent == NULL) {
102             fprintf(stderr, "lookup on server's name failed\n");
103             exit(1);
104         }
105         bzero(addr, sizeof(struct sockaddr_in));
106         my_addr = (struct in_addr *)(*(ent->h_addr_list));
107         bcopy(my_addr, &(addr->sin_addr.s_addr), sizeof(struct in_addr));
108         addr->sin_family = AF_INET;
109         addr->sin_port = htons(PMAPPORT);
110         return;                
111 #endif /* def FreeBSD */
112
113         if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
114             perror("get_myaddress: socket");
115             exit(1);
116         }
117         ifc.ifc_len = sizeof (buf);
118         ifc.ifc_buf = buf;
119         if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
120                 perror("get_myaddress: ioctl (get interface configuration)");
121                 exit(1);
122         }
123         ifr = ifc.ifc_req;
124 #ifdef _AIX
125         cplim = buf + ifc.ifc_len;
126         for (cp = buf; cp < cplim;
127              cp += MAX(sizeof(struct ifreq),
128                        (sizeof (ifr->ifr_name) +
129                         MAX((ifr->ifr_addr).sa_len, sizeof(ifr->ifr_addr))))) {
130                 ifr = (struct ifreq *)cp;
131 #else
132         for (len = ifc.ifc_len; len; len -= sizeof(ifreq), ifr++) {
133 #endif
134                 ifreq = *ifr;
135                 /* Save address, since SIOCGIFFLAGS may scribble over *ifr. */
136                 tmp_addr = *((struct sockaddr_in *)&ifr->ifr_addr);
137                 if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) {
138                         perror("get_myaddress: ioctl");
139                         exit(1);
140                 }
141                 if ((ifreq.ifr_flags & IFF_UP) &&
142                     ifr->ifr_addr.sa_family == AF_INET) {
143                         *addr = tmp_addr;
144                         addr->sin_port = htons(PMAPPORT);
145                         break;
146                 }
147         }
148         (void) close(s);
149 }
150
151 /*
152  * A generic gethostname
153  */
154 int
155 getmyhostname(char *name, int namelen)
156 {
157 #if !defined(HAS_GETHOSTNAME)
158         struct utsname utsname;
159         int ret;
160
161         ret = uname(&utsname);
162         if (ret == -1)
163                 return (-1);
164         (void) strncpy(name, utsname.nodename, namelen);
165         return (0);
166 #else
167         return(gethostname(name, namelen));
168 #endif
169 }