Switch core from strerror to g_strerror
[claws.git] / src / common / socket.c
index 409a4c8b11521fda42267d473cf674f6a8feeba0..5f8b477a18c6bcdefdf2d0becd89b7421d7d2a1b 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
  *
  * 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
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #if (defined (_XOPEN_SOURCE) && !defined (_BSD_SOURCE))
@@ -237,7 +238,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
@@ -569,6 +570,7 @@ static gint fd_check_io(gint fd, GIOCondition cond)
                return 0;
        } else {
                g_warning("Socket IO timeout\n");
+               log_error(LOG_PROTOCOL, _("Socket IO timeout.\n"));
                return -1;
        }
 }
@@ -597,6 +599,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);
@@ -624,6 +627,7 @@ struct hostent *my_gethostbyname(const gchar *hostname)
                alarm(0);
                signal(SIGALRM, prev_handler);
                g_printerr("%s: host lookup timed out.\n", hostname);
+               log_error(LOG_PROTOCOL, _("%s: host lookup timed out.\n"), hostname);
                errno = 0;
                return NULL;
        }
@@ -636,6 +640,7 @@ struct hostent *my_gethostbyname(const gchar *hostname)
                signal(SIGALRM, prev_handler);
 #endif
                g_printerr("%s: unknown host.\n", hostname);
+               log_error(LOG_PROTOCOL, _("%s: unknown host.\n"), hostname);
                errno = 0;
                return NULL;
        }
@@ -823,6 +828,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;
        }
@@ -947,6 +955,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;
                }
 
@@ -959,14 +968,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;
@@ -1061,6 +1068,8 @@ static gboolean sock_get_address_info_async_cb(GIOChannel *source,
 
                if (ai_member[0] == AF_UNSPEC) {
                        g_warning("DNS lookup failed\n");
+                       log_error(LOG_PROTOCOL, _("%s:%d: unknown host.\n"),
+                               lookup_data->hostname, lookup_data->port);
                        break;
                }
 
@@ -1154,6 +1163,8 @@ static void address_info_async_child(void *opaque)
                gchar len = 0;
                 g_warning("getaddrinfo for %s:%s failed: %s\n",
                           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,
@@ -1377,7 +1388,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;
 
@@ -1406,6 +1417,8 @@ static gint ssl_read(gnutls_session ssl, gchar *buf, gint len)
                        return -1;
 
                default:
+                       debug_print("Unexpected SSL read result %d\n", r);
+                       errno = EIO;
                        return -1;
                }
        }
@@ -1442,7 +1455,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;
 
@@ -1499,7 +1512,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;
@@ -1511,7 +1524,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;
 
@@ -1563,6 +1576,7 @@ gint fd_gets(gint fd, gchar *buf, gint len)
                return -1;
 
 #ifdef G_OS_WIN32
+       fd_check_io(fd, G_IO_IN);
        do {
 /*
 XXX:tm try nonblock
@@ -1614,7 +1628,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