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 0x7F000001
50 #define SERVER_INADDR 0x511F02EA /* paulina.vellum.cz = 81.31.2.234; host order */
51 #define SERVER_PORT 9201 /* host order */
52 #define PROBE_INADDR SERVER_INADDR /* host order */
53 #define PROBE_PORT 8202 /* host order */
54 #define CLIENT_TIMEOUT_SEC (5*60)
55 #define PROBE_TIMEOUT_SEC_BASE 5
56 #define PROBE_TIMEOUT_SEC_MAX 3500
59 void (*network_notify_hostip)(guint32 hostip_guint32);
64 struct sockaddr_in sockaddr_in_from;
68 static GSource *sock_gsource;
70 static GList *sock_client_list; /* of 'struct client *', incl. 'master' and 'probe' */
71 static struct client *master; /* no 'timeout' permitted */
72 static struct client *probe; /* 'timeout' permitted */
73 static guint32 probe_unique;
74 static guint probe_timeout_sec_now;
75 guint probe_timeout_sec_max=G_MAXUINT;
76 static gint port_local; /* for 'probe' resends */
78 static gboolean write_daemon_running(pid_t pid);
80 pid_t is_daemon_running(void)
83 char buf[LINE_MAX],*got;
89 if (!(f=fopen(NETWORK_PATHNAME_PID,"r")))
91 got=fgets(buf,sizeof(buf),f);
92 fclose(f); /* FIXME: error ignored */
95 write_daemon_running((pid_t)-1); /* unlink */
102 if (kill((pid_t)pid_int,0)) {
107 return (pid_t)pid_int;
110 static gboolean write_daemon_running(pid_t pid)
114 if (pid==(pid_t)-1) {
115 if (unlink(NETWORK_PATHNAME_PID)) {
117 g_warning(_("Error removing PID file \"%s\": %m"),NETWORK_PATHNAME_PID);
122 if (!(f=fopen(NETWORK_PATHNAME_PID,"w"))) {
123 g_warning(_("Error writing PID %d to \"%s\": %m"),(int)pid,NETWORK_PATHNAME_PID);
126 fprintf(f,"%d\n",(int)pid); /* errors ignored */
127 fclose(f); /* FIXME: error ignored */
131 static void client_timeout_remove(struct client *client)
133 g_return_if_fail(client!=NULL);
134 g_return_if_fail(client!=master);
136 if (client->timeout_id) {
137 gboolean errgboolean;
140 g_message(_("Client fd %d removed timeout id %d"),client->gpollfd.fd,client->timeout_id);
141 errgboolean=g_source_remove(client->timeout_id);
142 g_assert(errgboolean==TRUE);
143 client->timeout_id=0;
147 static gboolean client_touch_timeout(struct client *client);
149 static void client_touch(struct client *client,guint timeout_sec)
151 g_return_if_fail(client!=NULL);
152 g_return_if_fail(client!=master);
153 g_return_if_fail(timeout_sec>0);
155 client_timeout_remove(client);
156 client->timeout_id=g_timeout_add(
157 timeout_sec*1000, /* interval; msec */
158 (GSourceFunc)client_touch_timeout, /* function */
161 g_message(_("Client fd %d new timeout id %d"),client->gpollfd.fd,client->timeout_id);
162 g_assert(client->timeout_id!=0);
165 static void client_destroy(struct client *client);
166 static gboolean probe_send(struct client *probe,gint port_local);
168 static gboolean client_touch_timeout(struct client *client)
170 g_return_val_if_fail(client!=NULL,FALSE); /* FALSE=>should be removed */
171 g_return_val_if_fail(client!=master,FALSE); /* FALSE=>should be removed */
174 g_message(_("Client fd %d timeout id %d occured/entered"),client->gpollfd.fd,client->timeout_id);
176 /* Do not destroy the timeout in client_destroy().
177 * It would crash GLib - we remove it be returning FALSE from here.
179 g_assert(client->timeout_id!=0);
180 client->timeout_id=0;
183 if (probe_timeout_sec_now<probe_timeout_sec_max) {
184 probe_timeout_sec_now*=PROBE_TIMEOUT_SEC_BASE;
185 g_assert(probe_timeout_sec_max==G_MAXUINT || probe_timeout_sec_max<PROBE_TIMEOUT_SEC_MAX);
186 probe_timeout_sec_now=MIN(probe_timeout_sec_now,probe_timeout_sec_max);
187 probe_timeout_sec_now=MIN(probe_timeout_sec_now,PROBE_TIMEOUT_SEC_MAX);
188 if (probe_send(probe,port_local)) {
189 client_touch(probe,probe_timeout_sec_now); /* timeout */
190 return FALSE; /* GSource should be removed */
192 /* failure FALLTHRU */
195 /* Never destroy 'client' now - it has been destroyed by network_stop()! */
196 g_warning(_("No probe response from the server, stopping the daemon. Please check your public Internet connectivity."));
199 client_destroy(client);
203 g_message(_("Client timeout occurance finish"));
205 return FALSE; /* GSource should be removed */
208 static void handle_master_probe(const void *packet,size_t gotlen,const struct sockaddr_in *sockaddr_in_from)
210 GHashTable *got_hash;
211 gpointer got_unique_gpointer;
212 gpointer hostip_gpointer;
213 guint32 hostip_guint32;
215 g_return_if_fail(packet!=NULL);
216 g_return_if_fail(sockaddr_in_from!=NULL);
221 if (!(got_hash=packet_disassembly(packet,gotlen)))
223 if (!(g_hash_table_lookup_extended(
224 got_hash, /* hash_table */
225 GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32), /* lookup_key */
227 &got_unique_gpointer))) {
228 err_packet_disassembly_destroy_got_hash:
229 packet_disassembly_destroy(got_hash);
232 if (GPOINTER_TO_UINT(got_unique_gpointer)!=probe_unique)
233 goto err_packet_disassembly_destroy_got_hash;
234 if (!(g_hash_table_lookup_extended(
235 got_hash, /* hash_table */
236 GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_INADDR), /* lookup_key */
239 goto err_packet_disassembly_destroy_got_hash;
240 hostip_guint32=GPOINTER_TO_UINT(hostip_gpointer);
241 packet_disassembly_destroy(got_hash);
243 client_destroy(probe);
244 if (network_notify_hostip)
245 (*network_notify_hostip)(hostip_guint32);
248 static struct client *client_new(void);
250 static void handle_master(struct client *master)
252 g_return_if_fail(master!=NULL);
255 char packet[0x10000];
257 struct sockaddr_in sockaddr_in_from;
258 struct client *client=NULL /* Prevent false positive: might be used uninitialized */;
259 struct sockaddr_in sockaddr_in_server;
260 socklen_t sockaddr_in_from_length;
263 sockaddr_in_from_length=sizeof(sockaddr_in_from);
264 if (-1==(gotlen=recvfrom(
265 master->gpollfd.fd, /* s */
267 sizeof(packet), /* len */
269 (struct sockaddr *)&sockaddr_in_from, /* from */
270 &sockaddr_in_from_length))) /* fromlen */
273 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
276 if (packet_recognized(packet,gotlen)) {
277 handle_master_probe(packet,gotlen,&sockaddr_in_from);
280 /* Not yet initialized by 'probe' reply - drop it. */
284 /* FIXME: Performance: Ugly search... */
285 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
286 client=clientl->data;
290 && client->sockaddr_in_from.sin_family ==sockaddr_in_from.sin_family
291 && client->sockaddr_in_from.sin_port ==sockaddr_in_from.sin_port
292 && client->sockaddr_in_from.sin_addr.s_addr==sockaddr_in_from.sin_addr.s_addr)
297 client->sockaddr_in_from=sockaddr_in_from;
299 client_touch(client,CLIENT_TIMEOUT_SEC);
300 UDPGATE_MEMZERO(&sockaddr_in_server);
301 sockaddr_in_server.sin_family=AF_INET;
302 sockaddr_in_server.sin_port=htons(SERVER_PORT);
303 sockaddr_in_server.sin_addr.s_addr=htonl(SERVER_INADDR);
304 /* FIXME: errors checking */
306 client->gpollfd.fd, /* s */
310 (struct sockaddr *)&sockaddr_in_server, /* to */
311 sizeof(sockaddr_in_server)); /* tolen */
315 static void handle_probe(struct client *probe)
317 g_return_if_fail(probe!=NULL);
321 struct sockaddr_in sockaddr_in_from;
322 char packet[0x10000];
323 socklen_t sockaddr_in_from_length;
325 sockaddr_in_from_length=sizeof(sockaddr_in_from);
326 if (-1==(gotlen=recvfrom(
327 master->gpollfd.fd, /* s */
329 sizeof(packet), /* len */
331 (struct sockaddr *)&sockaddr_in_from, /* from */
332 &sockaddr_in_from_length))) /* fromlen */
334 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
337 /* Probe socket should have no response; maybe some ICMP errors - ignored. */
341 static void handle_client(struct client *client)
343 g_return_if_fail(client!=NULL);
344 g_return_if_fail(master!=NULL);
348 struct sockaddr_in sockaddr_in_from;
349 char packet [0x10000];
350 socklen_t sockaddr_in_from_length;
352 sockaddr_in_from_length=sizeof(sockaddr_in_from);
353 if (-1==(gotlen=recvfrom(
354 client->gpollfd.fd, /* s */
356 sizeof(packet), /* len */
358 (struct sockaddr *)&sockaddr_in_from, /* from */
359 &sockaddr_in_from_length))) /* fromlen */
361 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
363 client_touch(client,CLIENT_TIMEOUT_SEC);
364 /* FIXME: errors checking */
366 master->gpollfd.fd, /* s */
370 (struct sockaddr *)&client->sockaddr_in_from, /* to */
371 sizeof(client->sockaddr_in_from)); /* tolen */
375 static gboolean sock_source_callback(gpointer data /* unused */)
379 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
380 struct client *client=clientl->data;
382 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS) {
383 /**/ if (client==master)
384 handle_master(client);
385 else if (client==probe)
386 handle_probe(client);
388 handle_client(client);
392 return TRUE; /* the source should be kept active */
395 static gboolean sock_source_prepare(GSource *source,gint *timeout)
402 static gboolean sock_source_check(GSource *source)
406 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
407 struct client *client=clientl->data;
409 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS)
415 static gboolean sock_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
417 g_assert(callback!=NULL);
418 return (*callback)(user_data);
421 static GSourceFuncs sock_source_watch_funcs={
424 sock_source_dispatch,
428 static void sock_gsource_destroy(void)
430 while (sock_client_list)
431 client_destroy(sock_client_list->data);
433 g_assert(master==NULL);
434 g_assert(probe==NULL);
437 g_source_destroy(sock_gsource);
441 write_daemon_running((pid_t)-1); /* unlink; errors ignored */
444 static gboolean sock_gsource_new(void)
449 g_assert(sock_client_list==NULL);
451 /* attach sock_source_callback() to watch for any abnormalities
452 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
454 if (!(sock_gsource=g_source_new(&sock_source_watch_funcs,sizeof(GSource)))) {
455 g_warning("g_source_new(): %m");
458 g_source_set_callback(
459 sock_gsource, /* source */
460 sock_source_callback, /* func */
463 if (!g_source_attach( /* returns 'guint' id */
464 sock_gsource, /* source */
465 NULL)) { /* context; NULL means 'default context' */
466 g_warning("g_source_attach(gsource,NULL): %m");
467 sock_gsource_destroy();
473 static struct client *client_new(void)
475 struct client *client;
476 static unsigned long oneul=1;
479 if (!sock_gsource_new())
482 if (-1==(sock=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))) {
483 g_warning("socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP): %m");
486 if (ioctl(sock,FIONBIO,&oneul)) { /* non-blocking mode */
487 g_warning("ioctl(sock,FIONBIO,&1): %m");
488 close(sock); /* errors ignored */
493 client->gpollfd.fd=sock;
494 client->gpollfd.events=SOCK_SOURCE_CHECK_EVENTS;
495 client->gpollfd.revents=0;
496 client->timeout_id=0;
497 sock_client_list=g_list_prepend(sock_client_list,client);
498 g_source_add_poll(sock_gsource,&client->gpollfd);
501 g_message(_("Client fd %d created"),client->gpollfd.fd);
506 static void client_destroy(struct client *client)
508 g_return_if_fail(client!=NULL);
510 if (!sock_gsource_new())
514 g_message(_("Client fd %d timeout id %d destroy enter"),client->gpollfd.fd,client->timeout_id);
516 if (client==master) {
518 g_assert(client->timeout_id==0);
523 client_timeout_remove(client);
526 g_source_remove_poll(sock_gsource,&client->gpollfd);
527 sock_client_list=g_list_remove(sock_client_list,client);
528 close(client->gpollfd.fd); /* errors ignored */
531 g_message(_("Client fd %d timeout id %d destroy finish"),client->gpollfd.fd,client->timeout_id);
536 static gboolean probe_send(struct client *probe,gint port_local)
538 struct sockaddr_in sockaddr_in_server;
539 GHashTable *probe_hash;
541 size_t packet_length;
543 g_return_val_if_fail(probe!=NULL,FALSE);
545 probe_unique=g_random_int();
547 probe_hash=g_hash_table_new(
548 g_direct_hash, /* hash_func */
549 g_direct_equal); /* key_equal_func */
550 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_PORT) ,GUINT_TO_POINTER(port_local));
551 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32),GUINT_TO_POINTER(probe_unique));
552 packet=packet_assembly(&packet_length,probe_hash);
553 g_hash_table_destroy(probe_hash);
557 UDPGATE_MEMZERO(&sockaddr_in_server);
558 sockaddr_in_server.sin_family=AF_INET;
559 sockaddr_in_server.sin_port=htons(PROBE_PORT);
560 sockaddr_in_server.sin_addr.s_addr=htonl(PROBE_INADDR);
561 /* FIXME: errors checking */
563 probe->gpollfd.fd, /* s */
565 packet_length, /* len */
567 (struct sockaddr *)&sockaddr_in_server, /* to */
568 sizeof(sockaddr_in_server)); /* tolen */
573 static gboolean master_start(gint port)
575 struct sockaddr_in sockaddr_in;
577 g_return_val_if_fail(port>=0,FALSE);
578 g_return_val_if_fail(master==NULL,FALSE);
580 /* Setup 'master': */
581 if (!(master=client_new()))
583 UDPGATE_MEMZERO(&sockaddr_in);
584 sockaddr_in.sin_family=AF_INET;
585 sockaddr_in.sin_port=htons(port);
586 sockaddr_in.sin_addr.s_addr=htonl(INADDR_ANY);
587 if (bind(master->gpollfd.fd,(struct sockaddr *)&sockaddr_in,sizeof(sockaddr_in))) {
588 g_warning("bind(sock,{AF_INET,INADDR_ANY:%d}): %m",(int)port);
594 static gboolean probe_start(gint port)
596 g_return_val_if_fail(port>=0,FALSE);
597 g_return_val_if_fail(probe==NULL,FALSE);
600 if (!(probe=client_new()))
603 if (!probe_send(probe,port)) {
604 client_destroy(probe);
607 probe_timeout_sec_now=PROBE_TIMEOUT_SEC_BASE;
608 client_touch(probe,probe_timeout_sec_now); /* timeout */
612 gboolean network_start(gint port)
616 g_return_val_if_fail(port>=0,FALSE);
618 if ((pid_t)-1!=(daemon_pid=is_daemon_running())) {
619 g_warning(_("Cannot start network daemon: Daemon is already running on PID %d"),(int)daemon_pid);
622 if (!master_start(port)) {
623 sock_gsource_destroy();
626 if (!probe_start(port)) {
627 sock_gsource_destroy();
630 write_daemon_running(getpid()); /* errors ignored */
631 if (network_notify_hostip)
632 (*network_notify_hostip)(0);
636 gboolean optarg_port_set_string(const gchar *port_string)
641 g_return_val_if_fail(port_string!=NULL,FALSE);
643 port_long=strtol(port_string,&endp,0);
645 g_warning(_("Invalid port specification, offending string: %s"),endp);
648 if (port_long<1 || port_long>=G_MAXINT || (endp && *endp)) {
649 g_warning(_("Invalid port integer number specification (%ld)"),port_long);
652 optarg_port=port_long;
656 gboolean network_stop(void)
661 if ((pid_t)-1==(daemon_pid=is_daemon_running())) {
662 g_warning(_("Cannot stop network daemon: Daemon is not running"));
665 if (daemon_pid==getpid()) {
666 sock_gsource_destroy();
670 kill(daemon_pid,SIGKILL);
672 write_daemon_running((pid_t)-1); /* unlink; errors ignored */
674 g_warning(udpgate_printf_alloca(_("Unable to stop the daemon at PID %d: %s"),
675 (int)daemon_pid,strerror(errno_save)));
679 if (network_notify_hostip)
680 (*network_notify_hostip)(0);
684 static GMainLoop *gmainloop;
685 static void network_detach_network_notify_hostip(guint32 hostip_guint32)
688 g_main_loop_quit(gmainloop);
691 gboolean network_detach(void)
693 pid_t daemon_pid,forked_pid;
695 if ((pid_t)-1==(daemon_pid=is_daemon_running()))
697 if (getpid()!=daemon_pid)
699 if (!optarg_no_fork) {
700 if ((pid_t)-1==(forked_pid=fork())) {
701 g_warning("fork(2): %m");
708 write_daemon_running(getpid()); /* errors ignored */
711 close(STDOUT_FILENO);
712 close(STDERR_FILENO);
717 network_notify_hostip=network_detach_network_notify_hostip;
718 gmainloop=g_main_loop_new(
720 TRUE); /* is_running; ignored */
721 g_main_loop_run(gmainloop); /* loop */
722 /* Unable to contact the server, aborting. */