mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-27 15:31:17 +01:00
new socket API functions, added TCP Handler, introduced global network
helper, increased stack sizes of network threads
This commit is contained in:
parent
d9ff08b23a
commit
9e20944fde
@ -12,3 +12,4 @@ UseModule shell ;
|
||||
UseModule ps ;
|
||||
UseModule 6lowpan ;
|
||||
UseModule vtimer ;
|
||||
#UseModule net_help ;
|
||||
|
||||
@ -19,14 +19,117 @@
|
||||
#include "sys/net/sixlowpan/sixlowpan.h"
|
||||
#include "sys/net/sixlowpan/sixlowerror.h"
|
||||
#include "sys/net/destiny/udp.h"
|
||||
#include "sys/net/destiny/tcp.h"
|
||||
#include "sys/net/destiny/socket.h"
|
||||
#include "sys/net/destiny/in.h"
|
||||
#include "sys/net/destiny/destiny.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
uint8_t udp_server_thread_pid;
|
||||
char udp_server_stack_buffer[UDP_STACK_SIZE];
|
||||
|
||||
uint8_t tcp_server_thread_pid;
|
||||
char tcp_server_stack_buffer[TCP_STACK_SIZE];
|
||||
|
||||
uint8_t server_thread_pid;
|
||||
char server_stack_buffer[2048];
|
||||
void init_tl (char *str)
|
||||
{
|
||||
init_transport_layer();
|
||||
}
|
||||
|
||||
void init_udp_server(void)
|
||||
{
|
||||
struct sockaddr_in6 sa;
|
||||
char buffer_main[256];
|
||||
ssize_t recsize;
|
||||
uint32_t fromlen;
|
||||
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
memset(&sa, 0, sizeof sa);
|
||||
sa.sin6_family = AF_INET;
|
||||
sa.sin6_port = 7654;
|
||||
fromlen = sizeof(sa);
|
||||
if (-1 == bind(sock, &sa, sizeof(sa), udp_server_thread_pid))
|
||||
{
|
||||
printf("Error bind failed!\n");
|
||||
close(sock);
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
print_socket(sock);
|
||||
recsize = recvfrom(sock, (void *)buffer_main, 256, 0, &sa, &fromlen);
|
||||
if (recsize < 0)
|
||||
{
|
||||
printf("ERROR: recsize < 0!\n");
|
||||
}
|
||||
printf("recsize: %i\n ", recsize);
|
||||
printf("datagram: %.*s\n", (int)recsize, buffer_main);
|
||||
}
|
||||
}
|
||||
|
||||
void init_tcp_server(void)
|
||||
{
|
||||
struct sockaddr_in6 stSockAddr;
|
||||
int SocketFD = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
if(-1 == SocketFD)
|
||||
{
|
||||
perror("can not create socket");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
memset(&stSockAddr, 0, sizeof(stSockAddr));
|
||||
|
||||
stSockAddr.sin6_family = AF_INET6;
|
||||
stSockAddr.sin6_port = HTONS(1100);
|
||||
|
||||
if(-1 == bind(SocketFD, &stSockAddr, sizeof(stSockAddr), tcp_server_thread_pid))
|
||||
{
|
||||
perror("error bind failed");
|
||||
close(SocketFD);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if(-1 == listen(SocketFD, 10))
|
||||
{
|
||||
perror("error listen failed");
|
||||
close(SocketFD);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int ConnectFD = accept(SocketFD, NULL, 0);
|
||||
|
||||
if(0 > ConnectFD)
|
||||
{
|
||||
perror("error accept failed");
|
||||
close(SocketFD);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// TODO: read and writes
|
||||
|
||||
shutdown(ConnectFD, 0);
|
||||
|
||||
close(ConnectFD);
|
||||
}
|
||||
}
|
||||
|
||||
void init_udp_server_thread(char *str)
|
||||
{
|
||||
udp_server_thread_pid = thread_create(udp_server_stack_buffer, UDP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, init_udp_server, "init_udp_server");
|
||||
printf("UDP SERVER THREAD PID: %i\n", udp_server_thread_pid);
|
||||
}
|
||||
|
||||
void init_tcp_server_thread(char *str)
|
||||
{
|
||||
tcp_server_thread_pid = thread_create(tcp_server_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, init_tcp_server, "init_tcp_server");
|
||||
printf("TCP SERVER THREAD PID: %i\n", tcp_server_thread_pid);
|
||||
}
|
||||
|
||||
void init_tcp_client_thread(char *str)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void init(char *str){
|
||||
char command;
|
||||
@ -90,6 +193,7 @@ void init(char *str){
|
||||
printf("ERROR: Unknown command '%c'\n", command);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void bootstrapping(char *str){
|
||||
@ -137,8 +241,9 @@ void send_udp(char *str)
|
||||
struct sockaddr_in6 sa;
|
||||
ipv6_addr_t ipaddr;
|
||||
int bytes_sent;
|
||||
int address;
|
||||
uint8_t text[20];
|
||||
sscanf(str, "send_udp %s", text);
|
||||
sscanf(str, "send_udp %s %i", text, &address);
|
||||
|
||||
sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (-1 == sock)
|
||||
@ -149,7 +254,7 @@ void send_udp(char *str)
|
||||
|
||||
memset(&sa, 0, sizeof sa);
|
||||
|
||||
ipv6_init_address(&ipaddr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, 0x0005);
|
||||
ipv6_init_address(&ipaddr, 0xabcd, 0x0, 0x0, 0x0, 0x3612, 0x00ff, 0xfe00, (uint16_t)address);
|
||||
ipv6_print_addr(&ipaddr);
|
||||
|
||||
sa.sin6_family = AF_INET;
|
||||
@ -196,47 +301,6 @@ void context(char *str){
|
||||
}
|
||||
}
|
||||
|
||||
void init_tl (char *str)
|
||||
{
|
||||
init_transport_layer();
|
||||
}
|
||||
|
||||
void init_server(void)
|
||||
{
|
||||
int sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
||||
printf("Socket Number: %i\n", sock);
|
||||
struct sockaddr_in6 sa;
|
||||
char buffer[1024];
|
||||
ssize_t recsize;
|
||||
uint32_t fromlen;
|
||||
memset(&sa, 0, sizeof sa);
|
||||
sa.sin6_family = AF_INET;
|
||||
sa.sin6_port = 7654;
|
||||
fromlen = sizeof(sa);
|
||||
if (-1 == bind(sock, &sa, sizeof(sa), server_thread_pid))
|
||||
{
|
||||
printf("Error bind failed!\n");
|
||||
close(sock);
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
print_socket(sock);
|
||||
recsize = recvfrom(sock, (void *)buffer, 1024, 0, &sa, &fromlen);
|
||||
if (recsize < 0)
|
||||
{
|
||||
printf("ERROR: recsize < 0!\n");
|
||||
}
|
||||
printf("recsize: %i\n ", recsize);
|
||||
printf("datagram: %.*s\n", (int)recsize, buffer);
|
||||
}
|
||||
}
|
||||
|
||||
void init_server_thread(char *str)
|
||||
{
|
||||
server_thread_pid = thread_create(server_stack_buffer, 2048, PRIORITY_MAIN, CREATE_STACKTEST, init_server, "init_server");
|
||||
printf("SERVER THREAD PID: %i\n", server_thread_pid);
|
||||
}
|
||||
|
||||
const shell_command_t shell_commands[] = {
|
||||
//{"send", "", send_packet},
|
||||
{"init", "", init},
|
||||
@ -246,19 +310,27 @@ const shell_command_t shell_commands[] = {
|
||||
{"ip", "", ip},
|
||||
{"context", "", context},
|
||||
{"init_tl", "", init_tl},
|
||||
{"init_server_thread", "", init_server_thread},
|
||||
{"init_udp_server_thread", "", init_udp_server_thread},
|
||||
{"init_tcp_server_thread", "", init_tcp_server_thread},
|
||||
{"init_tcp_client_thread", "", init_tcp_client_thread},
|
||||
{"send_udp", "", send_udp},
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
int main(void) {
|
||||
printf("6LoWPAN Transport Layers\n");
|
||||
vtimer_init();
|
||||
posix_open(uart0_handler_pid, 0);
|
||||
// vtimer_init();
|
||||
// border_initialize(TRANSCEIVER_CC1100, NULL);
|
||||
init_tl(NULL);
|
||||
init_udp_server_thread(NULL);
|
||||
// printf("RAN TO THE END!\n");
|
||||
|
||||
// init("init a 2");
|
||||
shell_t shell;
|
||||
shell_init(&shell, shell_commands, uart0_readc, uart0_putc);
|
||||
|
||||
shell_run(&shell);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -45,3 +45,4 @@ SubInclude TOP sys lib ;
|
||||
SubInclude TOP sys shell ;
|
||||
SubInclude TOP sys net sixlowpan ;
|
||||
SubInclude TOP sys net destiny ;
|
||||
SubInclude TOP sys net net_help ;
|
||||
|
||||
@ -2,8 +2,9 @@ SubDir TOP sys net destiny ;
|
||||
|
||||
# HDRS += $(TOP)/sys/net/destiny/ ;
|
||||
|
||||
Module destiny : destiny.c udp.c socket.c ;
|
||||
Module destiny : destiny.c udp.c tcp.c socket.c ;
|
||||
|
||||
UseModule vtimer ;
|
||||
UseModule auto_init ;
|
||||
UseModule 6lowpan ;
|
||||
UseModule 6lowpan ;
|
||||
UseModule net_help ;
|
||||
@ -9,18 +9,20 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "udp.h"
|
||||
#include "tcp.h"
|
||||
#include "socket.h"
|
||||
|
||||
void init_transport_layer(void)
|
||||
{
|
||||
printf("Initiating Transport Layer packages.\n");
|
||||
// SOCKETS
|
||||
memset(sockets, 0, MAX_SOCKETS*sizeof(socket_t));
|
||||
memset(sockets, 0, MAX_SOCKETS*sizeof(socket_internal_t));
|
||||
|
||||
// UDP
|
||||
int udp_thread_pid = thread_create(udp_stack_buffer, 2048, PRIORITY_MAIN, CREATE_STACKTEST, udp_packet_handler, "udp_packet_handler");
|
||||
set_udp_packet_handler_pid(udp_thread_pid, buffer);
|
||||
int udp_thread_pid = thread_create(udp_stack_buffer, UDP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, udp_packet_handler, "udp_packet_handler");
|
||||
set_udp_packet_handler_pid(udp_thread_pid, buffer_udp);
|
||||
|
||||
// TCP
|
||||
|
||||
int tcp_thread_pid = thread_create(tcp_stack_buffer, TCP_STACK_SIZE, PRIORITY_MAIN, CREATE_STACKTEST, tcp_packet_handler, "tcp_packet_handler");
|
||||
set_tcp_packet_handler_pid(tcp_thread_pid, buffer_tcp);
|
||||
}
|
||||
|
||||
@ -8,20 +8,21 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "udp.h"
|
||||
#include "tcp.h"
|
||||
#include "socket.h"
|
||||
|
||||
void print_socket(uint8_t socket)
|
||||
{
|
||||
socket = socket - 1;
|
||||
printf ("---SOCKET---\n");
|
||||
printf("Value: %i, Domain: %i, Type: %i, Protocol: %i \n", sockets[socket].socket, sockets[socket].domain, sockets[socket].type, sockets[socket].protocol);
|
||||
printf("Port: %i, Family: %i\n", sockets[socket].sa.sin6_port, sockets[socket].sa.sin6_family);
|
||||
ipv6_print_addr(&sockets[socket].sa.sin6_addr);
|
||||
printf("Value: %i, Domain: %i, Type: %i, Protocol: %i \n", sockets[socket].in_socket.socket_id, sockets[socket].in_socket.domain, sockets[socket].in_socket.type, sockets[socket].in_socket.protocol);
|
||||
printf("Port: %i, Family: %i\n", sockets[socket].in_socket.sa.sin6_port, sockets[socket].in_socket.sa.sin6_family);
|
||||
ipv6_print_addr(&sockets[socket].in_socket.sa.sin6_addr);
|
||||
}
|
||||
|
||||
bool exists_socket(uint8_t socket)
|
||||
{
|
||||
if (sockets[socket-1].socket == 0)
|
||||
if (sockets[socket-1].in_socket.socket_id == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@ -31,7 +32,7 @@ bool exists_socket(uint8_t socket)
|
||||
}
|
||||
}
|
||||
|
||||
socket_t* getSocket(uint8_t s)
|
||||
socket_internal_t* getSocket(uint8_t s)
|
||||
{
|
||||
if (exists_socket(s))
|
||||
return &sockets[s-1];
|
||||
@ -41,7 +42,11 @@ socket_t* getSocket(uint8_t s)
|
||||
|
||||
bool isUDPSocket(uint8_t s)
|
||||
{
|
||||
if ((exists_socket(s)) && (getSocket(s)->domain == PF_INET6) && (getSocket(s)->type == SOCK_DGRAM) && ((getSocket(s)->protocol == IPPROTO_UDP) || (getSocket(s)->protocol == 0)))
|
||||
if ( (exists_socket(s)) &&
|
||||
(getSocket(s)->in_socket.domain == PF_INET6) &&
|
||||
(getSocket(s)->in_socket.type == SOCK_DGRAM) &&
|
||||
((getSocket(s)->in_socket.protocol == IPPROTO_UDP) ||
|
||||
(getSocket(s)->in_socket.protocol == 0)))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
@ -49,7 +54,11 @@ bool isUDPSocket(uint8_t s)
|
||||
|
||||
bool isTCPSocket(uint8_t s)
|
||||
{
|
||||
if ((exists_socket(s)) && (getSocket(s)->domain == PF_INET6) && (getSocket(s)->type == SOCK_DGRAM) && ((getSocket(s)->protocol == IPPROTO_UDP) || (getSocket(s)->protocol == 0)))
|
||||
if ( (exists_socket(s)) &&
|
||||
(getSocket(s)->in_socket.domain == PF_INET6) &&
|
||||
(getSocket(s)->in_socket.type == SOCK_STREAM) &&
|
||||
((getSocket(s)->in_socket.protocol == IPPROTO_TCP) ||
|
||||
(getSocket(s)->in_socket.protocol == 0)))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
@ -64,12 +73,31 @@ int bind_udp_socket(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid)
|
||||
}
|
||||
for (i = 1; i < MAX_SOCKETS+1; i++)
|
||||
{
|
||||
if (isUDPSocket(i) && (getSocket(i)->sa.sin6_port == name->sin6_port))
|
||||
if (isUDPSocket(i) && (getSocket(i)->in_socket.sa.sin6_port == name->sin6_port))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
memcpy(&getSocket(s)->sa, name, namelen);
|
||||
memcpy(&getSocket(s)->in_socket.sa, name, namelen);
|
||||
getSocket(s)->pid = pid;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bind_tcp_socket(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid)
|
||||
{
|
||||
int i;
|
||||
if (!exists_socket(s))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
for (i = 1; i < MAX_SOCKETS+1; i++)
|
||||
{
|
||||
if (isTCPSocket(i) && (getSocket(i)->in_socket.sa.sin6_port == name->sin6_port))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
memcpy(&getSocket(s)->in_socket.sa, name, namelen);
|
||||
getSocket(s)->pid = pid;
|
||||
return 1;
|
||||
}
|
||||
@ -87,12 +115,12 @@ int socket(int domain, int type, int protocol)
|
||||
}
|
||||
else
|
||||
{
|
||||
struct socket_t *current_socket = &sockets[i-1];
|
||||
current_socket->socket = i;
|
||||
struct socket_t *current_socket = &sockets[i-1].in_socket;
|
||||
current_socket->socket_id = i;
|
||||
current_socket->domain = domain;
|
||||
current_socket->type = type;
|
||||
current_socket->protocol = protocol;
|
||||
return current_socket->socket;
|
||||
return current_socket->socket_id;
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,70 +141,76 @@ int32_t recv(int s, void *buf, uint64_t len, int flags)
|
||||
|
||||
int32_t recvfrom(int s, void *buf, uint64_t len, int flags, struct sockaddr_in6 *from, uint32_t *fromlen)
|
||||
{
|
||||
msg_t m_recv, m_send;
|
||||
struct ipv6_hdr_t *ipv6_header;
|
||||
struct udp_hdr_t *udp_header;
|
||||
uint8_t *payload;
|
||||
uint16_t payload_size = 0;
|
||||
if (isUDPSocket(s))
|
||||
{
|
||||
msg_t m_recv, m_send;
|
||||
struct ipv6_hdr_t *ipv6_header;
|
||||
struct udp_hdr_t *udp_header;
|
||||
uint8_t *payload;
|
||||
uint16_t payload_size = 0;
|
||||
msg_receive(&m_recv);
|
||||
|
||||
msg_receive(&m_recv);
|
||||
ipv6_header = ((struct ipv6_hdr_t*)&buffer_udp);
|
||||
udp_header = ((struct udp_hdr_t*)(&buffer_udp[IPV6_HDR_LEN]));
|
||||
payload = &buffer_udp[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
|
||||
ipv6_header = ((struct ipv6_hdr_t*)&buffer);
|
||||
udp_header = ((struct udp_hdr_t*)(&buffer[IPV6_HDR_LEN]));
|
||||
payload = &buffer[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
|
||||
memset(buf, 0, len);
|
||||
memcpy(buf, payload, udp_header->length);
|
||||
payload_size = udp_header->length;
|
||||
memcpy(&from->sin6_addr, &ipv6_header->srcaddr, 16);
|
||||
memset(&from->sin6_family, AF_INET6, 1);
|
||||
memset(&from->sin6_flowinfo, 0, 4);
|
||||
memcpy(&from->sin6_port, &udp_header->src_port, sizeof(udp_header->src_port));
|
||||
memcpy(fromlen, (void*)(sizeof(sockaddr_in6)), sizeof(fromlen));
|
||||
msg_reply(&m_recv, &m_send);
|
||||
return payload_size;
|
||||
memset(buf, 0, len);
|
||||
memcpy(buf, payload, udp_header->length);
|
||||
payload_size = udp_header->length;
|
||||
memcpy(&from->sin6_addr, &ipv6_header->srcaddr, 16);
|
||||
memset(&from->sin6_family, AF_INET6, 1);
|
||||
memset(&from->sin6_flowinfo, 0, 4);
|
||||
memcpy(&from->sin6_port, &udp_header->src_port, sizeof(udp_header->src_port));
|
||||
memcpy(fromlen, (void*)(sizeof(sockaddr_in6)), sizeof(fromlen));
|
||||
msg_reply(&m_recv, &m_send);
|
||||
return payload_size;
|
||||
}
|
||||
else if (isTCPSocket(s))
|
||||
{
|
||||
return recv(s, buf, len, flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Socket Type not supported!\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t sendto(int s, void *msg, uint64_t len, int flags, struct sockaddr_in6 *to, uint32_t tolen)
|
||||
{
|
||||
if (exists_socket(s))
|
||||
if (exists_socket(s) && isUDPSocket(s) && (getSocket(s)->in_socket.sa.sin6_port == 0))
|
||||
{
|
||||
if (isUDPSocket(s) && (getSocket(s)->sa.sin6_port == 0))
|
||||
{
|
||||
uint8_t send_buffer[UDP_STACK_SIZE];
|
||||
uint8_t send_buffer[BUFFER_SIZE];
|
||||
|
||||
struct ipv6_hdr_t *temp_ipv6_header = ((struct ipv6_hdr_t*)(&send_buffer));
|
||||
struct udp_hdr_t *temp_udp_header = ((struct udp_hdr_t*)(&send_buffer[IPV6_HDR_LEN]));
|
||||
uint8_t *payload = &send_buffer[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
struct ipv6_hdr_t *temp_ipv6_header = ((struct ipv6_hdr_t*)(&send_buffer));
|
||||
struct udp_hdr_t *temp_udp_header = ((struct udp_hdr_t*)(&send_buffer[IPV6_HDR_LEN]));
|
||||
uint8_t *payload = &send_buffer[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
|
||||
ipv6_print_addr(&to->sin6_addr);
|
||||
ipv6_print_addr(&to->sin6_addr);
|
||||
|
||||
memcpy(&(temp_ipv6_header->destaddr), &to->sin6_addr, 16);
|
||||
ipv6_get_saddr(&(temp_ipv6_header->srcaddr), &(temp_ipv6_header->destaddr));
|
||||
temp_ipv6_header->version_trafficclass = IPV6_VER;
|
||||
temp_ipv6_header->trafficclass_flowlabel = 0;
|
||||
temp_ipv6_header->flowlabel = 0;
|
||||
temp_ipv6_header->nextheader = IPPROTO_UDP;
|
||||
temp_ipv6_header->hoplimit = MULTIHOP_HOPLIMIT;
|
||||
temp_ipv6_header->length = sizeof(temp_udp_header);
|
||||
memcpy(&(temp_ipv6_header->destaddr), &to->sin6_addr, 16);
|
||||
ipv6_get_saddr(&(temp_ipv6_header->srcaddr), &(temp_ipv6_header->destaddr));
|
||||
temp_ipv6_header->version_trafficclass = IPV6_VER;
|
||||
temp_ipv6_header->trafficclass_flowlabel = 0;
|
||||
temp_ipv6_header->flowlabel = 0;
|
||||
temp_ipv6_header->nextheader = IPPROTO_UDP;
|
||||
temp_ipv6_header->hoplimit = MULTIHOP_HOPLIMIT;
|
||||
|
||||
temp_udp_header->src_port = 0;
|
||||
temp_udp_header->dst_port = to->sin6_port;
|
||||
temp_udp_header->checksum = 0;
|
||||
|
||||
memcpy(payload, msg, len);
|
||||
temp_udp_header->length = UDP_HDR_LEN + len;
|
||||
temp_udp_header->src_port = 0;
|
||||
temp_udp_header->dst_port = to->sin6_port;
|
||||
temp_udp_header->checksum = 0;
|
||||
|
||||
temp_udp_header->checksum = ~udp_csum(temp_ipv6_header, temp_udp_header);
|
||||
memcpy(payload, msg, len);
|
||||
temp_udp_header->length = UDP_HDR_LEN + len;
|
||||
temp_ipv6_header->length = UDP_HDR_LEN + len;
|
||||
|
||||
printf("Content of UDP Packet: src_port: %i, dst_port: %i, length: %i, checksum: %x\n", temp_udp_header->src_port, temp_udp_header->dst_port, temp_udp_header->length, temp_udp_header->checksum);
|
||||
sixlowpan_send(&to->sin6_addr, (uint8_t*)(temp_udp_header), temp_udp_header->length, IPPROTO_UDP);
|
||||
return temp_udp_header->length;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
temp_udp_header->checksum = ~udp_csum(temp_ipv6_header, temp_udp_header);
|
||||
|
||||
printf("Content of IPv6 Packet: length: %i\n", temp_ipv6_header->length);
|
||||
printf("Content of UDP Packet: src_port: %i, dst_port: %i, length: %i, checksum: %x\n", temp_udp_header->src_port, temp_udp_header->dst_port, temp_udp_header->length, temp_udp_header->checksum);
|
||||
sixlowpan_send(&to->sin6_addr, (uint8_t*)(temp_udp_header), temp_udp_header->length, IPPROTO_UDP);
|
||||
return temp_udp_header->length;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -201,7 +235,7 @@ int bind(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid)
|
||||
{
|
||||
if (exists_socket(s))
|
||||
{
|
||||
socket_t *current_socket = getSocket(s);
|
||||
socket_t *current_socket = &getSocket(s)->in_socket;
|
||||
switch (current_socket->domain)
|
||||
{
|
||||
case (PF_INET):
|
||||
@ -214,12 +248,22 @@ int bind(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid)
|
||||
{
|
||||
switch (current_socket->type)
|
||||
{
|
||||
// TCP
|
||||
case (SOCK_STREAM):
|
||||
{
|
||||
// TODO: TCP sock allocation
|
||||
return -1;
|
||||
if ((current_socket->protocol == 0) || (current_socket->protocol == IPPROTO_TCP))
|
||||
{
|
||||
return bind_tcp_socket(s, name, namelen, pid);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
// UDP
|
||||
case (SOCK_DGRAM):
|
||||
{
|
||||
if ((current_socket->protocol == 0) || (current_socket->protocol == IPPROTO_UDP))
|
||||
@ -272,28 +316,109 @@ int bind(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid)
|
||||
|
||||
int listen(int s, int backlog)
|
||||
{
|
||||
|
||||
if (isTCPSocket(s) && getSocket(s)->in_socket.tcp_socket_status.state == CLOSED)
|
||||
{
|
||||
socket_internal_t *current_socket = getSocket(s);
|
||||
current_socket->in_socket.tcp_socket_status.state = LISTEN;
|
||||
memset(current_socket->queued_sockets, 0, MAX_QUEUED_SOCKETS*sizeof(socket_t));
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int accept(int s, struct sockaddr_in6 *addr, uint32_t addrlen)
|
||||
{
|
||||
if (isTCPSocket(s) && (getSocket(s)->in_socket.tcp_socket_status.state == LISTEN))
|
||||
{
|
||||
if (getSocket(s)->queued_sockets[0].socket_id != 0)
|
||||
{
|
||||
// Got waiting Connections (SYN received), get new socket number and establish connection
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No waiting connections, waiting for message from TCP Layer
|
||||
msg_t msg_recv;
|
||||
msg_receive(&msg_recv);
|
||||
// Got new connection request, get new socket number and establish connection:
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int get_udp_process(uint16_t port)
|
||||
int shutdown(int s , int how)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
socket_internal_t *get_udp_socket(uint16_t port)
|
||||
{
|
||||
uint8_t i = 1;
|
||||
while (i < MAX_SOCKETS+1)
|
||||
{
|
||||
if ((exists_socket(i)) && isUDPSocket(i) && (getSocket(i)->sa.sin6_port == port))
|
||||
if ((exists_socket(i)) && isUDPSocket(i) && (getSocket(i)->in_socket.sa.sin6_port == port))
|
||||
{
|
||||
return getSocket(i)->pid;
|
||||
return getSocket(i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
socket_internal_t *get_tcp_socket(uint16_t port)
|
||||
{
|
||||
uint8_t i = 1;
|
||||
while (i < MAX_SOCKETS+1)
|
||||
{
|
||||
if ((exists_socket(i)) && isTCPSocket(i) && (getSocket(i)->in_socket.sa.sin6_port == port))
|
||||
{
|
||||
return getSocket(i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
socket_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socket_internal_t *socket)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < MAX_QUEUED_SOCKETS; i++)
|
||||
{
|
||||
if (socket->queued_sockets[i].socket_id == 0)
|
||||
{
|
||||
socket_t *current_socket = &socket->queued_sockets[i];
|
||||
current_socket->socket_id = i+1;
|
||||
current_socket->domain = PF_INET6;
|
||||
current_socket->type = SOCK_STREAM;
|
||||
current_socket->protocol = IPPROTO_TCP;
|
||||
|
||||
memcpy(¤t_socket->sa.sin6_addr, (void*) &ipv6_header->srcaddr, sizeof(ipv6_header->srcaddr));
|
||||
current_socket->sa.sin6_family = AF_INET6;
|
||||
|
||||
// TODO: Decide what to do about "sa.sin6_flowlinfo"
|
||||
//memcpy(current_socket->sa.sin6_flowinfo, ipv6_header->flowlabel, sizeof(ipv6_header->flowlabel));
|
||||
|
||||
memcpy(¤t_socket->sa.sin6_port, (void*) &tcp_header->src_port, sizeof(tcp_header->src_port));
|
||||
|
||||
// TCP Values
|
||||
memcpy(¤t_socket->tcp_socket_status.ack_nr, (void*) tcp_header->ack_nr, sizeof(tcp_header->ack_nr));
|
||||
memcpy(¤t_socket->tcp_socket_status.seq_nr, (void*) tcp_header->seq_nr, sizeof(tcp_header->seq_nr));
|
||||
memcpy(¤t_socket->tcp_socket_status.window, (void*) &tcp_header->window, sizeof(tcp_header->window));
|
||||
current_socket->tcp_socket_status.state = SYN_RCVD;
|
||||
return current_socket;
|
||||
}
|
||||
else if (socket->queued_sockets[i].sa.sin6_port == tcp_header->src_port)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@
|
||||
#define SOCKET_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "tcp.h"
|
||||
#include "udp.h"
|
||||
#include "in.h"
|
||||
#include "sys/net/sixlowpan/sixlowip.h"
|
||||
|
||||
@ -104,6 +106,7 @@
|
||||
#define PF_MAX AF_MAX
|
||||
|
||||
#define MAX_SOCKETS 16
|
||||
#define MAX_QUEUED_SOCKETS 5
|
||||
|
||||
typedef struct __attribute__ ((packed)) sockaddr_in6
|
||||
{
|
||||
@ -115,15 +118,23 @@ typedef struct __attribute__ ((packed)) sockaddr_in6
|
||||
|
||||
typedef struct __attribute__ ((packed)) socket_t
|
||||
{
|
||||
uint8_t socket;
|
||||
uint8_t pid;
|
||||
uint8_t socket_id;
|
||||
uint8_t domain;
|
||||
uint8_t type;
|
||||
uint8_t protocol;
|
||||
tcp_socket_status_t tcp_socket_status;
|
||||
sockaddr_in6 sa;
|
||||
} socket_t;
|
||||
|
||||
socket_t sockets[MAX_SOCKETS];
|
||||
typedef struct __attribute__ ((packed)) socket_internal_t
|
||||
{
|
||||
uint8_t pid;
|
||||
//uint8_t socket_status; /* TCP only */
|
||||
socket_t in_socket;
|
||||
socket_t queued_sockets[MAX_QUEUED_SOCKETS];
|
||||
} socket_internal_t;
|
||||
|
||||
socket_internal_t sockets[MAX_SOCKETS];
|
||||
|
||||
int socket(int domain, int type, int protocol);
|
||||
int connect(int socket, struct sockaddr_in6 *addr, uint32_t addrlen);
|
||||
@ -135,8 +146,12 @@ int close(int s);
|
||||
int bind(int s, struct sockaddr_in6 *name, int namelen, uint8_t pid);
|
||||
int listen(int s, int backlog);
|
||||
int accept(int s, struct sockaddr_in6 *addr, uint32_t addrlen);
|
||||
int shutdown(int s , int how);
|
||||
void socket_init(void);
|
||||
int get_udp_process(uint16_t port);
|
||||
socket_internal_t *get_udp_socket(uint16_t port);
|
||||
socket_internal_t *get_tcp_socket(uint16_t port);
|
||||
void print_socket(uint8_t socket);
|
||||
bool exists_socket(uint8_t socket);
|
||||
socket_t *new_tcp_queued_socket(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header, socket_internal_t *socket);
|
||||
|
||||
#endif /* SOCKET_H_ */
|
||||
|
||||
116
sys/net/destiny/tcp.c
Normal file
116
sys/net/destiny/tcp.c
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
* tcp.c
|
||||
*
|
||||
* Created on: 29.09.2011
|
||||
* Author: Oliver
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <thread.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "tcp.h"
|
||||
#include "in.h"
|
||||
#include "socket.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
|
||||
void printArrayRange_tcp(uint8_t *udp_header, uint16_t len)
|
||||
{
|
||||
int i = 0;
|
||||
printf("-------------MEMORY-------------\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%#x ", *(udp_header+i));
|
||||
}
|
||||
printf("-------------MEMORY-------------\n");
|
||||
}
|
||||
|
||||
uint16_t tcp_csum(ipv6_hdr_t *ipv6_header, tcp_hdr_t *tcp_header){
|
||||
uint16_t sum;
|
||||
uint16_t len = ipv6_header->length;
|
||||
|
||||
sum = len + IPPROTO_TCP;
|
||||
sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
|
||||
sum = csum(sum, (uint8_t*)tcp_header, len);
|
||||
return (sum == 0) ? 0xffff : HTONS(sum);
|
||||
}
|
||||
|
||||
void tcp_packet_handler (void)
|
||||
{
|
||||
msg_t m_recv_ip, m_send_ip, m_recv_tcp, m_send_tcp;
|
||||
struct ipv6_hdr_t *ipv6_header;
|
||||
struct tcp_hdr_t *tcp_header;
|
||||
uint8_t *payload;
|
||||
socket_internal_t *tcp_socket = NULL;
|
||||
uint16_t chksum;
|
||||
|
||||
while (1)
|
||||
{
|
||||
msg_receive(&m_recv_ip);
|
||||
ipv6_header = ((struct ipv6_hdr_t*)&buffer);
|
||||
tcp_header = ((struct tcp_hdr_t*)(&buffer[IPV6_HDR_LEN]));
|
||||
payload = &buffer[IPV6_HDR_LEN+TCP_HDR_LEN];
|
||||
|
||||
chksum = tcp_csum(ipv6_header, tcp_header);
|
||||
printf("Checksum is %x!\n", chksum);
|
||||
|
||||
tcp_socket = get_tcp_socket(tcp_header->dst_port);
|
||||
|
||||
if ((chksum != 0xffff) && (tcp_socket != NULL))
|
||||
{
|
||||
uint8_t tcp_flags = tcp_header->reserved_flags;
|
||||
//TODO: URG Flag and PSH flag are currently being ignored
|
||||
|
||||
if (IS_TCP_ACK(tcp_flags))
|
||||
{
|
||||
// only ACK Bit set
|
||||
}
|
||||
else if (IS_TCP_RST(tcp_flags))
|
||||
{
|
||||
// only RST Bit set
|
||||
}
|
||||
else if (IS_TCP_SYN(tcp_flags))
|
||||
{
|
||||
// only SYN Bit set, request new queued socket
|
||||
|
||||
socket_t *new_socket = new_tcp_queued_socket(ipv6_header, tcp_header, tcp_socket);
|
||||
if (new_socket != NULL)
|
||||
{
|
||||
// notify socket function accept(..) that a new connection request has arrived
|
||||
m_send_tcp.content.ptr = (char*)buffer;
|
||||
m_send_tcp.content.value = IPV6_HDR_LEN + ipv6_header->length;
|
||||
msg_send(&m_send_tcp, tcp_socket->pid, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Dropped TCP Message because an error occured while requesting a new queued socket!\n");
|
||||
}
|
||||
}
|
||||
else if (IS_TCP_SYN_ACK(tcp_flags))
|
||||
{
|
||||
// only SYN and ACK Bit set
|
||||
}
|
||||
else if (IS_TCP_FIN(tcp_flags))
|
||||
{
|
||||
// only FIN Bit set
|
||||
}
|
||||
else if (IS_TCP_FIN_ACK(tcp_flags))
|
||||
{
|
||||
// only FIN and ACK Bit set
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: any other case
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Wrong checksum (%x) or no corresponding socket found!\n", chksum);
|
||||
}
|
||||
|
||||
tcp_socket = NULL;
|
||||
msg_reply(&m_recv_ip, &m_send_ip);
|
||||
}
|
||||
}
|
||||
|
||||
78
sys/net/destiny/tcp.h
Normal file
78
sys/net/destiny/tcp.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* tcp.h
|
||||
*
|
||||
* Created on: 29.09.2011
|
||||
* Author: Oliver
|
||||
*/
|
||||
|
||||
#ifndef TCP_H_
|
||||
#define TCP_H_
|
||||
|
||||
#define TCP_HDR_LEN 20
|
||||
|
||||
enum tcp_flags_
|
||||
{
|
||||
TCP_ACK = 0x08,
|
||||
TCP_URG_PSH = 0x14,
|
||||
TCP_RST = 0x20,
|
||||
TCP_SYN = 0x40,
|
||||
TCP_SYN_ACK = 0x48,
|
||||
TCP_FIN = 0x80,
|
||||
TCP_FIN_ACK = 0x88
|
||||
};
|
||||
|
||||
enum tcp_states
|
||||
{
|
||||
CLOSED = 0,
|
||||
LISTEN = 1,
|
||||
SYN_SENT = 2,
|
||||
SYN_RCVD = 3,
|
||||
ESTABLISHED = 4,
|
||||
FIN_WAIT_1 = 5,
|
||||
FIN_WAIT_2 = 6,
|
||||
CLOSE_WAIT = 7,
|
||||
CLOSING = 8,
|
||||
LAST_ACK = 9,
|
||||
TIME_WAIT = 10
|
||||
};
|
||||
|
||||
#define IS_TCP_ACK(a) ((a & ~TCP_URG_PSH) | TCP_ACK) // Test for ACK flag only, iognore URG und PSH flag
|
||||
#define IS_TCP_RST(a) ((a & ~TCP_URG_PSH) | TCP_RST)
|
||||
#define IS_TCP_SYN(a) ((a & ~TCP_URG_PSH) | TCP_SYN)
|
||||
#define IS_TCP_SYN_ACK(a) ((a & ~TCP_URG_PSH) | TCP_SYN_ACK)
|
||||
#define IS_TCP_FIN(a) ((a & ~TCP_URG_PSH) | TCP_FIN)
|
||||
#define IS_TCP_FIN_ACK(a) ((a & ~TCP_URG_PSH) | TCP_FIN_ACK)
|
||||
|
||||
// TODO: Probably stack size too high
|
||||
#define TCP_STACK_SIZE 2048
|
||||
|
||||
#include "sys/net/sixlowpan/sixlowip.h"
|
||||
|
||||
typedef struct __attribute__ ((packed)) tcp_socket_status_t
|
||||
{
|
||||
uint32_t seq_nr;
|
||||
uint32_t ack_nr;
|
||||
uint16_t window;
|
||||
uint8_t state;
|
||||
} tcp_socket_status_t;
|
||||
|
||||
typedef struct __attribute__ ((packed)) tcp_hdr_t
|
||||
{
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
uint32_t seq_nr;
|
||||
uint32_t ack_nr;
|
||||
uint8_t dataOffset_reserved;
|
||||
uint8_t reserved_flags;
|
||||
uint16_t window;
|
||||
uint16_t checksum;
|
||||
uint16_t urg_pointer;
|
||||
} tcp_hdr_t;
|
||||
|
||||
|
||||
uint8_t buffer_tcp[BUFFER_SIZE];
|
||||
char tcp_stack_buffer[TCP_STACK_SIZE];
|
||||
|
||||
void tcp_packet_handler (void);
|
||||
|
||||
#endif /* TCP_H_ */
|
||||
@ -8,6 +8,29 @@
|
||||
#include "sys/net/sixlowpan/sixlowip.h"
|
||||
#include "socket.h"
|
||||
#include "in.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
void printArrayRange_udp(uint8_t *array, uint16_t len)
|
||||
{
|
||||
int i = 0;
|
||||
printf("-------------MEMORY-------------\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%#x ", *(array+i));
|
||||
}
|
||||
printf("-------------MEMORY-------------\n");
|
||||
}
|
||||
|
||||
uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header)
|
||||
{
|
||||
uint16_t sum;
|
||||
uint16_t len = udp_header->length;
|
||||
|
||||
sum = len + IPPROTO_UDP;
|
||||
sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
|
||||
sum = csum(sum, (uint8_t*)udp_header, len);
|
||||
return (sum == 0) ? 0xffff : HTONS(sum);
|
||||
}
|
||||
|
||||
void udp_packet_handler(void)
|
||||
{
|
||||
@ -15,87 +38,41 @@ void udp_packet_handler(void)
|
||||
struct ipv6_hdr_t *ipv6_header;
|
||||
struct udp_hdr_t *udp_header;
|
||||
uint8_t *payload;
|
||||
int udp_process = 0;
|
||||
socket_internal_t *udp_socket = NULL;
|
||||
uint16_t chksum;
|
||||
|
||||
while (1)
|
||||
{
|
||||
msg_receive(&m_recv_ip);
|
||||
ipv6_header = ((struct ipv6_hdr_t*)&buffer);
|
||||
udp_header = ((struct udp_hdr_t*)(&buffer[IPV6_HDR_LEN]));
|
||||
payload = &buffer[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
printf("Inside UDP handler!\n");
|
||||
ipv6_header = ((struct ipv6_hdr_t*)&buffer_udp);
|
||||
udp_header = ((struct udp_hdr_t*)(&buffer_udp[IPV6_HDR_LEN]));
|
||||
payload = &buffer_udp[IPV6_HDR_LEN+UDP_HDR_LEN];
|
||||
|
||||
printf("Checksum is %x\n", (udp_csum(ipv6_header, udp_header)));
|
||||
m_send_udp.content.ptr = (char*)buffer;
|
||||
m_send_udp.content.value = IPV6_HDR_LEN + UDP_HDR_LEN + udp_header->length;
|
||||
udp_process = get_udp_process(udp_header->dst_port);
|
||||
printf("UDP_PROCESS: %i\n", udp_process);
|
||||
if (udp_process != -1)
|
||||
chksum = udp_csum(ipv6_header, udp_header);
|
||||
|
||||
if (chksum == 0xffff)
|
||||
{
|
||||
msg_send_receive(&m_send_udp, &m_recv_udp, udp_process);
|
||||
m_send_udp.content.ptr = (char*)buffer;
|
||||
m_send_udp.content.value = IPV6_HDR_LEN + UDP_HDR_LEN + udp_header->length;
|
||||
udp_socket = get_udp_socket(udp_header->dst_port);
|
||||
if (udp_socket != NULL)
|
||||
{
|
||||
msg_send_receive(&m_send_udp, &m_recv_udp, udp_socket->pid);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Dropped UDP Message because no process ID was found for delivery!\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Dropped UDP Message because no process ID was found for delivery!\n");
|
||||
printf("Wrong checksum (%x)!\n", chksum);
|
||||
}
|
||||
udp_process = -1;
|
||||
|
||||
udp_socket = NULL;
|
||||
msg_reply(&m_recv_ip, &m_send_ip);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header){
|
||||
uint16_t sum;
|
||||
uint16_t len = udp_header->length;
|
||||
|
||||
sum = len + IPPROTO_UDP;
|
||||
sum = checksum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
|
||||
sum = checksum(sum, (uint8_t*)udp_header, len);
|
||||
return (sum == 0) ? 0xffff : HTONS(sum);
|
||||
}
|
||||
|
||||
void printArrayRange(uint8_t *udp_header, uint16_t len)
|
||||
{
|
||||
int i = 0;
|
||||
printf("-------------MEMORY-------------\n");
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
printf("%#x ", *(udp_header+i));
|
||||
}
|
||||
printf("-------------MEMORY-------------\n");
|
||||
}
|
||||
|
||||
// TODO: Global file for commonly used functions like checksum
|
||||
uint16_t checksum(uint16_t sum, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
int count;
|
||||
uint16_t carry;
|
||||
|
||||
count = len >> 1;
|
||||
if(count)
|
||||
{
|
||||
if(count)
|
||||
{
|
||||
carry = 0;
|
||||
do
|
||||
{
|
||||
uint16_t t = (*buf << 8) + *(buf+1);
|
||||
count--;
|
||||
buf += 2;
|
||||
sum += carry;
|
||||
sum += t;
|
||||
carry = (t > sum);
|
||||
} while(count);
|
||||
sum += carry;
|
||||
}
|
||||
}
|
||||
if(len & 1)
|
||||
{
|
||||
uint16_t u = (*buf << 8);
|
||||
sum += (*buf << 8);
|
||||
if(sum < u)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
|
||||
// TODO: Probably stack size too high
|
||||
#define UDP_STACK_SIZE 256
|
||||
#define UDP_STACK_SIZE 4096
|
||||
|
||||
#include "sys/net/sixlowpan/sixlowip.h"
|
||||
|
||||
@ -23,12 +23,11 @@ typedef struct __attribute__ ((packed)) udp_hdr_t{
|
||||
uint16_t checksum;
|
||||
} udp_hdr_t;
|
||||
|
||||
uint8_t buffer[BUFFER_SIZE];
|
||||
char udp_stack_buffer[2048];
|
||||
uint8_t buffer_udp[BUFFER_SIZE];
|
||||
char udp_stack_buffer[UDP_STACK_SIZE];
|
||||
|
||||
uint16_t checksum(uint16_t sum, uint8_t *buf, uint16_t len);
|
||||
uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header);
|
||||
void udp_packet_handler(void);
|
||||
void printArrayRange(uint8_t *udp_header, uint16_t len);
|
||||
void printArrayRange_udp(uint8_t *array, uint16_t len);
|
||||
|
||||
#endif /* UDP_H_ */
|
||||
|
||||
6
sys/net/net_help/Jamfile
Normal file
6
sys/net/net_help/Jamfile
Normal file
@ -0,0 +1,6 @@
|
||||
SubDir TOP sys net net_help ;
|
||||
|
||||
# HDRS += $(TOP)/sys/net/net_help/ ;
|
||||
|
||||
Module net_help : net_help.c ;
|
||||
|
||||
48
sys/net/net_help/net_help.c
Normal file
48
sys/net/net_help/net_help.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* common.c
|
||||
*
|
||||
* Created on: 05.10.2011
|
||||
* Author: Oliver
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <thread.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "net_help.h"
|
||||
|
||||
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len)
|
||||
{
|
||||
int count;
|
||||
uint16_t carry;
|
||||
|
||||
count = len >> 1;
|
||||
if(count)
|
||||
{
|
||||
if(count)
|
||||
{
|
||||
carry = 0;
|
||||
do
|
||||
{
|
||||
uint16_t t = (*buf << 8) + *(buf+1);
|
||||
count--;
|
||||
buf += 2;
|
||||
sum += carry;
|
||||
sum += t;
|
||||
carry = (t > sum);
|
||||
} while(count);
|
||||
sum += carry;
|
||||
}
|
||||
}
|
||||
if(len & 1)
|
||||
{
|
||||
uint16_t u = (*buf << 8);
|
||||
sum += (*buf << 8);
|
||||
if(sum < u)
|
||||
{
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
19
sys/net/net_help/net_help.h
Normal file
19
sys/net/net_help/net_help.h
Normal file
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* common.h
|
||||
*
|
||||
* Created on: 05.10.2011
|
||||
* Author: Oliver
|
||||
*/
|
||||
|
||||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#define HTONS(a) ((((uint16_t) (a) >> 8) & 0xff) | ((((uint16_t) (a)) & 0xff) << 8))
|
||||
#define HTONL(a) ((((uint32_t) (a) & 0xff000000) >> 24) | \
|
||||
(((uint32_t) (a) & 0x00ff0000) >> 8) | \
|
||||
(((uint32_t) (a) & 0x0000ff00) << 8) | \
|
||||
(((uint32_t) (a) & 0x000000ff) << 24))
|
||||
|
||||
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len);
|
||||
|
||||
#endif /* COMMON_H_ */
|
||||
@ -7,3 +7,4 @@ Module 6lowpan : sixlowpan.c sixlowip.c sixlowmac.c sixlownd.c sixlowborder.c ie
|
||||
UseModule vtimer ;
|
||||
UseModule transceiver ;
|
||||
UseModule auto_init ;
|
||||
UseModule net_help ;
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
#include "sixlownd.h"
|
||||
#include "serialnumber.h"
|
||||
#include "sixlowerror.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
#define READER_STACK_SIZE 512
|
||||
|
||||
@ -61,6 +62,7 @@ void serial_reader_f(void) {
|
||||
while(1) {
|
||||
posix_open(uart0_handler_pid, 0);
|
||||
bytes = readpacket(get_serial_in_buffer(0), BORDER_BUFFER_SIZE);
|
||||
printf("GOT PACKET FROM RS232!\n");
|
||||
if (bytes < 0) {
|
||||
switch (bytes) {
|
||||
case (-SIXLOWERROR_ARRAYFULL):{
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "sixlownd.h"
|
||||
#include "sixlowpan.h"
|
||||
#include "sys/net/destiny/in.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
uint8_t buffer[BUFFER_SIZE];
|
||||
msg_t msg_queue[IP_PKT_RECV_BUF_SIZE];
|
||||
@ -124,43 +125,43 @@ void ipv6_process(void){
|
||||
icmpv6_demultiplex(icmp_buf);
|
||||
break;
|
||||
}
|
||||
case(IPPROTO_TCP):{
|
||||
// TODO: Notify TCP Handler
|
||||
case(IPPROTO_TCP):
|
||||
{
|
||||
printf("INFO: TCP Packet received.\n");
|
||||
char content[20];
|
||||
memcpy(content, ((char*)ipv6_buf)+IPV6_HDR_LEN, ipv6_buf->length);
|
||||
printf("Length: %i Content: %s\n", ipv6_buf->length, content);
|
||||
if (tcp_packet_handler_pid != 0){
|
||||
|
||||
}
|
||||
else{
|
||||
if (tcp_packet_handler_pid != 0)
|
||||
{
|
||||
memcpy(tcp_packet_buffer, (char*) ipv6_buf, IPV6_HDR_LEN+ipv6_buf->length);
|
||||
msg_send_receive(&m_send, &m_recv, tcp_packet_handler_pid);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("INFO: No TCP handler registered.\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case(IPPROTO_UDP):{
|
||||
// TODO: Notify UDP Handler
|
||||
}
|
||||
case(IPPROTO_UDP):
|
||||
{
|
||||
printf("INFO: UDP Packet received.\n");
|
||||
if (udp_packet_handler_pid != 0) {
|
||||
printf("IPv6 packet length: %i\n", IPV6_HDR_LEN+ipv6_buf->length);
|
||||
|
||||
if (udp_packet_handler_pid != 0)
|
||||
{
|
||||
memcpy(udp_packet_buffer, (char*) ipv6_buf, IPV6_HDR_LEN+ipv6_buf->length);
|
||||
|
||||
|
||||
// m_send.content.ptr = (char*) get_ipv6_buf();
|
||||
|
||||
printf("Copy 1!\n");
|
||||
msg_send_receive(&m_send, &m_recv, udp_packet_handler_pid);
|
||||
}
|
||||
else{
|
||||
printf("Copy 2!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("INFO: No UDP handler registered.\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case(PROTO_NUM_NONE):{
|
||||
// TODO: Notify all Transport Layer Protocols
|
||||
//uint8_t *ptr = get_payload_buf(ipv6_ext_hdr_len);
|
||||
}
|
||||
case(PROTO_NUM_NONE):
|
||||
{
|
||||
printf("INFO: Packet with no Header following the IPv6 Header received.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -454,14 +455,14 @@ uint8_t ipv6_is_router(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void set_tcp_packet_handler_pid(int pid, uint8_t *buffer)
|
||||
void set_tcp_packet_handler_pid(int pid, uint8_t *buf)
|
||||
{
|
||||
tcp_packet_handler_pid = pid;
|
||||
tcp_packet_buffer = buffer;
|
||||
tcp_packet_buffer = buf;
|
||||
}
|
||||
|
||||
void set_udp_packet_handler_pid(int pid, uint8_t *buffer)
|
||||
void set_udp_packet_handler_pid(int pid, uint8_t *buf)
|
||||
{
|
||||
udp_packet_handler_pid = pid;
|
||||
udp_packet_buffer = buffer;
|
||||
udp_packet_buffer = buf;
|
||||
}
|
||||
|
||||
@ -126,11 +126,7 @@ typedef struct __attribute__ ((packed)) iface_t {
|
||||
extern iface_t iface;
|
||||
|
||||
//#define HTONS(a) (uint16_t)((((uint16_t) (a)) << 8) | (((uint16_t) (a)) >> 8))
|
||||
#define HTONS(a) ((((uint16_t) (a) >> 8) & 0xff) | ((((uint16_t) (a)) & 0xff) << 8))
|
||||
#define HTONL(a) ((((uint32_t) (a) & 0xff000000) >> 24) | \
|
||||
(((uint32_t) (a) & 0x00ff0000) >> 8) | \
|
||||
(((uint32_t) (a) & 0x0000ff00) << 8) | \
|
||||
(((uint32_t) (a) & 0x000000ff) << 24))
|
||||
|
||||
|
||||
/* function prototypes */
|
||||
struct icmpv6_hdr_t* get_icmpv6_buf(uint8_t ext_len);
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include "transceiver.h"
|
||||
#include "vtimer.h"
|
||||
#include "ieee802154_frame.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
|
||||
char radio_stack_buffer[RADIO_STACK_SIZE];
|
||||
msg_t msg_q[RADIO_RCV_BUF_SIZE];
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "sixlowpan.h"
|
||||
#include "sixlowerror.h"
|
||||
#include "serialnumber.h"
|
||||
#include "sys/net/net_help/net_help.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -951,39 +952,6 @@ void set_llao(opt_stllao_t *sllao, uint8_t type, uint8_t length){
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// checksum calculation
|
||||
|
||||
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len){
|
||||
int count;
|
||||
uint16_t carry;
|
||||
|
||||
count = len >> 1;
|
||||
if(count){
|
||||
if(count){
|
||||
carry = 0;
|
||||
do {
|
||||
uint16_t t = (*buf << 8) + *(buf+1);
|
||||
count--;
|
||||
buf += 2;
|
||||
sum += carry;
|
||||
sum += t;
|
||||
carry = (t > sum);
|
||||
} while(count);
|
||||
sum += carry;
|
||||
}
|
||||
}
|
||||
if(len & 1){
|
||||
uint16_t u = (*buf << 8);
|
||||
sum += (*buf << 8);
|
||||
if(sum < u){
|
||||
sum++;
|
||||
}
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
uint16_t icmpv6_csum(uint8_t proto){
|
||||
ipv6_buf = get_ipv6_buf();
|
||||
uint16_t sum;
|
||||
|
||||
@ -244,7 +244,6 @@ uint8_t nbr_cache_add(ipv6_addr_t *ipaddr, ieee_802154_long_t *laddr,
|
||||
void nbr_cache_auto_rem(void);
|
||||
void nbr_cache_rem(ipv6_addr_t *addr);
|
||||
uint16_t icmpv6_csum(uint8_t proto);
|
||||
uint16_t csum(uint16_t sum, uint8_t *buf, uint16_t len);
|
||||
def_rtr_lst_t * def_rtr_lst_search(ipv6_addr_t *ipaddr);
|
||||
void def_rtr_lst_add(ipv6_addr_t *ipaddr, uint32_t rtr_ltime);
|
||||
void def_rtr_lst_rem(def_rtr_lst_t *entry);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user