Remove dependency on nethook and null hook.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 3 Jul 2009 18:35:34 +0000 (15:35 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 3 Jul 2009 21:15:40 +0000 (18:15 -0300)
With SSL as a connection layer, it's very simple to implement a
reverse pass-through proxy simply plugging the two connections.

popproxy.c

index 99a2352..3217b00 100644 (file)
@@ -26,8 +26,6 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "log.h"
-#include "nethook.h"
-#include "null.h"
 #include "pop.h"
 
 #include "hcconn.h"
@@ -41,10 +39,43 @@ struct pop_address
   char *port;
 };
 
+static HCConn *
+server_conn_new (char *server, char *port)
+{
+  int fd;
+  HCConn *conn;
+  HCConn *ssl_conn;
+  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);
+  return ssl_conn;
+}
+
+static void
+push_other (HCConn *conn, HCEvent event, gpointer data)
+{
+  char buffer[4096];
+  int r;
+  switch (event)
+    {
+    case HC_EVENT_READ:
+      while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
+        hc_conn_write (data, buffer, r);
+      break;
+    case HC_EVENT_CLOSE:
+      hc_conn_close (conn);
+      hc_conn_close (data);
+      break;
+    }
+}
+
 static void
 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
 {
   HCConn *conn;
+  HCConn *server_conn;
   net_hook_t *hook;
   struct pop_address *address = data;
   if (fd < 0)
@@ -56,8 +87,9 @@ new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
   conn = hc_conn_new (NULL, NULL);
   hc_conn_set_driver_channel (conn, fd);
-  hook = null_hook_new (conn, address->server, address->port);
-  pop_hook_new (hook);
+  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);
 }
 
 static gchar *configfile;