Browse Source

udp: put UDP header in its own module

dev/timer
Martine Lenders 8 years ago
parent
commit
a5e039cf4d
  1. 1
      Makefile.dep
  2. 3
      sys/Makefile
  3. 19
      sys/include/net/ng_udp.h
  4. 54
      sys/include/net/udp.h
  5. 4
      sys/net/crosslayer/ng_pktdump/ng_pktdump.c
  6. 20
      sys/net/transport_layer/ng_udp/ng_udp.c
  7. 1
      sys/net/transport_layer/udp/Makefile
  8. 4
      sys/net/transport_layer/udp/udp_hdr_print.c

1
Makefile.dep

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

3
sys/Makefile

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

19
sys/include/net/ng_udp.h

@ -26,6 +26,7 @@
#include "byteorder.h"
#include "net/ng_netbase.h"
#include "net/udp.h"
#ifdef __cplusplus
extern "C" {
@ -52,17 +53,6 @@ extern "C" {
#define NG_UDP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT)
#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
*
@ -93,13 +83,6 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
uint8_t *src, size_t src_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
*

54
sys/include/net/udp.h

@ -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_ */
/** @} */

4
sys/net/crosslayer/ng_pktdump/ng_pktdump.c

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

20
sys/net/transport_layer/ng_udp/ng_udp.c

@ -76,7 +76,7 @@ static uint16_t _calc_csum(ng_pktsnip_t *hdr, ng_pktsnip_t *pseudo_hdr,
payload = payload->next;
}
/* 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) {
#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)
{
ng_pktsnip_t *udp, *ipv6;
ng_udp_hdr_t *hdr;
udp_hdr_t *hdr;
uint32_t port;
/* mark UDP header */
@ -107,7 +107,7 @@ static void _receive(ng_pktsnip_t *pkt)
return;
}
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) {
DEBUG("udp: error marking UDP header, dropping packet\n");
ng_pktbuf_release(pkt);
@ -116,7 +116,7 @@ static void _receive(ng_pktsnip_t *pkt)
/* mark payload as Type: UNDEF */
pkt->type = NG_NETTYPE_UNDEF;
/* 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);
@ -141,7 +141,7 @@ static void _receive(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;
/* get udp snip and hdr */
@ -155,7 +155,7 @@ static void _send(ng_pktsnip_t *pkt)
ng_pktbuf_release(pkt);
return;
}
hdr = (ng_udp_hdr_t *)udp_snip->data;
hdr = (udp_hdr_t *)udp_snip->data;
/* fill in size field */
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) {
return -ENOENT;
}
((ng_udp_hdr_t *)hdr->data)->checksum = byteorder_htons(csum);
((udp_hdr_t *)hdr->data)->checksum = byteorder_htons(csum);
return 0;
}
@ -233,7 +233,7 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
uint8_t *dst, size_t dst_len)
{
ng_pktsnip_t *res;
ng_udp_hdr_t *hdr;
udp_hdr_t *hdr;
/* check parameters */
if (src == NULL || dst == NULL ||
@ -241,12 +241,12 @@ ng_pktsnip_t *ng_udp_hdr_build(ng_pktsnip_t *payload,
return NULL;
}
/* 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) {
return NULL;
}
/* initialize header */
hdr = (ng_udp_hdr_t *)res->data;
hdr = (udp_hdr_t *)res->data;
hdr->src_port = byteorder_htons(*((uint16_t *)src));
hdr->dst_port = byteorder_htons(*((uint16_t *)dst));
hdr->checksum = byteorder_htons(0);

1
sys/net/transport_layer/udp/Makefile

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

4
sys/net/transport_layer/ng_udp/ng_udp_hdr_print.c → sys/net/transport_layer/udp/udp_hdr_print.c

@ -21,9 +21,9 @@
#include <stdio.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",
byteorder_ntohs(hdr->src_port), byteorder_ntohs(hdr->dst_port));
Loading…
Cancel
Save