Hello! I use openssl to work with apache server via https. But I see a strange situation when second and third calls to send() in my test-case read 0 bytes from socket. Can you provide here any help?
I use 'Fedora Core 7 x86' and openssl-0.9.8e. Thanks! -Dima
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
using namespace std;
#define MAX_PACKET_SIZE 10000
BIO * bio, *ssl_bio, *sbio;
SSL * ssl;
SSL_CTX * ctx;
void send(){
char *write_buf = "GET / HTTP/1.0\n\n";
if(SSL_write(ssl, write_buf, strlen(write_buf)) <= 0){
std::cout << "Failed write" << std::endl;
}
char buf[MAX_PACKET_SIZE];
std::cout << "=====================" << std::endl;
int p;
char r[1024];
for(;;)
{
p = SSL_read(ssl, r, 1023);
printf("read %d bytes\n", p );
if(p <= 0) break;
r[p] = 0;
printf("%s", r);
}
std::cout << std::endl << "=====================" << std::endl;
/* To free it from memory, use this line */
int ssl_er = SSL_get_error(ssl, 0);
printf("ssl_er=%d\n", ssl_er);
}
int main() {
/* Initializing OpenSSL */
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
SSL_library_init(); //mandatory and missing from some examples
ctx = SSL_CTX_new(SSLv23_client_method());
if (ctx == NULL) {
std::cout << "Ctx is null" << std::endl;
ERR_print_errors_fp(stderr);
}
/*create ssl*/
ssl = SSL_new(ctx);
/*create bio*/
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in addr;
memset (&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(443); /* Server Port number */
addr.sin_addr.s_addr = inet_addr("192.168.40.27"); /* Server IP */
int err = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
sbio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(ssl, sbio, sbio);
err = SSL_connect(ssl);
send();
send();
send();
BIO_free_all(sbio);
ERR_print_errors_fp(stderr);
return 0;
}
signature.asc
Description: This is a digitally signed message part
