From 922cee7d8bdd3d1e9c116cec604a81c2b1cca959 Mon Sep 17 00:00:00 2001 From: un1c0rn Date: Thu, 19 Jan 2012 12:15:04 +0100 Subject: [PATCH] fix ssl --- main.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/main.c b/main.c index 01715b0..c61cef1 100644 --- a/main.c +++ b/main.c @@ -143,13 +143,8 @@ static void bio_seterr(struct bio *b, ssize_t err) b->err = errno; } -static int bio_fill(struct bio *b) +static void bio_fill(struct bio *b, ssize_t avail) { - int avail = 0; - if (ioctl(b->fd, FIONREAD, &avail) < 0) { - bio_seterr(b, -errno); - return -errno; - } if (b->maxlen < avail + b->len) { memmove(b->priv, b->priv + b->ofs, b->len - b->ofs); b->len -= b->ofs; @@ -159,14 +154,16 @@ static int bio_fill(struct bio *b) b->maxlen = b->len + avail; } } - return avail; } static int plain_fill(struct bio *b) { - int avail = bio_fill(b); - if (avail < 0) - return avail; + ssize_t avail = 0; + if (ioctl(b->fd, FIONREAD, &avail) < 0) { + bio_seterr(b, -errno); + return -errno; + } + bio_fill(b, avail); avail = read(b->fd, b->priv + b->len, avail); b->len += avail; return avail; @@ -174,10 +171,22 @@ static int plain_fill(struct bio *b) static int ssl_fill(struct bio *b) { - int avail = bio_fill(b); - if (avail < 0) - return avail; + ssize_t avail; + int ret; + + bio_fill(b, 1); + ret = SSL_read(b->ssl, b->priv + b->len, 1); + if (ret <= 0) + return 0; + b->len += ret; + + avail = SSL_pending(b->ssl); + if (avail <= 0) + return 0; + bio_fill(b, avail); avail = SSL_read(b->ssl, b->priv + b->len, avail); + if (avail <= 0) + return 0; b->len += avail; return avail; } @@ -297,7 +306,7 @@ static int get_connection(const char *server, const char *port) } for (rp = result; rp != NULL; rp = rp->ai_next) { - sfd = socket(rp->ai_family, rp->ai_socktype | SOCK_NONBLOCK, + sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sfd == -1) continue; @@ -354,7 +363,7 @@ static void mainloop(struct bio *b, const char *server) pinged = 0; if (ret > 0) ret = b->fill(b); - if (ret < 0) + if (ret <= 0) continue; while ((ret = b->readline(b, &line) > 0)) { char *prefix = NULL, *command, *ident = NULL, *host = NULL; -- 2.11.4.GIT