From: victor Date: Fri, 6 Nov 2009 12:43:14 +0000 (+0000) Subject: it even compiles X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=9dac76af08bfd44bcece10d73bd5d17645e328d7;p=swift-upb.git it even compiles git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@502 e16421f0-f15b-0410-abcd-98678b794739 --- diff --git a/compat/unixio.cpp b/compat/unixio.cpp index 01b08ea..12f3e36 100644 --- a/compat/unixio.cpp +++ b/compat/unixio.cpp @@ -28,4 +28,6 @@ int inet_aton(const char *cp, struct in_addr *inp) return 1; } + + #endif diff --git a/compat/unixio.h b/compat/unixio.h index 94255c1..46771c8 100644 --- a/compat/unixio.h +++ b/compat/unixio.h @@ -23,6 +23,7 @@ size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset); int inet_aton(const char *cp, struct in_addr *inp); + #endif /* UNIXIO_H_ */ #endif // WIN32 \ No newline at end of file diff --git a/compat/util.cpp b/compat/util.cpp index 986d9f6..c772a20 100644 --- a/compat/util.cpp +++ b/compat/util.cpp @@ -33,5 +33,15 @@ std::string gettmpdir(void) } + void print_error(const char* msg) { + perror(msg); +#ifdef _WIN32 + int e = WSAGetLastError(); + if (e) + fprintf(stderr,"network error #%i\n",e); +#endif + } + + }; // namespace diff --git a/compat/util.h b/compat/util.h index b4e542f..aa99df6 100644 --- a/compat/util.h +++ b/compat/util.h @@ -12,7 +12,6 @@ namespace p2tp { - std::string gettmpdir(void); /** * Return path of temporary directory. * @@ -20,8 +19,10 @@ namespace p2tp * * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen) */ + std::string gettmpdir(void); - + void print_error(const char* msg); + }; #endif /* UTIL_H_ */ diff --git a/datagram.cpp b/datagram.cpp index f6325eb..068c0c2 100644 --- a/datagram.cpp +++ b/datagram.cpp @@ -14,7 +14,6 @@ #else #include #endif -#include #include "datagram.h" namespace p2tp { @@ -65,12 +64,10 @@ int Datagram::Recv () { offset = 0; length = recvfrom (sock, (char *)buf, MAXDGRAMSZ, 0, (struct sockaddr*)&(addr), &addrlen); - if (length<0) // FIXME FIXME FIXME -#ifdef _WIN32 - PLOG(ERROR)<<"on recv" << WSAGetLastError() << "\n"; -#else - PLOG(ERROR)<<"on recv"; -#endif + if (length<0) { + length = 0; + print_error("error on recv"); + } dgrams_down++; bytes_down+=length; Time(); @@ -99,13 +96,9 @@ SOCKET Datagram::Wait (int sockcnt, SOCKET* sockets, tint usec) { if (FD_ISSET(sockets[i],&bases)) return sockets[i]; } else if (sel<0) { -#ifdef _WIN32 - PLOG(ERROR)<<"select fails" << WSAGetLastError() << "\n"; -#else - PLOG(ERROR)<<"select fails"; -#endif + print_error("select fails"); } - return -1; + return INVALID_SOCKET; } tint Datagram::Time () { @@ -120,40 +113,36 @@ SOCKET Datagram::Bind (Address addr_) { SOCKET fd; int len = sizeof(struct sockaddr_in), sndbuf=1<<20, rcvbuf=1<<20; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - PLOG(ERROR)<<"socket fails"; - return -1; + print_error("socket() fails"); + return INVALID_SOCKET; } #ifdef _WIN32 u_long enable = 1; ioctlsocket(fd, FIONBIO, &enable); if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char *)&sndbuf, sizeof(int)) != 0 ) { - PLOG(ERROR)<<"setsockopt fails"; - return -3; + print_error("setsockopt fails"); + return INVALID_SOCKET; } if ( setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char *)&rcvbuf, sizeof(int)) != 0 ) { - PLOG(ERROR)<<"setsockopt2 fails"; - return -3; + print_error("setsockopt2 fails"); + return INVALID_SOCKET; } #else if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) - return -2; + return INVALID_SOCKET; if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(int)) < 0 ) { - PLOG(ERROR)<<"setsockopt fails"; - return -3; + print_error("setsockopt fails"); + return INVALID_SOCKET; } if ( setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(int)) < 0 ) { - PLOG(ERROR)<<"setsockopt2 fails"; - return -3; + print_error("setsockopt2 fails"); + return INVALID_SOCKET; } #endif - printf("BUFS: %i %i\n",sndbuf,rcvbuf); - /*memset(&addr, 0, sizeof(struct sockaddr_in)); - addr.sin_family = AF_INET; - addr.sin_port = htons(portno); - addr.sin_addr.s_addr = INADDR_ANY;*/ + dprintf("socket buffers: %i send %i recv\n",sndbuf,rcvbuf); if (::bind(fd, (sockaddr*)&addr, len) != 0) { - PLOG(ERROR)<<"bind fails"; - return -4; + print_error("bind fails"); + return INVALID_SOCKET; } return fd; } @@ -164,7 +153,7 @@ void Datagram::Close (int sock) { // remove from fd_set #else if (::close(sock)!=0) #endif - PLOG(ERROR)<<"on closing a socket"; + print_error("on closing a socket"); } diff --git a/datagram.h b/datagram.h index 35da147..1f6ca2c 100644 --- a/datagram.h +++ b/datagram.h @@ -30,6 +30,7 @@ #include #include "hashtree.h" #include "compat/hirestimeofday.h" +#include "compat/util.h" namespace p2tp { @@ -61,6 +62,18 @@ struct Address { init(LOCALHOST,port); inet_aton(ip,&(addr.sin_addr)); } + Address(const char* ip_port) { + char ipp[32]; + strcpy(ipp,ip_port); + char* semi = strchr(ipp,':'); + if (semi) { + *semi = 0; + int port; + sscanf(semi+1, "%i", &port); + init(LOCALHOST,port); + } + inet_aton(ipp,&(addr.sin_addr)); + } Address(uint16_t port) { init(LOCALHOST,port); } diff --git a/exec/seeder.cpp b/exec/seeder.cpp index 3d957ed..59ef6ab 100644 --- a/exec/seeder.cpp +++ b/exec/seeder.cpp @@ -12,22 +12,39 @@ using namespace p2tp; +/** P2TP seeder. Params: filename, own ip/port, tracker ip/port */ int main (int argn, char** args) { - assert(0 #include #include -#include +//#include #include "p2tp.h" #include "datagram.h" diff --git a/sendrecv.cpp b/sendrecv.cpp index ac65e2d..14e427c 100644 --- a/sendrecv.cpp +++ b/sendrecv.cpp @@ -7,8 +7,9 @@ * */ #include -#include +//#include #include "p2tp.h" +#include "compat/util.h" using namespace p2tp; @@ -124,7 +125,8 @@ void Channel::Send () { AddAck(dgram); } dprintf("%s #%i sent %ib %s\n",Datagram::TimeStr(),id,dgram.size(),peer().str().c_str()); - PCHECK( dgram.Send() != -1 )<<"error sending"; + if (dgram.Send()==-1) + print_error("can't send datagram"); if (dgram.size()==4) // only the channel id; bare keep-alive data = bin64_t::ALL; cc_->OnDataSent(data); @@ -202,8 +204,8 @@ bin64_t Channel::AddData (Datagram& dgram) { size_t r = pread(file().file_descriptor(),buf,1024,tosend.base_offset()<<10); // TODO: ??? corrupted data, retries if (r<0) { - PLOG(ERROR)<<"error on reading"; - return 0; + print_error("error on reading"); + return bin64_t::NONE; } assert(dgram.space()>=r+4+1); dgram.Push8(P2TP_DATA); @@ -456,7 +458,7 @@ void Channel::Loop (tint howlong) { } if (send_time>limit) send_time = limit; - if (sender && send_time<=NOW) { + if ( sender && send_time <= NOW ) { dprintf("%s #%i sch_send %s\n",Datagram::TimeStr(),sender->id, Datagram::TimeStr(send_time)); sender->Send(); @@ -466,7 +468,7 @@ void Channel::Loop (tint howlong) { tint towait = send_time - NOW; dprintf("%s waiting %lliusec\n",Datagram::TimeStr(),towait); int rd = Datagram::Wait(socket_count,sockets,towait); - if (rd!=-1) + if (rd!=INVALID_SOCKET) Recv(rd); } @@ -474,50 +476,3 @@ void Channel::Loop (tint howlong) { } - - -/* - - tint untiltime = Datagram::Time()+time; - if (send_queue.empty()) - dprintf("%s empty send_queue\n", Datagram::TimeStr()); - - while ( NOW <= untiltime && !send_queue.empty() ) { - - // BUG BUG BUG no scheduled sends => just listen - - tintbin next_send = send_queue.front(); - tint wake_on = next_send.time; - Channel* sender = channel(next_send.bin); - - // BUG BUG BUG filter stale timeouts here - - //if (wake_on<=untiltime) { - pop_heap(send_queue.begin(), send_queue.end(), tblater); - send_queue.pop_back(); - //}// else - //sender = 0; // BUG will never wake up - - if (sender->next_send_time_!=next_send.time) - continue; - - if (wake_onuntiltime) - wake_on = untiltime; - tint towait = min(wake_on-NOW,TINT_SEC); - dprintf("%s waiting %lliusec\n",Datagram::TimeStr(),towait); - int rd = Datagram::Wait(socket_count,sockets,towait); - if (rd!=-1) - Recv(rd); - // BUG WRONG BUG WRONG another may need to send - if (sender) { - dprintf("%s #%i sch_send %s\n",Datagram::TimeStr(),sender->id, - Datagram::TimeStr(next_send.time)); - sender->Send(); - // if (sender->cc_->next_send_time==TINT_NEVER) - } - - } - - */ diff --git a/tests/connecttest.cpp b/tests/connecttest.cpp index f4b18b9..3d86fbf 100644 --- a/tests/connecttest.cpp +++ b/tests/connecttest.cpp @@ -8,7 +8,7 @@ */ #include -#include +//#include #include "p2tp.h" using namespace p2tp; diff --git a/tests/dgramtest.cpp b/tests/dgramtest.cpp index f12f5b1..5d580cb 100644 --- a/tests/dgramtest.cpp +++ b/tests/dgramtest.cpp @@ -7,7 +7,7 @@ * */ #include -#include +//#include #include "datagram.h" #include "p2tp.h" // Arno: for LibraryInit diff --git a/tests/ledbattest.cpp b/tests/ledbattest.cpp index 21818ea..8abd9da 100644 --- a/tests/ledbattest.cpp +++ b/tests/ledbattest.cpp @@ -5,7 +5,7 @@ #include "datagram.h" #include "p2tp.h" #include -#include +//#include using namespace p2tp; using namespace std; diff --git a/tests/transfertest.cpp b/tests/transfertest.cpp index 095d38b..86ea728 100644 --- a/tests/transfertest.cpp +++ b/tests/transfertest.cpp @@ -6,8 +6,8 @@ * Copyright 2009 Delft University of Technology. All rights reserved. * */ -#include -#include +//#include +//#include #include "p2tp.h" #include "compat/util.h" #ifdef _MSC_VER