coverity appeasement
[minix.git] / commands / dhcpd / dhcpd.h
blob93bf5a89987c9a6f1ba85aa56d6503f178f2bfc6
1 /* dhcpd.h - Dynamic Host Configuration Protocol daemon.
2 * Author: Kees J. Bot
3 * 16 Dec 2000
4 */
6 #define nil ((void*)0)
8 #include <paths.h>
10 /* Paths to files. */
11 #define PATH_DHCPCONF _PATH_DHCPCONF
12 #define PATH_DHCPPID _PATH_DHCPPID
13 #define PATH_DHCPCACHE _PATH_DHCPCACHE
14 #define PATH_DHCPPOOL _PATH_DHCPPOOL
16 #define CLID_MAX 32 /* Maximum client ID length. */
18 #ifndef EXTERN
19 #define EXTERN extern
20 #endif
22 extern int lwip;
24 EXTERN char *program; /* This program's name. */
25 extern char *configfile; /* Configuration file. */
26 extern char *poolfile; /* Dynamic address pool. */
27 EXTERN int serving; /* True if being a DHCP server. */
28 EXTERN unsigned test; /* Test level. */
29 EXTERN unsigned debug; /* Debug level. */
30 EXTERN asynchio_t asyn; /* Bookkeeping for all async I/O. */
32 /* BOOTP UDP ports: (That they are different is quite stupid.) */
33 EXTERN u16_t port_server; /* Port server listens on. */
34 EXTERN u16_t port_client; /* Port client listens on. */
36 #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
37 #define arraylimit(a) ((a) + arraysize(a))
38 #define between(a,c,z) (sizeof(c) <= sizeof(unsigned) ? \
39 (unsigned) (c) - (a) <= (unsigned) (z) - (a) : \
40 (unsigned long) (c) - (a) <= (unsigned long) (z) - (a))
42 /* To treat objects as octet arrays: */
43 #define B(a) ((u8_t *) (a))
45 /* Times. */
46 EXTERN time_t start, now; /* Start and current time. */
47 EXTERN time_t event; /* Time of the next timed event. */
49 /* Special times and periods: */
50 #define NEVER (sizeof(time_t) <= sizeof(int) ? INT_MAX : LONG_MAX)
51 #define DELTA_FIRST 4 /* Between first and second query. */
52 #define DELTA_FAST 64 /* Unbound queries this often. */
53 #define DELTA_SLOW 512 /* Bound queries are more relaxed. */
54 #define N_SOLICITS 3 /* Number of solicitations. */
55 #define DELTA_SOL 3 /* Time between solicitations. */
56 #define DELTA_ADV 2048 /* Router adverts to self lifetime. */
58 /* Buffers for packets. */
59 typedef struct buf {
60 eth_hdr_t *eth; /* Ethernet header in payload. */
61 ip_hdr_t *ip; /* IP header in payload. */
62 udp_hdr_t *udp; /* UDP header in payload. */
63 udp_io_hdr_t *udpio; /* UDP I/O header in payload. */
64 dhcp_t *dhcp; /* DHCP data in payload. */
65 u8_t pad[2]; /* buf[] must start at 2 mod 4. */
66 /* Payload: */
67 u8_t buf[ETH_MAX_PACK_SIZE];
68 } buf_t;
70 #define BUF_ETH_SIZE (ETH_MAX_PACK_SIZE)
71 #define BUF_IP_SIZE (BUF_ETH_SIZE - sizeof(eth_hdr_t))
72 #define BUF_UDP_SIZE (BUF_IP_SIZE - sizeof(ip_hdr_t) - sizeof(udp_hdr_t) \
73 + sizeof(udp_io_hdr_t))
75 /* Type of network device open: Ethernet, ICMP, BOOTP client, BOOTP server. */
76 typedef enum { FT_CLOSED, FT_ETHERNET, FT_ICMP, FT_BOOTPC, FT_BOOTPS } fdtype_t;
78 #define FT_ALL FT_CLOSED /* To close all open descriptors at once. */
80 typedef struct fd { /* An open descriptor. */
81 i8_t fd; /* Open descriptor. */
82 u8_t fdtype; /* Type of network open. */
83 char device[sizeof("/dev/eth###")]; /* Device name. */
84 u8_t n; /* Network that owns it. */
85 buf_t *bp; /* Associated packet buffer. */
86 time_t since; /* Open since when? */
87 } fd_t;
89 /* Network state: Any IP device, Ethernet in sink mode, True Ethernet. */
90 typedef enum { NT_IP, NT_SINK, NT_ETHERNET } nettype_t;
92 typedef struct network { /* Information on a network. */
93 u8_t n; /* Network number. */
94 ether_addr_t eth; /* Ethernet address of this net. */
95 u8_t type; /* What kind of net is this? */
96 i8_t sol_ct; /* Router solicitation count. */
97 ether_addr_t conflict; /* Address conflict with this one. */
98 unsigned flags; /* Various flags. */
99 fd_t *fdp; /* Current open device. */
100 struct network *wait; /* Wait for a resource list. */
101 ipaddr_t ip; /* IP address of this net. */
102 ipaddr_t mask; /* Associated netmask. */
103 ipaddr_t gateway; /* My router. */
104 ipaddr_t server; /* My DHCP server. */
105 const char *hostname; /* Optional hostname to query for. */
106 time_t start; /* Query or lease start time. */
107 time_t delta; /* Query again after delta seconds. */
108 time_t renew; /* Next query or go into renewal. */
109 time_t rebind; /* When to go into rebind. */
110 time_t lease; /* When our lease expires. */
111 time_t solicit; /* Time to do a router solicitation. */
112 } network_t;
114 /* Flags. */
115 #define NF_NEGOTIATING 0x001 /* Negotiating with a DHCP server. */
116 #define NF_BOUND 0x002 /* Address configured through DHCP. */
117 #define NF_SERVING 0x004 /* I'm a server on this network. */
118 #define NF_RELAYING 0x008 /* I'm relaying for this network. */
119 #define NF_WAIT 0x010 /* Wait for a resource to free up. */
120 #define NF_IRDP 0x020 /* IRDP is used on this net. */
121 #define NF_CONFLICT 0x040 /* There is an address conflict. */
122 #define NF_POSSESSIVE 0x080 /* Keep address if lease expires. */
123 #define NF_INFORM 0x100 /* It's ok to answer DHCPINFORM. */
125 /* Functions defined in dhcpd.c. */
126 void report(const char *label);
127 void fatal(const char *label);
128 void *allocate(size_t size);
129 int ifname2if(const char *name);
130 network_t *if2net(int n);
132 /* Devices.c */
133 void get_buf(buf_t **bp);
134 void put_buf(buf_t **bp);
135 void give_buf(buf_t **dbp, buf_t **sbp);
136 network_t *newnetwork(void);
137 void closefd(fd_t *fdp);
138 int opendev(network_t *np, fdtype_t fdtype, int compete);
139 void closedev(network_t *np, fdtype_t fdtype);
140 char *ipdev(int n);
141 void set_ipconf(char *device, ipaddr_t ip, ipaddr_t mask, unsigned mtu);
143 /* Ether.c */
144 void udp2ether(buf_t *bp, network_t *np);
145 int ether2udp(buf_t *bp);
146 void make_arp(buf_t *bp, network_t *np);
147 int is_arp_me(buf_t *bp, network_t *np);
148 void icmp_solicit(buf_t *bp);
149 void icmp_advert(buf_t *bp, network_t *np);
150 ipaddr_t icmp_is_advert(buf_t *bp);
152 /* Tags.c */
153 #define gettag(dp, st, pd, pl) dhcp_gettag((dp), (st), (pd), (pl))
154 void settag(dhcp_t *dp, int tag, void *data, size_t len);
155 char *cidr_ntoa(ipaddr_t addr, ipaddr_t mask);
156 void ether2clid(u8_t *clid, ether_addr_t *eth);
157 void initdhcpconf(void);
158 int makedhcp(dhcp_t *dp, u8_t *class, size_t calen, u8_t *client, size_t cilen,
159 ipaddr_t ip, ipaddr_t ifip, network_t *np);
160 char *dhcptypename(int type);
161 void printdhcp(dhcp_t *dp);