Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
socket.h
Go to the documentation of this file.
1
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
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
33extern "C" {
34#endif
35
37 int fd;
38 short events;
39 short revents;
40};
41
42/* ZSOCK_POLL* values are compatible with Linux */
44#define ZSOCK_POLLIN 1
46#define ZSOCK_POLLPRI 2
48#define ZSOCK_POLLOUT 4
50#define ZSOCK_POLLERR 8
52#define ZSOCK_POLLHUP 0x10
54#define ZSOCK_POLLNVAL 0x20
55
57#define ZSOCK_MSG_PEEK 0x02
61#define ZSOCK_MSG_TRUNC 0x20
63#define ZSOCK_MSG_DONTWAIT 0x40
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 */
72#define ZSOCK_SHUT_RD 0
74#define ZSOCK_SHUT_WR 1
76#define ZSOCK_SHUT_RDWR 2
77
81#define SOL_TLS 282
82
92#define TLS_SEC_TAG_LIST 1
97#define TLS_HOSTNAME 2
103#define TLS_CIPHERSUITE_LIST 3
108#define TLS_CIPHERSUITE_USED 4
119#define TLS_PEER_VERIFY 5
128#define TLS_DTLS_ROLE 6
134#define TLS_ALPN_LIST 7
139#define TLS_DTLS_HANDSHAKE_TIMEOUT_MIN 8
140#define TLS_DTLS_HANDSHAKE_TIMEOUT_MAX 9
141
144/* Valid values for TLS_PEER_VERIFY option */
145#define TLS_PEER_VERIFY_NONE 0
146#define TLS_PEER_VERIFY_OPTIONAL 1
147#define TLS_PEER_VERIFY_REQUIRED 2
149/* Valid values for TLS_DTLS_ROLE option */
150#define TLS_DTLS_ROLE_CLIENT 0
151#define TLS_DTLS_ROLE_SERVER 1
162
163 struct sockaddr _ai_addr;
164 char _ai_canonname[DNS_MAX_NAME_SIZE + 1];
165};
166
203__syscall void *zsock_get_context_object(int sock);
204
222__syscall int zsock_socket(int family, int type, int proto);
223
236__syscall int zsock_socketpair(int family, int type, int proto, int *sv);
237
249__syscall int zsock_close(int sock);
250
264__syscall int zsock_shutdown(int sock, int how);
265
278__syscall int zsock_bind(int sock, const struct sockaddr *addr,
279 socklen_t addrlen);
280
293__syscall int zsock_connect(int sock, const struct sockaddr *addr,
294 socklen_t addrlen);
295
308__syscall int zsock_listen(int sock, int backlog);
309
322__syscall int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen);
323
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
352static 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
370__syscall ssize_t zsock_sendmsg(int sock, const struct msghdr *msg,
371 int flags);
372
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
401static 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
419__syscall int zsock_fcntl(int sock, int cmd, int flags);
420
435__syscall int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout);
436
452__syscall int zsock_getsockopt(int sock, int level, int optname,
453 void *optval, socklen_t *optlen);
454
470__syscall int zsock_setsockopt(int sock, int level, int optname,
471 const void *optval, socklen_t optlen);
472
485__syscall int zsock_getsockname(int sock, struct sockaddr *addr,
486 socklen_t *addrlen);
487
500__syscall int zsock_gethostname(char *buf, size_t len);
501
514static 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
532__syscall int zsock_inet_pton(sa_family_t family, const char *src, void *dst);
533
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);
541/* Flags for getaddrinfo() hints. */
542
544#define AI_PASSIVE 0x1
546#define AI_CANONNAME 0x2
548#define AI_NUMERICHOST 0x4
550#define AI_V4MAPPED 0x8
552#define AI_ALL 0x10
554#define AI_ADDRCONFIG 0x20
556#define AI_NUMERICSERV 0x400
557
570int zsock_getaddrinfo(const char *host, const char *service,
571 const struct zsock_addrinfo *hints,
572 struct zsock_addrinfo **res);
573
587
600const char *zsock_gai_strerror(int errcode);
601
603#define NI_NUMERICHOST 1
605#define NI_NUMERICSERV 2
607#define NI_NOFQDN 4
609#define NI_NAMEREQD 8
611#define NI_DGRAM 16
612
613/* POSIX extensions */
614
616#ifndef NI_MAXHOST
617#define NI_MAXHOST 64
618#endif
619
632int 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
640static inline int socket(int family, int type, int proto)
641{
642 return zsock_socket(family, type, proto);
643}
644
645static inline int socketpair(int family, int type, int proto, int sv[2])
646{
647 return zsock_socketpair(family, type, proto, sv);
648}
649
650static inline int close(int sock)
651{
652 return zsock_close(sock);
653}
654
655static inline int shutdown(int sock, int how)
656{
657 return zsock_shutdown(sock, how);
658}
659
660static inline int bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
661{
662 return zsock_bind(sock, addr, addrlen);
663}
664
665static inline int connect(int sock, const struct sockaddr *addr,
666 socklen_t addrlen)
667{
668 return zsock_connect(sock, addr, addrlen);
669}
670
671static inline int listen(int sock, int backlog)
672{
673 return zsock_listen(sock, backlog);
674}
675
676static inline int accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
677{
678 return zsock_accept(sock, addr, addrlen);
679}
680
681static 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
686static 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 */
695static 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
708static 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
715static inline ssize_t sendmsg(int sock, const struct msghdr *message,
716 int flags)
717{
718 return zsock_sendmsg(sock, message, flags);
719}
720
721static 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
727static inline int poll(struct zsock_pollfd *fds, int nfds, int timeout)
728{
729 return zsock_poll(fds, nfds, timeout);
730}
731
732static 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
738static 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
744static inline int getsockname(int sock, struct sockaddr *addr,
745 socklen_t *addrlen)
746{
747 return zsock_getsockname(sock, addr, addrlen);
748}
749
750static 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
757static inline void freeaddrinfo(struct zsock_addrinfo *ai)
758{
760}
761
762static inline const char *gai_strerror(int errcode)
763{
764 return zsock_gai_strerror(errcode);
765}
766
767static 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
777static inline int gethostname(char *buf, size_t len)
778{
779 return zsock_gethostname(buf, len);
780}
781
782static inline int inet_pton(sa_family_t family, const char *src, void *dst)
783{
784 return zsock_inet_pton(family, src, dst);
785}
786
787static 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
823struct ifreq {
824 char ifr_name[IFNAMSIZ]; /* Interface name */
825};
826
828#define SOL_SOCKET 1
829
830/* Socket options for SOL_SOCKET level */
832#define SO_REUSEADDR 2
834#define SO_TYPE 3
836#define SO_ERROR 4
837
842#define SO_RCVTIMEO 20
844#define SO_SNDTIMEO 21
845
847#define SO_BINDTODEVICE 25
848
850#define SO_TIMESTAMPING 37
852#define SO_PROTOCOL 38
853
854/* Socket options for IPPROTO_TCP level */
856#define TCP_NODELAY 1
857
858/* Socket options for IPPROTO_IPV6 level */
860#define IPV6_V6ONLY 26
861
863#define SO_PRIORITY 12
864
866#define SO_TXTIME 61
867#define SCM_TXTIME SO_TXTIME
868
869/* Socket options for SOCKS5 proxy */
871#define SO_SOCKS5 60
872
877struct 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
896#ifdef __cplusplus
897}
898#endif
899
900#include <syscalls/socket.h>
901
906#endif /* ZEPHYR_INCLUDE_NET_SOCKET_H_ */
ZTEST_BMEM int timeout
Definition: main.c:31
DNS resolving library.
int zsock_fcntl(int sock, int cmd, int flags)
Control blocking/non-blocking mode of a socket.
ssize_t zsock_sendto(int sock, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
Send data to an arbitrary network address.
int zsock_connect(int sock, const struct sockaddr *addr, socklen_t addrlen)
Connect a socket to a peer network address.
int zsock_socketpair(int family, int type, int proto, int *sv)
Create an unnamed pair of connected sockets.
void * zsock_get_context_object(int sock)
Obtain a file descriptor's associated net context.
int zsock_accept(int sock, struct sockaddr *addr, socklen_t *addrlen)
Accept a connection on listening socket.
static ssize_t zsock_send(int sock, const void *buf, size_t len, int flags)
Send data to a connected peer.
Definition: socket.h:352
int zsock_bind(int sock, const struct sockaddr *addr, socklen_t addrlen)
Bind a socket to a local network address.
int zsock_socket(int family, int type, int proto)
Create a network socket.
int zsock_getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen)
Get various socket options.
void zsock_freeaddrinfo(struct zsock_addrinfo *ai)
Free results returned by zsock_getaddrinfo()
static ssize_t zsock_recv(int sock, void *buf, size_t max_len, int flags)
Receive data from a connected peer.
Definition: socket.h:401
int zsock_gethostname(char *buf, size_t len)
Get local host name.
int zsock_getsockname(int sock, struct sockaddr *addr, socklen_t *addrlen)
Get socket name.
int zsock_poll(struct zsock_pollfd *fds, int nfds, int timeout)
Efficiently poll multiple sockets for events.
const char * zsock_gai_strerror(int errcode)
Convert zsock_getaddrinfo() error code to textual message.
int zsock_shutdown(int sock, int how)
Shutdown socket send/receive operations.
ssize_t zsock_recvfrom(int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
Receive data from an arbitrary network address.
#define IFNAMSIZ
Definition: socket.h:820
int zsock_setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
Set various socket options.
ssize_t zsock_sendmsg(int sock, const struct msghdr *msg, int flags)
Send data to an arbitrary network address.
static char * zsock_inet_ntop(sa_family_t family, const void *src, char *dst, size_t size)
Convert network address from internal to numeric ASCII form.
Definition: socket.h:514
int zsock_inet_pton(sa_family_t family, const char *src, void *dst)
Convert network address from numeric ASCII form to internal representation.
int zsock_close(int sock)
Close a network socket.
int zsock_listen(int sock, int backlog)
Set up a STREAM socket to accept peer connections.
int zsock_getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Resolve a network address to a domain name or ASCII address.
int zsock_getaddrinfo(const char *host, const char *service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res)
Resolve a domain name to one or more network addresses.
#define DNS_MAX_NAME_SIZE
Definition: dns_resolve.h:42
static void cmd(uint32_t command)
Execute a display list command by co-processor engine.
Definition: ft8xx_reference_api.h:153
unsigned short int sa_family_t
Definition: net_ip.h:158
char * net_addr_ntop(sa_family_t family, const void *src, char *dst, size_t size)
Convert IP address to string form.
size_t socklen_t
Definition: net_ip.h:161
flags
Definition: http_parser.h:131
static int inet_pton(sa_family_t family, const char *src, void *dst)
Definition: inet.h:21
static char * inet_ntop(sa_family_t family, const void *src, char *dst, size_t size)
Definition: inet.h:15
__SIZE_TYPE__ ssize_t
Definition: types.h:28
IPv6 and IPv4 definitions.
static int getaddrinfo(const char *host, const char *service, const struct zsock_addrinfo *hints, struct zsock_addrinfo **res)
Definition: netdb.h:17
static int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
Definition: netdb.h:34
static const char * gai_strerror(int errcode)
Definition: netdb.h:29
static void freeaddrinfo(struct zsock_addrinfo *ai)
Definition: netdb.h:24
static int poll(struct zsock_pollfd *fds, int nfds, int timeout)
Definition: poll.h:23
static ssize_t sendmsg(int sock, const struct msghdr *message, int flags)
Definition: socket.h:78
static ssize_t recvfrom(int sock, void *buf, size_t max_len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
Definition: socket.h:84
static int getsockopt(int sock, int level, int optname, void *optval, socklen_t *optlen)
Definition: socket.h:90
static int setsockopt(int sock, int level, int optname, const void *optval, socklen_t optlen)
Definition: socket.h:96
static int getsockname(int sock, struct sockaddr *addr, socklen_t *addrlen)
Definition: socket.h:102
static int socketpair(int family, int type, int proto, int sv[2])
Definition: socket.h:21
static int shutdown(int sock, int how)
Definition: socket.h:35
#define bool
Definition: stdbool.h:13
Definition: socket.h:823
char ifr_name[Z_DEVICE_MAX_NAME_LEN]
Definition: socket.h:824
Definition: net_ip.h:229
Definition: net_ip.h:335
Definition: socket.h:153
struct zsock_addrinfo * ai_next
Definition: socket.h:154
int ai_family
Definition: socket.h:156
int ai_flags
Definition: socket.h:155
char * ai_canonname
Definition: socket.h:161
int ai_protocol
Definition: socket.h:158
struct sockaddr * ai_addr
Definition: socket.h:160
int ai_socktype
Definition: socket.h:157
socklen_t ai_addrlen
Definition: socket.h:159
Definition: socket.h:36
short events
Definition: socket.h:38
int fd
Definition: socket.h:37
short revents
Definition: socket.h:39
static void handler(struct k_timer *timer)
Definition: main.c:19
NMI_API SOCKET socket(uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
NMI_API sint8 close(SOCKET sock)
NMI_API sint8 connect(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
NMI_API sint8 listen(SOCKET sock, uint8 backlog)
NMI_API sint16 sendto(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen)
NMI_API sint16 send(SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags)
NMI_API sint8 bind(SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
NMI_API sint8 accept(SOCKET sock, struct sockaddr *addr, uint8 *addrlen)
NMI_API sint16 recv(SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)