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