Search code examples
copenssltimeoutclient

Openssl BIO_read does not return


I have a problem with BIO_read I do not get rid of. I want to print the lines to the screen that came from the server. With the help of the gdb I could find out where the problem is but I don't what to do now. The problem is that the function is called correctly but then when BIO_read is executed it feels like an endless loop as it does not return or end. My code looks like this:

//This function should print one line to screen
int WebPrintLine(BIO *bio) {
unsigned char *x;
unsigned int i;
unsigned char buffer[4096];
unsigned int sizeofbuffer = sizeof(buffer);

//Print each char until newline char or terminator char appear and next loop turn if it would affect memory not owned by the arry buffer
do {
    WebRead(bio, buffer, sizeofbuffer);

    for( i = 0; (buffer[i] != '\n') && (buffer[i] != '\0') && (i < sizeofbuffer); i++ )
        putchar(buffer[i]);

}while( (buffer[i] != '\n') && (buffer[i] != '\0') && (x != 0) );

putchar('\n');

return 0;
}


/*This function should receive and returns a char to the buffer
BIO_gets could also be used to get just one line but it does not work always so I have  not put it in
Return 0 on error*/
unsigned char *WebRead(BIO *bio, unsigned char buffer[], int sizeofbuffer) {
int bytes_read; //Temporarily store how many bytes were read: for error checking

bytes_read = BIO_read(bio, buffer, sizeofbuffer);
if( bytes_read == 0 ) {
    //No more data available on an non-blocking connection
    return 0;
}
else if( bytes_read < 0 ) {
    //Error occured, retry and if this fails return  0
    if( ! BIO_should_retry(bio) )
        return 0;
}
return buffer;
}

I am looking forward to hear from you.

The function WebPrintLine is called two more times before and works correctly. But in gdb the program stucks when BIO_read is called.

Does anyone has some experiences with that, I couldn't find anything relevant and on their site: openssl no such error is mentioned or I have notm found it yet.


Solution

  • Ok,

    I have found my error. I just write this if someone else has the same problems like me:

    BIO_write(bio, buf_write, strlenbuf)
    

    The problem was not BIO_write but the value of strlenbuf. I had added two chars to the buf_write array but not updated the variable strlenbuf with stored the length of the string( strlenbuf = strlen(buf_write) ), so the string terminato ('\0') was not sended and so I had not finished my server request, and this caused this problem.