2019-01-24 00:07:10 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Linaro Limited
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ztest_assert.h>
|
|
|
|
|
2022-05-06 16:44:33 +08:00
|
|
|
#include <zephyr/net/socket.h>
|
2019-01-24 00:07:10 +08:00
|
|
|
|
|
|
|
#define clear_buf(buf) memset(buf, 0, sizeof(buf))
|
|
|
|
|
2019-04-05 19:11:44 +08:00
|
|
|
static inline int prepare_listen_sock_udp_v4(struct sockaddr_in *addr)
|
|
|
|
{
|
|
|
|
int ret, sock;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null sockaddr");
|
|
|
|
|
|
|
|
ret = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
zassert_true(ret >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sock = ret;
|
|
|
|
|
|
|
|
zassert_equal(addr->sin_family, AF_INET, "Invalid family");
|
|
|
|
|
|
|
|
ret = bind(sock, (struct sockaddr *)addr, sizeof(*addr));
|
|
|
|
zassert_equal(ret, 0, "bind failed (%d/%d)", ret, errno);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int prepare_listen_sock_udp_v6(struct sockaddr_in6 *addr)
|
|
|
|
{
|
|
|
|
int ret, sock;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null sockaddr");
|
|
|
|
|
|
|
|
ret = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
zassert_true(ret >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sock = ret;
|
|
|
|
|
|
|
|
zassert_equal(addr->sin6_family, AF_INET6, "Invalid family");
|
|
|
|
|
|
|
|
ret = bind(sock, (struct sockaddr *)addr, sizeof(*addr));
|
|
|
|
zassert_equal(ret, 0, "bind failed (%d/%d)", ret, errno);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void prepare_sock_udp_v4(const char *addr, uint16_t port,
|
2019-01-24 00:07:10 +08:00
|
|
|
int *sock, struct sockaddr_in *sockaddr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
|
|
|
*sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin_family = AF_INET;
|
|
|
|
sockaddr->sin_port = htons(port);
|
|
|
|
rv = inet_pton(AF_INET, addr, &sockaddr->sin_addr);
|
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void prepare_sock_udp_v6(const char *addr, uint16_t port,
|
2019-01-24 00:07:10 +08:00
|
|
|
int *sock, struct sockaddr_in6 *sockaddr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
2019-02-22 03:50:05 +08:00
|
|
|
*sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
(void)memset(sockaddr, 0, sizeof(*sockaddr));
|
|
|
|
sockaddr->sin6_family = AF_INET6;
|
|
|
|
sockaddr->sin6_port = htons(port);
|
2019-02-22 03:50:05 +08:00
|
|
|
rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void prepare_sock_tcp_v4(const char *addr, uint16_t port,
|
2019-01-24 00:07:10 +08:00
|
|
|
int *sock, struct sockaddr_in *sockaddr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
2019-02-22 03:50:05 +08:00
|
|
|
*sock = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin_family = AF_INET;
|
|
|
|
sockaddr->sin_port = htons(port);
|
2019-02-22 03:50:05 +08:00
|
|
|
rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void prepare_sock_tcp_v6(const char *addr, uint16_t port,
|
2019-01-24 00:07:10 +08:00
|
|
|
int *sock, struct sockaddr_in6 *sockaddr)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
2019-02-22 03:50:05 +08:00
|
|
|
*sock = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin6_family = AF_INET6;
|
|
|
|
sockaddr->sin6_port = htons(port);
|
2019-02-22 03:50:05 +08:00
|
|
|
rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
|
2019-01-24 00:07:10 +08:00
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
2021-02-07 02:11:35 +08:00
|
|
|
|
|
|
|
static inline void prepare_sock_tls_v4(const char *addr, uint16_t port,
|
|
|
|
int *sock, struct sockaddr_in *sockaddr,
|
|
|
|
enum net_ip_protocol_secure proto)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
|
|
|
*sock = zsock_socket(AF_INET, SOCK_STREAM, proto);
|
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin_family = AF_INET;
|
|
|
|
sockaddr->sin_port = htons(port);
|
|
|
|
rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
|
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void prepare_sock_tls_v6(const char *addr, uint16_t port,
|
|
|
|
int *sock, struct sockaddr_in6 *sockaddr,
|
|
|
|
enum net_ip_protocol_secure proto)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
|
|
|
*sock = zsock_socket(AF_INET6, SOCK_STREAM, proto);
|
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin6_family = AF_INET6;
|
|
|
|
sockaddr->sin6_port = htons(port);
|
|
|
|
rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
|
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
2021-03-19 20:39:23 +08:00
|
|
|
|
|
|
|
static inline void prepare_sock_dtls_v4(const char *addr, uint16_t port,
|
|
|
|
int *sock, struct sockaddr_in *sockaddr,
|
|
|
|
enum net_ip_protocol_secure proto)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
|
|
|
*sock = zsock_socket(AF_INET, SOCK_DGRAM, proto);
|
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin_family = AF_INET;
|
|
|
|
sockaddr->sin_port = htons(port);
|
|
|
|
rv = zsock_inet_pton(AF_INET, addr, &sockaddr->sin_addr);
|
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void prepare_sock_dtls_v6(const char *addr, uint16_t port,
|
|
|
|
int *sock, struct sockaddr_in6 *sockaddr,
|
|
|
|
enum net_ip_protocol_secure proto)
|
|
|
|
{
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
zassert_not_null(addr, "null addr");
|
|
|
|
zassert_not_null(sock, "null sock");
|
|
|
|
zassert_not_null(sockaddr, "null sockaddr");
|
|
|
|
|
|
|
|
*sock = zsock_socket(AF_INET6, SOCK_DGRAM, proto);
|
|
|
|
zassert_true(*sock >= 0, "socket open failed");
|
|
|
|
|
|
|
|
sockaddr->sin6_family = AF_INET6;
|
|
|
|
sockaddr->sin6_port = htons(port);
|
|
|
|
rv = zsock_inet_pton(AF_INET6, addr, &sockaddr->sin6_addr);
|
|
|
|
zassert_equal(rv, 1, "inet_pton failed");
|
|
|
|
}
|