Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
api.h
00001 /** 00002 * @file 00003 * netconn API (to be used from non-TCPIP threads) 00004 */ 00005 00006 /* 00007 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00008 * All rights reserved. 00009 * 00010 * Redistribution and use in source and binary forms, with or without modification, 00011 * are permitted provided that the following conditions are met: 00012 * 00013 * 1. Redistributions of source code must retain the above copyright notice, 00014 * this list of conditions and the following disclaimer. 00015 * 2. Redistributions in binary form must reproduce the above copyright notice, 00016 * this list of conditions and the following disclaimer in the documentation 00017 * and/or other materials provided with the distribution. 00018 * 3. The name of the author may not be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00022 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00024 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00026 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00029 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00030 * OF SUCH DAMAGE. 00031 * 00032 * This file is part of the lwIP TCP/IP stack. 00033 * 00034 * Author: Adam Dunkels <adam@sics.se> 00035 * 00036 */ 00037 #ifndef LWIP_HDR_API_H 00038 #define LWIP_HDR_API_H 00039 00040 #include "lwip/opt.h" 00041 00042 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ 00043 /* Note: Netconn API is always available when sockets are enabled - 00044 * sockets are implemented on top of them */ 00045 00046 #include <stddef.h> /* for size_t */ 00047 00048 #include "lwip/netbuf.h" 00049 #include "lwip/sys.h" 00050 #include "lwip/ip_addr.h" 00051 #include "lwip/err.h" 00052 00053 #ifdef __cplusplus 00054 extern "C" { 00055 #endif 00056 00057 /* Throughout this file, IP addresses and port numbers are expected to be in 00058 * the same byte order as in the corresponding pcb. 00059 */ 00060 00061 /* Flags for netconn_write (u8_t) */ 00062 #define NETCONN_NOFLAG 0x00 00063 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */ 00064 #define NETCONN_COPY 0x01 00065 #define NETCONN_MORE 0x02 00066 #define NETCONN_DONTBLOCK 0x04 00067 00068 /* Flags for struct netconn.flags (u8_t) */ 00069 /** Should this netconn avoid blocking? */ 00070 #define NETCONN_FLAG_NON_BLOCKING 0x02 00071 /** Was the last connect action a non-blocking one? */ 00072 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04 00073 /** If a nonblocking write has been rejected before, poll_tcp needs to 00074 check if the netconn is writable again */ 00075 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10 00076 #if LWIP_IPV6 00077 /** If this flag is set then only IPv6 communication is allowed on the 00078 netconn. As per RFC#3493 this features defaults to OFF allowing 00079 dual-stack usage by default. */ 00080 #define NETCONN_FLAG_IPV6_V6ONLY 0x20 00081 #endif /* LWIP_IPV6 */ 00082 00083 00084 /* Helpers to process several netconn_types by the same code */ 00085 #define NETCONNTYPE_GROUP(t) ((t)&0xF0) 00086 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0) 00087 #if LWIP_IPV6 00088 #define NETCONN_TYPE_IPV6 0x08 00089 #define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0) 00090 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE) 00091 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM) 00092 #else /* LWIP_IPV6 */ 00093 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE) 00094 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM) 00095 #endif /* LWIP_IPV6 */ 00096 00097 /** @ingroup netconn_common 00098 * Protocol family and type of the netconn 00099 */ 00100 enum netconn_type { 00101 NETCONN_INVALID = 0, 00102 /** TCP IPv4 */ 00103 NETCONN_TCP = 0x10, 00104 #if LWIP_IPV6 00105 /** TCP IPv6 */ 00106 NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */, 00107 #endif /* LWIP_IPV6 */ 00108 /** UDP IPv4 */ 00109 NETCONN_UDP = 0x20, 00110 /** UDP IPv4 lite */ 00111 NETCONN_UDPLITE = 0x21, 00112 /** UDP IPv4 no checksum */ 00113 NETCONN_UDPNOCHKSUM = 0x22, 00114 00115 #if LWIP_IPV6 00116 /** UDP IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 00117 NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */, 00118 /** UDP IPv6 lite (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 00119 NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */, 00120 /** UDP IPv6 no checksum (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 00121 NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */, 00122 #endif /* LWIP_IPV6 */ 00123 00124 /** Raw connection IPv4 */ 00125 NETCONN_RAW = 0x40 00126 #if LWIP_IPV6 00127 /** Raw connection IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 00128 , NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */ 00129 #endif /* LWIP_IPV6 */ 00130 }; 00131 00132 /** Current state of the netconn. Non-TCP netconns are always 00133 * in state NETCONN_NONE! */ 00134 enum netconn_state { 00135 NETCONN_NONE, 00136 NETCONN_WRITE, 00137 NETCONN_LISTEN, 00138 NETCONN_CONNECT, 00139 NETCONN_CLOSE 00140 }; 00141 00142 /** Use to inform the callback function about changes */ 00143 enum netconn_evt { 00144 NETCONN_EVT_RCVPLUS, 00145 NETCONN_EVT_RCVMINUS, 00146 NETCONN_EVT_SENDPLUS, 00147 NETCONN_EVT_SENDMINUS, 00148 NETCONN_EVT_ERROR 00149 }; 00150 00151 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 00152 /** Used for netconn_join_leave_group() */ 00153 enum netconn_igmp { 00154 NETCONN_JOIN, 00155 NETCONN_LEAVE 00156 }; 00157 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 00158 00159 #if LWIP_DNS 00160 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */ 00161 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6 00162 #define NETCONN_DNS_IPV4 0 00163 #define NETCONN_DNS_IPV6 1 00164 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */ 00165 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */ 00166 #endif /* LWIP_DNS */ 00167 00168 /* forward-declare some structs to avoid to include their headers */ 00169 struct ip_pcb; 00170 struct tcp_pcb; 00171 struct udp_pcb; 00172 struct raw_pcb; 00173 struct netconn; 00174 struct api_msg; 00175 00176 /** A callback prototype to inform about events for a netconn */ 00177 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len); 00178 00179 /** A netconn descriptor */ 00180 struct netconn { 00181 /** type of the netconn (TCP, UDP or RAW) */ 00182 enum netconn_type type; 00183 /** current state of the netconn */ 00184 enum netconn_state state; 00185 /** the lwIP internal protocol control block */ 00186 union { 00187 struct ip_pcb *ip; 00188 struct tcp_pcb *tcp; 00189 struct udp_pcb *udp; 00190 struct raw_pcb *raw; 00191 } pcb; 00192 /** the last error this netconn had */ 00193 err_t last_err; 00194 #if !LWIP_NETCONN_SEM_PER_THREAD 00195 /** sem that is used to synchronously execute functions in the core context */ 00196 sys_sem_t op_completed; 00197 #endif 00198 /** mbox where received packets are stored until they are fetched 00199 by the netconn application thread (can grow quite big) */ 00200 sys_mbox_t recvmbox; 00201 #if LWIP_TCP 00202 /** mbox where new connections are stored until processed 00203 by the application thread */ 00204 sys_mbox_t acceptmbox; 00205 #endif /* LWIP_TCP */ 00206 /** only used for socket layer */ 00207 #if LWIP_SOCKET 00208 int socket; 00209 #endif /* LWIP_SOCKET */ 00210 #if LWIP_SO_SNDTIMEO 00211 /** timeout to wait for sending data (which means enqueueing data for sending 00212 in internal buffers) in milliseconds */ 00213 s32_t send_timeout; 00214 #endif /* LWIP_SO_RCVTIMEO */ 00215 #if LWIP_SO_RCVTIMEO 00216 /** timeout in milliseconds to wait for new data to be received 00217 (or connections to arrive for listening netconns) */ 00218 int recv_timeout; 00219 #endif /* LWIP_SO_RCVTIMEO */ 00220 #if LWIP_SO_RCVBUF 00221 /** maximum amount of bytes queued in recvmbox 00222 not used for TCP: adjust TCP_WND instead! */ 00223 int recv_bufsize; 00224 /** number of bytes currently in recvmbox to be received, 00225 tested against recv_bufsize to limit bytes on recvmbox 00226 for UDP and RAW, used for FIONREAD */ 00227 int recv_avail; 00228 #endif /* LWIP_SO_RCVBUF */ 00229 #if LWIP_SO_LINGER 00230 /** values <0 mean linger is disabled, values > 0 are seconds to linger */ 00231 s16_t linger; 00232 #endif /* LWIP_SO_LINGER */ 00233 /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */ 00234 u8_t flags; 00235 #if LWIP_TCP 00236 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 00237 this temporarily stores how much is already sent. */ 00238 size_t write_offset; 00239 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 00240 this temporarily stores the message. 00241 Also used during connect and close. */ 00242 struct api_msg *current_msg; 00243 #endif /* LWIP_TCP */ 00244 /** A callback function that is informed about events for this netconn */ 00245 netconn_callback callback; 00246 }; 00247 00248 /** Register an Network connection event */ 00249 #define API_EVENT(c,e,l) if (c->callback) { \ 00250 (*c->callback)(c, e, l); \ 00251 } 00252 00253 /** Set conn->last_err to err but don't overwrite fatal errors */ 00254 #define NETCONN_SET_SAFE_ERR(conn, err) do { if ((conn) != NULL) { \ 00255 SYS_ARCH_DECL_PROTECT(netconn_set_safe_err_lev); \ 00256 SYS_ARCH_PROTECT(netconn_set_safe_err_lev); \ 00257 if (!ERR_IS_FATAL((conn)->last_err)) { \ 00258 (conn)->last_err = err; \ 00259 } \ 00260 SYS_ARCH_UNPROTECT(netconn_set_safe_err_lev); \ 00261 }} while(0); 00262 00263 /* Network connection functions: */ 00264 00265 /** @ingroup netconn_common 00266 * Create new netconn connection 00267 * @param t @ref netconn_type */ 00268 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL) 00269 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c) 00270 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, 00271 netconn_callback callback); 00272 err_t netconn_delete(struct netconn *conn); 00273 /** Get the type of a netconn (as enum netconn_type). */ 00274 #define netconn_type(conn) (conn->type) 00275 00276 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr, 00277 u16_t *port, u8_t local); 00278 /** @ingroup netconn_common */ 00279 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0) 00280 /** @ingroup netconn_common */ 00281 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1) 00282 00283 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port); 00284 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port); 00285 err_t netconn_disconnect (struct netconn *conn); 00286 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog); 00287 /** @ingroup netconn_tcp */ 00288 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG) 00289 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn); 00290 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf); 00291 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf); 00292 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf, 00293 const ip_addr_t *addr, u16_t port); 00294 err_t netconn_send(struct netconn *conn, struct netbuf *buf); 00295 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size, 00296 u8_t apiflags, size_t *bytes_written); 00297 /** @ingroup netconn_tcp */ 00298 #define netconn_write(conn, dataptr, size, apiflags) \ 00299 netconn_write_partly(conn, dataptr, size, apiflags, NULL) 00300 err_t netconn_close(struct netconn *conn); 00301 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx); 00302 00303 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 00304 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr, 00305 const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave); 00306 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 00307 #if LWIP_DNS 00308 #if LWIP_IPV4 && LWIP_IPV6 00309 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype); 00310 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT) 00311 #else /* LWIP_IPV4 && LWIP_IPV6 */ 00312 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr); 00313 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr) 00314 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 00315 #endif /* LWIP_DNS */ 00316 00317 #define netconn_err(conn) ((conn)->last_err) 00318 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize) 00319 00320 /** Set the blocking status of netconn calls (@todo: write/send is missing) */ 00321 #define netconn_set_nonblocking(conn, val) do { if(val) { \ 00322 (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \ 00323 } else { \ 00324 (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0) 00325 /** Get the blocking status of netconn calls (@todo: write/send is missing) */ 00326 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0) 00327 00328 #if LWIP_IPV6 00329 /** @ingroup netconn_common 00330 * TCP: Set the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY) 00331 */ 00332 #define netconn_set_ipv6only(conn, val) do { if(val) { \ 00333 (conn)->flags |= NETCONN_FLAG_IPV6_V6ONLY; \ 00334 } else { \ 00335 (conn)->flags &= ~ NETCONN_FLAG_IPV6_V6ONLY; }} while(0) 00336 /** @ingroup netconn_common 00337 * TCP: Get the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY) 00338 */ 00339 #define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0) 00340 #endif /* LWIP_IPV6 */ 00341 00342 #if LWIP_SO_SNDTIMEO 00343 /** Set the send timeout in milliseconds */ 00344 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout)) 00345 /** Get the send timeout in milliseconds */ 00346 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout) 00347 #endif /* LWIP_SO_SNDTIMEO */ 00348 #if LWIP_SO_RCVTIMEO 00349 /** Set the receive timeout in milliseconds */ 00350 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout)) 00351 /** Get the receive timeout in milliseconds */ 00352 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout) 00353 #endif /* LWIP_SO_RCVTIMEO */ 00354 #if LWIP_SO_RCVBUF 00355 /** Set the receive buffer in bytes */ 00356 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize)) 00357 /** Get the receive buffer in bytes */ 00358 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize) 00359 #endif /* LWIP_SO_RCVBUF*/ 00360 00361 #if LWIP_NETCONN_SEM_PER_THREAD 00362 void netconn_thread_init(void); 00363 void netconn_thread_cleanup(void); 00364 #else /* LWIP_NETCONN_SEM_PER_THREAD */ 00365 #define netconn_thread_init() 00366 #define netconn_thread_cleanup() 00367 #endif /* LWIP_NETCONN_SEM_PER_THREAD */ 00368 00369 #ifdef __cplusplus 00370 } 00371 #endif 00372 00373 #endif /* LWIP_NETCONN || LWIP_SOCKET */ 00374 00375 #endif /* LWIP_HDR_API_H */
Generated on Tue Jul 12 2022 18:19:26 by
1.7.2