README.rst
1.. zephyr:code-sample:: sockets-echo-client
2 :name: Echo client (advanced)
3 :relevant-api: bsd_sockets tls_credentials
4
5 Implement a client that sends IP packets, waits for data to be sent back, and verifies it.
6
7Overview
8********
9
10The echo-client sample application for Zephyr implements a UDP/TCP client
11that will send IPv4 or IPv6 packets, wait for the data to be sent back,
12and then verify it matches the data that was sent.
13
14The source code for this sample application can be found at:
15:zephyr_file:`samples/net/sockets/echo_client`.
16
17Requirements
18************
19
20- :ref:`networking_with_host`
21
22Building and Running
23********************
24
25There are multiple ways to use this application. One of the most common
26usage scenario is to run echo-client application inside QEMU. This is
27described in :ref:`networking_with_qemu`.
28
29There are configuration files for different boards and setups in the
30echo-client directory:
31
32- :file:`prj.conf`
33 Generic config file, normally you should use this.
34
35- :file:`overlay-ot.conf`
36 This overlay config enables support for OpenThread.
37
38- :file:`overlay-802154.conf`
39 This overlay config enables support for native IEEE 802.15.4 connectivity.
40 Note, that by default IEEE 802.15.4 L2 uses unacknowledged communication. To
41 improve connection reliability, acknowledgments can be enabled with shell
42 command: ``ieee802154 ack set``.
43
44- :file:`overlay-qemu_802154.conf`
45 This overlay config enables support for two QEMU's when simulating
46 IEEE 802.15.4 network that are connected together.
47
48- :file:`overlay-tls.conf`
49 This overlay config enables support for TLS.
50
51- :file:`overlay-vlan.conf`
52 This overlay config enables support for Virtual LAN.
53 See :ref:`networking_samples_common` for details.
54
55Build echo-client sample application like this:
56
57.. zephyr-app-commands::
58 :zephyr-app: samples/net/sockets/echo_client
59 :board: <board to use>
60 :conf: <config file to use>
61 :goals: build
62 :compact:
63
64Example building for the nrf52840dk/nrf52840 with OpenThread support:
65
66.. zephyr-app-commands::
67 :zephyr-app: samples/net/sockets/echo_client
68 :host-os: unix
69 :board: nrf52840dk/nrf52840
70 :conf: "prj.conf overlay-ot.conf"
71 :goals: run
72 :compact:
73
74Example building for the IEEE 802.15.4 RF2XX transceiver:
75
76.. zephyr-app-commands::
77 :zephyr-app: samples/net/sockets/echo_client
78 :host-os: unix
79 :board: [samr21_xpro | sam4s_xplained | sam_v71_xult/samv71q21]
80 :gen-args: -DEXTRA_CONF_FILE=overlay-802154.conf
81 :goals: build flash
82 :compact:
83
84In a terminal window you can check if communication is happen:
85
86.. code-block:: console
87
88 $ minicom -D /dev/ttyACM1
89
90
91
92Enabling TLS support
93====================
94
95Enable TLS support in the sample by building the project with the
96``overlay-tls.conf`` overlay file enabled, for example, using these commands:
97
98.. zephyr-app-commands::
99 :zephyr-app: samples/net/sockets/echo_client
100 :board: qemu_x86
101 :conf: "prj.conf overlay-tls.conf"
102 :goals: build
103 :compact:
104
105An alternative way is to specify ``-DEXTRA_CONF_FILE=overlay-tls.conf`` when
106running ``west build`` or ``cmake``.
107
108The certificate and private key used by the sample can be found in the sample's
109``src`` directory. The default certificates used by Socket Echo Client and
110:zephyr:code-sample:`sockets-echo-server` enable establishing a secure connection
111between the samples.
112
113SOCKS5 proxy support
114====================
115
116It is also possible to connect to the echo-server through a SOCKS5 proxy.
117To enable it, use ``-DEXTRA_CONF_FILE=overlay-socks5.conf`` when running ``west
118build`` or ``cmake``.
119
120By default, to make the testing easier, the proxy is expected to run on the
121same host as the echo-server in Linux host.
122
123To start a proxy server, for example a builtin SOCKS server support in ssh
124can be used (-D option). Use the following command to run it on your host
125with the default port:
126
127For IPv4 proxy server:
128
129.. code-block:: console
130
131 $ ssh -N -D 0.0.0.0:1080 localhost
132
133For IPv6 proxy server:
134
135.. code-block:: console
136
137 $ ssh -N -D [::]:1080 localhost
138
139Run both commands if you are testing IPv4 and IPv6.
140
141To connect to a proxy server that is not running under the same IP as the
142echo-server or uses a different port number, modify the following values
143in echo_client/src/tcp.c.
144
145.. code-block:: c
146
147 #define SOCKS5_PROXY_V4_ADDR IPV4_ADDR
148 #define SOCKS5_PROXY_V6_ADDR IPV6_ADDR
149 #define SOCKS5_PROXY_PORT 1080
150
151Running echo-server in Linux Host
152=================================
153
154There is one useful testing scenario that can be used with Linux host.
155Here echo-client is run in QEMU and echo-server is run in Linux host.
156
157To use QEMU for testing, follow the :ref:`networking_with_qemu` guide.
158
159In a terminal window:
160
161.. code-block:: console
162
163 $ sudo ./echo-server -i tap0
164
165Run echo-client application in QEMU:
166
167.. zephyr-app-commands::
168 :zephyr-app: samples/net/sockets/echo_client
169 :host-os: unix
170 :board: qemu_x86
171 :conf: "prj.conf overlay-linux.conf"
172 :goals: run
173 :compact:
174
175Note that echo-server must be running in the Linux host terminal window
176before you start the echo-client application in QEMU.
177Exit QEMU by pressing :kbd:`CTRL+A` :kbd:`x`.
178
179You can verify TLS communication with a Linux host as well. See
180https://github.com/zephyrproject-rtos/net-tools documentation for information
181on how to test TLS with Linux host samples.
182
183See the :zephyr:code-sample:`sockets-echo-server` documentation for an alternate
184way of running, with the echo-client on the Linux host and the echo-server
185in QEMU.
186
187OpenThread RCP+Zephyr HOST (SPINEL connection via UART)
188=======================================================
189
190Prerequisites:
191--------------
192
193- Build ``echo-server`` for HOST PC (x86_64)
194 (https://github.com/zephyrproject-rtos/net-tools) SHA1:1c4fdba
195
196.. code-block:: console
197
198 $ make echo-server
199
200- Program nRF RCP from Nordic nrf SDK (v2.7.0):
201
202.. code-block:: console
203
204 (v2.7.0) ~/ncs$ west build -p always -b nrf21540dk/nrf52840 -S logging nrf/samples/openthread/coprocessor
205
206
207- Build mimxrt1020_evk HOST (Zephyr):
208
209.. zephyr-app-commands::
210 :zephyr-app: samples/net/sockets/echo_client
211 :board: mimxrt1020_evk
212 :conf: "prj.conf overlay-ot-rcp-host-uart.conf"
213 :goals: build
214 :compact:
215
216And flash
217
218.. code-block:: console
219
220 $ west flash -r pyocd -i 0226000047784e4500439004d9170013e56100009796990
221
222
223- Connect the nRF RCP with IMXRT1020 (HOST) via UART
224
225.. code-block:: c
226
227 /*
228 * imxrt1020_evk -> HOST
229 * nRF21540-DK -> RCP (nrf/samples/openthread/coprocessor)
230 * LPUART2 used for communication:
231 * nRF21540 (P6) P0.08 RXD -> IMXRT1020-EVK (J17) D1 (GPIO B1 08) (TXD)
232 * nRF21540 (P6) P0.07 CTS -> IMXRT1020-EVK (J19) D8 (GPIO B1 07) (RTS)
233 * nRF21540 (P6) P0.06 TXD -> IMXRT1020-EVK (J17) D0 (GPIO B1 09) (RXD)
234 * nRF21540 (P6) P0.05 RTS -> IMXRT1020-EVK (J17) D7 (GPIO B1 06) (CTS)
235 */
236
237
238- Install the OTBR (OpenThread Border Router) docker container on your HOST PC (x86_64)
239 Follow steps from https://docs.nordicsemi.com/bundle/ncs-2.5.1/page/nrf/protocols/thread/tools.html#running_otbr_using_docker
240
241**Most notable ones:**
242
243 1. Create ``otbr0`` network bridge to have access to OT network from HOST
244 Linux PC
245
246 .. code-block:: console
247
248 sudo docker network create --ipv6 --subnet fd11:db8:1::/64 -o com.docker.network.bridge.name=otbr0 otbr
249
250
251 2. Pull docker container for OTBR:
252
253 .. code-block:: console
254
255 docker pull nrfconnect/otbr:84c6aff
256
257
258 3. Start the docker image:
259
260 .. code-block:: console
261
262 sudo modprobe ip6table_filter
263 sudo docker run -it --rm --privileged --name otbr --network otbr -p 8080:80 --sysctl "net.ipv6.conf.all.disable_ipv6=0 net.ipv4.conf.all.forwarding=1 net.ipv6.conf.all.forwarding=1" --volume /dev/ttyACM5:/dev/radio nrfconnect/otbr:84c6aff --radio-url spinel+hdlc+uart:///dev/radio?uart-baudrate=1000000
264
265
266 4. Add proper routing (``fd11:22::/64`` are the IPv6 addresses - On-Mesh - which allow accessing the OT devices) on HOST PC (x86_64)
267
268 .. code-block:: console
269
270 sudo ip -6 route add fd11:22::/64 dev otbr0 via fd11:db8:1::2
271
272
273 And the output for on-OT address:
274
275 .. code-block:: console
276
277 ip route get fd11:22:0:0:5188:1678:d0c0:6893
278 fd11:22::5188:1678:d0c0:6893 from :: via fd11:db8:1::2 dev otbr0 src fd11:db8:1::1 metric 1024 pref medium
279
280
281 5. Start the console to the docker image:
282
283 .. code-block:: console
284
285 sudo docker exec -it otbr /bin/bash
286
287
288 Test with e.g.
289
290 .. code-block:: console
291
292 ot-ctl router table
293 ot-ctl ipaddr
294
295
296
297Configure OTBR
298--------------
299
300On the HOST PC's webbrowser: http://localhost:8080/
301
302Go to ``Form`` and leave default values - e.g:
303
304 * Network Key: ``00112233445566778899aabbccddeeff``
305 * On-Mesh Prefix: ``fd11:22::``
306 * Channel: ``15``
307
308
309to "FORM" the OT network.
310
311*Note:*
312The "On-Mesh Prefix" shall match the one setup in ``otbr0`` routing.
313
314
315Configure RCP (nRF21540-DK) + OT HOST (mimxrt1020)
316--------------------------------------------------
317
318.. code-block:: console
319
320 ot factoryreset
321 ot dataset networkkey 00112233445566778899aabbccddeeff
322 ot ifconfig up
323
324
325In the HOST PC www webpage interface please:
326Commission -> Joiner PSKd* set to ``J01NME`` -> START COMMISSION
327
328.. code-block:: console
329
330 ot joiner start J01NME
331 ot thread start
332
333
334The ``ot ipaddr`` shall show IPv6 address starting from ``fd11:22:0:0:``.
335This one can be accessed from HOST's PC network (via e.g.
336``ping -6 fd11:22:0:0:e8bf:266b:63ca:eff4``).
337
338Start ``echo-server`` on HOST PC (x86-64)
339-----------------------------------------
340
341.. code-block:: console
342
343 ./echo-server -i otbr0
344