dns hijacker code review
[oweals/gnunet.git] / src / vpn / gnunet-helper-vpn-api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff
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-helper-vpn-api.c
23  * @brief exposes the API (the convenience-functions) of dealing with the
24  *        helper-vpn
25  * @author Philipp Toelke
26  */
27
28 #include <platform.h>
29 #include <gnunet_common.h>
30 #include <gnunet_server_lib.h>
31 #include <gnunet_os_lib.h>
32
33 #include "gnunet-helper-vpn-api.h"
34
35 static void
36 stop_helper (struct GNUNET_VPN_HELPER_Handle *handle)
37 {
38   if (NULL == handle->helper_proc)
39     return;
40   GNUNET_OS_process_kill (handle->helper_proc, SIGKILL);
41   GNUNET_OS_process_wait (handle->helper_proc);
42   GNUNET_OS_process_close (handle->helper_proc);
43   handle->helper_proc = NULL;
44
45   GNUNET_DISK_pipe_close (handle->helper_in);
46   GNUNET_DISK_pipe_close (handle->helper_out);
47
48   GNUNET_SERVER_mst_destroy (handle->mst);
49 }
50
51 extern GNUNET_SCHEDULER_TaskIdentifier shs_task;
52
53 /**
54  * Read from the helper-process
55  */
56 static void
57 helper_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
58 {
59   struct GNUNET_VPN_HELPER_Handle *handle = cls;
60
61   /* no message can be bigger then 64k */
62   char buf[65535];
63
64   if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
65     return;
66
67   int t = GNUNET_DISK_file_read (handle->fh_from_helper, &buf, 65535);
68
69   /* On read-error, restart the helper */
70   if (t <= 0)
71   {
72     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
73                 "Read error for header from vpn-helper: %m\n");
74     stop_helper (handle);
75
76     /* Restart the helper */
77     shs_task =
78         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
79                                       handle->restart_task, handle);
80     return;
81   }
82
83   if (GNUNET_SYSERR ==
84       GNUNET_SERVER_mst_receive (handle->mst, handle->client, buf, t, 0, 0))
85   {
86     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "SYSERR from mst\n");
87     stop_helper (handle);
88
89     /* Restart the helper */
90     shs_task =
91         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
92                                       handle->restart_task, handle);
93     return;
94
95   }
96
97   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
98                                   handle->fh_from_helper, &helper_read, handle);
99 }
100
101 void
102 cleanup_helper (struct GNUNET_VPN_HELPER_Handle *handle)
103 {
104   stop_helper (handle);
105   GNUNET_free (handle);
106 }
107
108 struct GNUNET_VPN_HELPER_Handle *
109 start_helper (const char *ifname, const char *ipv6addr, const char *ipv6prefix,
110               const char *ipv4addr, const char *ipv4mask,
111               const char *process_name, GNUNET_SCHEDULER_Task restart_task,
112               GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
113 {
114   struct GNUNET_VPN_HELPER_Handle *handle =
115       GNUNET_malloc (sizeof (struct GNUNET_VPN_HELPER_Handle));
116
117   handle->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
118   handle->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
119
120   handle->restart_task = restart_task;
121
122   if (handle->helper_in == NULL || handle->helper_out == NULL)
123   {
124     GNUNET_free (handle);
125     return NULL;
126   }
127
128   handle->helper_proc =
129       GNUNET_OS_start_process (handle->helper_in, handle->helper_out,
130                                "gnunet-helper-vpn", process_name, ifname,
131                                ipv6addr, ipv6prefix, ipv4addr, ipv4mask, NULL);
132
133   handle->fh_from_helper =
134       GNUNET_DISK_pipe_handle (handle->helper_out, GNUNET_DISK_PIPE_END_READ);
135   handle->fh_to_helper =
136       GNUNET_DISK_pipe_handle (handle->helper_in, GNUNET_DISK_PIPE_END_WRITE);
137
138   GNUNET_DISK_pipe_close_end (handle->helper_out, GNUNET_DISK_PIPE_END_WRITE);
139   GNUNET_DISK_pipe_close_end (handle->helper_in, GNUNET_DISK_PIPE_END_READ);
140
141   handle->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
142
143   GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
144                                   handle->fh_from_helper, &helper_read, handle);
145
146   return handle;
147 }