5f0c8bd6c99cbfab5e26aaee2208105bd58733b5
[reactos.git] / reactos / subsys / system / dhcp / include / dhcpd.h
1 /* $OpenBSD: dhcpd.h,v 1.33 2004/05/06 22:29:15 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6 * The Internet Software Consortium. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of The Internet Software Consortium nor the names
18 * of its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * This software has been written for the Internet Software Consortium
36 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37 * Enterprises. To learn more about the Internet Software Consortium,
38 * see ``http://www.vix.com/isc''. To learn more about Vixie
39 * Enterprises, see ``http://www.vix.com''.
40 */
41
42 #ifndef DHCPD_H
43 #define DHCPD_H
44
45 #if defined (_MSC_VER)
46 #pragma warning( once : 4103 )
47 #endif
48
49 #if !defined(ssize_t)
50 #define ssize_t long
51 #endif
52
53 #include <winsock2.h>
54 #include <iphlpapi.h>
55 #include <stdint.h>
56
57 #define IFNAMSIZ MAX_INTERFACE_NAME_LEN
58
59 #define ETH_ALEN 6
60 #define ETHER_ADDR_LEN ETH_ALEN
61
62 #include <pshpack1.h>
63 struct ether_header
64 {
65 u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
66 u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
67 u_int16_t ether_type; /* packet type ID field */
68 } ;
69 #include <poppack.h>
70
71 struct ip
72 {
73 unsigned int ip_hl:4; /* header length */
74 unsigned int ip_v:4; /* version */
75 u_int8_t ip_tos; /* type of service */
76 u_short ip_len; /* total length */
77 u_short ip_id; /* identification */
78 u_short ip_off; /* fragment offset field */
79 #define IP_RF 0x8000 /* reserved fragment flag */
80 #define IP_DF 0x4000 /* dont fragment flag */
81 #define IP_MF 0x2000 /* more fragments flag */
82 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
83 u_int8_t ip_ttl; /* time to live */
84 u_int8_t ip_p; /* protocol */
85 u_short ip_sum; /* checksum */
86 struct in_addr ip_src, ip_dst; /* source and dest address */
87 };
88
89 struct udphdr {
90 u_int16_t uh_sport; /* source port */
91 u_int16_t uh_dport; /* destination port */
92 u_int16_t uh_ulen; /* udp length */
93 u_int16_t uh_sum; /* udp checksum */
94 };
95
96 #define ETHERTYPE_IP 0x0800
97 #define IPTOS_LOWDELAY 0x10
98 #define ARPHRD_ETHER 1
99
100 // FIXME: I have no idea what this should be!
101 #define SIZE_T_MAX 1600
102
103 #define USE_SOCKET_RECEIVE
104 #define USE_SOCKET_SEND
105
106 #if defined (_MSC_VER)
107 #include <types.h>
108 #include <stat.h>
109 #include <time.h>
110 #include <stddef.h>
111 #else
112 #include <sys/types.h>
113 #include <sys/stat.h>
114 #include <sys/time.h>
115 #endif
116
117 #include <ctype.h>
118 #include <errno.h>
119 #include <fcntl.h>
120 #include <limits.h>
121 #include <unistd.h>
122 #include <stdarg.h>
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <string.h>
126 #include <time.h>
127 #include <unistd.h>
128
129 #include "dhcp.h"
130 #include "tree.h"
131
132 #define LOCAL_PORT 68
133 #define REMOTE_PORT 67
134
135 struct option_data {
136 int len;
137 u_int8_t *data;
138 };
139
140 struct string_list {
141 struct string_list *next;
142 char *string;
143 };
144
145 struct iaddr {
146 int len;
147 unsigned char iabuf[16];
148 };
149
150 struct iaddrlist {
151 struct iaddrlist *next;
152 struct iaddr addr;
153 };
154
155 struct packet {
156 struct dhcp_packet *raw;
157 int packet_length;
158 int packet_type;
159 int options_valid;
160 int client_port;
161 struct iaddr client_addr;
162 struct interface_info *interface;
163 struct hardware *haddr;
164 struct option_data options[256];
165 };
166
167 struct hardware {
168 u_int8_t htype;
169 u_int8_t hlen;
170 u_int8_t haddr[16];
171 };
172
173 struct client_lease {
174 struct client_lease *next;
175 time_t expiry, renewal, rebind;
176 struct iaddr address;
177 char *server_name;
178 char *filename;
179 struct string_list *medium;
180 unsigned int is_static : 1;
181 unsigned int is_bootp : 1;
182 struct option_data options[256];
183 };
184
185 /* Possible states in which the client can be. */
186 enum dhcp_state {
187 S_REBOOTING,
188 S_INIT,
189 S_SELECTING,
190 S_REQUESTING,
191 S_BOUND,
192 S_RENEWING,
193 S_REBINDING,
194 S_STATIC
195 };
196
197 struct client_config {
198 struct option_data defaults[256];
199 enum {
200 ACTION_DEFAULT,
201 ACTION_SUPERSEDE,
202 ACTION_PREPEND,
203 ACTION_APPEND
204 } default_actions[256];
205
206 struct option_data send_options[256];
207 u_int8_t required_options[256];
208 u_int8_t requested_options[256];
209 int requested_option_count;
210 time_t timeout;
211 time_t initial_interval;
212 time_t retry_interval;
213 time_t select_interval;
214 time_t reboot_timeout;
215 time_t backoff_cutoff;
216 struct string_list *media;
217 char *script_name;
218 enum { IGNORE, ACCEPT, PREFER }
219 bootp_policy;
220 struct string_list *medium;
221 struct iaddrlist *reject_list;
222 };
223
224 struct client_state {
225 struct client_lease *active;
226 struct client_lease *new;
227 struct client_lease *offered_leases;
228 struct client_lease *leases;
229 struct client_lease *alias;
230 enum dhcp_state state;
231 struct iaddr destination;
232 u_int32_t xid;
233 u_int16_t secs;
234 time_t first_sending;
235 time_t interval;
236 struct string_list *medium;
237 struct dhcp_packet packet;
238 int packet_length;
239 struct iaddr requested_address;
240 struct client_config *config;
241 };
242
243 struct interface_info {
244 struct interface_info *next;
245 struct hardware hw_address;
246 struct in_addr primary_address;
247 char name[IFNAMSIZ];
248 int rfdesc;
249 int wfdesc;
250 unsigned char *rbuf;
251 size_t rbuf_max;
252 size_t rbuf_offset;
253 size_t rbuf_len;
254 struct client_state *client;
255 int noifmedia;
256 int errors;
257 int dead;
258 u_int16_t index;
259 };
260
261 struct timeout {
262 struct timeout *next;
263 time_t when;
264 void (*func)(void *);
265 void *what;
266 };
267
268 struct protocol {
269 struct protocol *next;
270 int fd;
271 void (*handler)(struct protocol *);
272 void *local;
273 };
274
275 #define DEFAULT_HASH_SIZE 97
276
277 struct hash_bucket {
278 struct hash_bucket *next;
279 unsigned char *name;
280 int len;
281 unsigned char *value;
282 };
283
284 struct hash_table {
285 int hash_count;
286 struct hash_bucket *buckets[DEFAULT_HASH_SIZE];
287 };
288
289 /* Default path to dhcpd config file. */
290 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
291 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
292 #define DHCPD_LOG_FACILITY LOG_DAEMON
293
294 #define MAX_TIME 0x7fffffff
295 #define MIN_TIME 0
296
297 /* External definitions... */
298
299 /* options.c */
300 int cons_options(struct packet *, struct dhcp_packet *, int,
301 struct tree_cache **, int, int, int, u_int8_t *, int);
302 char *pretty_print_option(unsigned int,
303 unsigned char *, int, int, int);
304 void do_packet(struct interface_info *, struct dhcp_packet *,
305 int, unsigned int, struct iaddr, struct hardware *);
306
307 /* errwarn.c */
308 #if defined (__GNUC__)
309 extern int warnings_occurred;
310 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
311 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
312 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
313 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
314 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
315 #endif
316
317 /* conflex.c */
318 extern int lexline, lexchar;
319 extern char *token_line, *tlname;
320 extern char comments[4096];
321 extern int comment_index;
322 extern int eol_token;
323 void new_parse(char *);
324 int next_token(char **, FILE *);
325 int peek_token(char **, FILE *);
326
327 /* parse.c */
328 void skip_to_semi(FILE *);
329 int parse_semi(FILE *);
330 char *parse_string(FILE *);
331 int parse_ip_addr(FILE *, struct iaddr *);
332 void parse_hardware_param(FILE *, struct hardware *);
333 void parse_lease_time(FILE *, time_t *);
334 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, int *,
335 int, int, int);
336 void convert_num(unsigned char *, char *, int, int);
337 time_t parse_date(FILE *);
338
339 /* tree.c */
340 pair cons(caddr_t, pair);
341
342 /* alloc.c */
343 struct string_list *new_string_list(size_t size);
344 struct hash_table *new_hash_table(int);
345 struct hash_bucket *new_hash_bucket(void);
346
347 /* bpf.c */
348 int if_register_bpf(struct interface_info *);
349 void if_register_send(struct interface_info *);
350 void if_register_receive(struct interface_info *);
351 ssize_t send_packet(struct interface_info *, struct dhcp_packet *, size_t,
352 struct in_addr, struct sockaddr_in *, struct hardware *);
353 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
354 struct sockaddr_in *, struct hardware *);
355
356 /* dispatch.c */
357 extern void (*bootp_packet_handler)(struct interface_info *,
358 struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *);
359 void discover_interfaces(struct interface_info *);
360 void reinitialize_interfaces(void);
361 void dispatch(void);
362 void got_one(struct protocol *);
363 void add_timeout(time_t, void (*)(void *), void *);
364 void cancel_timeout(void (*)(void *), void *);
365 void add_protocol(char *, int, void (*)(struct protocol *), void *);
366 void remove_protocol(struct protocol *);
367 struct protocol *find_protocol_by_adapter( struct interface_info * );
368 int interface_link_status(char *);
369
370 /* hash.c */
371 struct hash_table *new_hash(void);
372 void add_hash(struct hash_table *, unsigned char *, int, unsigned char *);
373 unsigned char *hash_lookup(struct hash_table *, unsigned char *, int);
374
375 /* tables.c */
376 extern struct dhcp_option dhcp_options[256];
377 extern unsigned char dhcp_option_default_priority_list[];
378 extern int sizeof_dhcp_option_default_priority_list;
379 extern struct hash_table universe_hash;
380 extern struct universe dhcp_universe;
381 void initialize_universes(void);
382
383 /* convert.c */
384 u_int32_t getULong(unsigned char *);
385 int32_t getLong(unsigned char *);
386 u_int16_t getUShort(unsigned char *);
387 int16_t getShort(unsigned char *);
388 void putULong(unsigned char *, u_int32_t);
389 void putLong(unsigned char *, int32_t);
390 void putUShort(unsigned char *, unsigned int);
391 void putShort(unsigned char *, int);
392
393 /* inet.c */
394 struct iaddr subnet_number(struct iaddr, struct iaddr);
395 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
396 int addr_eq(struct iaddr, struct iaddr);
397 char *piaddr(struct iaddr);
398
399 /* dhclient.c */
400 extern char *path_dhclient_conf;
401 extern char *path_dhclient_db;
402 extern time_t cur_time;
403 extern int log_priority;
404 extern int log_perror;
405
406 extern struct client_config top_level_config;
407
408 void dhcpoffer(struct packet *);
409 void dhcpack(struct packet *);
410 void dhcpnak(struct packet *);
411
412 void send_discover(void *);
413 void send_request(void *);
414 void send_decline(void *);
415
416 void state_reboot(void *);
417 void state_init(void *);
418 void state_selecting(void *);
419 void state_requesting(void *);
420 void state_bound(void *);
421 void state_panic(void *);
422
423 void bind_lease(struct interface_info *);
424
425 void make_discover(struct interface_info *, struct client_lease *);
426 void make_request(struct interface_info *, struct client_lease *);
427 void make_decline(struct interface_info *, struct client_lease *);
428
429 void free_client_lease(struct client_lease *);
430 void rewrite_client_leases(void);
431 void write_client_lease(struct interface_info *, struct client_lease *, int);
432
433 void priv_script_init(char *, char *);
434 void priv_script_write_params(char *, struct client_lease *);
435 int priv_script_go(void);
436
437 void script_init(char *, struct string_list *);
438 void script_write_params(char *, struct client_lease *);
439 int script_go(void);
440 void client_envadd(struct client_state *,
441 const char *, const char *, const char *, ...);
442 void script_set_env(struct client_state *, const char *, const char *,
443 const char *);
444 void script_flush_env(struct client_state *);
445 int dhcp_option_ev_name(char *, size_t, struct dhcp_option *);
446
447 struct client_lease *packet_to_lease(struct packet *);
448 void go_daemon(void);
449 void client_location_changed(void);
450
451 void bootp(struct packet *);
452 void dhcp(struct packet *);
453
454 /* packet.c */
455 void assemble_hw_header(struct interface_info *, unsigned char *,
456 int *, struct hardware *);
457 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
458 unsigned int, unsigned char *, int);
459 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
460 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
461 unsigned char *, int);
462
463 /* ethernet.c */
464 void assemble_ethernet_header(struct interface_info *, unsigned char *,
465 int *, struct hardware *);
466 ssize_t decode_ethernet_header(struct interface_info *, unsigned char *,
467 int, struct hardware *);
468
469 /* clparse.c */
470 int read_client_conf(void);
471 void read_client_leases(void);
472 void parse_client_statement(FILE *, struct interface_info *,
473 struct client_config *);
474 int parse_X(FILE *, u_int8_t *, int);
475 int parse_option_list(FILE *, u_int8_t *);
476 void parse_interface_declaration(FILE *, struct client_config *);
477 struct interface_info *interface_or_dummy(char *);
478 void make_client_state(struct interface_info *);
479 void make_client_config(struct interface_info *, struct client_config *);
480 void parse_client_lease_statement(FILE *, int);
481 void parse_client_lease_declaration(FILE *, struct client_lease *,
482 struct interface_info **);
483 struct dhcp_option *parse_option_decl(FILE *, struct option_data *);
484 void parse_string_list(FILE *, struct string_list **, int);
485 void parse_reject_statement(FILE *, struct client_config *);
486
487 /* privsep.c */
488 struct buf *buf_open(size_t);
489 int buf_add(struct buf *, void *, size_t);
490 int buf_close(int, struct buf *);
491 ssize_t buf_read(int, void *, size_t);
492 void dispatch_imsg(int);
493
494 #endif/*DHCPD_H*/