1 /** @file
2  @brief UDP utility functions
3  */
4 
5 /*
6  * Copyright (c) 2017 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #ifndef ZEPHYR_INCLUDE_NET_UDP_H_
12 #define ZEPHYR_INCLUDE_NET_UDP_H_
13 
14 #include <zephyr/types.h>
15 
16 #include <net/net_core.h>
17 #include <net/net_ip.h>
18 #include <net/net_pkt.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /* These APIs are mostly meant for Zephyr internal use so do not generate
25  * documentation for them.
26  */
27 /** @cond INTERNAL_HIDDEN */
28 
29 /**
30  * @brief UDP library
31  * @defgroup udp UDP Library
32  * @ingroup networking
33  * @{
34  */
35 
36 /**
37  * @brief Get UDP packet header data from net_pkt.
38  *
39  * @details The values in the returned header are in network byte order.
40  * Note that you must access the UDP header values by the returned pointer,
41  * the hdr parameter is just a placeholder for the header data and it might
42  * not contain anything if the header fits properly in the first fragment of
43  * the network packet.
44  *
45  * @param pkt Network packet
46  * @param hdr Where to place the header if it does not fit in first fragment
47  * of the network packet. This might not be populated if UDP header fits in
48  * net_buf fragment.
49  *
50  * @return Return pointer to header or NULL if something went wrong.
51  *         Always use the returned pointer to access the UDP header.
52  */
53 #if defined(CONFIG_NET_UDP)
54 struct net_udp_hdr *net_udp_get_hdr(struct net_pkt *pkt,
55 				    struct net_udp_hdr *hdr);
56 #else
57 static inline struct net_udp_hdr *net_udp_get_hdr(struct net_pkt *pkt,
58 						  struct net_udp_hdr *hdr)
59 {
60 	return NULL;
61 }
62 #endif /* CONFIG_NET_UDP */
63 
64 /**
65  * @brief Set UDP packet header data in net_pkt.
66  *
67  * @details The values in the header must be in network byte order.
68  * This function is normally called after a call to net_udp_get_hdr().
69  * The hdr parameter value should be the same that is returned by function
70  * net_udp_get_hdr() call. Note that if the UDP header fits in first net_pkt
71  * fragment, then this function will not do anything as your hdr parameter
72  * was pointing directly to net_pkt.
73  *
74  * @param pkt Network packet
75  * @param hdr Header data pointer that was returned by net_udp_get_hdr().
76  *
77  * @return Return pointer to header or NULL if something went wrong.
78  */
79 #if defined(CONFIG_NET_UDP)
80 struct net_udp_hdr *net_udp_set_hdr(struct net_pkt *pkt,
81 				    struct net_udp_hdr *hdr);
82 #else
net_udp_set_hdr(struct net_pkt * pkt,struct net_udp_hdr * hdr)83 static inline struct net_udp_hdr *net_udp_set_hdr(struct net_pkt *pkt,
84 						  struct net_udp_hdr *hdr)
85 {
86 	return NULL;
87 }
88 #endif /* CONFIG_NET_UDP */
89 
90 /**
91  * @}
92  */
93 
94 /** @endcond */
95 
96 #ifdef __cplusplus
97 }
98 #endif
99 
100 #endif /* ZEPHYR_INCLUDE_NET_UDP_H_ */
101