diff options
author | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:25 +0000 |
---|---|---|
committer | Philipp Tölke <toelke@in.tum.de> | 2010-07-20 05:45:25 +0000 |
commit | 729f0bb2677eb1f22ecff2284e987e99212541a3 (patch) | |
tree | a24e1233b50518d6aa555e513c5f846767d170c8 /src/vpn | |
parent | aea5d092050795eb39acd379283774360d352f3b (diff) | |
download | gnunet-729f0bb2677eb1f22ecff2284e987e99212541a3.tar.gz gnunet-729f0bb2677eb1f22ecff2284e987e99212541a3.zip |
start the helper from the daemon, end it correctly
Diffstat (limited to 'src/vpn')
-rw-r--r-- | src/vpn/gnunet-daemon-vpn.c | 47 | ||||
-rw-r--r-- | src/vpn/gnunet-vpn-helper.c | 43 |
2 files changed, 75 insertions, 15 deletions
diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c index 2b304faf0..23b50c699 100644 --- a/src/vpn/gnunet-daemon-vpn.c +++ b/src/vpn/gnunet-daemon-vpn.c | |||
@@ -26,6 +26,7 @@ | |||
26 | #include "platform.h" | 26 | #include "platform.h" |
27 | #include "gnunet_getopt_lib.h" | 27 | #include "gnunet_getopt_lib.h" |
28 | #include "gnunet_program_lib.h" | 28 | #include "gnunet_program_lib.h" |
29 | #include "gnunet_os_lib.h" | ||
29 | /* #include "gnunet_template_service.h" */ | 30 | /* #include "gnunet_template_service.h" */ |
30 | 31 | ||
31 | /** | 32 | /** |
@@ -33,6 +34,24 @@ | |||
33 | */ | 34 | */ |
34 | static int ret; | 35 | static int ret; |
35 | 36 | ||
37 | struct vpn_cls { | ||
38 | struct GNUNET_DISK_PipeHandle* helper_in; | ||
39 | struct GNUNET_DISK_PipeHandle* helper_out; | ||
40 | |||
41 | pid_t helper_pid; | ||
42 | }; | ||
43 | |||
44 | static void cleanup(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tskctx) { | ||
45 | struct vpn_cls* mycls = (struct vpn_cls*) cls; | ||
46 | if (tskctx->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) { | ||
47 | PLIBC_KILL(mycls->helper_pid, SIGTERM); | ||
48 | GNUNET_OS_process_wait(mycls->helper_pid); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | static void helper_read(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) { | ||
53 | } | ||
54 | |||
36 | /** | 55 | /** |
37 | * Main function that will be run by the scheduler. | 56 | * Main function that will be run by the scheduler. |
38 | * | 57 | * |
@@ -44,12 +63,23 @@ static int ret; | |||
44 | */ | 63 | */ |
45 | static void | 64 | static void |
46 | run (void *cls, | 65 | run (void *cls, |
47 | struct GNUNET_SCHEDULER_Handle *sched, | 66 | struct GNUNET_SCHEDULER_Handle *sched, |
48 | char *const *args, | 67 | char *const *args, |
49 | const char *cfgfile, | 68 | const char *cfgfile, |
50 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 69 | const struct GNUNET_CONFIGURATION_Handle *cfg) { |
51 | { | 70 | |
52 | /* main code here */ | 71 | struct vpn_cls* mycls = (struct vpn_cls*) cls; |
72 | |||
73 | GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls); | ||
74 | |||
75 | mycls->helper_in = GNUNET_DISK_pipe(1); | ||
76 | mycls->helper_out = GNUNET_DISK_pipe(1); | ||
77 | |||
78 | mycls->helper_pid = GNUNET_OS_start_process(mycls->helper_in, mycls->helper_out, "gnunet-vpn-helper", "gnunet-vpn-helper", NULL); | ||
79 | |||
80 | const struct GNUNET_DISK_FileHandle* fh = GNUNET_DISK_pipe_handle (mycls->helper_out, GNUNET_DISK_PIPE_END_READ); | ||
81 | |||
82 | GNUNET_SCHEDULER_add_read_file (sched, GNUNET_TIME_UNIT_FOREVER_REL, fh, &helper_read, mycls); | ||
53 | } | 83 | } |
54 | 84 | ||
55 | 85 | ||
@@ -66,12 +96,15 @@ main (int argc, char *const *argv) | |||
66 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | 96 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { |
67 | GNUNET_GETOPT_OPTION_END | 97 | GNUNET_GETOPT_OPTION_END |
68 | }; | 98 | }; |
99 | |||
100 | struct vpn_cls* cls = (struct vpn_cls*)malloc(sizeof(struct vpn_cls)); | ||
101 | |||
69 | return (GNUNET_OK == | 102 | return (GNUNET_OK == |
70 | GNUNET_PROGRAM_run (argc, | 103 | GNUNET_PROGRAM_run (argc, |
71 | argv, | 104 | argv, |
72 | "gnunet-daemon-vpn", | 105 | "gnunet-daemon-vpn", |
73 | gettext_noop ("help text"), | 106 | gettext_noop ("help text"), |
74 | options, &run, NULL)) ? ret : 1; | 107 | options, &run, cls)) ? ret : 1; |
75 | } | 108 | } |
76 | 109 | ||
77 | /* end of gnunet-daemon-vpn.c */ | 110 | /* end of gnunet-daemon-vpn.c */ |
diff --git a/src/vpn/gnunet-vpn-helper.c b/src/vpn/gnunet-vpn-helper.c index a5a569b9a..602bc4cfd 100644 --- a/src/vpn/gnunet-vpn-helper.c +++ b/src/vpn/gnunet-vpn-helper.c | |||
@@ -10,6 +10,8 @@ | |||
10 | 10 | ||
11 | #include <string.h> | 11 | #include <string.h> |
12 | 12 | ||
13 | #include <signal.h> | ||
14 | |||
13 | #include <stdio.h> | 15 | #include <stdio.h> |
14 | #include <unistd.h> | 16 | #include <unistd.h> |
15 | 17 | ||
@@ -27,6 +29,14 @@ struct in6_ifreq { | |||
27 | 29 | ||
28 | #endif | 30 | #endif |
29 | 31 | ||
32 | int running = 1; | ||
33 | |||
34 | void term(int sig) { | ||
35 | fprintf(stderr, "Got SIGTERM...\n"); | ||
36 | if (sig == SIGTERM) | ||
37 | running = 0; | ||
38 | } | ||
39 | |||
30 | static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */ | 40 | static void set_address(char* dev, char* address, unsigned long prefix_len) { /* {{{ */ |
31 | int fd = socket(AF_INET6, SOCK_DGRAM, 0); | 41 | int fd = socket(AF_INET6, SOCK_DGRAM, 0); |
32 | 42 | ||
@@ -87,11 +97,12 @@ static int copy (int in, int out) { | |||
87 | return 0; | 97 | return 0; |
88 | } | 98 | } |
89 | 99 | ||
90 | |||
91 | int main(int argc, char** argv) { | 100 | int main(int argc, char** argv) { |
92 | char dev[IFNAMSIZ]; | 101 | char dev[IFNAMSIZ]; |
93 | memset(dev, 0, IFNAMSIZ); | 102 | memset(dev, 0, IFNAMSIZ); |
94 | 103 | ||
104 | signal(SIGTERM, &term); | ||
105 | |||
95 | int fd_tun = init_tun(dev); | 106 | int fd_tun = init_tun(dev); |
96 | fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun); | 107 | fprintf(stderr, "Initialized the interface %s as %d.\n", dev, fd_tun); |
97 | 108 | ||
@@ -111,26 +122,42 @@ int main(int argc, char** argv) { | |||
111 | 122 | ||
112 | fd_set fds_w; | 123 | fd_set fds_w; |
113 | fd_set fds_r; | 124 | fd_set fds_r; |
114 | for(;;) { | 125 | |
126 | int r = 1; | ||
127 | int w = 1; | ||
128 | while(r != 0 && w != 0 && running == 1) { | ||
115 | FD_ZERO(&fds_w); | 129 | FD_ZERO(&fds_w); |
116 | FD_ZERO(&fds_r); | 130 | FD_ZERO(&fds_r); |
117 | 131 | ||
118 | FD_SET(0, &fds_r); | 132 | if (r) { |
119 | FD_SET(fd_tun, &fds_r); | 133 | FD_SET(fd_tun, &fds_r); |
134 | FD_SET(1, &fds_w); | ||
135 | } | ||
120 | 136 | ||
121 | FD_SET(1, &fds_w); | 137 | if (w) { |
122 | FD_SET(fd_tun, &fds_w); | 138 | FD_SET(0, &fds_r); |
139 | FD_SET(fd_tun, &fds_w); | ||
140 | } | ||
123 | 141 | ||
124 | int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0); | 142 | int r = select(fd_tun+1, &fds_r, &fds_w, (fd_set*)0, 0); |
125 | 143 | ||
126 | if(r > 0) { | 144 | if(r > 0) { |
127 | if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) { | 145 | if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) { |
128 | copy(0, fd_tun); | 146 | if (copy(0, fd_tun) < 0) { |
147 | fprintf(stderr, "Closing Write\n"); | ||
148 | shutdown(fd_tun, SHUT_WR); | ||
149 | w = 0; | ||
150 | } | ||
129 | } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, &fds_r)) { | 151 | } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, &fds_r)) { |
130 | copy(fd_tun, 1); | 152 | if (copy(fd_tun, 1) < 0) { |
153 | fprintf(stderr, "Closing Read\n"); | ||
154 | shutdown(fd_tun, SHUT_RD); | ||
155 | r = 0; | ||
156 | } | ||
131 | } | 157 | } |
132 | } | 158 | } |
133 | } | 159 | } |
160 | fprintf(stderr, "Quitting!\n"); | ||
134 | 161 | ||
135 | return 0; | 162 | return 0; |
136 | } | 163 | } |