X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=src%2Fmeshlink.c;h=7c5bbd22741476532e0d17cdfbe992a7e2056276;hb=64db0bb3f32236b55746bf62002faca08a54e364;hp=b85795d465e3c64c59bc6c0990f3deb60dfd3f86;hpb=fe7be9e345bcc652914ef6fc51302da13d4c9c8d;p=meshlink diff --git a/src/meshlink.c b/src/meshlink.c index b85795d4..7c5bbd22 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -1950,7 +1950,7 @@ void meshlink_set_error_cb(struct meshlink_handle *mesh, meshlink_error_cb_t cb) static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination, const void *data, size_t len, vpn_packet_t *packet) { meshlink_packethdr_t *hdr; - if(len >= MAXSIZE - sizeof(*hdr)) { + if(len > MAXSIZE - sizeof(*hdr)) { meshlink_errno = MESHLINK_EINVAL; return false; } @@ -1972,8 +1972,8 @@ static bool prepare_packet(meshlink_handle_t *mesh, meshlink_node_t *destination memset(hdr, 0, sizeof(*hdr)); // leave the last byte as 0 to make sure strings are always // null-terminated if they are longer than the buffer - strncpy((char *)hdr->destination, destination->name, (sizeof(hdr)->destination) - 1); - strncpy((char *)hdr->source, mesh->self->name, (sizeof(hdr)->source) - 1); + strncpy((char *)hdr->destination, destination->name, sizeof(hdr->destination) - 1); + strncpy((char *)hdr->source, mesh->self->name, sizeof(hdr->source) - 1); memcpy(packet->data + sizeof(*hdr), data, len); @@ -2023,6 +2023,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const if(!prepare_packet(mesh, destination, data, len, packet)) { free(packet); + return false; } // Queue it @@ -3454,16 +3455,46 @@ static bool channel_pre_accept(struct utcp *utcp, uint16_t port) { return mesh->channel_accept_cb; } -static void aio_signal(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_aio_buffer_t *aio) { - if(aio->data) { - if(aio->cb.buffer) { - aio->cb.buffer(mesh, channel, aio->data, aio->len, aio->priv); +/* Finish one AIO buffer, return true if the channel is still open. */ +static bool aio_finish_one(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_aio_buffer_t **head) { + meshlink_aio_buffer_t *aio = *head; + *head = aio->next; + + if(channel->c) { + channel->in_callback = true; + + if(aio->data) { + if(aio->cb.buffer) { + aio->cb.buffer(mesh, channel, aio->data, aio->len, aio->priv); + } + } else { + if(aio->cb.fd) { + aio->cb.fd(mesh, channel, aio->fd, aio->done, aio->priv); + } } - } else { - if(aio->cb.fd) { - aio->cb.fd(mesh, channel, aio->fd, aio->done, aio->priv); + + channel->in_callback = false; + + if(!channel->c) { + free(aio); + free(channel); + return false; } } + + free(aio); + return true; +} + +/* Finish all AIO buffers, return true if the channel is still open. */ +static bool aio_abort(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_aio_buffer_t **head) { + while(*head) { + if(!aio_finish_one(mesh, channel, head)) { + return false; + } + } + + return true; } static ssize_t channel_recv(struct utcp_connection *connection, const void *data, size_t len) { @@ -3485,6 +3516,15 @@ static ssize_t channel_recv(struct utcp_connection *connection, const void *data size_t left = len; while(channel->aio_receive) { + if(!len) { + /* This receive callback signalled an error, abort all outstanding AIO buffers. */ + if(!aio_abort(mesh, channel, &channel->aio_receive)) { + return len; + } + + break; + } + meshlink_aio_buffer_t *aio = channel->aio_receive; size_t todo = aio->len - aio->done; @@ -3497,23 +3537,35 @@ static ssize_t channel_recv(struct utcp_connection *connection, const void *data } else { ssize_t result = write(aio->fd, p, todo); - if(result > 0) { - todo = result; + if(result <= 0) { + if(result < 0 && errno == EINTR) { + continue; + } + + /* Writing to fd failed, cancel just this AIO buffer. */ + logger(mesh, MESHLINK_ERROR, "Writing to AIO fd %d failed: %s", aio->fd, strerror(errno)); + + if(!aio_finish_one(mesh, channel, &channel->aio_receive)) { + return len; + } + + continue; } + + todo = result; } aio->done += todo; + p += todo; + left -= todo; if(aio->done == aio->len) { - channel->aio_receive = aio->next; - aio_signal(mesh, channel, aio); - free(aio); + if(!aio_finish_one(mesh, channel, &channel->aio_receive)) { + return len; + } } - p += todo; - left -= todo; - - if(!left && len) { + if(!left) { return len; } } @@ -3549,6 +3601,17 @@ static void channel_accept(struct utcp_connection *utcp_connection, uint16_t por } } +static void channel_retransmit(struct utcp_connection *utcp_connection) { + node_t *n = utcp_connection->utcp->priv; + meshlink_handle_t *mesh = n->mesh; + + if(n->mtuprobes == 31) { + timeout_set(&mesh->loop, &n->mtutimeout, &(struct timespec) { + 0, 0 + }); + } +} + static ssize_t channel_send(struct utcp *utcp, const void *data, size_t len) { node_t *n = utcp->priv; @@ -3589,57 +3652,89 @@ static void channel_poll(struct utcp_connection *connection, size_t len) { node_t *n = channel->node; meshlink_handle_t *mesh = n->mesh; - meshlink_aio_buffer_t *aio = channel->aio_send; - if(aio) { - /* We at least one AIO buffer. Send as much as possible form the first buffer. */ - size_t left = aio->len - aio->done; + while(channel->aio_send) { + if(!len) { + /* This poll callback signalled an error, abort all outstanding AIO buffers. */ + if(!aio_abort(mesh, channel, &channel->aio_send)) { + return; + } + + break; + } + + /* We have at least one AIO buffer. Send as much as possible from the buffers. */ + meshlink_aio_buffer_t *aio = channel->aio_send; + size_t todo = aio->len - aio->done; ssize_t sent; - if(len > left) { - len = left; + if(todo > len) { + todo = len; } if(aio->data) { - sent = utcp_send(connection, (char *)aio->data + aio->done, len); + sent = utcp_send(connection, (char *)aio->data + aio->done, todo); } else { - char buf[65536]; - size_t todo = utcp_get_sndbuf_free(connection); + char buf[todo]; + ssize_t result = read(aio->fd, buf, todo); - if(todo > left) { - todo = left; - } + if(result > 0) { + todo = result; + sent = utcp_send(connection, buf, todo); + } else { + if(result < 0 && errno == EINTR) { + continue; + } + + /* Reading from fd failed, cancel just this AIO buffer. */ + if(result != 0) { + logger(mesh, MESHLINK_ERROR, "Reading from AIO fd %d failed: %s", aio->fd, strerror(errno)); + } + + if(!aio_finish_one(mesh, channel, &channel->aio_send)) { + return; + } - if(todo > sizeof(buf)) { - todo = sizeof(buf); + continue; } + } - ssize_t result = read(aio->fd, buf, todo); + if(sent != (ssize_t)todo) { + /* We should never get a partial send at this point */ + assert(sent < 0); - if(result > 0) { - sent = utcp_send(connection, buf, result); - } else { - sent = result; + /* Sending failed, abort all outstanding AIO buffers and send a poll callback. */ + if(!aio_abort(mesh, channel, &channel->aio_send)) { + return; } + + len = 0; + break; } - if(sent >= 0) { - aio->done += sent; + aio->done += sent; + len -= sent; + + /* If we didn't finish this buffer, exit early. */ + if(aio->done < aio->len) { + return; } - /* If the buffer is now completely sent, call the callback and dispose of it. */ - if(aio->done >= aio->len) { - channel->aio_send = aio->next; - aio_signal(mesh, channel, aio); - free(aio); + /* Signal completion of this buffer, and go to the next one. */ + if(!aio_finish_one(mesh, channel, &channel->aio_send)) { + return; } - } else { - if(channel->poll_cb) { - channel->poll_cb(mesh, channel, len); - } else { - utcp_set_poll_cb(connection, NULL); + + if(!len) { + return; } } + + if(channel->poll_cb) { + channel->poll_cb(mesh, channel, len); + } else { + utcp_set_poll_cb(connection, NULL); + } } void meshlink_set_channel_poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, meshlink_channel_poll_cb_t cb) { @@ -3668,6 +3763,7 @@ void meshlink_set_channel_accept_cb(meshlink_handle_t *mesh, meshlink_channel_ac if(!n->utcp && n != mesh->self) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t)); + utcp_set_retransmit_cb(n->utcp, channel_retransmit); } } @@ -3717,6 +3813,7 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n if(!n->utcp) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t)); + utcp_set_retransmit_cb(n->utcp, channel_retransmit); mesh->receive_cb = channel_receive; if(!n->utcp) { @@ -3777,24 +3874,20 @@ void meshlink_channel_close(meshlink_handle_t *mesh, meshlink_channel_t *channel pthread_mutex_lock(&mesh->mutex); - utcp_close(channel->c); + if(channel->c) { + utcp_close(channel->c); + channel->c = NULL; - /* Clean up any outstanding AIO buffers. */ - for(meshlink_aio_buffer_t *aio = channel->aio_send, *next; aio; aio = next) { - next = aio->next; - aio_signal(mesh, channel, aio); - free(aio); + /* Clean up any outstanding AIO buffers. */ + aio_abort(mesh, channel, &channel->aio_send); + aio_abort(mesh, channel, &channel->aio_receive); } - for(meshlink_aio_buffer_t *aio = channel->aio_receive, *next; aio; aio = next) { - next = aio->next; - aio_signal(mesh, channel, aio); - free(aio); + if(!channel->in_callback) { + free(channel); } pthread_mutex_unlock(&mesh->mutex); - - free(channel); } ssize_t meshlink_channel_send(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) { @@ -3867,7 +3960,11 @@ bool meshlink_channel_aio_send(meshlink_handle_t *mesh, meshlink_channel_t *chan /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - channel_poll(channel->c, len); + size_t todo = MIN(len, utcp_get_rcvbuf_free(channel->c)); + + if(todo) { + channel_poll(channel->c, todo); + } pthread_mutex_unlock(&mesh->mutex); @@ -3904,7 +4001,11 @@ bool meshlink_channel_aio_fd_send(meshlink_handle_t *mesh, meshlink_channel_t *c /* Ensure the poll callback is set, and call it right now to push data if possible */ utcp_set_poll_cb(channel->c, channel_poll); - channel_poll(channel->c, len); + size_t left = utcp_get_rcvbuf_free(channel->c); + + if(left) { + channel_poll(channel->c, left); + } pthread_mutex_unlock(&mesh->mutex); @@ -4026,6 +4127,7 @@ void meshlink_set_node_channel_timeout(meshlink_handle_t *mesh, meshlink_node_t if(!n->utcp) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t)); + utcp_set_retransmit_cb(n->utcp, channel_retransmit); } utcp_set_user_timeout(n->utcp, timeout); @@ -4037,6 +4139,7 @@ void update_node_status(meshlink_handle_t *mesh, node_t *n) { if(n->status.reachable && mesh->channel_accept_cb && !n->utcp) { n->utcp = utcp_init(channel_accept, channel_pre_accept, channel_send, n); utcp_set_mtu(n->utcp, n->mtu - sizeof(meshlink_packethdr_t)); + utcp_set_retransmit_cb(n->utcp, channel_retransmit); } if(mesh->node_status_cb) {