Added separate header for SSL connection support.
[cascardo/rnetproxy.git] / popproxy.c
index 3a12f71..713505c 100644 (file)
@@ -29,6 +29,7 @@
 #include "pop.h"
 
 #include "hcconn.h"
+#include "hcconn_ssl.h"
 #include "tcp_connect.h"
 
 #define CONFFILE SYSCONFDIR "/popproxy.conf"
@@ -72,6 +73,40 @@ server_conn_new (char *server, char *port)
   return ssl_conn;
 }
 
+static HCConn *
+client_conn_new (int fd)
+{
+  HCConn *conn;
+  HCConn *ssl_conn;
+  HCConn *pop_conn;
+  int r;
+  conn = hc_conn_new (NULL, NULL);
+  r = hc_conn_set_driver_channel (conn, fd);
+  if (r != 0)
+    {
+      hc_conn_close (conn);
+      close (fd);
+      return NULL;
+    }
+  ssl_conn = hc_conn_new (NULL, NULL);
+  hc_conn_set_driver_ssl_server (ssl_conn, conn);
+  if (r != 0)
+    {
+      hc_conn_close (ssl_conn);
+      hc_conn_close (conn);
+      return NULL;
+    }
+  pop_conn = hc_conn_new (NULL, NULL);
+  r = hc_conn_set_driver_pop (pop_conn, conn);
+  if (r != 0)
+    {
+      hc_conn_close (pop_conn);
+      hc_conn_close (ssl_conn);
+      return NULL;
+    }
+  return pop_conn;
+}
+
 static void
 push_other (HCConn *conn, HCEvent event, gpointer data)
 {
@@ -93,11 +128,9 @@ push_other (HCConn *conn, HCEvent event, gpointer data)
 static void
 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
 {
-  HCConn *conn;
-  HCConn *pop_conn;
+  HCConn *client_conn;
   HCConn *server_conn;
   struct pop_address *address = data;
-  int r;
   if (fd < 0)
     {
       g_critical ("Server has received an error event.");
@@ -113,27 +146,15 @@ new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
     {
       return;
     }
-
-  conn = hc_conn_new (NULL, NULL);
-  r = hc_conn_set_driver_channel (conn, fd);
-  if (r != 0)
+  client_conn = client_conn_new (fd);
+  if (client_conn == NULL)
     {
       hc_conn_close (server_conn);
-      hc_conn_close (conn);
-      close (fd);
-      return;
-    }
-  pop_conn = hc_conn_new (NULL, NULL);
-  r = hc_conn_set_driver_pop (pop_conn, conn);
-  if (r != 0)
-    {
-      hc_conn_close (server_conn);
-      hc_conn_close (pop_conn);
-      hc_conn_close (conn);
       return;
     }
-  hc_conn_set_callback (pop_conn, push_other, server_conn);
-  hc_conn_set_callback (server_conn, push_other, pop_conn);
+
+  hc_conn_set_callback (client_conn, push_other, server_conn);
+  hc_conn_set_callback (server_conn, push_other, client_conn);
 
 }