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, int sock);
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 size_t msghdr_non_empty_iov_count(const struct msghdr *msg);
57 
58 #if defined(CONFIG_NET_SOCKETS_OBJ_CORE)
59 int sock_obj_core_alloc(int sock, struct net_socket_register *reg,
60 			int family, int type, int proto);
61 int sock_obj_core_alloc_find(int sock, int new_sock, int type);
62 int sock_obj_core_dealloc(int sock);
63 void sock_obj_core_update_send_stats(int sock, int bytes);
64 void sock_obj_core_update_recv_stats(int sock, int bytes);
65 #else
sock_obj_core_alloc(int sock,struct net_socket_register * reg,int family,int type,int proto)66 static inline int sock_obj_core_alloc(int sock,
67 				      struct net_socket_register *reg,
68 				      int family, int type, int proto)
69 {
70 	ARG_UNUSED(sock);
71 	ARG_UNUSED(reg);
72 	ARG_UNUSED(family);
73 	ARG_UNUSED(type);
74 	ARG_UNUSED(proto);
75 
76 	return -ENOTSUP;
77 }
78 
sock_obj_core_alloc_find(int sock,int new_sock,int type)79 static inline int sock_obj_core_alloc_find(int sock, int new_sock, int type)
80 {
81 	ARG_UNUSED(sock);
82 	ARG_UNUSED(new_sock);
83 	ARG_UNUSED(type);
84 
85 	return -ENOTSUP;
86 }
87 
sock_obj_core_dealloc(int sock)88 static inline int sock_obj_core_dealloc(int sock)
89 {
90 	ARG_UNUSED(sock);
91 
92 	return -ENOTSUP;
93 }
94 
sock_obj_core_update_send_stats(int sock,int bytes)95 static inline void sock_obj_core_update_send_stats(int sock, int bytes)
96 {
97 	ARG_UNUSED(sock);
98 	ARG_UNUSED(bytes);
99 }
100 
sock_obj_core_update_recv_stats(int sock,int bytes)101 static inline void sock_obj_core_update_recv_stats(int sock, int bytes)
102 {
103 	ARG_UNUSED(sock);
104 	ARG_UNUSED(bytes);
105 }
106 #endif /* CONFIG_NET_SOCKETS_OBJ_CORE */
107 
108 #endif /* _SOCKETS_INTERNAL_H_ */
109