]>
git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bpwhoami.tproj/bpwhoami.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved
28 * - do a bootparams whoami call and echo the results to stdout
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>
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
42 * Modification History:
43 * Aug 5, 1997 Dieter Siegund (dieter@apple.com)
44 * - lifted code from the old hostname -AUTOMATIC- source
48 #include <sys/param.h>
56 void usage
__P((void));
60 #include <mach/boolean.h>
61 #include <sys/types.h>
63 #include <sys/fcntl.h>
64 #include <sys/ioctl_compat.h>
65 #include <sys/ioctl.h>
66 #include <sys/socket.h>
71 #include <arpa/inet.h>
72 #include "bootparam_prot.h"
74 struct in_addr ip_address
;
75 struct in_addr net_mask
;
78 u_long
iptohl(struct in_addr ip
)
80 return (ntohl(ip
.s_addr
));
83 static __inline__ boolean_t
84 in_subnet(struct in_addr netaddr
, struct in_addr netmask
, struct in_addr ip
)
86 if ((iptohl(ip
) & iptohl(netmask
))
87 != (iptohl(netaddr
) & iptohl(netmask
))) {
94 each_whoresult(result
, from
)
95 bp_whoami_res
*result
;
96 struct sockaddr_in
*from
;
99 struct hostent
* host
;
100 struct in_addr
* router
;
103 * guard against bogus router replies by making sure
104 * that the default router is on the same subnet
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
);
117 printf("server_name=%s\n", host
->h_name
);
119 printf("server_ip_address=%s\n", inet_ntoa(from
->sin_addr
));
128 getFirstInterface(struct sockaddr_in
*ret_p
)
130 struct ifaddrs
*ifap
;
131 struct ifaddrs
*ifcurrent
;
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
))
139 net_mask
= ((struct sockaddr_in
*)(ifcurrent
->ifa_netmask
))->sin_addr
;
140 *ret_p
= *((struct sockaddr_in
*)(ifcurrent
->ifa_addr
));
154 * Do a BOOTPARAMS WHOAMI RPC to find out hostname, domain,
155 * bootparams server, default router.
160 extern enum clnt_stat
clnt_broadcast();
161 struct sockaddr_in sockin
;
163 struct bp_whoami_arg who_arg
;
164 struct bp_whoami_res who_res
;
166 if (getFirstInterface(&sockin
) == FALSE
)
169 ip_address
= sockin
.sin_addr
;
170 who_arg
.client_address
.bp_address_u
.ip_addr
=
171 *((ip_addr_t
*)&sockin
.sin_addr
);
172 who_arg
.client_address
.address_type
= IP_ADDR_TYPE
;
173 bzero(&who_res
, sizeof (who_res
));
176 * Broadcast the whoami.
178 stat
= clnt_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
,
179 BOOTPARAMPROC_WHOAMI
, xdr_bp_whoami_arg
,
180 &who_arg
, xdr_bp_whoami_res
, &who_res
,
183 if (stat
== RPC_SUCCESS
) {
186 if (stat
== RPC_TIMEDOUT
)
188 fprintf(stderr
, "bpwhoami: ");