X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=popproxy.c;h=3b237737acf99eb9a887e0dc7d3cee02ce2316d5;hb=9e01543ef65baeb4e1d477b61391aaef051141e1;hp=d43c9d6f06a38eb89023932c56a0b8382eda080e;hpb=e15c63276b77ae8fe7308409da31804383b4dba2;p=cascardo%2Frnetproxy.git diff --git a/popproxy.c b/popproxy.c index d43c9d6..3b23773 100644 --- a/popproxy.c +++ b/popproxy.c @@ -19,75 +19,204 @@ */ #include -#include #include +#include #include +#include +#include +#include #include "log.h" -#include "nethook.h" -#include "proto_detect.h" +#include "pop.h" + +#include "hcconn.h" +#include "tcp_connect.h" #define CONFFILE SYSCONFDIR "/popproxy.conf" -void new_client (GServer* server, GConn* conn, gpointer data) +struct pop_address +{ + char *server; + char *port; +}; + +static HCConn * +server_conn_new (char *server, char *port) +{ + int fd; + HCConn *conn; + HCConn *ssl_conn; + fd = hc_tcp_connect (server, port); + if (fd < 0) + { + g_warning ("Could not connect to server at %s:%s.", server, port); + return NULL; + } + conn = hc_conn_new (NULL, NULL); + ssl_conn = hc_conn_new (NULL, NULL); + hc_conn_set_driver_channel (conn, fd); + hc_conn_set_driver_ssl (ssl_conn, conn); + return ssl_conn; +} + +static void +push_other (HCConn *conn, HCEvent event, gpointer data) +{ + char buffer[4096]; + int r; + switch (event) + { + case HC_EVENT_READ: + while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0) + hc_conn_write (data, buffer, r); + break; + case HC_EVENT_CLOSE: + hc_conn_close (conn); + hc_conn_close (data); + break; + } +} + +static void +new_client (int fd, struct sockaddr *addr, socklen_t saddr, gpointer data) { - net_hook_t* hook; - if (conn == NULL) + HCConn *conn; + HCConn *server_conn; + struct pop_address *address = data; + if (fd < 0) { g_critical ("Server has received an error event."); return; } - g_message ("Received connection from %s.", conn->hostname); - hook = proto_detect_new (conn); - gnet_conn_read (conn); + g_message ("Received connection from %s.", + inet_ntoa (((struct sockaddr_in *) addr)->sin_addr)); + + server_conn = server_conn_new (address->server, address->port); + if (server_conn == NULL) + { + return; + } + + conn = hc_conn_new (NULL, NULL); + hc_conn_set_driver_channel (conn, fd); + + hc_conn_set_callback (conn, push_other, server_conn); + hc_conn_set_callback (server_conn, push_other, conn); + } -static gchar* configfile; +static gchar *configfile; +static gboolean foreground; static GOptionEntry opt_entries[] = { { "config-file", 'c', 0, G_OPTION_ARG_FILENAME, &configfile, "Configuration file location", "file" }, + { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, + "Run in foreground", 0 }, { NULL } }; -int main (int argc, char** argv) +int main (int argc, char **argv) { - GOptionContext* opt_ctx; + GOptionContext *opt_ctx; GKeyFile *keyfile; - GInetAddr* inetaddr; - gchar* conf_address; - gint port; + GError *error; + int server_fd; + gchar *conf_address; + gchar *port; + gchar *server_address; + gchar *server_port; + struct pop_address pop_address; - gnet_init (); - pop_log_init (); + gnutls_global_init (); configfile = CONFFILE; opt_ctx = g_option_context_new (""); g_option_context_add_main_entries (opt_ctx, opt_entries, NULL); - if (!g_option_context_parse (opt_ctx, &argc, &argv, NULL)) + + error = NULL; + if (!g_option_context_parse (opt_ctx, &argc, &argv, &error)) { - g_log (NULL, G_LOG_LEVEL_WARNING | G_LOG_FLAG_FATAL, - "Could not parse command line options."); + g_critical ("Could not parse command line options: %s.", + error->message); + g_error_free (error); + exit (1); } g_option_context_free (opt_ctx); - keyfile = g_key_file_new (); + keyfile = g_key_file_new (); + + error = NULL; + if (g_key_file_load_from_file (keyfile, configfile, + G_KEY_FILE_NONE, &error) == FALSE) + { + g_critical ("Could not load configuration file %s: %s.", + configfile, error->message); + g_error_free (error); + exit (1); + } + + error = NULL; + conf_address = g_key_file_get_string (keyfile, "global", "address", + &error); + if (conf_address == NULL && error != NULL) + { + conf_address = g_strdup ("0.0.0.0"); + g_error_free (error); + } + error = NULL; + port = g_key_file_get_string (keyfile, "global", "port", &error); + if (port == NULL && error != NULL) + { + port = g_strdup ("110"); + g_error_free (error); + } + error = NULL; + server_address = g_key_file_get_string (keyfile, "global", "server", + &error); + if (server_address == NULL && error != NULL) + { + server_address = g_strdup ("127.0.0.1"); + g_error_free (error); + } + error = NULL; + server_port = g_key_file_get_string (keyfile, "global", "server_port", + &error); + if (server_port == NULL && error != NULL) + { + server_port = g_strdup ("995"); + g_error_free (error); + } + + pop_address.server = server_address; + pop_address.port = server_port; - g_key_file_load_from_file (keyfile, configfile, G_KEY_FILE_NONE, NULL); + server_fd = hc_tcp_server (port); + if (server_fd < 0) + { + g_critical ("Could not create server."); + exit (1); + } + hc_server_add_watch (server_fd, new_client, &pop_address); - conf_address = g_key_file_get_string (keyfile, "global", "address", NULL); - port = g_key_file_get_integer (keyfile, "global", "port", NULL); + pop_log_init (); - g_message ("Listen address is %s:%d.", conf_address, port); + g_message ("Listening at %s:%s.", conf_address, port); - inetaddr = gnet_inetaddr_new_nonblock (conf_address, port); - gnet_server_new (inetaddr, port, new_client, NULL); + if (!foreground) + daemon (0, 0); - daemon (0, 0); + g_free (conf_address); + g_free (port); g_main_loop_run (g_main_loop_new (g_main_context_default (), TRUE)); + gnutls_global_deinit (); + + g_free (server_address); + g_free (server_port); + return 0; }