socket_misc.c
Go to the documentation of this file.
1 /**
2  * @file socket_misc.c
3  * @brief Helper functions for sockets
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SOCKET_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/socket.h"
37 #include "core/socket_misc.h"
38 #include "core/raw_socket.h"
39 #include "core/udp.h"
40 #include "core/tcp.h"
41 #include "core/tcp_misc.h"
42 #include "debug.h"
43 
44 
45 /**
46  * @brief Allocate a socket
47  * @param[in] type Type specification for the new socket
48  * @param[in] protocol Protocol to be used
49  * @return Handle referencing the new socket
50  **/
51 
53 {
54  error_t error;
55  uint_t i;
56  uint16_t port;
57  Socket *socket;
58 
59  //Initialize socket handle
60  socket = NULL;
61 
62 #if (TCP_SUPPORT == ENABLED)
63  //Connection-oriented socket?
65  {
66  //Always use TCP as underlying transport protocol
68  //Get an ephemeral port number
70  //Continue processing
71  error = NO_ERROR;
72  }
73  else
74 #endif
75 #if (UDP_SUPPORT == ENABLED)
76  //Connectionless socket?
77  if(type == SOCKET_TYPE_DGRAM)
78  {
79  //Always use UDP as underlying transport protocol
81  //Get an ephemeral port number
83  //Continue processing
84  error = NO_ERROR;
85  }
86  else
87 #endif
88 #if (RAW_SOCKET_SUPPORT == ENABLED)
89  //Raw socket?
91  {
92  //Port numbers are not relevant for raw sockets
93  port = 0;
94  //Continue processing
95  error = NO_ERROR;
96  }
97  else
98 #endif
99  {
100  //The socket type is not supported
101  error = ERROR_INVALID_PARAMETER;
102  }
103 
104  //Check status code
105  if(!error)
106  {
107  //Loop through socket descriptors
108  for(i = 0; i < SOCKET_MAX_COUNT; i++)
109  {
110  //Unused socket found?
112  {
113  //Save socket handle
114  socket = &socketTable[i];
115  //We are done
116  break;
117  }
118  }
119 
120 #if (TCP_SUPPORT == ENABLED)
121  //No more sockets available?
122  if(socket == NULL)
123  {
124  //Kill the oldest connection in the TIME-WAIT state whenever the
125  //socket table runs out of space
127  }
128 #endif
129 
130  //Check whether the current entry is free
131  if(socket != NULL)
132  {
133  //Save socket descriptor
134  i = socket->descriptor;
135 
136  //Clear the structure keeping the event field untouched
137  osMemset(socket, 0, offsetof(Socket, event));
138 
139  osMemset((uint8_t *) socket + offsetof(Socket, event) + sizeof(OsEvent),
140  0, sizeof(Socket) - offsetof(Socket, event) - sizeof(OsEvent));
141 
142  //Save socket characteristics
143  socket->descriptor = i;
144  socket->type = type;
145  socket->protocol = protocol;
146  socket->localPort = port;
147  socket->timeout = INFINITE_DELAY;
148 
149 #if (ETH_VLAN_SUPPORT == ENABLED)
150  //Default VLAN PCP and DEI fields
151  socket->vlanPcp = -1;
152  socket->vlanDei = -1;
153 #endif
154 
155 #if (ETH_VMAN_SUPPORT == ENABLED)
156  //Default VMAN PCP and DEI fields
157  socket->vmanPcp = -1;
158  socket->vmanDei = -1;
159 #endif
160 
161 #if (TCP_SUPPORT == ENABLED && TCP_KEEP_ALIVE_SUPPORT == ENABLED)
162  //TCP keep-alive mechanism must be disabled by default (refer to
163  //RFC 1122, section 4.2.3.6)
164  socket->keepAliveEnabled = FALSE;
165 
166  //Default TCP keep-alive parameters
167  socket->keepAliveIdle = TCP_DEFAULT_KEEP_ALIVE_IDLE;
168  socket->keepAliveInterval = TCP_DEFAULT_KEEP_ALIVE_INTERVAL;
169  socket->keepAliveMaxProbes = TCP_DEFAULT_KEEP_ALIVE_PROBES;
170 #endif
171 
172 #if (TCP_SUPPORT == ENABLED)
173  //Default MSS value
174  socket->mss = TCP_MAX_MSS;
175 
176  //Default TX and RX buffer size
179 #endif
180  }
181  }
182 
183  //Return a handle to the freshly created socket
184  return socket;
185 }
186 
187 
188 /**
189  * @brief Subscribe to the specified socket events
190  * @param[in] socket Handle that identifies a socket
191  * @param[in] event Event object used to receive notifications
192  * @param[in] eventMask Logic OR of the requested socket events
193  **/
194 
196 {
197  //Valid socket handle?
198  if(socket != NULL)
199  {
200  //Get exclusive access
202 
203  //An user event may have been previously registered...
204  if(socket->userEvent != NULL)
205  {
206  socket->eventMask |= eventMask;
207  }
208  else
209  {
210  socket->eventMask = eventMask;
211  }
212 
213  //Suscribe to get notified of events
214  socket->userEvent = event;
215 
216 #if (TCP_SUPPORT == ENABLED)
217  //Handle TCP specific events
218  if(socket->type == SOCKET_TYPE_STREAM)
219  {
221  }
222 #endif
223 #if (UDP_SUPPORT == ENABLED)
224  //Handle UDP specific events
225  if(socket->type == SOCKET_TYPE_DGRAM)
226  {
228  }
229 #endif
230 #if (RAW_SOCKET_SUPPORT == ENABLED)
231  //Handle events that are specific to raw sockets
232  if(socket->type == SOCKET_TYPE_RAW_IP ||
233  socket->type == SOCKET_TYPE_RAW_ETH)
234  {
236  }
237 #endif
238 
239  //Release exclusive access
241  }
242 }
243 
244 
245 /**
246  * @brief Unsubscribe previously registered events
247  * @param[in] socket Handle that identifies a socket
248  **/
249 
251 {
252  //Valid socket handle?
253  if(socket != NULL)
254  {
255  //Get exclusive access
257 
258  //Unsuscribe socket events
259  socket->userEvent = NULL;
260 
261  //Release exclusive access
263  }
264 }
265 
266 
267 /**
268  * @brief Retrieve event flags for a specified socket
269  * @param[in] socket Handle that identifies a socket
270  * @return Logic OR of events in the signaled state
271  **/
272 
274 {
275  uint_t eventFlags;
276 
277  //Valid socket handle?
278  if(socket != NULL)
279  {
280  //Get exclusive access
282 
283  //Read event flags for the specified socket
284  eventFlags = socket->eventFlags;
285 
286  //Release exclusive access
288  }
289  else
290  {
291  //The socket handle is not valid
292  eventFlags = 0;
293  }
294 
295  //Return the events in the signaled state
296  return eventFlags;
297 }
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
uint8_t type
Definition: coap_common.h:176
unsigned int uint_t
Definition: compiler_port.h:50
Debugging facilities.
uint16_t port
Definition: dns_common.h:267
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
uint8_t protocol
Definition: ipv4.h:296
TCP/IP stack core.
#define netMutex
Definition: net_legacy.h:195
#define osMemset(p, value, length)
Definition: os_port.h:135
#define MIN(a, b)
Definition: os_port.h:63
#define FALSE
Definition: os_port.h:46
#define INFINITE_DELAY
Definition: os_port.h:75
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
void rawSocketUpdateEvents(Socket *socket)
Update event state for raw sockets.
Definition: raw_socket.c:999
TCP/IP raw sockets.
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:49
Socket API.
@ SOCKET_IP_PROTO_UDP
Definition: socket.h:101
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:100
@ SOCKET_TYPE_DGRAM
Definition: socket.h:86
@ SOCKET_TYPE_RAW_IP
Definition: socket.h:87
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:88
@ SOCKET_TYPE_UNUSED
Definition: socket.h:84
@ SOCKET_TYPE_STREAM
Definition: socket.h:85
#define Socket
Definition: socket.h:36
#define SOCKET_MAX_COUNT
Definition: socket.h:46
Socket * socketAllocate(uint_t type, uint_t protocol)
Allocate a socket.
Definition: socket_misc.c:52
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:195
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:250
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:273
Helper functions for sockets.
Event object.
Socket * tcpKillOldestConnection(void)
Kill the oldest socket in the TIME-WAIT state.
Definition: tcp.c:1045
uint16_t tcpGetDynamicPort(void)
Get an ephemeral port number.
Definition: tcp.c:106
TCP (Transmission Control Protocol)
#define TCP_DEFAULT_TX_BUFFER_SIZE
Definition: tcp.h:68
#define TCP_MAX_RX_BUFFER_SIZE
Definition: tcp.h:89
#define TCP_DEFAULT_KEEP_ALIVE_IDLE
Definition: tcp.h:215
#define TCP_MAX_MSS
Definition: tcp.h:54
#define TCP_MAX_TX_BUFFER_SIZE
Definition: tcp.h:75
#define TCP_DEFAULT_RX_BUFFER_SIZE
Definition: tcp.h:82
#define TCP_DEFAULT_KEEP_ALIVE_PROBES
Definition: tcp.h:229
#define TCP_DEFAULT_KEEP_ALIVE_INTERVAL
Definition: tcp.h:222
void tcpUpdateEvents(Socket *socket)
Update TCP related events.
Definition: tcp_misc.c:2052
Helper functions for TCP.
void udpUpdateEvents(Socket *socket)
Update UDP related events.
Definition: udp.c:928
uint16_t udpGetDynamicPort(void)
Get an ephemeral port number.
Definition: udp.c:80
UDP (User Datagram Protocol)