Skip to content

Commit

Permalink
firmware: CHAMOS & CHAMOC intercomunication
Browse files Browse the repository at this point in the history
  • Loading branch information
CW-75 committed May 13, 2022
1 parent 3649e68 commit 6113dd9
Show file tree
Hide file tree
Showing 27 changed files with 1,323 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dist/tools/chamoc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

CHAMOC.out
91 changes: 91 additions & 0 deletions dist/tools/chamoc/autoconfigure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash

SUDO=${SUDO:-sudo}
PREFIX=64
NPREFIX=::

unsupported_platform() {
echo "unsupported platform" >&2
echo "(currently supported \`uname -s\` 'Darwin' and 'Linux')" >&2
}

case "$(uname -s)" in
Darwin)
PLATFORM="OSX";;
Linux)
PLATFORM="Linux";;
*)
unsupported_platform
exit 1
;;
esac

INTERFACE_CHECK_COUNTER=5 # 5 attempts to find usb interface

find_interface() {
INTERFACE=$(ls -A /sys/bus/usb/drivers/cdc_ether/*/net/ 2>/dev/null)
INTERFACE_CHECK=$(echo -n "${INTERFACE}" | head -c1 | wc -c)
if [ "${INTERFACE_CHECK}" -eq 0 ] && [ ${INTERFACE_CHECK_COUNTER} != 0 ]; then
# We want to have multiple opportunities to find the USB interface
# as sometimes it can take a few seconds for it to enumerate after
# the device has been flashed.
sleep 1
((INTERFACE_CHECK_COUNTER=INTERFACE_CHECK_COUNTER-1))
find_interface
fi
INTERFACE=${INTERFACE%/}
}

echo "Waiting for network interface."
find_interface

add_addresses(){
case "${PLATFORM}" in
Linux)
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".forwarding=1
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".accept_ra=0
${SUDO} ip link set "${INTERFACE}" up
${SUDO} ip a a "${IPV6_GLOBAL}"/64 dev "${INTERFACE}"
${SUDO} ip route add "${NPREFIX}" via "${IPV6_GLOBAL}" dev "${INTERFACE}"
gcc -Iinclude *.c -o CHAMOC.out
echo "Start sending a nib add request"
${SUDO} ./CHAMOC.out nib add "${INTERFACE}" "${IPV6_GLOBAL}" "${PREFIX}"
;;
OSX)
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".forwarding=1
${SUDO} sysctl -w net.ipv6.conf."${INTERFACE}".accept_ra=0
${SUDO} ip link set "${INTERFACE}" up
${SUDO} ip a a "${IPV6_GLOBAL}"/64 dev "${INTERFACE}"
${SUDO} ip route add "${NPREFIX}" via "${IPV6_GLOBAL}" dev "${INTERFACE}"
gcc -Iinclude *.c -o CHAMOC.out
echo "Start sending a nib add request"
${SUDO} ./CHAMOC.out nib add "${INTERFACE}" "${IPV6_GLOBAL}" "${PREFIX}"
;;
esac
}

close_connection(){
echo "Start sending a nib delete request"
${SUDO} ./CHAMOC.out nib del "${INTERFACE}" "${IPV6_GLOBAL}" "${PREFIX}"
${SUDO} ip link set "${INTERFACE}" up
${SUDO} ip a d "${IPV6_GLOBAL}"/64 dev "${INTERFACE}"
${SUDO} ip route del "${NPREFIX}" via "${IPV6_GLOBAL}" dev "${INTERFACE}"
}

IPV6_GLOBAL=$1

if [ -z "${IPV6_GLOBAL}" ]; then
IPV6_GLOBAL="2001:db8::1"
fi

find_interface

if [ -z "${INTERFACE}" ]; then
echo "USB network interface not found"
else
add_addresses
echo "Connection started"
echo "Press any key to close connection"
read -r -n 1
close_connection
fi
90 changes: 90 additions & 0 deletions dist/tools/chamoc/chamoc_msg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2022 Mesh4all <mesh4all.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @brief CHAMOC_MSG
*
* @author eduazocar<[email protected]>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "client.h"
#include "chamoc_msg.h"

int serialize(chamoc_message_t msg, char *buffer, uint8_t len) {
uint8_t strbuff[19] = {msg.message, msg.seqno, msg.prefix};
for (int i = 0; i < 16; i++) {
strbuff[3 + i] = msg.addr.in_8[i];
}
memcpy(buffer, strbuff, len);
}

int connect_chamos(chamos_client_t client, char *buffer, uint8_t len) {
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
int tries = 0;
if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0) {
printf("configuration_failed\n");
return -1;
}

while (tries < 5) {
send_message(&client, (uint8_t *)buffer, len);
recv(client.socket, buffer, len, 0);
if (buffer[0] == MSG_ACK) {
printf("Request Confirmed\n");
break;
} else {
tries++;
if (tries < 5) {
printf("Bad Request, Trying Again\n");
} else {
printf("Time Out\n\n");
}
}
}
return 1;
}

uint8_t craft_seqno(chamos_client_t *client) { client->seqno += 1; }

int8_t send_message(chamos_client_t *client, uint8_t *msg, uint8_t len) {
if (client->socket < 0) {
return -1;
}
sendto(client->socket, msg, len, 0, (struct sockaddr *)&client->csock, sizeof(client->csock));
return 1;
}

#ifdef DEBUG_MODE
void printf_buff(char *buffer, uint8_t len) {
for (int i = 0; i < len; i++) {
if (i > 2) {
printf("%02hhx", (uint8_t)buffer[i]);
} else {
printf("%d", (uint8_t)buffer[i]);
}
}
printf("\n");
}
#endif
52 changes: 52 additions & 0 deletions dist/tools/chamoc/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2022 Mesh4all <mesh4all.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @brief CHAMOC_client
*
* @author eduazocar<[email protected]>
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "client.h"
#include "chamoc_msg.h"
#include "nib.h"

chamos_client_t new_client(char *netif) {
chamos_client_t client;
socklen_t client_len = sizeof(client.csock);
client.socket = socket(PF_INET6, SOCK_DGRAM, 0);
memset(&client.csock, '\0', sizeof(client.csock));
client.csock.sin6_family = AF_INET6;
client.csock.sin6_port = htons(CHAMOC_PORT);
if (inet_pton(AF_INET6, MULTICAST_ADDR, &client.csock.sin6_addr) < 0) {
client.socket = -1;
};
if (setsockopt(client.socket, SOL_SOCKET, SO_BINDTODEVICE, netif, client_len) < 0) {
printf("Failed binding\n");
}
if (client.socket < 0) {
printf("Wrong socket init \n");
exit(0);
}
return client;
}
102 changes: 102 additions & 0 deletions dist/tools/chamoc/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
@defgroup chamoc Chamoc
@ingroup network

## CHAMOC (Communication Handler for Addressing Management | Origin client)

The module CHAMOC It's used for client-side communication for nib routing table.
In this tools will be there an compiled program called CHAMOC.out that has a client
interface with an m4a-24g board. The client node will be sending `chamoc_message_t` data.
that contains an `[Type message]` tell to the system how process the message. (Nib_add or Nib_del),
others field are the `[sequence number], [host ip address] to add as neighborh route,
and the `[network lenght prefix] generally is /64.

### Steps to initialize the client socket

- You should has the chamos_server in your m4a-24g board (Also, could run the chamoc_server inside to the test directory)
- Run the ./start_network.sh script and assign the board global address as argument (e.g: 2001:db8::1 64)
- Run the ./autoconfig.sh script
- Wait that nib_request is accepted.

### start_network.sh

When you are running start_network.sh and add an ipv6 address like this:
```sh
./start_network.sh 2001:db8::1/64
```
you are assigning to the m4a-24g board (`usb-cdc element`) the global address to stablish the communication
in the wired interface.

### Testing communication

before to run the autoconfig.sh script you can do some pings to the m4a-24g board interface.
first follow these steps to locate, and do ping between both devices.

- Locate your interface

using net-tools
```sh
ifconfig
# Output
enx5a008df2d56b: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.199 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::98eb:d858:51a0:dcc0 prefixlen 64 scopeid 0x20<link>
inet6 2001:db8::1 prefixlen 64 scopeid 0x0<global>
ether 5a:00:8d:f2:d5:6b txqueuelen 1000 (Ethernet)
...

```

```sh
ifconfig
```

with ip address command.
```sh
ip address show
# Output
22: enx5a008df2d56b: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel s
...
valid_lft forever preferred_lft forever
inet6 2001:db8::1/64 scope global
valid_lft forever preferred_lft forever
inet6 fe80::98eb:d858:51a0:dcc0/64 scope link noprefixroute
valid_lft forever preferred_lft forever

```
@note : Observe that the interface has the wish global address (e.g: 2001:db8::1/64).

- Do ping6 to the M4a-24g address `2001:db8::2/64`
```sh
ping 2001:db8::2 -c3
# expected output
PING 2001:db8::2(2001:db8::2) 56 data bytes
64 bytes from 2001:db8::2: icmp_seq=1 ttl=64 time=0.911 ms
64 bytes from 2001:db8::2: icmp_seq=2 ttl=64 time=1.02 ms
64 bytes from 2001:db8::2: icmp_seq=3 ttl=64 time=1.01 ms
```

### autoconfigure:

Running the script you will has as result the compilation and execution to the client program CHAMOC.out
This test automatically will be start sending the nib_add and nib_del request taking as parameters.
`[Name of interface]``[ip address]``[prefix_len].

``sh
./autoconfigure.sh
Waiting for network interface.
Start sending a nib add request
Bad Request, Trying Again # Timeout time to wait a response
Request Confirmed # Case when the request was correctly sent
Start sending a nib delete request
Request Confirmed
```

@note: the chamoc intercomunnication has five attempts to wait a response from the server

### Running with chamoc_server
in the folder /test, there are an example of chamoc_server running. this will be
waiting and MSG_NIB_ADD or MSG_NIB_DEL, if these are received correctly response with a
MSG_ACK, in case that the message was wrong received, the server will send an MSG_NACK.
To start this server only has to run the script in the test_folder.
*/
Loading

0 comments on commit 6113dd9

Please sign in to comment.