Close the other end of the connection too.
[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 <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <gnutls/gnutls.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include "log.h"
29 #include "nethook.h"
30 #include "null.h"
31 #include "pop.h"
32
33 #include "hcconn.h"
34 #include "tcp_connect.h"
35
36 #define CONFFILE SYSCONFDIR "/popproxy.conf"
37
38 struct pop_address
39 {
40   char *server;
41   char *port;
42 };
43
44 static void
45 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
46 {
47   HCConn *conn;
48   net_hook_t *hook;
49   struct pop_address *address = data;
50   if (fd < 0)
51     {
52       g_critical ("Server has received an error event.");
53       return;
54     }
55   g_message ("Received connection from %s.",
56              inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
57   conn = hc_conn_new (NULL, NULL);
58   hc_conn_set_driver_channel (conn, fd);
59   hook = null_hook_new (conn, address->server, address->port);
60   pop_hook_new (hook);
61 }
62
63 static gchar *configfile;
64 static gboolean foreground;
65
66 static GOptionEntry opt_entries[] =
67   {
68     { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
69       "Configuration file location", "file" },
70     { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
71       "Run in foreground", 0 },
72     { NULL }
73   };
74
75 int main (int argc, char **argv)
76 {
77
78   GOptionContext *opt_ctx;
79   GKeyFile *keyfile;
80   GError *error;
81   int server_fd;
82   gchar *conf_address;
83   gchar *port;
84   gchar *server_address;
85   gchar *server_port;
86   struct pop_address pop_address;
87
88   gnutls_global_init ();
89
90   configfile = CONFFILE;
91   opt_ctx = g_option_context_new ("");
92   g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
93
94   error = NULL;
95   if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
96     {
97       g_critical ("Could not parse command line options: %s.",
98                   error->message);
99       g_error_free (error);
100       exit (1);
101     }
102   g_option_context_free (opt_ctx);
103   
104   keyfile = g_key_file_new ();
105
106   error = NULL;
107   if (g_key_file_load_from_file (keyfile, configfile,
108                                  G_KEY_FILE_NONE, &error) == FALSE)
109     {
110       g_critical ("Could not load configuration file %s: %s.",
111                   configfile, error->message);
112       g_error_free (error);
113       exit (1);
114     }
115
116   error = NULL;
117   conf_address = g_key_file_get_string (keyfile, "global", "address",
118                                         &error);
119   if (conf_address == NULL && error != NULL)
120     {
121       conf_address = g_strdup ("0.0.0.0");
122       g_error_free (error);
123     }
124   error = NULL;
125   port = g_key_file_get_string (keyfile, "global", "port", &error);
126   if (port == NULL && error != NULL)
127     {
128       port = g_strdup ("110");
129       g_error_free (error);
130     }
131   error = NULL;
132   server_address = g_key_file_get_string (keyfile, "global", "server",
133                                           &error);
134   if (server_address == NULL && error != NULL)
135     {
136       server_address = g_strdup ("127.0.0.1");
137       g_error_free (error);
138     }
139   error = NULL;
140   server_port = g_key_file_get_string (keyfile, "global", "server_port",
141                                        &error);
142   if (server_port == NULL && error != NULL)
143     {
144       server_port = g_strdup ("995");
145       g_error_free (error);
146     }
147
148   pop_address.server = server_address;
149   pop_address.port = server_port;
150
151   server_fd = hc_tcp_server (port);
152   if (server_fd < 0)
153     {
154       g_critical ("Could not create server.");
155       exit (1);
156     }
157   hc_server_add_watch (server_fd, new_client, &pop_address);
158
159   pop_log_init ();
160
161   g_message ("Listening at %s:%s.", conf_address, port);
162
163   if (!foreground)
164     daemon (0, 0);
165
166   g_free (conf_address);
167   g_free (port);
168
169   g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
170
171   gnutls_global_deinit ();
172
173   g_free (server_address);
174   g_free (server_port);
175
176   return 0;
177
178 }