]>
git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bpwhoami.tproj/bpwhoami.c
e0729d4df2116d8f000b30d44cf499e5edd27376
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved
27 * - do a bootparams whoami call and echo the results to stdout
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>
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
41 * Modification History:
42 * Aug 5, 1997 Dieter Siegund (dieter@apple.com)
43 * - lifted code from the old hostname -AUTOMATIC- source
47 #include <sys/param.h>
55 void usage
__P((void));
59 #include <mach/boolean.h>
60 #include <sys/types.h>
62 #include <sys/fcntl.h>
63 #include <sys/ioctl_compat.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
70 #include <arpa/inet.h>
71 #include "bootparam_prot.h"
73 struct in_addr ip_address
;
74 struct in_addr net_mask
;
77 u_long
iptohl(struct in_addr ip
)
79 return (ntohl(ip
.s_addr
));
82 static __inline__ boolean_t
83 in_subnet(struct in_addr netaddr
, struct in_addr netmask
, struct in_addr ip
)
85 if ((iptohl(ip
) & iptohl(netmask
))
86 != (iptohl(netaddr
) & iptohl(netmask
))) {
93 each_whoresult(result
, from
)
94 bp_whoami_res
*result
;
95 struct sockaddr_in
*from
;
98 struct hostent
* host
;
99 struct in_addr
* router
;
102 * guard against bogus router replies by making sure
103 * that the default router is on the same subnet
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
);
116 printf("server_name=%s\n", host
->h_name
);
118 printf("server_ip_address=%s\n", inet_ntoa(from
->sin_addr
));
127 getFirstInterface(struct sockaddr_in
*ret_p
)
129 struct ifaddrs
*ifap
;
130 struct ifaddrs
*ifcurrent
;
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
))
138 net_mask
= ((struct sockaddr_in
*)(ifcurrent
->ifa_netmask
))->sin_addr
;
139 *ret_p
= *((struct sockaddr_in
*)(ifcurrent
->ifa_addr
));
153 * Do a BOOTPARAMS WHOAMI RPC to find out hostname, domain,
154 * bootparams server, default router.
159 extern enum clnt_stat
clnt_broadcast();
160 struct sockaddr_in sockin
;
162 struct bp_whoami_arg who_arg
;
163 struct bp_whoami_res who_res
;
165 if (getFirstInterface(&sockin
) == FALSE
)
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
));
175 * Broadcast the whoami.
177 stat
= clnt_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
,
178 BOOTPARAMPROC_WHOAMI
, xdr_bp_whoami_arg
,
179 &who_arg
, xdr_bp_whoami_res
, &who_res
,
182 if (stat
== RPC_SUCCESS
) {
185 if (stat
== RPC_TIMEDOUT
)
187 fprintf(stderr
, "bpwhoami: ");