From c9f3b3a72cf5974f1924ed09eba77d2286e675ae Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 9 Nov 2009 15:35:39 +0000 Subject: [PATCH] new compats git-svn-id: https://ttuki.vtt.fi/svn/p2p-next/TUD/p2tp/trunk@515 e16421f0-f15b-0410-abcd-98678b794739 --- compat.cpp | 134 +++++++++++++++++++++++++++++++++++++++++++++++ compat.h | 61 +++++++++++++++++++++ datagram.h | 2 +- exec/leecher.cpp | 2 +- 4 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 compat.cpp create mode 100644 compat.h diff --git a/compat.cpp b/compat.cpp new file mode 100644 index 0000000..84ef76d --- /dev/null +++ b/compat.cpp @@ -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 +#include +#include +#ifdef _WIN32 +#include +#include +#include +#include +#include +#else +#include +#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 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 +#endif +#include +#include +#include + +#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 diff --git a/datagram.h b/datagram.h index c05cf38..b364518 100644 --- a/datagram.h +++ b/datagram.h @@ -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 diff --git a/exec/leecher.cpp b/exec/leecher.cpp index e4f8f55..57d05a3 100644 --- a/exec/leecher.cpp +++ b/exec/leecher.cpp @@ -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]); -- 2.20.1