Skip to content

Commit

Permalink
Merge pull request #3646 from authmillenon/posix/feat/sockets
Browse files Browse the repository at this point in the history
posix: redo socket API to use conn
  • Loading branch information
miri64 committed Sep 22, 2015
2 parents 67786b4 + c36f9e0 commit 4508e22
Show file tree
Hide file tree
Showing 17 changed files with 1,705 additions and 514 deletions.
11 changes: 6 additions & 5 deletions Makefile.dep
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
ifneq (,$(filter libcoap,$(USEPKG)))
USEMODULE += pnet
endif

ifneq (,$(filter pnet,$(USEMODULE)))
USEMODULE += vtimer
USEMODULE += posix_sockets
endif

ifneq (,$(filter gnrc_%,$(filter-out gnrc_netapi gnrc_netreg gnrc_netif% gnrc_pktbuf,$(USEMODULE))))
Expand Down Expand Up @@ -276,6 +272,11 @@ ifneq (,$(filter newlib,$(USEMODULE)))
USEMODULE += uart_stdio
endif

ifneq (,$(filter posix_sockets,$(USEMODULE)))
USEMODULE += posix
USEMODULE += random
endif

ifneq (,$(filter posix,$(USEMODULE)))
USEMODULE += timex
USEMODULE += vtimer
Expand Down
36 changes: 36 additions & 0 deletions examples/posix_sockets/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# name of your application
APPLICATION = posix_sockets

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

BOARD_INSUFFICIENT_MEMORY := airfy-beacon chronos msb-430 msb-430h nrf51dongle nrf6310 \
nucleo-f334 pca10000 pca10005 stm32f0discovery telosb wsn430-v1_3b \
wsn430-v1_4 yunjia-nrf51822 z1

# Include packages that pull up and auto-init the link layer.
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
USEMODULE += gnrc_netif_default
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for socket communication via UDP
USEMODULE += gnrc_ipv6_default
USEMODULE += gnrc_udp
USEMODULE += gnrc_conn_udp
USEMODULE += posix_sockets
# Add also the shell, some shell commands
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
CFLAGS += -DDEVELHELP

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

include $(RIOTBASE)/Makefile.include
92 changes: 92 additions & 0 deletions examples/posix_sockets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
examples/posix_sockets
======================
This application is a showcase for RIOT's POSIX socket support. To
keep things simple this application has only one-hop support and
no routing capabilities.

Usage
=====

Build, flash and start the application:
```
export BOARD=your_board
make
make flash
make term
```

The `term` make target starts a terminal emulator for your board. It
connects to a default port so you can interact with the shell, usually
that is `/dev/ttyUSB0`. If your port is named differently, the
`PORT=/dev/yourport` (not to be confused with the UDP port) variable can
be used to override this.


Example output
==============

The shell commands come with online help. Call `help` to see which commands
exist and what they do.


udp send fe80::1 1337 uiaeue
2015-09-22 14:55:30,686 - INFO # > udp send fe80::1 1337 uiaeue
2015-09-22 14:55:30,690 - INFO # Success: send 6 byte to fe80::1:1337

Running the `help` command on an iotlab-m3:
```
2015-09-22 14:54:54,442 - INFO # help
2015-09-22 14:54:54,443 - INFO # Command Description
2015-09-22 14:54:54,444 - INFO # ---------------------------------------
2015-09-22 14:54:54,446 - INFO # udp send data over UDP and listen on UDP ports
2015-09-22 14:54:54,447 - INFO # reboot Reboot the node
2015-09-22 14:54:54,449 - INFO # ps Prints information about running threads.
2015-09-22 14:54:54,451 - INFO # mersenne_init initializes the PRNG
2015-09-22 14:54:54,453 - INFO # mersenne_get returns 32 bit of pseudo randomness
2015-09-22 14:54:54,454 - INFO # ifconfig Configure network interfaces
2015-09-22 14:54:54,455 - INFO # txtsnd send raw data
2015-09-22 14:54:54,457 - INFO # ncache manage neighbor cache by hand
2015-09-22 14:54:54,459 - INFO # routers IPv6 default router list
```

Running the `ps` command on an iotlab-m3:

```
2015-09-22 14:54:57,134 - INFO # > ps
2015-09-22 14:54:57,139 - INFO # pid | name | state Q | pri | stack ( used) | location
2015-09-22 14:54:57,143 - INFO # 1 | idle | pending Q | 15 | 256 ( 136) | 0x200001cc
2015-09-22 14:54:57,157 - INFO # 2 | main | pending Q | 7 | 1536 ( 620) | 0x200002cc
2015-09-22 14:54:57,164 - INFO # 3 | 6lo | bl rx _ | 3 | 1024 ( 404) | 0x20003ef8
2015-09-22 14:54:57,169 - INFO # 4 | ipv6 | bl rx _ | 4 | 1024 ( 436) | 0x20001cc0
2015-09-22 14:54:57,172 - INFO # 5 | udp | bl rx _ | 5 | 1024 ( 268) | 0x20004660
2015-09-22 14:54:57,177 - INFO # 6 | at86rfxx | bl rx _ | 3 | 1024 ( 320) | 0x20001888
2015-09-22 14:54:57,183 - INFO # | SUM | | | 5888 ( 2184)
```

Start a UDP server with `udp server start <udp_port>`:

```
2015-09-22 14:55:09,563 - INFO # > udp server start 1337
2015-09-22 14:55:09,564 - INFO # Success: started UDP server on port 1337
```

Send a UDP package with `udp send <dst_addr> <dst_port> <data>`:

```
2015-09-22 14:55:30,686 - INFO # > udp send fe80::3432:4833:46d4:9e06 1337 test
2015-09-22 14:55:30,690 - INFO # Success: send 4 byte to [fe80::3432:4833:46d4:9e06]:1337
```

You can get the IPv6 address of the destination by using the `ifconfig` command on the receiver:

```
2015-09-22 14:58:10,394 - INFO # ifconfig
2015-09-22 14:58:10,397 - INFO # Iface 6 HWaddr: 9e:06 Channel: 26 NID: 0x23 TX-Power: 0dBm State: IDLE CSMA Retries: 4
2015-09-22 14:58:10,399 - INFO # Long HWaddr: 36:32:48:33:46:d4:9e:06
2015-09-22 14:58:10,400 - INFO # AUTOACK CSMA MTU:1280 6LO IPHC
2015-09-22 14:58:10,402 - INFO # Source address length: 8
2015-09-22 14:58:10,404 - INFO # Link type: wireless
2015-09-22 14:58:10,407 - INFO # inet6 addr: ff02::1/128 scope: local [multicast]
2015-09-22 14:58:10,415 - INFO # inet6 addr: fe80::3432:4833:46d4:9e06/64 scope: local
2015-09-22 14:58:10,416 - INFO #
```
44 changes: 44 additions & 0 deletions examples/posix_sockets/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup examples
* @{
*
* @file
* @brief Example application for demonstrating the RIOT's POSIX sockets
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/

#include <stdio.h>

#include "shell.h"

#define MAIN_MSG_QUEUE_SIZE (1)

extern int udp_cmd(int argc, char **argv);

static const shell_command_t shell_commands[] = {
{ "udp", "send data over UDP and listen on UDP ports", udp_cmd },
{ NULL, NULL, NULL }
};

int main(void)
{
puts("RIOT socket example application");
/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);

/* should be never reached */
return 0;
}
187 changes: 187 additions & 0 deletions examples/posix_sockets/udp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup examples
* @{
*
* @file
* @brief Demonstrating the sending and receiving of UDP data over POSIX sockets.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include "thread.h"

#define SERVER_MSG_QUEUE_SIZE (8)
#define SERVER_BUFFER_SIZE (64)

static int server_socket = -1;
static char server_buffer[SERVER_BUFFER_SIZE];
static char server_stack[THREAD_STACKSIZE_DEFAULT];
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];

static void *_server_thread(void *args)
{
struct sockaddr_in6 server_addr;
uint16_t port;
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
server_socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
/* parse port */
port = (uint16_t)atoi((char *)args);
if (port == 0) {
puts("Error: invalid port specified");
return NULL;
}
server_addr.sin6_family = AF_INET6;
memset(&server_addr.sin6_addr, 0, sizeof(server_addr.sin6_addr));
server_addr.sin6_port = htons(port);
if (server_socket < 0) {
puts("error initializing socket");
server_socket = 0;
return NULL;
}
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
server_socket = -1;
puts("error binding socket");
return NULL;
}
printf("Success: started UDP server on port %" PRIu16 "\n", port);
while (1) {
int res;
struct sockaddr_in6 src;
socklen_t src_len = sizeof(struct sockaddr_in6);
if ((res = recvfrom(server_socket, server_buffer, sizeof(server_buffer), 0,
(struct sockaddr *)&src, &src_len)) < 0) {
puts("Error on receive");
}
else if (res == 0) {
puts("Peer did shut down");
}
else {
printf("Received data: ");
puts(server_buffer);
}
}
return NULL;
}

static int udp_send(char *addr_str, char *port_str, char *data, unsigned int num,
unsigned int delay)
{
struct sockaddr_in6 src, dst;
size_t data_len = strlen(data);
uint16_t port;
int s;
src.sin6_family = AF_INET6;
dst.sin6_family = AF_INET6;
memset(&src.sin6_addr, 0, sizeof(src.sin6_addr));
/* parse destination address */
if (inet_pton(AF_INET6, addr_str, &dst.sin6_addr) != 1) {
puts("Error: unable to parse destination address");
return 1;
}
/* parse port */
port = (uint16_t)atoi(port_str);
dst.sin6_port = htons(port);
src.sin6_port = htons(port);
s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0) {
puts("error initializing socket");
return 1;
}
for (unsigned int i = 0; i < num; i++) {
if (sendto(s, data, data_len, 0, (struct sockaddr *)&dst, sizeof(dst)) < 0) {
puts("could not send");
}
else {
printf("Success: send %u byte to %s:%u\n", (unsigned)data_len, addr_str, port);
}

usleep(delay);
}
close(s);
return 0;
}

static int udp_start_server(char *port_str)
{
/* check if server is already running */
if (server_socket >= 0) {
puts("Error: server already running");
return 1;
}
/* start server (which means registering pktdump for the chosen port) */
if (thread_create(server_stack, sizeof(server_stack), THREAD_PRIORITY_MAIN - 1,
CREATE_STACKTEST, _server_thread, port_str, "UDP server") <= KERNEL_PID_UNDEF) {
server_socket = -1;
puts("error initializing thread");
return 1;
}
return 0;
}

int udp_cmd(int argc, char **argv)
{
if (argc < 2) {
printf("usage: %s [send|server]\n", argv[0]);
return 1;
}

if (strcmp(argv[1], "send") == 0) {
uint32_t num = 1;
uint32_t delay = 1000000;
if (argc < 5) {
printf("usage: %s send <addr> <port> <data> [<num> [<delay in us>]]\n",
argv[0]);
return 1;
}
if (argc > 5) {
num = (uint32_t)atoi(argv[5]);
}
if (argc > 6) {
delay = (uint32_t)atoi(argv[6]);
}
return udp_send(argv[2], argv[3], argv[4], num, delay);
}
else if (strcmp(argv[1], "server") == 0) {
if (argc < 3) {
printf("usage: %s server [start|stop]\n", argv[0]);
return 1;
}
if (strcmp(argv[2], "start") == 0) {
if (argc < 4) {
printf("usage %s server start <port>\n", argv[0]);
return 1;
}
return udp_start_server(argv[3]);
}
else {
puts("error: invalid command");
return 1;
}
}
else {
puts("error: invalid command");
return 1;
}
}

/** @} */
4 changes: 2 additions & 2 deletions sys/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ifneq (,$(filter pnet,$(USEMODULE)))
DIRS += posix/pnet
ifneq (,$(filter posix_sockets,$(USEMODULE)))
DIRS += posix/sockets
endif
ifneq (,$(filter pthread,$(USEMODULE)))
DIRS += posix/pthread
Expand Down
Loading

0 comments on commit 4508e22

Please sign in to comment.