gnutls_record_recv() hangs

Dinh Le dinh at flic.net
Mon Aug 15 22:15:52 CEST 2011


Hi,

Please straighten out my understanding of the gnutls_record_recv()
related functions.  Listed below is a portion of my code for connecting
to an imap server, login, and list all the mail boxes.

The "Connect" and "Login" responses from the server are less than 1024
bytes while the 'list "" "*"' response is almost 4000 bytes in my case.
Since I don't know how to check if there's data available to be received,
I wrote a loop that continuously calls gnutls_record_recv() if the
previous message received is 1024 bytes in length.  This is a bad hack,
but I just wanted to see the code works before reading the manual more
carefully.

But this hack does not work since the previously received message may
be less than 1024 bytes long but there are still more data available
for receiving.  Worse yet, gnutls_record_recv() would hang indefinitely
if there's no data available to be received.

I am using gnutls version 2.6 on Ubuntu 10.04 (with the latest official
updates).

Please help.

Thank you,

Dinh

------------------------------------------------------------------------

void
send_request(char *msg)
{
   gnutls_record_send(session, msg, strlen(msg));
}

int
recv_response()
{
   int ret, ii;

   do {
     ret = gnutls_record_recv (session, buffer, MAX_BUF);
     if (ret == 0)
       {
         success ("client: Peer has closed the TLS connection\n");
         close_and_cleanup();
         return 0;
       }
     else if (ret < 0)
       {
         fail ("client: Error: %s\n", gnutls_strerror (ret));
         close_and_cleanup();
         return -1;
       }

     // printf ("- Received %d bytes: ", ret);
     for (ii = 0; ii < ret; ii++)
       {
         fputc (buffer[ii], stdout);
       }
   } while (ret == 1024);

   return 1;
}

void
doit (void)
{
   connect_to_imap_server();
   recv_response();			// receive connected response
   send_request(". login dinh xxx\r\n");	// request log in as dinh
   recv_response();			// receive login OK
   send_request(". list \"\" \"*\"\r\n");// request list "" "*"
   recv_response();			// receive long list of mail boxes
   recv_response();
   send_request(". select inbox\r\n");
   recv_response();
   close_and_cleanup();
}




More information about the Gnutls-help mailing list