37#include "contiki-net.h"
39#include "ip64/ip64-dhcpc.h"
40#include "ipv6/ip64-addr.h"
45#define LOG_MODULE "IP64"
46#define LOG_LEVEL LOG_LEVEL_IP64
50#define STATE_INITIAL 0
51#define STATE_SENDING 1
52#define STATE_OFFER_RECEIVED 2
53#define STATE_CONFIG_RECEIVED 3
55static struct ip64_dhcpc_state s;
58 uint8_t op, htype, hlen, hops;
71#if (UIP_BUFSIZE - UIP_UDPIP_HLEN) < 548
72#error UIP_CONF_BUFFER_SIZE may be too small to accomodate DHCPv4 packets
73#error Increase UIP_CONF_BUFFER_SIZE in your project-conf.h, or contiki-conf.h
74#error A good size is 600 bytes
77#define BOOTP_BROADCAST 0x8000
81#define DHCP_HTYPE_ETHERNET 1
82#define DHCP_HLEN_ETHERNET 6
83#define DHCP_MSG_LEN 236
85#define IP64_DHCPC_SERVER_PORT 67
86#define IP64_DHCPC_CLIENT_PORT 68
96#define DHCP_OPTION_SUBNET_MASK 1
97#define DHCP_OPTION_ROUTER 3
98#define DHCP_OPTION_DNS_SERVER 6
99#define DHCP_OPTION_REQ_IPADDR 50
100#define DHCP_OPTION_LEASE_TIME 51
101#define DHCP_OPTION_MSG_TYPE 53
102#define DHCP_OPTION_SERVER_ID 54
103#define DHCP_OPTION_REQ_LIST 55
104#define DHCP_OPTION_END 255
107static const uint8_t magic_cookie[4] = {99, 130, 83, 99};
110add_msg_type(uint8_t *optptr, uint8_t type)
112 *optptr++ = DHCP_OPTION_MSG_TYPE;
119add_server_id(uint8_t *optptr)
121 *optptr++ = DHCP_OPTION_SERVER_ID;
123 memcpy(optptr, s.serverid, 4);
128add_req_ipaddr(uint8_t *optptr)
130 *optptr++ = DHCP_OPTION_REQ_IPADDR;
132 memcpy(optptr, s.ipaddr.u16, 4);
137add_req_options(uint8_t *optptr)
139 *optptr++ = DHCP_OPTION_REQ_LIST;
141 *optptr++ = DHCP_OPTION_SUBNET_MASK;
142 *optptr++ = DHCP_OPTION_ROUTER;
143 *optptr++ = DHCP_OPTION_DNS_SERVER;
148add_end(uint8_t *optptr)
150 *optptr++ = DHCP_OPTION_END;
155create_msg(
struct dhcp_msg *m)
157 m->op = DHCP_REQUEST;
158 m->htype = DHCP_HTYPE_ETHERNET;
161 memcpy(m->xid, &xid,
sizeof(m->xid));
165 memcpy(m->ciaddr, uip_hostaddr.u16,
sizeof(m->ciaddr));
166 memset(m->yiaddr, 0,
sizeof(m->yiaddr));
167 memset(m->siaddr, 0,
sizeof(m->siaddr));
168 memset(m->giaddr, 0,
sizeof(m->giaddr));
169 memcpy(m->chaddr, s.mac_addr, s.mac_len);
170 memset(&m->chaddr[s.mac_len], 0,
sizeof(m->chaddr) - s.mac_len);
172 memset(m->sname, 0,
sizeof(m->sname));
173 memset(m->file, 0,
sizeof(m->file));
176 memcpy(m->options, magic_cookie,
sizeof(magic_cookie));
183 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
187 end = add_msg_type(&m->options[4], DHCPDISCOVER);
188 end = add_req_options(end);
198 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
202 end = add_msg_type(&m->options[4], DHCPREQUEST);
203 end = add_server_id(end);
204 end = add_req_ipaddr(end);
211parse_options(uint8_t *optptr,
int len)
213 uint8_t *end = optptr + len;
216 while(optptr < end) {
218 case DHCP_OPTION_SUBNET_MASK:
219 memcpy(s.netmask.u16, optptr + 2, 4);
221 case DHCP_OPTION_ROUTER:
222 memcpy(s.default_router.u16, optptr + 2, 4);
224 case DHCP_OPTION_DNS_SERVER:
225 memcpy(s.dnsaddr.u16, optptr + 2, 4);
227 case DHCP_OPTION_MSG_TYPE:
228 type = *(optptr + 2);
230 case DHCP_OPTION_SERVER_ID:
231 memcpy(s.serverid, optptr + 2, 4);
233 case DHCP_OPTION_LEASE_TIME:
234 memcpy(s.lease_time, optptr + 2, 4);
236 case DHCP_OPTION_END:
240 optptr += optptr[1] + 2;
248 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
250 if(m->op == DHCP_REPLY &&
251 memcmp(m->xid, &xid,
sizeof(xid)) == 0 &&
252 memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) {
253 memcpy(s.ipaddr.u16, m->yiaddr, 4);
254 return parse_options(&m->options[4],
uip_datalen());
265 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
266 uint8_t *optptr = &m->options[4];
269 if(m->op == DHCP_REPLY &&
270 memcmp(m->xid, &xid,
sizeof(xid)) == 0 &&
271 memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) {
272 while(optptr < end) {
273 if(*optptr == DHCP_OPTION_MSG_TYPE) {
274 return *(optptr + 2);
275 }
else if (*optptr == DHCP_OPTION_END) {
278 optptr += optptr[1] + 2;
285PT_THREAD(handle_dhcp(process_event_t ev,
void *data))
293 s.state = STATE_SENDING;
306 s.state = STATE_OFFER_RECEIVED;
329 s.state = STATE_CONFIG_RECEIVED;
339 }
while(s.state != STATE_CONFIG_RECEIVED);
345 LOG_DBG(
"Got default router %d.%d.%d.%d\n",
347 LOG_DBG(
"Lease expires in %ld seconds\n",
348 uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]));
350 ip64_dhcpc_configured(&s);
352#define MAX_TICKS (~((clock_time_t)0) / 2)
353#define MAX_TICKS32 (~((uint32_t)0))
354#define IMIN(a, b) ((a) < (b) ? (a) : (b))
356 if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*
CLOCK_SECOND/2
358 s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])
361 s.ticks = MAX_TICKS32;
365 ticks = IMIN(s.ticks, MAX_TICKS);
371 if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*
CLOCK_SECOND/2
373 s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])
376 s.ticks = MAX_TICKS32;
386 ticks = IMIN(s.ticks / 2, MAX_TICKS);
401 ip64_dhcpc_unconfigured(&s);
408ip64_dhcpc_init(
const void *mac_addr,
int mac_len)
412 uip_ip6addr_t v6addr;
416 s.mac_addr = mac_addr;
419 s.state = STATE_INITIAL;
421 ip64_addr_4to6(&v4addr, &v6addr);
434ip64_dhcpc_appcall(process_event_t ev,
void *data)
436 if(ev ==
tcpip_event || ev == PROCESS_EVENT_TIMER) {
437 handle_dhcp(ev, data);
442ip64_dhcpc_request(
void)
446 if(s.state == STATE_INITIAL) {
449 handle_dhcp(PROCESS_EVENT_NONE, NULL);
#define CLOCK_SECOND
A second, measured in system clock time.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define PT_YIELD(pt)
Yield from the current protothread.
#define PT_THREAD(name_args)
Declaration of a protothread.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
#define PT_END(pt)
Declare the end of a protothread.
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define PT_INIT(pt)
Initialize a protothread.
process_event_t tcpip_event
The uIP event.
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
void tcpip_poll_udp(struct uip_udp_conn *conn)
Cause a specified UDP connection to be polled.
void * uip_appdata
Pointer to the application data in the packet buffer.
void uip_send(const void *data, int len)
Send data on the current connection.
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer.
#define uip_newdata()
Is new incoming data available?
#define uip_sethostaddr(addr)
Set the IP address of this host.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
Header file for the logging system.
Representation of a uIP UDP connection.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Representation of an IP address.