aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-nat-client-windows.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/gnunet-nat-client-windows.c')
-rw-r--r--src/transport/gnunet-nat-client-windows.c128
1 files changed, 64 insertions, 64 deletions
diff --git a/src/transport/gnunet-nat-client-windows.c b/src/transport/gnunet-nat-client-windows.c
index 3d3127157..2507a96b4 100644
--- a/src/transport/gnunet-nat-client-windows.c
+++ b/src/transport/gnunet-nat-client-windows.c
@@ -70,65 +70,65 @@
70/** 70/**
71 * IPv4 header. 71 * IPv4 header.
72 */ 72 */
73struct ip_header 73struct ip_header
74{ 74{
75 75
76 /** 76 /**
77 * Version (4 bits) + Internet header length (4 bits) 77 * Version (4 bits) + Internet header length (4 bits)
78 */ 78 */
79 uint8_t vers_ihl; 79 uint8_t vers_ihl;
80 80
81 /** 81 /**
82 * Type of service 82 * Type of service
83 */ 83 */
84 uint8_t tos; 84 uint8_t tos;
85 85
86 /** 86 /**
87 * Total length 87 * Total length
88 */ 88 */
89 uint16_t pkt_len; 89 uint16_t pkt_len;
90 90
91 /** 91 /**
92 * Identification 92 * Identification
93 */ 93 */
94 uint16_t id; 94 uint16_t id;
95 95
96 /** 96 /**
97 * Flags (3 bits) + Fragment offset (13 bits) 97 * Flags (3 bits) + Fragment offset (13 bits)
98 */ 98 */
99 uint16_t flags_frag_offset; 99 uint16_t flags_frag_offset;
100 100
101 /** 101 /**
102 * Time to live 102 * Time to live
103 */ 103 */
104 uint8_t ttl; 104 uint8_t ttl;
105 105
106 /** 106 /**
107 * Protocol 107 * Protocol
108 */ 108 */
109 uint8_t proto; 109 uint8_t proto;
110 110
111 /** 111 /**
112 * Header checksum 112 * Header checksum
113 */ 113 */
114 uint16_t checksum; 114 uint16_t checksum;
115 115
116 /** 116 /**
117 * Source address 117 * Source address
118 */ 118 */
119 uint32_t src_ip; 119 uint32_t src_ip;
120 120
121 /** 121 /**
122 * Destination address 122 * Destination address
123 */ 123 */
124 uint32_t dst_ip; 124 uint32_t dst_ip;
125}; 125};
126 126
127 127
128/** 128/**
129 * Format of ICMP packet. 129 * Format of ICMP packet.
130 */ 130 */
131struct icmp_ttl_exceeded_header 131struct icmp_ttl_exceeded_header
132{ 132{
133 uint8_t type; 133 uint8_t type;
134 134
@@ -179,7 +179,7 @@ static struct in_addr dummy;
179 179
180/** 180/**
181 * Port we are listening on (communicated to the server). 181 * Port we are listening on (communicated to the server).
182 */ 182 */
183static uint16_t port; 183static uint16_t port;
184 184
185 185
@@ -192,17 +192,17 @@ static uint16_t port;
192 * @param buf where to write the address result 192 * @param buf where to write the address result
193 * @return 1 on success 193 * @return 1 on success
194 */ 194 */
195static int 195static int
196inet_pton (int af, 196inet_pton (int af,
197 const char *cp, 197 const char *cp,
198 struct in_addr *buf) 198 struct in_addr *buf)
199{ 199{
200 buf->s_addr = inet_addr(cp); 200 buf->s_addr = inet_addr(cp);
201 if (buf->s_addr == INADDR_NONE) 201 if (buf->s_addr == INADDR_NONE)
202 { 202 {
203 fprintf(stderr, 203 fprintf(stderr,
204 "Error %d handling address %s", 204 "Error %d handling address %s",
205 WSAGetLastError(), 205 WSAGetLastError(),
206 cp); 206 cp);
207 return 0; 207 return 0;
208 } 208 }
@@ -217,16 +217,16 @@ inet_pton (int af,
217 * @param bytes number of bytes in data (must be multiple of 2) 217 * @param bytes number of bytes in data (must be multiple of 2)
218 * @return the CRC 16. 218 * @return the CRC 16.
219 */ 219 */
220static uint16_t 220static uint16_t
221calc_checksum(const uint16_t *data, 221calc_checksum(const uint16_t *data,
222 unsigned int bytes) 222 unsigned int bytes)
223{ 223{
224 uint32_t sum; 224 uint32_t sum;
225 unsigned int i; 225 unsigned int i;
226 226
227 sum = 0; 227 sum = 0;
228 for (i=0;i<bytes/2;i++) 228 for (i=0;i<bytes/2;i++)
229 sum += data[i]; 229 sum += data[i];
230 sum = (sum & 0xffff) + (sum >> 16); 230 sum = (sum & 0xffff) + (sum >> 16);
231 sum = htons(0xffff - sum); 231 sum = htons(0xffff - sum);
232 return sum; 232 return sum;
@@ -244,7 +244,7 @@ send_icmp_udp (const struct in_addr *my_ip,
244 const struct in_addr *other) 244 const struct in_addr *other)
245{ 245{
246 char packet[sizeof(struct ip_header) * 2 + 246 char packet[sizeof(struct ip_header) * 2 +
247 sizeof(struct icmp_ttl_exceeded_header) + 247 sizeof(struct icmp_ttl_exceeded_header) +
248 sizeof(struct udp_header)]; 248 sizeof(struct udp_header)];
249 struct ip_header ip_pkt; 249 struct ip_header ip_pkt;
250 struct icmp_ttl_exceeded_header icmp_pkt; 250 struct icmp_ttl_exceeded_header icmp_pkt;
@@ -265,10 +265,10 @@ send_icmp_udp (const struct in_addr *my_ip,
265 ip_pkt.checksum = 0; 265 ip_pkt.checksum = 0;
266 ip_pkt.src_ip = my_ip->s_addr; 266 ip_pkt.src_ip = my_ip->s_addr;
267 ip_pkt.dst_ip = other->s_addr; 267 ip_pkt.dst_ip = other->s_addr;
268 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, 268 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
269 sizeof (struct ip_header))); 269 sizeof (struct ip_header)));
270 memcpy(&packet[off], 270 memcpy(&packet[off],
271 &ip_pkt, 271 &ip_pkt,
272 sizeof(struct ip_header)); 272 sizeof(struct ip_header));
273 off += sizeof(struct ip_header); 273 off += sizeof(struct ip_header);
274 274
@@ -277,7 +277,7 @@ send_icmp_udp (const struct in_addr *my_ip,
277 icmp_pkt.checksum = 0; 277 icmp_pkt.checksum = 0;
278 icmp_pkt.unused = 0; 278 icmp_pkt.unused = 0;
279 memcpy(&packet[off], 279 memcpy(&packet[off],
280 &icmp_pkt, 280 &icmp_pkt,
281 sizeof(struct icmp_ttl_exceeded_header)); 281 sizeof(struct icmp_ttl_exceeded_header));
282 off += sizeof(struct icmp_ttl_exceeded_header); 282 off += sizeof(struct icmp_ttl_exceeded_header);
283 283
@@ -293,32 +293,32 @@ send_icmp_udp (const struct in_addr *my_ip,
293 ip_pkt.checksum = 0; 293 ip_pkt.checksum = 0;
294 ip_pkt.src_ip = other->s_addr; 294 ip_pkt.src_ip = other->s_addr;
295 ip_pkt.dst_ip = dummy.s_addr; 295 ip_pkt.dst_ip = dummy.s_addr;
296 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, 296 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
297 sizeof (struct ip_header))); 297 sizeof (struct ip_header)));
298 memcpy(&packet[off], 298 memcpy(&packet[off],
299 &ip_pkt, 299 &ip_pkt,
300 sizeof(struct ip_header)); 300 sizeof(struct ip_header));
301 off += sizeof(struct ip_header); 301 off += sizeof(struct ip_header);
302 302
303 /* build UDP header */ 303 /* build UDP header */
304 udp_pkt.src_port = htons(NAT_TRAV_PORT); 304 udp_pkt.src_port = htons(NAT_TRAV_PORT);
305 udp_pkt.dst_port = htons(NAT_TRAV_PORT); 305 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
306 udp_pkt.length = htons (port); 306 udp_pkt.length = htons (port);
307 udp_pkt.crc = 0; 307 udp_pkt.crc = 0;
308 memcpy(&packet[off], 308 memcpy(&packet[off],
309 &udp_pkt, 309 &udp_pkt,
310 sizeof(struct udp_header)); 310 sizeof(struct udp_header));
311 off += sizeof(struct udp_header); 311 off += sizeof(struct udp_header);
312 312
313 /* no go back to calculate ICMP packet checksum */ 313 /* no go back to calculate ICMP packet checksum */
314 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[off], 314 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[off],
315 sizeof (struct icmp_ttl_exceeded_header) + 315 sizeof (struct icmp_ttl_exceeded_header) +
316 sizeof (struct ip_header) + 316 sizeof (struct ip_header) +
317 sizeof (struct udp_header))); 317 sizeof (struct udp_header)));
318 memcpy (&packet[sizeof (struct ip_header)], 318 memcpy (&packet[sizeof (struct ip_header)],
319 &icmp_pkt, 319 &icmp_pkt,
320 sizeof (struct icmp_ttl_exceeded_header)); 320 sizeof (struct icmp_ttl_exceeded_header));
321 321
322 memset (&dst, 0, sizeof (dst)); 322 memset (&dst, 0, sizeof (dst));
323 dst.sin_family = AF_INET; 323 dst.sin_family = AF_INET;
324 dst.sin_addr = *other; 324 dst.sin_addr = *other;
@@ -354,8 +354,8 @@ send_icmp (const struct in_addr *my_ip,
354 struct icmp_ttl_exceeded_header icmp_ttl; 354 struct icmp_ttl_exceeded_header icmp_ttl;
355 struct icmp_echo_header icmp_echo; 355 struct icmp_echo_header icmp_echo;
356 struct sockaddr_in dst; 356 struct sockaddr_in dst;
357 char packet[sizeof (struct ip_header) * 2 + 357 char packet[sizeof (struct ip_header) * 2 +
358 sizeof (struct icmp_ttl_exceeded_header) + 358 sizeof (struct icmp_ttl_exceeded_header) +
359 sizeof(struct icmp_echo_header)]; 359 sizeof(struct icmp_echo_header)];
360 size_t off; 360 size_t off;
361 int err; 361 int err;
@@ -365,11 +365,11 @@ send_icmp (const struct in_addr *my_ip,
365 ip_pkt.vers_ihl = 0x45; 365 ip_pkt.vers_ihl = 0x45;
366 ip_pkt.tos = 0; 366 ip_pkt.tos = 0;
367 ip_pkt.pkt_len = htons (sizeof (packet)); 367 ip_pkt.pkt_len = htons (sizeof (packet));
368 ip_pkt.id = htons(256); 368 ip_pkt.id = htons(256);
369 ip_pkt.flags_frag_offset = 0; 369 ip_pkt.flags_frag_offset = 0;
370 ip_pkt.ttl = IPDEFTTL; 370 ip_pkt.ttl = IPDEFTTL;
371 ip_pkt.proto = IPPROTO_ICMP; 371 ip_pkt.proto = IPPROTO_ICMP;
372 ip_pkt.checksum = 0; 372 ip_pkt.checksum = 0;
373 ip_pkt.src_ip = my_ip->s_addr; 373 ip_pkt.src_ip = my_ip->s_addr;
374 ip_pkt.dst_ip = other->s_addr; 374 ip_pkt.dst_ip = other->s_addr;
375 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, 375 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
@@ -381,7 +381,7 @@ send_icmp (const struct in_addr *my_ip,
381 381
382 /* icmp reply: time exceeded */ 382 /* icmp reply: time exceeded */
383 icmp_ttl.type = ICMP_TIME_EXCEEDED; 383 icmp_ttl.type = ICMP_TIME_EXCEEDED;
384 icmp_ttl.code = 0; 384 icmp_ttl.code = 0;
385 icmp_ttl.reserved = 0; 385 icmp_ttl.reserved = 0;
386 icmp_ttl.checksum = 0; 386 icmp_ttl.checksum = 0;
387 memcpy (&packet[off], 387 memcpy (&packet[off],
@@ -393,7 +393,7 @@ send_icmp (const struct in_addr *my_ip,
393 ip_pkt.vers_ihl = 0x45; 393 ip_pkt.vers_ihl = 0x45;
394 ip_pkt.tos = 0; 394 ip_pkt.tos = 0;
395 ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header)); 395 ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
396 ip_pkt.id = htons (256); 396 ip_pkt.id = htons (256);
397 ip_pkt.flags_frag_offset = 0; 397 ip_pkt.flags_frag_offset = 0;
398 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ 398 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
399 ip_pkt.proto = IPPROTO_ICMP; 399 ip_pkt.proto = IPPROTO_ICMP;
@@ -401,9 +401,9 @@ send_icmp (const struct in_addr *my_ip,
401 ip_pkt.dst_ip = dummy.s_addr; 401 ip_pkt.dst_ip = dummy.s_addr;
402 ip_pkt.checksum = 0; 402 ip_pkt.checksum = 0;
403 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, 403 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
404 sizeof (struct ip_header))); 404 sizeof (struct ip_header)));
405 memcpy (&packet[off], 405 memcpy (&packet[off],
406 &ip_pkt, 406 &ip_pkt,
407 sizeof (struct ip_header)); 407 sizeof (struct ip_header));
408 off += sizeof (struct ip_header); 408 off += sizeof (struct ip_header);
409 409
@@ -412,17 +412,17 @@ send_icmp (const struct in_addr *my_ip,
412 icmp_echo.reserved = 0; 412 icmp_echo.reserved = 0;
413 icmp_echo.checksum = 0; 413 icmp_echo.checksum = 0;
414 icmp_echo.data = htons(port); 414 icmp_echo.data = htons(port);
415 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo, 415 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
416 sizeof (struct icmp_echo_header))); 416 sizeof (struct icmp_echo_header)));
417 memcpy (&packet[off], 417 memcpy (&packet[off],
418 &icmp_echo, 418 &icmp_echo,
419 sizeof(struct icmp_echo_header)); 419 sizeof(struct icmp_echo_header));
420 420
421 /* no go back to calculate ICMP packet checksum */ 421 /* no go back to calculate ICMP packet checksum */
422 off = sizeof (struct ip_header); 422 off = sizeof (struct ip_header);
423 icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off], 423 icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
424 sizeof (struct icmp_ttl_exceeded_header) + 424 sizeof (struct icmp_ttl_exceeded_header) +
425 sizeof (struct ip_header) + 425 sizeof (struct ip_header) +
426 sizeof (struct icmp_echo_header))); 426 sizeof (struct icmp_echo_header)));
427 memcpy (&packet[off], 427 memcpy (&packet[off],
428 &icmp_ttl, 428 &icmp_ttl,
@@ -432,18 +432,18 @@ send_icmp (const struct in_addr *my_ip,
432 dst.sin_family = AF_INET; 432 dst.sin_family = AF_INET;
433 dst.sin_addr = *other; 433 dst.sin_addr = *other;
434 434
435 err = sendto(rawsock, 435 err = sendto(rawsock,
436 packet, 436 packet,
437 sizeof (packet), 0, 437 sizeof (packet), 0,
438 (struct sockaddr*)&dst, 438 (struct sockaddr*)&dst,
439 sizeof(dst)); 439 sizeof(dst));
440 440
441 if (err < 0) 441 if (err < 0)
442 { 442 {
443 fprintf(stderr, 443 fprintf(stderr,
444 "sendto failed: %s\n", strerror(errno)); 444 "sendto failed: %s\n", strerror(errno));
445 } 445 }
446 else if (sizeof (packet) != (size_t) err) 446 else if (sizeof (packet) != (size_t) err)
447 { 447 {
448 fprintf(stderr, 448 fprintf(stderr,
449 "Error: partial send of ICMP message\n"); 449 "Error: partial send of ICMP message\n");
@@ -470,10 +470,10 @@ make_raw_socket ()
470 "Error opening RAW socket: %s\n", 470 "Error opening RAW socket: %s\n",
471 strerror (errno)); 471 strerror (errno));
472 return INVALID_SOCKET; 472 return INVALID_SOCKET;
473 } 473 }
474 if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen)) 474 if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen))
475 { 475 {
476 fprintf(stderr, 476 fprintf(stderr,
477 "Error setting SO_BROADCAST to ON: %s\n", 477 "Error setting SO_BROADCAST to ON: %s\n",
478 strerror (errno)); 478 strerror (errno));
479 closesocket(rawsock); 479 closesocket(rawsock);
@@ -482,7 +482,7 @@ make_raw_socket ()
482 482
483 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen)) 483 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen))
484 { 484 {
485 fprintf(stderr, 485 fprintf(stderr,
486 "Error setting IP_HDRINCL to ON: %s\n", 486 "Error setting IP_HDRINCL to ON: %s\n",
487 strerror (errno)); 487 strerror (errno));
488 closesocket(rawsock); 488 closesocket(rawsock);
@@ -531,7 +531,7 @@ main (int argc, char *const *argv)
531 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n"); 531 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
532 return 2; 532 return 2;
533 } 533 }
534 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) 534 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
535 { 535 {
536 fprintf (stderr, 536 fprintf (stderr,
537 "Internal error converting dummy IP to binary.\n"); 537 "Internal error converting dummy IP to binary.\n");