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_ipv4_autoconf_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 #include "net_sample_common.h"
24 
25 static struct net_mgmt_event_callback mgmt_cb;
26 
handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)27 static void handler(struct net_mgmt_event_callback *cb,
28 		    uint32_t mgmt_event,
29 		    struct net_if *iface)
30 {
31 	int i = 0;
32 	struct net_if_config *cfg;
33 
34 	cfg = net_if_get_config(iface);
35 	if (!cfg) {
36 		return;
37 	}
38 
39 	if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
40 		return;
41 	}
42 
43 	for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
44 		char buf[NET_IPV4_ADDR_LEN];
45 
46 		if (cfg->ip.ipv4->unicast[i].ipv4.addr_type != NET_ADDR_AUTOCONF) {
47 			continue;
48 		}
49 
50 		LOG_INF("Your address: %s",
51 			net_addr_ntop(AF_INET,
52 				    &cfg->ip.ipv4->unicast[i].ipv4.address.in_addr,
53 				    buf, sizeof(buf)));
54 		LOG_INF("Your netmask: %s",
55 			net_addr_ntop(AF_INET,
56 				    &cfg->ip.ipv4->unicast[i].netmask,
57 				    buf, sizeof(buf)));
58 	}
59 }
60 
main(void)61 int main(void)
62 {
63 	LOG_INF("Run ipv4 autoconf client");
64 
65 	wait_for_network();
66 
67 	net_mgmt_init_event_callback(&mgmt_cb, handler,
68 				     NET_EVENT_IPV4_ADDR_ADD);
69 	net_mgmt_add_event_callback(&mgmt_cb);
70 	return 0;
71 }
72