1 /** @file
2  @brief UDP data handler
3 
4  This is not to be included by the application and is only used by
5  core IP stack.
6  */
7 
8 /*
9  * Copyright (c) 2017 Intel Corporation
10  *
11  * SPDX-License-Identifier: Apache-2.0
12  */
13 
14 #ifndef __UDP_INTERNAL_H
15 #define __UDP_INTERNAL_H
16 
17 #include <zephyr/types.h>
18 
19 #include <zephyr/net/net_core.h>
20 #include <zephyr/net/net_ip.h>
21 #include <zephyr/net/net_pkt.h>
22 #include <zephyr/net/net_context.h>
23 
24 #include "connection.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31  * @brief Create UDP packet into net_pkt
32  *
33  * Note: pkt's cursor should be set a the right position.
34  *       (i.e. after IP header)
35  *
36  * @param pkt Network packet
37  * @param src_port Destination port in network byte order.
38  * @param dst_port Destination port in network byte order.
39  *
40  * @return 0 on success, negative errno otherwise.
41  */
42 #if defined(CONFIG_NET_NATIVE_UDP)
43 int net_udp_create(struct net_pkt *pkt, uint16_t src_port, uint16_t dst_port);
44 #else
45 static inline int net_udp_create(struct net_pkt *pkt,
46 				 uint16_t src_port, uint16_t dst_port)
47 {
48 	ARG_UNUSED(pkt);
49 	ARG_UNUSED(src_port);
50 	ARG_UNUSED(dst_port);
51 
52 	return 0;
53 }
54 #endif
55 
56 /**
57  * @brief Finalize UDP packet
58  *
59  * Note: calculates final length and setting up the checksum.
60  *
61  * @param pkt Network packet
62  *
63  * @return 0 on success, negative errno otherwise.
64  */
65 #if defined(CONFIG_NET_NATIVE_UDP)
66 int net_udp_finalize(struct net_pkt *pkt, bool force_chksum);
67 #else
net_udp_finalize(struct net_pkt * pkt,bool force_chksum)68 static inline int net_udp_finalize(struct net_pkt *pkt, bool force_chksum)
69 {
70 	ARG_UNUSED(pkt);
71 	ARG_UNUSED(force_chksum);
72 
73 	return 0;
74 }
75 #endif
76 
77 /**
78  * @brief Get pointer to UDP header in net_pkt
79  *
80  * @param pkt Network packet
81  * @param udp_access Helper variable for accessing UDP header
82  *
83  * @return UDP header on success, NULL on error
84  */
85 #if defined(CONFIG_NET_NATIVE_UDP)
86 struct net_udp_hdr *net_udp_input(struct net_pkt *pkt,
87 				  struct net_pkt_data_access *udp_access);
88 #else
89 static inline
net_udp_input(struct net_pkt * pkt,struct net_pkt_data_access * udp_access)90 struct net_udp_hdr *net_udp_input(struct net_pkt *pkt,
91 				  struct net_pkt_data_access *udp_access)
92 {
93 	ARG_UNUSED(pkt);
94 	ARG_UNUSED(udp_access);
95 
96 	return NULL;
97 }
98 #endif
99 
100 /**
101  * @brief Register a callback to be called when UDP packet
102  * is received corresponding to received packet.
103  *
104  * @param family Protocol family
105  * @param remote_addr Remote address of the connection end point.
106  * @param local_addr Local address of the connection end point.
107  * @param remote_port Remote port of the connection end point.
108  * @param local_port Local port of the connection end point.
109  * @param context net_context structure related to the connection.
110  * @param cb Callback to be called
111  * @param user_data User data supplied by caller.
112  * @param handle UDP handle that can be used when unregistering
113  *
114  * @return Return 0 if the registration succeed, <0 otherwise.
115  */
116 int net_udp_register(uint8_t family,
117 		     const struct sockaddr *remote_addr,
118 		     const struct sockaddr *local_addr,
119 		     uint16_t remote_port,
120 		     uint16_t local_port,
121 		     struct net_context *context,
122 		     net_conn_cb_t cb,
123 		     void *user_data,
124 		     struct net_conn_handle **handle);
125 
126 /**
127  * @brief Unregister UDP handler.
128  *
129  * @param handle Handle from registering.
130  *
131  * @return Return 0 if the unregistration succeed, <0 otherwise.
132  */
133 int net_udp_unregister(struct net_conn_handle *handle);
134 
135 #ifdef __cplusplus
136 }
137 #endif
138 
139 #endif /* __UDP_INTERNAL_H */
140