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