Simon Cooksey / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers udp.h Source File

udp.h

Go to the documentation of this file.
00001 /**
00002  * @file
00003  * UDP API (to be used from TCPIP thread)\n
00004  * See also @ref udp_raw
00005  */
00006 
00007 /*
00008  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
00009  * All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without modification,
00012  * are permitted provided that the following conditions are met:
00013  *
00014  * 1. Redistributions of source code must retain the above copyright notice,
00015  *    this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright notice,
00017  *    this list of conditions and the following disclaimer in the documentation
00018  *    and/or other materials provided with the distribution.
00019  * 3. The name of the author may not be used to endorse or promote products
00020  *    derived from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
00023  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
00025  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00026  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00027  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
00030  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
00031  * OF SUCH DAMAGE.
00032  *
00033  * This file is part of the lwIP TCP/IP stack.
00034  *
00035  * Author: Adam Dunkels <adam@sics.se>
00036  *
00037  */
00038 #ifndef LWIP_HDR_UDP_H
00039 #define LWIP_HDR_UDP_H
00040 
00041 #include "lwip/opt.h"
00042 
00043 #if LWIP_UDP /* don't build if not configured for use in lwipopts.h */
00044 
00045 #include "lwip/pbuf.h"
00046 #include "lwip/netif.h"
00047 #include "lwip/ip_addr.h"
00048 #include "lwip/ip.h"
00049 #include "lwip/ip6_addr.h"
00050 
00051 #ifdef __cplusplus
00052 extern "C" {
00053 #endif
00054 
00055 #define UDP_HLEN 8
00056 
00057 /* Fields are (of course) in network byte order. */
00058 #ifdef PACK_STRUCT_USE_INCLUDES
00059 #  include "arch/bpstruct.h"
00060 #endif
00061 PACK_STRUCT_BEGIN
00062 struct udp_hdr {
00063   PACK_STRUCT_FIELD(u16_t src);
00064   PACK_STRUCT_FIELD(u16_t dest);  /* src/dest UDP ports */
00065   PACK_STRUCT_FIELD(u16_t len);
00066   PACK_STRUCT_FIELD(u16_t chksum);
00067 } PACK_STRUCT_STRUCT;
00068 PACK_STRUCT_END
00069 #ifdef PACK_STRUCT_USE_INCLUDES
00070 #  include "arch/epstruct.h"
00071 #endif
00072 
00073 #define UDP_FLAGS_NOCHKSUM       0x01U
00074 #define UDP_FLAGS_UDPLITE        0x02U
00075 #define UDP_FLAGS_CONNECTED      0x04U
00076 #define UDP_FLAGS_MULTICAST_LOOP 0x08U
00077 
00078 struct udp_pcb;
00079 
00080 /** Function prototype for udp pcb receive callback functions
00081  * addr and port are in same byte order as in the pcb
00082  * The callback is responsible for freeing the pbuf
00083  * if it's not used any more.
00084  *
00085  * ATTENTION: Be aware that 'addr' might point into the pbuf 'p' so freeing this pbuf
00086  *            can make 'addr' invalid, too.
00087  *
00088  * @param arg user supplied argument (udp_pcb.recv_arg)
00089  * @param pcb the udp_pcb which received data
00090  * @param p the packet buffer that was received
00091  * @param addr the remote IP address from which the packet was received
00092  * @param port the remote port from which the packet was received
00093  */
00094 typedef void (*udp_recv_fn)(void *arg, struct udp_pcb *pcb, struct pbuf *p,
00095     const ip_addr_t *addr, u16_t port);
00096 
00097 /** the UDP protocol control block */
00098 struct udp_pcb {
00099 /** Common members of all PCB types */
00100   IP_PCB;
00101 
00102 /* Protocol specific PCB members */
00103 
00104   struct udp_pcb *next;
00105 
00106   u8_t flags;
00107   /** ports are in host byte order */
00108   u16_t local_port, remote_port;
00109 
00110 #if LWIP_MULTICAST_TX_OPTIONS
00111   /** outgoing network interface for multicast packets */
00112   ip_addr_t multicast_ip;
00113   /** TTL for outgoing multicast packets */
00114   u8_t mcast_ttl;
00115 #endif /* LWIP_MULTICAST_TX_OPTIONS */
00116 
00117 #if LWIP_UDPLITE
00118   /** used for UDP_LITE only */
00119   u16_t chksum_len_rx, chksum_len_tx;
00120 #endif /* LWIP_UDPLITE */
00121 
00122   /** receive callback function */
00123   udp_recv_fn recv;
00124   /** user-supplied argument for the recv callback */
00125   void *recv_arg;
00126 };
00127 /* udp_pcbs export for external reference (e.g. SNMP agent) */
00128 extern struct udp_pcb *udp_pcbs;
00129 
00130 /* The following functions is the application layer interface to the
00131    UDP code. */
00132 struct udp_pcb * udp_new        (void);
00133 struct udp_pcb * udp_new_ip_type(u8_t type);
00134 void             udp_remove     (struct udp_pcb *pcb);
00135 err_t            udp_bind       (struct udp_pcb *pcb, const ip_addr_t *ipaddr,
00136                                  u16_t port);
00137 err_t            udp_connect    (struct udp_pcb *pcb, const ip_addr_t *ipaddr,
00138                                  u16_t port);
00139 void             udp_disconnect (struct udp_pcb *pcb);
00140 void             udp_recv       (struct udp_pcb *pcb, udp_recv_fn recv,
00141                                  void *recv_arg);
00142 err_t            udp_sendto_if  (struct udp_pcb *pcb, struct pbuf *p,
00143                                  const ip_addr_t *dst_ip, u16_t dst_port,
00144                                  struct netif *netif);
00145 err_t            udp_sendto_if_src(struct udp_pcb *pcb, struct pbuf *p,
00146                                  const ip_addr_t *dst_ip, u16_t dst_port,
00147                                  struct netif *netif, const ip_addr_t *src_ip);
00148 err_t            udp_sendto     (struct udp_pcb *pcb, struct pbuf *p,
00149                                  const ip_addr_t *dst_ip, u16_t dst_port);
00150 err_t            udp_send       (struct udp_pcb *pcb, struct pbuf *p);
00151 
00152 #if LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP
00153 err_t            udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p,
00154                                  const ip_addr_t *dst_ip, u16_t dst_port,
00155                                  struct netif *netif, u8_t have_chksum,
00156                                  u16_t chksum);
00157 err_t            udp_sendto_chksum(struct udp_pcb *pcb, struct pbuf *p,
00158                                  const ip_addr_t *dst_ip, u16_t dst_port,
00159                                  u8_t have_chksum, u16_t chksum);
00160 err_t            udp_send_chksum(struct udp_pcb *pcb, struct pbuf *p,
00161                                  u8_t have_chksum, u16_t chksum);
00162 err_t            udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p,
00163                                  const ip_addr_t *dst_ip, u16_t dst_port, struct netif *netif,
00164                                  u8_t have_chksum, u16_t chksum, const ip_addr_t *src_ip);
00165 #endif /* LWIP_CHECKSUM_ON_COPY && CHECKSUM_GEN_UDP */
00166 
00167 #define          udp_flags(pcb) ((pcb)->flags)
00168 #define          udp_setflags(pcb, f)  ((pcb)->flags = (f))
00169 
00170 /* The following functions are the lower layer interface to UDP. */
00171 void             udp_input      (struct pbuf *p, struct netif *inp);
00172 
00173 void             udp_init       (void);
00174 
00175 /* for compatibility with older implementation */
00176 #define udp_new_ip6() udp_new_ip_type(IPADDR_TYPE_V6)
00177 
00178 #if LWIP_MULTICAST_TX_OPTIONS
00179 #define udp_set_multicast_netif_addr(pcb, ip4addr) ip_addr_copy_from_ip4((pcb)->multicast_ip, *(ip4addr))
00180 #define udp_get_multicast_netif_addr(pcb)          ip_2_ip4(&(pcb)->multicast_ip)
00181 #define udp_set_multicast_ttl(pcb, value)      do { (pcb)->mcast_ttl = value; } while(0)
00182 #define udp_get_multicast_ttl(pcb)                 ((pcb)->mcast_ttl)
00183 #endif /* LWIP_MULTICAST_TX_OPTIONS */
00184 
00185 #if UDP_DEBUG
00186 void udp_debug_print(struct udp_hdr *udphdr);
00187 #else
00188 #define udp_debug_print(udphdr)
00189 #endif
00190 
00191 #if LWIP_IPV4
00192 void udp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* new_addr);
00193 #endif /* LWIP_IPV4 */
00194 
00195 #ifdef __cplusplus
00196 }
00197 #endif
00198 
00199 #endif /* LWIP_UDP */
00200 
00201 #endif /* LWIP_HDR_UDP_H */