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>
41 #include "_lu_types.h"
45 extern struct ether_addr
*ether_aton(char *);
47 static pthread_mutex_t _bootp_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 struct ether_addr b_enaddr
;
53 struct in_addr b_ipaddr
;
58 free_bootp(struct bootpent
*b
)
60 if (b
== NULL
) return;
62 if (b
->b_name
!= NULL
) free(b
->b_name
);
63 if (b
->b_bootfile
!= NULL
) free(b
->b_bootfile
);
68 static struct bootpent
*
69 extract_bootp(XDR
*xdr
)
73 int i
, j
, nvals
, nkeys
, status
;
76 if (xdr
== NULL
) return NULL
;
78 if (!xdr_int(xdr
, &nkeys
)) return NULL
;
80 b
= (struct bootpent
*)calloc(1, sizeof(struct bootpent
));
82 for (i
= 0; i
< nkeys
; i
++)
88 status
= _lu_xdr_attribute(xdr
, &key
, &vals
, &nvals
);
103 if ((b
->b_name
== NULL
) && (!strcmp("name", key
)))
108 if ((b
->b_name
== NULL
) && (!strcmp("bootfile", key
)))
110 b
->b_bootfile
= vals
[0];
113 else if (!strcmp("ip_address", key
))
115 b
->b_ipaddr
.s_addr
= inet_addr(vals
[0]);
117 else if (!strcmp("en_address", key
))
119 pthread_mutex_lock(&_bootp_lock
);
120 e
= ether_aton(vals
[0]);
121 if (e
!= NULL
) memcpy(&(b
->b_enaddr
), e
, sizeof(struct ether_addr
));
122 pthread_mutex_unlock(&_bootp_lock
);
129 for (; j
< nvals
; j
++) free(vals
[j
]);
134 if (b
->b_name
== NULL
) b
->b_name
= strdup("");
135 if (b
->b_bootfile
== NULL
) b
->b_bootfile
= strdup("");
141 lu_bootp_getbyether(struct ether_addr
*enaddr
, char **name
,
142 struct in_addr
*ipaddr
, char **bootfile
)
147 static int proc
= -1;
153 if (_lookup_link(_lu_port
, "bootp_getbyether", &proc
) != KERN_SUCCESS
)
162 if (_lookup_all(_lu_port
, proc
, (unit
*)enaddr
, ((sizeof(*enaddr
) + sizeof(unit
) - 1) / sizeof(unit
)), &lookup_buf
, &datalen
) != KERN_SUCCESS
)
167 datalen
*= BYTES_PER_XDR_UNIT
;
168 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
170 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
173 if (!xdr_int(&inxdr
, &count
))
176 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
183 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
187 b
= extract_bootp(&inxdr
);
191 *bootfile
= b
->b_bootfile
;
192 ipaddr
->s_addr
= b
->b_ipaddr
.s_addr
;
195 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
201 lu_bootp_getbyip(struct ether_addr
*enaddr
, char **name
,
202 struct in_addr
*ipaddr
, char **bootfile
)
207 static int proc
= -1;
213 if (_lookup_link(_lu_port
, "bootp_getbyip", &proc
) != KERN_SUCCESS
)
222 if (_lookup_all(_lu_port
, proc
, (unit
*)ipaddr
, ((sizeof(*ipaddr
) + sizeof(unit
) - 1) / sizeof(unit
)), &lookup_buf
, &datalen
) != KERN_SUCCESS
)
227 datalen
*= BYTES_PER_XDR_UNIT
;
228 if ((lookup_buf
== NULL
) || (datalen
== 0)) return NULL
;
230 xdrmem_create(&inxdr
, lookup_buf
, datalen
, XDR_DECODE
);
233 if (!xdr_int(&inxdr
, &count
))
236 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
243 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
247 b
= extract_bootp(&inxdr
);
251 *bootfile
= b
->b_bootfile
;
252 memcpy(enaddr
, &(b
->b_enaddr
), sizeof(struct ether_addr
));
255 vm_deallocate(mach_task_self(), (vm_address_t
)lookup_buf
, datalen
);
261 bootp_getbyether(struct ether_addr
*enaddr
, char **name
,struct in_addr
*ipaddr
, char **bootfile
)
265 return (lu_bootp_getbyether(enaddr
, name
, ipaddr
, bootfile
));
271 bootp_getbyip(struct ether_addr
*enaddr
, char **name
, struct in_addr
*ipaddr
, char **bootfile
)
275 return (lu_bootp_getbyip(enaddr
, name
, ipaddr
, bootfile
));