18336a190b77fde1d7c41fa8e7272f196f34b57b
[cascardo/rnetproxy.git] / iochannel.c
1 /*
2  *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include "iochannel.h"
21 #include <unistd.h>
22
23 struct hc_server_cb
24 {
25   GIOChannel *channel;
26   HCServerFunc func;
27   gpointer data;
28 };
29
30 static void
31 hc_server_cb_destroy (gpointer cb)
32 {
33   g_slice_free (struct hc_server_cb, cb);
34 }
35
36 static gboolean
37 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
38 {
39   struct hc_server_cb *cb = data;
40   int fd = g_io_channel_unix_get_fd (channel);
41   struct sockaddr addr;
42   socklen_t saddr = sizeof (addr);
43   int client = accept (fd, &addr, &saddr);
44   if (client >= 0 && cb->func)
45     cb->func (client, &addr, saddr, cb->data);
46   return TRUE;
47 }
48
49 void
50 hc_server_add_watch (int fd,
51                      HCServerFunc func,
52                      gpointer data)
53 {
54   struct hc_server_cb *cb;
55   cb = g_slice_new (struct hc_server_cb);
56   cb->channel = g_io_channel_unix_new (fd);
57   cb->func = func;
58   cb->data = data;
59   g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
60                        hc_server_watch, cb, hc_server_cb_destroy);
61 }
62
63 struct _hc_conn_t
64 {
65   GIOChannel *channel;
66   HCClientFunc func;
67   gpointer data;
68   ssize_t (*read) (gpointer, char *, size_t);
69   ssize_t (*write) (gpointer, char *, size_t);
70   void (*close) (gpointer);
71   gpointer layer;
72   guint watch;
73 };
74
75 ssize_t
76 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
77 {
78   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
79   return read (fd, buffer, len);
80 }
81
82 ssize_t
83 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
84 {
85   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
86   return write (fd, buffer, len);
87 }
88
89 void
90 hc_conn_channel_close (gpointer data)
91 {
92   int fd = g_io_channel_unix_get_fd ((GIOChannel *) data);
93   shutdown (fd, SHUT_RDWR);
94 }
95
96 gboolean
97 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
98 {
99   HCConn *conn = data;
100   HCEvent event = HC_EVENT_READ;
101   if (conn->func)
102     conn->func (conn, event, conn->data);
103   return TRUE;
104 }
105
106 HCConn *
107 hc_conn_new (int fd, HCClientFunc func, gpointer data)
108 {
109   HCConn *conn;
110   conn = g_slice_new (HCConn);
111   conn->channel = g_io_channel_unix_new (fd);
112   conn->func = func;
113   conn->data = data;
114   conn->layer = conn->channel;
115   conn->read = hc_conn_channel_read;
116   conn->write = hc_conn_channel_write;
117   conn->close = hc_conn_channel_close;
118   conn->watch = g_io_add_watch (conn->channel, G_IO_IN, hc_conn_watch, conn);
119   return conn;
120 }
121
122 ssize_t
123 hc_conn_read (HCConn *conn, char *buffer, size_t len)
124 {
125   return conn->read (conn->layer, buffer, len);
126 }
127
128 void
129 hc_conn_write (HCConn *conn, char *buffer, size_t len)
130 {
131   /* TODO: Do buffering or something like that */
132   conn->write (conn->layer, buffer, len);
133 }
134
135 void
136 hc_conn_close (HCConn *conn)
137 {
138   conn->close (conn->layer);
139   g_source_remove (conn->watch);
140   g_io_channel_unref (conn->channel);
141   g_slice_free (HCConn, conn);
142 }