Check for driver methods before calling them and reset them on close.
[cascardo/rnetproxy.git] / popproxy.c
index 7f4f5a3..99a2352 100644 (file)
@@ -28,7 +28,6 @@
 #include "log.h"
 #include "nethook.h"
 #include "null.h"
-#include "ssl.h"
 #include "pop.h"
 
 #include "hcconn.h"
@@ -42,10 +41,11 @@ struct pop_address
   char *port;
 };
 
-void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data)
+static void
+new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
 {
   HCConn *conn;
-  net_hook_thook;
+  net_hook_t *hook;
   struct pop_address *address = data;
   if (fd < 0)
     {
@@ -54,54 +54,61 @@ void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data)
     }
   g_message ("Received connection from %s.",
              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
-  conn = hc_conn_new (fd, NULL, NULL);
-  hook = ssl_hook_new (conn, address->server, address->port);
+  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);
 }
 
-static gchar* configfile;
+static gchar *configfile;
+static gboolean foreground;
 
 static GOptionEntry opt_entries[] =
   {
     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
       "Configuration file location", "file" },
+    { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
+      "Run in foreground", 0 },
     { NULL }
   };
 
-int main (int argc, char** argv)
+int main (int argc, char **argv)
 {
 
-  GOptionContextopt_ctx;
+  GOptionContext *opt_ctx;
   GKeyFile *keyfile;
   GError *error;
   int server_fd;
-  gcharconf_address;
-  gcharport;
+  gchar *conf_address;
+  gchar *port;
   gchar *server_address;
   gchar *server_port;
   struct pop_address pop_address;
 
   gnutls_global_init ();
-  pop_log_init ();
 
   configfile = CONFFILE;
   opt_ctx = g_option_context_new ("");
   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
-  if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
+
+  error = NULL;
+  if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
     {
-      g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
-            "Could not parse command line options.");
+      g_critical ("Could not parse command line options: %s.",
+                  error->message);
+      g_error_free (error);
+      exit (1);
     }
   g_option_context_free (opt_ctx);
   
-  keyfile =  g_key_file_new ();
+  keyfile = g_key_file_new ();
 
   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_critical ("Could not load configuration file %s: %s.",
+                  configfile, error->message);
       g_error_free (error);
       exit (1);
     }
@@ -129,7 +136,7 @@ int main (int argc, char** argv)
       server_address = g_strdup ("127.0.0.1");
       g_error_free (error);
     }
-
+  error = NULL;
   server_port = g_key_file_get_string (keyfile, "global", "server_port",
                                        &error);
   if (server_port == NULL && error != NULL)
@@ -144,14 +151,17 @@ int main (int argc, char** argv)
   server_fd = hc_tcp_server (port);
   if (server_fd < 0)
     {
-      fprintf (stderr, "Could not create server.\n");
+      g_critical ("Could not create server.");
       exit (1);
     }
   hc_server_add_watch (server_fd, new_client, &pop_address);
 
+  pop_log_init ();
+
   g_message ("Listening at %s:%s.", conf_address, port);
 
-  daemon (0, 0);
+  if (!foreground)
+    daemon (0, 0);
 
   g_free (conf_address);
   g_free (port);