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()! */
179 if (network_notify_hostip)
180 (*network_notify_hostip)(0);
183 client_destroy(client);
187 g_message(_("Client timeout occurance finish"));
189 return FALSE; /* GSource should be removed */
192 static void handle_master_probe(const void *packet,size_t gotlen,const struct sockaddr_in *sockaddr_in_from)
194 GHashTable *got_hash;
195 gpointer got_unique_gpointer;
196 gpointer hostip_gpointer;
197 guint32 hostip_guint32;
199 g_return_if_fail(packet!=NULL);
200 g_return_if_fail(sockaddr_in_from!=NULL);
205 if (!(got_hash=packet_disassembly(packet,gotlen)))
207 if (!(g_hash_table_lookup_extended(
208 got_hash, /* hash_table */
209 GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32), /* lookup_key */
211 &got_unique_gpointer))) {
212 err_packet_disassembly_destroy_got_hash:
213 packet_disassembly_destroy(got_hash);
216 if (GPOINTER_TO_UINT(got_unique_gpointer)!=probe_unique)
217 goto err_packet_disassembly_destroy_got_hash;
218 if (!(g_hash_table_lookup_extended(
219 got_hash, /* hash_table */
220 GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_INADDR), /* lookup_key */
223 goto err_packet_disassembly_destroy_got_hash;
224 hostip_guint32=GPOINTER_TO_UINT(hostip_gpointer);
225 packet_disassembly_destroy(got_hash);
227 client_destroy(probe);
228 if (network_notify_hostip)
229 (*network_notify_hostip)(hostip_guint32);
232 static struct client *client_new(void);
234 static void handle_master(struct client *master)
237 struct sockaddr_in sockaddr_in_from;
238 char packet[0x10000];
239 struct client *client;
240 struct sockaddr_in sockaddr_in_server;
241 socklen_t sockaddr_in_from_length;
243 g_return_if_fail(master!=NULL);
245 sockaddr_in_from_length=sizeof(sockaddr_in_from);
246 while (-1!=(gotlen=recvfrom(
247 master->gpollfd.fd, /* s */
249 sizeof(packet), /* len */
251 (struct sockaddr *)&sockaddr_in_from, /* from */
252 &sockaddr_in_from_length))) { /* fromlen */
255 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
258 if (packet_recognized(packet,gotlen)) {
259 handle_master_probe(packet,gotlen,&sockaddr_in_from);
263 /* FIXME: Performance: Ugly search... */
264 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
265 client=clientl->data;
269 && client->sockaddr_in_from.sin_family ==sockaddr_in_from.sin_family
270 && client->sockaddr_in_from.sin_port ==sockaddr_in_from.sin_port
271 && client->sockaddr_in_from.sin_addr.s_addr==sockaddr_in_from.sin_addr.s_addr)
276 client->sockaddr_in_from=sockaddr_in_from;
278 client_touch(client);
279 UDPGATE_MEMZERO(&sockaddr_in_server);
280 sockaddr_in_server.sin_family=AF_INET;
281 sockaddr_in_server.sin_port=htons(SERVER_PORT);
282 sockaddr_in_server.sin_addr.s_addr=htonl(SERVER_INADDR);
283 /* FIXME: errors checking */
285 client->gpollfd.fd, /* s */
289 (struct sockaddr *)&sockaddr_in_server, /* to */
290 sizeof(sockaddr_in_server)); /* tolen */
294 static void handle_probe(struct client *probe)
297 struct sockaddr_in sockaddr_in_from;
298 char packet[0x10000];
299 socklen_t sockaddr_in_from_length;
301 g_return_if_fail(probe!=NULL);
303 sockaddr_in_from_length=sizeof(sockaddr_in_from);
304 while (-1!=(gotlen=recvfrom(
305 master->gpollfd.fd, /* s */
307 sizeof(packet), /* len */
309 (struct sockaddr *)&sockaddr_in_from, /* from */
310 &sockaddr_in_from_length))) { /* fromlen */
311 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
314 /* Probe socket should have no response; maybe some ICMP errors - ignored. */
318 static void handle_client(struct client *client)
321 struct sockaddr_in sockaddr_in_from;
322 char packet [0x10000];
323 socklen_t sockaddr_in_from_length;
325 g_return_if_fail(client!=NULL);
326 g_return_if_fail(master!=NULL);
328 while (-1!=(gotlen=recvfrom(
329 client->gpollfd.fd, /* s */
331 sizeof(packet), /* len */
333 (struct sockaddr *)&sockaddr_in_from, /* from */
334 &sockaddr_in_from_length))) { /* fromlen */
335 if (sockaddr_in_from_length!=sizeof(sockaddr_in_from)) /* FIXME: errors reporting */
337 client_touch(client);
338 /* FIXME: errors checking */
340 master->gpollfd.fd, /* s */
344 (struct sockaddr *)&client->sockaddr_in_from, /* to */
345 sizeof(client->sockaddr_in_from)); /* tolen */
349 static gboolean sock_source_callback(gpointer data /* unused */)
353 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
354 struct client *client=clientl->data;
356 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS) {
357 /**/ if (client==master)
358 handle_master(client);
359 else if (client==probe)
360 handle_probe(client);
362 handle_client(client);
366 return TRUE; /* the source should be kept active */
369 static gboolean sock_source_prepare(GSource *source,gint *timeout)
376 static gboolean sock_source_check(GSource *source)
380 for (clientl=sock_client_list;clientl;clientl=clientl->next) {
381 struct client *client=clientl->data;
383 if (client->gpollfd.revents&SOCK_SOURCE_CHECK_REVENTS)
389 static gboolean sock_source_dispatch(GSource *source,GSourceFunc callback,gpointer user_data)
391 g_assert(callback!=NULL);
392 return (*callback)(user_data);
395 static GSourceFuncs sock_source_watch_funcs={
398 sock_source_dispatch,
402 static void sock_gsource_destroy(void)
404 while (sock_client_list)
405 client_destroy(sock_client_list->data);
407 g_assert(master==NULL);
410 g_source_destroy(sock_gsource);
414 write_daemon_running((pid_t)-1); /* unlink */
417 static gboolean sock_gsource_new(void)
422 g_assert(sock_client_list==NULL);
424 /* attach sock_source_callback() to watch for any abnormalities
425 * on our open pipe 'parentheart_fds' and terminate the child if parent dies.
427 if (!(sock_gsource=g_source_new(&sock_source_watch_funcs,sizeof(GSource)))) {
428 g_warning("g_source_new(): %m");
431 g_source_set_callback(
432 sock_gsource, /* source */
433 sock_source_callback, /* func */
436 if (!g_source_attach( /* returns 'guint' id */
437 sock_gsource, /* source */
438 NULL)) { /* context; NULL means 'default context' */
439 g_warning("g_source_attach(gsource,NULL): %m");
440 sock_gsource_destroy();
446 static struct client *client_new(void)
448 struct client *client;
449 static unsigned long oneul=1;
452 if (!sock_gsource_new())
455 if (-1==(sock=socket(AF_INET,SOCK_DGRAM,0))) {
456 g_warning("socket(AF_INET,SOCK_DGRAM): %m");
459 if (ioctl(sock,FIONBIO,&oneul)) { /* non-blocking mode */
460 g_warning("ioctl(sock,FIONBIO,&1): %m");
461 close(sock); /* errors ignored */
466 client->gpollfd.fd=sock;
467 client->gpollfd.events=SOCK_SOURCE_CHECK_EVENTS;
468 client->gpollfd.revents=0;
469 client->timeout_id=0;
470 sock_client_list=g_list_prepend(sock_client_list,client);
471 g_source_add_poll(sock_gsource,&client->gpollfd);
474 g_message(_("Client fd %d created"),client->gpollfd.fd);
479 static void client_destroy(struct client *client)
481 g_return_if_fail(client!=NULL);
483 if (!sock_gsource_new())
487 g_message(_("Client fd %d timeout id %d destroy enter"),client->gpollfd.fd,client->timeout_id);
489 if (client==master) {
491 g_assert(client->timeout_id==0);
496 client_timeout_remove(client);
499 g_source_remove_poll(sock_gsource,&client->gpollfd);
500 sock_client_list=g_list_remove(sock_client_list,client);
501 close(client->gpollfd.fd); /* errors ignored */
504 g_message(_("Client fd %d timeout id %d destroy finish"),client->gpollfd.fd,client->timeout_id);
509 static gboolean probe_send(struct client *probe,gint port_local)
511 struct sockaddr_in sockaddr_in_server;
512 GHashTable *probe_hash;
514 size_t packet_length;
516 g_return_val_if_fail(probe!=NULL,FALSE);
518 probe_unique=g_random_int();
520 probe_hash=g_hash_table_new(
521 g_direct_hash, /* hash_func */
522 g_direct_equal); /* key_equal_func */
523 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_CLIENT_PORT) ,GUINT_TO_POINTER(port_local));
524 g_hash_table_insert(probe_hash,GUINT_TO_POINTER(PACKET_ELEM_TYPE_DATA_GUINT32),GUINT_TO_POINTER(probe_unique));
525 packet=packet_assembly(&packet_length,probe_hash);
526 g_hash_table_destroy(probe_hash);
530 UDPGATE_MEMZERO(&sockaddr_in_server);
531 sockaddr_in_server.sin_family=AF_INET;
532 sockaddr_in_server.sin_port=htons(PROBE_PORT);
533 sockaddr_in_server.sin_addr.s_addr=htonl(PROBE_INADDR);
534 /* FIXME: errors checking */
536 probe->gpollfd.fd, /* s */
538 packet_length, /* len */
540 (struct sockaddr *)&sockaddr_in_server, /* to */
541 sizeof(sockaddr_in_server)); /* tolen */
546 gboolean network_start(gint port)
549 struct sockaddr_in sockaddr_in;
551 g_return_val_if_fail(port>=0,FALSE);
553 if ((pid_t)-1!=(daemon_pid=is_daemon_running())) {
554 g_warning(_("Cannot start network daemon: Daemon is already running on PID %d"),(int)daemon_pid);
558 /* Setup 'master': */
559 g_assert(master==NULL);
560 if (!(master=client_new()))
562 UDPGATE_MEMZERO(&sockaddr_in);
563 sockaddr_in.sin_family=AF_INET;
564 sockaddr_in.sin_port=htons(port);
565 sockaddr_in.sin_addr.s_addr=htonl(INADDR_ANY);
566 if (bind(master->gpollfd.fd,(struct sockaddr *)&sockaddr_in,sizeof(sockaddr_in))) {
567 g_warning("bind(sock,{AF_INET,INADDR_ANY:%d}): %m",(int)port);
568 err_sock_gsource_destroy:
569 sock_gsource_destroy();
574 if (!(probe=client_new()))
575 goto err_sock_gsource_destroy;
576 probe_send(probe,port);
577 client_touch(probe); /* timeout */
579 write_daemon_running(getpid());
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)));