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