2011-09-28 16:29:01 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <thread.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "udp.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "sys/net/sixlowpan/sixlowip.h"
|
2011-12-26 02:59:58 +01:00
|
|
|
#include "sys/net/sixlowpan/sixlowpan.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
#include "socket.h"
|
|
|
|
#include "in.h"
|
2011-10-13 04:31:07 +02:00
|
|
|
#include "sys/net/net_help/net_help.h"
|
2011-11-25 01:31:54 +01:00
|
|
|
#include "sys/net/net_help/msg_help.h"
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
uint16_t udp_csum(ipv6_hdr_t *ipv6_header, udp_hdr_t *udp_header)
|
|
|
|
{
|
2011-09-28 16:29:01 +02:00
|
|
|
uint16_t sum;
|
|
|
|
uint16_t len = udp_header->length;
|
|
|
|
|
|
|
|
sum = len + IPPROTO_UDP;
|
2011-10-13 04:31:07 +02:00
|
|
|
sum = csum(sum, (uint8_t *)&ipv6_header->srcaddr, 2 * sizeof(ipv6_addr_t));
|
|
|
|
sum = csum(sum, (uint8_t*)udp_header, len);
|
2011-09-28 16:29:01 +02:00
|
|
|
return (sum == 0) ? 0xffff : HTONS(sum);
|
|
|
|
}
|
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
void udp_packet_handler(void)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-13 04:31:07 +02:00
|
|
|
msg_t m_recv_ip, m_send_ip, m_recv_udp, m_send_udp;
|
2011-11-01 21:04:28 +01:00
|
|
|
ipv6_hdr_t *ipv6_header;
|
|
|
|
udp_hdr_t *udp_header;
|
2011-10-13 04:31:07 +02:00
|
|
|
uint8_t *payload;
|
|
|
|
socket_internal_t *udp_socket = NULL;
|
|
|
|
uint16_t chksum;
|
2011-09-28 16:29:01 +02:00
|
|
|
|
2011-10-13 04:31:07 +02:00
|
|
|
while (1)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2012-01-24 03:19:11 +01:00
|
|
|
msg_receive(&m_recv_ip);
|
2011-12-26 02:59:58 +01:00
|
|
|
ipv6_header = ((ipv6_hdr_t*)m_recv_ip.content.ptr);
|
|
|
|
udp_header = ((udp_hdr_t*)(m_recv_ip.content.ptr + IPV6_HDR_LEN));
|
|
|
|
payload = (uint8_t*)(m_recv_ip.content.ptr + IPV6_HDR_LEN + UDP_HDR_LEN);
|
2011-10-13 04:31:07 +02:00
|
|
|
|
|
|
|
chksum = udp_csum(ipv6_header, udp_header);
|
|
|
|
|
|
|
|
if (chksum == 0xffff)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-28 04:37:12 +02:00
|
|
|
udp_socket = get_udp_socket(ipv6_header, udp_header);
|
2011-10-13 04:31:07 +02:00
|
|
|
if (udp_socket != NULL)
|
|
|
|
{
|
2011-12-26 02:59:58 +01:00
|
|
|
m_send_udp.content.ptr = (char*)ipv6_header;
|
2012-01-27 02:54:59 +01:00
|
|
|
msg_send_receive(&m_send_udp, &m_recv_udp, udp_socket->recv_pid);
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
|
|
|
else
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-13 04:31:07 +02:00
|
|
|
printf("Dropped UDP Message because no process ID was found for delivery!\n");
|
|
|
|
}
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2011-10-13 04:31:07 +02:00
|
|
|
else
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-13 04:31:07 +02:00
|
|
|
printf("Wrong checksum (%x)!\n", chksum);
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2012-01-24 03:19:11 +01:00
|
|
|
msg_reply(&m_recv_ip, &m_send_ip);
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2011-10-13 04:31:07 +02:00
|
|
|
|
|
|
|
|