udp: put UDP header in its own module

This commit is contained in:
Martine Lenders 2015-08-10 03:16:53 +02:00
parent 54fe08feda
commit a5e039cf4d
8 changed files with 74 additions and 32 deletions

View File

@ -145,6 +145,7 @@ endif
ifneq (,$(filter ng_udp,$(USEMODULE))) ifneq (,$(filter ng_udp,$(USEMODULE)))
USEMODULE += ng_netbase USEMODULE += ng_netbase
USEMODULE += inet_csum USEMODULE += inet_csum
USEMODULE += udp
endif endif
ifneq (,$(filter ng_nettest,$(USEMODULE))) ifneq (,$(filter ng_nettest,$(USEMODULE)))

View File

@ -121,6 +121,9 @@ endif
ifneq (,$(filter ng_netdev_eth,$(USEMODULE))) ifneq (,$(filter ng_netdev_eth,$(USEMODULE)))
DIRS += net/link_layer/ng_netdev_eth DIRS += net/link_layer/ng_netdev_eth
endif endif
ifneq (,$(filter udp,$(USEMODULE)))
DIRS += net/transport_layer/udp
endif
DIRS += $(dir $(wildcard $(addsuffix /Makefile, ${USEMODULE}))) DIRS += $(dir $(wildcard $(addsuffix /Makefile, ${USEMODULE})))

View File

@ -26,6 +26,7 @@
#include "byteorder.h" #include "byteorder.h"
#include "net/ng_netbase.h" #include "net/ng_netbase.h"
#include "net/udp.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -52,17 +53,6 @@ extern "C" {
#define NG_UDP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT) #define NG_UDP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT)
#endif #endif
/**
* @brief UDP header definition
*/
typedef struct __attribute__((packed)) {
network_uint16_t src_port; /**< source port, in network byte order */
network_uint16_t dst_port; /**< destination port, network byte order */
network_uint16_t length; /**< payload length (including the header),
* network byte order */
network_uint16_t checksum; /**< checksum */
} ng_udp_hdr_t;
/** /**
* @brief Calculate the checksum for the given packet * @brief Calculate the checksum for the given packet
* *
@ -93,13 +83,6 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
uint8_t *src, size_t src_len, uint8_t *src, size_t src_len,
uint8_t *dst, size_t dst_len); uint8_t *dst, size_t dst_len);
/**
* @brief Print the given UDP header to STDOUT
*
* @param[in] hdr UDP header to print
*/
void ng_udp_hdr_print(ng_udp_hdr_t *hdr);
/** /**
* @brief Initialize and start UDP * @brief Initialize and start UDP
* *

54
sys/include/net/udp.h Normal file
View File

@ -0,0 +1,54 @@
/*
* 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.
*/
/**
* @defgroup net_udp UDP
* @ingroup net
* @brief UDP header definition
* @see <a href="https://tools.ietf.org/html/rfc768">
* RFC 768
* </a>
* @{
*
* @file
* @brief UDP header definition
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef UDP_H_
#define UDP_H_
#include "byteorder.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief UDP header
*/
typedef struct __attribute__((packed)) {
network_uint16_t src_port; /**< source port */
network_uint16_t dst_port; /**< destination port */
network_uint16_t length; /**< payload length (including the header) */
network_uint16_t checksum; /**< checksum */
} udp_hdr_t;
/**
* @brief Print the given UDP header to STDOUT
*
* @param[in] hdr UDP header to print
*/
void udp_hdr_print(udp_hdr_t *hdr);
#ifdef __cplusplus
}
#endif
#endif /* UDP_H_ */
/** @} */

View File

@ -31,7 +31,7 @@
#include "net/ng_ipv6/addr.h" #include "net/ng_ipv6/addr.h"
#include "net/ng_ipv6/hdr.h" #include "net/ng_ipv6/hdr.h"
#include "net/ng_sixlowpan.h" #include "net/ng_sixlowpan.h"
#include "net/ng_udp.h" #include "net/udp.h"
#include "od.h" #include "od.h"
/** /**
@ -82,7 +82,7 @@ static void _dump_snip(ng_pktsnip_t *pkt)
#ifdef MODULE_NG_UDP #ifdef MODULE_NG_UDP
case NG_NETTYPE_UDP: case NG_NETTYPE_UDP:
printf("NETTYPE_UDP (%i)\n", pkt->type); printf("NETTYPE_UDP (%i)\n", pkt->type);
ng_udp_hdr_print(pkt->data); udp_hdr_print(pkt->data);
break; break;
#endif #endif
#ifdef TEST_SUITES #ifdef TEST_SUITES

View File

@ -76,7 +76,7 @@ static uint16_t _calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr,
payload = payload->next; payload = payload->next;
} }
/* process applicable UDP header bytes */ /* process applicable UDP header bytes */
csum = inet_csum(csum, (uint8_t *)hdr->data, sizeof(ng_udp_hdr_t)); csum = inet_csum(csum, (uint8_t *)hdr->data, sizeof(udp_hdr_t));
switch (pseudo_hdr->type) { switch (pseudo_hdr->type) {
#ifdef MODULE_NG_IPV6 #ifdef MODULE_NG_IPV6
@ -96,7 +96,7 @@ static uint16_t _calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr,
static void _receive(ng_pktsnip_t *pkt) static void _receive(ng_pktsnip_t *pkt)
{ {
ng_pktsnip_t *udp, *ipv6; ng_pktsnip_t *udp, *ipv6;
ng_udp_hdr_t *hdr; udp_hdr_t *hdr;
uint32_t port; uint32_t port;
/* mark UDP header */ /* mark UDP header */
@ -107,7 +107,7 @@ static void _receive(ng_pktsnip_t *pkt)
return; return;
} }
pkt = udp; pkt = udp;
udp = ng_pktbuf_mark(pkt, sizeof(ng_udp_hdr_t), NG_NETTYPE_UDP); udp = ng_pktbuf_mark(pkt, sizeof(udp_hdr_t), NG_NETTYPE_UDP);
if (udp == NULL) { if (udp == NULL) {
DEBUG("udp: error marking UDP header, dropping packet\n"); DEBUG("udp: error marking UDP header, dropping packet\n");
ng_pktbuf_release(pkt); ng_pktbuf_release(pkt);
@ -116,7 +116,7 @@ static void _receive(ng_pktsnip_t *pkt)
/* mark payload as Type: UNDEF */ /* mark payload as Type: UNDEF */
pkt->type = NG_NETTYPE_UNDEF; pkt->type = NG_NETTYPE_UNDEF;
/* get explicit pointer to UDP header */ /* get explicit pointer to UDP header */
hdr = (ng_udp_hdr_t *)udp->data; hdr = (udp_hdr_t *)udp->data;
LL_SEARCH_SCALAR(pkt, ipv6, type, NG_NETTYPE_IPV6); LL_SEARCH_SCALAR(pkt, ipv6, type, NG_NETTYPE_IPV6);
@ -141,7 +141,7 @@ static void _receive(ng_pktsnip_t *pkt)
static void _send(ng_pktsnip_t *pkt) static void _send(ng_pktsnip_t *pkt)
{ {
ng_udp_hdr_t *hdr; udp_hdr_t *hdr;
ng_pktsnip_t *udp_snip; ng_pktsnip_t *udp_snip;
/* get udp snip and hdr */ /* get udp snip and hdr */
@ -155,7 +155,7 @@ static void _send(ng_pktsnip_t *pkt)
ng_pktbuf_release(pkt); ng_pktbuf_release(pkt);
return; return;
} }
hdr = (ng_udp_hdr_t *)udp_snip->data; hdr = (udp_hdr_t *)udp_snip->data;
/* fill in size field */ /* fill in size field */
hdr->length = byteorder_htons(ng_pkt_len(udp_snip)); hdr->length = byteorder_htons(ng_pkt_len(udp_snip));
@ -224,7 +224,7 @@ int ng_udp_calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr)
if (csum == 0) { if (csum == 0) {
return -ENOENT; return -ENOENT;
} }
((ng_udp_hdr_t *)hdr->data)->checksum = byteorder_htons(csum); ((udp_hdr_t *)hdr->data)->checksum = byteorder_htons(csum);
return 0; return 0;
} }
@ -233,7 +233,7 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
uint8_t *dst, size_t dst_len) uint8_t *dst, size_t dst_len)
{ {
ng_pktsnip_t *res; ng_pktsnip_t *res;
ng_udp_hdr_t *hdr; udp_hdr_t *hdr;
/* check parameters */ /* check parameters */
if (src == NULL || dst == NULL || if (src == NULL || dst == NULL ||
@ -241,12 +241,12 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
return NULL; return NULL;
} }
/* allocate header */ /* allocate header */
res = ng_pktbuf_add(payload, NULL, sizeof(ng_udp_hdr_t), NG_NETTYPE_UDP); res = ng_pktbuf_add(payload, NULL, sizeof(udp_hdr_t), NG_NETTYPE_UDP);
if (res == NULL) { if (res == NULL) {
return NULL; return NULL;
} }
/* initialize header */ /* initialize header */
hdr = (ng_udp_hdr_t *)res->data; hdr = (udp_hdr_t *)res->data;
hdr->src_port = byteorder_htons(*((uint16_t *)src)); hdr->src_port = byteorder_htons(*((uint16_t *)src));
hdr->dst_port = byteorder_htons(*((uint16_t *)dst)); hdr->dst_port = byteorder_htons(*((uint16_t *)dst));
hdr->checksum = byteorder_htons(0); hdr->checksum = byteorder_htons(0);

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -21,9 +21,9 @@
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include "net/ng_udp.h" #include "net/udp.h"
void ng_udp_hdr_print(ng_udp_hdr_t *hdr) void udp_hdr_print(udp_hdr_t *hdr)
{ {
printf(" src-port: %5" PRIu16 " dst-port: %5" PRIu16 "\n", printf(" src-port: %5" PRIu16 " dst-port: %5" PRIu16 "\n",
byteorder_ntohs(hdr->src_port), byteorder_ntohs(hdr->dst_port)); byteorder_ntohs(hdr->src_port), byteorder_ntohs(hdr->dst_port));