2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "event2/event-config.h"
30 #include "evconfig-private.h"
32 #include <sys/types.h>
34 #ifdef EVENT__HAVE_SYS_TIME_H
42 #ifdef EVENT__HAVE_STDARG_H
45 #ifdef EVENT__HAVE_UNISTD_H
54 #ifdef EVENT__HAVE_SYS_SOCKET_H
55 #include <sys/socket.h>
57 #ifdef EVENT__HAVE_NETINET_IN_H
58 #include <netinet/in.h>
60 #ifdef EVENT__HAVE_NETINET_IN6_H
61 #include <netinet/in6.h>
64 #include "event2/util.h"
65 #include "event2/bufferevent.h"
66 #include "event2/buffer.h"
67 #include "event2/bufferevent_struct.h"
68 #include "event2/bufferevent_compat.h"
69 #include "event2/event.h"
70 #include "log-internal.h"
71 #include "mm-internal.h"
72 #include "bufferevent-internal.h"
73 #include "util-internal.h"
75 #include "iocp-internal.h"
79 static int be_socket_enable(struct bufferevent
*, short);
80 static int be_socket_disable(struct bufferevent
*, short);
81 static void be_socket_destruct(struct bufferevent
*);
82 static int be_socket_flush(struct bufferevent
*, short, enum bufferevent_flush_mode
);
83 static int be_socket_ctrl(struct bufferevent
*, enum bufferevent_ctrl_op
, union bufferevent_ctrl_data
*);
85 static void be_socket_setfd(struct bufferevent
*, evutil_socket_t
);
87 const struct bufferevent_ops bufferevent_ops_socket
= {
89 evutil_offsetof(struct bufferevent_private
, bev
),
94 bufferevent_generic_adj_existing_timeouts_
,
99 const struct sockaddr
*
100 bufferevent_socket_get_conn_address_(struct bufferevent
*bev
)
102 struct bufferevent_private
*bev_p
=
103 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
105 return (struct sockaddr
*)&bev_p
->conn_address
;
108 bufferevent_socket_set_conn_address_fd(struct bufferevent_private
*bev_p
, int fd
)
110 socklen_t len
= sizeof(bev_p
->conn_address
);
112 struct sockaddr
*addr
= (struct sockaddr
*)&bev_p
->conn_address
;
113 if (addr
->sa_family
!= AF_UNSPEC
)
114 getpeername(fd
, addr
, &len
);
117 bufferevent_socket_set_conn_address(struct bufferevent_private
*bev_p
,
118 struct sockaddr
*addr
, size_t addrlen
)
120 EVUTIL_ASSERT(addrlen
<= sizeof(bev_p
->conn_address
));
121 memcpy(&bev_p
->conn_address
, addr
, addrlen
);
125 bufferevent_socket_outbuf_cb(struct evbuffer
*buf
,
126 const struct evbuffer_cb_info
*cbinfo
,
129 struct bufferevent
*bufev
= arg
;
130 struct bufferevent_private
*bufev_p
=
131 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
133 if (cbinfo
->n_added
&&
134 (bufev
->enabled
& EV_WRITE
) &&
135 !event_pending(&bufev
->ev_write
, EV_WRITE
, NULL
) &&
136 !bufev_p
->write_suspended
) {
137 /* Somebody added data to the buffer, and we would like to
138 * write, and we were not writing. So, start writing. */
139 if (bufferevent_add_event_(&bufev
->ev_write
, &bufev
->timeout_write
) == -1) {
140 /* Should we log this? */
146 bufferevent_readcb(evutil_socket_t fd
, short event
, void *arg
)
148 struct bufferevent
*bufev
= arg
;
149 struct bufferevent_private
*bufev_p
=
150 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
151 struct evbuffer
*input
;
153 short what
= BEV_EVENT_READING
;
154 ev_ssize_t howmuch
= -1, readmax
=-1;
156 bufferevent_incref_and_lock_(bufev
);
158 if (event
== EV_TIMEOUT
) {
159 /* Note that we only check for event==EV_TIMEOUT. If
160 * event==EV_TIMEOUT|EV_READ, we can safely ignore the
161 * timeout, since a read has occurred */
162 what
|= BEV_EVENT_TIMEOUT
;
166 input
= bufev
->input
;
169 * If we have a high watermark configured then we don't want to
170 * read more data than would make us reach the watermark.
172 if (bufev
->wm_read
.high
!= 0) {
173 howmuch
= bufev
->wm_read
.high
- evbuffer_get_length(input
);
174 /* we somehow lowered the watermark, stop reading */
176 bufferevent_wm_suspend_read(bufev
);
180 readmax
= bufferevent_get_read_max_(bufev_p
);
181 if (howmuch
< 0 || howmuch
> readmax
) /* The use of -1 for "unlimited"
182 * uglifies this code. XXXX */
184 if (bufev_p
->read_suspended
)
187 evbuffer_unfreeze(input
, 0);
188 res
= evbuffer_read(input
, fd
, (int)howmuch
); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
189 evbuffer_freeze(input
, 0);
192 int err
= evutil_socket_geterror(fd
);
193 if (EVUTIL_ERR_RW_RETRIABLE(err
))
196 what
|= BEV_EVENT_ERROR
;
197 } else if (res
== 0) {
199 what
|= BEV_EVENT_EOF
;
205 bufferevent_decrement_read_buckets_(bufev_p
, res
);
207 /* Invoke the user callback - must always be called last */
208 bufferevent_trigger_nolock_(bufev
, EV_READ
, 0);
216 bufferevent_disable(bufev
, EV_READ
);
217 bufferevent_run_eventcb_(bufev
, what
, 0);
220 bufferevent_decref_and_unlock_(bufev
);
224 bufferevent_writecb(evutil_socket_t fd
, short event
, void *arg
)
226 struct bufferevent
*bufev
= arg
;
227 struct bufferevent_private
*bufev_p
=
228 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
230 short what
= BEV_EVENT_WRITING
;
232 ev_ssize_t atmost
= -1;
234 bufferevent_incref_and_lock_(bufev
);
236 if (event
== EV_TIMEOUT
) {
237 /* Note that we only check for event==EV_TIMEOUT. If
238 * event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
239 * timeout, since a read has occurred */
240 what
|= BEV_EVENT_TIMEOUT
;
243 if (bufev_p
->connecting
) {
244 int c
= evutil_socket_finished_connecting_(fd
);
245 /* we need to fake the error if the connection was refused
246 * immediately - usually connection to localhost on BSD */
247 if (bufev_p
->connection_refused
) {
248 bufev_p
->connection_refused
= 0;
255 bufev_p
->connecting
= 0;
257 event_del(&bufev
->ev_write
);
258 event_del(&bufev
->ev_read
);
259 bufferevent_run_eventcb_(bufev
, BEV_EVENT_ERROR
, 0);
263 bufferevent_socket_set_conn_address_fd(bufev_p
, fd
);
265 if (BEV_IS_ASYNC(bufev
)) {
266 event_del(&bufev
->ev_write
);
267 bufferevent_async_set_connected_(bufev
);
268 bufferevent_run_eventcb_(bufev
,
269 BEV_EVENT_CONNECTED
, 0);
273 bufferevent_run_eventcb_(bufev
,
274 BEV_EVENT_CONNECTED
, 0);
275 if (!(bufev
->enabled
& EV_WRITE
) ||
276 bufev_p
->write_suspended
) {
277 event_del(&bufev
->ev_write
);
283 atmost
= bufferevent_get_write_max_(bufev_p
);
285 if (bufev_p
->write_suspended
)
288 if (evbuffer_get_length(bufev
->output
)) {
289 evbuffer_unfreeze(bufev
->output
, 1);
290 res
= evbuffer_write_atmost(bufev
->output
, fd
, atmost
);
291 evbuffer_freeze(bufev
->output
, 1);
293 int err
= evutil_socket_geterror(fd
);
294 if (EVUTIL_ERR_RW_RETRIABLE(err
))
296 what
|= BEV_EVENT_ERROR
;
297 } else if (res
== 0) {
299 XXXX Actually, a 0 on write doesn't indicate
300 an EOF. An ECONNRESET might be more typical.
302 what
|= BEV_EVENT_EOF
;
307 bufferevent_decrement_write_buckets_(bufev_p
, res
);
310 if (evbuffer_get_length(bufev
->output
) == 0) {
311 event_del(&bufev
->ev_write
);
315 * Invoke the user callback if our buffer is drained or below the
318 if (res
|| !connected
) {
319 bufferevent_trigger_nolock_(bufev
, EV_WRITE
, 0);
325 if (evbuffer_get_length(bufev
->output
) == 0) {
326 event_del(&bufev
->ev_write
);
331 bufferevent_disable(bufev
, EV_WRITE
);
332 bufferevent_run_eventcb_(bufev
, what
, 0);
335 bufferevent_decref_and_unlock_(bufev
);
339 bufferevent_socket_new(struct event_base
*base
, evutil_socket_t fd
,
342 struct bufferevent_private
*bufev_p
;
343 struct bufferevent
*bufev
;
346 if (base
&& event_base_get_iocp_(base
))
347 return bufferevent_async_new_(base
, fd
, options
);
350 if ((bufev_p
= mm_calloc(1, sizeof(struct bufferevent_private
)))== NULL
)
353 if (bufferevent_init_common_(bufev_p
, base
, &bufferevent_ops_socket
,
358 bufev
= &bufev_p
->bev
;
359 evbuffer_set_flags(bufev
->output
, EVBUFFER_FLAG_DRAINS_TO_FD
);
361 event_assign(&bufev
->ev_read
, bufev
->ev_base
, fd
,
362 EV_READ
|EV_PERSIST
|EV_FINALIZE
, bufferevent_readcb
, bufev
);
363 event_assign(&bufev
->ev_write
, bufev
->ev_base
, fd
,
364 EV_WRITE
|EV_PERSIST
|EV_FINALIZE
, bufferevent_writecb
, bufev
);
366 evbuffer_add_cb(bufev
->output
, bufferevent_socket_outbuf_cb
, bufev
);
368 evbuffer_freeze(bufev
->input
, 0);
369 evbuffer_freeze(bufev
->output
, 1);
375 bufferevent_socket_connect(struct bufferevent
*bev
,
376 const struct sockaddr
*sa
, int socklen
)
378 struct bufferevent_private
*bufev_p
=
379 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
386 bufferevent_incref_and_lock_(bev
);
391 fd
= bufferevent_getfd(bev
);
395 fd
= evutil_socket_(sa
->sa_family
,
396 SOCK_STREAM
|EVUTIL_SOCK_NONBLOCK
, 0);
403 if (bufferevent_async_can_connect_(bev
)) {
404 bufferevent_setfd(bev
, fd
);
405 r
= bufferevent_async_connect_(bev
, fd
, sa
, socklen
);
408 bufev_p
->connecting
= 1;
413 r
= evutil_socket_connect_(&fd
, sa
, socklen
);
418 /* ConnectEx() isn't always around, even when IOCP is enabled.
419 * Here, we borrow the socket object's write handler to fall back
420 * on a non-blocking connect() when ConnectEx() is unavailable. */
421 if (BEV_IS_ASYNC(bev
)) {
422 event_assign(&bev
->ev_write
, bev
->ev_base
, fd
,
423 EV_WRITE
|EV_PERSIST
|EV_FINALIZE
, bufferevent_writecb
, bev
);
426 bufferevent_setfd(bev
, fd
);
428 if (! be_socket_enable(bev
, EV_WRITE
)) {
429 bufev_p
->connecting
= 1;
434 /* The connect succeeded already. How very BSD of it. */
436 bufev_p
->connecting
= 1;
437 event_active(&bev
->ev_write
, EV_WRITE
, 1);
439 /* The connect failed already. How very BSD of it. */
440 bufev_p
->connection_refused
= 1;
441 bufev_p
->connecting
= 1;
443 event_active(&bev
->ev_write
, EV_WRITE
, 1);
449 bufferevent_run_eventcb_(bev
, BEV_EVENT_ERROR
, 0);
451 evutil_closesocket(fd
);
452 /* do something about the error? */
454 bufferevent_decref_and_unlock_(bev
);
459 bufferevent_connect_getaddrinfo_cb(int result
, struct evutil_addrinfo
*ai
,
462 struct bufferevent
*bev
= arg
;
463 struct bufferevent_private
*bev_p
=
464 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
468 bufferevent_unsuspend_write_(bev
, BEV_SUSPEND_LOOKUP
);
469 bufferevent_unsuspend_read_(bev
, BEV_SUSPEND_LOOKUP
);
472 bev_p
->dns_error
= result
;
473 bufferevent_run_eventcb_(bev
, BEV_EVENT_ERROR
, 0);
474 bufferevent_decref_and_unlock_(bev
);
476 evutil_freeaddrinfo(ai
);
480 /* XXX use the other addrinfos? */
481 /* XXX use this return value */
482 bufferevent_socket_set_conn_address(bev_p
, ai
->ai_addr
, (int)ai
->ai_addrlen
);
483 r
= bufferevent_socket_connect(bev
, ai
->ai_addr
, (int)ai
->ai_addrlen
);
485 bufferevent_decref_and_unlock_(bev
);
486 evutil_freeaddrinfo(ai
);
490 bufferevent_socket_connect_hostname(struct bufferevent
*bev
,
491 struct evdns_base
*evdns_base
, int family
, const char *hostname
, int port
)
494 struct evutil_addrinfo hint
;
496 struct bufferevent_private
*bev_p
=
497 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
499 if (family
!= AF_INET
&& family
!= AF_INET6
&& family
!= AF_UNSPEC
)
501 if (port
< 1 || port
> 65535)
504 memset(&hint
, 0, sizeof(hint
));
505 hint
.ai_family
= family
;
506 hint
.ai_protocol
= IPPROTO_TCP
;
507 hint
.ai_socktype
= SOCK_STREAM
;
509 evutil_snprintf(portbuf
, sizeof(portbuf
), "%d", port
);
512 bev_p
->dns_error
= 0;
514 bufferevent_suspend_write_(bev
, BEV_SUSPEND_LOOKUP
);
515 bufferevent_suspend_read_(bev
, BEV_SUSPEND_LOOKUP
);
517 bufferevent_incref_(bev
);
518 err
= evutil_getaddrinfo_async_(evdns_base
, hostname
, portbuf
,
519 &hint
, bufferevent_connect_getaddrinfo_cb
, bev
);
525 bufferevent_unsuspend_write_(bev
, BEV_SUSPEND_LOOKUP
);
526 bufferevent_unsuspend_read_(bev
, BEV_SUSPEND_LOOKUP
);
527 bufferevent_decref_(bev
);
533 bufferevent_socket_get_dns_error(struct bufferevent
*bev
)
536 struct bufferevent_private
*bev_p
=
537 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
540 rv
= bev_p
->dns_error
;
547 * Create a new buffered event object.
549 * The read callback is invoked whenever we read new data.
550 * The write callback is invoked whenever the output buffer is drained.
551 * The error callback is invoked on a write/read error or on EOF.
553 * Both read and write callbacks maybe NULL. The error callback is not
554 * allowed to be NULL and have to be provided always.
558 bufferevent_new(evutil_socket_t fd
,
559 bufferevent_data_cb readcb
, bufferevent_data_cb writecb
,
560 bufferevent_event_cb eventcb
, void *cbarg
)
562 struct bufferevent
*bufev
;
564 if (!(bufev
= bufferevent_socket_new(NULL
, fd
, 0)))
567 bufferevent_setcb(bufev
, readcb
, writecb
, eventcb
, cbarg
);
574 be_socket_enable(struct bufferevent
*bufev
, short event
)
576 if (event
& EV_READ
&&
577 bufferevent_add_event_(&bufev
->ev_read
, &bufev
->timeout_read
) == -1)
579 if (event
& EV_WRITE
&&
580 bufferevent_add_event_(&bufev
->ev_write
, &bufev
->timeout_write
) == -1)
586 be_socket_disable(struct bufferevent
*bufev
, short event
)
588 struct bufferevent_private
*bufev_p
=
589 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
590 if (event
& EV_READ
) {
591 if (event_del(&bufev
->ev_read
) == -1)
594 /* Don't actually disable the write if we are trying to connect. */
595 if ((event
& EV_WRITE
) && ! bufev_p
->connecting
) {
596 if (event_del(&bufev
->ev_write
) == -1)
603 be_socket_destruct(struct bufferevent
*bufev
)
605 struct bufferevent_private
*bufev_p
=
606 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
608 EVUTIL_ASSERT(bufev
->be_ops
== &bufferevent_ops_socket
);
610 fd
= event_get_fd(&bufev
->ev_read
);
612 if ((bufev_p
->options
& BEV_OPT_CLOSE_ON_FREE
) && fd
>= 0)
613 EVUTIL_CLOSESOCKET(fd
);
617 be_socket_flush(struct bufferevent
*bev
, short iotype
,
618 enum bufferevent_flush_mode mode
)
625 be_socket_setfd(struct bufferevent
*bufev
, evutil_socket_t fd
)
628 EVUTIL_ASSERT(bufev
->be_ops
== &bufferevent_ops_socket
);
630 event_del(&bufev
->ev_read
);
631 event_del(&bufev
->ev_write
);
633 event_assign(&bufev
->ev_read
, bufev
->ev_base
, fd
,
634 EV_READ
|EV_PERSIST
|EV_FINALIZE
, bufferevent_readcb
, bufev
);
635 event_assign(&bufev
->ev_write
, bufev
->ev_base
, fd
,
636 EV_WRITE
|EV_PERSIST
|EV_FINALIZE
, bufferevent_writecb
, bufev
);
639 bufferevent_enable(bufev
, bufev
->enabled
);
644 /* XXXX Should non-socket bufferevents support this? */
646 bufferevent_priority_set(struct bufferevent
*bufev
, int priority
)
649 struct bufferevent_private
*bufev_p
=
650 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
653 if (bufev
->be_ops
!= &bufferevent_ops_socket
)
656 if (event_priority_set(&bufev
->ev_read
, priority
) == -1)
658 if (event_priority_set(&bufev
->ev_write
, priority
) == -1)
661 event_deferred_cb_set_priority_(&bufev_p
->deferred
, priority
);
669 /* XXXX Should non-socket bufferevents support this? */
671 bufferevent_base_set(struct event_base
*base
, struct bufferevent
*bufev
)
676 if (bufev
->be_ops
!= &bufferevent_ops_socket
)
679 bufev
->ev_base
= base
;
681 res
= event_base_set(base
, &bufev
->ev_read
);
685 res
= event_base_set(base
, &bufev
->ev_write
);
692 be_socket_ctrl(struct bufferevent
*bev
, enum bufferevent_ctrl_op op
,
693 union bufferevent_ctrl_data
*data
)
696 case BEV_CTRL_SET_FD
:
697 be_socket_setfd(bev
, data
->fd
);
699 case BEV_CTRL_GET_FD
:
700 data
->fd
= event_get_fd(&bev
->ev_read
);
702 case BEV_CTRL_GET_UNDERLYING
:
703 case BEV_CTRL_CANCEL_ALL
: