5cc8321276148d4be5e080143f7ff7e38a181546
[oweals/gnunet.git] / src / vpn / gnunet-daemon-vpn.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Philipp Tölke
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 Philipp Tölke
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-vpn-helper-p.h"
31 /* #include "gnunet_template_service.h" */
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37
38 struct vpn_cls {
39         struct GNUNET_DISK_PipeHandle* helper_in;
40         struct GNUNET_DISK_PipeHandle* helper_out;
41         const struct GNUNET_DISK_FileHandle* fh_from_helper;
42
43         struct GNUNET_SCHEDULER_Handle *sched;
44
45         pid_t helper_pid;
46 };
47
48 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
49         struct vpn_cls* mycls = (struct vpn_cls*) cls;
50         if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
51                 PLIBC_KILL(mycls->helper_pid, SIGTERM);
52                 GNUNET_OS_process_wait(mycls->helper_pid);
53         }
54 }
55
56 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
57         struct vpn_cls* mycls = (struct vpn_cls*) cls;
58         struct suid_packet_header hdr = { .size = 0 };
59
60         int r = 0;
61
62         if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
63                 return;
64
65         while (r < sizeof(struct suid_packet_header)) {
66                 int t = GNUNET_DISK_file_read(mycls->fh_from_helper, &hdr, sizeof(struct suid_packet_header));
67                 if (t< 0) {
68                         fprintf(stderr, "Read error for header: %m\n");
69                         return;
70                 }
71                 r += t;
72         }
73
74         fprintf(stderr, "Read %d bytes for the header. The 'size' is %x, that is %d\n", r, hdr.size, ntohl(hdr.size));
75
76         struct suid_packet *pkt = (struct suid_packet*) GNUNET_malloc(ntohl(hdr.size));
77
78         if (memcpy(pkt, &hdr, sizeof(struct suid_packet_header)) < 0) {
79                 fprintf(stderr, "Memcpy: %m\n");
80                 return;
81         }
82
83         while (r < ntohl(pkt->hdr.size)) {
84                 int t = GNUNET_DISK_file_read(mycls->fh_from_helper, (unsigned char*)pkt + r, ntohl(pkt->hdr.size) - r);
85                 if (t< 0) {
86                         fprintf(stderr, "Read error for data: %m\n");
87                         return;
88                 }
89                 r += t;
90         }
91
92         printf("read %d bytes. The first 87 are:\n\t", r);
93
94         for (r = 0; r < 87; r++)
95                 printf("%02x ", pkt->data[r]);
96         printf("\n");
97
98         GNUNET_free(pkt);
99
100         GNUNET_SCHEDULER_add_read_file (mycls->sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
101 }
102
103 /**
104  * Main function that will be run by the scheduler.
105  *
106  * @param cls closure
107  * @param sched the scheduler to use
108  * @param args remaining command-line arguments
109  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
110  * @param cfg configuration
111  */
112 static void
113 run (void *cls,
114                 struct GNUNET_SCHEDULER_Handle *sched,
115                 char *const *args,
116                 const char *cfgfile,
117                 const struct GNUNET_CONFIGURATION_Handle *cfg) {
118
119         struct vpn_cls* mycls = (struct vpn_cls*) cls;
120
121         mycls->sched = sched;
122
123         GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
124
125         mycls->helper_in = GNUNET_DISK_pipe(1);
126         mycls->helper_out = GNUNET_DISK_pipe(1);
127
128         mycls->helper_pid = GNUNET_OS_start_process(mycls->helper_in, mycls->helper_out, "gnunet-vpn-helper", "gnunet-vpn-helper", NULL);
129
130         mycls->fh_from_helper = GNUNET_DISK_pipe_handle (mycls->helper_out, GNUNET_DISK_PIPE_END_READ);
131         
132         GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
133 }
134
135
136 /**
137  * The main function to obtain template from gnunetd.
138  *
139  * @param argc number of arguments from the command line
140  * @param argv command line arguments
141  * @return 0 ok, 1 on error
142  */
143 int
144 main (int argc, char *const *argv)
145 {
146   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
147     GNUNET_GETOPT_OPTION_END
148   };
149
150   struct vpn_cls* cls = (struct vpn_cls*)malloc(sizeof(struct vpn_cls));
151
152   return (GNUNET_OK ==
153           GNUNET_PROGRAM_run (argc,
154                               argv,
155                               "gnunet-daemon-vpn",
156                               gettext_noop ("help text"),
157                               options, &run, cls)) ? ret : 1;
158 }
159
160 /* end of gnunet-daemon-vpn.c */