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