Connection events are handled by hooks
[cascardo/rnetproxy.git] / improxy.c
index 6e15f1e..6ab43c0 100644 (file)
--- a/improxy.c
+++ b/improxy.c
@@ -1,33 +1,29 @@
 #include <glib.h>
 #include <gnet.h>
 #include <stdio.h>
+#include "log.h"
+#include "nethook.h"
+#include "proto_detect.h"
+
+#define CONFFILE SYSCONFDIR "/improxy.conf"
 
 void client_event (GConn* conn, GConnEvent* event, gpointer data)
 {
-  GConn* server;
-  server = (GConn*) data;
+  net_hook_t* hook;
+  hook = (net_hook_t*) data;
   switch (event->type)
     {
     case GNET_CONN_CONNECT:
-      g_debug ("Connected to localhost:80.");
-      gnet_conn_read (conn);
+      hook->connect (hook);
       break;
     case GNET_CONN_READ:
-      if (server == NULL)
-       {
-         g_debug ("Establishing connection to localhost:80.");
-         server = gnet_conn_new ("127.0.0.1", 80, client_event, (gpointer) conn);
-         gnet_conn_connect (server);
-         gnet_conn_set_callback (conn, client_event, (gpointer) server);
-       }
-      gnet_conn_write (server, event->buffer, event->length);
-      gnet_conn_read (conn);
+      hook->read (hook, event->buffer, event->length);
       break;
     case GNET_CONN_WRITE:
+      hook->write (hook);
       break;
     case GNET_CONN_CLOSE:
-      gnet_conn_unref (server);
-      gnet_conn_unref (conn);
+      hook->close (hook);
       break;
     default:
       g_warning ("Received an unexpected client event.");
@@ -37,29 +33,52 @@ void client_event (GConn* conn, GConnEvent* event, gpointer data)
 
 void new_client (GServer* server, GConn* conn, gpointer data)
 {
+  net_hook_t* hook;
   if (conn == NULL)
     {
       g_critical ("Server has received an error event.");
       return;
     }
   g_message ("Received connection from %s.", conn->hostname);
-  gnet_conn_set_callback (conn, client_event, NULL);
+  hook = proto_detect_new (conn);
+  gnet_conn_set_callback (conn, client_event, hook);
   gnet_conn_read (conn);
 }
 
-int main ()
+static gchar* configfile;
+
+static GOptionEntry opt_entries[] =
+  {
+    { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
+      "Configuration file location", "file" },
+    { NULL }
+  };
+
+int main (int argc, char** argv)
 {
 
+  GOptionContext* opt_ctx;
   GKeyFile *keyfile;
   GInetAddr* inetaddr;
   gchar* conf_address;
   gint port;
 
   gnet_init ();
+  im_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))
+    {
+      g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
+            "Could not parse command line options.");
+    }
+  g_option_context_free (opt_ctx);
+  
   keyfile =  g_key_file_new ();
 
-  g_key_file_load_from_file (keyfile, "improxy.conf", G_KEY_FILE_NONE, NULL);
+  g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL);
 
   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
   port = g_key_file_get_integer (keyfile, "global", "port", NULL);