Another step toward IPv6 support with more cleanup.
authorcedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 6 Oct 2008 16:40:01 +0000 (16:40 +0000)
committercedric <cedric@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Mon, 6 Oct 2008 16:40:01 +0000 (16:40 +0000)
Patch from Arnaud de Turckheim.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/ecore@36483 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/lib/ecore_con/ecore_con.c
src/lib/ecore_con/ecore_con_info.c

index 4c5d5e9..0135331 100644 (file)
 # include <winsock2.h>
 #endif
 
-static void _ecore_con_cb_dns_lookup(void *data, struct hostent *he);
-static void _ecore_con_cb_udp_dns_lookup(void *data, struct hostent *he);
 static void _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Netinfo *info);
 static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Netinfo *info);
 static void _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Netinfo *info);
 static void _ecore_con_cb_udp_listen(void *data, Ecore_Con_Netinfo *info);
+
 static void _ecore_con_server_free(Ecore_Con_Server *svr);
 static void _ecore_con_client_free(Ecore_Con_Client *cl);
+
 static int _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler);
 static int _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
 static int _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
 static int _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
 static int _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
+
 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
+
 static void _ecore_con_event_client_add_free(void *data, void *ev);
 static void _ecore_con_event_client_del_free(void *data, void *ev);
 static void _ecore_con_event_client_data_free(void *data, void *ev);
@@ -57,8 +59,25 @@ EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
 
 static Ecore_List *servers = NULL;
 static int init_count = 0;
+
 #if USE_OPENSSL
 static int ssl_init_count = 0;
+static int _ecore_con_init_ssl(Ecore_Con_Server *svr);
+static int _ecore_con_shutdown_ssl(Ecore_Con_Server *svr);
+static int _ecore_con_free_ssl(Ecore_Con_Server *svr);
+
+# define INIT_SSL(svr) _ecore_con_init_ssl(svr)
+# define SHUTDOWN_SSL(svr) _ecore_con_shutdown_ssl(svr)
+# define FREE_SSL(svr) _ecore_con_free_ssl(svr)
+# define UNSET_SSL(svr)                \
+  do {                         \
+    svr->ssl = NULL;           \
+    svr->ssl_ctx = NULL;       \
+  } while (0);
+#else
+# define INIT_SSL(svr) 0
+# define SHUTDOWN_SSL(svr) 0
+# define FREE_SSL(svr) 0
 #endif
 
 #define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
@@ -163,10 +182,8 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
    Ecore_Con_Type      type;
    struct sockaddr_in  socket_addr;
    struct sockaddr_un  socket_unix;
-   struct ip_mreq      mreq;
    struct linger       lin;
    char                buf[4096];
-   const int           on=1;
 
    if (port < 0) return NULL;
    /* local  user   socket: FILE:   ~/.ecore/[name]/[port] */
@@ -175,11 +192,22 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
    svr = calloc(1, sizeof(Ecore_Con_Server));
    if (!svr) return NULL;
 
-   type = compl_type;
-#if USE_OPENSSL
-   /* unset the SSL flag for the following checks */
-   type &= ECORE_CON_TYPE;
-#endif
+   svr->name = strdup(name);
+   if (!svr->name) goto error;
+   svr->type = compl_type;
+   svr->port = port;
+   svr->data = (void *)data;
+   svr->created = 1;
+   svr->reject_excess_clients = 0;
+   svr->client_limit = -1;
+   svr->clients = ecore_list_new();
+   svr->ppid = getpid();
+   /* Set ssl and ssl_ctx fields to NULL */
+   UNSET_SSL(svr);
+   ecore_list_append(servers, svr);
+   ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
+
+   type = compl_type & ECORE_CON_TYPE;
 
    if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) ||
        (type == ECORE_CON_LOCAL_ABSTRACT))
@@ -270,7 +298,7 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
             if (connect(svr->fd, (struct sockaddr *)&socket_unix,
                         socket_unix_len) < 0)
               {
-                 if ((type == ECORE_CON_LOCAL_USER) || 
+                 if ((type == ECORE_CON_LOCAL_USER) ||
                      (type == ECORE_CON_LOCAL_SYSTEM))
                    {
                       if (unlink(buf) < 0)
@@ -309,99 +337,21 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
                                    _ecore_con_svr_handler, svr, NULL, NULL);
        umask(pmode);
        if (!svr->fd_handler) goto error;
+
+       INIT_SSL(svr);
      }
-   else if (type == ECORE_CON_REMOTE_TCP)
+
+   if (type == ECORE_CON_REMOTE_TCP)
      {
-       svr->fd = socket(AF_INET, SOCK_STREAM, 0);
-       if (svr->fd < 0) goto error;
-       if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
-       if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-       lin.l_onoff = 1;
-       lin.l_linger = 0;
-       if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error;
-       socket_addr.sin_family = AF_INET;
-       socket_addr.sin_port = htons(port);
-       socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-       if (bind(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0) goto error;
-       if (listen(svr->fd, 4096) < 0) goto error;
-       svr->fd_handler = 
-         ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
-                                   _ecore_con_svr_handler, svr, NULL, NULL);
-       if (!svr->fd_handler) goto error;
+        /* TCP */
+        if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen, svr)) goto error;
      }
    else if (type == ECORE_CON_REMOTE_MCAST || type == ECORE_CON_REMOTE_UDP)
      {
-       svr->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-       if(svr->fd < 0) goto error;
-
-       socket_addr.sin_family = AF_INET;
-       socket_addr.sin_port = htons(port);
-
-       if (type == ECORE_CON_REMOTE_MCAST)
-        {
-          socket_addr.sin_addr.s_addr = inet_addr(name);
-          mreq.imr_multiaddr.s_addr = inet_addr(name);
-          mreq.imr_interface.s_addr = htonl(INADDR_ANY);
-          if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,sizeof(mreq)) != 0) goto error;
-        }
-       else
-        socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
-
-       if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on)) != 0) goto error;
-       if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
-       if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-       if (bind(svr->fd, (struct sockaddr *)&socket_addr,sizeof(struct sockaddr_in)) < 0) goto error;
-       svr->fd_handler =
-        ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
-                                  _ecore_con_svr_udp_handler, svr, NULL, NULL);
-       if (!svr->fd_handler) goto error;
-     }
-
-#if USE_OPENSSL
-   if (compl_type & ECORE_CON_SSL)
-     {
-       if (!ssl_init_count)
-         {
-            SSL_library_init();
-            SSL_load_error_strings();
-         }
-       ssl_init_count++;
-
-       switch (compl_type & ECORE_CON_SSL)
-         {
-          case ECORE_CON_USE_SSL2:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_SSL3:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_TLS:
-             if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
-               goto error;
-             break;
-         }
-
-       if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
-         goto error;
-
-       SSL_set_fd(svr->ssl, svr->fd);
+        /* UDP and MCAST */
+        if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen, svr)) goto error;
      }
-#endif
 
-   svr->name = strdup(name);
-   if (!svr->name) goto error;
-   svr->type = compl_type;
-   svr->port = port;
-   svr->data = (void *)data;
-   svr->created = 1;
-   svr->reject_excess_clients = 0;
-   svr->client_limit = -1;
-   svr->clients = ecore_list_new();
-   svr->ppid = getpid();
-   ecore_list_append(servers, svr);
-   ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
    return svr;
 
    error:
@@ -411,10 +361,7 @@ ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
    if (svr->write_buf) free(svr->write_buf);
    if (svr->ip) free(svr->ip);
-#if USE_OPENSSL
-   if (svr->ssl) SSL_free(svr->ssl);
-   if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
-#endif
+   FREE_SSL(svr);
    free(svr);
    return NULL;
 }
@@ -463,11 +410,21 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
    svr = calloc(1, sizeof(Ecore_Con_Server));
    if (!svr) return NULL;
 
-   type = compl_type;
-#if USE_OPENSSL
-   /* unset the SSL flag for the following checks */
-   type &= ECORE_CON_TYPE;
-#endif
+   svr->name = strdup(name);
+   if (!svr->name) goto error;
+   svr->type = compl_type;
+   svr->port = port;
+   svr->data = (void *)data;
+   svr->created = 0;
+   svr->reject_excess_clients = 0;
+   svr->client_limit = -1;
+   svr->clients = ecore_list_new();
+   UNSET_SSL(svr);
+   ecore_list_append(servers, svr);
+   ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
+
+   type = compl_type & ECORE_CON_TYPE;
+
    if ((type == ECORE_CON_REMOTE_TCP || type == ECORE_CON_REMOTE_UDP) && (port < 0)) return NULL;
 
    if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) ||
@@ -531,7 +488,7 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
        if (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) goto error;
        svr->path = strdup(buf);
        if (!svr->path) goto error;
-       svr->fd_handler = 
+       svr->fd_handler =
          ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
                                    _ecore_con_cl_handler, svr, NULL, NULL);
        if (!svr->fd_handler) goto error;
@@ -552,24 +509,14 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
          }
      }
 
-   svr->name = strdup(name);
-   if (!svr->name) goto error;
-   svr->type = compl_type;
-   svr->port = port;
-   svr->data = (void *)data;
-   svr->created = 0;
-   svr->reject_excess_clients = 0;
-   svr->client_limit = -1;
-   svr->clients = ecore_list_new();
-   ecore_list_append(servers, svr);
-   ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
-
    if (type == ECORE_CON_REMOTE_TCP)
      {
-       if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr)) goto error;
+        /* TCP */
+        if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr)) goto error;
      }
    else if (type == ECORE_CON_REMOTE_UDP)
      {
+        /* UDP and MCAST */
         if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect, svr)) goto error;
      }
 
@@ -580,10 +527,7 @@ ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
    if (svr->path) free(svr->path);
    if (svr->fd >= 0) close(svr->fd);
    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
-#if USE_OPENSSL
-   if (svr->ssl) SSL_free(svr->ssl);
-   if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
-#endif
+   FREE_SSL(svr);
    free(svr);
    return NULL;
 }
@@ -878,7 +822,7 @@ ecore_con_client_del(Ecore_Con_Client *cl)
        return NULL;
      }
 
-   if(cl->data && cl->server && (cl->server->type == ECORE_CON_REMOTE_UDP || 
+   if(cl->data && cl->server && (cl->server->type == ECORE_CON_REMOTE_UDP ||
                                 cl->server->type == ECORE_CON_REMOTE_MCAST))
      free(cl->data);
    else
@@ -1014,17 +958,11 @@ _ecore_con_server_free(Ecore_Con_Server *svr)
    while (!ecore_list_empty_is(svr->clients))
      _ecore_con_client_free(ecore_list_first_remove(svr->clients));
    ecore_list_destroy(svr->clients);
-   if ((svr->created) && (svr->path) && (svr->ppid == getpid())) 
+   if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
      unlink(svr->path);
    if (svr->fd >= 0) close(svr->fd);
-#if USE_OPENSSL
-   if (svr->ssl)
-     {
-       SSL_shutdown(svr->ssl);
-       SSL_free(svr->ssl);
-     }
-   if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
-#endif
+   SHUTDOWN_SSL(svr);
+   FREE_SSL(svr);
    if (svr->name) free(svr->name);
    if (svr->path) free(svr->path);
    if (svr->ip) free(svr->ip);
@@ -1188,136 +1126,6 @@ kill_server(Ecore_Con_Server *svr)
 }
 
 static void
-_ecore_con_cb_dns_lookup(void *data, struct hostent *he)
-{
-   Ecore_Con_Server   *svr;
-   struct sockaddr_in  socket_addr;
-   int                 curstate = 0;
-   char                buf[64];
-   uint32_t            ip;
-
-   svr = data;
-
-   if (!he) goto error;
-   svr->fd = socket(AF_INET, SOCK_STREAM, 0);
-   if (svr->fd < 0) goto error;
-   if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
-   if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
-     goto error;
-   socket_addr.sin_family = AF_INET;
-   socket_addr.sin_port = htons(svr->port);
-   memcpy((struct in_addr *)&socket_addr.sin_addr,
-         he->h_addr, sizeof(struct in_addr));
-   if (connect(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0)
-     {
-       if (errno != EINPROGRESS)
-         goto error;
-       svr->connecting = 1;
-       svr->fd_handler =
-         ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
-                                   _ecore_con_cl_handler, svr, NULL, NULL);
-     }
-   else
-     svr->fd_handler =
-     ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
-                              _ecore_con_cl_handler, svr, NULL, NULL);
-
-   if (!svr->fd_handler) goto error;
-   ip = socket_addr.sin_addr.s_addr;
-   snprintf(buf, sizeof(buf),
-           "%i.%i.%i.%i",
-           (ip      ) & 0xff,
-           (ip >> 8 ) & 0xff,
-           (ip >> 16) & 0xff,
-           (ip >> 24) & 0xff);
-   svr->ip = strdup(buf);
-
-#if USE_OPENSSL
-   if (svr->type & ECORE_CON_SSL)
-     {
-       if (!ssl_init_count)
-         {
-            SSL_library_init();
-            SSL_load_error_strings();
-         }
-       ssl_init_count++;
-
-       switch (svr->type & ECORE_CON_SSL)
-         {
-          case ECORE_CON_USE_SSL2:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_SSL3:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_TLS:
-             if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
-               goto error;
-             break;
-         }
-
-       if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
-         goto error;
-
-       SSL_set_fd(svr->ssl, svr->fd);
-     }
-#endif
-
-   return;
-
-   error:
-   kill_server(svr);
-}
-
-static void
-_ecore_con_cb_udp_dns_lookup(void *data, struct hostent *he)
-{
-   Ecore_Con_Server   *svr;
-   struct sockaddr_in  socket_addr;
-   int                 curstate = 0;
-   char                buf[64];
-   uint32_t            ip;
-
-   svr = data;
-
-   if (!he) goto error;
-   svr->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
-   if (svr->fd < 0) goto error;
-   if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
-   if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
-     goto error;
-   socket_addr.sin_family = AF_INET;
-   socket_addr.sin_port = htons(svr->port);
-   memcpy((struct in_addr *)&socket_addr.sin_addr,
-         he->h_addr, sizeof(struct in_addr));
-   if (connect(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0)
-     goto error;
-   else
-     svr->fd_handler =
-       ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
-                                _ecore_con_cl_udp_handler, svr, NULL, NULL);
-
-   if (!svr->fd_handler) goto error;
-   ip = socket_addr.sin_addr.s_addr;
-   snprintf(buf, sizeof(buf),
-           "%i.%i.%i.%i",
-           (ip      ) & 0xff,
-           (ip >> 8 ) & 0xff,
-           (ip >> 16) & 0xff,
-           (ip >> 24) & 0xff);
-   svr->ip = strdup(buf);
-
-   return;
-
-   error:
-   kill_server(svr);
-}
-
-static void
 _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Netinfo *net_info)
 {
    Ecore_Con_Server *svr;
@@ -1334,9 +1142,6 @@ _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Netinfo *net_info)
    lin.l_onoff = 1;
    lin.l_linger = 0;
    if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error;
-/*    socket_addr.sin_family = AF_INET; */
-/*    socket_addr.sin_port = htons(port); */
-/*    socket_addr.sin_addr.s_addr = htonl(INADDR_ANY); */
    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
    if (listen(svr->fd, 4096) < 0) goto error;
    svr->fd_handler =
@@ -1344,76 +1149,51 @@ _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Netinfo *net_info)
                               _ecore_con_svr_handler, svr, NULL, NULL);
    if (!svr->fd_handler) goto error;
 
-#if USE_OPENSSL
-   if (svr->type & ECORE_CON_SSL)
-     {
-       if (!ssl_init_count)
-         {
-            SSL_library_init();
-            SSL_load_error_strings();
-         }
-       ssl_init_count++;
-
-       switch (svr->type & ECORE_CON_SSL)
-         {
-          case ECORE_CON_USE_SSL2:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_SSL3:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_TLS:
-             if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
-               goto error;
-             break;
-         }
-
-       if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
-         goto error;
-
-       SSL_set_fd(svr->ssl, svr->fd);
-     }
-#endif
+   if (INIT_SSL(svr))
+     goto error;
 
    return;
 
    error:
-#if USE_OPENSSL
-   if (svr->ssl) SSL_free(svr->ssl);
-   if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
-#endif
+   FREE_SSL(svr);
    kill_server(svr);
 }
 
 static void
-_ecore_con_cb_mcast_listen(void *data, Ecore_Con_Netinfo *net_info)
+_ecore_con_cb_udp_listen(void *data, Ecore_Con_Netinfo *net_info)
 {
    Ecore_Con_Server *svr;
+   Ecore_Con_Type type;
    struct ip_mreq mreq;
    struct ipv6_mreq mreq6;
-   const int on=1;
+   const int on = 1;
 
    svr = data;
+   type = svr->type;
+   type &= ECORE_CON_TYPE;
 
    if (!net_info) goto error;
 
    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
    if(svr->fd < 0) goto error;
-   if (net_info->info.ai_family == AF_INET)
-     {
-       if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq.imr_multiaddr)) goto error;
-       mreq.imr_interface.s_addr = htonl(INADDR_ANY);
-       if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,sizeof(mreq)) != 0) goto error;
-     }
-   else if (net_info->info.ai_family == AF_INET6)
+
+   if (type == ECORE_CON_REMOTE_MCAST)
      {
-       if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq6.ipv6mr_multiaddr)) goto error;
-       mreq6.ipv6mr_interface = htonl(INADDR_ANY);
-       if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq6,sizeof(mreq6)) != 0) goto error;
+       if (net_info->info.ai_family == AF_INET)
+        {
+          if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq.imr_multiaddr)) goto error;
+          mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+          if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,sizeof(mreq)) != 0) goto error;
+        }
+       else if (net_info->info.ai_family == AF_INET6)
+        {
+          if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq6.ipv6mr_multiaddr)) goto error;
+          mreq6.ipv6mr_interface = htonl(INADDR_ANY);
+          if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq6,sizeof(mreq6)) != 0) goto error;
+        }
+       if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on)) != 0) goto error;
      }
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on)) != 0) goto error;
+
    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
@@ -1421,80 +1201,15 @@ _ecore_con_cb_mcast_listen(void *data, Ecore_Con_Netinfo *net_info)
      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
                               _ecore_con_svr_udp_handler, svr, NULL, NULL);
    if (!svr->fd_handler) goto error;
+   svr->ip = strdup(net_info->ip);
 
-#if USE_OPENSSL
-   if (svr->type & ECORE_CON_SSL)
-     {
-       if (!ssl_init_count)
-         {
-            SSL_library_init();
-            SSL_load_error_strings();
-         }
-       ssl_init_count++;
-
-       switch (svr->type & ECORE_CON_SSL)
-         {
-          case ECORE_CON_USE_SSL2:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_SSL3:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_TLS:
-             if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
-               goto error;
-             break;
-         }
-
-       if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
-         goto error;
-
-       SSL_set_fd(svr->ssl, svr->fd);
-     }
-#endif
-
-   return;
-
-   error:
-#if USE_OPENSSL
-   if (svr->ssl) SSL_free(svr->ssl);
-   if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
-#endif
-   kill_server(svr);
-}
-
-static void
-_ecore_con_cb_udp_listen(void *data, Ecore_Con_Netinfo *net_info)
-{
-   //FIXME SSL
-   Ecore_Con_Server   *svr;
-   struct sockaddr_in  socket_addr;
-   int                 curstate = 0;
-   char                buf[64];
-
-   svr = data;
-
-   if (!net_info) goto error;
-   svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
-   if (svr->fd < 0) goto error;
-   if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
-   if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) 
+   if (INIT_SSL(svr))
      goto error;
-   if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
-     goto error;
-   else
-     svr->fd_handler = 
-       ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
-                                _ecore_con_cl_udp_handler, svr, NULL, NULL);
-   if (!svr->fd_handler) goto error;
-   svr->ip = strdup(net_info->ip);
 
    return;
 
    error:
+   FREE_SSL(svr);
    kill_server(svr);
 }
 
@@ -1513,7 +1228,7 @@ _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Netinfo *net_info)
    if (svr->fd < 0) goto error;
    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) 
+   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
      goto error;
    if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
      {
@@ -1532,42 +1247,13 @@ _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Netinfo *net_info)
    if (!svr->fd_handler) goto error;
    svr->ip = strdup(net_info->ip);
 
-#if USE_OPENSSL
-   if (svr->type & ECORE_CON_SSL)
-     {
-       if (!ssl_init_count)
-         {
-            SSL_library_init();
-            SSL_load_error_strings();
-         }
-       ssl_init_count++;
-
-       switch (svr->type & ECORE_CON_SSL)
-         {
-          case ECORE_CON_USE_SSL2:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_SSL3:
-             if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
-               goto error;
-             break;
-          case ECORE_CON_USE_TLS:
-             if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
-               goto error;
-             break;
-         }
-
-       if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
-         goto error;
-
-       SSL_set_fd(svr->ssl, svr->fd);
-     }
-#endif
+   if (INIT_SSL(svr))
+     goto error;
 
    return;
 
    error:
+   FREE_SSL(svr);
    kill_server(svr);
 }
 
@@ -1586,20 +1272,24 @@ _ecore_con_cb_udp_connect(void *data, Ecore_Con_Netinfo *net_info)
    if (svr->fd < 0) goto error;
    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
-   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) 
+   if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
      goto error;
    if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
      goto error;
    else
-     svr->fd_handler = 
+     svr->fd_handler =
        ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
                                 _ecore_con_cl_udp_handler, svr, NULL, NULL);
    if (!svr->fd_handler) goto error;
    svr->ip = strdup(net_info->ip);
 
+   if (INIT_SSL(svr))
+     goto error;
+
    return;
 
    error:
+   FREE_SSL(svr);
    kill_server(svr);
 }
 
@@ -2060,9 +1750,9 @@ _ecore_con_server_flush(Ecore_Con_Server *svr)
    if (!svr->write_buf) return;
 
    /* check whether we need to write anything at all.
-       * we must not write zero bytes with SSL_write() since it
-       * causes undefined behaviour
-       */
+    * we must not write zero bytes with SSL_write() since it
+    * causes undefined behaviour
+    */
    if (svr->write_buf_size == svr->write_buf_offset)
       return;
 
@@ -2245,3 +1935,60 @@ _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
      ecore_con_server_del(e->server);
    free(e);
 }
+
+#if USE_OPENSSL
+
+static int
+_ecore_con_init_ssl(Ecore_Con_Server *svr)
+{
+  if (svr->type & ECORE_CON_SSL)
+    {
+      if (!ssl_init_count)
+       {
+         SSL_library_init();
+         SSL_load_error_strings();
+       }
+      ssl_init_count++;
+
+      switch (svr->type & ECORE_CON_SSL)
+       {
+       case ECORE_CON_USE_SSL2:
+         if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
+           return 1;
+         break;
+       case ECORE_CON_USE_SSL3:
+         if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
+           return 1;
+         break;
+       case ECORE_CON_USE_TLS:
+         if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
+           return 1;
+         break;
+       }
+
+      if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
+       return 1;
+
+      SSL_set_fd(svr->ssl, svr->fd);
+    }
+  return 0;
+}
+
+static int
+_ecore_con_shutdown_ssl(Ecore_Con_Server *svr)
+{
+  if (svr->ssl) SSL_shutdown(svr->ssl);
+  return 0;
+}
+
+static int
+_ecore_con_free_ssl(Ecore_Con_Server *svr)
+{
+  if (svr->ssl) SSL_free(svr->ssl);
+  if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
+  UNSET_SSL(svr);
+
+  return 0;
+}
+
+#endif
index 3da72b8..1b5abd2 100644 (file)
@@ -194,6 +194,8 @@ _ecore_con_info_get(Ecore_Con_Server *svr,
            container.info.ai_canonname = NULL;
            container.info.ai_next = NULL;
            memcpy(&container.addr, result->ai_addr, sizeof(struct sockaddr));
+           memset(container.ip, 0, sizeof(container.ip));
+           memset(container.service, 0, sizeof(container.service));
            if (!getnameinfo(&container.addr, container.info.ai_addrlen,
                             hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
                             NI_NUMERICHOST | NI_NUMERICSERV))
@@ -201,12 +203,7 @@ _ecore_con_info_get(Ecore_Con_Server *svr,
                memcpy(container.ip, hbuf, sizeof(container.ip));
                memcpy(container.service, sbuf, sizeof(container.service));
              }
-           else
-             {
-               memset(container.ip, 0, sizeof(container.ip));
-               memset(container.service, 0, sizeof(container.service));
-             }
-           write(fd[1], &container, sizeof(container));
+           write(fd[1], &container, sizeof(Ecore_Con_Netinfo));
          }
        else
          write(fd[1], "", 1);