Contiki-NG
ip64-ipv4-dhcp.c
1 /*
2  * Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 #include "contiki.h"
33 #include "contiki-net.h"
34 #include "ip64/ip64-dhcpc.h"
35 
36 #include "ip64/ip64.h"
37 #include "ip64/ip64-eth.h"
38 #include "ipv6/ip64-addr.h"
39 
40 #include <stdio.h>
41 
42 #define DEBUG DEBUG_NONE
43 #include "net/ipv6/uip-debug.h"
44 
45 PROCESS(ip64_ipv4_dhcp_process, "IPv4 DHCP");
46 
47 uip_ipaddr_t uip_hostaddr; /* Needed because it is referenced by dhcpc.c */
48 
49 
50 /*---------------------------------------------------------------------------*/
51 void
52 ip64_ipv4_dhcp_init(void)
53 {
54  printf("IP64: Starting DHCPv4\n");
55  process_start(&ip64_ipv4_dhcp_process, NULL);
56 }
57 /*---------------------------------------------------------------------------*/
58 PROCESS_THREAD(ip64_ipv4_dhcp_process, ev, data)
59 {
60  PROCESS_BEGIN();
61 
62  ip64_dhcpc_init(&ip64_eth_addr, sizeof(ip64_eth_addr));
63 
64  PRINTF("IP64: Inited\n");
65 
66  ip64_dhcpc_request();
67  PRINTF("IP64: Requested\n");
68  while(1) {
70 
71  if(ev == tcpip_event ||
72  ev == PROCESS_EVENT_TIMER) {
73  ip64_dhcpc_appcall(ev, data);
74  }
75  }
76 
77  PROCESS_END();
78 }
79 /*---------------------------------------------------------------------------*/
80 void
81 ip64_dhcpc_configured(const struct ip64_dhcpc_state *s)
82 {
83  uip_ip6addr_t ip6dnsaddr;
84  PRINTF("IP64: DHCP Configured with %d.%d.%d.%d\n",
85  s->ipaddr.u8[0], s->ipaddr.u8[1],
86  s->ipaddr.u8[2], s->ipaddr.u8[3]);
87 
88  ip64_set_hostaddr((uip_ip4addr_t *)&s->ipaddr);
89  ip64_set_netmask((uip_ip4addr_t *)&s->netmask);
90  ip64_set_draddr((uip_ip4addr_t *)&s->default_router);
91  if(!uip_ip4addr_cmp((uip_ip4addr_t *)&s->dnsaddr, &uip_all_zeroes_addr)) {
92  /* Note: Currently we assume only one DNS server */
93  uip_ipaddr_t * dns = uip_nameserver_get(0);
94  /* Only update DNS entry if it is empty or already IPv4 */
95  if(uip_is_addr_unspecified(dns) || ip64_addr_is_ip64(dns)) {
96  ip64_addr_4to6((uip_ip4addr_t *)&s->dnsaddr, &ip6dnsaddr);
97  uip_nameserver_update(&ip6dnsaddr, uip_ntohs(s->lease_time[0])*65536ul + uip_ntohs(s->lease_time[1]));
98  }
99  }
100 }
101 /*---------------------------------------------------------------------------*/
102 void
103 ip64_dhcpc_unconfigured(const struct ip64_dhcpc_state *s)
104 {
105 }
106 /*---------------------------------------------------------------------------*/
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
Definition: uip.h:1044
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
The Ethernet address.
Definition: ip64-eth.h:40
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:62
A set of debugging macros for the IP stack
Representation of an IP address.
Definition: uip.h:95
void uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime)
Initialize the module variables.
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition: uip.h:1836
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1097
uip_ipaddr_t * uip_nameserver_get(uint8_t num)
Get a Nameserver ip address given in RA.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99