1 /* $NetBSD: bufferevent_sock.c,v 1.1.1.2 2015/01/29 06:38:05 spz Exp $ */
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
32 #include "event2/event-config.h"
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: bufferevent_sock.c,v 1.1.1.2 2015/01/29 06:38:05 spz Exp $");
36 #ifdef _EVENT_HAVE_SYS_TIME_H
44 #ifdef _EVENT_HAVE_STDARG_H
47 #ifdef _EVENT_HAVE_UNISTD_H
56 #ifdef _EVENT_HAVE_SYS_SOCKET_H
57 #include <sys/socket.h>
59 #ifdef _EVENT_HAVE_NETINET_IN_H
60 #include <netinet/in.h>
62 #ifdef _EVENT_HAVE_NETINET_IN6_H
63 #include <netinet/in6.h>
66 #include "event2/util.h"
67 #include "event2/bufferevent.h"
68 #include "event2/buffer.h"
69 #include "event2/bufferevent_struct.h"
70 #include "event2/bufferevent_compat.h"
71 #include "event2/event.h"
72 #include "log-internal.h"
73 #include "mm-internal.h"
74 #include "bufferevent-internal.h"
75 #include "util-internal.h"
77 #include "iocp-internal.h"
81 static int be_socket_enable(struct bufferevent
*, short);
82 static int be_socket_disable(struct bufferevent
*, short);
83 static void be_socket_destruct(struct bufferevent
*);
84 static int be_socket_adj_timeouts(struct bufferevent
*);
85 static int be_socket_flush(struct bufferevent
*, short, enum bufferevent_flush_mode
);
86 static int be_socket_ctrl(struct bufferevent
*, enum bufferevent_ctrl_op
, union bufferevent_ctrl_data
*);
88 static void be_socket_setfd(struct bufferevent
*, evutil_socket_t
);
90 const struct bufferevent_ops bufferevent_ops_socket
= {
92 evutil_offsetof(struct bufferevent_private
, bev
),
96 be_socket_adj_timeouts
,
101 #define be_socket_add(ev, t) \
102 _bufferevent_add_event((ev), (t))
105 bufferevent_socket_outbuf_cb(struct evbuffer
*buf
,
106 const struct evbuffer_cb_info
*cbinfo
,
109 struct bufferevent
*bufev
= arg
;
110 struct bufferevent_private
*bufev_p
=
111 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
113 if (cbinfo
->n_added
&&
114 (bufev
->enabled
& EV_WRITE
) &&
115 !event_pending(&bufev
->ev_write
, EV_WRITE
, NULL
) &&
116 !bufev_p
->write_suspended
) {
117 /* Somebody added data to the buffer, and we would like to
118 * write, and we were not writing. So, start writing. */
119 if (be_socket_add(&bufev
->ev_write
, &bufev
->timeout_write
) == -1) {
120 /* Should we log this? */
126 bufferevent_readcb(evutil_socket_t fd
, short event
, void *arg
)
128 struct bufferevent
*bufev
= arg
;
129 struct bufferevent_private
*bufev_p
=
130 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
131 struct evbuffer
*input
;
133 short what
= BEV_EVENT_READING
;
134 ev_ssize_t howmuch
= -1, readmax
=-1;
136 _bufferevent_incref_and_lock(bufev
);
138 if (event
== EV_TIMEOUT
) {
139 /* Note that we only check for event==EV_TIMEOUT. If
140 * event==EV_TIMEOUT|EV_READ, we can safely ignore the
141 * timeout, since a read has occurred */
142 what
|= BEV_EVENT_TIMEOUT
;
146 input
= bufev
->input
;
149 * If we have a high watermark configured then we don't want to
150 * read more data than would make us reach the watermark.
152 if (bufev
->wm_read
.high
!= 0) {
153 howmuch
= bufev
->wm_read
.high
- evbuffer_get_length(input
);
154 /* we somehow lowered the watermark, stop reading */
156 bufferevent_wm_suspend_read(bufev
);
160 readmax
= _bufferevent_get_read_max(bufev_p
);
161 if (howmuch
< 0 || howmuch
> readmax
) /* The use of -1 for "unlimited"
162 * uglifies this code. XXXX */
164 if (bufev_p
->read_suspended
)
167 evbuffer_unfreeze(input
, 0);
168 res
= evbuffer_read(input
, fd
, (int)howmuch
); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
169 evbuffer_freeze(input
, 0);
172 int err
= evutil_socket_geterror(fd
);
173 if (EVUTIL_ERR_RW_RETRIABLE(err
))
176 what
|= BEV_EVENT_ERROR
;
177 } else if (res
== 0) {
179 what
|= BEV_EVENT_EOF
;
185 _bufferevent_decrement_read_buckets(bufev_p
, res
);
187 /* Invoke the user callback - must always be called last */
188 if (evbuffer_get_length(input
) >= bufev
->wm_read
.low
)
189 _bufferevent_run_readcb(bufev
);
197 bufferevent_disable(bufev
, EV_READ
);
198 _bufferevent_run_eventcb(bufev
, what
);
201 _bufferevent_decref_and_unlock(bufev
);
205 bufferevent_writecb(evutil_socket_t fd
, short event
, void *arg
)
207 struct bufferevent
*bufev
= arg
;
208 struct bufferevent_private
*bufev_p
=
209 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
211 short what
= BEV_EVENT_WRITING
;
213 ev_ssize_t atmost
= -1;
215 _bufferevent_incref_and_lock(bufev
);
217 if (event
== EV_TIMEOUT
) {
218 /* Note that we only check for event==EV_TIMEOUT. If
219 * event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
220 * timeout, since a read has occurred */
221 what
|= BEV_EVENT_TIMEOUT
;
224 if (bufev_p
->connecting
) {
225 int c
= evutil_socket_finished_connecting(fd
);
226 /* we need to fake the error if the connection was refused
227 * immediately - usually connection to localhost on BSD */
228 if (bufev_p
->connection_refused
) {
229 bufev_p
->connection_refused
= 0;
236 bufev_p
->connecting
= 0;
238 event_del(&bufev
->ev_write
);
239 event_del(&bufev
->ev_read
);
240 _bufferevent_run_eventcb(bufev
, BEV_EVENT_ERROR
);
245 if (BEV_IS_ASYNC(bufev
)) {
246 event_del(&bufev
->ev_write
);
247 bufferevent_async_set_connected(bufev
);
248 _bufferevent_run_eventcb(bufev
,
249 BEV_EVENT_CONNECTED
);
253 _bufferevent_run_eventcb(bufev
,
254 BEV_EVENT_CONNECTED
);
255 if (!(bufev
->enabled
& EV_WRITE
) ||
256 bufev_p
->write_suspended
) {
257 event_del(&bufev
->ev_write
);
263 atmost
= _bufferevent_get_write_max(bufev_p
);
265 if (bufev_p
->write_suspended
)
268 if (evbuffer_get_length(bufev
->output
)) {
269 evbuffer_unfreeze(bufev
->output
, 1);
270 res
= evbuffer_write_atmost(bufev
->output
, fd
, atmost
);
271 evbuffer_freeze(bufev
->output
, 1);
273 int err
= evutil_socket_geterror(fd
);
274 if (EVUTIL_ERR_RW_RETRIABLE(err
))
276 what
|= BEV_EVENT_ERROR
;
277 } else if (res
== 0) {
279 XXXX Actually, a 0 on write doesn't indicate
280 an EOF. An ECONNRESET might be more typical.
282 what
|= BEV_EVENT_EOF
;
287 _bufferevent_decrement_write_buckets(bufev_p
, res
);
290 if (evbuffer_get_length(bufev
->output
) == 0) {
291 event_del(&bufev
->ev_write
);
295 * Invoke the user callback if our buffer is drained or below the
298 if ((res
|| !connected
) &&
299 evbuffer_get_length(bufev
->output
) <= bufev
->wm_write
.low
) {
300 _bufferevent_run_writecb(bufev
);
306 if (evbuffer_get_length(bufev
->output
) == 0) {
307 event_del(&bufev
->ev_write
);
312 bufferevent_disable(bufev
, EV_WRITE
);
313 _bufferevent_run_eventcb(bufev
, what
);
316 _bufferevent_decref_and_unlock(bufev
);
320 bufferevent_socket_new(struct event_base
*base
, evutil_socket_t fd
,
323 struct bufferevent_private
*bufev_p
;
324 struct bufferevent
*bufev
;
327 if (base
&& event_base_get_iocp(base
))
328 return bufferevent_async_new(base
, fd
, options
);
331 if ((bufev_p
= mm_calloc(1, sizeof(struct bufferevent_private
)))== NULL
)
334 if (bufferevent_init_common(bufev_p
, base
, &bufferevent_ops_socket
,
339 bufev
= &bufev_p
->bev
;
340 evbuffer_set_flags(bufev
->output
, EVBUFFER_FLAG_DRAINS_TO_FD
);
342 event_assign(&bufev
->ev_read
, bufev
->ev_base
, fd
,
343 EV_READ
|EV_PERSIST
, bufferevent_readcb
, bufev
);
344 event_assign(&bufev
->ev_write
, bufev
->ev_base
, fd
,
345 EV_WRITE
|EV_PERSIST
, bufferevent_writecb
, bufev
);
347 evbuffer_add_cb(bufev
->output
, bufferevent_socket_outbuf_cb
, bufev
);
349 evbuffer_freeze(bufev
->input
, 0);
350 evbuffer_freeze(bufev
->output
, 1);
356 bufferevent_socket_connect(struct bufferevent
*bev
,
357 struct sockaddr
*sa
, int socklen
)
359 struct bufferevent_private
*bufev_p
=
360 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
367 _bufferevent_incref_and_lock(bev
);
372 fd
= bufferevent_getfd(bev
);
376 fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0);
379 if (evutil_make_socket_nonblocking(fd
)<0)
385 if (bufferevent_async_can_connect(bev
)) {
386 bufferevent_setfd(bev
, fd
);
387 r
= bufferevent_async_connect(bev
, fd
, sa
, socklen
);
390 bufev_p
->connecting
= 1;
395 r
= evutil_socket_connect(&fd
, sa
, socklen
);
400 /* ConnectEx() isn't always around, even when IOCP is enabled.
401 * Here, we borrow the socket object's write handler to fall back
402 * on a non-blocking connect() when ConnectEx() is unavailable. */
403 if (BEV_IS_ASYNC(bev
)) {
404 event_assign(&bev
->ev_write
, bev
->ev_base
, fd
,
405 EV_WRITE
|EV_PERSIST
, bufferevent_writecb
, bev
);
408 bufferevent_setfd(bev
, fd
);
410 if (! be_socket_enable(bev
, EV_WRITE
)) {
411 bufev_p
->connecting
= 1;
416 /* The connect succeeded already. How very BSD of it. */
418 bufev_p
->connecting
= 1;
419 event_active(&bev
->ev_write
, EV_WRITE
, 1);
421 /* The connect failed already. How very BSD of it. */
422 bufev_p
->connection_refused
= 1;
423 bufev_p
->connecting
= 1;
425 event_active(&bev
->ev_write
, EV_WRITE
, 1);
431 _bufferevent_run_eventcb(bev
, BEV_EVENT_ERROR
);
433 evutil_closesocket(fd
);
434 /* do something about the error? */
436 _bufferevent_decref_and_unlock(bev
);
441 bufferevent_connect_getaddrinfo_cb(int result
, struct evutil_addrinfo
*ai
,
444 struct bufferevent
*bev
= arg
;
445 struct bufferevent_private
*bev_p
=
446 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
450 bufferevent_unsuspend_write(bev
, BEV_SUSPEND_LOOKUP
);
451 bufferevent_unsuspend_read(bev
, BEV_SUSPEND_LOOKUP
);
454 bev_p
->dns_error
= result
;
455 _bufferevent_run_eventcb(bev
, BEV_EVENT_ERROR
);
456 _bufferevent_decref_and_unlock(bev
);
458 evutil_freeaddrinfo(ai
);
462 /* XXX use the other addrinfos? */
463 /* XXX use this return value */
464 r
= bufferevent_socket_connect(bev
, ai
->ai_addr
, (int)ai
->ai_addrlen
);
466 _bufferevent_decref_and_unlock(bev
);
467 evutil_freeaddrinfo(ai
);
471 bufferevent_socket_connect_hostname(struct bufferevent
*bev
,
472 struct evdns_base
*evdns_base
, int family
, const char *hostname
, int port
)
475 struct evutil_addrinfo hint
;
477 struct bufferevent_private
*bev_p
=
478 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
480 if (family
!= AF_INET
&& family
!= AF_INET6
&& family
!= AF_UNSPEC
)
482 if (port
< 1 || port
> 65535)
486 bev_p
->dns_error
= 0;
489 evutil_snprintf(portbuf
, sizeof(portbuf
), "%d", port
);
491 memset(&hint
, 0, sizeof(hint
));
492 hint
.ai_family
= family
;
493 hint
.ai_protocol
= IPPROTO_TCP
;
494 hint
.ai_socktype
= SOCK_STREAM
;
496 bufferevent_suspend_write(bev
, BEV_SUSPEND_LOOKUP
);
497 bufferevent_suspend_read(bev
, BEV_SUSPEND_LOOKUP
);
499 bufferevent_incref(bev
);
500 err
= evutil_getaddrinfo_async(evdns_base
, hostname
, portbuf
,
501 &hint
, bufferevent_connect_getaddrinfo_cb
, bev
);
506 bufferevent_unsuspend_write(bev
, BEV_SUSPEND_LOOKUP
);
507 bufferevent_unsuspend_read(bev
, BEV_SUSPEND_LOOKUP
);
513 bufferevent_socket_get_dns_error(struct bufferevent
*bev
)
516 struct bufferevent_private
*bev_p
=
517 EVUTIL_UPCAST(bev
, struct bufferevent_private
, bev
);
520 rv
= bev_p
->dns_error
;
527 * Create a new buffered event object.
529 * The read callback is invoked whenever we read new data.
530 * The write callback is invoked whenever the output buffer is drained.
531 * The error callback is invoked on a write/read error or on EOF.
533 * Both read and write callbacks maybe NULL. The error callback is not
534 * allowed to be NULL and have to be provided always.
538 bufferevent_new(evutil_socket_t fd
,
539 bufferevent_data_cb readcb
, bufferevent_data_cb writecb
,
540 bufferevent_event_cb eventcb
, void *cbarg
)
542 struct bufferevent
*bufev
;
544 if (!(bufev
= bufferevent_socket_new(NULL
, fd
, 0)))
547 bufferevent_setcb(bufev
, readcb
, writecb
, eventcb
, cbarg
);
554 be_socket_enable(struct bufferevent
*bufev
, short event
)
556 if (event
& EV_READ
) {
557 if (be_socket_add(&bufev
->ev_read
,&bufev
->timeout_read
) == -1)
560 if (event
& EV_WRITE
) {
561 if (be_socket_add(&bufev
->ev_write
,&bufev
->timeout_write
) == -1)
568 be_socket_disable(struct bufferevent
*bufev
, short event
)
570 struct bufferevent_private
*bufev_p
=
571 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
572 if (event
& EV_READ
) {
573 if (event_del(&bufev
->ev_read
) == -1)
576 /* Don't actually disable the write if we are trying to connect. */
577 if ((event
& EV_WRITE
) && ! bufev_p
->connecting
) {
578 if (event_del(&bufev
->ev_write
) == -1)
585 be_socket_destruct(struct bufferevent
*bufev
)
587 struct bufferevent_private
*bufev_p
=
588 EVUTIL_UPCAST(bufev
, struct bufferevent_private
, bev
);
590 EVUTIL_ASSERT(bufev
->be_ops
== &bufferevent_ops_socket
);
592 fd
= event_get_fd(&bufev
->ev_read
);
594 event_del(&bufev
->ev_read
);
595 event_del(&bufev
->ev_write
);
597 if ((bufev_p
->options
& BEV_OPT_CLOSE_ON_FREE
) && fd
>= 0)
598 EVUTIL_CLOSESOCKET(fd
);
602 be_socket_adj_timeouts(struct bufferevent
*bufev
)
605 if (event_pending(&bufev
->ev_read
, EV_READ
, NULL
))
606 if (be_socket_add(&bufev
->ev_read
, &bufev
->timeout_read
) < 0)
608 if (event_pending(&bufev
->ev_write
, EV_WRITE
, NULL
)) {
609 if (be_socket_add(&bufev
->ev_write
, &bufev
->timeout_write
) < 0)
616 be_socket_flush(struct bufferevent
*bev
, short iotype
,
617 enum bufferevent_flush_mode mode
)
624 be_socket_setfd(struct bufferevent
*bufev
, evutil_socket_t fd
)
627 EVUTIL_ASSERT(bufev
->be_ops
== &bufferevent_ops_socket
);
629 event_del(&bufev
->ev_read
);
630 event_del(&bufev
->ev_write
);
632 event_assign(&bufev
->ev_read
, bufev
->ev_base
, fd
,
633 EV_READ
|EV_PERSIST
, bufferevent_readcb
, bufev
);
634 event_assign(&bufev
->ev_write
, bufev
->ev_base
, fd
,
635 EV_WRITE
|EV_PERSIST
, bufferevent_writecb
, bufev
);
638 bufferevent_enable(bufev
, bufev
->enabled
);
643 /* XXXX Should non-socket bufferevents support this? */
645 bufferevent_priority_set(struct bufferevent
*bufev
, int priority
)
650 if (bufev
->be_ops
!= &bufferevent_ops_socket
)
653 if (event_priority_set(&bufev
->ev_read
, priority
) == -1)
655 if (event_priority_set(&bufev
->ev_write
, priority
) == -1)
664 /* XXXX Should non-socket bufferevents support this? */
666 bufferevent_base_set(struct event_base
*base
, struct bufferevent
*bufev
)
671 if (bufev
->be_ops
!= &bufferevent_ops_socket
)
674 bufev
->ev_base
= base
;
676 res
= event_base_set(base
, &bufev
->ev_read
);
680 res
= event_base_set(base
, &bufev
->ev_write
);
687 be_socket_ctrl(struct bufferevent
*bev
, enum bufferevent_ctrl_op op
,
688 union bufferevent_ctrl_data
*data
)
691 case BEV_CTRL_SET_FD
:
692 be_socket_setfd(bev
, data
->fd
);
694 case BEV_CTRL_GET_FD
:
695 data
->fd
= event_get_fd(&bev
->ev_read
);
697 case BEV_CTRL_GET_UNDERLYING
:
698 case BEV_CTRL_CANCEL_ALL
: