c++ - Program exits on SSL_connect -
i'm having problem program i'm working on shut down after call ssl_connect(). can't error code ever, since calling ssl_get_error() afterwards ignored since exits program. if normal http request connect() works fine. have ideas on this?
i'm running on raspberry pi 3 raspbian. i'm new ssl, pointers appreciated.
edit: here's code snippet of i'm trying do
#include <openssl/ssl.h> #include <iostream> #include <resolv.h> #include <sys/socket.h> #include <sys/types.h> #include <netdb.h> #include <arpa/inet.h> #include <string> #include <unistd.h> int main() { int sockfd; struct addrinfo hints, *servinfo, *p; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = af_unspec; hints.ai_socktype = sock_stream; if ((rv = getaddrinfo("www.example.com", "https", &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(rv)); exit(1); } ssl_load_error_strings(); ssl_library_init(); ssl_ctx = ssl_ctx_new(sslv23_client_method()); ssl * connection = ssl_new(int sockfd; struct addrinfo hints, *servinfo, *p; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = af_unspec; hints.ai_socktype = sock_stream; if ((rv = getaddrinfo("www.example.com", "https", &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(rv)); exit(1); } ssl_load_error_strings(); ssl_library_init(); ssl_ctx = ssl_ctx_new(sslv23_client_method()); ssl * connection = ssl_new(ssl_ctx); (p = servinfo; p != null; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("socket"); continue; } int fd = ssl_set_fd(connection, sockfd); break; } if (p == null) { // looped off end of list no connection fprintf(stderr, "failed connect\n"); exit(2); } int connret = 0; while(connret != 1 ) { connret = ssl_connect(connection); cout << "error : " << ssl_get_error(connection, connret) << endl; } freeaddrinfo(servinfo); return 0; } edit 2 : managed run through debugger, i'm getting :
(program exited code: 141) press return continue
which according question : socket connection getting closed abruptly code 141 sigpipe signal.
unsure why caused crash, ssl_connect not equivalent of connect: initiates tls/ssl handshake on already established connection. sockfd not connected in code, fails , sets connret -1 causing endless loop.
you should connect socket after creation , before initiating ssl handshake:
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("socket"); continue; } /* connect @ tcp level */ if (connect(sockfd, servinfo->ai_addr, servinfo->ai_addrlen)) { perror("connect"); continue; } int fd = ssl_set_fd(connection, sockfd); ... but anyway, should control program remove possible endless loops, , consistent in error messages: either use c++ stream cout or c file* stderr. , better close socket if opened.
Comments
Post a Comment