]>
git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bpwhoami.tproj/bpwhoami.c
c846417eb6753b30517b89b1b3bc65291edc76a9
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>
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
));
128 getFirstInterface(struct ifreq
* ret_p
)
130 struct ifconf ifconf
; /* points to ifreq */
131 struct ifreq
* ifreq
= NULL
;
133 int size
= sizeof(struct ifreq
) * MAX_IF
;
136 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
137 fprintf(stderr
, "bpwhoami: socket call failed\n");
143 ifreq
= (struct ifreq
*)realloc(ifreq
, size
);
145 ifreq
= (struct ifreq
*)malloc(size
);
150 ifconf
.ifc_len
= size
;
151 ifconf
.ifc_req
= ifreq
;
152 if (ioctl(sockfd
, SIOCGIFCONF
, (caddr_t
)&ifconf
) < 0
153 || ifconf
.ifc_len
<= 0) {
154 fprintf(stderr
, "bpwhoami: ioctl SIOCGIFCONF failed\n");
157 if ((ifconf
.ifc_len
+ sizeof(struct ifreq
)) < size
)
161 #define IFR_NEXT(ifr) \
162 ((struct ifreq *) ((char *) (ifr) + sizeof(*(ifr)) + \
163 MAX(0, (int) (ifr)->ifr_addr.sa_len \
164 - (int) sizeof((ifr)->ifr_addr))))
165 for (ifrp
= (struct ifreq
*) ifconf
.ifc_buf
;
166 (char *) ifrp
< &ifconf
.ifc_buf
[ifconf
.ifc_len
];
167 ifrp
= IFR_NEXT(ifrp
)) {
168 if (ifrp
->ifr_addr
.sa_family
== AF_INET
) {
171 strncpy(ifr
.ifr_name
, ifrp
->ifr_name
, sizeof(ifr
.ifr_name
));
172 if (ioctl(sockfd
, SIOCGIFFLAGS
, (caddr_t
)&ifr
) < 0)
174 else if ((ifr
.ifr_flags
& IFF_LOOPBACK
)
175 || !(ifr
.ifr_flags
& IFF_UP
))
177 else if (ioctl(sockfd
, SIOCGIFNETMASK
, (caddr_t
)&ifr
) < 0)
180 net_mask
= ((struct sockaddr_in
*)&ifr
.ifr_addr
)->sin_addr
;
199 * Do a BOOTPARAMS WHOAMI RPC to find out hostname, domain,
200 * bootparams server, default router.
205 extern enum clnt_stat
clnt_broadcast();
207 struct sockaddr_in
*sockin
;
209 struct bp_whoami_arg who_arg
;
210 struct bp_whoami_res who_res
;
212 if (getFirstInterface(&ifr
) == FALSE
)
215 sockin
= (struct sockaddr_in
*) &ifr
.ifr_addr
;
216 ip_address
= sockin
->sin_addr
;
217 who_arg
.client_address
.bp_address_u
.ip_addr
=
218 *((ip_addr_t
*)&sockin
->sin_addr
);
219 who_arg
.client_address
.address_type
= IP_ADDR_TYPE
;
220 bzero(&who_res
, sizeof (who_res
));
223 * Broadcast the whoami.
225 stat
= clnt_broadcast(BOOTPARAMPROG
, BOOTPARAMVERS
,
226 BOOTPARAMPROC_WHOAMI
, xdr_bp_whoami_arg
,
227 &who_arg
, xdr_bp_whoami_res
, &who_res
,
230 if (stat
== RPC_SUCCESS
) {
233 if (stat
== RPC_TIMEDOUT
)
235 fprintf(stderr
, "bpwhoami: ");