From: Thadeu Lima de Souza Cascardo Date: Thu, 2 Jul 2009 18:45:22 +0000 (-0300) Subject: Rename iochannel to hcconn. X-Git-Tag: v0.1.3~64 X-Git-Url: http://git.cascardo.info/?p=cascardo%2Frnetproxy.git;a=commitdiff_plain;h=b7dc018999a689010314c3847576cef772fd1d16 Rename iochannel to hcconn. --- diff --git a/Makefile.am b/Makefile.am index 8d54ad8..93680df 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +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 \ + hcconn.c hcconn.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/hcconn.c b/hcconn.c new file mode 100644 index 0000000..5c6e60e --- /dev/null +++ b/hcconn.c @@ -0,0 +1,153 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#include "hcconn.h" +#include +#include + +struct hc_server_cb +{ + GIOChannel *channel; + HCServerFunc func; + gpointer data; +}; + +static void +hc_server_cb_destroy (gpointer cb) +{ + g_slice_free (struct hc_server_cb, cb); +} + +static gboolean +hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data) +{ + struct hc_server_cb *cb = data; + int fd = g_io_channel_unix_get_fd (channel); + struct sockaddr addr; + socklen_t saddr = sizeof (addr); + int client = accept (fd, &addr, &saddr); + if (client >= 0 && cb->func) + cb->func (client, &addr, saddr, cb->data); + return TRUE; +} + +void +hc_server_add_watch (int fd, + HCServerFunc func, + gpointer data) +{ + struct hc_server_cb *cb; + cb = g_slice_new (struct hc_server_cb); + cb->channel = g_io_channel_unix_new (fd); + cb->func = func; + cb->data = data; + g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN, + hc_server_watch, cb, hc_server_cb_destroy); +} + +struct _hc_conn_t +{ + GIOChannel *channel; + HCClientFunc func; + gpointer data; + ssize_t (*read) (gpointer, char *, size_t); + ssize_t (*write) (gpointer, char *, size_t); + void (*close) (gpointer); + gpointer layer; + guint watch; +}; + +ssize_t +hc_conn_channel_read (gpointer data, char *buffer, size_t len) +{ + int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); + return read (fd, buffer, len); +} + +ssize_t +hc_conn_channel_write (gpointer data, char *buffer, size_t len) +{ + int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); + return write (fd, buffer, len); +} + +void +hc_conn_channel_close (gpointer data) +{ + int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); + shutdown (fd, SHUT_RDWR); +} + +gboolean +hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data) +{ + HCConn *conn = data; + HCEvent event = HC_EVENT_READ; + if (conn->func) + conn->func (conn, event, conn->data); + return TRUE; +} + +HCConn * +hc_conn_new (int fd, HCClientFunc func, gpointer data) +{ + HCConn *conn; + conn = g_slice_new (HCConn); + conn->channel = g_io_channel_unix_new (fd); + conn->func = func; + conn->data = data; + conn->layer = conn->channel; + conn->read = hc_conn_channel_read; + conn->write = hc_conn_channel_write; + conn->close = hc_conn_channel_close; + conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn); + if (conn->func) + conn->func (conn, HC_EVENT_CONNECT, conn->data); + fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK); + return conn; +} + +void +hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data) +{ + conn->func = func; + conn->data = data; +} + +ssize_t +hc_conn_read (HCConn *conn, char *buffer, size_t len) +{ + return conn->read (conn->layer, buffer, len); +} + +void +hc_conn_write (HCConn *conn, char *buffer, size_t len) +{ + /* TODO: Do buffering or something like that */ + conn->write (conn->layer, buffer, len); +} + +void +hc_conn_close (HCConn *conn) +{ + conn->close (conn->layer); + g_source_remove (conn->watch); + g_io_channel_unref (conn->channel); + g_slice_free (HCConn, conn); +} diff --git a/hcconn.h b/hcconn.h new file mode 100644 index 0000000..3fbbea7 --- /dev/null +++ b/hcconn.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + + +#ifndef HC_IOCHANNEL_H +#define HC_IOCHANNEL_H + +#include +#include + +typedef void (*HCServerFunc) (int, struct sockaddr*, socklen_t, gpointer); + +void hc_server_add_watch (int, HCServerFunc, gpointer); + +typedef struct _hc_conn_t HCConn; + +typedef enum +{ + HC_EVENT_NULL = 0, + HC_EVENT_CONNECT, + HC_EVENT_READ, + HC_EVENT_CLOSE +} HCEvent; + +typedef void (*HCClientFunc) (HCConn *, HCEvent, gpointer); + +HCConn * hc_conn_new (int, HCClientFunc, gpointer); +ssize_t hc_conn_read (HCConn *, char *, size_t); +void hc_conn_write (HCConn *, char *, size_t); +void hc_conn_close (HCConn *); +void hc_conn_set_callback (HCConn *, HCClientFunc, gpointer); + +#endif diff --git a/iochannel.c b/iochannel.c deleted file mode 100644 index e4d5514..0000000 --- a/iochannel.c +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright (C) 2009 Thadeu Lima de Souza Cascardo - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#include "iochannel.h" -#include -#include - -struct hc_server_cb -{ - GIOChannel *channel; - HCServerFunc func; - gpointer data; -}; - -static void -hc_server_cb_destroy (gpointer cb) -{ - g_slice_free (struct hc_server_cb, cb); -} - -static gboolean -hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data) -{ - struct hc_server_cb *cb = data; - int fd = g_io_channel_unix_get_fd (channel); - struct sockaddr addr; - socklen_t saddr = sizeof (addr); - int client = accept (fd, &addr, &saddr); - if (client >= 0 && cb->func) - cb->func (client, &addr, saddr, cb->data); - return TRUE; -} - -void -hc_server_add_watch (int fd, - HCServerFunc func, - gpointer data) -{ - struct hc_server_cb *cb; - cb = g_slice_new (struct hc_server_cb); - cb->channel = g_io_channel_unix_new (fd); - cb->func = func; - cb->data = data; - g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN, - hc_server_watch, cb, hc_server_cb_destroy); -} - -struct _hc_conn_t -{ - GIOChannel *channel; - HCClientFunc func; - gpointer data; - ssize_t (*read) (gpointer, char *, size_t); - ssize_t (*write) (gpointer, char *, size_t); - void (*close) (gpointer); - gpointer layer; - guint watch; -}; - -ssize_t -hc_conn_channel_read (gpointer data, char *buffer, size_t len) -{ - int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); - return read (fd, buffer, len); -} - -ssize_t -hc_conn_channel_write (gpointer data, char *buffer, size_t len) -{ - int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); - return write (fd, buffer, len); -} - -void -hc_conn_channel_close (gpointer data) -{ - int fd = g_io_channel_unix_get_fd ((GIOChannel *) data); - shutdown (fd, SHUT_RDWR); -} - -gboolean -hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data) -{ - HCConn *conn = data; - HCEvent event = HC_EVENT_READ; - if (conn->func) - conn->func (conn, event, conn->data); - return TRUE; -} - -HCConn * -hc_conn_new (int fd, HCClientFunc func, gpointer data) -{ - HCConn *conn; - conn = g_slice_new (HCConn); - conn->channel = g_io_channel_unix_new (fd); - conn->func = func; - conn->data = data; - conn->layer = conn->channel; - conn->read = hc_conn_channel_read; - conn->write = hc_conn_channel_write; - conn->close = hc_conn_channel_close; - conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn); - if (conn->func) - conn->func (conn, HC_EVENT_CONNECT, conn->data); - fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK); - return conn; -} - -void -hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data) -{ - conn->func = func; - conn->data = data; -} - -ssize_t -hc_conn_read (HCConn *conn, char *buffer, size_t len) -{ - return conn->read (conn->layer, buffer, len); -} - -void -hc_conn_write (HCConn *conn, char *buffer, size_t len) -{ - /* TODO: Do buffering or something like that */ - conn->write (conn->layer, buffer, len); -} - -void -hc_conn_close (HCConn *conn) -{ - conn->close (conn->layer); - g_source_remove (conn->watch); - g_io_channel_unref (conn->channel); - g_slice_free (HCConn, conn); -} diff --git a/iochannel.h b/iochannel.h deleted file mode 100644 index 3fbbea7..0000000 --- a/iochannel.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2009 Thadeu Lima de Souza Cascardo - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - */ - - -#ifndef HC_IOCHANNEL_H -#define HC_IOCHANNEL_H - -#include -#include - -typedef void (*HCServerFunc) (int, struct sockaddr*, socklen_t, gpointer); - -void hc_server_add_watch (int, HCServerFunc, gpointer); - -typedef struct _hc_conn_t HCConn; - -typedef enum -{ - HC_EVENT_NULL = 0, - HC_EVENT_CONNECT, - HC_EVENT_READ, - HC_EVENT_CLOSE -} HCEvent; - -typedef void (*HCClientFunc) (HCConn *, HCEvent, gpointer); - -HCConn * hc_conn_new (int, HCClientFunc, gpointer); -ssize_t hc_conn_read (HCConn *, char *, size_t); -void hc_conn_write (HCConn *, char *, size_t); -void hc_conn_close (HCConn *); -void hc_conn_set_callback (HCConn *, HCClientFunc, gpointer); - -#endif diff --git a/nethook.h b/nethook.h index 3e1baa6..b428a14 100644 --- a/nethook.h +++ b/nethook.h @@ -21,7 +21,7 @@ #define NET_HOOK_H #include -#include "iochannel.h" +#include "hcconn.h" typedef struct _net_hook_t net_hook_t; typedef void (*net_connect) (net_hook_t*); diff --git a/null.h b/null.h index 790b2c7..e48a5eb 100644 --- a/null.h +++ b/null.h @@ -21,7 +21,7 @@ #define NULL_H #include "nethook.h" -#include "iochannel.h" +#include "hcconn.h" net_hook_t* null_hook_new (HCConn*, char*); void null_destroy (net_hook_t*); diff --git a/popproxy.c b/popproxy.c index 27f8a00..7f4f5a3 100644 --- a/popproxy.c +++ b/popproxy.c @@ -31,7 +31,7 @@ #include "ssl.h" #include "pop.h" -#include "iochannel.h" +#include "hcconn.h" #include "tcp_connect.h" #define CONFFILE SYSCONFDIR "/popproxy.conf" diff --git a/ssl.h b/ssl.h index ae541c5..62e75d5 100644 --- a/ssl.h +++ b/ssl.h @@ -22,7 +22,7 @@ #define POPPROXY_SSL_H #include "nethook.h" -#include "iochannel.h" +#include "hcconn.h" struct ssl_data {