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 <stdlib.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct zsock_pollfd {
38 	int fd;
39 	short events;
40 	short revents;
41 };
42 
43 /* ZSOCK_POLL* values are compatible with Linux */
44 /** zsock_poll: Poll for readability */
45 #define ZSOCK_POLLIN 1
46 /** zsock_poll: Compatibility value, ignored */
47 #define ZSOCK_POLLPRI 2
48 /** zsock_poll: Poll for writability */
49 #define ZSOCK_POLLOUT 4
50 /** zsock_poll: Poll results in error condition (output value only) */
51 #define ZSOCK_POLLERR 8
52 /** zsock_poll: Poll detected closed connection (output value only) */
53 #define ZSOCK_POLLHUP 0x10
54 /** zsock_poll: Invalid socket (output value only) */
55 #define ZSOCK_POLLNVAL 0x20
56 
57 /** zsock_recv: Read data without removing it from socket input queue */
58 #define ZSOCK_MSG_PEEK 0x02
59 /** zsock_recv: return the real length of the datagram, even when it was longer
60  *  than the passed buffer
61  */
62 #define ZSOCK_MSG_TRUNC 0x20
63 /** zsock_recv/zsock_send: Override operation to non-blocking */
64 #define ZSOCK_MSG_DONTWAIT 0x40
65 /** zsock_recv: block until the full amount of data can be returned */
66 #define ZSOCK_MSG_WAITALL 0x100
67 
68 /* Well-known values, e.g. from Linux man 2 shutdown:
69  * "The constants SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2,
70  * respectively". Some software uses numeric values.
71  */
72 /** zsock_shutdown: Shut down for reading */
73 #define ZSOCK_SHUT_RD 0
74 /** zsock_shutdown: Shut down for writing */
75 #define ZSOCK_SHUT_WR 1
76 /** zsock_shutdown: Shut down for both reading and writing */
77 #define ZSOCK_SHUT_RDWR 2
78 
79 /** Protocol level for TLS.
80  *  Here, the same socket protocol level for TLS as in Linux was used.
81  */
82 #define SOL_TLS 282
83 
84 /**
85  *  @defgroup secure_sockets_options Socket options for TLS
86  *  @{
87  */
88 
89 /** Socket option to select TLS credentials to use. It accepts and returns an
90  *  array of sec_tag_t that indicate which TLS credentials should be used with
91  *  specific socket.
92  */
93 #define TLS_SEC_TAG_LIST 1
94 /** Write-only socket option to set hostname. It accepts a string containing
95  *  the hostname (may be NULL to disable hostname verification). By default,
96  *  hostname check is enforced for TLS clients.
97  */
98 #define TLS_HOSTNAME 2
99 /** Socket option to select ciphersuites to use. It accepts and returns an array
100  *  of integers with IANA assigned ciphersuite identifiers.
101  *  If not set, socket will allow all ciphersuites available in the system
102  *  (mbedTLS default behavior).
103  */
104 #define TLS_CIPHERSUITE_LIST 3
105 /** Read-only socket option to read a ciphersuite chosen during TLS handshake.
106  *  It returns an integer containing an IANA assigned ciphersuite identifier
107  *  of chosen ciphersuite.
108  */
109 #define TLS_CIPHERSUITE_USED 4
110 /** Write-only socket option to set peer verification level for TLS connection.
111  *  This option accepts an integer with a peer verification level, compatible
112  *  with mbedTLS values:
113  *    - 0 - none
114  *    - 1 - optional
115  *    - 2 - required
116  *
117  *  If not set, socket will use mbedTLS defaults (none for servers, required
118  *  for clients).
119  */
120 #define TLS_PEER_VERIFY 5
121 /** Write-only socket option to set role for DTLS connection. This option
122  *  is irrelevant for TLS connections, as for them role is selected based on
123  *  connect()/listen() usage. By default, DTLS will assume client role.
124  *  This option accepts an integer with a TLS role, compatible with
125  *  mbedTLS values:
126  *    - 0 - client
127  *    - 1 - server
128  */
129 #define TLS_DTLS_ROLE 6
130 /** Socket option for setting the supported Application Layer Protocols.
131  *  It accepts and returns a const char array of NULL terminated strings
132  *  representing the supported application layer protocols listed during
133  *  the TLS handshake.
134  */
135 #define TLS_ALPN_LIST 7
136 /** Socket option to set DTLS handshake timeout. The timeout starts at min,
137  *  and upon retransmission the timeout is doubled util max is reached.
138  *  Min and max arguments are separate options. The time unit is ms.
139  */
140 #define TLS_DTLS_HANDSHAKE_TIMEOUT_MIN 8
141 #define TLS_DTLS_HANDSHAKE_TIMEOUT_MAX 9
142 /** Socket option for preventing certificates from being copied to the mbedTLS
143  *  heap if possible. The option is only effective for DER certificates and is
144  *  ignored for PEM certificates.
145  */
146 #define TLS_CERT_NOCOPY	       10
147 /** TLS socket option to use with offloading. The option instructs the network
148  *  stack only to offload underlying TCP/UDP communication. The TLS/DTLS
149  *  operation is handled by a native TLS/DTLS socket implementation from Zephyr.
150  *
151  *  Note, that this option is only applicable if socket dispatcher is used
152  *  (CONFIG_NET_SOCKETS_OFFLOAD_DISPATCHER is enabled).
153  *  In such case, it should be the first socket option set on a newly created
154  *  socket. After that, the application may use SO_BINDTODEVICE to choose the
155  *  dedicated network interface for the underlying TCP/UDP socket.
156  */
157 #define TLS_NATIVE 11
158 /** Socket option to control TLS session caching on a socket. Accepted values:
159  *  - 0 - Disabled.
160  *  - 1 - Enabled.
161  */
162 #define TLS_SESSION_CACHE 12
163 /** Write-only socket option to purge session cache immediately.
164  *  This option accepts any value.
165  */
166 #define TLS_SESSION_CACHE_PURGE 13
167 
168 /** @} */
169 
170 /* Valid values for TLS_PEER_VERIFY option */
171 #define TLS_PEER_VERIFY_NONE 0     /**< Peer verification disabled. */
172 #define TLS_PEER_VERIFY_OPTIONAL 1 /**< Peer verification optional. */
173 #define TLS_PEER_VERIFY_REQUIRED 2 /**< Peer verification required. */
174 
175 /* Valid values for TLS_DTLS_ROLE option */
176 #define TLS_DTLS_ROLE_CLIENT 0 /**< Client role in a DTLS session. */
177 #define TLS_DTLS_ROLE_SERVER 1 /**< Server role in a DTLS session. */
178 
179 /* Valid values for TLS_CERT_NOCOPY option */
180 #define TLS_CERT_NOCOPY_NONE 0     /**< Cert duplicated in heap */
181 #define TLS_CERT_NOCOPY_OPTIONAL 1 /**< Cert not copied in heap if DER */
182 
183 /* Valid values for TLS_SESSION_CACHE option */
184 #define TLS_SESSION_CACHE_DISABLED 0 /**< Disable TLS session caching. */
185 #define TLS_SESSION_CACHE_ENABLED 1 /**< Enable TLS session caching. */
186 
187 struct zsock_addrinfo {
188 	struct zsock_addrinfo *ai_next;
189 	int ai_flags;
190 	int ai_family;
191 	int ai_socktype;
192 	int ai_protocol;
193 	socklen_t ai_addrlen;
194 	struct sockaddr *ai_addr;
195 	char *ai_canonname;
196 
197 	struct sockaddr _ai_addr;
198 	char _ai_canonname[DNS_MAX_NAME_SIZE + 1];
199 };
200 
201 /**
202  * @brief Obtain a file descriptor's associated net context
203  *
204  * With CONFIG_USERSPACE enabled, the kernel's object permission system
205  * must apply to socket file descriptors. When a socket is opened, by default
206  * only the caller has permission, access by other threads will fail unless
207  * they have been specifically granted permission.
208  *
209  * This is achieved by tagging data structure definitions that implement the
210  * underlying object associated with a network socket file descriptor with
211  * '__net_socket`. All pointers to instances of these will be known to the
212  * kernel as kernel objects with type K_OBJ_NET_SOCKET.
213  *
214  * This API is intended for threads that need to grant access to the object
215  * associated with a particular file descriptor to another thread. The
216  * returned pointer represents the underlying K_OBJ_NET_SOCKET  and
217  * may be passed to APIs like k_object_access_grant().
218  *
219  * In a system like Linux which has the notion of threads running in processes
220  * in a shared virtual address space, this sort of management is unnecessary as
221  * the scope of file descriptors is implemented at the process level.
222  *
223  * However in Zephyr the file descriptor scope is global, and MPU-based systems
224  * are not able to implement a process-like model due to the lack of memory
225  * virtualization hardware. They use discrete object permissions and memory
226  * domains instead to define thread access scope.
227  *
228  * User threads will have no direct access to the returned object
229  * and will fault if they try to access its memory; the pointer can only be
230  * used to make permission assignment calls, which follow exactly the rules
231  * for other kernel objects like device drivers and IPC.
232  *
233  * @param sock file descriptor
234  * @return pointer to associated network socket object, or NULL if the
235  *         file descriptor wasn't valid or the caller had no access permission
236  */
237 __syscall void *zsock_get_context_object(int sock);
238 
239 /**
240  * @brief Create a network socket
241  *
242  * @details
243  * @rst
244  * See `POSIX.1-2017 article
245  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html>`__
246  * for normative description.
247  * This function is also exposed as ``socket()``
248  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
249  * @endrst
250  *
251  * If CONFIG_USERSPACE is enabled, the caller will be granted access to the
252  * context object associated with the returned file descriptor.
253  * @see zsock_get_context_object()
254  *
255  */
256 __syscall int zsock_socket(int family, int type, int proto);
257 
258 /**
259  * @brief Create an unnamed pair of connected sockets
260  *
261  * @details
262  * @rst
263  * See `POSIX.1-2017 article
264  * <https://pubs.opengroup.org/onlinepubs/009695399/functions/socketpair.html>`__
265  * for normative description.
266  * This function is also exposed as ``socketpair()``
267  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
268  * @endrst
269  */
270 __syscall int zsock_socketpair(int family, int type, int proto, int *sv);
271 
272 /**
273  * @brief Close a network socket
274  *
275  * @details
276  * @rst
277  * Close a network socket.
278  * This function is also exposed as ``close()``
279  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined (in which case it
280  * may conflict with generic POSIX ``close()`` function).
281  * @endrst
282  */
283 __syscall int zsock_close(int sock);
284 
285 /**
286  * @brief Shutdown socket send/receive operations
287  *
288  * @details
289  * @rst
290  * See `POSIX.1-2017 article
291  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html>`__
292  * for normative description, but currently this function has no effect in
293  * Zephyr and provided solely for compatibility with existing code.
294  * This function is also exposed as ``shutdown()``
295  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
296  * @endrst
297  */
298 __syscall int zsock_shutdown(int sock, int how);
299 
300 /**
301  * @brief Bind a socket to a local network address
302  *
303  * @details
304  * @rst
305  * See `POSIX.1-2017 article
306  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html>`__
307  * for normative description.
308  * This function is also exposed as ``bind()``
309  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
310  * @endrst
311  */
312 __syscall int zsock_bind(int sock, const struct sockaddr *addr,
313 			 socklen_t addrlen);
314 
315 /**
316  * @brief Connect a socket to a peer network address
317  *
318  * @details
319  * @rst
320  * See `POSIX.1-2017 article
321  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html>`__
322  * for normative description.
323  * This function is also exposed as ``connect()``
324  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
325  * @endrst
326  */
327 __syscall int zsock_connect(int sock, const struct sockaddr *addr,
328 			    socklen_t addrlen);
329 
330 /**
331  * @brief Set up a STREAM socket to accept peer connections
332  *
333  * @details
334  * @rst
335  * See `POSIX.1-2017 article
336  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html>`__
337  * for normative description.
338  * This function is also exposed as ``listen()``
339  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
340  * @endrst
341  */
342 __syscall int zsock_listen(int sock, int backlog);
343 
344 /**
345  * @brief Accept a connection on listening socket
346  *
347  * @details
348  * @rst
349  * See `POSIX.1-2017 article
350  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html>`__
351  * for normative description.
352  * This function is also exposed as ``accept()``
353  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
354  * @endrst
355  */
356 __syscall int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
357 
358 /**
359  * @brief Send data to an arbitrary network address
360  *
361  * @details
362  * @rst
363  * See `POSIX.1-2017 article
364  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html>`__
365  * for normative description.
366  * This function is also exposed as ``sendto()``
367  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
368  * @endrst
369  */
370 __syscall ssize_t zsock_sendto(int sock, const void *buf, size_t len,
371 			       int flags, const struct sockaddr *dest_addr,
372 			       socklen_t addrlen);
373 
374 /**
375  * @brief Send data to a connected peer
376  *
377  * @details
378  * @rst
379  * See `POSIX.1-2017 article
380  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html>`__
381  * for normative description.
382  * This function is also exposed as ``send()``
383  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
384  * @endrst
385  */
zsock_send(int sock,const void * buf,size_t len,int flags)386 static inline ssize_t zsock_send(int sock, const void *buf, size_t len,
387 				 int flags)
388 {
389 	return zsock_sendto(sock, buf, len, flags, NULL, 0);
390 }
391 
392 /**
393  * @brief Send data to an arbitrary network address
394  *
395  * @details
396  * @rst
397  * See `POSIX.1-2017 article
398  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html>`__
399  * for normative description.
400  * This function is also exposed as ``sendmsg()``
401  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
402  * @endrst
403  */
404 __syscall ssize_t zsock_sendmsg(int sock, const struct msghdr *msg,
405 				int flags);
406 
407 /**
408  * @brief Receive data from an arbitrary network address
409  *
410  * @details
411  * @rst
412  * See `POSIX.1-2017 article
413  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html>`__
414  * for normative description.
415  * This function is also exposed as ``recvfrom()``
416  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
417  * @endrst
418  */
419 __syscall ssize_t zsock_recvfrom(int sock, void *buf, size_t max_len,
420 				 int flags, struct sockaddr *src_addr,
421 				 socklen_t *addrlen);
422 
423 /**
424  * @brief Receive data from a connected peer
425  *
426  * @details
427  * @rst
428  * See `POSIX.1-2017 article
429  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html>`__
430  * for normative description.
431  * This function is also exposed as ``recv()``
432  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
433  * @endrst
434  */
zsock_recv(int sock,void * buf,size_t max_len,int flags)435 static inline ssize_t zsock_recv(int sock, void *buf, size_t max_len,
436 				 int flags)
437 {
438 	return zsock_recvfrom(sock, buf, max_len, flags, NULL, NULL);
439 }
440 
441 /**
442  * @brief Control blocking/non-blocking mode of a socket
443  *
444  * @details
445  * @rst
446  * This functions allow to (only) configure a socket for blocking or
447  * non-blocking operation (O_NONBLOCK).
448  * This function is also exposed as ``fcntl()``
449  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined (in which case
450  * it may conflict with generic POSIX ``fcntl()`` function).
451  * @endrst
452  */
453 __syscall int zsock_fcntl(int sock, int cmd, int flags);
454 
455 /**
456  * @brief Efficiently poll multiple sockets for events
457  *
458  * @details
459  * @rst
460  * See `POSIX.1-2017 article
461  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html>`__
462  * for normative description.
463  * This function is also exposed as ``poll()``
464  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined (in which case
465  * it may conflict with generic POSIX ``poll()`` function).
466  * @endrst
467  */
468 __syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
469 
470 /**
471  * @brief Get various socket options
472  *
473  * @details
474  * @rst
475  * See `POSIX.1-2017 article
476  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html>`__
477  * for normative description. In Zephyr this function supports a subset of
478  * socket options described by POSIX, but also some additional options
479  * available in Linux (some options are dummy and provided to ease porting
480  * of existing code).
481  * This function is also exposed as ``getsockopt()``
482  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
483  * @endrst
484  */
485 __syscall int zsock_getsockopt(int sock, int level, int optname,
486 			       void *optval, socklen_t *optlen);
487 
488 /**
489  * @brief Set various socket options
490  *
491  * @details
492  * @rst
493  * See `POSIX.1-2017 article
494  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html>`__
495  * for normative description. In Zephyr this function supports a subset of
496  * socket options described by POSIX, but also some additional options
497  * available in Linux (some options are dummy and provided to ease porting
498  * of existing code).
499  * This function is also exposed as ``setsockopt()``
500  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
501  * @endrst
502  */
503 __syscall int zsock_setsockopt(int sock, int level, int optname,
504 			       const void *optval, socklen_t optlen);
505 
506 /**
507  * @brief Get peer name
508  *
509  * @details
510  * @rst
511  * See `POSIX.1-2017 article
512  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html>`__
513  * for normative description.
514  * This function is also exposed as ``getpeername()``
515  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
516  * @endrst
517  */
518 __syscall int zsock_getpeername(int sock, struct sockaddr *addr,
519 				socklen_t *addrlen);
520 
521 /**
522  * @brief Get socket name
523  *
524  * @details
525  * @rst
526  * See `POSIX.1-2017 article
527  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html>`__
528  * for normative description.
529  * This function is also exposed as ``getsockname()``
530  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
531  * @endrst
532  */
533 __syscall int zsock_getsockname(int sock, struct sockaddr *addr,
534 				socklen_t *addrlen);
535 
536 /**
537  * @brief Get local host name
538  *
539  * @details
540  * @rst
541  * See `POSIX.1-2017 article
542  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html>`__
543  * for normative description.
544  * This function is also exposed as ``gethostname()``
545  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
546  * @endrst
547  */
548 __syscall int zsock_gethostname(char *buf, size_t len);
549 
550 /**
551  * @brief Convert network address from internal to numeric ASCII form
552  *
553  * @details
554  * @rst
555  * See `POSIX.1-2017 article
556  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_ntop.html>`__
557  * for normative description.
558  * This function is also exposed as ``inet_ntop()``
559  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
560  * @endrst
561  */
zsock_inet_ntop(sa_family_t family,const void * src,char * dst,size_t size)562 static inline char *zsock_inet_ntop(sa_family_t family, const void *src,
563 				    char *dst, size_t size)
564 {
565 	return net_addr_ntop(family, src, dst, size);
566 }
567 
568 /**
569  * @brief Convert network address from numeric ASCII form to internal representation
570  *
571  * @details
572  * @rst
573  * See `POSIX.1-2017 article
574  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/inet_pton.html>`__
575  * for normative description.
576  * This function is also exposed as ``inet_pton()``
577  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
578  * @endrst
579  */
580 __syscall int zsock_inet_pton(sa_family_t family, const char *src, void *dst);
581 
582 /** @cond INTERNAL_HIDDEN */
583 __syscall int z_zsock_getaddrinfo_internal(const char *host,
584 					   const char *service,
585 					   const struct zsock_addrinfo *hints,
586 					   struct zsock_addrinfo *res);
587 /** @endcond */
588 
589 /* Flags for getaddrinfo() hints. */
590 
591 /** Address for bind() (vs for connect()) */
592 #define AI_PASSIVE 0x1
593 /** Fill in ai_canonname */
594 #define AI_CANONNAME 0x2
595 /** Assume host address is in numeric notation, don't DNS lookup */
596 #define AI_NUMERICHOST 0x4
597 /** May return IPv4 mapped address for IPv6  */
598 #define AI_V4MAPPED 0x8
599 /** May return both native IPv6 and mapped IPv4 address for IPv6 */
600 #define AI_ALL 0x10
601 /** IPv4/IPv6 support depends on local system config */
602 #define AI_ADDRCONFIG 0x20
603 /** Assume service (port) is numeric */
604 #define AI_NUMERICSERV 0x400
605 
606 /**
607  * @brief Resolve a domain name to one or more network addresses
608  *
609  * @details
610  * @rst
611  * See `POSIX.1-2017 article
612  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getaddrinfo.html>`__
613  * for normative description.
614  * This function is also exposed as ``getaddrinfo()``
615  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
616  * @endrst
617  */
618 int zsock_getaddrinfo(const char *host, const char *service,
619 		      const struct zsock_addrinfo *hints,
620 		      struct zsock_addrinfo **res);
621 
622 /**
623  * @brief Free results returned by zsock_getaddrinfo()
624  *
625  * @details
626  * @rst
627  * See `POSIX.1-2017 article
628  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/freeaddrinfo.html>`__
629  * for normative description.
630  * This function is also exposed as ``freeaddrinfo()``
631  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
632  * @endrst
633  */
634 void zsock_freeaddrinfo(struct zsock_addrinfo *ai);
635 
636 /**
637  * @brief Convert zsock_getaddrinfo() error code to textual message
638  *
639  * @details
640  * @rst
641  * See `POSIX.1-2017 article
642  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/gai_strerror.html>`__
643  * for normative description.
644  * This function is also exposed as ``gai_strerror()``
645  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
646  * @endrst
647  */
648 const char *zsock_gai_strerror(int errcode);
649 
650 /** zsock_getnameinfo(): Resolve to numeric address. */
651 #define NI_NUMERICHOST 1
652 /** zsock_getnameinfo(): Resolve to numeric port number. */
653 #define NI_NUMERICSERV 2
654 /** zsock_getnameinfo(): Return only hostname instead of FQDN */
655 #define NI_NOFQDN 4
656 /** zsock_getnameinfo(): Dummy option for compatibility */
657 #define NI_NAMEREQD 8
658 /** zsock_getnameinfo(): Dummy option for compatibility */
659 #define NI_DGRAM 16
660 
661 /* POSIX extensions */
662 
663 /** zsock_getnameinfo(): Max supported hostname length */
664 #ifndef NI_MAXHOST
665 #define NI_MAXHOST 64
666 #endif
667 
668 /**
669  * @brief Resolve a network address to a domain name or ASCII address
670  *
671  * @details
672  * @rst
673  * See `POSIX.1-2017 article
674  * <http://pubs.opengroup.org/onlinepubs/9699919799/functions/getnameinfo.html>`__
675  * for normative description.
676  * This function is also exposed as ``getnameinfo()``
677  * if :kconfig:option:`CONFIG_NET_SOCKETS_POSIX_NAMES` is defined.
678  * @endrst
679  */
680 int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
681 		      char *host, socklen_t hostlen,
682 		      char *serv, socklen_t servlen, int flags);
683 
684 #if defined(CONFIG_NET_SOCKETS_POSIX_NAMES)
685 
686 #define pollfd zsock_pollfd
687 
688 /** POSIX wrapper for @ref zsock_socket */
socket(int family,int type,int proto)689 static inline int socket(int family, int type, int proto)
690 {
691 	return zsock_socket(family, type, proto);
692 }
693 
694 /** POSIX wrapper for @ref zsock_socketpair */
socketpair(int family,int type,int proto,int sv[2])695 static inline int socketpair(int family, int type, int proto, int sv[2])
696 {
697 	return zsock_socketpair(family, type, proto, sv);
698 }
699 
700 /** POSIX wrapper for @ref zsock_close */
close(int sock)701 static inline int close(int sock)
702 {
703 	return zsock_close(sock);
704 }
705 
706 /** POSIX wrapper for @ref zsock_shutdown */
shutdown(int sock,int how)707 static inline int shutdown(int sock, int how)
708 {
709 	return zsock_shutdown(sock, how);
710 }
711 
712 /** POSIX wrapper for @ref zsock_bind */
bind(int sock,const struct sockaddr * addr,socklen_t addrlen)713 static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
714 {
715 	return zsock_bind(sock, addr, addrlen);
716 }
717 
718 /** POSIX wrapper for @ref zsock_connect */
connect(int sock,const struct sockaddr * addr,socklen_t addrlen)719 static inline int connect(int sock, const struct sockaddr *addr,
720 			  socklen_t addrlen)
721 {
722 	return zsock_connect(sock, addr, addrlen);
723 }
724 
725 /** POSIX wrapper for @ref zsock_listen */
listen(int sock,int backlog)726 static inline int listen(int sock, int backlog)
727 {
728 	return zsock_listen(sock, backlog);
729 }
730 
731 /** POSIX wrapper for @ref zsock_accept */
accept(int sock,struct sockaddr * addr,socklen_t * addrlen)732 static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
733 {
734 	return zsock_accept(sock, addr, addrlen);
735 }
736 
737 /** POSIX wrapper for @ref zsock_send */
send(int sock,const void * buf,size_t len,int flags)738 static inline ssize_t send(int sock, const void *buf, size_t len, int flags)
739 {
740 	return zsock_send(sock, buf, len, flags);
741 }
742 
743 /** POSIX wrapper for @ref zsock_recv */
recv(int sock,void * buf,size_t max_len,int flags)744 static inline ssize_t recv(int sock, void *buf, size_t max_len, int flags)
745 {
746 	return zsock_recv(sock, buf, max_len, flags);
747 }
748 
749 /*
750  * Need this wrapper because newer GCC versions got too smart and "typecheck"
751  * even macros, so '#define fcntl zsock_fcntl' leads to error.
752  */
zsock_fcntl_wrapper(int sock,int cmd,...)753 static inline int zsock_fcntl_wrapper(int sock, int cmd, ...)
754 {
755 	va_list args;
756 	int flags;
757 
758 	va_start(args, cmd);
759 	flags = va_arg(args, int);
760 	va_end(args);
761 	return zsock_fcntl(sock, cmd, flags);
762 }
763 
764 #define fcntl zsock_fcntl_wrapper
765 
766 /** 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)767 static inline ssize_t sendto(int sock, const void *buf, size_t len, int flags,
768 			     const struct sockaddr *dest_addr,
769 			     socklen_t addrlen)
770 {
771 	return zsock_sendto(sock, buf, len, flags, dest_addr, addrlen);
772 }
773 
774 /** POSIX wrapper for @ref zsock_sendmsg */
sendmsg(int sock,const struct msghdr * message,int flags)775 static inline ssize_t sendmsg(int sock, const struct msghdr *message,
776 			      int flags)
777 {
778 	return zsock_sendmsg(sock, message, flags);
779 }
780 
781 /** POSIX wrapper for @ref zsock_recvfrom */
recvfrom(int sock,void * buf,size_t max_len,int flags,struct sockaddr * src_addr,socklen_t * addrlen)782 static inline ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags,
783 			       struct sockaddr *src_addr, socklen_t *addrlen)
784 {
785 	return zsock_recvfrom(sock, buf, max_len, flags, src_addr, addrlen);
786 }
787 
788 /** POSIX wrapper for @ref zsock_poll */
poll(struct zsock_pollfd * fds,int nfds,int timeout)789 static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
790 {
791 	return zsock_poll(fds, nfds, timeout);
792 }
793 
794 /** POSIX wrapper for @ref zsock_getsockopt */
getsockopt(int sock,int level,int optname,void * optval,socklen_t * optlen)795 static inline int getsockopt(int sock, int level, int optname,
796 			     void *optval, socklen_t *optlen)
797 {
798 	return zsock_getsockopt(sock, level, optname, optval, optlen);
799 }
800 
801 /** POSIX wrapper for @ref zsock_setsockopt */
setsockopt(int sock,int level,int optname,const void * optval,socklen_t optlen)802 static inline int setsockopt(int sock, int level, int optname,
803 			     const void *optval, socklen_t optlen)
804 {
805 	return zsock_setsockopt(sock, level, optname, optval, optlen);
806 }
807 
808 /** POSIX wrapper for @ref zsock_getpeername */
getpeername(int sock,struct sockaddr * addr,socklen_t * addrlen)809 static inline int getpeername(int sock, struct sockaddr *addr,
810 			      socklen_t *addrlen)
811 {
812 	return zsock_getpeername(sock, addr, addrlen);
813 }
814 
815 /** POSIX wrapper for @ref zsock_getsockname */
getsockname(int sock,struct sockaddr * addr,socklen_t * addrlen)816 static inline int getsockname(int sock, struct sockaddr *addr,
817 			      socklen_t *addrlen)
818 {
819 	return zsock_getsockname(sock, addr, addrlen);
820 }
821 
822 /** POSIX wrapper for @ref zsock_getaddrinfo */
getaddrinfo(const char * host,const char * service,const struct zsock_addrinfo * hints,struct zsock_addrinfo ** res)823 static inline int getaddrinfo(const char *host, const char *service,
824 			      const struct zsock_addrinfo *hints,
825 			      struct zsock_addrinfo **res)
826 {
827 	return zsock_getaddrinfo(host, service, hints, res);
828 }
829 
830 /** POSIX wrapper for @ref zsock_freeaddrinfo */
freeaddrinfo(struct zsock_addrinfo * ai)831 static inline void freeaddrinfo(struct zsock_addrinfo *ai)
832 {
833 	zsock_freeaddrinfo(ai);
834 }
835 
836 /** POSIX wrapper for @ref zsock_gai_strerror */
gai_strerror(int errcode)837 static inline const char *gai_strerror(int errcode)
838 {
839 	return zsock_gai_strerror(errcode);
840 }
841 
842 /** 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)843 static inline int getnameinfo(const struct sockaddr *addr, socklen_t addrlen,
844 			      char *host, socklen_t hostlen,
845 			      char *serv, socklen_t servlen, int flags)
846 {
847 	return zsock_getnameinfo(addr, addrlen, host, hostlen,
848 				 serv, servlen, flags);
849 }
850 
851 #define addrinfo zsock_addrinfo
852 
853 /** POSIX wrapper for @ref zsock_gethostname */
gethostname(char * buf,size_t len)854 static inline int gethostname(char *buf, size_t len)
855 {
856 	return zsock_gethostname(buf, len);
857 }
858 
859 /** POSIX wrapper for @ref zsock_inet_pton */
inet_pton(sa_family_t family,const char * src,void * dst)860 static inline int inet_pton(sa_family_t family, const char *src, void *dst)
861 {
862 	return zsock_inet_pton(family, src, dst);
863 }
864 
865 /** POSIX wrapper for @ref zsock_inet_ntop */
inet_ntop(sa_family_t family,const void * src,char * dst,size_t size)866 static inline char *inet_ntop(sa_family_t family, const void *src, char *dst,
867 			      size_t size)
868 {
869 	return zsock_inet_ntop(family, src, dst, size);
870 }
871 
872 /** POSIX wrapper for @ref ZSOCK_POLLIN */
873 #define POLLIN ZSOCK_POLLIN
874 /** POSIX wrapper for @ref ZSOCK_POLLOUT */
875 #define POLLOUT ZSOCK_POLLOUT
876 /** POSIX wrapper for @ref ZSOCK_POLLERR */
877 #define POLLERR ZSOCK_POLLERR
878 /** POSIX wrapper for @ref ZSOCK_POLLHUP */
879 #define POLLHUP ZSOCK_POLLHUP
880 /** POSIX wrapper for @ref ZSOCK_POLLNVAL */
881 #define POLLNVAL ZSOCK_POLLNVAL
882 
883 /** POSIX wrapper for @ref ZSOCK_MSG_PEEK */
884 #define MSG_PEEK ZSOCK_MSG_PEEK
885 /** POSIX wrapper for @ref ZSOCK_MSG_TRUNC */
886 #define MSG_TRUNC ZSOCK_MSG_TRUNC
887 /** POSIX wrapper for @ref ZSOCK_MSG_DONTWAIT */
888 #define MSG_DONTWAIT ZSOCK_MSG_DONTWAIT
889 /** POSIX wrapper for @ref ZSOCK_MSG_WAITALL */
890 #define MSG_WAITALL ZSOCK_MSG_WAITALL
891 
892 /** POSIX wrapper for @ref ZSOCK_SHUT_RD */
893 #define SHUT_RD ZSOCK_SHUT_RD
894 /** POSIX wrapper for @ref ZSOCK_SHUT_WR */
895 #define SHUT_WR ZSOCK_SHUT_WR
896 /** POSIX wrapper for @ref ZSOCK_SHUT_RDWR */
897 #define SHUT_RDWR ZSOCK_SHUT_RDWR
898 
899 /** POSIX wrapper for @ref DNS_EAI_BADFLAGS */
900 #define EAI_BADFLAGS DNS_EAI_BADFLAGS
901 /** POSIX wrapper for @ref DNS_EAI_NONAME */
902 #define EAI_NONAME DNS_EAI_NONAME
903 /** POSIX wrapper for @ref DNS_EAI_AGAIN */
904 #define EAI_AGAIN DNS_EAI_AGAIN
905 /** POSIX wrapper for @ref DNS_EAI_FAIL */
906 #define EAI_FAIL DNS_EAI_FAIL
907 /** POSIX wrapper for @ref DNS_EAI_NODATA */
908 #define EAI_NODATA DNS_EAI_NODATA
909 /** POSIX wrapper for @ref DNS_EAI_MEMORY */
910 #define EAI_MEMORY DNS_EAI_MEMORY
911 /** POSIX wrapper for @ref DNS_EAI_SYSTEM */
912 #define EAI_SYSTEM DNS_EAI_SYSTEM
913 /** POSIX wrapper for @ref DNS_EAI_SERVICE */
914 #define EAI_SERVICE DNS_EAI_SERVICE
915 /** POSIX wrapper for @ref DNS_EAI_SOCKTYPE */
916 #define EAI_SOCKTYPE DNS_EAI_SOCKTYPE
917 /** POSIX wrapper for @ref DNS_EAI_FAMILY */
918 #define EAI_FAMILY DNS_EAI_FAMILY
919 #endif /* defined(CONFIG_NET_SOCKETS_POSIX_NAMES) */
920 
921 #define IFNAMSIZ Z_DEVICE_MAX_NAME_LEN
922 
923 /** Interface description structure */
924 struct ifreq {
925 	char ifr_name[IFNAMSIZ]; /* Interface name */
926 };
927 
928 /** sockopt: Socket-level option */
929 #define SOL_SOCKET 1
930 
931 /* Socket options for SOL_SOCKET level */
932 
933 /** sockopt: Recording debugging information (ignored, for compatibility) */
934 #define SO_DEBUG 1
935 /** sockopt: address reuse (ignored, for compatibility) */
936 #define SO_REUSEADDR 2
937 /** sockopt: Type of the socket */
938 #define SO_TYPE 3
939 /** sockopt: Async error (ignored, for compatibility) */
940 #define SO_ERROR 4
941 /** sockopt: Bypass normal routing and send directly to host (ignored, for compatibility) */
942 #define SO_DONTROUTE 5
943 /** sockopt: Transmission of broadcast messages is supported (ignored, for compatibility) */
944 #define SO_BROADCAST 6
945 
946 /** sockopt: Size of socket socket send buffer (ignored, for compatibility) */
947 #define SO_SNDBUF 7
948 /** sockopt: Size of socket recv buffer */
949 #define SO_RCVBUF 8
950 
951 /** sockopt: Enable sending keep-alive messages on connections (ignored, for compatibility) */
952 #define SO_KEEPALIVE 9
953 /** sockopt: Place out-of-band data into receive stream (ignored, for compatibility) */
954 #define SO_OOBINLINE 10
955 /** sockopt: Socket lingers on close (ignored, for compatibility) */
956 #define SO_LINGER 13
957 /** sockopt: Allow multiple sockets to reuse a single port (ignored, for compatibility) */
958 #define SO_REUSEPORT 15
959 
960 /** sockopt: Receive low watermark (ignored, for compatibility) */
961 #define SO_RCVLOWAT 18
962 /** sockopt: Send low watermark (ignored, for compatibility) */
963 #define SO_SNDLOWAT 19
964 
965 /**
966  * sockopt: Receive timeout
967  * Applies to receive functions like recv(), but not to connect()
968  */
969 #define SO_RCVTIMEO 20
970 /** sockopt: Send timeout */
971 #define SO_SNDTIMEO 21
972 
973 /** sockopt: Bind a socket to an interface */
974 #define SO_BINDTODEVICE	25
975 
976 /** sockopt: Socket accepts incoming connections (ignored, for compatibility) */
977 #define SO_ACCEPTCONN 30
978 
979 /** sockopt: Timestamp TX packets */
980 #define SO_TIMESTAMPING 37
981 /** sockopt: Protocol used with the socket */
982 #define SO_PROTOCOL 38
983 
984 /** sockopt: Domain used with SOCKET (ignored, for compatibility) */
985 #define SO_DOMAIN 39
986 
987 /** End Socket options for SOL_SOCKET level */
988 
989 /* Socket options for IPPROTO_TCP level */
990 /** sockopt: Disable TCP buffering (ignored, for compatibility) */
991 #define TCP_NODELAY 1
992 
993 /* Socket options for IPPROTO_IP level */
994 /** sockopt: Set or receive the Type-Of-Service value for an outgoing packet. */
995 #define IP_TOS 1
996 
997 /* Socket options for IPPROTO_IPV6 level */
998 /** sockopt: Don't support IPv4 access (ignored, for compatibility) */
999 #define IPV6_V6ONLY 26
1000 
1001 /** sockopt: Set or receive the traffic class value for an outgoing packet. */
1002 #define IPV6_TCLASS 67
1003 
1004 /** sockopt: Socket priority */
1005 #define SO_PRIORITY 12
1006 
1007 /** sockopt: Socket TX time (when the data should be sent) */
1008 #define SO_TXTIME 61
1009 #define SCM_TXTIME SO_TXTIME
1010 
1011 /* Socket options for SOCKS5 proxy */
1012 /** sockopt: Enable SOCKS5 for Socket */
1013 #define SO_SOCKS5 60
1014 
1015 /** listen: The maximum backlog queue length (ignored, for compatibility) */
1016 #define SOMAXCONN 128
1017 
1018 /** @cond INTERNAL_HIDDEN */
1019 /**
1020  * @brief Registration information for a given BSD socket family.
1021  */
1022 struct net_socket_register {
1023 	int family;
1024 	bool is_offloaded;
1025 	bool (*is_supported)(int family, int type, int proto);
1026 	int (*handler)(int family, int type, int proto);
1027 };
1028 
1029 #define NET_SOCKET_DEFAULT_PRIO CONFIG_NET_SOCKETS_PRIORITY_DEFAULT
1030 
1031 #define NET_SOCKET_GET_NAME(socket_name, prio)	\
1032 	__net_socket_register_##prio##_##socket_name
1033 
1034 #define _NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, _is_offloaded) \
1035 	static const STRUCT_SECTION_ITERABLE(net_socket_register,	\
1036 			NET_SOCKET_GET_NAME(socket_name, prio)) = {	\
1037 		.family = _family,					\
1038 		.is_offloaded = _is_offloaded,				\
1039 		.is_supported = _is_supported,				\
1040 		.handler = _handler,					\
1041 	}
1042 
1043 #define NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler) \
1044 	_NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, false)
1045 
1046 #define NET_SOCKET_OFFLOAD_REGISTER(socket_name, prio, _family, _is_supported, _handler) \
1047 	_NET_SOCKET_REGISTER(socket_name, prio, _family, _is_supported, _handler, true)
1048 
1049 /** @endcond */
1050 
1051 #ifdef __cplusplus
1052 }
1053 #endif
1054 
1055 #include <syscalls/socket.h>
1056 
1057 /**
1058  * @}
1059  */
1060 
1061 #endif /* ZEPHYR_INCLUDE_NET_SOCKET_H_ */
1062