Browse code

sercmd: refactored reading the rest from socket on reply too long

- patch by by Savolainen Dmitri, FS#422

Daniel-Constantin Mierla authored on 22/04/2014 18:51:31
Showing 1 changed files
... ...
@@ -720,11 +720,21 @@ static int get_reply(int s, unsigned char* reply_buf, int max_reply_size,
720 720
 			}
721 721
 			msg_end=hdr_end+in_pkt->tlen;
722 722
 			if ((int)(msg_end-reply_buf)>max_reply_size) {
723
-				int flags = fcntl(s, F_GETFL, 0);
724
-				fcntl(s, F_SETFL, flags | O_NONBLOCK);
725 723
 				/* reading the rest from the socket */
726
-				while(read(s, reply_buf, max_reply_size)>0);
727
-				fcntl(s, F_SETFL, flags & (~O_NONBLOCK));
724
+				struct timeval timeout_save;
725
+				unsigned sizeoft = sizeof(timeout_save);
726
+				if (getsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
727
+							&timeout_save, &sizeoft)==0) {
728
+					struct timeval timeout;
729
+					timeout.tv_sec = 1;
730
+					timeout.tv_usec = 0;
731
+					if (setsockopt (s, SOL_SOCKET, SO_RCVTIMEO,
732
+								(char*)&timeout,sizeof(timeout))==0) {
733
+						while(read(s, reply_buf, max_reply_size)>0);
734
+						setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
735
+								(char*)&timeout_save,sizeof(timeout_save));
736
+					}
737
+				}
728 738
 				goto error_toolong;
729 739
 			}
730 740
 		}