Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion subsys/net/ip/net_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,49 @@ int net_context_put(struct net_context *context)
return 0;
}

static int bind_default(struct net_context *context,
const struct sockaddr *dest_addr)
{
struct net_if *iface = net_if_get_default();

if (!iface) {
NET_ERR("No default interface to bind");
return -EADDRNOTAVAIL;
}

#if defined(CONFIG_NET_IPV6)
if (dest_addr->family == AF_INET6) {
if (net_sin6_ptr(&context->local)->sin6_addr) {
return 0;
}

net_context_set_iface(context, iface);
net_sin6_ptr(&context->local)->sin6_family = AF_INET6;
net_sin6_ptr(&context->local)->sin6_addr =
(struct in6_addr *)net_ipv6_unspecified_address();

Copy link
Member

@jukkar jukkar Jul 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check the port number here too. The net_context_get() sets the initial value for the port, but it might be used already when we get into this point. So we need to check it by calling check_used_port() like the net_context_bind() is doing, and probably call find_available_port() again if the port was used already (unlikely to happen thou).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's treat this as a separate issue. So far, I indeed rely that random port was set at the time of context creation. As you say, that's actually too early, and may be already used at the time of actual network operation. But the obvious solution is to move setting random port to bind_default(), which is a separate patch, requiring separate consideration and testing (UDP cases, etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, lets deal with the port issue in later patches.

return 0;
}
#endif

#if defined(CONFIG_NET_IPV4)
if (dest_addr->family == AF_INET) {
if (net_sin_ptr(&context->local)->sin_addr) {
return 0;
}

net_context_set_iface(context, iface);
net_sin_ptr(&context->local)->sin_family = AF_INET;
net_sin_ptr(&context->local)->sin_addr =
(struct in_addr *)net_ipv4_unspecified_address();

return 0;
}
#endif

return -EINVAL;
}

int net_context_bind(struct net_context *context, const struct sockaddr *addr,
socklen_t addrlen)
{
Expand Down Expand Up @@ -1331,8 +1374,8 @@ int net_context_connect(struct net_context *context,
struct sockaddr *laddr = NULL;
struct sockaddr local_addr;
u16_t lport, rport;
int ret;
#endif
int ret;

NET_ASSERT(addr);
NET_ASSERT(PART_OF_ARRAY(contexts, context));
Expand All @@ -1344,6 +1387,11 @@ int net_context_connect(struct net_context *context,
return -EBADF;
}

ret = bind_default(context, addr);
if (ret) {
return ret;
}

if (addr->family != net_context_get_family(context)) {
NET_ASSERT_INFO(addr->family == \
net_context_get_family(context),
Expand Down