Browse code

sercmd: read the rest from socket if message was too big for rcv buffer

- reported by Savolainen Dmitri, FS#422

Daniel-Constantin Mierla authored on 18/04/2014 15:22:43
Showing 1 changed files
... ...
@@ -47,6 +47,7 @@
47 47
 #include <netinet/in.h> /* udp sock */
48 48
 #include <sys/uio.h> /* writev */
49 49
 #include <netdb.h> /* gethostbyname */
50
+#include <fcntl.h>
50 51
 #include <time.h> /* time */
51 52
 
52 53
 #ifdef USE_READLINE
... ...
@@ -718,8 +719,14 @@ static int get_reply(int s, unsigned char* reply_buf, int max_reply_size,
718 718
 				goto error;
719 719
 			}
720 720
 			msg_end=hdr_end+in_pkt->tlen;
721
-			if ((int)(msg_end-reply_buf)>max_reply_size)
721
+			if ((int)(msg_end-reply_buf)>max_reply_size) {
722
+				int flags = fcntl(s, F_GETFL, 0);
723
+				fcntl(s, F_SETFL, flags | O_NONBLOCK);
724
+				/* reading the rest from the socket */
725
+				while(read(s, reply_buf, max_reply_size)>0);
726
+				fcntl(s, F_SETFL, flags & (~O_NONBLOCK));
722 727
 				goto error_toolong;
728
+			}
723 729
 		}
724 730
 	}while(crt<msg_end);
725 731