From 619f181e8507d7195b5f855d9fb7922e956f8233 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 11 Oct 2015 17:02:37 +0200 Subject: [PATCH] Handle direction argument of utcp_shutdown(). For TCP, only shutting down the send direction makes sense, however to be compatible to the BSD sockets API, keep the direction argument, and when someone tries to shut down the receive direction, just disable the receive callback. Note that on most operating systems, SHUT_RD actually doesn't do anything at all, it won't prevent reads from returning data. Also be a bit more strict, return EINVAL or ENOTCONN when appropriate. --- utcp.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/utcp.c b/utcp.c index c63def1..d846fb1 100644 --- a/utcp.c +++ b/utcp.c @@ -1000,13 +1000,26 @@ int utcp_shutdown(struct utcp_connection *c, int dir) { return -1; } - // TODO: handle dir - // TODO: check that repeated calls with the same parameters should have no effect + if(!(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_WR || dir == UTCP_SHUT_RDWR)) { + errno = EINVAL; + return -1; + } + + // TCP does not have a provision for stopping incoming packets. + // The best we can do is to just ignore them. + if(dir == UTCP_SHUT_RD || dir == UTCP_SHUT_RDWR) + c->recv = NULL; + + // The rest of the code deals with shutting down writes. + if(dir == UTCP_SHUT_RD) + return 0; switch(c->state) { case CLOSED: - return 0; case LISTEN: + errno = ENOTCONN; + return -1; + case SYN_SENT: set_state(c, CLOSED); return 0; -- 2.39.5