Skip to content

Commit fe932dd

Browse files
jukkarhenrikbrixandersen
authored andcommitted
net: context: Remember socket shutdown status
Use the socket eof flag to determine whether the receive callback should be installed. If the receive cb is not installed, then the socket cannot receive any data. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent a08d48e commit fe932dd

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

subsys/net/lib/sockets/sockets_inet.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,21 @@ static void zsock_received_cb(struct net_context *ctx,
225225
int status,
226226
void *user_data)
227227
{
228+
if (sock_is_eof(ctx)) {
229+
/* If receiving is not desired and socket is shutdown,
230+
* ignore all incoming data.
231+
*/
232+
NET_DBG("%sctx=%p, pkt=%p, st=%d, user_data=%p",
233+
"DROP: ", ctx, pkt, status, user_data);
234+
net_pkt_unref(pkt);
235+
return;
236+
}
237+
228238
if (ctx->cond.lock) {
229239
(void)k_mutex_lock(ctx->cond.lock, K_FOREVER);
230240
}
231241

232-
NET_DBG("ctx=%p, pkt=%p, st=%d, user_data=%p", ctx, pkt, status,
242+
NET_DBG("%sctx=%p, pkt=%p, st=%d, user_data=%p", "", ctx, pkt, status,
233243
user_data);
234244

235245
if (status < 0) {
@@ -363,12 +373,16 @@ int zsock_connect_ctx(struct net_context *ctx, const struct sockaddr *addr,
363373
errno = -ret;
364374
return -1;
365375
}
366-
ret = net_context_recv(ctx, zsock_received_cb,
367-
K_NO_WAIT, ctx->user_data);
368-
if (ret < 0) {
369-
errno = -ret;
370-
return -1;
376+
377+
if (!sock_is_eof(ctx)) {
378+
ret = net_context_recv(ctx, zsock_received_cb,
379+
K_NO_WAIT, ctx->user_data);
380+
if (ret < 0) {
381+
errno = -ret;
382+
return -1;
383+
}
371384
}
385+
372386
return 0;
373387
}
374388
#endif
@@ -416,11 +430,14 @@ int zsock_connect_ctx(struct net_context *ctx, const struct sockaddr *addr,
416430
errno = -ret;
417431
return -1;
418432
}
419-
ret = net_context_recv(ctx, zsock_received_cb,
420-
K_NO_WAIT, ctx->user_data);
421-
if (ret < 0) {
422-
errno = -ret;
423-
return -1;
433+
434+
if (!sock_is_eof(ctx)) {
435+
ret = net_context_recv(ctx, zsock_received_cb,
436+
K_NO_WAIT, ctx->user_data);
437+
if (ret < 0) {
438+
errno = -ret;
439+
return -1;
440+
}
424441
}
425442
}
426443

@@ -643,11 +660,13 @@ ssize_t zsock_sendto_ctx(struct net_context *ctx, const void *buf, size_t len,
643660
/* Register the callback before sending in order to receive the response
644661
* from the peer.
645662
*/
646-
status = net_context_recv(ctx, zsock_received_cb,
647-
K_NO_WAIT, ctx->user_data);
648-
if (status < 0) {
649-
errno = -status;
650-
return -1;
663+
if (!sock_is_eof(ctx)) {
664+
status = net_context_recv(ctx, zsock_received_cb,
665+
K_NO_WAIT, ctx->user_data);
666+
if (status < 0) {
667+
errno = -status;
668+
return -1;
669+
}
651670
}
652671

653672
while (1) {

0 commit comments

Comments
 (0)