Hello All,

I had written a simple client earlier which did a blocking read and
was able to retrieve the document from the server. Now, I am trying to
rewrite the code to do non-blocking read. But only part of the data is
showing. Can someone please show what's wrong? I am eliminating
variable declarations, error checks and includes to keep this short.

TIA

int main(int argc, char **argv) {
        char request[] =  "GET / HTTP/1.0\x0D\x0AHost: 
www.google.com\x0D\x0A\x0D\x0A";
        char response[4096];
        char host[] = "www.google.com";

        /* Set up the library */
        SSL_library_init();
        ERR_load_BIO_strings();
        SSL_load_error_strings();
        OpenSSL_add_all_algorithms();

        /* Set up the SSL context */
        ctx = SSL_CTX_new(SSLv23_client_method());

        /* Load the trust store */
        SSL_CTX_load_verify_locations(ctx, "mycert.pem", NULL);

        /* Connect the TCP socket*/
        hp = gethostbyname(host);       
        memset(&addr,0,sizeof(addr));
        addr.sin_addr   = *(struct in_addr*)hp->h_addr_list[0];
        addr.sin_family = AF_INET;
        addr.sin_port   = htons(port);

        sock=socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);                  
        connect(sock,(struct sockaddr *)&addr, sizeof(addr));

        /* Connect the SSL socket */
        ssl = SSL_new(ctx);
        sbio = BIO_new_socket(sock,BIO_NOCLOSE);
        SSL_set_bio(ssl,sbio,sbio);

        if(SSL_connect(ssl) <= 0) {
                perror(NULL);
                return 0;
        }

        /* Now make our HTTP request */
        res = SSL_write(ssl,request,strlen(request));

        switch(SSL_get_error(ssl,res)){
          case SSL_ERROR_NONE:
                write_complete = 1;
                break;
          case SSL_ERROR_WANT_WRITE:
          case SSL_ERROR_WANT_READ:
                /* Bit confused by what to do. */               
          default:      
                break;
        }
        if (!write_complete)
                goto cleanup;
                
        /* Add socket descriptor to epoll. */
        s_epfd = epoll_create(EPOLL_INIT_SIZE);

        ev.events = EPOLLIN;
        ev.data.fd = sock;
        res = epoll_ctl(s_epfd, EPOLL_CTL_ADD, sock, &ev);

        num_fd = epoll_wait(s_epfd, events, MAXEVENTS, -1);
        if (num_fd > 0) {
                for (i=0; i<num_fd; i++) {
                        if ((events[i].events & EPOLLIN) && (events[i].data.fd 
== sock)) {
                                /* HTTPS server has responded. */
                                do {
                                        read_blocked_on_write = 0;
                                        read_blocked = 0;

                                        memset(response, 0, 4096);
                                        res = SSL_read(ssl, response, 4096);
                                        read_blocked = 0;
                                        switch(SSL_get_error(ssl,res)) {
                                                case SSL_ERROR_NONE:
                                                        printf("%s\n", 
response);
                                                        break;
                                                case SSL_ERROR_ZERO_RETURN:
                                                        /* End of data */
                                                        SSL_shutdown(ssl);
                                                        goto cleanup;
                                                        break;
                                                case SSL_ERROR_WANT_READ:
                                                        read_blocked=1;
                                                        break;
                                                case SSL_ERROR_WANT_WRITE:
                                                        goto cleanup;
                                                        break;
                                                default:
                                                        goto cleanup;
                                        }
                                } while (SSL_pending(ssl) && !read_blocked);
                        }
                }
        }


cleanup:
    SSL_free(ssl);
    SSL_CTX_free(ctx);
    close(sock);

    exit(0);
  }
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to