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-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL 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
1549  {
1550  //The socket is not bound to any address
1552  ret = SOCKET_ERROR;
1553  }
1554 
1555  //Release exclusive access
1557 
1558  //return status code
1559  return ret;
1560 }
1561 
1562 
1563 /**
1564  * @brief Retrieves the address of the peer to which a socket is connected
1565  * @param[in] s Descriptor identifying a socket
1566  * @param[out] addr Address of the peer
1567  * @param[in,out] addrlen Length in bytes of the address
1568  * @return If no error occurs, getpeername returns SOCKET_SUCCESS
1569  * Otherwise, it returns SOCKET_ERROR
1570  **/
1571 
1573 {
1574  int_t ret;
1575  Socket *sock;
1576 
1577  //Make sure the socket descriptor is valid
1578  if(s < 0 || s >= SOCKET_MAX_COUNT)
1579  {
1580  return SOCKET_ERROR;
1581  }
1582 
1583  //Point to the socket structure
1584  sock = &socketTable[s];
1585 
1586  //Get exclusive access
1588 
1589  //Check whether the socket is connected to a peer
1590  if(sock->remoteIpAddr.length != 0)
1591  {
1592 #if (IPV4_SUPPORT == ENABLED)
1593  //IPv4 address?
1594  if(sock->remoteIpAddr.length == sizeof(Ipv4Addr) &&
1595  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
1596  {
1597  //Point to the IPv4 address information
1598  SOCKADDR_IN *sa = (SOCKADDR_IN *) addr;
1599 
1600  //Set address family and port number
1601  sa->sin_family = AF_INET;
1602  sa->sin_port = htons(sock->remotePort);
1603 
1604  //Copy IPv4 address
1605  sa->sin_addr.s_addr = sock->remoteIpAddr.ipv4Addr;
1606 
1607  //Return the actual length of the address
1608  *addrlen = sizeof(SOCKADDR_IN);
1609  //Successful processing
1610  ret = SOCKET_SUCCESS;
1611  }
1612  else
1613 #endif
1614 #if (IPV6_SUPPORT == ENABLED)
1615  //IPv6 address?
1616  if(sock->remoteIpAddr.length == sizeof(Ipv6Addr) &&
1617  *addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
1618  {
1619  //Point to the IPv6 address information
1620  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) addr;
1621 
1622  //Set address family and port number
1623  sa->sin6_family = AF_INET6;
1624  sa->sin6_port = htons(sock->remotePort);
1625  sa->sin6_flowinfo = 0;
1626  sa->sin6_scope_id = 0;
1627 
1628  //Copy IPv6 address
1629  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sock->remoteIpAddr.ipv6Addr);
1630 
1631  //Return the actual length of the address
1632  *addrlen = sizeof(SOCKADDR_IN6);
1633  //Successful processing
1634  ret = SOCKET_SUCCESS;
1635  }
1636  else
1637 #endif
1638  {
1639  //The specified length is not valid
1640  socketSetErrnoCode(sock, EINVAL);
1641  ret = SOCKET_ERROR;
1642  }
1643  }
1644  else
1645  {
1646  //The socket is not connected to any peer
1648  ret = SOCKET_ERROR;
1649  }
1650 
1651  //Release exclusive access
1653 
1654  //return status code
1655  return ret;
1656 }
1657 
1658 
1659 /**
1660  * @brief The setsockopt function sets a socket option
1661  * @param[in] s Descriptor that identifies a socket
1662  * @param[in] level The level at which the option is defined
1663  * @param[in] optname The socket option for which the value is to be set
1664  * @param[in] optval A pointer to the buffer in which the value for the
1665  * requested option is specified
1666  * @param[in] optlen The size, in bytes, of the buffer pointed to by the optval
1667  * parameter
1668  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
1669  * Otherwise, it returns SOCKET_ERROR
1670  **/
1671 
1672 int_t setsockopt(int_t s, int_t level, int_t optname, const void *optval,
1673  socklen_t optlen)
1674 {
1675  int_t ret;
1676  Socket *sock;
1677 
1678  //Make sure the socket descriptor is valid
1679  if(s < 0 || s >= SOCKET_MAX_COUNT)
1680  {
1681  return SOCKET_ERROR;
1682  }
1683 
1684  //Point to the socket structure
1685  sock = &socketTable[s];
1686 
1687  //Make sure the option is valid
1688  if(optval != NULL)
1689  {
1690  //Check option level
1691  if(level == SOL_SOCKET)
1692  {
1693  //Check option type
1694  if(optname == SO_REUSEADDR)
1695  {
1696  //Set SO_REUSEADDR option
1697  ret = socketSetSoReuseAddrOption(sock, optval, optlen);
1698  }
1699  else if(optname == SO_BROADCAST)
1700  {
1701  //Set SO_BROADCAST option
1702  ret = socketSetSoBroadcastOption(sock, optval, optlen);
1703  }
1704  else if(optname == SO_SNDTIMEO)
1705  {
1706  //Set SO_SNDTIMEO option
1707  ret = socketSetSoSndTimeoOption(sock, optval, optlen);
1708  }
1709  else if(optname == SO_RCVTIMEO)
1710  {
1711  //Set SO_RCVTIMEO option
1712  ret = socketSetSoRcvTimeoOption(sock, optval, optlen);
1713  }
1714  else if(optname == SO_SNDBUF)
1715  {
1716  //Set SO_SNDBUF option
1717  ret = socketSetSoSndBufOption(sock, optval, optlen);
1718  }
1719  else if(optname == SO_RCVBUF)
1720  {
1721  //Set SO_RCVBUF option
1722  ret = socketSetSoRcvBufOption(sock, optval, optlen);
1723  }
1724  else if(optname == SO_KEEPALIVE)
1725  {
1726  //Set SO_KEEPALIVE option
1727  ret = socketSetSoKeepAliveOption(sock, optval, optlen);
1728  }
1729  else if(optname == SO_NO_CHECK)
1730  {
1731  //Set SO_NO_CHECK option
1732  ret = socketSetSoNoCheckOption(sock, optval, optlen);
1733  }
1734  else
1735  {
1736  //Unknown option
1738  //Report an error
1739  ret = SOCKET_ERROR;
1740  }
1741  }
1742  else if(level == IPPROTO_IP)
1743  {
1744  //Check option type
1745  if(optname == IP_TOS)
1746  {
1747  //Set IP_TOS option
1748  ret = socketSetIpTosOption(sock, optval, optlen);
1749  }
1750  else if(optname == IP_TTL)
1751  {
1752  //Set IP_TTL option
1753  ret = socketSetIpTtlOption(sock, optval, optlen);
1754  }
1755  else if(optname == IP_MULTICAST_IF)
1756  {
1757  //Set IP_MULTICAST_IF option
1758  ret = socketSetIpMulticastIfOption(sock, optval, optlen);
1759  }
1760  else if(optname == IP_MULTICAST_TTL)
1761  {
1762  //Set IP_MULTICAST_TTL option
1763  ret = socketSetIpMulticastTtlOption(sock, optval, optlen);
1764  }
1765  else if(optname == IP_MULTICAST_LOOP)
1766  {
1767  //Set IP_MULTICAST_LOOP option
1768  ret = socketSetIpMulticastLoopOption(sock, optval, optlen);
1769  }
1770  else if(optname == IP_ADD_MEMBERSHIP)
1771  {
1772  //Set IP_ADD_MEMBERSHIP option
1773  ret = socketSetIpAddMembershipOption(sock, optval, optlen);
1774  }
1775  else if(optname == IP_DROP_MEMBERSHIP)
1776  {
1777  //Set IP_DROP_MEMBERSHIP option
1778  ret = socketSetIpDropMembershipOption(sock, optval, optlen);
1779  }
1780  else if(optname == IP_BLOCK_SOURCE)
1781  {
1782  //Set IP_BLOCK_SOURCE option
1783  ret = socketSetIpBlockSourceOption(sock, optval, optlen);
1784  }
1785  else if(optname == IP_UNBLOCK_SOURCE)
1786  {
1787  //Set IP_UNBLOCK_SOURCE option
1788  ret = socketSetIpUnblockSourceOption(sock, optval, optlen);
1789  }
1790  else if(optname == IP_ADD_SOURCE_MEMBERSHIP)
1791  {
1792  //Set IP_ADD_SOURCE_MEMBERSHIP option
1793  ret = socketSetIpAddSourceMembershipOption(sock, optval, optlen);
1794  }
1795  else if(optname == IP_DROP_SOURCE_MEMBERSHIP)
1796  {
1797  //Set IP_DROP_SOURCE_MEMBERSHIP option
1798  ret = socketSetIpDropSourceMembershipOption(sock, optval, optlen);
1799  }
1800  else if(optname == MCAST_JOIN_GROUP)
1801  {
1802  //Set MCAST_JOIN_GROUP option
1803  ret = socketSetMcastJoinGroupOption(sock, optval, optlen);
1804  }
1805  else if(optname == MCAST_LEAVE_GROUP)
1806  {
1807  //Set MCAST_LEAVE_GROUP option
1808  ret = socketSetMcastLeaveGroupOption(sock, optval, optlen);
1809  }
1810  else if(optname == MCAST_BLOCK_SOURCE)
1811  {
1812  //Set MCAST_BLOCK_SOURCE option
1813  ret = socketSetMcastBlockSourceOption(sock, optval, optlen);
1814  }
1815  else if(optname == MCAST_UNBLOCK_SOURCE)
1816  {
1817  //Set MCAST_UNBLOCK_SOURCE option
1818  ret = socketSetMcastUnblockSourceOption(sock, optval, optlen);
1819  }
1820  else if(optname == MCAST_JOIN_SOURCE_GROUP)
1821  {
1822  //Set MCAST_JOIN_SOURCE_GROUP option
1823  ret = socketSetMcastJoinSourceGroupOption(sock, optval, optlen);
1824  }
1825  else if(optname == MCAST_LEAVE_SOURCE_GROUP)
1826  {
1827  //Set MCAST_LEAVE_SOURCE_GROUP option
1828  ret = socketSetMcastLeaveSourceGroupOption(sock, optval, optlen);
1829  }
1830  else if(optname == IP_DONTFRAG)
1831  {
1832  //Set IP_DONTFRAG option
1833  ret = socketSetIpDontFragOption(sock, optval, optlen);
1834  }
1835  else if(optname == IP_PKTINFO)
1836  {
1837  //Set IP_PKTINFO option
1838  ret = socketSetIpPktInfoOption(sock, optval, optlen);
1839  }
1840  else if(optname == IP_RECVTOS)
1841  {
1842  //Set IP_RECVTOS option
1843  ret = socketSetIpRecvTosOption(sock, optval, optlen);
1844  }
1845  else if(optname == IP_RECVTTL)
1846  {
1847  //Set IP_RECVTTL option
1848  ret = socketSetIpRecvTtlOption(sock, optval, optlen);
1849  }
1850  else
1851  {
1852  //Unknown option
1854  ret = SOCKET_ERROR;
1855  }
1856  }
1857  else if(level == IPPROTO_IPV6)
1858  {
1859  //Check option type
1860  if(optname == IPV6_TCLASS)
1861  {
1862  //Set IPV6_TCLASS option
1863  ret = socketSetIpv6TrafficClassOption(sock, optval, optlen);
1864  }
1865  else if(optname == IPV6_UNICAST_HOPS)
1866  {
1867  //Set IPV6_UNICAST_HOPS option
1868  ret = socketSetIpv6UnicastHopsOption(sock, optval, optlen);
1869  }
1870  else if(optname == IPV6_MULTICAST_IF)
1871  {
1872  //Set IPV6_MULTICAST_IF option
1873  ret = socketSetIpv6MulticastIfOption(sock, optval, optlen);
1874  }
1875  else if(optname == IPV6_MULTICAST_HOPS)
1876  {
1877  //Set IPV6_MULTICAST_HOPS option
1878  ret = socketSetIpv6MulticastHopsOption(sock, optval, optlen);
1879  }
1880  else if(optname == IPV6_MULTICAST_LOOP)
1881  {
1882  //Set IPV6_MULTICAST_LOOP option
1883  ret = socketSetIpv6MulticastLoopOption(sock, optval, optlen);
1884  }
1885  else if(optname == IPV6_ADD_MEMBERSHIP)
1886  {
1887  //Set IPV6_ADD_MEMBERSHIP option
1888  ret = socketSetIpv6AddMembershipOption(sock, optval, optlen);
1889  }
1890  else if(optname == IPV6_DROP_MEMBERSHIP)
1891  {
1892  //Set IPV6_DROP_MEMBERSHIP option
1893  ret = socketSetIpv6DropMembershipOption(sock, optval, optlen);
1894  }
1895  else if(optname == MCAST_JOIN_GROUP)
1896  {
1897  //Set MCAST_JOIN_GROUP option
1898  ret = socketSetMcastJoinGroupOption(sock, optval, optlen);
1899  }
1900  else if(optname == MCAST_LEAVE_GROUP)
1901  {
1902  //Set MCAST_LEAVE_GROUP option
1903  ret = socketSetMcastLeaveGroupOption(sock, optval, optlen);
1904  }
1905  else if(optname == MCAST_BLOCK_SOURCE)
1906  {
1907  //Set MCAST_BLOCK_SOURCE option
1908  ret = socketSetMcastBlockSourceOption(sock, optval, optlen);
1909  }
1910  else if(optname == MCAST_UNBLOCK_SOURCE)
1911  {
1912  //Set MCAST_UNBLOCK_SOURCE option
1913  ret = socketSetMcastUnblockSourceOption(sock, optval, optlen);
1914  }
1915  else if(optname == MCAST_JOIN_SOURCE_GROUP)
1916  {
1917  //Set MCAST_JOIN_SOURCE_GROUP option
1918  ret = socketSetMcastJoinSourceGroupOption(sock, optval, optlen);
1919  }
1920  else if(optname == MCAST_LEAVE_SOURCE_GROUP)
1921  {
1922  //Set MCAST_LEAVE_SOURCE_GROUP option
1923  ret = socketSetMcastLeaveSourceGroupOption(sock, optval, optlen);
1924  }
1925  else if(optname == IPV6_V6ONLY)
1926  {
1927  //Set IPV6_V6ONLY option
1928  ret = socketSetIpv6OnlyOption(sock, optval, optlen);
1929  }
1930  else if(optname == IPV6_DONTFRAG)
1931  {
1932  //Set IPV6_DONTFRAG option
1933  ret = socketSetIpv6DontFragOption(sock, optval, optlen);
1934  }
1935  else if(optname == IPV6_PKTINFO)
1936  {
1937  //Set IPV6_PKTINFO option
1938  ret = socketSetIpv6PktInfoOption(sock, optval, optlen);
1939  }
1940  else if(optname == IPV6_RECVTCLASS)
1941  {
1942  //Set IPV6_RECVTCLASS option
1943  ret = socketSetIpv6RecvTrafficClassOption(sock, optval, optlen);
1944  }
1945  else if(optname == IPV6_RECVHOPLIMIT)
1946  {
1947  //Set IPV6_RECVHOPLIMIT option
1948  ret = socketSetIpv6RecvHopLimitOption(sock, optval, optlen);
1949  }
1950  else
1951  {
1952  //Unknown option
1954  ret = SOCKET_ERROR;
1955  }
1956  }
1957  else if(level == IPPROTO_TCP)
1958  {
1959  //Check option type
1960  if(optname == TCP_NODELAY)
1961  {
1962  //Set TCP_NODELAY option
1963  ret = socketSetTcpNoDelayOption(sock, optval, optlen);
1964  }
1965  else if(optname == TCP_MAXSEG)
1966  {
1967  //Set TCP_MAXSEG option
1968  ret = socketSetTcpMaxSegOption(sock, optval, optlen);
1969  }
1970  else if(optname == TCP_KEEPIDLE)
1971  {
1972  //Set TCP_KEEPIDLE option
1973  ret = socketSetTcpKeepIdleOption(sock, optval, optlen);
1974  }
1975  else if(optname == TCP_KEEPINTVL)
1976  {
1977  //Set TCP_KEEPINTVL option
1978  ret = socketSetTcpKeepIntvlOption(sock, optval, optlen);
1979  }
1980  else if(optname == TCP_KEEPCNT)
1981  {
1982  //Set TCP_KEEPCNT option
1983  ret = socketSetTcpKeepCntOption(sock, optval, optlen);
1984  }
1985  else
1986  {
1987  //Unknown option
1989  ret = SOCKET_ERROR;
1990  }
1991  }
1992  else
1993  {
1994  //The specified level is not valid
1995  socketSetErrnoCode(sock, EINVAL);
1996  ret = SOCKET_ERROR;
1997  }
1998  }
1999  else
2000  {
2001  //The option is not valid
2002  socketSetErrnoCode(sock, EFAULT);
2003  ret = SOCKET_ERROR;
2004  }
2005 
2006  //return status code
2007  return ret;
2008 }
2009 
2010 
2011 /**
2012  * @brief The getsockopt function retrieves a socket option
2013  * @param[in] s Descriptor that identifies a socket
2014  * @param[in] level The level at which the option is defined
2015  * @param[in] optname The socket option for which the value is to be retrieved
2016  * @param[out] optval A pointer to the buffer in which the value for the
2017  * requested option is to be returned
2018  * @param[in,out] optlen The size, in bytes, of the buffer pointed to by the
2019  * optval parameter
2020  * @return If no error occurs, getsockopt returns SOCKET_SUCCESS
2021  * Otherwise, it returns SOCKET_ERROR
2022  **/
2023 
2024 int_t getsockopt(int_t s, int_t level, int_t optname, void *optval,
2025  socklen_t *optlen)
2026 {
2027  int_t ret;
2028  Socket *sock;
2029 
2030  //Make sure the socket descriptor is valid
2031  if(s < 0 || s >= SOCKET_MAX_COUNT)
2032  {
2033  return SOCKET_ERROR;
2034  }
2035 
2036  //Point to the socket structure
2037  sock = &socketTable[s];
2038 
2039  //Get exclusive access
2041 
2042  //Make sure the option is valid
2043  if(optval != NULL)
2044  {
2045  //Check option level
2046  if(level == SOL_SOCKET)
2047  {
2048  //Check option type
2049  if(optname == SO_REUSEADDR)
2050  {
2051  //Get SO_REUSEADDR option
2052  ret = socketGetSoReuseAddrOption(sock, optval, optlen);
2053  }
2054  else if(optname == SO_TYPE)
2055  {
2056  //Get SO_TYPE option
2057  ret = socketGetSoTypeOption(sock, optval, optlen);
2058  }
2059  else if(optname == SO_ERROR)
2060  {
2061  //Get SO_ERROR option
2062  ret = socketGetSoErrorOption(sock, optval, optlen);
2063  }
2064  else if(optname == SO_BROADCAST)
2065  {
2066  //Get SO_BROADCAST option
2067  ret = socketGetSoBroadcastOption(sock, optval, optlen);
2068  }
2069  else if(optname == SO_SNDTIMEO)
2070  {
2071  //Get SO_SNDTIMEO option
2072  ret = socketGetSoSndTimeoOption(sock, optval, optlen);
2073  }
2074  else if(optname == SO_RCVTIMEO)
2075  {
2076  //Get SO_RCVTIMEO option
2077  ret = socketGetSoRcvTimeoOption(sock, optval, optlen);
2078  }
2079  else if(optname == SO_SNDBUF)
2080  {
2081  //Get SO_SNDBUF option
2082  ret = socketGetSoSndBufOption(sock, optval, optlen);
2083  }
2084  else if(optname == SO_RCVBUF)
2085  {
2086  //Get SO_RCVBUF option
2087  ret = socketGetSoRcvBufOption(sock, optval, optlen);
2088  }
2089  else if(optname == SO_KEEPALIVE)
2090  {
2091  //Get SO_KEEPALIVE option
2092  ret = socketGetSoKeepAliveOption(sock, optval, optlen);
2093  }
2094  else if(optname == SO_NO_CHECK)
2095  {
2096  //Get SO_NO_CHECK option
2097  ret = socketGetSoNoCheckOption(sock, optval, optlen);
2098  }
2099  else
2100  {
2101  //Unknown option
2103  ret = SOCKET_ERROR;
2104  }
2105  }
2106  else if(level == IPPROTO_IP)
2107  {
2108  //Check option type
2109  if(optname == IP_TOS)
2110  {
2111  //Get IP_TOS option
2112  ret = socketGetIpTosOption(sock, optval, optlen);
2113  }
2114  else if(optname == IP_TTL)
2115  {
2116  //Get IP_TTL option
2117  ret = socketGetIpTtlOption(sock, optval, optlen);
2118  }
2119  else if(optname == IP_MULTICAST_TTL)
2120  {
2121  //Get IP_MULTICAST_TTL option
2122  ret = socketGetIpMulticastTtlOption(sock, optval, optlen);
2123  }
2124  else if(optname == IP_MULTICAST_LOOP)
2125  {
2126  //Get IP_MULTICAST_LOOP option
2127  ret = socketGetIpMulticastLoopOption(sock, optval, optlen);
2128  }
2129  else if(optname == IP_ADD_MEMBERSHIP ||
2130  optname == IP_DROP_MEMBERSHIP ||
2131  optname == IP_BLOCK_SOURCE ||
2132  optname == IP_UNBLOCK_SOURCE ||
2133  optname == IP_ADD_SOURCE_MEMBERSHIP ||
2134  optname == IP_DROP_SOURCE_MEMBERSHIP ||
2135  optname == MCAST_JOIN_GROUP ||
2136  optname == MCAST_LEAVE_GROUP ||
2137  optname == MCAST_BLOCK_SOURCE ||
2138  optname == MCAST_UNBLOCK_SOURCE ||
2139  optname == MCAST_JOIN_SOURCE_GROUP ||
2140  optname == MCAST_LEAVE_SOURCE_GROUP)
2141  {
2142  //When any of these options are used with getsockopt, the error
2143  //generated is EOPNOTSUPP (refer to RFC 3678, section 4.1.3)
2145  ret = SOCKET_ERROR;
2146  }
2147  else if(optname == IP_DONTFRAG)
2148  {
2149  //Get IP_DONTFRAG option
2150  ret = socketGetIpDontFragOption(sock, optval, optlen);
2151  }
2152  else if(optname == IP_PKTINFO)
2153  {
2154  //Get IP_PKTINFO option
2155  ret = socketGetIpPktInfoOption(sock, optval, optlen);
2156  }
2157  else if(optname == IP_RECVTOS)
2158  {
2159  //Get IP_RECVTOS option
2160  ret = socketGetIpRecvTosOption(sock, optval, optlen);
2161  }
2162  else if(optname == IP_RECVTTL)
2163  {
2164  //Get IP_RECVTTL option
2165  ret = socketGetIpRecvTtlOption(sock, optval, optlen);
2166  }
2167  else
2168  {
2169  //Unknown option
2171  ret = SOCKET_ERROR;
2172  }
2173  }
2174  else if(level == IPPROTO_IPV6)
2175  {
2176  //Check option type
2177  if(optname == IPV6_TCLASS)
2178  {
2179  //Get IPV6_TCLASS option
2180  ret = socketGetIpv6TrafficClassOption(sock, optval, optlen);
2181  }
2182  else if(optname == IPV6_UNICAST_HOPS)
2183  {
2184  //Get IPV6_UNICAST_HOPS option
2185  ret = socketGetIpv6UnicastHopsOption(sock, optval, optlen);
2186  }
2187  else if(optname == IPV6_MULTICAST_HOPS)
2188  {
2189  //Get IPV6_MULTICAST_HOPS option
2190  ret = socketGetIpv6MulticastHopsOption(sock, optval, optlen);
2191  }
2192  else if(optname == IPV6_MULTICAST_LOOP)
2193  {
2194  //Get IPV6_MULTICAST_LOOP option
2195  ret = socketGetIpv6MulticastLoopOption(sock, optval, optlen);
2196  }
2197  else if(optname == IPV6_ADD_MEMBERSHIP ||
2198  optname == IPV6_DROP_MEMBERSHIP ||
2199  optname == MCAST_JOIN_GROUP ||
2200  optname == MCAST_LEAVE_GROUP ||
2201  optname == MCAST_BLOCK_SOURCE ||
2202  optname == MCAST_UNBLOCK_SOURCE ||
2203  optname == MCAST_JOIN_SOURCE_GROUP ||
2204  optname == MCAST_LEAVE_SOURCE_GROUP)
2205  {
2206  //When any of these options are used with getsockopt, the error
2207  //generated is EOPNOTSUPP
2209  ret = SOCKET_ERROR;
2210  }
2211  else if(optname == IPV6_V6ONLY)
2212  {
2213  //Get IPV6_V6ONLY option
2214  ret = socketGetIpv6OnlyOption(sock, optval, optlen);
2215  }
2216  else if(optname == IPV6_DONTFRAG)
2217  {
2218  //Get IPV6_DONTFRAG option
2219  ret = socketGetIpv6DontFragOption(sock, optval, optlen);
2220  }
2221  else if(optname == IPV6_PKTINFO)
2222  {
2223  //Get IPV6_PKTINFO option
2224  ret = socketGetIpv6PktInfoOption(sock, optval, optlen);
2225  }
2226  else if(optname == IPV6_RECVTCLASS)
2227  {
2228  //Get IPV6_RECVTCLASS option
2229  ret = socketGetIpv6RecvTrafficClassOption(sock, optval, optlen);
2230  }
2231  else if(optname == IPV6_RECVHOPLIMIT)
2232  {
2233  //Get IPV6_RECVHOPLIMIT option
2234  ret = socketGetIpv6RecvHopLimitOption(sock, optval, optlen);
2235  }
2236  else
2237  {
2238  //Unknown option
2240  ret = SOCKET_ERROR;
2241  }
2242  }
2243  else if(level == IPPROTO_TCP)
2244  {
2245  //Check option type
2246  if(optname == TCP_NODELAY)
2247  {
2248  //Get TCP_NODELAY option
2249  ret = socketGetTcpNoDelayOption(sock, optval, optlen);
2250  }
2251  else if(optname == TCP_MAXSEG)
2252  {
2253  //Get TCP_MAXSEG option
2254  ret = socketGetTcpMaxSegOption(sock, optval, optlen);
2255  }
2256  else if(optname == TCP_KEEPIDLE)
2257  {
2258  //Get TCP_KEEPIDLE option
2259  ret = socketGetTcpKeepIdleOption(sock, optval, optlen);
2260  }
2261  else if(optname == TCP_KEEPINTVL)
2262  {
2263  //Get TCP_KEEPINTVL option
2264  ret = socketGetTcpKeepIntvlOption(sock, optval, optlen);
2265  }
2266  else if(optname == TCP_KEEPCNT)
2267  {
2268  //Get TCP_KEEPCNT option
2269  ret = socketGetTcpKeepCntOption(sock, optval, optlen);
2270  }
2271  else
2272  {
2273  //Unknown option
2275  ret = SOCKET_ERROR;
2276  }
2277  }
2278  else
2279  {
2280  //The specified level is not valid
2281  socketSetErrnoCode(sock, EINVAL);
2282  ret = SOCKET_ERROR;
2283  }
2284  }
2285  else
2286  {
2287  //The option is not valid
2288  socketSetErrnoCode(sock, EFAULT);
2289  ret = SOCKET_ERROR;
2290  }
2291 
2292  //Release exclusive access
2294 
2295  //return status code
2296  return ret;
2297 }
2298 
2299 
2300 /**
2301  * @brief Set multicast source filter (IPv4 only)
2302  * @param[in] s Descriptor that identifies a socket
2303  * @param[in] interface Local IP address of the interface
2304  * @param[in] group IP multicast address of the group
2305  * @param[in] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2306  * @param[in] numsrc Number of source addresses in the slist array
2307  * @param[in] slist Array of IP addresses of sources to include or exclude
2308  * depending on the filter mode
2309  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2310  * Otherwise, it returns SOCKET_ERROR
2311  **/
2312 
2313 int_t setipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group,
2314  uint32_t fmode, uint_t numsrc, struct in_addr *slist)
2315 {
2316 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2317  error_t error;
2318  uint_t i;
2319  Socket *sock;
2320  IpFilterMode filterMode;
2321  IpAddr groupAddr;
2323 
2324  //Make sure the socket descriptor is valid
2325  if(s < 0 || s >= SOCKET_MAX_COUNT)
2326  {
2327  return SOCKET_ERROR;
2328  }
2329 
2330  //Point to the socket structure
2331  sock = &socketTable[s];
2332 
2333  //Copy group address
2334  groupAddr.length = sizeof(Ipv4Addr);
2335  groupAddr.ipv4Addr = group.s_addr;
2336 
2337  //Check filter mode
2338  if(fmode == MCAST_INCLUDE)
2339  {
2340  //Select source-specific filter mode
2341  filterMode = IP_FILTER_MODE_INCLUDE;
2342  }
2343  else if(fmode == MCAST_INCLUDE)
2344  {
2345  //Select any-source filter mode
2346  filterMode = IP_FILTER_MODE_EXCLUDE;
2347  }
2348  else
2349  {
2350  //Report an error
2351  socketSetErrnoCode(sock, EINVAL);
2352  return SOCKET_ERROR;
2353  }
2354 
2355  //If the implementation imposes a limit on the maximum number of sources
2356  //in a source filter, ENOBUFS is generated when the operation would exceed
2357  //the maximum (refer to RFC3678, section 4.2.1)
2358  if(numsrc > SOCKET_MAX_MULTICAST_SOURCES)
2359  {
2360  socketSetErrnoCode(sock, ENOBUFS);
2361  return SOCKET_ERROR;
2362  }
2363 
2364  //If numsrc is 0, a NULL pointer may be supplied
2365  if(numsrc > 0 && slist == NULL)
2366  {
2367  socketSetErrnoCode(sock, EINVAL);
2368  return SOCKET_ERROR;
2369  }
2370 
2371  //Copy the list of source addresses to include or exclude
2372  for(i = 0; i < numsrc; i++)
2373  {
2374  sources[i].length = sizeof(Ipv4Addr);
2375  sources[i].ipv4Addr = slist[i].s_addr;
2376  }
2377 
2378  //Set multicast source filter
2379  error = socketSetMulticastSourceFilter(sock, &groupAddr, filterMode,
2380  sources, numsrc);
2381 
2382  //Any error to report?
2383  if(error)
2384  {
2385  socketTranslateErrorCode(sock, error);
2386  return SOCKET_ERROR;
2387  }
2388 
2389  //Successful processing
2390  return SOCKET_SUCCESS;
2391 #else
2392  //Not implemented
2393  return SOCKET_ERROR;
2394 #endif
2395 }
2396 
2397 
2398 /**
2399  * @brief Get multicast source filter (IPv4 only)
2400  * @param[in] s Descriptor that identifies a socket
2401  * @param[in] interface Local IP address of the interface
2402  * @param[in] group IP multicast address of the group
2403  * @param[out] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2404  * @param[out] numsrc On input, the numsrc argument holds the number of source
2405  * addresses that will fit in the slist array. On output, the numsrc argument
2406  * will hold the total number of sources in the filter
2407  * @param[out] slist Buffer into which an array of IP addresses of included or
2408  * excluded (depending on the filter mode) sources will be written. If numsrc
2409  * was 0 on input, a NULL pointer may be supplied
2410  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2411  * Otherwise, it returns SOCKET_ERROR
2412  **/
2413 
2414 int_t getipv4sourcefilter(int_t s, struct in_addr interface, struct in_addr group,
2415  uint32_t *fmode, uint_t *numsrc, struct in_addr *slist)
2416 {
2417 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2418  error_t error;
2419  uint_t i;
2420  uint_t numSources;
2421  Socket *sock;
2422  IpFilterMode filterMode;
2423  IpAddr groupAddr;
2425 
2426  //Make sure the socket descriptor is valid
2427  if(s < 0 || s >= SOCKET_MAX_COUNT)
2428  {
2429  return SOCKET_ERROR;
2430  }
2431 
2432  //Point to the socket structure
2433  sock = &socketTable[s];
2434 
2435  //Check parameters
2436  if(fmode == NULL || numsrc == NULL)
2437  {
2438  socketSetErrnoCode(sock, EINVAL);
2439  return SOCKET_ERROR;
2440  }
2441 
2442  //If numsrc was 0 on input, a NULL pointer may be supplied
2443  if(*numsrc > 0 && slist == NULL)
2444  {
2445  socketSetErrnoCode(sock, EINVAL);
2446  return SOCKET_ERROR;
2447  }
2448 
2449  //Copy group address
2450  groupAddr.length = sizeof(Ipv4Addr);
2451  groupAddr.ipv4Addr = group.s_addr;
2452 
2453  //Set multicast source filter
2454  error = socketGetMulticastSourceFilter(sock, &groupAddr, &filterMode,
2455  sources, &numSources);
2456 
2457  //Any error to report?
2458  if(error)
2459  {
2460  socketTranslateErrorCode(sock, error);
2461  return SOCKET_ERROR;
2462  }
2463 
2464  //Filter mode
2465  if(filterMode == IP_FILTER_MODE_INCLUDE)
2466  {
2467  *fmode = MCAST_INCLUDE;
2468  }
2469  else
2470  {
2471  *fmode = MCAST_EXCLUDE;
2472  }
2473 
2474  //The slist parameter will hold as many source addresses as fit, up to the
2475  //minimum of the array size passed in as the original numsrc value and the
2476  //total number of sources in the filter (refer to RFC3678, section 4.2.2)
2477  for(i = 0; i < *numsrc && i < numSources; i++)
2478  {
2479  slist[i].s_addr = sources[i].ipv4Addr;
2480  }
2481 
2482  //On return, numsrc is always updated to be the total number of sources in
2483  //the filter
2484  *numsrc = numSources;
2485 
2486  //Successful processing
2487  return SOCKET_SUCCESS;
2488 #else
2489  //Not implemented
2490  return SOCKET_ERROR;
2491 #endif
2492 }
2493 
2494 
2495 /**
2496  * @brief Set multicast source filter
2497  * @param[in] s Descriptor that identifies a socket
2498  * @param[in] interface Local IP address of the interface
2499  * @param[in] group IP multicast address of the group
2500  * @param[in] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2501  * @param[in] numsrc Number of source addresses in the slist array
2502  * @param[in] slist Array of IP addresses of sources to include or exclude
2503  * depending on the filter mode
2504  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2505  * Otherwise, it returns SOCKET_ERROR
2506  **/
2507 
2508 int_t setsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2509  socklen_t grouplen, uint32_t fmode, uint_t numsrc,
2510  struct sockaddr_storage *slist)
2511 {
2512 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2513  error_t error;
2514  uint_t i;
2515  Socket *sock;
2516  IpFilterMode filterMode;
2517  IpAddr groupAddr;
2519 
2520  //Make sure the socket descriptor is valid
2521  if(s < 0 || s >= SOCKET_MAX_COUNT)
2522  {
2523  return SOCKET_ERROR;
2524  }
2525 
2526  //Point to the socket structure
2527  sock = &socketTable[s];
2528 
2529 #if (IPV4_SUPPORT == ENABLED)
2530  //IPv4 group address?
2531  if(group->sa_family == AF_INET &&
2532  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2533  {
2534  //Point to the IPv4 address information
2535  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2536 
2537  //Copy IPv4 address
2538  groupAddr.length = sizeof(Ipv4Addr);
2539  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2540  }
2541  else
2542 #endif
2543 #if (IPV6_SUPPORT == ENABLED)
2544  //IPv6 group address?
2545  if(group->sa_family == AF_INET6 &&
2546  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2547  {
2548  //Point to the IPv6 address information
2549  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2550 
2551  //Copy IPv6 address
2552  groupAddr.length = sizeof(Ipv6Addr);
2553  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2554  }
2555  else
2556 #endif
2557  //Invalid group address?
2558  {
2559  //Report an error
2560  socketSetErrnoCode(sock, EINVAL);
2561  return SOCKET_ERROR;
2562  }
2563 
2564  //Check filter mode
2565  if(fmode == MCAST_INCLUDE)
2566  {
2567  //Select source-specific filter mode
2568  filterMode = IP_FILTER_MODE_INCLUDE;
2569  }
2570  else if(fmode == MCAST_INCLUDE)
2571  {
2572  //Select any-source filter mode
2573  filterMode = IP_FILTER_MODE_EXCLUDE;
2574  }
2575  else
2576  {
2577  //Report an error
2578  socketSetErrnoCode(sock, EINVAL);
2579  return SOCKET_ERROR;
2580  }
2581 
2582  //If the implementation imposes a limit on the maximum number of sources
2583  //in a source filter, ENOBUFS is generated when the operation would exceed
2584  //the maximum (refer to RFC3678, section 5.2.1)
2585  if(numsrc > SOCKET_MAX_MULTICAST_SOURCES)
2586  {
2587  socketSetErrnoCode(sock, ENOBUFS);
2588  return SOCKET_ERROR;
2589  }
2590 
2591  //If numsrc is 0, a NULL pointer may be supplied
2592  if(numsrc > 0 && slist == NULL)
2593  {
2594  socketSetErrnoCode(sock, EINVAL);
2595  return SOCKET_ERROR;
2596  }
2597 
2598  //Copy the list of source addresses to include or exclude
2599  for(i = 0; i < numsrc; i++)
2600  {
2601 #if (IPV4_SUPPORT == ENABLED)
2602  //IPv4 source address?
2603  if(slist[i].ss_family == AF_INET &&
2604  slist[i].ss_family == group->sa_family)
2605  {
2606  //Point to the IPv4 address information
2607  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2608 
2609  //Copy IPv4 address
2610  sources[i].length = sizeof(Ipv4Addr);
2611  sources[i].ipv4Addr = sa->sin_addr.s_addr;
2612  }
2613  else
2614 #endif
2615 #if (IPV6_SUPPORT == ENABLED)
2616  //IPv6 source address?
2617  if(slist[i].ss_family == AF_INET6 &&
2618  slist[i].ss_family == group->sa_family)
2619  {
2620  //Point to the IPv6 address information
2621  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2622 
2623  //Copy IPv6 address
2624  sources[i].length = sizeof(Ipv6Addr);
2625  ipv6CopyAddr(&sources[i].ipv6Addr, sa->sin6_addr.s6_addr);
2626  }
2627  else
2628 #endif
2629  //Invalid source address?
2630  {
2631  //Report an error
2632  socketSetErrnoCode(sock, EINVAL);
2633  return SOCKET_ERROR;
2634  }
2635  }
2636 
2637  //Set multicast source filter
2638  error = socketSetMulticastSourceFilter(sock, &groupAddr, filterMode,
2639  sources, numsrc);
2640 
2641  //Any error to report?
2642  if(error)
2643  {
2644  socketTranslateErrorCode(sock, error);
2645  return SOCKET_ERROR;
2646  }
2647 
2648  //Successful processing
2649  return SOCKET_SUCCESS;
2650 #else
2651  //Not implemented
2652  return SOCKET_ERROR;
2653 #endif
2654 }
2655 
2656 
2657 /**
2658  * @brief Get multicast source filter
2659  * @param[in] s Descriptor that identifies a socket
2660  * @param[in] interface Local IP address of the interface
2661  * @param[in] group IP multicast address of the group
2662  * @param[out] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2663  * @param[out] numsrc On input, the numsrc argument holds the number of source
2664  * addresses that will fit in the slist array. On output, the numsrc argument
2665  * will hold the total number of sources in the filter
2666  * @param[out] slist Buffer into which an array of IP addresses of included or
2667  * excluded (depending on the filter mode) sources will be written. If numsrc
2668  * was 0 on input, a NULL pointer may be supplied
2669  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2670  * Otherwise, it returns SOCKET_ERROR
2671  **/
2672 
2673 int_t getsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2674  socklen_t grouplen, uint32_t *fmode, uint_t *numsrc,
2675  struct sockaddr_storage *slist)
2676 {
2677 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2678  error_t error;
2679  uint_t i;
2680  uint_t numSources;
2681  Socket *sock;
2682  IpFilterMode filterMode;
2683  IpAddr groupAddr;
2685 
2686  //Make sure the socket descriptor is valid
2687  if(s < 0 || s >= SOCKET_MAX_COUNT)
2688  {
2689  return SOCKET_ERROR;
2690  }
2691 
2692  //Point to the socket structure
2693  sock = &socketTable[s];
2694 
2695  //Check parameters
2696  if(fmode == NULL || numsrc == NULL)
2697  {
2698  socketSetErrnoCode(sock, EINVAL);
2699  return SOCKET_ERROR;
2700  }
2701 
2702  //If numsrc was 0 on input, a NULL pointer may be supplied
2703  if(*numsrc > 0 && slist == NULL)
2704  {
2705  socketSetErrnoCode(sock, EINVAL);
2706  return SOCKET_ERROR;
2707  }
2708 
2709 #if (IPV4_SUPPORT == ENABLED)
2710  //IPv4 group address?
2711  if(group->sa_family == AF_INET &&
2712  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2713  {
2714  //Point to the IPv4 address information
2715  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2716 
2717  //Copy IPv4 address
2718  groupAddr.length = sizeof(Ipv4Addr);
2719  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2720  }
2721  else
2722 #endif
2723 #if (IPV6_SUPPORT == ENABLED)
2724  //IPv6 group address?
2725  if(group->sa_family == AF_INET6 &&
2726  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2727  {
2728  //Point to the IPv6 address information
2729  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2730 
2731  //Copy IPv6 address
2732  groupAddr.length = sizeof(Ipv6Addr);
2733  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2734  }
2735  else
2736 #endif
2737  //Invalid group address?
2738  {
2739  //Report an error
2740  socketSetErrnoCode(sock, EINVAL);
2741  return SOCKET_ERROR;
2742  }
2743 
2744  //Set multicast source filter
2745  error = socketGetMulticastSourceFilter(sock, &groupAddr, &filterMode,
2746  sources, &numSources);
2747 
2748  //Any error to report?
2749  if(error)
2750  {
2751  socketTranslateErrorCode(sock, error);
2752  return SOCKET_ERROR;
2753  }
2754 
2755  //Filter mode
2756  if(filterMode == IP_FILTER_MODE_INCLUDE)
2757  {
2758  *fmode = MCAST_INCLUDE;
2759  }
2760  else
2761  {
2762  *fmode = MCAST_EXCLUDE;
2763  }
2764 
2765  //The slist parameter will hold as many source addresses as fit, up to the
2766  //minimum of the array size passed in as the original numsrc value and the
2767  //total number of sources in the filter (refer to RFC3678, section 5.2.2)
2768  for(i = 0; i < *numsrc && i < numSources; i++)
2769  {
2770 #if (IPV4_SUPPORT == ENABLED)
2771  //IPv4 source address?
2772  if(sock->localIpAddr.length == sizeof(Ipv4Addr))
2773  {
2774  //Point to the IPv4 address information
2775  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2776 
2777  //Set address family and port number
2778  sa->sin_family = AF_INET;
2779  sa->sin_port = 0;
2780 
2781  //Copy IPv4 address
2782  sa->sin_addr.s_addr = sources[i].ipv4Addr;
2783  }
2784  else
2785 #endif
2786 #if (IPV6_SUPPORT == ENABLED)
2787  //IPv6 source address?
2788  if(sock->localIpAddr.length == sizeof(Ipv6Addr))
2789  {
2790  //Point to the IPv4 address information
2791  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2792 
2793  //Set address family and port number
2794  sa->sin6_family = AF_INET6;
2795  sa->sin6_port = 0;
2796  sa->sin6_flowinfo = 0;
2797  sa->sin6_scope_id = 0;
2798 
2799  //Copy IPv6 address
2800  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sources[i].ipv6Addr);
2801  }
2802  else
2803 #endif
2804  //Invalid source address?
2805  {
2806  //Just for sanity
2807  osMemset(&slist[i], 0, sizeof(SOCKADDR_STORAGE));
2808  }
2809  }
2810 
2811  //On return, numsrc is always updated to be the total number of sources in
2812  //the filter
2813  *numsrc = numSources;
2814 
2815  //Successful processing
2816  return SOCKET_SUCCESS;
2817 #else
2818  //Not implemented
2819  return SOCKET_ERROR;
2820 #endif
2821 }
2822 
2823 
2824 /**
2825  * @brief Control the I/O mode of a socket
2826  * @param[in] s Descriptor that identifies a socket
2827  * @param[in] cmd A command to perform on the socket
2828  * @param[in,out] arg A pointer to a parameter
2829  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2830  * Otherwise, it returns SOCKET_ERROR
2831  **/
2832 
2833 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
2834 {
2835  int_t ret;
2836  uint_t *val;
2837  Socket *sock;
2838 
2839  //Make sure the socket descriptor is valid
2840  if(s < 0 || s >= SOCKET_MAX_COUNT)
2841  {
2842  return SOCKET_ERROR;
2843  }
2844 
2845  //Point to the socket structure
2846  sock = &socketTable[s];
2847 
2848  //Get exclusive access
2850 
2851  //Make sure the parameter is valid
2852  if(arg != NULL)
2853  {
2854  //Check command type
2855  switch(cmd)
2856  {
2857  //Enable or disable non-blocking mode
2858  case FIONBIO:
2859  //Cast the parameter to the relevant type
2860  val = (uint_t *) arg;
2861  //Enable blocking or non-blocking operation
2862  sock->timeout = (*val != 0) ? 0 : INFINITE_DELAY;
2863  //Successful processing
2864  ret = SOCKET_SUCCESS;
2865  break;
2866 
2867 #if (TCP_SUPPORT == ENABLED)
2868  //Get the number of bytes that are immediately available for reading
2869  case FIONREAD:
2870  //Cast the parameter to the relevant type
2871  val = (uint_t *) arg;
2872  //Return the actual value
2873  *val = sock->rcvUser;
2874  //Successful processing
2875  ret = SOCKET_SUCCESS;
2876  break;
2877 
2878  //Get the number of bytes written to the send queue but not yet
2879  //acknowledged by the other side of the connection
2880  case FIONWRITE:
2881  //Cast the parameter to the relevant type
2882  val = (uint_t *) arg;
2883  //Return the actual value
2884  *val = sock->sndUser + sock->sndNxt - sock->sndUna;
2885  //Successful processing
2886  ret = SOCKET_SUCCESS;
2887  break;
2888 
2889  //Get the free space in the send queue
2890  case FIONSPACE:
2891  //Cast the parameter to the relevant type
2892  val = (uint_t *) arg;
2893  //Return the actual value
2894  *val = sock->txBufferSize - (sock->sndUser + sock->sndNxt -
2895  sock->sndUna);
2896  //Successful processing
2897  ret = SOCKET_SUCCESS;
2898  break;
2899 #endif
2900 
2901  //Unknown command?
2902  default:
2903  //Report an error
2904  socketSetErrnoCode(sock, EINVAL);
2905  ret = SOCKET_ERROR;
2906  break;
2907  }
2908  }
2909  else
2910  {
2911  //The parameter is not valid
2912  socketSetErrnoCode(sock, EFAULT);
2913  ret = SOCKET_ERROR;
2914  }
2915 
2916  //Release exclusive access
2918 
2919  //return status code
2920  return ret;
2921 }
2922 
2923 
2924 /**
2925  * @brief Perform specific operation
2926  * @param[in] s Descriptor that identifies a socket
2927  * @param[in] cmd A command to perform on the socket
2928  * @param[in] arg Argument value
2929  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2930  * Otherwise, it returns SOCKET_ERROR
2931  **/
2932 
2934 {
2935  int_t ret;
2936  Socket *sock;
2937 
2938  //Make sure the socket descriptor is valid
2939  if(s < 0 || s >= SOCKET_MAX_COUNT)
2940  {
2941  return SOCKET_ERROR;
2942  }
2943 
2944  //Point to the socket structure
2945  sock = &socketTable[s];
2946 
2947  //Get exclusive access
2949 
2950  //Check command type
2951  switch(cmd)
2952  {
2953  //Get the file status flags?
2954  case F_GETFL:
2955  //Return (as the function result) the file descriptor flags
2956  ret = (sock->timeout == 0) ? O_NONBLOCK : 0;
2957  break;
2958 
2959  //Set the file status flags?
2960  case F_SETFL:
2961  //Enable blocking or non-blocking operation
2962  sock->timeout = ((arg & O_NONBLOCK) != 0) ? 0 : INFINITE_DELAY;
2963  //Successful processing
2964  ret = SOCKET_SUCCESS;
2965  break;
2966 
2967  //Unknown command?
2968  default:
2969  //Report an error
2970  socketSetErrnoCode(sock, EINVAL);
2971  ret = SOCKET_ERROR;
2972  break;
2973  }
2974 
2975  //Release exclusive access
2977 
2978  //return status code
2979  return ret;
2980 }
2981 
2982 
2983 /**
2984  * @brief The shutdown function disables sends or receives on a socket
2985  * @param[in] s Descriptor that identifies a socket
2986  * @param[in] how A flag that describes what types of operation will no longer be allowed
2987  * @return If no error occurs, shutdown returns SOCKET_SUCCESS
2988  * Otherwise, it returns SOCKET_ERROR
2989  **/
2990 
2992 {
2993  error_t error;
2994  Socket *sock;
2995 
2996  //Make sure the socket descriptor is valid
2997  if(s < 0 || s >= SOCKET_MAX_COUNT)
2998  {
2999  return SOCKET_ERROR;
3000  }
3001 
3002  //Point to the socket structure
3003  sock = &socketTable[s];
3004 
3005  //Shut down socket
3006  error = socketShutdown(sock, how);
3007 
3008  //Any error to report?
3009  if(error)
3010  {
3011  socketTranslateErrorCode(sock, error);
3012  return SOCKET_ERROR;
3013  }
3014 
3015  //Successful processing
3016  return SOCKET_SUCCESS;
3017 }
3018 
3019 
3020 /**
3021  * @brief The closesocket function closes an existing socket
3022  * @param[in] s Descriptor that identifies a socket
3023  * @return If no error occurs, closesocket returns SOCKET_SUCCESS
3024  * Otherwise, it returns SOCKET_ERROR
3025  **/
3026 
3028 {
3029  Socket *sock;
3030 
3031  //Make sure the socket descriptor is valid
3032  if(s < 0 || s >= SOCKET_MAX_COUNT)
3033  {
3034  return SOCKET_ERROR;
3035  }
3036 
3037  //Point to the socket structure
3038  sock = &socketTable[s];
3039 
3040  //Close socket
3041  socketClose(sock);
3042 
3043  //Successful processing
3044  return SOCKET_SUCCESS;
3045 }
3046 
3047 
3048 /**
3049  * @brief Determine the status of one or more sockets
3050  *
3051  * The select function determines the status of one or more sockets,
3052  * waiting if necessary, to perform synchronous I/O
3053  *
3054  * @param[in] nfds Unused parameter included only for compatibility with
3055  * Berkeley socket
3056  * @param[in,out] readfds An optional pointer to a set of sockets to be
3057  * checked for readability
3058  * @param[in,out] writefds An optional pointer to a set of sockets to be
3059  * checked for writability
3060  * @param[in,out] exceptfds An optional pointer to a set of sockets to be
3061  * checked for errors
3062  * @param[in] timeout The maximum time for select to wait. Set the timeout
3063  * parameter to null for blocking operations
3064  * @return The select function returns the total number of socket handles that
3065  * are ready and contained in the fd_set structures, zero if the time limit
3066  * expired, or SOCKET_ERROR if an error occurred
3067  **/
3068 
3069 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
3070  const struct timeval *timeout)
3071 {
3072  int_t i;
3073  int_t j;
3074  int_t n;
3075  int_t s;
3076  systime_t time;
3077  uint_t eventMask;
3078  uint_t eventFlags;
3079  OsEvent event;
3080  fd_set *fds;
3081 
3082  //Parse all the descriptor sets
3083  for(i = 0; i < 3; i++)
3084  {
3085  //Select the suitable descriptor set
3086  switch(i)
3087  {
3088  case 0:
3089  //Set of sockets to be checked for readability
3090  fds = readfds;
3091  break;
3092 
3093  case 1:
3094  //Set of sockets to be checked for writability
3095  fds = writefds;
3096  break;
3097 
3098  default:
3099  //Set of sockets to be checked for errors
3100  fds = exceptfds;
3101  break;
3102  }
3103 
3104  //Each descriptor is optional and may be omitted
3105  if(fds != NULL)
3106  {
3107  //Parse the current set of sockets
3108  for(j = 0; j < fds->fd_count; j++)
3109  {
3110  //Invalid socket descriptor?
3111  if(fds->fd_array[j] < 0 || fds->fd_array[j] >= SOCKET_MAX_COUNT)
3112  {
3113  //Report an error
3114  return SOCKET_ERROR;
3115  }
3116  }
3117  }
3118  }
3119 
3120  //Create an event object to get notified of socket events
3121  if(!osCreateEvent(&event))
3122  {
3123  //Failed to create event
3124  return SOCKET_ERROR;
3125  }
3126 
3127  //Parse all the descriptor sets
3128  for(i = 0; i < 3; i++)
3129  {
3130  //Select the suitable descriptor set
3131  switch(i)
3132  {
3133  case 0:
3134  //Set of sockets to be checked for readability
3135  fds = readfds;
3136  eventMask = SOCKET_EVENT_RX_READY;
3137  break;
3138 
3139  case 1:
3140  //Set of sockets to be checked for writability
3141  fds = writefds;
3142  eventMask = SOCKET_EVENT_TX_READY;
3143  break;
3144 
3145  default:
3146  //Set of sockets to be checked for errors
3147  fds = exceptfds;
3148  eventMask = SOCKET_EVENT_CLOSED;
3149  break;
3150  }
3151 
3152  //Each descriptor is optional and may be omitted
3153  if(fds != NULL)
3154  {
3155  //Parse the current set of sockets
3156  for(j = 0; j < fds->fd_count; j++)
3157  {
3158  //Get the descriptor associated with the current entry
3159  s = fds->fd_array[j];
3160  //Subscribe to the requested events
3161  socketRegisterEvents(&socketTable[s], &event, eventMask);
3162  }
3163  }
3164  }
3165 
3166  //Retrieve timeout value
3167  if(timeout != NULL)
3168  {
3169  time = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
3170  }
3171  else
3172  {
3173  time = INFINITE_DELAY;
3174  }
3175 
3176  //Block the current task until an event occurs
3177  osWaitForEvent(&event, time);
3178 
3179  //Count the number of events in the signaled state
3180  n = 0;
3181 
3182  //Parse all the descriptor sets
3183  for(i = 0; i < 3; i++)
3184  {
3185  //Select the suitable descriptor set
3186  switch(i)
3187  {
3188  case 0:
3189  //Set of sockets to be checked for readability
3190  fds = readfds;
3191  eventMask = SOCKET_EVENT_RX_READY;
3192  break;
3193 
3194  case 1:
3195  //Set of sockets to be checked for writability
3196  fds = writefds;
3197  eventMask = SOCKET_EVENT_TX_READY;
3198  break;
3199 
3200  default:
3201  //Set of sockets to be checked for errors
3202  fds = exceptfds;
3203  eventMask = SOCKET_EVENT_CLOSED;
3204  break;
3205  }
3206 
3207  //Each descriptor is optional and may be omitted
3208  if(fds != NULL)
3209  {
3210  //Parse the current set of sockets
3211  for(j = 0; j < fds->fd_count; )
3212  {
3213  //Get the descriptor associated with the current entry
3214  s = fds->fd_array[j];
3215  //Retrieve event flags for the current socket
3216  eventFlags = socketGetEvents(&socketTable[s]);
3217  //Unsubscribe previously registered events
3219 
3220  //Event flag is set?
3221  if(eventFlags & eventMask)
3222  {
3223  //Track the number of events in the signaled state
3224  n++;
3225  //Jump to the next socket descriptor
3226  j++;
3227  }
3228  else
3229  {
3230  //Remove descriptor from the current set
3231  socketFdClr(fds, s);
3232  }
3233  }
3234  }
3235  }
3236 
3237  //Delete event object
3238  osDeleteEvent(&event);
3239  //Return the number of events in the signaled state
3240  return n;
3241 }
3242 
3243 
3244 /**
3245  * @brief Get system host name
3246  * @param[out] name Output buffer where to store the system host name
3247  * @param[in] len Length of the buffer, in bytes
3248  * @return On success, zero is returned. On error, -1 is returned
3249  **/
3250 
3252 {
3253  size_t n;
3254  NetInterface *interface;
3255 
3256  //Check parameter
3257  if(name == NULL)
3258  {
3259  //Report an error
3261  return -1;
3262  }
3263 
3264  //Select the default network interface
3265  interface = netGetDefaultInterface();
3266 
3267  //Retrieve the length of the host name
3268  n = osStrlen(interface->hostname);
3269 
3270  //Make sure the buffer is large enough to hold the string
3271  if(len <= n)
3272  {
3273  //Report an error
3275  return -1;
3276  }
3277 
3278  //Copy the host name
3279  osStrcpy(name, interface->hostname);
3280 
3281  //Successful processing
3282  return 0;
3283 }
3284 
3285 
3286 /**
3287  * @brief Host name resolution
3288  * @param[in] name Name of the host to resolve
3289  * @return Pointer to the hostent structure or a NULL if an error occurs
3290  **/
3291 
3293 {
3294  int_t herrno;
3295  static HOSTENT result;
3296 
3297  //The hostent structure returned by the function resides in static
3298  //memory area
3299  return gethostbyname_r(name, &result, NULL, 0, &herrno);
3300 }
3301 
3302 
3303 /**
3304  * @brief Host name resolution (reentrant version)
3305  * @param[in] name Name of the host to resolve
3306  * @param[in] result Pointer to a hostent structure where the function can
3307  * store the host entry
3308  * @param[out] buf Pointer to a temporary work buffer
3309  * @param[in] buflen Length of the temporary work buffer
3310  * @param[out] h_errnop Pointer to a location where the function can store an
3311  * h_errno value if an error occurs
3312  * @return Pointer to the hostent structure or a NULL if an error occurs
3313  **/
3314 
3315 struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
3316  char_t *buf, size_t buflen, int_t *h_errnop)
3317 {
3318  error_t error;
3319  IpAddr ipAddr;
3320 
3321  //Check input parameters
3322  if(name == NULL || result == NULL)
3323  {
3324  //Report an error
3325  *h_errnop = NO_RECOVERY;
3326  return NULL;
3327  }
3328 
3329  //Resolve host address
3330  error = getHostByName(NULL, name, &ipAddr, 0);
3331  //Address resolution failed?
3332  if(error)
3333  {
3334  //Report an error
3335  *h_errnop = HOST_NOT_FOUND;
3336  return NULL;
3337  }
3338 
3339 #if (IPV4_SUPPORT == ENABLED)
3340  //IPv4 address?
3341  if(ipAddr.length == sizeof(Ipv4Addr))
3342  {
3343  //Set address family
3344  result->h_addrtype = AF_INET;
3345  result->h_length = sizeof(Ipv4Addr);
3346 
3347  //Copy IPv4 address
3348  ipv4CopyAddr(result->h_addr, &ipAddr.ipv4Addr);
3349  }
3350  else
3351 #endif
3352 #if (IPV6_SUPPORT == ENABLED)
3353  //IPv6 address?
3354  if(ipAddr.length == sizeof(Ipv6Addr))
3355  {
3356  //Set address family
3357  result->h_addrtype = AF_INET6;
3358  result->h_length = sizeof(Ipv6Addr);
3359 
3360  //Copy IPv6 address
3361  ipv6CopyAddr(result->h_addr, &ipAddr.ipv6Addr);
3362  }
3363  else
3364 #endif
3365  //Invalid address?
3366  {
3367  //Report an error
3368  *h_errnop = NO_ADDRESS;
3369  return NULL;
3370  }
3371 
3372  //Successful host name resolution
3373  *h_errnop = NETDB_SUCCESS;
3374 
3375  //Return a pointer to the hostent structure
3376  return result;
3377 }
3378 
3379 
3380 /**
3381  * @brief Convert host and service names to socket address
3382  * @param[in] node Host name or numerical network address
3383  * @param[in] service Service name or decimal number
3384  * @param[in] hints Criteria for selecting the socket address structures
3385  * @param[out] res Dynamically allocated list of socket address structures
3386  * @return On success, zero is returned. On error, a non-zero value is returned
3387  **/
3388 
3389 int_t getaddrinfo(const char_t *node, const char_t *service,
3390  const struct addrinfo *hints, struct addrinfo **res)
3391 {
3392  error_t error;
3393  size_t n;
3394  char_t *p;
3395  uint_t flags;
3396  uint32_t port;
3397  IpAddr ipAddr;
3398  ADDRINFO h;
3399  ADDRINFO *ai;
3400 
3401  //Check whether both node and service name are NULL
3402  if(node == NULL && service == NULL)
3403  return EAI_NONAME;
3404 
3405  //The hints argument is optional
3406  if(hints != NULL)
3407  {
3408  //If hints is not NULL, it points to an addrinfo structure that specifies
3409  //criteria that limit the set of socket addresses that will be returned
3410  h = *hints;
3411  }
3412  else
3413  {
3414  //If hints is NULL, then default criteria are used
3415  osMemset(&h, 0, sizeof(ADDRINFO));
3416  h.ai_family = AF_UNSPEC;
3417  h.ai_socktype = 0;
3418  h.ai_protocol = 0;
3419  h.ai_flags = 0;
3420  }
3421 
3422  //The user may provide a hint to choose between IPv4 and IPv6
3423  if(h.ai_family == AF_UNSPEC)
3424  {
3425  //The value AF_UNSPEC indicates that function should return socket
3426  //addresses for any address family (either IPv4 or IPv6)
3427  flags = 0;
3428  }
3429 #if (IPV4_SUPPORT == ENABLED)
3430  else if(h.ai_family == AF_INET)
3431  {
3432  //The value AF_INET indicates that the function should return an IPv4
3433  //address
3435  }
3436 #endif
3437 #if (IPV6_SUPPORT == ENABLED)
3438  else if(h.ai_family == AF_INET6)
3439  {
3440  //The value AF_INET6 indicates that the function should return an IPv6
3441  //address
3443  }
3444 #endif
3445  else
3446  {
3447  //The requested address family is not supported
3448  return EAI_FAMILY;
3449  }
3450 
3451  //Check whether a host name or numerical network address is specified
3452  if(node != NULL)
3453  {
3454  //If the AI_NUMERICHOST flag, then node must be a numerical network
3455  //address
3456  if((h.ai_flags & AI_NUMERICHOST) != 0)
3457  {
3458  //Convert the string representation to a binary IP address
3459  error = ipStringToAddr(node, &ipAddr);
3460  }
3461  else
3462  {
3463  //Resolve host address
3464  error = getHostByName(NULL, node, &ipAddr, flags);
3465  }
3466 
3467  //Check status code
3468  if(error == NO_ERROR)
3469  {
3470  //Successful host name resolution
3471  }
3472  else if(error == ERROR_IN_PROGRESS)
3473  {
3474  //Host name resolution is in progress
3475  return EAI_AGAIN;
3476  }
3477  else
3478  {
3479  //Permanent failure indication
3480  return EAI_FAIL;
3481  }
3482  }
3483  else
3484  {
3485  //Check flags
3486  if((h.ai_flags & AI_PASSIVE) != 0)
3487  {
3488 #if (IPV4_SUPPORT == ENABLED)
3489  //IPv4 address family?
3490  if(h.ai_family == AF_INET || h.ai_family == AF_UNSPEC)
3491  {
3492  ipAddr.length = sizeof(Ipv4Addr);
3493  ipAddr.ipv4Addr = IPV4_UNSPECIFIED_ADDR;
3494  }
3495  else
3496 #endif
3497 #if (IPV6_SUPPORT == ENABLED)
3498  //IPv6 address family?
3499  if(h.ai_family == AF_INET6 || h.ai_family == AF_UNSPEC)
3500  {
3501  ipAddr.length = sizeof(Ipv6Addr);
3502  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
3503  }
3504  else
3505 #endif
3506  //Unknown address family?
3507  {
3508  //Report an error
3509  return EAI_ADDRFAMILY;
3510  }
3511  }
3512  else
3513  {
3514  //Invalid flags
3515  return EAI_BADFLAGS;
3516  }
3517  }
3518 
3519  //Only service names containing a numeric port number are supported
3520  port = osStrtoul(service, &p, 10);
3521  //Invalid service name?
3522  if(*p != '\0')
3523  {
3524  //The requested service is not available
3525  return EAI_SERVICE;
3526  }
3527 
3528 #if (IPV4_SUPPORT == ENABLED)
3529  //IPv4 address?
3530  if(ipAddr.length == sizeof(Ipv4Addr))
3531  {
3532  //Select the relevant socket family
3533  h.ai_family = AF_INET;
3534  //Get the length of the corresponding socket address
3535  n = sizeof(SOCKADDR_IN);
3536  }
3537  else
3538 #endif
3539 #if (IPV6_SUPPORT == ENABLED)
3540  //IPv6 address?
3541  if(ipAddr.length == sizeof(Ipv6Addr))
3542  {
3543  //Select the relevant socket family
3544  h.ai_family = AF_INET6;
3545  //Get the length of the corresponding socket address
3546  n = sizeof(SOCKADDR_IN6);
3547  }
3548  else
3549 #endif
3550  //Unknown address?
3551  {
3552  //Report an error
3553  return EAI_ADDRFAMILY;
3554  }
3555 
3556  //Allocate a memory buffer to hold the address information structure
3557  ai = osAllocMem(sizeof(ADDRINFO) + n);
3558  //Failed to allocate memory?
3559  if(ai == NULL)
3560  {
3561  //Out of memory
3562  return EAI_MEMORY;
3563  }
3564 
3565  //Initialize address information structure
3566  osMemset(ai, 0, sizeof(ADDRINFO) + n);
3567  ai->ai_family = h.ai_family;
3568  ai->ai_socktype = h.ai_socktype;
3569  ai->ai_protocol = h.ai_protocol;
3570  ai->ai_addr = (SOCKADDR *) ((uint8_t *) ai + sizeof(ADDRINFO));
3571  ai->ai_addrlen = n;
3572  ai->ai_next = NULL;
3573 
3574 #if (IPV4_SUPPORT == ENABLED)
3575  //IPv4 address?
3576  if(ipAddr.length == sizeof(Ipv4Addr))
3577  {
3578  //Point to the IPv4 address information
3579  SOCKADDR_IN *sa = (SOCKADDR_IN *) ai->ai_addr;
3580 
3581  //Set address family and port number
3582  sa->sin_family = AF_INET;
3583  sa->sin_port = htons(port);
3584 
3585  //Copy IPv4 address
3586  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
3587  }
3588  else
3589 #endif
3590 #if (IPV6_SUPPORT == ENABLED)
3591  //IPv6 address?
3592  if(ipAddr.length == sizeof(Ipv6Addr))
3593  {
3594  //Point to the IPv6 address information
3595  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) ai->ai_addr;
3596 
3597  //Set address family and port number
3598  sa->sin6_family = AF_INET6;
3599  sa->sin6_port = htons(port);
3600  sa->sin6_flowinfo = 0;
3601  sa->sin6_scope_id = 0;
3602 
3603  //Copy IPv6 address
3604  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
3605  }
3606  else
3607 #endif
3608  //Unknown address?
3609  {
3610  //Clean up side effects
3611  osFreeMem(ai);
3612  //Report an error
3613  return EAI_ADDRFAMILY;
3614  }
3615 
3616  //Return a pointer to the allocated address information
3617  *res = ai;
3618 
3619  //Successful processing
3620  return 0;
3621 }
3622 
3623 
3624 /**
3625  * @brief Free socket address structures
3626  * @param[in] res Dynamically allocated list of socket address structures
3627  **/
3628 
3630 {
3631  ADDRINFO *next;
3632 
3633  //Free the list of socket address structures
3634  while(res != NULL)
3635  {
3636  //Get next entry
3637  next = res->ai_next;
3638  //Free current socket address structure
3639  osFreeMem(res);
3640  //Point to the next entry
3641  res = next;
3642  }
3643 }
3644 
3645 
3646 /**
3647  * @brief Convert a socket address to a corresponding host and service
3648  * @param[in] addr Generic socket address structure
3649  * @param[in] addrlen Length in bytes of the address
3650  * @param[out] host Output buffer where to store the host name
3651  * @param[in] hostlen Length of the host name buffer, in bytes
3652  * @param[out] serv Output buffer where to store the service name
3653  * @param[in] servlen Length of the service name buffer, in bytes
3654  * @param[in] flags Set of flags that influences the behavior of this function
3655  * @return On success, zero is returned. On error, a non-zero value is returned
3656  **/
3657 
3658 int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
3659  char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
3660 {
3661  uint16_t port;
3662 
3663  //At least one of hostname or service name must be requested
3664  if(host == NULL && serv == NULL)
3665  return EAI_NONAME;
3666 
3667 #if (IPV4_SUPPORT == ENABLED)
3668  //IPv4 address?
3669  if(addr->sa_family == AF_INET &&
3670  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
3671  {
3672  SOCKADDR_IN *sa;
3673  Ipv4Addr ipv4Addr;
3674 
3675  //Point to the IPv4 address information
3676  sa = (SOCKADDR_IN *) addr;
3677 
3678  //The caller can specify that no host name is required
3679  if(host != NULL)
3680  {
3681  //Make sure the buffer is large enough to hold the host name
3682  if(hostlen < 16)
3683  return EAI_OVERFLOW;
3684 
3685  //Copy the binary representation of the IPv4 address
3686  ipv4CopyAddr(&ipv4Addr, &sa->sin_addr.s_addr);
3687  //Convert the IPv4 address to dot-decimal notation
3688  ipv4AddrToString(ipv4Addr, host);
3689  }
3690 
3691  //Retrieve port number
3692  port = ntohs(sa->sin_port);
3693  }
3694  else
3695 #endif
3696 #if (IPV6_SUPPORT == ENABLED)
3697  //IPv6 address?
3698  if(addr->sa_family == AF_INET6 &&
3699  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
3700  {
3701  SOCKADDR_IN6 *sa;
3702  Ipv6Addr ipv6Addr;
3703 
3704  //Point to the IPv6 address information
3705  sa = (SOCKADDR_IN6 *) addr;
3706 
3707  //The caller can specify that no host name is required
3708  if(host != NULL)
3709  {
3710  //Make sure the buffer is large enough to hold the host name
3711  if(hostlen < 40)
3712  return EAI_OVERFLOW;
3713 
3714  //Copy the binary representation of the IPv6 address
3715  ipv6CopyAddr(&ipv6Addr, sa->sin6_addr.s6_addr);
3716  //Convert the IPv6 address to string representation
3717  ipv6AddrToString(&ipv6Addr, host);
3718  }
3719 
3720  //Retrieve port number
3721  port = ntohs(sa->sin6_port);
3722  }
3723  else
3724 #endif
3725  //Invalid address?
3726  {
3727  //The address family was not recognized, or the address length was
3728  //invalid for the specified family
3729  return EAI_FAMILY;
3730  }
3731 
3732  //The caller can specify that no service name is required
3733  if(serv != NULL)
3734  {
3735  //Make sure the buffer is large enough to hold the service name
3736  if(servlen < 6)
3737  return EAI_OVERFLOW;
3738 
3739  //Convert the port number to string representation
3740  osSprintf(serv, "%" PRIu16, ntohs(port));
3741  }
3742 
3743  //Successful processing
3744  return 0;
3745 }
3746 
3747 
3748 /**
3749  * @brief Map an interface name into its corresponding index
3750  * @param[in] ifname Name of the interface
3751  * @return If ifname is the name of an interface, then the function returns the
3752  * interface index corresponding to name ifname. Otherwise, the function
3753  * returns zero
3754  **/
3755 
3757 {
3758  uint_t i;
3759  uint_t index;
3760 
3761  //Initialize interface index
3762  index = 0;
3763 
3764  //Valid parameter?
3765  if(ifname != NULL)
3766  {
3767  //Loop through network interfaces
3768  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3769  {
3770  //Matching interface name?
3771  if(osStrcmp(netInterface[i].name, ifname) == 0)
3772  {
3773  //Save the interface index
3774  index = netInterface[i].index + 1;
3775  //We are done
3776  break;
3777  }
3778  }
3779  }
3780 
3781  //Return the index corresponding to interface name
3782  return index;
3783 }
3784 
3785 
3786 /**
3787  * @brief Map an interface index into its corresponding name
3788  * @param[in] ifindex Interface index
3789  * @param[in] ifname Buffer of at least IF_NAMESIZE bytes
3790  * @return If ifindex is an interface index, then the function returns the value
3791  * supplied in ifname, which points to a buffer now containing the interface
3792  * name. Otherwise, the function returns a NULL pointer
3793  **/
3794 
3796 {
3797  uint_t i;
3798  char_t *name;
3799 
3800  //Initialize interface name
3801  name = NULL;
3802 
3803  //Valid parameters?
3804  if(ifindex != 0 && ifname != NULL)
3805  {
3806  //Loop through network interfaces
3807  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3808  {
3809  //Matching interface index?
3810  if((netInterface[i].index + 1) == ifindex)
3811  {
3812  //Copy the name of the interface
3813  osStrcpy(ifname, netInterface[i].name);
3814  name = ifname;
3815 
3816  //We are done
3817  break;
3818  }
3819  }
3820  }
3821 
3822  //Return the name of the interface
3823  return name;
3824 }
3825 
3826 
3827 /**
3828  * @brief Convert a dot-decimal string into binary data in network byte order
3829  * @param[in] cp NULL-terminated string representing the IPv4 address
3830  * @return Binary data in network byte order
3831  **/
3832 
3834 {
3835 #if (IPV4_SUPPORT == ENABLED)
3836  error_t error;
3837  Ipv4Addr ipv4Addr;
3838 
3839  //Convert a dot-decimal string to a binary IPv4 address
3840  error = ipv4StringToAddr(cp, &ipv4Addr);
3841 
3842  //Check status code
3843  if(error)
3844  {
3845  //The input is invalid
3846  return INADDR_NONE;
3847  }
3848  else
3849  {
3850  //Return the binary representation
3851  return ipv4Addr;
3852  }
3853 #else
3854  //IPv4 is not implemented
3855  return INADDR_NONE;
3856 #endif
3857 }
3858 
3859 
3860 /**
3861  * @brief Convert a dot-decimal string into binary form
3862  * @param[in] cp NULL-terminated string representing the IPv4 address
3863  * @param[out] inp Binary data in network byte order
3864  * @return The function returns non-zero if the address is valid, zero if not
3865  **/
3866 
3867 int_t inet_aton(const char_t *cp, struct in_addr *inp)
3868 {
3869 #if (IPV4_SUPPORT == ENABLED)
3870  error_t error;
3871  Ipv4Addr ipv4Addr;
3872 
3873  //Convert a dot-decimal string to a binary IPv4 address
3874  error = ipv4StringToAddr(cp, &ipv4Addr);
3875 
3876  //Check status code
3877  if(error)
3878  {
3879  //The input is invalid
3880  return 0;
3881  }
3882  else
3883  {
3884  //Copy the binary representation of the IPv4 address
3885  inp->s_addr = ipv4Addr;
3886  //The conversion succeeded
3887  return 1;
3888  }
3889 #else
3890  //IPv4 is not implemented
3891  return 0;
3892 #endif
3893 }
3894 
3895 
3896 /**
3897  * @brief Convert a binary IPv4 address to dot-decimal notation
3898  * @param[in] in Binary representation of the IPv4 address
3899  * @return Pointer to the formatted string
3900  **/
3901 
3902 const char_t *inet_ntoa(struct in_addr in)
3903 {
3904  static char_t buf[16];
3905 
3906  //The string returned by the function resides in static memory area
3907  return inet_ntoa_r(in, buf, sizeof(buf));
3908 }
3909 
3910 
3911 /**
3912  * @brief Convert a binary IPv4 address to dot-decimal notation (reentrant version)
3913  * @param[in] in Binary representation of the IPv4 address
3914  * @param[out] buf Pointer to the buffer where to format the string
3915  * @param[in] buflen Number of bytes available in the buffer
3916  * @return Pointer to the formatted string
3917  **/
3918 
3919 const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
3920 {
3921  //Properly terminate the string
3922  buf[0] = '\0';
3923 
3924 #if (IPV4_SUPPORT == ENABLED)
3925  //Check the length of the buffer
3926  if(buflen >= 16)
3927  {
3928  //Convert the binary IPv4 address to dot-decimal notation
3929  ipv4AddrToString(in.s_addr, buf);
3930  }
3931 #endif
3932 
3933  //Return a pointer to the formatted string
3934  return buf;
3935 }
3936 
3937 
3938 /**
3939  * @brief Convert an IPv4 or IPv6 address from text to binary form
3940  * @param[in] af Address family
3941  * @param[in] src NULL-terminated string representing the IP address
3942  * @param[out] dst Binary representation of the IP address
3943  * @return The function returns 1 on success. 0 is returned if the address
3944  * is not valid. If the address family is not valid, -1 is returned
3945  **/
3946 
3947 int_t inet_pton(int_t af, const char_t *src, void *dst)
3948 {
3949  error_t error;
3950 
3951 #if (IPV4_SUPPORT == ENABLED)
3952  //IPv4 address?
3953  if(af == AF_INET)
3954  {
3955  Ipv4Addr ipv4Addr;
3956 
3957  //Convert the IPv4 address from text to binary form
3958  error = ipv4StringToAddr(src, &ipv4Addr);
3959 
3960  //Check status code
3961  if(error)
3962  {
3963  //The input is invalid
3964  return 0;
3965  }
3966  else
3967  {
3968  //Copy the binary representation of the IPv4 address
3969  ipv4CopyAddr(dst, &ipv4Addr);
3970  //The conversion succeeded
3971  return 1;
3972  }
3973  }
3974  else
3975 #endif
3976 #if (IPV6_SUPPORT == ENABLED)
3977  //IPv6 address?
3978  if(af == AF_INET6)
3979  {
3980  Ipv6Addr ipv6Addr;
3981 
3982  //Convert the IPv6 address from text to binary form
3983  error = ipv6StringToAddr(src, &ipv6Addr);
3984 
3985  //Check status code
3986  if(error)
3987  {
3988  //The input is invalid
3989  return 0;
3990  }
3991  else
3992  {
3993  //Copy the binary representation of the IPv6 address
3994  ipv6CopyAddr(dst, &ipv6Addr);
3995  //The conversion succeeded
3996  return 1;
3997  }
3998  }
3999  else
4000 #endif
4001  //Invalid address family?
4002  {
4003  //Report an error
4004  return -1;
4005  }
4006 }
4007 
4008 
4009 /**
4010  * @brief Convert an IPv4 or IPv6 address from binary to text
4011  * @param[in] af Address family
4012  * @param[in] src Binary representation of the IP address
4013  * @param[out] dst NULL-terminated string representing the IP address
4014  * @param[in] size Number of bytes available in the buffer
4015  * @return On success, the function returns a pointer to the formatted string.
4016  * NULL is returned if there was an error
4017  **/
4018 
4019 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
4020 {
4021 #if (IPV4_SUPPORT == ENABLED)
4022  //IPv4 address?
4023  if(af == AF_INET && size >= INET_ADDRSTRLEN)
4024  {
4025  Ipv4Addr ipv4Addr;
4026 
4027  //Copy the binary representation of the IPv4 address
4028  ipv4CopyAddr(&ipv4Addr, src);
4029 
4030  //Convert the IPv4 address to dot-decimal notation
4031  return ipv4AddrToString(ipv4Addr, dst);
4032  }
4033  else
4034 #endif
4035 #if (IPV6_SUPPORT == ENABLED)
4036  //IPv6 address?
4037  if(af == AF_INET6 && size >= INET6_ADDRSTRLEN)
4038  {
4039  Ipv6Addr ipv6Addr;
4040 
4041  //Copy the binary representation of the IPv6 address
4042  ipv6CopyAddr(&ipv6Addr, src);
4043 
4044  //Convert the IPv6 address to string representation
4045  return ipv6AddrToString(&ipv6Addr, dst);
4046  }
4047  else
4048 #endif
4049  //Invalid address family?
4050  {
4051  //Report an error
4052  return NULL;
4053  }
4054 }
4055 
4056 #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:1486
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:2329
struct hostent * gethostbyname(const char_t *name)
Host name resolution.
Definition: bsd_socket.c:3292
#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:3947
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1316
#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:2673
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:1672
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:326
#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:49
#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:3902
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:2991
#define ETIMEDOUT
Definition: bsd_socket.h:261
uint8_t p
Definition: ndp.h:300
#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:222
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:2062
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:3251
@ 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:114
#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:4019
#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:171
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:165
#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:210
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:3919
#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:297
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:3756
@ 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:3629
#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:761
#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:213
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1634
#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:3795
#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:231
#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:1692
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:3315
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:2933
#define SO_BROADCAST
Definition: bsd_socket.h:141
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:467
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:1714
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:1894
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:2274
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:1349
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:2020
#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:368
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:3833
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:1451
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:2833
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:267
#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:255
uint8_t flags
Definition: tcp.h:351
#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:48
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:195
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:3069
#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:2313
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:3658
#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:2414
#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:1507
#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:2024
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:1572
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:1368
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:2508
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:2174
#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:123
#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
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:3389
unsigned int uint_t
Definition: compiler_port.h:50
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:250
#define osMemset(p, value, length)
Definition: os_port.h:135
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:1457
#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:3867
#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:207
#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:273
#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:3027
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:1413