bsd_socket.c
Go to the documentation of this file.
1 /**
2  * @file bsd_socket.c
3  * @brief BSD socket API
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.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;
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;
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
1730  {
1731  //Unknown option
1733  //Report an error
1734  ret = SOCKET_ERROR;
1735  }
1736  }
1737  else if(level == IPPROTO_IP)
1738  {
1739  //Check option type
1740  if(optname == IP_TOS)
1741  {
1742  //Set IP_TOS option
1743  ret = socketSetIpTosOption(sock, optval, optlen);
1744  }
1745  else if(optname == IP_TTL)
1746  {
1747  //Set IP_TTL option
1748  ret = socketSetIpTtlOption(sock, optval, optlen);
1749  }
1750  else if(optname == IP_MULTICAST_IF)
1751  {
1752  //Set IP_MULTICAST_IF option
1753  ret = socketSetIpMulticastIfOption(sock, optval, optlen);
1754  }
1755  else if(optname == IP_MULTICAST_TTL)
1756  {
1757  //Set IP_MULTICAST_TTL option
1758  ret = socketSetIpMulticastTtlOption(sock, optval, optlen);
1759  }
1760  else if(optname == IP_MULTICAST_LOOP)
1761  {
1762  //Set IP_MULTICAST_LOOP option
1763  ret = socketSetIpMulticastLoopOption(sock, optval, optlen);
1764  }
1765  else if(optname == IP_ADD_MEMBERSHIP)
1766  {
1767  //Set IP_ADD_MEMBERSHIP option
1768  ret = socketSetIpAddMembershipOption(sock, optval, optlen);
1769  }
1770  else if(optname == IP_DROP_MEMBERSHIP)
1771  {
1772  //Set IP_DROP_MEMBERSHIP option
1773  ret = socketSetIpDropMembershipOption(sock, optval, optlen);
1774  }
1775  else if(optname == IP_DONTFRAG)
1776  {
1777  //Set IP_DONTFRAG option
1778  ret = socketSetIpDontFragOption(sock, optval, optlen);
1779  }
1780  else if(optname == IP_PKTINFO)
1781  {
1782  //Set IP_PKTINFO option
1783  ret = socketSetIpPktInfoOption(sock, optval, optlen);
1784  }
1785  else if(optname == IP_RECVTOS)
1786  {
1787  //Set IP_RECVTOS option
1788  ret = socketSetIpRecvTosOption(sock, optval, optlen);
1789  }
1790  else if(optname == IP_RECVTTL)
1791  {
1792  //Set IP_RECVTTL option
1793  ret = socketSetIpRecvTtlOption(sock, optval, optlen);
1794  }
1795  else
1796  {
1797  //Unknown option
1799  ret = SOCKET_ERROR;
1800  }
1801  }
1802  else if(level == IPPROTO_IPV6)
1803  {
1804  //Check option type
1805  if(optname == IPV6_TCLASS)
1806  {
1807  //Set IPV6_TCLASS option
1808  ret = socketSetIpv6TrafficClassOption(sock, optval, optlen);
1809  }
1810  else if(optname == IPV6_UNICAST_HOPS)
1811  {
1812  //Set IPV6_UNICAST_HOPS option
1813  ret = socketSetIpv6UnicastHopsOption(sock, optval, optlen);
1814  }
1815  else if(optname == IPV6_MULTICAST_IF)
1816  {
1817  //Set IPV6_MULTICAST_IF option
1818  ret = socketSetIpv6MulticastIfOption(sock, optval, optlen);
1819  }
1820  else if(optname == IPV6_MULTICAST_HOPS)
1821  {
1822  //Set IPV6_MULTICAST_HOPS option
1823  ret = socketSetIpv6MulticastHopsOption(sock, optval, optlen);
1824  }
1825  else if(optname == IPV6_MULTICAST_LOOP)
1826  {
1827  //Set IPV6_MULTICAST_LOOP option
1828  ret = socketSetIpv6MulticastLoopOption(sock, optval, optlen);
1829  }
1830  else if(optname == IPV6_ADD_MEMBERSHIP)
1831  {
1832  //Set IPV6_ADD_MEMBERSHIP option
1833  ret = socketSetIpv6AddMembershipOption(sock, optval, optlen);
1834  }
1835  else if(optname == IPV6_DROP_MEMBERSHIP)
1836  {
1837  //Set IPV6_DROP_MEMBERSHIP option
1838  ret = socketSetIpv6DropMembershipOption(sock, optval, optlen);
1839  }
1840  else if(optname == IPV6_V6ONLY)
1841  {
1842  //Set IPV6_V6ONLY option
1843  ret = socketSetIpv6OnlyOption(sock, optval, optlen);
1844  }
1845  else if(optname == IPV6_DONTFRAG)
1846  {
1847  //Set IPV6_DONTFRAG option
1848  ret = socketSetIpv6DontFragOption(sock, optval, optlen);
1849  }
1850  else if(optname == IPV6_PKTINFO)
1851  {
1852  //Set IPV6_PKTINFO option
1853  ret = socketSetIpv6PktInfoOption(sock, optval, optlen);
1854  }
1855  else if(optname == IPV6_RECVTCLASS)
1856  {
1857  //Set IPV6_RECVTCLASS option
1858  ret = socketSetIpv6RecvTrafficClassOption(sock, optval, optlen);
1859  }
1860  else if(optname == IPV6_RECVHOPLIMIT)
1861  {
1862  //Set IPV6_RECVHOPLIMIT option
1863  ret = socketSetIpv6RecvHopLimitOption(sock, optval, optlen);
1864  }
1865  else
1866  {
1867  //Unknown option
1869  ret = SOCKET_ERROR;
1870  }
1871  }
1872  else if(level == IPPROTO_TCP)
1873  {
1874  //Check option type
1875  if(optname == TCP_NODELAY)
1876  {
1877  //Set TCP_NODELAY option
1878  ret = socketSetTcpNoDelayOption(sock, optval, optlen);
1879  }
1880  else if(optname == TCP_MAXSEG)
1881  {
1882  //Set TCP_MAXSEG option
1883  ret = socketSetTcpMaxSegOption(sock, optval, optlen);
1884  }
1885  else if(optname == TCP_KEEPIDLE)
1886  {
1887  //Set TCP_KEEPIDLE option
1888  ret = socketSetTcpKeepIdleOption(sock, optval, optlen);
1889  }
1890  else if(optname == TCP_KEEPINTVL)
1891  {
1892  //Set TCP_KEEPINTVL option
1893  ret = socketSetTcpKeepIntvlOption(sock, optval, optlen);
1894  }
1895  else if(optname == TCP_KEEPCNT)
1896  {
1897  //Set TCP_KEEPCNT option
1898  ret = socketSetTcpKeepCntOption(sock, optval, optlen);
1899  }
1900  else
1901  {
1902  //Unknown option
1904  ret = SOCKET_ERROR;
1905  }
1906  }
1907  else
1908  {
1909  //The specified level is not valid
1910  socketSetErrnoCode(sock, EINVAL);
1911  ret = SOCKET_ERROR;
1912  }
1913  }
1914  else
1915  {
1916  //The option is not valid
1917  socketSetErrnoCode(sock, EFAULT);
1918  ret = SOCKET_ERROR;
1919  }
1920 
1921  //return status code
1922  return ret;
1923 }
1924 
1925 
1926 /**
1927  * @brief The getsockopt function retrieves a socket option
1928  * @param[in] s Descriptor that identifies a socket
1929  * @param[in] level The level at which the option is defined
1930  * @param[in] optname The socket option for which the value is to be retrieved
1931  * @param[out] optval A pointer to the buffer in which the value for the
1932  * requested option is to be returned
1933  * @param[in,out] optlen The size, in bytes, of the buffer pointed to by the
1934  * optval parameter
1935  * @return If no error occurs, getsockopt returns SOCKET_SUCCESS
1936  * Otherwise, it returns SOCKET_ERROR
1937  **/
1938 
1939 int_t getsockopt(int_t s, int_t level, int_t optname, void *optval,
1940  socklen_t *optlen)
1941 {
1942  int_t ret;
1943  Socket *sock;
1944 
1945  //Make sure the socket descriptor is valid
1946  if(s < 0 || s >= SOCKET_MAX_COUNT)
1947  {
1948  return SOCKET_ERROR;
1949  }
1950 
1951  //Point to the socket structure
1952  sock = &socketTable[s];
1953 
1954  //Get exclusive access
1956 
1957  //Make sure the option is valid
1958  if(optval != NULL)
1959  {
1960  //Check option level
1961  if(level == SOL_SOCKET)
1962  {
1963  //Check option type
1964  if(optname == SO_REUSEADDR)
1965  {
1966  //Get SO_REUSEADDR option
1967  ret = socketGetSoReuseAddrOption(sock, optval, optlen);
1968  }
1969  else if(optname == SO_BROADCAST)
1970  {
1971  //Get SO_BROADCAST option
1972  ret = socketGetSoBroadcastOption(sock, optval, optlen);
1973  }
1974  else if(optname == SO_SNDTIMEO)
1975  {
1976  //Get SO_SNDTIMEO option
1977  ret = socketGetSoSndTimeoOption(sock, optval, optlen);
1978  }
1979  else if(optname == SO_RCVTIMEO)
1980  {
1981  //Get SO_RCVTIMEO option
1982  ret = socketGetSoRcvTimeoOption(sock, optval, optlen);
1983  }
1984  else if(optname == SO_SNDBUF)
1985  {
1986  //Get SO_SNDBUF option
1987  ret = socketGetSoSndBufOption(sock, optval, optlen);
1988  }
1989  else if(optname == SO_RCVBUF)
1990  {
1991  //Get SO_RCVBUF option
1992  ret = socketGetSoRcvBufOption(sock, optval, optlen);
1993  }
1994  else if(optname == SO_KEEPALIVE)
1995  {
1996  //Get SO_KEEPALIVE option
1997  ret = socketGetSoKeepAliveOption(sock, optval, optlen);
1998  }
1999  else if(optname == SO_TYPE)
2000  {
2001  //Get SO_TYPE option
2002  ret = socketGetSoTypeOption(sock, optval, optlen);
2003  }
2004  else if(optname == SO_ERROR)
2005  {
2006  //Get SO_ERROR option
2007  ret = socketGetSoErrorOption(sock, optval, optlen);
2008  }
2009  else
2010  {
2011  //Unknown option
2013  ret = SOCKET_ERROR;
2014  }
2015  }
2016  else if(level == IPPROTO_IP)
2017  {
2018  //Check option type
2019  if(optname == IP_TOS)
2020  {
2021  //Get IP_TOS option
2022  ret = socketGetIpTosOption(sock, optval, optlen);
2023  }
2024  else if(optname == IP_TTL)
2025  {
2026  //Get IP_TTL option
2027  ret = socketGetIpTtlOption(sock, optval, optlen);
2028  }
2029  else if(optname == IP_MULTICAST_TTL)
2030  {
2031  //Get IP_MULTICAST_TTL option
2032  ret = socketGetIpMulticastTtlOption(sock, optval, optlen);
2033  }
2034  else if(optname == IP_MULTICAST_LOOP)
2035  {
2036  //Get IP_MULTICAST_LOOP option
2037  ret = socketGetIpMulticastLoopOption(sock, optval, optlen);
2038  }
2039  else if(optname == IP_DONTFRAG)
2040  {
2041  //Get IP_DONTFRAG option
2042  ret = socketGetIpDontFragOption(sock, optval, optlen);
2043  }
2044  else if(optname == IP_PKTINFO)
2045  {
2046  //Get IP_PKTINFO option
2047  ret = socketGetIpPktInfoOption(sock, optval, optlen);
2048  }
2049  else if(optname == IP_RECVTOS)
2050  {
2051  //Get IP_RECVTOS option
2052  ret = socketGetIpRecvTosOption(sock, optval, optlen);
2053  }
2054  else if(optname == IP_RECVTTL)
2055  {
2056  //Get IP_RECVTTL option
2057  ret = socketGetIpRecvTtlOption(sock, optval, optlen);
2058  }
2059  else
2060  {
2061  //Unknown option
2063  ret = SOCKET_ERROR;
2064  }
2065  }
2066  else if(level == IPPROTO_IPV6)
2067  {
2068  //Check option type
2069  if(optname == IPV6_TCLASS)
2070  {
2071  //Get IPV6_TCLASS option
2072  ret = socketGetIpv6TrafficClassOption(sock, optval, optlen);
2073  }
2074  else if(optname == IPV6_UNICAST_HOPS)
2075  {
2076  //Get IPV6_UNICAST_HOPS option
2077  ret = socketGetIpv6UnicastHopsOption(sock, optval, optlen);
2078  }
2079  else if(optname == IPV6_MULTICAST_HOPS)
2080  {
2081  //Get IPV6_MULTICAST_HOPS option
2082  ret = socketGetIpv6MulticastHopsOption(sock, optval, optlen);
2083  }
2084  else if(optname == IPV6_MULTICAST_LOOP)
2085  {
2086  //Get IPV6_MULTICAST_LOOP option
2087  ret = socketGetIpv6MulticastLoopOption(sock, optval, optlen);
2088  }
2089  else if(optname == IPV6_V6ONLY)
2090  {
2091  //Get IPV6_V6ONLY option
2092  ret = socketGetIpv6OnlyOption(sock, optval, optlen);
2093  }
2094  else if(optname == IPV6_DONTFRAG)
2095  {
2096  //Get IPV6_DONTFRAG option
2097  ret = socketGetIpv6DontFragOption(sock, optval, optlen);
2098  }
2099  else if(optname == IPV6_PKTINFO)
2100  {
2101  //Get IPV6_PKTINFO option
2102  ret = socketGetIpv6PktInfoOption(sock, optval, optlen);
2103  }
2104  else if(optname == IPV6_RECVTCLASS)
2105  {
2106  //Get IPV6_RECVTCLASS option
2107  ret = socketGetIpv6RecvTrafficClassOption(sock, optval, optlen);
2108  }
2109  else if(optname == IPV6_RECVHOPLIMIT)
2110  {
2111  //Get IPV6_RECVHOPLIMIT option
2112  ret = socketGetIpv6RecvHopLimitOption(sock, optval, optlen);
2113  }
2114  else
2115  {
2116  //Unknown option
2118  ret = SOCKET_ERROR;
2119  }
2120  }
2121  else if(level == IPPROTO_TCP)
2122  {
2123  //Check option type
2124  if(optname == TCP_NODELAY)
2125  {
2126  //Get TCP_NODELAY option
2127  ret = socketGetTcpNoDelayOption(sock, optval, optlen);
2128  }
2129  else if(optname == TCP_MAXSEG)
2130  {
2131  //Get TCP_MAXSEG option
2132  ret = socketGetTcpMaxSegOption(sock, optval, optlen);
2133  }
2134  else if(optname == TCP_KEEPIDLE)
2135  {
2136  //Get TCP_KEEPIDLE option
2137  ret = socketGetTcpKeepIdleOption(sock, optval, optlen);
2138  }
2139  else if(optname == TCP_KEEPINTVL)
2140  {
2141  //Get TCP_KEEPINTVL option
2142  ret = socketGetTcpKeepIntvlOption(sock, optval, optlen);
2143  }
2144  else if(optname == TCP_KEEPCNT)
2145  {
2146  //Get TCP_KEEPCNT option
2147  ret = socketGetTcpKeepCntOption(sock, optval, optlen);
2148  }
2149  else
2150  {
2151  //Unknown option
2153  ret = SOCKET_ERROR;
2154  }
2155  }
2156  else
2157  {
2158  //The specified level is not valid
2159  socketSetErrnoCode(sock, EINVAL);
2160  ret = SOCKET_ERROR;
2161  }
2162  }
2163  else
2164  {
2165  //The option is not valid
2166  socketSetErrnoCode(sock, EFAULT);
2167  ret = SOCKET_ERROR;
2168  }
2169 
2170  //Release exclusive access
2172 
2173  //return status code
2174  return ret;
2175 }
2176 
2177 
2178 /**
2179  * @brief Control the I/O mode of a socket
2180  * @param[in] s Descriptor that identifies a socket
2181  * @param[in] cmd A command to perform on the socket
2182  * @param[in,out] arg A pointer to a parameter
2183  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2184  * Otherwise, it returns SOCKET_ERROR
2185  **/
2186 
2187 int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
2188 {
2189  int_t ret;
2190  uint_t *val;
2191  Socket *sock;
2192 
2193  //Make sure the socket descriptor is valid
2194  if(s < 0 || s >= SOCKET_MAX_COUNT)
2195  {
2196  return SOCKET_ERROR;
2197  }
2198 
2199  //Point to the socket structure
2200  sock = &socketTable[s];
2201 
2202  //Get exclusive access
2204 
2205  //Make sure the parameter is valid
2206  if(arg != NULL)
2207  {
2208  //Check command type
2209  switch(cmd)
2210  {
2211  //Enable or disable non-blocking mode
2212  case FIONBIO:
2213  //Cast the parameter to the relevant type
2214  val = (uint_t *) arg;
2215  //Enable blocking or non-blocking operation
2216  sock->timeout = (*val != 0) ? 0 : INFINITE_DELAY;
2217  //Successful processing
2218  ret = SOCKET_SUCCESS;
2219  break;
2220 
2221 #if (TCP_SUPPORT == ENABLED)
2222  //Get the number of bytes that are immediately available for reading
2223  case FIONREAD:
2224  //Cast the parameter to the relevant type
2225  val = (uint_t *) arg;
2226  //Return the actual value
2227  *val = sock->rcvUser;
2228  //Successful processing
2229  ret = SOCKET_SUCCESS;
2230  break;
2231 
2232  //Get the number of bytes written to the send queue but not yet
2233  //acknowledged by the other side of the connection
2234  case FIONWRITE:
2235  //Cast the parameter to the relevant type
2236  val = (uint_t *) arg;
2237  //Return the actual value
2238  *val = sock->sndUser + sock->sndNxt - sock->sndUna;
2239  //Successful processing
2240  ret = SOCKET_SUCCESS;
2241  break;
2242 
2243  //Get the free space in the send queue
2244  case FIONSPACE:
2245  //Cast the parameter to the relevant type
2246  val = (uint_t *) arg;
2247  //Return the actual value
2248  *val = sock->txBufferSize - (sock->sndUser + sock->sndNxt -
2249  sock->sndUna);
2250  //Successful processing
2251  ret = SOCKET_SUCCESS;
2252  break;
2253 #endif
2254 
2255  //Unknown command?
2256  default:
2257  //Report an error
2258  socketSetErrnoCode(sock, EINVAL);
2259  ret = SOCKET_ERROR;
2260  break;
2261  }
2262  }
2263  else
2264  {
2265  //The parameter is not valid
2266  socketSetErrnoCode(sock, EFAULT);
2267  ret = SOCKET_ERROR;
2268  }
2269 
2270  //Release exclusive access
2272 
2273  //return status code
2274  return ret;
2275 }
2276 
2277 
2278 /**
2279  * @brief Perform specific operation
2280  * @param[in] s Descriptor that identifies a socket
2281  * @param[in] cmd A command to perform on the socket
2282  * @param[in] arg Argument value
2283  * @return If no error occurs, setsockopt returns SOCKET_SUCCESS
2284  * Otherwise, it returns SOCKET_ERROR
2285  **/
2286 
2288 {
2289  int_t ret;
2290  Socket *sock;
2291 
2292  //Make sure the socket descriptor is valid
2293  if(s < 0 || s >= SOCKET_MAX_COUNT)
2294  {
2295  return SOCKET_ERROR;
2296  }
2297 
2298  //Point to the socket structure
2299  sock = &socketTable[s];
2300 
2301  //Get exclusive access
2303 
2304  //Check command type
2305  switch(cmd)
2306  {
2307  //Get the file status flags?
2308  case F_GETFL:
2309  //Return (as the function result) the file descriptor flags
2310  ret = (sock->timeout == 0) ? O_NONBLOCK : 0;
2311  break;
2312 
2313  //Set the file status flags?
2314  case F_SETFL:
2315  //Enable blocking or non-blocking operation
2316  sock->timeout = ((arg & O_NONBLOCK) != 0) ? 0 : INFINITE_DELAY;
2317  //Successful processing
2318  ret = SOCKET_SUCCESS;
2319  break;
2320 
2321  //Unknown command?
2322  default:
2323  //Report an error
2324  socketSetErrnoCode(sock, EINVAL);
2325  ret = SOCKET_ERROR;
2326  break;
2327  }
2328 
2329  //Release exclusive access
2331 
2332  //return status code
2333  return ret;
2334 }
2335 
2336 
2337 /**
2338  * @brief The shutdown function disables sends or receives on a socket
2339  * @param[in] s Descriptor that identifies a socket
2340  * @param[in] how A flag that describes what types of operation will no longer be allowed
2341  * @return If no error occurs, shutdown returns SOCKET_SUCCESS
2342  * Otherwise, it returns SOCKET_ERROR
2343  **/
2344 
2346 {
2347  error_t error;
2348  Socket *sock;
2349 
2350  //Make sure the socket descriptor is valid
2351  if(s < 0 || s >= SOCKET_MAX_COUNT)
2352  {
2353  return SOCKET_ERROR;
2354  }
2355 
2356  //Point to the socket structure
2357  sock = &socketTable[s];
2358 
2359  //Shut down socket
2360  error = socketShutdown(sock, how);
2361 
2362  //Any error to report?
2363  if(error)
2364  {
2365  socketTranslateErrorCode(sock, error);
2366  return SOCKET_ERROR;
2367  }
2368 
2369  //Successful processing
2370  return SOCKET_SUCCESS;
2371 }
2372 
2373 
2374 /**
2375  * @brief The closesocket function closes an existing socket
2376  * @param[in] s Descriptor that identifies a socket
2377  * @return If no error occurs, closesocket returns SOCKET_SUCCESS
2378  * Otherwise, it returns SOCKET_ERROR
2379  **/
2380 
2382 {
2383  Socket *sock;
2384 
2385  //Make sure the socket descriptor is valid
2386  if(s < 0 || s >= SOCKET_MAX_COUNT)
2387  {
2388  return SOCKET_ERROR;
2389  }
2390 
2391  //Point to the socket structure
2392  sock = &socketTable[s];
2393 
2394  //Close socket
2395  socketClose(sock);
2396 
2397  //Successful processing
2398  return SOCKET_SUCCESS;
2399 }
2400 
2401 
2402 /**
2403  * @brief Determine the status of one or more sockets
2404  *
2405  * The select function determines the status of one or more sockets,
2406  * waiting if necessary, to perform synchronous I/O
2407  *
2408  * @param[in] nfds Unused parameter included only for compatibility with
2409  * Berkeley socket
2410  * @param[in,out] readfds An optional pointer to a set of sockets to be
2411  * checked for readability
2412  * @param[in,out] writefds An optional pointer to a set of sockets to be
2413  * checked for writability
2414  * @param[in,out] exceptfds An optional pointer to a set of sockets to be
2415  * checked for errors
2416  * @param[in] timeout The maximum time for select to wait. Set the timeout
2417  * parameter to null for blocking operations
2418  * @return The select function returns the total number of socket handles that
2419  * are ready and contained in the fd_set structures, zero if the time limit
2420  * expired, or SOCKET_ERROR if an error occurred
2421  **/
2422 
2423 int_t select(int_t nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
2424  const struct timeval *timeout)
2425 {
2426  int_t i;
2427  int_t j;
2428  int_t n;
2429  int_t s;
2430  systime_t time;
2431  uint_t eventMask;
2432  uint_t eventFlags;
2433  OsEvent event;
2434  fd_set *fds;
2435 
2436  //Parse all the descriptor sets
2437  for(i = 0; i < 3; i++)
2438  {
2439  //Select the suitable descriptor set
2440  switch(i)
2441  {
2442  case 0:
2443  //Set of sockets to be checked for readability
2444  fds = readfds;
2445  break;
2446 
2447  case 1:
2448  //Set of sockets to be checked for writability
2449  fds = writefds;
2450  break;
2451 
2452  default:
2453  //Set of sockets to be checked for errors
2454  fds = exceptfds;
2455  break;
2456  }
2457 
2458  //Each descriptor is optional and may be omitted
2459  if(fds != NULL)
2460  {
2461  //Parse the current set of sockets
2462  for(j = 0; j < fds->fd_count; j++)
2463  {
2464  //Invalid socket descriptor?
2465  if(fds->fd_array[j] < 0 || fds->fd_array[j] >= SOCKET_MAX_COUNT)
2466  {
2467  //Report an error
2468  return SOCKET_ERROR;
2469  }
2470  }
2471  }
2472  }
2473 
2474  //Create an event object to get notified of socket events
2475  if(!osCreateEvent(&event))
2476  {
2477  //Failed to create event
2478  return SOCKET_ERROR;
2479  }
2480 
2481  //Parse all the descriptor sets
2482  for(i = 0; i < 3; i++)
2483  {
2484  //Select the suitable descriptor set
2485  switch(i)
2486  {
2487  case 0:
2488  //Set of sockets to be checked for readability
2489  fds = readfds;
2490  eventMask = SOCKET_EVENT_RX_READY;
2491  break;
2492 
2493  case 1:
2494  //Set of sockets to be checked for writability
2495  fds = writefds;
2496  eventMask = SOCKET_EVENT_TX_READY;
2497  break;
2498 
2499  default:
2500  //Set of sockets to be checked for errors
2501  fds = exceptfds;
2502  eventMask = SOCKET_EVENT_CLOSED;
2503  break;
2504  }
2505 
2506  //Each descriptor is optional and may be omitted
2507  if(fds != NULL)
2508  {
2509  //Parse the current set of sockets
2510  for(j = 0; j < fds->fd_count; j++)
2511  {
2512  //Get the descriptor associated with the current entry
2513  s = fds->fd_array[j];
2514  //Subscribe to the requested events
2515  socketRegisterEvents(&socketTable[s], &event, eventMask);
2516  }
2517  }
2518  }
2519 
2520  //Retrieve timeout value
2521  if(timeout != NULL)
2522  {
2523  time = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
2524  }
2525  else
2526  {
2527  time = INFINITE_DELAY;
2528  }
2529 
2530  //Block the current task until an event occurs
2531  osWaitForEvent(&event, time);
2532 
2533  //Count the number of events in the signaled state
2534  n = 0;
2535 
2536  //Parse all the descriptor sets
2537  for(i = 0; i < 3; i++)
2538  {
2539  //Select the suitable descriptor set
2540  switch(i)
2541  {
2542  case 0:
2543  //Set of sockets to be checked for readability
2544  fds = readfds;
2545  eventMask = SOCKET_EVENT_RX_READY;
2546  break;
2547 
2548  case 1:
2549  //Set of sockets to be checked for writability
2550  fds = writefds;
2551  eventMask = SOCKET_EVENT_TX_READY;
2552  break;
2553 
2554  default:
2555  //Set of sockets to be checked for errors
2556  fds = exceptfds;
2557  eventMask = SOCKET_EVENT_CLOSED;
2558  break;
2559  }
2560 
2561  //Each descriptor is optional and may be omitted
2562  if(fds != NULL)
2563  {
2564  //Parse the current set of sockets
2565  for(j = 0; j < fds->fd_count; )
2566  {
2567  //Get the descriptor associated with the current entry
2568  s = fds->fd_array[j];
2569  //Retrieve event flags for the current socket
2570  eventFlags = socketGetEvents(&socketTable[s]);
2571  //Unsubscribe previously registered events
2573 
2574  //Event flag is set?
2575  if(eventFlags & eventMask)
2576  {
2577  //Track the number of events in the signaled state
2578  n++;
2579  //Jump to the next socket descriptor
2580  j++;
2581  }
2582  else
2583  {
2584  //Remove descriptor from the current set
2585  socketFdClr(fds, s);
2586  }
2587  }
2588  }
2589  }
2590 
2591  //Delete event object
2592  osDeleteEvent(&event);
2593  //Return the number of events in the signaled state
2594  return n;
2595 }
2596 
2597 
2598 /**
2599  * @brief Get system host name
2600  * @param[out] name Output buffer where to store the system host name
2601  * @param[in] len Length of the buffer, in bytes
2602  * @return On success, zero is returned. On error, -1 is returned
2603  **/
2604 
2606 {
2607  size_t n;
2608  NetInterface *interface;
2609 
2610  //Check parameter
2611  if(name == NULL)
2612  {
2613  //Report an error
2615  return -1;
2616  }
2617 
2618  //Select the default network interface
2619  interface = netGetDefaultInterface();
2620 
2621  //Retrieve the length of the host name
2622  n = osStrlen(interface->hostname);
2623 
2624  //Make sure the buffer is large enough to hold the string
2625  if(len <= n)
2626  {
2627  //Report an error
2629  return -1;
2630  }
2631 
2632  //Copy the host name
2633  osStrcpy(name, interface->hostname);
2634 
2635  //Successful processing
2636  return 0;
2637 }
2638 
2639 
2640 /**
2641  * @brief Host name resolution
2642  * @param[in] name Name of the host to resolve
2643  * @return Pointer to the hostent structure or a NULL if an error occurs
2644  **/
2645 
2647 {
2648  int_t herrno;
2649  static HOSTENT result;
2650 
2651  //The hostent structure returned by the function resides in static
2652  //memory area
2653  return gethostbyname_r(name, &result, NULL, 0, &herrno);
2654 }
2655 
2656 
2657 /**
2658  * @brief Host name resolution (reentrant version)
2659  * @param[in] name Name of the host to resolve
2660  * @param[in] result Pointer to a hostent structure where the function can
2661  * store the host entry
2662  * @param[out] buf Pointer to a temporary work buffer
2663  * @param[in] buflen Length of the temporary work buffer
2664  * @param[out] h_errnop Pointer to a location where the function can store an
2665  * h_errno value if an error occurs
2666  * @return Pointer to the hostent structure or a NULL if an error occurs
2667  **/
2668 
2669 struct hostent *gethostbyname_r(const char_t *name, struct hostent *result,
2670  char_t *buf, size_t buflen, int_t *h_errnop)
2671 {
2672  error_t error;
2673  IpAddr ipAddr;
2674 
2675  //Check input parameters
2676  if(name == NULL || result == NULL)
2677  {
2678  //Report an error
2679  *h_errnop = NO_RECOVERY;
2680  return NULL;
2681  }
2682 
2683  //Resolve host address
2684  error = getHostByName(NULL, name, &ipAddr, 0);
2685  //Address resolution failed?
2686  if(error)
2687  {
2688  //Report an error
2689  *h_errnop = HOST_NOT_FOUND;
2690  return NULL;
2691  }
2692 
2693 #if (IPV4_SUPPORT == ENABLED)
2694  //IPv4 address?
2695  if(ipAddr.length == sizeof(Ipv4Addr))
2696  {
2697  //Set address family
2698  result->h_addrtype = AF_INET;
2699  result->h_length = sizeof(Ipv4Addr);
2700 
2701  //Copy IPv4 address
2702  ipv4CopyAddr(result->h_addr, &ipAddr.ipv4Addr);
2703  }
2704  else
2705 #endif
2706 #if (IPV6_SUPPORT == ENABLED)
2707  //IPv6 address?
2708  if(ipAddr.length == sizeof(Ipv6Addr))
2709  {
2710  //Set address family
2711  result->h_addrtype = AF_INET6;
2712  result->h_length = sizeof(Ipv6Addr);
2713 
2714  //Copy IPv6 address
2715  ipv6CopyAddr(result->h_addr, &ipAddr.ipv6Addr);
2716  }
2717  else
2718 #endif
2719  //Invalid address?
2720  {
2721  //Report an error
2722  *h_errnop = NO_ADDRESS;
2723  return NULL;
2724  }
2725 
2726  //Successful host name resolution
2727  *h_errnop = NETDB_SUCCESS;
2728 
2729  //Return a pointer to the hostent structure
2730  return result;
2731 }
2732 
2733 
2734 /**
2735  * @brief Convert host and service names to socket address
2736  * @param[in] node Host name or numerical network address
2737  * @param[in] service Service name or decimal number
2738  * @param[in] hints Criteria for selecting the socket address structures
2739  * @param[out] res Dynamically allocated list of socket address structures
2740  * @return On success, zero is returned. On error, a non-zero value is returned
2741  **/
2742 
2743 int_t getaddrinfo(const char_t *node, const char_t *service,
2744  const struct addrinfo *hints, struct addrinfo **res)
2745 {
2746  error_t error;
2747  size_t n;
2748  char_t *p;
2749  uint_t flags;
2750  uint32_t port;
2751  IpAddr ipAddr;
2752  ADDRINFO h;
2753  ADDRINFO *ai;
2754 
2755  //Check whether both node and service name are NULL
2756  if(node == NULL && service == NULL)
2757  return EAI_NONAME;
2758 
2759  //The hints argument is optional
2760  if(hints != NULL)
2761  {
2762  //If hints is not NULL, it points to an addrinfo structure that specifies
2763  //criteria that limit the set of socket addresses that will be returned
2764  h = *hints;
2765  }
2766  else
2767  {
2768  //If hints is NULL, then default criteria are used
2769  osMemset(&h, 0, sizeof(ADDRINFO));
2770  h.ai_family = AF_UNSPEC;
2771  h.ai_socktype = 0;
2772  h.ai_protocol = 0;
2773  h.ai_flags = 0;
2774  }
2775 
2776  //The user may provide a hint to choose between IPv4 and IPv6
2777  if(h.ai_family == AF_UNSPEC)
2778  {
2779  //The value AF_UNSPEC indicates that function should return socket
2780  //addresses for any address family (either IPv4 or IPv6)
2781  flags = 0;
2782  }
2783 #if (IPV4_SUPPORT == ENABLED)
2784  else if(h.ai_family == AF_INET)
2785  {
2786  //The value AF_INET indicates that the function should return an IPv4
2787  //address
2789  }
2790 #endif
2791 #if (IPV6_SUPPORT == ENABLED)
2792  else if(h.ai_family == AF_INET6)
2793  {
2794  //The value AF_INET6 indicates that the function should return an IPv6
2795  //address
2797  }
2798 #endif
2799  else
2800  {
2801  //The requested address family is not supported
2802  return EAI_FAMILY;
2803  }
2804 
2805  //Check whether a host name or numerical network address is specified
2806  if(node != NULL)
2807  {
2808  //If the AI_NUMERICHOST flag, then node must be a numerical network
2809  //address
2810  if((h.ai_flags & AI_NUMERICHOST) != 0)
2811  {
2812  //Convert the string representation to a binary IP address
2813  error = ipStringToAddr(node, &ipAddr);
2814  }
2815  else
2816  {
2817  //Resolve host address
2818  error = getHostByName(NULL, node, &ipAddr, flags);
2819  }
2820 
2821  //Check status code
2822  if(error == NO_ERROR)
2823  {
2824  //Successful host name resolution
2825  }
2826  else if(error == ERROR_IN_PROGRESS)
2827  {
2828  //Host name resolution is in progress
2829  return EAI_AGAIN;
2830  }
2831  else
2832  {
2833  //Permanent failure indication
2834  return EAI_FAIL;
2835  }
2836  }
2837  else
2838  {
2839  //Check flags
2840  if((h.ai_flags & AI_PASSIVE) != 0)
2841  {
2842 #if (IPV4_SUPPORT == ENABLED)
2843  //IPv4 address family?
2844  if(h.ai_family == AF_INET || h.ai_family == AF_UNSPEC)
2845  {
2846  ipAddr.length = sizeof(Ipv4Addr);
2847  ipAddr.ipv4Addr = IPV4_UNSPECIFIED_ADDR;
2848  }
2849  else
2850 #endif
2851 #if (IPV6_SUPPORT == ENABLED)
2852  //IPv6 address family?
2853  if(h.ai_family == AF_INET6 || h.ai_family == AF_UNSPEC)
2854  {
2855  ipAddr.length = sizeof(Ipv6Addr);
2856  ipAddr.ipv6Addr = IPV6_UNSPECIFIED_ADDR;
2857  }
2858  else
2859 #endif
2860  //Unknown address family?
2861  {
2862  //Report an error
2863  return EAI_ADDRFAMILY;
2864  }
2865  }
2866  else
2867  {
2868  //Invalid flags
2869  return EAI_BADFLAGS;
2870  }
2871  }
2872 
2873  //Only service names containing a numeric port number are supported
2874  port = osStrtoul(service, &p, 10);
2875  //Invalid service name?
2876  if(*p != '\0')
2877  {
2878  //The requested service is not available
2879  return EAI_SERVICE;
2880  }
2881 
2882 #if (IPV4_SUPPORT == ENABLED)
2883  //IPv4 address?
2884  if(ipAddr.length == sizeof(Ipv4Addr))
2885  {
2886  //Select the relevant socket family
2887  h.ai_family = AF_INET;
2888  //Get the length of the corresponding socket address
2889  n = sizeof(SOCKADDR_IN);
2890  }
2891  else
2892 #endif
2893 #if (IPV6_SUPPORT == ENABLED)
2894  //IPv6 address?
2895  if(ipAddr.length == sizeof(Ipv6Addr))
2896  {
2897  //Select the relevant socket family
2898  h.ai_family = AF_INET6;
2899  //Get the length of the corresponding socket address
2900  n = sizeof(SOCKADDR_IN6);
2901  }
2902  else
2903 #endif
2904  //Unknown address?
2905  {
2906  //Report an error
2907  return EAI_ADDRFAMILY;
2908  }
2909 
2910  //Allocate a memory buffer to hold the address information structure
2911  ai = osAllocMem(sizeof(ADDRINFO) + n);
2912  //Failed to allocate memory?
2913  if(ai == NULL)
2914  {
2915  //Out of memory
2916  return EAI_MEMORY;
2917  }
2918 
2919  //Initialize address information structure
2920  osMemset(ai, 0, sizeof(ADDRINFO) + n);
2921  ai->ai_family = h.ai_family;
2922  ai->ai_socktype = h.ai_socktype;
2923  ai->ai_protocol = h.ai_protocol;
2924  ai->ai_addr = (SOCKADDR *) ((uint8_t *) ai + sizeof(ADDRINFO));
2925  ai->ai_addrlen = n;
2926  ai->ai_next = NULL;
2927 
2928 #if (IPV4_SUPPORT == ENABLED)
2929  //IPv4 address?
2930  if(ipAddr.length == sizeof(Ipv4Addr))
2931  {
2932  //Point to the IPv4 address information
2933  SOCKADDR_IN *sa = (SOCKADDR_IN *) ai->ai_addr;
2934 
2935  //Set address family and port number
2936  sa->sin_family = AF_INET;
2937  sa->sin_port = htons(port);
2938 
2939  //Copy IPv4 address
2940  sa->sin_addr.s_addr = ipAddr.ipv4Addr;
2941  }
2942  else
2943 #endif
2944 #if (IPV6_SUPPORT == ENABLED)
2945  //IPv6 address?
2946  if(ipAddr.length == sizeof(Ipv6Addr))
2947  {
2948  //Point to the IPv6 address information
2949  SOCKADDR_IN6 *sa = (SOCKADDR_IN6 *) ai->ai_addr;
2950 
2951  //Set address family and port number
2952  sa->sin6_family = AF_INET6;
2953  sa->sin6_port = htons(port);
2954  sa->sin6_flowinfo = 0;
2955  sa->sin6_scope_id = 0;
2956 
2957  //Copy IPv6 address
2958  ipv6CopyAddr(sa->sin6_addr.s6_addr, &ipAddr.ipv6Addr);
2959  }
2960  else
2961 #endif
2962  //Unknown address?
2963  {
2964  //Clean up side effects
2965  osFreeMem(ai);
2966  //Report an error
2967  return EAI_ADDRFAMILY;
2968  }
2969 
2970  //Return a pointer to the allocated address information
2971  *res = ai;
2972 
2973  //Successful processing
2974  return 0;
2975 }
2976 
2977 
2978 /**
2979  * @brief Free socket address structures
2980  * @param[in] res Dynamically allocated list of socket address structures
2981  **/
2982 
2984 {
2985  ADDRINFO *next;
2986 
2987  //Free the list of socket address structures
2988  while(res != NULL)
2989  {
2990  //Get next entry
2991  next = res->ai_next;
2992  //Free current socket address structure
2993  osFreeMem(res);
2994  //Point to the next entry
2995  res = next;
2996  }
2997 }
2998 
2999 
3000 /**
3001  * @brief Convert a socket address to a corresponding host and service
3002  * @param[in] addr Generic socket address structure
3003  * @param[in] addrlen Length in bytes of the address
3004  * @param[out] host Output buffer where to store the host name
3005  * @param[in] hostlen Length of the host name buffer, in bytes
3006  * @param[out] serv Output buffer where to store the service name
3007  * @param[in] servlen Length of the service name buffer, in bytes
3008  * @param[in] flags Set of flags that influences the behavior of this function
3009  * @return On success, zero is returned. On error, a non-zero value is returned
3010  **/
3011 
3012 int_t getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
3013  char_t *host, size_t hostlen, char_t *serv, size_t servlen, int flags)
3014 {
3015  uint16_t port;
3016 
3017  //At least one of hostname or service name must be requested
3018  if(host == NULL && serv == NULL)
3019  return EAI_NONAME;
3020 
3021 #if (IPV4_SUPPORT == ENABLED)
3022  //IPv4 address?
3023  if(addr->sa_family == AF_INET &&
3024  addrlen >= (socklen_t) sizeof(SOCKADDR_IN))
3025  {
3026  SOCKADDR_IN *sa;
3027  Ipv4Addr ipv4Addr;
3028 
3029  //Point to the IPv4 address information
3030  sa = (SOCKADDR_IN *) addr;
3031 
3032  //The caller can specify that no host name is required
3033  if(host != NULL)
3034  {
3035  //Make sure the buffer is large enough to hold the host name
3036  if(hostlen < 16)
3037  return EAI_OVERFLOW;
3038 
3039  //Copy the binary representation of the IPv4 address
3040  ipv4CopyAddr(&ipv4Addr, &sa->sin_addr.s_addr);
3041  //Convert the IPv4 address to dot-decimal notation
3042  ipv4AddrToString(ipv4Addr, host);
3043  }
3044 
3045  //Retrieve port number
3046  port = ntohs(sa->sin_port);
3047  }
3048  else
3049 #endif
3050 #if (IPV6_SUPPORT == ENABLED)
3051  //IPv6 address?
3052  if(addr->sa_family == AF_INET6 &&
3053  addrlen >= (socklen_t) sizeof(SOCKADDR_IN6))
3054  {
3055  SOCKADDR_IN6 *sa;
3056  Ipv6Addr ipv6Addr;
3057 
3058  //Point to the IPv6 address information
3059  sa = (SOCKADDR_IN6 *) addr;
3060 
3061  //The caller can specify that no host name is required
3062  if(host != NULL)
3063  {
3064  //Make sure the buffer is large enough to hold the host name
3065  if(hostlen < 40)
3066  return EAI_OVERFLOW;
3067 
3068  //Copy the binary representation of the IPv6 address
3069  ipv6CopyAddr(&ipv6Addr, sa->sin6_addr.s6_addr);
3070  //Convert the IPv6 address to string representation
3071  ipv6AddrToString(&ipv6Addr, host);
3072  }
3073 
3074  //Retrieve port number
3075  port = ntohs(sa->sin6_port);
3076  }
3077  else
3078 #endif
3079  //Invalid address?
3080  {
3081  //The address family was not recognized, or the address length was
3082  //invalid for the specified family
3083  return EAI_FAMILY;
3084  }
3085 
3086  //The caller can specify that no service name is required
3087  if(serv != NULL)
3088  {
3089  //Make sure the buffer is large enough to hold the service name
3090  if(servlen < 6)
3091  return EAI_OVERFLOW;
3092 
3093  //Convert the port number to string representation
3094  osSprintf(serv, "%" PRIu16, ntohs(port));
3095  }
3096 
3097  //Successful processing
3098  return 0;
3099 }
3100 
3101 
3102 /**
3103  * @brief Convert a dot-decimal string into binary data in network byte order
3104  * @param[in] cp NULL-terminated string representing the IPv4 address
3105  * @return Binary data in network byte order
3106  **/
3107 
3109 {
3110 #if (IPV4_SUPPORT == ENABLED)
3111  error_t error;
3112  Ipv4Addr ipv4Addr;
3113 
3114  //Convert a dot-decimal string to a binary IPv4 address
3115  error = ipv4StringToAddr(cp, &ipv4Addr);
3116 
3117  //Check status code
3118  if(error)
3119  {
3120  //The input is invalid
3121  return INADDR_NONE;
3122  }
3123  else
3124  {
3125  //Return the binary representation
3126  return ipv4Addr;
3127  }
3128 #else
3129  //IPv4 is not implemented
3130  return INADDR_NONE;
3131 #endif
3132 }
3133 
3134 
3135 /**
3136  * @brief Convert a dot-decimal string into binary form
3137  * @param[in] cp NULL-terminated string representing the IPv4 address
3138  * @param[out] inp Binary data in network byte order
3139  * @return The function returns non-zero if the address is valid, zero if not
3140  **/
3141 
3142 int_t inet_aton(const char_t *cp, struct in_addr *inp)
3143 {
3144 #if (IPV4_SUPPORT == ENABLED)
3145  error_t error;
3146  Ipv4Addr ipv4Addr;
3147 
3148  //Convert a dot-decimal string to a binary IPv4 address
3149  error = ipv4StringToAddr(cp, &ipv4Addr);
3150 
3151  //Check status code
3152  if(error)
3153  {
3154  //The input is invalid
3155  return 0;
3156  }
3157  else
3158  {
3159  //Copy the binary representation of the IPv4 address
3160  inp->s_addr = ipv4Addr;
3161  //The conversion succeeded
3162  return 1;
3163  }
3164 #else
3165  //IPv4 is not implemented
3166  return 0;
3167 #endif
3168 }
3169 
3170 
3171 /**
3172  * @brief Convert a binary IPv4 address to dot-decimal notation
3173  * @param[in] in Binary representation of the IPv4 address
3174  * @return Pointer to the formatted string
3175  **/
3176 
3177 const char_t *inet_ntoa(struct in_addr in)
3178 {
3179  static char_t buf[16];
3180 
3181  //The string returned by the function resides in static memory area
3182  return inet_ntoa_r(in, buf, sizeof(buf));
3183 }
3184 
3185 
3186 /**
3187  * @brief Convert a binary IPv4 address to dot-decimal notation (reentrant version)
3188  * @param[in] in Binary representation of the IPv4 address
3189  * @param[out] buf Pointer to the buffer where to format the string
3190  * @param[in] buflen Number of bytes available in the buffer
3191  * @return Pointer to the formatted string
3192  **/
3193 
3194 const char_t *inet_ntoa_r(struct in_addr in, char_t *buf, socklen_t buflen)
3195 {
3196  //Properly terminate the string
3197  buf[0] = '\0';
3198 
3199 #if (IPV4_SUPPORT == ENABLED)
3200  //Check the length of the buffer
3201  if(buflen >= 16)
3202  {
3203  //Convert the binary IPv4 address to dot-decimal notation
3204  ipv4AddrToString(in.s_addr, buf);
3205  }
3206 #endif
3207 
3208  //Return a pointer to the formatted string
3209  return buf;
3210 }
3211 
3212 
3213 /**
3214  * @brief Convert an IPv4 or IPv6 address from text to binary form
3215  * @param[in] af Address family
3216  * @param[in] src NULL-terminated string representing the IP address
3217  * @param[out] dst Binary representation of the IP address
3218  * @return The function returns 1 on success. 0 is returned if the address
3219  * is not valid. If the address family is not valid, -1 is returned
3220  **/
3221 
3222 int_t inet_pton(int_t af, const char_t *src, void *dst)
3223 {
3224  error_t error;
3225 
3226 #if (IPV4_SUPPORT == ENABLED)
3227  //IPv4 address?
3228  if(af == AF_INET)
3229  {
3230  Ipv4Addr ipv4Addr;
3231 
3232  //Convert the IPv4 address from text to binary form
3233  error = ipv4StringToAddr(src, &ipv4Addr);
3234 
3235  //Check status code
3236  if(error)
3237  {
3238  //The input is invalid
3239  return 0;
3240  }
3241  else
3242  {
3243  //Copy the binary representation of the IPv4 address
3244  ipv4CopyAddr(dst, &ipv4Addr);
3245  //The conversion succeeded
3246  return 1;
3247  }
3248  }
3249  else
3250 #endif
3251 #if (IPV6_SUPPORT == ENABLED)
3252  //IPv6 address?
3253  if(af == AF_INET6)
3254  {
3255  Ipv6Addr ipv6Addr;
3256 
3257  //Convert the IPv6 address from text to binary form
3258  error = ipv6StringToAddr(src, &ipv6Addr);
3259 
3260  //Check status code
3261  if(error)
3262  {
3263  //The input is invalid
3264  return 0;
3265  }
3266  else
3267  {
3268  //Copy the binary representation of the IPv6 address
3269  ipv6CopyAddr(dst, &ipv6Addr);
3270  //The conversion succeeded
3271  return 1;
3272  }
3273  }
3274  else
3275 #endif
3276  //Invalid address family?
3277  {
3278  //Report an error
3279  return -1;
3280  }
3281 }
3282 
3283 
3284 /**
3285  * @brief Convert an IPv4 or IPv6 address from binary to text
3286  * @param[in] af Address family
3287  * @param[in] src Binary representation of the IP address
3288  * @param[out] dst NULL-terminated string representing the IP address
3289  * @param[in] size Number of bytes available in the buffer
3290  * @return On success, the function returns a pointer to the formatted string.
3291  * NULL is returned if there was an error
3292  **/
3293 
3294 const char_t *inet_ntop(int_t af, const void *src, char_t *dst, socklen_t size)
3295 {
3296 #if (IPV4_SUPPORT == ENABLED)
3297  //IPv4 address?
3298  if(af == AF_INET && size >= INET_ADDRSTRLEN)
3299  {
3300  Ipv4Addr ipv4Addr;
3301 
3302  //Copy the binary representation of the IPv4 address
3303  ipv4CopyAddr(&ipv4Addr, src);
3304 
3305  //Convert the IPv4 address to dot-decimal notation
3306  return ipv4AddrToString(ipv4Addr, dst);
3307  }
3308  else
3309 #endif
3310 #if (IPV6_SUPPORT == ENABLED)
3311  //IPv6 address?
3312  if(af == AF_INET6 && size >= INET6_ADDRSTRLEN)
3313  {
3314  Ipv6Addr ipv6Addr;
3315 
3316  //Copy the binary representation of the IPv6 address
3317  ipv6CopyAddr(&ipv6Addr, src);
3318 
3319  //Convert the IPv6 address to string representation
3320  return ipv6AddrToString(&ipv6Addr, dst);
3321  }
3322  else
3323 #endif
3324  //Invalid address family?
3325  {
3326  //Report an error
3327  return NULL;
3328  }
3329 }
3330 
3331 #endif
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:3108
int_t sendmsg(int_t s, struct msghdr *msg, int_t flags)
Send a message.
Definition: bsd_socket.c:661
int_t fcntl(int_t s, int_t cmd, int_t arg)
Perform specific operation.
Definition: bsd_socket.c:2287
int_t recvmsg(int_t s, struct msghdr *msg, int_t flags)
Receive a message.
Definition: bsd_socket.c:1115
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 listen(int_t s, int_t backlog)
Place a socket in the listening state.
Definition: bsd_socket.c:322
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:3294
int_t gethostname(char_t *name, size_t len)
Get system host name.
Definition: bsd_socket.c:2605
int_t ioctlsocket(int_t s, uint32_t cmd, void *arg)
Control the I/O mode of a socket.
Definition: bsd_socket.c:2187
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
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 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:2743
void freeaddrinfo(struct addrinfo *res)
Free socket address structures.
Definition: bsd_socket.c:2983
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
const struct in6_addr in6addr_loopback
Definition: bsd_socket.c:52
const char_t * inet_ntoa(struct in_addr in)
Convert a binary IPv4 address to dot-decimal notation.
Definition: bsd_socket.c:3177
int_t getsockname(int_t s, struct sockaddr *addr, socklen_t *addrlen)
Retrieves the local name for a socket.
Definition: bsd_socket.c:1476
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:3012
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
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 bind(int_t s, const struct sockaddr *addr, socklen_t addrlen)
Associate a local address with a socket.
Definition: bsd_socket.c:107
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
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:3222
struct hostent * gethostbyname(const char_t *name)
Host name resolution.
Definition: bsd_socket.c:2646
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
int_t closesocket(int_t s)
The closesocket function closes an existing socket.
Definition: bsd_socket.c:2381
int_t inet_aton(const char_t *cp, struct in_addr *inp)
Convert a dot-decimal string into binary form.
Definition: bsd_socket.c:3142
int_t shutdown(int_t s, int_t how)
The shutdown function disables sends or receives on a socket.
Definition: bsd_socket.c:2345
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
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:2669
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:2423
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:1939
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:3194
const struct in6_addr in6addr_any
Definition: bsd_socket.c:49
BSD socket API.
int_t socklen_t
Length type.
Definition: bsd_socket.h:282
#define IPV6_RECVTCLASS
Definition: bsd_socket.h:177
#define EAI_OVERFLOW
Definition: bsd_socket.h:237
#define F_GETFL
Definition: bsd_socket.h:199
#define IPPROTO_IPV6
Definition: bsd_socket.h:100
#define SO_ERROR
Definition: bsd_socket.h:146
#define SO_KEEPALIVE
Definition: bsd_socket.h:138
#define EAI_NONAME
Definition: bsd_socket.h:233
#define MSG_WAITALL
Definition: bsd_socket.h:124
#define SO_RCVBUF
Definition: bsd_socket.h:143
#define TCP_KEEPCNT
Definition: bsd_socket.h:185
void socketFdClr(fd_set *fds, int_t s)
Remove a descriptor from an existing set.
#define CMSG_LEN(len)
Definition: bsd_socket.h:518
#define EAI_FAMILY
Definition: bsd_socket.h:230
struct sockaddr_in SOCKADDR_IN
IPv4 address information.
#define AI_NUMERICHOST
Definition: bsd_socket.h:208
#define EINVAL
Definition: bsd_socket.h:244
#define IPV6_MULTICAST_LOOP
Definition: bsd_socket.h:169
#define EAI_MEMORY
Definition: bsd_socket.h:231
#define IPV6_DONTFRAG
Definition: bsd_socket.h:176
#define O_NONBLOCK
Definition: bsd_socket.h:203
#define INADDR_NONE
Definition: bsd_socket.h:264
#define IP_RECVTTL
Definition: bsd_socket.h:156
#define EFAULT
Definition: bsd_socket.h:243
#define EAI_BADFLAGS
Definition: bsd_socket.h:228
#define IPV6_HOPLIMIT
Definition: bsd_socket.h:175
#define ENAMETOOLONG
Definition: bsd_socket.h:247
struct sockaddr_in6 SOCKADDR_IN6
IPv6 address information.
#define IPV6_V6ONLY
Definition: bsd_socket.h:172
#define EWOULDBLOCK
Definition: bsd_socket.h:242
#define IPV6_UNICAST_HOPS
Definition: bsd_socket.h:166
#define IP_TTL
Definition: bsd_socket.h:153
#define IP_MULTICAST_LOOP
Definition: bsd_socket.h:160
#define SO_REUSEADDR
Definition: bsd_socket.h:137
#define ETIMEDOUT
Definition: bsd_socket.h:246
#define CMSG_DATA(cmsg)
Definition: bsd_socket.h:515
#define SOCKET_SUCCESS
Definition: bsd_socket.h:222
#define MSG_PEEK
Definition: bsd_socket.h:120
#define SOCKET_ERROR
Definition: bsd_socket.h:223
#define IPV6_TCLASS
Definition: bsd_socket.h:178
#define FIONWRITE
Definition: bsd_socket.h:195
#define FIONSPACE
Definition: bsd_socket.h:196
#define CMSG_SPACE(len)
Definition: bsd_socket.h:517
#define IPV6_DROP_MEMBERSHIP
Definition: bsd_socket.h:171
#define EINPROGRESS
Definition: bsd_socket.h:245
#define EAI_AGAIN
Definition: bsd_socket.h:227
#define IP_TOS
Definition: bsd_socket.h:152
#define SO_TYPE
Definition: bsd_socket.h:147
#define TCP_NODELAY
Definition: bsd_socket.h:181
#define IP_RECVTOS
Definition: bsd_socket.h:157
#define IPV6_MULTICAST_HOPS
Definition: bsd_socket.h:168
#define IP_ADD_MEMBERSHIP
Definition: bsd_socket.h:161
#define NO_RECOVERY
Definition: bsd_socket.h:260
#define SOL_SOCKET
Definition: bsd_socket.h:112
#define INET_ADDRSTRLEN
Definition: bsd_socket.h:267
#define IPPROTO_TCP
Definition: bsd_socket.h:98
#define IP_MULTICAST_IF
Definition: bsd_socket.h:158
#define MSG_DONTROUTE
Definition: bsd_socket.h:121
uint32_t in_addr_t
IPv4 address.
Definition: bsd_socket.h:289
#define AF_INET
Definition: bsd_socket.h:79
#define IP_DONTFRAG
Definition: bsd_socket.h:163
#define TCP_KEEPINTVL
Definition: bsd_socket.h:184
#define AF_INET6
Definition: bsd_socket.h:80
#define MSG_CTRUNC
Definition: bsd_socket.h:122
#define TCP_KEEPIDLE
Definition: bsd_socket.h:183
#define AF_PACKET
Definition: bsd_socket.h:81
#define EAI_ADDRFAMILY
Definition: bsd_socket.h:226
#define BSD_SOCKET_SET_ERRNO(e)
Definition: bsd_socket.h:50
#define MSG_DONTWAIT
Definition: bsd_socket.h:123
#define HOST_NOT_FOUND
Definition: bsd_socket.h:258
#define SO_SNDTIMEO
Definition: bsd_socket.h:144
#define IP_PKTINFO
Definition: bsd_socket.h:155
#define NETDB_SUCCESS
Definition: bsd_socket.h:257
#define IP_MULTICAST_TTL
Definition: bsd_socket.h:159
#define IPPROTO_IP
Definition: bsd_socket.h:95
#define FIONREAD
Definition: bsd_socket.h:194
#define IPV6_PKTINFO
Definition: bsd_socket.h:173
#define EAI_SERVICE
Definition: bsd_socket.h:234
#define ENOPROTOOPT
Definition: bsd_socket.h:249
#define EAI_FAIL
Definition: bsd_socket.h:229
#define SO_BROADCAST
Definition: bsd_socket.h:140
struct addrinfo ADDRINFO
Information about address of a service provider.
#define IP_DROP_MEMBERSHIP
Definition: bsd_socket.h:162
#define IPV6_ADD_MEMBERSHIP
Definition: bsd_socket.h:170
#define AF_UNSPEC
Definition: bsd_socket.h:78
#define NO_ADDRESS
Definition: bsd_socket.h:261
#define AI_PASSIVE
Definition: bsd_socket.h:206
#define TCP_MAXSEG
Definition: bsd_socket.h:182
#define ENOTCONN
Definition: bsd_socket.h:252
#define F_SETFL
Definition: bsd_socket.h:200
#define SO_RCVTIMEO
Definition: bsd_socket.h:145
#define FIONBIO
Definition: bsd_socket.h:193
#define SO_SNDBUF
Definition: bsd_socket.h:142
#define INET6_ADDRSTRLEN
Definition: bsd_socket.h:270
#define IPV6_MULTICAST_IF
Definition: bsd_socket.h:167
#define IPV6_RECVHOPLIMIT
Definition: bsd_socket.h:174
void socketTranslateErrorCode(Socket *socket, error_t errorCode)
Translate error code.
void socketSetErrnoCode(Socket *socket, uint_t errnoCode)
Set BSD error code.
Helper function for BSD socket API.
int_t socketSetSoRcvBufOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_RCVBUF option.
int_t socketGetSoKeepAliveOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_KEEPALIVE option.
int_t socketSetIpDropMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_DROP_MEMBERSHIP option.
int_t socketGetIpMulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_LOOP option.
int_t socketSetIpv6DropMembershipOption(Socket *socket, const struct ipv6_mreq *optval, socklen_t optlen)
Set IPV6_DROP_MEMBERSHIP option.
int_t socketSetSoBroadcastOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_BROADCAST option.
int_t socketGetIpv6RecvHopLimitOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_RECVHOPLIMIT option.
int_t socketSetTcpKeepCntOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPCNT option.
int_t socketGetTcpKeepIdleOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPIDLE option.
int_t socketGetIpRecvTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_RECVTTL option.
int_t socketGetIpv6RecvTrafficClassOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_RECVTCLASS option.
int_t socketGetIpTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TTL option.
int_t socketGetIpv6DontFragOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_DONTFRAG option.
int_t socketSetIpv6RecvHopLimitOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_RECVHOPLIMIT option.
int_t socketSetIpDontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_DONTFRAG option.
int_t socketGetIpv6PktInfoOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_PKTINFO option.
int_t socketGetIpv6MulticastLoopOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_MULTICAST_LOOP option.
int_t socketGetIpPktInfoOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_PKTINFO option.
int_t socketSetIpAddMembershipOption(Socket *socket, const struct ip_mreq *optval, socklen_t optlen)
Set IP_ADD_MEMBERSHIP option.
int_t socketSetIpTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_TTL option.
int_t socketSetSoSndBufOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_SNDBUF option.
int_t socketGetIpMulticastTtlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_MULTICAST_TTL option.
int_t socketGetSoTypeOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_TYPE option.
int_t socketGetTcpKeepIntvlOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPINTVL option.
int_t socketGetSoErrorOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_ERROR option.
int_t socketGetIpTosOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_TOS option.
int_t socketGetTcpMaxSegOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_MAXSEG option.
int_t socketSetIpRecvTosOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_RECVTOS option.
int_t socketGetTcpKeepCntOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_KEEPCNT option.
int_t socketSetIpv6TrafficClassOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_TCLASS option.
int_t socketGetIpDontFragOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_DONTFRAG option.
int_t socketGetIpv6TrafficClassOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_TCLASS option.
int_t socketSetIpv6AddMembershipOption(Socket *socket, const struct ipv6_mreq *optval, socklen_t optlen)
Set IPV6_ADD_MEMBERSHIP option.
int_t socketSetIpv6MulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_MULTICAST_LOOP option.
int_t socketSetIpPktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_PKTINFO option.
int_t socketSetIpv6RecvTrafficClassOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_RECVTCLASS option.
int_t socketSetIpMulticastTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_MULTICAST_TTL option.
int_t socketGetIpv6OnlyOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_V6ONLY option.
int_t socketSetIpv6MulticastHopsOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_MULTICAST_HOPS option.
int_t socketSetIpMulticastIfOption(Socket *socket, const struct in_addr *optval, socklen_t optlen)
Set IP_MULTICAST_IF option.
int_t socketSetIpMulticastLoopOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_MULTICAST_LOOP option.
int_t socketSetIpv6UnicastHopsOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_UNICAST_HOPS option.
int_t socketGetSoRcvBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_RCVBUF option.
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 socketGetSoRcvTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_RCVTIMEO option.
int_t socketSetSoKeepAliveOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set SO_KEEPALIVE option.
int_t socketGetIpv6UnicastHopsOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_UNICAST_HOPS option.
int_t socketSetIpv6MulticastIfOption(Socket *socket, const struct in_addr *optval, socklen_t optlen)
Set IPV6_MULTICAST_IF option.
int_t socketSetIpv6PktInfoOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_PKTINFO option.
int_t socketGetSoBroadcastOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_BROADCAST option.
int_t socketSetSoSndTimeoOption(Socket *socket, const struct timeval *optval, socklen_t optlen)
Set SO_SNDTIMEO option.
int_t socketGetSoReuseAddrOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_REUSEADDR option.
int_t socketGetSoSndTimeoOption(Socket *socket, struct timeval *optval, socklen_t *optlen)
Get SO_SNDTIMEO option.
int_t socketSetIpv6DontFragOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_DONTFRAG option.
int_t socketSetIpTosOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_TOS option.
int_t socketSetIpRecvTtlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IP_RECVTTL option.
int_t socketGetIpRecvTosOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IP_RECVTOS option.
int_t socketSetTcpMaxSegOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_MAXSEG option.
int_t socketGetIpv6MulticastHopsOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get IPV6_MULTICAST_HOPS option.
int_t socketGetSoSndBufOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get SO_SNDBUF option.
int_t socketGetTcpNoDelayOption(Socket *socket, int_t *optval, socklen_t *optlen)
Get TCP_NODELAY option.
int_t socketSetTcpKeepIdleOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPIDLE option.
int_t socketSetIpv6OnlyOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set IPV6_V6ONLY option.
int_t socketSetTcpKeepIntvlOption(Socket *socket, const int_t *optval, socklen_t optlen)
Set TCP_KEEPINTVL option.
int_t socketSetSoRcvTimeoOption(Socket *socket, const struct timeval *optval, socklen_t optlen)
Set SO_RCVTIMEO option.
BSD socket options.
uint8_t message[]
Definition: chap.h:154
uint8_t type
Definition: coap_common.h:176
signed int int_t
Definition: compiler_port.h:49
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
#define htons(value)
Definition: cpu_endian.h:413
#define ntohs(value)
Definition: cpu_endian.h:421
Debugging facilities.
uint8_t n
uint32_t time
uint16_t port
Definition: dns_common.h:267
error_t
Error codes.
Definition: error.h:43
@ ERROR_IN_PROGRESS
Definition: error.h:213
@ ERROR_TIMEOUT
Definition: error.h:95
@ ERROR_END_OF_STREAM
Definition: error.h:210
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
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:794
Ipv4Addr ipAddr
Definition: ipcp.h:105
char_t * ipv4AddrToString(Ipv4Addr ipAddr, char_t *str)
Convert a binary IPv4 address to dot-decimal notation.
Definition: ipv4.c:1636
error_t ipv4StringToAddr(const char_t *str, Ipv4Addr *ipAddr)
Convert a dot-decimal string to a binary IPv4 address.
Definition: ipv4.c:1547
#define ipv4CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv4.h:148
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:267
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:110
uint8_t protocol
Definition: ipv4.h:296
#define IPV4_SUPPORT
Definition: ipv4.h:48
uint16_t next
Definition: ipv4_frag.h:106
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:2221
char_t * ipv6AddrToString(const Ipv6Addr *ipAddr, char_t *str)
Convert a binary IPv6 address to a string representation.
Definition: ipv6.c:2376
const Ipv6Addr IPV6_UNSPECIFIED_ADDR
Definition: ipv6.c:65
Ipv6Addr
Definition: ipv6.h:251
#define ipv6CompAddr(ipAddr1, ipAddr2)
Definition: ipv6.h:120
#define ipv6CopyAddr(destIpAddr, srcIpAddr)
Definition: ipv6.h:116
Ipv4Addr addr
Definition: nbns_common.h:123
uint8_t h
Definition: ndp.h:302
uint8_t s
Definition: ndp.h:345
uint8_t p
Definition: ndp.h:300
NetInterface * netGetDefaultInterface(void)
Get default network interface.
Definition: net.c:470
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netMutex
Definition: net_legacy.h:195
#define osMemset(p, value, length)
Definition: os_port.h:135
#define osStrlen(s)
Definition: os_port.h:165
#define osSprintf(dest,...)
Definition: os_port.h:231
#define ENABLED
Definition: os_port.h:37
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define INFINITE_DELAY
Definition: os_port.h:75
#define osStrcpy(s1, s2)
Definition: os_port.h:207
#define osStrtoul(s, endptr, base)
Definition: os_port.h:249
bool_t osWaitForEvent(OsEvent *event, systime_t timeout)
Wait until the specified event is in the signaled state.
void osDeleteEvent(OsEvent *event)
Delete an event object.
__weak_func void * osAllocMem(size_t size)
Allocate a memory block.
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
__weak_func void osFreeMem(void *p)
Release a previously allocated memory block.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
bool_t osCreateEvent(OsEvent *event)
Create an event object.
uint32_t systime_t
System time.
const uint8_t res[]
char_t name[]
Socket * socketAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a socket.
Definition: socket.c:912
const SocketMsg SOCKET_DEFAULT_MSG
Definition: socket.c:52
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:1174
error_t socketBind(Socket *socket, const IpAddr *localIpAddr, uint16_t localPort)
Associate a local address with a socket.
Definition: socket.c:778
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:967
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:1152
error_t socketListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: socket.c:875
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:1717
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
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:946
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1517
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:1480
Socket socketTable[SOCKET_MAX_COUNT]
Definition: socket.c:49
error_t socketReceiveMsg(Socket *socket, SocketMsg *message, uint_t flags)
Receive a message from a connectionless socket.
Definition: socket.c:1354
error_t socketSendMsg(Socket *socket, const SocketMsg *message, uint_t flags)
Send a message to a connectionless socket.
Definition: socket.c:1094
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:811
Socket API.
@ SOCKET_FLAG_NO_DELAY
Definition: socket.h:133
@ SOCKET_FLAG_WAIT_ALL
Definition: socket.h:128
@ SOCKET_FLAG_PEEK
Definition: socket.h:126
@ SOCKET_FLAG_DONT_ROUTE
Definition: socket.h:127
@ SOCKET_FLAG_DONT_WAIT
Definition: socket.h:129
@ SOCKET_OPTION_IPV4_RECV_TTL
Definition: socket.h:188
@ SOCKET_OPTION_IPV6_RECV_HOP_LIMIT
Definition: socket.h:194
@ SOCKET_OPTION_TCP_NO_DELAY
Definition: socket.h:195
@ SOCKET_OPTION_IPV6_RECV_TRAFFIC_CLASS
Definition: socket.h:193
@ SOCKET_OPTION_IPV4_PKT_INFO
Definition: socket.h:186
@ SOCKET_OPTION_IPV6_PKT_INFO
Definition: socket.h:192
@ SOCKET_OPTION_IPV4_RECV_TOS
Definition: socket.h:187
@ SOCKET_TYPE_RAW_ETH
Definition: socket.h:88
#define Socket
Definition: socket.h:36
@ HOST_TYPE_IPV6
Definition: socket.h:207
@ HOST_TYPE_IPV4
Definition: socket.h:206
#define SOCKET_MAX_COUNT
Definition: socket.h:46
@ SOCKET_EVENT_TX_READY
Definition: socket.h:165
@ SOCKET_EVENT_RX_READY
Definition: socket.h:169
@ SOCKET_EVENT_CLOSED
Definition: socket.h:164
void socketRegisterEvents(Socket *socket, OsEvent *event, uint_t eventMask)
Subscribe to the specified socket events.
Definition: socket_misc.c:195
void socketUnregisterEvents(Socket *socket)
Unsubscribe previously registered events.
Definition: socket_misc.c:250
uint_t socketGetEvents(Socket *socket)
Retrieve event flags for a specified socket.
Definition: socket_misc.c:273
Helper functions for sockets.
IP network address.
Definition: ip.h:79
Event object.
Message and ancillary data.
Definition: socket.h:230
Information about address of a service provider.
Definition: bsd_socket.h:483
int_t ai_family
Definition: bsd_socket.h:485
struct sockaddr * ai_addr
Definition: bsd_socket.h:489
socklen_t ai_addrlen
Definition: bsd_socket.h:488
int_t ai_protocol
Definition: bsd_socket.h:487
int_t ai_socktype
Definition: bsd_socket.h:486
struct addrinfo * ai_next
Definition: bsd_socket.h:491
Ancillary data header.
Definition: bsd_socket.h:426
int_t cmsg_level
Definition: bsd_socket.h:428
size_t cmsg_len
Definition: bsd_socket.h:427
int_t cmsg_type
Definition: bsd_socket.h:429
Set of sockets.
Definition: bsd_socket.h:460
int_t fd_count
Definition: bsd_socket.h:461
int_t fd_array[FD_SETSIZE]
Definition: bsd_socket.h:462
Information about a given host.
Definition: bsd_socket.h:471
uint16_t h_length
Definition: bsd_socket.h:473
uint16_t h_addrtype
Definition: bsd_socket.h:472
uint8_t h_addr[16]
Definition: bsd_socket.h:474
Structure that represents an IPv6 address.
Definition: bsd_socket.h:342
uint8_t s6_addr[16]
Definition: bsd_socket.h:343
IPv6 packet information.
Definition: bsd_socket.h:449
int ipi6_ifindex
Definition: bsd_socket.h:450
struct in6_addr ipi6_addr
Definition: bsd_socket.h:451
Structure that represents an IPv4 address.
Definition: bsd_socket.h:319
in_addr_t s_addr
Definition: bsd_socket.h:320
IPv4 packet information.
Definition: bsd_socket.h:438
struct in_addr ipi_addr
Definition: bsd_socket.h:440
int ipi_ifindex
Definition: bsd_socket.h:439
void * iov_base
Definition: bsd_socket.h:400
size_t iov_len
Definition: bsd_socket.h:401
Message header.
Definition: bsd_socket.h:410
size_t msg_controllen
Definition: bsd_socket.h:416
void * msg_name
Definition: bsd_socket.h:411
int_t msg_flags
Definition: bsd_socket.h:417
socklen_t msg_namelen
Definition: bsd_socket.h:412
int_t msg_iovlen
Definition: bsd_socket.h:414
struct iovec * msg_iov
Definition: bsd_socket.h:413
void * msg_control
Definition: bsd_socket.h:415
IPv6 address information.
Definition: bsd_socket.h:352
uint32_t sin6_flowinfo
Definition: bsd_socket.h:355
uint16_t sin6_port
Definition: bsd_socket.h:354
uint16_t sin6_family
Definition: bsd_socket.h:353
uint32_t sin6_scope_id
Definition: bsd_socket.h:357
struct in6_addr sin6_addr
Definition: bsd_socket.h:356
IPv4 address information.
Definition: bsd_socket.h:329
uint16_t sin_port
Definition: bsd_socket.h:331
struct in_addr sin_addr
Definition: bsd_socket.h:332
uint16_t sin_family
Definition: bsd_socket.h:330
Socket address.
Definition: bsd_socket.h:297
Timeout structure.
Definition: bsd_socket.h:502
int32_t tv_sec
Definition: bsd_socket.h:503
int32_t tv_usec
Definition: bsd_socket.h:504
uint8_t length
Definition: tcp.h:368
uint8_t flags
Definition: tcp.h:351