Remove useless cruft from OpenSSL days
[claws.git] / src / common / socket.c
index ee98bc993c517ed9d234199a203fc40343c94057..d7773aa8766a237801766c006b1f31c5ebaaf258 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2012 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #if (defined (_XOPEN_SOURCE) && !defined (_BSD_SOURCE))
@@ -129,20 +130,6 @@ static guint io_timeout = 60;
 
 static GList *sock_connect_data_list = NULL;
 
-static gboolean sock_prepare           (GSource        *source,
-                                        gint           *timeout);
-static gboolean sock_check             (GSource        *source);
-static gboolean sock_dispatch          (GSource        *source,
-                                        GSourceFunc     callback,
-                                        gpointer        user_data);
-
-GSourceFuncs sock_watch_funcs = {
-       sock_prepare,
-       sock_check,
-       sock_dispatch,
-       NULL
-};
-
 static gint sock_connect_with_timeout  (gint                    sock,
                                         const struct sockaddr  *serv_addr,
                                         gint                    addrlen,
@@ -456,47 +443,6 @@ gboolean sock_is_nonblocking_mode(SockInfo *sock)
        return is_nonblocking_mode(sock->sock);
 }
 
-
-static gboolean sock_prepare(GSource *source, gint *timeout)
-{
-       *timeout = 1;
-       return FALSE;
-}
-
-static gboolean sock_check(GSource *source)
-{
-       SockInfo *sock = ((SockSource *)source)->sock;
-       struct timeval timeout = {0, 0};
-       fd_set fds;
-       GIOCondition condition = 0;
-        
-       if (!sock || !sock->sock)
-               return FALSE;
-
-       condition = sock->condition;
-
-       FD_ZERO(&fds);
-       FD_SET(sock->sock, &fds);
-
-       select(sock->sock + 1,
-              (condition & G_IO_IN)  ? &fds : NULL,
-              (condition & G_IO_OUT) ? &fds : NULL,
-              NULL, &timeout);
-
-       return FD_ISSET(sock->sock, &fds) != 0;
-}
-
-static gboolean sock_dispatch(GSource *source, GSourceFunc callback,
-                             gpointer user_data)
-{
-       SockInfo *sock = ((SockSource *)source)->sock;
-
-       if (!sock || !sock->callback || !sock->data)
-               return FALSE;
-
-       return sock->callback(sock, sock->condition, sock->data);
-}
-
 static gboolean sock_watch_cb(GIOChannel *source, GIOCondition condition,
                              gpointer data)
 {
@@ -518,20 +464,6 @@ guint sock_add_watch(SockInfo *sock, GIOCondition condition, SockFunc func,
        sock->condition = condition;
        sock->data = data;
 
-#ifdef USE_GNUTLS
-       if (sock->ssl)
-       {
-               GSource *source = g_source_new(&sock_watch_funcs,
-                                              sizeof(SockSource));
-               ((SockSource *) source)->sock = sock;
-               g_source_set_priority(source, G_PRIORITY_DEFAULT);
-               g_source_set_can_recurse(source, FALSE);
-               sock->g_source = g_source_attach(source, NULL);
-               g_source_unref (source); /* Refcount back down to 1 */
-               return sock->g_source;
-       }
-#endif
-
        return g_io_add_watch(sock->sock_ch, condition, sock_watch_cb, sock);
 }
 
@@ -561,6 +493,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;
        }
 }
@@ -589,6 +522,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);
@@ -616,6 +550,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;
        }
@@ -628,6 +563,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;
        }
@@ -815,6 +751,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,
+                         strerror(val));
                sock_connect_address_list_async(conn_data);
                return FALSE;
        }
@@ -939,6 +878,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;
                }
 
@@ -951,14 +891,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;
@@ -1053,6 +991,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;
                }
 
@@ -1146,6 +1086,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,
@@ -1369,7 +1311,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;
 
@@ -1398,6 +1340,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;
                }
        }
@@ -1434,7 +1378,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;
 
@@ -1503,7 +1447,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;
 
@@ -1555,6 +1499,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
@@ -1606,9 +1551,6 @@ gint sock_close(SockInfo *sock)
 #ifdef USE_GNUTLS
        if (sock->ssl)
                ssl_done_socket(sock);
-       if (sock->g_source != 0)
-               g_source_remove(sock->g_source);
-       sock->g_source = 0;
 #endif
 #ifdef G_OS_WIN32
        shutdown(sock->sock, 1); /* complete transfer before close */