etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libevent / dist / bufferevent_sock.c
blob30eef5754fcabe0989b5ef3ea17985128c2bc9bc
1 /* $NetBSD: bufferevent_sock.c,v 1.1.1.2 2015/01/29 06:38:05 spz Exp $ */
2 /*
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
37 #include <sys/time.h>
38 #endif
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #ifdef _EVENT_HAVE_STDARG_H
45 #include <stdarg.h>
46 #endif
47 #ifdef _EVENT_HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
51 #ifdef WIN32
52 #include <winsock2.h>
53 #include <ws2tcpip.h>
54 #endif
56 #ifdef _EVENT_HAVE_SYS_SOCKET_H
57 #include <sys/socket.h>
58 #endif
59 #ifdef _EVENT_HAVE_NETINET_IN_H
60 #include <netinet/in.h>
61 #endif
62 #ifdef _EVENT_HAVE_NETINET_IN6_H
63 #include <netinet/in6.h>
64 #endif
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"
76 #ifdef WIN32
77 #include "iocp-internal.h"
78 #endif
80 /* prototypes */
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 = {
91 "socket",
92 evutil_offsetof(struct bufferevent_private, bev),
93 be_socket_enable,
94 be_socket_disable,
95 be_socket_destruct,
96 be_socket_adj_timeouts,
97 be_socket_flush,
98 be_socket_ctrl,
101 #define be_socket_add(ev, t) \
102 _bufferevent_add_event((ev), (t))
104 static void
105 bufferevent_socket_outbuf_cb(struct evbuffer *buf,
106 const struct evbuffer_cb_info *cbinfo,
107 void *arg)
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? */
125 static void
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;
132 int res = 0;
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;
143 goto error;
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 */
155 if (howmuch <= 0) {
156 bufferevent_wm_suspend_read(bufev);
157 goto done;
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 */
163 howmuch = readmax;
164 if (bufev_p->read_suspended)
165 goto done;
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);
171 if (res == -1) {
172 int err = evutil_socket_geterror(fd);
173 if (EVUTIL_ERR_RW_RETRIABLE(err))
174 goto reschedule;
175 /* error case */
176 what |= BEV_EVENT_ERROR;
177 } else if (res == 0) {
178 /* eof case */
179 what |= BEV_EVENT_EOF;
182 if (res <= 0)
183 goto error;
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);
191 goto done;
193 reschedule:
194 goto done;
196 error:
197 bufferevent_disable(bufev, EV_READ);
198 _bufferevent_run_eventcb(bufev, what);
200 done:
201 _bufferevent_decref_and_unlock(bufev);
204 static void
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);
210 int res = 0;
211 short what = BEV_EVENT_WRITING;
212 int connected = 0;
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;
222 goto error;
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;
230 c = -1;
233 if (c == 0)
234 goto done;
236 bufev_p->connecting = 0;
237 if (c < 0) {
238 event_del(&bufev->ev_write);
239 event_del(&bufev->ev_read);
240 _bufferevent_run_eventcb(bufev, BEV_EVENT_ERROR);
241 goto done;
242 } else {
243 connected = 1;
244 #ifdef WIN32
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);
250 goto done;
252 #endif
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);
258 goto done;
263 atmost = _bufferevent_get_write_max(bufev_p);
265 if (bufev_p->write_suspended)
266 goto done;
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);
272 if (res == -1) {
273 int err = evutil_socket_geterror(fd);
274 if (EVUTIL_ERR_RW_RETRIABLE(err))
275 goto reschedule;
276 what |= BEV_EVENT_ERROR;
277 } else if (res == 0) {
278 /* eof case
279 XXXX Actually, a 0 on write doesn't indicate
280 an EOF. An ECONNRESET might be more typical.
282 what |= BEV_EVENT_EOF;
284 if (res <= 0)
285 goto error;
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
296 * low watermark.
298 if ((res || !connected) &&
299 evbuffer_get_length(bufev->output) <= bufev->wm_write.low) {
300 _bufferevent_run_writecb(bufev);
303 goto done;
305 reschedule:
306 if (evbuffer_get_length(bufev->output) == 0) {
307 event_del(&bufev->ev_write);
309 goto done;
311 error:
312 bufferevent_disable(bufev, EV_WRITE);
313 _bufferevent_run_eventcb(bufev, what);
315 done:
316 _bufferevent_decref_and_unlock(bufev);
319 struct bufferevent *
320 bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
321 int options)
323 struct bufferevent_private *bufev_p;
324 struct bufferevent *bufev;
326 #ifdef WIN32
327 if (base && event_base_get_iocp(base))
328 return bufferevent_async_new(base, fd, options);
329 #endif
331 if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
332 return NULL;
334 if (bufferevent_init_common(bufev_p, base, &bufferevent_ops_socket,
335 options) < 0) {
336 mm_free(bufev_p);
337 return NULL;
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);
352 return bufev;
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);
362 evutil_socket_t fd;
363 int r = 0;
364 int result=-1;
365 int ownfd = 0;
367 _bufferevent_incref_and_lock(bev);
369 if (!bufev_p)
370 goto done;
372 fd = bufferevent_getfd(bev);
373 if (fd < 0) {
374 if (!sa)
375 goto done;
376 fd = socket(sa->sa_family, SOCK_STREAM, 0);
377 if (fd < 0)
378 goto done;
379 if (evutil_make_socket_nonblocking(fd)<0)
380 goto done;
381 ownfd = 1;
383 if (sa) {
384 #ifdef WIN32
385 if (bufferevent_async_can_connect(bev)) {
386 bufferevent_setfd(bev, fd);
387 r = bufferevent_async_connect(bev, fd, sa, socklen);
388 if (r < 0)
389 goto freesock;
390 bufev_p->connecting = 1;
391 result = 0;
392 goto done;
393 } else
394 #endif
395 r = evutil_socket_connect(&fd, sa, socklen);
396 if (r < 0)
397 goto freesock;
399 #ifdef WIN32
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);
407 #endif
408 bufferevent_setfd(bev, fd);
409 if (r == 0) {
410 if (! be_socket_enable(bev, EV_WRITE)) {
411 bufev_p->connecting = 1;
412 result = 0;
413 goto done;
415 } else if (r == 1) {
416 /* The connect succeeded already. How very BSD of it. */
417 result = 0;
418 bufev_p->connecting = 1;
419 event_active(&bev->ev_write, EV_WRITE, 1);
420 } else {
421 /* The connect failed already. How very BSD of it. */
422 bufev_p->connection_refused = 1;
423 bufev_p->connecting = 1;
424 result = 0;
425 event_active(&bev->ev_write, EV_WRITE, 1);
428 goto done;
430 freesock:
431 _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
432 if (ownfd)
433 evutil_closesocket(fd);
434 /* do something about the error? */
435 done:
436 _bufferevent_decref_and_unlock(bev);
437 return result;
440 static void
441 bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
442 void *arg)
444 struct bufferevent *bev = arg;
445 struct bufferevent_private *bev_p =
446 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
447 int r;
448 BEV_LOCK(bev);
450 bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP);
451 bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP);
453 if (result != 0) {
454 bev_p->dns_error = result;
455 _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR);
456 _bufferevent_decref_and_unlock(bev);
457 if (ai)
458 evutil_freeaddrinfo(ai);
459 return;
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);
465 (void)r;
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)
474 char portbuf[10];
475 struct evutil_addrinfo hint;
476 int err;
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)
481 return -1;
482 if (port < 1 || port > 65535)
483 return -1;
485 BEV_LOCK(bev);
486 bev_p->dns_error = 0;
487 BEV_UNLOCK(bev);
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);
503 if (err == 0) {
504 return 0;
505 } else {
506 bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP);
507 bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP);
508 return -1;
513 bufferevent_socket_get_dns_error(struct bufferevent *bev)
515 int rv;
516 struct bufferevent_private *bev_p =
517 EVUTIL_UPCAST(bev, struct bufferevent_private, bev);
519 BEV_LOCK(bev);
520 rv = bev_p->dns_error;
521 BEV_UNLOCK(bev);
523 return rv;
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.
537 struct bufferevent *
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)))
545 return NULL;
547 bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
549 return bufev;
553 static int
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)
558 return -1;
560 if (event & EV_WRITE) {
561 if (be_socket_add(&bufev->ev_write,&bufev->timeout_write) == -1)
562 return -1;
564 return 0;
567 static int
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)
574 return -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)
579 return -1;
581 return 0;
584 static void
585 be_socket_destruct(struct bufferevent *bufev)
587 struct bufferevent_private *bufev_p =
588 EVUTIL_UPCAST(bufev, struct bufferevent_private, bev);
589 evutil_socket_t fd;
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);
601 static int
602 be_socket_adj_timeouts(struct bufferevent *bufev)
604 int r = 0;
605 if (event_pending(&bufev->ev_read, EV_READ, NULL))
606 if (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0)
607 r = -1;
608 if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) {
609 if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0)
610 r = -1;
612 return r;
615 static int
616 be_socket_flush(struct bufferevent *bev, short iotype,
617 enum bufferevent_flush_mode mode)
619 return 0;
623 static void
624 be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
626 BEV_LOCK(bufev);
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);
637 if (fd >= 0)
638 bufferevent_enable(bufev, bufev->enabled);
640 BEV_UNLOCK(bufev);
643 /* XXXX Should non-socket bufferevents support this? */
645 bufferevent_priority_set(struct bufferevent *bufev, int priority)
647 int r = -1;
649 BEV_LOCK(bufev);
650 if (bufev->be_ops != &bufferevent_ops_socket)
651 goto done;
653 if (event_priority_set(&bufev->ev_read, priority) == -1)
654 goto done;
655 if (event_priority_set(&bufev->ev_write, priority) == -1)
656 goto done;
658 r = 0;
659 done:
660 BEV_UNLOCK(bufev);
661 return r;
664 /* XXXX Should non-socket bufferevents support this? */
666 bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
668 int res = -1;
670 BEV_LOCK(bufev);
671 if (bufev->be_ops != &bufferevent_ops_socket)
672 goto done;
674 bufev->ev_base = base;
676 res = event_base_set(base, &bufev->ev_read);
677 if (res == -1)
678 goto done;
680 res = event_base_set(base, &bufev->ev_write);
681 done:
682 BEV_UNLOCK(bufev);
683 return res;
686 static int
687 be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
688 union bufferevent_ctrl_data *data)
690 switch (op) {
691 case BEV_CTRL_SET_FD:
692 be_socket_setfd(bev, data->fd);
693 return 0;
694 case BEV_CTRL_GET_FD:
695 data->fd = event_get_fd(&bev->ev_read);
696 return 0;
697 case BEV_CTRL_GET_UNDERLYING:
698 case BEV_CTRL_CANCEL_ALL:
699 default:
700 return -1;