net/sockets: Adapt net_pkt_get_src_addr to new net_pkt API

This function is only used in sockets, thus making it a private function
of socket library and renaming it relevantly.

Note that sockets should be reviewed at some point to avoid using such
function: zsock_received_cb() already get the ip header and the protocol
header, so it could grab the src addr/port from there. It would be way
more optimized to do so, since net_pkt_get_src_addr is costly as it
parses all over again the ip/protocol headers.

utils unit test is updated and the test of the former
net_pkt_get_src_addr/net_pkt_get_dst_addr are removed.

Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
This commit is contained in:
Tomasz Bursztyka 2019-02-01 11:45:04 +01:00 committed by Anas Nashif
parent 6ad1d617c9
commit 024a7e0502
4 changed files with 105 additions and 487 deletions

View File

@ -2022,32 +2022,6 @@ void net_pkt_get_info(struct k_mem_slab **rx,
struct net_buf_pool **rx_data,
struct net_buf_pool **tx_data);
/**
* @brief Get source socket address.
*
* @param pkt Network packet
* @param addr Source socket address
* @param addrlen The length of source socket address
* @return 0 on success, <0 otherwise.
*/
int net_pkt_get_src_addr(struct net_pkt *pkt,
struct sockaddr *addr,
socklen_t addrlen);
/**
* @brief Get destination socket address.
*
* @param pkt Network packet
* @param addr Destination socket address
* @param addrlen The length of destination socket address
* @return 0 on success, <0 otherwise.
*/
int net_pkt_get_dst_addr(struct net_pkt *pkt,
struct sockaddr *addr,
socklen_t addrlen);
#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
/**
* @brief Debug helper to print out the buffer allocations

View File

@ -1815,97 +1815,6 @@ void net_pkt_get_info(struct k_mem_slab **rx,
}
}
static int net_pkt_get_addr(struct net_pkt *pkt, bool is_src,
struct sockaddr *addr, socklen_t addrlen)
{
enum net_ip_protocol proto;
sa_family_t family;
u16_t port;
if (!addr || !pkt) {
return -EINVAL;
}
/* Set the family */
family = net_pkt_family(pkt);
addr->sa_family = family;
/* Examine the transport protocol */
proto = net_pkt_transport_proto(pkt);
/* Get the source port from transport protocol header */
if (IS_ENABLED(CONFIG_NET_TCP) && proto == IPPROTO_TCP) {
struct net_tcp_hdr hdr, *tcp_hdr;
tcp_hdr = net_tcp_get_hdr(pkt, &hdr);
if (!tcp_hdr) {
return -EINVAL;
}
if (is_src) {
port = tcp_hdr->src_port;
} else {
port = tcp_hdr->dst_port;
}
} else if (IS_ENABLED(CONFIG_NET_UDP) && proto == IPPROTO_UDP) {
struct net_udp_hdr hdr, *udp_hdr;
udp_hdr = net_udp_get_hdr(pkt, &hdr);
if (!udp_hdr) {
return -EINVAL;
}
if (is_src) {
port = udp_hdr->src_port;
} else {
port = udp_hdr->dst_port;
}
} else {
return -ENOTSUP;
}
/* Set address and port to addr */
if (IS_ENABLED(CONFIG_NET_IPV6) && family == AF_INET6) {
struct sockaddr_in6 *addr6 = net_sin6(addr);
if (addrlen < sizeof(struct sockaddr_in6)) {
return -EINVAL;
}
if (is_src) {
net_ipaddr_copy(&addr6->sin6_addr,
&NET_IPV6_HDR(pkt)->src);
} else {
net_ipaddr_copy(&addr6->sin6_addr,
&NET_IPV6_HDR(pkt)->dst);
}
addr6->sin6_port = port;
} else if (IS_ENABLED(CONFIG_NET_IPV4) && family == AF_INET) {
struct sockaddr_in *addr4 = net_sin(addr);
if (addrlen < sizeof(struct sockaddr_in)) {
return -EINVAL;
}
if (is_src) {
net_ipaddr_copy(&addr4->sin_addr,
&NET_IPV4_HDR(pkt)->src);
} else {
net_ipaddr_copy(&addr4->sin_addr,
&NET_IPV4_HDR(pkt)->dst);
}
addr4->sin_port = port;
} else {
return -ENOTSUP;
}
return 0;
}
int net_pkt_pull(struct net_pkt *pkt, u16_t offset, u16_t len)
{
struct net_buf *frag;
@ -2000,18 +1909,6 @@ int net_pkt_pull(struct net_pkt *pkt, u16_t offset, u16_t len)
return 0;
}
int net_pkt_get_src_addr(struct net_pkt *pkt, struct sockaddr *addr,
socklen_t addrlen)
{
return net_pkt_get_addr(pkt, true, addr, addrlen);
}
int net_pkt_get_dst_addr(struct net_pkt *pkt, struct sockaddr *addr,
socklen_t addrlen)
{
return net_pkt_get_addr(pkt, false, addr, addrlen);
}
#if defined(CONFIG_NET_DEBUG_NET_PKT_ALLOC)
void net_pkt_print(void)
{

View File

@ -471,6 +471,107 @@ Z_SYSCALL_HANDLER(zsock_sendto, sock, buf, len, flags, dest_addr, addrlen)
}
#endif /* CONFIG_USERSPACE */
static int sock_get_pkt_src_addr(struct net_pkt *pkt,
enum net_ip_protocol proto,
struct sockaddr *addr,
socklen_t addrlen)
{
int ret = 0;
struct net_pkt_cursor backup;
u16_t *port;
if (!addr || !pkt) {
return -EINVAL;
}
net_pkt_cursor_backup(pkt, &backup);
net_pkt_cursor_init(pkt);
addr->sa_family = net_pkt_family(pkt);
if (IS_ENABLED(CONFIG_NET_IPV4) &&
net_pkt_family(pkt) == AF_INET) {
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv4_access,
struct net_ipv4_hdr);
struct sockaddr_in *addr4 = net_sin(addr);
struct net_ipv4_hdr *ipv4_hdr;
if (addrlen < sizeof(struct sockaddr_in)) {
ret = -EINVAL;
goto error;
}
ipv4_hdr = (struct net_ipv4_hdr *)net_pkt_get_data_new(pkt,
&ipv4_access);
if (!ipv4_hdr || net_pkt_acknowledge_data(pkt, &ipv4_access)) {
ret = -ENOBUFS;
goto error;
}
net_ipaddr_copy(&addr4->sin_addr, &ipv4_hdr->src);
port = &addr4->sin_port;
} else if (IS_ENABLED(CONFIG_NET_IPV6) &&
net_pkt_family(pkt) == AF_INET6) {
NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(ipv6_access,
struct net_ipv6_hdr);
struct sockaddr_in6 *addr6 = net_sin6(addr);
struct net_ipv6_hdr *ipv6_hdr;
if (addrlen < sizeof(struct sockaddr_in6)) {
ret = -EINVAL;
goto error;
}
ipv6_hdr = (struct net_ipv6_hdr *)net_pkt_get_data_new(pkt,
&ipv6_access);
if (!ipv6_hdr ||
net_pkt_acknowledge_data(pkt, &ipv6_access) ||
net_pkt_skip(pkt, net_pkt_ipv6_ext_len(pkt))) {
ret = -ENOBUFS;
goto error;
}
net_ipaddr_copy(&addr6->sin6_addr, &ipv6_hdr->src);
port = &addr6->sin6_port;
} else {
ret = -ENOTSUP;
goto error;
}
if (IS_ENABLED(CONFIG_NET_UDP) && proto == IPPROTO_UDP) {
NET_PKT_DATA_ACCESS_DEFINE(udp_access, struct net_udp_hdr);
struct net_udp_hdr *udp_hdr;
udp_hdr = (struct net_udp_hdr *)net_pkt_get_data_new(pkt,
&udp_access);
if (!udp_hdr) {
ret = -ENOBUFS;
goto error;
}
*port = udp_hdr->src_port;
} else if (IS_ENABLED(CONFIG_NET_TCP) && proto == IPPROTO_TCP) {
NET_PKT_DATA_ACCESS_DEFINE(tcp_access, struct net_tcp_hdr);
struct net_tcp_hdr *tcp_hdr;
tcp_hdr = (struct net_tcp_hdr *)net_pkt_get_data_new(pkt,
&tcp_access);
if (!tcp_hdr) {
ret = -ENOBUFS;
goto error;
}
*port = tcp_hdr->src_port;
} else {
ret = -ENOTSUP;
}
error:
net_pkt_cursor_restore(pkt, &backup);
return ret;
}
static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
void *buf,
size_t max_len,
@ -510,9 +611,10 @@ static inline ssize_t zsock_recv_dgram(struct net_context *ctx,
if (src_addr && addrlen) {
int rv;
rv = net_pkt_get_src_addr(pkt, src_addr, *addrlen);
rv = sock_get_pkt_src_addr(pkt, net_context_get_ip_proto(ctx),
src_addr, *addrlen);
if (rv < 0) {
errno = rv;
errno = -rv;
return -1;
}

View File

@ -108,69 +108,6 @@ static const unsigned char pkt3[199] = {
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96 /* ....... */
};
/* IPv6 TCP (224 bytes) */
static const unsigned char v6_tcp_pkt1[224] = {
/* IPv6 header starts here */
0x60, 0x00, /* .J....`. */
0x00, 0x00, 0x00, 0xaa, 0x06, 0x80, 0xfe, 0x80, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf9, 0x21, /* .......! */
0x19, 0xda, 0xf8, 0x9d, 0xf9, 0xa0, 0xfe, 0x80, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xe0, /* ........ */
0x4c, 0xff, 0xfe, 0x36, 0x03, 0xa1,
/* TCP header starts here */
0xf2, 0x50, /* L..6...P */
0x10, 0x92, 0x84, 0x19, 0x69, 0xb5, 0xb5, 0x20, /* ....i.. */
0x2d, 0x50, 0x50, 0x18, 0xfd, 0x20, 0x60, 0xfd, /* -PP.. `. */
0x00, 0x00, 0x37, 0xd9, 0x7a, 0x92, 0x2c, 0x0d, /* ..7.z.,. */
0x5b, 0x2a, 0x96, 0xa9, 0x7f, 0xf7, 0x4f, 0x40, /* [*....O@ */
0x1c, 0xdb, 0x1d, 0x72, 0xe6, 0xbd, 0x16, 0xe4, /* ...r.... */
0xb2, 0x07, 0xb8, 0xc1, 0x78, 0xab, 0xd9, 0xd4, /* ....x... */
0x8c, 0x00, 0xd9, 0x93, 0x83, 0xeb, 0x93, 0x85, /* ........ */
0xa3, 0x6b, 0x84, 0xf2, 0x2c, 0x47, 0x02, 0xbb, /* .k..,G.. */
0xbf, 0xa4, 0x17, 0xe1, 0x2d, 0x0b, 0xd5, 0xcc, /* ....-... */
0x16, 0xee, 0x8f, 0x20, 0xcb, 0xf8, 0x7d, 0xd2, /* ... ..}. */
0x4d, 0x4e, 0xf4, 0xbd, 0x18, 0x94, 0xb4, 0xa3, /* MN...... */
0x36, 0xf8, 0x11, 0x55, 0xf9, 0x52, 0x5a, 0x75, /* 6..U.RZu */
0x8d, 0x8e, 0x86, 0x2e, 0x95, 0x3d, 0x53, 0x1c, /* .....=S. */
0x86, 0xad, 0xc2, 0x82, 0x00, 0xb5, 0xcd, 0x78, /* .......x */
0x2e, 0xfc, 0x06, 0xa7, 0x63, 0x1f, 0xea, 0xf0, /* ....c... */
0xd5, 0x37, 0xe9, 0x80, 0x54, 0xe9, 0xd1, 0xd7, /* .7..T... */
0x44, 0x75, 0x8d, 0xe6, 0x83, 0x51, 0x59, 0x84, /* Du...QY. */
0x14, 0xa0, 0xe3, 0xcc, 0x31, 0xa5, 0x98, 0x69, /* ....1..i */
0x2d, 0x5c, 0xb4, 0x6e, 0x15, 0xd6, 0x57, 0xf2, /* -\.n..W. */
0x1f, 0xad, 0x94, 0xea, 0x2d, 0x2a, 0x34, 0x19, /* ....-*4. */
0xb8, 0xad, 0xf6, 0x77, 0x3b, 0x51, 0x0d, 0x18 /* ...w;Q.. */
};
/* IPv6 UDP (179 bytes) */
static const unsigned char v6_udp_pkt1[179] = {
/* IPv6 header starts here */
0x60, 0x09, /* .i....`. */
0xe5, 0x0e, 0x00, 0x7d, 0x11, 0xff, 0xfe, 0x80, /* ...}.... */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0xa7, /* ........ */
0xae, 0x7d, 0xf8, 0x04, 0x0c, 0xa5, 0xff, 0x02, /* .}...... */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x00, 0x00, 0x00, 0x00, 0xfb,
/* UDP header starts here */
0x14, 0xe9, /* ........ */
0x14, 0xe9, 0x00, 0x7d, 0xad, 0x58, 0x00, 0x00, /* ...}.X.. */
0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, /* ........ */
0x00, 0x01, 0x08, 0x5f, 0x68, 0x6f, 0x6d, 0x65, /* ..._home */
0x6b, 0x69, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70, /* kit._tcp */
0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, /* .local.. */
0x0c, 0x80, 0x01, 0x08, 0x5f, 0x61, 0x69, 0x72, /* ...._air */
0x70, 0x6c, 0x61, 0x79, 0xc0, 0x15, 0x00, 0x0c, /* play.... */
0x80, 0x01, 0x05, 0x5f, 0x72, 0x61, 0x6f, 0x70, /* ..._raop */
0xc0, 0x15, 0x00, 0x0c, 0x80, 0x01, 0x0c, 0x5f, /* ......._ */
0x73, 0x6c, 0x65, 0x65, 0x70, 0x2d, 0x70, 0x72, /* sleep-pr */
0x6f, 0x78, 0x79, 0x04, 0x5f, 0x75, 0x64, 0x70, /* oxy._udp */
0xc0, 0x1a, 0x00, 0x0c, 0x80, 0x01, 0x00, 0x00, /* ........ */
0x29, 0x05, 0xa0, 0x00, 0x00, 0x11, 0x94, 0x00, /* )....... */
0x12, 0x00, 0x04, 0x00, 0x0e, 0x00, 0x7a, 0x7e, /* ......z~ */
0x01, 0x91, 0x69, 0xb5, 0xbb, 0x7c, 0x01, 0x91, /* ..i..|.. */
0x69, 0xb5, 0xbb /* i.. */
};
#endif /* CONFIG_NET_IPV6 */
#if defined(CONFIG_NET_IPV4)
@ -212,54 +149,6 @@ static const unsigned char pkt5[98] = {
0x36, 0x37 /* 67 */
};
/* IPv4 TCP (90 bytes) */
static const unsigned char v4_tcp_pkt1[90] = {
/* IPv4 header starts here */
0x45, 0x10, /* L6....E. */
0x00, 0x4c, 0x99, 0x3c, 0x40, 0x00, 0x40, 0x06, /* .L.<@.@. */
0x1c, 0x5a, 0xc0, 0xa8, 0x01, 0xdb, 0xc0, 0xa8, /* .Z...... */
0x01, 0xda,
/* TCP header starts here */
0x00, 0x16, 0xcc, 0x1d, 0xa4, 0xeb, /* ........ */
0xe4, 0x82, 0x43, 0x60, 0x23, 0x46, 0x50, 0x18, /* ..C`#FP. */
0x01, 0x46, 0x85, 0x44, 0x00, 0x00, 0xe7, 0xe7, /* .F.D.... */
0x3f, 0x13, 0xe2, 0xc1, 0x09, 0x37, 0x90, 0x37, /* ?....7.7 */
0x02, 0x0e, 0x17, 0x12, 0xb6, 0x31, 0xd6, 0x22, /* .....1." */
0xb9, 0x04, 0x71, 0x57, 0x21, 0x1a, 0xe2, 0x5d, /* ..qW!..] */
0xef, 0xfc, 0xc3, 0x66, 0x47, 0x7f, 0xf3, 0x5b, /* ...fG..[ */
0xc7, 0x9b /* .. */
};
/* IPv4 UDP (192 bytes) */
static const unsigned char v4_udp_pkt1[192] = {
/* IPv4 header starts here */
0x45, 0x00, /* .J....E. */
0x00, 0xb2, 0x06, 0x85, 0x00, 0x00, 0x80, 0x11, /* ........ */
0xae, 0xb1, 0xc0, 0xa8, 0x01, 0xd9, 0xc0, 0xa8, /* ........ */
0x01, 0xdb,
/* UDP header starts here */
0xfb, 0xaa, 0x10, 0x92, 0x00, 0x9e, /* ........ */
0xd2, 0x30, 0x57, 0x6d, 0x3d, 0xfc, 0x78, 0x76, /* .0Wm=.xv */
0xf7, 0xa5, 0xd8, 0x21, 0x92, 0x34, 0x7e, 0xe9, /* ...!.4~. */
0x5f, 0x7f, 0xe4, 0xbe, 0x39, 0x54, 0x51, 0x04, /* _...9TQ. */
0xd5, 0x00, 0x19, 0x76, 0x3b, 0xf3, 0xdc, 0x6a, /* ...v;..j */
0xbd, 0xaa, 0x8a, 0x7e, 0xc3, 0xf5, 0x57, 0xea, /* ...~..W. */
0x28, 0xb5, 0xea, 0x34, 0x41, 0x0c, 0xbc, 0x1b, /* (..4A... */
0x3d, 0xfb, 0xf3, 0x71, 0x58, 0xda, 0x51, 0x01, /* =..qX.Q. */
0x39, 0xcc, 0x03, 0xdf, 0x5e, 0xef, 0x43, 0x16, /* 9...^.C. */
0x8d, 0xe7, 0xe5, 0xae, 0x49, 0xa4, 0x9b, 0xa1, /* ....I... */
0xe1, 0x78, 0xc5, 0x25, 0x96, 0xec, 0x2f, 0xda, /* .x.%../. */
0x70, 0xa5, 0x57, 0x4f, 0xd0, 0xb8, 0x32, 0xcd, /* p.WO..2. */
0x20, 0xd3, 0x51, 0x2f, 0x2f, 0x09, 0x6b, 0x7f, /* .Q//.k. */
0xce, 0x55, 0x2c, 0x3b, 0x77, 0x20, 0x75, 0xb4, /* .U,;w u. */
0x73, 0x3f, 0x51, 0xba, 0x68, 0x7b, 0x7f, 0xfe, /* s?Q.h{.. */
0x54, 0x72, 0xc1, 0x20, 0x14, 0x9c, 0xd3, 0x45, /* Tr. ...E */
0xf3, 0x30, 0x5f, 0x06, 0xdc, 0xac, 0x00, 0x67, /* .0_....g */
0x56, 0xb7, 0x3f, 0xdd, 0x7d, 0xd7, 0xb0, 0xc3, /* V.?.}... */
0xd8, 0x45, 0x73, 0xa6, 0x6f, 0xb8, 0x14, 0x13, /* .Es.o... */
0xaf, 0xcf, 0xcf, 0x53, 0x93, 0xf1, 0xa0, 0x1a /* ...S.... */
};
#endif /* CONFIG_NET_IPV4 */
void test_utils(void)
@ -1318,256 +1207,12 @@ void test_addr_parse(void)
#endif
}
void test_net_pkt_addr_parse(void)
{
#if defined(CONFIG_NET_IPV6)
static struct ipv6_test_data {
const unsigned char *payload;
int payload_len;
u8_t proto;
struct sockaddr_in6 src;
struct sockaddr_in6 dst;
} ipv6_test_data_set[] = {
{
.payload = v6_udp_pkt1,
.payload_len = sizeof(v6_udp_pkt1),
.proto = IPPROTO_UDP,
.src = {
.sin6_family = AF_INET6,
.sin6_port = htons(5353),
.sin6_addr = {
.s6_addr16[0] = ntohs(0xfe80),
.s6_addr16[1] = 0,
.s6_addr16[2] = 0,
.s6_addr16[3] = 0,
.s6_addr16[4] = ntohs(0x14a7),
.s6_addr16[5] = ntohs(0xae7d),
.s6_addr16[6] = ntohs(0xf804),
.s6_addr16[7] = ntohs(0x0ca5),
},
},
.dst = {
.sin6_family = AF_INET6,
.sin6_port = htons(5353),
.sin6_addr = {
.s6_addr16[0] = ntohs(0xff02),
.s6_addr16[1] = 0,
.s6_addr16[2] = 0,
.s6_addr16[3] = 0,
.s6_addr16[4] = 0,
.s6_addr16[5] = 0,
.s6_addr16[6] = 0,
.s6_addr16[7] = ntohs(0x00fb),
},
},
},
{
.payload = v6_tcp_pkt1,
.payload_len = sizeof(v6_tcp_pkt1),
.proto = IPPROTO_TCP,
.src = {
.sin6_family = AF_INET6,
.sin6_port = htons(62032),
.sin6_addr = {
.s6_addr16[0] = ntohs(0xfe80),
.s6_addr16[1] = 0,
.s6_addr16[2] = 0,
.s6_addr16[3] = 0,
.s6_addr16[4] = ntohs(0xf921),
.s6_addr16[5] = ntohs(0x19da),
.s6_addr16[6] = ntohs(0xf89d),
.s6_addr16[7] = ntohs(0xf9a0),
},
},
.dst = {
.sin6_family = AF_INET6,
.sin6_port = htons(4242),
.sin6_addr = {
.s6_addr16[0] = ntohs(0xfe80),
.s6_addr16[1] = 0,
.s6_addr16[2] = 0,
.s6_addr16[3] = 0,
.s6_addr16[4] = ntohs(0x02e0),
.s6_addr16[5] = ntohs(0x4cff),
.s6_addr16[6] = ntohs(0xfe36),
.s6_addr16[7] = ntohs(0x03a1),
},
},
},
};
#endif
#if defined(CONFIG_NET_IPV4)
static struct ipv4_test_data {
const unsigned char *payload;
int payload_len;
u8_t proto;
struct sockaddr_in src;
struct sockaddr_in dst;
} ipv4_test_data_set[] = {
{
.payload = v4_tcp_pkt1,
.payload_len = sizeof(v4_tcp_pkt1),
.proto = IPPROTO_TCP,
.src = {
.sin_family = AF_INET,
.sin_port = htons(22),
.sin_addr = {
.s4_addr[0] = 192,
.s4_addr[1] = 168,
.s4_addr[2] = 1,
.s4_addr[3] = 219,
},
},
.dst = {
.sin_family = AF_INET,
.sin_port = htons(52253),
.sin_addr = {
.s4_addr[0] = 192,
.s4_addr[1] = 168,
.s4_addr[2] = 1,
.s4_addr[3] = 218,
},
},
},
{
.payload = v4_udp_pkt1,
.payload_len = sizeof(v4_udp_pkt1),
.proto = IPPROTO_UDP,
.src = {
.sin_family = AF_INET,
.sin_port = htons(64426),
.sin_addr = {
.s4_addr[0] = 192,
.s4_addr[1] = 168,
.s4_addr[2] = 1,
.s4_addr[3] = 217,
},
},
.dst = {
.sin_family = AF_INET,
.sin_port = htons(4242),
.sin_addr = {
.s4_addr[0] = 192,
.s4_addr[1] = 168,
.s4_addr[2] = 1,
.s4_addr[3] = 219,
},
},
},
};
#endif
#if defined(CONFIG_NET_IPV6)
for (int i = 0; i < ARRAY_SIZE(ipv6_test_data_set); i++) {
struct net_pkt *pkt;
struct net_buf *frag;
struct sockaddr_in6 addr;
struct ipv6_test_data *data = &ipv6_test_data_set[i];
pkt = net_pkt_get_reserve_rx(K_SECONDS(1));
zassert_not_null(pkt, "Out of mem");
frag = net_pkt_get_reserve_rx_data(K_SECONDS(1));
zassert_not_null(frag, "Out of mem");
net_pkt_frag_add(pkt, frag);
/* Copy data */
net_pkt_append(pkt, data->payload_len, data->payload,
K_FOREVER);
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv6_hdr));
net_pkt_set_family(pkt, AF_INET6);
net_pkt_set_transport_proto(pkt, data->proto);
zassert_equal(net_pkt_get_src_addr(pkt,
(struct sockaddr *)&addr,
sizeof(addr)),
0,
"parse failed");
zassert_equal(addr.sin6_port, data->src.sin6_port,
"wrong src port");
zassert_true(net_ipv6_addr_cmp(&addr.sin6_addr,
&data->src.sin6_addr),
"wrong src addr");
zassert_equal(net_pkt_get_dst_addr(pkt,
(struct sockaddr *)&addr,
sizeof(addr)),
0,
"parse failed");
zassert_equal(addr.sin6_port, data->dst.sin6_port,
"wrong dst port");
zassert_true(net_ipv6_addr_cmp(&addr.sin6_addr,
&data->dst.sin6_addr),
"unexpected dst addr");
net_pkt_unref(pkt);
}
#endif
#if defined(CONFIG_NET_IPV4)
for (int i = 0; i < ARRAY_SIZE(ipv4_test_data_set); i++) {
struct net_pkt *pkt;
struct net_buf *frag;
struct sockaddr_in addr;
struct ipv4_test_data *data = &ipv4_test_data_set[i];
pkt = net_pkt_get_reserve_rx(K_SECONDS(1));
zassert_not_null(pkt, "Out of mem");
frag = net_pkt_get_reserve_rx_data(K_SECONDS(1));
zassert_not_null(frag, "Out of mem");
net_pkt_frag_add(pkt, frag);
/* Copy data */
net_pkt_append(pkt, data->payload_len, data->payload,
K_FOREVER);
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
net_pkt_set_family(pkt, AF_INET);
net_pkt_set_transport_proto(pkt, data->proto);
zassert_equal(net_pkt_get_src_addr(pkt,
(struct sockaddr *)&addr,
sizeof(addr)),
0,
"parse failed");
zassert_equal(addr.sin_port, data->src.sin_port,
"wrong src port");
zassert_true(net_ipv4_addr_cmp(&addr.sin_addr,
&data->src.sin_addr),
"wrong src addr");
zassert_equal(net_pkt_get_dst_addr(pkt,
(struct sockaddr *)&addr,
sizeof(addr)),
0,
"parse failed");
zassert_equal(addr.sin_port, data->dst.sin_port,
"wrong dst port");
zassert_true(net_ipv4_addr_cmp(&addr.sin_addr,
&data->dst.sin_addr),
"unexpected dst addr");
net_pkt_unref(pkt);
}
#endif
}
void test_main(void)
{
ztest_test_suite(test_utils_fn,
ztest_unit_test(test_utils),
ztest_unit_test(test_net_addr),
ztest_unit_test(test_addr_parse),
ztest_unit_test(test_net_pkt_addr_parse));
ztest_unit_test(test_addr_parse));
ztest_run_test_suite(test_utils_fn);
}