Implement server port support and update copyright year and style.
[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 struct hc_server_cb
26 {
27   GIOChannel *channel;
28   HCServerFunc func;
29   gpointer data;
30 };
31
32 static void
33 hc_server_cb_destroy (gpointer cb)
34 {
35   g_slice_free (struct hc_server_cb, cb);
36 }
37
38 static gboolean
39 hc_server_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
40 {
41   struct hc_server_cb *cb = data;
42   int fd = g_io_channel_unix_get_fd (channel);
43   struct sockaddr addr;
44   socklen_t saddr = sizeof (addr);
45   int client = accept (fd, &addr, &saddr);
46   if (client >= 0 && cb->func)
47     cb->func (client, &addr, saddr, cb->data);
48   return TRUE;
49 }
50
51 void
52 hc_server_add_watch (int fd,
53                      HCServerFunc func,
54                      gpointer data)
55 {
56   struct hc_server_cb *cb;
57   cb = g_slice_new (struct hc_server_cb);
58   cb->channel = g_io_channel_unix_new (fd);
59   cb->func = func;
60   cb->data = data;
61   g_io_add_watch_full (cb->channel, G_PRIORITY_DEFAULT, G_IO_IN,
62                        hc_server_watch, cb, hc_server_cb_destroy);
63 }
64
65 struct channel_layer
66 {
67   GIOChannel *channel;
68   guint watch;
69 };
70
71
72 ssize_t
73 hc_conn_channel_read (gpointer data, char *buffer, size_t len)
74 {
75   struct channel_layer *layer = data;
76   int fd = g_io_channel_unix_get_fd (layer->channel);
77   return read (fd, buffer, len);
78 }
79
80 ssize_t
81 hc_conn_channel_write (gpointer data, char *buffer, size_t len)
82 {
83   struct channel_layer *layer = data;
84   int fd = g_io_channel_unix_get_fd (layer->channel);
85   return write (fd, buffer, len);
86 }
87
88 void
89 hc_conn_channel_close (gpointer data)
90 {
91   struct channel_layer *layer = data;
92   int fd = g_io_channel_unix_get_fd (layer->channel);
93   g_source_remove (layer->watch);
94   shutdown (fd, SHUT_RDWR);
95   g_io_channel_unref (layer->channel);
96   g_slice_free (struct channel_layer, layer);
97 }
98
99 gboolean
100 hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data)
101 {
102   HCConn *conn = data;
103   HCEvent event = HC_EVENT_READ;
104   if (conn->func)
105     conn->func (conn, event, conn->data);
106   return TRUE;
107 }
108
109 void
110 hc_conn_set_driver_channel (HCConn *conn, int fd)
111 {
112   struct channel_layer *layer = g_slice_new (struct channel_layer);
113   layer->channel = g_io_channel_unix_new (fd);
114   conn->layer = layer;
115   conn->read = hc_conn_channel_read;
116   conn->write = hc_conn_channel_write;
117   conn->close = hc_conn_channel_close;
118   layer->watch = g_io_add_watch (layer->channel, G_IO_IN, hc_conn_watch, conn);
119   if (conn->func)
120     conn->func (conn, HC_EVENT_CONNECT, conn->data);
121   fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK);
122 }
123
124 HCConn *
125 hc_conn_new (HCClientFunc func, gpointer data)
126 {
127   HCConn *conn;
128   conn = g_slice_new (HCConn);
129   conn->func = func;
130   conn->data = data;
131   return conn;
132 }
133
134 void
135 hc_conn_set_callback (HCConn *conn, HCClientFunc func, gpointer data)
136 {
137   conn->func = func;
138   conn->data = data;
139 }
140
141 ssize_t
142 hc_conn_read (HCConn *conn, char *buffer, size_t len)
143 {
144   return conn->read (conn->layer, buffer, len);
145 }
146
147 void
148 hc_conn_write (HCConn *conn, char *buffer, size_t len)
149 {
150   /* TODO: Do buffering or something like that */
151   conn->write (conn->layer, buffer, len);
152 }
153
154 void
155 hc_conn_close (HCConn *conn)
156 {
157   conn->close (conn->layer);
158   g_slice_free (HCConn, conn);
159 }