From fba83c023fada1b542898fba73cd93f4e46162f7 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 2 Jul 2009 14:49:13 -0300 Subject: [PATCH] Change from GNet to HCConn. Now, we use our own implementation of TCP connection and server, instead of using GNet. There is some missing pieces and it doesn't work right now, but it's easier to hack on and we don't depend on this library anymore. --- Makefile.am | 1 + configure.ac | 6 +++--- nethook.c | 21 ++++++++++----------- nethook.h | 7 ++++--- null.c | 15 ++++++--------- null.h | 4 ++-- pop.c | 4 +--- pop.h | 1 - popproxy.c | 49 ++++++++++++++++++++++++++++--------------------- popproxy.conf | 2 +- ssl.c | 9 ++++----- ssl.h | 6 +++--- ssl_server.c | 21 ++++++++------------- 13 files changed, 71 insertions(+), 75 deletions(-) diff --git a/Makefile.am b/Makefile.am index 22dae9e..8d54ad8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,6 @@ bin_PROGRAMS = popproxy ppmanager popproxy_SOURCES = popproxy.c log.c log.h nethook.c nethook.h \ + iochannel.c iochannel.h tcp_connect.h tcp_connect.c tcp_server.c \ null.c null.h ssl.c ssl.h \ ssl_server.c pop.c pop.h usermap.c usermap.h dist_sysconf_DATA = popproxy.conf diff --git a/configure.ac b/configure.ac index 2a6072f..3c5b42b 100644 --- a/configure.ac +++ b/configure.ac @@ -2,11 +2,11 @@ AC_INIT(popproxy,0.1,cascardo@holoscopio.com) AM_INIT_AUTOMAKE(AC_PACKAGE_NAME,AC_PACKAGE_VERSION) AC_PROG_CC AC_PROG_INSTALL -AM_PATH_GNET_2_0(,,AC_MSG_ERROR(GNet not found)) +AM_PATH_GLIB_2_0(,,AC_MSG_ERROR(GLib not found)) PKG_CHECK_MODULES(GNUTLS, gnutls >= 1.4.0, , AC_MSG_ERROR(Could not find gnutls)) PKG_CHECK_MODULES(QDBM, qdbm, , AC_MSG_ERROR(Could not find qdbm)) -LIBS="$GNUTLS_LIBS $GNET_LIBS $QDBM_LIBS $LIBS" -CFLAGS="$GNUTLS_CFLAGS $GNET_CFLAGS $QDBM_CFLAGS $CFLAGS" +LIBS="$GLIB_LIBS $GNUTLS_LIBS $QDBM_LIBS $LIBS" +CFLAGS="$GLIB_CFLAGS $GNUTLS_CFLAGS $QDBM_CFLAGS $CFLAGS" if test "${sysconfdir}x" = '${prefix}/etcx'; then if test "${prefix}x" = 'NONEx'; then AC_DEFINE_UNQUOTED(SYSCONFDIR, "${ac_default_prefix}/etc") diff --git a/nethook.c b/nethook.c index 2ed9aeb..8380990 100644 --- a/nethook.c +++ b/nethook.c @@ -17,26 +17,25 @@ ** */ -#include #include "nethook.h" -void nethook_event (GConn* conn, GConnEvent* event, gpointer data) +void +nethook_event (HCConn* conn, HCEvent event, gpointer data) { + char buffer[4096]; + int r; net_hook_t* hook; hook = (net_hook_t*) data; - switch (event->type) + switch (event) { - case GNET_CONN_CONNECT: + case HC_EVENT_CONNECT: hook->connect (hook); break; - case GNET_CONN_READ: - hook->read (hook, event->buffer, event->length); - gnet_conn_read (conn); + case HC_EVENT_READ: + while ((r = hc_conn_read (conn, buffer, sizeof (buffer))) > 0) + hook->read (hook, buffer, r); break; - case GNET_CONN_WRITE: - hook->write (hook); - break; - case GNET_CONN_CLOSE: + case HC_EVENT_CLOSE: hook->close (hook); break; default: diff --git a/nethook.h b/nethook.h index ec1a60f..e3178e4 100644 --- a/nethook.h +++ b/nethook.h @@ -20,7 +20,8 @@ #ifndef NET_HOOK_H #define NET_HOOK_H -#include +#include +#include "iochannel.h" typedef struct _net_hook_t net_hook_t; typedef void (*net_connect) (net_hook_t*); @@ -30,7 +31,7 @@ typedef void (*net_read) (net_hook_t*, gchar*, size_t); struct _net_hook_t { - GConn* conn; + HCConn* conn; net_hook_t* peer; gboolean server; net_connect connect; @@ -40,6 +41,6 @@ struct _net_hook_t gpointer data; }; -void nethook_event (GConn*, GConnEvent*, gpointer); +void nethook_event (HCConn*, HCEvent, gpointer); #endif diff --git a/null.c b/null.c index fd4de84..1ad81c9 100644 --- a/null.c +++ b/null.c @@ -18,7 +18,6 @@ ** */ -#include #include #include "null.h" @@ -31,9 +30,9 @@ static void null_close (net_hook_t* hook) if (hook->peer) { hook->peer->peer = NULL; - gnet_conn_disconnect (hook->peer->conn); + hc_conn_close (hook->peer->conn); } - gnet_conn_delete (hook->conn); + hc_conn_close (hook->conn); g_slice_free (net_hook_t, hook); } @@ -43,7 +42,7 @@ static void null_write (net_hook_t* hook) static void null_read (net_hook_t* hook, gchar* buffer, size_t len) { - gnet_conn_write (hook->peer->conn, buffer, len); + hc_conn_write (hook->peer->conn, buffer, len); } static void null_error (net_hook_t* hook) @@ -55,7 +54,6 @@ static net_hook_t* null_server_hook_new (net_hook_t* client_hook, char *server) { net_hook_t* hook; hook = g_slice_new (net_hook_t); - hook->conn = gnet_conn_new (server, 110, nethook_event, hook); hook->peer = client_hook; hook->server = TRUE; hook->connect = null_connect; @@ -63,12 +61,11 @@ static net_hook_t* null_server_hook_new (net_hook_t* client_hook, char *server) hook->write = null_write; hook->read = null_read; hook->data = NULL; - gnet_conn_connect (hook->conn); - gnet_conn_read (hook->conn); + hook->conn = hc_conn_new (hc_tcp_connect (server, "110"), nethook_event, hook); return hook; } -net_hook_t* null_hook_new (GConn* conn, char *server) +net_hook_t* null_hook_new (HCConn* conn, char *server) { net_hook_t* hook; hook = g_slice_new (net_hook_t); @@ -81,7 +78,7 @@ net_hook_t* null_hook_new (GConn* conn, char *server) hook->read = null_read; hook->data = server; hook->peer = null_server_hook_new (hook, server); - gnet_conn_set_callback (hook->conn, nethook_event, hook); + hc_conn_set_callback (hook->conn, nethook_event, hook); return hook; } diff --git a/null.h b/null.h index 0f463f5..790b2c7 100644 --- a/null.h +++ b/null.h @@ -20,10 +20,10 @@ #ifndef NULL_H #define NULL_H -#include #include "nethook.h" +#include "iochannel.h" -net_hook_t* null_hook_new (GConn*, char*); +net_hook_t* null_hook_new (HCConn*, char*); void null_destroy (net_hook_t*); #endif diff --git a/pop.c b/pop.c index 09af8a2..0477815 100644 --- a/pop.c +++ b/pop.c @@ -18,7 +18,6 @@ ** */ -#include #include #include #include "nethook.h" @@ -92,8 +91,7 @@ pop_read (net_hook_t *hook, gchar *buffer, size_t len) { g_message ("Denying access to user %s.", pop->user); pop_destroy (hook); - gnet_conn_disconnect (hook->conn); - hook->close (hook); + hc_conn_close (hook->conn); return; } } diff --git a/pop.h b/pop.h index 02873ff..647fae0 100644 --- a/pop.h +++ b/pop.h @@ -21,7 +21,6 @@ #ifndef POPPROXY_POP_H #define POPPROXY_POP_H -#include #include "nethook.h" net_hook_t* pop_hook_new (net_hook_t *); diff --git a/popproxy.c b/popproxy.c index 6aaf804..27f8a00 100644 --- a/popproxy.c +++ b/popproxy.c @@ -19,38 +19,44 @@ */ #include -#include #include #include #include #include +#include +#include #include "log.h" #include "nethook.h" #include "null.h" #include "ssl.h" #include "pop.h" +#include "iochannel.h" +#include "tcp_connect.h" + #define CONFFILE SYSCONFDIR "/popproxy.conf" struct pop_address { char *server; - int port; + char *port; }; -void new_client (GServer* server, GConn* conn, gpointer data) +void new_client (int fd, struct sockaddr* addr, socklen_t saddr, gpointer data) { + HCConn *conn; net_hook_t* hook; struct pop_address *address = data; - if (conn == NULL) + if (fd < 0) { g_critical ("Server has received an error event."); return; } - g_message ("Received connection from %s.", conn->hostname); + g_message ("Received connection from %s.", + inet_ntoa (((struct sockaddr_in *) addr)->sin_addr)); + conn = hc_conn_new (fd, NULL, NULL); hook = ssl_hook_new (conn, address->server, address->port); pop_hook_new (hook); - gnet_conn_read (conn); } static gchar* configfile; @@ -67,16 +73,15 @@ int main (int argc, char** argv) GOptionContext* opt_ctx; GKeyFile *keyfile; - GInetAddr* inetaddr; GError *error; + int server_fd; gchar* conf_address; - gint port; + gchar* port; gchar *server_address; - gint server_port; + gchar *server_port; struct pop_address pop_address; gnutls_global_init (); - gnet_init (); pop_log_init (); configfile = CONFFILE; @@ -110,10 +115,10 @@ int main (int argc, char** argv) g_error_free (error); } error = NULL; - port = g_key_file_get_integer (keyfile, "global", "port", &error); - if (port == 0 && error != NULL) + port = g_key_file_get_string (keyfile, "global", "port", &error); + if (port == NULL && error != NULL) { - port = 110; + port = g_strdup ("110"); g_error_free (error); } error = NULL; @@ -125,36 +130,38 @@ int main (int argc, char** argv) g_error_free (error); } - server_port = g_key_file_get_integer (keyfile, "global", "server_port", - &error); - if (server_port == 0 && error != NULL) + server_port = g_key_file_get_string (keyfile, "global", "server_port", + &error); + if (server_port == NULL && error != NULL) { - port = 995; + server_port = g_strdup ("995"); g_error_free (error); } pop_address.server = server_address; pop_address.port = server_port; - inetaddr = gnet_inetaddr_new_nonblock (conf_address, port); - if (gnet_server_new (inetaddr, port, - new_client, &pop_address) == NULL) + server_fd = hc_tcp_server (port); + if (server_fd < 0) { fprintf (stderr, "Could not create server.\n"); exit (1); } + hc_server_add_watch (server_fd, new_client, &pop_address); - g_message ("Listening at %s:%d.", conf_address, port); + g_message ("Listening at %s:%s.", conf_address, port); 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; diff --git a/popproxy.conf b/popproxy.conf index e61a0f8..90d8b27 100644 --- a/popproxy.conf +++ b/popproxy.conf @@ -1,5 +1,5 @@ [global] address = 0.0.0.0 -port = 110 +port = 1100 server = 127.0.0.1 diff --git a/ssl.c b/ssl.c index c1bfc88..299981d 100644 --- a/ssl.c +++ b/ssl.c @@ -19,7 +19,6 @@ */ #include -#include #include #include #include @@ -36,9 +35,9 @@ ssl_close (net_hook_t *hook) if (hook->peer) { hook->peer->peer = NULL; - gnet_conn_disconnect (hook->peer->conn); + hc_conn_close (hook->peer->conn); } - gnet_conn_delete (hook->conn); + hc_conn_close (hook->conn); g_slice_free (net_hook_t, hook); } @@ -60,7 +59,7 @@ ssl_error (net_hook_t *hook) } net_hook_t * -ssl_hook_new (GConn *conn, char *server, int port) +ssl_hook_new (HCConn *conn, char *server, char *port) { net_hook_t *hook; hook = g_slice_new (net_hook_t); @@ -73,7 +72,7 @@ ssl_hook_new (GConn *conn, char *server, int port) hook->read = ssl_read; hook->data = NULL; hook->peer = ssl_server_hook_new (hook, server, port); - gnet_conn_set_callback (hook->conn, nethook_event, hook); + hc_conn_set_callback (hook->conn, nethook_event, hook); return hook; } diff --git a/ssl.h b/ssl.h index 56e577f..ae541c5 100644 --- a/ssl.h +++ b/ssl.h @@ -21,8 +21,8 @@ #ifndef POPPROXY_SSL_H #define POPPROXY_SSL_H -#include #include "nethook.h" +#include "iochannel.h" struct ssl_data { @@ -32,9 +32,9 @@ struct ssl_data gboolean handshaking; }; -net_hook_t* ssl_hook_new (GConn*, char*, int); +net_hook_t* ssl_hook_new (HCConn*, char*, char *); void ssl_destroy (net_hook_t*); -net_hook_t * ssl_server_hook_new (net_hook_t *, char *, int); +net_hook_t * ssl_server_hook_new (net_hook_t *, char *, char *); #endif diff --git a/ssl_server.c b/ssl_server.c index d4f79b6..9fa269d 100644 --- a/ssl_server.c +++ b/ssl_server.c @@ -19,7 +19,6 @@ */ #include -#include #include #include #include @@ -61,11 +60,10 @@ ssl_push (gnutls_transport_ptr_t ptr, const void *buffer, size_t len) int r; if (ssl->handshaking == TRUE) { - g_io_channel_write_chars (hook->conn->iochannel, buffer, len, - &r, NULL); + r = hc_conn_read (hook->conn, buffer, len); return r; } - gnet_conn_write (hook->conn, (void *) buffer, len); + hc_conn_write (hook->conn, (void *) buffer, len); return len; } @@ -77,8 +75,7 @@ ssl_pull (gnutls_transport_ptr_t ptr, void *buffer, size_t len) int r; if (ssl->handshaking == TRUE) { - g_io_channel_read_chars (hook->conn->iochannel, buffer, len, - &r, NULL); + r = hc_conn_read (hook->conn, buffer, len); return r; } if (len > ssl->buffer->len) @@ -126,9 +123,9 @@ ssl_server_close (net_hook_t *hook) if (hook->peer) { hook->peer->peer = NULL; - gnet_conn_disconnect (hook->peer->conn); + hc_conn_close (hook->peer->conn); } - gnet_conn_delete (hook->conn); + hc_conn_close (hook->conn); if (ssl != NULL) { gnutls_bye (ssl->session, GNUTLS_SHUT_RDWR); @@ -152,7 +149,7 @@ ssl_server_read (net_hook_t *hook, gchar *buffer, size_t len) { r = gnutls_record_recv (ssl->session, buffer, len); if (r > 0) - gnet_conn_write (hook->peer->conn, buffer, r); + hc_conn_write (hook->peer->conn, buffer, r); } while (r > 0); } @@ -163,11 +160,10 @@ ssl_server_error (net_hook_t *hook) } net_hook_t * -ssl_server_hook_new (net_hook_t *client_hook, char *server, int port) +ssl_server_hook_new (net_hook_t *client_hook, char *server, char *port) { net_hook_t *hook; hook = g_slice_new (net_hook_t); - hook->conn = gnet_conn_new (server, port, nethook_event, hook); hook->peer = client_hook; hook->server = TRUE; hook->connect = ssl_server_connect; @@ -175,7 +171,6 @@ ssl_server_hook_new (net_hook_t *client_hook, char *server, int port) hook->write = ssl_server_write; hook->read = ssl_server_read; hook->data = ssl_data_new (server); - gnet_conn_connect (hook->conn); - gnet_conn_read (hook->conn); + hook->conn = hc_conn_new (hc_tcp_connect (server, port), nethook_event, hook); return hook; } -- 2.20.1