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