Separate iochannel implementation from HCConn interface.
[cascardo/rnetproxy.git] / popproxy.c
index b13c49a..3882f75 100644 (file)
 */
 
 #include <glib.h>
-#include <gnet.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <unistd.h>
+#include <gnutls/gnutls.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include "log.h"
 #include "nethook.h"
-#include "proto_detect.h"
+#include "null.h"
+#include "ssl.h"
+#include "pop.h"
 
-#define CONFFILE SYSCONFDIR "/improxy.conf"
+#include "hcconn.h"
+#include "tcp_connect.h"
 
-void new_client (GServer* server, GConn* conn, gpointer data)
+#define CONFFILE SYSCONFDIR "/popproxy.conf"
+
+struct pop_address
+{
+  char *server;
+  char *port;
+};
+
+void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data)
 {
+  HCConn *conn;
   net_hook_t* hook;
-  if (conn == NULL)
+  struct pop_address *address = data;
+  if (fd < 0)
     {
       g_critical ("Server has received an error event.");
       return;
     }
-  g_message ("Received connection from %s.", conn->hostname);
-  hook = proto_detect_new (conn);
-  gnet_conn_read (conn);
+  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);
+  hook = ssl_hook_new (conn, address->server, address->port);
+  pop_hook_new (hook);
 }
 
 static gchar* configfile;
@@ -55,12 +74,16 @@ int main (int argc, char** argv)
 
   GOptionContext* opt_ctx;
   GKeyFile *keyfile;
-  GInetAddr* inetaddr;
+  GError *error;
+  int server_fd;
   gchar* conf_address;
-  gint port;
+  gchar* port;
+  gchar *server_address;
+  gchar *server_port;
+  struct pop_address pop_address;
 
-  gnet_init ();
-  im_log_init ();
+  gnutls_global_init ();
+  pop_log_init ();
 
   configfile = CONFFILE;
   opt_ctx = g_option_context_new ("");
@@ -74,20 +97,73 @@ int main (int argc, char** argv)
   
   keyfile =  g_key_file_new ();
 
-  g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL);
+  error = NULL;
+  if (g_key_file_load_from_file (keyfile, configfile,
+                                 G_KEY_FILE_NONE, &error) == FALSE)
+    {
+      fprintf (stderr, "Could not load configuration file %s: %s.\n",
+               configfile, error->message);
+      g_error_free (error);
+      exit (1);
+    }
+
+  error = NULL;
+  conf_address = g_key_file_get_string (keyfile, "global", "address",
+                                        &error);
+  if (conf_address == NULL && error != NULL)
+    {
+      conf_address = g_strdup ("0.0.0.0");
+      g_error_free (error);
+    }
+  error = NULL;
+  port = g_key_file_get_string (keyfile, "global", "port", &error);
+  if (port == NULL && error != NULL)
+    {
+      port = g_strdup ("110");
+      g_error_free (error);
+    }
+  error = NULL;
+  server_address = g_key_file_get_string (keyfile, "global", "server",
+                                          &error);
+  if (server_address == NULL && error != NULL)
+    {
+      server_address = g_strdup ("127.0.0.1");
+      g_error_free (error);
+    }
+
+  server_port = g_key_file_get_string (keyfile, "global", "server_port",
+                                       &error);
+  if (server_port == NULL && error != NULL)
+    {
+      server_port = g_strdup ("995");
+      g_error_free (error);
+    }
 
-  conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
-  port = g_key_file_get_integer (keyfile, "global", "port", NULL);
+  pop_address.server = server_address;
+  pop_address.port = server_port;
 
-  g_message ("Listen address is %s:%d.", conf_address, port);
+  server_fd = hc_tcp_server (port);
+  if (server_fd < 0)
+    {
+      fprintf (stderr, "Could not create server.\n");
+      exit (1);
+    }
+  hc_server_add_watch (server_fd, new_client, &pop_address);
 
-  inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
-  gnet_server_new (inetaddr, port, new_client, NULL);
+  g_message ("Listening at %s:%s.", conf_address, port);
 
   daemon (0, 0);
 
+  g_free (conf_address);
+  g_free (port);
+
   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
 
+  gnutls_global_deinit ();
+
+  g_free (server_address);
+  g_free (server_port);
+
   return 0;
 
 }