it even compiles
authorvictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Fri, 6 Nov 2009 12:43:14 +0000 (12:43 +0000)
committervictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Fri, 6 Nov 2009 12:43:14 +0000 (12:43 +0000)
git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@502 e16421f0-f15b-0410-abcd-98678b794739

13 files changed:
compat/unixio.cpp
compat/unixio.h
compat/util.cpp
compat/util.h
datagram.cpp
datagram.h
exec/seeder.cpp
p2tp.cpp
sendrecv.cpp
tests/connecttest.cpp
tests/dgramtest.cpp
tests/ledbattest.cpp
tests/transfertest.cpp

index 01b08ea..12f3e36 100644 (file)
@@ -28,4 +28,6 @@ int inet_aton(const char *cp, struct in_addr *inp)
        return 1;\r
 }\r
 \r
+\r
+\r
 #endif\r
index 94255c1..46771c8 100644 (file)
@@ -23,6 +23,7 @@ size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset);
 \r
 int inet_aton(const char *cp, struct in_addr *inp);\r
 \r
+\r
 #endif /* UNIXIO_H_ */\r
 \r
 #endif // WIN32
\ No newline at end of file
index 986d9f6..c772a20 100644 (file)
@@ -33,5 +33,15 @@ std::string gettmpdir(void)
 }\r
 \r
 \r
+    void print_error(const char* msg) {\r
+        perror(msg);\r
+#ifdef _WIN32\r
+        int e = WSAGetLastError();\r
+        if (e)\r
+            fprintf(stderr,"network error #%i\n",e);\r
+#endif\r
+    }\r
+    \r
+    \r
 }; // namespace\r
 \r
index b4e542f..aa99df6 100644 (file)
@@ -12,7 +12,6 @@
 \r
 namespace p2tp\r
 {\r
-       std::string gettmpdir(void);\r
        /**\r
         * Return path of temporary directory.\r
         *\r
@@ -20,8 +19,10 @@ namespace p2tp
         *\r
         * TODO: Unicode... (gets hairy with open() call on Linux. Win32 has _wopen)\r
         */\r
+       std::string gettmpdir(void);\r
 \r
-\r
+    void print_error(const char* msg);\r
+    \r
 };\r
 \r
 #endif /* UTIL_H_ */\r
index f6325eb..068c0c2 100644 (file)
@@ -14,7 +14,6 @@
 #else
     #include <arpa/inet.h>
 #endif
-#include <glog/logging.h>
 #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");
 }
 
 
index 35da147..1f6ca2c 100644 (file)
@@ -30,6 +30,7 @@
 #include <string>
 #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);
     }
index 3d957ed..59ef6ab 100644 (file)
 using namespace p2tp;
 
 
+/** P2TP seeder. Params: filename, own ip/port, tracker ip/port */
 int main (int argn, char** args) {
     
-    assert(0<p2tp::Listen(7001));
-       
-       int file = p2tp::Open("doc/sofi.jpg");
+    if (argn<3) {
+        fprintf(stderr,"parameters: filename own_ip/port [tracker_ip/port]\n");
+        return -1;
+    }
+    
+    char* filename = args[1];
+    
+    Address tracker(args[3]), bindaddr(args[2]);
+    
+    if (bindaddr==Address()) {
+        fprintf(stderr,"Bind address format: [1.2.3.4:]12345\n");
+        return -2;
+    }
+    
+    assert(0<p2tp::Listen(bindaddr));
+
+    if (tracker!=Address())
+        p2tp::SetTracker(tracker);
+    
+       int file = p2tp::Open(filename);
     
     while (true) {
            p2tp::Loop(TINT_SEC);
         printf("%lli dgram %lli bytes up, %lli dgram %lli bytes down\n",
-            Datagram::dgrams_up, Datagram::bytes_up,
-            Datagram::dgrams_down, Datagram::bytes_down );
+               Datagram::dgrams_up, Datagram::bytes_up,
+               Datagram::dgrams_down, Datagram::bytes_down );
     }
     
        p2tp::Close(file);
-
+    
        p2tp::Shutdown();
-
+    
 }
-
index 485b2a2..949cfac 100644 (file)
--- a/p2tp.cpp
+++ b/p2tp.cpp
@@ -16,7 +16,7 @@
 #include <arpa/inet.h>
 #include <string.h>
 #include <unistd.h>
-#include <glog/logging.h>
+//#include <glog/logging.h>
 #include "p2tp.h"
 #include "datagram.h"
 
index ac65e2d..14e427c 100644 (file)
@@ -7,8 +7,9 @@
  *
  */
 #include <algorithm>
-#include <glog/logging.h>
+//#include <glog/logging.h>
 #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_on<NOW)
- wake_on = NOW;
- if (wake_on>untiltime)
- 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) 
- }
- }
- */
index f4b18b9..3d86fbf 100644 (file)
@@ -8,7 +8,7 @@
  */
 
 #include <gtest/gtest.h>
-#include <glog/logging.h>
+//#include <glog/logging.h>
 #include "p2tp.h"
 
 using namespace p2tp;
index f12f5b1..5d580cb 100644 (file)
@@ -7,7 +7,7 @@
  *
  */
 #include <gtest/gtest.h>
-#include <glog/logging.h>
+//#include <glog/logging.h>
 #include "datagram.h"
 #include "p2tp.h" // Arno: for LibraryInit
 
index 21818ea..8abd9da 100644 (file)
@@ -5,7 +5,7 @@
 #include "datagram.h"
 #include "p2tp.h"
 #include <gtest/gtest.h>
-#include <glog/logging.h>
+//#include <glog/logging.h>
 
 using namespace p2tp;
 using namespace std;
index 095d38b..86ea728 100644 (file)
@@ -6,8 +6,8 @@
  *  Copyright 2009 Delft University of Technology. All rights reserved.
  *
  */
-#include <gtest/gtest.h>
-#include <glog/logging.h>
+//#include <gtest/gtest.h>
+//#include <glog/logging.h>
 #include "p2tp.h"
 #include "compat/util.h"
 #ifdef _MSC_VER