socket.c
Go to the documentation of this file.
1 /**
2  * @file socket.c
3  * @brief Socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2022 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.1.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SOCKET_TRACE_LEVEL
33 
34 //Dependencies
35 #include <string.h>
36 #include "core/net.h"
37 #include "core/socket.h"
38 #include "core/socket_misc.h"
39 #include "core/raw_socket.h"
40 #include "core/udp.h"
41 #include "core/tcp.h"
42 #include "core/tcp_misc.h"
43 #include "dns/dns_client.h"
44 #include "mdns/mdns_client.h"
45 #include "netbios/nbns_client.h"
46 #include "llmnr/llmnr_client.h"
47 #include "debug.h"
48 
49 //Socket table
51 
52 //Default socket message
54 {
55  NULL, //Pointer to the payload
56  0, //Size of the payload, in bytes
57  0, //Actual length of the payload, in bytes
58  0, //Time-to-live value
59  NULL, //Underlying network interface
60  {0}, //Source IP address
61  0, //Source port
62  {0}, //Destination IP address
63  0, //Destination port
64 #if (ETH_SUPPORT == ENABLED)
65  {{{0}}}, //Source MAC address
66  {{{0}}}, //Destination MAC address
67  0, //Ethernet type field
68 #endif
69 #if (ETH_PORT_TAGGING_SUPPORT == ENABLED)
70  0, //Switch port identifier
71 #endif
72 #if (ETH_TIMESTAMP_SUPPORT == ENABLED)
73  -1, //Unique identifier for hardware time stamping
74  {0}, //Captured time stamp
75 #endif
76 };
77 
78 
79 /**
80  * @brief Socket related initialization
81  * @return Error code
82  **/
83 
85 {
86  uint_t i;
87  uint_t j;
88 
89  //Initialize socket descriptors
90  osMemset(socketTable, 0, sizeof(socketTable));
91 
92  //Loop through socket descriptors
93  for(i = 0; i < SOCKET_MAX_COUNT; i++)
94  {
95  //Set socket identifier
96  socketTable[i].descriptor = i;
97 
98  //Create an event object to track socket events
99  if(!osCreateEvent(&socketTable[i].event))
100  {
101  //Clean up side effects
102  for(j = 0; j < i; j++)
103  osDeleteEvent(&socketTable[j].event);
104 
105  //Report an error
106  return ERROR_OUT_OF_RESOURCES;
107  }
108  }
109 
110  //Successful initialization
111  return NO_ERROR;
112 }
113 
114 
115 /**
116  * @brief Create a socket (UDP or TCP)
117  * @param[in] type Type specification for the new socket
118  * @param[in] protocol Protocol to be used
119  * @return Handle referencing the new socket
120  **/
121 
123 {
124  Socket *socket;
125 
126  //Get exclusive access
128  //Allocate a new socket
130  //Release exclusive access
132 
133  //Return a handle to the freshly created socket
134  return socket;
135 }
136 
137 
138 /**
139  * @brief Set timeout value for blocking operations
140  * @param[in] socket Handle to a socket
141  * @param[in] timeout Maximum time to wait
142  * @return Error code
143  **/
144 
146 {
147  //Make sure the socket handle is valid
148  if(socket == NULL)
150 
151  //Get exclusive access
153  //Record timeout value
154  socket->timeout = timeout;
155  //Release exclusive access
157 
158  //No error to report
159  return NO_ERROR;
160 }
161 
162 
163 /**
164  * @brief Set TTL value for unicast datagrams
165  * @param[in] socket Handle to a socket
166  * @param[in] ttl Time-to-live value
167  * @return Error code
168  **/
169 
171 {
172  //Make sure the socket handle is valid
173  if(socket == NULL)
175 
176  //Get exclusive access
178  //Set TTL value
179  socket->ttl = ttl;
180  //Release exclusive access
182 
183  //No error to report
184  return NO_ERROR;
185 }
186 
187 
188 /**
189  * @brief Set TTL value for multicast datagrams
190  * @param[in] socket Handle to a socket
191  * @param[in] ttl Time-to-live value
192  * @return Error code
193  **/
194 
196 {
197  //Make sure the socket handle is valid
198  if(socket == NULL)
200 
201  //Get exclusive access
203  //Set TTL value
204  socket->multicastTtl = ttl;
205  //Release exclusive access
207 
208  //No error to report
209  return NO_ERROR;
210 }
211 
212 
213 /**
214  * @brief Set DSCP field
215  * @param[in] socket Handle to a socket
216  * @param[in] dscp Differentiated services codepoint
217  * @return Error code
218  **/
219 
221 {
222 #if (IP_DIFF_SERV_SUPPORT == ENABLED)
223  //Make sure the socket handle is valid
224  if(socket == NULL)
226 
227  //The DSCP field is 6 bits wide
228  if(dscp >= 64)
230 
231  //Get exclusive access
233  //Set differentiated services codepoint
234  socket->dscp = dscp;
235  //Release exclusive access
237 
238  //No error to report
239  return NO_ERROR;
240 #else
241  //Not implemented
242  return ERROR_NOT_IMPLEMENTED;
243 #endif
244 }
245 
246 
247 /**
248  * @brief Set VLAN priority
249  * @param[in] socket Handle to a socket
250  * @param[in] pcp VLAN priority value
251  * @return Error code
252  **/
253 
255 {
256 #if (ETH_VLAN_SUPPORT == ENABLED)
257  //Make sure the socket handle is valid
258  if(socket == NULL)
260 
261  //The PCP field is 3 bits wide
262  if(pcp >= 8)
264 
265  //Get exclusive access
267 
268  //The PCP field specifies the frame priority level. Different PCP values
269  //can be used to prioritize different classes of traffic
270  socket->vlanPcp = pcp;
271 
272  //Release exclusive access
274 
275  //No error to report
276  return NO_ERROR;
277 #else
278  //Not implemented
279  return ERROR_NOT_IMPLEMENTED;
280 #endif
281 }
282 
283 
284 /**
285  * @brief Set VLAN DEI flag
286  * @param[in] socket Handle to a socket
287  * @param[in] dei Drop eligible indicator
288  * @return Error code
289  **/
290 
292 {
293 #if (ETH_VLAN_SUPPORT == ENABLED)
294  //Make sure the socket handle is valid
295  if(socket == NULL)
297 
298  //Get exclusive access
300 
301  //The DEI flag may be used to indicate frames eligible to be dropped in
302  //the presence of congestion
303  socket->vlanDei = dei;
304 
305  //Release exclusive access
307 
308  //No error to report
309  return NO_ERROR;
310 #else
311  //Not implemented
312  return ERROR_NOT_IMPLEMENTED;
313 #endif
314 }
315 
316 
317 /**
318  * @brief Set VMAN priority
319  * @param[in] socket Handle to a socket
320  * @param[in] pcp VLAN priority value
321  * @return Error code
322  **/
323 
325 {
326 #if (ETH_VMAN_SUPPORT == ENABLED)
327  //Make sure the socket handle is valid
328  if(socket == NULL)
330 
331  //The PCP field is 3 bits wide
332  if(pcp >= 8)
334 
335  //Get exclusive access
337 
338  //The PCP field specifies the frame priority level. Different PCP values
339  //can be used to prioritize different classes of traffic
340  socket->vmanPcp = pcp;
341 
342  //Release exclusive access
344 
345  //No error to report
346  return NO_ERROR;
347 #else
348  //Not implemented
349  return ERROR_NOT_IMPLEMENTED;
350 #endif
351 }
352 
353 
354 /**
355  * @brief Set VMAN DEI flag
356  * @param[in] socket Handle to a socket
357  * @param[in] dei Drop eligible indicator
358  * @return Error code
359  **/
360 
362 {
363 #if (ETH_VMAN_SUPPORT == ENABLED)
364  //Make sure the socket handle is valid
365  if(socket == NULL)
367 
368  //Get exclusive access
370 
371  //The DEI flag may be used to indicate frames eligible to be dropped in
372  //the presence of congestion
373  socket->vmanDei = dei;
374 
375  //Release exclusive access
377 
378  //No error to report
379  return NO_ERROR;
380 #else
381  //Not implemented
382  return ERROR_NOT_IMPLEMENTED;
383 #endif
384 }
385 
386 
387 /**
388  * @brief Enable TCP keep-alive
389  * @param[in] socket Handle to a socket
390  * @param[in] enabled Specifies whether TCP keep-alive is enabled
391  * @return Error code
392  **/
393 
395 {
396 #if (TCP_SUPPORT == ENABLED && TCP_KEEP_ALIVE_SUPPORT == ENABLED)
397  //Make sure the socket handle is valid
398  if(socket == NULL)
400 
401  //Get exclusive access
403 
404  //Check whether TCP keep-alive mechanism should be enabled
405  if(enabled)
406  {
407  //Enable TCP keep-alive mechanism
408  socket->keepAliveEnabled = TRUE;
409  //Reset keep-alive probe counter
410  socket->keepAliveProbeCount = 0;
411  //Start keep-alive timer
412  socket->keepAliveTimestamp = osGetSystemTime();
413  }
414  else
415  {
416  //Disable TCP keep-alive mechanism
417  socket->keepAliveEnabled = FALSE;
418  }
419 
420  //Release exclusive access
422 
423  //Successful processing
424  return NO_ERROR;
425 #else
426  //Not implemented
427  return ERROR_NOT_IMPLEMENTED;
428 #endif
429 }
430 
431 
432 /**
433  * @brief Set TCP keep-alive parameters
434  * @param[in] socket Handle to a socket
435  * @param[in] idle Time interval between last data packet sent and first
436  * keep-alive probe
437  * @param[in] interval Time interval between subsequent keep-alive probes
438  * @param[in] maxProbes Number of unacknowledged keep-alive probes to send before
439  * considering the connection is dead
440  * @return Error code
441  **/
442 
444  systime_t interval, uint_t maxProbes)
445 {
446 #if (TCP_SUPPORT == ENABLED && TCP_KEEP_ALIVE_SUPPORT == ENABLED)
447  //Check parameters
448  if(socket == NULL || idle == 0 || interval == 0 || maxProbes == 0)
449  {
451  }
452 
453  //Get exclusive access
455 
456  //Time interval between last data packet sent and first keep-alive probe
457  socket->keepAliveIdle = idle;
458 
459  //Time interval between subsequent keep-alive probes
460  socket->keepAliveInterval = interval;
461 
462  //Number of unacknowledged keep-alive probes to send before considering
463  //the connection is dead
464  socket->keepAliveMaxProbes = maxProbes;
465 
466  //Release exclusive access
468 
469  //Successful processing
470  return NO_ERROR;
471 #else
472  //Not implemented
473  return ERROR_NOT_IMPLEMENTED;
474 #endif
475 }
476 
477 
478 /**
479  * @brief Specify the size of the send buffer
480  * @param[in] socket Handle to a socket
481  * @param[in] size Desired buffer size in bytes
482  * @return Error code
483  **/
484 
486 {
487 #if (TCP_SUPPORT == ENABLED)
488  //Make sure the socket handle is valid
489  if(socket == NULL)
491  //Check parameter value
492  if(size < 1 || size > TCP_MAX_TX_BUFFER_SIZE)
494 
495  //This function shall be used with connection-oriented socket types
496  if(socket->type != SOCKET_TYPE_STREAM)
497  return ERROR_INVALID_SOCKET;
498  //The buffer size cannot be changed when the connection is established
500  return ERROR_INVALID_SOCKET;
501 
502  //Use the specified buffer size
503  socket->txBufferSize = size;
504  //No error to report
505  return NO_ERROR;
506 #else
507  return ERROR_NOT_IMPLEMENTED;
508 #endif
509 }
510 
511 
512 /**
513  * @brief Specify the size of the receive buffer
514  * @param[in] socket Handle to a socket
515  * @param[in] size Desired buffer size in bytes
516  * @return Error code
517  **/
518 
520 {
521 #if (TCP_SUPPORT == ENABLED)
522  //Make sure the socket handle is valid
523  if(socket == NULL)
525  //Check parameter value
526  if(size < 1 || size > TCP_MAX_RX_BUFFER_SIZE)
528 
529  //This function shall be used with connection-oriented socket types
530  if(socket->type != SOCKET_TYPE_STREAM)
531  return ERROR_INVALID_SOCKET;
532  //The buffer size cannot be changed when the connection is established
534  return ERROR_INVALID_SOCKET;
535 
536  //Use the specified buffer size
537  socket->rxBufferSize = size;
538  //No error to report
539  return NO_ERROR;
540 #else
541  return ERROR_NOT_IMPLEMENTED;
542 #endif
543 }
544 
545 
546 /**
547  * @brief Bind a socket to a particular network interface
548  * @param[in] socket Handle to a socket
549  * @param[in] interface Network interface to be used
550  * @return Error code
551  **/
552 
554 {
555  //Make sure the socket handle is valid
556  if(socket == NULL)
558 
559  //Explicitly associate the socket with the specified interface
560  socket->interface = interface;
561 
562  //No error to report
563  return NO_ERROR;
564 }
565 
566 
567 /**
568  * @brief Retrieve the underlying interface
569  * @param[in] socket Handle to a socket
570  * @return Pointer to the underlying network interface
571  **/
572 
574 {
575  NetInterface *interface = NULL;
576 
577  //Make sure the socket handle is valid
578  if(socket != NULL)
579  {
580  interface = socket->interface;
581  }
582 
583  //Return a pointer to the underlying network interface
584  return interface;
585 }
586 
587 
588 /**
589  * @brief Associate a local address with a socket
590  * @param[in] socket Handle to a socket
591  * @param[in] localIpAddr Local address to assign to the bound socket
592  * @param[in] localPort Local port number to assign to the bound socket
593  * @return Error code
594  **/
595 
596 error_t socketBind(Socket *socket, const IpAddr *localIpAddr,
597  uint16_t localPort)
598 {
599  //Check input parameters
600  if(socket == NULL || localIpAddr == NULL)
602 
603  //Make sure the socket type is correct
604  if(socket->type != SOCKET_TYPE_STREAM && socket->type != SOCKET_TYPE_DGRAM)
605  return ERROR_INVALID_SOCKET;
606 
607  //Associate the specified IP address and port number
608  socket->localIpAddr = *localIpAddr;
609  socket->localPort = localPort;
610 
611  //No error to report
612  return NO_ERROR;
613 }
614 
615 
616 /**
617  * @brief Establish a connection to a specified socket
618  * @param[in] socket Handle to an unconnected socket
619  * @param[in] remoteIpAddr IP address of the remote host
620  * @param[in] remotePort Remote port number that will be used to establish
621  * the connection
622  * @return Error code
623  **/
624 
625 error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr,
626  uint16_t remotePort)
627 {
628  error_t error;
629 
630  //Check input parameters
631  if(socket == NULL || remoteIpAddr == NULL)
633 
634 #if (TCP_SUPPORT == ENABLED)
635  //Connection-oriented socket?
636  if(socket->type == SOCKET_TYPE_STREAM)
637  {
638  //Get exclusive access
640 
641  //Establish TCP connection
642  error = tcpConnect(socket, remoteIpAddr, remotePort);
643 
644  //Release exclusive access
646  }
647  else
648 #endif
649  //Connectionless socket?
650  if(socket->type == SOCKET_TYPE_DGRAM)
651  {
652  //Save port number and IP address of the remote host
653  socket->remoteIpAddr = *remoteIpAddr;
654  socket->remotePort = remotePort;
655  //No error to report
656  error = NO_ERROR;
657  }
658  //Raw socket?
659  else if(socket->type == SOCKET_TYPE_RAW_IP)
660  {
661  //Save the IP address of the remote host
662  socket->remoteIpAddr = *remoteIpAddr;
663  //No error to report
664  error = NO_ERROR;
665  }
666  //Invalid socket type?
667  else
668  {
669  //Report an error
670  error = ERROR_INVALID_SOCKET;
671  }
672 
673  //Return status code
674  return error;
675 }
676 
677 
678 /**
679  * @brief Place a socket in the listening state
680  *
681  * Place a socket in a state in which it is listening for an incoming connection
682  *
683  * @param[in] socket Socket to place in the listening state
684  * @param[in] backlog backlog The maximum length of the pending connection queue.
685  * If this parameter is zero, then the default backlog value is used instead
686  * @return Error code
687  **/
688 
690 {
691 #if (TCP_SUPPORT == ENABLED)
692  error_t error;
693 
694  //Make sure the socket handle is valid
695  if(socket == NULL)
697  //This function shall be used with connection-oriented socket types
698  if(socket->type != SOCKET_TYPE_STREAM)
699  return ERROR_INVALID_SOCKET;
700 
701  //Get exclusive access
703 
704  //Start listening for an incoming connection
705  error = tcpListen(socket, backlog);
706 
707  //Release exclusive access
709 
710  //Return status code
711  return error;
712 #else
713  return ERROR_NOT_IMPLEMENTED;
714 #endif
715 }
716 
717 
718 /**
719  * @brief Permit an incoming connection attempt on a socket
720  * @param[in] socket Handle to a socket previously placed in a listening state
721  * @param[out] clientIpAddr IP address of the client
722  * @param[out] clientPort Port number used by the client
723  * @return Handle to the socket in which the actual connection is made
724  **/
725 
727  uint16_t *clientPort)
728 {
729 #if (TCP_SUPPORT == ENABLED)
730  Socket *newSocket;
731 
732  //Make sure the socket handle is valid
733  if(socket == NULL)
734  return NULL;
735  //This function shall be used with connection-oriented socket types
736  if(socket->type != SOCKET_TYPE_STREAM)
737  return NULL;
738 
739  //Accept an incoming connection attempt
740  newSocket = tcpAccept(socket, clientIpAddr, clientPort);
741 
742  //Return a handle to the newly created socket
743  return newSocket;
744 #else
745  return NULL;
746 #endif
747 }
748 
749 
750 /**
751  * @brief Send data to a connected socket
752  * @param[in] socket Handle that identifies a connected socket
753  * @param[in] data Pointer to a buffer containing the data to be transmitted
754  * @param[in] length Number of data bytes to send
755  * @param[out] written Actual number of bytes written (optional parameter)
756  * @param[in] flags Set of flags that influences the behavior of this function
757  * @return Error code
758  **/
759 
760 error_t socketSend(Socket *socket, const void *data, size_t length,
761  size_t *written, uint_t flags)
762 {
763  //Use default remote IP address for connectionless or raw sockets
764  return socketSendTo(socket, &socket->remoteIpAddr, socket->remotePort,
765  data, length, written, flags);
766 }
767 
768 
769 /**
770  * @brief Send a datagram to a specific destination
771  * @param[in] socket Handle that identifies a socket
772  * @param[in] destIpAddr IP address of the target host
773  * @param[in] destPort Target port number
774  * @param[in] data Pointer to a buffer containing the data to be transmitted
775  * @param[in] length Number of data bytes to send
776  * @param[out] written Actual number of bytes written (optional parameter)
777  * @param[in] flags Set of flags that influences the behavior of this function
778  * @return Error code
779  **/
780 
782  const void *data, size_t length, size_t *written, uint_t flags)
783 {
784  error_t error;
785 
786  //No data has been transmitted yet
787  if(written != NULL)
788  *written = 0;
789 
790  //Make sure the socket handle is valid
791  if(socket == NULL)
793 
794  //Get exclusive access
796 
797 #if (TCP_SUPPORT == ENABLED)
798  //Connection-oriented socket?
799  if(socket->type == SOCKET_TYPE_STREAM)
800  {
801  //For connection-oriented sockets, target address is ignored
802  error = tcpSend(socket, data, length, written, flags);
803  }
804  else
805 #endif
806  {
808 
809  //Initialize structure
811 
812  //Set destination IP address
813  if(destIpAddr != NULL)
814  {
815  message.destIpAddr = *destIpAddr;
816  }
817 
818  //Set destination port
819  message.destPort = destPort;
820 
821 #if (UDP_SUPPORT == ENABLED)
822  //Connectionless socket?
823  if(socket->type == SOCKET_TYPE_DGRAM)
824  {
825  //Set data payload
826  message.data = (uint8_t *) data;
827  message.length = length;
828 
829  //Send UDP datagram
830  error = udpSendDatagram(socket, &message, flags);
831  }
832  else
833 #endif
834 #if (RAW_SOCKET_SUPPORT == ENABLED)
835  //Raw socket?
836  if(socket->type == SOCKET_TYPE_RAW_IP)
837  {
838  //Set data payload
839  message.data = (uint8_t *) data;
840  message.length = length;
841 
842  //Send a raw IP packet
844  }
845  else if(socket->type == SOCKET_TYPE_RAW_ETH)
846  {
847  //Sanity check
848  if(length >= sizeof(EthHeader))
849  {
850  EthHeader *header;
851 
852  //Point to the Ethernet header
853  header = (EthHeader *) data;
854 
855  //Set source and destination MAC addresses
856  message.srcMacAddr = header->srcAddr;
857  message.destMacAddr = header->destAddr;
858 
859  //Set the value of the EtherType field
860  message.ethType = ntohs(header->type);
861 
862  //Set data payload
863  message.data = (uint8_t *) data + sizeof(EthHeader);
864  message.length = length - sizeof(EthHeader);
865 
866  //Send a raw Ethernet packet
868  }
869  else
870  {
871  //Report an error
872  error = ERROR_INVALID_LENGTH;
873  }
874  }
875  else
876 #endif
877  //Invalid socket type?
878  {
879  //Report an error
880  error = ERROR_INVALID_SOCKET;
881  }
882 
883  //Check status code
884  if(!error)
885  {
886  //Total number of data bytes successfully transmitted
887  if(written != NULL)
888  *written = message.length;
889  }
890  }
891 
892  //Release exclusive access
894 
895  //Return status code
896  return error;
897 }
898 
899 
900 /**
901  * @brief Send a message to a connectionless socket
902  * @param[in] socket Handle that identifies a socket
903  * @param[in] message Pointer to the structure describing the message
904  * @param[in] flags Set of flags that influences the behavior of this function
905  * @return Error code
906  **/
907 
909 {
910  error_t error;
911 
912  //Make sure the socket handle is valid
913  if(socket == NULL)
915 
916  //Get exclusive access
918 
919 #if (UDP_SUPPORT == ENABLED)
920  //Connectionless socket?
921  if(socket->type == SOCKET_TYPE_DGRAM)
922  {
923  //Send UDP datagram
924  error = udpSendDatagram(socket, message, flags);
925  }
926  else
927 #endif
928 #if (RAW_SOCKET_SUPPORT == ENABLED)
929  //Raw socket?
930  if(socket->type == SOCKET_TYPE_RAW_IP)
931  {
932  //Send a raw IP packet
934  }
935  else if(socket->type == SOCKET_TYPE_RAW_ETH)
936  {
937  //Send a raw Ethernet packet
939  }
940  else
941 #endif
942  //Invalid socket type?
943  {
944  //Report an error
945  error = ERROR_INVALID_SOCKET;
946  }
947 
948  //Release exclusive access
950 
951  //Return status code
952  return error;
953 }
954 
955 
956 /**
957  * @brief Receive data from a connected socket
958  * @param[in] socket Handle that identifies a connected socket
959  * @param[out] data Buffer where to store the incoming data
960  * @param[in] size Maximum number of bytes that can be received
961  * @param[out] received Number of bytes that have been received
962  * @param[in] flags Set of flags that influences the behavior of this function
963  * @return Error code
964  **/
965 
967  size_t size, size_t *received, uint_t flags)
968 {
969  //For connection-oriented sockets, source and destination addresses are
970  //no use
971  return socketReceiveEx(socket, NULL, NULL, NULL, data, size, received,
972  flags);
973 }
974 
975 
976 /**
977  * @brief Receive a datagram from a connectionless socket
978  * @param[in] socket Handle that identifies a socket
979  * @param[out] srcIpAddr Source IP address (optional)
980  * @param[out] srcPort Source port number (optional)
981  * @param[out] data Buffer where to store the incoming data
982  * @param[in] size Maximum number of bytes that can be received
983  * @param[out] received Number of bytes that have been received
984  * @param[in] flags Set of flags that influences the behavior of this function
985  * @return Error code
986  **/
987 
989  void *data, size_t size, size_t *received, uint_t flags)
990 {
991  //Destination address is no use
992  return socketReceiveEx(socket, srcIpAddr, srcPort, NULL, data, size,
993  received, flags);
994 }
995 
996 
997 /**
998  * @brief Receive a datagram
999  * @param[in] socket Handle that identifies a socket
1000  * @param[out] srcIpAddr Source IP address (optional)
1001  * @param[out] srcPort Source port number (optional)
1002  * @param[out] destIpAddr Destination IP address (optional)
1003  * @param[out] data Buffer where to store the incoming data
1004  * @param[in] size Maximum number of bytes that can be received
1005  * @param[out] received Number of bytes that have been received
1006  * @param[in] flags Set of flags that influences the behavior of this function
1007  * @return Error code
1008  **/
1009 
1011  IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags)
1012 {
1013  error_t error;
1014 
1015  //No data has been received yet
1016  if(received != NULL)
1017  *received = 0;
1018 
1019  //Make sure the socket handle is valid
1020  if(socket == NULL)
1021  return ERROR_INVALID_PARAMETER;
1022 
1023  //Get exclusive access
1025 
1026 #if (TCP_SUPPORT == ENABLED)
1027  //Connection-oriented socket?
1028  if(socket->type == SOCKET_TYPE_STREAM)
1029  {
1030  //Receive data
1031  error = tcpReceive(socket, data, size, received, flags);
1032 
1033  //Save the source IP address
1034  if(srcIpAddr != NULL)
1035  {
1036  *srcIpAddr = socket->remoteIpAddr;
1037  }
1038 
1039  //Save the source port number
1040  if(srcPort != NULL)
1041  {
1042  *srcPort = socket->remotePort;
1043  }
1044 
1045  //Save the destination IP address
1046  if(destIpAddr != NULL)
1047  {
1048  *destIpAddr = socket->localIpAddr;
1049  }
1050  }
1051  else
1052 #endif
1053  {
1055 
1056  //Initialize structure
1058 
1059 #if (UDP_SUPPORT == ENABLED)
1060  //Connectionless socket?
1061  if(socket->type == SOCKET_TYPE_DGRAM)
1062  {
1063  //Set data buffer
1064  message.data = data;
1065  message.size = size;
1066 
1067  //Receive UDP datagram
1068  error = udpReceiveDatagram(socket, &message, flags);
1069  }
1070  else
1071 #endif
1072 #if (RAW_SOCKET_SUPPORT == ENABLED)
1073  //Raw socket?
1074  if(socket->type == SOCKET_TYPE_RAW_IP)
1075  {
1076  //Set data buffer
1077  message.data = data;
1078  message.size = size;
1079 
1080  //Receive a raw IP packet
1082  }
1083  else if(socket->type == SOCKET_TYPE_RAW_ETH)
1084  {
1085  //Sanity check
1086  if(size >= sizeof(EthHeader))
1087  {
1088  //Set data buffer
1089  message.data = (uint8_t *) data + sizeof(EthHeader);
1090  message.size = size - sizeof(EthHeader);
1091 
1092  //Receive a raw Ethernet packet
1094 
1095  //Check status code
1096  if(!error)
1097  {
1098  EthHeader *header;
1099 
1100  //Point to the Ethernet header
1101  header = (EthHeader *) data;
1102 
1103  //Reconstruct Ethernet header
1104  header->destAddr = message.destMacAddr;
1105  header->srcAddr = message.srcMacAddr;
1106  header->type = htons(message.ethType);
1107 
1108  //Ajuste the length of the frame
1109  message.length += sizeof(EthHeader);
1110  }
1111  }
1112  else
1113  {
1114  //Report an error
1115  error = ERROR_BUFFER_OVERFLOW;
1116  }
1117  }
1118  else
1119 #endif
1120  //Invalid socket type?
1121  {
1122  //Report an error
1123  error = ERROR_INVALID_SOCKET;
1124  }
1125 
1126  //Check status code
1127  if(!error)
1128  {
1129  //Save the source IP address
1130  if(srcIpAddr != NULL)
1131  {
1132  *srcIpAddr = message.srcIpAddr;
1133  }
1134 
1135  //Save the source port number
1136  if(srcPort != NULL)
1137  {
1138  *srcPort = message.srcPort;
1139  }
1140 
1141  //Save the destination IP address
1142  if(destIpAddr != NULL)
1143  {
1144  *destIpAddr = message.destIpAddr;
1145  }
1146 
1147  //Total number of data that have been received
1148  *received = message.length;
1149  }
1150  }
1151 
1152  //Release exclusive access
1154 
1155  //Return status code
1156  return error;
1157 }
1158 
1159 
1160 /**
1161  * @brief Receive a message from a connectionless socket
1162  * @param[in] socket Handle that identifies a socket
1163  * @param[in,out] message Pointer to the structure describing the message
1164  * @param[in] flags Set of flags that influences the behavior of this function
1165  * @return Error code
1166  **/
1167 
1169 {
1170  error_t error;
1171 
1172  //No data has been received yet
1173  message->length = 0;
1174 
1175  //Make sure the socket handle is valid
1176  if(socket == NULL)
1177  return ERROR_INVALID_PARAMETER;
1178 
1179  //Get exclusive access
1181 
1182 #if (UDP_SUPPORT == ENABLED)
1183  //Connectionless socket?
1184  if(socket->type == SOCKET_TYPE_DGRAM)
1185  {
1186  //Receive UDP datagram
1188  }
1189  else
1190 #endif
1191 #if (RAW_SOCKET_SUPPORT == ENABLED)
1192  //Raw socket?
1193  if(socket->type == SOCKET_TYPE_RAW_IP)
1194  {
1195  //Receive a raw IP packet
1197  }
1198  else if(socket->type == SOCKET_TYPE_RAW_ETH)
1199  {
1200  //Receive a raw Ethernet packet
1202  }
1203  else
1204 #endif
1205  //Invalid socket type?
1206  {
1207  //Report an error
1208  error = ERROR_INVALID_SOCKET;
1209  }
1210 
1211  //Release exclusive access
1213 
1214  //Return status code
1215  return error;
1216 }
1217 
1218 
1219 /**
1220  * @brief Retrieve the local address for a given socket
1221  * @param[in] socket Handle that identifies a socket
1222  * @param[out] localIpAddr Local IP address (optional)
1223  * @param[out] localPort Local port number (optional)
1224  * @return Error code
1225  **/
1226 
1228  uint16_t *localPort)
1229 {
1230  //Make sure the socket handle is valid
1231  if(socket == NULL)
1232  return ERROR_INVALID_PARAMETER;
1233 
1234  //Retrieve local IP address
1235  if(localIpAddr != NULL)
1236  {
1237  *localIpAddr = socket->localIpAddr;
1238  }
1239 
1240  //Retrieve local port number
1241  if(localPort != NULL)
1242  {
1243  *localPort = socket->localPort;
1244  }
1245 
1246  //Successful processing
1247  return NO_ERROR;
1248 }
1249 
1250 
1251 /**
1252  * @brief Retrieve the address of the peer to which a socket is connected
1253  * @param[in] socket Handle that identifies a socket
1254  * @param[out] remoteIpAddr IP address of the remote host (optional)
1255  * @param[out] remotePort Remote port number (optional)
1256  * @return Error code
1257  **/
1258 
1260  uint16_t *remotePort)
1261 {
1262  //Make sure the socket handle is valid
1263  if(socket == NULL)
1264  return ERROR_INVALID_PARAMETER;
1265 
1266  //Retrieve local IP address
1267  if(remoteIpAddr != NULL)
1268  {
1269  *remoteIpAddr = socket->remoteIpAddr;
1270  }
1271 
1272  //Retrieve local port number
1273  if(remotePort != NULL)
1274  {
1275  *remotePort = socket->remotePort;
1276  }
1277 
1278  //Successful processing
1279  return NO_ERROR;
1280 }
1281 
1282 
1283 /**
1284  * @brief Disable reception, transmission, or both
1285  *
1286  * Note that socketShutdown() does not close the socket, and resources attached
1287  * to the socket will not be freed until socketClose() is invoked
1288  *
1289  * @param[in] socket Handle to a socket
1290  * @param[in] how Flag that describes what types of operation will no longer be allowed
1291  * @return Error code
1292  **/
1293 
1295 {
1296 #if (TCP_SUPPORT == ENABLED)
1297  error_t error;
1298 
1299  //Make sure the socket handle is valid
1300  if(socket == NULL)
1301  return ERROR_INVALID_PARAMETER;
1302  //Make sure the socket type is correct
1303  if(socket->type != SOCKET_TYPE_STREAM)
1304  return ERROR_INVALID_SOCKET;
1305  //Check flags
1306  if((how != SOCKET_SD_SEND) && (how != SOCKET_SD_RECEIVE) && (how != SOCKET_SD_BOTH))
1307  return ERROR_INVALID_PARAMETER;
1308 
1309  //Get exclusive access
1311 
1312  //Graceful shutdown
1313  error = tcpShutdown(socket, how);
1314 
1315  //Release exclusive access
1317 
1318  //Return status code
1319  return error;
1320 #else
1321  return ERROR_NOT_IMPLEMENTED;
1322 #endif
1323 }
1324 
1325 
1326 /**
1327  * @brief Close an existing socket
1328  * @param[in] socket Handle identifying the socket to close
1329  **/
1330 
1332 {
1333  //Make sure the socket handle is valid
1334  if(socket == NULL)
1335  return;
1336 
1337  //Get exclusive access
1339 
1340 #if (TCP_SUPPORT == ENABLED)
1341  //Connection-oriented socket?
1342  if(socket->type == SOCKET_TYPE_STREAM)
1343  {
1344  //Abort the current TCP connection
1345  tcpAbort(socket);
1346  }
1347 #endif
1348 #if (UDP_SUPPORT == ENABLED || RAW_SOCKET_SUPPORT == ENABLED)
1349  //Connectionless socket or raw socket?
1350  if(socket->type == SOCKET_TYPE_DGRAM ||
1351  socket->type == SOCKET_TYPE_RAW_IP ||
1352  socket->type == SOCKET_TYPE_RAW_ETH)
1353  {
1354  //Point to the first item in the receive queue
1355  SocketQueueItem *queueItem = socket->receiveQueue;
1356 
1357  //Purge the receive queue
1358  while(queueItem)
1359  {
1360  //Keep track of the next item in the queue
1361  SocketQueueItem *nextQueueItem = queueItem->next;
1362  //Free previously allocated memory
1363  netBufferFree(queueItem->buffer);
1364  //Point to the next item
1365  queueItem = nextQueueItem;
1366  }
1367 
1368  //Mark the socket as closed
1369  socket->type = SOCKET_TYPE_UNUSED;
1370  }
1371 #endif
1372 
1373  //Release exclusive access
1375 }
1376 
1377 
1378 /**
1379  * @brief Wait for one of a set of sockets to become ready to perform I/O
1380  *
1381  * This function determines the status of one or more sockets, waiting if
1382  * necessary, to perform synchronous I/O
1383  *
1384  * @param[in,out] eventDesc Set of entries specifying the events the user is interested in
1385  * @param[in] size Number of entries in the descriptor set
1386  * @param[in] extEvent External event that can abort the wait if necessary (optional)
1387  * @param[in] timeout Maximum time to wait before returning
1388  * @return Error code
1389  **/
1390 
1391 error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent,
1392  systime_t timeout)
1393 {
1394  uint_t i;
1395  bool_t status;
1396  OsEvent *event;
1397  OsEvent eventObject;
1398 
1399  //Check parameters
1400  if(eventDesc == NULL || size == 0)
1401  return ERROR_INVALID_PARAMETER;
1402 
1403  //Try to use the supplied event object to receive notifications
1404  if(extEvent == NULL)
1405  {
1406  //Create an event object only if necessary
1407  if(!osCreateEvent(&eventObject))
1408  {
1409  //Report an error
1410  return ERROR_OUT_OF_RESOURCES;
1411  }
1412 
1413  //Reference to the newly created event
1414  event = &eventObject;
1415  }
1416  else
1417  {
1418  //Reference to the external event
1419  event = extEvent;
1420  }
1421 
1422  //Loop through descriptors
1423  for(i = 0; i < size; i++)
1424  {
1425  //Valid socket handle?
1426  if(eventDesc[i].socket != NULL)
1427  {
1428  //Clear event flags
1429  eventDesc[i].eventFlags = 0;
1430 
1431  //Subscribe to the requested events
1432  socketRegisterEvents(eventDesc[i].socket, event,
1433  eventDesc[i].eventMask);
1434  }
1435  }
1436 
1437  //Block the current task until an event occurs
1438  status = osWaitForEvent(event, timeout);
1439 
1440  //Any socket event in the signaled state?
1441  if(status)
1442  {
1443  //Clear flag
1444  status = FALSE;
1445 
1446  //Loop through descriptors
1447  for(i = 0; i < size; i++)
1448  {
1449  //Valid socket handle?
1450  if(eventDesc[i].socket != NULL)
1451  {
1452  //Retrieve event flags for the current socket
1453  eventDesc[i].eventFlags = socketGetEvents(eventDesc[i].socket);
1454  //Clear unnecessary flags
1455  eventDesc[i].eventFlags &= eventDesc[i].eventMask;
1456 
1457  //Check whether the socket is ready to perform I/O
1458  if(eventDesc[i].eventFlags != 0)
1459  {
1460  status = TRUE;
1461  }
1462  }
1463  }
1464  }
1465 
1466  //Loop through descriptors
1467  for(i = 0; i < size; i++)
1468  {
1469  //Valid socket handle?
1470  if(eventDesc[i].socket != NULL)
1471  {
1472  //Unsubscribe previously registered events
1473  socketUnregisterEvents(eventDesc[i].socket);
1474  }
1475  }
1476 
1477  //Reset event object
1478  osResetEvent(event);
1479 
1480  //Release previously allocated resources
1481  if(extEvent == NULL)
1482  {
1483  osDeleteEvent(&eventObject);
1484  }
1485 
1486  //Return status code
1487  return status ? NO_ERROR : ERROR_TIMEOUT;
1488 }
1489 
1490 
1491 /**
1492  * @brief Resolve a host name into an IP address
1493  * @param[in] interface Underlying network interface (optional parameter)
1494  * @param[in] name Name of the host to be resolved
1495  * @param[out] ipAddr IP address corresponding to the specified host name
1496  * @param[in] flags Set of flags that influences the behavior of this function
1497  * @return Error code
1498  **/
1499 
1501  const char_t *name, IpAddr *ipAddr, uint_t flags)
1502 {
1503  error_t error;
1504  HostType type;
1506 
1507  //Default address type depends on TCP/IP stack configuration
1508 #if (IPV4_SUPPORT == ENABLED)
1509  type = HOST_TYPE_IPV4;
1510 #elif (IPV6_SUPPORT == ENABLED)
1511  type = HOST_TYPE_IPV6;
1512 #else
1513  type = HOST_TYPE_ANY;
1514 #endif
1515 
1516  //Default name resolution protocol depends on TCP/IP stack configuration
1517 #if (DNS_CLIENT_SUPPORT == ENABLED)
1519 #elif (MDNS_CLIENT_SUPPORT == ENABLED)
1521 #elif (NBNS_CLIENT_SUPPORT == ENABLED)
1523 #elif (LLMNR_CLIENT_SUPPORT == ENABLED)
1525 #else
1527 #endif
1528 
1529  //Check parameters
1530  if(name == NULL || ipAddr == NULL)
1531  return ERROR_INVALID_PARAMETER;
1532 
1533  //Use default network interface?
1534  if(interface == NULL)
1535  {
1536  interface = netGetDefaultInterface();
1537  }
1538 
1539  //The specified name can be either an IP or a host name
1540  error = ipStringToAddr(name, ipAddr);
1541 
1542  //Perform name resolution if necessary
1543  if(error)
1544  {
1545  //The user may provide a hint to choose between IPv4 and IPv6
1546  if((flags & HOST_TYPE_IPV4) != 0)
1547  {
1548  type = HOST_TYPE_IPV4;
1549  }
1550  else if((flags & HOST_TYPE_IPV6) != 0)
1551  {
1552  type = HOST_TYPE_IPV6;
1553  }
1554  else
1555  {
1556  //Just for sanity
1557  }
1558 
1559  //The user may provide a hint to to select the desired protocol to be used
1560  if((flags & HOST_NAME_RESOLVER_DNS) != 0)
1561  {
1562  //Use DNS to resolve the specified host name
1564  }
1565  else if((flags & HOST_NAME_RESOLVER_MDNS) != 0)
1566  {
1567  //Use mDNS to resolve the specified host name
1569  }
1570  else if((flags & HOST_NAME_RESOLVER_NBNS) != 0)
1571  {
1572  //Use NBNS to resolve the specified host name
1574  }
1575  else if((flags & HOST_NAME_RESOLVER_LLMNR) != 0)
1576  {
1577  //Use LLMNR to resolve the specified host name
1579  }
1580  else
1581  {
1582  //Retrieve the length of the host name to be resolved
1583  size_t n = osStrlen(name);
1584 
1585  //Select the most suitable protocol
1586  if(n >= 6 && !osStrcasecmp(name + n - 6, ".local"))
1587  {
1588 #if (MDNS_CLIENT_SUPPORT == ENABLED)
1589  //Use mDNS to resolve the specified host name
1591 #endif
1592  }
1593  else if(n <= 15 && !osStrchr(name, '.') && type == HOST_TYPE_IPV4)
1594  {
1595 #if (NBNS_CLIENT_SUPPORT == ENABLED)
1596  //Use NetBIOS Name Service to resolve the specified host name
1598 #elif (LLMNR_CLIENT_SUPPORT == ENABLED)
1599  //Use LLMNR to resolve the specified host name
1601 #endif
1602  }
1603  else if(!osStrchr(name, '.'))
1604  {
1605 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
1606  //Use LLMNR to resolve the specified host name
1608 #endif
1609  }
1610  }
1611 
1612 #if (DNS_CLIENT_SUPPORT == ENABLED)
1613  //Use DNS protocol?
1615  {
1616  //Perform host name resolution
1617  error = dnsResolve(interface, name, type, ipAddr);
1618  }
1619  else
1620 #endif
1621 #if (MDNS_CLIENT_SUPPORT == ENABLED)
1622  //Use mDNS protocol?
1624  {
1625  //Perform host name resolution
1626  error = mdnsClientResolve(interface, name, type, ipAddr);
1627  }
1628  else
1629 #endif
1630 #if (NBNS_CLIENT_SUPPORT == ENABLED && IPV4_SUPPORT == ENABLED)
1631  //Use NetBIOS Name Service protocol?
1633  {
1634  //Perform host name resolution
1635  error = nbnsResolve(interface, name, ipAddr);
1636  }
1637  else
1638 #endif
1639 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
1640  //Use LLMNR protocol?
1642  {
1643  //Perform host name resolution
1644  error = llmnrResolve(interface, name, type, ipAddr);
1645  }
1646  else
1647 #endif
1648  //Invalid protocol?
1649  {
1650  //Report an error
1651  error = ERROR_INVALID_PARAMETER;
1652  }
1653  }
1654 
1655  //Return status code
1656  return error;
1657 }
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:760
uint8_t length
Definition: coap_common.h:193
#define htons(value)
Definition: cpu_endian.h:413
error_t rawSocketSendEthPacket(Socket *socket, const SocketMsg *message, uint_t flags)
Send a raw Ethernet packet.
Definition: raw_socket.c:634
HostType
Host types.
Definition: socket.h:174
#define osStrchr(s, c)
Definition: os_port.h:185
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:596
error_t socketSetDscp(Socket *socket, uint8_t dscp)
Set DSCP field.
Definition: socket.c:220
HostnameResolver
Name resolution protocols.
Definition: socket.h:186
int bool_t
Definition: compiler_port.h:48
__start_packed struct @2 EthHeader
Ethernet frame header.
@ HOST_NAME_RESOLVER_DNS
Definition: socket.h:188
uint8_t data[]
Definition: ethernet.h:220
#define netMutex
Definition: net_legacy.h:266
IP network address.
Definition: ip.h:79
@ ERROR_BUFFER_OVERFLOW
Definition: error.h:141
@ ERROR_NOT_IMPLEMENTED
Definition: error.h:66
@ HOST_TYPE_ANY
Definition: socket.h:175
error_t tcpReceive(Socket *socket, uint8_t *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: tcp.c:604
#define TRUE
Definition: os_port.h:50
Message and ancillary data.
Definition: socket.h:200
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1331
Event object.
@ ERROR_OUT_OF_RESOURCES
Definition: error.h:64
@ HOST_NAME_RESOLVER_LLMNR
Definition: socket.h:191
@ SOCKET_TYPE_DGRAM
Definition: socket.h:79
char_t name[]
uint16_t destPort
Definition: tcp.h:338
#define osStrlen(s)
Definition: os_port.h:155
Ipv4Addr srcIpAddr
Definition: ipcp.h:77
struct _SocketQueueItem * next
Definition: socket.h:231
error_t tcpListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: tcp.c:241
@ SOCKET_TYPE_STREAM
Definition: socket.h:78
error_t socketGetRemoteAddr(Socket *socket, IpAddr *remoteIpAddr, uint16_t *remotePort)
Retrieve the address of the peer to which a socket is connected.
Definition: socket.c:1259
error_t udpReceiveDatagram(Socket *socket, SocketMsg *message, uint_t flags)
Receive data from a UDP socket.
Definition: udp.c:689
Structure describing socket events.
Definition: socket.h:365
error_t socketSetVmanDei(Socket *socket, bool_t dei)
Set VMAN DEI flag.
Definition: socket.c:361
error_t rawSocketSendIpPacket(Socket *socket, const SocketMsg *message, uint_t flags)
Send a raw IP packet.
Definition: raw_socket.c:461
error_t socketSetTxBufferSize(Socket *socket, size_t size)
Specify the size of the send buffer.
Definition: socket.c:485
@ HOST_TYPE_IPV6
Definition: socket.h:177
error_t ipStringToAddr(const char_t *str, IpAddr *ipAddr)
Convert a string representation of an IP address to a binary IP address.
Definition: ip.c:762
@ SOCKET_SD_SEND
Definition: socket.h:143
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:908
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:53
error_t socketSetRxBufferSize(Socket *socket, size_t size)
Specify the size of the receive buffer.
Definition: socket.c:519
Helper functions for TCP.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
void osResetEvent(OsEvent *event)
Set the specified event object to the nonsignaled state.
error_t socketEnableKeepAlive(Socket *socket, bool_t enabled)
Enable TCP keep-alive.
Definition: socket.c:394
error_t dnsResolve(NetInterface *interface, const char_t *name, HostType type, IpAddr *ipAddr)
Resolve a host name using DNS.
Definition: dns_client.c:54
char_t type
error_t
Error codes.
Definition: error.h:43
#define TCP_MAX_RX_BUFFER_SIZE
Definition: tcp.h:89
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:966
error_t tcpSend(Socket *socket, const uint8_t *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: tcp.c:477
uint8_t protocol
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:63
@ HOST_NAME_RESOLVER_MDNS
Definition: socket.h:189
NBNS client (NetBIOS Name Service)
TcpState tcpGetState(Socket *socket)
Get the current state of the TCP FSM.
Definition: tcp.c:967
error_t socketSetVmanPcp(Socket *socket, uint8_t pcp)
Set VMAN priority.
Definition: socket.c:324
void osDeleteEvent(OsEvent *event)
Delete an event object.
@ ERROR_INVALID_SOCKET
Definition: error.h:82
#define NetInterface
Definition: net.h:36
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:282
@ ERROR_INVALID_LENGTH
Definition: error.h:110
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:410
#define osStrcasecmp(s1, s2)
Definition: os_port.h:173
error_t socketReceiveFrom(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, void *data, size_t size, size_t *received, uint_t flags)
Receive a datagram from a connectionless socket.
Definition: socket.c:988
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1168
error_t getHostByName(NetInterface *interface, const char_t *name, IpAddr *ipAddr, uint_t flags)
Resolve a host name into an IP address.
Definition: socket.c:1500
error_t socketSetTtl(Socket *socket, uint8_t ttl)
Set TTL value for unicast datagrams.
Definition: socket.c:170
mDNS client (Multicast DNS)
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:625
@ SOCKET_TYPE_RAW_IP
Definition: socket.h:80
error_t llmnrResolve(NetInterface *interface, const char_t *name, HostType type, IpAddr *ipAddr)
Resolve a host name using LLMNR.
Definition: llmnr_client.c:53
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:1294
error_t udpSendDatagram(Socket *socket, const SocketMsg *message, uint_t flags)
Send a UDP datagram.
Definition: udp.c:418
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:122
@ HOST_TYPE_IPV4
Definition: socket.h:176
uint_t eventFlags
Returned events.
Definition: socket.h:368
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:50
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:1391
#define TCP_MAX_TX_BUFFER_SIZE
Definition: tcp.h:75
error_t socketSetVlanDei(Socket *socket, bool_t dei)
Set VLAN DEI flag.
Definition: socket.c:291
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:726
DNS client (Domain Name System)
TCP/IP raw sockets.
error_t socketSetVlanPcp(Socket *socket, uint8_t pcp)
Set VLAN priority.
Definition: socket.c:254
error_t socketSetKeepAliveParams(Socket *socket, systime_t idle, systime_t interval, uint_t maxProbes)
Set TCP keep-alive parameters.
Definition: socket.c:443
uint32_t systime_t
System time.
@ TCP_STATE_CLOSED
Definition: tcp.h:268
#define ntohs(value)
Definition: cpu_endian.h:421
Receive queue item.
Definition: socket.h:230
uint8_t flags
Definition: tcp.h:349
@ ERROR_TIMEOUT
Definition: error.h:94
char char_t
Definition: compiler_port.h:43
LLMNR client (Link-Local Multicast Name Resolution)
Socket * socketAllocate(uint_t type, uint_t protocol)
Allocate a socket.
Definition: socket_misc.c:53
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:195
Helper functions for sockets.
error_t socketSetInterface(Socket *socket, NetInterface *interface)
Bind a socket to a particular network interface.
Definition: socket.c:553
@ HOST_NAME_RESOLVER_ANY
Definition: socket.h:187
uint8_t n
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
TCP (Transmission Control Protocol)
@ SOCKET_TYPE_UNUSED
Definition: socket.h:77
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
error_t socketGetLocalAddr(Socket *socket, IpAddr *localIpAddr, uint16_t *localPort)
Retrieve the local address for a given socket.
Definition: socket.c:1227
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
@ SOCKET_SD_RECEIVE
Definition: socket.h:142
error_t tcpAbort(Socket *socket)
Abort an existing TCP connection.
Definition: tcp.c:903
error_t rawSocketReceiveEthPacket(Socket *socket, SocketMsg *message, uint_t flags)
Receive an Ethernet packet from a raw socket.
Definition: raw_socket.c:830
error_t mdnsClientResolve(NetInterface *interface, const char_t *name, HostType type, IpAddr *ipAddr)
Resolve a host name using mDNS.
Definition: mdns_client.c:55
UDP (User Datagram Protocol)
#define Socket
Definition: socket.h:36
@ HOST_NAME_RESOLVER_NBNS
Definition: socket.h:190
NetInterface * socketGetInterface(Socket *socket)
Retrieve the underlying interface.
Definition: socket.c:573
bool_t osCreateEvent(OsEvent *event)
Create an event object.
uint8_t message[]
Definition: chap.h:152
Socket API.
error_t socketSendTo(Socket *socket, const IpAddr *destIpAddr, uint16_t destPort, const void *data, size_t length, size_t *written, uint_t flags)
Send a datagram to a specific destination.
Definition: socket.c:781
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:81
error_t socketSetMulticastTtl(Socket *socket, uint8_t ttl)
Set TTL value for multicast datagrams.
Definition: socket.c:195
error_t tcpConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a TCP connection.
Definition: tcp.c:116
error_t socketReceiveEx(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, IpAddr *destIpAddr, void *data, size_t size, size_t *received, uint_t flags)
Receive a datagram.
Definition: socket.c:1010
error_t tcpShutdown(Socket *socket, uint_t how)
Shutdown gracefully reception, transmission, or both.
Definition: tcp.c:752
uint32_t ttl
Definition: dns_common.h:205
uint16_t srcPort
Definition: tcp.h:337
Socket * tcpAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a TCP socket.
Definition: tcp.c:268
unsigned int uint_t
Definition: compiler_port.h:45
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:250
#define osMemset(p, value, length)
Definition: os_port.h:131
TCP/IP stack core.
error_t rawSocketReceiveIpPacket(Socket *socket, SocketMsg *message, uint_t flags)
Receive an IP packet from a raw socket.
Definition: raw_socket.c:726
#define SOCKET_MAX_COUNT
Definition: socket.h:46
@ SOCKET_SD_BOTH
Definition: socket.h:144
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:145
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:273
uint8_t ipAddr[4]
Definition: mib_common.h:187
NetBuffer * buffer
Definition: socket.h:236
uint_t eventMask
Requested events.
Definition: socket.h:367
error_t socketInit(void)
Socket related initialization.
Definition: socket.c:84
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t nbnsResolve(NetInterface *interface, const char_t *name, IpAddr *ipAddr)
Resolve a host name using NBNS.
Definition: nbns_client.c:53
systime_t osGetSystemTime(void)
Retrieve system time.
Ipv4Addr destIpAddr
Definition: ipcp.h:78
error_t socketListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: socket.c:689