f252dc30392cbd7776d6ea532b7b9f5afff25585
[cascardo/rnetproxy.git] / improxy.c
1 #include <glib.h>
2 #include <gnet.h>
3 #include <stdio.h>
4 #include "log.h"
5 #include "nethook.h"
6 #include "proto_detect.h"
7
8 #define CONFFILE SYSCONFDIR "/improxy.conf"
9
10 void new_client (GServer* server, GConn* conn, gpointer data)
11 {
12   net_hook_t* hook;
13   if (conn == NULL)
14     {
15       g_critical ("Server has received an error event.");
16       return;
17     }
18   g_message ("Received connection from %s.", conn->hostname);
19   hook = proto_detect_new (conn);
20   gnet_conn_read (conn);
21 }
22
23 static gchar* configfile;
24
25 static GOptionEntry opt_entries[] =
26   {
27     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
28       "Configuration file location", "file" },
29     { NULL }
30   };
31
32 int main (int argc, char** argv)
33 {
34
35   GOptionContext* opt_ctx;
36   GKeyFile *keyfile;
37   GInetAddr* inetaddr;
38   gchar* conf_address;
39   gint port;
40
41   gnet_init ();
42   im_log_init ();
43
44   configfile = CONFFILE;
45   opt_ctx = g_option_context_new ("");
46   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
47   if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
48     {
49       g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
50              "Could not parse command line options.");
51     }
52   g_option_context_free (opt_ctx);
53   
54   keyfile =  g_key_file_new ();
55
56   g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL);
57
58   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
59   port = g_key_file_get_integer (keyfile, "global", "port", NULL);
60
61   g_message ("Listen address is %s:%d.", conf_address, port);
62
63   inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
64   gnet_server_new (inetaddr, port, new_client, NULL);
65
66   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
67
68   return 0;
69
70 }