From f3b3cec4f20cb9c4be5764eab761555f3f4fc39c Mon Sep 17 00:00:00 2001 From: arno Date: Wed, 11 Nov 2009 13:30:17 +0000 Subject: [PATCH] - Made to work on Win32 again. Passes all but ledbattest.exe git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@535 e16421f0-f15b-0410-abcd-98678b794739 --- bin.h | 2 +- bin64.h | 4 ++-- compat.cpp | 43 +++++++++++++++++++++++++++-------------- compat.h | 30 +++++++++++++++++++--------- compat/hirestimeofday.h | 7 ++++--- datagram.h | 32 ++++++++++++++---------------- exec/SConscript | 3 ++- exec/hasher.cpp | 6 +++--- exec/leecher.cpp | 31 +++++++++++++++-------------- exec/seeder.cpp | 22 +++++++++++---------- hashtree.cpp | 17 ++++++++-------- p2tp.cpp | 15 ++++++++------ p2tp.h | 32 +++++++++++++++--------------- sha1.cpp | 4 ++++ tests/connecttest.cpp | 23 ++++++++++++---------- tests/hashtest.cpp | 6 +++--- transfer.cpp | 2 +- 17 files changed, 159 insertions(+), 120 deletions(-) diff --git a/bin.h b/bin.h index c16d398..ed02494 100644 --- a/bin.h +++ b/bin.h @@ -9,7 +9,7 @@ #ifndef BIN_H #define BIN_H #include -#ifdef _WIN32 +#ifdef _MSC_VER // To avoid complaints about std::max. Appears to work in VS2008 #undef min #undef max diff --git a/bin64.h b/bin64.h index ecc0b9b..988de0b 100644 --- a/bin64.h +++ b/bin64.h @@ -1,7 +1,7 @@ #ifndef BIN64_H #define BIN64_H #include -#ifdef _WIN32 +#ifdef _MSC_VER #include "compat/stdint.h" #else #include @@ -106,7 +106,7 @@ struct bin64_t { else return right(); } - + bin64_t twisted (uint64_t mask) const { return bin64_t( v ^ ((mask<<1)&~tail_bits()) ); } diff --git a/compat.cpp b/compat.cpp index d6b0f48..208828d 100644 --- a/compat.cpp +++ b/compat.cpp @@ -10,19 +10,23 @@ #include "compat.h" #include #include -#include +#include #ifdef _WIN32 -#include #include #include -#include #include +#include "compat/hirestimeofday.h" #else +#include #include #endif namespace p2tp { +#ifdef _WIN32 +static HANDLE map_handles[1024]; +#endif + size_t file_size (int fd) { struct stat st; fstat(fd, &st); @@ -50,7 +54,7 @@ void print_error(const char* msg) { #ifdef _WIN32 int e = WSAGetLastError(); if (e) - fprintf(stderr,"network error #%i\n",e); + fprintf(stderr,"network error #%i\n",e); #endif } @@ -66,8 +70,7 @@ void* memory_map (int fd, size_t size) { #else HANDLE fhandle = (HANDLE)_get_osfhandle(fd); assert(fd<1024); - static HANDLE map_handles[1024]; - maphandle = CreateFileMapping( fhandle, + HANDLE maphandle = CreateFileMapping( fhandle, NULL, PAGE_READWRITE, 0, @@ -76,13 +79,13 @@ void* memory_map (int fd, size_t size) { if (maphandle == NULL) return NULL; map_handles[fd] = maphandle; - + mapping = MapViewOfFile ( maphandle, FILE_MAP_WRITE, 0, 0, 0 ); - + return mapping; #endif } @@ -96,9 +99,9 @@ void memory_unmap (int fd, void* mapping, size_t size) { CloseHandle(map_handles[fd]); #endif } - + #ifdef _WIN32 - + size_t pread(int fildes, void *buf, size_t nbyte, long offset) { _lseek(fildes,offset,SEEK_SET); @@ -118,9 +121,17 @@ int inet_aton(const char *cp, struct in_addr *inp) return 1; } -#endif - -tint usec_time () { +tint usec_time(void) +{ + HiResTimeOfDay* tod = HiResTimeOfDay::Instance(); + return tod->getTimeUSec(); +} + + +#else + +tint usec_time(void) +{ struct timeval t; gettimeofday(&t,NULL); tint ret; @@ -128,6 +139,8 @@ tint usec_time () { ret *= 1000000; ret += t.tv_usec; return ret; -} - +} + +#endif + } diff --git a/compat.h b/compat.h index 4f73489..19f9349 100644 --- a/compat.h +++ b/compat.h @@ -8,7 +8,16 @@ */ #ifndef P2TP_COMPAT_H #define P2TP_COMPAT_H -#ifndef _WIN32 + +#ifdef _MSC_VER +#include "compat/stdint.h" +#else +#include +#endif +#ifdef _WIN32 +#include +#include +#else #include #endif #include @@ -23,13 +32,16 @@ #define S_IROTH _S_IREAD #endif +namespace p2tp { + typedef int64_t tint; +#define TINT_HOUR ((tint)1000000*60*60) +#define TINT_MIN ((tint)1000000*60) #define TINT_SEC ((tint)1000000) #define TINT_MSEC ((tint)1000) #define TINT_uSEC ((tint)1) #define TINT_NEVER ((tint)0x7fffffffffffffffLL) -namespace p2tp { size_t file_size (int fd); @@ -37,25 +49,25 @@ int file_seek (int fd, size_t offset); int file_resize (int fd, size_t new_size); -void* memory_map (int fd, size_t size=0); +void* memory_map (int fd, size_t size=0); void memory_unmap (int fd, void*, size_t size); void print_error (const char* msg); - + #ifdef _WIN32 - + /** UNIX pread approximation. Does change file pointer. Is not thread-safe */ size_t pread(int fildes, void *buf, size_t nbyte, long offset); - + /** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */ size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset); - + int inet_aton(const char *cp, struct in_addr *inp); #endif - + tint usec_time (); - + }; #endif diff --git a/compat/hirestimeofday.h b/compat/hirestimeofday.h index 186a6a8..731da1e 100644 --- a/compat/hirestimeofday.h +++ b/compat/hirestimeofday.h @@ -8,13 +8,14 @@ #ifndef HIRESTIMEOFDAY_H #define HIRESTIMEOFDAY_H -#ifdef _WIN32 +#ifdef _MSC_VER #include "compat/stdint.h" -#include #else #include #endif - +#ifdef _WIN32 +#include +#endif namespace p2tp { diff --git a/datagram.h b/datagram.h index 95dbae0..a2f238a 100644 --- a/datagram.h +++ b/datagram.h @@ -9,13 +9,17 @@ #ifndef DATAGRAM_H #define DATAGRAM_H -#ifdef _WIN32 +#ifdef _MSC_VER #include "compat/stdint.h" - #include - #include "compat/unixio.h" #else - typedef int SOCKET; #include +#endif +#ifdef _WIN32 + #include + #include "compat.h" +#else + typedef int SOCKET; + #include #include #include @@ -29,25 +33,17 @@ #include #include #include "hashtree.h" -#include "compat/hirestimeofday.h" #include "compat/util.h" namespace p2tp { -typedef int64_t tint; -#define TINT_HOUR ((tint)1000000*60*60) -#define TINT_MIN ((tint)1000000*60) -#define TINT_SEC ((tint)1000000) -#define TINT_MSEC ((tint)1000) -#define TINT_uSEC ((tint)1) -#define TINT_NEVER ((tint)0x7fffffffffffffffLL) #define MAXDGRAMSZ 2800 #ifndef _WIN32 #define INVALID_SOCKET -1 #endif - + struct Address { struct sockaddr_in addr; static uint32_t LOCALHOST; @@ -69,7 +65,7 @@ struct Address { memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family = AF_INET; } - Address() { + Address() { clear(); } Address(const char* ip, uint16_t port) { @@ -92,7 +88,7 @@ struct Address { uint32_t ipv4 () const { return ntohl(addr.sin_addr.s_addr); } uint16_t port () const { return ntohs(addr.sin_port); } operator sockaddr_in () const {return addr;} - bool operator == (const Address& b) const { + bool operator == (const Address& b) const { return addr.sin_family==b.addr.sin_family && addr.sin_port==b.addr.sin_port && addr.sin_addr.s_addr==b.addr.sin_addr.s_addr; @@ -105,8 +101,8 @@ struct Address { } bool operator != (const Address& b) const { return !(*this==b); } }; - - + + struct Datagram { Address addr; @@ -210,7 +206,7 @@ std::string sock2str (struct sockaddr_in addr); #define dprintf(...) printf(__VA_ARGS__) #define eprintf(...) fprintf(stderr,__VA_ARGS__) //#define dprintf(...) {} - + } #endif diff --git a/exec/SConscript b/exec/SConscript index acf407f..5f07d26 100644 --- a/exec/SConscript +++ b/exec/SConscript @@ -2,10 +2,11 @@ import sys Import("DEBUG") Import("env") +Import("libs") Import("libpath") cpppath = env["CPPPATH"] -libs = ['p2tp'] +libs = ['p2tp'] + libs # order is important, crypto needs to be last if sys.platform == "win32": cpppath = ".." diff --git a/exec/hasher.cpp b/exec/hasher.cpp index 8f4857c..1e7127e 100644 --- a/exec/hasher.cpp +++ b/exec/hasher.cpp @@ -11,15 +11,15 @@ int main (int argn, char** args) { - + if (argn<2) { fprintf(stderr,"Usage: %s file_name\n",args[0]); return 1; } p2tp::HashTree* ht = new p2tp::HashTree(args[1]); - + printf("SHA1 Merkle tree root hash: %s\n",ht->root_hash().hex().c_str()); - + } diff --git a/exec/leecher.cpp b/exec/leecher.cpp index c18945c..dbbe1a4 100644 --- a/exec/leecher.cpp +++ b/exec/leecher.cpp @@ -7,6 +7,7 @@ * */ #include "p2tp.h" +#include using namespace p2tp; @@ -14,10 +15,10 @@ using namespace p2tp; /** P2TP downloader. Params: root hash, filename, tracker ip/port, own ip/port */ int main (int argn, char** args) { - + srand(time(NULL)); FileTransfer::instance = rand(); - + if (argn<4) { fprintf(stderr,"parameters: root_hash filename tracker_ip:port [own_ip:port]\n"); return -1; @@ -27,27 +28,29 @@ int main (int argn, char** args) { fprintf(stderr,"Sha1 hash format: hex, 40 symbols\n"); return -2; } - + + p2tp::LibraryInit(); + char* filename = args[2]; - + Address tracker(args[3]), bindaddr; - + if (tracker==Address()) { fprintf(stderr,"Tracker address format: [1.2.3.4:]12345\n"); return -2; } - if (argn>=5) + if (argn>=5) bindaddr = Address(args[4]); else - bindaddr = Address(INADDR_ANY,rand()%10000+7000); - + bindaddr = Address((uint32_t)INADDR_ANY,rand()%10000+7000); + assert(0=4) { Address tracker(args[3]); p2tp::SetTracker(tracker); } Address bindaddr(args[2]); - + if (bindaddr==Address()) { fprintf(stderr,"Bind address format: [1.2.3.4:]12345\n"); return -2; } - + assert(0 #include +#include #include #include "compat.h" @@ -131,7 +132,7 @@ void HashTree::Submit () { } root_hash_ = DeriveRoot(); - + } @@ -191,13 +192,13 @@ bool HashTree::OfferPeakHash (bin64_t pos, const Sha1Hash& hash) { return false; for(int i=0; i>10) + ((size_&1023) ? 1 : 0); - + size_t cur_size = file_size(fd_); if ( cur_size<=(sizek_-1)<<10 || cur_size>sizek_<<10 ) if (file_resize(fd_, size_)) { @@ -205,19 +206,19 @@ bool HashTree::OfferPeakHash (bin64_t pos, const Sha1Hash& hash) { size_=0; // remain in the 0-state return false; } - + // mmap the hash file into memory size_t expected_size = sizeof(Sha1Hash)*sizek_*2; if ( file_size(hash_fd_) != expected_size ) file_resize (hash_fd_, expected_size); - + hashes_ = (Sha1Hash*) memory_map(hash_fd_,expected_size); if (!hashes_) { size_ = sizek_ = complete_ = completek_ = 0; print_error("mmap failed"); return false; } - + for(int i=0; i #include +#ifndef _WIN32 #include -#include #include #include #include -#include #include +#endif +#include +#include + //#include #include "p2tp.h" #include "datagram.h" @@ -27,7 +30,7 @@ p2tp::tint Channel::last_tick = 0; int Channel::MAX_REORDERING = 4; p2tp::tint Channel::TIMEOUT = TINT_SEC*60; std::vector Channel::channels(1); -int Channel::sockets[8] = {0,0,0,0,0,0,0,0}; +SOCKET Channel::sockets[8] = {0,0,0,0,0,0,0,0}; int Channel::socket_count = 0; Address Channel::tracker; tbqueue Channel::send_queue; @@ -95,15 +98,15 @@ int p2tp::Open (const char* filename, const Sha1Hash& hash) { FileTransfer* ft = new FileTransfer(filename, hash); int fdes = ft->file().file_descriptor(); if (fdes>0) { - + /*if (FileTransfer::files.size() @@ -89,7 +89,7 @@ namespace p2tp { P2TP_PEX_RM = 6, P2TP_MESSAGE_COUNT = 7 } messageid_t; - + class PiecePicker; class CongestionController; class PeerSelector; @@ -112,7 +112,7 @@ namespace p2tp { //bin64_t RevealAck (uint64_t& offset); /** Rotating queue read for channels of this transmission. */ int RevealChannel (int& i); - + static FileTransfer* Find (const Sha1Hash& hash); static FileTransfer* file (int fd) { return fd files; HashTree file_; - + /** Piece picker strategy. */ PiecePicker* picker_; - + /** Channels working for this transfer. */ binqueue hs_in_; int hs_in_offset_; std::deque
pex_in_; - + /** Messages we are accepting. */ uint64_t cap_out_; @@ -157,9 +157,9 @@ namespace p2tp { friend void Close (int fd) ; }; - + #include "ext/send_control.h" - + class PiecePicker { public: @@ -169,14 +169,14 @@ namespace p2tp { virtual void Received (bin64_t b) = 0; }; - + class PeerSelector { public: virtual void AddPeer (const Address& addr, const Sha1Hash& root) = 0; virtual Address GetPeer (const Sha1Hash& for_root) = 0; }; - + class DataStorer { public: DataStorer (const Sha1Hash& id, size_t size); @@ -224,7 +224,7 @@ namespace p2tp { FileTransfer& transfer() { return *transfer_; } HashTree& file () { return transfer_->file(); } const Address& peer() const { return peer_; } - + static int DecodeID(int scrambled); static int EncodeID(int unscrambled); static Channel* channel(int i) { @@ -271,7 +271,7 @@ namespace p2tp { tint next_send_time_; tint peer_send_time_; static tbqueue send_queue; - + void RequeueSend (tint next_time); int PeerBPS() const { return TINT_SEC / dip_avg_ * 1024; @@ -285,7 +285,7 @@ namespace p2tp { static int MAX_REORDERING; static tint TIMEOUT; - static int sockets[8]; + static SOCKET sockets[8]; static int socket_count; static tint last_tick; @@ -297,7 +297,7 @@ namespace p2tp { friend void AddPeer (Address address, const Sha1Hash& root); friend void SetTracker(const Address& tracker); friend int Open (const char*, const Sha1Hash&) ; // FIXME - + friend class FileTransfer; // FIXME!!! friend class SendController; // FIXME!!! }; @@ -323,9 +323,9 @@ namespace p2tp { root hash is zero, the peer might be talked to regarding any transmission (likely, a tracker, cache or an archive). */ void AddPeer (Address address, const Sha1Hash& root=Sha1Hash::ZERO); - + void SetTracker(const Address& tracker); - + /** Returns size of the file in bytes, 0 if unknown. Might be rounded up to a kilobyte before the transmission is complete. */ uint64_t Size (int fdes); diff --git a/sha1.cpp b/sha1.cpp index 0b7e38a..67fe90b 100644 --- a/sha1.cpp +++ b/sha1.cpp @@ -8,7 +8,11 @@ /* this is only to get definitions for memcpy(), ntohl() and htonl() */ //#include "../git-compat-util.h" +#ifdef _WIN32 +#include +#else #include +#endif #include #include "sha1.h" diff --git a/tests/connecttest.cpp b/tests/connecttest.cpp index 37d0587..113a2e2 100644 --- a/tests/connecttest.cpp +++ b/tests/connecttest.cpp @@ -10,6 +10,8 @@ #include //#include #include "p2tp.h" +#include + using namespace p2tp; @@ -17,30 +19,30 @@ using namespace p2tp; TEST(P2TP,CwndTest) { srand ( time(NULL) ); - + unlink("doc/sofi-copy.jpg"); struct stat st; ASSERT_EQ(0,stat("doc/sofi.jpg", &st)); int size = st.st_size;//, sizek = (st.st_size>>10) + (st.st_size%1024?1:0) ; - + int sock1 = p2tp::Listen(7001); ASSERT_TRUE(sock1>=0); - + int file = p2tp::Open("doc/sofi.jpg"); FileTransfer* fileobj = FileTransfer::file(file); FileTransfer::instance++; - + p2tp::SetTracker(Address("127.0.0.1",7001)); - + int copy = p2tp::Open("doc/sofi-copy.jpg",fileobj->root_hash()); - + p2tp::Loop(TINT_SEC); - + int count = 0; while (p2tp::SeqComplete(copy)!=size && count++<600) p2tp::Loop(TINT_SEC); ASSERT_EQ(size,p2tp::SeqComplete(copy)); - + p2tp::Close(file); p2tp::Close(copy); @@ -50,9 +52,10 @@ TEST(P2TP,CwndTest) { int main (int argc, char** argv) { - + + p2tp::LibraryInit(); testing::InitGoogleTest(&argc, argv); int ret = RUN_ALL_TESTS(); return ret; - + } diff --git a/tests/hashtest.cpp b/tests/hashtest.cpp index 3b24fc9..d988fb1 100644 --- a/tests/hashtest.cpp +++ b/tests/hashtest.cpp @@ -38,7 +38,7 @@ TEST(Sha1HashTest,OfferDataTest) { TEST(Sha1HashTest,SubmitTest) { - FILE* f123 = fopen("123","w+"); + FILE* f123 = fopen("123","wb+"); fprintf(f123, "123\n"); fclose(f123); HashTree ht123("123"); @@ -83,8 +83,8 @@ TEST(Sha1HashTest,SubmitTest) { int main (int argc, char** argv) { //bin::init(); - + testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); - + } diff --git a/transfer.cpp b/transfer.cpp index b1effc0..37c1a79 100644 --- a/transfer.cpp +++ b/transfer.cpp @@ -7,7 +7,7 @@ * */ #ifdef _WIN32 -#include "compat/unixio.h" +#include "compat.h" #else #include #endif -- 2.20.1