xref: /nrf52832-nimble/rt-thread/components/net/lwip-2.1.0/src/include/lwip/priv/sockets_priv.h (revision 104654410c56c573564690304ae786df310c91fc)
1 /**
2  * @file
3  * Sockets API internal implementations (do not use in application code)
4  */
5 
6 /*
7  * Copyright (c) 2017 Joel Cunningham, Garmin International, Inc. <[email protected]>
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: Joel Cunningham <[email protected]>
35  *
36  */
37 #ifndef LWIP_HDR_SOCKETS_PRIV_H
38 #define LWIP_HDR_SOCKETS_PRIV_H
39 
40 #include "lwip/opt.h"
41 
42 #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
43 
44 #include "lwip/err.h"
45 #include "lwip/sockets.h"
46 #include "lwip/sys.h"
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 #define NUM_SOCKETS MEMP_NUM_NETCONN
53 
54 /** This is overridable for the rare case where more than 255 threads
55  * select on the same socket...
56  */
57 #ifndef SELWAIT_T
58 #define SELWAIT_T u8_t
59 #endif
60 
61 union lwip_sock_lastdata {
62   struct netbuf *netbuf;
63   struct pbuf *pbuf;
64 };
65 
66 #include <rtthread.h>
67 #ifdef SAL_USING_POSIX
68 #include <ipc/waitqueue.h>
69 #endif
70 
71 /** Contains all internal pointers and states used for a socket */
72 struct lwip_sock {
73   /** sockets currently are built on netconns, each socket has one netconn */
74   struct netconn *conn;
75   /** data that was left from the previous read */
76   union lwip_sock_lastdata lastdata;
77 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
78   /** number of times data was received, set by event_callback(),
79       tested by the receive and select functions */
80   s16_t rcvevent;
81   /** number of times data was ACKed (free send buffer), set by event_callback(),
82       tested by select */
83   u16_t sendevent;
84   /** error happened for this socket, set by event_callback(), tested by select */
85   u16_t errevent;
86   /** counter of how many threads are waiting for this socket using select */
87   SELWAIT_T select_waiting;
88 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
89 #if LWIP_NETCONN_FULLDUPLEX
90   /* counter of how many threads are using a struct lwip_sock (not the 'int') */
91   u8_t fd_used;
92   /* status of pending close/delete actions */
93   u8_t fd_free_pending;
94 #define LWIP_SOCK_FD_FREE_TCP  1
95 #define LWIP_SOCK_FD_FREE_FREE 2
96 #endif
97 
98 #ifdef SAL_USING_POSIX
99   rt_wqueue_t wait_head;
100 #endif
101 };
102 
103 #ifndef set_errno
104 #define set_errno(err) do { if (err) { errno = (err); } } while(0)
105 #endif
106 
107 #if !LWIP_TCPIP_CORE_LOCKING
108 /** Maximum optlen used by setsockopt/getsockopt */
109 #define LWIP_SETGETSOCKOPT_MAXOPTLEN LWIP_MAX(16, sizeof(struct ifreq))
110 
111 /** This struct is used to pass data to the set/getsockopt_internal
112  * functions running in tcpip_thread context (only a void* is allowed) */
113 struct lwip_setgetsockopt_data {
114   /** socket index for which to change options */
115   int s;
116   /** level of the option to process */
117   int level;
118   /** name of the option to process */
119   int optname;
120   /** set: value to set the option to
121     * get: value of the option is stored here */
122 #if LWIP_MPU_COMPATIBLE
123   u8_t optval[LWIP_SETGETSOCKOPT_MAXOPTLEN];
124 #else
125   union {
126     void *p;
127     const void *pc;
128   } optval;
129 #endif
130   /** size of *optval */
131   socklen_t optlen;
132   /** if an error occurs, it is temporarily stored here */
133   int err;
134   /** semaphore to wake up the calling task */
135   void* completed_sem;
136 };
137 #endif /* !LWIP_TCPIP_CORE_LOCKING */
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 struct lwip_sock* lwip_socket_dbg_get_socket(int fd);
144 
145 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
146 
147 #if LWIP_NETCONN_SEM_PER_THREAD
148 #define SELECT_SEM_T        sys_sem_t*
149 #define SELECT_SEM_PTR(sem) (sem)
150 #else /* LWIP_NETCONN_SEM_PER_THREAD */
151 #define SELECT_SEM_T        sys_sem_t
152 #define SELECT_SEM_PTR(sem) (&(sem))
153 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
154 
155 /** Description for a task waiting in select */
156 struct lwip_select_cb {
157   /** Pointer to the next waiting task */
158   struct lwip_select_cb *next;
159   /** Pointer to the previous waiting task */
160   struct lwip_select_cb *prev;
161 #if LWIP_SOCKET_SELECT
162   /** readset passed to select */
163   fd_set *readset;
164   /** writeset passed to select */
165   fd_set *writeset;
166   /** unimplemented: exceptset passed to select */
167   fd_set *exceptset;
168 #endif /* LWIP_SOCKET_SELECT */
169 #if LWIP_SOCKET_POLL
170   /** fds passed to poll; NULL if select */
171   struct pollfd *poll_fds;
172   /** nfds passed to poll; 0 if select */
173   nfds_t poll_nfds;
174 #endif /* LWIP_SOCKET_POLL */
175   /** don't signal the same semaphore twice: set to 1 when signalled */
176   int sem_signalled;
177   /** semaphore to wake up a task waiting for select */
178   SELECT_SEM_T sem;
179 };
180 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
181 
182 #endif /* LWIP_SOCKET */
183 
184 #endif /* LWIP_HDR_SOCKETS_PRIV_H */
185