aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-nat-client-windows.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-08-19 09:57:24 +0000
committerNathan S. Evans <evans@in.tum.de>2010-08-19 09:57:24 +0000
commit3f6ca29ce69c4900627686b6b916b93f97826bbf (patch)
tree00619f9a8887e10a9f76c35818c76e42cb9b9480 /src/transport/gnunet-nat-client-windows.c
parented0515f4f5bbf646818e36b9d106c9a34b4cde26 (diff)
downloadgnunet-3f6ca29ce69c4900627686b6b916b93f97826bbf.tar.gz
gnunet-3f6ca29ce69c4900627686b6b916b93f97826bbf.zip
workingish?
Diffstat (limited to 'src/transport/gnunet-nat-client-windows.c')
-rw-r--r--src/transport/gnunet-nat-client-windows.c139
1 files changed, 15 insertions, 124 deletions
diff --git a/src/transport/gnunet-nat-client-windows.c b/src/transport/gnunet-nat-client-windows.c
index 997fa7051..64ee932fe 100644
--- a/src/transport/gnunet-nat-client-windows.c
+++ b/src/transport/gnunet-nat-client-windows.c
@@ -42,17 +42,9 @@
42 * - Nathan Evans 42 * - Nathan Evans
43 */ 43 */
44#define _GNU_SOURCE 44#define _GNU_SOURCE
45#ifdef WIN32 45
46#include <ws2tcpip.h>
46#include <winsock2.h> 47#include <winsock2.h>
47#else
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <arpa/inet.h>
51#include <sys/select.h>
52#include <netinet/ip.h>
53#include <netinet/ip_icmp.h>
54#include <netinet/in.h>
55#endif
56#include <sys/time.h> 48#include <sys/time.h>
57#include <sys/types.h> 49#include <sys/types.h>
58#include <unistd.h> 50#include <unistd.h>
@@ -64,17 +56,12 @@
64#include <time.h> 56#include <time.h>
65 57
66 58
67#ifdef WIN32
68typedef unsigned int uid_t; 59typedef unsigned int uid_t;
69typedef SOCKET Socket; 60typedef SOCKET Socket;
70typedef unsigned short ushort; 61typedef unsigned short ushort;
71#define ICMP_ECHO 8 62#define ICMP_ECHO 8
72#define IPDEFTTL 64 /* default ttl, from RFC 1340 */ 63#define IPDEFTTL 64 /* default ttl, from RFC 1340 */
73#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */ 64#define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
74#define IP_HDRINCL 3 /* int; Header is included with data. */
75#else
76typedef int Socket;
77#endif
78 65
79/** 66/**
80 * Must match IP given in the server. 67 * Must match IP given in the server.
@@ -130,7 +117,6 @@ static struct in_addr dummy;
130 117
131static uint32_t port; 118static uint32_t port;
132 119
133#if WIN32
134/** 120/**
135 * @param af address family 121 * @param af address family
136 * @param cp the address to print 122 * @param cp the address to print
@@ -148,7 +134,6 @@ static int inet_pton (int af, char *cp, struct in_addr *buf)
148 else 134 else
149 return 1; 135 return 1;
150} 136}
151#endif
152 137
153static uint16_t 138static uint16_t
154calc_checksum(const uint16_t *data, 139calc_checksum(const uint16_t *data,
@@ -180,76 +165,6 @@ make_echo (const struct in_addr *src_ip,
180 sizeof (struct icmp_echo_packet))); 165 sizeof (struct icmp_echo_packet)));
181} 166}
182 167
183static void
184make_echo2 (const struct in_addr *src_ip,
185 struct icmp_packet *echo)
186{
187 memset(echo, 0, sizeof(struct icmp_packet));
188 echo->type = ICMP_ECHO;
189 echo->code = 0;
190 echo->reserved = 0;
191 echo->checksum = 0;
192 echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
193}
194
195/**
196 * Send an ICMP message to the dummy IP.
197 *
198 * @param my_ip source address (our ip address)
199 */
200static void
201send_icmp_echo (const struct in_addr *my_ip)
202{
203 struct icmp_packet icmp_echo;
204 struct sockaddr_in dst;
205 size_t off;
206 int err;
207 struct ip_packet ip_pkt;
208 struct icmp_packet icmp_pkt;
209 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
210
211 off = 0;
212 memset(&ip_pkt, 0, sizeof(ip_pkt));
213 ip_pkt.vers_ihl = 0x45;
214 ip_pkt.tos = 0;
215 ip_pkt.pkt_len = sizeof (packet);
216 ip_pkt.id = 1;
217 ip_pkt.flags_frag_offset = 0;
218 ip_pkt.ttl = IPDEFTTL;
219 ip_pkt.proto = IPPROTO_ICMP;
220 ip_pkt.checksum = 0;
221 ip_pkt.src_ip = my_ip->s_addr;
222 ip_pkt.dst_ip = dummy.s_addr;
223 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
224 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
225 off += sizeof (ip_pkt);
226 make_echo2 (my_ip, &icmp_echo);
227 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
228 off += sizeof (icmp_echo);
229
230 memset (&dst, 0, sizeof (dst));
231 dst.sin_family = AF_INET;
232 dst.sin_addr = dummy;
233 err = sendto(rawsock,
234 packet, off, 0,
235 (struct sockaddr*)&dst,
236 sizeof(dst));
237
238 fprintf(stderr, "Sent %d bytes\n", err);
239 if (err < 0)
240 {
241#if VERBOSE
242 fprintf(stderr,
243 "sendto failed: %s\n", strerror(errno));
244#endif
245 }
246 else if (err != off)
247 {
248 fprintf(stderr,
249 "Error: partial send of ICMP message\n");
250 }
251}
252
253/** 168/**
254 * Send an ICMP message to the target. 169 * Send an ICMP message to the target.
255 * 170 *
@@ -339,8 +254,6 @@ send_icmp_udp (const struct in_addr *my_ip,
339 (struct sockaddr*)&dst, 254 (struct sockaddr*)&dst,
340 sizeof(dst)); 255 sizeof(dst));
341 256
342 fprintf(stderr, "Sent %d bytes\n", err);
343
344 if (err < 0) 257 if (err < 0)
345 { 258 {
346 fprintf(stderr, 259 fprintf(stderr,
@@ -410,7 +323,6 @@ send_icmp (const struct in_addr *my_ip,
410 ip_pkt.vers_ihl = 0x45; 323 ip_pkt.vers_ihl = 0x45;
411 ip_pkt.tos = 0; 324 ip_pkt.tos = 0;
412 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet)); 325 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
413 fprintf(stderr, "Set pkt_len to %d\n", ip_pkt.pkt_len);
414 ip_pkt.id = 1; 326 ip_pkt.id = 1;
415 ip_pkt.flags_frag_offset = 0; 327 ip_pkt.flags_frag_offset = 0;
416 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */ 328 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
@@ -435,7 +347,6 @@ send_icmp (const struct in_addr *my_ip,
435 (struct sockaddr*)&dst, 347 (struct sockaddr*)&dst,
436 sizeof(dst)); /* or sizeof 'struct sockaddr'? */ 348 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
437 349
438 fprintf(stderr, "Sent %d bytes\n", err);
439 if (err < 0) 350 if (err < 0)
440 { 351 {
441 fprintf(stderr, 352 fprintf(stderr,
@@ -452,8 +363,9 @@ send_icmp (const struct in_addr *my_ip,
452static Socket 363static Socket
453make_raw_socket () 364make_raw_socket ()
454{ 365{
455 const int one = 1; 366 DWORD bOptVal = TRUE;
456 int ret; 367 int bOptLen = sizeof(bOptVal);
368 Socket ret;
457 369
458 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW); 370 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
459 if (-1 == ret) 371 if (-1 == ret)
@@ -463,16 +375,12 @@ make_raw_socket ()
463 strerror (errno)); 375 strerror (errno));
464 return -1; 376 return -1;
465 } 377 }
466 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, 378 if (setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
467 (char *)&one, sizeof(one)) == -1) 379 fprintf(stderr, "Error setting SO_BROADCAST: ON\n");
468 fprintf(stderr, 380
469 "setsockopt failed: %s\n", 381 if (setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
470 strerror (errno)); 382 fprintf(stderr, "Error setting IP_HDRINCL: ON\n");
471 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, 383
472 (char *)&one, sizeof(one)) == -1)
473 fprintf(stderr,
474 "setsockopt failed: %s\n",
475 strerror (errno));
476 return ret; 384 return ret;
477} 385}
478 386
@@ -482,11 +390,7 @@ main (int argc, char *const *argv)
482{ 390{
483 struct in_addr external; 391 struct in_addr external;
484 struct in_addr target; 392 struct in_addr target;
485#ifndef WIN32
486 uid_t uid;
487#endif
488 393
489#ifdef WIN32
490 // WSA startup 394 // WSA startup
491 WSADATA wsaData; 395 WSADATA wsaData;
492 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0) 396 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
@@ -494,18 +398,10 @@ main (int argc, char *const *argv)
494 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n"); 398 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
495 return 4; // ERROR 399 return 4; // ERROR
496 } 400 }
497#endif
498 401
499 if (-1 == (rawsock = make_raw_socket())) 402 if (-1 == (rawsock = make_raw_socket()))
500 return 1; 403 return 1;
501 404
502#ifndef WIN32
503 uid = getuid ();
504 if (0 != setresuid (uid, uid, uid))
505 fprintf (stderr,
506 "Failed to setresuid: %s\n",
507 strerror (errno));
508#endif
509 if (argc != 4) 405 if (argc != 4)
510 { 406 {
511 fprintf (stderr, 407 fprintf (stderr,
@@ -529,20 +425,15 @@ main (int argc, char *const *argv)
529 strerror (errno)); 425 strerror (errno));
530 abort (); 426 abort ();
531 } 427 }
532 fprintf(stderr, "Sending icmp message.\n"); 428
533 send_icmp_echo(&target);
534 fprintf(stderr, "Sending icmp message.\n");
535 send_icmp (&external, 429 send_icmp (&external,
536 &target); 430 &target);
537 fprintf(stderr, "Sending icmp udp message.\n");
538 send_icmp_udp (&external, 431 send_icmp_udp (&external,
539 &target); 432 &target);
540 433
541#ifdef WIN32
542 WSACleanup (); 434 WSACleanup ();
543#endif 435 closesocket (rawsock);
544 close (rawsock);
545 return 0; 436 return 0;
546} 437}
547 438
548/* end of gnunet-nat-client.c */ 439/* end of gnunet-nat-client-windows.c */