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.4
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 }
298 
299 
300 /**
301  * @brief Filter out incoming multicast traffic
302  * @param[in] socket Handle that identifies a socket
303  * @param[in] destAddr Destination IP address of the received packet
304  * @param[in] srcAddr Source IP address of the received packet
305  * @return Return TRUE if the multicast packet should be accepted, else FALSE
306  **/
307 
309  const IpAddr *srcAddr)
310 {
311 #if (SOCKET_MAX_MULTICAST_GROUPS > 0)
312  uint_t i;
313  bool_t acceptable;
314  SocketMulticastGroup *group;
315 
316  //Initialize flag
317  acceptable = FALSE;
318 
319  //Loop through multicast groups
320  for(i = 0; i < SOCKET_MAX_MULTICAST_GROUPS; i++)
321  {
322  //Point to the current multicast group
323  group = &socket->multicastGroups[i];
324 
325  //Matching multicast address?
326  if(ipCompAddr(&group->addr, destAddr))
327  {
328 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
329  uint_t j;
330 
331  //Check filter mode
332  if(group->filterMode == IP_FILTER_MODE_INCLUDE)
333  {
334  //In INCLUDE mode, reception of packets sent to the specified
335  //multicast address is requested only from those IP source
336  //addresses listed in the source list
337  for(j = 0; j < SOCKET_MAX_MULTICAST_SOURCES && !acceptable; j++)
338  {
339  //Compare source addresses
340  if(ipCompAddr(&group->sources[j], srcAddr))
341  {
342  acceptable = TRUE;
343  }
344  }
345  }
346  else
347  {
348  //In EXCLUDE mode, reception of packets sent to the given multicast
349  //address is requested from all IP source addresses except those
350  //listed in the source list
351  acceptable = TRUE;
352 
353  //Loop through the list of excluded source addresses
354  for(j = 0; j < group->numSources && acceptable; j++)
355  {
356  //Compare source addresses
357  if(ipCompAddr(&group->sources[j], srcAddr))
358  {
359  acceptable = FALSE;
360  }
361  }
362  }
363 #else
364  //The multicast address is acceptable
365  acceptable = TRUE;
366 #endif
367  }
368  }
369 
370  //Return TRUE if the multicast packet should be accepted
371  return acceptable;
372 #else
373  //Not implemented
374  return FALSE;
375 #endif
376 }
377 
378 
379 /**
380  * @brief Create a new multicast group
381  * @param[in] socket Handle to a socket
382  * @param[in] groupAddr IP address identifying a multicast group
383  * @return Pointer to the newly created multicast group
384  **/
385 
387  const IpAddr *groupAddr)
388 {
389 #if (SOCKET_MAX_MULTICAST_GROUPS > 0)
390  uint_t i;
391  SocketMulticastGroup *group;
392 
393  //Initialize pointer
394  group = NULL;
395 
396  //Loop through multicast groups
397  for(i = 0; i < SOCKET_MAX_MULTICAST_GROUPS; i++)
398  {
399  //Check whether the current entry is available for use
400  if(socket->multicastGroups[i].addr.length == 0)
401  {
402  //Point to the current group
403  group = &socket->multicastGroups[i];
404 
405  //Save multicast group address
406  group->addr = *groupAddr;
407 
408 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
409  //By default, all sources are accepted
410  group->filterMode = IP_FILTER_MODE_EXCLUDE;
411  group->numSources = 0;
412 #endif
413  //We are done
414  break;
415  }
416  }
417 
418  //Return a pointer to the newly created multicast group
419  return group;
420 #else
421  //Not implemented
422  return NULL;
423 #endif
424 }
425 
426 
427 /**
428  * @brief Search the list of multicast groups for a given group address
429  * @param[in] socket Handle to a socket
430  * @param[in] groupAddr IP address identifying a multicast group
431  * @return A pointer to the matching multicast group is returned. NULL is
432  * returned if the specified group address cannot be found
433  **/
434 
436  const IpAddr *groupAddr)
437 {
438 #if (SOCKET_MAX_MULTICAST_GROUPS > 0)
439  uint_t i;
440  SocketMulticastGroup *group;
441 
442  //Initialize pointer
443  group = NULL;
444 
445  //Loop through multicast groups
446  for(i = 0; i < SOCKET_MAX_MULTICAST_GROUPS; i++)
447  {
448  //Compare group addresses
449  if(ipCompAddr(&socket->multicastGroups[i].addr, groupAddr))
450  {
451  //Point to the current group
452  group = &socket->multicastGroups[i];
453  break;
454  }
455  }
456 
457  //Return a pointer to the matching multicast group
458  return group;
459 #else
460  //Not implemented
461  return NULL;
462 #endif
463 }
464 
465 
466 /**
467  * @brief Delete a multicast group
468  * @param[in] group Pointer to the multicast group
469  **/
470 
472 {
473  //Delete the specified entry
474  group->addr = IP_ADDR_UNSPECIFIED;
475 }
476 
477 
478 /**
479  * @brief Add an address to the multicast source filter
480  * @param[in] group Pointer to the multicast group
481  * @param[in] srcAddr IP address to be added to the list
482  * @return Error code
483  **/
484 
486  const IpAddr *srcAddr)
487 {
488 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
489  error_t error;
490 
491  //Initialize status code
492  error = NO_ERROR;
493 
494  //Make sure that the source address is not a duplicate
495  if(socketFindMulticastSrcAddr(group, srcAddr) < 0)
496  {
497  //Check the length of the list
498  if(group->numSources < SOCKET_MAX_MULTICAST_SOURCES)
499  {
500  //Append the source address to the list
501  group->sources[group->numSources] = *srcAddr;
502  group->numSources++;
503  }
504  else
505  {
506  //The implementation limits the number of source addresses
507  error = ERROR_OUT_OF_RESOURCES;
508  }
509  }
510 
511  //Return status code
512  return error;
513 #else
514  //Not implemented
515  return ERROR_NOT_IMPLEMENTED;
516 #endif
517 }
518 
519 
520 /**
521  * @brief Remove an address from the multicast source filter
522  * @param[in] group Pointer to the multicast group
523  * @param[in] srcAddr IP address to be removed from the list
524  **/
525 
527  const IpAddr *srcAddr)
528 {
529 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
530  uint_t i;
531  uint_t j;
532 
533  //Loop through the list of source addresses
534  for(i = 0; i < group->numSources; i++)
535  {
536  //Matching IP address?
537  if(ipCompAddr(&group->sources[i], srcAddr))
538  {
539  //Remove the source address from the list
540  for(j = i + 1; j < group->numSources; j++)
541  {
542  group->sources[j - 1] = group->sources[j];
543  }
544 
545  //Update the length of the list
546  group->numSources--;
547 
548  //We are done
549  break;
550  }
551  }
552 #endif
553 }
554 
555 
556 /**
557  * @brief Search the list of multicast sources for a given IP address
558  * @param[in] socket Handle to a socket
559  * @param[in] srcAddr Source IP address
560  * @return Index of the matching IP address is returned. -1 is
561  * returned if the specified IP address cannot be found
562  **/
563 
565  const IpAddr *srcAddr)
566 {
567 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
568  int_t i;
569  int_t index;
570 
571  //Initialize index
572  index = -1;
573 
574  //Loop through the list of source addresses
575  for(i = 0; i < group->numSources; i++)
576  {
577  //Matching IP address?
578  if(ipCompAddr(&group->sources[i], srcAddr))
579  {
580  index = i;
581  break;
582  }
583  }
584 
585  //Return the index of the matching IP address, if any
586  return index;
587 #else
588  //Not implemented
589  return -1;
590 #endif
591 }
@ SOCKET_IP_PROTO_UDP
Definition: socket.h:108
Multicast group.
Definition: socket.h:273
int bool_t
Definition: compiler_port.h:53
Ipv4Addr destAddr
Definition: ipv4.h:329
uint8_t protocol
Definition: ipv4.h:326
signed int int_t
Definition: compiler_port.h:49
#define netMutex
Definition: net_legacy.h:195
void udpUpdateEvents(Socket *socket)
Update UDP related events.
Definition: udp.c:971
IP network address.
Definition: ip.h:90
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
const IpAddr IP_ADDR_UNSPECIFIED
Definition: ip.c:54
#define TRUE
Definition: os_port.h:50
#define SOCKET_MAX_MULTICAST_GROUPS
Definition: socket.h:53
Event object.
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
@ SOCKET_TYPE_DGRAM
Definition: socket.h:93
@ IP_FILTER_MODE_EXCLUDE
Definition: ip.h:68
uint8_t type
Definition: coap_common.h:176
SocketMulticastGroup * socketCreateMulticastGroupEntry(Socket *socket, const IpAddr *groupAddr)
Create a new multicast group.
Definition: socket_misc.c:386
#define TCP_MAX_MSS
Definition: tcp.h:54
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
#define TCP_DEFAULT_KEEP_ALIVE_INTERVAL
Definition: tcp.h:222
#define TCP_DEFAULT_TX_BUFFER_SIZE
Definition: tcp.h:68
IpAddr addr
Multicast address.
Definition: socket.h:274
#define FALSE
Definition: os_port.h:46
void socketRemoveMulticastSrcAddr(SocketMulticastGroup *group, const IpAddr *srcAddr)
Remove an address from the multicast source filter.
Definition: socket_misc.c:526
Helper functions for TCP.
void socketDeleteMulticastGroupEntry(SocketMulticastGroup *group)
Delete a multicast group.
Definition: socket_misc.c:471
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
error_t
Error codes.
Definition: error.h:43
Socket * tcpKillOldestConnection(void)
Kill the oldest socket in the TIME-WAIT state.
Definition: tcp.c:1045
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:317
#define TCP_MAX_RX_BUFFER_SIZE
Definition: tcp.h:89
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
@ SOCKET_TYPE_RAW_IP
Definition: socket.h:94
void tcpUpdateEvents(Socket *socket)
Update TCP related events.
Definition: tcp_misc.c:2052
#define MIN(a, b)
Definition: os_port.h:63
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:49
void rawSocketUpdateEvents(Socket *socket)
Update event state for raw sockets.
Definition: raw_socket.c:1025
#define TCP_MAX_TX_BUFFER_SIZE
Definition: tcp.h:75
uint16_t udpGetDynamicPort(void)
Get an ephemeral port number.
Definition: udp.c:81
TCP/IP raw sockets.
uint16_t port
Definition: dns_common.h:267
#define TCP_DEFAULT_KEEP_ALIVE_PROBES
Definition: tcp.h:229
error_t socketAddMulticastSrcAddr(SocketMulticastGroup *group, const IpAddr *srcAddr)
Add an address to the multicast source filter.
Definition: socket_misc.c:485
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
uint16_t tcpGetDynamicPort(void)
Get an ephemeral port number.
Definition: tcp.c:106
Helper functions for sockets.
TCP (Transmission Control Protocol)
@ SOCKET_TYPE_UNUSED
Definition: socket.h:91
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
Ipv4Addr groupAddr
Definition: igmp_common.h:214
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
UDP (User Datagram Protocol)
#define Socket
Definition: socket.h:36
@ IP_FILTER_MODE_INCLUDE
Definition: ip.h:69
#define SOCKET_MAX_MULTICAST_SOURCES
Definition: socket.h:60
MacAddr srcAddr
Definition: ethernet.h:220
Socket API.
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:95
#define TCP_DEFAULT_KEEP_ALIVE_IDLE
Definition: tcp.h:215
bool_t socketMulticastFilter(Socket *socket, const IpAddr *destAddr, const IpAddr *srcAddr)
Filter out incoming multicast traffic.
Definition: socket_misc.c:308
#define TCP_DEFAULT_RX_BUFFER_SIZE
Definition: tcp.h:82
unsigned int uint_t
Definition: compiler_port.h:50
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:250
#define osMemset(p, value, length)
Definition: os_port.h:135
TCP/IP stack core.
#define SOCKET_MAX_COUNT
Definition: socket.h:46
SocketMulticastGroup * socketFindMulticastGroupEntry(Socket *socket, const IpAddr *groupAddr)
Search the list of multicast groups for a given group address.
Definition: socket_misc.c:435
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:273
int_t socketFindMulticastSrcAddr(SocketMulticastGroup *group, const IpAddr *srcAddr)
Search the list of multicast sources for a given IP address.
Definition: socket_misc.c:564
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define INFINITE_DELAY
Definition: os_port.h:75