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