mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-06-12 00:23:08 -04:00
First pass for windows compilation:
* brought over tou_net / tou_error => bdnet for windoze compatibility. * updated udplayer to inline with rs changes + win compatibility. * removed unix only networking #includes, replaced with "util/bdnet.h" * added subdirectory to #includes. * made udpbitdht_nettest use a random node id. git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3302 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
d53a6c9d29
commit
d70337a642
30 changed files with 1209 additions and 635 deletions
346
libbitdht/src/util/bdnet.cc
Normal file
346
libbitdht/src/util/bdnet.cc
Normal file
|
@ -0,0 +1,346 @@
|
|||
|
||||
/*
|
||||
* util/bdnet.cc
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2010 by Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 3 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "bitdht@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include "bdnet.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
|
||||
/* error handling */
|
||||
int bdnet_int_errno;
|
||||
|
||||
int bdnet_errno()
|
||||
{
|
||||
return bdnet_int_errno;
|
||||
}
|
||||
|
||||
int bdnet_init()
|
||||
{
|
||||
std::cerr << "bdnet_init()" << std::endl;
|
||||
bdnet_int_errno = 0;
|
||||
|
||||
// Windows Networking Init.
|
||||
WORD wVerReq = MAKEWORD(2,2);
|
||||
WSADATA wsaData;
|
||||
|
||||
if (0 != WSAStartup(wVerReq, &wsaData))
|
||||
{
|
||||
std::cerr << "Failed to Startup Windows Networking";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Started Windows Networking";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int fd)
|
||||
{
|
||||
std::cerr << "bdnet_checkTTL()" << std::endl;
|
||||
int optlen = 4;
|
||||
char optval[optlen];
|
||||
|
||||
int ret = getsockopt(fd, IPPROTO_IP, IP_TTL, optval, &optlen);
|
||||
//int ret = getsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, optval, &optlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
std::cerr << "bdnet_checkTTL() Failed!";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "bdnet_checkTTL() :";
|
||||
std::cerr << (int) optval[0] << ":";
|
||||
std::cerr << (int) optval[1] << ":";
|
||||
std::cerr << (int) optval[2] << ":";
|
||||
std::cerr << (int) optval[3] << ": RET: ";
|
||||
std::cerr << ret << ":";
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_close(int fd)
|
||||
{
|
||||
std::cerr << "bdnet_close()" << std::endl;
|
||||
return closesocket(fd);
|
||||
}
|
||||
|
||||
int bdnet_socket(int domain, int type, int protocol)
|
||||
{
|
||||
int osock = socket(domain, type, protocol);
|
||||
std::cerr << "bdnet_socket()" << std::endl;
|
||||
|
||||
if ((unsigned) osock == INVALID_SOCKET)
|
||||
{
|
||||
// Invalidate socket Unix style.
|
||||
osock = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
bdnet_checkTTL(osock);
|
||||
return osock;
|
||||
}
|
||||
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
|
||||
{
|
||||
std::cerr << "bdnet_bind()" << std::endl;
|
||||
int ret = bind(sockfd,my_addr,addrlen);
|
||||
if (ret != 0)
|
||||
{
|
||||
/* store unix-style error
|
||||
*/
|
||||
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_fcntl(int fd, int cmd, long arg)
|
||||
{
|
||||
int ret;
|
||||
|
||||
unsigned long int on = 1;
|
||||
std::cerr << "bdnet_fcntl()" << std::endl;
|
||||
|
||||
/* can only do NONBLOCK at the moment */
|
||||
if ((cmd != F_SETFL) || (arg != O_NONBLOCK))
|
||||
{
|
||||
std::cerr << "bdnet_fcntl() limited to fcntl(fd, F_SETFL, O_NONBLOCK)";
|
||||
std::cerr << std::endl;
|
||||
bdnet_int_errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = ioctlsocket(fd, FIONBIO, &on);
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
/* store unix-style error
|
||||
*/
|
||||
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen)
|
||||
{
|
||||
std::cerr << "bdnet_setsockopt() val:" << *((int *) optval) << std::endl;
|
||||
std::cerr << "bdnet_setsockopt() len:" << optlen << std::endl;
|
||||
if ((level != IPPROTO_IP) || (optname != IP_TTL))
|
||||
{
|
||||
std::cerr << "bdnet_setsockopt() limited to ";
|
||||
std::cerr << "setsockopt(fd, IPPROTO_IP, IP_TTL, ....)";
|
||||
std::cerr << std::endl;
|
||||
bdnet_int_errno = EOPNOTSUPP;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = setsockopt(s, level, optname, (const char *) optval, optlen);
|
||||
//int ret = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) optval, optlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
bdnet_checkTTL(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen)
|
||||
{
|
||||
std::cerr << "bdnet_recvfrom()" << std::endl;
|
||||
int ret = recvfrom(s, (char *) buf, len, flags, from, fromlen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen)
|
||||
{
|
||||
std::cerr << "bdnet_sendto()" << std::endl;
|
||||
int ret = sendto(s, (const char *) buf, len, flags, to, tolen);
|
||||
if (ret == SOCKET_ERROR)
|
||||
{
|
||||
ret = -1;
|
||||
bdnet_int_errno = bdnet_w2u_errno(WSAGetLastError());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bdnet_w2u_errno(int err)
|
||||
{
|
||||
/* switch */
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ")" << std::endl;
|
||||
switch(err)
|
||||
{
|
||||
case WSAEINPROGRESS:
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
case WSAEWOULDBLOCK:
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
case WSAENETUNREACH:
|
||||
return ENETUNREACH;
|
||||
break;
|
||||
case WSAETIMEDOUT:
|
||||
return ETIMEDOUT;
|
||||
break;
|
||||
case WSAEHOSTDOWN:
|
||||
return EHOSTDOWN;
|
||||
break;
|
||||
case WSAECONNREFUSED:
|
||||
return ECONNREFUSED;
|
||||
break;
|
||||
case WSAEADDRINUSE:
|
||||
return EADDRINUSE;
|
||||
break;
|
||||
case WSAEUSERS:
|
||||
return EUSERS;
|
||||
break;
|
||||
/* This one is returned for UDP recvfrom, when nothing there
|
||||
* but not a real error... translate into EINPROGRESS
|
||||
*/
|
||||
case WSAECONNRESET:
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ")";
|
||||
std::cerr << " = WSAECONNRESET ---> EINPROGRESS";
|
||||
std::cerr << std::endl;
|
||||
return EINPROGRESS;
|
||||
break;
|
||||
/***
|
||||
*
|
||||
case WSAECONNRESET:
|
||||
return ECONNRESET;
|
||||
break;
|
||||
*
|
||||
***/
|
||||
|
||||
default:
|
||||
std::cerr << "tou_net_w2u_errno(" << err << ") Unknown";
|
||||
std::cerr << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
return ECONNREFUSED; /* sensible default? */
|
||||
}
|
||||
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr)
|
||||
{
|
||||
return (((*addr).s_addr = inet_addr(name)) != INADDR_NONE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void sleep(int sec)
|
||||
{
|
||||
Sleep(sec * 1000);
|
||||
}
|
||||
|
||||
void usleep(int usec)
|
||||
{
|
||||
Sleep(usec / 1000);
|
||||
}
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#else // UNIX
|
||||
|
||||
/* Unix Version is easy -> just call the unix fn
|
||||
*/
|
||||
|
||||
#include <unistd.h> /* for close definition */
|
||||
|
||||
/* the universal interface */
|
||||
int bdnet_init() { return 0; }
|
||||
int bdnet_errno() { return errno; }
|
||||
|
||||
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int fd) { return 1;}
|
||||
|
||||
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr)
|
||||
{
|
||||
return inet_aton(name, addr);
|
||||
}
|
||||
|
||||
int bdnet_close(int fd) { return close(fd); }
|
||||
|
||||
int bdnet_socket(int domain, int type, int protocol)
|
||||
{
|
||||
return socket(domain, type, protocol);
|
||||
}
|
||||
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen)
|
||||
{
|
||||
return bind(sockfd,my_addr,addrlen);
|
||||
}
|
||||
|
||||
int bdnet_fcntl(int fd, int cmd, long arg)
|
||||
{
|
||||
return fcntl(fd, cmd, arg);
|
||||
}
|
||||
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen)
|
||||
{
|
||||
return setsockopt(s, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen)
|
||||
{
|
||||
return recvfrom(s, buf, len, flags, from, fromlen);
|
||||
}
|
||||
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen)
|
||||
{
|
||||
return sendto(s, buf, len, flags, to, tolen);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
171
libbitdht/src/util/bdnet.h
Normal file
171
libbitdht/src/util/bdnet.h
Normal file
|
@ -0,0 +1,171 @@
|
|||
#ifndef BITDHT_UNIVERSAL_NETWORK_HEADER
|
||||
#define BITDHT_UNIVERSAL_NETWORK_HEADER
|
||||
/*
|
||||
*
|
||||
* util/bdnet.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2004-2010 by Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License Version 3 as published by the Free Software Foundation.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA.
|
||||
*
|
||||
* Please report all bugs and problems to "bitdht@lunamutt.com".
|
||||
*
|
||||
*/
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#include <stdio.h> /* for ssize_t */
|
||||
typedef uint32_t in_addr_t;
|
||||
|
||||
#else // UNIX
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#endif
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
|
||||
|
||||
/* C Interface */
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*******
|
||||
* This defines a (unix-like) universal networking layer
|
||||
* that should function on both windows and unix. (C - interface)
|
||||
*
|
||||
* This is of course only a subset of the full interface.
|
||||
* functions required are:
|
||||
*
|
||||
* int bdnet_close(int fd);
|
||||
* int bdnet_socket(int domain, int type, int protocol);
|
||||
* int bdnet_bind(int sockfd, const struct sockaddr *my_addr,
|
||||
* socklen_t addrlen);
|
||||
* int bdnet_fcntl(int fd, int cmd, long arg);
|
||||
* int bdnet_setsockopt(int s, int level, int optname,
|
||||
* const void *optval, socklen_t optlen);
|
||||
* ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
* struct sockaddr *from, socklen_t *fromlen);
|
||||
* ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
* const struct sockaddr *to, socklen_t tolen);
|
||||
*
|
||||
* There are some non-standard ones as well:
|
||||
* int bdnet_errno(); for internal networking errors
|
||||
* int bdnet_init(); required for windows
|
||||
* int bdnet_checkTTL(); a check if we can modify the ttl
|
||||
*/
|
||||
|
||||
|
||||
/* the universal interface */
|
||||
int bdnet_errno(); /* for internal networking errors */
|
||||
int bdnet_init(); /* required for windows */
|
||||
int bdnet_close(int fd);
|
||||
int bdnet_socket(int domain, int type, int protocol);
|
||||
int bdnet_bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);
|
||||
int bdnet_fcntl(int fd, int cmd, long arg);
|
||||
int bdnet_setsockopt(int s, int level, int optname,
|
||||
const void *optval, socklen_t optlen);
|
||||
ssize_t bdnet_recvfrom(int s, void *buf, size_t len, int flags,
|
||||
struct sockaddr *from, socklen_t *fromlen);
|
||||
ssize_t bdnet_sendto(int s, const void *buf, size_t len, int flags,
|
||||
const struct sockaddr *to, socklen_t tolen);
|
||||
|
||||
/* address filling */
|
||||
int bdnet_inet_aton(const char *name, struct in_addr *addr);
|
||||
/* check if we can modify the TTL on a UDP packet */
|
||||
int bdnet_checkTTL(int fd);
|
||||
|
||||
|
||||
|
||||
/* Extra stuff to declare for windows error handling (mimics unix errno)
|
||||
*/
|
||||
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
#if defined(_WIN32) || defined(__MINGW32__)
|
||||
|
||||
// Some Network functions that are missing from windows.
|
||||
//in_addr_t inet_netof(struct in_addr addr);
|
||||
//in_addr_t inet_network(char *inet_name);
|
||||
//int inet_aton(const char *name, struct in_addr *addr);
|
||||
|
||||
|
||||
// definitions for fcntl (NON_BLOCK) (random?)
|
||||
#define F_SETFL 0x1010
|
||||
#define O_NONBLOCK 0x0100
|
||||
|
||||
// definitions for setsockopt (TTL) (random?)
|
||||
//#define IPPROTO_IP 0x0011
|
||||
//#define IP_TTL 0x0110
|
||||
|
||||
/* define the Unix Error Codes that we use...
|
||||
* NB. we should make the same, but not necessary
|
||||
*/
|
||||
|
||||
#define EAGAIN 11
|
||||
#define EWOULDBLOCK EAGAIN
|
||||
|
||||
#define EUSERS 87
|
||||
#define ENOTSOCK 88
|
||||
|
||||
#define EOPNOTSUPP 95
|
||||
|
||||
#define EADDRINUSE 98
|
||||
#define EADDRNOTAVAIL 99
|
||||
#define ENETDOWN 100
|
||||
#define ENETUNREACH 101
|
||||
|
||||
#define ECONNRESET 104
|
||||
|
||||
#define ETIMEDOUT 110
|
||||
#define ECONNREFUSED 111
|
||||
#define EHOSTDOWN 112
|
||||
#define EHOSTUNREACH 113
|
||||
#define EALREADY 114
|
||||
#define EINPROGRESS 115
|
||||
|
||||
int bdnet_w2u_errno(int error);
|
||||
|
||||
/* also put the sleep commands in here (where else to go)
|
||||
* ms uses millisecs.
|
||||
* void Sleep(int ms);
|
||||
*/
|
||||
void sleep(int sec);
|
||||
//void usleep(int usec);
|
||||
|
||||
#endif // END of WINDOWS defines.
|
||||
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* C Interface */
|
||||
#endif
|
||||
|
||||
#endif /* BITDHT_UNIVERSAL_NETWORK_HEADER */
|
|
@ -1,9 +1,9 @@
|
|||
/*
|
||||
* bitdht/bdthreads.cc
|
||||
* util/bdthreads.cc
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2010 by Robert Fernie
|
||||
* Copyright 2004-2010 by Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
#define BITDHT_THREADS_H
|
||||
|
||||
/*
|
||||
* bitdht/bdthreads.h
|
||||
* util/bdthreads.h
|
||||
*
|
||||
* BitDHT: An Flexible DHT library.
|
||||
*
|
||||
* Copyright 2010 by Robert Fernie
|
||||
* Copyright 2004-2010 by Robert Fernie
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue