README.rst
1.. _dhcpv4-client-sample:
2
3Sample DHCPv4 client application
4################################
5
6Overview
7********
8
9This application starts a DHCPv4 client, gets an IPv4 address from the
10DHCPv4 server, and prints address, lease time, netmask and router
11information to a serial console.
12
13Requirements
14************
15
16- :ref:`networking_with_host`
17
18Building and Running
19********************
20
21Running DHCPv4 client in Linux Host
22===================================
23
24These are instructions for how to use this sample application using
25QEMU on a Linux host to negotiate IP address from DHCPv4 server running
26on Linux host.
27
28To use QEMU for testing, follow the :ref:`networking_with_qemu` guide.
29
30Here's a sample server configuration file '/etc/dhcp/dhcpd.conf'
31used to configure the DHCPv4 server:
32
33.. code-block:: console
34
35 log-facility local7;
36 default-lease-time 600;
37 max-lease-time 7200;
38
39 subnet 192.0.2.0 netmask 255.255.255.0 {
40 range 192.0.2.10 192.0.2.100;
41 }
42
43Use another terminal window to start up a DHCPv4 server on the Linux host,
44using this conf file:
45
46.. code-block:: console
47
48 $ sudo dhcpd -d -4 -cf /etc/dhcp/dhcpd.conf -lf /var/lib/dhcp/dhcpd.leases tap0
49
50Run Zephyr samples/net/dhcpv4_client application in QEMU:
51
52.. zephyr-app-commands::
53 :zephyr-app: samples/net/dhcpv4_client
54 :host-os: unix
55 :board: qemu_x86
56 :goals: run
57 :compact:
58
59Once DHCPv4 client address negotiation completed with server, details
60are shown like this:
61
62.. code-block:: console
63
64 [dhcpv4] [INF] main: In main
65 [dhcpv4] [INF] main_thread: Run dhcpv4 client
66 [dhcpv4] [INF] handler: Your address: 192.0.2.10
67 [dhcpv4] [INF] handler: Lease time: 600
68 [dhcpv4] [INF] handler: Subnet: 255.255.255.0
69 [dhcpv4] [INF] handler: Router: 0.0.0.0
70
71To verify the Zephyr application client is running and has received
72an ip address by typing:
73
74.. code-block:: console
75
76 $ ping -I tap0 192.0.2.10
77
78
79FRDM_K64F
80=========
81
82These are instructions for how to use this sample application running on
83:ref:`frdm_k64f` board to negotiate IP address from DHCPv4 server running on
84Linux host.
85
86Connect ethernet cable from :ref:`Freedom-K64F board <frdm_k64f>` to Linux host
87machine and check for new interfaces:
88
89.. code-block:: console
90
91 $ ifconfig
92
93Add ip address and routing information to interface:
94
95.. code-block:: console
96
97 $ sudo ip addr add 192.0.2.2 dev eth1
98 $ sudo ip route add 192.0.2.0/24 dev eth1
99
100Here's a sample server configuration file '/etc/dhcpd/dhcp.conf'
101used to configure the DHCPv4 server:
102
103.. code-block:: console
104
105 log-facility local7;
106 default-lease-time 600;
107 max-lease-time 7200;
108
109 subnet 192.0.2.0 netmask 255.255.255.0 {
110 range 192.0.2.10 192.0.2.100;
111 }
112
113Use another terminal window to start up a DHCPv4 server on the Linux host,
114using this conf file:
115
116.. code-block:: console
117
118 $ sudo dhcpd -d -4 -cf /etc/dhcp/dhcpd.conf -lf /var/lib/dhcp/dhcpd.leases eth1
119
120Build Zephyr samples/net/dhcpv4_client application:
121
122.. zephyr-app-commands::
123 :zephyr-app: samples/net/dhcpv4_client
124 :host-os: unix
125 :board: frdm_k64f
126 :goals: build flash
127 :compact:
128
129Once DHCPv4 client address negotiation completed with server, details
130are shown like this:
131
132.. code-block:: console
133
134 $ sudo screen /dev/ttyACM0 115200
135 [dhcpv4] [INF] main: In main
136 [dhcpv4] [INF] main_thread: Run dhcpv4 client
137 [dhcpv4] [INF] handler: Your address: 192.0.2.10
138 [dhcpv4] [INF] handler: Lease time: 600
139 [dhcpv4] [INF] handler: Subnet: 255.255.255.0
140 [dhcpv4] [INF] handler: Router: 0.0.0.0
141
142To verify the Zephyr application client is running and has received
143an ip address by typing:
144
145.. code-block:: console
146
147 $ ping -I eth1 192.0.2.10
148