Added comment to fix address printing later.
[cascardo/rnetproxy.git] / popproxy.c
index 3217b00..3a12f71 100644 (file)
@@ -45,11 +45,30 @@ server_conn_new (char *server, char *port)
   int fd;
   HCConn *conn;
   HCConn *ssl_conn;
+  int r;
+  fd = hc_tcp_connect (server, port);
+  if (fd < 0)
+    {
+      g_warning ("Could not connect to server at %s:%s.", server, port);
+      return NULL;
+    }
   conn = hc_conn_new (NULL, NULL);
   ssl_conn = hc_conn_new (NULL, NULL);
-  fd = hc_tcp_connect (server, port);
-  hc_conn_set_driver_channel (conn, fd);
-  hc_conn_set_driver_ssl (ssl_conn, conn);
+  r = hc_conn_set_driver_channel (conn, fd);
+  if (r != 0)
+    {
+      hc_conn_close (ssl_conn);
+      hc_conn_close (conn);
+      close (fd);
+      return NULL;
+    }
+  r = hc_conn_set_driver_ssl_client (ssl_conn, conn);
+  if (r != 0)
+    {
+      hc_conn_close (ssl_conn);
+      hc_conn_close (conn);
+      return NULL;
+    }
   return ssl_conn;
 }
 
@@ -75,21 +94,47 @@ static void
 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
 {
   HCConn *conn;
+  HCConn *pop_conn;
   HCConn *server_conn;
-  net_hook_t *hook;
   struct pop_address *address = data;
+  int r;
   if (fd < 0)
     {
       g_critical ("Server has received an error event.");
       return;
     }
+
+  /* FIXME: Should be independent of address type. */
   g_message ("Received connection from %s.",
              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
-  conn = hc_conn_new (NULL, NULL);
-  hc_conn_set_driver_channel (conn, fd);
+
   server_conn = server_conn_new (address->server, address->port);
-  hc_conn_set_callback (conn, push_other, server_conn);
-  hc_conn_set_callback (server_conn, push_other, conn);
+  if (server_conn == NULL)
+    {
+      return;
+    }
+
+  conn = hc_conn_new (NULL, NULL);
+  r = hc_conn_set_driver_channel (conn, fd);
+  if (r != 0)
+    {
+      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);
+
 }
 
 static gchar *configfile;