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>
41 #include "main.h" /* for optarg_verbose */
45 #define NETWORK_PATHNAME_PID "/var/run/udpgate.pid"
46 #define SOCK_SOURCE_CHECK_EVENTS (G_IO_IN) /* |G_IO_PRI */
47 #define SOCK_SOURCE_CHECK_REVENTS (SOCK_SOURCE_CHECK_EVENTS) /* |G_IO_ERR|G_IO_HUP|G_IO_NVAL */
48 #define SERVER_INADDR 0x511F02EA /* paulina.vellum.cz = 81.31.2.234; host order */
49 #define SERVER_PORT 9201 /* host order */
50 #define PROBE_INADDR SERVER_INADDR /* host order */
51 #define PROBE_PORT 8201 /* host order */
52 #define CLIENT_TIMEOUT_SEC (5*60)
53 #define PROBE_TIMEOUT_SEC_BASE 5
54 #define PROBE_TIMEOUT_SEC_MAX 3500
57 void (*network_notify_hostip)(guint32 hostip_guint32);
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;
72 static guint probe_timeout_sec_now;
73 guint probe_timeout_sec_max=G_MAXUINT;
74 static gint port_local; /* for 'probe' resends */
76 static gboolean write_daemon_running(pid_t pid);
78 pid_t is_daemon_running(void)
81 char buf[LINE_MAX],*got;
87 if (!(f=fopen(NETWORK_PATHNAME_PID,"r")))
89 got=fgets(buf,sizeof(buf),f);
90 fclose(f); /* FIXME: error ignored */
93 write_daemon_running((pid_t)-1); /* unlink */
100 if (kill((pid_t)pid_int,0)) {
105 return (pid_t)pid_int;
108 static gboolean write_daemon_running(pid_t pid)
112 if (pid==(pid_t)-1) {
113 if (unlink(NETWORK_PATHNAME_PID)) {
115 g_warning(_("Error removing PID file \"%s\": %m"),NETWORK_PATHNAME_PID);
120 if (!(f=fopen(NETWORK_PATHNAME_PID,"w"))) {
121 g_warning(_("Error writing PID %d to \"%s\": %m"),(int)pid,NETWORK_PATHNAME_PID);
124 fprintf(f,"%d\n",(int)pid); /* errors ignored */
125 fclose(f); /* FIXME: error ignored */
129 static void client_timeout_remove(struct client *client)
131 g_return_if_fail(client!=NULL);
132 g_return_if_fail(client!=master);
134 if (client->timeout_id) {
135 gboolean errgboolean;
138 g_message(_("Client fd %d removed timeout id %d"),client->gpollfd.fd,client->timeout_id);
139 errgboolean=g_source_remove(client->timeout_id);
140 g_assert(errgboolean==TRUE);
141 client->timeout_id=0;
145 static gboolean client_touch_timeout(struct client *client);
147 static void client_touch(struct client *client,guint timeout_sec)
149 g_return_if_fail(client!=NULL);
150 g_return_if_fail(client!=master);
151 g_return_if_fail(timeout_sec>0);
153 client_timeout_remove(client);
154 client->timeout_id=g_timeout_add(
155 timeout_sec*1000, /* interval; msec */
156 (GSourceFunc)client_touch_timeout, /* function */
159 g_message(_("Client fd %d new timeout id %d"),client->gpollfd.fd,client->timeout_id);
160 g_assert(client->timeout_id!=0);
163 static void client_destroy(struct client *client);
164 static gboolean probe_send(struct client *probe,gint port_local);
166 static gboolean client_touch_timeout(struct client *client)
168 g_return_val_if_fail(client!=NULL,FALSE); /* FALSE=>should be removed */
169 g_return_val_if_fail(client!=master,FALSE); /* FALSE=>should be removed */
172 g_message(_("Client fd %d timeout id %d occured/entered"),client->gpollfd.fd,client->timeout_id);
174 /* Do not destroy the timeout in client_destroy().
175 * It would crash GLib - we remove it be returning FALSE from here.
177 g_assert(client->timeout_id!=0);
178 client->timeout_id=0;
181 if (probe_timeout_sec_now<probe_timeout_sec_max) {
182 probe_timeout_sec_now*=PROBE_TIMEOUT_SEC_BASE;
183 g_assert(probe_timeout_sec_max==G_MAXUINT || probe_timeout_sec_max<PROBE_TIMEOUT_SEC_MAX);
184 probe_timeout_sec_now=MIN(probe_timeout_sec_now,probe_timeout_sec_max);
185 probe_timeout_sec_now=MIN(probe_timeout_sec_now,PROBE_TIMEOUT_SEC_MAX);
186 if (probe_send(probe,port_local)) {
187 client_touch(probe,probe_timeout_sec_now); /* timeout */
188 return FALSE; /* GSource should be removed */
190 /* failure FALLTHRU */
193 /* Never destroy 'client' now - it has been destroyed by network_stop()! */
194 g_warning(_("No probe response from the server, stopping the daemon. Please check your public Internet connectivity."));
197 client_destroy(client);
201 g_message(_("Client timeout occurance finish"));
203 return FALSE; /* GSource should be removed */
206 static void handle_master_probe(const void *packet,size_t gotlen,const struct sockaddr_in *sockaddr_in_from)
208 GHashTable *got_hash;
209 gpointer got_unique_gpointer;
210 gpointer hostip_gpointer;
211 guint32 hostip_guint32;
213 g_return_if_fail(packet!=NULL);
214 g_return_if_fail(sockaddr_in_from!=NULL);
219 if (!(got_hash=packet_disassembly(packet,gotlen)))
221 if (!(g_hash_table_lookup_extended(
222 got_hash, /* hash_table */
223 GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32), /* lookup_key */
225 &got_unique_gpointer))) {
226 err_packet_disassembly_destroy_got_hash:
227 packet_disassembly_destroy(got_hash);
230 if (GPOINTER_TO_UINT(got_unique_gpointer)!=probe_unique)
231 goto err_packet_disassembly_destroy_got_hash;
232 if (!(g_hash_table_lookup_extended(
233 got_hash, /* hash_table */
234 GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_INADDR), /* lookup_key */
237 goto err_packet_disassembly_destroy_got_hash;
238 hostip_guint32=GPOINTER_TO_UINT(hostip_gpointer);
239 packet_disassembly_destroy(got_hash);
241 client_destroy(probe);
242 if (network_notify_hostip)
243 (*network_notify_hostip)(hostip_guint32);
246 static struct client *client_new(void);
248 static void handle_master(struct client *master)
250 g_return_if_fail(master!=NULL);
253 char packet[0x10000];
255 struct sockaddr_in sockaddr_in_from;
256 struct client *client=NULL /* Prevent false positive: might be used uninitialized */;
257 struct sockaddr_in sockaddr_in_server;
258 socklen_t sockaddr_in_from_length;
261 sockaddr_in_from_length=sizeof(sockaddr_in_from);
262 if (-1==(gotlen=recvfrom(
263 master->gpollfd.fd, /* s */
265 sizeof(packet), /* len */
267 (struct sockaddr *)&sockaddr_in_from, /* from */
268 &sockaddr_in_from_length))) /* fromlen */
271 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
274 if (packet_recognized(packet,gotlen)) {
275 handle_master_probe(packet,gotlen,&sockaddr_in_from);
278 /* Not yet initialized by 'probe' reply - drop it. */
281 g_message(_("Data packet received from %s but no probe reply yet; dropping packet."),
282 SOCKADDR_IN_TO_STRING(&sockaddr_in_from));
286 /* FIXME: Performance: Ugly search... */
287 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
288 client=clientl->data;
292 && client->sockaddr_in_from.sin_family ==sockaddr_in_from.sin_family
293 && client->sockaddr_in_from.sin_port ==sockaddr_in_from.sin_port
294 && client->sockaddr_in_from.sin_addr.s_addr==sockaddr_in_from.sin_addr.s_addr)
299 client->sockaddr_in_from=sockaddr_in_from;
301 client_touch(client,CLIENT_TIMEOUT_SEC);
302 UDPGATE_MEMZERO(&sockaddr_in_server);
303 sockaddr_in_server.sin_family=AF_INET;
304 sockaddr_in_server.sin_port=htons(SERVER_PORT);
305 sockaddr_in_server.sin_addr.s_addr=htonl(SERVER_INADDR);
306 /* FIXME: errors checking */
308 client->gpollfd.fd, /* s */
312 (struct sockaddr *)&sockaddr_in_server, /* to */
313 sizeof(sockaddr_in_server)); /* tolen */
317 static void handle_probe(struct client *probe)
319 g_return_if_fail(probe!=NULL);
323 struct sockaddr_in sockaddr_in_from;
324 char packet[0x10000];
325 socklen_t sockaddr_in_from_length;
327 sockaddr_in_from_length=sizeof(sockaddr_in_from);
328 if (-1==(gotlen=recvfrom(
329 master->gpollfd.fd, /* s */
331 sizeof(packet), /* len */
333 (struct sockaddr *)&sockaddr_in_from, /* from */
334 &sockaddr_in_from_length))) /* fromlen */
336 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
339 /* Probe socket should have no response; maybe some ICMP errors - ignored. */
343 static void handle_client(struct client *client)
345 g_return_if_fail(client!=NULL);
346 g_return_if_fail(master!=NULL);
350 struct sockaddr_in sockaddr_in_from;
351 char packet [0x10000];
352 socklen_t sockaddr_in_from_length;
354 sockaddr_in_from_length=sizeof(sockaddr_in_from);
355 if (-1==(gotlen=recvfrom(
356 client->gpollfd.fd, /* s */
358 sizeof(packet), /* len */
360 (struct sockaddr *)&sockaddr_in_from, /* from */
361 &sockaddr_in_from_length))) /* fromlen */
363 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
365 client_touch(client,CLIENT_TIMEOUT_SEC);
366 /* FIXME: errors checking */
368 master->gpollfd.fd, /* s */
372 (struct sockaddr *)&client->sockaddr_in_from, /* to */
373 sizeof(client->sockaddr_in_from)); /* tolen */
377 static gboolean sock_source_callback(gpointer data /* unused */)
381 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
382 struct client *client=clientl->data;
384 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS) {
385 /**/ if (client==master)
386 handle_master(client);
387 else if (client==probe)
388 handle_probe(client);
390 handle_client(client);
394 return TRUE; /* the source should be kept active */
397 static gboolean sock_source_prepare(GSource *source,gint *timeout)
404 static gboolean sock_source_check(GSource *source)
408 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
409 struct client *client=clientl->data;
411 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS)
417 static gboolean sock_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
419 g_assert(callback!=NULL);
420 return (*callback)(user_data);
423 static GSourceFuncs sock_source_watch_funcs={
426 sock_source_dispatch,
430 static void sock_gsource_destroy(void)
432 while (sock_client_list)
433 client_destroy(sock_client_list->data);
435 g_assert(master==NULL);
436 g_assert(probe==NULL);
439 g_source_destroy(sock_gsource);
443 write_daemon_running((pid_t)-1); /* unlink; errors ignored */
446 static gboolean sock_gsource_new(void)
451 g_assert(sock_client_list==NULL);
453 /* attach sock_source_callback() to watch for any abnormalities
454 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
456 if (!(sock_gsource=g_source_new(&sock_source_watch_funcs,sizeof(GSource)))) {
457 g_warning("g_source_new(): %m");
460 g_source_set_callback(
461 sock_gsource, /* source */
462 sock_source_callback, /* func */
465 if (!g_source_attach( /* returns 'guint' id */
466 sock_gsource, /* source */
467 NULL)) { /* context; NULL means 'default context' */
468 g_warning("g_source_attach(gsource,NULL): %m");
469 sock_gsource_destroy();
475 static struct client *client_new(void)
477 struct client *client;
478 static unsigned long oneul=1;
481 if (!sock_gsource_new())
484 if (-1==(sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))) {
485 g_warning("socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP): %m");
488 if (ioctl(sock,FIONBIO,&oneul)) { /* non-blocking mode */
489 g_warning("ioctl(sock,FIONBIO,&1): %m");
490 close(sock); /* errors ignored */
495 client->gpollfd.fd=sock;
496 client->gpollfd.events=SOCK_SOURCE_CHECK_EVENTS;
497 client->gpollfd.revents=0;
498 client->timeout_id=0;
499 sock_client_list=g_list_prepend(sock_client_list,client);
500 g_source_add_poll(sock_gsource,&client->gpollfd);
503 g_message(_("Client fd %d created"),client->gpollfd.fd);
508 static void client_destroy(struct client *client)
510 g_return_if_fail(client!=NULL);
512 if (!sock_gsource_new())
516 g_message(_("Client fd %d timeout id %d destroy enter"),client->gpollfd.fd,client->timeout_id);
518 if (client==master) {
520 g_assert(client->timeout_id==0);
525 client_timeout_remove(client);
528 g_source_remove_poll(sock_gsource,&client->gpollfd);
529 sock_client_list=g_list_remove(sock_client_list,client);
530 close(client->gpollfd.fd); /* errors ignored */
533 g_message(_("Client fd %d timeout id %d destroy finish"),client->gpollfd.fd,client->timeout_id);
538 static gboolean probe_send(struct client *probe,gint port_local)
540 struct sockaddr_in sockaddr_in_server;
541 GHashTable *probe_hash;
543 size_t packet_length;
545 g_return_val_if_fail(probe!=NULL,FALSE);
547 probe_unique=g_random_int();
549 probe_hash=g_hash_table_new(
550 g_direct_hash, /* hash_func */
551 g_direct_equal); /* key_equal_func */
552 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_PORT) ,GUINT_TO_POINTER(port_local));
553 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32),GUINT_TO_POINTER(probe_unique));
554 packet=packet_assembly(&packet_length,probe_hash);
555 g_hash_table_destroy(probe_hash);
559 UDPGATE_MEMZERO(&sockaddr_in_server);
560 sockaddr_in_server.sin_family=AF_INET;
561 sockaddr_in_server.sin_port=htons(PROBE_PORT);
562 sockaddr_in_server.sin_addr.s_addr=htonl(PROBE_INADDR);
563 /* FIXME: errors checking */
565 probe->gpollfd.fd, /* s */
567 packet_length, /* len */
569 (struct sockaddr *)&sockaddr_in_server, /* to */
570 sizeof(sockaddr_in_server)); /* tolen */
575 static gboolean master_start(gint port)
577 struct sockaddr_in sockaddr_in;
579 g_return_val_if_fail(port>=0,FALSE);
580 g_return_val_if_fail(master==NULL,FALSE);
582 /* Setup 'master': */
583 if (!(master=client_new()))
585 UDPGATE_MEMZERO(&sockaddr_in);
586 sockaddr_in.sin_family=AF_INET;
587 sockaddr_in.sin_port=htons(port);
588 sockaddr_in.sin_addr.s_addr=htonl(INADDR_ANY);
589 if (bind(master->gpollfd.fd,(struct sockaddr *)&sockaddr_in,sizeof(sockaddr_in))) {
590 g_warning("bind(sock,{AF_INET,INADDR_ANY:%d}): %m",(int)port);
596 static gboolean probe_start(gint port)
598 g_return_val_if_fail(port>=0,FALSE);
599 g_return_val_if_fail(probe==NULL,FALSE);
602 if (!(probe=client_new()))
605 if (!probe_send(probe,port)) {
606 client_destroy(probe);
609 probe_timeout_sec_now=PROBE_TIMEOUT_SEC_BASE;
610 client_touch(probe,probe_timeout_sec_now); /* timeout */
614 gboolean network_start(gint port)
618 g_return_val_if_fail(port>=0,FALSE);
620 if ((pid_t)-1!=(daemon_pid=is_daemon_running())) {
621 g_warning(_("Cannot start network daemon: Daemon is already running on PID %d"),(int)daemon_pid);
624 if (!master_start(port)) {
625 sock_gsource_destroy();
628 if (!probe_start(port)) {
629 sock_gsource_destroy();
632 write_daemon_running(getpid()); /* errors ignored */
633 if (network_notify_hostip)
634 (*network_notify_hostip)(0);
638 gboolean optarg_port_set_string(const gchar *port_string)
643 g_return_val_if_fail(port_string!=NULL,FALSE);
645 port_long=strtol(port_string,&endp,0);
647 g_warning(_("Invalid port specification, offending string: %s"),endp);
650 if (port_long<1 || port_long>=G_MAXINT || (endp && *endp)) {
651 g_warning(_("Invalid port integer number specification (%ld)"),port_long);
654 optarg_port=port_long;
658 gboolean network_stop(void)
663 if ((pid_t)-1==(daemon_pid=is_daemon_running())) {
664 g_warning(_("Cannot stop network daemon: Daemon is not running"));
667 if (daemon_pid==getpid()) {
668 sock_gsource_destroy();
672 kill(daemon_pid,SIGKILL);
674 write_daemon_running((pid_t)-1); /* unlink; errors ignored */
676 g_warning(udpgate_printf_alloca(_("Unable to stop the daemon at PID %d: %s"),
677 (int)daemon_pid,strerror(errno_save)));
681 if (network_notify_hostip)
682 (*network_notify_hostip)(0);
686 static GMainLoop *gmainloop;
687 static void network_detach_network_notify_hostip(guint32 hostip_guint32)
690 g_main_loop_quit(gmainloop);
693 gboolean network_detach(void)
695 pid_t daemon_pid,forked_pid;
697 if ((pid_t)-1==(daemon_pid=is_daemon_running()))
699 if (getpid()!=daemon_pid)
701 if (!optarg_no_fork) {
702 if ((pid_t)-1==(forked_pid=fork())) {
703 g_warning("fork(2): %m");
710 write_daemon_running(getpid()); /* errors ignored */
713 close(STDOUT_FILENO);
714 close(STDERR_FILENO);
719 network_notify_hostip=network_detach_network_notify_hostip;
720 gmainloop=g_main_loop_new(
722 TRUE); /* is_running; ignored */
723 g_main_loop_run(gmainloop); /* loop */
724 /* Unable to contact the server, aborting. */