-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add test app for raw IPv6 sockets
- Loading branch information
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
* @todo | ||
*/ | ||
|
||
#include <arpa/inet.h> | ||
#include <errno.h> | ||
|
||
#include "fd.h" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
APPLICATION = gnrc_ip6_sockets | ||
include ../Makefile.tests_common | ||
|
||
USEMODULE += conn_ip6_gnrc | ||
USEMODULE += ng_icmpv6_echo # include to have system internal handler | ||
USEMODULE += ng_ipv6_default | ||
USEMODULE += od | ||
USEMODULE += posix_sockets | ||
USEMODULE += shell | ||
USEMODULE += shell_commands | ||
USEMODULE += vtimer | ||
|
||
include $(RIOTBASE)/Makefile.include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* @{ | ||
* | ||
* @file | ||
*/ | ||
|
||
#include <arpa/inet.h> | ||
#include <netinet/in.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
|
||
#include "net/icmpv6.h" | ||
#include "od.h" | ||
#include "shell.h" | ||
#include "thread.h" | ||
|
||
#define RCV_STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF) | ||
#define RCV_PRIO (THREAD_PRIORITY_MAIN - 5) | ||
#define BUFSIZE (16) | ||
|
||
static char rcv_stack[RCV_STACKSIZE]; | ||
static char rcv_buf[BUFSIZE]; | ||
static char snd_buf[BUFSIZE]; | ||
|
||
static void *rcv(void *arg) | ||
{ | ||
int s; | ||
struct sockaddr_in6 local, addr; | ||
socklen_t addr_len = sizeof(addr); | ||
(void)arg; | ||
if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { | ||
puts("could not open receive socket"); | ||
return NULL; | ||
} | ||
memcpy(&local, &in6addr_any, sizeof(struct sockaddr_in6)); | ||
if (bind(s, (struct sockaddr *)&local, sizeof(local)) < 0) { | ||
puts("could not bind receive socket"); | ||
close(s); | ||
return NULL; | ||
} | ||
while (1) { | ||
int res = recvfrom(s, rcv_buf, sizeof(rcv_buf), 0, (struct sockaddr *)&addr, &addr_len); | ||
if (res < 0) { | ||
puts("error receiving packet"); | ||
continue; | ||
} | ||
puts("received ICMPv6 packet"); | ||
od_hex_dump(rcv_buf, res, 0); | ||
} | ||
return NULL; | ||
} | ||
|
||
static int snd(int argc, char **argv) | ||
{ | ||
icmpv6_hdr_t *hdr = (icmpv6_hdr_t *)snd_buf; | ||
void *data = snd_buf + sizeof(icmpv6_hdr_t); | ||
int s; | ||
struct sockaddr_in6 local, addr; | ||
const size_t max_size = BUFSIZE - sizeof(icmpv6_hdr_t); | ||
size_t size; | ||
if (argc < 3) { | ||
printf("usage: %s ipv6_addr data\n", argv[0]); | ||
return 1; | ||
} | ||
memset(&addr, 0, sizeof(addr)); | ||
addr.sin6_family = 0; | ||
if (inet_pton(AF_INET6, argv[1], &addr.sin6_addr) < 0) { | ||
printf("usage: %s ipv6_addr data\n", argv[0]); | ||
return 1; | ||
} | ||
if ((s = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { | ||
puts("could not open send socket"); | ||
return 1; | ||
} | ||
memcpy(&local, &in6addr_any, sizeof(struct sockaddr_in6)); | ||
if (bind(s, (struct sockaddr *)&local, sizeof(local)) < 0) { | ||
puts("could not bind send socket"); | ||
close(s); | ||
return 1; | ||
} | ||
hdr->type = ICMPV6_ECHO_REQ; | ||
hdr->code = 0; | ||
hdr->csum.u16 = 0; | ||
size = strlen(argv[2]); | ||
size = (size > max_size) ? max_size : size; | ||
memcpy(data, argv[2], size); | ||
if (sendto(s, snd_buf, size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | ||
puts("error sending packet"); | ||
} | ||
close(s); | ||
return 0; | ||
} | ||
|
||
static const shell_command_t shell_commands[] = { | ||
{ "snd", "send data over ICMPv6 echo request", snd }, | ||
{ NULL, NULL, NULL } | ||
}; | ||
|
||
int main(void) | ||
{ | ||
shell_t shell; | ||
thread_create(rcv_stack, sizeof(rcv_stack), RCV_PRIO, CREATE_STACKTEST, | ||
rcv, NULL, "rcv"); | ||
shell_init(&shell, shell_commands, UART0_BUFSIZE, getchar, putchar); | ||
shell_run(&shell); | ||
return 0; | ||
} | ||
|
||
/** @} */ |