X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=utcp.c;h=d846fb1965bd4384bef7239ae0781e08fb257d8f;hb=e572986223f5a373e2873f916d55d7cc282da074;hp=e8755ed14470577b6baafbab0d127476454e4dba;hpb=b570e123128f3fafa583bae186fd61429dcb49a7;p=utcp diff --git a/utcp.c b/utcp.c index e8755ed..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; @@ -1154,6 +1167,17 @@ struct timeval utcp_timeout(struct utcp *utcp) { return diff; } +bool utcp_is_active(struct utcp *utcp) { + if(!utcp) + return false; + + for(int i = 0; i < utcp->nconnections; i++) + if(utcp->connections[i]->state != CLOSED && utcp->connections[i]->state != TIME_WAIT) + return true; + + return false; +} + struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_send_t send, void *priv) { struct utcp *utcp = calloc(1, sizeof *utcp); if(!utcp) @@ -1256,3 +1280,10 @@ void utcp_set_poll_cb(struct utcp_connection *c, utcp_poll_t poll) { if(c) c->poll = poll; } + +void utcp_set_accept_cb(struct utcp *utcp, utcp_accept_t accept, utcp_pre_accept_t pre_accept) { + if(utcp) { + utcp->accept = accept; + utcp->pre_accept = pre_accept; + } +}