ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/slirp/bootp.c
Revision: 1.3
Committed: 2005-05-14T17:37:07Z (19 years, 6 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
CVS Tags: nigel-build-19, nigel-build-17
Changes since 1.2: +1 -13 lines
Log Message:
Remove bootp debug code as it uses varargs macros that are not supported in
all foreign compilers (including preprocessor shipped with MacOS X!). Also
fix build on platforms not including <stdint.h> by default.

File Contents

# Content
1 /*
2 * QEMU BOOTP/DHCP server
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <slirp.h>
25
26 /* XXX: only DHCP is supported */
27
28 #define NB_ADDR 16
29
30 #define START_ADDR 15
31
32 #define LEASE_TIME (24 * 3600)
33
34 typedef struct {
35 uint8_t allocated;
36 uint8_t macaddr[6];
37 } BOOTPClient;
38
39 BOOTPClient bootp_clients[NB_ADDR];
40
41 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
42
43 static BOOTPClient *get_new_addr(struct in_addr *paddr)
44 {
45 BOOTPClient *bc;
46 int i;
47
48 for(i = 0; i < NB_ADDR; i++) {
49 if (!bootp_clients[i].allocated)
50 goto found;
51 }
52 return NULL;
53 found:
54 bc = &bootp_clients[i];
55 bc->allocated = 1;
56 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
57 return bc;
58 }
59
60 static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
61 {
62 BOOTPClient *bc;
63 int i;
64
65 for(i = 0; i < NB_ADDR; i++) {
66 if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
67 goto found;
68 }
69 return NULL;
70 found:
71 bc = &bootp_clients[i];
72 bc->allocated = 1;
73 paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
74 return bc;
75 }
76
77 static void dhcp_decode(const uint8_t *buf, int size,
78 int *pmsg_type)
79 {
80 const uint8_t *p, *p_end;
81 int len, tag;
82
83 *pmsg_type = 0;
84
85 p = buf;
86 p_end = buf + size;
87 if (size < 5)
88 return;
89 if (memcmp(p, rfc1533_cookie, 4) != 0)
90 return;
91 p += 4;
92 while (p < p_end) {
93 tag = p[0];
94 if (tag == RFC1533_PAD) {
95 p++;
96 } else if (tag == RFC1533_END) {
97 break;
98 } else {
99 p++;
100 if (p >= p_end)
101 break;
102 len = *p++;
103
104 switch(tag) {
105 case RFC2132_MSG_TYPE:
106 if (len >= 1)
107 *pmsg_type = p[0];
108 break;
109 default:
110 break;
111 }
112 p += len;
113 }
114 }
115 }
116
117 static void bootp_reply(struct bootp_t *bp)
118 {
119 BOOTPClient *bc;
120 struct mbuf *m;
121 struct bootp_t *rbp;
122 struct sockaddr_in saddr, daddr;
123 struct in_addr dns_addr;
124 int dhcp_msg_type, val;
125 uint8_t *q;
126
127 /* extract exact DHCP msg type */
128 dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
129
130 if (dhcp_msg_type == 0)
131 dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
132
133 if (dhcp_msg_type != DHCPDISCOVER &&
134 dhcp_msg_type != DHCPREQUEST)
135 return;
136 /* XXX: this is a hack to get the client mac address */
137 memcpy(client_ethaddr, bp->bp_hwaddr, 6);
138
139 if ((m = m_get()) == NULL)
140 return;
141 m->m_data += if_maxlinkhdr;
142 rbp = (struct bootp_t *)m->m_data;
143 m->m_data += sizeof(struct udpiphdr);
144 memset(rbp, 0, sizeof(struct bootp_t));
145
146 if (dhcp_msg_type == DHCPDISCOVER) {
147 new_addr:
148 bc = get_new_addr(&daddr.sin_addr);
149 if (!bc)
150 return;
151 memcpy(bc->macaddr, client_ethaddr, 6);
152 } else {
153 bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
154 if (!bc) {
155 /* if never assigned, behaves as if it was already
156 assigned (windows fix because it remembers its address) */
157 goto new_addr;
158 }
159 }
160
161 saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
162 saddr.sin_port = htons(BOOTP_SERVER);
163
164 daddr.sin_port = htons(BOOTP_CLIENT);
165
166 rbp->bp_op = BOOTP_REPLY;
167 rbp->bp_xid = bp->bp_xid;
168 rbp->bp_htype = 1;
169 rbp->bp_hlen = 6;
170 memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
171
172 rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
173 rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
174
175 q = rbp->bp_vend;
176 memcpy(q, rfc1533_cookie, 4);
177 q += 4;
178
179 if (dhcp_msg_type == DHCPDISCOVER) {
180 *q++ = RFC2132_MSG_TYPE;
181 *q++ = 1;
182 *q++ = DHCPOFFER;
183 } else if (dhcp_msg_type == DHCPREQUEST) {
184 *q++ = RFC2132_MSG_TYPE;
185 *q++ = 1;
186 *q++ = DHCPACK;
187 }
188
189 if (dhcp_msg_type == DHCPDISCOVER ||
190 dhcp_msg_type == DHCPREQUEST) {
191 *q++ = RFC2132_SRV_ID;
192 *q++ = 4;
193 memcpy(q, &saddr.sin_addr, 4);
194 q += 4;
195
196 *q++ = RFC1533_NETMASK;
197 *q++ = 4;
198 *q++ = 0xff;
199 *q++ = 0xff;
200 *q++ = 0xff;
201 *q++ = 0x00;
202
203 *q++ = RFC1533_GATEWAY;
204 *q++ = 4;
205 memcpy(q, &saddr.sin_addr, 4);
206 q += 4;
207
208 *q++ = RFC1533_DNS;
209 *q++ = 4;
210 dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
211 memcpy(q, &dns_addr, 4);
212 q += 4;
213
214 *q++ = RFC2132_LEASE_TIME;
215 *q++ = 4;
216 val = htonl(LEASE_TIME);
217 memcpy(q, &val, 4);
218 q += 4;
219 }
220 *q++ = RFC1533_END;
221
222 m->m_len = sizeof(struct bootp_t) -
223 sizeof(struct ip) - sizeof(struct udphdr);
224 udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
225 }
226
227 void bootp_input(struct mbuf *m)
228 {
229 struct bootp_t *bp = mtod(m, struct bootp_t *);
230
231 if (bp->bp_op == BOOTP_REQUEST) {
232 bootp_reply(bp);
233 }
234 }