I'm trying to put together a shared library that does a HTTP GET request to a remote server. My library will be loaded by another app and I want to post some statistical data over to a webserver API. My code currently looks like this:
void connect_send(char *value)
{
char *toSend = (char *)malloc((strlen(value) + 2048) * sizeof(char));
char *host = "www.example.com";
struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd;
sprintf(toSend, "GET api.php?key=1234&toggle=cpu&value=%s HTTP/1.0\r\nUser-Agent: API Client\r\n\r\n", value);
SSL_load_error_strings ();
SSL_library_init ();
SSL_CTX *ssl_ctx = SSL_CTX_new (SSLv23_client_method ());
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server = gethostbyname(host);
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(443);
memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
{
fflush(stdout);
printf("connect failed");
}
SSL *conn = SSL_new(ssl_ctx);
SSL_set_fd(conn, sockfd);
SSL_connect(conn);
SSL_write(conn,toSend,strlen(toSend));
SSL_shutdown(conn);
close(sockfd);
return;
}
When I compile this into a standalone binary it works fine, but when I compile this via:
gcc -fPIC -shared -m32 -ggdb -o api.so api.c -lc /usr/lib/i386-linux-gnu/libssl.a -ldl -lssl /usr/lib/i386-linux-gnu/libcrypto.a -lcrypto
It crashes with errors such as:
0xa6ac36a7 in i2c_ASN1_INTEGER () from ./api.so
Since the code works when compiled to a standalone binary I guess I'm doing something when compiling it as a library. Any ideas?
Disclaimer: this is my first time trying to use libssl/libcrypto, but I have done a lot of searching for the answer.