From c9e6ef5331e66102f35e0296afebd686d20dcf71 Mon Sep 17 00:00:00 2001 From: Jukka Rissanen Date: Fri, 2 Jun 2017 08:51:54 +0300 Subject: [PATCH] net: http: Use random source port when connecting If we re-connect to same peer server, then we should select a new source port. Noticed that if the same source port as before is used for the new connection, the peer might drop the packet. This was seen when connecting to Linux peer. Signed-off-by: Jukka Rissanen --- subsys/net/lib/http/http_client.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/subsys/net/lib/http/http_client.c b/subsys/net/lib/http/http_client.c index 0ce4c9923ac..5f3ce7cb8b2 100644 --- a/subsys/net/lib/http/http_client.c +++ b/subsys/net/lib/http/http_client.c @@ -491,6 +491,17 @@ static int tcp_connect(struct http_client_ctx *ctx) if (ctx->tcp.remote.family == AF_INET6) { addrlen = sizeof(struct sockaddr_in6); + + /* If we are reconnecting, then make sure the source port + * is re-calculated so that the peer will not get confused + * which connection the connection is related to. + * This was seen in Linux which dropped packets when the same + * source port was for a new connection after the old connection + * was terminated. + */ + net_sin6(&ctx->tcp.local)->sin6_port = 0; + } else { + net_sin(&ctx->tcp.local)->sin_port = 0; } ret = get_local_addr(ctx);