1 /*
2 * Copyright (c) 2018 Linaro Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest_assert.h>
8
9 #if defined(CONFIG_POSIX_API)
10 #include <zephyr/posix/sys/socket.h>
11 #include <zephyr/posix/arpa/inet.h>
12 #include <zephyr/posix/unistd.h>
13 #else
14 #include <zephyr/net/socket.h>
15 #endif
16
17 #define clear_buf(buf) memset(buf, 0, sizeof(buf))
18
prepare_listen_sock_udp_v4(struct sockaddr_in * addr)19 static inline int prepare_listen_sock_udp_v4(struct sockaddr_in *addr)
20 {
21 int ret, sock;
22
23 zassert_not_null(addr, "null sockaddr");
24
25 ret = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
26 zassert_true(ret >= 0, "socket open failed");
27
28 sock = ret;
29
30 zassert_equal(addr->sin_family, AF_INET, "Invalid family");
31
32 ret = zsock_bind(sock, (struct sockaddr *)addr, sizeof(*addr));
33 zassert_equal(ret, 0, "bind failed (%d/%d)", ret, errno);
34
35 return sock;
36 }
37
prepare_listen_sock_udp_v6(struct sockaddr_in6 * addr)38 static inline int prepare_listen_sock_udp_v6(struct sockaddr_in6 *addr)
39 {
40 int ret, sock;
41
42 zassert_not_null(addr, "null sockaddr");
43
44 ret = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
45 zassert_true(ret >= 0, "socket open failed");
46
47 sock = ret;
48
49 zassert_equal(addr->sin6_family, AF_INET6, "Invalid family");
50
51 ret = zsock_bind(sock, (struct sockaddr *)addr, sizeof(*addr));
52 zassert_equal(ret, 0, "bind failed (%d/%d)", ret, errno);
53
54 return sock;
55 }
56
prepare_sock_udp_v4(const char * addr,uint16_t port,int * sock,struct sockaddr_in * sockaddr)57 static inline void prepare_sock_udp_v4(const char *addr, uint16_t port,
58 int *sock, struct sockaddr_in *sockaddr)
59 {
60 int rv;
61
62 zassert_not_null(addr, "null addr");
63 zassert_not_null(sock, "null sock");
64 zassert_not_null(sockaddr, "null sockaddr");
65
66 *sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
67 zassert_true(*sock >= 0, "socket open failed");
68
69 sockaddr->sin_family = AF_INET;
70 sockaddr->sin_port = htons(port);
71 rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
72 zassert_equal(rv, 1, "inet_pton failed");
73 }
74
prepare_sock_udp_v6(const char * addr,uint16_t port,int * sock,struct sockaddr_in6 * sockaddr)75 static inline void prepare_sock_udp_v6(const char *addr, uint16_t port,
76 int *sock, struct sockaddr_in6 *sockaddr)
77 {
78 int rv;
79
80 zassert_not_null(addr, "null addr");
81 zassert_not_null(sock, "null sock");
82 zassert_not_null(sockaddr, "null sockaddr");
83
84 *sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
85 zassert_true(*sock >= 0, "socket open failed");
86
87 (void)memset(sockaddr, 0, sizeof(*sockaddr));
88 sockaddr->sin6_family = AF_INET6;
89 sockaddr->sin6_port = htons(port);
90 rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
91 zassert_equal(rv, 1, "inet_pton failed");
92 }
93
prepare_sock_tcp_v4(const char * addr,uint16_t port,int * sock,struct sockaddr_in * sockaddr)94 static inline void prepare_sock_tcp_v4(const char *addr, uint16_t port,
95 int *sock, struct sockaddr_in *sockaddr)
96 {
97 int rv;
98
99 zassert_not_null(addr, "null addr");
100 zassert_not_null(sock, "null sock");
101 zassert_not_null(sockaddr, "null sockaddr");
102
103 *sock = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
104 zassert_true(*sock >= 0, "socket open failed");
105
106 sockaddr->sin_family = AF_INET;
107 sockaddr->sin_port = htons(port);
108 rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
109 zassert_equal(rv, 1, "inet_pton failed");
110 }
111
prepare_sock_tcp_v6(const char * addr,uint16_t port,int * sock,struct sockaddr_in6 * sockaddr)112 static inline void prepare_sock_tcp_v6(const char *addr, uint16_t port,
113 int *sock, struct sockaddr_in6 *sockaddr)
114 {
115 int rv;
116
117 zassert_not_null(addr, "null addr");
118 zassert_not_null(sock, "null sock");
119 zassert_not_null(sockaddr, "null sockaddr");
120
121 *sock = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
122 zassert_true(*sock >= 0, "socket open failed");
123
124 sockaddr->sin6_family = AF_INET6;
125 sockaddr->sin6_port = htons(port);
126 rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
127 zassert_equal(rv, 1, "inet_pton failed");
128 }
129
prepare_sock_tls_v4(const char * addr,uint16_t port,int * sock,struct sockaddr_in * sockaddr,enum net_ip_protocol_secure proto)130 static inline void prepare_sock_tls_v4(const char *addr, uint16_t port,
131 int *sock, struct sockaddr_in *sockaddr,
132 enum net_ip_protocol_secure proto)
133 {
134 int rv;
135
136 zassert_not_null(addr, "null addr");
137 zassert_not_null(sock, "null sock");
138 zassert_not_null(sockaddr, "null sockaddr");
139
140 *sock = zsock_socket(AF_INET, SOCK_STREAM, proto);
141 zassert_true(*sock >= 0, "socket open failed");
142
143 sockaddr->sin_family = AF_INET;
144 sockaddr->sin_port = htons(port);
145 rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
146 zassert_equal(rv, 1, "inet_pton failed");
147 }
148
prepare_sock_tls_v6(const char * addr,uint16_t port,int * sock,struct sockaddr_in6 * sockaddr,enum net_ip_protocol_secure proto)149 static inline void prepare_sock_tls_v6(const char *addr, uint16_t port,
150 int *sock, struct sockaddr_in6 *sockaddr,
151 enum net_ip_protocol_secure proto)
152 {
153 int rv;
154
155 zassert_not_null(addr, "null addr");
156 zassert_not_null(sock, "null sock");
157 zassert_not_null(sockaddr, "null sockaddr");
158
159 *sock = zsock_socket(AF_INET6, SOCK_STREAM, proto);
160 zassert_true(*sock >= 0, "socket open failed");
161
162 sockaddr->sin6_family = AF_INET6;
163 sockaddr->sin6_port = htons(port);
164 rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
165 zassert_equal(rv, 1, "inet_pton failed");
166 }
167
prepare_sock_dtls_v4(const char * addr,uint16_t port,int * sock,struct sockaddr_in * sockaddr,enum net_ip_protocol_secure proto)168 static inline void prepare_sock_dtls_v4(const char *addr, uint16_t port,
169 int *sock, struct sockaddr_in *sockaddr,
170 enum net_ip_protocol_secure proto)
171 {
172 int rv;
173
174 zassert_not_null(addr, "null addr");
175 zassert_not_null(sock, "null sock");
176 zassert_not_null(sockaddr, "null sockaddr");
177
178 *sock = zsock_socket(AF_INET, SOCK_DGRAM, proto);
179 zassert_true(*sock >= 0, "socket open failed");
180
181 sockaddr->sin_family = AF_INET;
182 sockaddr->sin_port = htons(port);
183 rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
184 zassert_equal(rv, 1, "inet_pton failed");
185 }
186
prepare_sock_dtls_v6(const char * addr,uint16_t port,int * sock,struct sockaddr_in6 * sockaddr,enum net_ip_protocol_secure proto)187 static inline void prepare_sock_dtls_v6(const char *addr, uint16_t port,
188 int *sock, struct sockaddr_in6 *sockaddr,
189 enum net_ip_protocol_secure proto)
190 {
191 int rv;
192
193 zassert_not_null(addr, "null addr");
194 zassert_not_null(sock, "null sock");
195 zassert_not_null(sockaddr, "null sockaddr");
196
197 *sock = zsock_socket(AF_INET6, SOCK_DGRAM, proto);
198 zassert_true(*sock >= 0, "socket open failed");
199
200 sockaddr->sin6_family = AF_INET6;
201 sockaddr->sin6_port = htons(port);
202 rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
203 zassert_equal(rv, 1, "inet_pton failed");
204 }
205