fixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-remsim/+/44182?usp=email ) Change subject: bankd: fix handling of short reads in blocking_ipa_read() ...................................................................... bankd: fix handling of short reads in blocking_ipa_read() blocking_ipa_read() calls recv() once for the IPA header and once for the payload, and treats any short read as an error. However, TCP is a stream protocol and gives no guarantee that a message is delivered by a single recv() call: both the header and the payload may arrive split into several chunks. In this case the client connection would be torn down for no good reason. Add blocking_recv_exact(), which keeps calling recv() until the requested amount of bytes has been received, the peer has closed the connection, or an error occurs. Change-Id: Ibba2002a1cb9b3d84d294151de13056c5d1cd136 --- M src/bankd/bankd_main.c 1 file changed, 31 insertions(+), 17 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-remsim refs/changes/82/44182/1 diff --git a/src/bankd/bankd_main.c b/src/bankd/bankd_main.c index fdd2865..d04b468 100644 --- a/src/bankd/bankd_main.c +++ b/src/bankd/bankd_main.c @@ -639,6 +639,33 @@ } +/* blocking recv of exactly 'len' bytes from the client socket. Returns 'len' on + * success, a smaller non-negative value if the peer closed the connection, -23 if + * the worker got unmapped meanwhile, or a negative value on error. */ +static int blocking_recv_exact(struct bankd_worker *worker, uint8_t *buf, unsigned int len) +{ + unsigned int received = 0; + int rc; + + /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR + * in case of a signal being received */ + + while (received < len) { + rc = recv(worker->client.fd, buf + received, len - received, 0); + if (rc == -1 && errno == EINTR) { + if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) + return -23; + continue; + } else if (rc < 0) + return rc; + else if (rc == 0) + break; /* connection closed by peer */ + received += rc; + } + + return received; +} + static int blocking_ipa_read(struct bankd_worker *worker, uint8_t *buf, unsigned int buf_size) { struct ipaccess_head *hh; @@ -650,17 +677,9 @@ hh = (struct ipaccess_head *) buf; - /* we use 'recv' and not 'read' below, as 'recv' will always fail with -EINTR - * in case of a signal being received */ - -restart_hdr: /* 1) blocking recv from the socket (IPA header) */ - rc = recv(worker->client.fd, buf, sizeof(*hh), 0); - if (rc == -1 && errno == EINTR) { - if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) - return -23; - goto restart_hdr; - } else if (rc < 0) + rc = blocking_recv_exact(worker, buf, sizeof(*hh)); + if (rc < 0) return rc; else if (rc < sizeof(*hh)) return -2; @@ -673,14 +692,9 @@ } needed = len; //- sizeof(*hh); -restart_body: /* 2) blocking recv from the socket (payload) */ - rc = recv(worker->client.fd, buf+sizeof(*hh), needed, 0); - if (rc == -1 && errno == EINTR) { - if (worker->state == BW_ST_CONN_CLIENT_UNMAPPED) - return -23; - goto restart_body; - } else if (rc < 0) + rc = blocking_recv_exact(worker, buf + sizeof(*hh), needed); + if (rc < 0) return rc; else if (rc < needed) return -3; -- To view, visit https://gerrit.osmocom.org/c/osmo-remsim/+/44182?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings?usp=email Gerrit-MessageType: newchange Gerrit-Project: osmo-remsim Gerrit-Branch: master Gerrit-Change-Id: Ibba2002a1cb9b3d84d294151de13056c5d1cd136 Gerrit-Change-Number: 44182 Gerrit-PatchSet: 1 Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de>