bsd_socket.c
Go to the documentation of this file.
1 /**
2  * @file bsd_socket.c
3  * @brief BSD socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL BSD_SOCKET_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/bsd_socket.h"
37 #include "core/socket.h"
38 #include "core/socket_misc.h"
39 #include "debug.h"
40 
41 //Check TCP/IP stack configuration
42 #if (BSD_SOCKET_SUPPORT == ENABLED)
43 
44 //Dependencies
46 #include "core/bsd_socket_misc.h"
47 
48 //Common IPv6 addresses
49 const struct in6_addr in6addr_any =
50  {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
51 
52 const struct in6_addr in6addr_loopback =
53  {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}};
54 
55 
56 /**
57  * @brief Create a socket that is bound to a specific transport service provider
58  * @param[in] family Address family
59  * @param[in] type Type specification for the new socket
60  * @param[in] protocol Protocol to be used
61  * @return On success, a file descriptor for the new socket is returned.
62  * On failure, SOCKET_ERROR is returned
63  **/
64 
66 {
67  Socket *sock;
68 
69  //Check address family
70  if(family == AF_INET || family == AF_INET6)
71  {
72  //Create a socket
73  sock = socketOpen(type, protocol);
74  }
75  else if(family == AF_PACKET)
76  {
77  //Create a socket
79  }
80  else
81  {
82  //The address family is not valid
83  return SOCKET_ERROR;
84  }
85 
86  //Failed to create a new socket?
87  if(sock == NULL)
88  {
89  //Report an error
90  return SOCKET_ERROR;
91  }
92 
93  //Return the socket descriptor
94  return sock->descriptor;
95 }
96 
97 
98 /**
99  * @brief Associate a local address with a socket
100  * @param[in] s Descriptor identifying an unbound socket
101  * @param[in] addr Local address to assign to the bound socket
102  * @param[in] addrlen Length in bytes of the address
103  * @return If no error occurs, bind returns SOCKET_SUCCESS.
104  * Otherwise, it returns SOCKET_ERROR
105  **/
106 
107 int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen)
108 {
109  error_t error;
110  uint16_t port;
111  IpAddr ipAddr;
112  Socket *sock;
113 
114  //Make sure the socket descriptor is valid
115  if(s < 0 || s >= SOCKET_MAX_COUNT)
116  {
117  return SOCKET_ERROR;
118  }
119 
120  //Point to the socket structure
121  sock = &socketTable[s];
122 
123  //Check the length of the address
124  if(addrlen < (socklen_t) sizeof(SOCKADDR))
125  {
126  //Report an error
127  socketSetErrnoCode(sock, EINVAL);
128  return SOCKET_ERROR;
129  }
130 
131 #if (IPV4_SUPPORT == ENABLED)
132  //IPv4 address?
133  if(addr->sa_family == AF_INET &&
134  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
135  {
136  //Point to the IPv4 address information
137  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
138 
139  //Get port number
140  port = ntohs(sa->sin_port);
141 
142  //Copy IPv4 address
143  ipAddr.length = sizeof(Ipv4Addr);
144  ipAddr.ipv4Addr = sa->sin_addr.s_addr;
145  }
146  else
147 #endif
148 #if (IPV6_SUPPORT == ENABLED)
149  //IPv6 address?
150  if(addr->sa_family == AF_INET6 &&
151  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
152  {
153  //Point to the IPv6 address information
154  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
155 
156  //Get port number
157  port = ntohs(sa->sin6_port);
158 
159  //Copy IPv6 address
161  {
162  ipAddr.length = 0;
163  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
164  }
165  else
166  {
167  ipAddr.length = sizeof(Ipv6Addr);
168  ipv6CopyAddr(&ipAddr.ipv6Addr, sa->sin6_addr.s6_addr);
169  }
170  }
171  else
172 #endif
173  //Invalid address?
174  {
175  //Report an error
176  socketSetErrnoCode(sock, EINVAL);
177  return SOCKET_ERROR;
178  }
179 
180  //Associate the local address with the socket
181  error = socketBind(sock, &ipAddr, port);
182 
183  //Any error to report?
184  if(error)
185  {
186  socketTranslateErrorCode(sock, error);
187  return SOCKET_ERROR;
188  }
189 
190  //Successful processing
191  return SOCKET_SUCCESS;
192 }
193 
194 
195 /**
196  * @brief Establish a connection to a specified socket
197  * @param[in] s Descriptor identifying an unconnected socket
198  * @param[in] addr Address to which the connection should be established
199  * @param[in] addrlen Length in bytes of the address
200  * @return If no error occurs, connect returns SOCKET_SUCCESS.
201  * Otherwise, it returns SOCKET_ERROR
202  **/
203 
204 int_t connect(int_t s, const struct sockaddr *addr, socklen_t addrlen)
205 {
206  error_t error;
207  uint16_t port;
208  IpAddr ipAddr;
209  Socket *sock;
210 
211  //Make sure the socket descriptor is valid
212  if(s < 0 || s >= SOCKET_MAX_COUNT)
213  {
214  return SOCKET_ERROR;
215  }
216 
217  //Point to the socket structure
218  sock = &socketTable[s];
219 
220  //Check the length of the address
221  if(addrlen < (socklen_t) sizeof(SOCKADDR))
222  {
223  socketSetErrnoCode(sock, EINVAL);
224  return SOCKET_ERROR;
225  }
226 
227 #if (IPV4_SUPPORT == ENABLED)
228  //IPv4 address?
229  if(addr->sa_family == AF_INET &&
230  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
231  {
232  //Point to the IPv4 address information
233  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
234 
235  //Get port number
236  port = ntohs(sa->sin_port);
237 
238  //Copy IPv4 address
239  ipAddr.length = sizeof(Ipv4Addr);
240  ipAddr.ipv4Addr = sa->sin_addr.s_addr;
241  }
242  else
243 #endif
244 #if (IPV6_SUPPORT == ENABLED)
245  //IPv6 address?
246  if(addr->sa_family == AF_INET6 &&
247  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
248  {
249  //Point to the IPv6 address information
250  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
251 
252  //Get port number
253  port = ntohs(sa->sin6_port);
254 
255  //Copy IPv6 address
257  {
258  ipAddr.length = 0;
259  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
260  }
261  else
262  {
263  ipAddr.length = sizeof(Ipv6Addr);
264  ipv6CopyAddr(&ipAddr.ipv6Addr, sa->sin6_addr.s6_addr);
265  }
266  }
267  else
268 #endif
269  //Invalid address?
270  {
271  //Report an error
272  socketSetErrnoCode(sock, EINVAL);
273  return SOCKET_ERROR;
274  }
275 
276  //Establish connection
277  error = socketConnect(sock, &ipAddr, port);
278 
279  //Check status code
280  if(error == NO_ERROR)
281  {
282  //Successful processing
283  return SOCKET_SUCCESS;
284  }
285  else if(error == ERROR_TIMEOUT)
286  {
287  //Non-blocking socket?
288  if(sock->timeout == 0)
289  {
290  //The connection cannot be completed immediately
292  }
293  else
294  {
295  //Timeout while attempting connection
297  }
298 
299  //Report an error
300  return SOCKET_ERROR;
301  }
302  else
303  {
304  //Report an error
305  socketTranslateErrorCode(sock, error);
306  return SOCKET_ERROR;
307  }
308 }
309 
310 
311 /**
312  * @brief Place a socket in the listening state
313  *
314  * Place a socket in a state in which it is listening for an incoming connection
315  *
316  * @param[in] s Descriptor identifying a bound, unconnected socket
317  * @param[in] backlog Maximum length of the queue of pending connections
318  * @return If no error occurs, listen returns SOCKET_SUCCESS.
319  * Otherwise, it returns SOCKET_ERROR
320  **/
321 
323 {
324  error_t error;
325  Socket *sock;
326 
327  //Make sure the socket descriptor is valid
328  if(s < 0 || s >= SOCKET_MAX_COUNT)
329  {
330  return SOCKET_ERROR;
331  }
332 
333  //Point to the socket structure
334  sock = &socketTable[s];
335 
336  //Place the socket in the listening state
337  error = socketListen(sock, backlog);
338 
339  //Any error to report?
340  if(error)
341  {
342  socketTranslateErrorCode(sock, error);
343  return SOCKET_ERROR;
344  }
345 
346  //Successful processing
347  return SOCKET_SUCCESS;
348 }
349 
350 
351 /**
352  * @brief Permit an incoming connection attempt on a socket
353  * @param[in] s Descriptor that identifies a socket in the listening state
354  * @param[out] addr Address of the connecting entity (optional)
355  * @param[in,out] addrlen Length in bytes of the address (optional)
356  * @return If no error occurs, accept returns a descriptor for the new socket.
357  * Otherwise, it returns SOCKET_ERROR
358  **/
359 
360 int_t accept(int_t s, struct sockaddr *addr, socklen_t *addrlen)
361 {
362  uint16_t port;
363  IpAddr ipAddr;
364  Socket *sock;
365  Socket *newSock;
366 
367  //Make sure the socket descriptor is valid
368  if(s < 0 || s >= SOCKET_MAX_COUNT)
369  {
370  return SOCKET_ERROR;
371  }
372 
373  //Point to the socket structure
374  sock = &socketTable[s];
375 
376  //Permit an incoming connection attempt on a socket
377  newSock = socketAccept(sock, &ipAddr, &port);
378 
379  //No connection request is pending in the SYN queue?
380  if(newSock == NULL)
381  {
382  //Report an error
384  return SOCKET_ERROR;
385  }
386 
387  //The address parameter is optional
388  if(addr != NULL && addrlen != NULL)
389  {
390 #if (IPV4_SUPPORT == ENABLED)
391  //IPv4 address?
392  if(ipAddr.length == sizeof(Ipv4Addr) &&
393  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
394  {
395  //Point to the IPv4 address information
396  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
397 
398  //Set address family and port number
399  sa->sin_family = AF_INET;
400  sa->sin_port = htons(port);
401 
402  //Copy IPv4 address
403  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
404 
405  //Return the actual length of the address
406  *addrlen = sizeof(SOCKADDR_IN);
407  }
408  else
409 #endif
410 #if (IPV6_SUPPORT == ENABLED)
411  //IPv6 address?
412  if(ipAddr.length == sizeof(Ipv6Addr) &&
413  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
414  {
415  //Point to the IPv6 address information
416  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
417 
418  //Set address family and port number
419  sa->sin6_family = AF_INET6;
420  sa->sin6_port = htons(port);
421  sa->sin6_flowinfo = 0;
422  sa->sin6_scope_id = 0;
423 
424  //Copy IPv6 address
425  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
426 
427  //Return the actual length of the address
428  *addrlen = sizeof(SOCKADDR_IN6);
429  }
430  else
431 #endif
432  //Invalid address?
433  {
434  //Close socket
435  socketClose(newSock);
436 
437  //Report an error
438  socketSetErrnoCode(sock, EINVAL);
439  return SOCKET_ERROR;
440  }
441  }
442 
443  //Return the descriptor to the new socket
444  return newSock->descriptor;
445 }
446 
447 
448 /**
449  * @brief Send data to a connected socket
450  * @param[in] s Descriptor that identifies a connected socket
451  * @param[in] data Pointer to a buffer containing the data to be transmitted
452  * @param[in] length Number of bytes to be transmitted
453  * @param[in] flags Set of flags that influences the behavior of this function
454  * @return If no error occurs, send returns the total number of bytes sent,
455  * which can be less than the number requested to be sent in the
456  * length parameter. Otherwise, a value of SOCKET_ERROR is returned
457  **/
458 
459 int_t send(int_t s, const void *data, size_t length, int_t flags)
460 {
461  error_t error;
462  size_t written;
463  uint_t socketFlags;
464  Socket *sock;
465 
466  //Make sure the socket descriptor is valid
467  if(s < 0 || s >= SOCKET_MAX_COUNT)
468  {
469  return SOCKET_ERROR;
470  }
471 
472  //Point to the socket structure
473  sock = &socketTable[s];
474 
475  //The flags parameter can be used to influence the behavior of the function
476  socketFlags = 0;
477 
478  //The MSG_DONTROUTE flag specifies that the data should not be subject
479  //to routing
480  if((flags & MSG_DONTROUTE) != 0)
481  {
482  socketFlags |= SOCKET_FLAG_DONT_ROUTE;
483  }
484 
485  //The TCP_NODELAY option disables the Nagle algorithm for TCP sockets
486  if((sock->options & SOCKET_OPTION_TCP_NO_DELAY) != 0)
487  {
488  socketFlags |= SOCKET_FLAG_NO_DELAY;
489  }
490 
491  //Send data
492  error = socketSend(sock, data, length, &written, socketFlags);
493 
494  //Any error to report?
495  if(error == ERROR_TIMEOUT)
496  {
497  //Check whether some data has been written
498  if(written > 0)
499  {
500  //If a timeout error occurs and some data has been written, the
501  //count of bytes transferred so far is returned...
502  }
503  else
504  {
505  //If no data has been written, a value of SOCKET_ERROR is returned
506  socketTranslateErrorCode(sock, error);
507  return SOCKET_ERROR;
508  }
509  }
510  else if(error != NO_ERROR)
511  {
512  //Otherwise, a value of SOCKET_ERROR is returned
513  socketTranslateErrorCode(sock, error);
514  return SOCKET_ERROR;
515  }
516 
517  //Return the number of bytes transferred so far
518  return written;
519 }
520 
521 
522 /**
523  * @brief Send a datagram to a specific destination
524  * @param[in] s Descriptor that identifies a socket
525  * @param[in] data Pointer to a buffer containing the data to be transmitted
526  * @param[in] length Number of bytes to be transmitted
527  * @param[in] flags Set of flags that influences the behavior of this function
528  * @param[in] addr Destination address
529  * @param[in] addrlen Length in bytes of the destination address
530  * @return If no error occurs, sendto returns the total number of bytes sent,
531  * which can be less than the number requested to be sent in the
532  * length parameter. Otherwise, a value of SOCKET_ERROR is returned
533  **/
534 
535 int_t sendto(int_t s, const void *data, size_t length, int_t flags,
536  const struct sockaddr *addr, socklen_t addrlen)
537 {
538  error_t error;
539  size_t written;
540  uint_t socketFlags;
541  uint16_t port;
542  IpAddr ipAddr;
543  Socket *sock;
544 
545  //Make sure the socket descriptor is valid
546  if(s < 0 || s >= SOCKET_MAX_COUNT)
547  {
548  return SOCKET_ERROR;
549  }
550 
551  //Point to the socket structure
552  sock = &socketTable[s];
553 
554  //The flags parameter can be used to influence the behavior of the function
555  socketFlags = 0;
556 
557  //The MSG_DONTROUTE flag specifies that the data should not be subject
558  //to routing
559  if((flags & MSG_DONTROUTE) != 0)
560  {
561  socketFlags |= SOCKET_FLAG_DONT_ROUTE;
562  }
563 
564  //The TCP_NODELAY option disables the Nagle algorithm for TCP sockets
565  if((sock->options & SOCKET_OPTION_TCP_NO_DELAY) != 0)
566  {
567  socketFlags |= SOCKET_FLAG_NO_DELAY;
568  }
569 
570  //Check the length of the address
571  if(addrlen < (socklen_t) sizeof(SOCKADDR))
572  {
573  //Report an error
574  socketSetErrnoCode(sock, EINVAL);
575  return SOCKET_ERROR;
576  }
577 
578 #if (IPV4_SUPPORT == ENABLED)
579  //IPv4 address?
580  if(addr->sa_family == AF_INET &&
581  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
582  {
583  //Point to the IPv4 address information
584  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
585 
586  //Get port number
587  port = ntohs(sa->sin_port);
588 
589  //Copy IPv4 address
590  ipAddr.length = sizeof(Ipv4Addr);
591  ipAddr.ipv4Addr = sa->sin_addr.s_addr;
592  }
593  else
594 #endif
595 #if (IPV6_SUPPORT == ENABLED)
596  //IPv6 address?
597  if(addr->sa_family == AF_INET6 &&
598  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
599  {
600  //Point to the IPv6 address information
601  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
602 
603  //Get port number
604  port = ntohs(sa->sin6_port);
605 
606  //Copy IPv6 address
607  ipAddr.length = sizeof(Ipv6Addr);
608  ipv6CopyAddr(&ipAddr.ipv6Addr, sa->sin6_addr.s6_addr);
609  }
610  else
611 #endif
612  //Invalid address?
613  {
614  //Report an error
615  socketSetErrnoCode(sock, EINVAL);
616  return SOCKET_ERROR;
617  }
618 
619  //Send data
620  error = socketSendTo(sock, &ipAddr, port, data, length, &written,
621  socketFlags);
622 
623  //Any error to report?
624  if(error == ERROR_TIMEOUT)
625  {
626  //Check whether some data has been written
627  if(written > 0)
628  {
629  //If a timeout error occurs and some data has been written, the
630  //count of bytes transferred so far is returned...
631  }
632  else
633  {
634  //If no data has been written, a value of SOCKET_ERROR is returned
635  socketTranslateErrorCode(sock, error);
636  return SOCKET_ERROR;
637  }
638  }
639  else if(error != NO_ERROR)
640  {
641  //Otherwise, a value of SOCKET_ERROR is returned
642  socketTranslateErrorCode(sock, error);
643  return SOCKET_ERROR;
644  }
645 
646  //Return the number of bytes transferred so far
647  return written;
648 }
649 
650 
651 /**
652  * @brief Send a message
653  * @param[in] s Descriptor that identifies a socket
654  * @param[in] msg Pointer to the structure describing the message
655  * @param[in] flags Set of flags that influences the behavior of this function
656  * @return If no error occurs, sendmsg returns the total number of bytes sent,
657  * which can be less than the number requested to be sent in the
658  * length parameter. Otherwise, a value of SOCKET_ERROR is returned
659  **/
660 
662 {
663  error_t error;
664  uint_t socketFlags;
665  Socket *sock;
667  SOCKADDR *addr;
668 
669  //Make sure the socket descriptor is valid
670  if(s < 0 || s >= SOCKET_MAX_COUNT)
671  {
672  return SOCKET_ERROR;
673  }
674 
675  //Point to the socket structure
676  sock = &socketTable[s];
677 
678  //Check parameters
679  if(msg == NULL || msg->msg_iov == NULL || msg->msg_iovlen != 1)
680  {
681  socketSetErrnoCode(sock, EINVAL);
682  return SOCKET_ERROR;
683  }
684 
685  //Point to the message to be transmitted
687  message.data = msg->msg_iov[0].iov_base;
688  message.length = msg->msg_iov[0].iov_len;
689 
690  //Check the length of the address
691  if(msg->msg_namelen < (socklen_t) sizeof(SOCKADDR))
692  {
693  //Report an error
694  socketSetErrnoCode(sock, EINVAL);
695  return SOCKET_ERROR;
696  }
697 
698  //Point to the destination address
699  addr = (SOCKADDR *) msg->msg_name;
700 
701 #if (IPV4_SUPPORT == ENABLED)
702  //IPv4 address?
703  if(addr->sa_family == AF_INET &&
704  msg->msg_namelen >= (socklen_t) sizeof(SOCKADDR_IN))
705  {
706  //Point to the IPv4 address information
707  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
708 
709  //Get port number
710  message.destPort = ntohs(sa->sin_port);
711 
712  //Copy IPv4 address
713  message.destIpAddr.length = sizeof(Ipv4Addr);
714  message.destIpAddr.ipv4Addr = sa->sin_addr.s_addr;
715  }
716  else
717 #endif
718 #if (IPV6_SUPPORT == ENABLED)
719  //IPv6 address?
720  if(addr->sa_family == AF_INET6 &&
721  msg->msg_namelen >= (socklen_t) sizeof(SOCKADDR_IN6))
722  {
723  //Point to the IPv6 address information
724  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
725 
726  //Get port number
727  message.destPort = ntohs(sa->sin6_port);
728 
729  //Copy IPv6 address
730  message.destIpAddr.length = sizeof(Ipv6Addr);
731  ipv6CopyAddr(&message.destIpAddr.ipv6Addr, sa->sin6_addr.s6_addr);
732  }
733  else
734 #endif
735  //Invalid address?
736  {
737  //Report an error
738  socketSetErrnoCode(sock, EINVAL);
739  return SOCKET_ERROR;
740  }
741 
742  //The ancillary data buffer parameter is optional
743  if(msg->msg_control != NULL)
744  {
745  uint_t n;
746  int_t *val;
747  CMSGHDR *cmsg;
748 
749  //Point to the first control message
750  n = 0;
751 
752  //Loop through control messages
753  while((n + sizeof(CMSGHDR)) <= msg->msg_controllen)
754  {
755  //Point to the ancillary data header
756  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
757 
758  //Check the length of the control message
759  if(cmsg->cmsg_len >= sizeof(CMSGHDR) &&
760  cmsg->cmsg_len <= (msg->msg_controllen - n))
761  {
762 #if (IPV4_SUPPORT == ENABLED)
763  //IPv4 protocol?
764  if(addr->sa_family == AF_INET && cmsg->cmsg_level == IPPROTO_IP)
765  {
766  //Check control message type
767  if(cmsg->cmsg_type == IP_PKTINFO &&
768  cmsg->cmsg_len >= CMSG_LEN(sizeof(IN_PKTINFO)))
769  {
770  //Point to the ancillary data value
771  IN_PKTINFO *pktInfo = (IN_PKTINFO *) CMSG_DATA(cmsg);
772 
773  //Specify source IPv4 address
774  message.srcIpAddr.length = sizeof(Ipv4Addr);
775  message.srcIpAddr.ipv4Addr = pktInfo->ipi_addr.s_addr;
776  }
777  else if(cmsg->cmsg_type == IP_TOS &&
778  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
779  {
780  //Point to the ancillary data value
781  val = (int_t *) CMSG_DATA(cmsg);
782  //Specify ToS value
783  message.tos = (uint8_t) *val;
784  }
785  else if(cmsg->cmsg_type == IP_TTL &&
786  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
787  {
788  //Point to the ancillary data value
789  val = (int_t *) CMSG_DATA(cmsg);
790  //Specify TTL value
791  message.ttl = (uint8_t) *val;
792  }
793  else if(cmsg->cmsg_type == IP_DONTFRAG &&
794  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
795  {
796  //Point to the ancillary data value
797  val = (int_t *) CMSG_DATA(cmsg);
798 
799  //This option can be used to set the "don't fragment" flag
800  //on IP packets
801  message.dontFrag = (*val != 0) ? TRUE : FALSE;
802  }
803  else
804  {
805  //Unknown control message type
806  }
807  }
808  else
809 #endif
810 #if (IPV6_SUPPORT == ENABLED)
811  //IPv6 protocol?
812  if(addr->sa_family == AF_INET6 && cmsg->cmsg_level == IPPROTO_IPV6)
813  {
814  //Check control message type
815  if(cmsg->cmsg_type == IPV6_PKTINFO &&
816  cmsg->cmsg_len >= CMSG_LEN(sizeof(IN_PKTINFO)))
817  {
818  //Point to the ancillary data value
819  IN6_PKTINFO *pktInfo = (IN6_PKTINFO *) CMSG_DATA(cmsg);
820 
821  //Specify source IPv6 address
822  message.srcIpAddr.length = sizeof(Ipv6Addr);
823  ipv6CopyAddr(&message.srcIpAddr.ipv6Addr, pktInfo->ipi6_addr.s6_addr);
824  }
825  else if(cmsg->cmsg_type == IPV6_TCLASS &&
826  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
827  {
828  //Point to the ancillary data value
829  val = (int_t *) CMSG_DATA(cmsg);
830  //Specify Traffic Class value
831  message.tos = (uint8_t) *val;
832  }
833  else if(cmsg->cmsg_type == IPV6_HOPLIMIT &&
834  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
835  {
836  //Point to the ancillary data value
837  val = (int_t *) CMSG_DATA(cmsg);
838  //Specify Hop Limit value
839  message.ttl = (uint8_t) *val;
840  }
841  else if(cmsg->cmsg_type == IPV6_DONTFRAG &&
842  cmsg->cmsg_len >= CMSG_LEN(sizeof(int_t)))
843  {
844  //Point to the ancillary data value
845  val = (int_t *) CMSG_DATA(cmsg);
846 
847  //This option be used to turn off the automatic inserting
848  //of a fragment header for UDP and raw sockets
849  message.dontFrag = (*val != 0) ? TRUE : FALSE;
850  }
851  else
852  {
853  //Unknown control message type
854  }
855  }
856  //Unknown protocol?
857  else
858 #endif
859  {
860  //Discard control message
861  }
862 
863  //Next control message
864  n += cmsg->cmsg_len;
865  }
866  else
867  {
868  //Malformed control message
869  break;
870  }
871  }
872  }
873 
874  //The flags parameter can be used to influence the behavior of the function
875  socketFlags = 0;
876 
877  //The MSG_DONTROUTE flag specifies that the data should not be subject
878  //to routing
879  if((flags & MSG_DONTROUTE) != 0)
880  {
881  socketFlags |= SOCKET_FLAG_DONT_ROUTE;
882  }
883 
884  //The TCP_NODELAY option disables the Nagle algorithm for TCP sockets
885  if((sock->options & SOCKET_OPTION_TCP_NO_DELAY) != 0)
886  {
887  socketFlags |= SOCKET_FLAG_NO_DELAY;
888  }
889 
890  //Send message
891  error = socketSendMsg(sock, &message, socketFlags);
892 
893  //Any error to report?
894  if(error != NO_ERROR)
895  {
896  //Otherwise, a value of SOCKET_ERROR is returned
897  socketTranslateErrorCode(sock, error);
898  return SOCKET_ERROR;
899  }
900 
901  //Return the number of bytes transferred so far
902  return message.length;
903 }
904 
905 
906 /**
907  * @brief Receive data from a connected socket
908  * @param[in] s Descriptor that identifies a connected socket
909  * @param[out] data Buffer where to store the incoming data
910  * @param[in] size Maximum number of bytes that can be received
911  * @param[in] flags Set of flags that influences the behavior of this function
912  * @return If no error occurs, recv returns the number of bytes received. If the
913  * connection has been gracefully closed, the return value is zero.
914  * Otherwise, a value of SOCKET_ERROR is returned
915  **/
916 
917 int_t recv(int_t s, void *data, size_t size, int_t flags)
918 {
919  error_t error;
920  size_t received;
921  uint_t socketFlags;
922  Socket *sock;
923 
924  //Make sure the socket descriptor is valid
925  if(s < 0 || s >= SOCKET_MAX_COUNT)
926  {
927  return SOCKET_ERROR;
928  }
929 
930  //Point to the socket structure
931  sock = &socketTable[s];
932 
933  //The flags parameter can be used to influence the behavior of the function
934  socketFlags = 0;
935 
936  //When the MSG_PEEK flag is specified, the data is copied into the buffer,
937  //but is not removed from the input queue
938  if((flags & MSG_PEEK) != 0)
939  {
940  socketFlags |= SOCKET_FLAG_PEEK;
941  }
942 
943  //When the MSG_WAITALL flag is specified, the receive request will complete
944  //when the buffer supplied by the caller is completely full
945  if((flags & MSG_WAITALL) != 0)
946  {
947  socketFlags |= SOCKET_FLAG_WAIT_ALL;
948  }
949 
950  //The MSG_DONTWAIT flag enables non-blocking operation
951  if((flags & MSG_DONTWAIT) != 0)
952  {
953  socketFlags |= SOCKET_FLAG_DONT_WAIT;
954  }
955 
956  //Receive data
957  error = socketReceive(sock, data, size, &received, socketFlags);
958 
959  //Any error to report?
960  if(error == ERROR_END_OF_STREAM)
961  {
962  //If the connection has been gracefully closed, the return value is zero
963  return 0;
964  }
965  else if(error != NO_ERROR)
966  {
967  //Otherwise, a value of SOCKET_ERROR is returned
968  socketTranslateErrorCode(sock, error);
969  return SOCKET_ERROR;
970  }
971 
972  //Return the number of bytes received
973  return received;
974 }
975 
976 
977 /**
978  * @brief Receive a datagram
979  * @param[in] s Descriptor that identifies a socket
980  * @param[out] data Buffer where to store the incoming data
981  * @param[in] size Maximum number of bytes that can be received
982  * @param[in] flags Set of flags that influences the behavior of this function
983  * @param[out] addr Source address upon return (optional)
984  * @param[in,out] addrlen Length in bytes of the address (optional)
985  * @return If no error occurs, recvfrom returns the number of bytes received.
986  * Otherwise, a value of SOCKET_ERROR is returned
987  **/
988 
989 int_t recvfrom(int_t s, void *data, size_t size, int_t flags,
990  struct sockaddr *addr, socklen_t *addrlen)
991 {
992  error_t error;
993  size_t received;
994  uint_t socketFlags;
995  uint16_t port;
996  IpAddr ipAddr;
997  Socket *sock;
998 
999  //Make sure the socket descriptor is valid
1000  if(s < 0 || s >= SOCKET_MAX_COUNT)
1001  {
1002  return SOCKET_ERROR;
1003  }
1004 
1005  //Point to the socket structure
1006  sock = &socketTable[s];
1007 
1008  //The flags parameter can be used to influence the behavior of the function
1009  socketFlags = 0;
1010 
1011  //When the MSG_PEEK flag is specified, the data is copied into the buffer,
1012  //but is not removed from the input queue
1013  if((flags & MSG_PEEK) != 0)
1014  {
1015  socketFlags |= SOCKET_FLAG_PEEK;
1016  }
1017 
1018  //When the MSG_WAITALL flag is specified, the receive request will complete
1019  //when the buffer supplied by the caller is completely full
1020  if((flags & MSG_WAITALL) != 0)
1021  {
1022  socketFlags |= SOCKET_FLAG_WAIT_ALL;
1023  }
1024 
1025  //The MSG_DONTWAIT flag enables non-blocking operation
1026  if((flags & MSG_DONTWAIT) != 0)
1027  {
1028  socketFlags |= SOCKET_FLAG_DONT_WAIT;
1029  }
1030 
1031  //Receive data
1032  error = socketReceiveFrom(sock, &ipAddr, &port, data, size, &received,
1033  socketFlags);
1034 
1035  //Any error to report?
1036  if(error == ERROR_END_OF_STREAM)
1037  {
1038  //If the connection has been gracefully closed, the return value is zero
1039  return 0;
1040  }
1041  else if(error != NO_ERROR)
1042  {
1043  //Otherwise, a value of SOCKET_ERROR is returned
1044  socketTranslateErrorCode(sock, error);
1045  return SOCKET_ERROR;
1046  }
1047 
1048  //The address parameter is optional
1049  if(addr != NULL && addrlen != NULL)
1050  {
1051 #if (IPV4_SUPPORT == ENABLED)
1052  //IPv4 address?
1053  if(ipAddr.length == sizeof(Ipv4Addr) &&
1054  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
1055  {
1056  //Point to the IPv4 address information
1057  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
1058 
1059  //Set address family and port number
1060  sa->sin_family = AF_INET;
1061  sa->sin_port = htons(port);
1062 
1063  //Copy IPv4 address
1064  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
1065 
1066  //Return the actual length of the address
1067  *addrlen = sizeof(SOCKADDR_IN);
1068  }
1069  else
1070 #endif
1071 #if (IPV6_SUPPORT == ENABLED)
1072  //IPv6 address?
1073  if(ipAddr.length == sizeof(Ipv6Addr) &&
1074  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
1075  {
1076  //Point to the IPv6 address information
1077  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
1078 
1079  //Set address family and port number
1080  sa->sin6_family = AF_INET6;
1081  sa->sin6_port = htons(port);
1082  sa->sin6_flowinfo = 0;
1083  sa->sin6_scope_id = 0;
1084 
1085  //Copy IPv6 address
1086  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
1087 
1088  //Return the actual length of the address
1089  *addrlen = sizeof(SOCKADDR_IN6);
1090  }
1091  else
1092 #endif
1093  //Invalid address?
1094  {
1095  //Report an error
1096  socketSetErrnoCode(sock, EINVAL);
1097  return SOCKET_ERROR;
1098  }
1099  }
1100 
1101  //Return the number of bytes received
1102  return received;
1103 }
1104 
1105 
1106 /**
1107  * @brief Receive a message
1108  * @param[in] s Descriptor that identifies a socket
1109  * @param[in,out] msg Pointer to the structure describing the message
1110  * @param[in] flags Set of flags that influences the behavior of this function
1111  * @return If no error occurs, recvmsg returns the number of bytes received.
1112  * Otherwise, a value of SOCKET_ERROR is returned
1113  **/
1114 
1116 {
1117  error_t error;
1118  size_t n;
1119  uint_t socketFlags;
1120  Socket *sock;
1122 
1123  //Make sure the socket descriptor is valid
1124  if(s < 0 || s >= SOCKET_MAX_COUNT)
1125  {
1126  return SOCKET_ERROR;
1127  }
1128 
1129  //Point to the socket structure
1130  sock = &socketTable[s];
1131 
1132  //Check parameters
1133  if(msg == NULL || msg->msg_iov == NULL || msg->msg_iovlen != 1)
1134  {
1135  socketSetErrnoCode(sock, EINVAL);
1136  return SOCKET_ERROR;
1137  }
1138 
1139  //Point to the receive buffer
1141  message.data = msg->msg_iov[0].iov_base;
1142  message.size = msg->msg_iov[0].iov_len;
1143 
1144  //The flags parameter can be used to influence the behavior of the function
1145  socketFlags = 0;
1146 
1147  //When the MSG_PEEK flag is specified, the data is copied into the buffer,
1148  //but is not removed from the input queue
1149  if((flags & MSG_PEEK) != 0)
1150  {
1151  socketFlags |= SOCKET_FLAG_PEEK;
1152  }
1153 
1154  //When the MSG_WAITALL flag is specified, the receive request will complete
1155  //when the buffer supplied by the caller is completely full
1156  if((flags & MSG_WAITALL) != 0)
1157  {
1158  socketFlags |= SOCKET_FLAG_WAIT_ALL;
1159  }
1160 
1161  //The MSG_DONTWAIT flag enables non-blocking operation
1162  if((flags & MSG_DONTWAIT) != 0)
1163  {
1164  socketFlags |= SOCKET_FLAG_DONT_WAIT;
1165  }
1166 
1167  //Receive message
1168  error = socketReceiveMsg(sock, &message, socketFlags);
1169 
1170  //Any error to report?
1171  if(error)
1172  {
1173  socketTranslateErrorCode(sock, error);
1174  return SOCKET_ERROR;
1175  }
1176 
1177  //The source address parameter is optional
1178  if(msg->msg_name != NULL)
1179  {
1180 #if (IPV4_SUPPORT == ENABLED)
1181  //IPv4 address?
1182  if(message.srcIpAddr.length == sizeof(Ipv4Addr) &&
1183  msg->msg_namelen >= (socklen_t) sizeof(SOCKADDR_IN))
1184  {
1185  //Point to the IPv4 address information
1186  SOCKADDR_IN *sa = (SOCKADDR_IN *) msg->msg_name;
1187 
1188  //Set address family and port number
1189  sa->sin_family = AF_INET;
1190  sa->sin_port = htons(message.srcPort);
1191 
1192  //Copy IPv4 address
1193  sa->sin_addr.s_addr = message.srcIpAddr.ipv4Addr;
1194 
1195  //Return the actual length of the address
1196  msg->msg_namelen = sizeof(SOCKADDR_IN);
1197  }
1198  else
1199 #endif
1200 #if (IPV6_SUPPORT == ENABLED)
1201  //IPv6 address?
1202  if(message.srcIpAddr.length == sizeof(Ipv6Addr) &&
1203  msg->msg_namelen >= (socklen_t) sizeof(SOCKADDR_IN6))
1204  {
1205  //Point to the IPv6 address information
1206  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) msg->msg_name;
1207 
1208  //Set address family and port number
1209  sa->sin6_family = AF_INET6;
1210  sa->sin6_port = htons(message.srcPort);
1211  sa->sin6_flowinfo = 0;
1212  sa->sin6_scope_id = 0;
1213 
1214  //Copy IPv6 address
1215  ipv6CopyAddr(sa->sin6_addr.s6_addr, &message.srcIpAddr.ipv6Addr);
1216 
1217  //Return the actual length of the address
1218  msg->msg_namelen = sizeof(SOCKADDR_IN6);
1219  }
1220  else
1221 #endif
1222  //Invalid address?
1223  {
1224  //Report an error
1225  socketSetErrnoCode(sock, EINVAL);
1226  return SOCKET_ERROR;
1227  }
1228  }
1229  else
1230  {
1231  msg->msg_namelen = 0;
1232  }
1233 
1234  //Clear flags
1235  msg->msg_flags = 0;
1236 
1237  //Length of the ancillary data buffer
1238  n = 0;
1239 
1240  //The ancillary data buffer parameter is optional
1241  if(msg->msg_control != NULL)
1242  {
1243 #if (IPV4_SUPPORT == ENABLED)
1244  //IPv4 address?
1245  if(message.destIpAddr.length == sizeof(Ipv4Addr))
1246  {
1247  int_t *val;
1248  CMSGHDR *cmsg;
1249  IN_PKTINFO *pktInfo;
1250 
1251  //The IP_PKTINFO option allows an application to enable or disable
1252  //the return of IPv4 packet information
1253  if((sock->options & SOCKET_OPTION_IPV4_PKT_INFO) != 0)
1254  {
1255  //Make sure there is enough room to add the control message
1256  if((n + CMSG_SPACE(sizeof(IN_PKTINFO))) <= msg->msg_controllen)
1257  {
1258  //Point to the ancillary data header
1259  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1260 
1261  //Format ancillary data header
1262  cmsg->cmsg_len = CMSG_LEN(sizeof(IN_PKTINFO));
1263  cmsg->cmsg_level = IPPROTO_IP;
1264  cmsg->cmsg_type = IP_PKTINFO;
1265 
1266  //Point to the ancillary data value
1267  pktInfo = (IN_PKTINFO *) CMSG_DATA(cmsg);
1268 
1269  //Format packet information
1270  pktInfo->ipi_ifindex = message.interface->index + 1;
1271  pktInfo->ipi_addr.s_addr = message.destIpAddr.ipv4Addr;
1272 
1273  //Adjust the actual length of the ancillary data buffer
1274  n += CMSG_SPACE(sizeof(IN_PKTINFO));
1275  }
1276  else
1277  {
1278  //When the control message buffer is too short to store all
1279  //messages, the MSG_CTRUNC flag must be set
1280  msg->msg_flags |= MSG_CTRUNC;
1281  }
1282  }
1283 
1284  //The IP_RECVTOS option allows an application to enable or disable
1285  //the return of ToS header field on received datagrams
1286  if((sock->options & SOCKET_OPTION_IPV4_RECV_TOS) != 0)
1287  {
1288  //Make sure there is enough room to add the control message
1289  if((n + CMSG_SPACE(sizeof(int_t))) <= msg->msg_controllen)
1290  {
1291  //Point to the ancillary data header
1292  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1293 
1294  //Format ancillary data header
1295  cmsg->cmsg_len = CMSG_LEN(sizeof(int_t));
1296  cmsg->cmsg_level = IPPROTO_IP;
1297  cmsg->cmsg_type = IP_TOS;
1298 
1299  //Point to the ancillary data value
1300  val = (int_t *) CMSG_DATA(cmsg);
1301  //Set ancillary data value
1302  *val = message.tos;
1303 
1304  //Adjust the actual length of the ancillary data buffer
1305  n += CMSG_SPACE(sizeof(int_t));
1306  }
1307  else
1308  {
1309  //When the control message buffer is too short to store all
1310  //messages, the MSG_CTRUNC flag must be set
1311  msg->msg_flags |= MSG_CTRUNC;
1312  }
1313  }
1314 
1315  //The IP_RECVTTL option allows an application to enable or disable
1316  //the return of TTL header field on received datagrams
1317  if((sock->options & SOCKET_OPTION_IPV4_RECV_TTL) != 0)
1318  {
1319  //Make sure there is enough room to add the control message
1320  if((n + CMSG_SPACE(sizeof(int_t))) <= msg->msg_controllen)
1321  {
1322  //Point to the ancillary data header
1323  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1324 
1325  //Format ancillary data header
1326  cmsg->cmsg_len = CMSG_LEN(sizeof(int_t));
1327  cmsg->cmsg_level = IPPROTO_IP;
1328  cmsg->cmsg_type = IP_TTL;
1329 
1330  //Point to the ancillary data value
1331  val = (int_t *) CMSG_DATA(cmsg);
1332  //Set ancillary data value
1333  *val = message.ttl;
1334 
1335  //Adjust the actual length of the ancillary data buffer
1336  n += CMSG_SPACE(sizeof(int_t));
1337  }
1338  else
1339  {
1340  //When the control message buffer is too short to store all
1341  //messages, the MSG_CTRUNC flag must be set
1342  msg->msg_flags |= MSG_CTRUNC;
1343  }
1344  }
1345  }
1346  else
1347 #endif
1348 #if (IPV6_SUPPORT == ENABLED)
1349  //IPv6 address?
1350  if(message.destIpAddr.length == sizeof(Ipv6Addr))
1351  {
1352  int_t *val;
1353  CMSGHDR *cmsg;
1354  IN6_PKTINFO *pktInfo;
1355 
1356  //The IPV6_PKTINFO option allows an application to enable or disable
1357  //the return of IPv6 packet information
1358  if((sock->options & SOCKET_OPTION_IPV6_PKT_INFO) != 0)
1359  {
1360  //Make sure there is enough room to add the control message
1361  if((n + CMSG_SPACE(sizeof(IN6_PKTINFO))) <= msg->msg_controllen)
1362  {
1363  //Point to the ancillary data header
1364  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1365 
1366  //Format ancillary data header
1367  cmsg->cmsg_len = CMSG_LEN(sizeof(IN6_PKTINFO));
1368  cmsg->cmsg_level = IPPROTO_IPV6;
1369  cmsg->cmsg_type = IPV6_PKTINFO;
1370 
1371  //Point to the ancillary data value
1372  pktInfo = (IN6_PKTINFO *) CMSG_DATA(cmsg);
1373 
1374  //Format packet information
1375  pktInfo->ipi6_ifindex = message.interface->index + 1;
1376  ipv6CopyAddr(pktInfo->ipi6_addr.s6_addr, &message.destIpAddr.ipv6Addr);
1377 
1378  //Adjust the actual length of the ancillary data buffer
1379  n += CMSG_SPACE(sizeof(IN6_PKTINFO));
1380  }
1381  else
1382  {
1383  //When the control message buffer is too short to store all
1384  //messages, the MSG_CTRUNC flag must be set
1385  msg->msg_flags |= MSG_CTRUNC;
1386  }
1387  }
1388 
1389  //The IPV6_RECVTCLASS option allows an application to enable or disable
1390  //the return of Traffic Class header field on received datagrams
1391  if((sock->options & SOCKET_OPTION_IPV6_RECV_TRAFFIC_CLASS) != 0)
1392  {
1393  //Make sure there is enough room to add the control message
1394  if((n + CMSG_SPACE(sizeof(int_t))) <= msg->msg_controllen)
1395  {
1396  //Point to the ancillary data header
1397  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1398 
1399  //Format ancillary data header
1400  cmsg->cmsg_len = CMSG_LEN(sizeof(int_t));
1401  cmsg->cmsg_level = IPPROTO_IPV6;
1402  cmsg->cmsg_type = IPV6_TCLASS;
1403 
1404  //Point to the ancillary data value
1405  val = (int_t *) CMSG_DATA(cmsg);
1406  //Set ancillary data value
1407  *val = message.tos;
1408 
1409  //Adjust the actual length of the ancillary data buffer
1410  n += CMSG_SPACE(sizeof(int_t));
1411  }
1412  else
1413  {
1414  //When the control message buffer is too short to store all
1415  //messages, the MSG_CTRUNC flag must be set
1416  msg->msg_flags |= MSG_CTRUNC;
1417  }
1418  }
1419 
1420  //The IPV6_RECVHOPLIMIT option allows an application to enable or
1421  //disable the return of Hop Limit header field on received datagrams
1422  if((sock->options & SOCKET_OPTION_IPV6_RECV_HOP_LIMIT) != 0)
1423  {
1424  //Make sure there is enough room to add the control message
1425  if((n + CMSG_SPACE(sizeof(int_t))) <= msg->msg_controllen)
1426  {
1427  //Point to the ancillary data header
1428  cmsg = (CMSGHDR *) ((uint8_t *) msg->msg_control + n);
1429 
1430  //Format ancillary data header
1431  cmsg->cmsg_len = CMSG_LEN(sizeof(int_t));
1432  cmsg->cmsg_level = IPPROTO_IPV6;
1433  cmsg->cmsg_type = IPV6_HOPLIMIT;
1434 
1435  //Point to the ancillary data value
1436  val = (int_t *) CMSG_DATA(cmsg);
1437  //Set ancillary data value
1438  *val = message.ttl;
1439 
1440  //Adjust the actual length of the ancillary data buffer
1441  n += CMSG_SPACE(sizeof(int_t));
1442  }
1443  else
1444  {
1445  //When the control message buffer is too short to store all
1446  //messages, the MSG_CTRUNC flag must be set
1447  msg->msg_flags |= MSG_CTRUNC;
1448  }
1449  }
1450  }
1451  else
1452 #endif
1453  //Invalid address?
1454  {
1455  //Just for sanity
1456  }
1457  }
1458 
1459  //Length of the actual length of the ancillary data buffer
1460  msg->msg_controllen = n;
1461 
1462  //Return the number of bytes received
1463  return message.length;
1464 }
1465 
1466 
1467 /**
1468  * @brief Retrieves the local name for a socket
1469  * @param[in] s Descriptor identifying a socket
1470  * @param[out] addr Address of the socket
1471  * @param[in,out] addrlen Length in bytes of the address
1472  * @return If no error occurs, getsockname returns SOCKET_SUCCESS
1473  * Otherwise, it returns SOCKET_ERROR
1474  **/
1475 
1477 {
1478  int_t ret;
1479  Socket *sock;
1480 
1481  //Make sure the socket descriptor is valid
1482  if(s < 0 || s >= SOCKET_MAX_COUNT)
1483  {
1484  return SOCKET_ERROR;
1485  }
1486 
1487  //Point to the socket structure
1488  sock = &socketTable[s];
1489 
1490  //Get exclusive access
1492 
1493  //Check whether the socket has been bound to an address
1494  if(sock->localIpAddr.length != 0)
1495  {
1496 #if (IPV4_SUPPORT == ENABLED)
1497  //IPv4 address?
1498  if(sock->localIpAddr.length == sizeof(Ipv4Addr) &&
1499  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
1500  {
1501  //Point to the IPv4 address information
1502  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
1503 
1504  //Set address family and port number
1505  sa->sin_family = AF_INET;
1506  sa->sin_port = htons(sock->localPort);
1507 
1508  //Copy IPv4 address
1509  sa->sin_addr.s_addr = sock->localIpAddr.ipv4Addr;
1510 
1511  //Return the actual length of the address
1512  *addrlen = sizeof(SOCKADDR_IN);
1513  //Successful processing
1514  ret = SOCKET_SUCCESS;
1515  }
1516  else
1517 #endif
1518 #if (IPV6_SUPPORT == ENABLED)
1519  //IPv6 address?
1520  if(sock->localIpAddr.length == sizeof(Ipv6Addr) &&
1521  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
1522  {
1523  //Point to the IPv6 address information
1524  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
1525 
1526  //Set address family and port number
1527  sa->sin6_family = AF_INET6;
1528  sa->sin6_port = htons(sock->localPort);
1529  sa->sin6_flowinfo = 0;
1530  sa->sin6_scope_id = 0;
1531 
1532  //Copy IPv6 address
1533  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sock->localIpAddr.ipv6Addr);
1534 
1535  //Return the actual length of the address
1536  *addrlen = sizeof(SOCKADDR_IN6);
1537  //Successful processing
1538  ret = SOCKET_SUCCESS;
1539  }
1540  else
1541 #endif
1542  {
1543  //The specified length is not valid
1544  socketSetErrnoCode(sock, EINVAL);
1545  ret = SOCKET_ERROR;
1546  }
1547  }
1548  else if(sock->remoteIpAddr.length != 0)
1549  {
1550 #if (IPV4_SUPPORT == ENABLED)
1551  //IPv4 address?
1552  if(sock->remoteIpAddr.length == sizeof(Ipv4Addr) &&
1553  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
1554  {
1555  //Point to the IPv4 address information
1556  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
1557 
1558  //Set address family and port number
1559  sa->sin_family = AF_INET;
1560  sa->sin_port = htons(sock->localPort);
1561 
1562  //The socket is not bound to any address
1563  sa->sin_addr.s_addr = INADDR_ANY;
1564 
1565  //Return the actual length of the address
1566  *addrlen = sizeof(SOCKADDR_IN);
1567  //Successful processing
1568  ret = SOCKET_SUCCESS;
1569  }
1570  else
1571 #endif
1572 #if (IPV6_SUPPORT == ENABLED)
1573  //IPv6 address?
1574  if(sock->remoteIpAddr.length == sizeof(Ipv6Addr) &&
1575  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
1576  {
1577  //Point to the IPv6 address information
1578  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
1579 
1580  //Set address family and port number
1581  sa->sin6_family = AF_INET6;
1582  sa->sin6_port = htons(sock->localPort);
1583  sa->sin6_flowinfo = 0;
1584  sa->sin6_scope_id = 0;
1585 
1586  //The socket is not bound to any address
1588 
1589  //Return the actual length of the address
1590  *addrlen = sizeof(SOCKADDR_IN6);
1591  //Successful processing
1592  ret = SOCKET_SUCCESS;
1593  }
1594  else
1595 #endif
1596  {
1597  //The specified length is not valid
1598  socketSetErrnoCode(sock, EINVAL);
1599  ret = SOCKET_ERROR;
1600  }
1601  }
1602  else
1603  {
1604  //The socket is not bound to any address
1606  ret = SOCKET_ERROR;
1607  }
1608 
1609  //Release exclusive access
1611 
1612  //return status code
1613  return ret;
1614 }
1615 
1616 
1617 /**
1618  * @brief Retrieves the address of the peer to which a socket is connected
1619  * @param[in] s Descriptor identifying a socket
1620  * @param[out] addr Address of the peer
1621  * @param[in,out] addrlen Length in bytes of the address
1622  * @return If no error occurs, getpeername returns SOCKET_SUCCESS
1623  * Otherwise, it returns SOCKET_ERROR
1624  **/
1625 
1627 {
1628  int_t ret;
1629  Socket *sock;
1630 
1631  //Make sure the socket descriptor is valid
1632  if(s < 0 || s >= SOCKET_MAX_COUNT)
1633  {
1634  return SOCKET_ERROR;
1635  }
1636 
1637  //Point to the socket structure
1638  sock = &socketTable[s];
1639 
1640  //Get exclusive access
1642 
1643  //Check whether the socket is connected to a peer
1644  if(sock->remoteIpAddr.length != 0)
1645  {
1646 #if (IPV4_SUPPORT == ENABLED)
1647  //IPv4 address?
1648  if(sock->remoteIpAddr.length == sizeof(Ipv4Addr) &&
1649  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
1650  {
1651  //Point to the IPv4 address information
1652  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
1653 
1654  //Set address family and port number
1655  sa->sin_family = AF_INET;
1656  sa->sin_port = htons(sock->remotePort);
1657 
1658  //Copy IPv4 address
1659  sa->sin_addr.s_addr = sock->remoteIpAddr.ipv4Addr;
1660 
1661  //Return the actual length of the address
1662  *addrlen = sizeof(SOCKADDR_IN);
1663  //Successful processing
1664  ret = SOCKET_SUCCESS;
1665  }
1666  else
1667 #endif
1668 #if (IPV6_SUPPORT == ENABLED)
1669  //IPv6 address?
1670  if(sock->remoteIpAddr.length == sizeof(Ipv6Addr) &&
1671  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
1672  {
1673  //Point to the IPv6 address information
1674  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
1675 
1676  //Set address family and port number
1677  sa->sin6_family = AF_INET6;
1678  sa->sin6_port = htons(sock->remotePort);
1679  sa->sin6_flowinfo = 0;
1680  sa->sin6_scope_id = 0;
1681 
1682  //Copy IPv6 address
1683  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sock->remoteIpAddr.ipv6Addr);
1684 
1685  //Return the actual length of the address
1686  *addrlen = sizeof(SOCKADDR_IN6);
1687  //Successful processing
1688  ret = SOCKET_SUCCESS;
1689  }
1690  else
1691 #endif
1692  {
1693  //The specified length is not valid
1694  socketSetErrnoCode(sock, EINVAL);
1695  ret = SOCKET_ERROR;
1696  }
1697  }
1698  else
1699  {
1700  //The socket is not connected to any peer
1702  ret = SOCKET_ERROR;
1703  }
1704 
1705  //Release exclusive access
1707 
1708  //return status code
1709  return ret;
1710 }
1711 
1712 
1713 /**
1714  * @brief The setsockopt function sets a socket option
1715  * @param[in] s Descriptor that identifies a socket
1716  * @param[in] level The level at which the option is defined
1717  * @param[in] optname The socket option for which the value is to be set
1718  * @param[in] optval A pointer to the buffer in which the value for the
1719  * requested option is specified
1720  * @param[in] optlen The size, in bytes, of the buffer pointed to by the optval
1721  * parameter
1722  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
1723  * Otherwise, it returns SOCKET_ERROR
1724  **/
1725 
1726 int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval,
1727  socklen_t optlen)
1728 {
1729  int_t ret;
1730  Socket *sock;
1731 
1732  //Make sure the socket descriptor is valid
1733  if(s < 0 || s >= SOCKET_MAX_COUNT)
1734  {
1735  return SOCKET_ERROR;
1736  }
1737 
1738  //Point to the socket structure
1739  sock = &socketTable[s];
1740 
1741  //Make sure the option is valid
1742  if(optval != NULL)
1743  {
1744  //Check option level
1745  if(level == SOL_SOCKET)
1746  {
1747  //Check option type
1748  if(optname == SO_REUSEADDR)
1749  {
1750  //Set SO_REUSEADDR option
1751  ret = socketSetSoReuseAddrOption(sock, optval, optlen);
1752  }
1753  else if(optname == SO_BROADCAST)
1754  {
1755  //Set SO_BROADCAST option
1756  ret = socketSetSoBroadcastOption(sock, optval, optlen);
1757  }
1758  else if(optname == SO_SNDTIMEO)
1759  {
1760  //Set SO_SNDTIMEO option
1761  ret = socketSetSoSndTimeoOption(sock, optval, optlen);
1762  }
1763  else if(optname == SO_RCVTIMEO)
1764  {
1765  //Set SO_RCVTIMEO option
1766  ret = socketSetSoRcvTimeoOption(sock, optval, optlen);
1767  }
1768  else if(optname == SO_SNDBUF)
1769  {
1770  //Set SO_SNDBUF option
1771  ret = socketSetSoSndBufOption(sock, optval, optlen);
1772  }
1773  else if(optname == SO_RCVBUF)
1774  {
1775  //Set SO_RCVBUF option
1776  ret = socketSetSoRcvBufOption(sock, optval, optlen);
1777  }
1778  else if(optname == SO_KEEPALIVE)
1779  {
1780  //Set SO_KEEPALIVE option
1781  ret = socketSetSoKeepAliveOption(sock, optval, optlen);
1782  }
1783  else if(optname == SO_NO_CHECK)
1784  {
1785  //Set SO_NO_CHECK option
1786  ret = socketSetSoNoCheckOption(sock, optval, optlen);
1787  }
1788  else
1789  {
1790  //Unknown option
1792  //Report an error
1793  ret = SOCKET_ERROR;
1794  }
1795  }
1796  else if(level == IPPROTO_IP)
1797  {
1798  //Check option type
1799  if(optname == IP_TOS)
1800  {
1801  //Set IP_TOS option
1802  ret = socketSetIpTosOption(sock, optval, optlen);
1803  }
1804  else if(optname == IP_TTL)
1805  {
1806  //Set IP_TTL option
1807  ret = socketSetIpTtlOption(sock, optval, optlen);
1808  }
1809  else if(optname == IP_MULTICAST_IF)
1810  {
1811  //Set IP_MULTICAST_IF option
1812  ret = socketSetIpMulticastIfOption(sock, optval, optlen);
1813  }
1814  else if(optname == IP_MULTICAST_TTL)
1815  {
1816  //Set IP_MULTICAST_TTL option
1817  ret = socketSetIpMulticastTtlOption(sock, optval, optlen);
1818  }
1819  else if(optname == IP_MULTICAST_LOOP)
1820  {
1821  //Set IP_MULTICAST_LOOP option
1822  ret = socketSetIpMulticastLoopOption(sock, optval, optlen);
1823  }
1824  else if(optname == IP_ADD_MEMBERSHIP)
1825  {
1826  //Set IP_ADD_MEMBERSHIP option
1827  ret = socketSetIpAddMembershipOption(sock, optval, optlen);
1828  }
1829  else if(optname == IP_DROP_MEMBERSHIP)
1830  {
1831  //Set IP_DROP_MEMBERSHIP option
1832  ret = socketSetIpDropMembershipOption(sock, optval, optlen);
1833  }
1834  else if(optname == IP_BLOCK_SOURCE)
1835  {
1836  //Set IP_BLOCK_SOURCE option
1837  ret = socketSetIpBlockSourceOption(sock, optval, optlen);
1838  }
1839  else if(optname == IP_UNBLOCK_SOURCE)
1840  {
1841  //Set IP_UNBLOCK_SOURCE option
1842  ret = socketSetIpUnblockSourceOption(sock, optval, optlen);
1843  }
1844  else if(optname == IP_ADD_SOURCE_MEMBERSHIP)
1845  {
1846  //Set IP_ADD_SOURCE_MEMBERSHIP option
1847  ret = socketSetIpAddSourceMembershipOption(sock, optval, optlen);
1848  }
1849  else if(optname == IP_DROP_SOURCE_MEMBERSHIP)
1850  {
1851  //Set IP_DROP_SOURCE_MEMBERSHIP option
1852  ret = socketSetIpDropSourceMembershipOption(sock, optval, optlen);
1853  }
1854  else if(optname == MCAST_JOIN_GROUP)
1855  {
1856  //Set MCAST_JOIN_GROUP option
1857  ret = socketSetMcastJoinGroupOption(sock, optval, optlen);
1858  }
1859  else if(optname == MCAST_LEAVE_GROUP)
1860  {
1861  //Set MCAST_LEAVE_GROUP option
1862  ret = socketSetMcastLeaveGroupOption(sock, optval, optlen);
1863  }
1864  else if(optname == MCAST_BLOCK_SOURCE)
1865  {
1866  //Set MCAST_BLOCK_SOURCE option
1867  ret = socketSetMcastBlockSourceOption(sock, optval, optlen);
1868  }
1869  else if(optname == MCAST_UNBLOCK_SOURCE)
1870  {
1871  //Set MCAST_UNBLOCK_SOURCE option
1872  ret = socketSetMcastUnblockSourceOption(sock, optval, optlen);
1873  }
1874  else if(optname == MCAST_JOIN_SOURCE_GROUP)
1875  {
1876  //Set MCAST_JOIN_SOURCE_GROUP option
1877  ret = socketSetMcastJoinSourceGroupOption(sock, optval, optlen);
1878  }
1879  else if(optname == MCAST_LEAVE_SOURCE_GROUP)
1880  {
1881  //Set MCAST_LEAVE_SOURCE_GROUP option
1882  ret = socketSetMcastLeaveSourceGroupOption(sock, optval, optlen);
1883  }
1884  else if(optname == IP_DONTFRAG)
1885  {
1886  //Set IP_DONTFRAG option
1887  ret = socketSetIpDontFragOption(sock, optval, optlen);
1888  }
1889  else if(optname == IP_PKTINFO)
1890  {
1891  //Set IP_PKTINFO option
1892  ret = socketSetIpPktInfoOption(sock, optval, optlen);
1893  }
1894  else if(optname == IP_RECVTOS)
1895  {
1896  //Set IP_RECVTOS option
1897  ret = socketSetIpRecvTosOption(sock, optval, optlen);
1898  }
1899  else if(optname == IP_RECVTTL)
1900  {
1901  //Set IP_RECVTTL option
1902  ret = socketSetIpRecvTtlOption(sock, optval, optlen);
1903  }
1904  else
1905  {
1906  //Unknown option
1908  ret = SOCKET_ERROR;
1909  }
1910  }
1911  else if(level == IPPROTO_IPV6)
1912  {
1913  //Check option type
1914  if(optname == IPV6_TCLASS)
1915  {
1916  //Set IPV6_TCLASS option
1917  ret = socketSetIpv6TrafficClassOption(sock, optval, optlen);
1918  }
1919  else if(optname == IPV6_UNICAST_HOPS)
1920  {
1921  //Set IPV6_UNICAST_HOPS option
1922  ret = socketSetIpv6UnicastHopsOption(sock, optval, optlen);
1923  }
1924  else if(optname == IPV6_MULTICAST_IF)
1925  {
1926  //Set IPV6_MULTICAST_IF option
1927  ret = socketSetIpv6MulticastIfOption(sock, optval, optlen);
1928  }
1929  else if(optname == IPV6_MULTICAST_HOPS)
1930  {
1931  //Set IPV6_MULTICAST_HOPS option
1932  ret = socketSetIpv6MulticastHopsOption(sock, optval, optlen);
1933  }
1934  else if(optname == IPV6_MULTICAST_LOOP)
1935  {
1936  //Set IPV6_MULTICAST_LOOP option
1937  ret = socketSetIpv6MulticastLoopOption(sock, optval, optlen);
1938  }
1939  else if(optname == IPV6_ADD_MEMBERSHIP)
1940  {
1941  //Set IPV6_ADD_MEMBERSHIP option
1942  ret = socketSetIpv6AddMembershipOption(sock, optval, optlen);
1943  }
1944  else if(optname == IPV6_DROP_MEMBERSHIP)
1945  {
1946  //Set IPV6_DROP_MEMBERSHIP option
1947  ret = socketSetIpv6DropMembershipOption(sock, optval, optlen);
1948  }
1949  else if(optname == MCAST_JOIN_GROUP)
1950  {
1951  //Set MCAST_JOIN_GROUP option
1952  ret = socketSetMcastJoinGroupOption(sock, optval, optlen);
1953  }
1954  else if(optname == MCAST_LEAVE_GROUP)
1955  {
1956  //Set MCAST_LEAVE_GROUP option
1957  ret = socketSetMcastLeaveGroupOption(sock, optval, optlen);
1958  }
1959  else if(optname == MCAST_BLOCK_SOURCE)
1960  {
1961  //Set MCAST_BLOCK_SOURCE option
1962  ret = socketSetMcastBlockSourceOption(sock, optval, optlen);
1963  }
1964  else if(optname == MCAST_UNBLOCK_SOURCE)
1965  {
1966  //Set MCAST_UNBLOCK_SOURCE option
1967  ret = socketSetMcastUnblockSourceOption(sock, optval, optlen);
1968  }
1969  else if(optname == MCAST_JOIN_SOURCE_GROUP)
1970  {
1971  //Set MCAST_JOIN_SOURCE_GROUP option
1972  ret = socketSetMcastJoinSourceGroupOption(sock, optval, optlen);
1973  }
1974  else if(optname == MCAST_LEAVE_SOURCE_GROUP)
1975  {
1976  //Set MCAST_LEAVE_SOURCE_GROUP option
1977  ret = socketSetMcastLeaveSourceGroupOption(sock, optval, optlen);
1978  }
1979  else if(optname == IPV6_V6ONLY)
1980  {
1981  //Set IPV6_V6ONLY option
1982  ret = socketSetIpv6OnlyOption(sock, optval, optlen);
1983  }
1984  else if(optname == IPV6_DONTFRAG)
1985  {
1986  //Set IPV6_DONTFRAG option
1987  ret = socketSetIpv6DontFragOption(sock, optval, optlen);
1988  }
1989  else if(optname == IPV6_PKTINFO)
1990  {
1991  //Set IPV6_PKTINFO option
1992  ret = socketSetIpv6PktInfoOption(sock, optval, optlen);
1993  }
1994  else if(optname == IPV6_RECVTCLASS)
1995  {
1996  //Set IPV6_RECVTCLASS option
1997  ret = socketSetIpv6RecvTrafficClassOption(sock, optval, optlen);
1998  }
1999  else if(optname == IPV6_RECVHOPLIMIT)
2000  {
2001  //Set IPV6_RECVHOPLIMIT option
2002  ret = socketSetIpv6RecvHopLimitOption(sock, optval, optlen);
2003  }
2004  else
2005  {
2006  //Unknown option
2008  ret = SOCKET_ERROR;
2009  }
2010  }
2011  else if(level == IPPROTO_TCP)
2012  {
2013  //Check option type
2014  if(optname == TCP_NODELAY)
2015  {
2016  //Set TCP_NODELAY option
2017  ret = socketSetTcpNoDelayOption(sock, optval, optlen);
2018  }
2019  else if(optname == TCP_MAXSEG)
2020  {
2021  //Set TCP_MAXSEG option
2022  ret = socketSetTcpMaxSegOption(sock, optval, optlen);
2023  }
2024  else if(optname == TCP_KEEPIDLE)
2025  {
2026  //Set TCP_KEEPIDLE option
2027  ret = socketSetTcpKeepIdleOption(sock, optval, optlen);
2028  }
2029  else if(optname == TCP_KEEPINTVL)
2030  {
2031  //Set TCP_KEEPINTVL option
2032  ret = socketSetTcpKeepIntvlOption(sock, optval, optlen);
2033  }
2034  else if(optname == TCP_KEEPCNT)
2035  {
2036  //Set TCP_KEEPCNT option
2037  ret = socketSetTcpKeepCntOption(sock, optval, optlen);
2038  }
2039  else
2040  {
2041  //Unknown option
2043  ret = SOCKET_ERROR;
2044  }
2045  }
2046  else
2047  {
2048  //The specified level is not valid
2049  socketSetErrnoCode(sock, EINVAL);
2050  ret = SOCKET_ERROR;
2051  }
2052  }
2053  else
2054  {
2055  //The option is not valid
2056  socketSetErrnoCode(sock, EFAULT);
2057  ret = SOCKET_ERROR;
2058  }
2059 
2060  //return status code
2061  return ret;
2062 }
2063 
2064 
2065 /**
2066  * @brief The getsockopt function retrieves a socket option
2067  * @param[in] s Descriptor that identifies a socket
2068  * @param[in] level The level at which the option is defined
2069  * @param[in] optname The socket option for which the value is to be retrieved
2070  * @param[out] optval A pointer to the buffer in which the value for the
2071  * requested option is to be returned
2072  * @param[in,out] optlen The size, in bytes, of the buffer pointed to by the
2073  * optval parameter
2074  * @return If no error occurs, getsockopt returns SOCKET_SUCCESS
2075  * Otherwise, it returns SOCKET_ERROR
2076  **/
2077 
2078 int_t getsockopt(int_t s, int_t level, int_t optname, void *optval,
2079  socklen_t *optlen)
2080 {
2081  int_t ret;
2082  Socket *sock;
2083 
2084  //Make sure the socket descriptor is valid
2085  if(s < 0 || s >= SOCKET_MAX_COUNT)
2086  {
2087  return SOCKET_ERROR;
2088  }
2089 
2090  //Point to the socket structure
2091  sock = &socketTable[s];
2092 
2093  //Get exclusive access
2095 
2096  //Make sure the option is valid
2097  if(optval != NULL)
2098  {
2099  //Check option level
2100  if(level == SOL_SOCKET)
2101  {
2102  //Check option type
2103  if(optname == SO_REUSEADDR)
2104  {
2105  //Get SO_REUSEADDR option
2106  ret = socketGetSoReuseAddrOption(sock, optval, optlen);
2107  }
2108  else if(optname == SO_TYPE)
2109  {
2110  //Get SO_TYPE option
2111  ret = socketGetSoTypeOption(sock, optval, optlen);
2112  }
2113  else if(optname == SO_ERROR)
2114  {
2115  //Get SO_ERROR option
2116  ret = socketGetSoErrorOption(sock, optval, optlen);
2117  }
2118  else if(optname == SO_BROADCAST)
2119  {
2120  //Get SO_BROADCAST option
2121  ret = socketGetSoBroadcastOption(sock, optval, optlen);
2122  }
2123  else if(optname == SO_SNDTIMEO)
2124  {
2125  //Get SO_SNDTIMEO option
2126  ret = socketGetSoSndTimeoOption(sock, optval, optlen);
2127  }
2128  else if(optname == SO_RCVTIMEO)
2129  {
2130  //Get SO_RCVTIMEO option
2131  ret = socketGetSoRcvTimeoOption(sock, optval, optlen);
2132  }
2133  else if(optname == SO_SNDBUF)
2134  {
2135  //Get SO_SNDBUF option
2136  ret = socketGetSoSndBufOption(sock, optval, optlen);
2137  }
2138  else if(optname == SO_RCVBUF)
2139  {
2140  //Get SO_RCVBUF option
2141  ret = socketGetSoRcvBufOption(sock, optval, optlen);
2142  }
2143  else if(optname == SO_KEEPALIVE)
2144  {
2145  //Get SO_KEEPALIVE option
2146  ret = socketGetSoKeepAliveOption(sock, optval, optlen);
2147  }
2148  else if(optname == SO_NO_CHECK)
2149  {
2150  //Get SO_NO_CHECK option
2151  ret = socketGetSoNoCheckOption(sock, optval, optlen);
2152  }
2153  else
2154  {
2155  //Unknown option
2157  ret = SOCKET_ERROR;
2158  }
2159  }
2160  else if(level == IPPROTO_IP)
2161  {
2162  //Check option type
2163  if(optname == IP_TOS)
2164  {
2165  //Get IP_TOS option
2166  ret = socketGetIpTosOption(sock, optval, optlen);
2167  }
2168  else if(optname == IP_TTL)
2169  {
2170  //Get IP_TTL option
2171  ret = socketGetIpTtlOption(sock, optval, optlen);
2172  }
2173  else if(optname == IP_MULTICAST_TTL)
2174  {
2175  //Get IP_MULTICAST_TTL option
2176  ret = socketGetIpMulticastTtlOption(sock, optval, optlen);
2177  }
2178  else if(optname == IP_MULTICAST_LOOP)
2179  {
2180  //Get IP_MULTICAST_LOOP option
2181  ret = socketGetIpMulticastLoopOption(sock, optval, optlen);
2182  }
2183  else if(optname == IP_ADD_MEMBERSHIP ||
2184  optname == IP_DROP_MEMBERSHIP ||
2185  optname == IP_BLOCK_SOURCE ||
2186  optname == IP_UNBLOCK_SOURCE ||
2187  optname == IP_ADD_SOURCE_MEMBERSHIP ||
2188  optname == IP_DROP_SOURCE_MEMBERSHIP ||
2189  optname == MCAST_JOIN_GROUP ||
2190  optname == MCAST_LEAVE_GROUP ||
2191  optname == MCAST_BLOCK_SOURCE ||
2192  optname == MCAST_UNBLOCK_SOURCE ||
2193  optname == MCAST_JOIN_SOURCE_GROUP ||
2194  optname == MCAST_LEAVE_SOURCE_GROUP)
2195  {
2196  //When any of these options are used with getsockopt, the error
2197  //generated is EOPNOTSUPP (refer to RFC 3678, section 4.1.3)
2199  ret = SOCKET_ERROR;
2200  }
2201  else if(optname == IP_DONTFRAG)
2202  {
2203  //Get IP_DONTFRAG option
2204  ret = socketGetIpDontFragOption(sock, optval, optlen);
2205  }
2206  else if(optname == IP_PKTINFO)
2207  {
2208  //Get IP_PKTINFO option
2209  ret = socketGetIpPktInfoOption(sock, optval, optlen);
2210  }
2211  else if(optname == IP_RECVTOS)
2212  {
2213  //Get IP_RECVTOS option
2214  ret = socketGetIpRecvTosOption(sock, optval, optlen);
2215  }
2216  else if(optname == IP_RECVTTL)
2217  {
2218  //Get IP_RECVTTL option
2219  ret = socketGetIpRecvTtlOption(sock, optval, optlen);
2220  }
2221  else
2222  {
2223  //Unknown option
2225  ret = SOCKET_ERROR;
2226  }
2227  }
2228  else if(level == IPPROTO_IPV6)
2229  {
2230  //Check option type
2231  if(optname == IPV6_TCLASS)
2232  {
2233  //Get IPV6_TCLASS option
2234  ret = socketGetIpv6TrafficClassOption(sock, optval, optlen);
2235  }
2236  else if(optname == IPV6_UNICAST_HOPS)
2237  {
2238  //Get IPV6_UNICAST_HOPS option
2239  ret = socketGetIpv6UnicastHopsOption(sock, optval, optlen);
2240  }
2241  else if(optname == IPV6_MULTICAST_HOPS)
2242  {
2243  //Get IPV6_MULTICAST_HOPS option
2244  ret = socketGetIpv6MulticastHopsOption(sock, optval, optlen);
2245  }
2246  else if(optname == IPV6_MULTICAST_LOOP)
2247  {
2248  //Get IPV6_MULTICAST_LOOP option
2249  ret = socketGetIpv6MulticastLoopOption(sock, optval, optlen);
2250  }
2251  else if(optname == IPV6_ADD_MEMBERSHIP ||
2252  optname == IPV6_DROP_MEMBERSHIP ||
2253  optname == MCAST_JOIN_GROUP ||
2254  optname == MCAST_LEAVE_GROUP ||
2255  optname == MCAST_BLOCK_SOURCE ||
2256  optname == MCAST_UNBLOCK_SOURCE ||
2257  optname == MCAST_JOIN_SOURCE_GROUP ||
2258  optname == MCAST_LEAVE_SOURCE_GROUP)
2259  {
2260  //When any of these options are used with getsockopt, the error
2261  //generated is EOPNOTSUPP
2263  ret = SOCKET_ERROR;
2264  }
2265  else if(optname == IPV6_V6ONLY)
2266  {
2267  //Get IPV6_V6ONLY option
2268  ret = socketGetIpv6OnlyOption(sock, optval, optlen);
2269  }
2270  else if(optname == IPV6_DONTFRAG)
2271  {
2272  //Get IPV6_DONTFRAG option
2273  ret = socketGetIpv6DontFragOption(sock, optval, optlen);
2274  }
2275  else if(optname == IPV6_PKTINFO)
2276  {
2277  //Get IPV6_PKTINFO option
2278  ret = socketGetIpv6PktInfoOption(sock, optval, optlen);
2279  }
2280  else if(optname == IPV6_RECVTCLASS)
2281  {
2282  //Get IPV6_RECVTCLASS option
2283  ret = socketGetIpv6RecvTrafficClassOption(sock, optval, optlen);
2284  }
2285  else if(optname == IPV6_RECVHOPLIMIT)
2286  {
2287  //Get IPV6_RECVHOPLIMIT option
2288  ret = socketGetIpv6RecvHopLimitOption(sock, optval, optlen);
2289  }
2290  else
2291  {
2292  //Unknown option
2294  ret = SOCKET_ERROR;
2295  }
2296  }
2297  else if(level == IPPROTO_TCP)
2298  {
2299  //Check option type
2300  if(optname == TCP_NODELAY)
2301  {
2302  //Get TCP_NODELAY option
2303  ret = socketGetTcpNoDelayOption(sock, optval, optlen);
2304  }
2305  else if(optname == TCP_MAXSEG)
2306  {
2307  //Get TCP_MAXSEG option
2308  ret = socketGetTcpMaxSegOption(sock, optval, optlen);
2309  }
2310  else if(optname == TCP_KEEPIDLE)
2311  {
2312  //Get TCP_KEEPIDLE option
2313  ret = socketGetTcpKeepIdleOption(sock, optval, optlen);
2314  }
2315  else if(optname == TCP_KEEPINTVL)
2316  {
2317  //Get TCP_KEEPINTVL option
2318  ret = socketGetTcpKeepIntvlOption(sock, optval, optlen);
2319  }
2320  else if(optname == TCP_KEEPCNT)
2321  {
2322  //Get TCP_KEEPCNT option
2323  ret = socketGetTcpKeepCntOption(sock, optval, optlen);
2324  }
2325  else
2326  {
2327  //Unknown option
2329  ret = SOCKET_ERROR;
2330  }
2331  }
2332  else
2333  {
2334  //The specified level is not valid
2335  socketSetErrnoCode(sock, EINVAL);
2336  ret = SOCKET_ERROR;
2337  }
2338  }
2339  else
2340  {
2341  //The option is not valid
2342  socketSetErrnoCode(sock, EFAULT);
2343  ret = SOCKET_ERROR;
2344  }
2345 
2346  //Release exclusive access
2348 
2349  //return status code
2350  return ret;
2351 }
2352 
2353 
2354 /**
2355  * @brief Set multicast source filter (IPv4 only)
2356  * @param[in] s Descriptor that identifies a socket
2357  * @param[in] interface Local IP address of the interface
2358  * @param[in] group IP multicast address of the group
2359  * @param[in] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2360  * @param[in] numsrc Number of source addresses in the slist array
2361  * @param[in] slist Array of IP addresses of sources to include or exclude
2362  * depending on the filter mode
2363  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2364  * Otherwise, it returns SOCKET_ERROR
2365  **/
2366 
2367 int_t setipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group,
2368  uint32_t fmode, uint_t numsrc, struct in_addr *slist)
2369 {
2370 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2371  error_t error;
2372  uint_t i;
2373  Socket *sock;
2374  IpFilterMode filterMode;
2375  IpAddr groupAddr;
2377 
2378  //Make sure the socket descriptor is valid
2379  if(s < 0 || s >= SOCKET_MAX_COUNT)
2380  {
2381  return SOCKET_ERROR;
2382  }
2383 
2384  //Point to the socket structure
2385  sock = &socketTable[s];
2386 
2387  //Copy group address
2388  groupAddr.length = sizeof(Ipv4Addr);
2389  groupAddr.ipv4Addr = group.s_addr;
2390 
2391  //Check filter mode
2392  if(fmode == MCAST_INCLUDE)
2393  {
2394  //Select source-specific filter mode
2395  filterMode = IP_FILTER_MODE_INCLUDE;
2396  }
2397  else if(fmode == MCAST_INCLUDE)
2398  {
2399  //Select any-source filter mode
2400  filterMode = IP_FILTER_MODE_EXCLUDE;
2401  }
2402  else
2403  {
2404  //Report an error
2405  socketSetErrnoCode(sock, EINVAL);
2406  return SOCKET_ERROR;
2407  }
2408 
2409  //If the implementation imposes a limit on the maximum number of sources
2410  //in a source filter, ENOBUFS is generated when the operation would exceed
2411  //the maximum (refer to RFC3678, section 4.2.1)
2412  if(numsrc > SOCKET_MAX_MULTICAST_SOURCES)
2413  {
2414  socketSetErrnoCode(sock, ENOBUFS);
2415  return SOCKET_ERROR;
2416  }
2417 
2418  //If numsrc is 0, a NULL pointer may be supplied
2419  if(numsrc > 0 && slist == NULL)
2420  {
2421  socketSetErrnoCode(sock, EINVAL);
2422  return SOCKET_ERROR;
2423  }
2424 
2425  //Copy the list of source addresses to include or exclude
2426  for(i = 0; i < numsrc; i++)
2427  {
2428  sources[i].length = sizeof(Ipv4Addr);
2429  sources[i].ipv4Addr = slist[i].s_addr;
2430  }
2431 
2432  //Set multicast source filter
2433  error = socketSetMulticastSourceFilter(sock, &groupAddr, filterMode,
2434  sources, numsrc);
2435 
2436  //Any error to report?
2437  if(error)
2438  {
2439  socketTranslateErrorCode(sock, error);
2440  return SOCKET_ERROR;
2441  }
2442 
2443  //Successful processing
2444  return SOCKET_SUCCESS;
2445 #else
2446  //Not implemented
2447  return SOCKET_ERROR;
2448 #endif
2449 }
2450 
2451 
2452 /**
2453  * @brief Get multicast source filter (IPv4 only)
2454  * @param[in] s Descriptor that identifies a socket
2455  * @param[in] interface Local IP address of the interface
2456  * @param[in] group IP multicast address of the group
2457  * @param[out] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2458  * @param[out] numsrc On input, the numsrc argument holds the number of source
2459  * addresses that will fit in the slist array. On output, the numsrc argument
2460  * will hold the total number of sources in the filter
2461  * @param[out] slist Buffer into which an array of IP addresses of included or
2462  * excluded (depending on the filter mode) sources will be written. If numsrc
2463  * was 0 on input, a NULL pointer may be supplied
2464  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2465  * Otherwise, it returns SOCKET_ERROR
2466  **/
2467 
2468 int_t getipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group,
2469  uint32_t *fmode, uint_t *numsrc, struct in_addr *slist)
2470 {
2471 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2472  error_t error;
2473  uint_t i;
2474  uint_t numSources;
2475  Socket *sock;
2476  IpFilterMode filterMode;
2477  IpAddr groupAddr;
2479 
2480  //Make sure the socket descriptor is valid
2481  if(s < 0 || s >= SOCKET_MAX_COUNT)
2482  {
2483  return SOCKET_ERROR;
2484  }
2485 
2486  //Point to the socket structure
2487  sock = &socketTable[s];
2488 
2489  //Check parameters
2490  if(fmode == NULL || numsrc == NULL)
2491  {
2492  socketSetErrnoCode(sock, EINVAL);
2493  return SOCKET_ERROR;
2494  }
2495 
2496  //If numsrc was 0 on input, a NULL pointer may be supplied
2497  if(*numsrc > 0 && slist == NULL)
2498  {
2499  socketSetErrnoCode(sock, EINVAL);
2500  return SOCKET_ERROR;
2501  }
2502 
2503  //Copy group address
2504  groupAddr.length = sizeof(Ipv4Addr);
2505  groupAddr.ipv4Addr = group.s_addr;
2506 
2507  //Set multicast source filter
2508  error = socketGetMulticastSourceFilter(sock, &groupAddr, &filterMode,
2509  sources, &numSources);
2510 
2511  //Any error to report?
2512  if(error)
2513  {
2514  socketTranslateErrorCode(sock, error);
2515  return SOCKET_ERROR;
2516  }
2517 
2518  //Filter mode
2519  if(filterMode == IP_FILTER_MODE_INCLUDE)
2520  {
2521  *fmode = MCAST_INCLUDE;
2522  }
2523  else
2524  {
2525  *fmode = MCAST_EXCLUDE;
2526  }
2527 
2528  //The slist parameter will hold as many source addresses as fit, up to the
2529  //minimum of the array size passed in as the original numsrc value and the
2530  //total number of sources in the filter (refer to RFC3678, section 4.2.2)
2531  for(i = 0; i < *numsrc && i < numSources; i++)
2532  {
2533  slist[i].s_addr = sources[i].ipv4Addr;
2534  }
2535 
2536  //On return, numsrc is always updated to be the total number of sources in
2537  //the filter
2538  *numsrc = numSources;
2539 
2540  //Successful processing
2541  return SOCKET_SUCCESS;
2542 #else
2543  //Not implemented
2544  return SOCKET_ERROR;
2545 #endif
2546 }
2547 
2548 
2549 /**
2550  * @brief Set multicast source filter
2551  * @param[in] s Descriptor that identifies a socket
2552  * @param[in] interface Local IP address of the interface
2553  * @param[in] group IP multicast address of the group
2554  * @param[in] grouplen Length in bytes of the group
2555  * @param[in] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2556  * @param[in] numsrc Number of source addresses in the slist array
2557  * @param[in] slist Array of IP addresses of sources to include or exclude
2558  * depending on the filter mode
2559  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2560  * Otherwise, it returns SOCKET_ERROR
2561  **/
2562 
2563 int_t setsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2564  socklen_t grouplen, uint32_t fmode, uint_t numsrc,
2565  struct sockaddr_storage *slist)
2566 {
2567 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2568  error_t error;
2569  uint_t i;
2570  Socket *sock;
2571  IpFilterMode filterMode;
2572  IpAddr groupAddr;
2574 
2575  //Make sure the socket descriptor is valid
2576  if(s < 0 || s >= SOCKET_MAX_COUNT)
2577  {
2578  return SOCKET_ERROR;
2579  }
2580 
2581  //Point to the socket structure
2582  sock = &socketTable[s];
2583 
2584 #if (IPV4_SUPPORT == ENABLED)
2585  //IPv4 group address?
2586  if(group->sa_family == AF_INET &&
2587  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2588  {
2589  //Point to the IPv4 address information
2590  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2591 
2592  //Copy IPv4 address
2593  groupAddr.length = sizeof(Ipv4Addr);
2594  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2595  }
2596  else
2597 #endif
2598 #if (IPV6_SUPPORT == ENABLED)
2599  //IPv6 group address?
2600  if(group->sa_family == AF_INET6 &&
2601  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2602  {
2603  //Point to the IPv6 address information
2604  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2605 
2606  //Copy IPv6 address
2607  groupAddr.length = sizeof(Ipv6Addr);
2608  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2609  }
2610  else
2611 #endif
2612  //Invalid group address?
2613  {
2614  //Report an error
2615  socketSetErrnoCode(sock, EINVAL);
2616  return SOCKET_ERROR;
2617  }
2618 
2619  //Check filter mode
2620  if(fmode == MCAST_INCLUDE)
2621  {
2622  //Select source-specific filter mode
2623  filterMode = IP_FILTER_MODE_INCLUDE;
2624  }
2625  else if(fmode == MCAST_INCLUDE)
2626  {
2627  //Select any-source filter mode
2628  filterMode = IP_FILTER_MODE_EXCLUDE;
2629  }
2630  else
2631  {
2632  //Report an error
2633  socketSetErrnoCode(sock, EINVAL);
2634  return SOCKET_ERROR;
2635  }
2636 
2637  //If the implementation imposes a limit on the maximum number of sources
2638  //in a source filter, ENOBUFS is generated when the operation would exceed
2639  //the maximum (refer to RFC3678, section 5.2.1)
2640  if(numsrc > SOCKET_MAX_MULTICAST_SOURCES)
2641  {
2642  socketSetErrnoCode(sock, ENOBUFS);
2643  return SOCKET_ERROR;
2644  }
2645 
2646  //If numsrc is 0, a NULL pointer may be supplied
2647  if(numsrc > 0 && slist == NULL)
2648  {
2649  socketSetErrnoCode(sock, EINVAL);
2650  return SOCKET_ERROR;
2651  }
2652 
2653  //Copy the list of source addresses to include or exclude
2654  for(i = 0; i < numsrc; i++)
2655  {
2656 #if (IPV4_SUPPORT == ENABLED)
2657  //IPv4 source address?
2658  if(slist[i].ss_family == AF_INET &&
2659  slist[i].ss_family == group->sa_family)
2660  {
2661  //Point to the IPv4 address information
2662  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2663 
2664  //Copy IPv4 address
2665  sources[i].length = sizeof(Ipv4Addr);
2666  sources[i].ipv4Addr = sa->sin_addr.s_addr;
2667  }
2668  else
2669 #endif
2670 #if (IPV6_SUPPORT == ENABLED)
2671  //IPv6 source address?
2672  if(slist[i].ss_family == AF_INET6 &&
2673  slist[i].ss_family == group->sa_family)
2674  {
2675  //Point to the IPv6 address information
2676  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2677 
2678  //Copy IPv6 address
2679  sources[i].length = sizeof(Ipv6Addr);
2680  ipv6CopyAddr(&sources[i].ipv6Addr, sa->sin6_addr.s6_addr);
2681  }
2682  else
2683 #endif
2684  //Invalid source address?
2685  {
2686  //Report an error
2687  socketSetErrnoCode(sock, EINVAL);
2688  return SOCKET_ERROR;
2689  }
2690  }
2691 
2692  //Set multicast source filter
2693  error = socketSetMulticastSourceFilter(sock, &groupAddr, filterMode,
2694  sources, numsrc);
2695 
2696  //Any error to report?
2697  if(error)
2698  {
2699  socketTranslateErrorCode(sock, error);
2700  return SOCKET_ERROR;
2701  }
2702 
2703  //Successful processing
2704  return SOCKET_SUCCESS;
2705 #else
2706  //Not implemented
2707  return SOCKET_ERROR;
2708 #endif
2709 }
2710 
2711 
2712 /**
2713  * @brief Get multicast source filter
2714  * @param[in] s Descriptor that identifies a socket
2715  * @param[in] interface Local IP address of the interface
2716  * @param[in] group IP multicast address of the group
2717  * @param[in] grouplen Length in bytes of the group
2718  * @param[out] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2719  * @param[out] numsrc On input, the numsrc argument holds the number of source
2720  * addresses that will fit in the slist array. On output, the numsrc argument
2721  * will hold the total number of sources in the filter
2722  * @param[out] slist Buffer into which an array of IP addresses of included or
2723  * excluded (depending on the filter mode) sources will be written. If numsrc
2724  * was 0 on input, a NULL pointer may be supplied
2725  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2726  * Otherwise, it returns SOCKET_ERROR
2727  **/
2728 
2729 int_t getsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2730  socklen_t grouplen, uint32_t *fmode, uint_t *numsrc,
2731  struct sockaddr_storage *slist)
2732 {
2733 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2734  error_t error;
2735  uint_t i;
2736  uint_t numSources;
2737  Socket *sock;
2738  IpFilterMode filterMode;
2739  IpAddr groupAddr;
2741 
2742  //Make sure the socket descriptor is valid
2743  if(s < 0 || s >= SOCKET_MAX_COUNT)
2744  {
2745  return SOCKET_ERROR;
2746  }
2747 
2748  //Point to the socket structure
2749  sock = &socketTable[s];
2750 
2751  //Check parameters
2752  if(fmode == NULL || numsrc == NULL)
2753  {
2754  socketSetErrnoCode(sock, EINVAL);
2755  return SOCKET_ERROR;
2756  }
2757 
2758  //If numsrc was 0 on input, a NULL pointer may be supplied
2759  if(*numsrc > 0 && slist == NULL)
2760  {
2761  socketSetErrnoCode(sock, EINVAL);
2762  return SOCKET_ERROR;
2763  }
2764 
2765 #if (IPV4_SUPPORT == ENABLED)
2766  //IPv4 group address?
2767  if(group->sa_family == AF_INET &&
2768  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2769  {
2770  //Point to the IPv4 address information
2771  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2772 
2773  //Copy IPv4 address
2774  groupAddr.length = sizeof(Ipv4Addr);
2775  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2776  }
2777  else
2778 #endif
2779 #if (IPV6_SUPPORT == ENABLED)
2780  //IPv6 group address?
2781  if(group->sa_family == AF_INET6 &&
2782  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2783  {
2784  //Point to the IPv6 address information
2785  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2786 
2787  //Copy IPv6 address
2788  groupAddr.length = sizeof(Ipv6Addr);
2789  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2790  }
2791  else
2792 #endif
2793  //Invalid group address?
2794  {
2795  //Report an error
2796  socketSetErrnoCode(sock, EINVAL);
2797  return SOCKET_ERROR;
2798  }
2799 
2800  //Set multicast source filter
2801  error = socketGetMulticastSourceFilter(sock, &groupAddr, &filterMode,
2802  sources, &numSources);
2803 
2804  //Any error to report?
2805  if(error)
2806  {
2807  socketTranslateErrorCode(sock, error);
2808  return SOCKET_ERROR;
2809  }
2810 
2811  //Filter mode
2812  if(filterMode == IP_FILTER_MODE_INCLUDE)
2813  {
2814  *fmode = MCAST_INCLUDE;
2815  }
2816  else
2817  {
2818  *fmode = MCAST_EXCLUDE;
2819  }
2820 
2821  //The slist parameter will hold as many source addresses as fit, up to the
2822  //minimum of the array size passed in as the original numsrc value and the
2823  //total number of sources in the filter (refer to RFC3678, section 5.2.2)
2824  for(i = 0; i < *numsrc && i < numSources; i++)
2825  {
2826 #if (IPV4_SUPPORT == ENABLED)
2827  //IPv4 source address?
2828  if(sock->localIpAddr.length == sizeof(Ipv4Addr))
2829  {
2830  //Point to the IPv4 address information
2831  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2832 
2833  //Set address family and port number
2834  sa->sin_family = AF_INET;
2835  sa->sin_port = 0;
2836 
2837  //Copy IPv4 address
2838  sa->sin_addr.s_addr = sources[i].ipv4Addr;
2839  }
2840  else
2841 #endif
2842 #if (IPV6_SUPPORT == ENABLED)
2843  //IPv6 source address?
2844  if(sock->localIpAddr.length == sizeof(Ipv6Addr))
2845  {
2846  //Point to the IPv4 address information
2847  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2848 
2849  //Set address family and port number
2850  sa->sin6_family = AF_INET6;
2851  sa->sin6_port = 0;
2852  sa->sin6_flowinfo = 0;
2853  sa->sin6_scope_id = 0;
2854 
2855  //Copy IPv6 address
2856  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sources[i].ipv6Addr);
2857  }
2858  else
2859 #endif
2860  //Invalid source address?
2861  {
2862  //Just for sanity
2863  osMemset(&slist[i], 0, sizeof(SOCKADDR_STORAGE));
2864  }
2865  }
2866 
2867  //On return, numsrc is always updated to be the total number of sources in
2868  //the filter
2869  *numsrc = numSources;
2870 
2871  //Successful processing
2872  return SOCKET_SUCCESS;
2873 #else
2874  //Not implemented
2875  return SOCKET_ERROR;
2876 #endif
2877 }
2878 
2879 
2880 /**
2881  * @brief Control the I/O mode of a socket
2882  * @param[in] s Descriptor that identifies a socket
2883  * @param[in] cmd A command to perform on the socket
2884  * @param[in,out] arg A pointer to a parameter
2885  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2886  * Otherwise, it returns SOCKET_ERROR
2887  **/
2888 
2889 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
2890 {
2891  int_t ret;
2892  uint_t *val;
2893  Socket *sock;
2894 
2895  //Make sure the socket descriptor is valid
2896  if(s < 0 || s >= SOCKET_MAX_COUNT)
2897  {
2898  return SOCKET_ERROR;
2899  }
2900 
2901  //Point to the socket structure
2902  sock = &socketTable[s];
2903 
2904  //Get exclusive access
2906 
2907  //Make sure the parameter is valid
2908  if(arg != NULL)
2909  {
2910  //Check command type
2911  switch(cmd)
2912  {
2913  //Enable or disable non-blocking mode
2914  case FIONBIO:
2915  //Cast the parameter to the relevant type
2916  val = (uint_t *) arg;
2917  //Enable blocking or non-blocking operation
2918  sock->timeout = (*val != 0) ? 0 : INFINITE_DELAY;
2919  //Successful processing
2920  ret = SOCKET_SUCCESS;
2921  break;
2922 
2923 #if (TCP_SUPPORT == ENABLED)
2924  //Get the number of bytes that are immediately available for reading
2925  case FIONREAD:
2926  //Cast the parameter to the relevant type
2927  val = (uint_t *) arg;
2928  //Return the actual value
2929  *val = sock->rcvUser;
2930  //Successful processing
2931  ret = SOCKET_SUCCESS;
2932  break;
2933 
2934  //Get the number of bytes written to the send queue but not yet
2935  //acknowledged by the other side of the connection
2936  case FIONWRITE:
2937  //Cast the parameter to the relevant type
2938  val = (uint_t *) arg;
2939  //Return the actual value
2940  *val = sock->sndUser + sock->sndNxt - sock->sndUna;
2941  //Successful processing
2942  ret = SOCKET_SUCCESS;
2943  break;
2944 
2945  //Get the free space in the send queue
2946  case FIONSPACE:
2947  //Cast the parameter to the relevant type
2948  val = (uint_t *) arg;
2949  //Return the actual value
2950  *val = sock->txBufferSize - (sock->sndUser + sock->sndNxt -
2951  sock->sndUna);
2952  //Successful processing
2953  ret = SOCKET_SUCCESS;
2954  break;
2955 #endif
2956 
2957  //Unknown command?
2958  default:
2959  //Report an error
2960  socketSetErrnoCode(sock, EINVAL);
2961  ret = SOCKET_ERROR;
2962  break;
2963  }
2964  }
2965  else
2966  {
2967  //The parameter is not valid
2968  socketSetErrnoCode(sock, EFAULT);
2969  ret = SOCKET_ERROR;
2970  }
2971 
2972  //Release exclusive access
2974 
2975  //return status code
2976  return ret;
2977 }
2978 
2979 
2980 /**
2981  * @brief Perform specific operation
2982  * @param[in] s Descriptor that identifies a socket
2983  * @param[in] cmd A command to perform on the socket
2984  * @param[in] arg Argument value
2985  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2986  * Otherwise, it returns SOCKET_ERROR
2987  **/
2988 
2990 {
2991  int_t ret;
2992  Socket *sock;
2993 
2994  //Make sure the socket descriptor is valid
2995  if(s < 0 || s >= SOCKET_MAX_COUNT)
2996  {
2997  return SOCKET_ERROR;
2998  }
2999 
3000  //Point to the socket structure
3001  sock = &socketTable[s];
3002 
3003  //Get exclusive access
3005 
3006  //Check command type
3007  switch(cmd)
3008  {
3009  //Get the file status flags?
3010  case F_GETFL:
3011  //Return (as the function result) the file descriptor flags
3012  ret = (sock->timeout == 0) ? O_NONBLOCK : 0;
3013  break;
3014 
3015  //Set the file status flags?
3016  case F_SETFL:
3017  //Enable blocking or non-blocking operation
3018  sock->timeout = ((arg & O_NONBLOCK) != 0) ? 0 : INFINITE_DELAY;
3019  //Successful processing
3020  ret = SOCKET_SUCCESS;
3021  break;
3022 
3023  //Unknown command?
3024  default:
3025  //Report an error
3026  socketSetErrnoCode(sock, EINVAL);
3027  ret = SOCKET_ERROR;
3028  break;
3029  }
3030 
3031  //Release exclusive access
3033 
3034  //return status code
3035  return ret;
3036 }
3037 
3038 
3039 /**
3040  * @brief The shutdown function disables sends or receives on a socket
3041  * @param[in] s Descriptor that identifies a socket
3042  * @param[in] how A flag that describes what types of operation will no longer be allowed
3043  * @return If no error occurs, shutdown returns SOCKET_SUCCESS
3044  * Otherwise, it returns SOCKET_ERROR
3045  **/
3046 
3048 {
3049  error_t error;
3050  Socket *sock;
3051 
3052  //Make sure the socket descriptor is valid
3053  if(s < 0 || s >= SOCKET_MAX_COUNT)
3054  {
3055  return SOCKET_ERROR;
3056  }
3057 
3058  //Point to the socket structure
3059  sock = &socketTable[s];
3060 
3061  //Shut down socket
3062  error = socketShutdown(sock, how);
3063 
3064  //Any error to report?
3065  if(error)
3066  {
3067  socketTranslateErrorCode(sock, error);
3068  return SOCKET_ERROR;
3069  }
3070 
3071  //Successful processing
3072  return SOCKET_SUCCESS;
3073 }
3074 
3075 
3076 /**
3077  * @brief The closesocket function closes an existing socket
3078  * @param[in] s Descriptor that identifies a socket
3079  * @return If no error occurs, closesocket returns SOCKET_SUCCESS
3080  * Otherwise, it returns SOCKET_ERROR
3081  **/
3082 
3084 {
3085  Socket *sock;
3086 
3087  //Make sure the socket descriptor is valid
3088  if(s < 0 || s >= SOCKET_MAX_COUNT)
3089  {
3090  return SOCKET_ERROR;
3091  }
3092 
3093  //Point to the socket structure
3094  sock = &socketTable[s];
3095 
3096  //Close socket
3097  socketClose(sock);
3098 
3099  //Successful processing
3100  return SOCKET_SUCCESS;
3101 }
3102 
3103 
3104 /**
3105  * @brief Determine the status of one or more sockets
3106  *
3107  * The select function determines the status of one or more sockets,
3108  * waiting if necessary, to perform synchronous I/O
3109  *
3110  * @param[in] nfds Unused parameter included only for compatibility with
3111  * Berkeley socket
3112  * @param[in,out] readfds An optional pointer to a set of sockets to be
3113  * checked for readability
3114  * @param[in,out] writefds An optional pointer to a set of sockets to be
3115  * checked for writability
3116  * @param[in,out] exceptfds An optional pointer to a set of sockets to be
3117  * checked for errors
3118  * @param[in] timeout The maximum time for select to wait. Set the timeout
3119  * parameter to null for blocking operations
3120  * @return The select function returns the total number of socket handles that
3121  * are ready and contained in the fd_set structures, zero if the time limit
3122  * expired, or SOCKET_ERROR if an error occurred
3123  **/
3124 
3125 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
3126  const struct timeval *timeout)
3127 {
3128  int_t i;
3129  int_t j;
3130  int_t n;
3131  int_t s;
3132  systime_t time;
3133  uint_t eventMask;
3134  uint_t eventFlags;
3135  OsEvent event;
3136  fd_set *fds;
3137 
3138  //Parse all the descriptor sets
3139  for(i = 0; i < 3; i++)
3140  {
3141  //Select the suitable descriptor set
3142  switch(i)
3143  {
3144  case 0:
3145  //Set of sockets to be checked for readability
3146  fds = readfds;
3147  break;
3148 
3149  case 1:
3150  //Set of sockets to be checked for writability
3151  fds = writefds;
3152  break;
3153 
3154  default:
3155  //Set of sockets to be checked for errors
3156  fds = exceptfds;
3157  break;
3158  }
3159 
3160  //Each descriptor is optional and may be omitted
3161  if(fds != NULL)
3162  {
3163  //Parse the current set of sockets
3164  for(j = 0; j < fds->fd_count; j++)
3165  {
3166  //Invalid socket descriptor?
3167  if(fds->fd_array[j] < 0 || fds->fd_array[j] >= SOCKET_MAX_COUNT)
3168  {
3169  //Report an error
3170  return SOCKET_ERROR;
3171  }
3172  }
3173  }
3174  }
3175 
3176  //Create an event object to get notified of socket events
3177  if(!osCreateEvent(&event))
3178  {
3179  //Failed to create event
3180  return SOCKET_ERROR;
3181  }
3182 
3183  //Parse all the descriptor sets
3184  for(i = 0; i < 3; i++)
3185  {
3186  //Select the suitable descriptor set
3187  switch(i)
3188  {
3189  case 0:
3190  //Set of sockets to be checked for readability
3191  fds = readfds;
3192  eventMask = SOCKET_EVENT_RX_READY;
3193  break;
3194 
3195  case 1:
3196  //Set of sockets to be checked for writability
3197  fds = writefds;
3198  eventMask = SOCKET_EVENT_TX_READY;
3199  break;
3200 
3201  default:
3202  //Set of sockets to be checked for errors
3203  fds = exceptfds;
3204  eventMask = SOCKET_EVENT_CLOSED;
3205  break;
3206  }
3207 
3208  //Each descriptor is optional and may be omitted
3209  if(fds != NULL)
3210  {
3211  //Parse the current set of sockets
3212  for(j = 0; j < fds->fd_count; j++)
3213  {
3214  //Get the descriptor associated with the current entry
3215  s = fds->fd_array[j];
3216  //Subscribe to the requested events
3217  socketRegisterEvents(&socketTable[s], &event, eventMask);
3218  }
3219  }
3220  }
3221 
3222  //Retrieve timeout value
3223  if(timeout != NULL)
3224  {
3225  time = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
3226  }
3227  else
3228  {
3229  time = INFINITE_DELAY;
3230  }
3231 
3232  //Block the current task until an event occurs
3233  osWaitForEvent(&event, time);
3234 
3235  //Count the number of events in the signaled state
3236  n = 0;
3237 
3238  //Parse all the descriptor sets
3239  for(i = 0; i < 3; i++)
3240  {
3241  //Select the suitable descriptor set
3242  switch(i)
3243  {
3244  case 0:
3245  //Set of sockets to be checked for readability
3246  fds = readfds;
3247  eventMask = SOCKET_EVENT_RX_READY;
3248  break;
3249 
3250  case 1:
3251  //Set of sockets to be checked for writability
3252  fds = writefds;
3253  eventMask = SOCKET_EVENT_TX_READY;
3254  break;
3255 
3256  default:
3257  //Set of sockets to be checked for errors
3258  fds = exceptfds;
3259  eventMask = SOCKET_EVENT_CLOSED;
3260  break;
3261  }
3262 
3263  //Each descriptor is optional and may be omitted
3264  if(fds != NULL)
3265  {
3266  //Parse the current set of sockets
3267  for(j = 0; j < fds->fd_count; )
3268  {
3269  //Get the descriptor associated with the current entry
3270  s = fds->fd_array[j];
3271  //Retrieve event flags for the current socket
3272  eventFlags = socketGetEvents(&socketTable[s]);
3273  //Unsubscribe previously registered events
3275 
3276  //Event flag is set?
3277  if(eventFlags & eventMask)
3278  {
3279  //Track the number of events in the signaled state
3280  n++;
3281  //Jump to the next socket descriptor
3282  j++;
3283  }
3284  else
3285  {
3286  //Remove descriptor from the current set
3287  socketFdClr(fds, s);
3288  }
3289  }
3290  }
3291  }
3292 
3293  //Delete event object
3294  osDeleteEvent(&event);
3295  //Return the number of events in the signaled state
3296  return n;
3297 }
3298 
3299 
3300 /**
3301  * @brief Get system host name
3302  * @param[out] name Output buffer where to store the system host name
3303  * @param[in] len Length of the buffer, in bytes
3304  * @return On success, zero is returned. On error, -1 is returned
3305  **/
3306 
3308 {
3309  size_t n;
3310  NetInterface *interface;
3311 
3312  //Check parameter
3313  if(name == NULL)
3314  {
3315  //Report an error
3317  return -1;
3318  }
3319 
3320  //Select the default network interface
3321  interface = netGetDefaultInterface();
3322 
3323  //Retrieve the length of the host name
3324  n = osStrlen(interface->hostname);
3325 
3326  //Make sure the buffer is large enough to hold the string
3327  if(len <= n)
3328  {
3329  //Report an error
3331  return -1;
3332  }
3333 
3334  //Copy the host name
3335  osStrcpy(name, interface->hostname);
3336 
3337  //Successful processing
3338  return 0;
3339 }
3340 
3341 
3342 /**
3343  * @brief Host name resolution
3344  * @param[in] name Name of the host to resolve
3345  * @return Pointer to the hostent structure or a NULL if an error occurs
3346  **/
3347 
3349 {
3350  int_t herrno;
3351  static HOSTENT result;
3352 
3353  //The hostent structure returned by the function resides in static
3354  //memory area
3355  return gethostbyname_r(name, &result, NULL, 0, &herrno);
3356 }
3357 
3358 
3359 /**
3360  * @brief Host name resolution (reentrant version)
3361  * @param[in] name Name of the host to resolve
3362  * @param[in] result Pointer to a hostent structure where the function can
3363  * store the host entry
3364  * @param[out] buf Pointer to a temporary work buffer
3365  * @param[in] buflen Length of the temporary work buffer
3366  * @param[out] h_errnop Pointer to a location where the function can store an
3367  * h_errno value if an error occurs
3368  * @return Pointer to the hostent structure or a NULL if an error occurs
3369  **/
3370 
3371 struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
3372  char_t *buf, size_t buflen, int_t *h_errnop)
3373 {
3374  error_t error;
3375  IpAddr ipAddr;
3376 
3377  //Check input parameters
3378  if(name == NULL || result == NULL)
3379  {
3380  //Report an error
3381  *h_errnop = NO_RECOVERY;
3382  return NULL;
3383  }
3384 
3385  //Resolve host address
3386  error = getHostByName(NULL, name, &ipAddr, 0);
3387  //Address resolution failed?
3388  if(error)
3389  {
3390  //Report an error
3391  *h_errnop = HOST_NOT_FOUND;
3392  return NULL;
3393  }
3394 
3395 #if (IPV4_SUPPORT == ENABLED)
3396  //IPv4 address?
3397  if(ipAddr.length == sizeof(Ipv4Addr))
3398  {
3399  //Set address family
3400  result->h_addrtype = AF_INET;
3401  result->h_length = sizeof(Ipv4Addr);
3402 
3403  //Copy IPv4 address
3404  ipv4CopyAddr(result->h_addr, &ipAddr.ipv4Addr);
3405  }
3406  else
3407 #endif
3408 #if (IPV6_SUPPORT == ENABLED)
3409  //IPv6 address?
3410  if(ipAddr.length == sizeof(Ipv6Addr))
3411  {
3412  //Set address family
3413  result->h_addrtype = AF_INET6;
3414  result->h_length = sizeof(Ipv6Addr);
3415 
3416  //Copy IPv6 address
3417  ipv6CopyAddr(result->h_addr, &ipAddr.ipv6Addr);
3418  }
3419  else
3420 #endif
3421  //Invalid address?
3422  {
3423  //Report an error
3424  *h_errnop = NO_ADDRESS;
3425  return NULL;
3426  }
3427 
3428  //Successful host name resolution
3429  *h_errnop = NETDB_SUCCESS;
3430 
3431  //Return a pointer to the hostent structure
3432  return result;
3433 }
3434 
3435 
3436 /**
3437  * @brief Convert host and service names to socket address
3438  * @param[in] node Host name or numerical network address
3439  * @param[in] service Service name or decimal number
3440  * @param[in] hints Criteria for selecting the socket address structures
3441  * @param[out] res Dynamically allocated list of socket address structures
3442  * @return On success, zero is returned. On error, a non-zero value is returned
3443  **/
3444 
3445 int_t getaddrinfo(const char_t *node, const char_t *service,
3446  const struct addrinfo *hints, struct addrinfo **res)
3447 {
3448  error_t error;
3449  size_t n;
3450  char_t *p;
3451  uint_t flags;
3452  uint32_t port;
3453  IpAddr ipAddr;
3454  ADDRINFO h;
3455  ADDRINFO *ai;
3456 
3457  //Check whether both node and service name are NULL
3458  if(node == NULL && service == NULL)
3459  return EAI_NONAME;
3460 
3461  //The hints argument is optional
3462  if(hints != NULL)
3463  {
3464  //If hints is not NULL, it points to an addrinfo structure that specifies
3465  //criteria that limit the set of socket addresses that will be returned
3466  h = *hints;
3467  }
3468  else
3469  {
3470  //If hints is NULL, then default criteria are used
3471  osMemset(&h, 0, sizeof(ADDRINFO));
3472  h.ai_family = AF_UNSPEC;
3473  h.ai_socktype = 0;
3474  h.ai_protocol = 0;
3475  h.ai_flags = 0;
3476  }
3477 
3478  //The user may provide a hint to choose between IPv4 and IPv6
3479  if(h.ai_family == AF_UNSPEC)
3480  {
3481  //The value AF_UNSPEC indicates that function should return socket
3482  //addresses for any address family (either IPv4 or IPv6)
3483  flags = 0;
3484  }
3485 #if (IPV4_SUPPORT == ENABLED)
3486  else if(h.ai_family == AF_INET)
3487  {
3488  //The value AF_INET indicates that the function should return an IPv4
3489  //address
3491  }
3492 #endif
3493 #if (IPV6_SUPPORT == ENABLED)
3494  else if(h.ai_family == AF_INET6)
3495  {
3496  //The value AF_INET6 indicates that the function should return an IPv6
3497  //address
3499  }
3500 #endif
3501  else
3502  {
3503  //The requested address family is not supported
3504  return EAI_FAMILY;
3505  }
3506 
3507  //Check whether a host name or numerical network address is specified
3508  if(node != NULL)
3509  {
3510  //If the AI_NUMERICHOST flag, then node must be a numerical network
3511  //address
3512  if((h.ai_flags & AI_NUMERICHOST) != 0)
3513  {
3514  //Convert the string representation to a binary IP address
3515  error = ipStringToAddr(node, &ipAddr);
3516  }
3517  else
3518  {
3519  //Resolve host address
3520  error = getHostByName(NULL, node, &ipAddr, flags);
3521  }
3522 
3523  //Check status code
3524  if(error == NO_ERROR)
3525  {
3526  //Successful host name resolution
3527  }
3528  else if(error == ERROR_IN_PROGRESS)
3529  {
3530  //Host name resolution is in progress
3531  return EAI_AGAIN;
3532  }
3533  else
3534  {
3535  //Permanent failure indication
3536  return EAI_FAIL;
3537  }
3538  }
3539  else
3540  {
3541  //Check flags
3542  if((h.ai_flags & AI_PASSIVE) != 0)
3543  {
3544 #if (IPV4_SUPPORT == ENABLED)
3545  //IPv4 address family?
3546  if(h.ai_family == AF_INET || h.ai_family == AF_UNSPEC)
3547  {
3548  ipAddr.length = sizeof(Ipv4Addr);
3549  ipAddr.ipv4Addr = IPV4_UNSPECIFIED_ADDR;
3550  }
3551  else
3552 #endif
3553 #if (IPV6_SUPPORT == ENABLED)
3554  //IPv6 address family?
3555  if(h.ai_family == AF_INET6 || h.ai_family == AF_UNSPEC)
3556  {
3557  ipAddr.length = sizeof(Ipv6Addr);
3558  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
3559  }
3560  else
3561 #endif
3562  //Unknown address family?
3563  {
3564  //Report an error
3565  return EAI_ADDRFAMILY;
3566  }
3567  }
3568  else
3569  {
3570  //Invalid flags
3571  return EAI_BADFLAGS;
3572  }
3573  }
3574 
3575  //Only service names containing a numeric port number are supported
3576  port = osStrtoul(service, &p, 10);
3577  //Invalid service name?
3578  if(*p != '\0')
3579  {
3580  //The requested service is not available
3581  return EAI_SERVICE;
3582  }
3583 
3584 #if (IPV4_SUPPORT == ENABLED)
3585  //IPv4 address?
3586  if(ipAddr.length == sizeof(Ipv4Addr))
3587  {
3588  //Select the relevant socket family
3589  h.ai_family = AF_INET;
3590  //Get the length of the corresponding socket address
3591  n = sizeof(SOCKADDR_IN);
3592  }
3593  else
3594 #endif
3595 #if (IPV6_SUPPORT == ENABLED)
3596  //IPv6 address?
3597  if(ipAddr.length == sizeof(Ipv6Addr))
3598  {
3599  //Select the relevant socket family
3600  h.ai_family = AF_INET6;
3601  //Get the length of the corresponding socket address
3602  n = sizeof(SOCKADDR_IN6);
3603  }
3604  else
3605 #endif
3606  //Unknown address?
3607  {
3608  //Report an error
3609  return EAI_ADDRFAMILY;
3610  }
3611 
3612  //Allocate a memory buffer to hold the address information structure
3613  ai = osAllocMem(sizeof(ADDRINFO) + n);
3614  //Failed to allocate memory?
3615  if(ai == NULL)
3616  {
3617  //Out of memory
3618  return EAI_MEMORY;
3619  }
3620 
3621  //Initialize address information structure
3622  osMemset(ai, 0, sizeof(ADDRINFO) + n);
3623  ai->ai_family = h.ai_family;
3624  ai->ai_socktype = h.ai_socktype;
3625  ai->ai_protocol = h.ai_protocol;
3626  ai->ai_addr = (SOCKADDR *) ((uint8_t *) ai + sizeof(ADDRINFO));
3627  ai->ai_addrlen = n;
3628  ai->ai_next = NULL;
3629 
3630 #if (IPV4_SUPPORT == ENABLED)
3631  //IPv4 address?
3632  if(ipAddr.length == sizeof(Ipv4Addr))
3633  {
3634  //Point to the IPv4 address information
3635  SOCKADDR_IN *sa = (SOCKADDR_IN *) ai->ai_addr;
3636 
3637  //Set address family and port number
3638  sa->sin_family = AF_INET;
3639  sa->sin_port = htons(port);
3640 
3641  //Copy IPv4 address
3642  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
3643  }
3644  else
3645 #endif
3646 #if (IPV6_SUPPORT == ENABLED)
3647  //IPv6 address?
3648  if(ipAddr.length == sizeof(Ipv6Addr))
3649  {
3650  //Point to the IPv6 address information
3651  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) ai->ai_addr;
3652 
3653  //Set address family and port number
3654  sa->sin6_family = AF_INET6;
3655  sa->sin6_port = htons(port);
3656  sa->sin6_flowinfo = 0;
3657  sa->sin6_scope_id = 0;
3658 
3659  //Copy IPv6 address
3660  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
3661  }
3662  else
3663 #endif
3664  //Unknown address?
3665  {
3666  //Clean up side effects
3667  osFreeMem(ai);
3668  //Report an error
3669  return EAI_ADDRFAMILY;
3670  }
3671 
3672  //Return a pointer to the allocated address information
3673  *res = ai;
3674 
3675  //Successful processing
3676  return 0;
3677 }
3678 
3679 
3680 /**
3681  * @brief Free socket address structures
3682  * @param[in] res Dynamically allocated list of socket address structures
3683  **/
3684 
3686 {
3687  ADDRINFO *next;
3688 
3689  //Free the list of socket address structures
3690  while(res != NULL)
3691  {
3692  //Get next entry
3693  next = res->ai_next;
3694  //Free current socket address structure
3695  osFreeMem(res);
3696  //Point to the next entry
3697  res = next;
3698  }
3699 }
3700 
3701 
3702 /**
3703  * @brief Convert a socket address to a corresponding host and service
3704  * @param[in] addr Generic socket address structure
3705  * @param[in] addrlen Length in bytes of the address
3706  * @param[out] host Output buffer where to store the host name
3707  * @param[in] hostlen Length of the host name buffer, in bytes
3708  * @param[out] serv Output buffer where to store the service name
3709  * @param[in] servlen Length of the service name buffer, in bytes
3710  * @param[in] flags Set of flags that influences the behavior of this function
3711  * @return On success, zero is returned. On error, a non-zero value is returned
3712  **/
3713 
3714 int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
3715  char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
3716 {
3717  uint16_t port;
3718 
3719  //At least one of host name or service name must be requested
3720  if(host == NULL && serv == NULL)
3721  return EAI_NONAME;
3722 
3723 #if (IPV4_SUPPORT == ENABLED)
3724  //IPv4 address?
3725  if(addr->sa_family == AF_INET &&
3726  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
3727  {
3728  SOCKADDR_IN *sa;
3729  Ipv4Addr ipv4Addr;
3730 
3731  //Point to the IPv4 address information
3732  sa = (SOCKADDR_IN *) addr;
3733 
3734  //The caller can specify that no host name is required
3735  if(host != NULL)
3736  {
3737  //Make sure the buffer is large enough to hold the host name
3738  if(hostlen < 16)
3739  return EAI_OVERFLOW;
3740 
3741  //Copy the binary representation of the IPv4 address
3742  ipv4CopyAddr(&ipv4Addr, &sa->sin_addr.s_addr);
3743  //Convert the IPv4 address to dot-decimal notation
3744  ipv4AddrToString(ipv4Addr, host);
3745  }
3746 
3747  //Retrieve port number
3748  port = ntohs(sa->sin_port);
3749  }
3750  else
3751 #endif
3752 #if (IPV6_SUPPORT == ENABLED)
3753  //IPv6 address?
3754  if(addr->sa_family == AF_INET6 &&
3755  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
3756  {
3757  SOCKADDR_IN6 *sa;
3758  Ipv6Addr ipv6Addr;
3759 
3760  //Point to the IPv6 address information
3761  sa = (SOCKADDR_IN6 *) addr;
3762 
3763  //The caller can specify that no host name is required
3764  if(host != NULL)
3765  {
3766  //Make sure the buffer is large enough to hold the host name
3767  if(hostlen < 40)
3768  return EAI_OVERFLOW;
3769 
3770  //Copy the binary representation of the IPv6 address
3771  ipv6CopyAddr(&ipv6Addr, sa->sin6_addr.s6_addr);
3772  //Convert the IPv6 address to string representation
3773  ipv6AddrToString(&ipv6Addr, host);
3774  }
3775 
3776  //Retrieve port number
3777  port = ntohs(sa->sin6_port);
3778  }
3779  else
3780 #endif
3781  //Invalid address?
3782  {
3783  //The address family was not recognized, or the address length was
3784  //invalid for the specified family
3785  return EAI_FAMILY;
3786  }
3787 
3788  //The caller can specify that no service name is required
3789  if(serv != NULL)
3790  {
3791  //Make sure the buffer is large enough to hold the service name
3792  if(servlen < 6)
3793  return EAI_OVERFLOW;
3794 
3795  //Convert the port number to string representation
3796  osSprintf(serv, "%" PRIu16, ntohs(port));
3797  }
3798 
3799  //Successful processing
3800  return 0;
3801 }
3802 
3803 
3804 /**
3805  * @brief Map an interface name into its corresponding index
3806  * @param[in] ifname Name of the interface
3807  * @return If ifname is the name of an interface, then the function returns the
3808  * interface index corresponding to name ifname. Otherwise, the function
3809  * returns zero
3810  **/
3811 
3813 {
3814  uint_t i;
3815  uint_t index;
3816 
3817  //Initialize interface index
3818  index = 0;
3819 
3820  //Valid parameter?
3821  if(ifname != NULL)
3822  {
3823  //Loop through network interfaces
3824  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3825  {
3826  //Matching interface name?
3827  if(osStrcmp(netInterface[i].name, ifname) == 0)
3828  {
3829  //Save the interface index
3830  index = netInterface[i].index + 1;
3831  //We are done
3832  break;
3833  }
3834  }
3835  }
3836 
3837  //Return the index corresponding to interface name
3838  return index;
3839 }
3840 
3841 
3842 /**
3843  * @brief Map an interface index into its corresponding name
3844  * @param[in] ifindex Interface index
3845  * @param[in] ifname Buffer of at least IF_NAMESIZE bytes
3846  * @return If ifindex is an interface index, then the function returns the value
3847  * supplied in ifname, which points to a buffer now containing the interface
3848  * name. Otherwise, the function returns a NULL pointer
3849  **/
3850 
3852 {
3853  uint_t i;
3854  char_t *name;
3855 
3856  //Initialize interface name
3857  name = NULL;
3858 
3859  //Valid parameters?
3860  if(ifindex != 0 && ifname != NULL)
3861  {
3862  //Loop through network interfaces
3863  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3864  {
3865  //Matching interface index?
3866  if((netInterface[i].index + 1) == ifindex)
3867  {
3868  //Copy the name of the interface
3869  osStrcpy(ifname, netInterface[i].name);
3870  name = ifname;
3871 
3872  //We are done
3873  break;
3874  }
3875  }
3876  }
3877 
3878  //Return the name of the interface
3879  return name;
3880 }
3881 
3882 
3883 /**
3884  * @brief Convert a dot-decimal string into binary data in network byte order
3885  * @param[in] cp NULL-terminated string representing the IPv4 address
3886  * @return Binary data in network byte order
3887  **/
3888 
3890 {
3891 #if (IPV4_SUPPORT == ENABLED)
3892  error_t error;
3893  Ipv4Addr ipv4Addr;
3894 
3895  //Convert a dot-decimal string to a binary IPv4 address
3896  error = ipv4StringToAddr(cp, &ipv4Addr);
3897 
3898  //Check status code
3899  if(error)
3900  {
3901  //The input is invalid
3902  return INADDR_NONE;
3903  }
3904  else
3905  {
3906  //Return the binary representation
3907  return ipv4Addr;
3908  }
3909 #else
3910  //IPv4 is not implemented
3911  return INADDR_NONE;
3912 #endif
3913 }
3914 
3915 
3916 /**
3917  * @brief Convert a dot-decimal string into binary form
3918  * @param[in] cp NULL-terminated string representing the IPv4 address
3919  * @param[out] inp Binary data in network byte order
3920  * @return The function returns non-zero if the address is valid, zero if not
3921  **/
3922 
3923 int_t inet_aton(const char_t *cp, struct in_addr *inp)
3924 {
3925 #if (IPV4_SUPPORT == ENABLED)
3926  error_t error;
3927  Ipv4Addr ipv4Addr;
3928 
3929  //Convert a dot-decimal string to a binary IPv4 address
3930  error = ipv4StringToAddr(cp, &ipv4Addr);
3931 
3932  //Check status code
3933  if(error)
3934  {
3935  //The input is invalid
3936  return 0;
3937  }
3938  else
3939  {
3940  //Copy the binary representation of the IPv4 address
3941  inp->s_addr = ipv4Addr;
3942  //The conversion succeeded
3943  return 1;
3944  }
3945 #else
3946  //IPv4 is not implemented
3947  return 0;
3948 #endif
3949 }
3950 
3951 
3952 /**
3953  * @brief Convert a binary IPv4 address to dot-decimal notation
3954  * @param[in] in Binary representation of the IPv4 address
3955  * @return Pointer to the formatted string
3956  **/
3957 
3958 const char_t *inet_ntoa(struct in_addr in)
3959 {
3960  static char_t buf[16];
3961 
3962  //The string returned by the function resides in static memory area
3963  return inet_ntoa_r(in, buf, sizeof(buf));
3964 }
3965 
3966 
3967 /**
3968  * @brief Convert a binary IPv4 address to dot-decimal notation (reentrant version)
3969  * @param[in] in Binary representation of the IPv4 address
3970  * @param[out] buf Pointer to the buffer where to format the string
3971  * @param[in] buflen Number of bytes available in the buffer
3972  * @return Pointer to the formatted string
3973  **/
3974 
3975 const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
3976 {
3977  //Properly terminate the string
3978  buf[0] = '\0';
3979 
3980 #if (IPV4_SUPPORT == ENABLED)
3981  //Check the length of the buffer
3982  if(buflen >= 16)
3983  {
3984  //Convert the binary IPv4 address to dot-decimal notation
3985  ipv4AddrToString(in.s_addr, buf);
3986  }
3987 #endif
3988 
3989  //Return a pointer to the formatted string
3990  return buf;
3991 }
3992 
3993 
3994 /**
3995  * @brief Convert an IPv4 or IPv6 address from text to binary form
3996  * @param[in] af Address family
3997  * @param[in] src NULL-terminated string representing the IP address
3998  * @param[out] dst Binary representation of the IP address
3999  * @return The function returns 1 on success. 0 is returned if the address
4000  * is not valid. If the address family is not valid, -1 is returned
4001  **/
4002 
4003 int_t inet_pton(int_t af, const char_t *src, void *dst)
4004 {
4005  error_t error;
4006 
4007 #if (IPV4_SUPPORT == ENABLED)
4008  //IPv4 address?
4009  if(af == AF_INET)
4010  {
4011  Ipv4Addr ipv4Addr;
4012 
4013  //Convert the IPv4 address from text to binary form
4014  error = ipv4StringToAddr(src, &ipv4Addr);
4015 
4016  //Check status code
4017  if(error)
4018  {
4019  //The input is invalid
4020  return 0;
4021  }
4022  else
4023  {
4024  //Copy the binary representation of the IPv4 address
4025  ipv4CopyAddr(dst, &ipv4Addr);
4026  //The conversion succeeded
4027  return 1;
4028  }
4029  }
4030  else
4031 #endif
4032 #if (IPV6_SUPPORT == ENABLED)
4033  //IPv6 address?
4034  if(af == AF_INET6)
4035  {
4036  Ipv6Addr ipv6Addr;
4037 
4038  //Convert the IPv6 address from text to binary form
4039  error = ipv6StringToAddr(src, &ipv6Addr);
4040 
4041  //Check status code
4042  if(error)
4043  {
4044  //The input is invalid
4045  return 0;
4046  }
4047  else
4048  {
4049  //Copy the binary representation of the IPv6 address
4050  ipv6CopyAddr(dst, &ipv6Addr);
4051  //The conversion succeeded
4052  return 1;
4053  }
4054  }
4055  else
4056 #endif
4057  //Invalid address family?
4058  {
4059  //Report an error
4060  return -1;
4061  }
4062 }
4063 
4064 
4065 /**
4066  * @brief Convert an IPv4 or IPv6 address from binary to text
4067  * @param[in] af Address family
4068  * @param[in] src Binary representation of the IP address
4069  * @param[out] dst NULL-terminated string representing the IP address
4070  * @param[in] size Number of bytes available in the buffer
4071  * @return On success, the function returns a pointer to the formatted string.
4072  * NULL is returned if there was an error
4073  **/
4074 
4075 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
4076 {
4077 #if (IPV4_SUPPORT == ENABLED)
4078  //IPv4 address?
4079  if(af == AF_INET && size >= INET_ADDRSTRLEN)
4080  {
4081  Ipv4Addr ipv4Addr;
4082 
4083  //Copy the binary representation of the IPv4 address
4084  ipv4CopyAddr(&ipv4Addr, src);
4085 
4086  //Convert the IPv4 address to dot-decimal notation
4087  return ipv4AddrToString(ipv4Addr, dst);
4088  }
4089  else
4090 #endif
4091 #if (IPV6_SUPPORT == ENABLED)
4092  //IPv6 address?
4093  if(af == AF_INET6 && size >= INET6_ADDRSTRLEN)
4094  {
4095  Ipv6Addr ipv6Addr;
4096 
4097  //Copy the binary representation of the IPv6 address
4098  ipv6CopyAddr(&ipv6Addr, src);
4099 
4100  //Convert the IPv6 address to string representation
4101  return ipv6AddrToString(&ipv6Addr, dst);
4102  }
4103  else
4104 #endif
4105  //Invalid address family?
4106  {
4107  //Report an error
4108  return NULL;
4109  }
4110 }
4111 
4112 #endif
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:1491
const struct in6_addr in6addr_any
Definition: bsd_socket.c:49
IpFilterMode
Multicast filter mode.
Definition: ip.h:67
#define F_GETFL
Definition: bsd_socket.h:214
int_t socketSetSoSndBufOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_SNDBUF option.
char_t * ipv6AddrToString(const Ipv6Addr *ipAddr, char_t *str)
Convert a binary IPv6 address to a string representation.
Definition: ipv6.c:2339
struct hostent * gethostbyname(const char_t *name)
Host name resolution.
Definition: bsd_socket.c:3348
#define htons(value)
Definition: cpu_endian.h:413
struct sockaddr * ai_addr
Definition: bsd_socket.h:559
#define IP_BLOCK_SOURCE
Definition: bsd_socket.h:165
int_t socketGetTcpMaxSegOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_MAXSEG option.
int_t socklen_t
Length type.
Definition: bsd_socket.h:303
int_t socketSetTcpKeepIdleOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPIDLE option.
int_t inet_pton(int_t af, const char_t *src, void *dst)
Convert an IPv4 or IPv6 address from text to binary form.
Definition: bsd_socket.c:4003
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1321
#define AF_INET6
Definition: bsd_socket.h:80
Ancillary data header.
Definition: bsd_socket.h:496
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:324
#define IPV6_HOPLIMIT
Definition: bsd_socket.h:186
#define CMSG_DATA(cmsg)
Definition: bsd_socket.h:585
@ SOCKET_OPTION_IPV4_RECV_TOS
Definition: socket.h:197
int_t socketGetTcpKeepIntvlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPINTVL option.
int_t getsourcefilter(int_t s, uint32_t interface, struct sockaddr *group, socklen_t grouplen, uint32_t *fmode, uint_t *numsrc, struct sockaddr_storage *slist)
Get multicast source filter.
Definition: bsd_socket.c:2729
int_t socketSetSoRcvBufOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_RCVBUF option.
int_t socketSetSoNoCheckOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_NO_CHECK option.
int_t connect(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Establish a connection to a specified socket.
Definition: bsd_socket.c:204
#define ENOPROTOOPT
Definition: bsd_socket.h:264
int_t recvmsg(int_t s, struct msghdr *msg, int_t flags)
Receive a message.
Definition: bsd_socket.c:1115
@ SOCKET_FLAG_WAIT_ALL
Definition: socket.h:138
int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval, socklen_t optlen)
The setsockopt function sets a socket option.
Definition: bsd_socket.c:1726
Helper function for BSD socket API.
#define EAI_BADFLAGS
Definition: bsd_socket.h:243
int_t socketSetMcastLeaveGroupOption(Socket *socket, const struct group_req *optval, socklen_t optlen)
Set MCAST_LEAVE_GROUP option.
#define IP_TOS
Definition: bsd_socket.h:153
uint8_t protocol
Definition: ipv4.h:327
#define EINPROGRESS
Definition: bsd_socket.h:260
int_t socketGetSoRcvBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_RCVBUF option.
uint32_t sin6_flowinfo
Definition: bsd_socket.h:390
signed int int_t
Definition: compiler_port.h:56
#define netMutex
Definition: net_legacy.h:195
int_t socketGetIpv6RecvHopLimitOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_RECVHOPLIMIT option.
void * iov_base
Definition: bsd_socket.h:470
int_t socketSetIpv6TrafficClassOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_TCLASS option.
IP network address.
Definition: ip.h:90
int_t socketSetTcpMaxSegOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_MAXSEG option.
in_port_t sin6_port
Definition: bsd_socket.h:389
#define IP_UNBLOCK_SOURCE
Definition: bsd_socket.h:164
size_t iov_len
Definition: bsd_socket.h:471
int_t socketGetSoErrorOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_ERROR option.
const char_t * inet_ntoa(struct in_addr in)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:3958
int_t socketSetMcastLeaveSourceGroupOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_LEAVE_SOURCE_GROUP option.
int_t socketGetIpv6MulticastHopsOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_MULTICAST_HOPS option.
int_t shutdown(int_t s, int_t how)
The shutdown function disables sends or receives on a socket.
Definition: bsd_socket.c:3047
#define ETIMEDOUT
Definition: bsd_socket.h:261
uint8_t p
Definition: ndp.h:300
#define INADDR_ANY
Definition: bsd_socket.h:115
#define SOCKET_ERROR
Definition: bsd_socket.h:238
@ SOCKET_FLAG_DONT_ROUTE
Definition: socket.h:137
#define NO_RECOVERY
Definition: bsd_socket.h:278
uint8_t message[]
Definition: chap.h:154
int_t recv(int_t s, void *data, size_t size, int_t flags)
Receive data from a connected socket.
Definition: bsd_socket.c:917
#define EAI_ADDRFAMILY
Definition: bsd_socket.h:241
Set of sockets.
Definition: bsd_socket.h:530
#define SO_RCVTIMEO
Definition: bsd_socket.h:148
#define TRUE
Definition: os_port.h:50
uint8_t data[]
Definition: ethernet.h:224
Message and ancillary data.
Definition: socket.h:241
int_t socketSetIpv6MulticastHopsOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_MULTICAST_HOPS option.
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2071
Event object.
#define IP_RECVTOS
Definition: bsd_socket.h:158
socklen_t msg_namelen
Definition: bsd_socket.h:482
in_port_t sin_port
Definition: bsd_socket.h:366
struct in6_addr sin6_addr
Definition: bsd_socket.h:391
#define AF_INET
Definition: bsd_socket.h:79
void * msg_control
Definition: bsd_socket.h:485
#define INADDR_NONE
Definition: bsd_socket.h:282
#define IP_MULTICAST_LOOP
Definition: bsd_socket.h:161
Ipv6Addr
Definition: ipv6.h:260
int_t gethostname(char_t *name, size_t len)
Get system host name.
Definition: bsd_socket.c:3307
@ IP_FILTER_MODE_EXCLUDE
Definition: ip.h:68
error_t socketSetMulticastSourceFilter(Socket *socket, const IpAddr *groupAddr, IpFilterMode filterMode, const IpAddr *sources, uint_t numSources)
Set multicast source filter (full-state API)
Definition: socket.c:562
int_t socketGetSoRcvTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_RCVTIMEO option.
uint8_t type
Definition: coap_common.h:176
struct sockaddr_in6 SOCKADDR_IN6
IPv6 address information.
#define SO_SNDBUF
Definition: bsd_socket.h:142
Socket address storage.
Definition: bsd_socket.h:343
#define NET_INTERFACE_COUNT
Definition: net.h:115
#define IPV6_UNICAST_HOPS
Definition: bsd_socket.h:177
int_t socketGetIpTosOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TOS option.
const char_t * inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
Convert an IPv4 or IPv6 address from binary to text.
Definition: bsd_socket.c:4075
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:127
char_t name[]
#define EAI_FAIL
Definition: bsd_socket.h:244
#define osStrcmp(s1, s2)
Definition: os_port.h:174
sa_family_t sin6_family
Definition: bsd_socket.h:388
#define FIONREAD
Definition: bsd_socket.h:209
int_t listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:322
#define EINVAL
Definition: bsd_socket.h:259
#define osStrlen(s)
Definition: os_port.h:168
#define MSG_DONTROUTE
Definition: bsd_socket.h:121
#define IPV4_SUPPORT
Definition: ipv4.h:48
#define IPV6_RECVTCLASS
Definition: bsd_socket.h:188
@ ERROR_END_OF_STREAM
Definition: error.h:211
int_t socketSetIpv6MulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_MULTICAST_LOOP option.
const uint8_t res[]
IPv4 address information.
Definition: bsd_socket.h:364
const char_t * inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
Convert a binary IPv4 address to dot-decimal notation (reentrant version)
Definition: bsd_socket.c:3975
#define BSD_SOCKET_SET_ERRNO(e)
Definition: bsd_socket.h:50
#define MSG_DONTWAIT
Definition: bsd_socket.h:123
#define MCAST_UNBLOCK_SOURCE
Definition: bsd_socket.h:170
#define EWOULDBLOCK
Definition: bsd_socket.h:257
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:298
int_t socketGetIpRecvTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_RECVTTL option.
int_t cmsg_level
Definition: bsd_socket.h:498
int_t socketGetTcpKeepIdleOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPIDLE option.
void socketFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
#define TCP_MAXSEG
Definition: bsd_socket.h:193
#define IP_MULTICAST_IF
Definition: bsd_socket.h:159
uint_t if_nametoindex(const char_t *ifname)
Map an interface name into its corresponding index.
Definition: bsd_socket.c:3812
@ SOCKET_OPTION_IPV6_RECV_TRAFFIC_CLASS
Definition: socket.h:203
IPv6 packet information.
Definition: bsd_socket.h:519
#define IPV6_V6ONLY
Definition: bsd_socket.h:183
void * msg_name
Definition: bsd_socket.h:481
@ SOCKET_FLAG_PEEK
Definition: socket.h:136
int_t socketSetIpRecvTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_RECVTTL option.
int_t socketSetIpv6UnicastHopsOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_UNICAST_HOPS option.
#define MCAST_EXCLUDE
Definition: bsd_socket.h:204
uint8_t h_addr[16]
Definition: bsd_socket.h:544
void freeaddrinfo(struct addrinfo *res)
Free socket address structures.
Definition: bsd_socket.c:3685
#define AF_PACKET
Definition: bsd_socket.h:81
int_t socketSetIpDropSourceMembershipOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_DROP_SOURCE_MEMBERSHIP option.
@ HOST_TYPE_IPV6
Definition: socket.h:218
#define NO_ADDRESS
Definition: bsd_socket.h:279
int_t socketGetTcpKeepCntOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPCNT option.
@ SOCKET_OPTION_IPV4_RECV_TTL
Definition: socket.h:198
int_t socketSetSoKeepAliveOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_KEEPALIVE option.
int_t socketGetIpv6MulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_MULTICAST_LOOP option.
Structure that represents an IPv6 address.
Definition: bsd_socket.h:377
#define IP_ADD_SOURCE_MEMBERSHIP
Definition: bsd_socket.h:166
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
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:760
#define SO_RCVBUF
Definition: bsd_socket.h:143
int_t socketSetIpv6OnlyOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_V6ONLY option.
int_t socketSetIpDontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_DONTFRAG option.
#define EAI_NONAME
Definition: bsd_socket.h:248
@ ERROR_IN_PROGRESS
Definition: error.h:214
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1643
#define TCP_KEEPCNT
Definition: bsd_socket.h:196
char_t * if_indextoname(uint_t ifindex, char_t *ifname)
Map an interface index into its corresponding name.
Definition: bsd_socket.c:3851
#define TCP_NODELAY
Definition: bsd_socket.h:192
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
int32_t tv_sec
Definition: bsd_socket.h:573
uint8_t h
Definition: ndp.h:302
int ipi_ifindex
Definition: bsd_socket.h:509
void socketTranslateErrorCode(Socket *socket, error_t errorCode)
Translate error code.
int_t fd_count
Definition: bsd_socket.h:531
int_t socketSetMcastJoinSourceGroupOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_JOIN_SOURCE_GROUP option.
#define IPPROTO_IP
Definition: bsd_socket.h:95
#define AI_NUMERICHOST
Definition: bsd_socket.h:223
int_t socketSetIpv6DontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_DONTFRAG option.
Socket address.
Definition: bsd_socket.h:332
int_t socketGetSoReuseAddrOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_REUSEADDR option.
error_t
Error codes.
Definition: error.h:43
#define netInterface
Definition: net_legacy.h:199
int_t ai_protocol
Definition: bsd_socket.h:557
int_t socketSetMcastUnblockSourceOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_UNBLOCK_SOURCE option.
IPv6 address information.
Definition: bsd_socket.h:387
#define osSprintf(dest,...)
Definition: os_port.h:234
#define AI_PASSIVE
Definition: bsd_socket.h:221
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:1701
int_t socketGetIpv6RecvTrafficClassOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_RECVTCLASS option.
Structure that represents an IPv4 address.
Definition: bsd_socket.h:354
@ SOCKET_OPTION_IPV6_RECV_HOP_LIMIT
Definition: socket.h:204
uint16_t h_length
Definition: bsd_socket.h:543
__weak_func void osFreeMem(void *p)
Release a previously allocated memory block.
int_t socketGetIpv6DontFragOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_DONTFRAG option.
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
#define MSG_WAITALL
Definition: bsd_socket.h:124
@ SOCKET_EVENT_TX_READY
Definition: socket.h:175
#define EAI_SERVICE
Definition: bsd_socket.h:249
#define EAI_OVERFLOW
Definition: bsd_socket.h:252
void socketSetErrnoCode(Socket *socket, uint_t errnoCode)
Set BSD error code.
int_t socketGetIpPktInfoOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_PKTINFO option.
struct sockaddr_in SOCKADDR_IN
IPv4 address information.
int_t socketGetIpv6UnicastHopsOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_UNICAST_HOPS option.
sa_family_t sin_family
Definition: bsd_socket.h:365
int_t getsockname(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Retrieves the local name for a socket.
Definition: bsd_socket.c:1476
struct hostent * gethostbyname_r(const char_t *name, struct hostent *result, char_t *buf, size_t buflen, int_t *h_errnop)
Host name resolution (reentrant version)
Definition: bsd_socket.c:3371
void osDeleteEvent(OsEvent *event)
Delete an event object.
#define NetInterface
Definition: net.h:36
int ipi6_ifindex
Definition: bsd_socket.h:520
#define ENOTCONN
Definition: bsd_socket.h:270
#define SO_REUSEADDR
Definition: bsd_socket.h:137
int_t fcntl(int_t s, int_t cmd, int_t arg)
Perform specific operation.
Definition: bsd_socket.c:2989
#define SO_BROADCAST
Definition: bsd_socket.h:141
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:471
BSD socket API.
int_t socketGetIpv6OnlyOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_V6ONLY option.
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:1723
int_t socketSetIpRecvTosOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_RECVTOS option.
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1903
int_t socketSetIpTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_TTL option.
#define IP_MULTICAST_TTL
Definition: bsd_socket.h:160
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:2283
int_t msg_iovlen
Definition: bsd_socket.h:484
#define SOCKET_SUCCESS
Definition: bsd_socket.h:237
#define IP_PKTINFO
Definition: bsd_socket.h:156
int_t socketSetIpAddSourceMembershipOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_ADD_SOURCE_MEMBERSHIP option.
int_t socketSetIpAddMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_ADD_MEMBERSHIP option.
int_t socketSetTcpKeepCntOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPCNT option.
Message header.
Definition: bsd_socket.h:480
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1354
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:66
int_t socketGetSoNoCheckOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_NO_CHECK option.
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2029
#define IPV6_MULTICAST_HOPS
Definition: bsd_socket.h:179
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:532
int_t socketSetIpPktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_PKTINFO option.
#define IPPROTO_TCP
Definition: bsd_socket.h:98
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
#define SO_SNDTIMEO
Definition: bsd_socket.h:147
in_addr_t inet_addr(const char_t *cp)
Convert a dot-decimal string into binary data in network byte order.
Definition: bsd_socket.c:3889
int_t socketSetIpDropMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_DROP_MEMBERSHIP option.
#define ENAMETOOLONG
Definition: bsd_socket.h:262
#define MCAST_JOIN_GROUP
Definition: bsd_socket.h:168
int32_t tv_usec
Definition: bsd_socket.h:574
size_t length
Definition: ip.h:91
int_t socketSetIpv6RecvHopLimitOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_RECVHOPLIMIT option.
socklen_t ai_addrlen
Definition: bsd_socket.h:558
@ HOST_TYPE_IPV4
Definition: socket.h:217
struct addrinfo ADDRINFO
Information about address of a service provider.
#define HOST_NOT_FOUND
Definition: bsd_socket.h:276
uint32_t sin6_scope_id
Definition: bsd_socket.h:392
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:49
#define ENABLED
Definition: os_port.h:37
#define IP_DROP_SOURCE_MEMBERSHIP
Definition: bsd_socket.h:167
int_t socketSetIpv6AddMembershipOption(Socket *socket, const struct ipv6_mreq *optval, socklen_t optlen)
Set IPV6_ADD_MEMBERSHIP option.
int_t socketGetIpMulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_LOOP option.
int_t socketGetIpTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TTL option.
int_t socketSetIpTosOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_TOS option.
#define MCAST_BLOCK_SOURCE
Definition: bsd_socket.h:169
int_t recvfrom(int_t s, void *data, size_t size, int_t flags, struct sockaddr *addr, socklen_t *addrlen)
Receive a datagram.
Definition: bsd_socket.c:989
#define FIONSPACE
Definition: bsd_socket.h:211
#define MSG_CTRUNC
Definition: bsd_socket.h:122
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:1456
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:2889
int_t socketGetIpv6TrafficClassOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_TCLASS option.
#define IP_RECVTTL
Definition: bsd_socket.h:157
uint32_t systime_t
System time.
#define IPV6_DONTFRAG
Definition: bsd_socket.h:187
uint16_t port
Definition: dns_common.h:270
#define ntohs(value)
Definition: cpu_endian.h:421
int_t socketSetTcpKeepIntvlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPINTVL option.
int_t socketGetSoTypeOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_TYPE option.
in_addr_t s_addr
Definition: bsd_socket.h:355
#define osStrtoul(s, endptr, base)
Definition: os_port.h:258
#define SO_NO_CHECK
Definition: bsd_socket.h:145
int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:107
#define EFAULT
Definition: bsd_socket.h:258
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:198
Ipv4Addr ipv4Addr
Definition: ip.h:95
int_t socketGetSoSndTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_SNDTIMEO option.
struct in_addr ipi_addr
Definition: bsd_socket.h:510
error_t socketGetMulticastSourceFilter(Socket *socket, const IpAddr *groupAddr, IpFilterMode *filterMode, IpAddr *sources, uint_t *numSources)
Get multicast source filter.
Definition: socket.c:679
#define SO_TYPE
Definition: bsd_socket.h:138
Information about a given host.
Definition: bsd_socket.h:541
#define F_SETFL
Definition: bsd_socket.h:215
BSD socket options.
int_t socketGetSoBroadcastOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_BROADCAST option.
uint32_t time
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:123
int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
Determine the status of one or more sockets.
Definition: bsd_socket.c:3125
#define FIONBIO
Definition: bsd_socket.h:208
Helper functions for sockets.
size_t cmsg_len
Definition: bsd_socket.h:497
#define FIONWRITE
Definition: bsd_socket.h:210
@ SOCKET_FLAG_NO_DELAY
Definition: socket.h:143
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
uint8_t n
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
#define MCAST_LEAVE_SOURCE_GROUP
Definition: bsd_socket.h:173
#define INET_ADDRSTRLEN
Definition: bsd_socket.h:285
int_t setipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group, uint32_t fmode, uint_t numsrc, struct in_addr *slist)
Set multicast source filter (IPv4 only)
Definition: bsd_socket.c:2367
Timeout structure.
Definition: bsd_socket.h:572
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
#define IPV6_MULTICAST_LOOP
Definition: bsd_socket.h:180
struct in6_addr ipi6_addr
Definition: bsd_socket.h:521
#define IP_DONTFRAG
Definition: bsd_socket.h:174
int_t socketSetIpUnblockSourceOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_UNBLOCK_SOURCE option.
Ipv4Addr groupAddr
Definition: igmp_common.h:214
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
int_t socketSetIpv6DropMembershipOption(Socket *socket, const struct ipv6_mreq *optval, socklen_t optlen)
Set IPV6_DROP_MEMBERSHIP option.
struct in_addr sin_addr
Definition: bsd_socket.h:367
int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
Convert a socket address to a corresponding host and service.
Definition: bsd_socket.c:3714
#define Socket
Definition: socket.h:36
int_t socketSetSoSndTimeoOption(Socket *socket, const struct timeval *optval, socklen_t optlen)
Set SO_SNDTIMEO option.
#define CMSG_SPACE(len)
Definition: bsd_socket.h:587
int_t socketSetSoBroadcastOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_BROADCAST option.
@ IP_FILTER_MODE_INCLUDE
Definition: ip.h:69
int_t ai_socktype
Definition: bsd_socket.h:556
#define MCAST_INCLUDE
Definition: bsd_socket.h:205
bool_t osCreateEvent(OsEvent *event)
Create an event object.
#define SOCKET_MAX_MULTICAST_SOURCES
Definition: socket.h:60
#define IPV6_PKTINFO
Definition: bsd_socket.h:184
#define INET6_ADDRSTRLEN
Definition: bsd_socket.h:288
#define SOL_SOCKET
Definition: bsd_socket.h:112
@ SOCKET_OPTION_IPV6_PKT_INFO
Definition: socket.h:202
#define EAI_AGAIN
Definition: bsd_socket.h:242
int_t getipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group, uint32_t *fmode, uint_t *numsrc, struct in_addr *slist)
Get multicast source filter (IPv4 only)
Definition: bsd_socket.c:2468
#define CMSG_LEN(len)
Definition: bsd_socket.h:588
const struct in6_addr in6addr_loopback
Definition: bsd_socket.c:52
#define EAI_FAMILY
Definition: bsd_socket.h:245
#define IPV6_RECVHOPLIMIT
Definition: bsd_socket.h:185
int_t accept(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Permit an incoming connection attempt on a socket.
Definition: bsd_socket.c:360
sa_family_t sa_family
Definition: bsd_socket.h:333
int_t socketGetIpRecvTosOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_RECVTOS option.
int_t socketSetSoRcvTimeoOption(Socket *socket, const struct timeval *optval, socklen_t optlen)
Set SO_RCVTIMEO option.
Socket API.
#define EOPNOTSUPP
Definition: bsd_socket.h:265
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:1512
#define MCAST_JOIN_SOURCE_GROUP
Definition: bsd_socket.h:172
int_t socketGetSoKeepAliveOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_KEEPALIVE option.
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:95
int_t getsockopt(int_t s, int_t level, int_t optname, void *optval, socklen_t *optlen)
The getsockopt function retrieves a socket option.
Definition: bsd_socket.c:2078
int_t socketGetIpDontFragOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_DONTFRAG option.
#define AF_UNSPEC
Definition: bsd_socket.h:78
int_t getpeername(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Retrieves the address of the peer to which a socket is connected.
Definition: bsd_socket.c:1626
int_t socketSetIpMulticastTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_MULTICAST_TTL option.
uint8_t s
Definition: igmp_common.h:234
#define ipv4CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv4.h:155
error_t ipv4StringToAddr(const char_t *str, Ipv4Addr *ipAddr)
Convert a dot-decimal string to a binary IPv4 address.
Definition: ipv4.c:1389
Ipv4Addr ipAddr
Definition: ipcp.h:105
#define MSG_PEEK
Definition: bsd_socket.h:120
struct iovec * msg_iov
Definition: bsd_socket.h:483
#define NETDB_SUCCESS
Definition: bsd_socket.h:275
int_t setsourcefilter(int_t s, uint32_t interface, struct sockaddr *group, socklen_t grouplen, uint32_t fmode, uint_t numsrc, struct sockaddr_storage *slist)
Set multicast source filter.
Definition: bsd_socket.c:2563
int_t sendto(int_t s, const void *data, size_t length, int_t flags, const struct sockaddr *addr, socklen_t addrlen)
Send a datagram to a specific destination.
Definition: bsd_socket.c:535
int_t socketSetIpMulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_MULTICAST_LOOP option.
error_t ipv6StringToAddr(const char_t *str, Ipv6Addr *ipAddr)
Convert a string representation of an IPv6 address to a binary IPv6 address.
Definition: ipv6.c:2184
#define SO_KEEPALIVE
Definition: bsd_socket.h:144
#define IP_ADD_MEMBERSHIP
Definition: bsd_socket.h:162
#define O_NONBLOCK
Definition: bsd_socket.h:218
Ipv4Addr addr
Definition: nbns_common.h:141
#define EAI_MEMORY
Definition: bsd_socket.h:246
int_t socketSetSoReuseAddrOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_REUSEADDR option.
int_t socketSetTcpNoDelayOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_NODELAY option.
int_t socketSetIpv6MulticastIfOption(Socket *socket, const struct in_addr *optval, socklen_t optlen)
Set IPV6_MULTICAST_IF option.
#define IP_TTL
Definition: bsd_socket.h:154
#define IPV6_MULTICAST_IF
Definition: bsd_socket.h:178
int_t cmsg_type
Definition: bsd_socket.h:499
#define ENOBUFS
Definition: bsd_socket.h:268
uint8_t flags
Definition: tcp.h:358
Ipv6Addr ipv6Addr
Definition: ip.h:98
int_t socketSetIpv6RecvTrafficClassOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_RECVTCLASS option.
#define IPPROTO_IPV6
Definition: bsd_socket.h:100
int_t ai_family
Definition: bsd_socket.h:555
int_t getaddrinfo(const char_t *node, const char_t *service, const struct addrinfo *hints, struct addrinfo **res)
Convert host and service names to socket address.
Definition: bsd_socket.c:3445
unsigned int uint_t
Definition: compiler_port.h:57
size_t msg_controllen
Definition: bsd_socket.h:486
@ SOCKET_FLAG_DONT_WAIT
Definition: socket.h:139
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:253
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
int_t socketSetMcastBlockSourceOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_BLOCK_SOURCE option.
@ SOCKET_EVENT_CLOSED
Definition: socket.h:174
int_t socketSetIpv6PktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_PKTINFO option.
char_t * ipv4AddrToString(Ipv4Addr ipAddr, char_t *str)
Convert a binary IPv4 address to dot-decimal notation.
Definition: ipv4.c:1478
#define IPV6_TCLASS
Definition: bsd_socket.h:189
int_t socketGetSoSndBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_SNDBUF option.
int_t socketGetIpv6PktInfoOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_PKTINFO option.
#define SOCKET_MAX_COUNT
Definition: socket.h:46
int_t inet_aton(const char_t *cp, struct in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:3923
#define IPV6_DROP_MEMBERSHIP
Definition: bsd_socket.h:182
int_t socketGetTcpNoDelayOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_NODELAY option.
#define osStrcpy(s1, s2)
Definition: os_port.h:210
#define TCP_KEEPINTVL
Definition: bsd_socket.h:195
int_t send(int_t s, const void *data, size_t length, int_t flags)
Send data to a connected socket.
Definition: bsd_socket.c:459
int_t socketGetIpMulticastTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_TTL option.
#define MCAST_LEAVE_GROUP
Definition: bsd_socket.h:171
uint16_t next
Definition: ipv4_frag.h:106
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:276
#define TCP_KEEPIDLE
Definition: bsd_socket.h:194
int_t socketSetMcastJoinGroupOption(Socket *socket, const struct group_req *optval, socklen_t optlen)
Set MCAST_JOIN_GROUP option.
int_t socketSetIpMulticastIfOption(Socket *socket, const struct in_addr *optval, socklen_t optlen)
Set IP_MULTICAST_IF option.
Information about address of a service provider.
Definition: bsd_socket.h:553
int_t msg_flags
Definition: bsd_socket.h:487
#define IPV6_ADD_MEMBERSHIP
Definition: bsd_socket.h:181
int_t socketSetIpBlockSourceOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_BLOCK_SOURCE option.
@ NO_ERROR
Success.
Definition: error.h:44
#define IP_DROP_MEMBERSHIP
Definition: bsd_socket.h:163
Debugging facilities.
@ SOCKET_OPTION_TCP_NO_DELAY
Definition: socket.h:205
uint8_t s6_addr[16]
Definition: bsd_socket.h:378
#define INFINITE_DELAY
Definition: os_port.h:75
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:117
@ SOCKET_OPTION_IPV4_PKT_INFO
Definition: socket.h:196
int_t sendmsg(int_t s, struct msghdr *msg, int_t flags)
Send a message.
Definition: bsd_socket.c:661
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:3083
uint16_t h_addrtype
Definition: bsd_socket.h:542
struct addrinfo * ai_next
Definition: bsd_socket.h:561
IPv4 packet information.
Definition: bsd_socket.h:508
#define SO_ERROR
Definition: bsd_socket.h:139
error_t socketListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: socket.c:1418