diff -ru PingTunnel.orig\Makefile PingTunnel\Makefile --- PingTunnel.orig\Makefile Thu May 26 17:06:24 2005 +++ PingTunnel\Makefile Mon Jan 12 11:18:54 2009 @@ -1,10 +1,17 @@ # Makefile for the pingtunnel utility # (c) 2004-2005 Daniel Stødle, daniels@stud.cs.uit.no +# ptunnel.exe target added by Mike Miller, mike@mikeage.net CC = gcc CFLAGS = -Wall -g LDOPTS = -lpthread -lpcap PT_OBJS = ptunnel.o md5.o + +WIN32_CC = mingw32-gcc +WIN32_CFLAGS = -g -Wall -DWIN32 -I"c:\Program Files\WpdPack\Include" +WIN32_LDOPTS = -lwpcap -lwsock32 -L"c:\Program Files\WpdPack\Lib" +WIN32_PT_OBJS = ptunnel.obj md5.obj + prefix = /usr bindir = $(prefix)/bin mandir = $(prefix)/share/man/man8 @@ -27,9 +34,13 @@ ptunnel: $(PT_OBJS) $(CC) -o $@ $^ $(LDOPTS) +ptunnel.exe: $(WIN32_PT_OBJS) + $(CC) -o $@ $^ $(WIN32_LDOPTS) + clean: - rm -f *.o ptunnel - rm -f .depend + -rm -f *.o ptunnel + -rm -f *.obj ptunnel.exe + -rm -f .depend depend: .depend .depend: @@ -37,5 +48,8 @@ %.o:%.c $(CC) $(CFLAGS) -c -o $@ $< + +%.obj:%.c + $(WIN32_CC) $(WIN32_CFLAGS) -c -o $@ $< -include .depend diff -ru PingTunnel.orig\ptunnel.c PingTunnel\ptunnel.c --- PingTunnel.orig\ptunnel.c Thu May 26 17:06:24 2005 +++ PingTunnel\ptunnel.c Mon Jan 12 11:14:04 2009 @@ -41,25 +41,53 @@ */ #include "ptunnel.h" +#ifndef WIN32 #include +#else /* WIN32 */ +/* pthread porting to windows */ +typedef CRITICAL_SECTION pthread_mutex_t; +typedef unsigned long pthread_t; +#define pthread_mutex_init InitializeCriticalSectionAndSpinCount +#define pthread_mutex_lock EnterCriticalSection +#define pthread_mutex_unlock LeaveCriticalSection +#endif /* WIN32 */ #include +#ifndef WIN32 #include #include #include #include #include #include +#endif /* !WIN32 */ #include #include #include #include #include +#ifndef WIN32 #include +#endif /* !WIN32 */ #include +#ifndef WIN32 #include +#endif /* !WIN32 */ #include #include "md5.h" +#ifdef WIN32 +#include + +/* Map errno (which Winsock doesn't use) to GetLastError; include the code in the strerror */ +#ifdef errno +#undef errno +#endif /* errno */ +#define errno GetLastError() +/* Local error string storage */ +static char errorstr[255]; +static char * print_last_windows_error() { DWORD last_error = GetLastError(); memset(errorstr, 0, sizeof(errorstr)); FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, last_error, 0, errorstr, sizeof(errorstr), NULL); snprintf(errorstr, sizeof(errorstr), "%s (%d)", errorstr, last_error); return errorstr; } +#define strerror(x) print_last_windows_error() +#endif /* WIN32 */ // Lots of globals pthread_mutex_t chain_lock, // Lock protecting the chain of connections num_threads_lock; // Lock protecting the num_threads variable @@ -189,11 +217,33 @@ pt_log(kLog_info, "Starting ptunnel v %d.%.2d.\n", kMajor_version, kMinor_version); pt_log(kLog_info, "(c) 2004-2005 Daniel Stoedle, daniels@cs.uit.no\n"); pt_log(kLog_info, "%s.\n", (mode == kMode_forward ? "Relaying packets from incoming TCP streams" : "Forwarding incoming ping packets over TCP")); - + +#ifndef WIN32 signal(SIGPIPE, SIG_IGN); +#endif /* !WIN32 */ + +#ifdef WIN32 + WORD wVersionRequested; + WSADATA wsaData; + int err; + + wVersionRequested = MAKEWORD( 2, 2 ); + + err = WSAStartup( wVersionRequested, &wsaData ); + if ( err != 0 ) { + return -1; + } + + if ( LOBYTE( wsaData.wVersion ) != 2 || + HIBYTE( wsaData.wVersion ) != 2 ) { + WSACleanup(); + return -1; + } +#endif /* WIN32 */ + pthread_mutex_init(&chain_lock, 0); pthread_mutex_init(&num_threads_lock, 0); - + // Check mode, validate arguments and start either client or proxy. if (mode == kMode_forward) { if (!dest_proxy_addr || !dest_tcp_addr || !tcp_port || !tcp_listen_port) { @@ -209,7 +259,11 @@ // Clean up if (log_file != stdout) fclose(log_file); - + +#ifdef WIN32 + WSACleanup(); +#endif /* WIN32 */ + pt_log(kLog_info, "ptunnel is exiting.\n"); return 0; } @@ -262,7 +316,7 @@ pt_log(kLog_debug, "Starting forwarder..\n"); // Open our listening socket sock = socket(AF_INET, SOCK_STREAM, 0); - if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &yes, sizeof(int)) == -1) { pt_log(kLog_error, "Failed to set SO_REUSEADDR option on listening socket: %s\n", strerror(errno)); close(sock); return; @@ -279,9 +333,19 @@ server_sock = sock; // Look up IP of final destination (ie, where the proxy should send the packets) host_ent = gethostbyname(dest_tcp_addr); + if (!host_ent) { + pt_log(kLog_error, "Couldn't look up destination address: %s\n", dest_tcp_addr); + close(sock); + return; + } dst_ip = *(uint32_t*)host_ent->h_addr_list[0]; // Look up proxy's IP host_ent = gethostbyname(dest_proxy_addr); + if (!host_ent) { + pt_log(kLog_error, "Couldn't look up proxy: %s\n", dest_proxy_addr); + close(sock); + return; + } proxy_ip = *(uint32_t*)host_ent->h_addr_list[0]; // Fill out address structure memset(&dest_addr, 0, sizeof(struct sockaddr_in)); @@ -307,7 +371,12 @@ pthread_mutex_lock(&num_threads_lock); if (num_threads <= 0) { pt_log(kLog_event, "No running proxy thread - starting it.\n"); - if (pthread_create(&pid, 0, pt_proxy, 0) != 0) { +#ifndef WIN32 + if (pthread_create(&pid, 0, pt_proxy, 0) != 0) +#else + if (0 == (pid = _beginthreadex(0, 0, (unsigned int (__stdcall *)(void *))pt_proxy, 0, 0, 0))) +#endif + { pt_log(kLog_error, "Couldn't create thread! Dropping incoming connection.\n"); close(new_sock); pthread_mutex_unlock(&num_threads_lock); @@ -480,7 +549,7 @@ cur->send_ring[idx].pkt->checksum = 0; cur->send_ring[idx].pkt->checksum = htons(calc_icmp_checksum((uint16_t*)cur->send_ring[idx].pkt, cur->send_ring[idx].pkt_len)); //printf("ID: %d\n", htons(cur->send_ring[idx].pkt->identifier)); - sendto(icmp_sock, cur->send_ring[idx].pkt, cur->send_ring[idx].pkt_len, 0, (struct sockaddr*)&cur->dest_addr, sizeof(struct sockaddr)); + sendto(icmp_sock, (const void *) cur->send_ring[idx].pkt, cur->send_ring[idx].pkt_len, 0, (struct sockaddr*)&cur->dest_addr, sizeof(struct sockaddr)); } if (cur->last_ack+1.0 < now && cur->send_wait_ack < kPing_window_size && cur->remote_ack_val+1 != cur->next_remote_seq) { cur->last_ack = now; @@ -858,7 +927,7 @@ // Send it! pt_log(kLog_sendrecv, "Send: %d [%d] bytes [seq = %d] [type = %s] [ack = %d] [icmp = %d] [user = %s]\n", pkt_len, num_bytes, *seq, state_name[state & (~kFlag_mask)], ack_val, type, ((state & kUser_flag) == kUser_flag ? "yes" : "no")); - err = sendto(icmp_sock, pkt, pkt_len, 0, (struct sockaddr*)dest_addr, sizeof(struct sockaddr)); + err = sendto(icmp_sock, (const void *)pkt, pkt_len, 0, (struct sockaddr*)dest_addr, sizeof(struct sockaddr)); if (err < 0) { pt_log(kLog_error, "Failed to send ICMP packet: %s\n", strerror(errno)); return -1; diff -ru PingTunnel.orig\ptunnel.h PingTunnel\ptunnel.h --- PingTunnel.orig\ptunnel.h Thu May 26 17:06:24 2005 +++ PingTunnel\ptunnel.h Mon Jan 12 09:20:47 2009 @@ -44,21 +44,38 @@ #define PING_TUNNEL_H // Includes +#ifndef WIN32 #include #include #include #include #include #include +#endif /* !WIN32 */ #include #include #include #include +#ifndef WIN32 #include +#endif /* !WIN32 */ #include #include #include - + +#ifdef WIN32 + #include + typedef int socklen_t; + typedef uint32_t in_addr_t; + #define ETH_ALEN 6 /* Octets in one ethernet addr */ + struct ether_header + { + u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */ + u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */ + u_int16_t ether_type; /* packet type ID field */ + }; +#endif /* WIN32 */ + // Constants #define false 0 #define true 1