[gnutls-help] Corrupted data when reading from gnutls input buffer

mw at atomicode.de mw at atomicode.de
Thu Apr 18 19:49:57 CEST 2013


I am developing a software using gnutls 3.1.0 and got a little problem,
which I hope somebody can help me get rid of.

On client side I am using a non-blocking socket and to check it for 
read readiness
I call "gnutls_record_check_pending" to see if there is data in the 
input buffer
remaining from the last read process. If not I call "select" on the 
socket to
see if data is available to be read by "gnutls_record_recv".

Every time when data is read from the internal input buffer of gnutls
I get corrupted data.

It seems to be sporadically that data is read into the input buffer of 
and varies in size.

Here some code:

bool Client::readable(uint32_t ms)
     // check for data to be read from the gnutls input buffer

     uint32_t r = gnutls_record_check_pending(m_session);

     if (r > 0) {

         return true;

     // check for socket read readiness

     fd_set fds;
     FD_SET(m_socket, &fds);

     struct timeval tv;
     tv.tv_sec = 0;
     tv.tv_usec = ms * 1000;

     int res = select(m_socket+1, &fds, NULL, NULL, &tv);

     if (res <= 0) {

         return false;

     return true;

uint32_t Client::recv(uint8_t* data, uint32_t size)
     uint32_t s = 0;

     while (s < size) {

     	// wait for read readiness

     	if (!readable(200)) {


     	// read data

         int32_t ret = gnutls_record_recv(m_session, &data[s], size - 

         if (gnutls_error_is_fatal(ret) != 0 || ret == 0) {

             if (ret == 0) {

                 return 0;
             else {

                 std::cerr << "Error: " << gnutls_strerror(ret) << "\n";

                 return ret;

         if (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) {


         s += ret;

     return s;

Hope somebody can give me a hint what I may be doing wrong.

Thanks in advance.


More information about the Gnutls-help mailing list