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-helper-vpn-api.c
23 * @brief exposes the API (the convenience-functions) of dealing with the
25 * @author Philipp Toelke
29 #include <gnunet_common.h>
30 #include <gnunet_server_lib.h>
31 #include <gnunet_os_lib.h>
33 #include "gnunet-helper-vpn-api.h"
36 stop_helper (struct GNUNET_VPN_HELPER_Handle *handle)
38 if (NULL == handle->helper_proc)
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;
45 GNUNET_DISK_pipe_close (handle->helper_in);
46 GNUNET_DISK_pipe_close (handle->helper_out);
48 GNUNET_SERVER_mst_destroy(handle->mst);
51 extern GNUNET_SCHEDULER_TaskIdentifier shs_task;
54 * Read from the helper-process
57 helper_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
59 struct GNUNET_VPN_HELPER_Handle *handle = cls;
60 /* no message can be bigger then 64k */
63 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
66 int t = GNUNET_DISK_file_read (handle->fh_from_helper, &buf, 65535);
68 /* On read-error, restart the helper */
71 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
72 "Read error for header from vpn-helper: %m\n");
75 /* Restart the helper */
76 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
77 handle->restart_task, handle);
82 GNUNET_SERVER_mst_receive (handle->mst, handle->client, buf, t, 0, 0))
84 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
88 /* Restart the helper */
89 shs_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
90 handle->restart_task, handle);
95 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
96 handle->fh_from_helper, &helper_read,
101 cleanup_helper (struct GNUNET_VPN_HELPER_Handle *handle)
103 stop_helper (handle);
104 GNUNET_free (handle);
107 struct GNUNET_VPN_HELPER_Handle *
108 start_helper (const char *ifname,
109 const char *ipv6addr,
110 const char *ipv6prefix,
111 const char *ipv4addr,
112 const char *ipv4mask, const char *process_name,
113 GNUNET_SCHEDULER_Task restart_task,
114 GNUNET_SERVER_MessageTokenizerCallback cb, void *cb_cls)
116 struct GNUNET_VPN_HELPER_Handle *handle =
117 GNUNET_malloc (sizeof (struct GNUNET_VPN_HELPER_Handle));
119 handle->helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO);
120 handle->helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
122 handle->restart_task = restart_task;
124 if (handle->helper_in == NULL || handle->helper_out == NULL)
126 GNUNET_free (handle);
130 handle->helper_proc =
131 GNUNET_OS_start_process (handle->helper_in, handle->helper_out,
132 "gnunet-helper-vpn", process_name, ifname,
133 ipv6addr, ipv6prefix, ipv4addr, ipv4mask, NULL);
135 handle->fh_from_helper =
136 GNUNET_DISK_pipe_handle (handle->helper_out, GNUNET_DISK_PIPE_END_READ);
137 handle->fh_to_helper =
138 GNUNET_DISK_pipe_handle (handle->helper_in, GNUNET_DISK_PIPE_END_WRITE);
140 GNUNET_DISK_pipe_close_end (handle->helper_out, GNUNET_DISK_PIPE_END_WRITE);
141 GNUNET_DISK_pipe_close_end (handle->helper_in, GNUNET_DISK_PIPE_END_READ);
143 handle->mst = GNUNET_SERVER_mst_create (cb, cb_cls);
145 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
146 handle->fh_from_helper, &helper_read,