• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * @file
3  * UDP API (to be used from TCPIP thread)\n
4  * See also @ref udp_raw
5  */
6 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without modification,
12  * are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  *    this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  *    this list of conditions and the following disclaimer in the documentation
18  *    and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31  * OF SUCH DAMAGE.
32  *
33  * This file is part of the lwIP TCP/IP stack.
34  *
35  * Author: Adam Dunkels <adam@sics.se>
36  *
37  */
38 #ifndef LWIP_HDR_UDP_H
39 #define LWIP_HDR_UDP_H
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
44 
45 #include "lwip/pbuf.h"
46 #include "lwip/netif.h"
47 #include "lwip/ip_addr.h"
48 #include "lwip/ip.h"
49 #include "lwip/ip6_addr.h"
50 #include "lwip/prot/udp.h"
51 
52 #if defined (__cplusplus) && __cplusplus
53 extern "C" {
54 #endif
55 
56 #define UDP_FLAGS_NOCHKSUM       0x01U
57 #define UDP_FLAGS_UDPLITE        0x02U
58 #define UDP_FLAGS_CONNECTED      0x04U
59 #define UDP_FLAGS_MULTICAST_LOOP 0x08U
60 #define UDP_FLAGS_PEER_ADDR_SET  0x10U
61 
62 struct udp_pcb;
63 
64 /* Function prototype for udp pcb receive callback functions
65  * addr and port are in same byte order as in the pcb
66  * The callback is responsible for freeing the pbuf
67  * if it's not used any more.
68  *
69  * ATTENTION: Be aware that 'addr' might point into the pbuf 'p' so freeing this pbuf
70  *            can make 'addr' invalid, too.
71  *
72  * @param arg user supplied argument (udp_pcb.recv_arg)
73  * @param pcb the udp_pcb which received data
74  * @param p the packet buffer that was received
75  * @param addr the remote IP address from which the packet was received
76  * @param port the remote port from which the packet was received
77  */
78 typedef void (*udp_recv_fn)(void *arg, struct udp_pcb *pcb, struct pbuf *p,
79                             const ip_addr_t *addr, u16_t port);
80 
81 /* the UDP protocol control block */
82 struct udp_pcb {
83   /* Common members of all PCB types */
84   IP_PCB;
85 
86   /* Protocol specific PCB members */
87 
88   struct udp_pcb *next;
89 
90   u8_t flags;
91   /* ports are in host byte order */
92   u16_t local_port, remote_port;
93   u32_t   last_payload_len;
94 #if LWIP_MULTICAST_TX_OPTIONS
95 #if LWIP_IPV4
96   /** outgoing network interface for multicast packets, by IPv4 address (if not 'any') */
97   ip4_addr_t mcast_ip4;
98 #endif /* LWIP_IPV4 */
99   /** outgoing network interface for multicast packets, by interface index (if nonzero) */
100   u8_t mcast_ifindex;
101   /** TTL for outgoing multicast packets */
102   u8_t mcast_ttl;
103 #endif /* LWIP_MULTICAST_TX_OPTIONS */
104 
105 #if LWIP_UDPLITE
106   /* used for UDP_LITE only */
107   u16_t chksum_len_rx, chksum_len_tx;
108 #endif /* LWIP_UDPLITE */
109 
110   /* receive callback function */
111   udp_recv_fn recv;
112   /* user-supplied argument for the recv callback */
113   void *recv_arg;
114 
115 #if LWIP_SO_PRIORITY
116   prio_t priority;
117 #endif /* LWIP_SO_PRIORITY */
118 
119 #if LWIP_IPV6 && LWIP_MAC_SECURITY
120   u8_t macsec_reqd: 1;
121 #endif
122 };
123 /* udp_pcbs export for external reference (e.g. SNMP agent) */
124 extern struct udp_pcb *udp_pcbs;
125 
126 /* The following functions is the application layer interface to the
127    UDP code. */
128 struct udp_pcb  *udp_new          (void);
129 struct udp_pcb  *udp_new_ip_type  (u8_t type);
130 void             udp_remove       (struct udp_pcb *pcb);
131 err_t            udp_bind         (struct udp_pcb *pcb, const ip_addr_t *ipaddr,
132                                    u16_t port);
133 err_t            udp_connect      (struct udp_pcb *pcb, const ip_addr_t *ipaddr,
134                                    u16_t port);
135 void             udp_disconnect   (struct udp_pcb *pcb);
136 void             udp_recv         (struct udp_pcb *pcb, udp_recv_fn recv,
137                                    void *recv_arg);
138 err_t            udp_sendto_if    (struct udp_pcb *pcb, struct pbuf *p,
139                                    const ip_addr_t *dst_ip, u16_t dst_port,
140                                    struct netif *netif);
141 err_t            udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
142                                    const ip_addr_t *dst_ip, u16_t dst_port,
143                                    struct netif *netif, const ip_addr_t *src_ip);
144 err_t            udp_sendto       (struct udp_pcb *pcb, struct pbuf *p,
145                                    const ip_addr_t *dst_ip, u16_t dst_port);
146 err_t            udp_send         (struct udp_pcb *pcb, struct pbuf *p);
147 
148 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
149 err_t            udp_sendto_if_chksum    (struct udp_pcb *pcb, struct pbuf *p,
150                                           const ip_addr_t *dst_ip, u16_t dst_port,
151                                           struct netif *netif, u8_t have_chksum,
152                                           u16_t chksum);
153 err_t            udp_sendto_chksum       (struct udp_pcb *pcb, struct pbuf *p,
154                                           const ip_addr_t *dst_ip, u16_t dst_port,
155                                           u8_t have_chksum, u16_t chksum);
156 err_t            udp_send_chksum         (struct udp_pcb *pcb, struct pbuf *p,
157                                           u8_t have_chksum, u16_t chksum);
158 err_t            udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p,
159                                           const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif,
160                                           u8_t have_chksum, u16_t chksum, const ip_addr_t *src_ip);
161 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
162 
163 #define          udp_flags(pcb) ((pcb)->flags)
164 #define          udp_setflags(pcb, f)  ((pcb)->flags = (u8_t)(f))
165 
166 #if LWIP_SO_PRIORITY
167 #define udp_getpriority(pcb)  ((pcb)->priority)
168 #endif /* LWIP_SO_PRIORITY */
169 
170 /* The following functions are the lower layer interface to UDP. */
171 void             udp_input      (struct pbuf *p, struct netif *inp);
172 
173 void             udp_init       (void);
174 
175 /* for compatibility with older implementation */
176 #define udp_new_ip6() udp_new_ip_type(IPADDR_TYPE_V6)
177 
178 #if LWIP_MULTICAST_TX_OPTIONS
179 #if LWIP_IPV4
180 #define udp_set_multicast_netif_addr(pcb, ip4addr) ip4_addr_copy((pcb)->mcast_ip4, *(ip4addr))
181 #define udp_get_multicast_netif_addr(pcb)          (&(pcb)->mcast_ip4)
182 #endif /* LWIP_IPV4 */
183 #define udp_set_multicast_netif_index(pcb, idx)    ((pcb)->mcast_ifindex = (idx))
184 #define udp_get_multicast_netif_index(pcb)         ((pcb)->mcast_ifindex)
185 #define udp_set_multicast_ttl(pcb, value)          ((pcb)->mcast_ttl = (value))
186 #define udp_get_multicast_ttl(pcb)                 ((pcb)->mcast_ttl)
187 #endif /* LWIP_MULTICAST_TX_OPTIONS */
188 
189 #if UDP_DEBUG
190 void udp_debug_print(struct udp_hdr *udphdr);
191 #else
192 #define udp_debug_print(udphdr)
193 #endif
194 
195 void udp_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr);
196 
197 #if defined (__cplusplus) && __cplusplus
198 }
199 #endif
200 
201 #endif /* LWIP_UDP */
202 
203 #endif /* LWIP_HDR_UDP_H */
204