1 /* Networking DHCPv4 client */
2 
3 /*
4  * Copyright (c) 2017 ARM Ltd.
5  * Copyright (c) 2016 Intel Corporation.
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_REGISTER(net_dhcpv4_client_sample, LOG_LEVEL_DBG);
12 
13 #include <zephyr/kernel.h>
14 #include <zephyr/linker/sections.h>
15 #include <errno.h>
16 #include <stdio.h>
17 
18 #include <zephyr/net/net_if.h>
19 #include <zephyr/net/net_core.h>
20 #include <zephyr/net/net_context.h>
21 #include <zephyr/net/net_mgmt.h>
22 
23 #define DHCP_OPTION_NTP (42)
24 
25 static uint8_t ntp_server[4];
26 
27 static struct net_mgmt_event_callback mgmt_cb;
28 
29 static struct net_dhcpv4_option_callback dhcp_cb;
30 
start_dhcpv4_client(struct net_if * iface,void * user_data)31 static void start_dhcpv4_client(struct net_if *iface, void *user_data)
32 {
33 	ARG_UNUSED(user_data);
34 
35 	LOG_INF("Start on %s: index=%d", net_if_get_device(iface)->name,
36 		net_if_get_by_iface(iface));
37 	net_dhcpv4_start(iface);
38 }
39 
handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)40 static void handler(struct net_mgmt_event_callback *cb,
41 		    uint32_t mgmt_event,
42 		    struct net_if *iface)
43 {
44 	int i = 0;
45 
46 	if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
47 		return;
48 	}
49 
50 	for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
51 		char buf[NET_IPV4_ADDR_LEN];
52 
53 		if (iface->config.ip.ipv4->unicast[i].ipv4.addr_type !=
54 							NET_ADDR_DHCP) {
55 			continue;
56 		}
57 
58 		LOG_INF("   Address[%d]: %s", net_if_get_by_iface(iface),
59 			net_addr_ntop(AF_INET,
60 			    &iface->config.ip.ipv4->unicast[i].ipv4.address.in_addr,
61 						  buf, sizeof(buf)));
62 		LOG_INF("    Subnet[%d]: %s", net_if_get_by_iface(iface),
63 			net_addr_ntop(AF_INET,
64 				       &iface->config.ip.ipv4->unicast[i].netmask,
65 				       buf, sizeof(buf)));
66 		LOG_INF("    Router[%d]: %s", net_if_get_by_iface(iface),
67 			net_addr_ntop(AF_INET,
68 						 &iface->config.ip.ipv4->gw,
69 						 buf, sizeof(buf)));
70 		LOG_INF("Lease time[%d]: %u seconds", net_if_get_by_iface(iface),
71 			iface->config.dhcpv4.lease_time);
72 	}
73 }
74 
option_handler(struct net_dhcpv4_option_callback * cb,size_t length,enum net_dhcpv4_msg_type msg_type,struct net_if * iface)75 static void option_handler(struct net_dhcpv4_option_callback *cb,
76 			   size_t length,
77 			   enum net_dhcpv4_msg_type msg_type,
78 			   struct net_if *iface)
79 {
80 	char buf[NET_IPV4_ADDR_LEN];
81 
82 	LOG_INF("DHCP Option %d: %s", cb->option,
83 		net_addr_ntop(AF_INET, cb->data, buf, sizeof(buf)));
84 }
85 
main(void)86 int main(void)
87 {
88 	LOG_INF("Run dhcpv4 client");
89 
90 	net_mgmt_init_event_callback(&mgmt_cb, handler,
91 				     NET_EVENT_IPV4_ADDR_ADD);
92 	net_mgmt_add_event_callback(&mgmt_cb);
93 
94 	net_dhcpv4_init_option_callback(&dhcp_cb, option_handler,
95 					DHCP_OPTION_NTP, ntp_server,
96 					sizeof(ntp_server));
97 
98 	net_dhcpv4_add_option_callback(&dhcp_cb);
99 
100 	net_if_foreach(start_dhcpv4_client, NULL);
101 	return 0;
102 }
103