Program messages are logged to syslog
[cascardo/rnetproxy.git] / improxy.c
1 #include <glib.h>
2 #include <gnet.h>
3 #include <stdio.h>
4 #include "log.h"
5
6 void client_event (GConn* conn, GConnEvent* event, gpointer data)
7 {
8   GConn* server;
9   server = (GConn*) data;
10   switch (event->type)
11     {
12     case GNET_CONN_CONNECT:
13       g_debug ("Connected to localhost:80.");
14       gnet_conn_read (conn);
15       break;
16     case GNET_CONN_READ:
17       if (server == NULL)
18         {
19           g_debug ("Establishing connection to localhost:80.");
20           server = gnet_conn_new ("127.0.0.1", 80, client_event, (gpointer) conn);
21           gnet_conn_connect (server);
22           gnet_conn_set_callback (conn, client_event, (gpointer) server);
23         }
24       gnet_conn_write (server, event->buffer, event->length);
25       gnet_conn_read (conn);
26       break;
27     case GNET_CONN_WRITE:
28       break;
29     case GNET_CONN_CLOSE:
30       gnet_conn_unref (server);
31       gnet_conn_unref (conn);
32       break;
33     default:
34       g_warning ("Received an unexpected client event.");
35       break;
36     }
37 }
38
39 void new_client (GServer* server, GConn* conn, gpointer data)
40 {
41   if (conn == NULL)
42     {
43       g_critical ("Server has received an error event.");
44       return;
45     }
46   g_message ("Received connection from %s.", conn->hostname);
47   gnet_conn_set_callback (conn, client_event, NULL);
48   gnet_conn_read (conn);
49 }
50
51 int main ()
52 {
53
54   GKeyFile *keyfile;
55   GInetAddr* inetaddr;
56   gchar* conf_address;
57   gint port;
58
59   gnet_init ();
60   im_log_init ();
61
62   keyfile =  g_key_file_new ();
63
64   g_key_file_load_from_file (keyfile, "improxy.conf", G_KEY_FILE_NONE, NULL);
65
66   conf_address = g_key_file_get_string (keyfile, "global", "address", NULL);
67   port = g_key_file_get_integer (keyfile, "global", "port", NULL);
68
69   g_message ("Listen address is %s:%d.", conf_address, port);
70
71   inetaddr = gnet_inetaddr_new_nonblock (conf_address, port);
72   gnet_server_new (inetaddr, port, new_client, NULL);
73
74   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
75
76   return 0;
77
78 }