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#include "ip64/ip64.h"
36#include "ip64/ip64-eth.h"
37#include "ipv6/ip64-addr.h"
38
39/*---------------------------------------------------------------------------*/
40
41#include "sys/log.h"
42#define LOG_MODULE "IP64"
43#define LOG_LEVEL LOG_LEVEL_IP64
44
45/*---------------------------------------------------------------------------*/
46
47PROCESS(ip64_ipv4_dhcp_process, "IPv4 DHCP");
48
49uip_ipaddr_t uip_hostaddr; /* Needed because it is referenced by dhcpc.c */
50
51
52/*---------------------------------------------------------------------------*/
53void
54ip64_ipv4_dhcp_init(void)
55{
56 LOG_INFO("Starting DHCPv4\n");
57 process_start(&ip64_ipv4_dhcp_process, NULL);
58}
59/*---------------------------------------------------------------------------*/
60PROCESS_THREAD(ip64_ipv4_dhcp_process, ev, data)
61{
63
64 ip64_dhcpc_init(&ip64_eth_addr, sizeof(ip64_eth_addr));
65
66 LOG_INFO("IPv4 DHCP Inited\n");
67
68 ip64_dhcpc_request();
69 LOG_DBG("IPv4 DHCP Requested\n");
70 while(1) {
72
73 if(ev == tcpip_event ||
74 ev == PROCESS_EVENT_TIMER) {
75 ip64_dhcpc_appcall(ev, data);
76 }
77 }
78
80}
81/*---------------------------------------------------------------------------*/
82void
83ip64_dhcpc_configured(const struct ip64_dhcpc_state *s)
84{
85 uip_ip6addr_t ip6dnsaddr;
86 LOG_INFO("DHCP Configured with %d.%d.%d.%d\n",
87 s->ipaddr.u8[0], s->ipaddr.u8[1],
88 s->ipaddr.u8[2], s->ipaddr.u8[3]);
89
90 ip64_set_hostaddr((uip_ip4addr_t *)&s->ipaddr);
91 ip64_set_netmask((uip_ip4addr_t *)&s->netmask);
92 ip64_set_draddr((uip_ip4addr_t *)&s->default_router);
93 if(!uip_ip4addr_cmp((uip_ip4addr_t *)&s->dnsaddr, &uip_all_zeroes_addr)) {
94 /* Note: Currently we assume only one DNS server */
95 uip_ipaddr_t * dns = uip_nameserver_get(0);
96 /* Only update DNS entry if it is empty or already IPv4 */
97 if(uip_is_addr_unspecified(dns) || ip64_addr_is_ip64(dns)) {
98 ip64_addr_4to6((uip_ip4addr_t *)&s->dnsaddr, &ip6dnsaddr);
99 uip_nameserver_update(&ip6dnsaddr, uip_ntohs(s->lease_time[0])*65536ul + uip_ntohs(s->lease_time[1]));
100 }
101 }
102}
103/*---------------------------------------------------------------------------*/
104void
105ip64_dhcpc_unconfigured(const struct ip64_dhcpc_state *s)
106{
107}
108/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
#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
#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
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
process_event_t tcpip_event
The uIP event.
Definition: tcpip.c:62
#define uip_is_addr_unspecified(a)
Is IPv6 address a the unspecified address a is of type uip_ipaddr_t.
Definition: uip.h:1734
void uip_nameserver_update(const uip_ipaddr_t *nameserver, uint32_t lifetime)
Initialize the module variables.
uip_ipaddr_t * uip_nameserver_get(uint8_t num)
Get a Nameserver ip address given in RA.
#define uip_ip4addr_cmp(addr1, addr2)
Compare two IP addresses.
Definition: uip.h:998
Header file for the logging system.
The Ethernet address.
Definition: ip64-eth.h:40
Representation of an IP address.
Definition: uip.h:95