197403Sobrien/*
2169691Skan * Copyright 2007-2012 Niels Provos and Nick Mathewson
3169691Skan * Copyright 2000-2007 Niels Provos <provos@citi.umich.edu>
497403Sobrien * Copyright 2003 Michael A. Davis <mike@datanerds.net>
597403Sobrien *
6132720Skan * Redistribution and use in source and binary forms, with or without
797403Sobrien * modification, are permitted provided that the following conditions
8132720Skan * are met:
997403Sobrien * 1. Redistributions of source code must retain the above copyright
1097403Sobrien *    notice, this list of conditions and the following disclaimer.
1197403Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1297403Sobrien *    notice, this list of conditions and the following disclaimer in the
13132720Skan *    documentation and/or other materials provided with the distribution.
1497403Sobrien * 3. The name of the author may not be used to endorse or promote products
1597403Sobrien *    derived from this software without specific prior written permission.
1697403Sobrien *
1797403Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1897403Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19132720Skan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20169691Skan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21169691Skan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2297403Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2397403Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2497403Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2597403Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2697403Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2797403Sobrien */
2897403Sobrien#include "event2/event-config.h"
2997403Sobrien#include "evconfig-private.h"
3097403Sobrien
3197403Sobrien#ifdef _WIN32
3297403Sobrien
33169691Skan#include <winsock2.h>
3497403Sobrien#include <windows.h>
3597403Sobrien#include <sys/types.h>
36132720Skan#include <sys/queue.h>
37132720Skan#include <limits.h>
3897403Sobrien#include <signal.h>
3997403Sobrien#include <stdio.h>
4097403Sobrien#include <stdlib.h>
41169691Skan#include <string.h>
42169691Skan#include <errno.h>
4397403Sobrien
4497403Sobrien#include "event2/util.h"
4597403Sobrien#include "util-internal.h"
4697403Sobrien#include "log-internal.h"
4797403Sobrien#include "event2/event.h"
4897403Sobrien#include "event-internal.h"
4997403Sobrien#include "evmap-internal.h"
50169691Skan#include "event2/thread.h"
51169691Skan#include "evthread-internal.h"
52169691Skan#include "time-internal.h"
53169691Skan
54169691Skan#define XFREE(ptr) do { if (ptr) mm_free(ptr); } while (0)
55169691Skan
56169691Skanextern struct event_list timequeue;
57169691Skanextern struct event_list addqueue;
5897403Sobrien
5997403Sobrienstruct win_fd_set {
6097403Sobrien	u_int fd_count;
6197403Sobrien	SOCKET fd_array[1];
62117397Skan};
63117397Skan
64117397Skan/* MSDN says this is required to handle SIGFPE */
65117397Skanvolatile double SIGFPE_REQ = 0.0f;
6697403Sobrien
67117397Skanstruct idx_info {
6897403Sobrien	int read_pos_plus1;
6997403Sobrien	int write_pos_plus1;
7097403Sobrien};
71171827Skan
7297403Sobrienstruct win32op {
7397403Sobrien	unsigned num_fds_in_fd_sets;
7497403Sobrien	int resize_out_sets;
7597403Sobrien	struct win_fd_set *readset_in;
7697403Sobrien	struct win_fd_set *writeset_in;
7797403Sobrien	struct win_fd_set *readset_out;
7897403Sobrien	struct win_fd_set *writeset_out;
7997403Sobrien	struct win_fd_set *exset_out;
8097403Sobrien	unsigned signals_are_broken : 1;
8197403Sobrien};
8297403Sobrien
8397403Sobrienstatic void *win32_init(struct event_base *);
84171827Skanstatic int win32_add(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
8597403Sobrienstatic int win32_del(struct event_base *, evutil_socket_t, short old, short events, void *idx_);
8697403Sobrienstatic int win32_dispatch(struct event_base *base, struct timeval *);
8797403Sobrienstatic void win32_dealloc(struct event_base *);
8897403Sobrien
8997403Sobrienstruct eventop win32ops = {
9097403Sobrien	"win32",
9197403Sobrien	win32_init,
9297403Sobrien	win32_add,
9397403Sobrien	win32_del,
9497403Sobrien	win32_dispatch,
9597403Sobrien	win32_dealloc,
96171827Skan	0, /* doesn't need reinit */
9797403Sobrien	0, /* No features supported. */
9897403Sobrien	sizeof(struct idx_info),
9997403Sobrien};
10097403Sobrien
10197403Sobrien#define FD_SET_ALLOC_SIZE(n) ((sizeof(struct win_fd_set) + ((n)-1)*sizeof(SOCKET)))
102228780Spfg
103233699Stheravenstatic int
104233699Stheravengrow_fd_sets(struct win32op *op, unsigned new_num_fds)
105233699Stheraven{
106233699Stheraven	size_t size;
107233699Stheraven
108233699Stheraven	EVUTIL_ASSERT(new_num_fds >= op->readset_in->fd_count &&
10997403Sobrien	       new_num_fds >= op->writeset_in->fd_count);
11097403Sobrien	EVUTIL_ASSERT(new_num_fds >= 1);
11197403Sobrien
11297403Sobrien	size = FD_SET_ALLOC_SIZE(new_num_fds);
11397403Sobrien	if (!(op->readset_in = mm_realloc(op->readset_in, size)))
11497403Sobrien		return (-1);
11597403Sobrien	if (!(op->writeset_in = mm_realloc(op->writeset_in, size)))
11697403Sobrien		return (-1);
11797403Sobrien	op->resize_out_sets = 1;
118171827Skan	op->num_fds_in_fd_sets = new_num_fds;
11997403Sobrien	return (0);
12097403Sobrien}
121171827Skan
122171827Skanstatic int
123171827Skando_fd_set(struct win32op *op, struct idx_info *ent, evutil_socket_t s, int read)
124171827Skan{
125171827Skan	struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
126171827Skan	if (read) {
127171827Skan		if (ent->read_pos_plus1 > 0)
128171827Skan			return (0);
129171827Skan	} else {
130171827Skan		if (ent->write_pos_plus1 > 0)
13197403Sobrien			return (0);
13297403Sobrien	}
133117397Skan	if (set->fd_count == op->num_fds_in_fd_sets) {
134117397Skan		if (grow_fd_sets(op, op->num_fds_in_fd_sets*2))
135117397Skan			return (-1);
136117397Skan		/* set pointer will have changed and needs reiniting! */
13797403Sobrien		set = read ? op->readset_in : op->writeset_in;
13897403Sobrien	}
13997403Sobrien	set->fd_array[set->fd_count] = s;
14097403Sobrien	if (read)
14197403Sobrien		ent->read_pos_plus1 = set->fd_count+1;
142171827Skan	else
14397403Sobrien		ent->write_pos_plus1 = set->fd_count+1;
14497403Sobrien	return (set->fd_count++);
14597403Sobrien}
146171827Skan
147169691Skanstatic int
148169691Skando_fd_clear(struct event_base *base,
14997403Sobrien			struct win32op *op, struct idx_info *ent, int read)
15097403Sobrien{
15197403Sobrien	int i;
15297403Sobrien	struct win_fd_set *set = read ? op->readset_in : op->writeset_in;
15397403Sobrien	if (read) {
15497403Sobrien		i = ent->read_pos_plus1 - 1;
15597403Sobrien		ent->read_pos_plus1 = 0;
156171827Skan	} else {
15797403Sobrien		i = ent->write_pos_plus1 - 1;
15897403Sobrien		ent->write_pos_plus1 = 0;
15997403Sobrien	}
160171827Skan	if (i < 0)
161169691Skan		return (0);
162169691Skan	if (--set->fd_count != (unsigned)i) {
16397403Sobrien		struct idx_info *ent2;
16497403Sobrien		SOCKET s2;
16597403Sobrien		s2 = set->fd_array[i] = set->fd_array[set->fd_count];
166169691Skan
167169691Skan		ent2 = evmap_io_get_fdinfo_(&base->io, s2);
16897403Sobrien
16997403Sobrien		if (!ent2) /* This indicates a bug. */
170			return (0);
171		if (read)
172			ent2->read_pos_plus1 = i+1;
173		else
174			ent2->write_pos_plus1 = i+1;
175	}
176	return (0);
177}
178
179#define NEVENT 32
180void *
181win32_init(struct event_base *base)
182{
183	struct win32op *winop;
184	size_t size;
185	if (!(winop = mm_calloc(1, sizeof(struct win32op))))
186		return NULL;
187	winop->num_fds_in_fd_sets = NEVENT;
188	size = FD_SET_ALLOC_SIZE(NEVENT);
189	if (!(winop->readset_in = mm_malloc(size)))
190		goto err;
191	if (!(winop->writeset_in = mm_malloc(size)))
192		goto err;
193	if (!(winop->readset_out = mm_malloc(size)))
194		goto err;
195	if (!(winop->writeset_out = mm_malloc(size)))
196		goto err;
197	if (!(winop->exset_out = mm_malloc(size)))
198		goto err;
199	winop->readset_in->fd_count = winop->writeset_in->fd_count = 0;
200	winop->readset_out->fd_count = winop->writeset_out->fd_count
201		= winop->exset_out->fd_count = 0;
202
203	if (evsig_init_(base) < 0)
204		winop->signals_are_broken = 1;
205
206	evutil_weakrand_seed_(&base->weakrand_seed, 0);
207
208	return (winop);
209 err:
210	XFREE(winop->readset_in);
211	XFREE(winop->writeset_in);
212	XFREE(winop->readset_out);
213	XFREE(winop->writeset_out);
214	XFREE(winop->exset_out);
215	XFREE(winop);
216	return (NULL);
217}
218
219int
220win32_add(struct event_base *base, evutil_socket_t fd,
221			 short old, short events, void *idx_)
222{
223	struct win32op *win32op = base->evbase;
224	struct idx_info *idx = idx_;
225
226	if ((events & EV_SIGNAL) && win32op->signals_are_broken)
227		return (-1);
228
229	if (!(events & (EV_READ|EV_WRITE)))
230		return (0);
231
232	event_debug(("%s: adding event for %d", __func__, (int)fd));
233	if (events & EV_READ) {
234		if (do_fd_set(win32op, idx, fd, 1)<0)
235			return (-1);
236	}
237	if (events & EV_WRITE) {
238		if (do_fd_set(win32op, idx, fd, 0)<0)
239			return (-1);
240	}
241	return (0);
242}
243
244int
245win32_del(struct event_base *base, evutil_socket_t fd, short old, short events,
246		  void *idx_)
247{
248	struct win32op *win32op = base->evbase;
249	struct idx_info *idx = idx_;
250
251	event_debug(("%s: Removing event for "EV_SOCK_FMT,
252		__func__, EV_SOCK_ARG(fd)));
253	if (events & EV_READ)
254		do_fd_clear(base, win32op, idx, 1);
255	if (events & EV_WRITE)
256		do_fd_clear(base, win32op, idx, 0);
257
258	return 0;
259}
260
261static void
262fd_set_copy(struct win_fd_set *out, const struct win_fd_set *in)
263{
264	out->fd_count = in->fd_count;
265	memcpy(out->fd_array, in->fd_array, in->fd_count * (sizeof(SOCKET)));
266}
267
268/*
269  static void dump_fd_set(struct win_fd_set *s)
270  {
271  unsigned int i;
272  printf("[ ");
273  for(i=0;i<s->fd_count;++i)
274  printf("%d ",(int)s->fd_array[i]);
275  printf("]\n");
276  }
277*/
278
279int
280win32_dispatch(struct event_base *base, struct timeval *tv)
281{
282	struct win32op *win32op = base->evbase;
283	int res = 0;
284	unsigned j, i;
285	int fd_count;
286	SOCKET s;
287
288	if (win32op->resize_out_sets) {
289		size_t size = FD_SET_ALLOC_SIZE(win32op->num_fds_in_fd_sets);
290		if (!(win32op->readset_out = mm_realloc(win32op->readset_out, size)))
291			return (-1);
292		if (!(win32op->exset_out = mm_realloc(win32op->exset_out, size)))
293			return (-1);
294		if (!(win32op->writeset_out = mm_realloc(win32op->writeset_out, size)))
295			return (-1);
296		win32op->resize_out_sets = 0;
297	}
298
299	fd_set_copy(win32op->readset_out, win32op->readset_in);
300	fd_set_copy(win32op->exset_out, win32op->writeset_in);
301	fd_set_copy(win32op->writeset_out, win32op->writeset_in);
302
303	fd_count =
304	    (win32op->readset_out->fd_count > win32op->writeset_out->fd_count) ?
305	    win32op->readset_out->fd_count : win32op->writeset_out->fd_count;
306
307	if (!fd_count) {
308		long msec = tv ? evutil_tv_to_msec_(tv) : LONG_MAX;
309		/* Sleep's DWORD argument is unsigned long */
310		if (msec < 0)
311			msec = LONG_MAX;
312		/* Windows doesn't like you to call select() with no sockets */
313		Sleep(msec);
314		return (0);
315	}
316
317	EVBASE_RELEASE_LOCK(base, th_base_lock);
318
319	res = select(fd_count,
320		     (struct fd_set*)win32op->readset_out,
321		     (struct fd_set*)win32op->writeset_out,
322		     (struct fd_set*)win32op->exset_out, tv);
323
324	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
325
326	event_debug(("%s: select returned %d", __func__, res));
327
328	if (res <= 0) {
329		return res;
330	}
331
332	if (win32op->readset_out->fd_count) {
333		i = evutil_weakrand_range_(&base->weakrand_seed,
334		    win32op->readset_out->fd_count);
335		for (j=0; j<win32op->readset_out->fd_count; ++j) {
336			if (++i >= win32op->readset_out->fd_count)
337				i = 0;
338			s = win32op->readset_out->fd_array[i];
339			evmap_io_active_(base, s, EV_READ);
340		}
341	}
342	if (win32op->exset_out->fd_count) {
343		i = evutil_weakrand_range_(&base->weakrand_seed,
344		    win32op->exset_out->fd_count);
345		for (j=0; j<win32op->exset_out->fd_count; ++j) {
346			if (++i >= win32op->exset_out->fd_count)
347				i = 0;
348			s = win32op->exset_out->fd_array[i];
349			evmap_io_active_(base, s, EV_WRITE);
350		}
351	}
352	if (win32op->writeset_out->fd_count) {
353		SOCKET s;
354		i = evutil_weakrand_range_(&base->weakrand_seed,
355		    win32op->writeset_out->fd_count);
356		for (j=0; j<win32op->writeset_out->fd_count; ++j) {
357			if (++i >= win32op->writeset_out->fd_count)
358				i = 0;
359			s = win32op->writeset_out->fd_array[i];
360			evmap_io_active_(base, s, EV_WRITE);
361		}
362	}
363	return (0);
364}
365
366void
367win32_dealloc(struct event_base *base)
368{
369	struct win32op *win32op = base->evbase;
370
371	evsig_dealloc_(base);
372	if (win32op->readset_in)
373		mm_free(win32op->readset_in);
374	if (win32op->writeset_in)
375		mm_free(win32op->writeset_in);
376	if (win32op->readset_out)
377		mm_free(win32op->readset_out);
378	if (win32op->writeset_out)
379		mm_free(win32op->writeset_out);
380	if (win32op->exset_out)
381		mm_free(win32op->exset_out);
382	/* XXXXX free the tree. */
383
384	memset(win32op, 0, sizeof(*win32op));
385	mm_free(win32op);
386}
387
388#endif
389