[sr-dev] [tracker] Comment added: sercmd after "reply too big"

sip-router bugtracker at sip-router.org
Sun Apr 20 11:18:26 CEST 2014


THIS IS AN AUTOMATED MESSAGE, DO NOT REPLY.

The following task has a new comment added:

FS#422 - sercmd after "reply too big" 
User who did this - Savolainen Dmitri (sdi)

----------
It doesn't work. 
read(s, reply_buf, max_reply_size) returns EAGAIN error after setting socket to non-blocking mode 
so it is possible  to use "select":

<code>
diff --git a/utils/sercmd/sercmd.c b/utils/sercmd/sercmd.c
index 7bb435a..8bd40cb 100644
--- a/utils/sercmd/sercmd.c
+++ b/utils/sercmd/sercmd.c
@@ -48,6 +48,7 @@
 #include <sys/uio.h> /* writev */
 #include <netdb.h> /* gethostbyname */
 #include <time.h> /* time */
+#include <fcntl.h>

 #ifdef USE_READLINE
 #include <readline/readline.h>
@@ -718,8 +719,38 @@ static int get_reply(int s, unsigned char* reply_buf, int max_reply_size,
                                goto error;
                        }
                        msg_end=hdr_end+in_pkt->tlen;
-                       if ((int)(msg_end-reply_buf)>max_reply_size)
-                               goto error_toolong;
+                       if ((int)(msg_end-reply_buf)>max_reply_size) {
+                           int flags = fcntl(s, F_GETFL, 0);
+                           fcntl(s, F_SETFL, flags | O_NONBLOCK);
+                           /* reading the rest from the socket */
+                           int read_continue = 1;
+                           while (read_continue){
+                               int read_ret = read(s, reply_buf, max_reply_size);
+                               if (read_ret == 0)
+                                   read_continue = 0;
+                               else if (read_ret<0){
+                                   if (errno == EAGAIN){
+                                       fd_set rfds;
+                                       struct timeval tv;
+                                       int retval;
+                                       FD_ZERO(&rfds);
+                                       FD_SET(s, &rfds);
+                                       tv.tv_sec = 1;
+                                       tv.tv_usec = 0;
+                                       retval = select(s+1, &rfds, NULL, NULL, &tv);
+                                       if (retval<=0)
+                                           read_continue = 0;
+                                       //continue;
+                                   }
+                                   else{
+                                       //some read erro; hope for best
+                                       read_continue = 0;
+                                   }
+                               }
+                           }
+                           fcntl(s, F_SETFL, flags & (~O_NONBLOCK));
+                           goto error_toolong;
+                       }
                }
        }while(crt<msg_end);

</code>

but setting blocking read timeout is better (in my mind)

<code>
diff --git a/utils/sercmd/sercmd.c b/utils/sercmd/sercmd.c
index 7bb435a..486ef5d 100644
--- a/utils/sercmd/sercmd.c
+++ b/utils/sercmd/sercmd.c
@@ -718,8 +718,21 @@ static int get_reply(int s, unsigned char* reply_buf, int max_reply_size,
                                goto error;
                        }
                        msg_end=hdr_end+in_pkt->tlen;
-                       if ((int)(msg_end-reply_buf)>max_reply_size)
-                               goto error_toolong;
+                       if ((int)(msg_end-reply_buf)>max_reply_size){
+                           /* reading the rest from the socket */
+                           struct timeval timeout_save;
+                           unsigned sizeoft =  sizeof(timeout_save);
+                           if ( getsockopt (s, SOL_SOCKET, SO_RCVTIMEO, &timeout_save, &sizeoft)==0 ){
+                               struct timeval timeout;
+                               timeout.tv_sec = 1;
+                               timeout.tv_usec = 0;
+                               if (setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(timeout))==0){
+                                   while(read(s, reply_buf, max_reply_size)>0);
+                                   setsockopt (s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout_save,sizeof(timeout_save));
+                               }
+                           }
+                           goto error_toolong;
+                       }
                }
        }while(crt<msg_end);

</code>

The first and second works in my tests



----------

More information can be found at the following URL:
http://sip-router.org/tracker/index.php?do=details&task_id=422#comment1403

You are receiving this message because you have requested it from the Flyspray bugtracking system.  If you did not expect this message or don't want to receive mails in future, you can change your notification settings at the URL shown above.



More information about the sr-dev mailing list