summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2013-09-17 16:46:03 -0500
committerNathan Straz <nstraz@redhat.com>2013-09-17 18:04:44 -0400
commit0871b7a61e68c532f3b4386c9ae584b24304f649 (patch)
tree3937b33c62ada01576c514bcaf6d221344a0e72b
parent42a431da0abe614a7efa03164430918e55f35609 (diff)
downloadqarsh-0871b7a61e68c532f3b4386c9ae584b24304f649.tar.gz
qarsh-0871b7a61e68c532f3b4386c9ae584b24304f649.tar.xz
qarsh-0871b7a61e68c532f3b4386c9ae584b24304f649.zip
Add a loop around getting the packet size
Believe it or not, we can get short reads here.
-rw-r--r--sockutil.c21
1 files changed, 11 insertions, 10 deletions
diff --git a/sockutil.c b/sockutil.c
index 73ab304..05aa0bc 100644
--- a/sockutil.c
+++ b/sockutil.c
@@ -106,26 +106,27 @@ recv_packet(int fd)
{
char buf[QARSH_MAX_PACKET_SIZE];
uint32_t packetsize;
+ char *psbuf = &packetsize;
int bufused = 0;
int ret = 0;
- if ((ret = read(fd, &packetsize, sizeof packetsize)) < 0) {
- if (errno != ECONNRESET) {
- fprintf(stderr, "Read error while reading packet size: %s\n", strerror(errno));
+ do {
+ if ((ret = read(fd, psbuf+bufused, sizeof packetsize - bufused)) < 0) {
+ fprintf(stderr, "Read error while reading packet size: %s\n", strerror(errno));
+ return NULL;
+ } else if (ret == 0) {
+ return NULL;
}
- return NULL;
- } else if (ret == 0) {
- return NULL;
- } else if (ret != sizeof packetsize) {
- fprintf(stderr, "Did not read packet size");
- return NULL;
- }
+ bufused += ret;
+ } while (bufused < sizeof packetsize);
+
packetsize = ntohl(packetsize);
if (packetsize > QARSH_MAX_PACKET_SIZE) {
fprintf(stderr, "Packet size too large, %d > %d\n", packetsize, QARSH_MAX_PACKET_SIZE);
return NULL;
}
/* Keep reading until we get the whole packet and nothing but the packet, so help me socket */
+ bufused = 0;
do {
if ((ret = read(fd, buf+bufused, packetsize-bufused)) < 0) {
fprintf(stderr, "Read error while reading packet data: %s\n", strerror(errno));