Sync with trunk r43123
[reactos.git] / reactos / base / services / 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 #include <winsock2.h>
46 #include <iphlpapi.h>
47 #include "stdint.h"
48
49 #define IFNAMSIZ MAX_INTERFACE_NAME_LEN
50
51 #define ETH_ALEN 6
52 #define ETHER_ADDR_LEN ETH_ALEN
53 #include <pshpack1.h>
54 struct ether_header
55 {
56 u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
57 u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
58 u_int16_t ether_type; /* packet type ID field */
59 };
60 #include <poppack.h>
61
62 struct ip
63 {
64 unsigned int ip_hl:4; /* header length */
65 unsigned int ip_v:4; /* version */
66 u_int8_t ip_tos; /* type of service */
67 u_short ip_len; /* total length */
68 u_short ip_id; /* identification */
69 u_short ip_off; /* fragment offset field */
70 #define IP_RF 0x8000 /* reserved fragment flag */
71 #define IP_DF 0x4000 /* dont fragment flag */
72 #define IP_MF 0x2000 /* more fragments flag */
73 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
74 u_int8_t ip_ttl; /* time to live */
75 u_int8_t ip_p; /* protocol */
76 u_short ip_sum; /* checksum */
77 struct in_addr ip_src, ip_dst; /* source and dest address */
78 };
79
80 struct udphdr {
81 u_int16_t uh_sport; /* source port */
82 u_int16_t uh_dport; /* destination port */
83 u_int16_t uh_ulen; /* udp length */
84 u_int16_t uh_sum; /* udp checksum */
85 };
86
87 #define ETHERTYPE_IP 0x0800
88 #define IPTOS_LOWDELAY 0x10
89 #define ARPHRD_ETHER 1
90
91 // FIXME: I have no idea what this should be!
92 #define SIZE_T_MAX 1600
93
94 #define USE_SOCKET_RECEIVE
95 #define USE_SOCKET_SEND
96
97 #include <sys/types.h>
98 #include <sys/stat.h>
99 //#include <sys/time.h>
100 #include <ctype.h>
101 #include <fcntl.h>
102 #include <limits.h>
103 //#include <unistd.h>
104 #include <stdio.h>
105 #include <stdlib.h>
106 #include <string.h>
107 #include <time.h>
108 //#include <unistd.h>
109
110 #include "dhcp.h"
111 #include "tree.h"
112
113 #define LOCAL_PORT 68
114 #define REMOTE_PORT 67
115
116 struct option_data {
117 int len;
118 u_int8_t *data;
119 };
120
121 struct string_list {
122 struct string_list *next;
123 char *string;
124 };
125
126 struct iaddr {
127 int len;
128 unsigned char iabuf[16];
129 };
130
131 struct iaddrlist {
132 struct iaddrlist *next;
133 struct iaddr addr;
134 };
135
136 struct packet {
137 struct dhcp_packet *raw;
138 int packet_length;
139 int packet_type;
140 int options_valid;
141 int client_port;
142 struct iaddr client_addr;
143 struct interface_info *interface;
144 struct hardware *haddr;
145 struct option_data options[256];
146 };
147
148 struct hardware {
149 u_int8_t htype;
150 u_int8_t hlen;
151 u_int8_t haddr[16];
152 };
153
154 struct client_lease {
155 struct client_lease *next;
156 time_t expiry, renewal, rebind;
157 struct iaddr address;
158 char *server_name;
159 #ifdef __REACTOS__
160 time_t obtained;
161 struct iaddr serveraddress;
162 #endif
163 char *filename;
164 struct string_list *medium;
165 unsigned int is_static : 1;
166 unsigned int is_bootp : 1;
167 struct option_data options[256];
168 };
169
170 /* Possible states in which the client can be. */
171 enum dhcp_state {
172 S_REBOOTING,
173 S_INIT,
174 S_SELECTING,
175 S_REQUESTING,
176 S_BOUND,
177 S_RENEWING,
178 S_REBINDING,
179 S_STATIC
180 };
181
182 struct client_config {
183 struct option_data defaults[256];
184 enum {
185 ACTION_DEFAULT,
186 ACTION_SUPERSEDE,
187 ACTION_PREPEND,
188 ACTION_APPEND
189 } default_actions[256];
190
191 struct option_data send_options[256];
192 u_int8_t required_options[256];
193 u_int8_t requested_options[256];
194 int requested_option_count;
195 time_t timeout;
196 time_t initial_interval;
197 time_t retry_interval;
198 time_t select_interval;
199 time_t reboot_timeout;
200 time_t backoff_cutoff;
201 struct string_list *media;
202 char *script_name;
203 enum { IGNORE, ACCEPT, PREFER }
204 bootp_policy;
205 struct string_list *medium;
206 struct iaddrlist *reject_list;
207 };
208
209 struct client_state {
210 struct client_lease *active;
211 struct client_lease *new;
212 struct client_lease *offered_leases;
213 struct client_lease *leases;
214 struct client_lease *alias;
215 enum dhcp_state state;
216 struct iaddr destination;
217 u_int32_t xid;
218 u_int16_t secs;
219 time_t first_sending;
220 time_t interval;
221 struct string_list *medium;
222 struct dhcp_packet packet;
223 int packet_length;
224 struct iaddr requested_address;
225 struct client_config *config;
226 };
227
228 struct interface_info {
229 struct interface_info *next;
230 struct hardware hw_address;
231 struct in_addr primary_address;
232 char name[IFNAMSIZ];
233 int rfdesc;
234 int wfdesc;
235 unsigned char *rbuf;
236 size_t rbuf_max;
237 size_t rbuf_offset;
238 size_t rbuf_len;
239 struct client_state *client;
240 int noifmedia;
241 int errors;
242 int dead;
243 u_int16_t index;
244 };
245
246 struct timeout {
247 struct timeout *next;
248 time_t when;
249 void (*func)(void *);
250 void *what;
251 };
252
253 struct protocol {
254 struct protocol *next;
255 int fd;
256 void (*handler)(struct protocol *);
257 void *local;
258 };
259
260 #define DEFAULT_HASH_SIZE 97
261
262 struct hash_bucket {
263 struct hash_bucket *next;
264 unsigned char *name;
265 int len;
266 unsigned char *value;
267 };
268
269 struct hash_table {
270 int hash_count;
271 struct hash_bucket *buckets[DEFAULT_HASH_SIZE];
272 };
273
274 /* Default path to dhcpd config file. */
275 #define _PATH_DHCLIENT_CONF "/etc/dhclient.conf"
276 #define _PATH_DHCLIENT_DB "/var/db/dhclient.leases"
277 #define DHCPD_LOG_FACILITY LOG_DAEMON
278
279 #define MAX_TIME 0x7fffffff
280 #define MIN_TIME 0
281
282 /* External definitions... */
283
284 /* options.c */
285 int cons_options(struct packet *, struct dhcp_packet *, int,
286 struct tree_cache **, int, int, int, u_int8_t *, int);
287 char *pretty_print_option(unsigned int,
288 unsigned char *, int, int, int);
289 void do_packet(struct interface_info *, struct dhcp_packet *,
290 int, unsigned int, struct iaddr, struct hardware *);
291
292 /* errwarn.c */
293 extern int warnings_occurred;
294 #ifdef _MSC_VER
295 void error(char *, ...);
296 int warning(char *, ...);
297 int note(char *, ...);
298 int debug(char *, ...);
299 int parse_warn(char *, ...);
300 #else
301 void error(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
302 int warning(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
303 int note(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
304 int debug(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
305 int parse_warn(char *, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
306 #endif
307
308 /* conflex.c */
309 extern int lexline, lexchar;
310 extern char *token_line, *tlname;
311 extern char comments[4096];
312 extern int comment_index;
313 extern int eol_token;
314 void new_parse(char *);
315 int next_token(char **, FILE *);
316 int peek_token(char **, FILE *);
317
318 /* parse.c */
319 void skip_to_semi(FILE *);
320 int parse_semi(FILE *);
321 char *parse_string(FILE *);
322 int parse_ip_addr(FILE *, struct iaddr *);
323 void parse_hardware_param(FILE *, struct hardware *);
324 void parse_lease_time(FILE *, time_t *);
325 unsigned char *parse_numeric_aggregate(FILE *, unsigned char *, int *,
326 int, int, int);
327 void convert_num(unsigned char *, char *, int, int);
328 time_t parse_date(FILE *);
329
330 /* tree.c */
331 pair cons(caddr_t, pair);
332
333 /* alloc.c */
334 struct string_list *new_string_list(size_t size);
335 struct hash_table *new_hash_table(int);
336 struct hash_bucket *new_hash_bucket(void);
337 void dfree(void *, char *);
338 void free_hash_bucket(struct hash_bucket *, char *);
339
340
341 /* bpf.c */
342 int if_register_bpf(struct interface_info *);
343 void if_register_send(struct interface_info *);
344 void if_register_receive(struct interface_info *);
345 ssize_t send_packet(struct interface_info *, struct dhcp_packet *, size_t,
346 struct in_addr, struct sockaddr_in *, struct hardware *);
347 ssize_t receive_packet(struct interface_info *, unsigned char *, size_t,
348 struct sockaddr_in *, struct hardware *);
349
350 /* dispatch.c */
351 extern void (*bootp_packet_handler)(struct interface_info *,
352 struct dhcp_packet *, int, unsigned int, struct iaddr, struct hardware *);
353 void discover_interfaces(struct interface_info *);
354 void reinitialize_interfaces(void);
355 void dispatch(void);
356 void got_one(struct protocol *);
357 void add_timeout(time_t, void (*)(void *), void *);
358 void cancel_timeout(void (*)(void *), void *);
359 void add_protocol(char *, int, void (*)(struct protocol *), void *);
360 void remove_protocol(struct protocol *);
361 struct protocol *find_protocol_by_adapter( struct interface_info * );
362 int interface_link_status(char *);
363
364 /* hash.c */
365 struct hash_table *new_hash(void);
366 void add_hash(struct hash_table *, unsigned char *, int, unsigned char *);
367 unsigned char *hash_lookup(struct hash_table *, unsigned char *, int);
368
369 /* tables.c */
370 extern struct dhcp_option dhcp_options[256];
371 extern unsigned char dhcp_option_default_priority_list[];
372 extern int sizeof_dhcp_option_default_priority_list;
373 extern struct hash_table universe_hash;
374 extern struct universe dhcp_universe;
375 void initialize_universes(void);
376
377 /* convert.c */
378 u_int32_t getULong(unsigned char *);
379 int32_t getLong(unsigned char *);
380 u_int16_t getUShort(unsigned char *);
381 int16_t getShort(unsigned char *);
382 void putULong(unsigned char *, u_int32_t);
383 void putLong(unsigned char *, int32_t);
384 void putUShort(unsigned char *, unsigned int);
385 void putShort(unsigned char *, int);
386
387 /* inet.c */
388 struct iaddr subnet_number(struct iaddr, struct iaddr);
389 struct iaddr broadcast_addr(struct iaddr, struct iaddr);
390 int addr_eq(struct iaddr, struct iaddr);
391 char *piaddr(struct iaddr);
392
393 /* dhclient.c */
394 extern char *path_dhclient_conf;
395 extern char *path_dhclient_db;
396 extern time_t cur_time;
397 extern int log_priority;
398 extern int log_perror;
399
400 extern struct client_config top_level_config;
401
402 void dhcpoffer(struct packet *);
403 void dhcpack(struct packet *);
404 void dhcpnak(struct packet *);
405
406 void send_discover(void *);
407 void send_request(void *);
408 void send_decline(void *);
409
410 void state_reboot(void *);
411 void state_init(void *);
412 void state_selecting(void *);
413 void state_requesting(void *);
414 void state_bound(void *);
415 void state_panic(void *);
416
417 void bind_lease(struct interface_info *);
418
419 void make_discover(struct interface_info *, struct client_lease *);
420 void make_request(struct interface_info *, struct client_lease *);
421 void make_decline(struct interface_info *, struct client_lease *);
422
423 void free_client_lease(struct client_lease *);
424 void rewrite_client_leases(struct interface_info *);
425 void write_client_lease(struct interface_info *, struct client_lease *, int);
426
427 void priv_script_init(struct interface_info *, char *, char *);
428 void priv_script_write_params(struct interface_info *, char *, struct client_lease *);
429 int priv_script_go(void);
430
431 void script_init(char *, struct string_list *);
432 void script_write_params(char *, struct client_lease *);
433 int script_go(void);
434 void client_envadd(struct client_state *,
435 const char *, const char *, const char *, ...);
436 void script_set_env(struct client_state *, const char *, const char *,
437 const char *);
438 void script_flush_env(struct client_state *);
439 int dhcp_option_ev_name(char *, size_t, struct dhcp_option *);
440
441 struct client_lease *packet_to_lease(struct packet *);
442 void go_daemon(void);
443 void client_location_changed(void);
444
445 void bootp(struct packet *);
446 void dhcp(struct packet *);
447
448 /* packet.c */
449 void assemble_hw_header(struct interface_info *, unsigned char *,
450 int *, struct hardware *);
451 void assemble_udp_ip_header(unsigned char *, int *, u_int32_t, u_int32_t,
452 unsigned int, unsigned char *, int);
453 ssize_t decode_hw_header(unsigned char *, int, struct hardware *);
454 ssize_t decode_udp_ip_header(unsigned char *, int, struct sockaddr_in *,
455 unsigned char *, int);
456
457 /* ethernet.c */
458 void assemble_ethernet_header(struct interface_info *, unsigned char *,
459 int *, struct hardware *);
460 ssize_t decode_ethernet_header(struct interface_info *, unsigned char *,
461 int, struct hardware *);
462
463 /* clparse.c */
464 int read_client_conf(struct interface_info *);
465 void read_client_leases(void);
466 void parse_client_statement(FILE *, struct interface_info *,
467 struct client_config *);
468 int parse_X(FILE *, u_int8_t *, int);
469 int parse_option_list(FILE *, u_int8_t *);
470 void parse_interface_declaration(FILE *, struct client_config *);
471 struct interface_info *interface_or_dummy(char *);
472 void make_client_state(struct interface_info *);
473 void make_client_config(struct interface_info *, struct client_config *);
474 void parse_client_lease_statement(FILE *, int);
475 void parse_client_lease_declaration(FILE *, struct client_lease *,
476 struct interface_info **);
477 struct dhcp_option *parse_option_decl(FILE *, struct option_data *);
478 void parse_string_list(FILE *, struct string_list **, int);
479 void parse_reject_statement(FILE *, struct client_config *);
480
481 /* privsep.c */
482 struct buf *buf_open(size_t);
483 int buf_add(struct buf *, void *, size_t);
484 int buf_close(int, struct buf *);
485 ssize_t buf_read(int, void *, size_t);
486 void dispatch_imsg(int);
487
488 #endif/*DHCPD_H*/