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 ssize_t (*recvmsg)(void *obj, struct msghdr *msg, int flags);
74 int (*getpeername)(void *obj, struct sockaddr *addr,
75 socklen_t *addrlen);
76 int (*getsockname)(void *obj, struct sockaddr *addr,
77 socklen_t *addrlen);
78 };
79
80 size_t msghdr_non_empty_iov_count(const struct msghdr *msg);
81
82 #if defined(CONFIG_NET_SOCKETS_OBJ_CORE)
83 int sock_obj_core_alloc(int sock, struct net_socket_register *reg,
84 int family, int type, int proto);
85 int sock_obj_core_alloc_find(int sock, int new_sock, int type);
86 int sock_obj_core_dealloc(int sock);
87 void sock_obj_core_update_send_stats(int sock, int bytes);
88 void sock_obj_core_update_recv_stats(int sock, int bytes);
89 #else
sock_obj_core_alloc(int sock,struct net_socket_register * reg,int family,int type,int proto)90 static inline int sock_obj_core_alloc(int sock,
91 struct net_socket_register *reg,
92 int family, int type, int proto)
93 {
94 ARG_UNUSED(sock);
95 ARG_UNUSED(reg);
96 ARG_UNUSED(family);
97 ARG_UNUSED(type);
98 ARG_UNUSED(proto);
99
100 return -ENOTSUP;
101 }
102
sock_obj_core_alloc_find(int sock,int new_sock,int type)103 static inline int sock_obj_core_alloc_find(int sock, int new_sock, int type)
104 {
105 ARG_UNUSED(sock);
106 ARG_UNUSED(new_sock);
107 ARG_UNUSED(type);
108
109 return -ENOTSUP;
110 }
111
sock_obj_core_dealloc(int sock)112 static inline int sock_obj_core_dealloc(int sock)
113 {
114 ARG_UNUSED(sock);
115
116 return -ENOTSUP;
117 }
118
sock_obj_core_update_send_stats(int sock,int bytes)119 static inline void sock_obj_core_update_send_stats(int sock, int bytes)
120 {
121 ARG_UNUSED(sock);
122 ARG_UNUSED(bytes);
123 }
124
sock_obj_core_update_recv_stats(int sock,int bytes)125 static inline void sock_obj_core_update_recv_stats(int sock, int bytes)
126 {
127 ARG_UNUSED(sock);
128 ARG_UNUSED(bytes);
129 }
130 #endif /* CONFIG_NET_SOCKETS_OBJ_CORE */
131
132 #endif /* _SOCKETS_INTERNAL_H_ */
133