diff options
| author | jubeormk1 <[email protected]> | 2025-05-22 15:41:43 +1000 |
|---|---|---|
| committer | jubeormk1 <[email protected]> | 2025-05-22 15:41:43 +1000 |
| commit | 43ff562b5a0327d575d2c02f299ad6d15680384e (patch) | |
| tree | c079f6e7e9395353286ee9ff19a9c6965b173792 /examples | |
| parent | 9409afb02e7fd46d0c3b2257a938a8bc88244f5b (diff) | |
Adjustments for std examples
I extended the README.md file to extend instructions for the rest of network examples
I modified the tap.sh script to give ownership to the user running it and avoiding running the examples with sudo. This would help someone using a debuger.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/std/README.md | 115 | ||||
| -rw-r--r-- | examples/std/tap.sh | 2 |
2 files changed, 112 insertions, 5 deletions
diff --git a/examples/std/README.md b/examples/std/README.md index dcc152fc2..5d7c384ae 100644 --- a/examples/std/README.md +++ b/examples/std/README.md | |||
| @@ -1,19 +1,126 @@ | |||
| 1 | 1 | ||
| 2 | ## Running the `embassy-net` examples | 2 | ## Running the `embassy-net` examples |
| 3 | 3 | ||
| 4 | First, create the tap99 interface. (The number was chosen to | 4 | To run `net`, `tcp_accept`, `net_udp` examples you will need a tap interface. Before running any example, create the tap99 interface. (The number was chosen to |
| 5 | hopefully not collide with anything.) You only need to do | 5 | hopefully not collide with anything.) You only need to do |
| 6 | this once. | 6 | this once every time you reboot your computer. |
| 7 | 7 | ||
| 8 | ```sh | 8 | ```sh |
| 9 | sudo sh tap.sh | 9 | sudo sh tap.sh |
| 10 | ``` | 10 | ``` |
| 11 | 11 | ||
| 12 | Second, have something listening there. For example `nc -lp 8000` | 12 | ### `net` example |
| 13 | |||
| 14 | For this example, you need to have something listening in the correct port. For example `nc -lp 8000`. | ||
| 13 | 15 | ||
| 14 | Then run the example located in the `examples` folder: | 16 | Then run the example located in the `examples` folder: |
| 15 | 17 | ||
| 16 | ```sh | 18 | ```sh |
| 17 | cd $EMBASSY_ROOT/examples/std/ | 19 | cd $EMBASSY_ROOT/examples/std/ |
| 18 | sudo cargo run --bin net -- --tap tap99 --static-ip | 20 | cargo run --bin net -- --tap tap99 --static-ip |
| 21 | ``` | ||
| 22 | ### `tcp_accept` example | ||
| 23 | |||
| 24 | This example listen for a tcp connection. | ||
| 25 | |||
| 26 | First run the example located in the `examples` folder: | ||
| 27 | |||
| 28 | ```sh | ||
| 29 | cd $EMBASSY_ROOT/examples/std/ | ||
| 30 | cargo run --bin tcp_accept -- --tap tap99 --static-ip | ||
| 31 | ``` | ||
| 32 | |||
| 33 | Then open a connection to the port. For example `nc 192.168.69.2 9999`. | ||
| 34 | |||
| 35 | ### `net_udp` example | ||
| 36 | |||
| 37 | This example listen for a udp connection. | ||
| 38 | |||
| 39 | First run the example located in the `examples` folder: | ||
| 40 | |||
| 41 | ```sh | ||
| 42 | cd $EMBASSY_ROOT/examples/std/ | ||
| 43 | cargo run --bin net_udp -- --tap tap99 --static-ip | ||
| 44 | ``` | ||
| 45 | |||
| 46 | Then open a connection to the port. For example `nc -u 192.168.69.2 9400`. | ||
| 47 | |||
| 48 | ### `net_dns` example | ||
| 49 | |||
| 50 | This example queries a `DNS` for the IP address of `www.example.com`. | ||
| 51 | |||
| 52 | In order to achieve this, the `tap99` interface requires configuring tap99 as a gateway device temporarily. | ||
| 53 | |||
| 54 | For example, in Ubuntu you can do this by: | ||
| 55 | |||
| 56 | 1. Identifying your default route device. In the next example `eth0` | ||
| 57 | |||
| 58 | ```sh | ||
| 59 | ip r | grep "default" | ||
| 60 | default via 192.168.2.1 dev eth0 proto kernel metric 35 | ||
| 61 | ``` | ||
| 62 | |||
| 63 | 2. Enabling temporarily IP Forwarding: | ||
| 64 | |||
| 65 | ```sh | ||
| 66 | sudo sysctl -w net.ipv4.ip_forward=1 | ||
| 67 | ``` | ||
| 68 | |||
| 69 | 3. Configuring NAT to mascarade traffic from `tap99` to `eth0` | ||
| 70 | |||
| 71 | ```sh | ||
| 72 | sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE | ||
| 73 | sudo iptables -A FORWARD -i tap99 -j ACCEPT | ||
| 74 | sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT | ||
| 75 | ``` | ||
| 76 | |||
| 77 | 4. Then you can run the example located in the `examples` folder: | ||
| 78 | |||
| 79 | ```sh | ||
| 80 | cd $EMBASSY_ROOT/examples/std/ | ||
| 81 | cargo run --bin net_dns -- --tap tap99 --static-ip | ||
| 82 | ``` | ||
| 83 | |||
| 84 | ### `net_ppp` example | ||
| 85 | |||
| 86 | This example establish a Point-to-Point Protocol (PPP) connection that can be used, for example, for connecting to internet through a 4G modem via a serial channel. | ||
| 87 | |||
| 88 | The example creates a PPP bridge over a virtual serial channel between `pty1` and `pty2` for the example code and a PPP server running on the same computer. | ||
| 89 | |||
| 90 | To run this example you will need: | ||
| 91 | - ppp (pppd server) | ||
| 92 | - socat (socket CAT) | ||
| 93 | |||
| 94 | To run the examples you may follow the next steps: | ||
| 95 | |||
| 96 | 1. Save the PPP server configuration: | ||
| 97 | ```sh | ||
| 98 | sudo sh -c 'echo "myuser $(hostname) mypass 192.168.7.10" >> /etc/ppp/pap-secrets' | ||
| 99 | ``` | ||
| 100 | |||
| 101 | 2. Create a files `pty1` and `pty2` and link them | ||
| 102 | ```sh | ||
| 103 | cd $EMBASSY_ROOT/examples/std/ | ||
| 104 | socat -v -x PTY,link=pty1,rawer PTY,link=pty2,rawer | ||
| 105 | ``` | ||
| 106 | |||
| 107 | 3. open a second terminal and start the PPP server: | ||
| 108 | ```sh | ||
| 109 | cd $EMBASSY_ROOT/examples/std/ | ||
| 110 | sudo pppd $PWD/pty1 115200 192.168.7.1: ms-dns 8.8.4.4 ms-dns 8.8.8.8 nodetach debug local persist silent | ||
| 111 | ``` | ||
| 112 | |||
| 113 | 4. Open a third terminal and run the example | ||
| 114 | ```sh | ||
| 115 | cd $EMBASSY_ROOT/examples/std/ | ||
| 116 | RUST_LOG=trace cargo run --bin net_ppp -- --device pty2 | ||
| 117 | ``` | ||
| 118 | 5. Observe the output in the second and third terminal | ||
| 119 | 6. Open one last terminal to interact with `net_ppp` example through the PPP connection | ||
| 120 | ```sh | ||
| 121 | # ping the net_ppp client | ||
| 122 | ping 192.168.7.10 | ||
| 123 | # open an tcp connection | ||
| 124 | nc 192.168.7.10 1234 | ||
| 125 | # Type anything and observe the output in the different terminals | ||
| 19 | ``` | 126 | ``` |
diff --git a/examples/std/tap.sh b/examples/std/tap.sh index 39d92a099..fb89d2381 100644 --- a/examples/std/tap.sh +++ b/examples/std/tap.sh | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | ip tuntap add name tap99 mode tap user $USER | 1 | ip tuntap add name tap99 mode tap user $SUDO_USER |
| 2 | ip link set tap99 up | 2 | ip link set tap99 up |
| 3 | ip addr add 192.168.69.100/24 dev tap99 | 3 | ip addr add 192.168.69.100/24 dev tap99 |
| 4 | ip -6 addr add fe80::100/64 dev tap99 | 4 | ip -6 addr add fe80::100/64 dev tap99 |
