From a5e039cf4d5e3691d8ad91704cd89c63661597ca Mon Sep 17 00:00:00 2001
From: Martine Lenders <mlenders@inf.fu-berlin.de>
Date: Mon, 10 Aug 2015 03:16:53 +0200
Subject: [PATCH] udp: put UDP header in its own module

---
 Makefile.dep                                  |  1 +
 sys/Makefile                                  |  3 ++
 sys/include/net/ng_udp.h                      | 19 +------
 sys/include/net/udp.h                         | 54 +++++++++++++++++++
 sys/net/crosslayer/ng_pktdump/ng_pktdump.c    |  4 +-
 sys/net/transport_layer/ng_udp/ng_udp.c       | 20 +++----
 sys/net/transport_layer/udp/Makefile          |  1 +
 .../udp_hdr_print.c}                          |  4 +-
 8 files changed, 74 insertions(+), 32 deletions(-)
 create mode 100644 sys/include/net/udp.h
 create mode 100644 sys/net/transport_layer/udp/Makefile
 rename sys/net/transport_layer/{ng_udp/ng_udp_hdr_print.c => udp/udp_hdr_print.c} (92%)

diff --git a/Makefile.dep b/Makefile.dep
index 71c21be140..bb3c67e9f8 100644
--- a/Makefile.dep
+++ b/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)))
diff --git a/sys/Makefile b/sys/Makefile
index 2e616b9f49..97cf5c2d87 100644
--- a/sys/Makefile
+++ b/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})))
 
diff --git a/sys/include/net/ng_udp.h b/sys/include/net/ng_udp.h
index 0caa8352ee..259d642d11 100644
--- a/sys/include/net/ng_udp.h
+++ b/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
  *
diff --git a/sys/include/net/udp.h b/sys/include/net/udp.h
new file mode 100644
index 0000000000..636797ead2
--- /dev/null
+++ b/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_ */
+/** @} */
diff --git a/sys/net/crosslayer/ng_pktdump/ng_pktdump.c b/sys/net/crosslayer/ng_pktdump/ng_pktdump.c
index d52a36cd2d..1646db90cd 100644
--- a/sys/net/crosslayer/ng_pktdump/ng_pktdump.c
+++ b/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
diff --git a/sys/net/transport_layer/ng_udp/ng_udp.c b/sys/net/transport_layer/ng_udp/ng_udp.c
index f43f68e29e..620d8a5916 100644
--- a/sys/net/transport_layer/ng_udp/ng_udp.c
+++ b/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);
diff --git a/sys/net/transport_layer/udp/Makefile b/sys/net/transport_layer/udp/Makefile
new file mode 100644
index 0000000000..48422e909a
--- /dev/null
+++ b/sys/net/transport_layer/udp/Makefile
@@ -0,0 +1 @@
+include $(RIOTBASE)/Makefile.base
diff --git a/sys/net/transport_layer/ng_udp/ng_udp_hdr_print.c b/sys/net/transport_layer/udp/udp_hdr_print.c
similarity index 92%
rename from sys/net/transport_layer/ng_udp/ng_udp_hdr_print.c
rename to sys/net/transport_layer/udp/udp_hdr_print.c
index 1701038b08..757a00ba6e 100644
--- a/sys/net/transport_layer/ng_udp/ng_udp_hdr_print.c
+++ b/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));
-- 
GitLab