2
* Copyright (c) 2002-2009 BalaBit IT Ltd, Budapest, Hungary
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 as published
6
* by the Free Software Foundation.
8
* Note that this permission is granted for only version 2 of the GPL.
10
* As an additional exemption you are allowed to compile & link against the
11
* OpenSSL libraries as published by the OpenSSL project. See the file
12
* COPYING for details.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
#include <arpa/inet.h>
28
typedef struct _GListenSource
35
g_listen_prepare(GSource *source,
38
GListenSource *self = (GListenSource *) source;
40
self->pollfd.events = G_IO_IN;
41
self->pollfd.revents = 0;
47
g_listen_check(GSource *source)
49
GListenSource *self = (GListenSource *) source;
51
return !!(self->pollfd.revents & (G_IO_IN | G_IO_ERR | G_IO_HUP));
55
g_listen_dispatch(GSource *source,
59
return callback(user_data);
62
GSourceFuncs g_listen_source_funcs =
71
g_listen_source_new(gint fd)
73
GListenSource *self = (GListenSource *) g_source_new(&g_listen_source_funcs, sizeof(GListenSource));
76
g_source_set_priority(&self->super, LOG_PRIORITY_LISTEN);
77
g_source_add_poll(&self->super, &self->pollfd);
81
typedef struct _GConnectSource
88
g_connect_prepare(GSource *source,
91
GConnectSource *self = (GConnectSource *) source;
93
self->pollfd.events = G_IO_OUT;
94
self->pollfd.revents = 0;
100
g_connect_check(GSource *source)
102
GConnectSource *self = (GConnectSource *) source;
104
return !!(self->pollfd.revents & (G_IO_OUT | G_IO_ERR | G_IO_HUP));
108
g_connect_dispatch(GSource *source,
109
GSourceFunc callback,
116
GSourceFuncs g_connect_source_funcs =
125
g_connect_source_new(gint fd)
127
GConnectSource *self = (GConnectSource *) g_source_new(&g_connect_source_funcs, sizeof(GConnectSource));
129
self->pollfd.fd = fd;
130
g_source_set_priority(&self->super, LOG_PRIORITY_CONNECT);
131
g_source_add_poll(&self->super, &self->pollfd);
137
* @buf: store result in this buffer
138
* @bufsize: the available space in buf
139
* @a: address to convert.
141
* Thread friendly version of inet_ntoa(), converts an IP address to
142
* human readable form. Returns: the address of buf
145
g_inet_ntoa(char *buf, size_t bufsize, struct in_addr a)
147
unsigned int ip = ntohl(a.s_addr);
149
g_snprintf(buf, bufsize, "%d.%d.%d.%d",
150
(ip & 0xff000000) >> 24,
151
(ip & 0x00ff0000) >> 16,
152
(ip & 0x0000ff00) >> 8,
158
g_inet_aton(char *buf, struct in_addr *a)
160
return inet_aton(buf, a);
166
* @addr: address to bind to
168
* A thin interface around bind() using a GSockAddr structure for
169
* socket address. It enables the NET_BIND_SERVICE capability (should be
170
* in the permitted set.
173
g_bind(int fd, GSockAddr *addr)
177
if (addr->sa_funcs && addr->sa_funcs->sa_bind_prepare)
178
addr->sa_funcs->sa_bind_prepare(fd, addr);
180
if (addr->sa_funcs && addr->sa_funcs->sa_bind)
181
rc = addr->sa_funcs->sa_bind(fd, addr);
184
if (addr && bind(fd, &addr->sa, addr->salen) < 0)
186
return G_IO_STATUS_ERROR;
188
rc = G_IO_STATUS_NORMAL;
195
* @fd: accept connection on this socket
196
* @newfd: fd of the accepted connection
197
* @addr: store the address of the client here
199
* Accept a connection on the given fd, returning the newfd and the
200
* address of the client in a Zorp SockAddr structure.
202
* Returns: glib style I/O error
205
g_accept(int fd, int *newfd, GSockAddr **addr)
208
socklen_t salen = sizeof(sabuf);
212
*newfd = accept(fd, (struct sockaddr *) sabuf, &salen);
214
while (*newfd == -1 && errno == EINTR);
217
*addr = g_sockaddr_new((struct sockaddr *) sabuf, salen);
219
else if (errno == EAGAIN)
221
return G_IO_STATUS_AGAIN;
225
return G_IO_STATUS_ERROR;
227
return G_IO_STATUS_NORMAL;
232
* @fd: socket to connect
233
* @remote: remote address
235
* Connect a socket using Zorp style GSockAddr structure.
237
* Returns: glib style I/O error
240
g_connect(int fd, GSockAddr *remote)
246
rc = connect(fd, &remote->sa, remote->salen);
248
while (rc == -1 && errno == EINTR);
252
return G_IO_STATUS_AGAIN;
254
return G_IO_STATUS_ERROR;
258
return G_IO_STATUS_NORMAL;