Fix some more memory issues
[project/odhcpd.git] / src / dhcpv4.h
1 /**
2  *   Copyright (C) 2012 Steven Barth <steven@midlink.org>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU General Public License version 2
6  *   as published by the Free Software Foundation.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License version 2 for more details.
12  *
13  */
14 #pragma once
15
16 #define DHCPV4_CLIENT_PORT 68
17 #define DHCPV4_SERVER_PORT 67
18
19 #define DHCPV4_FLAG_BROADCAST  0x8000
20
21 enum dhcpv4_op {
22         DHCPV4_BOOTREQUEST = 1,
23         DHCPV4_BOOTREPLY = 2
24 };
25
26 enum dhcpv4_msg {
27         DHCPV4_MSG_DISCOVER = 1,
28         DHCPV4_MSG_OFFER = 2,
29         DHCPV4_MSG_REQUEST = 3,
30         DHCPV4_MSG_DECLINE = 4,
31         DHCPV4_MSG_ACK = 5,
32         DHCPV4_MSG_NAK = 6,
33         DHCPV4_MSG_RELEASE = 7,
34         DHCPV4_MSG_INFORM = 8,
35 };
36
37 enum dhcpv4_opt {
38         DHCPV4_OPT_NETMASK = 1,
39         DHCPV4_OPT_ROUTER = 3,
40         DHCPV4_OPT_DNSSERVER = 6,
41         DHCPV4_OPT_DOMAIN = 15,
42         DHCPV4_OPT_MTU = 26,
43         DHCPV4_OPT_BROADCAST = 28,
44         DHCPV4_OPT_NTPSERVER = 42,
45         DHCPV4_OPT_LEASETIME = 51,
46         DHCPV4_OPT_MESSAGE = 53,
47         DHCPV4_OPT_SERVERID = 54,
48         DHCPV4_OPT_RENEW = 58,
49         DHCPV4_OPT_REBIND = 59,
50         DHCPV4_OPT_IPADDRESS = 50,
51         DHCPV4_OPT_HOSTNAME = 10,
52         DHCPV4_OPT_REQUEST = 17,
53         DHCPV4_OPT_END = 255,
54 };
55
56 struct dhcpv4_message {
57         uint8_t op;
58         uint8_t htype;
59         uint8_t hlen;
60         uint8_t hops;
61         uint32_t xid;
62         uint16_t secs;
63         uint16_t flags;
64         struct in_addr ciaddr;
65         struct in_addr yiaddr;
66         struct in_addr siaddr;
67         struct in_addr giaddr;
68         uint8_t chaddr[16];
69         char sname[64];
70         char file[128];
71         uint8_t options[312];
72 };
73
74 struct dhcpv4_assignment {
75         struct list_head head;
76         uint32_t addr;
77         time_t valid_until;
78         uint8_t hwaddr[6];
79         char hostname[];
80 };
81
82 struct dhcpv4_option {
83         uint8_t type;
84         uint8_t len;
85         uint8_t data[];
86 };
87
88
89 #define dhcpv4_for_each_option(start, end, opt)\
90         for (opt = (struct dhcpv4_option*)(start); \
91                 &opt[1] <= (struct dhcpv4_option*)(end) && \
92                         &opt->data[opt->len] <= (end); \
93                 opt = (struct dhcpv4_option*)&opt->data[opt->len])