VSF Documented
socket.h
Go to the documentation of this file.
1#ifndef __VSF_LINUX_SYS_SOCKET_H__
2#define __VSF_LINUX_SYS_SOCKET_H__
3
5
6#if VSF_LINUX_CFG_RELATIVE_PATH == ENABLED
7# include "../sys/types.h"
8#else
9# include <sys/types.h>
10#endif
11// for stdint.h
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18#define SOMAXCONN 256
19
20#define SCM_RIGHTS 0x01
21#define SCM_CREDENTIALS 0x02
22#define SCM_SECURITY 0x03
23
24// protocol families
25// refer: https://code.woboq.org/gtk/include/bits/socket.h.html
26#define PF_UNSPEC 0
27#define PF_LOCAL 1
28#define PF_UNIX PF_LOCAL
29#define PF_INET 2
30#define PF_INET6 10
31#define PF_NETLINK 16
32#define PF_PACKET 17
33
34// standard address families
35#define AF_UNSPEC PF_UNSPEC
36#define AF_LOCAL PF_LOCAL
37#define AF_UNIX PF_UNIX
38#define AF_FILE PF_FILE
39#define AF_INET PF_INET
40#define AF_INET6 PF_INET6
41#define AF_NETLINK PF_NETLINK
42#define AF_PACKET PF_PACKET
43
44#define SOL_IP IPPROTO_IP
45#define SOL_TCP IPPROTO_TCP
46#define SOL_UDP IPPROTO_UDP
47
48#define IP_TOS 1
49#define IP_TTL 2
50#define IP_HDRINCL 3
51#define IP_OPTIONS 4
52
60};
61#define SOCK_MAX (SOCK_PACKET + 1)
62
63// flags for socket/socketpair/accept4
64#define SOCK_CLOEXEC O_CLOEXEC
65#define SOCK_NONBLOCK O_NONBLOCK
66
67#define INVALID_SOCKET -1
68#define SOCKET_ERROR -1
69
71#define __socklen_t_defined
72
74struct sockaddr {
76 char sa_data[32];
77};
80 // TODO: need to be larger?
81 char sa_data[32];
82};
83
84struct linger {
87};
88
89#if VSF_LINUX_SOCKET_CFG_WRAPPER == ENABLED
90#define VSF_LINUX_SOCKET_WRAPPER(__api) VSF_SHELL_WRAPPER(vsf_linux_socket, __api)
91
92#define setsockopt VSF_LINUX_SOCKET_WRAPPER(setsockopt)
93#define getsockopt VSF_LINUX_SOCKET_WRAPPER(getsockopt)
94
95#define getpeername VSF_LINUX_SOCKET_WRAPPER(getpeername)
96#define getsockname VSF_LINUX_SOCKET_WRAPPER(getsockname)
97
98#define accept VSF_LINUX_SOCKET_WRAPPER(accept)
99#define accept4 VSF_LINUX_SOCKET_WRAPPER(accept4)
100#define bind VSF_LINUX_SOCKET_WRAPPER(bind)
101#define connect VSF_LINUX_SOCKET_WRAPPER(connect)
102#define listen VSF_LINUX_SOCKET_WRAPPER(listen)
103#define recv VSF_LINUX_SOCKET_WRAPPER(recv)
104#define recvmsg VSF_LINUX_SOCKET_WRAPPER(recvmsg)
105#define recvfrom VSF_LINUX_SOCKET_WRAPPER(recvfrom)
106#define send VSF_LINUX_SOCKET_WRAPPER(send)
107#define sendmsg VSF_LINUX_SOCKET_WRAPPER(sendmsg)
108#define sendto VSF_LINUX_SOCKET_WRAPPER(sendto)
109#define shutdown VSF_LINUX_SOCKET_WRAPPER(shutdown)
110#define socket VSF_LINUX_SOCKET_WRAPPER(socket)
111#define socketpair VSF_LINUX_SOCKET_WRAPPER(socketpair)
112#endif
113
114// syscalls
115
116#define __NR_connect connect
117#define __NR_accept accept
118#define __NR_accept4 accept4
119#define __NR_bind bind
120
121// level for sockopt
122#define SOL_SOCKET 0xFFFF
123// sock options
124// refer: https://code.woboq.org/gtk/include/asm-generic/socket.h.html
125#define SO_DEBUG 1
126#define SO_REUSEADDR 2
127#define SO_TYPE 3
128#define SO_ERROR 4
129#define SO_DONTROUTE 5
130#define SO_BROADCAST 6
131#define SO_SNDBUF 7
132#define SO_RCVBUF 8
133#define SO_KEEPALIVE 9
134#define SO_OOBINLINE 10
135#define SO_NO_CHECK 11
136#define SO_PRIORITY 12
137#define SO_LINGER 13
138#define SO_BSDCOMPAT 14
139#define SO_REUSEPORT 15
140// struct ucred is not implemented
141//#define SO_PASSCRED 16
142//#define SO_PEERCRED 17
143#define SO_RCVLOWAT 18
144#define SO_SNDLOWAT 19
145#define SO_RCVTIMEO 20
146#define SO_SNDTIMEO 21
147#define SO_BINDTODEVICE 25
148#define SO_MAX_PACING_RATE 47
149#define SO_NONBLOCK 100
150
151// IP options
152#define IP_TOS 1
153#define IP_TTL 2
154#define IP_HDRINCL 3
155#define IP_OPTIONS 4
156#define IP_PKTINFO 8
157
158// multicast
159// in.h
160#define IP_MULTICAST_IF 32
161#define IP_MULTICAST_TTL 33
162#define IP_MULTICAST_LOOP 34
163#define IP_ADD_MEMBERSHIP 35
164#define IP_DROP_MEMBERSHIP 36
165#define IP_MULTICAST_ALL 49
166// in6.h
167#define IPV6_UNICAST_HOPS 16
168#define IPV6_MULTICAST_IF 17
169#define IPV6_MULTICAST_HOPS 18
170#define IPV6_MULTICAST_LOOP 19
171#define IPV6_JOIN_GROUP 20
172#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
173#define IPV6_LEAVE_GROUP 21
174#define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
175#define IPV6_V6ONLY 26
176
177// flags for send/recv
178enum {
179 MSG_OOB = 1 << 0,
180#define MSG_OOB MSG_OOB
181 MSG_PEEK = 1 << 1,
182#define MSG_PEEK MSG_PEEK
184#define MSG_DONTROUTE MSG_DONTROUTE
185 MSG_CTRUNC = 1 << 3,
186#define MSG_CTRUNC MSG_CTRUNC
187 MSG_TRUNC = 1 << 5,
188#define MSG_TRUNC MSG_TRUNC
189 MSG_DONTWAIT = 1 << 6,
190#define MSG_DONTWAIT MSG_DONTWAIT
191 MSG_WAITALL = 1 << 8,
192#define MSG_WAITALL MSG_WAITALL
193 MSG_NOSIGNAL = 1 << 14,
194#define MSG_NOSIGNAL MSG_NOSIGNAL
195};
196
197struct msghdr {
198 void *msg_name;
200 struct iovec *msg_iov;
205};
206
207#define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
208#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
209#define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))
210#define CMSG_DATA(cmsg) (void *)((uint8_t *)(cmsg) + sizeof(struct cmsghdr))
211#define CMSG_FIRSTHDR(msg) \
212 ((msg)->msg_controllen >= sizeof(struct cmsghdr) ? \
213 (struct cmsghdr *)(msg)->msg_control \
214 : (struct cmsghdr *)NULL)
215struct cmsghdr {
216 size_t cmsg_len;
219};
220
221#if __IS_COMPILER_GCC__
222# pragma GCC diagnostic push
223# pragma GCC diagnostic ignored "-Wcast-align"
224#elif __IS_COMPILER_LLVM__ || __IS_COMPILER_ARM_COMPILER_6__
225# pragma clang diagnostic push
226# pragma clang diagnostic ignored "-Wcast-align"
227#endif
228
229static inline struct cmsghdr * CMSG_NXTHDR(struct msghdr *__msg, struct cmsghdr *__cmsg)
230{
231 struct cmsghdr * __ptr;
232
233 __ptr = (struct cmsghdr*)(((unsigned char *) __cmsg) + CMSG_ALIGN(__cmsg->cmsg_len));
234 if ((unsigned long)((char*)(__ptr+1) - (char *)__msg->msg_control) > __msg->msg_controllen)
235 return (struct cmsghdr *)0;
236
237 return __ptr;
238}
239
240#if __IS_COMPILER_GCC__
241# pragma GCC diagnostic pop
242#elif __IS_COMPILER_LLVM__ || __IS_COMPILER_ARM_COMPILER_6__
243# pragma clang diagnostic pop
244#endif
245
246// how for shutdown
247enum {
248 SHUT_RD = 1 << 0,
249 SHUT_WR = 1 << 1,
251};
252
253#if VSF_LINUX_APPLET_USE_SYS_SOCKET == ENABLED
256
259
262
267
274
278
279 // in6addr_any and in6addr_loopback belongs to <netinet/in.h>, which has no vplt, so add to socket vplt
283# ifndef __VSF_APPLET__
285# endif
286#endif
287
288#if defined(__VSF_APPLET__) && (defined(__VSF_APPLET_LIB__) || defined(__VSF_APPLET_LINUX_SYS_SOCKET_LIB__))\
289 && VSF_APPLET_CFG_ABI_PATCH != ENABLED && VSF_LINUX_APPLET_USE_SYS_SOCKET == ENABLED
290
291#ifndef VSF_LINUX_APPLET_SYS_SOCKET_VPLT
292# if VSF_LINUX_USE_APPLET == ENABLED
293# define VSF_LINUX_APPLET_SYS_SOCKET_VPLT \
294 ((vsf_linux_sys_socket_vplt_t *)(VSF_LINUX_APPLET_VPLT->sys_socket_vplt))
295# else
296# define VSF_LINUX_APPLET_SYS_SOCKET_VPLT \
297 ((vsf_linux_sys_socket_vplt_t *)vsf_vplt((void *)0))
298# endif
299#endif
300
301#define VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(__NAME) \
302 VSF_APPLET_VPLT_ENTRY_FUNC_ENTRY(VSF_LINUX_APPLET_SYS_SOCKET_VPLT, __NAME)
303#define VSF_LINUX_APPLET_SYS_SOCKET_IMP(...) \
304 VSF_APPLET_VPLT_ENTRY_FUNC_IMP(VSF_LINUX_APPLET_SYS_SOCKET_VPLT, __VA_ARGS__)
305
306VSF_LINUX_APPLET_SYS_SOCKET_IMP(setsockopt, int, int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
308 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(setsockopt)(sockfd, level, optname, optval, optlen);
309}
310VSF_LINUX_APPLET_SYS_SOCKET_IMP(getsockopt, int, int sockfd, int level, int optname, void *optval, socklen_t *optlen) {
312 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(getsockopt)(sockfd, level, optname, optval, optlen);
313}
314VSF_LINUX_APPLET_SYS_SOCKET_IMP(getpeername, int, int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
316 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(getpeername)(sockfd, addr, addrlen);
317}
318VSF_LINUX_APPLET_SYS_SOCKET_IMP(getsockname, int, int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
320 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(getsockname)(sockfd, addr, addrlen);
321}
322VSF_LINUX_APPLET_SYS_SOCKET_IMP(accept, int, int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
324 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(accept)(sockfd, addr, addrlen);
325}
326VSF_LINUX_APPLET_SYS_SOCKET_IMP(bind, int, int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
328 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(bind)(sockfd, addr, addrlen);
329}
330VSF_LINUX_APPLET_SYS_SOCKET_IMP(connect, int, int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
332 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(connect)(sockfd, addr, addrlen);
333}
334VSF_LINUX_APPLET_SYS_SOCKET_IMP(listen, int, int sockfd, int backlog) {
336 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(listen)(sockfd, backlog);
337}
338VSF_LINUX_APPLET_SYS_SOCKET_IMP(recv, ssize_t, int sockfd, void *buffer, size_t length, int flags) {
340 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(recv)(sockfd, buffer, length, flags);
341}
342VSF_LINUX_APPLET_SYS_SOCKET_IMP(recvmsg, ssize_t, int sockfd, struct msghdr *msg, int flags) {
344 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(recvmsg)(sockfd, msg, flags);
345}
346VSF_LINUX_APPLET_SYS_SOCKET_IMP(recvfrom, ssize_t, int sockfd, void *buffer, size_t length, int flags, struct sockaddr *src_addr, socklen_t *addrlen) {
348 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(recvfrom)(sockfd, buffer, length, flags, src_addr, addrlen);
349}
350VSF_LINUX_APPLET_SYS_SOCKET_IMP(send, ssize_t, int sockfd, const void *buffer, size_t length, int flags) {
352 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(send)(sockfd, buffer, length, flags);
353}
354VSF_LINUX_APPLET_SYS_SOCKET_IMP(sendmsg, ssize_t, int sockfd, const struct msghdr *msg, int flags) {
356 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(sendmsg)(sockfd, msg, flags);
357}
358VSF_LINUX_APPLET_SYS_SOCKET_IMP(sendto, ssize_t, int sockfd, const void *buffer, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
360 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(sendto)(sockfd, buffer, length, flags, dest_addr, addrlen);
361}
362VSF_LINUX_APPLET_SYS_SOCKET_IMP(shutdown, int, int sockfd, int how) {
364 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(shutdown)(sockfd, how);
365}
366VSF_LINUX_APPLET_SYS_SOCKET_IMP(socket, int, int domain, int type, int protocol) {
368 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(socket)(domain, type, protocol);
369}
370VSF_LINUX_APPLET_SYS_SOCKET_IMP(socketpair, int, int domain, int type, int protocol, int socket_vector[2]) {
372 return VSF_LINUX_APPLET_SYS_SOCKET_ENTRY(socketpair)(domain, type, protocol, socket_vector);
373}
374
375#else // __VSF_APPLET__ && VSF_LINUX_APPLET_USE_SYS_SOCKET
376
377int setsockopt(int sockfd, int level, int optname, const void *optval,
378 socklen_t optlen);
379int getsockopt(int sockfd, int level, int optname, void *optval,
380 socklen_t *optlen);
381
382int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
383int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
384
385int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
386int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
387int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
388int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
389int listen(int sockfd, int backlog);
390
391ssize_t recv(int sockfd, void *buffer, size_t length, int flags);
392ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
393ssize_t recvfrom(int sockfd, void *buffer, size_t length, int flags,
394 struct sockaddr *src_addr, socklen_t *addrlen);
395ssize_t send(int sockfd, const void *buffer, size_t length, int flags);
396ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
397ssize_t sendto(int sockfd, const void *buffer, size_t length, int flags,
398 const struct sockaddr *dest_addr, socklen_t addrlen);
399
400int shutdown(int sockfd, int how);
401int socket(int domain, int type, int protocol);
402int socketpair(int domain, int type, int protocol, int socket_vector[2]);
403
404#endif // __VSF_APPLET__ && VSF_LINUX_APPLET_USE_SYS_SOCKET
405
406#ifdef __cplusplus
407}
408#endif
409
410#endif
const struct in6_addr in6addr_loopback
Definition vsf_linux_socket.c:72
const struct in6_addr in6addr_any
Definition vsf_linux_socket.c:70
int ssize_t
Definition types.h:68
unsigned short uint16_t
Definition lvgl.h:41
unsigned int uint32_t
Definition lvgl.h:43
@ SHUT_RD
Definition socket.h:248
@ SHUT_WR
Definition socket.h:249
@ SHUT_RDWR
Definition socket.h:250
sock_type
Definition socket.h:53
@ SOCK_DGRAM
Definition socket.h:55
@ SOCK_RDM
Definition socket.h:57
@ SOCK_SEQPACKET
Definition socket.h:58
@ SOCK_RAW
Definition socket.h:56
@ SOCK_PACKET
Definition socket.h:59
@ SOCK_STREAM
Definition socket.h:54
#define MSG_WAITALL
Definition socket.h:192
#define getpeername
Definition socket.h:95
uint16_t sa_family_t
Definition socket.h:73
#define listen
Definition socket.h:102
#define sendmsg
Definition socket.h:107
#define setsockopt
Definition socket.h:92
#define accept
Definition socket.h:98
#define recvfrom
Definition socket.h:105
uint32_t socklen_t
Definition socket.h:70
#define sendto
Definition socket.h:108
__VSF_VPLT_DECORATOR__ vsf_linux_sys_socket_vplt_t vsf_linux_sys_socket_vplt
Definition vsf_linux_socket.c:792
#define MSG_PEEK
Definition socket.h:182
#define recvmsg
Definition socket.h:104
#define MSG_TRUNC
Definition socket.h:188
#define getsockopt
Definition socket.h:93
#define accept4
Definition socket.h:99
#define CMSG_ALIGN(len)
Definition socket.h:207
#define MSG_DONTROUTE
Definition socket.h:184
#define MSG_OOB
Definition socket.h:180
#define MSG_NOSIGNAL
Definition socket.h:194
#define getsockname
Definition socket.h:96
#define MSG_CTRUNC
Definition socket.h:186
#define socket
Definition socket.h:110
#define MSG_DONTWAIT
Definition socket.h:190
#define socketpair
Definition socket.h:111
#define shutdown
Definition socket.h:109
#define connect
Definition socket.h:101
#define recv
Definition socket.h:103
#define bind
Definition socket.h:100
#define send
Definition socket.h:106
Definition socket.h:215
int cmsg_type
Definition socket.h:218
size_t cmsg_len
Definition socket.h:216
int cmsg_level
Definition socket.h:217
Definition unistd.h:231
Definition socket.h:84
int l_linger
Definition socket.h:86
int l_onoff
Definition socket.h:85
Definition socket.h:197
struct iovec * msg_iov
Definition socket.h:200
socklen_t msg_namelen
Definition socket.h:199
void * msg_name
Definition socket.h:198
int msg_flags
Definition socket.h:204
size_t msg_controllen
Definition socket.h:203
size_t msg_iovlen
Definition socket.h:201
void * msg_control
Definition socket.h:202
Definition socket.h:78
sa_family_t ss_family
Definition socket.h:79
char sa_data[32]
Definition socket.h:81
Definition socket.h:74
char sa_data[32]
Definition socket.h:76
sa_family_t sa_family
Definition socket.h:75
Definition socket.h:254
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(socketpair)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(setsockopt)
vsf_vplt_info_t info
Definition socket.h:255
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(shutdown)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(getsockopt)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(getpeername)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(recvfrom)
VSF_APPLET_VPLT_ENTRY_VAR_DEF(in6addr_loopback)
VSF_APPLET_VPLT_ENTRY_FUNC_DEF(getsockname)
VSF_APPLET_VPLT_ENTRY_VAR_DEF(in6addr_any)
Definition vsf_cfg.h:95
vk_av_control_type_t type
Definition vsf_audio.h:170
#define __VSF_VPLT_DECORATOR__
Definition vsf_cfg.h:93
#define VSF_APPLET_VPLT_ENTRY_FUNC_TRACE()
Definition vsf_cfg.h:165
uint_fast8_t length
Definition vsf_pbuf.c:38