Question

I am using the following code as a guide and modified the code below. http://bendecplusplus.googlecode.com/svn/trunk/ssl_mycode/epoll_ssl/server.c http://bendecplusplus.googlecode.com/svn/trunk/ssl_mycode/epoll_ssl/client.c

I have modified the server side code as follows:

do {
count = SSL_read (ssl,  buf, sizeof(buf)); // get request
switch (SSL_get_error (ssl, count) ) {
    case SSL_ERROR_NONE: 
            buf[count] = 0;
            printf("Client msg: \"%s\"\n", buf);
            sprintf(reply, HTMLecho, buf);   // construct reply
            SSL_write(ssl, reply, strlen(reply)); // send reply
            break;
    case SSL_ERROR_WANT_READ:
    case SSL_ERROR_WANT_WRITE:
            continue;
    case SSL_ERROR_ZERO_RETURN:         
            ERR_print_errors_fp(stderr);
            printf("Performing exchange Error 2.\n");
            done = 1;
            break;
    default:
            ERR_print_errors_fp(stderr);
            printf("Performing exchange Error 3.\n");
            done = 1;
            break;
  }
} while ( ssl && count > 0 );  // SSL_pending(ssl) seems unreliable

On the client side I have the code as follows:

SSL_library_init();
ctx = InitCTX();
LoadCertificates(ctx, CertFile, KeyFile);
server = OpenConnection(hostname, atoi(portnum));
ssl = SSL_new(ctx); /* create new SSL connection state */
SSL_set_fd(ssl, server); /* attach the socket descriptor */
    if ( SSL_connect(ssl) == FAIL ) /* perform the connection */
    {
        ERR_print_errors_fp(stderr);
    } else {
    while(1){
        char *msg = "Hello??? are you there. lolololololololoooooooooooooooooooooooooooo";
        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
        ShowCerts(ssl); /* get any certs */
        SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
        bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
        buf[bytes] = 0;
        printf("Received: \"%s\"\n", buf);
        sleep(1);
        }
        SSL_free(ssl); /* release connection state */
    }
close(server); /* close socket */
SSL_CTX_free(ctx); /* release context */

I am observing getting stuck in the loop on server side. Any guidance is appreciated.

Was it helpful?

Solution 2

The following worked code for me.

                count = SSL_read(ssl, buf, sizeof(buf)); // get request
                int32_t ssl_error = SSL_get_error (ssl, count);
                switch (ssl_error) {
                case SSL_ERROR_NONE: 
                            printf("SSL_ERROR_NONE\n");
                            break;
                case SSL_ERROR_WANT_READ:
                            printf("SSL_ERROR_WANT_READ\n");
                            break;
                case SSL_ERROR_WANT_WRITE:
                            printf("SSL_ERROR_WANT_WRITE\n");
                            break;
                case SSL_ERROR_ZERO_RETURN:
                            printf("SSL_ERROR_ZERO_RETURN\n");  
                            break;
                default:
                            break;
                 }

                if (( count > 0 ) && (ssl_error == SSL_ERROR_NONE))
                {
                    buf[count] = 0;
                    printf("count > 0 Client msg: \"%s\"\n", buf);
                    sprintf(reply, HTMLecho, buf);   // construct reply
                    SSL_write(ssl, reply, strlen(reply)); // send reply
                } else if ((count < 0)  && (ssl_error == SSL_ERROR_WANT_READ)){
                    printf("count < 0 \n");
                    if (errno != EAGAIN)
                    {
                        printf("count < 0 errno != EAGAIN \n");
                        perror ("read");
                        done = 1;
                    }
                    break;
                } else if (count==0){
                    ERR_print_errors_fp(stderr);
                    printf("count == 0 Client Disconnected.\n");
                    done = 1;
                    break;
                }

OTHER TIPS

Your loop is obvious. If there is nothing to read, SSL_read returns zero and error code is SSL_ERROR_WANT_READ, then you execute continue which goes back to ssl_read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top