aboutsummaryrefslogtreecommitdiff
path: root/src/vpn
diff options
context:
space:
mode:
authorPhilipp Tölke <toelke@in.tum.de>2010-07-20 05:45:29 +0000
committerPhilipp Tölke <toelke@in.tum.de>2010-07-20 05:45:29 +0000
commitdd29458d6f7cbedb11088882913d4657aa6a90d0 (patch)
tree34bf80df9b1e68e6b1241c51f89273616689d87d /src/vpn
parent0d46ad9c6bc1dcf99403a77791bbd85a97c671b3 (diff)
downloadgnunet-dd29458d6f7cbedb11088882913d4657aa6a90d0.tar.gz
gnunet-dd29458d6f7cbedb11088882913d4657aa6a90d0.zip
communicate with the daemon over a specified protocoll
Diffstat (limited to 'src/vpn')
-rw-r--r--src/vpn/gnunet-vpn-helper-p.h6
-rw-r--r--src/vpn/gnunet-vpn-helper.c95
2 files changed, 75 insertions, 26 deletions
diff --git a/src/vpn/gnunet-vpn-helper-p.h b/src/vpn/gnunet-vpn-helper-p.h
index d2fac593a..664c40389 100644
--- a/src/vpn/gnunet-vpn-helper-p.h
+++ b/src/vpn/gnunet-vpn-helper-p.h
@@ -1,8 +1,12 @@
1#ifndef GN_VPN_HELPER_P_H 1#ifndef GN_VPN_HELPER_P_H
2#define GN_VPN_HELPER_P_H 2#define GN_VPN_HELPER_P_H
3 3
4struct suid_packet_header {
5 uint32_t size;
6};
7
4struct suid_packet { 8struct suid_packet {
5 unsigned int size; 9 struct suid_packet_header hdr;
6 unsigned char data[1]; 10 unsigned char data[1];
7}; 11};
8 12
diff --git a/src/vpn/gnunet-vpn-helper.c b/src/vpn/gnunet-vpn-helper.c
index 602bc4cfd..9bbf66283 100644
--- a/src/vpn/gnunet-vpn-helper.c
+++ b/src/vpn/gnunet-vpn-helper.c
@@ -70,7 +70,7 @@ static void set_address(char* dev, char* address, unsigned long prefix_len) { /*
70 /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr); 70 /* FIXME */ ioctl(fd, SIOCSIFFLAGS, &ifr);
71} /* }}} */ 71} /* }}} */
72 72
73void setnonblocking(int fd) { 73void setnonblocking(int fd) {/*{{{*/
74 int opts; 74 int opts;
75 75
76 opts = fcntl(fd,F_GETFL); 76 opts = fcntl(fd,F_GETFL);
@@ -82,22 +82,11 @@ void setnonblocking(int fd) {
82 perror("fcntl(F_SETFL)"); 82 perror("fcntl(F_SETFL)");
83 } 83 }
84 return; 84 return;
85} 85}/*}}}*/
86 86
87static int copy (int in, int out) { 87int main(int argc, char** argv) {
88 unsigned char buf[65600]; // 64k + 64; 88 unsigned char buf[65600]; // 64k + 64;
89 int r = read(in, buf, 65600);
90 int w = 0;
91 if (r < 0) return r;
92 while (w < r) {
93 int t = write(out, buf + w, r - w);
94 if (t > 0) w += t;
95 if (t < 0) return t;
96 }
97 return 0;
98}
99 89
100int main(int argc, char** argv) {
101 char dev[IFNAMSIZ]; 90 char dev[IFNAMSIZ];
102 memset(dev, 0, IFNAMSIZ); 91 memset(dev, 0, IFNAMSIZ);
103 92
@@ -123,18 +112,19 @@ int main(int argc, char** argv) {
123 fd_set fds_w; 112 fd_set fds_w;
124 fd_set fds_r; 113 fd_set fds_r;
125 114
126 int r = 1; 115 int rea = 1;
127 int w = 1; 116 int wri = 1;
128 while(r != 0 && w != 0 && running == 1) { 117outer:
118 while(rea != 0 && wri != 0 && running == 1) {
129 FD_ZERO(&fds_w); 119 FD_ZERO(&fds_w);
130 FD_ZERO(&fds_r); 120 FD_ZERO(&fds_r);
131 121
132 if (r) { 122 if (rea) {
133 FD_SET(fd_tun, &fds_r); 123 FD_SET(fd_tun, &fds_r);
134 FD_SET(1, &fds_w); 124 FD_SET(1, &fds_w);
135 } 125 }
136 126
137 if (w) { 127 if (wri) {
138 FD_SET(0, &fds_r); 128 FD_SET(0, &fds_r);
139 FD_SET(fd_tun, &fds_w); 129 FD_SET(fd_tun, &fds_w);
140 } 130 }
@@ -143,16 +133,71 @@ int main(int argc, char** argv) {
143 133
144 if(r > 0) { 134 if(r > 0) {
145 if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) { 135 if (FD_ISSET(0, &fds_r) && FD_ISSET(fd_tun, &fds_w)) {
146 if (copy(0, fd_tun) < 0) { 136 struct suid_packet *pkt = (struct suid_packet*) buf;
147 fprintf(stderr, "Closing Write\n"); 137 r = read(0, buf, sizeof(struct suid_packet_header));
138 if (r < 0) {
139 fprintf(stderr, "read-error: %m\n");
148 shutdown(fd_tun, SHUT_WR); 140 shutdown(fd_tun, SHUT_WR);
149 w = 0; 141 shutdown(0, SHUT_RD);
142 wri=0;
143 goto outer;
144 }
145 r = 0;
146 while (r < ntohl(pkt->hdr.size)) {
147 int t = read(0, buf + r, ntohl(pkt->hdr.size) - r);
148 if (r < 0) {
149 fprintf(stderr, "read-error: %m\n");
150 shutdown(fd_tun, SHUT_WR);
151 shutdown(0, SHUT_RD);
152 wri=0;
153 goto outer;
154 }
155 r += t;
156 }
157 r = 0;
158 while (r < ntohl(pkt->hdr.size) - sizeof(struct suid_packet_header)) {
159 int t = write(fd_tun, pkt->data, ntohl(pkt->hdr.size) - sizeof(struct suid_packet_header) - r);
160 if (t < 0) {
161 fprintf(stderr, "write-error 3: %m\n");
162 shutdown(fd_tun, SHUT_WR);
163 shutdown(0, SHUT_RD);
164 wri = 0;
165 goto outer;
166 }
167 r += t;
150 } 168 }
151 } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, &fds_r)) { 169 } else if (FD_ISSET(1, &fds_w) && FD_ISSET(fd_tun, &fds_r)) {
152 if (copy(fd_tun, 1) < 0) { 170 r = read(fd_tun, buf, 65600);
153 fprintf(stderr, "Closing Read\n"); 171 if (r < 0) {
172 fprintf(stderr, "read-error: %m\n");
154 shutdown(fd_tun, SHUT_RD); 173 shutdown(fd_tun, SHUT_RD);
155 r = 0; 174 shutdown(1, SHUT_WR);
175 rea = 0;
176 goto outer;
177 }
178 struct suid_packet_header hdr = { .size = htonl(r + sizeof(struct suid_packet_header))};
179 r = 0;
180 while(r < sizeof(struct suid_packet_header)) {
181 int t = write(1, &hdr, sizeof(struct suid_packet_header) - r);
182 if (t < 0) {
183 fprintf(stderr, "write-error 2: %m\n");
184 shutdown(fd_tun, SHUT_RD);
185 shutdown(1, SHUT_WR);
186 rea = 0;
187 goto outer;
188 }
189 r += t;
190 }
191 while(r < ntohl(hdr.size)) {
192 int t = write(1, buf, ntohl(hdr.size) - r);
193 if (t < 0) {
194 fprintf(stderr, "write-error 1: %m, written %d/%d\n", r, ntohl(hdr.size));
195 shutdown(fd_tun, SHUT_RD);
196 shutdown(1, SHUT_WR);
197 rea = 0;
198 goto outer;
199 }
200 r += t;
156 } 201 }
157 } 202 }
158 } 203 }