2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
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.
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.
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.
22 * @file vpn/gnunet-daemon-vpn.c
24 * @author Philipp Tölke
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-vpn-packet.h"
32 #include "gnunet-vpn-pretty-print.h"
33 /* #include "gnunet_template_service.h" */
41 struct GNUNET_DISK_PipeHandle* helper_in;
42 struct GNUNET_DISK_PipeHandle* helper_out;
43 const struct GNUNET_DISK_FileHandle* fh_from_helper;
45 struct GNUNET_SCHEDULER_Handle *sched;
50 static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) {
51 struct vpn_cls* mycls = (struct vpn_cls*) cls;
52 if (tskctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) {
53 PLIBC_KILL(mycls->helper_pid, SIGTERM);
54 GNUNET_OS_process_wait(mycls->helper_pid);
58 static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
59 struct vpn_cls* mycls = (struct vpn_cls*) cls;
60 struct suid_packet_header hdr = { .size = 0 };
64 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
67 while (r < sizeof(struct suid_packet_header)) {
68 int t = GNUNET_DISK_file_read(mycls->fh_from_helper, &hdr, sizeof(struct suid_packet_header));
70 fprintf(stderr, "Read error for header: %m\n");
76 struct suid_packet *pkt = (struct suid_packet*) GNUNET_malloc(ntohl(hdr.size));
78 if (memcpy(pkt, &hdr, sizeof(struct suid_packet_header)) < 0) {
79 fprintf(stderr, "Memcpy: %m\n");
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);
86 fprintf(stderr, "Read error for data: %m\n");
92 struct ip6_pkt *pkt6 = (struct ip6_pkt*) pkt;
93 struct ip6_tcp *pkt6_tcp;
94 struct ip6_udp *pkt6_udp;
97 switch(pkt6->ip6_hdr.nxthdr) {
99 pkt6_tcp = (struct ip6_tcp*)pkt6;
100 pkt_printf_ip6tcp(pkt6_tcp);
103 pkt6_udp = (struct ip6_udp*)pkt6;
104 pkt_printf_ip6udp(pkt6_udp);
105 if (ntohs(pkt6_udp->udp_hdr.dpt) == 53) {
106 pkt_printf_ip6dns((struct ip6_udp_dns*)pkt6_udp);
113 GNUNET_SCHEDULER_add_read_file (mycls->sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
117 * Main function that will be run by the scheduler.
120 * @param sched the scheduler to use
121 * @param args remaining command-line arguments
122 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
123 * @param cfg configuration
127 struct GNUNET_SCHEDULER_Handle *sched,
130 const struct GNUNET_CONFIGURATION_Handle *cfg) {
132 struct vpn_cls* mycls = (struct vpn_cls*) cls;
134 mycls->sched = sched;
136 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
138 mycls->helper_in = GNUNET_DISK_pipe(1);
139 mycls->helper_out = GNUNET_DISK_pipe(1);
141 mycls->helper_pid = GNUNET_OS_start_process(mycls->helper_in, mycls->helper_out, "gnunet-vpn-helper", "gnunet-vpn-helper", NULL);
143 mycls->fh_from_helper = GNUNET_DISK_pipe_handle (mycls->helper_out, GNUNET_DISK_PIPE_END_READ);
145 GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, mycls->fh_from_helper, &helper_read, mycls);
150 * The main function to obtain template from gnunetd.
152 * @param argc number of arguments from the command line
153 * @param argv command line arguments
154 * @return 0 ok, 1 on error
157 main (int argc, char *const *argv)
159 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
160 GNUNET_GETOPT_OPTION_END
163 struct vpn_cls* cls = (struct vpn_cls*)malloc(sizeof(struct vpn_cls));
166 GNUNET_PROGRAM_run (argc,
169 gettext_noop ("help text"),
170 options, &run, cls)) ? ret : 1;
173 /* end of gnunet-daemon-vpn.c */