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