• 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 
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 /** Contains all internal pointers and states used for a socket */
67 struct lwip_sock {
68   /** sockets currently are built on netconns, each socket has one netconn */
69   struct netconn *conn;
70   /** data that was left from the previous read */
71   union lwip_sock_lastdata lastdata;
72 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
73   /** number of times data was received, set by event_callback(),
74       tested by the receive and select functions */
75   s16_t rcvevent;
76   /** number of times data was ACKed (free send buffer), set by event_callback(),
77       tested by select */
78   u16_t sendevent;
79   /** error happened for this socket, set by event_callback(), tested by select */
80   u16_t errevent;
81   /** counter of how many threads are waiting for this socket using select */
82   SELWAIT_T select_waiting;
83 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
84 #if LWIP_NETCONN_FULLDUPLEX
85   /* counter of how many threads are using a struct lwip_sock (not the 'int') */
86   u8_t fd_used;
87   /* status of pending close/delete actions */
88   u8_t fd_free_pending;
89 #define LWIP_SOCK_FD_FREE_TCP  1
90 #define LWIP_SOCK_FD_FREE_FREE 2
91 #endif
92 };
93 
94 #ifndef set_errno
95 #define set_errno(err) do { if (err) { errno = (err); } } while(0)
96 #endif
97 
98 #if !LWIP_TCPIP_CORE_LOCKING
99 /** Maximum optlen used by setsockopt/getsockopt */
100 #define LWIP_SETGETSOCKOPT_MAXOPTLEN LWIP_MAX(16, sizeof(struct ifreq))
101 
102 /** This struct is used to pass data to the set/getsockopt_internal
103  * functions running in tcpip_thread context (only a void* is allowed) */
104 struct lwip_setgetsockopt_data {
105   /** socket index for which to change options */
106   int s;
107   /** level of the option to process */
108   int level;
109   /** name of the option to process */
110   int optname;
111   /** set: value to set the option to
112     * get: value of the option is stored here */
113 #if LWIP_MPU_COMPATIBLE
114   u8_t optval[LWIP_SETGETSOCKOPT_MAXOPTLEN];
115 #else
116   union {
117     void *p;
118     const void *pc;
119   } optval;
120 #endif
121   /** size of *optval */
122   socklen_t optlen;
123   /** if an error occurs, it is temporarily stored here */
124   int err;
125   /** semaphore to wake up the calling task */
126   void* completed_sem;
127 };
128 #endif /* !LWIP_TCPIP_CORE_LOCKING */
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 struct lwip_sock* lwip_socket_dbg_get_socket(int fd);
135 
136 #if LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL
137 
138 #if LWIP_NETCONN_SEM_PER_THREAD
139 #define SELECT_SEM_T        sys_sem_t*
140 #define SELECT_SEM_PTR(sem) (sem)
141 #else /* LWIP_NETCONN_SEM_PER_THREAD */
142 #define SELECT_SEM_T        sys_sem_t
143 #define SELECT_SEM_PTR(sem) (&(sem))
144 #endif /* LWIP_NETCONN_SEM_PER_THREAD */
145 
146 /** Description for a task waiting in select */
147 struct lwip_select_cb {
148   /** Pointer to the next waiting task */
149   struct lwip_select_cb *next;
150   /** Pointer to the previous waiting task */
151   struct lwip_select_cb *prev;
152 #if LWIP_SOCKET_SELECT
153   /** readset passed to select */
154   fd_set *readset;
155   /** writeset passed to select */
156   fd_set *writeset;
157   /** unimplemented: exceptset passed to select */
158   fd_set *exceptset;
159 #endif /* LWIP_SOCKET_SELECT */
160 #if LWIP_SOCKET_POLL
161   /** fds passed to poll; NULL if select */
162   struct pollfd *poll_fds;
163   /** nfds passed to poll; 0 if select */
164   nfds_t poll_nfds;
165 #endif /* LWIP_SOCKET_POLL */
166   /** don't signal the same semaphore twice: set to 1 when signalled */
167   int sem_signalled;
168   /** semaphore to wake up a task waiting for select */
169   SELECT_SEM_T sem;
170 };
171 #endif /* LWIP_SOCKET_SELECT || LWIP_SOCKET_POLL */
172 
173 #endif /* LWIP_SOCKET */
174 
175 #endif /* LWIP_HDR_SOCKETS_PRIV_H */
176