23b50c6994a746c4bd79974fcc94ee0a6bb5b98c
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file vpn/gnunet-daemon-vpn.c
23  * @brief 
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_os_lib.h"
30 /* #include "gnunet_template_service.h" */
31
32 /**
33  * Final status code.
34  */
35 static int ret;
36
37 struct vpn_cls {
38         struct GNUNET_DISK_PipeHandle* helper_in;
39         struct GNUNET_DISK_PipeHandle* helper_out;
40
41         pid_t helper_pid;
42 };
43
44 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
45         struct vpn_cls* mycls = (struct vpn_cls*) cls;
46         if (tskctx->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) {
47                 PLIBC_KILL(mycls->helper_pid, SIGTERM);
48                 GNUNET_OS_process_wait(mycls->helper_pid);
49         }
50 }
51
52 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
53 }
54
55 /**
56  * Main function that will be run by the scheduler.
57  *
58  * @param cls closure
59  * @param sched the scheduler to use
60  * @param args remaining command-line arguments
61  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
62  * @param cfg configuration
63  */
64 static void
65 run (void *cls,
66          struct GNUNET_SCHEDULER_Handle *sched,
67          char *const *args,
68          const char *cfgfile,
69          const struct GNUNET_CONFIGURATION_Handle *cfg) {
70
71         struct vpn_cls* mycls = (struct vpn_cls*) cls;
72
73         GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
74
75         mycls->helper_in = GNUNET_DISK_pipe(1);
76         mycls->helper_out = GNUNET_DISK_pipe(1);
77
78         mycls->helper_pid = GNUNET_OS_start_process(mycls->helper_in, mycls->helper_out, "gnunet-vpn-helper", "gnunet-vpn-helper", NULL);
79
80         const struct GNUNET_DISK_FileHandle* fh = GNUNET_DISK_pipe_handle (mycls->helper_out, GNUNET_DISK_PIPE_END_READ);
81         
82         GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, fh, &helper_read, mycls);
83 }
84
85
86 /**
87  * The main function to obtain template from gnunetd.
88  *
89  * @param argc number of arguments from the command line
90  * @param argv command line arguments
91  * @return 0 ok, 1 on error
92  */
93 int
94 main (int argc, char *const *argv)
95 {
96   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
97     GNUNET_GETOPT_OPTION_END
98   };
99
100   struct vpn_cls* cls = (struct vpn_cls*)malloc(sizeof(struct vpn_cls));
101
102   return (GNUNET_OK ==
103           GNUNET_PROGRAM_run (argc,
104                               argv,
105                               "gnunet-daemon-vpn",
106                               gettext_noop ("help text"),
107                               options, &run, cls)) ? ret : 1;
108 }
109
110 /* end of gnunet-daemon-vpn.c */