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"
|
|
|
|
#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
|
|
|
void printArrayRange_udp(uint8_t *array, uint16_t len)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-13 04:31:07 +02:00
|
|
|
int i = 0;
|
|
|
|
printf("-------------MEMORY-------------\n");
|
|
|
|
for (i = 0; i < len; i++)
|
2011-09-28 16:29:01 +02:00
|
|
|
{
|
2011-10-13 04:31:07 +02:00
|
|
|
printf("%#x ", *(array+i));
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2011-10-13 04:31:07 +02:00
|
|
|
printf("-------------MEMORY-------------\n");
|
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
|
|
|
{
|
2011-11-25 01:31:54 +01:00
|
|
|
net_msg_receive(&m_recv_ip, FID_UDP_PH);
|
2011-11-01 21:04:28 +01:00
|
|
|
ipv6_header = ((ipv6_hdr_t*)&buffer_udp);
|
|
|
|
udp_header = ((udp_hdr_t*)(&buffer_udp[IPV6_HDR_LEN]));
|
2011-10-13 04:31:07 +02:00
|
|
|
payload = &buffer_udp[IPV6_HDR_LEN+UDP_HDR_LEN];
|
|
|
|
|
|
|
|
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-11-25 01:31:54 +01:00
|
|
|
net_msg_send_recv(&m_send_udp, &m_recv_udp, udp_socket->pid, FID_RECV_FROM, FID_UDP_PH);
|
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
|
|
|
}
|
2011-11-25 01:31:54 +01:00
|
|
|
net_msg_reply(&m_recv_ip, &m_send_ip, FID_SIXLOWIP_UDP);
|
2011-10-13 04:31:07 +02:00
|
|
|
}
|
2011-09-28 16:29:01 +02:00
|
|
|
}
|
2011-10-13 04:31:07 +02:00
|
|
|
|
|
|
|
|