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 8201 /* host order */
54 #define CLIENT_TIMEOUT_SEC (5*60)
55 #define PROBE_TIMEOUT_SEC (5)
58 void (*network_notify_hostip)(guint32 hostip_guint32);
63 struct sockaddr_in sockaddr_in_from;
67 static GSource *sock_gsource;
69 static GList *sock_client_list; /* of 'struct client *', incl. 'master' and 'probe' */
70 static struct client *master; /* no 'timeout' permitted */
71 static struct client *probe; /* 'timeout' permitted */
72 static guint32 probe_unique;
74 static gboolean write_daemon_running(pid_t pid);
76 pid_t is_daemon_running(void)
79 char buf[LINE_MAX],*got;
85 if (!(f=fopen(NETWORK_PATHNAME_PID,"r")))
87 got=fgets(buf,sizeof(buf),f);
88 fclose(f); /* FIXME: error ignored */
91 write_daemon_running((pid_t)-1); /* unlink */
98 if (kill((pid_t)pid_int,0)) {
103 return (pid_t)pid_int;
106 static gboolean write_daemon_running(pid_t pid)
110 if (pid==(pid_t)-1) {
111 if (unlink(NETWORK_PATHNAME_PID)) {
113 g_warning(_("Error removing PID file \"%s\": %m"),NETWORK_PATHNAME_PID);
118 if (!(f=fopen(NETWORK_PATHNAME_PID,"w"))) {
119 g_warning(_("Error writing PID %d to \"%s\": %m"),(int)pid,NETWORK_PATHNAME_PID);
122 fprintf(f,"%d\n",(int)pid); /* errors ignored */
123 fclose(f); /* FIXME: error ignored */
127 static void client_timeout_remove(struct client *client)
129 g_return_if_fail(client!=NULL);
130 g_return_if_fail(client!=master);
132 if (client->timeout_id) {
133 gboolean errgboolean;
136 g_message(_("Client fd %d removed timeout id %d"),client->gpollfd.fd,client->timeout_id);
137 errgboolean=g_source_remove(client->timeout_id);
138 g_assert(errgboolean==TRUE);
139 client->timeout_id=0;
143 static gboolean client_touch_timeout(struct client *client);
145 static void client_touch(struct client *client)
147 g_return_if_fail(client!=NULL);
148 g_return_if_fail(client!=master);
150 client_timeout_remove(client);
151 client->timeout_id=g_timeout_add(
152 (client==probe ? PROBE_TIMEOUT_SEC*1000 : CLIENT_TIMEOUT_SEC*1000), /* interval; msec */
153 (GSourceFunc)client_touch_timeout, /* function */
156 g_message(_("Client fd %d new timeout id %d"),client->gpollfd.fd,client->timeout_id);
157 g_assert(client->timeout_id!=0);
160 static void client_destroy(struct client *client);
162 static gboolean client_touch_timeout(struct client *client)
164 g_return_val_if_fail(client!=NULL,FALSE); /* FALSE=>should be removed */
165 g_return_val_if_fail(client!=master,FALSE); /* FALSE=>should be removed */
168 g_message(_("Client fd %d timeout id %d occured/entered"),client->gpollfd.fd,client->timeout_id);
170 /* Do not destroy the timeout in client_destroy().
171 * It would crash GLib - we remove it be returning FALSE from here.
173 g_assert(client->timeout_id!=0);
174 client->timeout_id=0;
178 /* Never destroy 'client' now - it has been destroyed by network_stop()! */
181 client_destroy(client);
185 g_message(_("Client timeout occurance finish"));
187 return FALSE; /* GSource should be removed */
190 static void handle_master_probe(const void *packet,size_t gotlen,const struct sockaddr_in *sockaddr_in_from)
192 GHashTable *got_hash;
193 gpointer got_unique_gpointer;
194 gpointer hostip_gpointer;
195 guint32 hostip_guint32;
197 g_return_if_fail(packet!=NULL);
198 g_return_if_fail(sockaddr_in_from!=NULL);
203 if (!(got_hash=packet_disassembly(packet,gotlen)))
205 if (!(g_hash_table_lookup_extended(
206 got_hash, /* hash_table */
207 GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32), /* lookup_key */
209 &got_unique_gpointer))) {
210 err_packet_disassembly_destroy_got_hash:
211 packet_disassembly_destroy(got_hash);
214 if (GPOINTER_TO_UINT(got_unique_gpointer)!=probe_unique)
215 goto err_packet_disassembly_destroy_got_hash;
216 if (!(g_hash_table_lookup_extended(
217 got_hash, /* hash_table */
218 GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_INADDR), /* lookup_key */
221 goto err_packet_disassembly_destroy_got_hash;
222 hostip_guint32=GPOINTER_TO_UINT(hostip_gpointer);
223 packet_disassembly_destroy(got_hash);
225 client_destroy(probe);
226 if (network_notify_hostip)
227 (*network_notify_hostip)(hostip_guint32);
230 static struct client *client_new(void);
232 static void handle_master(struct client *master)
235 struct sockaddr_in sockaddr_in_from;
236 char packet[0x10000];
237 struct client *client;
238 struct sockaddr_in sockaddr_in_server;
239 socklen_t sockaddr_in_from_length;
241 g_return_if_fail(master!=NULL);
243 sockaddr_in_from_length=sizeof(sockaddr_in_from);
244 while (-1!=(gotlen=recvfrom(
245 master->gpollfd.fd, /* s */
247 sizeof(packet), /* len */
249 (struct sockaddr *)&sockaddr_in_from, /* from */
250 &sockaddr_in_from_length))) { /* fromlen */
253 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
256 if (packet_recognized(packet,gotlen)) {
257 handle_master_probe(packet,gotlen,&sockaddr_in_from);
261 /* FIXME: Performance: Ugly search... */
262 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
263 client=clientl->data;
267 && client->sockaddr_in_from.sin_family ==sockaddr_in_from.sin_family
268 && client->sockaddr_in_from.sin_port ==sockaddr_in_from.sin_port
269 && client->sockaddr_in_from.sin_addr.s_addr==sockaddr_in_from.sin_addr.s_addr)
274 client->sockaddr_in_from=sockaddr_in_from;
276 client_touch(client);
277 UDPGATE_MEMZERO(&sockaddr_in_server);
278 sockaddr_in_server.sin_family=AF_INET;
279 sockaddr_in_server.sin_port=htons(SERVER_PORT);
280 sockaddr_in_server.sin_addr.s_addr=htonl(SERVER_INADDR);
281 /* FIXME: errors checking */
283 client->gpollfd.fd, /* s */
287 (struct sockaddr *)&sockaddr_in_server, /* to */
288 sizeof(sockaddr_in_server)); /* tolen */
292 static void handle_probe(struct client *probe)
295 struct sockaddr_in sockaddr_in_from;
296 char packet[0x10000];
297 socklen_t sockaddr_in_from_length;
299 g_return_if_fail(probe!=NULL);
301 sockaddr_in_from_length=sizeof(sockaddr_in_from);
302 while (-1!=(gotlen=recvfrom(
303 master->gpollfd.fd, /* s */
305 sizeof(packet), /* len */
307 (struct sockaddr *)&sockaddr_in_from, /* from */
308 &sockaddr_in_from_length))) { /* fromlen */
309 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
312 /* Probe socket should have no response; maybe some ICMP errors - ignored. */
316 static void handle_client(struct client *client)
319 struct sockaddr_in sockaddr_in_from;
320 char packet [0x10000];
321 socklen_t sockaddr_in_from_length;
323 g_return_if_fail(client!=NULL);
324 g_return_if_fail(master!=NULL);
326 while (-1!=(gotlen=recvfrom(
327 client->gpollfd.fd, /* s */
329 sizeof(packet), /* len */
331 (struct sockaddr *)&sockaddr_in_from, /* from */
332 &sockaddr_in_from_length))) { /* fromlen */
333 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
335 client_touch(client);
336 /* FIXME: errors checking */
338 master->gpollfd.fd, /* s */
342 (struct sockaddr *)&client->sockaddr_in_from, /* to */
343 sizeof(client->sockaddr_in_from)); /* tolen */
347 static gboolean sock_source_callback(gpointer data /* unused */)
351 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
352 struct client *client=clientl->data;
354 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS) {
355 /**/ if (client==master)
356 handle_master(client);
357 else if (client==probe)
358 handle_probe(client);
360 handle_client(client);
364 return TRUE; /* the source should be kept active */
367 static gboolean sock_source_prepare(GSource *source,gint *timeout)
374 static gboolean sock_source_check(GSource *source)
378 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
379 struct client *client=clientl->data;
381 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS)
387 static gboolean sock_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
389 g_assert(callback!=NULL);
390 return (*callback)(user_data);
393 static GSourceFuncs sock_source_watch_funcs={
396 sock_source_dispatch,
400 static void sock_gsource_destroy(void)
402 while (sock_client_list)
403 client_destroy(sock_client_list->data);
405 g_assert(master==NULL);
408 g_source_destroy(sock_gsource);
412 write_daemon_running((pid_t)-1); /* unlink */
415 static gboolean sock_gsource_new(void)
420 g_assert(sock_client_list==NULL);
422 /* attach sock_source_callback() to watch for any abnormalities
423 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
425 if (!(sock_gsource=g_source_new(&sock_source_watch_funcs,sizeof(GSource)))) {
426 g_warning("g_source_new(): %m");
429 g_source_set_callback(
430 sock_gsource, /* source */
431 sock_source_callback, /* func */
434 if (!g_source_attach( /* returns 'guint' id */
435 sock_gsource, /* source */
436 NULL)) { /* context; NULL means 'default context' */
437 g_warning("g_source_attach(gsource,NULL): %m");
438 sock_gsource_destroy();
444 static struct client *client_new(void)
446 struct client *client;
447 static unsigned long oneul=1;
450 if (!sock_gsource_new())
453 if (-1==(sock=socket(AF_INET,SOCK_DGRAM,0))) {
454 g_warning("socket(AF_INET,SOCK_DGRAM): %m");
457 if (ioctl(sock,FIONBIO,&oneul)) { /* non-blocking mode */
458 g_warning("ioctl(sock,FIONBIO,&1): %m");
459 close(sock); /* errors ignored */
464 client->gpollfd.fd=sock;
465 client->gpollfd.events=SOCK_SOURCE_CHECK_EVENTS;
466 client->gpollfd.revents=0;
467 client->timeout_id=0;
468 sock_client_list=g_list_prepend(sock_client_list,client);
469 g_source_add_poll(sock_gsource,&client->gpollfd);
472 g_message(_("Client fd %d created"),client->gpollfd.fd);
477 static void client_destroy(struct client *client)
479 g_return_if_fail(client!=NULL);
481 if (!sock_gsource_new())
485 g_message(_("Client fd %d timeout id %d destroy enter"),client->gpollfd.fd,client->timeout_id);
487 if (client==master) {
489 g_assert(client->timeout_id==0);
494 client_timeout_remove(client);
497 g_source_remove_poll(sock_gsource,&client->gpollfd);
498 sock_client_list=g_list_remove(sock_client_list,client);
499 close(client->gpollfd.fd); /* errors ignored */
502 g_message(_("Client fd %d timeout id %d destroy finish"),client->gpollfd.fd,client->timeout_id);
507 static gboolean probe_send(struct client *probe,gint port_local)
509 struct sockaddr_in sockaddr_in_server;
510 GHashTable *probe_hash;
512 size_t packet_length;
514 g_return_val_if_fail(probe!=NULL,FALSE);
516 probe_unique=g_random_int();
518 probe_hash=g_hash_table_new(
519 g_direct_hash, /* hash_func */
520 g_direct_equal); /* key_equal_func */
521 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_PORT) ,GUINT_TO_POINTER(port_local));
522 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32),GUINT_TO_POINTER(probe_unique));
523 packet=packet_assembly(&packet_length,probe_hash);
524 g_hash_table_destroy(probe_hash);
528 UDPGATE_MEMZERO(&sockaddr_in_server);
529 sockaddr_in_server.sin_family=AF_INET;
530 sockaddr_in_server.sin_port=htons(PROBE_PORT);
531 sockaddr_in_server.sin_addr.s_addr=htonl(PROBE_INADDR);
532 /* FIXME: errors checking */
534 probe->gpollfd.fd, /* s */
536 packet_length, /* len */
538 (struct sockaddr *)&sockaddr_in_server, /* to */
539 sizeof(sockaddr_in_server)); /* tolen */
544 gboolean network_start(gint port)
547 struct sockaddr_in sockaddr_in;
549 g_return_val_if_fail(port>=0,FALSE);
551 if ((pid_t)-1!=(daemon_pid=is_daemon_running())) {
552 g_warning(_("Cannot start network daemon: Daemon is already running on PID %d"),(int)daemon_pid);
556 /* Setup 'master': */
557 g_assert(master==NULL);
558 if (!(master=client_new()))
560 UDPGATE_MEMZERO(&sockaddr_in);
561 sockaddr_in.sin_family=AF_INET;
562 sockaddr_in.sin_port=htons(port);
563 sockaddr_in.sin_addr.s_addr=htonl(INADDR_ANY);
564 if (bind(master->gpollfd.fd,(struct sockaddr *)&sockaddr_in,sizeof(sockaddr_in))) {
565 g_warning("bind(sock,{AF_INET,INADDR_ANY:%d}): %m",(int)port);
566 err_sock_gsource_destroy:
567 sock_gsource_destroy();
572 if (!(probe=client_new()))
573 goto err_sock_gsource_destroy;
574 probe_send(probe,port);
575 client_touch(probe); /* timeout */
577 write_daemon_running(getpid());
578 if (network_notify_hostip)
579 (*network_notify_hostip)(0);
583 gboolean network_stop(void)
588 if ((pid_t)-1==(daemon_pid=is_daemon_running())) {
589 g_warning(_("Cannot stop network daemon: Daemon is not running"));
592 if (daemon_pid==getpid()) {
593 sock_gsource_destroy();
597 kill(daemon_pid,SIGKILL);
600 g_warning(udpgate_printf_alloca(_("Unable to stop the daemon at PID %d: %s"),
601 (int)daemon_pid,strerror(errno_save)));
605 if (network_notify_hostip)
606 (*network_notify_hostip)(0);