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>
35 #include "tcp_connect.h"
37 #define CONFFILE SYSCONFDIR "/popproxy.conf"
45 void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data)
49 struct pop_address *address = data;
52 g_critical ("Server has received an error event.");
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 = ssl_hook_new (conn, address->server, address->port);
63 static gchar* configfile;
65 static GOptionEntry opt_entries[] =
67 { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile,
68 "Configuration file location", "file" },
72 int main (int argc, char** argv)
75 GOptionContext* opt_ctx;
81 gchar *server_address;
83 struct pop_address pop_address;
85 gnutls_global_init ();
88 configfile = CONFFILE;
89 opt_ctx = g_option_context_new ("");
90 g_option_context_add_main_entries (opt_ctx, opt_entries, NULL);
91 if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL))
93 g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL,
94 "Could not parse command line options.");
96 g_option_context_free (opt_ctx);
98 keyfile = g_key_file_new ();
101 if (g_key_file_load_from_file (keyfile, configfile,
102 G_KEY_FILE_NONE, &error) == FALSE)
104 fprintf (stderr, "Could not load configuration file %s: %s.\n",
105 configfile, error->message);
106 g_error_free (error);
111 conf_address = g_key_file_get_string (keyfile, "global", "address",
113 if (conf_address == NULL && error != NULL)
115 conf_address = g_strdup ("0.0.0.0");
116 g_error_free (error);
119 port = g_key_file_get_string (keyfile, "global", "port", &error);
120 if (port == NULL && error != NULL)
122 port = g_strdup ("110");
123 g_error_free (error);
126 server_address = g_key_file_get_string (keyfile, "global", "server",
128 if (server_address == NULL && error != NULL)
130 server_address = g_strdup ("127.0.0.1");
131 g_error_free (error);
134 server_port = g_key_file_get_string (keyfile, "global", "server_port",
136 if (server_port == NULL && error != NULL)
138 server_port = g_strdup ("995");
139 g_error_free (error);
142 pop_address.server = server_address;
143 pop_address.port = server_port;
145 server_fd = hc_tcp_server (port);
148 fprintf (stderr, "Could not create server.\n");
151 hc_server_add_watch (server_fd, new_client, &pop_address);
153 g_message ("Listening at %s:%s.", conf_address, port);
157 g_free (conf_address);
160 g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE));
162 gnutls_global_deinit ();
164 g_free (server_address);
165 g_free (server_port);