You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2024/04/11 14:23:39 UTC
(nuttx) 02/04: net/utils: Add net_ipv6_payload to get IPv6 L4 payload
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 676826cb7c52250692ba5ea626e0debb4d10d9e7
Author: Zhe Weng <we...@xiaomi.com>
AuthorDate: Mon Mar 18 18:22:50 2024 +0800
net/utils: Add net_ipv6_payload to get IPv6 L4 payload
Signed-off-by: Zhe Weng <we...@xiaomi.com>
---
net/utils/CMakeLists.txt | 2 +-
net/utils/Make.defs | 2 +-
net/utils/net_ipv6_payload.c | 74 ++++++++++++++++++++++++++++++++++++++++++++
net/utils/utils.h | 20 ++++++++++++
4 files changed, 96 insertions(+), 2 deletions(-)
diff --git a/net/utils/CMakeLists.txt b/net/utils/CMakeLists.txt
index 27868b2fcd..94dd7e70e4 100644
--- a/net/utils/CMakeLists.txt
+++ b/net/utils/CMakeLists.txt
@@ -37,7 +37,7 @@ set(SRCS
# IPv6 utilities
if(CONFIG_NET_IPv6)
- list(APPEND SRCS net_ipv6_maskcmp.c net_ipv6_pref2mask.c)
+ list(APPEND SRCS net_ipv6_maskcmp.c net_ipv6_pref2mask.c net_ipv6_payload.c)
endif()
# TCP utilities
diff --git a/net/utils/Make.defs b/net/utils/Make.defs
index 764dee1481..5039a07980 100644
--- a/net/utils/Make.defs
+++ b/net/utils/Make.defs
@@ -27,7 +27,7 @@ NET_CSRCS += net_cmsg.c net_iob_concat.c net_getrandom.c net_mask2pref.c
# IPv6 utilities
ifeq ($(CONFIG_NET_IPv6),y)
-NET_CSRCS += net_ipv6_maskcmp.c net_ipv6_pref2mask.c
+NET_CSRCS += net_ipv6_maskcmp.c net_ipv6_pref2mask.c net_ipv6_payload.c
endif
# TCP utilities
diff --git a/net/utils/net_ipv6_payload.c b/net/utils/net_ipv6_payload.c
new file mode 100644
index 0000000000..fc1c5d8962
--- /dev/null
+++ b/net/utils/net_ipv6_payload.c
@@ -0,0 +1,74 @@
+/****************************************************************************
+ * net/utils/net_ipv6_payload.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <nuttx/net/ip.h>
+#include <nuttx/net/ipv6ext.h>
+
+#ifdef CONFIG_NET_IPv6
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: net_ipv6_payload
+ *
+ * Description:
+ * Given a pointer to the IPv6 header, this function will return a pointer
+ * to the beginning of the L4 payload.
+ *
+ * Input Parameters:
+ * ipv6 - A pointer to the IPv6 header.
+ * proto - The location to return the protocol number in the IPv6 header.
+ *
+ * Returned Value:
+ * A pointer to the beginning of the payload.
+ *
+ ****************************************************************************/
+
+FAR void *net_ipv6_payload(FAR struct ipv6_hdr_s *ipv6, FAR uint8_t *proto)
+{
+ FAR struct ipv6_extension_s *exthdr;
+ FAR uint8_t *payload = (FAR uint8_t *)ipv6 + IPv6_HDRLEN;
+ uint8_t nxthdr = ipv6->proto;
+ uint16_t extlen;
+
+ while (ipv6_exthdr(nxthdr))
+ {
+ /* Just skip over the extension header */
+
+ exthdr = (FAR struct ipv6_extension_s *)payload;
+ extlen = EXTHDR_LEN(exthdr->len);
+
+ payload += extlen;
+ nxthdr = exthdr->nxthdr;
+ }
+
+ *proto = nxthdr;
+ return payload;
+}
+
+#endif /* CONFIG_NET_IPv6 */
diff --git a/net/utils/utils.h b/net/utils/utils.h
index 04fe60a442..b132f593f6 100644
--- a/net/utils/utils.h
+++ b/net/utils/utils.h
@@ -254,6 +254,26 @@ uint8_t net_ipv6_mask2pref(FAR const uint16_t *mask);
void net_ipv6_pref2mask(net_ipv6addr_t mask, uint8_t preflen);
#endif
+/****************************************************************************
+ * Name: net_ipv6_payload
+ *
+ * Description:
+ * Given a pointer to the IPv6 header, this function will return a pointer
+ * to the beginning of the L4 payload.
+ *
+ * Input Parameters:
+ * ipv6 - A pointer to the IPv6 header.
+ * proto - The location to return the protocol number in the IPv6 header.
+ *
+ * Returned Value:
+ * A pointer to the beginning of the payload.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NET_IPv6
+FAR void *net_ipv6_payload(FAR struct ipv6_hdr_s *ipv6, FAR uint8_t *proto);
+#endif
+
/****************************************************************************
* Name: net_iob_concat
*