38 #include "contiki-net.h" 39 #include "ip64/ip64-dhcpc.h" 41 #include "ipv6/ip64-addr.h" 43 #define STATE_INITIAL 0 44 #define STATE_SENDING 1 45 #define STATE_OFFER_RECEIVED 2 46 #define STATE_CONFIG_RECEIVED 3 48 static struct ip64_dhcpc_state s;
51 uint8_t op, htype, hlen, hops;
64 #if (UIP_BUFSIZE - UIP_LLH_LEN - UIP_UDPIP_HLEN) < 548 65 #error UIP_CONF_BUFFER_SIZE may be too small to accomodate DHCPv4 packets 66 #error Increase UIP_CONF_BUFFER_SIZE in your project-conf.h, or contiki-conf.h 67 #error A good size is 600 bytes 70 #define BOOTP_BROADCAST 0x8000 72 #define DHCP_REQUEST 1 74 #define DHCP_HTYPE_ETHERNET 1 75 #define DHCP_HLEN_ETHERNET 6 76 #define DHCP_MSG_LEN 236 78 #define IP64_DHCPC_SERVER_PORT 67 79 #define IP64_DHCPC_CLIENT_PORT 68 81 #define DHCPDISCOVER 1 89 #define DHCP_OPTION_SUBNET_MASK 1 90 #define DHCP_OPTION_ROUTER 3 91 #define DHCP_OPTION_DNS_SERVER 6 92 #define DHCP_OPTION_REQ_IPADDR 50 93 #define DHCP_OPTION_LEASE_TIME 51 94 #define DHCP_OPTION_MSG_TYPE 53 95 #define DHCP_OPTION_SERVER_ID 54 96 #define DHCP_OPTION_REQ_LIST 55 97 #define DHCP_OPTION_END 255 100 static const uint8_t magic_cookie[4] = {99, 130, 83, 99};
103 add_msg_type(uint8_t *optptr, uint8_t type)
105 *optptr++ = DHCP_OPTION_MSG_TYPE;
112 add_server_id(uint8_t *optptr)
114 *optptr++ = DHCP_OPTION_SERVER_ID;
116 memcpy(optptr, s.serverid, 4);
121 add_req_ipaddr(uint8_t *optptr)
123 *optptr++ = DHCP_OPTION_REQ_IPADDR;
125 memcpy(optptr, s.ipaddr.u16, 4);
130 add_req_options(uint8_t *optptr)
132 *optptr++ = DHCP_OPTION_REQ_LIST;
134 *optptr++ = DHCP_OPTION_SUBNET_MASK;
135 *optptr++ = DHCP_OPTION_ROUTER;
136 *optptr++ = DHCP_OPTION_DNS_SERVER;
141 add_end(uint8_t *optptr)
143 *optptr++ = DHCP_OPTION_END;
150 m->op = DHCP_REQUEST;
151 m->htype = DHCP_HTYPE_ETHERNET;
154 memcpy(m->xid, &xid,
sizeof(m->xid));
158 memcpy(m->ciaddr, uip_hostaddr.u16,
sizeof(m->ciaddr));
159 memset(m->yiaddr, 0,
sizeof(m->yiaddr));
160 memset(m->siaddr, 0,
sizeof(m->siaddr));
161 memset(m->giaddr, 0,
sizeof(m->giaddr));
162 memcpy(m->chaddr, s.mac_addr, s.mac_len);
163 memset(&m->chaddr[s.mac_len], 0,
sizeof(m->chaddr) - s.mac_len);
165 memset(m->sname, 0,
sizeof(m->sname));
166 memset(m->file, 0,
sizeof(m->file));
169 memcpy(m->options, magic_cookie,
sizeof(magic_cookie));
176 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
180 end = add_msg_type(&m->options[4], DHCPDISCOVER);
181 end = add_req_options(end);
191 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
195 end = add_msg_type(&m->options[4], DHCPREQUEST);
196 end = add_server_id(end);
197 end = add_req_ipaddr(end);
204 parse_options(uint8_t *optptr,
int len)
206 uint8_t *end = optptr + len;
209 while(optptr < end) {
211 case DHCP_OPTION_SUBNET_MASK:
212 memcpy(s.netmask.u16, optptr + 2, 4);
214 case DHCP_OPTION_ROUTER:
215 memcpy(s.default_router.u16, optptr + 2, 4);
217 case DHCP_OPTION_DNS_SERVER:
218 memcpy(s.dnsaddr.u16, optptr + 2, 4);
220 case DHCP_OPTION_MSG_TYPE:
221 type = *(optptr + 2);
223 case DHCP_OPTION_SERVER_ID:
224 memcpy(s.serverid, optptr + 2, 4);
226 case DHCP_OPTION_LEASE_TIME:
227 memcpy(s.lease_time, optptr + 2, 4);
229 case DHCP_OPTION_END:
233 optptr += optptr[1] + 2;
241 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
243 if(m->op == DHCP_REPLY &&
244 memcmp(m->xid, &xid,
sizeof(xid)) == 0 &&
245 memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) {
246 memcpy(s.ipaddr.u16, m->yiaddr, 4);
247 return parse_options(&m->options[4],
uip_datalen());
258 struct dhcp_msg *m = (
struct dhcp_msg *)
uip_appdata;
259 uint8_t *optptr = &m->options[4];
262 if(m->op == DHCP_REPLY &&
263 memcmp(m->xid, &xid,
sizeof(xid)) == 0 &&
264 memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) {
265 while(optptr < end) {
266 if(*optptr == DHCP_OPTION_MSG_TYPE) {
267 return *(optptr + 2);
268 }
else if (*optptr == DHCP_OPTION_END) {
271 optptr += optptr[1] + 2;
278 PT_THREAD(handle_dhcp(process_event_t ev,
void *data))
286 s.state = STATE_SENDING;
299 s.state = STATE_OFFER_RECEIVED;
322 s.state = STATE_CONFIG_RECEIVED;
332 }
while(s.state != STATE_CONFIG_RECEIVED);
339 printf(
"Got default router %d.%d.%d.%d\n",
341 printf(
"Lease expires in %ld seconds\n",
342 uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]));
345 ip64_dhcpc_configured(&s);
347 #define MAX_TICKS (~((clock_time_t)0) / 2) 348 #define MAX_TICKS32 (~((uint32_t)0)) 349 #define IMIN(a, b) ((a) < (b) ? (a) : (b)) 351 if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*
CLOCK_SECOND/2
353 s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])
356 s.ticks = MAX_TICKS32;
360 ticks = IMIN(s.ticks, MAX_TICKS);
366 if((uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1]))*
CLOCK_SECOND/2
368 s.ticks = (uip_ntohs(s.lease_time[0])*65536ul + uip_ntohs(s.lease_time[1])
371 s.ticks = MAX_TICKS32;
381 ticks = IMIN(s.ticks / 2, MAX_TICKS);
396 ip64_dhcpc_unconfigured(&s);
403 ip64_dhcpc_init(
const void *mac_addr,
int mac_len)
407 uip_ip6addr_t v6addr;
411 s.mac_addr = mac_addr;
414 s.state = STATE_INITIAL;
416 ip64_addr_4to6(&v4addr, &v6addr);
429 ip64_dhcpc_appcall(process_event_t ev,
void *data)
431 if(ev ==
tcpip_event || ev == PROCESS_EVENT_TIMER) {
432 handle_dhcp(ev, data);
437 ip64_dhcpc_request(
void)
441 if(s.state == STATE_INITIAL) {
444 handle_dhcp(PROCESS_EVENT_NONE, NULL);
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
void tcpip_poll_udp(struct uip_udp_conn *conn)
Cause a specified UDP connection to be polled.
process_event_t tcpip_event
The uIP event.
#define uip_ipaddr_to_quad(a)
Convert an IP address to four bytes separated by commas.
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Representation of an IP address.
#define CC_REGISTER_ARG
Configure if the C compiler supports the "register" keyword for function arguments.
#define uip_ipaddr(addr, addr0, addr1, addr2, addr3)
Construct an IP address from four bytes.
#define PT_YIELD_UNTIL(pt, cond)
Yield from the protothread until a condition occurs.
#define PT_INIT(pt)
Initialize a protothread.
#define CLOCK_SECOND
A second, measured in system clock time.
#define PT_END(pt)
Declare the end of a protothread.
#define uip_newdata()
Is new incoming data available?
struct uip_udp_conn * udp_new(const uip_ipaddr_t *ripaddr, uint16_t port, void *appstate)
Create a new UDP connection.
#define PT_YIELD(pt)
Yield from the current protothread.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
#define PT_THREAD(name_args)
Declaration of a protothread.
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
void uip_send(const void *data, int len)
Send data on the current connection.
#define uip_sethostaddr(addr)
Set the IP address of this host.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
#define udp_bind(conn, port)
Bind a UDP connection to a local port.
void * uip_appdata
Pointer to the application data in the packet buffer.
#define uip_datalen()
The length of any incoming data that is currently available (if available) in the uip_appdata buffer...
Representation of a uIP UDP connection.