aboutsummaryrefslogtreecommitdiff
path: root/src/vpn
diff options
context:
space:
mode:
authorPhilipp Tölke <toelke@in.tum.de>2011-03-14 15:16:29 +0000
committerPhilipp Tölke <toelke@in.tum.de>2011-03-14 15:16:29 +0000
commit85196cb7eec16cf4888fc26759c80e6f76eebd9f (patch)
treedaa9182db7141ca0747c0db796f0067865783457 /src/vpn
parentf934fdb1b54cc25c3524f76460937a65fd43edeb (diff)
downloadgnunet-85196cb7eec16cf4888fc26759c80e6f76eebd9f.tar.gz
gnunet-85196cb7eec16cf4888fc26759c80e6f76eebd9f.zip
prepare for tcp
Diffstat (limited to 'src/vpn')
-rw-r--r--src/vpn/gnunet-daemon-vpn-helper.c265
-rw-r--r--src/vpn/gnunet-daemon-vpn.c22
-rw-r--r--src/vpn/gnunet-daemon-vpn.h2
3 files changed, 162 insertions, 127 deletions
diff --git a/src/vpn/gnunet-daemon-vpn-helper.c b/src/vpn/gnunet-daemon-vpn-helper.c
index 7d9eb7d6e..9f8bc49f2 100644
--- a/src/vpn/gnunet-daemon-vpn-helper.c
+++ b/src/vpn/gnunet-daemon-vpn-helper.c
@@ -194,120 +194,165 @@ helper_write(void* cls, const struct GNUNET_SCHEDULER_TaskContext* tsdkctx) {
194 &helper_write, 194 &helper_write,
195 NULL); 195 NULL);
196} 196}
197
197/** 198/**
198 * Receive packets from the helper-process 199 * Receive packets from the helper-process
199 */ 200 */
200void 201void
201message_token(void *cls, 202message_token (void *cls,
202 void *client, 203 void *client, const struct GNUNET_MessageHeader *message)
203 const struct GNUNET_MessageHeader *message) { 204{
204 GNUNET_assert(ntohs(message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER); 205 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
205 206
206 struct tun_pkt *pkt_tun = (struct tun_pkt*) message; 207 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
207 208
208 /* ethertype is ipv6 */ 209 /* ethertype is ipv6 */
209 if (ntohs(pkt_tun->tun.type) == 0x86dd) 210 if (ntohs (pkt_tun->tun.type) == 0x86dd)
210 { 211 {
211 struct ip6_pkt *pkt6 = (struct ip6_pkt*) message; 212 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
212 GNUNET_assert(pkt6->ip6_hdr.version == 6); 213 GNUNET_assert (pkt6->ip6_hdr.version == 6);
213 struct ip6_tcp *pkt6_tcp; 214 struct ip6_tcp *pkt6_tcp;
214 struct ip6_udp *pkt6_udp; 215 struct ip6_udp *pkt6_udp;
215 struct ip6_icmp *pkt6_icmp; 216 struct ip6_icmp *pkt6_icmp;
216 GNUNET_HashCode* key; 217 GNUNET_HashCode *key;
217 218
218 switch(pkt6->ip6_hdr.nxthdr) 219 switch (pkt6->ip6_hdr.nxthdr)
219 { 220 {
220 case 0x06: 221 case 0x06: /* TCP */
221 pkt6_tcp = (struct ip6_tcp*)pkt6; 222 case 0x11: /* UDP */
222 break; 223 pkt6_tcp = (struct ip6_tcp *) pkt6;
223 case 0x11: 224 pkt6_udp = (struct ip6_udp *) pkt6;
224 pkt6_udp = (struct ip6_udp*)pkt6; 225
225 if ((key = address_mapping_exists(pkt6->ip6_hdr.dadr)) != NULL) 226 if ((key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
226 { 227 {
227 struct map_entry* me = GNUNET_CONTAINER_multihashmap_get(hashmap, key); 228 struct map_entry *me =
228 GNUNET_assert(me != NULL); 229 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
229 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Mapping exists; type: %d; UDP is %d; port: %x/%x!\n", me->desc.service_type, htonl(GNUNET_DNS_SERVICE_TYPE_UDP), pkt6_udp->udp_hdr.dpt, me->desc.ports); 230 GNUNET_assert (me != NULL);
230 GNUNET_free(key); 231 GNUNET_free (key);
231 if (me->desc.service_type & htonl(GNUNET_DNS_SERVICE_TYPE_UDP) && 232
232 (port_in_ports(me->desc.ports, pkt6_udp->udp_hdr.dpt) || 233 size_t size =
233 testBit(me->additional_ports, ntohs(pkt6_udp->udp_hdr.dpt)))) 234 sizeof (struct GNUNET_MESH_Tunnel *) +
234 { 235 sizeof (struct GNUNET_MessageHeader) +
235 size_t size = sizeof(struct GNUNET_MESH_Tunnel*) + sizeof(struct GNUNET_MessageHeader) + sizeof(GNUNET_HashCode) + ntohs(pkt6_udp->udp_hdr.len); 236 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth);
236 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc(size); 237
237 struct GNUNET_MessageHeader *hdr = (struct GNUNET_MessageHeader*)(cls+1); 238 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
238 GNUNET_HashCode* hc = (GNUNET_HashCode*)(hdr + 1); 239 struct GNUNET_MessageHeader *hdr =
239 240 (struct GNUNET_MessageHeader *) (cls + 1);
240 memcpy(hc, &me->desc.service_descriptor, sizeof(GNUNET_HashCode)); 241 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
241 memcpy(hc+1, &pkt6_udp->udp_hdr, ntohs(pkt6_udp->udp_hdr.len)); 242
242 243 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
243 if (me->tunnel == NULL) 244 sizeof (GNUNET_HashCode) +
244 { 245 ntohs (pkt6->ip6_hdr.paylgth));
245 *cls = GNUNET_MESH_peer_request_connect_all(mesh_handle, 246
246 GNUNET_TIME_UNIT_FOREVER_REL, 247 memcpy (hc, &me->desc.service_descriptor,
247 1, 248 sizeof (GNUNET_HashCode));
248 (struct GNUNET_PeerIdentity*)&me->desc.peer, 249
249 send_udp_to_peer, 250 if (0x11 == pkt6->ip6_hdr.nxthdr
250 NULL, 251 && me->
251 cls); 252 desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)
252 me->tunnel = *cls; 253 && (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt)
253 } 254 || testBit (me->additional_ports,
254 else 255 ntohs (pkt6_udp->udp_hdr.dpt))))
255 { 256 {
256 *cls = me->tunnel; 257 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
257 send_udp_to_peer(cls, (struct GNUNET_PeerIdentity*)1, NULL); 258
258 } 259 memcpy (hc + 1, &pkt6_udp->udp_hdr,
259 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Queued to send to peer %x\n", *((unsigned int*)&me->desc.peer)); 260 ntohs (pkt6_udp->udp_hdr.len));
260 } 261
261 } 262 }
262 break; 263 else if (0x11 == pkt6->ip6_hdr.nxthdr
263 case 0x3a: 264 && me->desc.
264 /* ICMPv6 */ 265 service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP)
265 pkt6_icmp = (struct ip6_icmp*)pkt6; 266 &&
266 /* If this packet is an icmp-echo-request and a mapping exists, answer */ 267 (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)
267 if (pkt6_icmp->icmp_hdr.type == 0x80 && (key = address_mapping_exists(pkt6->ip6_hdr.dadr)) != NULL) 268 && testBit (me->additional_ports,
268 { 269 ntohs (pkt6_tcp->tcp_hdr.spt))))
269 GNUNET_free(key); 270 {
270 pkt6_icmp = GNUNET_malloc(ntohs(pkt6->shdr.size)); 271 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_TCP);
271 memcpy(pkt6_icmp, pkt6, ntohs(pkt6->shdr.size)); 272
272 GNUNET_SCHEDULER_add_now(&send_icmp_response, pkt6_icmp); 273 memcpy (hc + 1, &pkt6_tcp->tcp_hdr,
273 } 274 ntohs (pkt6->ip6_hdr.paylgth));
274 break; 275
275 } 276 }
276 } 277 else
277 /* ethertype is ipv4 */ 278 {
278 else if (ntohs(pkt_tun->tun.type) == 0x0800) 279 GNUNET_free (cls);
279 { 280 cls = NULL;
280 struct ip_pkt *pkt = (struct ip_pkt*) message; 281 }
281 struct ip_udp *udp = (struct ip_udp*) message; 282 if (me->tunnel == NULL && NULL != cls)
282 GNUNET_assert(pkt->ip_hdr.version == 4); 283 {
283 284 *cls =
284 /* Send dns-packets to the service-dns */ 285 GNUNET_MESH_peer_request_connect_all (mesh_handle,
285 if (pkt->ip_hdr.proto == 0x11 && ntohs(udp->udp_hdr.dpt) == 53 ) 286 GNUNET_TIME_UNIT_FOREVER_REL,
286 { 287 1,
287 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */ 288 (struct
288 size_t len = sizeof(struct query_packet) + ntohs(udp->udp_hdr.len) - 9; 289 GNUNET_PeerIdentity
289 290 *) &me->desc.peer,
290 struct query_packet_list* query = GNUNET_malloc(len + 2*sizeof(struct query_packet_list*)); 291 send_pkt_to_peer,
291 query->pkt.hdr.type = htons(GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS); 292 NULL, cls);
292 query->pkt.hdr.size = htons(len); 293 me->tunnel = *cls;
293 query->pkt.orig_to = pkt->ip_hdr.dadr; 294 }
294 query->pkt.orig_from = pkt->ip_hdr.sadr; 295 else if (NULL != cls)
295 query->pkt.src_port = udp->udp_hdr.spt; 296 {
296 memcpy(query->pkt.data, udp->data, ntohs(udp->udp_hdr.len) - 8); 297 *cls = me->tunnel;
297 298 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1,
298 GNUNET_CONTAINER_DLL_insert_after(head, tail, tail, query); 299 NULL);
299 300 }
300 GNUNET_assert(head != NULL); 301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 302 "Queued to send to peer %x\n",
302 if (dns_connection != NULL) 303 *((unsigned int *) &me->desc.peer));
303 GNUNET_CLIENT_notify_transmit_ready(dns_connection, 304 }
304 len, 305 break;
305 GNUNET_TIME_UNIT_FOREVER_REL, 306 case 0x3a:
306 GNUNET_YES, 307 /* ICMPv6 */
307 &send_query, 308 pkt6_icmp = (struct ip6_icmp *) pkt6;
308 NULL); 309 /* If this packet is an icmp-echo-request and a mapping exists, answer */
309 } 310 if (pkt6_icmp->icmp_hdr.type == 0x80
310 } 311 && (key = address_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
312 {
313 GNUNET_free (key);
314 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
315 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
316 GNUNET_SCHEDULER_add_now (&send_icmp_response, pkt6_icmp);
317 }
318 break;
319 }
320 }
321 /* ethertype is ipv4 */
322 else if (ntohs (pkt_tun->tun.type) == 0x0800)
323 {
324 struct ip_pkt *pkt = (struct ip_pkt *) message;
325 struct ip_udp *udp = (struct ip_udp *) message;
326 GNUNET_assert (pkt->ip_hdr.version == 4);
327
328 /* Send dns-packets to the service-dns */
329 if (pkt->ip_hdr.proto == 0x11 && ntohs (udp->udp_hdr.dpt) == 53)
330 {
331 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
332 size_t len =
333 sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
334
335 struct query_packet_list *query =
336 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
337 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_LOCAL_QUERY_DNS);
338 query->pkt.hdr.size = htons (len);
339 query->pkt.orig_to = pkt->ip_hdr.dadr;
340 query->pkt.orig_from = pkt->ip_hdr.sadr;
341 query->pkt.src_port = udp->udp_hdr.spt;
342 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
343
344 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
345
346 GNUNET_assert (head != NULL);
347
348 if (dns_connection != NULL)
349 GNUNET_CLIENT_notify_transmit_ready (dns_connection,
350 len,
351 GNUNET_TIME_UNIT_FOREVER_REL,
352 GNUNET_YES,
353 &send_query, NULL);
354 }
355 }
311} 356}
312 357
313void write_to_helper(void* buf, size_t len) 358void write_to_helper(void* buf, size_t len)
diff --git a/src/vpn/gnunet-daemon-vpn.c b/src/vpn/gnunet-daemon-vpn.c
index 4d6a569f4..86a616109 100644
--- a/src/vpn/gnunet-daemon-vpn.c
+++ b/src/vpn/gnunet-daemon-vpn.c
@@ -139,20 +139,15 @@ send_icmp_response(void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc) {
139 139
140/** 140/**
141 * cls is the pointer to a GNUNET_MessageHeader that is 141 * cls is the pointer to a GNUNET_MessageHeader that is
142 * followed by the service-descriptor and the udp-packet that should be sent; 142 * followed by the service-descriptor and the packet that should be sent;
143 */ 143 */
144static size_t 144static size_t
145send_udp_to_peer_notify_callback (void *cls, size_t size, void *buf) 145send_pkt_to_peer_notify_callback (void *cls, size_t size, void *buf)
146{ 146{
147 struct GNUNET_MESH_Tunnel **tunnel = cls; 147 struct GNUNET_MESH_Tunnel **tunnel = cls;
148 struct GNUNET_MessageHeader *hdr = 148 struct GNUNET_MessageHeader *hdr =
149 (struct GNUNET_MessageHeader *) (tunnel + 1); 149 (struct GNUNET_MessageHeader *) (tunnel + 1);
150 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1); 150 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_pkt_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
151 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
152 hdr->size = htons (sizeof (struct GNUNET_MessageHeader) +
153 sizeof (GNUNET_HashCode) + ntohs (udp->len));
154 hdr->type = ntohs (GNUNET_MESSAGE_TYPE_SERVICE_UDP);
155 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "send_udp_to_peer_notify_callback: buf = %x; size = %u;\n", buf, size);
156 GNUNET_assert (size >= ntohs (hdr->size)); 151 GNUNET_assert (size >= ntohs (hdr->size));
157 memcpy (buf, hdr, ntohs (hdr->size)); 152 memcpy (buf, hdr, ntohs (hdr->size));
158 size = ntohs(hdr->size); 153 size = ntohs(hdr->size);
@@ -169,7 +164,7 @@ port_in_ports (uint64_t ports, uint16_t port)
169} 164}
170 165
171void 166void
172send_udp_to_peer (void *cls, 167send_pkt_to_peer (void *cls,
173 const struct GNUNET_PeerIdentity *peer, 168 const struct GNUNET_PeerIdentity *peer,
174 const struct GNUNET_TRANSPORT_ATS_Information *atsi) 169 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
175{ 170{
@@ -177,17 +172,12 @@ send_udp_to_peer (void *cls,
177 struct GNUNET_MESH_Tunnel **tunnel = cls; 172 struct GNUNET_MESH_Tunnel **tunnel = cls;
178 struct GNUNET_MessageHeader *hdr = 173 struct GNUNET_MessageHeader *hdr =
179 (struct GNUNET_MessageHeader *) (tunnel + 1); 174 (struct GNUNET_MessageHeader *) (tunnel + 1);
180 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
181 struct udp_pkt *udp = (struct udp_pkt *) (hc + 1);
182 GNUNET_MESH_notify_transmit_ready (*tunnel, 175 GNUNET_MESH_notify_transmit_ready (*tunnel,
183 GNUNET_NO, 176 GNUNET_NO,
184 42, 177 42,
185 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2), 178 GNUNET_TIME_relative_divide(GNUNET_CONSTANTS_MAX_CORK_DELAY, 2),
186 htons (sizeof 179 ntohs(hdr->size),
187 (struct GNUNET_MessageHeader) + 180 send_pkt_to_peer_notify_callback,
188 sizeof (GNUNET_HashCode) +
189 ntohs (udp->len)),
190 send_udp_to_peer_notify_callback,
191 cls); 181 cls);
192} 182}
193 183
diff --git a/src/vpn/gnunet-daemon-vpn.h b/src/vpn/gnunet-daemon-vpn.h
index e0e688f99..c640123b7 100644
--- a/src/vpn/gnunet-daemon-vpn.h
+++ b/src/vpn/gnunet-daemon-vpn.h
@@ -49,7 +49,7 @@ GNUNET_HashCode* address_mapping_exists(unsigned char addr[]);
49unsigned int port_in_ports (uint64_t ports, uint16_t port); 49unsigned int port_in_ports (uint64_t ports, uint16_t port);
50 50
51void 51void
52send_udp_to_peer (void *cls, 52send_pkt_to_peer (void *cls,
53 const struct GNUNET_PeerIdentity *peer, 53 const struct GNUNET_PeerIdentity *peer,
54 const struct GNUNET_TRANSPORT_ATS_Information *atsi); 54 const struct GNUNET_TRANSPORT_ATS_Information *atsi);
55 55