]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/include/lwip/dhcp.h
0314171ad28536e1d3866920d286ad3d56b110d2
[pes-rpp/rpp-lwip.git] / src / include / lwip / dhcp.h
1 /** @file
2  */
3
4 #ifndef __LWIP_DHCP_H__
5 #define __LWIP_DHCP_H__
6
7 #include "lwip/opt.h"
8 #include "lwip/netif.h"
9 #include "lwip/udp.h"
10
11 /** period (in seconds) of the application calling dhcp_coarse_tmr() */
12 #define DHCP_COARSE_TIMER_SECS 60 
13 /** period (in milliseconds) of the application calling dhcp_fine_tmr() */
14 #define DHCP_FINE_TIMER_MSECS 500 
15
16 struct dhcp
17 {
18   /** current DHCP state machine state */
19   u8_t state;
20   /** retries of current request */
21   u8_t tries;
22   /** transaction identifier of last sent request */ 
23   u32_t xid;
24   /** our connection to the DHCP server */ 
25   struct udp_pcb *pcb;
26   /** (first) pbuf of incoming msg */
27   struct pbuf *p;
28   /** incoming msg */
29   struct dhcp_msg *msg_in;
30   /** incoming msg options */
31   struct dhcp_msg *options_in; 
32   /** ingoing msg options length */
33   u16_t options_in_len;
34
35   struct pbuf *p_out; /* pbuf of outcoming msg */
36   struct dhcp_msg *msg_out; /* outgoing msg */
37   u16_t options_out_len; /* outgoing msg options length */
38   u16_t request_timeout; /* #ticks with period DHCP_FINE_TIMER_SECS for request timeout */
39   u16_t t1_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for renewal time */
40   u16_t t2_timeout;  /* #ticks with period DHCP_COARSE_TIMER_SECS for rebind time */
41   struct ip_addr server_ip_addr; /* dhcp server address that offered this lease */
42   struct ip_addr offered_ip_addr;
43   struct ip_addr offered_sn_mask;
44   struct ip_addr offered_gw_addr;
45   struct ip_addr offered_bc_addr;
46   u32_t offered_t0_lease; /* lease period (in seconds) */
47   u32_t offered_t1_renew; /* recommended renew time (usually 50% of lease period) */
48   u32_t offered_t2_rebind; /* recommended rebind time (usually 66% of lease period)  */
49 /** Patch #1308
50  *  TODO: See dhcp.c "TODO"s
51  */
52 #if 0
53   struct ip_addr offered_si_addr;
54   u8_t *boot_file_name;
55 #endif
56 };
57
58 /** minimum set of fields of any DHCP message */
59 struct dhcp_msg
60 {
61   u8_t op;
62   u8_t htype;
63   u8_t hlen;
64   u8_t hops;
65   u32_t xid;
66   u16_t secs;
67   u16_t flags;
68   struct ip_addr ciaddr;
69   struct ip_addr yiaddr;
70   struct ip_addr siaddr;
71   struct ip_addr giaddr;
72 #define DHCP_CHADDR_LEN 16U
73   u8_t chaddr[DHCP_CHADDR_LEN];
74 #define DHCP_SNAME_LEN 64U
75   u8_t sname[DHCP_SNAME_LEN];
76 #define DHCP_FILE_LEN 128U
77   u8_t file[DHCP_FILE_LEN];
78   u32_t cookie;
79 #define DHCP_MIN_OPTIONS_LEN 68U
80 /** allow this to be configured in lwipopts.h, but not too small */
81 #if ((!defined(DHCP_OPTIONS_LEN)) || (DHCP_OPTIONS_LEN < DHCP_MIN_OPTIONS_LEN))
82 /** set this to be sufficient for your options in outgoing DHCP msgs */
83 #  define DHCP_OPTIONS_LEN DHCP_MIN_OPTIONS_LEN
84 #endif
85   u8_t options[DHCP_OPTIONS_LEN];
86 };
87
88 /** start DHCP configuration */
89 err_t dhcp_start(struct netif *netif);
90 /** stop DHCP configuration */
91 void dhcp_stop(struct netif *netif);
92 /** enforce lease renewal */
93 err_t dhcp_renew(struct netif *netif);
94 /** inform server of our IP address */
95 void dhcp_inform(struct netif *netif);
96
97 /** if enabled, check whether the offered IP address is not in use, using ARP */
98 #if DHCP_DOES_ARP_CHECK
99 void dhcp_arp_reply(struct netif *netif, struct ip_addr *addr);
100 #endif
101
102 /** to be called every minute */
103 void dhcp_coarse_tmr(void);
104 /** to be called every half second */
105 void dhcp_fine_tmr(void);
106  
107 /** DHCP message item offsets and length */
108 #define DHCP_MSG_OFS (UDP_DATA_OFS)  
109   #define DHCP_OP_OFS (DHCP_MSG_OFS + 0)
110   #define DHCP_HTYPE_OFS (DHCP_MSG_OFS + 1)
111   #define DHCP_HLEN_OFS (DHCP_MSG_OFS + 2)
112   #define DHCP_HOPS_OFS (DHCP_MSG_OFS + 3)
113   #define DHCP_XID_OFS (DHCP_MSG_OFS + 4)
114   #define DHCP_SECS_OFS (DHCP_MSG_OFS + 8)
115   #define DHCP_FLAGS_OFS (DHCP_MSG_OFS + 10)
116   #define DHCP_CIADDR_OFS (DHCP_MSG_OFS + 12)
117   #define DHCP_YIADDR_OFS (DHCP_MSG_OFS + 16)
118   #define DHCP_SIADDR_OFS (DHCP_MSG_OFS + 20)
119   #define DHCP_GIADDR_OFS (DHCP_MSG_OFS + 24)
120   #define DHCP_CHADDR_OFS (DHCP_MSG_OFS + 28)
121   #define DHCP_SNAME_OFS (DHCP_MSG_OFS + 44)
122   #define DHCP_FILE_OFS (DHCP_MSG_OFS + 108)
123 #define DHCP_MSG_LEN 236
124
125 #define DHCP_COOKIE_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN)
126 #define DHCP_OPTIONS_OFS (DHCP_MSG_OFS + DHCP_MSG_LEN + 4)
127
128 #define DHCP_CLIENT_PORT 68  
129 #define DHCP_SERVER_PORT 67
130
131 /** DHCP client states */
132 #define DHCP_REQUESTING 1
133 #define DHCP_INIT 2
134 #define DHCP_REBOOTING 3
135 #define DHCP_REBINDING 4
136 #define DHCP_RENEWING 5
137 #define DHCP_SELECTING 6
138 #define DHCP_INFORMING 7
139 #define DHCP_CHECKING 8
140 #define DHCP_PERMANENT 9
141 #define DHCP_BOUND 10
142 /** not yet implemented #define DHCP_RELEASING 11 */
143 #define DHCP_BACKING_OFF 12
144 #define DHCP_OFF 13
145  
146 #define DHCP_BOOTREQUEST 1
147 #define DHCP_BOOTREPLY 2
148
149 #define DHCP_DISCOVER 1
150 #define DHCP_OFFER 2
151 #define DHCP_REQUEST 3
152 #define DHCP_DECLINE 4
153 #define DHCP_ACK 5
154 #define DHCP_NAK 6
155 #define DHCP_RELEASE 7
156 #define DHCP_INFORM 8
157
158 #define DHCP_HTYPE_ETH 1
159
160 #define DHCP_HLEN_ETH 6
161
162 #define DHCP_BROADCAST_FLAG 15
163 #define DHCP_BROADCAST_MASK (1 << DHCP_FLAG_BROADCAST)
164
165 /** BootP options */
166 #define DHCP_OPTION_PAD 0
167 #define DHCP_OPTION_SUBNET_MASK 1 /* RFC 2132 3.3 */
168 #define DHCP_OPTION_ROUTER 3 
169 #define DHCP_OPTION_HOSTNAME 12
170 #define DHCP_OPTION_IP_TTL 23
171 #define DHCP_OPTION_MTU 26
172 #define DHCP_OPTION_BROADCAST 28
173 #define DHCP_OPTION_TCP_TTL 37
174 #define DHCP_OPTION_END 255
175
176 /** DHCP options */
177 #define DHCP_OPTION_REQUESTED_IP 50 /* RFC 2132 9.1, requested IP address */
178 #define DHCP_OPTION_LEASE_TIME 51 /* RFC 2132 9.2, time in seconds, in 4 bytes */
179 #define DHCP_OPTION_OVERLOAD 52 /* RFC2132 9.3, use file and/or sname field for options */
180
181 #define DHCP_OPTION_MESSAGE_TYPE 53 /* RFC 2132 9.6, important for DHCP */
182 #define DHCP_OPTION_MESSAGE_TYPE_LEN 1
183
184
185 #define DHCP_OPTION_SERVER_ID 54 /* RFC 2132 9.7, server IP address */
186 #define DHCP_OPTION_PARAMETER_REQUEST_LIST 55 /* RFC 2132 9.8, requested option types */
187
188 #define DHCP_OPTION_MAX_MSG_SIZE 57 /* RFC 2132 9.10, message size accepted >= 576 */
189 #define DHCP_OPTION_MAX_MSG_SIZE_LEN 2
190
191 #define DHCP_OPTION_T1 58 /* T1 renewal time */
192 #define DHCP_OPTION_T2 59 /* T2 rebinding time */
193 #define DHCP_OPTION_CLIENT_ID 61
194 #define DHCP_OPTION_TFTP_SERVERNAME 66
195 #define DHCP_OPTION_BOOTFILE 67
196
197 /** possible combinations of overloading the file and sname fields with options */
198 #define DHCP_OVERLOAD_NONE 0
199 #define DHCP_OVERLOAD_FILE 1
200 #define DHCP_OVERLOAD_SNAME  2
201 #define DHCP_OVERLOAD_SNAME_FILE 3
202
203 #endif /*__LWIP_DHCP_H__*/