Reminder of changes required by the Windows port. This solves the issues 228, 229, 230 and 232.

This commit is contained in:
Christian Huitema 2016-12-05 11:38:59 -08:00
parent 598d7d103e
commit dee33f53b6
4 changed files with 129 additions and 12 deletions

View File

@ -38,6 +38,9 @@
#ifndef GETDNS_ON_WINDOWS #ifndef GETDNS_ON_WINDOWS
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#if defined(GETDNS_ON_WINDOWS) && !defined(MAP_INHERIT_ZERO)
#define explicit_bzero(rnd, rnd_size) memset(rnd, 0, rnd_size)
#endif
#define KEYSTREAM_ONLY #define KEYSTREAM_ONLY
#include "chacha_private.h" #include "chacha_private.h"
@ -136,7 +139,15 @@ _rs_stir_if_needed(size_t len)
{ {
#ifndef MAP_INHERIT_ZERO #ifndef MAP_INHERIT_ZERO
static pid_t _rs_pid = 0; static pid_t _rs_pid = 0;
#ifdef GETDNS_ON_WINDOWS
/*
* TODO: if compiling for the Windows Runtime, use GetCurrentProcessId(),
* but this requires linking with kernel32.lib
*/
pid_t pid = _getpid();
#else
pid_t pid = getpid(); pid_t pid = getpid();
#endif
/* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */ /* If a system lacks MAP_INHERIT_ZERO, resort to getpid() */
if (_rs_pid == 0 || _rs_pid != pid) { if (_rs_pid == 0 || _rs_pid != pid) {

73
src/compat/gettimeofday.c Normal file
View File

@ -0,0 +1,73 @@
/*
* Copyright (c) 2016 Christian Huitema <huitema@huitema.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Numerous places in the code make reference to the Unix/Linux
* "gettimeofday()" function, which is not available in the standard
* windows libraries. This code provides a compatible implementation.
*/
#include "config.h"
#ifdef GETDNS_ON_WINDOWS
int gettimeofday(struct timeval* tv, struct timezone* tz)
{
FILETIME ft;
uint64_t now = 0;
/*
* The GetSystemTimeAsFileTime API returns the number
* of 100-nanosecond intervals since January 1, 1601 (UTC),
* in FILETIME format.
*/
GetSystemTimeAsFileTime(&ft);
/*
* Convert to plain 64 bit format, without making
* assumptions about the FILETIME structure alignment.
*/
now |= ft.dwHighDateTime;
now <<= 32;
now |= ft.dwLowDateTime;
/*
* Convert units from 100ns to 1us
*/
now /= 10;
/*
* Account for microseconds elapsed between 1601 and 1970.
*/
now -= 11644473600000000ULL;
if (tv != NULL)
{
uint64_t sec = now / 1000000;
uint64_t usec = now % 1000000;
tv->tv_sec = (long)sec;
tv->tv_usec = (long)usec;
}
if (tz != NULL)
{
/*
* TODO: implement a timezone retrieval function.
* Not urgent, since the GetDNS code always set this parameter to NULL.
*/
return -1;
}
return 0;
}
#endif /* GETDNS_ON_WINDOWS */

View File

@ -25,6 +25,8 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "Config.h"
#include "extension/default_eventloop.h" #include "extension/default_eventloop.h"
#include "debug.h" #include "debug.h"
@ -81,8 +83,7 @@ default_eventloop_schedule(getdns_eventloop *loop,
#endif #endif
default_loop->fd_events[fd] = event; default_loop->fd_events[fd] = event;
default_loop->fd_timeout_times[fd] = get_now_plus(timeout); default_loop->fd_timeout_times[fd] = get_now_plus(timeout);
event->ev = (void *) (intptr_t) fd + 1; event->ev = (void *)(intptr_t)(fd + 1);
DEBUG_SCHED( "scheduled read/write at %d\n", fd); DEBUG_SCHED( "scheduled read/write at %d\n", fd);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
@ -102,8 +103,7 @@ default_eventloop_schedule(getdns_eventloop *loop,
if (default_loop->timeout_events[i] == NULL) { if (default_loop->timeout_events[i] == NULL) {
default_loop->timeout_events[i] = event; default_loop->timeout_events[i] = event;
default_loop->timeout_times[i] = get_now_plus(timeout); default_loop->timeout_times[i] = get_now_plus(timeout);
event->ev = (void *) (intptr_t) i + 1; event->ev = (void *)(intptr_t)(i + 1);
DEBUG_SCHED( "scheduled timeout at %d\n", (int)i); DEBUG_SCHED( "scheduled timeout at %d\n", (int)i);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
@ -219,8 +219,8 @@ default_eventloop_run_once(getdns_eventloop *loop, int blocking)
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 0; tv.tv_usec = 0;
} else { } else {
tv.tv_sec = (timeout - now) / 1000000; tv.tv_sec = (long)((timeout - now) / 1000000);
tv.tv_usec = (timeout - now) % 1000000; tv.tv_usec = (long)((timeout - now) % 1000000);
} }
if (select(max_fd + 1, &readfds, &writefds, NULL, if (select(max_fd + 1, &readfds, &writefds, NULL,
(timeout == ((uint64_t)-1) ? NULL : &tv)) < 0) { (timeout == ((uint64_t)-1) ? NULL : &tv)) < 0) {

View File

@ -399,7 +399,11 @@ tcp_connect(getdns_upstream *upstream, getdns_transport_list_t transport)
upstream->addr_len) == -1) { upstream->addr_len) == -1) {
if (_getdns_EINPROGRESS || _getdns_EWOULDBLOCK) if (_getdns_EINPROGRESS || _getdns_EWOULDBLOCK)
return fd; return fd;
#ifdef USE_WINSOCK
closesocket(fd);
#else
close(fd); close(fd);
#endif
return -1; return -1;
} }
return fd; return fd;
@ -541,7 +545,13 @@ void
_getdns_cancel_stub_request(getdns_network_req *netreq) _getdns_cancel_stub_request(getdns_network_req *netreq)
{ {
stub_cleanup(netreq); stub_cleanup(netreq);
if (netreq->fd >= 0) close(netreq->fd); if (netreq->fd >= 0) {
#ifdef USE_WINSOCK
closesocket(netreq->fd);
#else
close(netreq->fd);
#endif
}
} }
/* May be needed in future for better UDP error handling?*/ /* May be needed in future for better UDP error handling?*/
@ -564,7 +574,12 @@ stub_timeout_cb(void *userarg)
STUB_DEBUG_CLEANUP, __FUNCTION__, netreq); STUB_DEBUG_CLEANUP, __FUNCTION__, netreq);
stub_next_upstream(netreq); stub_next_upstream(netreq);
stub_cleanup(netreq); stub_cleanup(netreq);
if (netreq->fd >= 0) close(netreq->fd); if (netreq->fd >= 0)
#ifdef USE_WINSOCK
closesocket(netreq->fd);
#else
close(netreq->fd);
#endif
netreq->state = NET_REQ_TIMED_OUT; netreq->state = NET_REQ_TIMED_OUT;
if (netreq->owner->user_callback) { if (netreq->owner->user_callback) {
netreq->debug_end_time = _getdns_get_time_as_uintt64(); netreq->debug_end_time = _getdns_get_time_as_uintt64();
@ -807,8 +822,13 @@ stub_tcp_write(int fd, getdns_tcp_state *tcp, getdns_network_req *netreq)
/* Coming back from an earlier unfinished write or handshake. /* Coming back from an earlier unfinished write or handshake.
* Try to send remaining data */ * Try to send remaining data */
#ifdef USE_WINSOCK
written = send(fd, tcp->write_buf + tcp->written,
tcp->write_buf_len - tcp->written, 0);
#else
written = write(fd, tcp->write_buf + tcp->written, written = write(fd, tcp->write_buf + tcp->written,
tcp->write_buf_len - tcp->written); tcp->write_buf_len - tcp->written);
#endif
if (written == -1) { if (written == -1) {
if (_getdns_EWOULDBLOCK) if (_getdns_EWOULDBLOCK)
return STUB_TCP_WOULDBLOCK; return STUB_TCP_WOULDBLOCK;
@ -1268,6 +1288,7 @@ _getdns_get_time_as_uintt64() {
return now; return now;
} }
/**************************/ /**************************/
/* UDP callback functions */ /* UDP callback functions */
/**************************/ /**************************/
@ -1305,7 +1326,11 @@ stub_udp_read_cb(void *userarg)
upstream, netreq->response, read)) upstream, netreq->response, read))
return; /* Client cookie didn't match? */ return; /* Client cookie didn't match? */
#ifdef USE_WINSOCK
closesocket(netreq->fd);
#else
close(netreq->fd); close(netreq->fd);
#endif
while (GLDNS_TC_WIRE(netreq->response)) { while (GLDNS_TC_WIRE(netreq->response)) {
DEBUG_STUB("%s %-35s: MSG: %p TC bit set in response \n", STUB_DEBUG_READ, DEBUG_STUB("%s %-35s: MSG: %p TC bit set in response \n", STUB_DEBUG_READ,
__FUNCTION__, netreq); __FUNCTION__, netreq);
@ -1369,7 +1394,11 @@ stub_udp_write_cb(void *userarg)
netreq->fd, (const void *)netreq->query, pkt_len, 0, netreq->fd, (const void *)netreq->query, pkt_len, 0,
(struct sockaddr *)&netreq->upstream->addr, (struct sockaddr *)&netreq->upstream->addr,
netreq->upstream->addr_len)) { netreq->upstream->addr_len)) {
#ifdef USE_WINSOCK
closesocket(netreq->fd);
#else
close(netreq->fd); close(netreq->fd);
#endif
return; return;
} }
GETDNS_SCHEDULE_EVENT( GETDNS_SCHEDULE_EVENT(
@ -1706,7 +1735,11 @@ upstream_connect(getdns_upstream *upstream, getdns_transport_list_t transport,
if (fd == -1) return -1; if (fd == -1) return -1;
upstream->tls_obj = tls_create_object(dnsreq, fd, upstream); upstream->tls_obj = tls_create_object(dnsreq, fd, upstream);
if (upstream->tls_obj == NULL) { if (upstream->tls_obj == NULL) {
#ifdef USE_WINSOCK
closesocket(fd);
#else
close(fd); close(fd);
#endif
return -1; return -1;
} }