2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
25 #include <gnutls/gnutls.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
32 #include "tcp_connect.h"
34 #define CONFFILE SYSCONFDIR "/popproxy.conf"
43 server_conn_new (char *server, char *port)
48 fd = hc_tcp_connect (server, port);
51 g_warning ("Could not connect to server at %s:%s.", server, port);
54 conn = hc_conn_new (NULL, NULL);
55 ssl_conn = hc_conn_new (NULL, NULL);
56 hc_conn_set_driver_channel (conn, fd);
57 hc_conn_set_driver_ssl (ssl_conn, conn);
62 push_other (HCConn *conn, HCEvent event, gpointer data)
69 while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0)
70 hc_conn_write (data, buffer, r);
80 new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data)
84 struct pop_address *address = data;
87 g_critical ("Server has received an error event.");
90 g_message ("Received connection from %s.",
91 inet_ntoa (((struct sockaddr_in *) addr)->sin_addr));
93 server_conn = server_conn_new (address->server, address->port);
94 if (server_conn == NULL)
99 conn = hc_conn_new (NULL, NULL);
100 hc_conn_set_driver_channel (conn, fd);
102 hc_conn_set_callback (conn, push_other, server_conn);
103 hc_conn_set_callback (server_conn, push_other, conn);
107 static gchar *configfile;
108 static gboolean foreground;
110 static GOptionEntry opt_entries[] =
112 { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
113 "Configuration file location", "file" },
114 { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground,
115 "Run in foreground", 0 },
119 int main (int argc, char **argv)
122 GOptionContext *opt_ctx;
128 gchar *server_address;
130 struct pop_address pop_address;
132 gnutls_global_init ();
134 configfile = CONFFILE;
135 opt_ctx = g_option_context_new ("");
136 g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
139 if (!g_option_context_parse (opt_ctx, &argc, &argv, &error))
141 g_critical ("Could not parse command line options: %s.",
143 g_error_free (error);
146 g_option_context_free (opt_ctx);
148 keyfile = g_key_file_new ();
151 if (g_key_file_load_from_file (keyfile, configfile,
152 G_KEY_FILE_NONE, &error) == FALSE)
154 g_critical ("Could not load configuration file %s: %s.",
155 configfile, error->message);
156 g_error_free (error);
161 conf_address = g_key_file_get_string (keyfile, "global", "address",
163 if (conf_address == NULL && error != NULL)
165 conf_address = g_strdup ("0.0.0.0");
166 g_error_free (error);
169 port = g_key_file_get_string (keyfile, "global", "port", &error);
170 if (port == NULL && error != NULL)
172 port = g_strdup ("110");
173 g_error_free (error);
176 server_address = g_key_file_get_string (keyfile, "global", "server",
178 if (server_address == NULL && error != NULL)
180 server_address = g_strdup ("127.0.0.1");
181 g_error_free (error);
184 server_port = g_key_file_get_string (keyfile, "global", "server_port",
186 if (server_port == NULL && error != NULL)
188 server_port = g_strdup ("995");
189 g_error_free (error);
192 pop_address.server = server_address;
193 pop_address.port = server_port;
195 server_fd = hc_tcp_server (port);
198 g_critical ("Could not create server.");
201 hc_server_add_watch (server_fd, new_client, &pop_address);
205 g_message ("Listening at %s:%s.", conf_address, port);
210 g_free (conf_address);
213 g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
215 gnutls_global_deinit ();
217 g_free (server_address);
218 g_free (server_port);