diff --git a/subsys/net/ip/tcp2.c b/subsys/net/ip/tcp2.c index 0d931351548f6..9cc2af7047313 100644 --- a/subsys/net/ip/tcp2.c +++ b/subsys/net/ip/tcp2.c @@ -58,38 +58,57 @@ static size_t tcp_endpoint_len(sa_family_t af) sizeof(struct sockaddr_in6); } -static union tcp_endpoint *tcp_endpoint_new(struct net_pkt *pkt, int src) +static union tcp_endpoint *tcp_endpoint_new(struct net_pkt *pkt) { sa_family_t af = net_pkt_family(pkt); union tcp_endpoint *ep = tcp_calloc(1, tcp_endpoint_len(af)); - ep->sa.sa_family = af; + if (ep) { + ep->sa.sa_family = af; + } - switch (af) { - case AF_INET: { - struct net_ipv4_hdr *ip = (struct net_ipv4_hdr *) - net_pkt_ip_data(pkt); - struct tcphdr *th = th_get(pkt); + return ep; +} - ep->sin.sin_port = src ? th->th_sport : th->th_dport; +static union tcp_endpoint *tcp_endpoint_set(union tcp_endpoint *ep, + struct net_pkt *pkt, + enum pkt_addr src) +{ + if (!ep) { + return NULL; + } - ep->sin.sin_addr = src ? ip->src : ip->dst; + switch (ep->sa.sa_family) { + case AF_INET: + if (IS_ENABLED(CONFIG_NET_IPV4)) { + struct net_ipv4_hdr *ip = (struct net_ipv4_hdr *) + net_pkt_ip_data(pkt); + struct tcphdr *th = th_get(pkt); + + ep->sin.sin_port = src == TCP_EP_SRC ? th->th_sport : + th->th_dport; + ep->sin.sin_addr = src == TCP_EP_SRC ? ip->src : + ip->dst; + } break; - } - case AF_INET6: { - struct net_ipv6_hdr *ip = (struct net_ipv6_hdr *) - net_pkt_ip_data(pkt); - struct tcphdr *th = th_get(pkt); - - ep->sin6.sin6_port = src ? th->th_sport : th->th_dport; - ep->sin6.sin6_addr = src ? ip->src : ip->dst; + case AF_INET6: + if (IS_ENABLED(CONFIG_NET_IPV6)) { + struct net_ipv6_hdr *ip = (struct net_ipv6_hdr *) + net_pkt_ip_data(pkt); + struct tcphdr *th = th_get(pkt); + + ep->sin6.sin6_port = src == TCP_EP_SRC ? th->th_sport : + th->th_dport; + ep->sin6.sin6_addr = src == TCP_EP_SRC ? ip->src : + ip->dst; + } break; - } + default: - NET_ERR("Unknown address family: %hu", af); + NET_ERR("Unknown address family: %hu", ep->sa.sa_family); } return ep; @@ -689,15 +708,15 @@ int net_tcp_get(struct net_context *context) } static bool tcp_endpoint_cmp(union tcp_endpoint *ep, struct net_pkt *pkt, - int which) + enum pkt_addr which) { - union tcp_endpoint *ep_new = tcp_endpoint_new(pkt, which); - bool is_equal = memcmp(ep, ep_new, tcp_endpoint_len(ep->sa.sa_family)) ? - false : true; + union tcp_endpoint ep_tmp = { 0 }, *ep_ptr; + + ep_tmp.sa.sa_family = net_pkt_family(pkt); - tcp_free(ep_new); + ep_ptr = tcp_endpoint_set(&ep_tmp, pkt, which); - return is_equal; + return !memcmp(ep, ep_ptr, tcp_endpoint_len(ep->sa.sa_family)); } static bool tcp_conn_cmp(struct tcp *conn, struct net_pkt *pkt) @@ -789,8 +808,8 @@ static struct tcp *tcp_conn_new(struct net_pkt *pkt) net_context_set_family(conn->context, pkt->family); - conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); - conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); + conn->dst = tcp_endpoint_set(tcp_endpoint_new(pkt), pkt, TCP_EP_SRC); + conn->src = tcp_endpoint_set(tcp_endpoint_new(pkt), pkt, TCP_EP_DST); NET_DBG("conn: src: %s, dst: %s", log_strdup(tcp_endpoint_to_string(conn->src)), @@ -1259,8 +1278,10 @@ static enum net_verdict tcp_input(struct net_conn *net_conn, net_tcp_get(context); net_context_set_family(context, pkt->family); conn = context->tcp; - conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); - conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); + conn->dst = tcp_endpoint_set(tcp_endpoint_new(pkt), + pkt, TCP_EP_SRC); + conn->src = tcp_endpoint_set(tcp_endpoint_new(pkt), + pkt, TCP_EP_DST); /* Make an extra reference, the sanity check suite * will delete the connection explicitly */ @@ -1379,8 +1400,12 @@ enum net_verdict tp_input(struct net_conn *net_conn, net_tcp_get(context); net_context_set_family(context, pkt->family); conn = context->tcp; - conn->dst = tcp_endpoint_new(pkt, TCP_EP_SRC); - conn->src = tcp_endpoint_new(pkt, TCP_EP_DST); + conn->dst = + tcp_endpoint_set(tcp_endpoint_new(pkt), + pkt, TCP_EP_SRC); + conn->src = + tcp_endpoint_set(tcp_endpoint_new(pkt), + pkt, TCP_EP_DST); conn->iface = pkt->iface; tcp_conn_ref(conn); } diff --git a/tests/net/tcp2/src/main.c b/tests/net/tcp2/src/main.c index 3a9b334090b52..17b7c29204192 100644 --- a/tests/net/tcp2/src/main.c +++ b/tests/net/tcp2/src/main.c @@ -149,13 +149,13 @@ static void test_sem_give(void) k_sem_give(&test_sem); } -static void test_sem_take(k_timeout_t timeout) +static void test_sem_take(k_timeout_t timeout, int line) { sem = true; k_sem_take(&test_sem, timeout); if (sem) { - zassert_true(false, "semaphore timed out"); + zassert_true(false, "semaphore timed out (line %d)", line); } } @@ -467,7 +467,7 @@ static void test_client_ipv4(void) /* Peer will release the semaphone after it receives * proper ACK to SYN | ACK */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL); if (ret < 0) { @@ -475,14 +475,14 @@ static void test_client_ipv4(void) } /* Peer will release the semaphone after it sends ACK for data */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); net_tcp_put(ctx); /* Peer will release the semaphone after it receives * proper ACK to FIN | ACK */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); } /* Test case scenario IPv6 @@ -522,7 +522,7 @@ static void test_client_ipv6(void) /* Peer will release the semaphone after it receives * proper ACK to SYN | ACK */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL); if (ret < 0) { @@ -530,14 +530,14 @@ static void test_client_ipv6(void) } /* Peer will release the semaphone after it sends ACK for data */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); net_tcp_put(ctx); /* Peer will release the semaphone after it receives * proper ACK to FIN | ACK */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); } static void handle_server_test(sa_family_t af, struct tcphdr *th) @@ -681,7 +681,7 @@ static void test_server_ipv4(void) /* test_tcp_accept_cb will release the semaphone after succesfull * connection. */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); /* Trigger the peer to send DATA */ k_delayed_work_submit(&test_server, K_NO_WAIT); @@ -744,7 +744,7 @@ static void test_server_with_options_ipv4(void) /* test_tcp_accept_cb will release the semaphone after succesfull * connection. */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); /* Trigger the peer to send DATA */ k_delayed_work_submit(&test_server, K_NO_WAIT); @@ -807,7 +807,7 @@ static void test_server_ipv6(void) /* test_tcp_accept_cb will release the semaphone after succesfull * connection. */ - test_sem_take(K_MSEC(100)); + test_sem_take(K_MSEC(100), __LINE__); /* Trigger the peer to send DATA */ k_delayed_work_submit(&test_server, K_NO_WAIT); @@ -863,7 +863,7 @@ static void test_client_syn_resend(void) } /* test handler will release the sem once it receives SYN again */ - test_sem_take(K_MSEC(500)); + test_sem_take(K_MSEC(500), __LINE__); net_context_put(ctx); }