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 #ifdef __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 61 struct udp_pcb; 62 63 /** Function prototype for udp pcb receive callback functions 64 * addr and port are in same byte order as in the pcb 65 * The callback is responsible for freeing the pbuf 66 * if it's not used any more. 67 * 68 * ATTENTION: Be aware that 'addr' might point into the pbuf 'p' so freeing this pbuf 69 * can make 'addr' invalid, too. 70 * 71 * @param arg user supplied argument (udp_pcb.recv_arg) 72 * @param pcb the udp_pcb which received data 73 * @param p the packet buffer that was received 74 * @param addr the remote IP address from which the packet was received 75 * @param port the remote port from which the packet was received 76 */ 77 typedef void (*udp_recv_fn)(void *arg, struct udp_pcb *pcb, struct pbuf *p, 78 const ip_addr_t *addr, u16_t port); 79 80 /** the UDP protocol control block */ 81 struct udp_pcb { 82 /** Common members of all PCB types */ 83 IP_PCB; 84 85 /* Protocol specific PCB members */ 86 87 struct udp_pcb *next; 88 89 u8_t flags; 90 /** ports are in host byte order */ 91 u16_t local_port, remote_port; 92 93 #if LWIP_MULTICAST_TX_OPTIONS 94 #if LWIP_IPV4 95 /** outgoing network interface for multicast packets, by IPv4 address (if not 'any') */ 96 ip4_addr_t mcast_ip4; 97 #endif /* LWIP_IPV4 */ 98 /** outgoing network interface for multicast packets, by interface index (if nonzero) */ 99 u8_t mcast_ifindex; 100 /** TTL for outgoing multicast packets */ 101 u8_t mcast_ttl; 102 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 103 104 #if LWIP_UDPLITE 105 /** used for UDP_LITE only */ 106 u16_t chksum_len_rx, chksum_len_tx; 107 #endif /* LWIP_UDPLITE */ 108 109 /** receive callback function */ 110 udp_recv_fn recv; 111 /** user-supplied argument for the recv callback */ 112 void *recv_arg; 113 }; 114 /* udp_pcbs export for external reference (e.g. SNMP agent) */ 115 extern struct udp_pcb *udp_pcbs; 116 117 /* The following functions is the application layer interface to the 118 UDP code. */ 119 #ifdef LOSCFG_NET_CONTAINER 120 void set_udp_pcb_net_group(struct udp_pcb *pcb, struct net_group *group); 121 struct net_group *get_net_group_from_udp_pcb(struct udp_pcb *pcb); 122 #endif 123 struct udp_pcb * udp_new (void); 124 struct udp_pcb * udp_new_ip_type(u8_t type); 125 void udp_remove (struct udp_pcb *pcb); 126 err_t udp_bind (struct udp_pcb *pcb, const ip_addr_t *ipaddr, 127 u16_t port); 128 void udp_bind_netif (struct udp_pcb *pcb, const struct netif* netif); 129 err_t udp_connect (struct udp_pcb *pcb, const ip_addr_t *ipaddr, 130 u16_t port); 131 void udp_disconnect (struct udp_pcb *pcb); 132 void udp_recv (struct udp_pcb *pcb, udp_recv_fn recv, 133 void *recv_arg); 134 err_t udp_sendto_if (struct udp_pcb *pcb, struct pbuf *p, 135 const ip_addr_t *dst_ip, u16_t dst_port, 136 struct netif *netif); 137 err_t udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p, 138 const ip_addr_t *dst_ip, u16_t dst_port, 139 struct netif *netif, const ip_addr_t *src_ip); 140 err_t udp_sendto (struct udp_pcb *pcb, struct pbuf *p, 141 const ip_addr_t *dst_ip, u16_t dst_port); 142 err_t udp_send (struct udp_pcb *pcb, struct pbuf *p); 143 144 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP 145 err_t udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, 146 const ip_addr_t *dst_ip, u16_t dst_port, 147 struct netif *netif, u8_t have_chksum, 148 u16_t chksum); 149 err_t udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p, 150 const ip_addr_t *dst_ip, u16_t dst_port, 151 u8_t have_chksum, u16_t chksum); 152 err_t udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p, 153 u8_t have_chksum, u16_t chksum); 154 err_t udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, 155 const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif, 156 u8_t have_chksum, u16_t chksum, const ip_addr_t *src_ip); 157 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */ 158 159 #define udp_flags(pcb) ((pcb)->flags) 160 #define udp_setflags(pcb, f) ((pcb)->flags = (f)) 161 162 #define udp_set_flags(pcb, set_flags) do { (pcb)->flags = (u8_t)((pcb)->flags | (set_flags)); } while(0) 163 #define udp_clear_flags(pcb, clr_flags) do { (pcb)->flags = (u8_t)((pcb)->flags & (u8_t)(~(clr_flags) & 0xff)); } while(0) 164 #define udp_is_flag_set(pcb, flag) (((pcb)->flags & (flag)) != 0) 165 166 /* The following functions are the lower layer interface to UDP. */ 167 void udp_input (struct pbuf *p, struct netif *inp); 168 169 void udp_init (void); 170 171 /* for compatibility with older implementation */ 172 #define udp_new_ip6() udp_new_ip_type(IPADDR_TYPE_V6) 173 174 #if LWIP_MULTICAST_TX_OPTIONS 175 #if LWIP_IPV4 176 #define udp_set_multicast_netif_addr(pcb, ip4addr) ip4_addr_copy((pcb)->mcast_ip4, *(ip4addr)) 177 #define udp_get_multicast_netif_addr(pcb) (&(pcb)->mcast_ip4) 178 #endif /* LWIP_IPV4 */ 179 #define udp_set_multicast_netif_index(pcb, idx) ((pcb)->mcast_ifindex = (idx)) 180 #define udp_get_multicast_netif_index(pcb) ((pcb)->mcast_ifindex) 181 #define udp_set_multicast_ttl(pcb, value) ((pcb)->mcast_ttl = (value)) 182 #define udp_get_multicast_ttl(pcb) ((pcb)->mcast_ttl) 183 #endif /* LWIP_MULTICAST_TX_OPTIONS */ 184 185 #if UDP_DEBUG 186 void udp_debug_print(struct udp_hdr *udphdr); 187 #else 188 #define udp_debug_print(udphdr) 189 #endif 190 191 void udp_netif_ip_addr_changed(const ip_addr_t* old_addr, const ip_addr_t* new_addr); 192 193 #ifdef __cplusplus 194 } 195 #endif 196 197 #endif /* LWIP_UDP */ 198 199 #endif /* LWIP_HDR_UDP_H */ 200