Documentation files
[cascardo/rnetproxy.git] / improxy.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 **  
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **  
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 ** GNU General Public License for more details.
13 **  
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 **  
18 */
19
20 #include <glib.h>
21 #include <gnet.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include "log.h"
25 #include "nethook.h"
26 #include "proto_detect.h"
27
28 #define CONFFILE SYSCONFDIR "/improxy.conf"
29
30 void new_client (GServer* server, GConn* conn, gpointer data)
31 {
32   net_hook_t* hook;
33   if (conn == NULL)
34     {
35       g_critical ("Server has received an error event.");
36       return;
37     }
38   g_message ("Received connection from %s.", conn->hostname);
39   hook = proto_detect_new (conn);
40   gnet_conn_read (conn);
41 }
42
43 static gchar* configfile;
44
45 static GOptionEntry opt_entries[] =
46   {
47     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
48       "Configuration file location", "file" },
49     { NULL }
50   };
51
52 int main (int argc, char** argv)
53 {
54
55   GOptionContext* opt_ctx;
56   GKeyFile *keyfile;
57   GInetAddr* inetaddr;
58   gchar* conf_address;
59   gint port;
60
61   daemon (0, 0);
62
63   gnet_init ();
64   im_log_init ();
65
66   configfile = CONFFILE;
67   opt_ctx = g_option_context_new ("");
68   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
69   if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
70     {
71       g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
72              "Could not parse command line options.");
73     }
74   g_option_context_free (opt_ctx);
75   
76   keyfile =  g_key_file_new ();
77
78   g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL);
79
80   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
81   port = g_key_file_get_integer (keyfile, "global", "port", NULL);
82
83   g_message ("Listen address is %s:%d.", conf_address, port);
84
85   inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
86   gnet_server_new (inetaddr, port, new_client, NULL);
87
88   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
89
90   return 0;
91
92 }