2 * Copyright (c) 1999-2002 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.1 (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 * Bootp lookup - netinfo only
26 * Copyright (C) 1989 by NeXT, Inc.
28 #include <mach/mach.h>
32 #include <rpc/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <netinet/if_ether.h>
39 #include <net/ethernet.h>
42 #include "_lu_types.h"
46 static pthread_mutex_t _bootp_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 struct ether_addr b_enaddr
;
52 struct in_addr b_ipaddr
;
57 free_bootp(struct bootpent
*b
)
59 if (b
== NULL
) return;
61 if (b
->b_name
!= NULL
) free(b
->b_name
);
62 if (b
->b_bootfile
!= NULL
) free(b
->b_bootfile
);
67 static struct bootpent
*
68 extract_bootp(XDR
*xdr
)
72 int i
, j
, nvals
, nkeys
, status
;
75 if (xdr
== NULL
) return NULL
;
77 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
79 b
= (struct bootpent
*)calloc(1, sizeof(struct bootpent
));
81 for (i
= 0; i
< nkeys
; i
++)
87 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
102 if ((b
->b_name
== NULL
) && (!strcmp("name", key
)))
107 if ((b
->b_name
== NULL
) && (!strcmp("bootfile", key
)))
109 b
->b_bootfile
= vals
[0];
112 else if (!strcmp("ip_address", key
))
114 b
->b_ipaddr
.s_addr
= inet_addr(vals
[0]);
116 else if (!strcmp("en_address", key
))
118 pthread_mutex_lock(&_bootp_lock
);
119 e
= ether_aton(vals
[0]);
120 if (e
!= NULL
) memcpy(&(b
->b_enaddr
), e
, sizeof(struct ether_addr
));
121 pthread_mutex_unlock(&_bootp_lock
);
128 for (; j
< nvals
; j
++) free(vals
[j
]);
133 if (b
->b_name
== NULL
) b
->b_name
= strdup("");
134 if (b
->b_bootfile
== NULL
) b
->b_bootfile
= strdup("");
140 lu_bootp_getbyether(struct ether_addr
*enaddr
, char **name
,
141 struct in_addr
*ipaddr
, char **bootfile
)
146 static int proc
= -1;
152 if (_lookup_link(_lu_port
, "bootp_getbyether", &proc
) != KERN_SUCCESS
)
161 if (_lookup_all(_lu_port
, proc
, (unit
*)enaddr
, ((sizeof(*enaddr
) + sizeof(unit
) - 1) / sizeof(unit
)), &lookup_buf
, &datalen
) != KERN_SUCCESS
)
166 datalen
*= BYTES_PER_XDR_UNIT
;
167 if ((lookup_buf
== NULL
) || (datalen
== 0)) return 0;
169 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
172 if (!xdr_int(&inxdr
, &count
))
175 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
182 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
186 b
= extract_bootp(&inxdr
);
190 *bootfile
= b
->b_bootfile
;
191 ipaddr
->s_addr
= b
->b_ipaddr
.s_addr
;
194 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
200 lu_bootp_getbyip(struct ether_addr
*enaddr
, char **name
,
201 struct in_addr
*ipaddr
, char **bootfile
)
206 static int proc
= -1;
212 if (_lookup_link(_lu_port
, "bootp_getbyip", &proc
) != KERN_SUCCESS
)
221 if (_lookup_all(_lu_port
, proc
, (unit
*)ipaddr
, ((sizeof(*ipaddr
) + sizeof(unit
) - 1) / sizeof(unit
)), &lookup_buf
, &datalen
) != KERN_SUCCESS
)
226 datalen
*= BYTES_PER_XDR_UNIT
;
227 if ((lookup_buf
== NULL
) || (datalen
== 0)) return 0;
229 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
232 if (!xdr_int(&inxdr
, &count
))
235 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
242 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
246 b
= extract_bootp(&inxdr
);
250 *bootfile
= b
->b_bootfile
;
251 memcpy(enaddr
, &(b
->b_enaddr
), sizeof(struct ether_addr
));
254 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
260 bootp_getbyether(struct ether_addr
*enaddr
, char **name
,struct in_addr
*ipaddr
, char **bootfile
)
264 return (lu_bootp_getbyether(enaddr
, name
, ipaddr
, bootfile
));
270 bootp_getbyip(struct ether_addr
*enaddr
, char **name
, struct in_addr
*ipaddr
, char **bootfile
)
274 return (lu_bootp_getbyip(enaddr
, name
, ipaddr
, bootfile
));