]> git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bpwhoami.tproj/bpwhoami.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / bootparams / bpwhoami.tproj / bpwhoami.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved
27 *
28 * bpwhoami.c
29 * - do a bootparams whoami call and echo the results to stdout
30 *
31 * The output is of the form:
32 * host_name=<hostname>
33 * nis_domain=<nis domain name>
34 * router=<router ip address>
35 * server_name=<server host name>
36 * server_ip_address=<server ip address>
37 *
38 * The program will exit with the following codes:
39 * 0 Successfully retrieved info
40 * 1 RPC timed out while attempting to retrieve info
41 * 2 Unrecoverable error ie. don't bother trying to call again
42 *
43 * Modification History:
44 * Aug 5, 1997 Dieter Siegund (dieter@apple.com)
45 * - lifted code from the old hostname -AUTOMATIC- source
46 */
47
48
49 #include <sys/param.h>
50
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 void usage __P((void));
58
59 #include <netdb.h>
60 #include <signal.h>
61 #include <mach/boolean.h>
62 #include <sys/types.h>
63 #include <sys/file.h>
64 #include <sys/fcntl.h>
65 #include <sys/ioctl_compat.h>
66 #include <sys/ioctl.h>
67 #include <sys/socket.h>
68 #include <sys/time.h>
69 #include <net/if.h>
70 #include <ifaddrs.h>
71 #include <rpc/rpc.h>
72 #include <arpa/inet.h>
73 #include "bootparam_prot.h"
74
75 struct in_addr ip_address;
76 struct in_addr net_mask;
77
78 static __inline__
79 u_long iptohl(struct in_addr ip)
80 {
81 return (ntohl(ip.s_addr));
82 }
83
84 static __inline__ boolean_t
85 in_subnet(struct in_addr netaddr, struct in_addr netmask, struct in_addr ip)
86 {
87 if ((iptohl(ip) & iptohl(netmask))
88 != (iptohl(netaddr) & iptohl(netmask))) {
89 return (FALSE);
90 }
91 return (TRUE);
92 }
93
94 bool_t
95 each_whoresult(result, from)
96 bp_whoami_res *result;
97 struct sockaddr_in *from;
98 {
99 if (result) {
100 struct hostent * host;
101 struct in_addr * router;
102
103 /*
104 * guard against bogus router replies by making sure
105 * that the default router is on the same subnet
106 */
107 router = (struct in_addr *)
108 &result->router_address.bp_address_u.ip_addr;
109 if (in_subnet(*router, net_mask, ip_address)) {
110 if (result->client_name && result->client_name[0])
111 printf("host_name=%s\n", result->client_name);
112 if (result->domain_name && result->domain_name[0])
113 printf("nis_domain=%s\n", result->domain_name);
114 printf("router=%s\n", inet_ntoa(*router));
115 host = gethostbyaddr((char *) &from->sin_addr,
116 sizeof (from->sin_addr), AF_INET);
117 if (host) {
118 printf("server_name=%s\n", host->h_name);
119 }
120 printf("server_ip_address=%s\n", inet_ntoa(from->sin_addr));
121 return(TRUE);
122 }
123 }
124 return(FALSE);
125 }
126
127
128 static boolean_t
129 getFirstInterface(struct sockaddr_in *ret_p)
130 {
131 struct ifaddrs *ifap;
132 struct ifaddrs *ifcurrent;
133 getifaddrs(&ifap);
134
135 for (ifcurrent = ifap; ifcurrent; ifcurrent = ifcurrent->ifa_next) {
136 if (ifcurrent->ifa_addr->sa_family == AF_INET) {
137 if ((ifcurrent->ifa_flags & IFF_LOOPBACK)
138 || !(ifcurrent->ifa_flags & IFF_UP))
139 continue;
140 net_mask = ((struct sockaddr_in*)(ifcurrent->ifa_netmask))->sin_addr;
141 *ret_p = *((struct sockaddr_in*)(ifcurrent->ifa_addr));
142 freeifaddrs(ifap);
143 return (TRUE);
144 }
145 }
146 if (ifap)
147 freeifaddrs(ifap);
148 return (FALSE);
149 }
150
151
152 /*
153 * Routine: bp_whoami
154 * Function:
155 * Do a BOOTPARAMS WHOAMI RPC to find out hostname, domain,
156 * bootparams server, default router.
157 */
158 int
159 bp_whoami()
160 {
161 extern enum clnt_stat clnt_broadcast();
162 struct sockaddr_in sockin;
163 enum clnt_stat stat;
164 struct bp_whoami_arg who_arg;
165 struct bp_whoami_res who_res;
166
167 if (getFirstInterface(&sockin) == FALSE)
168 return (2);
169
170 ip_address = sockin.sin_addr;
171 who_arg.client_address.bp_address_u.ip_addr =
172 *((ip_addr_t *)&sockin.sin_addr);
173 who_arg.client_address.address_type = IP_ADDR_TYPE;
174 bzero(&who_res, sizeof (who_res));
175
176 /*
177 * Broadcast the whoami.
178 */
179 stat = clnt_broadcast(BOOTPARAMPROG, BOOTPARAMVERS,
180 BOOTPARAMPROC_WHOAMI, xdr_bp_whoami_arg,
181 &who_arg, xdr_bp_whoami_res, &who_res,
182 each_whoresult);
183
184 if (stat == RPC_SUCCESS) {
185 return (0);
186 }
187 if (stat == RPC_TIMEDOUT)
188 return (1);
189 fprintf(stderr, "bpwhoami: ");
190 clnt_perrno(stat);
191 return (2);
192 }
193
194 int
195 main(argc,argv)
196 int argc;
197 char *argv[];
198 {
199 exit(bp_whoami());
200 }