mirror of https://github.com/getdnsapi/getdns.git
Bug fix in get_os_defaults, clean up code in winsock_event, add code to handle event handling differences in Winsock2
This commit is contained in:
parent
2d58ed465c
commit
22a8550caa
|
@ -803,10 +803,9 @@ set_os_defaults_windows(struct getdns_context *context)
|
|||
hints.ai_addr = NULL;
|
||||
hints.ai_next = NULL;
|
||||
|
||||
//g
|
||||
FIXED_INFO *info;
|
||||
ULONG buflen = sizeof(*info);
|
||||
IP_ADDR_STRING *ptr;
|
||||
IP_ADDR_STRING *ptr = 0;
|
||||
|
||||
info = (FIXED_INFO *)malloc(sizeof(FIXED_INFO));
|
||||
if (info == NULL)
|
||||
|
@ -821,7 +820,7 @@ set_os_defaults_windows(struct getdns_context *context)
|
|||
|
||||
if (GetNetworkParams(info, &buflen) == NO_ERROR) {
|
||||
int retval = 0;
|
||||
ptr = &(info->DnsServerList);
|
||||
ptr = info->DnsServerList.Next;
|
||||
*domain = 0;
|
||||
while (ptr) {
|
||||
for (size_t i = 0; i < GETDNS_UPSTREAM_TRANSPORTS; i++) {
|
||||
|
@ -843,7 +842,6 @@ set_os_defaults_windows(struct getdns_context *context)
|
|||
free(info);
|
||||
}
|
||||
|
||||
|
||||
(void)getdns_list_get_length(context->suffix, &length);
|
||||
if (length == 0 && *domain != 0) {
|
||||
bindata.data = (uint8_t *)domain;
|
||||
|
@ -994,7 +992,7 @@ getdns_context_create_with_extended_memory_functions(
|
|||
result->fchg_resolvconf = NULL;
|
||||
result->fchg_hosts = NULL;
|
||||
|
||||
//g resolv.conf does not exist on Windows, handle differently
|
||||
// resolv.conf does not exist on Windows, handle differently
|
||||
#ifndef USE_WINSOCK
|
||||
if (set_from_os && (r = set_os_defaults(result)))
|
||||
goto error;
|
||||
|
@ -2358,18 +2356,24 @@ _getdns_context_prepare_for_resolution(struct getdns_context *context,
|
|||
/* Create client context, use TLS v1.2 only for now */
|
||||
context->tls_ctx = SSL_CTX_new(TLSv1_2_client_method());
|
||||
if (context->tls_ctx == NULL)
|
||||
printf("ERROR! Bad TLS context!");
|
||||
//g return GETDNS_RETURN_BAD_CONTEXT;
|
||||
#ifndef USE_WINSOCK
|
||||
return GETDNS_RETURN_BAD_CONTEXT;
|
||||
#else
|
||||
printf("Warning! Bad TLS context, check openssl version on Windows!\n");;
|
||||
#endif
|
||||
/* Be strict and only use the cipher suites recommended in RFC7525
|
||||
Unless we later fallback to opportunistic. */
|
||||
const char* const PREFERRED_CIPHERS = "EECDH+aRSA+AESGCM:EECDH+aECDSA+AESGCM:EDH+aRSA+AESGCM";
|
||||
if (!SSL_CTX_set_cipher_list(context->tls_ctx, PREFERRED_CIPHERS))
|
||||
if (!SSL_CTX_set_cipher_list(context->tls_ctx, PREFERRED_CIPHERS)) {
|
||||
return GETDNS_RETURN_BAD_CONTEXT;
|
||||
if (!SSL_CTX_set_default_verify_paths(context->tls_ctx))
|
||||
}
|
||||
if (!SSL_CTX_set_default_verify_paths(context->tls_ctx)) {
|
||||
return GETDNS_RETURN_BAD_CONTEXT;
|
||||
}
|
||||
#else
|
||||
if (tls_only_is_in_transports_list(context) == 1)
|
||||
if (tls_only_is_in_transports_list(context) == 1) {
|
||||
return GETDNS_RETURN_BAD_CONTEXT;
|
||||
}
|
||||
/* A null tls_ctx will make TLS fail and fallback to the other
|
||||
transports will kick-in.*/
|
||||
#endif
|
||||
|
|
21
src/stub.c
21
src/stub.c
|
@ -50,6 +50,7 @@
|
|||
#define EINPROGRESS 112
|
||||
#define EWOULDBLOCK 140
|
||||
typedef u_short sa_family_t;
|
||||
#include "util/winsock_event.h"
|
||||
#endif
|
||||
|
||||
#define STUB_OUT_OF_OPTIONS -5 /* upstream options exceeded MAXIMUM_UPSTREAM_OPTION_SPACE */
|
||||
|
@ -623,7 +624,7 @@ stub_tls_timeout_cb(void *userarg)
|
|||
/****************************/
|
||||
|
||||
static int
|
||||
stub_tcp_read(int fd, getdns_tcp_state *tcp, struct mem_funcs *mf)
|
||||
stub_tcp_read(int fd, getdns_tcp_state *tcp, struct mem_funcs *mf, getdns_eventloop_event* event)
|
||||
{
|
||||
ssize_t read;
|
||||
uint8_t *buf;
|
||||
|
@ -640,10 +641,24 @@ stub_tcp_read(int fd, getdns_tcp_state *tcp, struct mem_funcs *mf)
|
|||
}
|
||||
read = recv(fd, tcp->read_pos, tcp->to_read, 0);
|
||||
if (read == -1) {
|
||||
#ifdef USE_WINSOCK
|
||||
printf("read (in tcp ) %s\n",
|
||||
wsa_strerror(WSAGetLastError()));
|
||||
if (WSAGetLastError() == WSAECONNRESET)
|
||||
return STUB_TCP_AGAIN;
|
||||
if (WSAGetLastError() == WSAEINPROGRESS)
|
||||
return STUB_TCP_AGAIN;
|
||||
if (WSAGetLastError() == WSAEWOULDBLOCK) {
|
||||
winsock_tcp_wouldblock(event, EV_READ);
|
||||
return STUB_TCP_AGAIN;
|
||||
}
|
||||
|
||||
#else
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||
return STUB_TCP_AGAIN;
|
||||
else
|
||||
return STUB_TCP_ERROR;
|
||||
#endif
|
||||
} else if (read == 0) {
|
||||
/* Remote end closed the socket */
|
||||
/* TODO: Try to reconnect */
|
||||
|
@ -1359,7 +1374,7 @@ stub_tcp_read_cb(void *userarg)
|
|||
int q;
|
||||
|
||||
switch ((q = stub_tcp_read(netreq->fd, &netreq->tcp,
|
||||
&dnsreq->context->mf))) {
|
||||
&dnsreq->context->mf, &netreq->event))) {
|
||||
|
||||
case STUB_TCP_AGAIN:
|
||||
return;
|
||||
|
@ -1437,7 +1452,7 @@ upstream_read_cb(void *userarg)
|
|||
&upstream->upstreams->mf);
|
||||
else
|
||||
q = stub_tcp_read(upstream->fd, &upstream->tcp,
|
||||
&upstream->upstreams->mf);
|
||||
&upstream->upstreams->mf, &netreq->event);
|
||||
|
||||
switch (q) {
|
||||
case STUB_TCP_AGAIN:
|
||||
|
|
|
@ -36,10 +36,11 @@
|
|||
* \file
|
||||
* Implementation of the getdns WinSock2 API event notification handler
|
||||
* for the getdns Windows port.
|
||||
* Code is originally from the Unbound source for Windows.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#ifdef USE_WINSOCK
|
||||
#ifdef USE_WINSOCK // only included for Windows builds
|
||||
#include <signal.h>
|
||||
#ifdef HAVE_TIME_H
|
||||
#include <time.h>
|
||||
|
@ -61,6 +62,7 @@ log_err(const char *format, ...)
|
|||
va_end(args);
|
||||
}
|
||||
|
||||
|
||||
char* wsa_strerror(DWORD err)
|
||||
{
|
||||
static char unknown[32];
|
||||
|
@ -200,7 +202,7 @@ settime(struct _getdns_event_base* base)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#ifdef UNBOUND_DEBUG
|
||||
#ifdef WINSOCK_DEBUG
|
||||
/**
|
||||
* Find a fd in the list of items.
|
||||
* Note that not all items have a fd associated (those are -1).
|
||||
|
@ -258,14 +260,15 @@ void *_getdns_event_init(time_t* time_secs, struct timeval* time_tv)
|
|||
_getdns_event_base_free(base);
|
||||
return NULL;
|
||||
}
|
||||
base->signals = (struct _getdns_event**)calloc(MAX_SIG, sizeof(struct _getdns_event*));
|
||||
base->signals = (struct _getdns_event**)calloc(MAX_SIG,
|
||||
sizeof(struct _getdns_event*));
|
||||
if(!base->signals) {
|
||||
_getdns_event_base_free(base);
|
||||
return NULL;
|
||||
}
|
||||
base->tcp_stickies = 0;
|
||||
base->tcp_reinvigorated = 0;
|
||||
//verbose(VERB_CLIENT, "winsock_event inited");
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
|
@ -279,7 +282,6 @@ const char *_getdns_event_get_method(void)
|
|||
return "WSAWaitForMultipleEvents";
|
||||
}
|
||||
|
||||
//gowri remove static
|
||||
/** call timeouts handlers, and return how long to wait for next one or -1 */
|
||||
void _getdns_handle_timeouts(struct _getdns_event_base* base, struct timeval* now,
|
||||
struct timeval* wait)
|
||||
|
@ -288,7 +290,6 @@ void _getdns_handle_timeouts(struct _getdns_event_base* base, struct timeval* no
|
|||
#ifndef S_SPLINT_S
|
||||
wait->tv_sec = (time_t)-1;
|
||||
#endif
|
||||
//verbose(VERB_CLIENT, "winsock_event handle_timeouts");
|
||||
|
||||
while((_getdns_rbnode_t*)(p = (struct _getdns_event*)_getdns_rbtree_first(base->times))
|
||||
!=RBTREE_NULL) {
|
||||
|
@ -306,8 +307,6 @@ void _getdns_handle_timeouts(struct _getdns_event_base* base, struct timeval* no
|
|||
wait->tv_usec = p->ev_timeout.tv_usec
|
||||
- now->tv_usec;
|
||||
}
|
||||
//verbose(VERB_CLIENT, "winsock_event wait=" ARG_LL "d.%6.6d",
|
||||
// (long long)wait->tv_sec, (int)wait->tv_usec);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
@ -317,7 +316,6 @@ void _getdns_handle_timeouts(struct _getdns_event_base* base, struct timeval* no
|
|||
fptr_ok(fptr_whitelist_event(p->ev_callback));
|
||||
(*p->ev_callback)(p->ev_fd, EV_TIMEOUT, p->ev_arg);
|
||||
}
|
||||
//verbose(VERB_CLIENT, "winsock_event wait=(-1)");
|
||||
}
|
||||
|
||||
/** handle is_signal events and see if signalled */
|
||||
|
@ -380,7 +378,8 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
eventlist[numwait] = base->items[i];
|
||||
base->waitfor[numwait++] = base->items[i]->hEvent;
|
||||
printf("winsock_event bmax=%d numwait=%d wait=%x "
|
||||
"timeout=%d hEvent %d\n", base->max, numwait, (int)wait, (int)timeout, base->items[i]->hEvent);
|
||||
"timeout=%d hEvent %d\n", base->max, numwait, (int)wait,
|
||||
(int)timeout, base->items[i]->hEvent);
|
||||
if (numwait == WSK_MAX_ITEMS)
|
||||
break; /* sanity check */
|
||||
}
|
||||
|
@ -398,19 +397,19 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
//gv: do not schedule udp write
|
||||
for (i = 0; i<base->max; i++) {
|
||||
if (!base->items[i]->is_tcp && base->items[i]->ev_events&EV_WRITE) {
|
||||
//gprintf("skip UDP sched\n");
|
||||
printf("skip UDP sched\n");
|
||||
(*eventlist[i]->ev_callback)(eventlist[i]->ev_fd,
|
||||
EV_WRITE & eventlist[i]->ev_events,
|
||||
eventlist[i]->ev_arg);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
//gprintf("before wait %d\n", base->items[0]->ev_events);
|
||||
printf("before wait %d\n", base->items[0]->ev_events);
|
||||
ret = WSAWaitForMultipleEvents(numwait, base->waitfor,
|
||||
0 /* do not wait for all, just one will do */,
|
||||
wait?timeout:WSA_INFINITE,
|
||||
0); /* we are not alertable (IO completion events) */
|
||||
//gprintf("after wait %d %d\n", ret, numwait);
|
||||
printf("after wait %d %d\n", ret, numwait);
|
||||
if(ret == WSA_WAIT_IO_COMPLETION) {
|
||||
//printf("getdns: WSAWaitForMultipleEvents failed: WSA_WAIT_IO_COMPLETION");
|
||||
return -1;
|
||||
|
@ -419,7 +418,7 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
// wsa_strerror(WSAGetLastError()));
|
||||
return -1;
|
||||
} else if(ret == WSA_WAIT_TIMEOUT) {
|
||||
//printf("timeout\n");
|
||||
printf("timeout\n");
|
||||
was_timeout = 1;
|
||||
} else
|
||||
startidx = ret - WSA_WAIT_EVENT_0;
|
||||
|
@ -510,12 +509,12 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
printf("FD_CLOSE_BIT\n");
|
||||
}
|
||||
if(eventlist[i]->is_tcp && eventlist[i]->stick_events) {
|
||||
/*
|
||||
|
||||
printf("winsock %d pass sticky %s%s\n",
|
||||
eventlist[i]->ev_fd,
|
||||
(eventlist[i]->old_events&EV_READ)?"EV_READ":"",
|
||||
(eventlist[i]->old_events&EV_WRITE)?"EV_WRITE":"");
|
||||
*/
|
||||
|
||||
bits |= eventlist[i]->old_events;
|
||||
}
|
||||
if(eventlist[i]->is_tcp && bits) {
|
||||
|
@ -524,12 +523,12 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
if((eventlist[i]->ev_events & bits)) {
|
||||
newstickies = 1;
|
||||
}
|
||||
/*
|
||||
|
||||
printf("winsock %d store sticky %s%s",
|
||||
eventlist[i]->ev_fd,
|
||||
(eventlist[i]->old_events&EV_READ)?"EV_READ":"",
|
||||
(eventlist[i]->old_events&EV_WRITE)?"EV_WRITE":"");
|
||||
*/
|
||||
(eventlist[i]->old_events&EV_WRITE) ? "EV_WRITE" : "");
|
||||
|
||||
}
|
||||
if((bits & eventlist[i]->ev_events)) {
|
||||
printf( "winsock event callback %p fd=%d "
|
||||
|
@ -552,13 +551,13 @@ int _getdns_handle_select(struct _getdns_event_base* base, struct timeval* wait)
|
|||
bits & eventlist[i]->ev_events,
|
||||
eventlist[i]->ev_arg);
|
||||
}
|
||||
/*
|
||||
|
||||
if(eventlist[i]->is_tcp && bits)
|
||||
printf( "winsock %d got sticky %s%s\n",
|
||||
eventlist[i]->ev_fd,
|
||||
(eventlist[i]->old_events&EV_READ)?"EV_READ":"",
|
||||
(eventlist[i]->old_events&EV_WRITE)?"EV_WRITE":"");
|
||||
*/
|
||||
|
||||
}
|
||||
//verbose(VERB_CLIENT, "winsock_event net");
|
||||
if(base->tcp_reinvigorated) {
|
||||
|
@ -595,14 +594,12 @@ int _getdns_event_base_dispatch(struct _getdns_event_base *base)
|
|||
int _getdns_event_base_loopexit(struct _getdns_event_base *base,
|
||||
struct timeval * ATTR_UNUSED(tv))
|
||||
{
|
||||
//verbose(VERB_CLIENT, "winsock_event loopexit");
|
||||
base->need_to_exit = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _getdns_event_base_free(struct _getdns_event_base *base)
|
||||
{
|
||||
//verbose(VERB_CLIENT, "winsock_event event_base_free");
|
||||
if(!base)
|
||||
return;
|
||||
if(base->items)
|
||||
|
@ -718,8 +715,6 @@ int _getdns_event_add(struct _getdns_event *ev, struct timeval *tv)
|
|||
/* automatically sets fd to nonblocking mode.
|
||||
* nonblocking cannot be disabled, until wsaES(fd, NULL, 0) */
|
||||
printf("\nWSAEventSelect %d events %d hEvent %d\n", ev->ev_fd, events, ev->hEvent);
|
||||
//gg if (WSAEventSelect(ev->ev_fd, ev->hEvent, FD_ACCEPT | FD_CONNECT | FD_READ | FD_CLOSE | FD_WRITE) != 0) {
|
||||
//if (WSAEventSelect(ev->ev_fd, ev->hEvent,FD_READ | FD_WRITE) != 0) {
|
||||
if (WSAEventSelect(ev->ev_fd, ev->hEvent, events) != 0) {
|
||||
log_err("getdns: WSAEventSelect in getdns failed: %s",
|
||||
wsa_strerror(WSAGetLastError()));
|
||||
|
@ -727,11 +722,14 @@ int _getdns_event_add(struct _getdns_event *ev, struct timeval *tv)
|
|||
if(ev->is_tcp && ev->stick_events &&
|
||||
(ev->ev_events & ev->old_events)) {
|
||||
/* go to processing the sticky event right away */
|
||||
printf("\nWSAEventSelect sticky %d events %d hEvent %d\n", ev->ev_fd, events, ev->hEvent);
|
||||
ev->ev_base->tcp_reinvigorated = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(tv && (ev->ev_events&EV_TIMEOUT)) {
|
||||
printf("\nWSAEventSelect timeout %d hEvent %d\n", ev->ev_fd, ev->hEvent);
|
||||
|
||||
#ifndef S_SPLINT_S
|
||||
struct timeval *now = ev->ev_base->time_tv;
|
||||
ev->ev_timeout.tv_sec = tv->tv_sec + now->tv_sec;
|
||||
|
@ -823,8 +821,8 @@ int _getdns_signal_del(struct _getdns_event *ev)
|
|||
|
||||
void winsock_tcp_wouldblock(struct _getdns_event* ev, int eventbits)
|
||||
{
|
||||
//verbose(VERB_ALGO, "winsock: tcp wouldblock %s",
|
||||
//eventbits==EV_READ?"EV_READ":"EV_WRITE");
|
||||
printf("winsock: tcp wouldblock %s\n",
|
||||
eventbits==EV_READ?"EV_READ":"EV_WRITE");
|
||||
ev->old_events &= (~eventbits);
|
||||
if(ev->old_events == 0)
|
||||
ev->stick_events = 0;
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
* This file contains interface functions with the WinSock2 API on Windows.
|
||||
* It uses the winsock WSAWaitForMultipleEvents interface on a number of
|
||||
* sockets.
|
||||
* Code is originally from the Unbound source for Windows.
|
||||
*
|
||||
* Note that windows can only wait for max 64 events at one time.
|
||||
*
|
||||
|
@ -267,7 +268,7 @@ int getdns_mini_ev_cmp(const void* a, const void* b);
|
|||
* retesting the event.
|
||||
* Pass if EV_READ or EV_WRITE gave wouldblock.
|
||||
*/
|
||||
static void winsock_tcp_wouldblock(struct _getdns_event* ev, int eventbit);
|
||||
void winsock_tcp_wouldblock(struct _getdns_event* ev, int eventbit);
|
||||
|
||||
/**
|
||||
* Routine for windows only. where you pass a signal WSAEvent that
|
||||
|
|
Loading…
Reference in New Issue