bsd_socket.c
Go to the documentation of this file.
1 /**
2  * @file bsd_socket.c
3  * @brief BSD socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.0
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] grouplen Length in bytes of the group
2501  * @param[in] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2502  * @param[in] numsrc Number of source addresses in the slist array
2503  * @param[in] slist Array of IP addresses of sources to include or exclude
2504  * depending on the filter mode
2505  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2506  * Otherwise, it returns SOCKET_ERROR
2507  **/
2508 
2509 int_t setsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2510  socklen_t grouplen, uint32_t fmode, uint_t numsrc,
2511  struct sockaddr_storage *slist)
2512 {
2513 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2514  error_t error;
2515  uint_t i;
2516  Socket *sock;
2517  IpFilterMode filterMode;
2518  IpAddr groupAddr;
2520 
2521  //Make sure the socket descriptor is valid
2522  if(s < 0 || s >= SOCKET_MAX_COUNT)
2523  {
2524  return SOCKET_ERROR;
2525  }
2526 
2527  //Point to the socket structure
2528  sock = &socketTable[s];
2529 
2530 #if (IPV4_SUPPORT == ENABLED)
2531  //IPv4 group address?
2532  if(group->sa_family == AF_INET &&
2533  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2534  {
2535  //Point to the IPv4 address information
2536  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2537 
2538  //Copy IPv4 address
2539  groupAddr.length = sizeof(Ipv4Addr);
2540  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2541  }
2542  else
2543 #endif
2544 #if (IPV6_SUPPORT == ENABLED)
2545  //IPv6 group address?
2546  if(group->sa_family == AF_INET6 &&
2547  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2548  {
2549  //Point to the IPv6 address information
2550  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2551 
2552  //Copy IPv6 address
2553  groupAddr.length = sizeof(Ipv6Addr);
2554  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2555  }
2556  else
2557 #endif
2558  //Invalid group address?
2559  {
2560  //Report an error
2561  socketSetErrnoCode(sock, EINVAL);
2562  return SOCKET_ERROR;
2563  }
2564 
2565  //Check filter mode
2566  if(fmode == MCAST_INCLUDE)
2567  {
2568  //Select source-specific filter mode
2569  filterMode = IP_FILTER_MODE_INCLUDE;
2570  }
2571  else if(fmode == MCAST_INCLUDE)
2572  {
2573  //Select any-source filter mode
2574  filterMode = IP_FILTER_MODE_EXCLUDE;
2575  }
2576  else
2577  {
2578  //Report an error
2579  socketSetErrnoCode(sock, EINVAL);
2580  return SOCKET_ERROR;
2581  }
2582 
2583  //If the implementation imposes a limit on the maximum number of sources
2584  //in a source filter, ENOBUFS is generated when the operation would exceed
2585  //the maximum (refer to RFC3678, section 5.2.1)
2586  if(numsrc > SOCKET_MAX_MULTICAST_SOURCES)
2587  {
2588  socketSetErrnoCode(sock, ENOBUFS);
2589  return SOCKET_ERROR;
2590  }
2591 
2592  //If numsrc is 0, a NULL pointer may be supplied
2593  if(numsrc > 0 && slist == NULL)
2594  {
2595  socketSetErrnoCode(sock, EINVAL);
2596  return SOCKET_ERROR;
2597  }
2598 
2599  //Copy the list of source addresses to include or exclude
2600  for(i = 0; i < numsrc; i++)
2601  {
2602 #if (IPV4_SUPPORT == ENABLED)
2603  //IPv4 source address?
2604  if(slist[i].ss_family == AF_INET &&
2605  slist[i].ss_family == group->sa_family)
2606  {
2607  //Point to the IPv4 address information
2608  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2609 
2610  //Copy IPv4 address
2611  sources[i].length = sizeof(Ipv4Addr);
2612  sources[i].ipv4Addr = sa->sin_addr.s_addr;
2613  }
2614  else
2615 #endif
2616 #if (IPV6_SUPPORT == ENABLED)
2617  //IPv6 source address?
2618  if(slist[i].ss_family == AF_INET6 &&
2619  slist[i].ss_family == group->sa_family)
2620  {
2621  //Point to the IPv6 address information
2622  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2623 
2624  //Copy IPv6 address
2625  sources[i].length = sizeof(Ipv6Addr);
2626  ipv6CopyAddr(&sources[i].ipv6Addr, sa->sin6_addr.s6_addr);
2627  }
2628  else
2629 #endif
2630  //Invalid source address?
2631  {
2632  //Report an error
2633  socketSetErrnoCode(sock, EINVAL);
2634  return SOCKET_ERROR;
2635  }
2636  }
2637 
2638  //Set multicast source filter
2639  error = socketSetMulticastSourceFilter(sock, &groupAddr, filterMode,
2640  sources, numsrc);
2641 
2642  //Any error to report?
2643  if(error)
2644  {
2645  socketTranslateErrorCode(sock, error);
2646  return SOCKET_ERROR;
2647  }
2648 
2649  //Successful processing
2650  return SOCKET_SUCCESS;
2651 #else
2652  //Not implemented
2653  return SOCKET_ERROR;
2654 #endif
2655 }
2656 
2657 
2658 /**
2659  * @brief Get multicast source filter
2660  * @param[in] s Descriptor that identifies a socket
2661  * @param[in] interface Local IP address of the interface
2662  * @param[in] group IP multicast address of the group
2663  * @param[in] grouplen Length in bytes of the group
2664  * @param[out] fmode Multicast filter mode (MCAST_INCLUDE or MCAST_EXCLUDE)
2665  * @param[out] numsrc On input, the numsrc argument holds the number of source
2666  * addresses that will fit in the slist array. On output, the numsrc argument
2667  * will hold the total number of sources in the filter
2668  * @param[out] slist Buffer into which an array of IP addresses of included or
2669  * excluded (depending on the filter mode) sources will be written. If numsrc
2670  * was 0 on input, a NULL pointer may be supplied
2671  * @return If no error occurs, setipv4sourcefilter returns SOCKET_SUCCESS
2672  * Otherwise, it returns SOCKET_ERROR
2673  **/
2674 
2675 int_t getsourcefilter(int_t s, uint32_t interface, struct sockaddr *group,
2676  socklen_t grouplen, uint32_t *fmode, uint_t *numsrc,
2677  struct sockaddr_storage *slist)
2678 {
2679 #if (SOCKET_MAX_MULTICAST_SOURCES > 0)
2680  error_t error;
2681  uint_t i;
2682  uint_t numSources;
2683  Socket *sock;
2684  IpFilterMode filterMode;
2685  IpAddr groupAddr;
2687 
2688  //Make sure the socket descriptor is valid
2689  if(s < 0 || s >= SOCKET_MAX_COUNT)
2690  {
2691  return SOCKET_ERROR;
2692  }
2693 
2694  //Point to the socket structure
2695  sock = &socketTable[s];
2696 
2697  //Check parameters
2698  if(fmode == NULL || numsrc == NULL)
2699  {
2700  socketSetErrnoCode(sock, EINVAL);
2701  return SOCKET_ERROR;
2702  }
2703 
2704  //If numsrc was 0 on input, a NULL pointer may be supplied
2705  if(*numsrc > 0 && slist == NULL)
2706  {
2707  socketSetErrnoCode(sock, EINVAL);
2708  return SOCKET_ERROR;
2709  }
2710 
2711 #if (IPV4_SUPPORT == ENABLED)
2712  //IPv4 group address?
2713  if(group->sa_family == AF_INET &&
2714  grouplen >= (socklen_t) sizeof(SOCKADDR_IN))
2715  {
2716  //Point to the IPv4 address information
2717  SOCKADDR_IN *sa = (SOCKADDR_IN *) group;
2718 
2719  //Copy IPv4 address
2720  groupAddr.length = sizeof(Ipv4Addr);
2721  groupAddr.ipv4Addr = sa->sin_addr.s_addr;
2722  }
2723  else
2724 #endif
2725 #if (IPV6_SUPPORT == ENABLED)
2726  //IPv6 group address?
2727  if(group->sa_family == AF_INET6 &&
2728  grouplen >= (socklen_t) sizeof(SOCKADDR_IN6))
2729  {
2730  //Point to the IPv6 address information
2731  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) group;
2732 
2733  //Copy IPv6 address
2734  groupAddr.length = sizeof(Ipv6Addr);
2735  ipv6CopyAddr(&groupAddr.ipv6Addr, sa->sin6_addr.s6_addr);
2736  }
2737  else
2738 #endif
2739  //Invalid group address?
2740  {
2741  //Report an error
2742  socketSetErrnoCode(sock, EINVAL);
2743  return SOCKET_ERROR;
2744  }
2745 
2746  //Set multicast source filter
2747  error = socketGetMulticastSourceFilter(sock, &groupAddr, &filterMode,
2748  sources, &numSources);
2749 
2750  //Any error to report?
2751  if(error)
2752  {
2753  socketTranslateErrorCode(sock, error);
2754  return SOCKET_ERROR;
2755  }
2756 
2757  //Filter mode
2758  if(filterMode == IP_FILTER_MODE_INCLUDE)
2759  {
2760  *fmode = MCAST_INCLUDE;
2761  }
2762  else
2763  {
2764  *fmode = MCAST_EXCLUDE;
2765  }
2766 
2767  //The slist parameter will hold as many source addresses as fit, up to the
2768  //minimum of the array size passed in as the original numsrc value and the
2769  //total number of sources in the filter (refer to RFC3678, section 5.2.2)
2770  for(i = 0; i < *numsrc && i < numSources; i++)
2771  {
2772 #if (IPV4_SUPPORT == ENABLED)
2773  //IPv4 source address?
2774  if(sock->localIpAddr.length == sizeof(Ipv4Addr))
2775  {
2776  //Point to the IPv4 address information
2777  SOCKADDR_IN *sa = (SOCKADDR_IN *) &slist[i];
2778 
2779  //Set address family and port number
2780  sa->sin_family = AF_INET;
2781  sa->sin_port = 0;
2782 
2783  //Copy IPv4 address
2784  sa->sin_addr.s_addr = sources[i].ipv4Addr;
2785  }
2786  else
2787 #endif
2788 #if (IPV6_SUPPORT == ENABLED)
2789  //IPv6 source address?
2790  if(sock->localIpAddr.length == sizeof(Ipv6Addr))
2791  {
2792  //Point to the IPv4 address information
2793  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) &slist[i];
2794 
2795  //Set address family and port number
2796  sa->sin6_family = AF_INET6;
2797  sa->sin6_port = 0;
2798  sa->sin6_flowinfo = 0;
2799  sa->sin6_scope_id = 0;
2800 
2801  //Copy IPv6 address
2802  ipv6CopyAddr(sa->sin6_addr.s6_addr, &sources[i].ipv6Addr);
2803  }
2804  else
2805 #endif
2806  //Invalid source address?
2807  {
2808  //Just for sanity
2809  osMemset(&slist[i], 0, sizeof(SOCKADDR_STORAGE));
2810  }
2811  }
2812 
2813  //On return, numsrc is always updated to be the total number of sources in
2814  //the filter
2815  *numsrc = numSources;
2816 
2817  //Successful processing
2818  return SOCKET_SUCCESS;
2819 #else
2820  //Not implemented
2821  return SOCKET_ERROR;
2822 #endif
2823 }
2824 
2825 
2826 /**
2827  * @brief Control the I/O mode of a socket
2828  * @param[in] s Descriptor that identifies a socket
2829  * @param[in] cmd A command to perform on the socket
2830  * @param[in,out] arg A pointer to a parameter
2831  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2832  * Otherwise, it returns SOCKET_ERROR
2833  **/
2834 
2835 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
2836 {
2837  int_t ret;
2838  uint_t *val;
2839  Socket *sock;
2840 
2841  //Make sure the socket descriptor is valid
2842  if(s < 0 || s >= SOCKET_MAX_COUNT)
2843  {
2844  return SOCKET_ERROR;
2845  }
2846 
2847  //Point to the socket structure
2848  sock = &socketTable[s];
2849 
2850  //Get exclusive access
2852 
2853  //Make sure the parameter is valid
2854  if(arg != NULL)
2855  {
2856  //Check command type
2857  switch(cmd)
2858  {
2859  //Enable or disable non-blocking mode
2860  case FIONBIO:
2861  //Cast the parameter to the relevant type
2862  val = (uint_t *) arg;
2863  //Enable blocking or non-blocking operation
2864  sock->timeout = (*val != 0) ? 0 : INFINITE_DELAY;
2865  //Successful processing
2866  ret = SOCKET_SUCCESS;
2867  break;
2868 
2869 #if (TCP_SUPPORT == ENABLED)
2870  //Get the number of bytes that are immediately available for reading
2871  case FIONREAD:
2872  //Cast the parameter to the relevant type
2873  val = (uint_t *) arg;
2874  //Return the actual value
2875  *val = sock->rcvUser;
2876  //Successful processing
2877  ret = SOCKET_SUCCESS;
2878  break;
2879 
2880  //Get the number of bytes written to the send queue but not yet
2881  //acknowledged by the other side of the connection
2882  case FIONWRITE:
2883  //Cast the parameter to the relevant type
2884  val = (uint_t *) arg;
2885  //Return the actual value
2886  *val = sock->sndUser + sock->sndNxt - sock->sndUna;
2887  //Successful processing
2888  ret = SOCKET_SUCCESS;
2889  break;
2890 
2891  //Get the free space in the send queue
2892  case FIONSPACE:
2893  //Cast the parameter to the relevant type
2894  val = (uint_t *) arg;
2895  //Return the actual value
2896  *val = sock->txBufferSize - (sock->sndUser + sock->sndNxt -
2897  sock->sndUna);
2898  //Successful processing
2899  ret = SOCKET_SUCCESS;
2900  break;
2901 #endif
2902 
2903  //Unknown command?
2904  default:
2905  //Report an error
2906  socketSetErrnoCode(sock, EINVAL);
2907  ret = SOCKET_ERROR;
2908  break;
2909  }
2910  }
2911  else
2912  {
2913  //The parameter is not valid
2914  socketSetErrnoCode(sock, EFAULT);
2915  ret = SOCKET_ERROR;
2916  }
2917 
2918  //Release exclusive access
2920 
2921  //return status code
2922  return ret;
2923 }
2924 
2925 
2926 /**
2927  * @brief Perform specific operation
2928  * @param[in] s Descriptor that identifies a socket
2929  * @param[in] cmd A command to perform on the socket
2930  * @param[in] arg Argument value
2931  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2932  * Otherwise, it returns SOCKET_ERROR
2933  **/
2934 
2936 {
2937  int_t ret;
2938  Socket *sock;
2939 
2940  //Make sure the socket descriptor is valid
2941  if(s < 0 || s >= SOCKET_MAX_COUNT)
2942  {
2943  return SOCKET_ERROR;
2944  }
2945 
2946  //Point to the socket structure
2947  sock = &socketTable[s];
2948 
2949  //Get exclusive access
2951 
2952  //Check command type
2953  switch(cmd)
2954  {
2955  //Get the file status flags?
2956  case F_GETFL:
2957  //Return (as the function result) the file descriptor flags
2958  ret = (sock->timeout == 0) ? O_NONBLOCK : 0;
2959  break;
2960 
2961  //Set the file status flags?
2962  case F_SETFL:
2963  //Enable blocking or non-blocking operation
2964  sock->timeout = ((arg & O_NONBLOCK) != 0) ? 0 : INFINITE_DELAY;
2965  //Successful processing
2966  ret = SOCKET_SUCCESS;
2967  break;
2968 
2969  //Unknown command?
2970  default:
2971  //Report an error
2972  socketSetErrnoCode(sock, EINVAL);
2973  ret = SOCKET_ERROR;
2974  break;
2975  }
2976 
2977  //Release exclusive access
2979 
2980  //return status code
2981  return ret;
2982 }
2983 
2984 
2985 /**
2986  * @brief The shutdown function disables sends or receives on a socket
2987  * @param[in] s Descriptor that identifies a socket
2988  * @param[in] how A flag that describes what types of operation will no longer be allowed
2989  * @return If no error occurs, shutdown returns SOCKET_SUCCESS
2990  * Otherwise, it returns SOCKET_ERROR
2991  **/
2992 
2994 {
2995  error_t error;
2996  Socket *sock;
2997 
2998  //Make sure the socket descriptor is valid
2999  if(s < 0 || s >= SOCKET_MAX_COUNT)
3000  {
3001  return SOCKET_ERROR;
3002  }
3003 
3004  //Point to the socket structure
3005  sock = &socketTable[s];
3006 
3007  //Shut down socket
3008  error = socketShutdown(sock, how);
3009 
3010  //Any error to report?
3011  if(error)
3012  {
3013  socketTranslateErrorCode(sock, error);
3014  return SOCKET_ERROR;
3015  }
3016 
3017  //Successful processing
3018  return SOCKET_SUCCESS;
3019 }
3020 
3021 
3022 /**
3023  * @brief The closesocket function closes an existing socket
3024  * @param[in] s Descriptor that identifies a socket
3025  * @return If no error occurs, closesocket returns SOCKET_SUCCESS
3026  * Otherwise, it returns SOCKET_ERROR
3027  **/
3028 
3030 {
3031  Socket *sock;
3032 
3033  //Make sure the socket descriptor is valid
3034  if(s < 0 || s >= SOCKET_MAX_COUNT)
3035  {
3036  return SOCKET_ERROR;
3037  }
3038 
3039  //Point to the socket structure
3040  sock = &socketTable[s];
3041 
3042  //Close socket
3043  socketClose(sock);
3044 
3045  //Successful processing
3046  return SOCKET_SUCCESS;
3047 }
3048 
3049 
3050 /**
3051  * @brief Determine the status of one or more sockets
3052  *
3053  * The select function determines the status of one or more sockets,
3054  * waiting if necessary, to perform synchronous I/O
3055  *
3056  * @param[in] nfds Unused parameter included only for compatibility with
3057  * Berkeley socket
3058  * @param[in,out] readfds An optional pointer to a set of sockets to be
3059  * checked for readability
3060  * @param[in,out] writefds An optional pointer to a set of sockets to be
3061  * checked for writability
3062  * @param[in,out] exceptfds An optional pointer to a set of sockets to be
3063  * checked for errors
3064  * @param[in] timeout The maximum time for select to wait. Set the timeout
3065  * parameter to null for blocking operations
3066  * @return The select function returns the total number of socket handles that
3067  * are ready and contained in the fd_set structures, zero if the time limit
3068  * expired, or SOCKET_ERROR if an error occurred
3069  **/
3070 
3071 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
3072  const struct timeval *timeout)
3073 {
3074  int_t i;
3075  int_t j;
3076  int_t n;
3077  int_t s;
3078  systime_t time;
3079  uint_t eventMask;
3080  uint_t eventFlags;
3081  OsEvent event;
3082  fd_set *fds;
3083 
3084  //Parse all the descriptor sets
3085  for(i = 0; i < 3; i++)
3086  {
3087  //Select the suitable descriptor set
3088  switch(i)
3089  {
3090  case 0:
3091  //Set of sockets to be checked for readability
3092  fds = readfds;
3093  break;
3094 
3095  case 1:
3096  //Set of sockets to be checked for writability
3097  fds = writefds;
3098  break;
3099 
3100  default:
3101  //Set of sockets to be checked for errors
3102  fds = exceptfds;
3103  break;
3104  }
3105 
3106  //Each descriptor is optional and may be omitted
3107  if(fds != NULL)
3108  {
3109  //Parse the current set of sockets
3110  for(j = 0; j < fds->fd_count; j++)
3111  {
3112  //Invalid socket descriptor?
3113  if(fds->fd_array[j] < 0 || fds->fd_array[j] >= SOCKET_MAX_COUNT)
3114  {
3115  //Report an error
3116  return SOCKET_ERROR;
3117  }
3118  }
3119  }
3120  }
3121 
3122  //Create an event object to get notified of socket events
3123  if(!osCreateEvent(&event))
3124  {
3125  //Failed to create event
3126  return SOCKET_ERROR;
3127  }
3128 
3129  //Parse all the descriptor sets
3130  for(i = 0; i < 3; i++)
3131  {
3132  //Select the suitable descriptor set
3133  switch(i)
3134  {
3135  case 0:
3136  //Set of sockets to be checked for readability
3137  fds = readfds;
3138  eventMask = SOCKET_EVENT_RX_READY;
3139  break;
3140 
3141  case 1:
3142  //Set of sockets to be checked for writability
3143  fds = writefds;
3144  eventMask = SOCKET_EVENT_TX_READY;
3145  break;
3146 
3147  default:
3148  //Set of sockets to be checked for errors
3149  fds = exceptfds;
3150  eventMask = SOCKET_EVENT_CLOSED;
3151  break;
3152  }
3153 
3154  //Each descriptor is optional and may be omitted
3155  if(fds != NULL)
3156  {
3157  //Parse the current set of sockets
3158  for(j = 0; j < fds->fd_count; j++)
3159  {
3160  //Get the descriptor associated with the current entry
3161  s = fds->fd_array[j];
3162  //Subscribe to the requested events
3163  socketRegisterEvents(&socketTable[s], &event, eventMask);
3164  }
3165  }
3166  }
3167 
3168  //Retrieve timeout value
3169  if(timeout != NULL)
3170  {
3171  time = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
3172  }
3173  else
3174  {
3175  time = INFINITE_DELAY;
3176  }
3177 
3178  //Block the current task until an event occurs
3179  osWaitForEvent(&event, time);
3180 
3181  //Count the number of events in the signaled state
3182  n = 0;
3183 
3184  //Parse all the descriptor sets
3185  for(i = 0; i < 3; i++)
3186  {
3187  //Select the suitable descriptor set
3188  switch(i)
3189  {
3190  case 0:
3191  //Set of sockets to be checked for readability
3192  fds = readfds;
3193  eventMask = SOCKET_EVENT_RX_READY;
3194  break;
3195 
3196  case 1:
3197  //Set of sockets to be checked for writability
3198  fds = writefds;
3199  eventMask = SOCKET_EVENT_TX_READY;
3200  break;
3201 
3202  default:
3203  //Set of sockets to be checked for errors
3204  fds = exceptfds;
3205  eventMask = SOCKET_EVENT_CLOSED;
3206  break;
3207  }
3208 
3209  //Each descriptor is optional and may be omitted
3210  if(fds != NULL)
3211  {
3212  //Parse the current set of sockets
3213  for(j = 0; j < fds->fd_count; )
3214  {
3215  //Get the descriptor associated with the current entry
3216  s = fds->fd_array[j];
3217  //Retrieve event flags for the current socket
3218  eventFlags = socketGetEvents(&socketTable[s]);
3219  //Unsubscribe previously registered events
3221 
3222  //Event flag is set?
3223  if(eventFlags & eventMask)
3224  {
3225  //Track the number of events in the signaled state
3226  n++;
3227  //Jump to the next socket descriptor
3228  j++;
3229  }
3230  else
3231  {
3232  //Remove descriptor from the current set
3233  socketFdClr(fds, s);
3234  }
3235  }
3236  }
3237  }
3238 
3239  //Delete event object
3240  osDeleteEvent(&event);
3241  //Return the number of events in the signaled state
3242  return n;
3243 }
3244 
3245 
3246 /**
3247  * @brief Get system host name
3248  * @param[out] name Output buffer where to store the system host name
3249  * @param[in] len Length of the buffer, in bytes
3250  * @return On success, zero is returned. On error, -1 is returned
3251  **/
3252 
3254 {
3255  size_t n;
3256  NetInterface *interface;
3257 
3258  //Check parameter
3259  if(name == NULL)
3260  {
3261  //Report an error
3263  return -1;
3264  }
3265 
3266  //Select the default network interface
3267  interface = netGetDefaultInterface();
3268 
3269  //Retrieve the length of the host name
3270  n = osStrlen(interface->hostname);
3271 
3272  //Make sure the buffer is large enough to hold the string
3273  if(len <= n)
3274  {
3275  //Report an error
3277  return -1;
3278  }
3279 
3280  //Copy the host name
3281  osStrcpy(name, interface->hostname);
3282 
3283  //Successful processing
3284  return 0;
3285 }
3286 
3287 
3288 /**
3289  * @brief Host name resolution
3290  * @param[in] name Name of the host to resolve
3291  * @return Pointer to the hostent structure or a NULL if an error occurs
3292  **/
3293 
3295 {
3296  int_t herrno;
3297  static HOSTENT result;
3298 
3299  //The hostent structure returned by the function resides in static
3300  //memory area
3301  return gethostbyname_r(name, &result, NULL, 0, &herrno);
3302 }
3303 
3304 
3305 /**
3306  * @brief Host name resolution (reentrant version)
3307  * @param[in] name Name of the host to resolve
3308  * @param[in] result Pointer to a hostent structure where the function can
3309  * store the host entry
3310  * @param[out] buf Pointer to a temporary work buffer
3311  * @param[in] buflen Length of the temporary work buffer
3312  * @param[out] h_errnop Pointer to a location where the function can store an
3313  * h_errno value if an error occurs
3314  * @return Pointer to the hostent structure or a NULL if an error occurs
3315  **/
3316 
3317 struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
3318  char_t *buf, size_t buflen, int_t *h_errnop)
3319 {
3320  error_t error;
3321  IpAddr ipAddr;
3322 
3323  //Check input parameters
3324  if(name == NULL || result == NULL)
3325  {
3326  //Report an error
3327  *h_errnop = NO_RECOVERY;
3328  return NULL;
3329  }
3330 
3331  //Resolve host address
3332  error = getHostByName(NULL, name, &ipAddr, 0);
3333  //Address resolution failed?
3334  if(error)
3335  {
3336  //Report an error
3337  *h_errnop = HOST_NOT_FOUND;
3338  return NULL;
3339  }
3340 
3341 #if (IPV4_SUPPORT == ENABLED)
3342  //IPv4 address?
3343  if(ipAddr.length == sizeof(Ipv4Addr))
3344  {
3345  //Set address family
3346  result->h_addrtype = AF_INET;
3347  result->h_length = sizeof(Ipv4Addr);
3348 
3349  //Copy IPv4 address
3350  ipv4CopyAddr(result->h_addr, &ipAddr.ipv4Addr);
3351  }
3352  else
3353 #endif
3354 #if (IPV6_SUPPORT == ENABLED)
3355  //IPv6 address?
3356  if(ipAddr.length == sizeof(Ipv6Addr))
3357  {
3358  //Set address family
3359  result->h_addrtype = AF_INET6;
3360  result->h_length = sizeof(Ipv6Addr);
3361 
3362  //Copy IPv6 address
3363  ipv6CopyAddr(result->h_addr, &ipAddr.ipv6Addr);
3364  }
3365  else
3366 #endif
3367  //Invalid address?
3368  {
3369  //Report an error
3370  *h_errnop = NO_ADDRESS;
3371  return NULL;
3372  }
3373 
3374  //Successful host name resolution
3375  *h_errnop = NETDB_SUCCESS;
3376 
3377  //Return a pointer to the hostent structure
3378  return result;
3379 }
3380 
3381 
3382 /**
3383  * @brief Convert host and service names to socket address
3384  * @param[in] node Host name or numerical network address
3385  * @param[in] service Service name or decimal number
3386  * @param[in] hints Criteria for selecting the socket address structures
3387  * @param[out] res Dynamically allocated list of socket address structures
3388  * @return On success, zero is returned. On error, a non-zero value is returned
3389  **/
3390 
3391 int_t getaddrinfo(const char_t *node, const char_t *service,
3392  const struct addrinfo *hints, struct addrinfo **res)
3393 {
3394  error_t error;
3395  size_t n;
3396  char_t *p;
3397  uint_t flags;
3398  uint32_t port;
3399  IpAddr ipAddr;
3400  ADDRINFO h;
3401  ADDRINFO *ai;
3402 
3403  //Check whether both node and service name are NULL
3404  if(node == NULL && service == NULL)
3405  return EAI_NONAME;
3406 
3407  //The hints argument is optional
3408  if(hints != NULL)
3409  {
3410  //If hints is not NULL, it points to an addrinfo structure that specifies
3411  //criteria that limit the set of socket addresses that will be returned
3412  h = *hints;
3413  }
3414  else
3415  {
3416  //If hints is NULL, then default criteria are used
3417  osMemset(&h, 0, sizeof(ADDRINFO));
3418  h.ai_family = AF_UNSPEC;
3419  h.ai_socktype = 0;
3420  h.ai_protocol = 0;
3421  h.ai_flags = 0;
3422  }
3423 
3424  //The user may provide a hint to choose between IPv4 and IPv6
3425  if(h.ai_family == AF_UNSPEC)
3426  {
3427  //The value AF_UNSPEC indicates that function should return socket
3428  //addresses for any address family (either IPv4 or IPv6)
3429  flags = 0;
3430  }
3431 #if (IPV4_SUPPORT == ENABLED)
3432  else if(h.ai_family == AF_INET)
3433  {
3434  //The value AF_INET indicates that the function should return an IPv4
3435  //address
3437  }
3438 #endif
3439 #if (IPV6_SUPPORT == ENABLED)
3440  else if(h.ai_family == AF_INET6)
3441  {
3442  //The value AF_INET6 indicates that the function should return an IPv6
3443  //address
3445  }
3446 #endif
3447  else
3448  {
3449  //The requested address family is not supported
3450  return EAI_FAMILY;
3451  }
3452 
3453  //Check whether a host name or numerical network address is specified
3454  if(node != NULL)
3455  {
3456  //If the AI_NUMERICHOST flag, then node must be a numerical network
3457  //address
3458  if((h.ai_flags & AI_NUMERICHOST) != 0)
3459  {
3460  //Convert the string representation to a binary IP address
3461  error = ipStringToAddr(node, &ipAddr);
3462  }
3463  else
3464  {
3465  //Resolve host address
3466  error = getHostByName(NULL, node, &ipAddr, flags);
3467  }
3468 
3469  //Check status code
3470  if(error == NO_ERROR)
3471  {
3472  //Successful host name resolution
3473  }
3474  else if(error == ERROR_IN_PROGRESS)
3475  {
3476  //Host name resolution is in progress
3477  return EAI_AGAIN;
3478  }
3479  else
3480  {
3481  //Permanent failure indication
3482  return EAI_FAIL;
3483  }
3484  }
3485  else
3486  {
3487  //Check flags
3488  if((h.ai_flags & AI_PASSIVE) != 0)
3489  {
3490 #if (IPV4_SUPPORT == ENABLED)
3491  //IPv4 address family?
3492  if(h.ai_family == AF_INET || h.ai_family == AF_UNSPEC)
3493  {
3494  ipAddr.length = sizeof(Ipv4Addr);
3495  ipAddr.ipv4Addr = IPV4_UNSPECIFIED_ADDR;
3496  }
3497  else
3498 #endif
3499 #if (IPV6_SUPPORT == ENABLED)
3500  //IPv6 address family?
3501  if(h.ai_family == AF_INET6 || h.ai_family == AF_UNSPEC)
3502  {
3503  ipAddr.length = sizeof(Ipv6Addr);
3504  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
3505  }
3506  else
3507 #endif
3508  //Unknown address family?
3509  {
3510  //Report an error
3511  return EAI_ADDRFAMILY;
3512  }
3513  }
3514  else
3515  {
3516  //Invalid flags
3517  return EAI_BADFLAGS;
3518  }
3519  }
3520 
3521  //Only service names containing a numeric port number are supported
3522  port = osStrtoul(service, &p, 10);
3523  //Invalid service name?
3524  if(*p != '\0')
3525  {
3526  //The requested service is not available
3527  return EAI_SERVICE;
3528  }
3529 
3530 #if (IPV4_SUPPORT == ENABLED)
3531  //IPv4 address?
3532  if(ipAddr.length == sizeof(Ipv4Addr))
3533  {
3534  //Select the relevant socket family
3535  h.ai_family = AF_INET;
3536  //Get the length of the corresponding socket address
3537  n = sizeof(SOCKADDR_IN);
3538  }
3539  else
3540 #endif
3541 #if (IPV6_SUPPORT == ENABLED)
3542  //IPv6 address?
3543  if(ipAddr.length == sizeof(Ipv6Addr))
3544  {
3545  //Select the relevant socket family
3546  h.ai_family = AF_INET6;
3547  //Get the length of the corresponding socket address
3548  n = sizeof(SOCKADDR_IN6);
3549  }
3550  else
3551 #endif
3552  //Unknown address?
3553  {
3554  //Report an error
3555  return EAI_ADDRFAMILY;
3556  }
3557 
3558  //Allocate a memory buffer to hold the address information structure
3559  ai = osAllocMem(sizeof(ADDRINFO) + n);
3560  //Failed to allocate memory?
3561  if(ai == NULL)
3562  {
3563  //Out of memory
3564  return EAI_MEMORY;
3565  }
3566 
3567  //Initialize address information structure
3568  osMemset(ai, 0, sizeof(ADDRINFO) + n);
3569  ai->ai_family = h.ai_family;
3570  ai->ai_socktype = h.ai_socktype;
3571  ai->ai_protocol = h.ai_protocol;
3572  ai->ai_addr = (SOCKADDR *) ((uint8_t *) ai + sizeof(ADDRINFO));
3573  ai->ai_addrlen = n;
3574  ai->ai_next = NULL;
3575 
3576 #if (IPV4_SUPPORT == ENABLED)
3577  //IPv4 address?
3578  if(ipAddr.length == sizeof(Ipv4Addr))
3579  {
3580  //Point to the IPv4 address information
3581  SOCKADDR_IN *sa = (SOCKADDR_IN *) ai->ai_addr;
3582 
3583  //Set address family and port number
3584  sa->sin_family = AF_INET;
3585  sa->sin_port = htons(port);
3586 
3587  //Copy IPv4 address
3588  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
3589  }
3590  else
3591 #endif
3592 #if (IPV6_SUPPORT == ENABLED)
3593  //IPv6 address?
3594  if(ipAddr.length == sizeof(Ipv6Addr))
3595  {
3596  //Point to the IPv6 address information
3597  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) ai->ai_addr;
3598 
3599  //Set address family and port number
3600  sa->sin6_family = AF_INET6;
3601  sa->sin6_port = htons(port);
3602  sa->sin6_flowinfo = 0;
3603  sa->sin6_scope_id = 0;
3604 
3605  //Copy IPv6 address
3606  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
3607  }
3608  else
3609 #endif
3610  //Unknown address?
3611  {
3612  //Clean up side effects
3613  osFreeMem(ai);
3614  //Report an error
3615  return EAI_ADDRFAMILY;
3616  }
3617 
3618  //Return a pointer to the allocated address information
3619  *res = ai;
3620 
3621  //Successful processing
3622  return 0;
3623 }
3624 
3625 
3626 /**
3627  * @brief Free socket address structures
3628  * @param[in] res Dynamically allocated list of socket address structures
3629  **/
3630 
3632 {
3633  ADDRINFO *next;
3634 
3635  //Free the list of socket address structures
3636  while(res != NULL)
3637  {
3638  //Get next entry
3639  next = res->ai_next;
3640  //Free current socket address structure
3641  osFreeMem(res);
3642  //Point to the next entry
3643  res = next;
3644  }
3645 }
3646 
3647 
3648 /**
3649  * @brief Convert a socket address to a corresponding host and service
3650  * @param[in] addr Generic socket address structure
3651  * @param[in] addrlen Length in bytes of the address
3652  * @param[out] host Output buffer where to store the host name
3653  * @param[in] hostlen Length of the host name buffer, in bytes
3654  * @param[out] serv Output buffer where to store the service name
3655  * @param[in] servlen Length of the service name buffer, in bytes
3656  * @param[in] flags Set of flags that influences the behavior of this function
3657  * @return On success, zero is returned. On error, a non-zero value is returned
3658  **/
3659 
3660 int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
3661  char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
3662 {
3663  uint16_t port;
3664 
3665  //At least one of hostname or service name must be requested
3666  if(host == NULL && serv == NULL)
3667  return EAI_NONAME;
3668 
3669 #if (IPV4_SUPPORT == ENABLED)
3670  //IPv4 address?
3671  if(addr->sa_family == AF_INET &&
3672  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
3673  {
3674  SOCKADDR_IN *sa;
3675  Ipv4Addr ipv4Addr;
3676 
3677  //Point to the IPv4 address information
3678  sa = (SOCKADDR_IN *) addr;
3679 
3680  //The caller can specify that no host name is required
3681  if(host != NULL)
3682  {
3683  //Make sure the buffer is large enough to hold the host name
3684  if(hostlen < 16)
3685  return EAI_OVERFLOW;
3686 
3687  //Copy the binary representation of the IPv4 address
3688  ipv4CopyAddr(&ipv4Addr, &sa->sin_addr.s_addr);
3689  //Convert the IPv4 address to dot-decimal notation
3690  ipv4AddrToString(ipv4Addr, host);
3691  }
3692 
3693  //Retrieve port number
3694  port = ntohs(sa->sin_port);
3695  }
3696  else
3697 #endif
3698 #if (IPV6_SUPPORT == ENABLED)
3699  //IPv6 address?
3700  if(addr->sa_family == AF_INET6 &&
3701  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
3702  {
3703  SOCKADDR_IN6 *sa;
3704  Ipv6Addr ipv6Addr;
3705 
3706  //Point to the IPv6 address information
3707  sa = (SOCKADDR_IN6 *) addr;
3708 
3709  //The caller can specify that no host name is required
3710  if(host != NULL)
3711  {
3712  //Make sure the buffer is large enough to hold the host name
3713  if(hostlen < 40)
3714  return EAI_OVERFLOW;
3715 
3716  //Copy the binary representation of the IPv6 address
3717  ipv6CopyAddr(&ipv6Addr, sa->sin6_addr.s6_addr);
3718  //Convert the IPv6 address to string representation
3719  ipv6AddrToString(&ipv6Addr, host);
3720  }
3721 
3722  //Retrieve port number
3723  port = ntohs(sa->sin6_port);
3724  }
3725  else
3726 #endif
3727  //Invalid address?
3728  {
3729  //The address family was not recognized, or the address length was
3730  //invalid for the specified family
3731  return EAI_FAMILY;
3732  }
3733 
3734  //The caller can specify that no service name is required
3735  if(serv != NULL)
3736  {
3737  //Make sure the buffer is large enough to hold the service name
3738  if(servlen < 6)
3739  return EAI_OVERFLOW;
3740 
3741  //Convert the port number to string representation
3742  osSprintf(serv, "%" PRIu16, ntohs(port));
3743  }
3744 
3745  //Successful processing
3746  return 0;
3747 }
3748 
3749 
3750 /**
3751  * @brief Map an interface name into its corresponding index
3752  * @param[in] ifname Name of the interface
3753  * @return If ifname is the name of an interface, then the function returns the
3754  * interface index corresponding to name ifname. Otherwise, the function
3755  * returns zero
3756  **/
3757 
3759 {
3760  uint_t i;
3761  uint_t index;
3762 
3763  //Initialize interface index
3764  index = 0;
3765 
3766  //Valid parameter?
3767  if(ifname != NULL)
3768  {
3769  //Loop through network interfaces
3770  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3771  {
3772  //Matching interface name?
3773  if(osStrcmp(netInterface[i].name, ifname) == 0)
3774  {
3775  //Save the interface index
3776  index = netInterface[i].index + 1;
3777  //We are done
3778  break;
3779  }
3780  }
3781  }
3782 
3783  //Return the index corresponding to interface name
3784  return index;
3785 }
3786 
3787 
3788 /**
3789  * @brief Map an interface index into its corresponding name
3790  * @param[in] ifindex Interface index
3791  * @param[in] ifname Buffer of at least IF_NAMESIZE bytes
3792  * @return If ifindex is an interface index, then the function returns the value
3793  * supplied in ifname, which points to a buffer now containing the interface
3794  * name. Otherwise, the function returns a NULL pointer
3795  **/
3796 
3798 {
3799  uint_t i;
3800  char_t *name;
3801 
3802  //Initialize interface name
3803  name = NULL;
3804 
3805  //Valid parameters?
3806  if(ifindex != 0 && ifname != NULL)
3807  {
3808  //Loop through network interfaces
3809  for(i = 0; i < NET_INTERFACE_COUNT; i++)
3810  {
3811  //Matching interface index?
3812  if((netInterface[i].index + 1) == ifindex)
3813  {
3814  //Copy the name of the interface
3815  osStrcpy(ifname, netInterface[i].name);
3816  name = ifname;
3817 
3818  //We are done
3819  break;
3820  }
3821  }
3822  }
3823 
3824  //Return the name of the interface
3825  return name;
3826 }
3827 
3828 
3829 /**
3830  * @brief Convert a dot-decimal string into binary data in network byte order
3831  * @param[in] cp NULL-terminated string representing the IPv4 address
3832  * @return Binary data in network byte order
3833  **/
3834 
3836 {
3837 #if (IPV4_SUPPORT == ENABLED)
3838  error_t error;
3839  Ipv4Addr ipv4Addr;
3840 
3841  //Convert a dot-decimal string to a binary IPv4 address
3842  error = ipv4StringToAddr(cp, &ipv4Addr);
3843 
3844  //Check status code
3845  if(error)
3846  {
3847  //The input is invalid
3848  return INADDR_NONE;
3849  }
3850  else
3851  {
3852  //Return the binary representation
3853  return ipv4Addr;
3854  }
3855 #else
3856  //IPv4 is not implemented
3857  return INADDR_NONE;
3858 #endif
3859 }
3860 
3861 
3862 /**
3863  * @brief Convert a dot-decimal string into binary form
3864  * @param[in] cp NULL-terminated string representing the IPv4 address
3865  * @param[out] inp Binary data in network byte order
3866  * @return The function returns non-zero if the address is valid, zero if not
3867  **/
3868 
3869 int_t inet_aton(const char_t *cp, struct in_addr *inp)
3870 {
3871 #if (IPV4_SUPPORT == ENABLED)
3872  error_t error;
3873  Ipv4Addr ipv4Addr;
3874 
3875  //Convert a dot-decimal string to a binary IPv4 address
3876  error = ipv4StringToAddr(cp, &ipv4Addr);
3877 
3878  //Check status code
3879  if(error)
3880  {
3881  //The input is invalid
3882  return 0;
3883  }
3884  else
3885  {
3886  //Copy the binary representation of the IPv4 address
3887  inp->s_addr = ipv4Addr;
3888  //The conversion succeeded
3889  return 1;
3890  }
3891 #else
3892  //IPv4 is not implemented
3893  return 0;
3894 #endif
3895 }
3896 
3897 
3898 /**
3899  * @brief Convert a binary IPv4 address to dot-decimal notation
3900  * @param[in] in Binary representation of the IPv4 address
3901  * @return Pointer to the formatted string
3902  **/
3903 
3904 const char_t *inet_ntoa(struct in_addr in)
3905 {
3906  static char_t buf[16];
3907 
3908  //The string returned by the function resides in static memory area
3909  return inet_ntoa_r(in, buf, sizeof(buf));
3910 }
3911 
3912 
3913 /**
3914  * @brief Convert a binary IPv4 address to dot-decimal notation (reentrant version)
3915  * @param[in] in Binary representation of the IPv4 address
3916  * @param[out] buf Pointer to the buffer where to format the string
3917  * @param[in] buflen Number of bytes available in the buffer
3918  * @return Pointer to the formatted string
3919  **/
3920 
3921 const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
3922 {
3923  //Properly terminate the string
3924  buf[0] = '\0';
3925 
3926 #if (IPV4_SUPPORT == ENABLED)
3927  //Check the length of the buffer
3928  if(buflen >= 16)
3929  {
3930  //Convert the binary IPv4 address to dot-decimal notation
3931  ipv4AddrToString(in.s_addr, buf);
3932  }
3933 #endif
3934 
3935  //Return a pointer to the formatted string
3936  return buf;
3937 }
3938 
3939 
3940 /**
3941  * @brief Convert an IPv4 or IPv6 address from text to binary form
3942  * @param[in] af Address family
3943  * @param[in] src NULL-terminated string representing the IP address
3944  * @param[out] dst Binary representation of the IP address
3945  * @return The function returns 1 on success. 0 is returned if the address
3946  * is not valid. If the address family is not valid, -1 is returned
3947  **/
3948 
3949 int_t inet_pton(int_t af, const char_t *src, void *dst)
3950 {
3951  error_t error;
3952 
3953 #if (IPV4_SUPPORT == ENABLED)
3954  //IPv4 address?
3955  if(af == AF_INET)
3956  {
3957  Ipv4Addr ipv4Addr;
3958 
3959  //Convert the IPv4 address from text to binary form
3960  error = ipv4StringToAddr(src, &ipv4Addr);
3961 
3962  //Check status code
3963  if(error)
3964  {
3965  //The input is invalid
3966  return 0;
3967  }
3968  else
3969  {
3970  //Copy the binary representation of the IPv4 address
3971  ipv4CopyAddr(dst, &ipv4Addr);
3972  //The conversion succeeded
3973  return 1;
3974  }
3975  }
3976  else
3977 #endif
3978 #if (IPV6_SUPPORT == ENABLED)
3979  //IPv6 address?
3980  if(af == AF_INET6)
3981  {
3982  Ipv6Addr ipv6Addr;
3983 
3984  //Convert the IPv6 address from text to binary form
3985  error = ipv6StringToAddr(src, &ipv6Addr);
3986 
3987  //Check status code
3988  if(error)
3989  {
3990  //The input is invalid
3991  return 0;
3992  }
3993  else
3994  {
3995  //Copy the binary representation of the IPv6 address
3996  ipv6CopyAddr(dst, &ipv6Addr);
3997  //The conversion succeeded
3998  return 1;
3999  }
4000  }
4001  else
4002 #endif
4003  //Invalid address family?
4004  {
4005  //Report an error
4006  return -1;
4007  }
4008 }
4009 
4010 
4011 /**
4012  * @brief Convert an IPv4 or IPv6 address from binary to text
4013  * @param[in] af Address family
4014  * @param[in] src Binary representation of the IP address
4015  * @param[out] dst NULL-terminated string representing the IP address
4016  * @param[in] size Number of bytes available in the buffer
4017  * @return On success, the function returns a pointer to the formatted string.
4018  * NULL is returned if there was an error
4019  **/
4020 
4021 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
4022 {
4023 #if (IPV4_SUPPORT == ENABLED)
4024  //IPv4 address?
4025  if(af == AF_INET && size >= INET_ADDRSTRLEN)
4026  {
4027  Ipv4Addr ipv4Addr;
4028 
4029  //Copy the binary representation of the IPv4 address
4030  ipv4CopyAddr(&ipv4Addr, src);
4031 
4032  //Convert the IPv4 address to dot-decimal notation
4033  return ipv4AddrToString(ipv4Addr, dst);
4034  }
4035  else
4036 #endif
4037 #if (IPV6_SUPPORT == ENABLED)
4038  //IPv6 address?
4039  if(af == AF_INET6 && size >= INET6_ADDRSTRLEN)
4040  {
4041  Ipv6Addr ipv6Addr;
4042 
4043  //Copy the binary representation of the IPv6 address
4044  ipv6CopyAddr(&ipv6Addr, src);
4045 
4046  //Convert the IPv6 address to string representation
4047  return ipv6AddrToString(&ipv6Addr, dst);
4048  }
4049  else
4050 #endif
4051  //Invalid address family?
4052  {
4053  //Report an error
4054  return NULL;
4055  }
4056 }
4057 
4058 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1491
const struct in6_addr in6addr_any
Definition: bsd_socket.c:49
IpFilterMode
Multicast filter mode.
Definition: ip.h:67
#define F_GETFL
Definition: bsd_socket.h:214
int_t socketSetSoSndBufOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_SNDBUF option.
char_t * ipv6AddrToString(const Ipv6Addr *ipAddr, char_t *str)
Convert a binary IPv6 address to a string representation.
Definition: ipv6.c:2339
struct hostent * gethostbyname(const char_t *name)
Host name resolution.
Definition: bsd_socket.c:3294
#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:3949
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:1321
#define AF_INET6
Definition: bsd_socket.h:80
Ancillary data header.
Definition: bsd_socket.h:496
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:324
#define IPV6_HOPLIMIT
Definition: bsd_socket.h:186
#define CMSG_DATA(cmsg)
Definition: bsd_socket.h:585
@ SOCKET_OPTION_IPV4_RECV_TOS
Definition: socket.h:197
int_t socketGetTcpKeepIntvlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPINTVL option.
int_t getsourcefilter(int_t s, uint32_t interface, struct sockaddr *group, socklen_t grouplen, uint32_t *fmode, uint_t *numsrc, struct sockaddr_storage *slist)
Get multicast source filter.
Definition: bsd_socket.c:2675
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:327
#define EINPROGRESS
Definition: bsd_socket.h:260
int_t socketGetSoRcvBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_RCVBUF option.
uint32_t sin6_flowinfo
Definition: bsd_socket.h:390
signed int int_t
Definition: compiler_port.h:56
#define netMutex
Definition: net_legacy.h:195
int_t socketGetIpv6RecvHopLimitOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_RECVHOPLIMIT option.
void * iov_base
Definition: bsd_socket.h:470
int_t socketSetIpv6TrafficClassOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_TCLASS option.
IP network address.
Definition: ip.h:90
int_t socketSetTcpMaxSegOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_MAXSEG option.
in_port_t sin6_port
Definition: bsd_socket.h:389
#define IP_UNBLOCK_SOURCE
Definition: bsd_socket.h:164
size_t iov_len
Definition: bsd_socket.h:471
int_t socketGetSoErrorOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_ERROR option.
const char_t * inet_ntoa(struct in_addr in)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:3904
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:2993
#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:2067
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:3253
@ IP_FILTER_MODE_EXCLUDE
Definition: ip.h:68
error_t socketSetMulticastSourceFilter(Socket *socket, const IpAddr *groupAddr, IpFilterMode filterMode, const IpAddr *sources, uint_t numSources)
Set multicast source filter (full-state API)
Definition: socket.c:562
int_t socketGetSoRcvTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_RCVTIMEO option.
uint8_t type
Definition: coap_common.h:176
struct sockaddr_in6 SOCKADDR_IN6
IPv6 address information.
#define SO_SNDBUF
Definition: bsd_socket.h:142
Socket address storage.
Definition: bsd_socket.h:343
#define NET_INTERFACE_COUNT
Definition: net.h:115
#define IPV6_UNICAST_HOPS
Definition: bsd_socket.h:177
int_t socketGetIpTosOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TOS option.
const char_t * inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
Convert an IPv4 or IPv6 address from binary to text.
Definition: bsd_socket.c:4021
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:127
char_t name[]
#define EAI_FAIL
Definition: bsd_socket.h:244
#define osStrcmp(s1, s2)
Definition: os_port.h:174
sa_family_t sin6_family
Definition: bsd_socket.h:388
#define FIONREAD
Definition: bsd_socket.h:209
int_t listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:322
#define EINVAL
Definition: bsd_socket.h:259
#define osStrlen(s)
Definition: os_port.h:168
#define MSG_DONTROUTE
Definition: bsd_socket.h:121
#define IPV4_SUPPORT
Definition: ipv4.h:48
#define IPV6_RECVTCLASS
Definition: bsd_socket.h:188
@ ERROR_END_OF_STREAM
Definition: error.h:211
int_t socketSetIpv6MulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_MULTICAST_LOOP option.
const uint8_t res[]
IPv4 address information.
Definition: bsd_socket.h:364
const char_t * inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
Convert a binary IPv4 address to dot-decimal notation (reentrant version)
Definition: bsd_socket.c:3921
#define BSD_SOCKET_SET_ERRNO(e)
Definition: bsd_socket.h:50
#define MSG_DONTWAIT
Definition: bsd_socket.h:123
#define MCAST_UNBLOCK_SOURCE
Definition: bsd_socket.h:170
#define EWOULDBLOCK
Definition: bsd_socket.h:257
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:298
int_t socketGetIpRecvTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_RECVTTL option.
int_t cmsg_level
Definition: bsd_socket.h:498
int_t socketGetTcpKeepIdleOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPIDLE option.
void socketFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
#define TCP_MAXSEG
Definition: bsd_socket.h:193
#define IP_MULTICAST_IF
Definition: bsd_socket.h:159
uint_t if_nametoindex(const char_t *ifname)
Map an interface name into its corresponding index.
Definition: bsd_socket.c:3758
@ 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:3631
#define AF_PACKET
Definition: bsd_socket.h:81
int_t socketSetIpDropSourceMembershipOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_DROP_SOURCE_MEMBERSHIP option.
@ HOST_TYPE_IPV6
Definition: socket.h:218
#define NO_ADDRESS
Definition: bsd_socket.h:279
int_t socketGetTcpKeepCntOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPCNT option.
@ SOCKET_OPTION_IPV4_RECV_TTL
Definition: socket.h:198
int_t socketSetSoKeepAliveOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_KEEPALIVE option.
int_t socketGetIpv6MulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_MULTICAST_LOOP option.
Structure that represents an IPv6 address.
Definition: bsd_socket.h:377
#define IP_ADD_SOURCE_MEMBERSHIP
Definition: bsd_socket.h:166
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
error_t ipStringToAddr(const char_t *str, IpAddr *ipAddr)
Convert a string representation of an IP address to a binary IP address.
Definition: ip.c:760
#define SO_RCVBUF
Definition: bsd_socket.h:143
int_t socketSetIpv6OnlyOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_V6ONLY option.
int_t socketSetIpDontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_DONTFRAG option.
#define EAI_NONAME
Definition: bsd_socket.h:248
@ ERROR_IN_PROGRESS
Definition: error.h:214
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1639
#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:3797
#define TCP_NODELAY
Definition: bsd_socket.h:192
#define FALSE
Definition: os_port.h:46
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
int32_t tv_sec
Definition: bsd_socket.h:573
uint8_t h
Definition: ndp.h:302
int ipi_ifindex
Definition: bsd_socket.h:509
void socketTranslateErrorCode(Socket *socket, error_t errorCode)
Translate error code.
int_t fd_count
Definition: bsd_socket.h:531
int_t socketSetMcastJoinSourceGroupOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_JOIN_SOURCE_GROUP option.
#define IPPROTO_IP
Definition: bsd_socket.h:95
#define AI_NUMERICHOST
Definition: bsd_socket.h:223
int_t socketSetIpv6DontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_DONTFRAG option.
Socket address.
Definition: bsd_socket.h:332
int_t socketGetSoReuseAddrOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_REUSEADDR option.
error_t
Error codes.
Definition: error.h:43
#define netInterface
Definition: net_legacy.h:199
int_t ai_protocol
Definition: bsd_socket.h:557
int_t socketSetMcastUnblockSourceOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_UNBLOCK_SOURCE option.
IPv6 address information.
Definition: bsd_socket.h:387
#define osSprintf(dest,...)
Definition: os_port.h:234
#define AI_PASSIVE
Definition: bsd_socket.h:221
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1697
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:3317
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:2935
#define SO_BROADCAST
Definition: bsd_socket.h:141
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:471
BSD socket API.
int_t socketGetIpv6OnlyOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_V6ONLY option.
error_t socketReceiveFrom(Socket *socket, IpAddr *srcIpAddr, uint16_t *srcPort, void *data, size_t size, size_t *received, uint_t flags)
Receive a datagram from a connectionless socket.
Definition: socket.c:1719
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:1899
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:2279
int_t msg_iovlen
Definition: bsd_socket.h:484
#define SOCKET_SUCCESS
Definition: bsd_socket.h:237
#define IP_PKTINFO
Definition: bsd_socket.h:156
int_t socketSetIpAddSourceMembershipOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_ADD_SOURCE_MEMBERSHIP option.
int_t socketSetIpAddMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_ADD_MEMBERSHIP option.
int_t socketSetTcpKeepCntOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPCNT option.
Message header.
Definition: bsd_socket.h:480
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1354
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:66
int_t socketGetSoNoCheckOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_NO_CHECK option.
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2025
#define IPV6_MULTICAST_HOPS
Definition: bsd_socket.h:179
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:532
int_t socketSetIpPktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_PKTINFO option.
#define IPPROTO_TCP
Definition: bsd_socket.h:98
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
#define SO_SNDTIMEO
Definition: bsd_socket.h:147
in_addr_t inet_addr(const char_t *cp)
Convert a dot-decimal string into binary data in network byte order.
Definition: bsd_socket.c:3835
int_t socketSetIpDropMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_DROP_MEMBERSHIP option.
#define ENAMETOOLONG
Definition: bsd_socket.h:262
#define MCAST_JOIN_GROUP
Definition: bsd_socket.h:168
int32_t tv_usec
Definition: bsd_socket.h:574
size_t length
Definition: ip.h:91
int_t socketSetIpv6RecvHopLimitOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_RECVHOPLIMIT option.
socklen_t ai_addrlen
Definition: bsd_socket.h:558
@ HOST_TYPE_IPV4
Definition: socket.h:217
struct addrinfo ADDRINFO
Information about address of a service provider.
#define HOST_NOT_FOUND
Definition: bsd_socket.h:276
uint32_t sin6_scope_id
Definition: bsd_socket.h:392
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:49
#define ENABLED
Definition: os_port.h:37
#define IP_DROP_SOURCE_MEMBERSHIP
Definition: bsd_socket.h:167
int_t socketSetIpv6AddMembershipOption(Socket *socket, const struct ipv6_mreq *optval, socklen_t optlen)
Set IPV6_ADD_MEMBERSHIP option.
int_t socketGetIpMulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_LOOP option.
int_t socketGetIpTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TTL option.
int_t socketSetIpTosOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_TOS option.
#define MCAST_BLOCK_SOURCE
Definition: bsd_socket.h:169
int_t recvfrom(int_t s, void *data, size_t size, int_t flags, struct sockaddr *addr, socklen_t *addrlen)
Receive a datagram.
Definition: bsd_socket.c:989
#define FIONSPACE
Definition: bsd_socket.h:211
#define MSG_CTRUNC
Definition: bsd_socket.h:122
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:1456
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:2835
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:258
uint8_t flags
Definition: tcp.h:358
#define SO_NO_CHECK
Definition: bsd_socket.h:145
int_t bind(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:107
#define EFAULT
Definition: bsd_socket.h:258
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:198
Ipv4Addr ipv4Addr
Definition: ip.h:95
int_t socketGetSoSndTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_SNDTIMEO option.
struct in_addr ipi_addr
Definition: bsd_socket.h:510
error_t socketGetMulticastSourceFilter(Socket *socket, const IpAddr *groupAddr, IpFilterMode *filterMode, IpAddr *sources, uint_t *numSources)
Get multicast source filter.
Definition: socket.c:679
#define SO_TYPE
Definition: bsd_socket.h:138
Information about a given host.
Definition: bsd_socket.h:541
#define F_SETFL
Definition: bsd_socket.h:215
BSD socket options.
int_t socketGetSoBroadcastOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_BROADCAST option.
uint32_t time
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:123
int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
Determine the status of one or more sockets.
Definition: bsd_socket.c:3071
#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:3660
#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:1512
#define MCAST_JOIN_SOURCE_GROUP
Definition: bsd_socket.h:172
int_t socketGetSoKeepAliveOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_KEEPALIVE option.
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:95
int_t getsockopt(int_t s, int_t level, int_t optname, void *optval, socklen_t *optlen)
The getsockopt function retrieves a socket option.
Definition: bsd_socket.c: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:1389
Ipv4Addr ipAddr
Definition: ipcp.h:105
#define MSG_PEEK
Definition: bsd_socket.h:120
struct iovec * msg_iov
Definition: bsd_socket.h:483
#define NETDB_SUCCESS
Definition: bsd_socket.h:275
int_t setsourcefilter(int_t s, uint32_t interface, struct sockaddr *group, socklen_t grouplen, uint32_t fmode, uint_t numsrc, struct sockaddr_storage *slist)
Set multicast source filter.
Definition: bsd_socket.c:2509
int_t sendto(int_t s, const void *data, size_t length, int_t flags, const struct sockaddr *addr, socklen_t addrlen)
Send a datagram to a specific destination.
Definition: bsd_socket.c:535
int_t socketSetIpMulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_MULTICAST_LOOP option.
error_t ipv6StringToAddr(const char_t *str, Ipv6Addr *ipAddr)
Convert a string representation of an IPv6 address to a binary IPv6 address.
Definition: ipv6.c:2184
#define SO_KEEPALIVE
Definition: bsd_socket.h:144
#define IP_ADD_MEMBERSHIP
Definition: bsd_socket.h:162
#define O_NONBLOCK
Definition: bsd_socket.h:218
Ipv4Addr addr
Definition: nbns_common.h: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:3391
unsigned int uint_t
Definition: compiler_port.h:57
size_t msg_controllen
Definition: bsd_socket.h:486
@ SOCKET_FLAG_DONT_WAIT
Definition: socket.h:139
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:253
#define osMemset(p, value, length)
Definition: os_port.h:138
TCP/IP stack core.
int_t socketSetMcastBlockSourceOption(Socket *socket, const struct group_source_req *optval, socklen_t optlen)
Set MCAST_BLOCK_SOURCE option.
@ SOCKET_EVENT_CLOSED
Definition: socket.h:174
int_t socketSetIpv6PktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_PKTINFO option.
char_t * ipv4AddrToString(Ipv4Addr ipAddr, char_t *str)
Convert a binary IPv4 address to dot-decimal notation.
Definition: ipv4.c:1478
#define IPV6_TCLASS
Definition: bsd_socket.h:189
int_t socketGetSoSndBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_SNDBUF option.
int_t socketGetIpv6PktInfoOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_PKTINFO option.
#define SOCKET_MAX_COUNT
Definition: socket.h:46
int_t inet_aton(const char_t *cp, struct in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:3869
#define IPV6_DROP_MEMBERSHIP
Definition: bsd_socket.h:182
int_t socketGetTcpNoDelayOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_NODELAY option.
#define osStrcpy(s1, s2)
Definition: os_port.h:210
#define TCP_KEEPINTVL
Definition: bsd_socket.h:195
int_t send(int_t s, const void *data, size_t length, int_t flags)
Send data to a connected socket.
Definition: bsd_socket.c:459
int_t socketGetIpMulticastTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_TTL option.
#define MCAST_LEAVE_GROUP
Definition: bsd_socket.h:171
uint16_t next
Definition: ipv4_frag.h:106
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:276
#define TCP_KEEPIDLE
Definition: bsd_socket.h:194
int_t socketSetMcastJoinGroupOption(Socket *socket, const struct group_req *optval, socklen_t optlen)
Set MCAST_JOIN_GROUP option.
int_t socketSetIpMulticastIfOption(Socket *socket, const struct in_addr *optval, socklen_t optlen)
Set IP_MULTICAST_IF option.
Information about address of a service provider.
Definition: bsd_socket.h:553
int_t msg_flags
Definition: bsd_socket.h:487
#define IPV6_ADD_MEMBERSHIP
Definition: bsd_socket.h:181
int_t socketSetIpBlockSourceOption(Socket *socket, const struct ip_mreq_source *optval, socklen_t optlen)
Set IP_BLOCK_SOURCE option.
@ NO_ERROR
Success.
Definition: error.h:44
#define IP_DROP_MEMBERSHIP
Definition: bsd_socket.h:163
Debugging facilities.
@ SOCKET_OPTION_TCP_NO_DELAY
Definition: socket.h:205
uint8_t s6_addr[16]
Definition: bsd_socket.h:378
#define INFINITE_DELAY
Definition: os_port.h:75
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:117
@ SOCKET_OPTION_IPV4_PKT_INFO
Definition: socket.h:196
int_t sendmsg(int_t s, struct msghdr *msg, int_t flags)
Send a message.
Definition: bsd_socket.c:661
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:3029
uint16_t h_addrtype
Definition: bsd_socket.h:542
struct addrinfo * ai_next
Definition: bsd_socket.h:561
IPv4 packet information.
Definition: bsd_socket.h:508
#define SO_ERROR
Definition: bsd_socket.h:139
error_t socketListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: socket.c:1418