Code cleanup, file restrucuturing, minor bug fixed. Xbnet service in stable stae

This commit is contained in:
Emran Billah 2024-08-29 12:25:32 -03:00
parent 35eaa56008
commit 317e4dfaff
10 changed files with 88 additions and 109 deletions

View File

@ -11,6 +11,8 @@ RUN apt-get update && apt-get install -y \
traceroute \ traceroute \
nmap \ nmap \
tcpdump \ tcpdump \
util-linux \
bsdmainutils \
vim \ vim \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*

View File

@ -35,9 +35,12 @@ services-xbee_net/
│ ├── entrypoint.sh # Entrypoint script for Docker container │ ├── entrypoint.sh # Entrypoint script for Docker container
│ ├── get_connected_wifi_info.sh # Script to fetch connected Wi-Fi device information │ ├── get_connected_wifi_info.sh # Script to fetch connected Wi-Fi device information
│ ├── health_check.sh # Script to perform health checks on the container │ ├── health_check.sh # Script to perform health checks on the container
│ ├── setup_end_device.sh # Script to set up the XBee end device | ├── debug/ # Directory for testing-related scripts and files
│ ├── setup_host_device.sh # Script to set up the XBee host device │ | ├── create_tap_device.sh # Create a tap device directly on an OS
│ ├── test.sh # Script to run tests │ | ├── destroy_tap_device.sh # Destroy a tap device directly on an OS
│ | ├── enable_host_macvlan.sh # Destroy a tap device directly on an OS
│ | ├── host_setup.sh # Script to set up Masquerade bridge docker0 network with active wifi
│ | ├── test.sh # Script to run tests
├── tests/ # Directory for testing-related scripts and files ├── tests/ # Directory for testing-related scripts and files
├── xbnet/ # The xbnet source code directory ├── xbnet/ # The xbnet source code directory
├── .env # Environment variables for the Docker setup ├── .env # Environment variables for the Docker setup
@ -46,7 +49,7 @@ services-xbee_net/
├── docker-compose.yml # Main Docker Compose file ├── docker-compose.yml # Main Docker Compose file
├── Dockerfile # Dockerfile to build the Docker image ├── Dockerfile # Dockerfile to build the Docker image
├── README.md # Project documentation ├── README.md # Project documentation
└── supervisord.conf # Supervisor configuration file └── run-single.sh # Script to set up env vars and start a docker service with a single xbnet node
``` ```
## Prerequisites ## Prerequisites
@ -64,47 +67,48 @@ cd services-xbee_net
#### 2. Run a single xbnet node using the following cmd: #### 2. Run a single xbnet node using the following cmd:
```bash - Router mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router`
- sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=router - Gateway mode: `sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway`
- sh ./run-single.sh --subnet=7.7.7 --serial-speed=230400 --port=/dev/ttyUSB0 --interface-type=gateway
```
#### 3. OR Manually build and Start the Docker Container
##### NOTE: This will require the config `.env` to be manually modified, based on user needs.
--- ---
- Configure a single xbnet net service using:
##### OPTIONAL: Config `.env` (NOTE: run-single.sh will configure this for you, you don't need to modify this manually): ```bash
```bash # Configuration for xbnet0
# Configuration for xbnet0 XBEE_PORT=/dev/ttyUSB0
XBEE_PORT=/dev/ttyUSB0 XBEE_BAUDRATE=230400
XBEE_BAUDRATE=230400 XBEE_NET_SRC_IP=192.168.1.100 # Ensure this IP matches the network range
XBEE_NET_SRC_IP=192.168.1.100 # Ensure this IP matches the network range XBEE_NET_IFACE_NAME=xbnet0
XBEE_NET_IFACE_NAME=xbnet0
# Default Gateway # Default Gateway
DEFAULT_GATEWAY=192.168.1.1 DEFAULT_GATEWAY=192.168.1.1
``` ```
##### b. Sample `.env-run-multiple` config: - Configure multiple xbnet net services using:
```bash
# Configuration for xbnet0 (REQUIRED: minimum config to run a single xbnet node)
XBEE0_PORT=/dev/ttyUSB0 # The serial port for the XBee device
XBEE0_BAUDRATE=230400 # Baud rate for the XBee device
XBEE0_NET_SRC_IP=192.168.1.100 # Source IP for the XBee network
XBEE0_NET_IFACE_NAME=xbnet0 # Interface name for the XBee network
# Configuration for xbnet1 (OPTIONAL: only required for multi node xbnets) ```bash
XBEE1_PORT=/dev/ttyUSB1 # Configuration for xbnet0 (REQUIRED: minimum config to run a single xbnet node)
XBEE1_BAUDRATE=230400 XBEE0_PORT=/dev/ttyUSB0 # The serial port for the XBee device
XBEE1_NET_SRC_IP=192.168.1.101 XBEE0_BAUDRATE=230400 # Baud rate for the XBee device
XBEE1_NET_IFACE_NAME=xbnet1 XBEE0_NET_SRC_IP=192.168.1.100 # Source IP for the XBee network
XBEE0_NET_IFACE_NAME=xbnet0 # Interface name for the XBee network
# Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node) # Configuration for xbnet1 (OPTIONAL: only required for multi node xbnets)
DEFAULT_GATEWAY=192.168.1.1 XBEE1_PORT=/dev/ttyUSB1
``` XBEE1_BAUDRATE=230400
XBEE1_NET_SRC_IP=192.168.1.101
XBEE1_NET_IFACE_NAME=xbnet1
# Default Gateway (REQUIRED: part of minimum concifg to run a single xbnet node)
DEFAULT_GATEWAY=192.168.1.1
```
--- ---
#### 3. Build and Start the Docker Container
To build the Docker image and start the container, run: To build the Docker image and start the container, run:
```bash ```bash

View File

@ -12,6 +12,7 @@
echo "Starting entrypoint.sh" echo "Starting entrypoint.sh"
# Main loop
loop() { loop() {
while true; do while true; do
# Check if the XBee device is connected # Check if the XBee device is connected
@ -21,7 +22,6 @@ loop() {
cleanup cleanup
continue continue
fi fi
sleep 0.5 sleep 0.5
# Check if the network interface is up # Check if the network interface is up
@ -30,18 +30,15 @@ loop() {
echo "Network interface $XBEE_NET_IFACE_NAME not found. Creating TAP interface..." echo "Network interface $XBEE_NET_IFACE_NAME not found. Creating TAP interface..."
create_tap_interface create_tap_interface
fi fi
sleep 0.5 sleep 0.5
# Log messages sent and received over xbnet # Log messages sent and received over xbnet
log_xbnet_messages log_xbnet_messages
sleep 0.5 sleep 0.5
done done
} }
# Check if the XBee device is connected
# Function to check if the XBee device is connected
check_device_port() { check_device_port() {
if [ -e "$XBEE_PORT" ]; then if [ -e "$XBEE_PORT" ]; then
return 0 return 0
@ -50,7 +47,7 @@ check_device_port() {
fi fi
} }
# Function to check if the network interface is up # Check if the network interface is up
check_network_state() { check_network_state() {
if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then if ip link show $XBEE_NET_IFACE_NAME > /dev/null 2>&1; then
return 0 return 0
@ -59,7 +56,7 @@ check_network_state() {
fi fi
} }
# Function to create a TAP network interface # Create a TAP network interface
create_tap_interface() { create_tap_interface() {
# Start xbnet and run it in the background # Start xbnet and run it in the background
xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} & xbnet -d --serial-speed ${XBEE_BAUDRATE} ${XBEE_PORT} tap --iface-name ${XBEE_NET_IFACE_NAME} &
@ -85,11 +82,10 @@ create_tap_interface() {
return 0 return 0
} }
# Function to log messages sent and received over xbnet using netstat # Log incoming and outgoing messages from Xbees over xbnet
log_xbnet_messages() { log_xbnet_messages() {
echo "Monitoring messages on $XBEE_NET_IFACE_NAME..." echo "Monitoring messages on $XBEE_NET_IFACE_NAME..."
# Continuously monitor the interface for packet statistics
while true; do while true; do
RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets) RX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/rx_packets)
TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets) TX_PACKETS_BEFORE=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/statistics/tx_packets)
@ -103,16 +99,54 @@ log_xbnet_messages() {
TX_DIFF=$((TX_PACKETS_AFTER - TX_PACKETS_BEFORE)) TX_DIFF=$((TX_PACKETS_AFTER - TX_PACKETS_BEFORE))
if [[ $RX_DIFF -gt 0 ]]; then if [[ $RX_DIFF -gt 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $RX_DIFF packets received on $XBEE_NET_IFACE_NAME" for packet in $(seq 1 $RX_DIFF); do
SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address)
SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1)
# Assuming destination info is available
DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1)
DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1)
# Capture payload data (in hex format for simplicity)
PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1)
echo "******************************************************************"
echo "$(date +'%Y-%m-%d %H:%M:%S')"
echo "$packet packets received on $XBEE_NET_IFACE_NAME"
echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }"
echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }"
echo "Length: $(echo "$PAYLOAD" | wc -c) bytes"
echo "Payload: $PAYLOAD"
echo "******************************************************************"
done
fi fi
if [[ $TX_DIFF -gt 0 ]]; then if [[ $TX_DIFF -gt 0 ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $TX_DIFF packets sent on $XBEE_NET_IFACE_NAME" for packet in $(seq 1 $TX_DIFF); do
SRC_MAC=$(cat /sys/class/net/$XBEE_NET_IFACE_NAME/address)
SRC_IP=$(ip -o -4 addr show $XBEE_NET_IFACE_NAME | awk '{print $4}' | cut -d/ -f1)
# Assuming destination info is available
DST_IP=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '((\d+\.){3}\d+)' | head -n 1)
DST_MAC=$(ip neighbor show dev $XBEE_NET_IFACE_NAME | grep -oP '([a-f0-9]{2}:){5}[a-f0-9]{2}' | head -n 1)
# Capture payload data (in hex format for simplicity)
PAYLOAD=$(cat /proc/net/dev_snmp6/$XBEE_NET_IFACE_NAME | hexdump -C | head -n 1)
echo "******************************************************************"
echo "$(date +'%Y-%m-%d %H:%M:%S')"
echo "$packet packets sent on $XBEE_NET_IFACE_NAME"
echo "Src: { IP: $SRC_IP, MAC: $SRC_MAC }"
echo "Dst: { IP: $DST_IP, MAC: $DST_MAC }"
echo "Length: $(echo "$PAYLOAD" | wc -c) bytes"
echo "Payload: $PAYLOAD"
echo "******************************************************************"
done
fi fi
done done
} }
# Function to clean up resources and exit the script # Clean up resources and exit the script
cleanup() { cleanup() {
echo "Cleaning up resources..." echo "Cleaning up resources..."
pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap" pkill -f "xbnet -d --serial-speed $XBEE_BAUDRATE $XBEE_PORT tap"

View File

@ -1,16 +0,0 @@
# Configure xbnet on remote device
sudo xbnet \
/dev/ttyUSB0 \ # Port where the XBee device is connected \
tun # Create a TUN (network tunnel) interface for routing IP packets
sudo ip addr \
add 192.168.100.2/24 \ # Assign the IP address 192.168.100.2 with a subnet mask of 255.255.255.0 \
dev xbnet1 # to the xbnet1 interface
sudo ip link \
set dev xbnet1 up # Activate the xbnet1 interface
# Add default route to use host machine as gateway
sudo ip route \
add default \ # Add a default route for all outgoing traffic \
via 192.168.100.1 # Use 192.168.100.1 (the host machine) as the gateway

View File

@ -1,45 +0,0 @@
# Enable IP forwarding
sudo sysctl \
-w # Write (or set) the value of a kernel parameter
net.ipv4.ip_forward=1 # Temporarily enable IP forwarding to forward network packets from one network interface to another
# Configure NAT for the bridge
sudo iptables \
-t nat # Specify the nat table used for Network Address Translation
-A POSTROUTING # Append this rule to the POSTROUTING chain, which alters packets as they are about to leave the network interface
-o wlp0s20f3 # Apply this rule to packets leaving the wlp0s20f3 interface (Wi-Fi interface)
-j MASQUERADE # Use the MASQUERADE target to change the source IP address to the IP address of the outgoing interface, enabling multiple devices on a private network to share a single public IP address
# Configure xbnet and bridge interfaces
sudo xbnet \
/dev/ttyUSB0 \ # Port where the XBee device is connected
tun # Create a TUN (network tunnel) interface for routing IP packets
sudo ip addr \
add 192.168.100.1/24 \ # Assign the IP address 192.168.100.1 with a subnet mask of 255.255.255.0
dev xbnet0 # to the xbnet0 interface
sudo ip link \
set dev xbnet0 up # Activate the xbnet0 interface
sudo brctl \
addbr br0 # Create a new bridge interface named br0
sudo brctl \
addif br0 wlp0s20f3 # Add the Wi-Fi interface wlp0s20f3 to the bridge br0
sudo brctl \
addif br0 xbnet0 # Add the xbnet0 interface to the bridge br0
sudo ip link \
set dev br0 up # Activate the bridge interface br0
sudo ip addr \
flush dev wlp0s20f3 # Remove any IP addresses assigned to the wlp0s20f3 interface
sudo ip addr \
add 192.168.7.122/24 \ # Assign the IP address 192.168.7.122 with a subnet mask of 255.255.255.0
dev br0 # to the bridge interface br0
sudo ip link \
set dev wlp0s20f3 up # Reactivate the wlp0s20f3 interface