I have a programm that use ssl_write , but when i try to do send 2 ssl_write one after the other , the first one works the second one not and return me segmentation fault , how i can sent multiple ssl_write to the same server one after the other ?
int sock;
struct sockaddr_in sockaddr;
struct hostent *host; /* Host information. */
sock = socket(AF_INET, /* IPV4 protocol. */
SOCK_STREAM, /* TCP socket. */
0); /* O for socket() function choose the correct protocol based on the socket type. */
printf("\n print\n");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return 0;
}
if((host = gethostbyname("192.168.0.100")) == NULL) {
close(sock);
printf("\n errore hostname\n");
return -1;
}
printf("\n print1\n");
/* zero buffer */
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
memcpy(&sockaddr.sin_addr,
host -> h_addr,
host -> h_length );
sockaddr.sin_port = htons(443);
// first connect to the remote as usual, but use the port 443 instead of 80
if(connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))==-1){
printf("\n ERRORE connect to socket \n");
}
// initialize OpenSSL - do this once and stash ssl_ctx in a global var
SSL_load_error_strings();
SSL_library_init ();
SSL_CTX *ssl_ctx = SSL_CTX_new (SSLv23_client_method ());
// create an SSL connection and attach it to the socket
SSL *conn = SSL_new(ssl_ctx);
SSL_set_fd(conn, sock);
printf("\n print2\n");
// perform the SSL/TLS handshake with the server - when on the
// server side, this would use SSL_accept()
int err = SSL_connect(conn);
printf("\n print3\n");
if (err != 1) {
printf("\n errore connessione socketssl\n");
}
char sendmessage[395];
sprintf(sendmessage," hello:%s","hello");
SSL_write(conn, sendmessage, strlen(sendmessage) );
sprintf(sendmessage,"message from:%s n:%d","alex",5);
printf("ssl write ret %d\n",SSL_write(conn, sendmessage, strlen(sendmessage) ));