1 /** 2 * @file 3 * netconn API lwIP internal implementations (do not use in application code) 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_MSG_H 38 #define LWIP_HDR_API_MSG_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 /* 44 * Note: Netconn API is always available when sockets are enabled - 45 * sockets are implemented on top of them 46 */ 47 #include "lwip/arch.h" 48 #include "lwip/ip_addr.h" 49 #include "lwip/err.h" 50 #include "lwip/sys.h" 51 #include "lwip/igmp.h" 52 #include "lwip/api.h" 53 #include "lwip/priv/tcpip_priv.h" 54 55 #if LWIP_DNS_REVERSE 56 #include "lwip/netdb.h" 57 #endif 58 59 #if defined (__cplusplus) && __cplusplus 60 extern "C" { 61 #endif 62 63 #if LWIP_MPU_COMPATIBLE 64 #if LWIP_NETCONN_SEM_PER_THREAD 65 #define API_MSG_M_DEF_SEM(m) *m 66 #else 67 #define API_MSG_M_DEF_SEM(m) API_MSG_M_DEF(m) 68 #endif 69 #else /* LWIP_MPU_COMPATIBLE */ 70 #define API_MSG_M_DEF_SEM(m) API_MSG_M_DEF(m) 71 #endif /* LWIP_MPU_COMPATIBLE */ 72 73 /* For the netconn API, these values are use as a bitmask! */ 74 #define NETCONN_SHUT_RD 1 75 #define NETCONN_SHUT_WR 2 76 #define NETCONN_SHUT_RDWR (NETCONN_SHUT_RD | NETCONN_SHUT_WR) 77 78 /* RST is received and is pending to be handled */ 79 #define NETCONN_PENDING_ERR_RST 1 80 /* Close event is pending to be handled in refused data (when recvmbox is full) */ 81 #define NETCONN_PENDING_ERR_CLSD 2 82 /* RST is ready to be handled after NETCONN_PENDING_ERR_CLSD */ 83 #define NETCONN_READY_ERR_RST 3 84 /* FIN and RST received in the order, yet to be processed */ 85 #define NETCONN_PENDING_ERR_FIN_RST 4 86 87 #if LWIP_TCP 88 extern u8_t netconn_reset; 89 #endif 90 91 #if PF_PKT_SUPPORT 92 /* Packet Types */ 93 #define PACKET_HOST 0 /* To us */ 94 #define PACKET_BROADCAST 1 /* To all */ 95 #define PACKET_MULTICAST 2 /* To group */ 96 #define PACKET_OTHERHOST 3 /* To someone else */ 97 #define PACKET_OUTGOING 4 98 #endif 99 100 /* 101 * IP addresses and port numbers are expected to be in 102 * the same byte order as in the corresponding pcb. 103 */ 104 /** This struct includes everything that is necessary to execute a function 105 for a netconn in another thread context (mainly used to process netconns 106 in the tcpip_thread context to be thread safe). */ 107 struct api_msg { 108 /** The netconn which to process - always needed: it includes the semaphore 109 which is used to block the application thread until the function finished. */ 110 struct netconn *conn; 111 /** The return value of the function executed in tcpip_thread. */ 112 err_t err; 113 /** Depending on the executed function, one of these union members is used */ 114 union { 115 struct tcpip_conn *conn_info; 116 /** used for lwip_netconn_do_send */ 117 struct netbuf *b; 118 /** used for lwip_netconn_do_newconn */ 119 struct { 120 #if PF_PKT_SUPPORT 121 u16_t proto; 122 #else 123 u8_t proto; 124 #endif 125 } n; 126 /** used for lwip_netconn_do_bind and lwip_netconn_do_connect */ 127 struct { 128 API_MSG_M_DEF_C(ip_addr_t, ipaddr); 129 u16_t port; 130 #if PF_PKT_SUPPORT 131 u8_t netifindex; 132 #endif 133 } bc; 134 /** used for lwip_netconn_do_getaddr */ 135 struct { 136 ip_addr_t API_MSG_M_DEF(ipaddr); 137 u16_t API_MSG_M_DEF(port); 138 u8_t local; 139 } ad; 140 /** used for lwip_netconn_do_write */ 141 struct { 142 const void *dataptr; 143 size_t len; 144 u8_t apiflags; 145 #if LWIP_SO_SNDTIMEO 146 u32_t time_started; 147 #endif /* LWIP_SO_SNDTIMEO */ 148 } w; 149 /** used for lwip_netconn_do_recv */ 150 struct { 151 u32_t len; 152 } r; 153 #if LWIP_TCP 154 /** used for lwip_netconn_do_close (/shutdown) */ 155 struct { 156 u8_t shut; 157 #if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER 158 u32_t time_started; 159 #else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ 160 u8_t polls_left; 161 #endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */ 162 } sd; 163 #endif /* LWIP_TCP */ 164 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 165 /* used for lwip_netconn_do_leave_group */ 166 struct { 167 API_MSG_M_DEF_C(ip_addr_t, multiaddr); 168 API_MSG_M_DEF_C(ip_addr_t, netif_addr); 169 u8_t if_idx; 170 enum netconn_igmp join_or_leave; 171 } jl; 172 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 173 #if TCP_LISTEN_BACKLOG 174 struct { 175 u8_t backlog; 176 } lb; 177 #endif /* TCP_LISTEN_BACKLOG */ 178 } msg; 179 #if LWIP_NETCONN_SEM_PER_THREAD 180 sys_sem_t *op_completed_sem; 181 #endif /* LWIP_NETCONN_SEM_PER_THREAD */ 182 }; 183 184 #if LWIP_NETCONN_SEM_PER_THREAD 185 #define LWIP_API_MSG_SEM(msg) ((msg)->op_completed_sem) 186 #else /* LWIP_NETCONN_SEM_PER_THREAD */ 187 #define LWIP_API_MSG_SEM(msg) (&(msg)->conn->op_completed) 188 #endif /* LWIP_NETCONN_SEM_PER_THREAD */ 189 190 191 #if LWIP_DNS 192 /** As lwip_netconn_do_gethostbyname requires more arguments but doesn't require a netconn, 193 it has its own struct (to avoid struct api_msg getting bigger than necessary). 194 lwip_netconn_do_gethostbyname must be called using tcpip_callback instead of tcpip_apimsg 195 (see netconn_gethostbyname). */ 196 struct dns_api_msg { 197 /** Hostname to query or dotted IP address string */ 198 #if LWIP_MPU_COMPATIBLE 199 char name[DNS_MAX_NAME_LENGTH]; 200 #else /* LWIP_MPU_COMPATIBLE */ 201 const char *name; 202 #endif /* LWIP_MPU_COMPATIBLE */ 203 /** The resolved address is stored here */ 204 ip_addr_t API_MSG_M_DEF(addr); 205 #if LWIP_IPV4 && LWIP_IPV6 206 /** Type of resolve call */ 207 u8_t dns_addrtype; 208 #endif /* LWIP_IPV4 && LWIP_IPV6 */ 209 /** This semaphore is posted when the name is resolved, the application thread 210 should wait on it. */ 211 sys_sem_t API_MSG_M_DEF_SEM(sem); 212 /** Errors are given back here */ 213 err_t API_MSG_M_DEF(err); 214 /* to check how many IPaddress are updated */ 215 u32_t *count; 216 }; 217 218 #if LWIP_DNS_REVERSE 219 struct reverse_dns_api_msg { 220 /* The IP Address to be resolved to hostname via reverse DNS */ 221 ip_addr_t API_MSG_M_DEF(addr); 222 /* Resolved hostname of addr */ 223 #if LWIP_MPU_COMPATIBLE 224 char hostname[NI_MAXHOST]; 225 #else /* LWIP_MPU_COMPATIBLE */ 226 char *hostname; 227 #endif /* LWIP_MPU_COMPATIBLE */ 228 /* This semaphore is posted when the name is resolved, the application thread should wait on it. */ 229 sys_sem_t API_MSG_M_DEF_SEM(sem); 230 /* Errors are given back here */ 231 err_t API_MSG_M_DEF(err); 232 }; 233 #endif /* LWIP_DNS_REVERSE */ 234 #endif /* LWIP_DNS */ 235 236 #if LWIP_TCP 237 extern u8_t netconn_aborted; 238 extern u8_t netconn_memory_err; 239 240 #endif /* LWIP_TCP */ 241 242 #if LWIP_LOWPOWER 243 /* check wether need to poll tcp */ 244 u8_t poll_tcp_needed(void *arg, struct tcp_pcb *pcb); 245 #endif 246 247 void lwip_netconn_do_newconn (void *m); 248 void lwip_netconn_do_delconn (void *m); 249 void lwip_netconn_do_bind (void *m); 250 void lwip_netconn_do_connect (void *m); 251 void lwip_netconn_do_disconnect (void *m); 252 void lwip_netconn_do_listen (void *m); 253 void lwip_netconn_do_send (void *m); 254 void lwip_netconn_do_recv (void *m); 255 #if TCP_LISTEN_BACKLOG 256 void lwip_netconn_do_accepted (void *m); 257 #endif /* TCP_LISTEN_BACKLOG */ 258 void lwip_netconn_do_write (void *m); 259 void lwip_netconn_do_getaddr (void *m); 260 void lwip_netconn_do_close (void *m); 261 void lwip_netconn_do_shutdown (void *m); 262 #ifdef LWIP_GET_CONN_INFO 263 void do_getconninfo (void *m); 264 #endif /* LWIP_GET_CONN_INFO */ 265 266 /* Internal functions */ 267 #if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) 268 void lwip_netconn_do_leave_group(void *m); 269 270 #endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */ 271 272 #if LWIP_DNS 273 void lwip_netconn_do_gethostbyname(void *arg); 274 #if LWIP_DNS_REVERSE 275 void lwip_netconn_do_getnamebyhost(void *arg); 276 #endif /* LWIP_DNS_REVERSE */ 277 #endif /* LWIP_DNS */ 278 279 struct netconn *netconn_alloc(enum netconn_type t, netconn_callback callback); 280 void netconn_free(struct netconn *conn); 281 282 #if LWIP_UDP 283 void recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p, 284 const ip_addr_t *addr, u16_t port); 285 #endif 286 #if DRIVER_STATUS_CHECK 287 void update_tcp_sndplus_event(void *arg, struct tcp_pcb *pcb); 288 #endif 289 #if defined (__cplusplus) && __cplusplus 290 } 291 #endif 292 293 #endif /* LWIP_NETCONN || LWIP_SOCKET */ 294 295 #endif /* LWIP_HDR_API_MSG_H */ 296