Home | History | Annotate | Line # | Download | only in libevent
      1  1.6  christos /*	$NetBSD: bufferevent_async.c,v 1.7 2024/08/18 20:47:20 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
      5  1.1  christos  *
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     17  1.1  christos  *    derived from this software without specific prior written permission.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  christos  */
     30  1.1  christos 
     31  1.1  christos #include "event2/event-config.h"
     32  1.1  christos #include "evconfig-private.h"
     33  1.1  christos 
     34  1.1  christos #ifdef EVENT__HAVE_SYS_TIME_H
     35  1.1  christos #include <sys/time.h>
     36  1.1  christos #endif
     37  1.1  christos 
     38  1.1  christos #include <errno.h>
     39  1.1  christos #include <stdio.h>
     40  1.1  christos #include <stdlib.h>
     41  1.1  christos #include <string.h>
     42  1.1  christos #ifdef EVENT__HAVE_STDARG_H
     43  1.1  christos #include <stdarg.h>
     44  1.1  christos #endif
     45  1.1  christos #ifdef EVENT__HAVE_UNISTD_H
     46  1.1  christos #include <unistd.h>
     47  1.1  christos #endif
     48  1.1  christos 
     49  1.1  christos #ifdef _WIN32
     50  1.1  christos #include <winsock2.h>
     51  1.7  christos #include <winerror.h>
     52  1.1  christos #include <ws2tcpip.h>
     53  1.1  christos #endif
     54  1.1  christos 
     55  1.1  christos #include <sys/queue.h>
     56  1.1  christos 
     57  1.1  christos #include "event2/util.h"
     58  1.1  christos #include "event2/bufferevent.h"
     59  1.1  christos #include "event2/buffer.h"
     60  1.1  christos #include "event2/bufferevent_struct.h"
     61  1.1  christos #include "event2/event.h"
     62  1.1  christos #include "event2/util.h"
     63  1.1  christos #include "event-internal.h"
     64  1.1  christos #include "log-internal.h"
     65  1.1  christos #include "mm-internal.h"
     66  1.1  christos #include "bufferevent-internal.h"
     67  1.1  christos #include "util-internal.h"
     68  1.1  christos #include "iocp-internal.h"
     69  1.1  christos 
     70  1.1  christos #ifndef SO_UPDATE_CONNECT_CONTEXT
     71  1.1  christos /* Mingw is sometimes missing this */
     72  1.1  christos #define SO_UPDATE_CONNECT_CONTEXT 0x7010
     73  1.1  christos #endif
     74  1.1  christos 
     75  1.1  christos /* prototypes */
     76  1.1  christos static int be_async_enable(struct bufferevent *, short);
     77  1.1  christos static int be_async_disable(struct bufferevent *, short);
     78  1.1  christos static void be_async_destruct(struct bufferevent *);
     79  1.1  christos static int be_async_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
     80  1.1  christos static int be_async_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
     81  1.1  christos 
     82  1.1  christos struct bufferevent_async {
     83  1.1  christos 	struct bufferevent_private bev;
     84  1.1  christos 	struct event_overlapped connect_overlapped;
     85  1.1  christos 	struct event_overlapped read_overlapped;
     86  1.1  christos 	struct event_overlapped write_overlapped;
     87  1.1  christos 	size_t read_in_progress;
     88  1.1  christos 	size_t write_in_progress;
     89  1.1  christos 	unsigned ok : 1;
     90  1.1  christos 	unsigned read_added : 1;
     91  1.1  christos 	unsigned write_added : 1;
     92  1.1  christos };
     93  1.1  christos 
     94  1.1  christos const struct bufferevent_ops bufferevent_ops_async = {
     95  1.1  christos 	"socket_async",
     96  1.1  christos 	evutil_offsetof(struct bufferevent_async, bev.bev),
     97  1.1  christos 	be_async_enable,
     98  1.1  christos 	be_async_disable,
     99  1.2  christos 	NULL, /* Unlink */
    100  1.1  christos 	be_async_destruct,
    101  1.1  christos 	bufferevent_generic_adj_timeouts_,
    102  1.1  christos 	be_async_flush,
    103  1.1  christos 	be_async_ctrl,
    104  1.1  christos };
    105  1.1  christos 
    106  1.7  christos static inline void
    107  1.7  christos be_async_run_eventcb(struct bufferevent *bev, short what, int options)
    108  1.7  christos { bufferevent_run_eventcb_(bev, what, options|BEV_TRIG_DEFER_CALLBACKS); }
    109  1.7  christos 
    110  1.7  christos static inline void
    111  1.7  christos be_async_trigger_nolock(struct bufferevent *bev, short what, int options)
    112  1.7  christos { bufferevent_trigger_nolock_(bev, what, options|BEV_TRIG_DEFER_CALLBACKS); }
    113  1.7  christos 
    114  1.7  christos static inline int
    115  1.7  christos fatal_error(int err)
    116  1.7  christos {
    117  1.7  christos 	switch (err) {
    118  1.7  christos 		/* We may have already associated this fd with a port.
    119  1.7  christos 		 * Let's hope it's this port, and that the error code
    120  1.7  christos 		 * for doing this neer changes. */
    121  1.7  christos 		case ERROR_INVALID_PARAMETER:
    122  1.7  christos 			return 0;
    123  1.7  christos 	}
    124  1.7  christos 	return 1;
    125  1.7  christos }
    126  1.7  christos 
    127  1.1  christos static inline struct bufferevent_async *
    128  1.1  christos upcast(struct bufferevent *bev)
    129  1.1  christos {
    130  1.1  christos 	struct bufferevent_async *bev_a;
    131  1.7  christos 	if (!BEV_IS_ASYNC(bev))
    132  1.1  christos 		return NULL;
    133  1.1  christos 	bev_a = EVUTIL_UPCAST(bev, struct bufferevent_async, bev.bev);
    134  1.1  christos 	return bev_a;
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos static inline struct bufferevent_async *
    138  1.1  christos upcast_connect(struct event_overlapped *eo)
    139  1.1  christos {
    140  1.1  christos 	struct bufferevent_async *bev_a;
    141  1.1  christos 	bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, connect_overlapped);
    142  1.1  christos 	EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
    143  1.1  christos 	return bev_a;
    144  1.1  christos }
    145  1.1  christos 
    146  1.1  christos static inline struct bufferevent_async *
    147  1.1  christos upcast_read(struct event_overlapped *eo)
    148  1.1  christos {
    149  1.1  christos 	struct bufferevent_async *bev_a;
    150  1.1  christos 	bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, read_overlapped);
    151  1.1  christos 	EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
    152  1.1  christos 	return bev_a;
    153  1.1  christos }
    154  1.1  christos 
    155  1.1  christos static inline struct bufferevent_async *
    156  1.1  christos upcast_write(struct event_overlapped *eo)
    157  1.1  christos {
    158  1.1  christos 	struct bufferevent_async *bev_a;
    159  1.1  christos 	bev_a = EVUTIL_UPCAST(eo, struct bufferevent_async, write_overlapped);
    160  1.1  christos 	EVUTIL_ASSERT(BEV_IS_ASYNC(&bev_a->bev.bev));
    161  1.1  christos 	return bev_a;
    162  1.1  christos }
    163  1.1  christos 
    164  1.1  christos static void
    165  1.1  christos bev_async_del_write(struct bufferevent_async *beva)
    166  1.1  christos {
    167  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    168  1.1  christos 
    169  1.1  christos 	if (beva->write_added) {
    170  1.1  christos 		beva->write_added = 0;
    171  1.1  christos 		event_base_del_virtual_(bev->ev_base);
    172  1.1  christos 	}
    173  1.1  christos }
    174  1.1  christos 
    175  1.1  christos static void
    176  1.1  christos bev_async_del_read(struct bufferevent_async *beva)
    177  1.1  christos {
    178  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    179  1.1  christos 
    180  1.1  christos 	if (beva->read_added) {
    181  1.1  christos 		beva->read_added = 0;
    182  1.1  christos 		event_base_del_virtual_(bev->ev_base);
    183  1.1  christos 	}
    184  1.1  christos }
    185  1.1  christos 
    186  1.1  christos static void
    187  1.1  christos bev_async_add_write(struct bufferevent_async *beva)
    188  1.1  christos {
    189  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    190  1.1  christos 
    191  1.1  christos 	if (!beva->write_added) {
    192  1.1  christos 		beva->write_added = 1;
    193  1.1  christos 		event_base_add_virtual_(bev->ev_base);
    194  1.1  christos 	}
    195  1.1  christos }
    196  1.1  christos 
    197  1.1  christos static void
    198  1.1  christos bev_async_add_read(struct bufferevent_async *beva)
    199  1.1  christos {
    200  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    201  1.1  christos 
    202  1.1  christos 	if (!beva->read_added) {
    203  1.1  christos 		beva->read_added = 1;
    204  1.1  christos 		event_base_add_virtual_(bev->ev_base);
    205  1.1  christos 	}
    206  1.1  christos }
    207  1.1  christos 
    208  1.1  christos static void
    209  1.1  christos bev_async_consider_writing(struct bufferevent_async *beva)
    210  1.1  christos {
    211  1.1  christos 	size_t at_most;
    212  1.1  christos 	int limit;
    213  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    214  1.1  christos 
    215  1.1  christos 	/* Don't write if there's a write in progress, or we do not
    216  1.1  christos 	 * want to write, or when there's nothing left to write. */
    217  1.1  christos 	if (beva->write_in_progress || beva->bev.connecting)
    218  1.1  christos 		return;
    219  1.1  christos 	if (!beva->ok || !(bev->enabled&EV_WRITE) ||
    220  1.1  christos 	    !evbuffer_get_length(bev->output)) {
    221  1.1  christos 		bev_async_del_write(beva);
    222  1.1  christos 		return;
    223  1.1  christos 	}
    224  1.1  christos 
    225  1.1  christos 	at_most = evbuffer_get_length(bev->output);
    226  1.1  christos 
    227  1.1  christos 	/* This is safe so long as bufferevent_get_write_max never returns
    228  1.1  christos 	 * more than INT_MAX.  That's true for now. XXXX */
    229  1.1  christos 	limit = (int)bufferevent_get_write_max_(&beva->bev);
    230  1.1  christos 	if (at_most >= (size_t)limit && limit >= 0)
    231  1.1  christos 		at_most = limit;
    232  1.1  christos 
    233  1.1  christos 	if (beva->bev.write_suspended) {
    234  1.1  christos 		bev_async_del_write(beva);
    235  1.1  christos 		return;
    236  1.1  christos 	}
    237  1.1  christos 
    238  1.1  christos 	/*  XXXX doesn't respect low-water mark very well. */
    239  1.1  christos 	bufferevent_incref_(bev);
    240  1.1  christos 	if (evbuffer_launch_write_(bev->output, at_most,
    241  1.1  christos 	    &beva->write_overlapped)) {
    242  1.1  christos 		bufferevent_decref_(bev);
    243  1.1  christos 		beva->ok = 0;
    244  1.7  christos 		be_async_run_eventcb(bev, BEV_EVENT_ERROR, 0);
    245  1.1  christos 	} else {
    246  1.1  christos 		beva->write_in_progress = at_most;
    247  1.1  christos 		bufferevent_decrement_write_buckets_(&beva->bev, at_most);
    248  1.1  christos 		bev_async_add_write(beva);
    249  1.1  christos 	}
    250  1.1  christos }
    251  1.1  christos 
    252  1.1  christos static void
    253  1.1  christos bev_async_consider_reading(struct bufferevent_async *beva)
    254  1.1  christos {
    255  1.1  christos 	size_t cur_size;
    256  1.1  christos 	size_t read_high;
    257  1.1  christos 	size_t at_most;
    258  1.1  christos 	int limit;
    259  1.1  christos 	struct bufferevent *bev = &beva->bev.bev;
    260  1.1  christos 
    261  1.1  christos 	/* Don't read if there is a read in progress, or we do not
    262  1.1  christos 	 * want to read. */
    263  1.1  christos 	if (beva->read_in_progress || beva->bev.connecting)
    264  1.1  christos 		return;
    265  1.1  christos 	if (!beva->ok || !(bev->enabled&EV_READ)) {
    266  1.1  christos 		bev_async_del_read(beva);
    267  1.1  christos 		return;
    268  1.1  christos 	}
    269  1.1  christos 
    270  1.1  christos 	/* Don't read if we're full */
    271  1.1  christos 	cur_size = evbuffer_get_length(bev->input);
    272  1.1  christos 	read_high = bev->wm_read.high;
    273  1.1  christos 	if (read_high) {
    274  1.1  christos 		if (cur_size >= read_high) {
    275  1.1  christos 			bev_async_del_read(beva);
    276  1.1  christos 			return;
    277  1.1  christos 		}
    278  1.1  christos 		at_most = read_high - cur_size;
    279  1.1  christos 	} else {
    280  1.1  christos 		at_most = 16384; /* FIXME totally magic. */
    281  1.1  christos 	}
    282  1.1  christos 
    283  1.1  christos 	/* XXXX This over-commits. */
    284  1.1  christos 	/* XXXX see also not above on cast on bufferevent_get_write_max_() */
    285  1.1  christos 	limit = (int)bufferevent_get_read_max_(&beva->bev);
    286  1.1  christos 	if (at_most >= (size_t)limit && limit >= 0)
    287  1.1  christos 		at_most = limit;
    288  1.1  christos 
    289  1.1  christos 	if (beva->bev.read_suspended) {
    290  1.1  christos 		bev_async_del_read(beva);
    291  1.1  christos 		return;
    292  1.1  christos 	}
    293  1.1  christos 
    294  1.1  christos 	bufferevent_incref_(bev);
    295  1.1  christos 	if (evbuffer_launch_read_(bev->input, at_most, &beva->read_overlapped)) {
    296  1.1  christos 		beva->ok = 0;
    297  1.7  christos 		be_async_run_eventcb(bev, BEV_EVENT_ERROR, 0);
    298  1.1  christos 		bufferevent_decref_(bev);
    299  1.1  christos 	} else {
    300  1.1  christos 		beva->read_in_progress = at_most;
    301  1.1  christos 		bufferevent_decrement_read_buckets_(&beva->bev, at_most);
    302  1.1  christos 		bev_async_add_read(beva);
    303  1.1  christos 	}
    304  1.1  christos 
    305  1.1  christos 	return;
    306  1.1  christos }
    307  1.1  christos 
    308  1.1  christos static void
    309  1.1  christos be_async_outbuf_callback(struct evbuffer *buf,
    310  1.1  christos     const struct evbuffer_cb_info *cbinfo,
    311  1.1  christos     void *arg)
    312  1.1  christos {
    313  1.1  christos 	struct bufferevent *bev = arg;
    314  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    315  1.1  christos 
    316  1.1  christos 	/* If we added data to the outbuf and were not writing before,
    317  1.1  christos 	 * we may want to write now. */
    318  1.1  christos 
    319  1.1  christos 	bufferevent_incref_and_lock_(bev);
    320  1.1  christos 
    321  1.1  christos 	if (cbinfo->n_added)
    322  1.1  christos 		bev_async_consider_writing(bev_async);
    323  1.1  christos 
    324  1.1  christos 	bufferevent_decref_and_unlock_(bev);
    325  1.1  christos }
    326  1.1  christos 
    327  1.1  christos static void
    328  1.1  christos be_async_inbuf_callback(struct evbuffer *buf,
    329  1.1  christos     const struct evbuffer_cb_info *cbinfo,
    330  1.1  christos     void *arg)
    331  1.1  christos {
    332  1.1  christos 	struct bufferevent *bev = arg;
    333  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    334  1.1  christos 
    335  1.1  christos 	/* If we drained data from the inbuf and were not reading before,
    336  1.1  christos 	 * we may want to read now */
    337  1.1  christos 
    338  1.1  christos 	bufferevent_incref_and_lock_(bev);
    339  1.1  christos 
    340  1.1  christos 	if (cbinfo->n_deleted)
    341  1.1  christos 		bev_async_consider_reading(bev_async);
    342  1.1  christos 
    343  1.1  christos 	bufferevent_decref_and_unlock_(bev);
    344  1.1  christos }
    345  1.1  christos 
    346  1.1  christos static int
    347  1.1  christos be_async_enable(struct bufferevent *buf, short what)
    348  1.1  christos {
    349  1.1  christos 	struct bufferevent_async *bev_async = upcast(buf);
    350  1.1  christos 
    351  1.1  christos 	if (!bev_async->ok)
    352  1.1  christos 		return -1;
    353  1.1  christos 
    354  1.1  christos 	if (bev_async->bev.connecting) {
    355  1.1  christos 		/* Don't launch anything during connection attempts. */
    356  1.1  christos 		return 0;
    357  1.1  christos 	}
    358  1.1  christos 
    359  1.1  christos 	if (what & EV_READ)
    360  1.1  christos 		BEV_RESET_GENERIC_READ_TIMEOUT(buf);
    361  1.1  christos 	if (what & EV_WRITE)
    362  1.1  christos 		BEV_RESET_GENERIC_WRITE_TIMEOUT(buf);
    363  1.1  christos 
    364  1.1  christos 	/* If we newly enable reading or writing, and we aren't reading or
    365  1.1  christos 	   writing already, consider launching a new read or write. */
    366  1.1  christos 
    367  1.1  christos 	if (what & EV_READ)
    368  1.1  christos 		bev_async_consider_reading(bev_async);
    369  1.1  christos 	if (what & EV_WRITE)
    370  1.1  christos 		bev_async_consider_writing(bev_async);
    371  1.1  christos 	return 0;
    372  1.1  christos }
    373  1.1  christos 
    374  1.1  christos static int
    375  1.1  christos be_async_disable(struct bufferevent *bev, short what)
    376  1.1  christos {
    377  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    378  1.1  christos 	/* XXXX If we disable reading or writing, we may want to consider
    379  1.1  christos 	 * canceling any in-progress read or write operation, though it might
    380  1.1  christos 	 * not work. */
    381  1.1  christos 
    382  1.1  christos 	if (what & EV_READ) {
    383  1.1  christos 		BEV_DEL_GENERIC_READ_TIMEOUT(bev);
    384  1.1  christos 		bev_async_del_read(bev_async);
    385  1.1  christos 	}
    386  1.1  christos 	if (what & EV_WRITE) {
    387  1.1  christos 		BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
    388  1.1  christos 		bev_async_del_write(bev_async);
    389  1.1  christos 	}
    390  1.1  christos 
    391  1.1  christos 	return 0;
    392  1.1  christos }
    393  1.1  christos 
    394  1.1  christos static void
    395  1.1  christos be_async_destruct(struct bufferevent *bev)
    396  1.1  christos {
    397  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    398  1.1  christos 	struct bufferevent_private *bev_p = BEV_UPCAST(bev);
    399  1.1  christos 	evutil_socket_t fd;
    400  1.1  christos 
    401  1.1  christos 	EVUTIL_ASSERT(!upcast(bev)->write_in_progress &&
    402  1.1  christos 			!upcast(bev)->read_in_progress);
    403  1.1  christos 
    404  1.1  christos 	bev_async_del_read(bev_async);
    405  1.1  christos 	bev_async_del_write(bev_async);
    406  1.1  christos 
    407  1.1  christos 	fd = evbuffer_overlapped_get_fd_(bev->input);
    408  1.7  christos 	if (fd != (evutil_socket_t)EVUTIL_INVALID_SOCKET &&
    409  1.3  christos 		(bev_p->options & BEV_OPT_CLOSE_ON_FREE)) {
    410  1.1  christos 		evutil_closesocket(fd);
    411  1.7  christos 		evbuffer_overlapped_set_fd_(bev->input, EVUTIL_INVALID_SOCKET);
    412  1.1  christos 	}
    413  1.1  christos }
    414  1.1  christos 
    415  1.1  christos /* GetQueuedCompletionStatus doesn't reliably yield WSA error codes, so
    416  1.1  christos  * we use WSAGetOverlappedResult to translate. */
    417  1.1  christos static void
    418  1.1  christos bev_async_set_wsa_error(struct bufferevent *bev, struct event_overlapped *eo)
    419  1.1  christos {
    420  1.1  christos 	DWORD bytes, flags;
    421  1.1  christos 	evutil_socket_t fd;
    422  1.1  christos 
    423  1.1  christos 	fd = evbuffer_overlapped_get_fd_(bev->input);
    424  1.1  christos 	WSAGetOverlappedResult(fd, &eo->overlapped, &bytes, FALSE, &flags);
    425  1.1  christos }
    426  1.1  christos 
    427  1.1  christos static int
    428  1.1  christos be_async_flush(struct bufferevent *bev, short what,
    429  1.1  christos     enum bufferevent_flush_mode mode)
    430  1.1  christos {
    431  1.1  christos 	return 0;
    432  1.1  christos }
    433  1.1  christos 
    434  1.1  christos static void
    435  1.1  christos connect_complete(struct event_overlapped *eo, ev_uintptr_t key,
    436  1.1  christos     ev_ssize_t nbytes, int ok)
    437  1.1  christos {
    438  1.1  christos 	struct bufferevent_async *bev_a = upcast_connect(eo);
    439  1.1  christos 	struct bufferevent *bev = &bev_a->bev.bev;
    440  1.1  christos 	evutil_socket_t sock;
    441  1.1  christos 
    442  1.1  christos 	BEV_LOCK(bev);
    443  1.1  christos 
    444  1.1  christos 	EVUTIL_ASSERT(bev_a->bev.connecting);
    445  1.1  christos 	bev_a->bev.connecting = 0;
    446  1.1  christos 	sock = evbuffer_overlapped_get_fd_(bev_a->bev.bev.input);
    447  1.1  christos 	/* XXXX Handle error? */
    448  1.1  christos 	setsockopt(sock, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
    449  1.1  christos 
    450  1.1  christos 	if (ok)
    451  1.1  christos 		bufferevent_async_set_connected_(bev);
    452  1.1  christos 	else
    453  1.1  christos 		bev_async_set_wsa_error(bev, eo);
    454  1.1  christos 
    455  1.7  christos 	be_async_run_eventcb(bev, ok ? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR, 0);
    456  1.1  christos 
    457  1.1  christos 	event_base_del_virtual_(bev->ev_base);
    458  1.1  christos 
    459  1.1  christos 	bufferevent_decref_and_unlock_(bev);
    460  1.1  christos }
    461  1.1  christos 
    462  1.1  christos static void
    463  1.1  christos read_complete(struct event_overlapped *eo, ev_uintptr_t key,
    464  1.1  christos     ev_ssize_t nbytes, int ok)
    465  1.1  christos {
    466  1.1  christos 	struct bufferevent_async *bev_a = upcast_read(eo);
    467  1.1  christos 	struct bufferevent *bev = &bev_a->bev.bev;
    468  1.1  christos 	short what = BEV_EVENT_READING;
    469  1.1  christos 	ev_ssize_t amount_unread;
    470  1.1  christos 	BEV_LOCK(bev);
    471  1.1  christos 	EVUTIL_ASSERT(bev_a->read_in_progress);
    472  1.1  christos 
    473  1.1  christos 	amount_unread = bev_a->read_in_progress - nbytes;
    474  1.1  christos 	evbuffer_commit_read_(bev->input, nbytes);
    475  1.1  christos 	bev_a->read_in_progress = 0;
    476  1.1  christos 	if (amount_unread)
    477  1.1  christos 		bufferevent_decrement_read_buckets_(&bev_a->bev, -amount_unread);
    478  1.1  christos 
    479  1.1  christos 	if (!ok)
    480  1.1  christos 		bev_async_set_wsa_error(bev, eo);
    481  1.1  christos 
    482  1.1  christos 	if (bev_a->ok) {
    483  1.1  christos 		if (ok && nbytes) {
    484  1.1  christos 			BEV_RESET_GENERIC_READ_TIMEOUT(bev);
    485  1.7  christos 			be_async_trigger_nolock(bev, EV_READ, 0);
    486  1.1  christos 			bev_async_consider_reading(bev_a);
    487  1.1  christos 		} else if (!ok) {
    488  1.1  christos 			what |= BEV_EVENT_ERROR;
    489  1.1  christos 			bev_a->ok = 0;
    490  1.7  christos 			be_async_run_eventcb(bev, what, 0);
    491  1.1  christos 		} else if (!nbytes) {
    492  1.1  christos 			what |= BEV_EVENT_EOF;
    493  1.1  christos 			bev_a->ok = 0;
    494  1.7  christos 			be_async_run_eventcb(bev, what, 0);
    495  1.1  christos 		}
    496  1.1  christos 	}
    497  1.1  christos 
    498  1.1  christos 	bufferevent_decref_and_unlock_(bev);
    499  1.1  christos }
    500  1.1  christos 
    501  1.1  christos static void
    502  1.1  christos write_complete(struct event_overlapped *eo, ev_uintptr_t key,
    503  1.1  christos     ev_ssize_t nbytes, int ok)
    504  1.1  christos {
    505  1.1  christos 	struct bufferevent_async *bev_a = upcast_write(eo);
    506  1.1  christos 	struct bufferevent *bev = &bev_a->bev.bev;
    507  1.1  christos 	short what = BEV_EVENT_WRITING;
    508  1.1  christos 	ev_ssize_t amount_unwritten;
    509  1.1  christos 
    510  1.1  christos 	BEV_LOCK(bev);
    511  1.1  christos 	EVUTIL_ASSERT(bev_a->write_in_progress);
    512  1.1  christos 
    513  1.1  christos 	amount_unwritten = bev_a->write_in_progress - nbytes;
    514  1.1  christos 	evbuffer_commit_write_(bev->output, nbytes);
    515  1.1  christos 	bev_a->write_in_progress = 0;
    516  1.1  christos 
    517  1.1  christos 	if (amount_unwritten)
    518  1.1  christos 		bufferevent_decrement_write_buckets_(&bev_a->bev,
    519  1.1  christos 		                                     -amount_unwritten);
    520  1.1  christos 
    521  1.1  christos 
    522  1.1  christos 	if (!ok)
    523  1.1  christos 		bev_async_set_wsa_error(bev, eo);
    524  1.1  christos 
    525  1.1  christos 	if (bev_a->ok) {
    526  1.1  christos 		if (ok && nbytes) {
    527  1.1  christos 			BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
    528  1.7  christos 			be_async_trigger_nolock(bev, EV_WRITE, 0);
    529  1.1  christos 			bev_async_consider_writing(bev_a);
    530  1.1  christos 		} else if (!ok) {
    531  1.1  christos 			what |= BEV_EVENT_ERROR;
    532  1.1  christos 			bev_a->ok = 0;
    533  1.7  christos 			be_async_run_eventcb(bev, what, 0);
    534  1.1  christos 		} else if (!nbytes) {
    535  1.1  christos 			what |= BEV_EVENT_EOF;
    536  1.1  christos 			bev_a->ok = 0;
    537  1.7  christos 			be_async_run_eventcb(bev, what, 0);
    538  1.1  christos 		}
    539  1.1  christos 	}
    540  1.1  christos 
    541  1.1  christos 	bufferevent_decref_and_unlock_(bev);
    542  1.1  christos }
    543  1.1  christos 
    544  1.1  christos struct bufferevent *
    545  1.1  christos bufferevent_async_new_(struct event_base *base,
    546  1.1  christos     evutil_socket_t fd, int options)
    547  1.1  christos {
    548  1.1  christos 	struct bufferevent_async *bev_a;
    549  1.1  christos 	struct bufferevent *bev;
    550  1.1  christos 	struct event_iocp_port *iocp;
    551  1.1  christos 
    552  1.1  christos 	options |= BEV_OPT_THREADSAFE;
    553  1.1  christos 
    554  1.1  christos 	if (!(iocp = event_base_get_iocp_(base)))
    555  1.1  christos 		return NULL;
    556  1.1  christos 
    557  1.1  christos 	if (fd >= 0 && event_iocp_port_associate_(iocp, fd, 1)<0) {
    558  1.7  christos 		if (fatal_error(GetLastError()))
    559  1.1  christos 			return NULL;
    560  1.1  christos 	}
    561  1.1  christos 
    562  1.1  christos 	if (!(bev_a = mm_calloc(1, sizeof(struct bufferevent_async))))
    563  1.1  christos 		return NULL;
    564  1.1  christos 
    565  1.1  christos 	bev = &bev_a->bev.bev;
    566  1.1  christos 	if (!(bev->input = evbuffer_overlapped_new_(fd))) {
    567  1.1  christos 		mm_free(bev_a);
    568  1.1  christos 		return NULL;
    569  1.1  christos 	}
    570  1.1  christos 	if (!(bev->output = evbuffer_overlapped_new_(fd))) {
    571  1.1  christos 		evbuffer_free(bev->input);
    572  1.1  christos 		mm_free(bev_a);
    573  1.1  christos 		return NULL;
    574  1.1  christos 	}
    575  1.1  christos 
    576  1.1  christos 	if (bufferevent_init_common_(&bev_a->bev, base, &bufferevent_ops_async,
    577  1.1  christos 		options)<0)
    578  1.1  christos 		goto err;
    579  1.1  christos 
    580  1.1  christos 	evbuffer_add_cb(bev->input, be_async_inbuf_callback, bev);
    581  1.1  christos 	evbuffer_add_cb(bev->output, be_async_outbuf_callback, bev);
    582  1.1  christos 
    583  1.1  christos 	event_overlapped_init_(&bev_a->connect_overlapped, connect_complete);
    584  1.1  christos 	event_overlapped_init_(&bev_a->read_overlapped, read_complete);
    585  1.1  christos 	event_overlapped_init_(&bev_a->write_overlapped, write_complete);
    586  1.1  christos 
    587  1.3  christos 	bufferevent_init_generic_timeout_cbs_(bev);
    588  1.3  christos 
    589  1.1  christos 	bev_a->ok = fd >= 0;
    590  1.1  christos 
    591  1.1  christos 	return bev;
    592  1.1  christos err:
    593  1.1  christos 	bufferevent_free(&bev_a->bev.bev);
    594  1.1  christos 	return NULL;
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos void
    598  1.1  christos bufferevent_async_set_connected_(struct bufferevent *bev)
    599  1.1  christos {
    600  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    601  1.1  christos 	bev_async->ok = 1;
    602  1.1  christos 	/* Now's a good time to consider reading/writing */
    603  1.1  christos 	be_async_enable(bev, bev->enabled);
    604  1.1  christos }
    605  1.1  christos 
    606  1.1  christos int
    607  1.1  christos bufferevent_async_can_connect_(struct bufferevent *bev)
    608  1.1  christos {
    609  1.1  christos 	const struct win32_extension_fns *ext =
    610  1.1  christos 	    event_get_win32_extension_fns_();
    611  1.1  christos 
    612  1.1  christos 	if (BEV_IS_ASYNC(bev) &&
    613  1.1  christos 	    event_base_get_iocp_(bev->ev_base) &&
    614  1.1  christos 	    ext && ext->ConnectEx)
    615  1.1  christos 		return 1;
    616  1.1  christos 
    617  1.1  christos 	return 0;
    618  1.1  christos }
    619  1.1  christos 
    620  1.1  christos int
    621  1.1  christos bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
    622  1.1  christos 	const struct sockaddr *sa, int socklen)
    623  1.1  christos {
    624  1.1  christos 	BOOL rc;
    625  1.1  christos 	struct bufferevent_async *bev_async = upcast(bev);
    626  1.1  christos 	struct sockaddr_storage ss;
    627  1.1  christos 	const struct win32_extension_fns *ext =
    628  1.1  christos 	    event_get_win32_extension_fns_();
    629  1.1  christos 
    630  1.1  christos 	EVUTIL_ASSERT(ext && ext->ConnectEx && fd >= 0 && sa != NULL);
    631  1.1  christos 
    632  1.1  christos 	/* ConnectEx() requires that the socket be bound to an address
    633  1.1  christos 	 * with bind() before using, otherwise it will fail. We attempt
    634  1.1  christos 	 * to issue a bind() here, taking into account that the error
    635  1.1  christos 	 * code is set to WSAEINVAL when the socket is already bound. */
    636  1.1  christos 	memset(&ss, 0, sizeof(ss));
    637  1.1  christos 	if (sa->sa_family == AF_INET) {
    638  1.1  christos 		struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
    639  1.1  christos 		sin->sin_family = AF_INET;
    640  1.1  christos 		sin->sin_addr.s_addr = INADDR_ANY;
    641  1.1  christos 	} else if (sa->sa_family == AF_INET6) {
    642  1.1  christos 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
    643  1.1  christos 		sin6->sin6_family = AF_INET6;
    644  1.1  christos 		sin6->sin6_addr = in6addr_any;
    645  1.1  christos 	} else {
    646  1.1  christos 		/* Well, the user will have to bind() */
    647  1.1  christos 		return -1;
    648  1.1  christos 	}
    649  1.1  christos 	if (bind(fd, (struct sockaddr *)&ss, sizeof(ss)) < 0 &&
    650  1.1  christos 	    WSAGetLastError() != WSAEINVAL)
    651  1.1  christos 		return -1;
    652  1.1  christos 
    653  1.1  christos 	event_base_add_virtual_(bev->ev_base);
    654  1.1  christos 	bufferevent_incref_(bev);
    655  1.1  christos 	rc = ext->ConnectEx(fd, sa, socklen, NULL, 0, NULL,
    656  1.1  christos 			    &bev_async->connect_overlapped.overlapped);
    657  1.1  christos 	if (rc || WSAGetLastError() == ERROR_IO_PENDING)
    658  1.1  christos 		return 0;
    659  1.1  christos 
    660  1.1  christos 	event_base_del_virtual_(bev->ev_base);
    661  1.1  christos 	bufferevent_decref_(bev);
    662  1.1  christos 
    663  1.1  christos 	return -1;
    664  1.1  christos }
    665  1.1  christos 
    666  1.1  christos static int
    667  1.1  christos be_async_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
    668  1.1  christos     union bufferevent_ctrl_data *data)
    669  1.1  christos {
    670  1.1  christos 	switch (op) {
    671  1.1  christos 	case BEV_CTRL_GET_FD:
    672  1.1  christos 		data->fd = evbuffer_overlapped_get_fd_(bev->input);
    673  1.1  christos 		return 0;
    674  1.1  christos 	case BEV_CTRL_SET_FD: {
    675  1.7  christos 		struct bufferevent_async *bev_a = upcast(bev);
    676  1.1  christos 		struct event_iocp_port *iocp;
    677  1.1  christos 
    678  1.1  christos 		if (data->fd == evbuffer_overlapped_get_fd_(bev->input))
    679  1.1  christos 			return 0;
    680  1.1  christos 		if (!(iocp = event_base_get_iocp_(bev->ev_base)))
    681  1.1  christos 			return -1;
    682  1.7  christos 		if (event_iocp_port_associate_(iocp, data->fd, 1) < 0) {
    683  1.7  christos 			if (fatal_error(GetLastError()))
    684  1.7  christos 				return -1;
    685  1.7  christos 		}
    686  1.1  christos 		evbuffer_overlapped_set_fd_(bev->input, data->fd);
    687  1.1  christos 		evbuffer_overlapped_set_fd_(bev->output, data->fd);
    688  1.7  christos 		bev_a->ok = data->fd >= 0;
    689  1.1  christos 		return 0;
    690  1.1  christos 	}
    691  1.1  christos 	case BEV_CTRL_CANCEL_ALL: {
    692  1.1  christos 		struct bufferevent_async *bev_a = upcast(bev);
    693  1.1  christos 		evutil_socket_t fd = evbuffer_overlapped_get_fd_(bev->input);
    694  1.7  christos 		if (fd != (evutil_socket_t)EVUTIL_INVALID_SOCKET &&
    695  1.1  christos 		    (bev_a->bev.options & BEV_OPT_CLOSE_ON_FREE)) {
    696  1.1  christos 			closesocket(fd);
    697  1.7  christos 			evbuffer_overlapped_set_fd_(bev->input, EVUTIL_INVALID_SOCKET);
    698  1.1  christos 		}
    699  1.1  christos 		bev_a->ok = 0;
    700  1.1  christos 		return 0;
    701  1.1  christos 	}
    702  1.1  christos 	case BEV_CTRL_GET_UNDERLYING:
    703  1.1  christos 	default:
    704  1.1  christos 		return -1;
    705  1.1  christos 	}
    706  1.1  christos }
    707  1.1  christos 
    708  1.1  christos 
    709