1 /** 2 * @file 3 * netconn API (to be used from non-TCPIP threads) 4 */ 5 6 /* 7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * This file is part of the lwIP TCP/IP stack. 33 * 34 * Author: Adam Dunkels <adam@sics.se> 35 * 36 */ 37 #ifndef LWIP_HDR_API_H 38 #define LWIP_HDR_API_H 39 40 #include "lwip/opt.h" 41 42 #if LWIP_NETCONN || LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */ 43 /* Note: Netconn API is always available when sockets are enabled - 44 * sockets are implemented on top of them */ 45 46 #include "lwip/arch.h" 47 #include "lwip/netbuf.h" 48 #include "lwip/sys.h" 49 #include "lwip/ip_addr.h" 50 #include "lwip/err.h" 51 52 #ifdef __cplusplus 53 extern "C" { 54 #endif 55 56 /* Throughout this file, IP addresses and port numbers are expected to be in 57 * the same byte order as in the corresponding pcb. 58 */ 59 60 /* Flags for netconn_write (u8_t) */ 61 #define NETCONN_NOFLAG 0x00 62 #define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */ 63 #define NETCONN_COPY 0x01 64 #define NETCONN_MORE 0x02 65 #define NETCONN_DONTBLOCK 0x04 66 #define NETCONN_NOAUTORCVD 0x08 /* prevent netconn_recv_data_tcp() from updating the tcp window - must be done manually via netconn_tcp_recvd() */ 67 68 /* Flags for struct netconn.flags (u8_t) */ 69 /** Should this netconn avoid blocking? */ 70 #define NETCONN_FLAG_NON_BLOCKING 0x02 71 /** Was the last connect action a non-blocking one? */ 72 #define NETCONN_FLAG_IN_NONBLOCKING_CONNECT 0x04 73 /** If a nonblocking write has been rejected before, poll_tcp needs to 74 check if the netconn is writable again */ 75 #define NETCONN_FLAG_CHECK_WRITESPACE 0x10 76 #if LWIP_IPV6 77 /** If this flag is set then only IPv6 communication is allowed on the 78 netconn. As per RFC#3493 this features defaults to OFF allowing 79 dual-stack usage by default. */ 80 #define NETCONN_FLAG_IPV6_V6ONLY 0x20 81 #endif /* LWIP_IPV6 */ 82 83 84 /* Helpers to process several netconn_types by the same code */ 85 #define NETCONNTYPE_GROUP(t) ((t)&0xF0) 86 #define NETCONNTYPE_DATAGRAM(t) ((t)&0xE0) 87 #if LWIP_IPV6 88 #define NETCONN_TYPE_IPV6 0x08 89 #define NETCONNTYPE_ISIPV6(t) (((t)&NETCONN_TYPE_IPV6) != 0) 90 #define NETCONNTYPE_ISUDPLITE(t) (((t)&0xF3) == NETCONN_UDPLITE) 91 #define NETCONNTYPE_ISUDPNOCHKSUM(t) (((t)&0xF3) == NETCONN_UDPNOCHKSUM) 92 #else /* LWIP_IPV6 */ 93 #define NETCONNTYPE_ISIPV6(t) (0) 94 #define NETCONNTYPE_ISUDPLITE(t) ((t) == NETCONN_UDPLITE) 95 #define NETCONNTYPE_ISUDPNOCHKSUM(t) ((t) == NETCONN_UDPNOCHKSUM) 96 #endif /* LWIP_IPV6 */ 97 98 /** @ingroup netconn_common 99 * Protocol family and type of the netconn 100 */ 101 enum netconn_type { 102 NETCONN_INVALID = 0, 103 /** TCP IPv4 */ 104 NETCONN_TCP = 0x10, 105 #if LWIP_IPV6 106 /** TCP IPv6 */ 107 NETCONN_TCP_IPV6 = NETCONN_TCP | NETCONN_TYPE_IPV6 /* 0x18 */, 108 #endif /* LWIP_IPV6 */ 109 /** UDP IPv4 */ 110 NETCONN_UDP = 0x20, 111 /** UDP IPv4 lite */ 112 NETCONN_UDPLITE = 0x21, 113 /** UDP IPv4 no checksum */ 114 NETCONN_UDPNOCHKSUM = 0x22, 115 116 #if LWIP_IPV6 117 /** UDP IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 118 NETCONN_UDP_IPV6 = NETCONN_UDP | NETCONN_TYPE_IPV6 /* 0x28 */, 119 /** UDP IPv6 lite (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 120 NETCONN_UDPLITE_IPV6 = NETCONN_UDPLITE | NETCONN_TYPE_IPV6 /* 0x29 */, 121 /** UDP IPv6 no checksum (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 122 NETCONN_UDPNOCHKSUM_IPV6 = NETCONN_UDPNOCHKSUM | NETCONN_TYPE_IPV6 /* 0x2a */, 123 #endif /* LWIP_IPV6 */ 124 125 /** Raw connection IPv4 */ 126 NETCONN_RAW = 0x40 127 #if LWIP_IPV6 128 /** Raw connection IPv6 (dual-stack by default, unless you call @ref netconn_set_ipv6only) */ 129 , NETCONN_RAW_IPV6 = NETCONN_RAW | NETCONN_TYPE_IPV6 /* 0x48 */ 130 #endif /* LWIP_IPV6 */ 131 }; 132 133 /** Current state of the netconn. Non-TCP netconns are always 134 * in state NETCONN_NONE! */ 135 enum netconn_state { 136 NETCONN_NONE, 137 NETCONN_WRITE, 138 NETCONN_LISTEN, 139 NETCONN_CONNECT, 140 NETCONN_CLOSE 141 }; 142 143 /** Used to inform the callback function about changes 144 * 145 * Event explanation: 146 * 147 * In the netconn implementation, there are three ways to block a client: 148 * 149 * - accept mbox (sys_arch_mbox_fetch(&conn->acceptmbox, &accept_ptr, 0); in netconn_accept()) 150 * - receive mbox (sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0); in netconn_recv_data()) 151 * - send queue is full (sys_arch_sem_wait(LWIP_API_MSG_SEM(msg), 0); in lwip_netconn_do_write()) 152 * 153 * The events have to be seen as events signaling the state of these mboxes/semaphores. For non-blocking 154 * connections, you need to know in advance whether a call to a netconn function call would block or not, 155 * and these events tell you about that. 156 * 157 * RCVPLUS events say: Safe to perform a potentially blocking call call once more. 158 * They are counted in sockets - three RCVPLUS events for accept mbox means you are safe 159 * to call netconn_accept 3 times without being blocked. 160 * Same thing for receive mbox. 161 * 162 * RCVMINUS events say: Your call to to a possibly blocking function is "acknowledged". 163 * Socket implementation decrements the counter. 164 * 165 * For TX, there is no need to count, its merely a flag. SENDPLUS means you may send something. 166 * SENDPLUS occurs when enough data was delivered to peer so netconn_send() can be called again. 167 * A SENDMINUS event occurs when the next call to a netconn_send() would be blocking. 168 */ 169 enum netconn_evt { 170 NETCONN_EVT_RCVPLUS, 171 NETCONN_EVT_RCVMINUS, 172 NETCONN_EVT_SENDPLUS, 173 NETCONN_EVT_SENDMINUS, 174 NETCONN_EVT_ERROR 175 }; 176 177 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 178 /** Used for netconn_join_leave_group() */ 179 enum netconn_igmp { 180 NETCONN_JOIN, 181 NETCONN_LEAVE 182 }; 183 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 184 185 #if LWIP_DNS 186 /* Used for netconn_gethostbyname_addrtype(), these should match the DNS_ADDRTYPE defines in dns.h */ 187 #define NETCONN_DNS_DEFAULT NETCONN_DNS_IPV4_IPV6 188 #define NETCONN_DNS_IPV4 0 189 #define NETCONN_DNS_IPV6 1 190 #define NETCONN_DNS_IPV4_IPV6 2 /* try to resolve IPv4 first, try IPv6 if IPv4 fails only */ 191 #define NETCONN_DNS_IPV6_IPV4 3 /* try to resolve IPv6 first, try IPv4 if IPv6 fails only */ 192 #endif /* LWIP_DNS */ 193 194 /* forward-declare some structs to avoid to include their headers */ 195 struct ip_pcb; 196 struct tcp_pcb; 197 struct udp_pcb; 198 struct raw_pcb; 199 struct netconn; 200 struct api_msg; 201 202 /** A callback prototype to inform about events for a netconn */ 203 typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len); 204 205 /** A netconn descriptor */ 206 struct netconn { 207 /** type of the netconn (TCP, UDP or RAW) */ 208 enum netconn_type type; 209 /** current state of the netconn */ 210 enum netconn_state state; 211 /** the lwIP internal protocol control block */ 212 union { 213 struct ip_pcb *ip; 214 struct tcp_pcb *tcp; 215 struct udp_pcb *udp; 216 struct raw_pcb *raw; 217 } pcb; 218 /** the last error this netconn had */ 219 err_t last_err; 220 #if !LWIP_NETCONN_SEM_PER_THREAD 221 /** sem that is used to synchronously execute functions in the core context */ 222 sys_sem_t op_completed; 223 #endif 224 /** mbox where received packets are stored until they are fetched 225 by the netconn application thread (can grow quite big) */ 226 sys_mbox_t recvmbox; 227 #if LWIP_TCP 228 /** mbox where new connections are stored until processed 229 by the application thread */ 230 sys_mbox_t acceptmbox; 231 #endif /* LWIP_TCP */ 232 /** only used for socket layer */ 233 #if LWIP_SOCKET 234 int socket; 235 #endif /* LWIP_SOCKET */ 236 #if LWIP_SO_SNDTIMEO 237 /** timeout to wait for sending data (which means enqueueing data for sending 238 in internal buffers) in milliseconds */ 239 s32_t send_timeout; 240 #endif /* LWIP_SO_RCVTIMEO */ 241 #if LWIP_SO_RCVTIMEO 242 /** timeout in milliseconds to wait for new data to be received 243 (or connections to arrive for listening netconns) */ 244 int recv_timeout; 245 #endif /* LWIP_SO_RCVTIMEO */ 246 #if LWIP_SO_RCVBUF 247 /** maximum amount of bytes queued in recvmbox 248 not used for TCP: adjust TCP_WND instead! */ 249 int recv_bufsize; 250 /** number of bytes currently in recvmbox to be received, 251 tested against recv_bufsize to limit bytes on recvmbox 252 for UDP and RAW, used for FIONREAD */ 253 int recv_avail; 254 #endif /* LWIP_SO_RCVBUF */ 255 #if LWIP_SO_LINGER 256 /** values <0 mean linger is disabled, values > 0 are seconds to linger */ 257 s16_t linger; 258 #endif /* LWIP_SO_LINGER */ 259 /** flags holding more netconn-internal state, see NETCONN_FLAG_* defines */ 260 u8_t flags; 261 #if LWIP_TCP 262 /** TCP: when data passed to netconn_write doesn't fit into the send buffer, 263 this temporarily stores the message. 264 Also used during connect and close. */ 265 struct api_msg *current_msg; 266 #endif /* LWIP_TCP */ 267 /** A callback function that is informed about events for this netconn */ 268 netconn_callback callback; 269 }; 270 271 struct netvector { 272 const void *ptr; 273 size_t len; 274 }; 275 276 /** Register an Network connection event */ 277 #define API_EVENT(c,e,l) if (c->callback) { \ 278 (*c->callback)(c, e, l); \ 279 } 280 281 /** Set conn->last_err to err but don't overwrite fatal errors */ 282 #define NETCONN_SET_SAFE_ERR(conn, err) do { if ((conn) != NULL) { \ 283 SYS_ARCH_DECL_PROTECT(netconn_set_safe_err_lev); \ 284 SYS_ARCH_PROTECT(netconn_set_safe_err_lev); \ 285 if (!ERR_IS_FATAL((conn)->last_err)) { \ 286 (conn)->last_err = err; \ 287 } \ 288 SYS_ARCH_UNPROTECT(netconn_set_safe_err_lev); \ 289 }} while(0); 290 291 /* Network connection functions: */ 292 293 /** @ingroup netconn_common 294 * Create new netconn connection 295 * @param t @ref netconn_type */ 296 #define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL) 297 #define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c) 298 struct netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, 299 netconn_callback callback); 300 err_t netconn_delete(struct netconn *conn); 301 /** Get the type of a netconn (as enum netconn_type). */ 302 #define netconn_type(conn) (conn->type) 303 304 err_t netconn_getaddr(struct netconn *conn, ip_addr_t *addr, 305 u16_t *port, u8_t local); 306 /** @ingroup netconn_common */ 307 #define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0) 308 /** @ingroup netconn_common */ 309 #define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1) 310 311 err_t netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port); 312 err_t netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port); 313 err_t netconn_disconnect (struct netconn *conn); 314 err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog); 315 /** @ingroup netconn_tcp */ 316 #define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG) 317 err_t netconn_accept(struct netconn *conn, struct netconn **new_conn); 318 err_t netconn_recv(struct netconn *conn, struct netbuf **new_buf); 319 err_t netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf); 320 err_t netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags); 321 err_t netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf); 322 err_t netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags); 323 err_t netconn_tcp_recvd(struct netconn *conn, u32_t len); 324 err_t netconn_sendto(struct netconn *conn, struct netbuf *buf, 325 const ip_addr_t *addr, u16_t port); 326 err_t netconn_send(struct netconn *conn, struct netbuf *buf); 327 err_t netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size, 328 u8_t apiflags, size_t *bytes_written); 329 err_t netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt, 330 u8_t apiflags, size_t *bytes_written); 331 /** @ingroup netconn_tcp */ 332 #define netconn_write(conn, dataptr, size, apiflags) \ 333 netconn_write_partly(conn, dataptr, size, apiflags, NULL) 334 err_t netconn_close(struct netconn *conn); 335 err_t netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx); 336 337 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 338 err_t netconn_join_leave_group(struct netconn *conn, const ip_addr_t *multiaddr, 339 const ip_addr_t *netif_addr, enum netconn_igmp join_or_leave); 340 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 341 #if LWIP_DNS 342 #if LWIP_IPV4 && LWIP_IPV6 343 err_t netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype); 344 #define netconn_gethostbyname(name, addr) netconn_gethostbyname_addrtype(name, addr, NETCONN_DNS_DEFAULT) 345 #else /* LWIP_IPV4 && LWIP_IPV6 */ 346 err_t netconn_gethostbyname(const char *name, ip_addr_t *addr); 347 #define netconn_gethostbyname_addrtype(name, addr, dns_addrtype) netconn_gethostbyname(name, addr) 348 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 349 #endif /* LWIP_DNS */ 350 351 #define netconn_err(conn) ((conn)->last_err) 352 #define netconn_recv_bufsize(conn) ((conn)->recv_bufsize) 353 354 /** Set the blocking status of netconn calls (@todo: write/send is missing) */ 355 #define netconn_set_nonblocking(conn, val) do { if(val) { \ 356 (conn)->flags |= NETCONN_FLAG_NON_BLOCKING; \ 357 } else { \ 358 (conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0) 359 /** Get the blocking status of netconn calls (@todo: write/send is missing) */ 360 #define netconn_is_nonblocking(conn) (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0) 361 362 #if LWIP_IPV6 363 /** @ingroup netconn_common 364 * TCP: Set the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY) 365 */ 366 #define netconn_set_ipv6only(conn, val) do { if(val) { \ 367 (conn)->flags |= NETCONN_FLAG_IPV6_V6ONLY; \ 368 } else { \ 369 (conn)->flags &= ~ NETCONN_FLAG_IPV6_V6ONLY; }} while(0) 370 /** @ingroup netconn_common 371 * TCP: Get the IPv6 ONLY status of netconn calls (see NETCONN_FLAG_IPV6_V6ONLY) 372 */ 373 #define netconn_get_ipv6only(conn) (((conn)->flags & NETCONN_FLAG_IPV6_V6ONLY) != 0) 374 #endif /* LWIP_IPV6 */ 375 376 #if LWIP_SO_SNDTIMEO 377 /** Set the send timeout in milliseconds */ 378 #define netconn_set_sendtimeout(conn, timeout) ((conn)->send_timeout = (timeout)) 379 /** Get the send timeout in milliseconds */ 380 #define netconn_get_sendtimeout(conn) ((conn)->send_timeout) 381 #endif /* LWIP_SO_SNDTIMEO */ 382 #if LWIP_SO_RCVTIMEO 383 /** Set the receive timeout in milliseconds */ 384 #define netconn_set_recvtimeout(conn, timeout) ((conn)->recv_timeout = (timeout)) 385 /** Get the receive timeout in milliseconds */ 386 #define netconn_get_recvtimeout(conn) ((conn)->recv_timeout) 387 #endif /* LWIP_SO_RCVTIMEO */ 388 #if LWIP_SO_RCVBUF 389 /** Set the receive buffer in bytes */ 390 #define netconn_set_recvbufsize(conn, recvbufsize) ((conn)->recv_bufsize = (recvbufsize)) 391 /** Get the receive buffer in bytes */ 392 #define netconn_get_recvbufsize(conn) ((conn)->recv_bufsize) 393 #endif /* LWIP_SO_RCVBUF*/ 394 395 #if LWIP_NETCONN_SEM_PER_THREAD 396 void netconn_thread_init(void); 397 void netconn_thread_cleanup(void); 398 #else /* LWIP_NETCONN_SEM_PER_THREAD */ 399 #define netconn_thread_init() 400 #define netconn_thread_cleanup() 401 #endif /* LWIP_NETCONN_SEM_PER_THREAD */ 402 403 #ifdef __cplusplus 404 } 405 #endif 406 407 #endif /* LWIP_NETCONN || LWIP_SOCKET */ 408 409 #endif /* LWIP_HDR_API_H */ 410