dmc

dynamic mail client
git clone git://git.suckless.org/dmc
Log | Files | Refs | README | LICENSE

commit 9226dae786375b2d3df930119ed13ceb76c56377
parent 798096948182373ee11668bc8428701955dcb001
Author: pancake@flubox <unknown>
Date:   Thu, 12 Nov 2009 13:08:49 +0100

* Apply nibble's patch fixing issues in sock(ssl) and pop3
Diffstat:
pop3.c | 22++++++++--------------
sock.c | 40++++++++++++++++------------------------
2 files changed, 24 insertions(+), 38 deletions(-)

diff --git a/pop3.c b/pop3.c @@ -24,36 +24,30 @@ static char *getword () { static int waitreply (int res) { char result[256]; char *ch, *str = word; - int lock = 1; int reply = -1; - result[0] = 0; + result[0] = '\0'; ftruncate (2, 0); lseek (2, 0, SEEK_SET); - while (lock || sock_ready ()) { - lock = 0; - if (sock_read (word, 512) <1) + do { + if (sock_read (word, 512) < 1) break; if (reply==-1) { - ch = strchr (str, '\r'); - if (!ch) - ch = strchr (str, '\n'); - if (ch) { + if ((ch = strchr (str, '\r')) || (ch = strchr (str, '\n'))) { reply = (word[0] == '+'); - *ch = 0; + *ch = '\0'; snprintf (result, 1023, "### %s %d \"%s\"\n", cmd, reply, str); str = ch+((ch[1]=='\n')?2:1); } } // TODO: Fix possible \r\n issues - ch = strstr (str, "\r\n."); - if (ch) + if ((ch = strstr (str, "\r\n."))) *ch = '\0'; write (2, str, strlen (str)); - } + } while (sock_ready ()); write (2, "\n", 1); if (res) { - if (result[0] == 0) + if (result[0] == '\0') snprintf (result, 1023, "### %s %d \"\"\n", cmd, reply); write (1, result, strlen (result)); } diff --git a/sock.c b/sock.c @@ -18,30 +18,24 @@ static SSL *sfd; #endif static int fd = -1; -int sock_ssl (int enable) { - int ret = fd; -#if HAVE_SSL - if (enable) { - // TODO Check certificate - SSL_library_init (); - SSL_load_error_strings (); - OpenSSL_add_all_algorithms (); - ctx = SSL_CTX_new (SSLv23_method ()); - sfd = SSL_new (ctx); - SSL_set_fd (sfd, fd); - if (SSL_connect (sfd) < 1) - ret = -1; - } - ssl = enable; -#endif - return ret; +int sock_ssl (int fd) { + // TODO Check certificate + SSL_library_init (); + SSL_load_error_strings (); + OpenSSL_add_all_algorithms (); + ctx = SSL_CTX_new (SSLv23_method ()); + sfd = SSL_new (ctx); + SSL_set_fd (sfd, fd); + if (SSL_connect (sfd) < 1) + return -1; + else return fd; } -int sock_connect(const char *host, int port, int ssl) { +int sock_connect(const char *host, int port, int ssl_enable) { struct sockaddr_in sa; struct hostent *he; int s = socket (AF_INET, SOCK_STREAM, 0); - fd = -1; + ssl = ssl_enable, fd = -1; if (s != -1) { memset (&sa, 0, sizeof (sa)); sa.sin_family = AF_INET; @@ -49,10 +43,8 @@ int sock_connect(const char *host, int port, int ssl) { if (he != (struct hostent*)0) { sa.sin_addr = *((struct in_addr *)he->h_addr); sa.sin_port = htons (port); - if (!connect (s, (const struct sockaddr*)&sa, sizeof (struct sockaddr))) { - fd = s; - fd = sock_ssl (ssl); - } + if (!connect (s, (const struct sockaddr*)&sa, sizeof (struct sockaddr))) + fd = ssl ? sock_ssl (s) : s; } if (fd == -1) close (s); @@ -65,7 +57,7 @@ int sock_ready() { fds[0].fd = fd; fds[0].events = POLLIN|POLLPRI; fds[0].revents = POLLNVAL|POLLHUP|POLLERR; - return poll(fds, 1, 10); + return poll(fds, 1, 100); } void sock_close() {