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