3b2a8907e5a10ed239a424acdb5bce5eb8b0fe15
[cascardo/rnetproxy.git] / hcconn.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 "hcconn.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include "hcconn_internal.h"
24
25 /* The server connection watch */
26
27 struct hc_server_cb
28 {
29   GIOChannel *channel;
30   HCServerFunc func;
31   gpointer data;
32 };
33
34 static void
35 hc_server_cb_destroy (gpointer cb)
36 {
37   g_slice_free (struct hc_server_cb, cb);
38 }
39
40 static gboolean
41 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
42 {
43   struct hc_server_cb *cb = data;
44   int fd = g_io_channel_unix_get_fd (channel);
45   struct sockaddr addr;
46   socklen_t saddr = sizeof (addr);
47   int client = accept (fd, &addr, &saddr);
48   if (client >= 0 && cb->func)
49     cb->func (client, &addr, saddr, cb->data);
50   return TRUE;
51 }
52
53 void
54 hc_server_add_watch (int fd,
55                      HCServerFunc func,
56                      gpointer data)
57 {
58   struct hc_server_cb *cb;
59   cb = g_slice_new (struct hc_server_cb);
60   cb->channel = g_io_channel_unix_new (fd);
61   cb->func = func;
62   cb->data = data;
63   /* TODO: we should have some way to remove this watch */
64   g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
65                        hc_server_watch, cb, hc_server_cb_destroy);
66 }
67
68
69 /* The IOChannel (simple socket) layer */
70
71 struct channel_layer
72 {
73   GIOChannel *channel;
74   guint watch;
75 };
76
77 ssize_t
78 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
79 {
80   struct channel_layer *layer = data;
81   int fd = g_io_channel_unix_get_fd (layer->channel);
82   return read (fd, buffer, len);
83 }
84
85 ssize_t
86 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
87 {
88   struct channel_layer *layer = data;
89   int fd = g_io_channel_unix_get_fd (layer->channel);
90   return write (fd, buffer, len);
91 }
92
93 void
94 hc_conn_channel_close (gpointer data)
95 {
96   struct channel_layer *layer = data;
97   int fd = g_io_channel_unix_get_fd (layer->channel);
98   g_source_remove (layer->watch);
99   shutdown (fd, SHUT_RDWR);
100   g_io_channel_unref (layer->channel);
101   g_slice_free (struct channel_layer, layer);
102 }
103
104 gboolean
105 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
106 {
107   HCConn *conn = data;
108   /* TODO: What about other events, like closing? */
109   HCEvent event = HC_EVENT_READ;
110   if (conn->func)
111     conn->func (conn, event, conn->data);
112   return TRUE;
113 }
114
115 void
116 hc_conn_set_driver_channel (HCConn *conn, int fd)
117 {
118   struct channel_layer *layer = g_slice_new (struct channel_layer);
119   layer->channel = g_io_channel_unix_new (fd);
120   conn->layer = layer;
121   conn->read = hc_conn_channel_read;
122   conn->write = hc_conn_channel_write;
123   conn->close = hc_conn_channel_close;
124   /* TODO: We must watch other events */
125   layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
126   /* TODO: connection should be asynchronous so this could make sense */
127   if (conn->func)
128     conn->func (conn, HC_EVENT_CONNECT, conn->data);
129   fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
130 }
131
132
133 /* The core connection system */
134
135 HCConn *
136 hc_conn_new (HCClientFunc func, gpointer data)
137 {
138   HCConn *conn;
139   conn = g_slice_new (HCConn);
140   conn->func = func;
141   conn->data = data;
142   return conn;
143 }
144
145 void
146 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
147 {
148   conn->func = func;
149   conn->data = data;
150 }
151
152 ssize_t
153 hc_conn_read (HCConn *conn, char *buffer, size_t len)
154 {
155   return conn->read (conn->layer, buffer, len);
156 }
157
158 void
159 hc_conn_write (HCConn *conn, char *buffer, size_t len)
160 {
161   /* TODO: Do buffering or something like that */
162   /* Do we really need to? */
163   /* In case of error, we should do something */
164   conn->write (conn->layer, buffer, len);
165 }
166
167 void
168 hc_conn_close (HCConn *conn)
169 {
170   conn->close (conn->layer);
171   g_slice_free (HCConn, conn);
172 }