Libinfo-78.tar.gz
[apple/libinfo.git] / lookup.subproj / lu_bootp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Bootp lookup - netinfo only
26 * Copyright (C) 1989 by NeXT, Inc.
27 */
28 #include <mach/mach.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "lookup.h"
32 #include <rpc/types.h>
33 #include <rpc/xdr.h>
34 #include "_lu_types.h"
35 #include "lu_utils.h"
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <net/if.h>
40 #include <netinet/if_ether.h>
41
42 static int
43 lu_bootp_getbyether(struct ether_addr *enaddr, char **name,
44 struct in_addr *ipaddr, char **bootfile)
45 {
46 unsigned datalen;
47 XDR xdr;
48 static _lu_bootp_ent_ptr bp;
49 static int proc = -1;
50 unit lookup_buf[MAX_INLINE_UNITS];
51
52 if (proc < 0)
53 {
54 if (_lookup_link(_lu_port, "bootp_getbyether", &proc) != KERN_SUCCESS)
55 {
56 return (0);
57 }
58 }
59
60 datalen = MAX_INLINE_UNITS;
61 if (_lookup_one(_lu_port, proc, (unit *)enaddr,
62 ((sizeof(*enaddr) + sizeof(unit) - 1) / sizeof(unit)), lookup_buf,
63 &datalen) != KERN_SUCCESS)
64 {
65 return (0);
66 }
67
68 datalen *= BYTES_PER_XDR_UNIT;
69 xdrmem_create(&xdr, lookup_buf, datalen, XDR_DECODE);
70 xdr_free(xdr__lu_bootp_ent_ptr, &bp);
71 if (!xdr__lu_bootp_ent_ptr(&xdr, &bp) || (bp == NULL))
72 {
73 xdr_destroy(&xdr);
74 return (0);
75 }
76
77 xdr_destroy(&xdr);
78
79 *name = bp->bootp_name;
80 *bootfile = bp->bootp_bootfile;
81 ipaddr->s_addr = bp->bootp_ipaddr;
82 return (1);
83 }
84
85 static int
86 lu_bootp_getbyip(struct ether_addr *enaddr, char **name,
87 struct in_addr *ipaddr, char **bootfile)
88 {
89 unsigned datalen;
90 XDR xdr;
91 static _lu_bootp_ent_ptr bp;
92 static int proc = -1;
93 unit lookup_buf[MAX_INLINE_UNITS];
94
95 if (proc < 0)
96 {
97 if (_lookup_link(_lu_port, "bootp_getbyip", &proc) != KERN_SUCCESS)
98 {
99 return (0);
100 }
101 }
102
103 datalen = MAX_INLINE_UNITS;
104 if (_lookup_one(_lu_port, proc, (unit *)ipaddr,
105 ((sizeof(*ipaddr) + sizeof(unit) - 1) / sizeof(unit)), lookup_buf,
106 &datalen) != KERN_SUCCESS)
107 {
108 return (0);
109 }
110
111 datalen *= BYTES_PER_XDR_UNIT;
112 xdrmem_create(&xdr, lookup_buf, datalen, XDR_DECODE);
113 xdr_free(xdr__lu_bootp_ent_ptr, &bp);
114 if (!xdr__lu_bootp_ent_ptr(&xdr, &bp) || (bp == NULL))
115 {
116 xdr_destroy(&xdr);
117 return (0);
118 }
119
120 xdr_destroy(&xdr);
121
122 *name = bp->bootp_name;
123 *bootfile = bp->bootp_bootfile;
124 bcopy(bp->bootp_enaddr, enaddr, sizeof(*enaddr));
125 return (1);
126 }
127
128 int
129 bootp_getbyether(struct ether_addr *enaddr, char **name,
130 struct in_addr *ipaddr, char **bootfile)
131 {
132 if (_lu_running())
133 return (lu_bootp_getbyether(enaddr, name, ipaddr, bootfile));
134 return (0);
135 }
136
137 int
138 bootp_getbyip(struct ether_addr *enaddr, char **name,
139 struct in_addr *ipaddr, char **bootfile)
140 {
141 if (_lu_running())
142 return (lu_bootp_getbyip(enaddr, name, ipaddr, bootfile));
143 return (0);
144 }
145