2 * UDP Gateway utility network layer
3 * Copyright (C) 2004 Jan Kratochvil <project-udpgate@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
23 #include <glib/gmain.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <glib/glist.h>
29 #include <sys/types.h>
32 #include <glib/giochannel.h>
33 #include <sys/ioctl.h>
35 #include <glib/galloca.h>
36 #include <glib/gprintf.h>
37 #include <glib/grand.h>
44 #define NETWORK_PATHNAME_PID "/var/run/udpgate.pid"
45 #define SOCK_SOURCE_CHECK_EVENTS (G_IO_IN) /* |G_IO_PRI */
46 #define SOCK_SOURCE_CHECK_REVENTS (SOCK_SOURCE_CHECK_EVENTS) /* |G_IO_ERR|G_IO_HUP|G_IO_NVAL */
47 #define SERVER_INADDR 0x7F000001
49 #define SERVER_INADDR 0x511F02EA /* paulina.vellum.cz = 81.31.2.234; host order */
50 #define SERVER_PORT 9201 /* host order */
51 #define PROBE_INADDR SERVER_INADDR /* host order */
52 #define PROBE_PORT 8201 /* host order */
53 #define CLIENT_TIMEOUT_SEC (5*60)
54 #define PROBE_TIMEOUT_SEC (5)
57 void (*network_notify_hostip)(guint32 hostip_guint32,const gchar *hostip_string);
62 struct sockaddr_in sockaddr_in_from;
66 static GSource *sock_gsource;
68 static GList *sock_client_list; /* of 'struct client *', incl. 'master' and 'probe' */
69 static struct client *master; /* no 'timeout' permitted */
70 static struct client *probe; /* 'timeout' permitted */
71 static guint32 probe_unique;
73 static gboolean write_daemon_running(pid_t pid);
75 pid_t is_daemon_running(void)
78 char buf[LINE_MAX],*got;
84 if (!(f=fopen(NETWORK_PATHNAME_PID,"r")))
86 got=fgets(buf,sizeof(buf),f);
87 fclose(f); /* FIXME: error ignored */
90 write_daemon_running((pid_t)-1); /* unlink */
97 if (kill((pid_t)pid_int,0)) {
102 return (pid_t)pid_int;
105 static gboolean write_daemon_running(pid_t pid)
109 if (pid==(pid_t)-1) {
110 if (unlink(NETWORK_PATHNAME_PID)) {
112 g_warning(_("Error removing PID file \"%s\": %m"),NETWORK_PATHNAME_PID);
117 if (!(f=fopen(NETWORK_PATHNAME_PID,"w"))) {
118 g_warning(_("Error writing PID %d to \"%s\": %m"),(int)pid,NETWORK_PATHNAME_PID);
121 fprintf(f,"%d\n",(int)pid); /* errors ignored */
122 fclose(f); /* FIXME: error ignored */
126 static void client_timeout_remove(struct client *client)
128 g_return_if_fail(client!=NULL);
129 g_return_if_fail(client!=master);
131 if (client->timeout_id) {
132 gboolean errgboolean;
134 errgboolean=g_source_remove(client->timeout_id);
135 g_assert(errgboolean==TRUE);
136 client->timeout_id=0;
140 static gboolean client_touch_timeout(struct client *client);
142 static void client_touch(struct client *client)
144 g_return_if_fail(client!=NULL);
145 g_return_if_fail(client!=master);
147 client_timeout_remove(client);
148 client->timeout_id=g_timeout_add(
149 (client==probe ? PROBE_TIMEOUT_SEC*1000 : CLIENT_TIMEOUT_SEC*1000), /* interval; msec */
150 (GSourceFunc)client_touch_timeout, /* function */
152 g_assert(client->timeout_id!=0);
155 static void client_destroy(struct client *client);
157 static gboolean client_touch_timeout(struct client *client)
159 g_return_val_if_fail(client!=NULL,FALSE); /* FALSE=>should be removed */
160 g_return_val_if_fail(client!=master,FALSE); /* FALSE=>should be removed */
164 if (network_notify_hostip)
165 (*network_notify_hostip)(0,NULL);
168 client_destroy(client);
170 return FALSE; /* GSource should be removed */
173 static struct client *client_new(void);
175 static void handle_master(struct client *master)
178 struct sockaddr_in sockaddr_in_from;
179 char packet[0x10000];
180 struct client *client;
181 struct sockaddr_in sockaddr_in_server;
182 socklen_t sockaddr_in_from_length;
184 g_return_if_fail(master!=NULL);
186 sockaddr_in_from_length=sizeof(sockaddr_in_from);
187 while (-1!=(gotlen=recvfrom(
188 master->gpollfd.fd, /* s */
190 sizeof(packet), /* len */
192 (struct sockaddr *)&sockaddr_in_from, /* from */
193 &sockaddr_in_from_length))) { /* fromlen */
196 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
198 /* FIXME: Performance: Ugly search... */
199 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
200 client=clientl->data;
204 && client->sockaddr_in_from.sin_family ==sockaddr_in_from.sin_family
205 && client->sockaddr_in_from.sin_port ==sockaddr_in_from.sin_port
206 && client->sockaddr_in_from.sin_addr.s_addr==sockaddr_in_from.sin_addr.s_addr)
211 client->sockaddr_in_from=sockaddr_in_from;
213 client_touch(client);
214 UDPGATE_MEMZERO(&sockaddr_in_server);
215 sockaddr_in_server.sin_family=AF_INET;
216 sockaddr_in_server.sin_port=htons(SERVER_PORT);
217 sockaddr_in_server.sin_addr.s_addr=htonl(SERVER_INADDR);
218 /* FIXME: errors checking */
220 client->gpollfd.fd, /* s */
224 (struct sockaddr *)&sockaddr_in_server, /* to */
225 sizeof(sockaddr_in_server)); /* tolen */
229 static void handle_probe(struct client *probe)
232 struct sockaddr_in sockaddr_in_from;
233 char packet[0x10000];
234 socklen_t sockaddr_in_from_length;
236 g_return_if_fail(probe!=NULL);
238 sockaddr_in_from_length=sizeof(sockaddr_in_from);
239 while (-1!=(gotlen=recvfrom(
240 master->gpollfd.fd, /* s */
242 sizeof(packet), /* len */
244 (struct sockaddr *)&sockaddr_in_from, /* from */
245 &sockaddr_in_from_length))) { /* fromlen */
246 static GHashTable *got_hash;
247 static gpointer got_unique_gpointer;
248 static gpointer hostip_gpointer;
249 static guint32 hostip_guint32;
251 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
253 if (!(got_hash=packet_disassembly(packet,gotlen)))
255 if (!(g_hash_table_lookup_extended(
256 got_hash, /* hash_table */
257 GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32), /* lookup_key */
259 &got_unique_gpointer))) {
260 err_packet_disassembly_destroy_got_hash:
261 packet_disassembly_destroy(got_hash);
264 if (GPOINTER_TO_UINT(got_unique_gpointer)!=probe_unique)
265 goto err_packet_disassembly_destroy_got_hash;
266 if (!(g_hash_table_lookup_extended(
267 got_hash, /* hash_table */
268 GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_INADDR), /* lookup_key */
271 goto err_packet_disassembly_destroy_got_hash;
272 hostip_guint32=GPOINTER_TO_UINT(hostip_gpointer);
273 packet_disassembly_destroy(got_hash);
275 client_destroy(probe);
276 if (network_notify_hostip)
277 (*network_notify_hostip)(hostip_guint32,HOSTIP_GUINT32_TO_STRING(hostip_guint32));
281 static void handle_client(struct client *client)
284 struct sockaddr_in sockaddr_in_from;
285 char packet [0x10000];
286 socklen_t sockaddr_in_from_length;
288 g_return_if_fail(client!=NULL);
289 g_return_if_fail(master!=NULL);
291 while (-1!=(gotlen=recvfrom(
292 client->gpollfd.fd, /* s */
294 sizeof(packet), /* len */
296 (struct sockaddr *)&sockaddr_in_from, /* from */
297 &sockaddr_in_from_length))) { /* fromlen */
298 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
300 client_touch(client);
301 /* FIXME: errors checking */
303 master->gpollfd.fd, /* s */
307 (struct sockaddr *)&client->sockaddr_in_from, /* to */
308 sizeof(client->sockaddr_in_from)); /* tolen */
312 static gboolean sock_source_callback(gpointer data /* unused */)
316 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
317 struct client *client=clientl->data;
319 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS) {
320 /**/ if (client==master)
321 handle_master(client);
322 else if (client==probe)
323 handle_probe(client);
325 handle_client(client);
329 return TRUE; /* the source should be kept active */
332 static gboolean sock_source_prepare(GSource *source,gint *timeout)
339 static gboolean sock_source_check(GSource *source)
343 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
344 struct client *client=clientl->data;
346 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS)
352 static gboolean sock_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
354 g_assert(callback!=NULL);
355 return (*callback)(user_data);
358 static GSourceFuncs sock_source_watch_funcs={
361 sock_source_dispatch,
365 static void sock_gsource_destroy(void)
367 while (sock_client_list)
368 client_destroy(sock_client_list->data);
370 g_assert(master==NULL);
373 g_source_destroy(sock_gsource);
377 write_daemon_running((pid_t)-1); /* unlink */
380 static gboolean sock_gsource_new(void)
385 g_assert(sock_client_list==NULL);
387 /* attach sock_source_callback() to watch for any abnormalities
388 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
390 if (!(sock_gsource=g_source_new(&sock_source_watch_funcs,sizeof(GSource)))) {
391 g_warning("g_source_new(): %m");
394 g_source_set_callback(
395 sock_gsource, /* source */
396 sock_source_callback, /* func */
399 if (!g_source_attach( /* returns 'guint' id */
400 sock_gsource, /* source */
401 NULL)) { /* context; NULL means 'default context' */
402 g_warning("g_source_attach(gsource,NULL): %m");
403 sock_gsource_destroy();
409 static struct client *client_new(void)
411 struct client *client;
412 static unsigned long oneul=1;
415 if (!sock_gsource_new())
418 if (-1==(sock=socket(AF_INET,SOCK_DGRAM,0))) {
419 g_warning("socket(AF_INET,SOCK_DGRAM): %m");
422 if (ioctl(sock,FIONBIO,&oneul)) { /* non-blocking mode */
423 g_warning("ioctl(sock,FIONBIO,&1): %m");
424 close(sock); /* errors ignored */
429 client->gpollfd.fd=sock;
430 client->gpollfd.events=SOCK_SOURCE_CHECK_EVENTS;
431 client->gpollfd.revents=0;
432 client->timeout_id=0;
433 sock_client_list=g_list_prepend(sock_client_list,client);
434 g_source_add_poll(sock_gsource,&client->gpollfd);
439 static void client_destroy(struct client *client)
441 g_return_if_fail(client!=NULL);
443 if (!sock_gsource_new())
446 if (client==master) {
448 g_assert(client->timeout_id==0);
453 client_timeout_remove(client);
456 g_source_remove_poll(sock_gsource,&client->gpollfd);
457 sock_client_list=g_list_remove(sock_client_list,client);
458 close(client->gpollfd.fd); /* errors ignored */
462 static gboolean probe_send(struct client *probe,gint port_local)
464 struct sockaddr_in sockaddr_in_server;
465 GHashTable *probe_hash;
467 size_t packet_length;
469 g_return_val_if_fail(probe!=NULL,FALSE);
471 probe_unique=g_random_int();
473 probe_hash=g_hash_table_new(
474 g_direct_hash, /* hash_func */
475 g_direct_equal); /* key_equal_func */
476 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_PORT) ,GUINT_TO_POINTER(port_local));
477 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32),GUINT_TO_POINTER(probe_unique));
478 packet=packet_assembly(&packet_length,probe_hash);
479 g_hash_table_destroy(probe_hash);
483 UDPGATE_MEMZERO(&sockaddr_in_server);
484 sockaddr_in_server.sin_family=AF_INET;
485 sockaddr_in_server.sin_port=htons(PROBE_PORT);
486 sockaddr_in_server.sin_addr.s_addr=htonl(PROBE_INADDR);
487 /* FIXME: errors checking */
489 probe->gpollfd.fd, /* s */
491 packet_length, /* len */
493 (struct sockaddr *)&sockaddr_in_server, /* to */
494 sizeof(sockaddr_in_server)); /* tolen */
499 gboolean network_start(gint port)
502 struct sockaddr_in sockaddr_in;
504 g_return_val_if_fail(port>=0,FALSE);
506 if ((pid_t)-1!=(daemon_pid=is_daemon_running())) {
507 g_warning(_("Cannot start network daemon: Daemon is already running on PID %d"),(int)daemon_pid);
511 /* Setup 'master': */
512 g_assert(master==NULL);
513 if (!(master=client_new()))
515 UDPGATE_MEMZERO(&sockaddr_in);
516 sockaddr_in.sin_family=AF_INET;
517 sockaddr_in.sin_port=htons(port);
518 sockaddr_in.sin_addr.s_addr=INADDR_ANY;
519 if (bind(master->gpollfd.fd,(struct sockaddr *)&sockaddr_in,sizeof(sockaddr_in))) {
520 g_warning("bind(sock,{AF_INET,INADDR_ANY:%d}): %m",(int)port);
521 err_sock_gsource_destroy:
522 sock_gsource_destroy();
527 if (!(probe=client_new()))
528 goto err_sock_gsource_destroy;
529 probe_send(probe,port);
530 client_touch(probe); /* timeout */
532 write_daemon_running(getpid());
536 gboolean network_stop(void)
541 if ((pid_t)-1==(daemon_pid=is_daemon_running())) {
542 g_warning(_("Cannot stop network daemon: Daemon is not running"));
545 if (daemon_pid==getpid()) {
546 sock_gsource_destroy();
550 kill(daemon_pid,SIGKILL);
553 g_warning(udpgate_printf_alloca(_("Unable to stop the daemon at PID %d: %s"),
554 (int)daemon_pid,strerror(errno_save)));