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