repetitive ssl send

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I've a small C code and I can send and receive from google.
Here is the code

---------
char HEADERS[] = "GET /search?q=arduino HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
char HOST_NAME_PORT[] = "www.google.com:443";
const char* PREFERRED_CIPHERS = "HIGH:!aNULL:!kRSA:!SRP:!PSK:!CAMELLIA:!RC4:!MD5:!DSS";

void init_openssl()
{
    SSL_load_error_strings();
    SSL_library_init();
}

void CreateSSL()
{
 // Create SSL context
 meth = SSLv23_client_method();
 if (!meth) throw Exception("SSL: method");

 ctx = SSL_CTX_new(meth);
 if (!ctx) throw Exception("SSL: SSL_CTX_new");

 old_opts = SSL_CTX_set_options(ctx, SSL_OP_ALL);

 web = BIO_new_ssl_connect(ctx);
 if (!web) throw Exception("SSL: BIO_new_ssl_connect");
}

void ConnectSSL()
{
 // Connect
 res = BIO_set_conn_hostname(web, HOST_NAME_PORT);
 if (!res) throw Exception("SSL: BIO_ctrl");

 res = BIO_get_ssl(web, &ssl);
 if (!res) throw Exception("SSL: BIO_ctrl");

 res = SSL_set_cipher_list(ssl, PREFERRED_CIPHERS);
 if (!res) throw Exception("SSL: SSL_set_cipher_list");

 res = BIO_do_connect(web);
 if (res <= 0) throw Exception("SSL: BIO_do_connect");

 res = BIO_do_handshake(web);
 if (res <= 0) throw Exception("SSL: BIO_do_handshake");
}

void SendSSL()
{
 // Send
 err = BIO_puts(web, HEADERS);
 if (err <= 0) throw Exception("SSL: BIO_puts");
}

void ReceiveSSL()
{
 // Read
 sResult = "";
 for (;;) {
  len = BIO_read(web, buf, sizeof(buf));
  sResult += buf;
  if (len <= 0)
   break;
 }
}

void TestProc()
{
 init_openssl();

 CreateSSL();
 ConnectSSL();
 SendSSL();
 ReceiveSSL();

 // Next request
 //

 lTmp = new TStringList;
 lTmp->Add(sResult);
 lTmp->SaveToFile("c:\\temp\\a1.htm");

 // Free
 BIO_free_all(web);
 EVP_cleanup();

}

---------
It is ok for one request.

My problem when I trying to send a new search request to google it works only when I call 
 CreateSSL();
 ConnectSSL();
again

so my new search request is 
 CreateSSL();
 ConnectSSL();
 SendSSL();
 ReceiveSSL();

Is this normal?

When I trying to use
 SendSSL();
 ReceiveSSL();
only
I have got a page with errors (I think). The response is shorter, starts with 0 (not with HTTP/1.1 200 OK)

What may the problem?


Thanks for your help



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux