diff options
author | Philipp Tölke <toelke@in.tum.de> | 2011-02-02 23:02:52 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2011-02-02 23:02:52 +0000 |
commit | ac0d240e011c27b1d660b6db05d067bf8766db73 (patch) | |
tree | a8e3192455ab15ab08b2c38f1d075751795529ff /src/vpn/gnunet-daemon-vpn-helper.c | |
parent | b13a84b528dc581a6c1a042317f8e9cc3874642e (diff) | |
download | gnunet-ac0d240e011c27b1d660b6db05d067bf8766db73.tar.gz gnunet-ac0d240e011c27b1d660b6db05d067bf8766db73.zip |
refactor the interface to the helper a bit to make it easier to use from -exit
Diffstat (limited to 'src/vpn/gnunet-daemon-vpn-helper.c')
-rw-r--r-- | src/vpn/gnunet-daemon-vpn-helper.c | 118 |
1 files changed, 32 insertions, 86 deletions
diff --git a/src/vpn/gnunet-daemon-vpn-helper.c b/src/vpn/gnunet-daemon-vpn-helper.c index d2815d4e5..ae6d18fd9 100644 --- a/src/vpn/gnunet-daemon-vpn-helper.c +++ b/src/vpn/gnunet-daemon-vpn-helper.c | |||
@@ -41,27 +41,10 @@ | |||
41 | #include "gnunet-vpn-packet.h" | 41 | #include "gnunet-vpn-packet.h" |
42 | 42 | ||
43 | /** | 43 | /** |
44 | * PipeHandle to receive data from the helper | ||
45 | */ | ||
46 | static struct GNUNET_DISK_PipeHandle* helper_in; | ||
47 | |||
48 | /** | ||
49 | * PipeHandle to send data to the helper | ||
50 | */ | ||
51 | static struct GNUNET_DISK_PipeHandle* helper_out; | ||
52 | |||
53 | /** | ||
54 | * FileHandle to receive data from the helper | ||
55 | */ | ||
56 | static const struct GNUNET_DISK_FileHandle* fh_from_helper; | ||
57 | |||
58 | /** | ||
59 | * FileHandle to send data to the helper | ||
60 | */ | ||
61 | static const struct GNUNET_DISK_FileHandle* fh_to_helper; | ||
62 | |||
63 | /** | ||
64 | * Start the helper-process | 44 | * Start the helper-process |
45 | * | ||
46 | * If cls != NULL it is assumed that this function is called as a result of a dying | ||
47 | * helper. cls is then taken as handle to the old helper and is cleaned up. | ||
65 | * {{{ | 48 | * {{{ |
66 | */ | 49 | */ |
67 | void | 50 | void |
@@ -70,16 +53,22 @@ start_helper_and_schedule(void *cls, | |||
70 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | 53 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) |
71 | return; | 54 | return; |
72 | 55 | ||
73 | helper_in = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO); | 56 | if (cls != NULL) |
74 | helper_out = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES); | 57 | cleanup_helper(cls); |
75 | 58 | cls = NULL; | |
76 | if (helper_in == NULL || helper_out == NULL) return; | ||
77 | 59 | ||
60 | char* ifname; | ||
78 | char* ipv6addr; | 61 | char* ipv6addr; |
79 | char* ipv6prefix; | 62 | char* ipv6prefix; |
80 | char* ipv4addr; | 63 | char* ipv4addr; |
81 | char* ipv4mask; | 64 | char* ipv4mask; |
82 | 65 | ||
66 | if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IFNAME", &ifname)) | ||
67 | { | ||
68 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IFNAME' in configuration!\n"); | ||
69 | exit(1); | ||
70 | } | ||
71 | |||
83 | if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr)) | 72 | if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "vpn", "IPV6ADDR", &ipv6addr)) |
84 | { | 73 | { |
85 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n"); | 74 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "No entry 'IPV6ADDR' in configuration!\n"); |
@@ -104,75 +93,32 @@ start_helper_and_schedule(void *cls, | |||
104 | exit(1); | 93 | exit(1); |
105 | } | 94 | } |
106 | 95 | ||
107 | helper_proc = | 96 | /* Start the helper |
108 | GNUNET_OS_start_process (helper_in, helper_out, "gnunet-helper-vpn", | 97 | * Messages get passed to the function message_token |
109 | "gnunet-helper-vpn", ipv6addr, ipv6prefix, | 98 | * When the helper dies, this function will be called again with the |
110 | ipv4addr, ipv4mask, NULL); | 99 | * helper_handle as cls. |
100 | */ | ||
101 | helper_handle = start_helper(ifname, | ||
102 | ipv6addr, | ||
103 | ipv6prefix, | ||
104 | ipv4addr, | ||
105 | ipv4mask, | ||
106 | "vpn-gnunet", | ||
107 | start_helper_and_schedule, | ||
108 | message_token, | ||
109 | NULL, | ||
110 | NULL); | ||
111 | 111 | ||
112 | GNUNET_free(ipv6addr); | 112 | GNUNET_free(ipv6addr); |
113 | GNUNET_free(ipv6prefix); | 113 | GNUNET_free(ipv6prefix); |
114 | GNUNET_free(ipv4addr); | 114 | GNUNET_free(ipv4addr); |
115 | GNUNET_free(ipv4mask); | 115 | GNUNET_free(ipv4mask); |
116 | 116 | ||
117 | fh_from_helper = GNUNET_DISK_pipe_handle (helper_out, GNUNET_DISK_PIPE_END_READ); | ||
118 | fh_to_helper = GNUNET_DISK_pipe_handle (helper_in, GNUNET_DISK_PIPE_END_WRITE); | ||
119 | |||
120 | GNUNET_DISK_pipe_close_end(helper_out, GNUNET_DISK_PIPE_END_WRITE); | ||
121 | GNUNET_DISK_pipe_close_end(helper_in, GNUNET_DISK_PIPE_END_READ); | ||
122 | |||
123 | /* Tell the dns-service to rehijack the dns-port | 117 | /* Tell the dns-service to rehijack the dns-port |
124 | * The routing-table gets flushed if an interface disappears. | 118 | * The routing-table gets flushed if an interface disappears. |
125 | */ | 119 | */ |
126 | restart_hijack = 1; | 120 | restart_hijack = 1; |
127 | GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); | 121 | GNUNET_CLIENT_notify_transmit_ready(dns_connection, sizeof(struct GNUNET_MessageHeader), GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, &send_query, NULL); |
128 | |||
129 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL); | ||
130 | } | ||
131 | /*}}}*/ | ||
132 | /** | ||
133 | * Restart the helper-process | ||
134 | * {{{ | ||
135 | */ | ||
136 | void | ||
137 | restart_helper(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) { | ||
138 | // Kill the helper | ||
139 | GNUNET_OS_process_kill (helper_proc, SIGKILL); | ||
140 | GNUNET_OS_process_wait (helper_proc); | ||
141 | GNUNET_OS_process_close (helper_proc); | ||
142 | helper_proc = NULL; | ||
143 | |||
144 | GNUNET_DISK_pipe_close(helper_in); | ||
145 | GNUNET_DISK_pipe_close(helper_out); | ||
146 | |||
147 | /* Restart the helper */ | ||
148 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, start_helper_and_schedule, NULL); | ||
149 | } | ||
150 | /*}}}*/ | ||
151 | |||
152 | /** | ||
153 | * Read from the helper-process | ||
154 | * {{{ | ||
155 | */ | ||
156 | void | ||
157 | helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) { | ||
158 | /* no message can be bigger then 64k */ | ||
159 | char buf[65535]; | ||
160 | |||
161 | if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) | ||
162 | return; | ||
163 | |||
164 | int t = GNUNET_DISK_file_read(fh_from_helper, &buf, 65535); | ||
165 | |||
166 | /* On read-error, restart the helper */ | ||
167 | if (t<=0) { | ||
168 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read error for header from vpn-helper: %m\n"); | ||
169 | GNUNET_SCHEDULER_add_now(restart_helper, cls); | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | /* FIXME */ GNUNET_SERVER_mst_receive(mst, NULL, buf, t, 0, 0); | ||
174 | |||
175 | GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh_from_helper, &helper_read, NULL); | ||
176 | } | 122 | } |
177 | /*}}}*/ | 123 | /*}}}*/ |
178 | 124 | ||
@@ -234,12 +180,12 @@ helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) { | |||
234 | GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans); | 180 | GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans); |
235 | GNUNET_free(ans); | 181 | GNUNET_free(ans); |
236 | 182 | ||
237 | /* FIXME */ GNUNET_DISK_file_write(fh_to_helper, pkt, pkt_len); | 183 | /* FIXME */ GNUNET_DISK_file_write(helper_handle->fh_to_helper, pkt, pkt_len); |
238 | 184 | ||
239 | /* if more packets are available, reschedule */ | 185 | /* if more packets are available, reschedule */ |
240 | if (answer_proc_head != NULL) | 186 | if (answer_proc_head != NULL) |
241 | GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, | 187 | GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL, |
242 | fh_to_helper, | 188 | helper_handle->fh_to_helper, |
243 | &helper_write, | 189 | &helper_write, |
244 | NULL); | 190 | NULL); |
245 | } | 191 | } |
@@ -361,10 +307,10 @@ message_token(void *cls, | |||
361 | 307 | ||
362 | void write_to_helper(void* buf, size_t len) | 308 | void write_to_helper(void* buf, size_t len) |
363 | { | 309 | { |
364 | (void)GNUNET_DISK_file_write(fh_to_helper, buf, len); | 310 | (void)GNUNET_DISK_file_write(helper_handle->fh_to_helper, buf, len); |
365 | } | 311 | } |
366 | 312 | ||
367 | void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls) | 313 | void schedule_helper_write(struct GNUNET_TIME_Relative time, void* cls) |
368 | { | 314 | { |
369 | GNUNET_SCHEDULER_add_write_file (time, fh_to_helper, &helper_write, cls); | 315 | GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper, &helper_write, cls); |
370 | } | 316 | } |