1 /*
2  * Copyright (c) 2017 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _SOCKETS_INTERNAL_H_
8 #define _SOCKETS_INTERNAL_H_
9 
10 #include <zephyr/sys/fdtable.h>
11 #include <zephyr/net/net_context.h>
12 #include <zephyr/net/socket.h>
13 
14 #define SOCK_EOF 1
15 #define SOCK_NONBLOCK 2
16 #define SOCK_ERROR 4
17 
18 int zsock_close_ctx(struct net_context *ctx);
19 int zsock_poll_internal(struct zsock_pollfd *fds, int nfds, k_timeout_t timeout);
20 
21 int zsock_wait_data(struct net_context *ctx, k_timeout_t *timeout);
22 
sock_set_flag(struct net_context * ctx,uintptr_t mask,uintptr_t flag)23 static inline void sock_set_flag(struct net_context *ctx, uintptr_t mask,
24 				 uintptr_t flag)
25 {
26 	uintptr_t val = POINTER_TO_UINT(ctx->socket_data);
27 
28 	val = (val & ~mask) | flag;
29 	(ctx)->socket_data = UINT_TO_POINTER(val);
30 }
31 
sock_get_flag(struct net_context * ctx,uintptr_t mask)32 static inline uintptr_t sock_get_flag(struct net_context *ctx, uintptr_t mask)
33 {
34 	return POINTER_TO_UINT(ctx->socket_data) & mask;
35 }
36 
37 void net_socket_update_tc_rx_time(struct net_pkt *pkt, uint32_t end_tick);
38 
39 #if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
40 bool net_socket_is_tls(void *obj);
41 #else
net_socket_is_tls(void * obj)42 static inline bool net_socket_is_tls(void *obj)
43 {
44 	ARG_UNUSED(obj);
45 
46 	return false;
47 }
48 #endif
49 
50 #define sock_is_eof(ctx) sock_get_flag(ctx, SOCK_EOF)
51 #define sock_set_eof(ctx) sock_set_flag(ctx, SOCK_EOF, SOCK_EOF)
52 #define sock_is_nonblock(ctx) sock_get_flag(ctx, SOCK_NONBLOCK)
53 #define sock_is_error(ctx) sock_get_flag(ctx, SOCK_ERROR)
54 #define sock_set_error(ctx) sock_set_flag(ctx, SOCK_ERROR, SOCK_ERROR)
55 
56 struct socket_op_vtable {
57 	struct fd_op_vtable fd_vtable;
58 	int (*shutdown)(void *obj, int how);
59 	int (*bind)(void *obj, const struct sockaddr *addr, socklen_t addrlen);
60 	int (*connect)(void *obj, const struct sockaddr *addr,
61 		       socklen_t addrlen);
62 	int (*listen)(void *obj, int backlog);
63 	int (*accept)(void *obj, struct sockaddr *addr, socklen_t *addrlen);
64 	ssize_t (*sendto)(void *obj, const void *buf, size_t len, int flags,
65 			  const struct sockaddr *dest_addr, socklen_t addrlen);
66 	ssize_t (*recvfrom)(void *obj, void *buf, size_t max_len, int flags,
67 			    struct sockaddr *src_addr, socklen_t *addrlen);
68 	int (*getsockopt)(void *obj, int level, int optname,
69 			  void *optval, socklen_t *optlen);
70 	int (*setsockopt)(void *obj, int level, int optname,
71 			  const void *optval, socklen_t optlen);
72 	ssize_t (*sendmsg)(void *obj, const struct msghdr *msg, int flags);
73 	int (*getpeername)(void *obj, struct sockaddr *addr,
74 			   socklen_t *addrlen);
75 	int (*getsockname)(void *obj, struct sockaddr *addr,
76 			   socklen_t *addrlen);
77 };
78 
79 size_t msghdr_non_empty_iov_count(const struct msghdr *msg);
80 
81 #endif /* _SOCKETS_INTERNAL_H_ */
82