net: sockets: Fix connected datagram socket packet filtering
The previous patch to address race condition on STREAM sockets had a side effect on DGRAM socket, where net_context_recv() is not only installing recv callback, but also registering a connection at net_conn level. Doing so before setting remote address first (which is done in net_context_connect()) had an impact on the connected DGRAM socket operation, which now accepted packets from any remote peer, and not only the one socket was connected to. Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
This commit is contained in:
parent
e8a00b7ef6
commit
6e1a205819
1 changed files with 16 additions and 4 deletions
|
@ -525,10 +525,22 @@ int zsock_connect_ctx(struct net_context *ctx, const struct sockaddr *addr,
|
|||
cb = zsock_connected_cb;
|
||||
}
|
||||
|
||||
SET_ERRNO(net_context_recv(ctx, zsock_received_cb, K_NO_WAIT,
|
||||
ctx->user_data));
|
||||
SET_ERRNO(net_context_connect(ctx, addr, addrlen, cb, timeout,
|
||||
ctx->user_data));
|
||||
if (net_context_get_type(ctx) == SOCK_STREAM) {
|
||||
/* For STREAM sockets net_context_recv() only installs
|
||||
* recv callback w/o side effects, and it has to be done
|
||||
* first to avoid race condition, when TCP stream data
|
||||
* arrives right after connect.
|
||||
*/
|
||||
SET_ERRNO(net_context_recv(ctx, zsock_received_cb,
|
||||
K_NO_WAIT, ctx->user_data));
|
||||
SET_ERRNO(net_context_connect(ctx, addr, addrlen, cb,
|
||||
timeout, ctx->user_data));
|
||||
} else {
|
||||
SET_ERRNO(net_context_connect(ctx, addr, addrlen, cb,
|
||||
timeout, ctx->user_data));
|
||||
SET_ERRNO(net_context_recv(ctx, zsock_received_cb,
|
||||
K_NO_WAIT, ctx->user_data));
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue