(trunk third-party/dht) #3311 "mingw build of Transmission" -- use jch's upstream win32 portability fixes

This commit is contained in:
Charles Kerr 2010-06-30 23:30:21 +00:00
parent 954a309d8f
commit ba3d117b81
2 changed files with 49 additions and 26 deletions

View File

@ -1,3 +1,7 @@
1 July 2010: dht-0.15
* Port to Windows, for the needs of Transmission.
25 March 2010: dht-0.14
* Fixed ordering of entries in parameter dictionaries.

71
third-party/dht/dht.c vendored
View File

@ -39,6 +39,7 @@ THE SOFTWARE.
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#ifndef WIN32
#include <arpa/inet.h>
#include <sys/types.h>
@ -46,11 +47,8 @@ THE SOFTWARE.
#include <netinet/in.h>
#else
#include <w32api.h>
#define WINVER WindowsXP /* freeaddrinfo(),getaddrinfo(),getnameinfo() */
#define WINVER WindowsXP
#include <ws2tcpip.h>
#define random rand /* int rand() since no long random() */
extern const char *inet_ntop(int, const void *, char *, socklen_t); /* from libtransmission (utils.c) */
#define EAFNOSUPPORT WSAEAFNOSUPPORT
#endif
#include "dht.h"
@ -65,6 +63,47 @@ extern const char *inet_ntop(int, const void *, char *, socklen_t); /* from libt
#define MSG_CONFIRM 0
#endif
#ifdef WIN32
#define EAFNOSUPPORT WSAEAFNOSUPPORT
static int
set_nonblocking(int fd, int nonblocking)
{
int rc;
unsigned long mode = !!nonblocking;
rc = ioctlsocket(fd, FIONBIO, &mode);
if(rc != 0)
errno = WSAGetLastError();
return (rc == 0 ? 0 : -1);
}
static int
random(void)
{
return rand();
}
extern const char *inet_ntop(int, const void *, char *, socklen_t);
#else
static int
set_nonblocking(int fd, int nonblocking)
{
int rc;
rc = fcntl(fd, F_GETFL, 0);
if(rc < 0)
return -1;
rc = fcntl(fd, F_SETFL, nonblocking?(rc | O_NONBLOCK):(rc & ~O_NONBLOCK));
if(rc < 0)
return -1;
return 0;
}
#endif
/* We set sin_family to 0 to mark unused slots. */
#if AF_INET == 0 || AF_INET6 == 0
#error You lose
@ -1539,10 +1578,6 @@ int
dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)
{
int rc;
#ifdef WIN32
unsigned long flags = 1;
#endif
if(dht_socket >= 0 || dht_socket6 >= 0 || buckets || buckets6) {
errno = EBUSY;
@ -1561,15 +1596,7 @@ dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)
return -1;
buckets->af = AF_INET;
#ifndef WIN32
rc = fcntl(s, F_GETFL, 0);
if(rc < 0)
goto fail;
rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
#else
rc = ioctlsocket(s, FIONBIO, &flags);
#endif
rc = set_nonblocking(s, 1);
if(rc < 0)
goto fail;
}
@ -1580,15 +1607,7 @@ dht_init(int s, int s6, const unsigned char *id, const unsigned char *v)
return -1;
buckets6->af = AF_INET6;
#ifndef WIN32
rc = fcntl(s6, F_GETFL, 0);
if(rc < 0)
goto fail;
rc = fcntl(s6, F_SETFL, (rc | O_NONBLOCK));
#else
rc = ioctlsocket(s6, FIONBIO, &flags);
#endif
rc = set_nonblocking(s6, 1);
if(rc < 0)
goto fail;
}