1 /**
2  * @file
3  * @brief BSD Sockets compatible API definitions
4  *
5  * An API for applications to use BSD Sockets like API.
6  */
7 
8 /*
9  * Copyright (c) 2017-2018 Linaro Limited
10  * Copyright (c) 2021 Nordic Semiconductor
11  *
12  * SPDX-License-Identifier: Apache-2.0
13  */
14 
15 #ifndef ZEPHYR_INCLUDE_NET_SOCKET_H_
16 #define ZEPHYR_INCLUDE_NET_SOCKET_H_
17 
18 /**
19  * @brief BSD Sockets compatible API
20  * @defgroup bsd_sockets BSD Sockets compatible API
21  * @since 1.9
22  * @version 1.0.0
23  * @ingroup networking
24  * @{
25  */
26 
27 #include <zephyr/kernel.h>
28 #include <sys/types.h>
29 #include <zephyr/types.h>
30 #include <zephyr/device.h>
31 #include <zephyr/net/net_ip.h>
32 #include <zephyr/net/socket_select.h>
33 #include <zephyr/net/socket_poll.h>
34 #include <zephyr/sys/iterable_sections.h>
35 #include <zephyr/sys/fdtable.h>
36 #include <zephyr/net/dns_resolve.h>
37 #include <stdlib.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @name Options for poll()
45  * @{
46  */
47 /* ZSOCK_POLL* values are compatible with Linux */
48 /** zsock_poll: Poll for readability */
49 #define ZSOCK_POLLIN 1
50 /** zsock_poll: Poll for exceptional condition */
51 #define ZSOCK_POLLPRI 2
52 /** zsock_poll: Poll for writability */
53 #define ZSOCK_POLLOUT 4
54 /** zsock_poll: Poll results in error condition (output value only) */
55 #define ZSOCK_POLLERR 8
56 /** zsock_poll: Poll detected closed connection (output value only) */
57 #define ZSOCK_POLLHUP 0x10
58 /** zsock_poll: Invalid socket (output value only) */
59 #define ZSOCK_POLLNVAL 0x20
60 /** @} */
61 
62 /**
63  * @name Options for sending and receiving data
64  * @{
65  */
66 /** zsock_recv: Read data without removing it from socket input queue */
67 #define ZSOCK_MSG_PEEK 0x02
68 /** zsock_recvmsg: Control data buffer too small.
69  */
70 #define ZSOCK_MSG_CTRUNC 0x08
71 /** zsock_recv: return the real length of the datagram, even when it was longer
72  *  than the passed buffer
73  */
74 #define ZSOCK_MSG_TRUNC 0x20
75 /** zsock_recv/zsock_send: Override operation to non-blocking */
76 #define ZSOCK_MSG_DONTWAIT 0x40
77 /** zsock_recv: block until the full amount of data can be returned */
78 #define ZSOCK_MSG_WAITALL 0x100
79 /** @} */
80 
81 /**
82  * @name Options for shutdown() function
83  * @{
84  */
85 /* Well-known values, e.g. from Linux man 2 shutdown:
86  * "The constants SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2,
87  * respectively". Some software uses numeric values.
88  */
89 /** zsock_shutdown: Shut down for reading */
90 #define ZSOCK_SHUT_RD 0
91 /** zsock_shutdown: Shut down for writing */
92 #define ZSOCK_SHUT_WR 1
93 /** zsock_shutdown: Shut down for both reading and writing */
94 #define ZSOCK_SHUT_RDWR 2
95 /** @} */
96 
97 /**
98  * @defgroup secure_sockets_options Socket options for TLS
99  * @since 1.13
100  * @version 0.8.0
101  * @{
102  */
103 /**
104  * @name Socket options for TLS
105  * @{
106  */
107 
108 /** Protocol level for TLS.
109  *  Here, the same socket protocol level for TLS as in Linux was used.
110  */
111 #define SOL_TLS 282
112 
113 /** Socket option to select TLS credentials to use. It accepts and returns an
114  *  array of sec_tag_t that indicate which TLS credentials should be used with
115  *  specific socket.
116  */
117 #define TLS_SEC_TAG_LIST 1
118 /** Write-only socket option to set hostname. It accepts a string containing
119  *  the hostname (may be NULL to disable hostname verification). By default,
120  *  hostname check is enforced for TLS clients.
121  */
122 #define TLS_HOSTNAME 2
123 /** Socket option to select ciphersuites to use. It accepts and returns an array
124  *  of integers with IANA assigned ciphersuite identifiers.
125  *  If not set, socket will allow all ciphersuites available in the system
126  *  (mbedTLS default behavior).
127  */
128 #define TLS_CIPHERSUITE_LIST 3
129 /** Read-only socket option to read a ciphersuite chosen during TLS handshake.
130  *  It returns an integer containing an IANA assigned ciphersuite identifier
131  *  of chosen ciphersuite.
132  */
133 #define TLS_CIPHERSUITE_USED 4
134 /** Write-only socket option to set peer verification level for TLS connection.
135  *  This option accepts an integer with a peer verification level, compatible
136  *  with mbedTLS values:
137  *    - 0 - none
138  *    - 1 - optional
139  *    - 2 - required
140  *
141  *  If not set, socket will use mbedTLS defaults (none for servers, required
142  *  for clients).
143  */
144 #define TLS_PEER_VERIFY 5
145 /** Write-only socket option to set role for DTLS connection. This option
146  *  is irrelevant for TLS connections, as for them role is selected based on
147  *  connect()/listen() usage. By default, DTLS will assume client role.
148  *  This option accepts an integer with a TLS role, compatible with
149  *  mbedTLS values:
150  *    - 0 - client
151  *    - 1 - server
152  */
153 #define TLS_DTLS_ROLE 6
154 /** Socket option for setting the supported Application Layer Protocols.
155  *  It accepts and returns a const char array of NULL terminated strings
156  *  representing the supported application layer protocols listed during
157  *  the TLS handshake.
158  */
159 #define TLS_ALPN_LIST 7
160 /** Socket option to set DTLS min handshake timeout. The timeout starts at min,
161  *  and upon retransmission the timeout is doubled util max is reached.
162  *  Min and max arguments are separate options. The time unit is ms.
163  */
164 #define TLS_DTLS_HANDSHAKE_TIMEOUT_MIN 8
165 
166 /** Socket option to set DTLS max handshake timeout. The timeout starts at min,
167  *  and upon retransmission the timeout is doubled util max is reached.
168  *  Min and max arguments are separate options. The time unit is ms.
169  */
170 #define TLS_DTLS_HANDSHAKE_TIMEOUT_MAX 9
171 
172 /** Socket option for preventing certificates from being copied to the mbedTLS
173  *  heap if possible. The option is only effective for DER certificates and is
174  *  ignored for PEM certificates.
175  */
176 #define TLS_CERT_NOCOPY	       10
177 /** TLS socket option to use with offloading. The option instructs the network
178  *  stack only to offload underlying TCP/UDP communication. The TLS/DTLS
179  *  operation is handled by a native TLS/DTLS socket implementation from Zephyr.
180  *
181  *  Note, that this option is only applicable if socket dispatcher is used
182  *  (CONFIG_NET_SOCKETS_OFFLOAD_DISPATCHER is enabled).
183  *  In such case, it should be the first socket option set on a newly created
184  *  socket. After that, the application may use SO_BINDTODEVICE to choose the
185  *  dedicated network interface for the underlying TCP/UDP socket.
186  */
187 #define TLS_NATIVE 11
188 /** Socket option to control TLS session caching on a socket. Accepted values:
189  *  - 0 - Disabled.
190  *  - 1 - Enabled.
191  */
192 #define TLS_SESSION_CACHE 12
193 /** Write-only socket option to purge session cache immediately.
194  *  This option accepts any value.
195  */
196 #define TLS_SESSION_CACHE_PURGE 13
197 /** Write-only socket option to control DTLS CID.
198  *  The option accepts an integer, indicating the setting.
199  *  Accepted values for the option are: 0, 1 and 2.
200  *  Effective when set before connecting to the socket.
201  *  - 0 - DTLS CID will be disabled.
202  *  - 1 - DTLS CID will be enabled, and a 0 length CID value to be sent to the
203  *        peer.
204  *  - 2 - DTLS CID will be enabled, and the most recent value set with
205  *        TLS_DTLS_CID_VALUE will be sent to the peer. Otherwise, a random value
206  *        will be used.
207  */
208 #define TLS_DTLS_CID 14
209 /** Read-only socket option to get DTLS CID status.
210  *  The option accepts a pointer to an integer, indicating the setting upon
211  *  return.
212  *  Returned values for the option are:
213  *  - 0 - DTLS CID is disabled.
214  *  - 1 - DTLS CID is received on the downlink.
215  *  - 2 - DTLS CID is sent to the uplink.
216  *  - 3 - DTLS CID is used in both directions.
217  */
218 #define TLS_DTLS_CID_STATUS 15
219 /** Socket option to set or get the value of the DTLS connection ID to be
220  *  used for the DTLS session.
221  *  The option accepts a byte array, holding the CID value.
222  */
223 #define TLS_DTLS_CID_VALUE 16
224 /** Read-only socket option to get the value of the DTLS connection ID
225  *  received from the peer.
226  *  The option accepts a pointer to a byte array, holding the CID value upon
227  *  return. The optlen returned will be 0 if the peer did not provide a
228  *  connection ID, otherwise will contain the length of the CID value.
229  */
230 #define TLS_DTLS_PEER_CID_VALUE 17
231 /** Socket option to configure DTLS socket behavior on connect().
232  *  If set, DTLS connect() will execute the handshake with the configured peer.
233  *  This is the default behavior.
234  *  Otherwise, DTLS connect() will only configure peer address (as with regular
235  *  UDP socket) and will not attempt to execute DTLS handshake. The handshake
236  *  will take place in consecutive send()/recv() call.
237  */
238 #define TLS_DTLS_HANDSHAKE_ON_CONNECT 18
239 
240 /* Valid values for @ref TLS_PEER_VERIFY option */
241 #define TLS_PEER_VERIFY_NONE 0     /**< Peer verification disabled. */
242 #define TLS_PEER_VERIFY_OPTIONAL 1 /**< Peer verification optional. */
243 #define TLS_PEER_VERIFY_REQUIRED 2 /**< Peer verification required. */
244 
245 /* Valid values for @ref TLS_DTLS_ROLE option */
246 #define TLS_DTLS_ROLE_CLIENT 0 /**< Client role in a DTLS session. */
247 #define TLS_DTLS_ROLE_SERVER 1 /**< Server role in a DTLS session. */
248 
249 /* Valid values for @ref TLS_CERT_NOCOPY option */
250 #define TLS_CERT_NOCOPY_NONE 0     /**< Cert duplicated in heap */
251 #define TLS_CERT_NOCOPY_OPTIONAL 1 /**< Cert not copied in heap if DER */
252 
253 /* Valid values for @ref TLS_SESSION_CACHE option */
254 #define TLS_SESSION_CACHE_DISABLED 0 /**< Disable TLS session caching. */
255 #define TLS_SESSION_CACHE_ENABLED 1 /**< Enable TLS session caching. */
256 
257 /* Valid values for @ref TLS_DTLS_CID (Connection ID) option */
258 #define TLS_DTLS_CID_DISABLED		0 /**< CID is disabled  */
259 #define TLS_DTLS_CID_SUPPORTED		1 /**< CID is supported */
260 #define TLS_DTLS_CID_ENABLED		2 /**< CID is enabled   */
261 
262 /* Valid values for @ref TLS_DTLS_CID_STATUS option */
263 #define TLS_DTLS_CID_STATUS_DISABLED		0 /**< CID is disabled */
264 #define TLS_DTLS_CID_STATUS_DOWNLINK		1 /**< CID is in use by us */
265 #define TLS_DTLS_CID_STATUS_UPLINK		2 /**< CID is in use by peer */
266 #define TLS_DTLS_CID_STATUS_BIDIRECTIONAL	3 /**< CID is in use by us and peer */
267 /** @} */ /* for @name */
268 /** @} */ /* for @defgroup */
269 
270 /**
271  * @brief Definition used when querying address information.
272  *
273  * A linked list of these descriptors is returned by getaddrinfo(). The struct
274  * is also passed as hints when calling the getaddrinfo() function.
275  */
276 struct zsock_addrinfo {
277 	struct zsock_addrinfo *ai_next; /**< Pointer to next address entry */
278 	int ai_flags;             /**< Additional options */
279 	int ai_family;            /**< Address family of the returned addresses */
280 	int ai_socktype;          /**< Socket type, for example SOCK_STREAM or SOCK_DGRAM */
281 	int ai_protocol;          /**< Protocol for addresses, 0 means any protocol */
282 	int ai_eflags;            /**< Extended flags for special usage */
283 	socklen_t ai_addrlen;     /**< Length of the socket address */
284 	struct sockaddr *ai_addr; /**< Pointer to the address */
285 	char *ai_canonname;       /**< Optional official name of the host */
286 
287 /** @cond INTERNAL_HIDDEN */
288 	struct sockaddr _ai_addr;
289 	char _ai_canonname[DNS_MAX_NAME_SIZE + 1];
290 /** @endcond */
291 };
292 
293 /**
294  * @brief Obtain a file descriptor's associated net context
295  *
296  * With CONFIG_USERSPACE enabled, the kernel's object permission system
297  * must apply to socket file descriptors. When a socket is opened, by default
298  * only the caller has permission, access by other threads will fail unless
299  * they have been specifically granted permission.
300  *
301  * This is achieved by tagging data structure definitions that implement the
302  * underlying object associated with a network socket file descriptor with
303  * '__net_socket`. All pointers to instances of these will be known to the
304  * kernel as kernel objects with type K_OBJ_NET_SOCKET.
305  *
306  * This API is intended for threads that need to grant access to the object
307  * associated with a particular file descriptor to another thread. The
308  * returned pointer represents the underlying K_OBJ_NET_SOCKET  and
309  * may be passed to APIs like k_object_access_grant().
310  *
311  * In a system like Linux which has the notion of threads running in processes
312  * in a shared virtual address space, this sort of management is unnecessary as
313  * the scope of file descriptors is implemented at the process level.
314  *
315  * However in Zephyr the file descriptor scope is global, and MPU-based systems
316  * are not able to implement a process-like model due to the lack of memory
317  * virtualization hardware. They use discrete object permissions and memory
318  * domains instead to define thread access scope.
319  *
320  * User threads will have no direct access to the returned object
321  * and will fault if they try to access its memory; the pointer can only be
322  * used to make permission assignment calls, which follow exactly the rules
323  * for other kernel objects like device drivers and IPC.
324  *
325  * @param sock file descriptor
326  * @return pointer to associated network socket object, or NULL if the
327  *         file descriptor wasn't valid or the caller had no access permission
328  */
329 __syscall void *zsock_get_context_object(int sock);
330 
331 /**
332  * @brief Create a network socket
333  *
334  * @details
335  * @rst
336  * See `POSIX.1-2017 article
337  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>`__
338  * for normative description.
339  * This function is also exposed as ``socket()``
340  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
341  * @endrst
342  *
343  * If CONFIG_USERSPACE is enabled, the caller will be granted access to the
344  * context object associated with the returned file descriptor.
345  * @see zsock_get_context_object()
346  *
347  */
348 __syscall int zsock_socket(int family, int type, int proto);
349 
350 /**
351  * @brief Create an unnamed pair of connected sockets
352  *
353  * @details
354  * @rst
355  * See `POSIX.1-2017 article
356  * <https://pubs.opengroup.org/onlinepubs/009695399/functions/socketpair.html>`__
357  * for normative description.
358  * This function is also exposed as ``socketpair()``
359  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
360  * @endrst
361  */
362 __syscall int zsock_socketpair(int family, int type, int proto, int *sv);
363 
364 /**
365  * @brief Close a network socket
366  *
367  * @details
368  * @rst
369  * Close a network socket.
370  * This function is also exposed as ``close()``
371  * if :kconfig:option:`CONFIG_POSIX_API` is defined (in which case it
372  * may conflict with generic POSIX ``close()`` function).
373  * @endrst
374  */
375 __syscall int zsock_close(int sock);
376 
377 /**
378  * @brief Shutdown socket send/receive operations
379  *
380  * @details
381  * @rst
382  * See `POSIX.1-2017 article
383  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>`__
384  * for normative description, but currently this function has no effect in
385  * Zephyr and provided solely for compatibility with existing code.
386  * This function is also exposed as ``shutdown()``
387  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
388  * @endrst
389  */
390 __syscall int zsock_shutdown(int sock, int how);
391 
392 /**
393  * @brief Bind a socket to a local network address
394  *
395  * @details
396  * @rst
397  * See `POSIX.1-2017 article
398  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>`__
399  * for normative description.
400  * This function is also exposed as ``bind()``
401  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
402  * @endrst
403  */
404 __syscall int zsock_bind(int sock, const struct sockaddr *addr,
405 			 socklen_t addrlen);
406 
407 /**
408  * @brief Connect a socket to a peer network address
409  *
410  * @details
411  * @rst
412  * See `POSIX.1-2017 article
413  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>`__
414  * for normative description.
415  * This function is also exposed as ``connect()``
416  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
417  * @endrst
418  */
419 __syscall int zsock_connect(int sock, const struct sockaddr *addr,
420 			    socklen_t addrlen);
421 
422 /**
423  * @brief Set up a STREAM socket to accept peer connections
424  *
425  * @details
426  * @rst
427  * See `POSIX.1-2017 article
428  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>`__
429  * for normative description.
430  * This function is also exposed as ``listen()``
431  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
432  * @endrst
433  */
434 __syscall int zsock_listen(int sock, int backlog);
435 
436 /**
437  * @brief Accept a connection on listening socket
438  *
439  * @details
440  * @rst
441  * See `POSIX.1-2017 article
442  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>`__
443  * for normative description.
444  * This function is also exposed as ``accept()``
445  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
446  * @endrst
447  */
448 __syscall int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
449 
450 /**
451  * @brief Send data to an arbitrary network address
452  *
453  * @details
454  * @rst
455  * See `POSIX.1-2017 article
456  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html>`__
457  * for normative description.
458  * This function is also exposed as ``sendto()``
459  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
460  * @endrst
461  */
462 __syscall ssize_t zsock_sendto(int sock, const void *buf, size_t len,
463 			       int flags, const struct sockaddr *dest_addr,
464 			       socklen_t addrlen);
465 
466 /**
467  * @brief Send data to a connected peer
468  *
469  * @details
470  * @rst
471  * See `POSIX.1-2017 article
472  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html>`__
473  * for normative description.
474  * This function is also exposed as ``send()``
475  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
476  * @endrst
477  */
zsock_send(int sock,const void * buf,size_t len,int flags)478 static inline ssize_t zsock_send(int sock, const void *buf, size_t len,
479 				 int flags)
480 {
481 	return zsock_sendto(sock, buf, len, flags, NULL, 0);
482 }
483 
484 /**
485  * @brief Send data to an arbitrary network address
486  *
487  * @details
488  * @rst
489  * See `POSIX.1-2017 article
490  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html>`__
491  * for normative description.
492  * This function is also exposed as ``sendmsg()``
493  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
494  * @endrst
495  */
496 __syscall ssize_t zsock_sendmsg(int sock, const struct msghdr *msg,
497 				int flags);
498 
499 /**
500  * @brief Receive data from an arbitrary network address
501  *
502  * @details
503  * @rst
504  * See `POSIX.1-2017 article
505  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html>`__
506  * for normative description.
507  * This function is also exposed as ``recvfrom()``
508  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
509  * @endrst
510  */
511 __syscall ssize_t zsock_recvfrom(int sock, void *buf, size_t max_len,
512 				 int flags, struct sockaddr *src_addr,
513 				 socklen_t *addrlen);
514 
515 /**
516  * @brief Receive a message from an arbitrary network address
517  *
518  * @details
519  * @rst
520  * See `POSIX.1-2017 article
521  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/recvmsg.html>`__
522  * for normative description.
523  * This function is also exposed as ``recvmsg()``
524  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
525  * @endrst
526  */
527 __syscall ssize_t zsock_recvmsg(int sock, struct msghdr *msg, int flags);
528 
529 /**
530  * @brief Receive data from a connected peer
531  *
532  * @details
533  * @rst
534  * See `POSIX.1-2017 article
535  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html>`__
536  * for normative description.
537  * This function is also exposed as ``recv()``
538  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
539  * @endrst
540  */
zsock_recv(int sock,void * buf,size_t max_len,int flags)541 static inline ssize_t zsock_recv(int sock, void *buf, size_t max_len,
542 				 int flags)
543 {
544 	return zsock_recvfrom(sock, buf, max_len, flags, NULL, NULL);
545 }
546 
547 /**
548  * @brief Control blocking/non-blocking mode of a socket
549  *
550  * @details
551  * @rst
552  * This functions allow to (only) configure a socket for blocking or
553  * non-blocking operation (O_NONBLOCK).
554  * This function is also exposed as ``fcntl()``
555  * if :kconfig:option:`CONFIG_POSIX_API` is defined (in which case
556  * it may conflict with generic POSIX ``fcntl()`` function).
557  * @endrst
558  */
559 __syscall int zsock_fcntl_impl(int sock, int cmd, int flags);
560 
561 /** @cond INTERNAL_HIDDEN */
562 
563 /*
564  * Need this wrapper because newer GCC versions got too smart and "typecheck"
565  * even macros.
566  */
zsock_fcntl_wrapper(int sock,int cmd,...)567 static inline int zsock_fcntl_wrapper(int sock, int cmd, ...)
568 {
569 	va_list args;
570 	int flags;
571 
572 	va_start(args, cmd);
573 	flags = va_arg(args, int);
574 	va_end(args);
575 	return zsock_fcntl_impl(sock, cmd, flags);
576 }
577 
578 #define zsock_fcntl zsock_fcntl_wrapper
579 
580 /** @endcond */
581 
582 /**
583  * @brief Control underlying socket parameters
584  *
585  * @details
586  * @rst
587  * See `POSIX.1-2017 article
588  * <https://pubs.opengroup.org/onlinepubs/9699919799/functions/ioctl.html>`__
589  * for normative description.
590  * This function enables querying or manipulating underlying socket parameters.
591  * Currently supported @p request values include ``ZFD_IOCTL_FIONBIO``, and
592  * ``ZFD_IOCTL_FIONREAD``, to set non-blocking mode, and query the number of
593  * bytes available to read, respectively.
594  * This function is also exposed as ``ioctl()``
595  * if :kconfig:option:`CONFIG_POSIX_API` is defined (in which case
596  * it may conflict with generic POSIX ``ioctl()`` function).
597  * @endrst
598  */
599 __syscall int zsock_ioctl_impl(int sock, unsigned long request, va_list ap);
600 
601 /** @cond INTERNAL_HIDDEN */
602 
zsock_ioctl_wrapper(int sock,unsigned long request,...)603 static inline int zsock_ioctl_wrapper(int sock, unsigned long request, ...)
604 {
605 	int ret;
606 	va_list args;
607 
608 	va_start(args, request);
609 	ret = zsock_ioctl_impl(sock, request, args);
610 	va_end(args);
611 
612 	return ret;
613 }
614 
615 #define zsock_ioctl zsock_ioctl_wrapper
616 
617 /** @endcond */
618 
619 /**
620  * @brief Efficiently poll multiple sockets for events
621  *
622  * @details
623  * @rst
624  * See `POSIX.1-2017 article
625  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html>`__
626  * for normative description.
627  * This function is also exposed as ``poll()``
628  * if :kconfig:option:`CONFIG_POSIX_API` is defined (in which case
629  * it may conflict with generic POSIX ``poll()`` function).
630  * @endrst
631  */
632 __syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
633 
634 /**
635  * @brief Get various socket options
636  *
637  * @details
638  * @rst
639  * See `POSIX.1-2017 article
640  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html>`__
641  * for normative description. In Zephyr this function supports a subset of
642  * socket options described by POSIX, but also some additional options
643  * available in Linux (some options are dummy and provided to ease porting
644  * of existing code).
645  * This function is also exposed as ``getsockopt()``
646  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
647  * @endrst
648  */
649 __syscall int zsock_getsockopt(int sock, int level, int optname,
650 			       void *optval, socklen_t *optlen);
651 
652 /**
653  * @brief Set various socket options
654  *
655  * @details
656  * @rst
657  * See `POSIX.1-2017 article
658  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html>`__
659  * for normative description. In Zephyr this function supports a subset of
660  * socket options described by POSIX, but also some additional options
661  * available in Linux (some options are dummy and provided to ease porting
662  * of existing code).
663  * This function is also exposed as ``setsockopt()``
664  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
665  * @endrst
666  */
667 __syscall int zsock_setsockopt(int sock, int level, int optname,
668 			       const void *optval, socklen_t optlen);
669 
670 /**
671  * @brief Get peer name
672  *
673  * @details
674  * @rst
675  * See `POSIX.1-2017 article
676  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>`__
677  * for normative description.
678  * This function is also exposed as ``getpeername()``
679  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
680  * @endrst
681  */
682 __syscall int zsock_getpeername(int sock, struct sockaddr *addr,
683 				socklen_t *addrlen);
684 
685 /**
686  * @brief Get socket name
687  *
688  * @details
689  * @rst
690  * See `POSIX.1-2017 article
691  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>`__
692  * for normative description.
693  * This function is also exposed as ``getsockname()``
694  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
695  * @endrst
696  */
697 __syscall int zsock_getsockname(int sock, struct sockaddr *addr,
698 				socklen_t *addrlen);
699 
700 /**
701  * @brief Get local host name
702  *
703  * @details
704  * @rst
705  * See `POSIX.1-2017 article
706  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html>`__
707  * for normative description.
708  * This function is also exposed as ``gethostname()``
709  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
710  * @endrst
711  */
712 __syscall int zsock_gethostname(char *buf, size_t len);
713 
714 /**
715  * @brief Convert network address from internal to numeric ASCII form
716  *
717  * @details
718  * @rst
719  * See `POSIX.1-2017 article
720  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_ntop.html>`__
721  * for normative description.
722  * This function is also exposed as ``inet_ntop()``
723  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
724  * @endrst
725  */
zsock_inet_ntop(sa_family_t family,const void * src,char * dst,size_t size)726 static inline char *zsock_inet_ntop(sa_family_t family, const void *src,
727 				    char *dst, size_t size)
728 {
729 	return net_addr_ntop(family, src, dst, size);
730 }
731 
732 /**
733  * @brief Convert network address from numeric ASCII form to internal representation
734  *
735  * @details
736  * @rst
737  * See `POSIX.1-2017 article
738  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_pton.html>`__
739  * for normative description.
740  * This function is also exposed as ``inet_pton()``
741  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
742  * @endrst
743  */
744 __syscall int zsock_inet_pton(sa_family_t family, const char *src, void *dst);
745 
746 /** @cond INTERNAL_HIDDEN */
747 __syscall int z_zsock_getaddrinfo_internal(const char *host,
748 					   const char *service,
749 					   const struct zsock_addrinfo *hints,
750 					   struct zsock_addrinfo *res);
751 /** @endcond */
752 
753 /* Flags for getaddrinfo() hints. */
754 
755 /**
756  * @name Flags for getaddrinfo() hints
757  * @{
758  */
759 /** Address for bind() (vs for connect()) */
760 #define AI_PASSIVE 0x1
761 /** Fill in ai_canonname */
762 #define AI_CANONNAME 0x2
763 /** Assume host address is in numeric notation, don't DNS lookup */
764 #define AI_NUMERICHOST 0x4
765 /** May return IPv4 mapped address for IPv6  */
766 #define AI_V4MAPPED 0x8
767 /** May return both native IPv6 and mapped IPv4 address for IPv6 */
768 #define AI_ALL 0x10
769 /** IPv4/IPv6 support depends on local system config */
770 #define AI_ADDRCONFIG 0x20
771 /** Assume service (port) is numeric */
772 #define AI_NUMERICSERV 0x400
773 /** Extra flags present (see RFC 5014) */
774 #define AI_EXTFLAGS 0x800
775 /** @} */
776 
777 /**
778  * @brief Resolve a domain name to one or more network addresses
779  *
780  * @details
781  * @rst
782  * See `POSIX.1-2017 article
783  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html>`__
784  * for normative description.
785  * This function is also exposed as ``getaddrinfo()``
786  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
787  * @endrst
788  */
789 int zsock_getaddrinfo(const char *host, const char *service,
790 		      const struct zsock_addrinfo *hints,
791 		      struct zsock_addrinfo **res);
792 
793 /**
794  * @brief Free results returned by zsock_getaddrinfo()
795  *
796  * @details
797  * @rst
798  * See `POSIX.1-2017 article
799  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/freeaddrinfo.html>`__
800  * for normative description.
801  * This function is also exposed as ``freeaddrinfo()``
802  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
803  * @endrst
804  */
805 void zsock_freeaddrinfo(struct zsock_addrinfo *ai);
806 
807 /**
808  * @brief Convert zsock_getaddrinfo() error code to textual message
809  *
810  * @details
811  * @rst
812  * See `POSIX.1-2017 article
813  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/gai_strerror.html>`__
814  * for normative description.
815  * This function is also exposed as ``gai_strerror()``
816  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
817  * @endrst
818  */
819 const char *zsock_gai_strerror(int errcode);
820 
821 /**
822  * @name Flags for getnameinfo()
823  * @{
824  */
825 /** zsock_getnameinfo(): Resolve to numeric address. */
826 #define NI_NUMERICHOST 1
827 /** zsock_getnameinfo(): Resolve to numeric port number. */
828 #define NI_NUMERICSERV 2
829 /** zsock_getnameinfo(): Return only hostname instead of FQDN */
830 #define NI_NOFQDN 4
831 /** zsock_getnameinfo(): Dummy option for compatibility */
832 #define NI_NAMEREQD 8
833 /** zsock_getnameinfo(): Dummy option for compatibility */
834 #define NI_DGRAM 16
835 
836 /* POSIX extensions */
837 
838 /** zsock_getnameinfo(): Max supported hostname length */
839 #ifndef NI_MAXHOST
840 #define NI_MAXHOST 64
841 #endif
842 /** @} */
843 
844 /**
845  * @brief Resolve a network address to a domain name or ASCII address
846  *
847  * @details
848  * @rst
849  * See `POSIX.1-2017 article
850  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getnameinfo.html>`__
851  * for normative description.
852  * This function is also exposed as ``getnameinfo()``
853  * if :kconfig:option:`CONFIG_POSIX_API` is defined.
854  * @endrst
855  */
856 int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
857 		      char *host, socklen_t hostlen,
858 		      char *serv, socklen_t servlen, int flags);
859 
860 #if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
861 
862 /**
863  * @name Socket APIs available if CONFIG_NET_SOCKETS_POSIX_NAMES is enabled
864  * @{
865  */
866 
867 /** POSIX wrapper for @ref zsock_pollfd */
868 #define pollfd zsock_pollfd
869 
870 /** POSIX wrapper for @ref zsock_socket */
socket(int family,int type,int proto)871 static inline int socket(int family, int type, int proto)
872 {
873 	return zsock_socket(family, type, proto);
874 }
875 
876 /** POSIX wrapper for @ref zsock_socketpair */
socketpair(int family,int type,int proto,int sv[2])877 static inline int socketpair(int family, int type, int proto, int sv[2])
878 {
879 	return zsock_socketpair(family, type, proto, sv);
880 }
881 
882 /** POSIX wrapper for @ref zsock_close */
close(int sock)883 static inline int close(int sock)
884 {
885 	return zsock_close(sock);
886 }
887 
888 /** POSIX wrapper for @ref zsock_shutdown */
shutdown(int sock,int how)889 static inline int shutdown(int sock, int how)
890 {
891 	return zsock_shutdown(sock, how);
892 }
893 
894 /** POSIX wrapper for @ref zsock_bind */
bind(int sock,const struct sockaddr * addr,socklen_t addrlen)895 static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
896 {
897 	return zsock_bind(sock, addr, addrlen);
898 }
899 
900 /** POSIX wrapper for @ref zsock_connect */
connect(int sock,const struct sockaddr * addr,socklen_t addrlen)901 static inline int connect(int sock, const struct sockaddr *addr,
902 			  socklen_t addrlen)
903 {
904 	return zsock_connect(sock, addr, addrlen);
905 }
906 
907 /** POSIX wrapper for @ref zsock_listen */
listen(int sock,int backlog)908 static inline int listen(int sock, int backlog)
909 {
910 	return zsock_listen(sock, backlog);
911 }
912 
913 /** POSIX wrapper for @ref zsock_accept */
accept(int sock,struct sockaddr * addr,socklen_t * addrlen)914 static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
915 {
916 	return zsock_accept(sock, addr, addrlen);
917 }
918 
919 /** POSIX wrapper for @ref zsock_send */
send(int sock,const void * buf,size_t len,int flags)920 static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
921 {
922 	return zsock_send(sock, buf, len, flags);
923 }
924 
925 /** POSIX wrapper for @ref zsock_recv */
recv(int sock,void * buf,size_t max_len,int flags)926 static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
927 {
928 	return zsock_recv(sock, buf, max_len, flags);
929 }
930 
931 /** POSIX wrapper for @ref zsock_sendto */
sendto(int sock,const void * buf,size_t len,int flags,const struct sockaddr * dest_addr,socklen_t addrlen)932 static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
933 			     const struct sockaddr *dest_addr,
934 			     socklen_t addrlen)
935 {
936 	return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
937 }
938 
939 /** POSIX wrapper for @ref zsock_sendmsg */
sendmsg(int sock,const struct msghdr * message,int flags)940 static inline ssize_t sendmsg(int sock, const struct msghdr *message,
941 			      int flags)
942 {
943 	return zsock_sendmsg(sock, message, flags);
944 }
945 
946 /** POSIX wrapper for @ref zsock_recvfrom */
recvfrom(int sock,void * buf,size_t max_len,int flags,struct sockaddr * src_addr,socklen_t * addrlen)947 static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
948 			       struct sockaddr *src_addr, socklen_t *addrlen)
949 {
950 	return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
951 }
952 
953 /** POSIX wrapper for @ref zsock_recvmsg */
recvmsg(int sock,struct msghdr * msg,int flags)954 static inline ssize_t recvmsg(int sock, struct msghdr *msg, int flags)
955 {
956 	return zsock_recvmsg(sock, msg, flags);
957 }
958 
959 /** POSIX wrapper for @ref zsock_poll */
poll(struct zsock_pollfd * fds,int nfds,int timeout)960 static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
961 {
962 	return zsock_poll(fds, nfds, timeout);
963 }
964 
965 /** POSIX wrapper for @ref zsock_getsockopt */
getsockopt(int sock,int level,int optname,void * optval,socklen_t * optlen)966 static inline int getsockopt(int sock, int level, int optname,
967 			     void *optval, socklen_t *optlen)
968 {
969 	return zsock_getsockopt(sock, level, optname, optval, optlen);
970 }
971 
972 /** POSIX wrapper for @ref zsock_setsockopt */
setsockopt(int sock,int level,int optname,const void * optval,socklen_t optlen)973 static inline int setsockopt(int sock, int level, int optname,
974 			     const void *optval, socklen_t optlen)
975 {
976 	return zsock_setsockopt(sock, level, optname, optval, optlen);
977 }
978 
979 /** POSIX wrapper for @ref zsock_getpeername */
getpeername(int sock,struct sockaddr * addr,socklen_t * addrlen)980 static inline int getpeername(int sock, struct sockaddr *addr,
981 			      socklen_t *addrlen)
982 {
983 	return zsock_getpeername(sock, addr, addrlen);
984 }
985 
986 /** POSIX wrapper for @ref zsock_getsockname */
getsockname(int sock,struct sockaddr * addr,socklen_t * addrlen)987 static inline int getsockname(int sock, struct sockaddr *addr,
988 			      socklen_t *addrlen)
989 {
990 	return zsock_getsockname(sock, addr, addrlen);
991 }
992 
993 /** POSIX wrapper for @ref zsock_getaddrinfo */
getaddrinfo(const char * host,const char * service,const struct zsock_addrinfo * hints,struct zsock_addrinfo ** res)994 static inline int getaddrinfo(const char *host, const char *service,
995 			      const struct zsock_addrinfo *hints,
996 			      struct zsock_addrinfo **res)
997 {
998 	return zsock_getaddrinfo(host, service, hints, res);
999 }
1000 
1001 /** POSIX wrapper for @ref zsock_freeaddrinfo */
freeaddrinfo(struct zsock_addrinfo * ai)1002 static inline void freeaddrinfo(struct zsock_addrinfo *ai)
1003 {
1004 	zsock_freeaddrinfo(ai);
1005 }
1006 
1007 /** POSIX wrapper for @ref zsock_gai_strerror */
gai_strerror(int errcode)1008 static inline const char *gai_strerror(int errcode)
1009 {
1010 	return zsock_gai_strerror(errcode);
1011 }
1012 
1013 /** POSIX wrapper for @ref zsock_getnameinfo */
getnameinfo(const struct sockaddr * addr,socklen_t addrlen,char * host,socklen_t hostlen,char * serv,socklen_t servlen,int flags)1014 static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
1015 			      char *host, socklen_t hostlen,
1016 			      char *serv, socklen_t servlen, int flags)
1017 {
1018 	return zsock_getnameinfo(addr, addrlen, host, hostlen,
1019 				 serv, servlen, flags);
1020 }
1021 
1022 /** POSIX wrapper for @ref zsock_addrinfo */
1023 #define addrinfo zsock_addrinfo
1024 
1025 /** POSIX wrapper for @ref zsock_gethostname */
gethostname(char * buf,size_t len)1026 static inline int gethostname(char *buf, size_t len)
1027 {
1028 	return zsock_gethostname(buf, len);
1029 }
1030 
1031 /** POSIX wrapper for @ref zsock_inet_pton */
inet_pton(sa_family_t family,const char * src,void * dst)1032 static inline int inet_pton(sa_family_t family, const char *src, void *dst)
1033 {
1034 	return zsock_inet_pton(family, src, dst);
1035 }
1036 
1037 /** POSIX wrapper for @ref zsock_inet_ntop */
inet_ntop(sa_family_t family,const void * src,char * dst,size_t size)1038 static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
1039 			      size_t size)
1040 {
1041 	return zsock_inet_ntop(family, src, dst, size);
1042 }
1043 
1044 /** POSIX wrapper for @ref ZSOCK_POLLIN */
1045 #define POLLIN ZSOCK_POLLIN
1046 /** POSIX wrapper for @ref ZSOCK_POLLOUT */
1047 #define POLLOUT ZSOCK_POLLOUT
1048 /** POSIX wrapper for @ref ZSOCK_POLLERR */
1049 #define POLLERR ZSOCK_POLLERR
1050 /** POSIX wrapper for @ref ZSOCK_POLLHUP */
1051 #define POLLHUP ZSOCK_POLLHUP
1052 /** POSIX wrapper for @ref ZSOCK_POLLNVAL */
1053 #define POLLNVAL ZSOCK_POLLNVAL
1054 
1055 /** POSIX wrapper for @ref ZSOCK_MSG_PEEK */
1056 #define MSG_PEEK ZSOCK_MSG_PEEK
1057 /** POSIX wrapper for @ref ZSOCK_MSG_CTRUNC */
1058 #define MSG_CTRUNC ZSOCK_MSG_CTRUNC
1059 /** POSIX wrapper for @ref ZSOCK_MSG_TRUNC */
1060 #define MSG_TRUNC ZSOCK_MSG_TRUNC
1061 /** POSIX wrapper for @ref ZSOCK_MSG_DONTWAIT */
1062 #define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT
1063 /** POSIX wrapper for @ref ZSOCK_MSG_WAITALL */
1064 #define MSG_WAITALL ZSOCK_MSG_WAITALL
1065 
1066 /** POSIX wrapper for @ref ZSOCK_SHUT_RD */
1067 #define SHUT_RD ZSOCK_SHUT_RD
1068 /** POSIX wrapper for @ref ZSOCK_SHUT_WR */
1069 #define SHUT_WR ZSOCK_SHUT_WR
1070 /** POSIX wrapper for @ref ZSOCK_SHUT_RDWR */
1071 #define SHUT_RDWR ZSOCK_SHUT_RDWR
1072 
1073 /** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
1074 #define EAI_BADFLAGS DNS_EAI_BADFLAGS
1075 /** POSIX wrapper for @ref DNS_EAI_NONAME */
1076 #define EAI_NONAME DNS_EAI_NONAME
1077 /** POSIX wrapper for @ref DNS_EAI_AGAIN */
1078 #define EAI_AGAIN DNS_EAI_AGAIN
1079 /** POSIX wrapper for @ref DNS_EAI_FAIL */
1080 #define EAI_FAIL DNS_EAI_FAIL
1081 /** POSIX wrapper for @ref DNS_EAI_NODATA */
1082 #define EAI_NODATA DNS_EAI_NODATA
1083 /** POSIX wrapper for @ref DNS_EAI_MEMORY */
1084 #define EAI_MEMORY DNS_EAI_MEMORY
1085 /** POSIX wrapper for @ref DNS_EAI_SYSTEM */
1086 #define EAI_SYSTEM DNS_EAI_SYSTEM
1087 /** POSIX wrapper for @ref DNS_EAI_SERVICE */
1088 #define EAI_SERVICE DNS_EAI_SERVICE
1089 /** POSIX wrapper for @ref DNS_EAI_SOCKTYPE */
1090 #define EAI_SOCKTYPE DNS_EAI_SOCKTYPE
1091 /** POSIX wrapper for @ref DNS_EAI_FAMILY */
1092 #define EAI_FAMILY DNS_EAI_FAMILY
1093 /** @} */
1094 #endif /* defined(CONFIG_NET_SOCKETS_POSIX_NAMES) */
1095 
1096 /**
1097  * @name Network interface name description
1098  * @{
1099  */
1100 /** Network interface name length */
1101 #if defined(CONFIG_NET_INTERFACE_NAME)
1102 #define IFNAMSIZ CONFIG_NET_INTERFACE_NAME_LEN
1103 #else
1104 #define IFNAMSIZ Z_DEVICE_MAX_NAME_LEN
1105 #endif
1106 
1107 /** Interface description structure */
1108 struct ifreq {
1109 	char ifr_name[IFNAMSIZ]; /**< Network interface name */
1110 };
1111 /** @} */
1112 
1113 /**
1114  * @name Socket level options (SOL_SOCKET)
1115  * @{
1116  */
1117 /** Socket-level option */
1118 #define SOL_SOCKET 1
1119 
1120 /* Socket options for SOL_SOCKET level */
1121 
1122 /** Recording debugging information (ignored, for compatibility) */
1123 #define SO_DEBUG 1
1124 /** address reuse */
1125 #define SO_REUSEADDR 2
1126 /** Type of the socket */
1127 #define SO_TYPE 3
1128 /** Async error */
1129 #define SO_ERROR 4
1130 /** Bypass normal routing and send directly to host (ignored, for compatibility) */
1131 #define SO_DONTROUTE 5
1132 /** Transmission of broadcast messages is supported (ignored, for compatibility) */
1133 #define SO_BROADCAST 6
1134 
1135 /** Size of socket send buffer */
1136 #define SO_SNDBUF 7
1137 /** Size of socket recv buffer */
1138 #define SO_RCVBUF 8
1139 
1140 /** Enable sending keep-alive messages on connections */
1141 #define SO_KEEPALIVE 9
1142 /** Place out-of-band data into receive stream (ignored, for compatibility) */
1143 #define SO_OOBINLINE 10
1144 /** Socket priority */
1145 #define SO_PRIORITY 12
1146 /** Socket lingers on close (ignored, for compatibility) */
1147 #define SO_LINGER 13
1148 /** Allow multiple sockets to reuse a single port */
1149 #define SO_REUSEPORT 15
1150 
1151 /** Receive low watermark (ignored, for compatibility) */
1152 #define SO_RCVLOWAT 18
1153 /** Send low watermark (ignored, for compatibility) */
1154 #define SO_SNDLOWAT 19
1155 
1156 /**
1157  * Receive timeout
1158  * Applies to receive functions like recv(), but not to connect()
1159  */
1160 #define SO_RCVTIMEO 20
1161 /** Send timeout */
1162 #define SO_SNDTIMEO 21
1163 
1164 /** Bind a socket to an interface */
1165 #define SO_BINDTODEVICE	25
1166 
1167 /** Socket accepts incoming connections (ignored, for compatibility) */
1168 #define SO_ACCEPTCONN 30
1169 
1170 /** Timestamp TX RX or both packets. Supports multiple timestamp sources. */
1171 #define SO_TIMESTAMPING 37
1172 
1173 /** Protocol used with the socket */
1174 #define SO_PROTOCOL 38
1175 
1176 /** Domain used with SOCKET */
1177 #define SO_DOMAIN 39
1178 
1179 /** Enable SOCKS5 for Socket */
1180 #define SO_SOCKS5 60
1181 
1182 /** Socket TX time (when the data should be sent) */
1183 #define SO_TXTIME 61
1184 /** Socket TX time (same as SO_TXTIME) */
1185 #define SCM_TXTIME SO_TXTIME
1186 
1187 /** Timestamp generation flags */
1188 
1189 /** Request RX timestamps generated by network adapter. */
1190 #define SOF_TIMESTAMPING_RX_HARDWARE BIT(0)
1191 /**
1192  * Request TX timestamps generated by network adapter.
1193  * This can be enabled via socket option or control messages.
1194  */
1195 #define SOF_TIMESTAMPING_TX_HARDWARE BIT(1)
1196 
1197 /** */
1198 
1199 /** @} */
1200 
1201 /**
1202  * @name TCP level options (IPPROTO_TCP)
1203  * @{
1204  */
1205 /* Socket options for IPPROTO_TCP level */
1206 /** Disable TCP buffering (ignored, for compatibility) */
1207 #define TCP_NODELAY 1
1208 /** Start keepalives after this period (seconds) */
1209 #define TCP_KEEPIDLE 2
1210 /** Interval between keepalives (seconds) */
1211 #define TCP_KEEPINTVL 3
1212 /** Number of keepalives before dropping connection */
1213 #define TCP_KEEPCNT 4
1214 
1215 /** @} */
1216 
1217 /**
1218  * @name IPv4 level options (IPPROTO_IP)
1219  * @{
1220  */
1221 /* Socket options for IPPROTO_IP level */
1222 /** Set or receive the Type-Of-Service value for an outgoing packet. */
1223 #define IP_TOS 1
1224 
1225 /** Set or receive the Time-To-Live value for an outgoing packet. */
1226 #define IP_TTL 2
1227 
1228 /** Pass an IP_PKTINFO ancillary message that contains a
1229  *  pktinfo structure that supplies some information about the
1230  *  incoming packet.
1231  */
1232 #define IP_PKTINFO 8
1233 
1234 /**
1235  * @brief Incoming IPv4 packet information.
1236  *
1237  * Used as ancillary data when calling recvmsg() and IP_PKTINFO socket
1238  * option is set.
1239  */
1240 struct in_pktinfo {
1241 	unsigned int   ipi_ifindex;  /**< Network interface index */
1242 	struct in_addr ipi_spec_dst; /**< Local address */
1243 	struct in_addr ipi_addr;     /**< Header Destination address */
1244 };
1245 
1246 /** Set IPv4 multicast TTL value. */
1247 #define IP_MULTICAST_TTL 33
1248 /** Join IPv4 multicast group. */
1249 #define IP_ADD_MEMBERSHIP 35
1250 /** Leave IPv4 multicast group. */
1251 #define IP_DROP_MEMBERSHIP 36
1252 
1253 /**
1254  * @brief Struct used when joining or leaving a IPv4 multicast group.
1255  */
1256 struct ip_mreqn {
1257 	struct in_addr imr_multiaddr; /**< IP multicast group address */
1258 	struct in_addr imr_address;   /**< IP address of local interface */
1259 	int            imr_ifindex;   /**< Network interface index */
1260 };
1261 
1262 /** @} */
1263 
1264 /**
1265  * @name IPv6 level options (IPPROTO_IPV6)
1266  * @{
1267  */
1268 /* Socket options for IPPROTO_IPV6 level */
1269 /** Set the unicast hop limit for the socket. */
1270 #define IPV6_UNICAST_HOPS	16
1271 
1272 /** Set the multicast hop limit for the socket. */
1273 #define IPV6_MULTICAST_HOPS 18
1274 
1275 /** Join IPv6 multicast group. */
1276 #define IPV6_ADD_MEMBERSHIP 20
1277 
1278 /** Leave IPv6 multicast group. */
1279 #define IPV6_DROP_MEMBERSHIP 21
1280 
1281 /**
1282  * @brief Struct used when joining or leaving a IPv6 multicast group.
1283  */
1284 struct ipv6_mreq {
1285 	/** IPv6 multicast address of group */
1286 	struct in6_addr ipv6mr_multiaddr;
1287 
1288 	/** Network interface index of the local IPv6 address */
1289 	int ipv6mr_ifindex;
1290 };
1291 
1292 /** Don't support IPv4 access */
1293 #define IPV6_V6ONLY 26
1294 
1295 /** Pass an IPV6_RECVPKTINFO ancillary message that contains a
1296  *  in6_pktinfo structure that supplies some information about the
1297  *  incoming packet. See RFC 3542.
1298  */
1299 #define IPV6_RECVPKTINFO 49
1300 
1301 /** RFC5014: Source address selection. */
1302 #define IPV6_ADDR_PREFERENCES   72
1303 
1304 /** Prefer temporary address as source. */
1305 #define IPV6_PREFER_SRC_TMP             0x0001
1306 /** Prefer public address as source. */
1307 #define IPV6_PREFER_SRC_PUBLIC          0x0002
1308 /** Either public or temporary address is selected as a default source
1309  *  depending on the output interface configuration (this is the default value).
1310  *  This is Linux specific option not found in the RFC.
1311  */
1312 #define IPV6_PREFER_SRC_PUBTMP_DEFAULT  0x0100
1313 /** Prefer Care-of address as source. Ignored in Zephyr. */
1314 #define IPV6_PREFER_SRC_COA             0x0004
1315 /** Prefer Home address as source. Ignored in Zephyr. */
1316 #define IPV6_PREFER_SRC_HOME            0x0400
1317 /** Prefer CGA (Cryptographically Generated Address) address as source. Ignored in Zephyr. */
1318 #define IPV6_PREFER_SRC_CGA             0x0008
1319 /** Prefer non-CGA address as source. Ignored in Zephyr. */
1320 #define IPV6_PREFER_SRC_NONCGA          0x0800
1321 
1322 /**
1323  * @brief Incoming IPv6 packet information.
1324  *
1325  * Used as ancillary data when calling recvmsg() and IPV6_RECVPKTINFO socket
1326  * option is set.
1327  */
1328 struct in6_pktinfo {
1329 	struct in6_addr ipi6_addr;    /**< Destination IPv6 address */
1330 	unsigned int    ipi6_ifindex; /**< Receive interface index */
1331 };
1332 
1333 /** Set or receive the traffic class value for an outgoing packet. */
1334 #define IPV6_TCLASS 67
1335 /** @} */
1336 
1337 /**
1338  * @name Backlog size for listen()
1339  * @{
1340  */
1341 /** listen: The maximum backlog queue length */
1342 #define SOMAXCONN 128
1343 /** @} */
1344 
1345 /** @cond INTERNAL_HIDDEN */
1346 /**
1347  * @brief Registration information for a given BSD socket family.
1348  */
1349 struct net_socket_register {
1350 	int family;
1351 	bool is_offloaded;
1352 	bool (*is_supported)(int family, int type, int proto);
1353 	int (*handler)(int family, int type, int proto);
1354 #if defined(CONFIG_NET_SOCKETS_OBJ_CORE)
1355 	/* Store also the name of the socket type in order to be able to
1356 	 * print it later.
1357 	 */
1358 	const char * const name;
1359 #endif
1360 };
1361 
1362 #define NET_SOCKET_DEFAULT_PRIO CONFIG_NET_SOCKETS_PRIORITY_DEFAULT
1363 
1364 #define NET_SOCKET_GET_NAME(socket_name, prio)	\
1365 	__net_socket_register_##prio##_##socket_name
1366 
1367 #if defined(CONFIG_NET_SOCKETS_OBJ_CORE)
1368 #define K_OBJ_TYPE_SOCK  K_OBJ_TYPE_ID_GEN("SOCK")
1369 
1370 #define NET_SOCKET_REGISTER_NAME(_name)		\
1371 	.name = STRINGIFY(_name),
1372 #else
1373 #define NET_SOCKET_REGISTER_NAME(_name)
1374 #endif
1375 
1376 #define _NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, _is_offloaded) \
1377 	static const STRUCT_SECTION_ITERABLE(net_socket_register,	\
1378 			NET_SOCKET_GET_NAME(socket_name, prio)) = {	\
1379 		.family = _family,					\
1380 		.is_offloaded = _is_offloaded,				\
1381 		.is_supported = _is_supported,				\
1382 		.handler = _handler,					\
1383 		NET_SOCKET_REGISTER_NAME(socket_name)			\
1384 	}
1385 
1386 #define NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler) \
1387 	_NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, false)
1388 
1389 #define NET_SOCKET_OFFLOAD_REGISTER(socket_name, prio, _family, _is_supported, _handler) \
1390 	_NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, true)
1391 
1392 /** @endcond */
1393 
1394 #ifdef __cplusplus
1395 }
1396 #endif
1397 
1398 #include <zephyr/syscalls/socket.h>
1399 
1400 /**
1401  * @}
1402  */
1403 
1404 /* Avoid circular loops with POSIX socket headers.
1405  * We have these includes here so that we do not need
1406  * to change the applications that were only including
1407  * zephyr/net/socket.h header file.
1408  *
1409  * Additionally, if non-zephyr-prefixed headers are used here,
1410  * native_sim pulls in those from the host rather than Zephyr's.
1411  *
1412  * This should be removed when CONFIG_NET_SOCKETS_POSIX_NAMES is removed.
1413  */
1414 #if defined(CONFIG_POSIX_API)
1415 #if !defined(ZEPHYR_INCLUDE_POSIX_ARPA_INET_H_)
1416 #include <zephyr/posix/arpa/inet.h>
1417 #endif
1418 #if !defined(ZEPHYR_INCLUDE_POSIX_NETDB_H_)
1419 #include <zephyr/posix/netdb.h>
1420 #endif
1421 #if !defined(ZEPHYR_INCLUDE_POSIX_UNISTD_H_)
1422 #include <zephyr/posix/unistd.h>
1423 #endif
1424 #if !defined(ZEPHYR_INCLUDE_POSIX_POLL_H_)
1425 #include <zephyr/posix/poll.h>
1426 #endif
1427 #if !defined(ZEPHYR_INCLUDE_POSIX_SYS_SOCKET_H_)
1428 #include <zephyr/posix/sys/socket.h>
1429 #endif
1430 #endif /* CONFIG_POSIX_API */
1431 
1432 #endif /* ZEPHYR_INCLUDE_NET_SOCKET_H_ */
1433