First, we need to create a C program that runs on Contiki OS. This program will establish a TCP/IP connection and exchange data using Contiki’s special operating system functions.
#include "contiki.h"
#include "net/rime/rime.h"
#include "net/rpl/rpl-private.h"
#include "sys/rtimer.h"
#include "net/ip/uip.h"
#include "lib/random.h"
#include <stdio.h>
#include "decreased-rank.h"
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
#define DECREASED_RANK_PORT 4445
#define UIP_HTONS(n) (uint16_t)((((uint16_t) (n)) << 8) | (((uint16_t) (n)) >> 8))
#define PERIOD 20
#define SEND_INTERVAL (PERIOD * CLOCK_SECOND)
#include "net/ip/uip-debug.h"
/*-------------------------------------------------------------*/
PROCESS(udp_python_process, "Decreased Rank Attack Process");
AUTOSTART_PROCESSES(&udp_python_process);
/*-------------------------------------------------------------*/
void get_data_from_python_server(){
char *str;
if(uip_newdata()) {
str = uip_appdata;
str[uip_datalen()] = '\0';
printf("DATA recv get_data_from_rcu '%s'\n", str);
}
}
/*-------------------------------------------------------------
* decreased_rank_attack_simulated_init
*/
void decreased_rank_attack_simulated_init()
{
PRINTF("Decreased Rank Attack Process Started\n");
process_start(&udp_python_process, NULL);
}
/*-------------------------------------------------------------
*
*/
PROCESS_THREAD(rank_attack_process, ev, data)
{
static uint8_t ret;
static struct etimer et;
static struct uip_udp_conn *client_conn = NULL;
PROCESS_BEGIN();
/* new connection with remote host */
client_conn = udp_new(NULL, 0, NULL);
if(client_conn == NULL) {
PRINTF("No UDP connection available, exiting the process!\n");
PROCESS_EXIT();
}
udp_bind(client_conn, UIP_HTONS(DECREASED_RANK_PORT));
printf("rank_attack_process()\n");
while(1){
PROCESS_YIELD();
if(ev == tcpip_event){
get_data_from_python_server();
}
}
PROCESS_END();
}
/*-------------------------------------------------------------
*
*/
void
decreased_rank_process_init(int joined){
decreased_rank_attack_simulated_init();
}
This C program will create a server in Contiki OS and listen for incoming data on a port defined as SERVER_PORT. It will process the incoming data with the tcpip_handler function and print it to the console.
import socket
# Create ipv6 datagram socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
# Allow own messages to be sent back (for local testing)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, True)
str_ip_pool = ip_addr
find_node_to_end_node(matrix_neighbor, str(list_ip_addrs[i]))
sock.sendto("hello world from JRC", (str_ip_pool, 4445))
First, the socket
module is imported.
To create an IPv6 datagram socket, socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
is used. The AF_INET6
parameter specifies that we will be using IPv6 addressing, while the SOCK_DGRAM
parameter specifies that we will be using the UDP protocol.
To allow our own messages to be sent back (for local testing), sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, True)
is used. This enables loopback of multicast messages, allowing us to receive our own messages during local testing.
The variable str_ip_pool
is assigned an IPv6 address.
A message is sent over UDP using sock.sendto("hello world from JRC", (str_ip_pool, 4445))
. The message “hello world from JRC” is sent to the specified destination IP address (str_ip_pool
) and port number (4445).
Explanation of UIP_HTONS()
and other defined macros:
The UIP_HTONS(n)
macro is used to convert a 16-bit value to network byte order. This swaps the bytes, converting the value from host byte order to big-endian format. The expression (((uint16_t) (n)) << 8)
shifts the 16-bit value to the left by 8 bits, adding zero bits on the right. This moves the most significant byte to the leftmost 8 bits.
The expression (((uint16_t) (n)) >> 8)
shifts the 16-bit value to the right by 8 bits, adding zero bits on the left. This moves the least significant byte to the rightmost 8 bits. Finally, these two expressions are combined using the |
operator, resulting in a 16-bit value with its byte order swapped. The most significant byte and the least significant byte have swapped positions.