summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util.c38
-rw-r--r--util.h1
2 files changed, 17 insertions, 22 deletions
diff --git a/util.c b/util.c
index eaee2d5..d672c9e 100644
--- a/util.c
+++ b/util.c
@@ -19,6 +19,7 @@ conn *dial(char *host, char *port, int ssl)
static struct addrinfo hints;
int srv;
struct addrinfo *res, *r;
+ SSL_CTX *sslcontext;
conn *c;
c = malloc(sizeof(conn));
c->fd = -1;
@@ -52,17 +53,15 @@ conn *dial(char *host, char *port, int ssl)
}
c->fd = srv;
- c->sslcontext = NULL;
c->sslhandle = NULL;
if(ssl) {
SSL_load_error_strings();
SSL_library_init();
- if((c->sslcontext = SSL_CTX_new(SSLv23_client_method())) == NULL) {
- /*if((c->sslcontext = SSL_CTX_new(TLS_client_method())) == NULL) {*/
+ if((sslcontext = SSL_CTX_new(TLSv1_2_client_method())) == NULL) {
ERR_print_errors_fp(stderr);
}
- if((c->sslhandle = SSL_new(c->sslcontext)) == NULL) {
+ if((c->sslhandle = SSL_new(sslcontext)) == NULL) {
ERR_print_errors_fp(stderr);
}
if(SSL_set_fd(c->sslhandle, c->fd) != 1) {
@@ -99,28 +98,18 @@ int request(char *host, int ssl, char *request, char *savecookie, char **body, i
return 0;
}
- if(ssl) {
- if(!SSL_write(c->sslhandle, request, strlen(request))) {
- ERR_print_errors_fp(stderr);
- return 0;
- }
- } else {
- if(!write(c->fd, request, strlen(request))) {
- return 0;
- }
+ if(ssl ? !SSL_write(c->sslhandle, request, strlen(request)) :
+ !write(c->fd, request, strlen(request))) {
+ return 0;
}
/* download everything into buf */
l = 0;
buf = malloc(sizeof(char *) * BUFSIZ);
- /* TODO: rewrite this so it's clear that only the read call differs, e.g. with
- * macros as ii does it, or maybe as a function pointer or something */
- if(ssl) {
- for(; buf != NULL && (res = SSL_read(c->sslhandle, buf+l, BUFSIZ)) > 0; l+=res)
- buf = realloc(buf, sizeof(char *) * (l+BUFSIZ));
- } else {
- for(; buf != NULL && (res = read(c->fd, buf+l, BUFSIZ)) > 0; l+=res)
- buf = realloc(buf, sizeof(char *) * (l+BUFSIZ));
+ for(; buf != NULL &&
+ (res = ssl ? SSL_read(c->sslhandle, buf+l, BUFSIZ) : read(c->fd, buf+l, BUFSIZ)) > 0;
+ l+=res) {
+ buf = realloc(buf, sizeof(char *) * (l+BUFSIZ));
}
/* strstr to find end of header */
@@ -168,6 +157,13 @@ int request(char *host, int ssl, char *request, char *savecookie, char **body, i
free(buf);
+ if(ssl) {
+ SSL_shutdown(c->sslhandle);
+ SSL_free(c->sslhandle);
+ }
+ close(c->fd);
+ free(c);
+
return l;
}
diff --git a/util.h b/util.h
index a48a12e..7e62ded 100644
--- a/util.h
+++ b/util.h
@@ -5,7 +5,6 @@
typedef struct {
int fd;
SSL *sslhandle;
- SSL_CTX *sslcontext;
} conn;
conn *dial(char *host, char *port, int ssl);
int get(char *host, int ssl, char *path, char *sendcookie, char *savecookie, char **body, int istext);