new compats
authorvictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Mon, 9 Nov 2009 15:35:39 +0000 (15:35 +0000)
committervictor <victor@e16421f0-f15b-0410-abcd-98678b794739>
Mon, 9 Nov 2009 15:35:39 +0000 (15:35 +0000)
git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@515 e16421f0-f15b-0410-abcd-98678b794739

compat.cpp [new file with mode: 0644]
compat.h [new file with mode: 0644]
datagram.h
exec/leecher.cpp

diff --git a/compat.cpp b/compat.cpp
new file mode 100644 (file)
index 0000000..84ef76d
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ *  compat.cpp
+ *  p2tp
+ *
+ *  Created by Arno Bakker, Victor Grishchenko
+ *  Copyright 2009 Delft University of Technology. All rights reserved.
+ *
+ */
+
+#include "compat.h"
+#include "util.h"
+#include <sys/stat.h>
+#include <stdio.h>
+#include <unistd.h>
+#ifdef _WIN32
+#include <windows.h>
+#include <Tchar.h>
+#include <io.h>
+#include <winsock2.h>
+#include <sys/timeb.h>
+#else
+#include <sys/time.h>
+#endif
+
+namespace p2tp {
+
+size_t file_size (int fd) {
+    struct stat st;
+    fstat(fd, &st);
+    return st.st_size;
+}
+
+int     file_seek (int fd, size_t offset) {
+#ifndef _WIN32
+    return lseek(fd,offset,SEEK_SET);
+#else
+       return _lseek(fd,offset,SEEK_SET);
+#endif
+}
+
+int     file_resize (int fd, size_t new_size) {
+#ifndef _WIN32
+    return ftruncate(fd, new_size);
+#else
+    return _chsize(fd,new_size);
+#endif
+}
+
+void print_error(const char* msg) {
+    perror(msg);
+#ifdef _WIN32
+    int e = WSAGetLastError();
+    if (e)
+        fprintf(stderr,"network error #%i\n",e);    
+#endif
+}
+
+void*   memory_map (int fd, size_t size) {
+    if (!size)
+        size = file_size(fd);
+    void *mapping;
+#ifndef _WIN32
+    mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+    if (mapping==MAP_FAILED)
+        return NULL;
+    return mapping;
+#else
+    HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
+    assert(fd<1024);
+    static HANDLE map_handles[1024];
+    maphandle = CreateFileMapping(     fhandle,
+                                       NULL,
+                                       PAGE_READWRITE,
+                                       0,
+                                       0,
+                                       NULL    );
+       if (maphandle == NULL)
+        return NULL;
+    map_handles[fd] = maphandle;
+    
+    mapping = MapViewOfFile         (  maphandle,
+                                       FILE_MAP_WRITE,
+                                       0,
+                                       0,
+                                       0  );
+        
+    return mapping;
+#endif
+}
+
+void    memory_unmap (int fd, void* mapping, size_t size) {
+#ifndef _WIN32
+    munmap(mapping,size);
+    close(fd);
+#else
+       UnmapViewOfFile(mapping);
+       CloseHandle(map_handles[fd]);
+#endif
+}
+    
+#ifdef _WIN32
+    
+size_t pread(int fildes, void *buf, size_t nbyte, long offset)
+{
+    _lseek(fildes,offset,SEEK_SET);
+    return read(fildes,buf,nbyte);
+}
+
+size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
+{
+    _lseek(fildes,offset,SEEK_SET);
+    return write(fildes,buf,nbyte);
+}
+
+
+int inet_aton(const char *cp, struct in_addr *inp)
+{
+    inp->S_un.S_addr = inet_addr(cp);
+    return 1;
+}
+
+#endif
+tint usec_time () {
+    struct timeval t;
+       gettimeofday(&t,NULL);
+       tint ret;
+       ret = t.tv_sec;
+       ret *= 1000000;
+       ret += t.tv_usec;
+       return ret;
+}    
+    
+}
diff --git a/compat.h b/compat.h
new file mode 100644 (file)
index 0000000..0593e44
--- /dev/null
+++ b/compat.h
@@ -0,0 +1,61 @@
+/*
+ *  compat.h
+ *  p2tp
+ *
+ *  Created by Arno Bakker, Victor Grishchenko
+ *  Copyright 2009 Delft University of Technology. All rights reserved.
+ *
+ */
+#ifndef P2TP_COMPAT_H
+#define P2TP_COMPAT_H
+#ifndef _WIN32
+#include <sys/mman.h>
+#endif
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef _WIN32
+#define open(a,b,c)    _open(a,b,c)
+#define S_IRUSR _S_IREAD
+#define S_IWUSR        _S_IWRITE
+#define S_IRGRP _S_IREAD
+#define S_IROTH _S_IREAD
+#endif
+
+typedef int64_t tint;
+#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);
+
+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_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
\ No newline at end of file
index c05cf38..b364518 100644 (file)
@@ -42,7 +42,7 @@ typedef int64_t tint;
 #define TINT_MSEC ((tint)1000)
 #define TINT_uSEC ((tint)1)
 #define TINT_NEVER ((tint)0x7fffffffffffffffLL)
-#define MAXDGRAMSZ 1400
+#define MAXDGRAMSZ 2800
 #ifndef _WIN32
 #define INVALID_SOCKET -1
 #endif
index e4f8f55..57d05a3 100644 (file)
@@ -19,7 +19,7 @@ int main (int argn, char** args) {
     FileTransfer::instance = rand();
     
     if (argn<4) {
-        fprintf(stderr,"parameters: root_hash filename tracker_ip/port [own_ip/port]\n");
+        fprintf(stderr,"parameters: root_hash filename tracker_ip:port [own_ip:port]\n");
         return -1;
     }
     Sha1Hash root_hash(true,args[1]);