1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/pnet/main.c

122 lines
2.9 KiB
C
Raw Normal View History

2014-02-03 23:03:13 +01:00
/*
* Copyright (C) 2013 Freie Universität Berlin
*
* 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.
2014-02-03 23:03:13 +01:00
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief PNet test application
*
* @author Martin Lenders <mlenders@inf.fu-berlin.de>
*
* @}
*/
2014-01-17 12:49:48 +01:00
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
2014-02-13 16:06:25 +01:00
#include "net_if.h"
2014-03-01 19:14:14 +01:00
#include "sixlowpan.h"
2014-01-17 12:49:48 +01:00
#include "ipv6.h"
#ifndef R_ADDR
#define R_ADDR (1)
#endif
#define PORT (1234)
2014-03-01 19:14:14 +01:00
#define ERROR(...) printf("ERROR: " __VA_ARGS__)
2014-02-13 16:06:25 +01:00
int init_local_address(uint16_t r_addr)
{
2014-01-17 12:49:48 +01:00
ipv6_addr_t std_addr;
ipv6_addr_init(&std_addr, 0xabcd, 0xef12, 0, 0, 0x1034, 0x00ff, 0xfe00,
2014-02-13 16:06:25 +01:00
0);
2014-03-01 19:14:14 +01:00
net_if_set_src_address_mode(0, NET_IF_TRANS_ADDR_M_SHORT);
return net_if_set_hardware_address(0, r_addr) &&
sixlowpan_lowpan_init_adhoc_interface(0, &std_addr);
2014-01-17 12:49:48 +01:00
}
int main(void)
{
2014-01-17 12:49:48 +01:00
int sockfd, res;
2014-03-01 19:14:14 +01:00
struct sockaddr_in6 my_addr, their_addr;
char buffer[14];
their_addr.sin6_family = AF_INET6;
2014-01-17 12:49:48 +01:00
#if R_ADDR == 1
2014-03-01 19:14:14 +01:00
their_addr.sin6_port = PORT;
2014-01-17 12:49:48 +01:00
#else
2014-03-01 19:14:14 +01:00
their_addr.sin6_port = 0;
2014-01-17 12:49:48 +01:00
#endif
2014-03-01 19:14:14 +01:00
their_addr.sin6_flowinfo = 0;
memset(&(their_addr.sin6_addr), 0, sizeof(their_addr.sin6_addr));
their_addr.sin6_addr.uint16[0] = htons(0xabcd);
their_addr.sin6_addr.uint16[1] = htons(0xef12);
their_addr.sin6_addr.uint16[5] = htons(0x00ff);
their_addr.sin6_addr.uint16[6] = htons(0xfe00);
2014-01-17 12:49:48 +01:00
#if R_ADDR == 1
2014-03-01 19:14:14 +01:00
their_addr.sin6_addr.uint16[7] = htons(2);
2014-01-17 12:49:48 +01:00
#else
2014-03-01 19:14:14 +01:00
their_addr.sin6_addr.uint16[7] = htons(1);
2014-01-17 12:49:48 +01:00
#endif
2014-03-01 19:14:14 +01:00
their_addr.sin6_scope_id = 0;
2014-01-17 12:49:48 +01:00
2014-02-13 16:06:25 +01:00
if (!init_local_address(R_ADDR)) {
2014-03-01 19:14:14 +01:00
ERROR("Can not initialize IP for hardware address %d.", R_ADDR);
2014-02-13 16:06:25 +01:00
return 1;
}
2014-01-17 12:49:48 +01:00
memcpy(buffer, "Hello, World!", 14);
memcpy(&my_addr, &in6addr_any, sizeof(my_addr));
2014-01-17 12:49:48 +01:00
my_addr.sin6_port = PORT;
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
res = bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr));
2014-01-17 12:49:48 +01:00
if (res < 0) {
2014-03-01 19:14:14 +01:00
ERROR("Socket could not be bind");
2014-01-17 12:49:48 +01:00
return 1;
}
#if R_ADDR == 1
res = sendto(sockfd, buffer, strlen(buffer), 0,
(struct sockaddr *)&their_addr,
(socklen_t) sizeof(their_addr));
2014-01-17 12:49:48 +01:00
#else
socklen_t their_len;
res = recvfrom(sockfd, buffer, strlen(buffer), 0,
(struct sockaddr *)&their_addr,
&their_len);
2014-01-17 12:49:48 +01:00
if (their_addr.sin6_addr.uint8[15] != 1) {
2014-03-01 19:14:14 +01:00
ERROR("Wrong sender address: %d\n", their_addr.sin6_addr.uint8[11]);
2014-01-17 12:49:48 +01:00
return 1;
}
2014-01-17 12:49:48 +01:00
printf("Port: %d\n", their_addr.sin6_port);
#endif
2014-01-17 12:49:48 +01:00
if (res < 0) {
2014-03-01 19:14:14 +01:00
ERROR("Message error");
2014-01-17 12:49:48 +01:00
return 1;
}
close(sockfd);
printf("All tests successful.\n");
return 0;
}