• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. <joel.cunningham@garmin.com>
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 <joel.cunningham@me.com>
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 #if LWIP_EXT_POLL_SUPPORT
48 #include "poll.h"
49 #include "fs/fs.h"
50 #if !LWIP_FREERTOS_COMPAT
51 #include "linux/wait.h"
52 #endif
53 #endif  /* LWIP_EXT_POLL_SUPPORT */
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 /** This is overridable for the rare case where more than 255 threads
60  * select on the same socket...
61  */
62 #ifndef SELWAIT_T
63 #define SELWAIT_T u8_t
64 #endif
65 
66 union lwip_sock_lastdata {
67   struct netbuf *netbuf;
68   struct pbuf *pbuf;
69 };
70 
71 #if LWIP_EXT_POLL_SUPPORT
72 extern void __init_waitqueue_head(wait_queue_head_t *wait);
73 extern void poll_wait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p);
74 extern void __wake_up_interruptible_poll(wait_queue_head_t *wait, pollevent_t key);
75 #endif
76 
77 /** Contains all internal pointers and states used for a socket */
78 struct lwip_sock {
79   /** sockets currently are built on netconns, each socket has one netconn */
80   struct netconn *conn;
81   /** data that was left from the previous read */
82   union lwip_sock_lastdata lastdata;
83   /** offset in the data that was left from the previous read */
84   u16_t lastoffset;
85   /* socket level mutex used for multithread recv support */
86   sys_mutex_t mutex;
87 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
88 #if LWIP_EXT_POLL_SUPPORT
89   /* socket waitqueue */
90   wait_queue_head_t wq;
91 #endif
92   /** number of times data was received, set by event_callback(),
93       tested by the receive and select functions */
94   s16_t rcvevent;
95   /** number of times data was ACKed (free send buffer), set by event_callback(),
96       tested by select */
97   u16_t sendevent;
98   /** error happened for this socket, set by event_callback(), tested by select */
99   u16_t errevent;
100   /** last error that occurred on this socket (in fact, all our errnos fit into an u8_t) */
101   atomic_t err;  /* all usage is u8_t , but atomic is int */
102   /** counter of how many threads are waiting for this socket using select */
103   SELWAIT_T select_waiting;
104 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
105 #if LWIP_NETCONN_FULLDUPLEX
106   /* counter of how many threads are using a struct lwip_sock (not the 'int') */
107   u8_t fd_used;
108   /* status of close() is called */
109   u8_t closing;
110   /* status of pending close/delete actions */
111   u8_t fd_free_pending;
112 #define LWIP_SOCK_FD_FREE_TCP  1
113 #define LWIP_SOCK_FD_FREE_FREE 2
114 #endif
115 #ifdef LWIP_LITEOS_A_COMPAT
116   /* the number of process who reference this socket */
117   unsigned long s_refcount;
118 #endif
119 };
120 
121 #ifndef set_errno
122 #define set_errno(err) do { if (err) { errno = (err); } } while(0)
123 #endif
124 
125 #if !LWIP_TCPIP_CORE_LOCKING
126 /** Maximum optlen used by setsockopt/getsockopt */
127 #define LWIP_SETGETSOCKOPT_MAXOPTLEN LWIP_MAX(16, sizeof(struct ifreq))
128 
129 /** This struct is used to pass data to the set/getsockopt_internal
130  * functions running in tcpip_thread context (only a void* is allowed) */
131 struct lwip_setgetsockopt_data {
132   /** socket index for which to change options */
133   int s;
134   /** level of the option to process */
135   int level;
136   /** name of the option to process */
137   int optname;
138   /** set: value to set the option to
139     * get: value of the option is stored here */
140 #if LWIP_MPU_COMPATIBLE
141   u8_t optval[LWIP_SETGETSOCKOPT_MAXOPTLEN];
142 #else
143   union {
144     void *p;
145     const void *pc;
146   } optval;
147 #endif
148   /** size of *optval */
149   socklen_t optlen;
150   /** if an error occurs, it is temporarily stored here */
151   int err;
152   /** semaphore to wake up the calling task */
153   void* completed_sem;
154 };
155 #endif /* !LWIP_TCPIP_CORE_LOCKING */
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 struct lwip_sock* lwip_socket_dbg_get_socket(int fd);
162 
163 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
164 
165 #if LWIP_NETCONN_SEM_PER_THREAD
166 #define SELECT_SEM_T        sys_sem_t*
167 #define SELECT_SEM_PTR(sem) (sem)
168 #else /* LWIP_NETCONN_SEM_PER_THREAD */
169 #define SELECT_SEM_T        sys_sem_t
170 #define SELECT_SEM_PTR(sem) (&(sem))
171 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
172 
173 /** Description for a task waiting in select */
174 struct lwip_select_cb {
175   /** Pointer to the next waiting task */
176   struct lwip_select_cb *next;
177   /** Pointer to the previous waiting task */
178   struct lwip_select_cb *prev;
179 #if LWIP_SOCKET_SELECT
180   /** readset passed to select */
181   fd_set *readset;
182   /** writeset passed to select */
183   fd_set *writeset;
184   /** unimplemented: exceptset passed to select */
185   fd_set *exceptset;
186 #endif /* LWIP_SOCKET_SELECT */
187 #if LWIP_SOCKET_POLL
188   /** fds passed to poll; NULL if select */
189   struct pollfd *poll_fds;
190   /** nfds passed to poll; 0 if select */
191   nfds_t poll_nfds;
192 #endif /* LWIP_SOCKET_POLL */
193   /** don't signal the same semaphore twice: set to 1 when signalled */
194   int sem_signalled;
195   /** semaphore to wake up a task waiting for select */
196   SELECT_SEM_T sem;
197 };
198 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
199 
200 #endif /* LWIP_SOCKET */
201 
202 #endif /* LWIP_HDR_SOCKETS_PRIV_H */
203