Fix more compile warnings for Windows in socket.c.
[claws.git] / src / common / socket.c
index b9ecb92e525bdc395f11c47bf8fd4ef3881a3ccc..2fc18b120c6b9c7d4080ebb1e13eefaf2e244667 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
+ *
  */
 
 #ifdef HAVE_CONFIG_H
 #define _BSD_SOURCE
 #endif
 
+/* This can probably be handled better, e.g. define it in config.h. */
+#define _WIN32_WINNT _WIN32_WINNT_WIN6
 #include <glib.h>
 #include <glib/gi18n.h>
 
 #include <sys/time.h>
 #include <sys/types.h>
 #ifdef G_OS_WIN32
-#  include <winsock2.h>
+#  include <ws2tcpip.h>
 #  ifndef EINPROGRESS
 #    define EINPROGRESS WSAEINPROGRESS
 #  endif
@@ -151,14 +153,8 @@ static gint sock_connect_with_timeout      (gint                    sock,
                                         gint                    addrlen,
                                         guint                   timeout_secs);
 
-#ifndef INET6
-static gint sock_connect_by_hostname   (gint            sock,
-                                        const gchar    *hostname,
-                                        gushort         port);
-#else
 static gint sock_connect_by_getaddrinfo        (const gchar    *hostname,
                                         gushort         port);
-#endif
 
 static SockInfo *sockinfo_from_fd(const gchar *hostname,
                                  gushort port,
@@ -193,7 +189,7 @@ gint sock_init(void)
 
        result = WSAStartup(MAKEWORD(2, 2), &wsadata);
        if (result != NO_ERROR) {
-               g_warning("WSAStartup() failed\n");
+               g_warning("WSAStartup() failed");
                return -1;
        }
 #endif
@@ -218,14 +214,14 @@ void refresh_resolvers(void)
 {
 #ifdef G_OS_UNIX
        static time_t resolv_conf_changed = (time_t)NULL;
-       struct stat s;
+       GStatBuf s;
 
        /* This makes the glibc re-read resolv.conf, if it changed
         * since our startup. Maybe that should be #ifdef'ed, I don't
         * know if it'd work on BSDs.
         * Why doesn't the glibc do it by itself?
         */
-       if (stat("/etc/resolv.conf", &s) == 0) {
+       if (g_stat("/etc/resolv.conf", &s) == 0) {
                if (s.st_mtime > resolv_conf_changed) {
                        resolv_conf_changed = s.st_mtime;
                        res_init();
@@ -238,7 +234,7 @@ void refresh_resolvers(void)
 #ifdef G_OS_WIN32
 #define SOCKET_IS_VALID(s)      ((s) != INVALID_SOCKET)
 #else
-#define SOCKET_IS_VALID(s)     TRUE
+#define SOCKET_IS_VALID(s)     (s != -1)
 #endif
 
 /* Due to the fact that socket under Windows are not represented by
@@ -294,7 +290,7 @@ gint fd_open_inet(gushort port)
        sock = socket(AF_INET, SOCK_STREAM, 0);
        if (!SOCKET_IS_VALID(sock)) {
 #ifdef G_OS_WIN32
-               g_warning("fd_open_inet(): socket() failed: %d\n",
+               g_warning("fd_open_inet(): socket() failed: %d",
                          WSAGetLastError());
 #else
                perror("fd_open_inet(): socket");
@@ -569,7 +565,8 @@ static gint fd_check_io(gint fd, GIOCondition cond)
        if (FD_ISSET(fd, &fds)) {
                return 0;
        } else {
-               g_warning("Socket IO timeout\n");
+               g_warning("Socket IO timeout");
+               log_error(LOG_PROTOCOL, _("Socket IO timeout.\n"));
                return -1;
        }
 }
@@ -598,6 +595,7 @@ static gint sock_connect_with_timeout(gint sock,
                alarm(0);
                signal(SIGALRM, prev_handler);
                errno = ETIMEDOUT;
+               log_error(LOG_PROTOCOL, _("Connection timed out.\n"));
                return -1;
        }
        alarm(timeout_secs);
@@ -613,96 +611,6 @@ static gint sock_connect_with_timeout(gint sock,
        return ret;
 }
 
-struct hostent *my_gethostbyname(const gchar *hostname)
-{
-       struct hostent *hp;
-#ifdef G_OS_UNIX
-       void (*prev_handler)(gint);
-       
-       alarm(0);
-       prev_handler = signal(SIGALRM, timeout_handler);
-       if (sigsetjmp(jmpenv, 1)) {
-               alarm(0);
-               signal(SIGALRM, prev_handler);
-               g_printerr("%s: host lookup timed out.\n", hostname);
-               errno = 0;
-               return NULL;
-       }
-       alarm(io_timeout);
-#endif
-
-       if ((hp = gethostbyname(hostname)) == NULL) {
-#ifdef G_OS_UNIX
-               alarm(0);
-               signal(SIGALRM, prev_handler);
-#endif
-               g_printerr("%s: unknown host.\n", hostname);
-               errno = 0;
-               return NULL;
-       }
-
-#ifdef G_OS_UNIX
-       alarm(0);
-       signal(SIGALRM, prev_handler);
-#endif
-
-       return hp;
-}
-
-#ifndef INET6
-static gint my_inet_aton(const gchar *hostname, struct in_addr *inp)
-{
-#if HAVE_INET_ATON
-       return inet_aton(hostname, inp);
-#else
-#if HAVE_INET_ADDR
-       guint32 inaddr;
-
-       inaddr = inet_addr(hostname);
-       if (inaddr != -1) {
-               memcpy(inp, &inaddr, sizeof(inaddr));
-               return 1;
-       } else
-               return 0;
-#else
-       return 0;
-#endif
-#endif /* HAVE_INET_ATON */
-}
-
-static gint sock_connect_by_hostname(gint sock, const gchar *hostname,
-                                    gushort port)
-{
-       struct hostent *hp;
-       struct sockaddr_in ad;
-
-       memset(&ad, 0, sizeof(ad));
-       ad.sin_family = AF_INET;
-       ad.sin_port = htons(port);
-
-       refresh_resolvers();
-
-       if (!my_inet_aton(hostname, &ad.sin_addr)) {
-               if ((hp = my_gethostbyname(hostname)) == NULL) {
-                       g_printerr("%s: unknown host.\n", hostname);
-                       errno = 0;
-                       return -1;
-               }
-
-               if (hp->h_length != 4 && hp->h_length != 8) {
-                       g_printerr("illegal address length received for host %s\n", hostname);
-                       errno = 0;
-                       return -1;
-               }
-
-               memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
-       }
-
-       return sock_connect_with_timeout(sock, (struct sockaddr *)&ad,
-                                        sizeof(ad), io_timeout);
-}
-
-#else /* INET6 */
 static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
 {
        gint sock = -1, gai_error;
@@ -712,8 +620,14 @@ static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort     port)
        refresh_resolvers();
 
        memset(&hints, 0, sizeof(hints));
-       /* hints.ai_flags = AI_CANONNAME; */
+       hints.ai_flags = AI_ADDRCONFIG;
+
+#ifdef INET6
        hints.ai_family = AF_UNSPEC;
+#else
+       hints.ai_family = AF_INET;
+#endif
+
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
 
@@ -727,9 +641,18 @@ static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort     port)
        }
 
        for (ai = res; ai != NULL; ai = ai->ai_next) {
+#ifndef INET6
+               if (ai->ai_family == AF_INET6)
+                       continue;
+#endif
+
                sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
-               if (sock < 0)
+               if (sock < 0 )
+                       continue;
+#ifdef G_OS_WIN32
+               if (sock == INVALID_SOCKET)
                        continue;
+#endif
 
                if (sock_connect_with_timeout
                        (sock, ai->ai_addr, ai->ai_addrlen, io_timeout) == 0)
@@ -746,7 +669,6 @@ static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort      port)
 
        return sock;
 }
-#endif /* !INET6 */
 
 SockInfo *sock_connect(const gchar *hostname, gushort port)
 {
@@ -756,26 +678,9 @@ SockInfo *sock_connect(const gchar *hostname, gushort port)
        gint sock;
 #endif
 
-#ifdef INET6
-       if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
-               return NULL;
-#else
-#ifdef G_OS_WIN32
-       if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
-               g_warning("socket() failed: %d\n", WSAGetLastError());
-#else
-       if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
-               perror("socket");
-#endif /* G_OS_WIN32 */
-               return NULL;
-       }
-
-       if (sock_connect_by_hostname(sock, hostname, port) < 0) {
-               if (errno != 0) perror("connect");
-               close(sock);
+       if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0) {
                return NULL;
        }
-#endif /* INET6 */
 
        return sockinfo_from_fd(hostname, port, sock);
 }
@@ -815,7 +720,7 @@ static gboolean sock_connect_async_cb(GIOChannel *source,
        g_io_channel_unref(source);
 
        len = sizeof(val);
-       if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) < 0) {
+       if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&val, &len) < 0) {
                perror("getsockopt");
                close(fd);
                sock_connect_address_list_async(conn_data);
@@ -824,6 +729,9 @@ static gboolean sock_connect_async_cb(GIOChannel *source,
 
        if (val != 0) {
                close(fd);
+               log_error(LOG_PROTOCOL, _("%s:%d: connection failed (%s).\n"),
+                         conn_data->hostname, conn_data->port,
+                         g_strerror(val));
                sock_connect_address_list_async(conn_data);
                return FALSE;
        }
@@ -929,7 +837,7 @@ gint sock_connect_async_cancel(gint id)
                g_free(conn_data->hostname);
                g_free(conn_data);
        } else {
-               g_warning("sock_connect_async_cancel: id %d not found.\n", id);
+               g_warning("sock_connect_async_cancel: id %d not found", id);
                return -1;
        }
 
@@ -948,6 +856,7 @@ static gint sock_connect_address_list_async(SockConnectData *conn_data)
                if ((sock = socket(addr_data->family, addr_data->socktype,
                                   addr_data->protocol)) < 0) {
                        perror("socket");
+
                        continue;
                }
 
@@ -960,14 +869,12 @@ static gint sock_connect_address_list_async(SockConnectData *conn_data)
                                perror("connect");
                                close(sock);
                        }
-               } else
+               } else {
                        break;
+               }
        }
 
        if (conn_data->cur_addr == NULL) {
-               g_warning("sock_connect_address_list_async: "
-                         "connection to %s:%d failed\n",
-                         conn_data->hostname, conn_data->port);
                conn_data->func(NULL, conn_data->data);
                sock_connect_async_cancel(conn_data->id);
                return -1;
@@ -1004,7 +911,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
        
        g_io_channel_set_encoding(source, NULL, &err);
        if (err) {
-               g_warning("can unset encoding: %s\n", err->message);
+               g_warning("can unset encoding: %s", err->message);
                g_error_free(err);
                return FALSE;
        }
@@ -1012,7 +919,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
        if (g_io_channel_read_chars(source, &len, sizeof(len),
                              &bytes_read, &err) == G_IO_STATUS_NORMAL) {
                if (err != NULL) {
-                       g_warning("g_io_channel_read_chars: %s\n", err->message);
+                       g_warning("g_io_channel_read_chars: %s", err->message);
                        g_error_free(err);
                        return FALSE;
                } 
@@ -1025,7 +932,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
                                if (g_io_channel_read_chars(source, cur, todo,
                                      &bytes_read, &err) != G_IO_STATUS_NORMAL) {
                                        if (err) {
-                                             g_warning("canonical name not read %s\n", err->message);
+                                             g_warning("canonical name not read %s", err->message);
                                              g_free(canonical_name);
                                              canonical_name = NULL;
                                              g_error_free(err);
@@ -1037,7 +944,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
                                        todo -= bytes_read;
                                }
                                if (bytes_read == 0) {
-                                     g_warning("canonical name not read\n");
+                                     g_warning("canonical name not read");
                                      g_free(canonical_name);
                                      canonical_name = NULL;
                                      break;
@@ -1050,7 +957,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
                                      sizeof(ai_member), &bytes_read, &err) 
                    != G_IO_STATUS_NORMAL) {
                        if (err != NULL) {
-                               g_warning("g_io_channel_read_chars: addr len %s\n", err->message);
+                               g_warning("g_io_channel_read_chars: addr len %s", err->message);
                                g_error_free(err);
                                err = NULL;
                                break;
@@ -1061,7 +968,9 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
                        break;
 
                if (ai_member[0] == AF_UNSPEC) {
-                       g_warning("DNS lookup failed\n");
+                       g_warning("DNS lookup failed");
+                       log_error(LOG_PROTOCOL, _("%s:%d: unknown host.\n"),
+                               lookup_data->hostname, lookup_data->port);
                        break;
                }
 
@@ -1070,7 +979,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
                                      &bytes_read, &err) 
                    != G_IO_STATUS_NORMAL) {
                        if (err != NULL) {
-                               g_warning("g_io_channel_read_chars: addr data read %s\n", err->message);
+                               g_warning("g_io_channel_read_chars: addr data read %s", err->message);
                                g_error_free(err);
                                err = NULL;
                                g_free(addr);
@@ -1080,7 +989,7 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
 
                if (bytes_read != ai_member[3]) {
                        g_warning("sock_get_address_info_async_cb: "
-                                 "incomplete address data\n");
+                                 "incomplete address data");
                        g_free(addr);
                        break;
                }
@@ -1125,15 +1034,9 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
 static void address_info_async_child(void *opaque)
 {
         SockLookupData *parm = opaque;
-#ifdef INET6
         gint gai_err;
         struct addrinfo hints, *res, *ai;
         gchar port_str[6];
-#else /* !INET6 */
-        struct hostent *hp;
-        gchar **addr_list_p;
-        struct sockaddr_in ad;
-#endif /* INET6 */
         gint ai_member[4] = {AF_UNSPEC, 0, 0, 0};
 
 #ifndef G_OS_WIN32
@@ -1141,10 +1044,13 @@ static void address_info_async_child(void *opaque)
         parm->pipe_fds[0] = -1;
 #endif
 
-#ifdef INET6
         memset(&hints, 0, sizeof(hints));
-        hints.ai_flags = AI_CANONNAME;
+        hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
+#ifdef INET6
         hints.ai_family = AF_UNSPEC;
+#else
+                               hints.ai_family = AF_INET;
+#endif
         hints.ai_socktype = SOCK_STREAM;
         hints.ai_protocol = IPPROTO_TCP;
 
@@ -1153,8 +1059,10 @@ static void address_info_async_child(void *opaque)
         gai_err = getaddrinfo(parm->hostname, port_str, &hints, &res);
         if (gai_err != 0) {
                gchar len = 0;
-                g_warning("getaddrinfo for %s:%s failed: %s\n",
+                g_warning("getaddrinfo for %s:%s failed: %s",
                           parm->hostname, port_str, gai_strerror(gai_err));
+               log_error(LOG_PROTOCOL, _("%s:%s: host lookup failed (%s).\n"),
+                         parm->hostname, port_str, gai_strerror(gai_err));
                fd_write_all(parm->pipe_fds[1], &len,
                      sizeof(len));
                 fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
@@ -1200,51 +1108,6 @@ static void address_info_async_child(void *opaque)
 
         if (res != NULL)
                 freeaddrinfo(res);
-#else /* !INET6 */
-        hp = my_gethostbyname(parm->hostname);
-        if (hp == NULL || hp->h_addrtype != AF_INET) {
-               gchar len = 0;
-               fd_write_all(parm->pipe_fds[1], &len,
-                     sizeof(len));
-                fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
-                             sizeof(ai_member));
-               close(parm->pipe_fds[1]);
-                parm->pipe_fds[1] = -1;
-#ifdef G_OS_WIN32
-                _endthread();
-#else
-                _exit(1);
-#endif
-        }
-
-        ai_member[0] = AF_INET;
-        ai_member[1] = SOCK_STREAM;
-        ai_member[2] = IPPROTO_TCP;
-        ai_member[3] = sizeof(ad);
-
-        memset(&ad, 0, sizeof(ad));
-        ad.sin_family = AF_INET;
-        ad.sin_port = htons(parm->port);
-
-       if (hp->h_name && strlen(hp->h_name) < 255) {
-               gchar len = strlen(hp->h_name);
-               fd_write_all(parm->pipe_fds[1], &len,
-                     sizeof(len));
-               fd_write_all(parm->pipe_fds[1], hp->h_name,
-                     len);                      
-       } else {
-               gchar len = 0;
-               fd_write_all(parm->pipe_fds[1], &len,
-                     sizeof(len));
-       }
-        for (addr_list_p = hp->h_addr_list; *addr_list_p != NULL;
-             addr_list_p++) {
-                memcpy(&ad.sin_addr, *addr_list_p, hp->h_length);
-                fd_write_all(parm->pipe_fds[1], (gchar *)ai_member,
-                             sizeof(ai_member));
-                fd_write_all(parm->pipe_fds[1], (gchar *)&ad, sizeof(ad));
-        }
-#endif /* INET6 */
 
         close(parm->pipe_fds[1]);
         parm->pipe_fds[1] = -1;
@@ -1378,7 +1241,7 @@ static gint fd_read(gint fd, gchar *buf, gint len)
 }
 
 #if USE_GNUTLS
-static gint ssl_read(gnutls_session ssl, gchar *buf, gint len)
+static gint ssl_read(gnutls_session_t ssl, gchar *buf, gint len)
 {
        gint r;
 
@@ -1407,6 +1270,8 @@ static gint ssl_read(gnutls_session ssl, gchar *buf, gint len)
                        return -1;
 
                default:
+                       debug_print("Unexpected SSL/TLS read result %d\n", r);
+                       errno = EIO;
                        return -1;
                }
        }
@@ -1443,7 +1308,7 @@ gint fd_write(gint fd, const gchar *buf, gint len)
 }
 
 #if USE_GNUTLS
-static gint ssl_write(gnutls_session ssl, const gchar *buf, gint len)
+static gint ssl_write(gnutls_session_t ssl, const gchar *buf, gint len)
 {
        gint ret;
 
@@ -1500,7 +1365,7 @@ gint fd_write_all(gint fd, const gchar *buf, gint len)
                         n = write(fd, buf, len);
 
                if (n <= 0) {
-                       log_error(LOG_PROTOCOL, _("write on fd%d: %s\n"), fd, strerror(errno));
+                       log_error(LOG_PROTOCOL, _("write on fd%d: %s\n"), fd, g_strerror(errno));
                        return -1;
                }
                len -= n;
@@ -1512,7 +1377,7 @@ gint fd_write_all(gint fd, const gchar *buf, gint len)
 }
 
 #ifdef USE_GNUTLS
-static gint ssl_write_all(gnutls_session ssl, const gchar *buf, gint len)
+static gint ssl_write_all(gnutls_session_t ssl, const gchar *buf, gint len)
 {
        gint n, wrlen = 0;
 
@@ -1557,13 +1422,13 @@ static gint fd_recv(gint fd, gchar *buf, gint len, gint flags)
 
 gint fd_gets(gint fd, gchar *buf, gint len)
 {
-       gchar *newline, *bp = buf;
-       gint n;
+       gchar *bp = buf;
 
        if (--len < 1)
                return -1;
 
 #ifdef G_OS_WIN32
+       fd_check_io(fd, G_IO_IN);
        do {
 /*
 XXX:tm try nonblock
@@ -1586,6 +1451,8 @@ Single-byte send() and recv().
                len--;
        } while (0 < len);
 #else /*!G_OS_WIN32*/
+       gchar *newline;
+       gint n;
        do {
                if ((n = fd_recv(fd, bp, len, MSG_PEEK)) <= 0)
                        return -1;
@@ -1615,7 +1482,7 @@ gint sock_close(SockInfo *sock)
 #ifdef USE_GNUTLS
        if (sock->ssl)
                ssl_done_socket(sock);
-       if (sock->g_source != 0)
+       if (sock->g_source != 0 && g_main_context_find_source_by_id(NULL, sock->g_source) != NULL)
                g_source_remove(sock->g_source);
        sock->g_source = 0;
 #endif