1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3c43e99fdSEd Maste * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
4c43e99fdSEd Maste * All rights reserved.
5c43e99fdSEd Maste *
6c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
7c43e99fdSEd Maste * modification, are permitted provided that the following conditions
8c43e99fdSEd Maste * are met:
9c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
10c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
11c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
12c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
13c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
14c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products
15c43e99fdSEd Maste * derived from this software without specific prior written permission.
16c43e99fdSEd Maste *
17c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27c43e99fdSEd Maste */
28c43e99fdSEd Maste
29c43e99fdSEd Maste #include "event2/event-config.h"
30c43e99fdSEd Maste #include "evconfig-private.h"
31c43e99fdSEd Maste
32c43e99fdSEd Maste #include <sys/types.h>
33c43e99fdSEd Maste
34c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
35c43e99fdSEd Maste #include <sys/time.h>
36c43e99fdSEd Maste #endif
37c43e99fdSEd Maste
38c43e99fdSEd Maste #include <errno.h>
39c43e99fdSEd Maste #include <stdio.h>
40c43e99fdSEd Maste #include <stdlib.h>
41c43e99fdSEd Maste #include <string.h>
42c43e99fdSEd Maste #ifdef EVENT__HAVE_STDARG_H
43c43e99fdSEd Maste #include <stdarg.h>
44c43e99fdSEd Maste #endif
45c43e99fdSEd Maste #ifdef EVENT__HAVE_UNISTD_H
46c43e99fdSEd Maste #include <unistd.h>
47c43e99fdSEd Maste #endif
48c43e99fdSEd Maste
49c43e99fdSEd Maste #ifdef _WIN32
50c43e99fdSEd Maste #include <winsock2.h>
51c43e99fdSEd Maste #include <ws2tcpip.h>
52c43e99fdSEd Maste #endif
53c43e99fdSEd Maste
54c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_SOCKET_H
55c43e99fdSEd Maste #include <sys/socket.h>
56c43e99fdSEd Maste #endif
57c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN_H
58c43e99fdSEd Maste #include <netinet/in.h>
59c43e99fdSEd Maste #endif
60c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN6_H
61c43e99fdSEd Maste #include <netinet/in6.h>
62c43e99fdSEd Maste #endif
63c43e99fdSEd Maste
64c43e99fdSEd Maste #include "event2/util.h"
65c43e99fdSEd Maste #include "event2/bufferevent.h"
66c43e99fdSEd Maste #include "event2/buffer.h"
67c43e99fdSEd Maste #include "event2/bufferevent_struct.h"
68c43e99fdSEd Maste #include "event2/bufferevent_compat.h"
69c43e99fdSEd Maste #include "event2/event.h"
70c43e99fdSEd Maste #include "log-internal.h"
71c43e99fdSEd Maste #include "mm-internal.h"
72c43e99fdSEd Maste #include "bufferevent-internal.h"
73c43e99fdSEd Maste #include "util-internal.h"
74c43e99fdSEd Maste #ifdef _WIN32
75c43e99fdSEd Maste #include "iocp-internal.h"
76c43e99fdSEd Maste #endif
77c43e99fdSEd Maste
78c43e99fdSEd Maste /* prototypes */
79c43e99fdSEd Maste static int be_socket_enable(struct bufferevent *, short);
80c43e99fdSEd Maste static int be_socket_disable(struct bufferevent *, short);
81c43e99fdSEd Maste static void be_socket_destruct(struct bufferevent *);
82c43e99fdSEd Maste static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
83c43e99fdSEd Maste static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
84c43e99fdSEd Maste
85c43e99fdSEd Maste static void be_socket_setfd(struct bufferevent *, evutil_socket_t);
86c43e99fdSEd Maste
87c43e99fdSEd Maste const struct bufferevent_ops bufferevent_ops_socket = {
88c43e99fdSEd Maste "socket",
89c43e99fdSEd Maste evutil_offsetof(struct bufferevent_private, bev),
90c43e99fdSEd Maste be_socket_enable,
91c43e99fdSEd Maste be_socket_disable,
92c43e99fdSEd Maste NULL, /* unlink */
93c43e99fdSEd Maste be_socket_destruct,
94c43e99fdSEd Maste bufferevent_generic_adj_existing_timeouts_,
95c43e99fdSEd Maste be_socket_flush,
96c43e99fdSEd Maste be_socket_ctrl,
97c43e99fdSEd Maste };
98c43e99fdSEd Maste
99c43e99fdSEd Maste const struct sockaddr*
bufferevent_socket_get_conn_address_(struct bufferevent * bev)100c43e99fdSEd Maste bufferevent_socket_get_conn_address_(struct bufferevent *bev)
101c43e99fdSEd Maste {
102*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
103c43e99fdSEd Maste return (struct sockaddr *)&bev_p->conn_address;
104c43e99fdSEd Maste }
105*b50261e2SCy Schubert
106*b50261e2SCy Schubert void
bufferevent_socket_set_conn_address_fd_(struct bufferevent * bev,evutil_socket_t fd)107*b50261e2SCy Schubert bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev,
108*b50261e2SCy Schubert evutil_socket_t fd)
109c43e99fdSEd Maste {
110*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
111*b50261e2SCy Schubert
112c43e99fdSEd Maste socklen_t len = sizeof(bev_p->conn_address);
113c43e99fdSEd Maste
114c43e99fdSEd Maste struct sockaddr *addr = (struct sockaddr *)&bev_p->conn_address;
115c43e99fdSEd Maste if (addr->sa_family != AF_UNSPEC)
116c43e99fdSEd Maste getpeername(fd, addr, &len);
117c43e99fdSEd Maste }
118*b50261e2SCy Schubert
119*b50261e2SCy Schubert void
bufferevent_socket_set_conn_address_(struct bufferevent * bev,struct sockaddr * addr,size_t addrlen)120*b50261e2SCy Schubert bufferevent_socket_set_conn_address_(struct bufferevent *bev,
121c43e99fdSEd Maste struct sockaddr *addr, size_t addrlen)
122c43e99fdSEd Maste {
123*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
124c43e99fdSEd Maste EVUTIL_ASSERT(addrlen <= sizeof(bev_p->conn_address));
125c43e99fdSEd Maste memcpy(&bev_p->conn_address, addr, addrlen);
126c43e99fdSEd Maste }
127c43e99fdSEd Maste
128c43e99fdSEd Maste static void
bufferevent_socket_outbuf_cb(struct evbuffer * buf,const struct evbuffer_cb_info * cbinfo,void * arg)129c43e99fdSEd Maste bufferevent_socket_outbuf_cb(struct evbuffer *buf,
130c43e99fdSEd Maste const struct evbuffer_cb_info *cbinfo,
131c43e99fdSEd Maste void *arg)
132c43e99fdSEd Maste {
133c43e99fdSEd Maste struct bufferevent *bufev = arg;
134*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
135c43e99fdSEd Maste
136c43e99fdSEd Maste if (cbinfo->n_added &&
137c43e99fdSEd Maste (bufev->enabled & EV_WRITE) &&
138c43e99fdSEd Maste !event_pending(&bufev->ev_write, EV_WRITE, NULL) &&
139c43e99fdSEd Maste !bufev_p->write_suspended) {
140c43e99fdSEd Maste /* Somebody added data to the buffer, and we would like to
141c43e99fdSEd Maste * write, and we were not writing. So, start writing. */
142c43e99fdSEd Maste if (bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1) {
143c43e99fdSEd Maste /* Should we log this? */
144c43e99fdSEd Maste }
145c43e99fdSEd Maste }
146c43e99fdSEd Maste }
147c43e99fdSEd Maste
148c43e99fdSEd Maste static void
bufferevent_readcb(evutil_socket_t fd,short event,void * arg)149c43e99fdSEd Maste bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
150c43e99fdSEd Maste {
151c43e99fdSEd Maste struct bufferevent *bufev = arg;
152*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
153c43e99fdSEd Maste struct evbuffer *input;
154c43e99fdSEd Maste int res = 0;
155c43e99fdSEd Maste short what = BEV_EVENT_READING;
156c43e99fdSEd Maste ev_ssize_t howmuch = -1, readmax=-1;
157c43e99fdSEd Maste
158c43e99fdSEd Maste bufferevent_incref_and_lock_(bufev);
159c43e99fdSEd Maste
160c43e99fdSEd Maste if (event == EV_TIMEOUT) {
161c43e99fdSEd Maste /* Note that we only check for event==EV_TIMEOUT. If
162c43e99fdSEd Maste * event==EV_TIMEOUT|EV_READ, we can safely ignore the
163c43e99fdSEd Maste * timeout, since a read has occurred */
164c43e99fdSEd Maste what |= BEV_EVENT_TIMEOUT;
165c43e99fdSEd Maste goto error;
166c43e99fdSEd Maste }
167c43e99fdSEd Maste
168c43e99fdSEd Maste input = bufev->input;
169c43e99fdSEd Maste
170c43e99fdSEd Maste /*
171c43e99fdSEd Maste * If we have a high watermark configured then we don't want to
172c43e99fdSEd Maste * read more data than would make us reach the watermark.
173c43e99fdSEd Maste */
174c43e99fdSEd Maste if (bufev->wm_read.high != 0) {
175c43e99fdSEd Maste howmuch = bufev->wm_read.high - evbuffer_get_length(input);
176c43e99fdSEd Maste /* we somehow lowered the watermark, stop reading */
177c43e99fdSEd Maste if (howmuch <= 0) {
178c43e99fdSEd Maste bufferevent_wm_suspend_read(bufev);
179c43e99fdSEd Maste goto done;
180c43e99fdSEd Maste }
181c43e99fdSEd Maste }
182c43e99fdSEd Maste readmax = bufferevent_get_read_max_(bufev_p);
183c43e99fdSEd Maste if (howmuch < 0 || howmuch > readmax) /* The use of -1 for "unlimited"
184c43e99fdSEd Maste * uglifies this code. XXXX */
185c43e99fdSEd Maste howmuch = readmax;
186c43e99fdSEd Maste if (bufev_p->read_suspended)
187c43e99fdSEd Maste goto done;
188c43e99fdSEd Maste
189c43e99fdSEd Maste evbuffer_unfreeze(input, 0);
190c43e99fdSEd Maste res = evbuffer_read(input, fd, (int)howmuch); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
191c43e99fdSEd Maste evbuffer_freeze(input, 0);
192c43e99fdSEd Maste
193c43e99fdSEd Maste if (res == -1) {
194c43e99fdSEd Maste int err = evutil_socket_geterror(fd);
195c43e99fdSEd Maste if (EVUTIL_ERR_RW_RETRIABLE(err))
196c43e99fdSEd Maste goto reschedule;
197c43e99fdSEd Maste if (EVUTIL_ERR_CONNECT_REFUSED(err)) {
198c43e99fdSEd Maste bufev_p->connection_refused = 1;
199c43e99fdSEd Maste goto done;
200c43e99fdSEd Maste }
201c43e99fdSEd Maste /* error case */
202c43e99fdSEd Maste what |= BEV_EVENT_ERROR;
203c43e99fdSEd Maste } else if (res == 0) {
204c43e99fdSEd Maste /* eof case */
205c43e99fdSEd Maste what |= BEV_EVENT_EOF;
206c43e99fdSEd Maste }
207c43e99fdSEd Maste
208c43e99fdSEd Maste if (res <= 0)
209c43e99fdSEd Maste goto error;
210c43e99fdSEd Maste
211c43e99fdSEd Maste bufferevent_decrement_read_buckets_(bufev_p, res);
212c43e99fdSEd Maste
213c43e99fdSEd Maste /* Invoke the user callback - must always be called last */
214c43e99fdSEd Maste bufferevent_trigger_nolock_(bufev, EV_READ, 0);
215c43e99fdSEd Maste
216c43e99fdSEd Maste goto done;
217c43e99fdSEd Maste
218c43e99fdSEd Maste reschedule:
219c43e99fdSEd Maste goto done;
220c43e99fdSEd Maste
221c43e99fdSEd Maste error:
222c43e99fdSEd Maste bufferevent_disable(bufev, EV_READ);
223c43e99fdSEd Maste bufferevent_run_eventcb_(bufev, what, 0);
224c43e99fdSEd Maste
225c43e99fdSEd Maste done:
226c43e99fdSEd Maste bufferevent_decref_and_unlock_(bufev);
227c43e99fdSEd Maste }
228c43e99fdSEd Maste
229c43e99fdSEd Maste static void
bufferevent_writecb(evutil_socket_t fd,short event,void * arg)230c43e99fdSEd Maste bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
231c43e99fdSEd Maste {
232c43e99fdSEd Maste struct bufferevent *bufev = arg;
233*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
234c43e99fdSEd Maste int res = 0;
235c43e99fdSEd Maste short what = BEV_EVENT_WRITING;
236c43e99fdSEd Maste int connected = 0;
237c43e99fdSEd Maste ev_ssize_t atmost = -1;
238c43e99fdSEd Maste
239c43e99fdSEd Maste bufferevent_incref_and_lock_(bufev);
240c43e99fdSEd Maste
241c43e99fdSEd Maste if (event == EV_TIMEOUT) {
242c43e99fdSEd Maste /* Note that we only check for event==EV_TIMEOUT. If
243c43e99fdSEd Maste * event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
244c43e99fdSEd Maste * timeout, since a read has occurred */
245c43e99fdSEd Maste what |= BEV_EVENT_TIMEOUT;
246c43e99fdSEd Maste goto error;
247c43e99fdSEd Maste }
248c43e99fdSEd Maste if (bufev_p->connecting) {
249c43e99fdSEd Maste int c = evutil_socket_finished_connecting_(fd);
250c43e99fdSEd Maste /* we need to fake the error if the connection was refused
251c43e99fdSEd Maste * immediately - usually connection to localhost on BSD */
252c43e99fdSEd Maste if (bufev_p->connection_refused) {
253c43e99fdSEd Maste bufev_p->connection_refused = 0;
254c43e99fdSEd Maste c = -1;
255c43e99fdSEd Maste }
256c43e99fdSEd Maste
257c43e99fdSEd Maste if (c == 0)
258c43e99fdSEd Maste goto done;
259c43e99fdSEd Maste
260c43e99fdSEd Maste bufev_p->connecting = 0;
261c43e99fdSEd Maste if (c < 0) {
262c43e99fdSEd Maste event_del(&bufev->ev_write);
263c43e99fdSEd Maste event_del(&bufev->ev_read);
264c43e99fdSEd Maste bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR, 0);
265c43e99fdSEd Maste goto done;
266c43e99fdSEd Maste } else {
267c43e99fdSEd Maste connected = 1;
268*b50261e2SCy Schubert bufferevent_socket_set_conn_address_fd_(bufev, fd);
269c43e99fdSEd Maste #ifdef _WIN32
270c43e99fdSEd Maste if (BEV_IS_ASYNC(bufev)) {
271c43e99fdSEd Maste event_del(&bufev->ev_write);
272c43e99fdSEd Maste bufferevent_async_set_connected_(bufev);
273c43e99fdSEd Maste bufferevent_run_eventcb_(bufev,
274c43e99fdSEd Maste BEV_EVENT_CONNECTED, 0);
275c43e99fdSEd Maste goto done;
276c43e99fdSEd Maste }
277c43e99fdSEd Maste #endif
278c43e99fdSEd Maste bufferevent_run_eventcb_(bufev,
279c43e99fdSEd Maste BEV_EVENT_CONNECTED, 0);
280c43e99fdSEd Maste if (!(bufev->enabled & EV_WRITE) ||
281c43e99fdSEd Maste bufev_p->write_suspended) {
282c43e99fdSEd Maste event_del(&bufev->ev_write);
283c43e99fdSEd Maste goto done;
284c43e99fdSEd Maste }
285c43e99fdSEd Maste }
286c43e99fdSEd Maste }
287c43e99fdSEd Maste
288c43e99fdSEd Maste atmost = bufferevent_get_write_max_(bufev_p);
289c43e99fdSEd Maste
290c43e99fdSEd Maste if (bufev_p->write_suspended)
291c43e99fdSEd Maste goto done;
292c43e99fdSEd Maste
293c43e99fdSEd Maste if (evbuffer_get_length(bufev->output)) {
294c43e99fdSEd Maste evbuffer_unfreeze(bufev->output, 1);
295c43e99fdSEd Maste res = evbuffer_write_atmost(bufev->output, fd, atmost);
296c43e99fdSEd Maste evbuffer_freeze(bufev->output, 1);
297c43e99fdSEd Maste if (res == -1) {
298c43e99fdSEd Maste int err = evutil_socket_geterror(fd);
299c43e99fdSEd Maste if (EVUTIL_ERR_RW_RETRIABLE(err))
300c43e99fdSEd Maste goto reschedule;
301c43e99fdSEd Maste what |= BEV_EVENT_ERROR;
302c43e99fdSEd Maste } else if (res == 0) {
303c43e99fdSEd Maste /* eof case
304c43e99fdSEd Maste XXXX Actually, a 0 on write doesn't indicate
305c43e99fdSEd Maste an EOF. An ECONNRESET might be more typical.
306c43e99fdSEd Maste */
307c43e99fdSEd Maste what |= BEV_EVENT_EOF;
308c43e99fdSEd Maste }
309c43e99fdSEd Maste if (res <= 0)
310c43e99fdSEd Maste goto error;
311c43e99fdSEd Maste
312c43e99fdSEd Maste bufferevent_decrement_write_buckets_(bufev_p, res);
313c43e99fdSEd Maste }
314c43e99fdSEd Maste
315c43e99fdSEd Maste if (evbuffer_get_length(bufev->output) == 0) {
316c43e99fdSEd Maste event_del(&bufev->ev_write);
317c43e99fdSEd Maste }
318c43e99fdSEd Maste
319c43e99fdSEd Maste /*
320c43e99fdSEd Maste * Invoke the user callback if our buffer is drained or below the
321c43e99fdSEd Maste * low watermark.
322c43e99fdSEd Maste */
323c43e99fdSEd Maste if (res || !connected) {
324c43e99fdSEd Maste bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
325c43e99fdSEd Maste }
326c43e99fdSEd Maste
327c43e99fdSEd Maste goto done;
328c43e99fdSEd Maste
329c43e99fdSEd Maste reschedule:
330c43e99fdSEd Maste if (evbuffer_get_length(bufev->output) == 0) {
331c43e99fdSEd Maste event_del(&bufev->ev_write);
332c43e99fdSEd Maste }
333c43e99fdSEd Maste goto done;
334c43e99fdSEd Maste
335c43e99fdSEd Maste error:
336c43e99fdSEd Maste bufferevent_disable(bufev, EV_WRITE);
337c43e99fdSEd Maste bufferevent_run_eventcb_(bufev, what, 0);
338c43e99fdSEd Maste
339c43e99fdSEd Maste done:
340c43e99fdSEd Maste bufferevent_decref_and_unlock_(bufev);
341c43e99fdSEd Maste }
342c43e99fdSEd Maste
343c43e99fdSEd Maste struct bufferevent *
bufferevent_socket_new(struct event_base * base,evutil_socket_t fd,int options)344c43e99fdSEd Maste bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
345c43e99fdSEd Maste int options)
346c43e99fdSEd Maste {
347c43e99fdSEd Maste struct bufferevent_private *bufev_p;
348c43e99fdSEd Maste struct bufferevent *bufev;
349c43e99fdSEd Maste
350c43e99fdSEd Maste #ifdef _WIN32
351c43e99fdSEd Maste if (base && event_base_get_iocp_(base))
352c43e99fdSEd Maste return bufferevent_async_new_(base, fd, options);
353c43e99fdSEd Maste #endif
354c43e99fdSEd Maste
355c43e99fdSEd Maste if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
356c43e99fdSEd Maste return NULL;
357c43e99fdSEd Maste
358c43e99fdSEd Maste if (bufferevent_init_common_(bufev_p, base, &bufferevent_ops_socket,
359c43e99fdSEd Maste options) < 0) {
360c43e99fdSEd Maste mm_free(bufev_p);
361c43e99fdSEd Maste return NULL;
362c43e99fdSEd Maste }
363c43e99fdSEd Maste bufev = &bufev_p->bev;
364c43e99fdSEd Maste evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
365c43e99fdSEd Maste
366c43e99fdSEd Maste event_assign(&bufev->ev_read, bufev->ev_base, fd,
367c43e99fdSEd Maste EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
368c43e99fdSEd Maste event_assign(&bufev->ev_write, bufev->ev_base, fd,
369c43e99fdSEd Maste EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
370c43e99fdSEd Maste
371c43e99fdSEd Maste evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
372c43e99fdSEd Maste
373c43e99fdSEd Maste evbuffer_freeze(bufev->input, 0);
374c43e99fdSEd Maste evbuffer_freeze(bufev->output, 1);
375c43e99fdSEd Maste
376c43e99fdSEd Maste return bufev;
377c43e99fdSEd Maste }
378c43e99fdSEd Maste
379c43e99fdSEd Maste int
bufferevent_socket_connect(struct bufferevent * bev,const struct sockaddr * sa,int socklen)380c43e99fdSEd Maste bufferevent_socket_connect(struct bufferevent *bev,
381c43e99fdSEd Maste const struct sockaddr *sa, int socklen)
382c43e99fdSEd Maste {
383*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bev);
384c43e99fdSEd Maste
385c43e99fdSEd Maste evutil_socket_t fd;
386c43e99fdSEd Maste int r = 0;
387c43e99fdSEd Maste int result=-1;
388c43e99fdSEd Maste int ownfd = 0;
389c43e99fdSEd Maste
390c43e99fdSEd Maste bufferevent_incref_and_lock_(bev);
391c43e99fdSEd Maste
392c43e99fdSEd Maste fd = bufferevent_getfd(bev);
393c43e99fdSEd Maste if (fd < 0) {
394c43e99fdSEd Maste if (!sa)
395c43e99fdSEd Maste goto done;
396c43e99fdSEd Maste fd = evutil_socket_(sa->sa_family,
397c43e99fdSEd Maste SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
398c43e99fdSEd Maste if (fd < 0)
399*b50261e2SCy Schubert goto freesock;
400c43e99fdSEd Maste ownfd = 1;
401c43e99fdSEd Maste }
402c43e99fdSEd Maste if (sa) {
403c43e99fdSEd Maste #ifdef _WIN32
404c43e99fdSEd Maste if (bufferevent_async_can_connect_(bev)) {
405c43e99fdSEd Maste bufferevent_setfd(bev, fd);
406c43e99fdSEd Maste r = bufferevent_async_connect_(bev, fd, sa, socklen);
407c43e99fdSEd Maste if (r < 0)
408c43e99fdSEd Maste goto freesock;
409c43e99fdSEd Maste bufev_p->connecting = 1;
410c43e99fdSEd Maste result = 0;
411c43e99fdSEd Maste goto done;
412c43e99fdSEd Maste } else
413c43e99fdSEd Maste #endif
414c43e99fdSEd Maste r = evutil_socket_connect_(&fd, sa, socklen);
415c43e99fdSEd Maste if (r < 0)
416c43e99fdSEd Maste goto freesock;
417c43e99fdSEd Maste }
418c43e99fdSEd Maste #ifdef _WIN32
419c43e99fdSEd Maste /* ConnectEx() isn't always around, even when IOCP is enabled.
420c43e99fdSEd Maste * Here, we borrow the socket object's write handler to fall back
421c43e99fdSEd Maste * on a non-blocking connect() when ConnectEx() is unavailable. */
422c43e99fdSEd Maste if (BEV_IS_ASYNC(bev)) {
423c43e99fdSEd Maste event_assign(&bev->ev_write, bev->ev_base, fd,
424c43e99fdSEd Maste EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bev);
425c43e99fdSEd Maste }
426c43e99fdSEd Maste #endif
427c43e99fdSEd Maste bufferevent_setfd(bev, fd);
428c43e99fdSEd Maste if (r == 0) {
429c43e99fdSEd Maste if (! be_socket_enable(bev, EV_WRITE)) {
430c43e99fdSEd Maste bufev_p->connecting = 1;
431c43e99fdSEd Maste result = 0;
432c43e99fdSEd Maste goto done;
433c43e99fdSEd Maste }
434c43e99fdSEd Maste } else if (r == 1) {
435c43e99fdSEd Maste /* The connect succeeded already. How very BSD of it. */
436c43e99fdSEd Maste result = 0;
437c43e99fdSEd Maste bufev_p->connecting = 1;
438c43e99fdSEd Maste bufferevent_trigger_nolock_(bev, EV_WRITE, BEV_OPT_DEFER_CALLBACKS);
439c43e99fdSEd Maste } else {
440c43e99fdSEd Maste /* The connect failed already. How very BSD of it. */
441c43e99fdSEd Maste result = 0;
442c43e99fdSEd Maste bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, BEV_OPT_DEFER_CALLBACKS);
443c43e99fdSEd Maste bufferevent_disable(bev, EV_WRITE|EV_READ);
444c43e99fdSEd Maste }
445c43e99fdSEd Maste
446c43e99fdSEd Maste goto done;
447c43e99fdSEd Maste
448c43e99fdSEd Maste freesock:
449c43e99fdSEd Maste if (ownfd)
450c43e99fdSEd Maste evutil_closesocket(fd);
451c43e99fdSEd Maste done:
452c43e99fdSEd Maste bufferevent_decref_and_unlock_(bev);
453c43e99fdSEd Maste return result;
454c43e99fdSEd Maste }
455c43e99fdSEd Maste
456c43e99fdSEd Maste static void
bufferevent_connect_getaddrinfo_cb(int result,struct evutil_addrinfo * ai,void * arg)457c43e99fdSEd Maste bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
458c43e99fdSEd Maste void *arg)
459c43e99fdSEd Maste {
460c43e99fdSEd Maste struct bufferevent *bev = arg;
461*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
462c43e99fdSEd Maste int r;
463c43e99fdSEd Maste BEV_LOCK(bev);
464c43e99fdSEd Maste
465c43e99fdSEd Maste bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
466c43e99fdSEd Maste bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
467c43e99fdSEd Maste
468c43e99fdSEd Maste bev_p->dns_request = NULL;
469c43e99fdSEd Maste
470c43e99fdSEd Maste if (result == EVUTIL_EAI_CANCEL) {
471c43e99fdSEd Maste bev_p->dns_error = result;
472c43e99fdSEd Maste bufferevent_decref_and_unlock_(bev);
473c43e99fdSEd Maste return;
474c43e99fdSEd Maste }
475c43e99fdSEd Maste if (result != 0) {
476c43e99fdSEd Maste bev_p->dns_error = result;
477c43e99fdSEd Maste bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
478c43e99fdSEd Maste bufferevent_decref_and_unlock_(bev);
479c43e99fdSEd Maste if (ai)
480c43e99fdSEd Maste evutil_freeaddrinfo(ai);
481c43e99fdSEd Maste return;
482c43e99fdSEd Maste }
483c43e99fdSEd Maste
484c43e99fdSEd Maste /* XXX use the other addrinfos? */
485*b50261e2SCy Schubert bufferevent_socket_set_conn_address_(bev, ai->ai_addr, (int)ai->ai_addrlen);
486c43e99fdSEd Maste r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen);
487*b50261e2SCy Schubert if (r < 0)
488*b50261e2SCy Schubert bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
489c43e99fdSEd Maste bufferevent_decref_and_unlock_(bev);
490c43e99fdSEd Maste evutil_freeaddrinfo(ai);
491c43e99fdSEd Maste }
492c43e99fdSEd Maste
493c43e99fdSEd Maste int
bufferevent_socket_connect_hostname(struct bufferevent * bev,struct evdns_base * evdns_base,int family,const char * hostname,int port)494c43e99fdSEd Maste bufferevent_socket_connect_hostname(struct bufferevent *bev,
495c43e99fdSEd Maste struct evdns_base *evdns_base, int family, const char *hostname, int port)
496c43e99fdSEd Maste {
497c43e99fdSEd Maste char portbuf[10];
498c43e99fdSEd Maste struct evutil_addrinfo hint;
499*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
500c43e99fdSEd Maste
501c43e99fdSEd Maste if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC)
502c43e99fdSEd Maste return -1;
503c43e99fdSEd Maste if (port < 1 || port > 65535)
504c43e99fdSEd Maste return -1;
505c43e99fdSEd Maste
506c43e99fdSEd Maste memset(&hint, 0, sizeof(hint));
507c43e99fdSEd Maste hint.ai_family = family;
508c43e99fdSEd Maste hint.ai_protocol = IPPROTO_TCP;
509c43e99fdSEd Maste hint.ai_socktype = SOCK_STREAM;
510c43e99fdSEd Maste
511c43e99fdSEd Maste evutil_snprintf(portbuf, sizeof(portbuf), "%d", port);
512c43e99fdSEd Maste
513c43e99fdSEd Maste BEV_LOCK(bev);
514c43e99fdSEd Maste bev_p->dns_error = 0;
515c43e99fdSEd Maste
516c43e99fdSEd Maste bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
517c43e99fdSEd Maste bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
518c43e99fdSEd Maste
519c43e99fdSEd Maste bufferevent_incref_(bev);
520c43e99fdSEd Maste bev_p->dns_request = evutil_getaddrinfo_async_(evdns_base, hostname,
521c43e99fdSEd Maste portbuf, &hint, bufferevent_connect_getaddrinfo_cb, bev);
522c43e99fdSEd Maste BEV_UNLOCK(bev);
523c43e99fdSEd Maste
524c43e99fdSEd Maste return 0;
525c43e99fdSEd Maste }
526c43e99fdSEd Maste
527c43e99fdSEd Maste int
bufferevent_socket_get_dns_error(struct bufferevent * bev)528c43e99fdSEd Maste bufferevent_socket_get_dns_error(struct bufferevent *bev)
529c43e99fdSEd Maste {
530c43e99fdSEd Maste int rv;
531*b50261e2SCy Schubert struct bufferevent_private *bev_p = BEV_UPCAST(bev);
532c43e99fdSEd Maste
533c43e99fdSEd Maste BEV_LOCK(bev);
534c43e99fdSEd Maste rv = bev_p->dns_error;
535c43e99fdSEd Maste BEV_UNLOCK(bev);
536c43e99fdSEd Maste
537c43e99fdSEd Maste return rv;
538c43e99fdSEd Maste }
539c43e99fdSEd Maste
540c43e99fdSEd Maste /*
541c43e99fdSEd Maste * Create a new buffered event object.
542c43e99fdSEd Maste *
543c43e99fdSEd Maste * The read callback is invoked whenever we read new data.
544c43e99fdSEd Maste * The write callback is invoked whenever the output buffer is drained.
545c43e99fdSEd Maste * The error callback is invoked on a write/read error or on EOF.
546c43e99fdSEd Maste *
547c43e99fdSEd Maste * Both read and write callbacks maybe NULL. The error callback is not
548c43e99fdSEd Maste * allowed to be NULL and have to be provided always.
549c43e99fdSEd Maste */
550c43e99fdSEd Maste
551c43e99fdSEd Maste struct bufferevent *
bufferevent_new(evutil_socket_t fd,bufferevent_data_cb readcb,bufferevent_data_cb writecb,bufferevent_event_cb eventcb,void * cbarg)552c43e99fdSEd Maste bufferevent_new(evutil_socket_t fd,
553c43e99fdSEd Maste bufferevent_data_cb readcb, bufferevent_data_cb writecb,
554c43e99fdSEd Maste bufferevent_event_cb eventcb, void *cbarg)
555c43e99fdSEd Maste {
556c43e99fdSEd Maste struct bufferevent *bufev;
557c43e99fdSEd Maste
558c43e99fdSEd Maste if (!(bufev = bufferevent_socket_new(NULL, fd, 0)))
559c43e99fdSEd Maste return NULL;
560c43e99fdSEd Maste
561c43e99fdSEd Maste bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
562c43e99fdSEd Maste
563c43e99fdSEd Maste return bufev;
564c43e99fdSEd Maste }
565c43e99fdSEd Maste
566c43e99fdSEd Maste
567c43e99fdSEd Maste static int
be_socket_enable(struct bufferevent * bufev,short event)568c43e99fdSEd Maste be_socket_enable(struct bufferevent *bufev, short event)
569c43e99fdSEd Maste {
570c43e99fdSEd Maste if (event & EV_READ &&
571c43e99fdSEd Maste bufferevent_add_event_(&bufev->ev_read, &bufev->timeout_read) == -1)
572c43e99fdSEd Maste return -1;
573c43e99fdSEd Maste if (event & EV_WRITE &&
574c43e99fdSEd Maste bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1)
575c43e99fdSEd Maste return -1;
576c43e99fdSEd Maste return 0;
577c43e99fdSEd Maste }
578c43e99fdSEd Maste
579c43e99fdSEd Maste static int
be_socket_disable(struct bufferevent * bufev,short event)580c43e99fdSEd Maste be_socket_disable(struct bufferevent *bufev, short event)
581c43e99fdSEd Maste {
582*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
583c43e99fdSEd Maste if (event & EV_READ) {
584c43e99fdSEd Maste if (event_del(&bufev->ev_read) == -1)
585c43e99fdSEd Maste return -1;
586c43e99fdSEd Maste }
587c43e99fdSEd Maste /* Don't actually disable the write if we are trying to connect. */
588c43e99fdSEd Maste if ((event & EV_WRITE) && ! bufev_p->connecting) {
589c43e99fdSEd Maste if (event_del(&bufev->ev_write) == -1)
590c43e99fdSEd Maste return -1;
591c43e99fdSEd Maste }
592c43e99fdSEd Maste return 0;
593c43e99fdSEd Maste }
594c43e99fdSEd Maste
595c43e99fdSEd Maste static void
be_socket_destruct(struct bufferevent * bufev)596c43e99fdSEd Maste be_socket_destruct(struct bufferevent *bufev)
597c43e99fdSEd Maste {
598*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
599c43e99fdSEd Maste evutil_socket_t fd;
600*b50261e2SCy Schubert EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
601c43e99fdSEd Maste
602c43e99fdSEd Maste fd = event_get_fd(&bufev->ev_read);
603c43e99fdSEd Maste
604c43e99fdSEd Maste if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
605c43e99fdSEd Maste EVUTIL_CLOSESOCKET(fd);
606c43e99fdSEd Maste
607c43e99fdSEd Maste evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
608c43e99fdSEd Maste }
609c43e99fdSEd Maste
610c43e99fdSEd Maste static int
be_socket_flush(struct bufferevent * bev,short iotype,enum bufferevent_flush_mode mode)611c43e99fdSEd Maste be_socket_flush(struct bufferevent *bev, short iotype,
612c43e99fdSEd Maste enum bufferevent_flush_mode mode)
613c43e99fdSEd Maste {
614c43e99fdSEd Maste return 0;
615c43e99fdSEd Maste }
616c43e99fdSEd Maste
617c43e99fdSEd Maste
618c43e99fdSEd Maste static void
be_socket_setfd(struct bufferevent * bufev,evutil_socket_t fd)619c43e99fdSEd Maste be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
620c43e99fdSEd Maste {
621*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
622c43e99fdSEd Maste
623c43e99fdSEd Maste BEV_LOCK(bufev);
624*b50261e2SCy Schubert EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
625c43e99fdSEd Maste
626c43e99fdSEd Maste event_del(&bufev->ev_read);
627c43e99fdSEd Maste event_del(&bufev->ev_write);
628c43e99fdSEd Maste
629c43e99fdSEd Maste evbuffer_unfreeze(bufev->input, 0);
630c43e99fdSEd Maste evbuffer_unfreeze(bufev->output, 1);
631c43e99fdSEd Maste
632c43e99fdSEd Maste event_assign(&bufev->ev_read, bufev->ev_base, fd,
633c43e99fdSEd Maste EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
634c43e99fdSEd Maste event_assign(&bufev->ev_write, bufev->ev_base, fd,
635c43e99fdSEd Maste EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
636c43e99fdSEd Maste
637c43e99fdSEd Maste if (fd >= 0)
638c43e99fdSEd Maste bufferevent_enable(bufev, bufev->enabled);
639c43e99fdSEd Maste
640c43e99fdSEd Maste evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
641c43e99fdSEd Maste
642c43e99fdSEd Maste BEV_UNLOCK(bufev);
643c43e99fdSEd Maste }
644c43e99fdSEd Maste
645c43e99fdSEd Maste /* XXXX Should non-socket bufferevents support this? */
646c43e99fdSEd Maste int
bufferevent_priority_set(struct bufferevent * bufev,int priority)647c43e99fdSEd Maste bufferevent_priority_set(struct bufferevent *bufev, int priority)
648c43e99fdSEd Maste {
649c43e99fdSEd Maste int r = -1;
650*b50261e2SCy Schubert struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
651c43e99fdSEd Maste
652c43e99fdSEd Maste BEV_LOCK(bufev);
653*b50261e2SCy Schubert if (BEV_IS_ASYNC(bufev) || BEV_IS_FILTER(bufev) || BEV_IS_PAIR(bufev))
654c43e99fdSEd Maste goto done;
655c43e99fdSEd Maste
656c43e99fdSEd Maste if (event_priority_set(&bufev->ev_read, priority) == -1)
657c43e99fdSEd Maste goto done;
658c43e99fdSEd Maste if (event_priority_set(&bufev->ev_write, priority) == -1)
659c43e99fdSEd Maste goto done;
660c43e99fdSEd Maste
661c43e99fdSEd Maste event_deferred_cb_set_priority_(&bufev_p->deferred, priority);
662c43e99fdSEd Maste
663c43e99fdSEd Maste r = 0;
664c43e99fdSEd Maste done:
665c43e99fdSEd Maste BEV_UNLOCK(bufev);
666c43e99fdSEd Maste return r;
667c43e99fdSEd Maste }
668c43e99fdSEd Maste
669c43e99fdSEd Maste /* XXXX Should non-socket bufferevents support this? */
670c43e99fdSEd Maste int
bufferevent_base_set(struct event_base * base,struct bufferevent * bufev)671c43e99fdSEd Maste bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
672c43e99fdSEd Maste {
673c43e99fdSEd Maste int res = -1;
674c43e99fdSEd Maste
675c43e99fdSEd Maste BEV_LOCK(bufev);
676*b50261e2SCy Schubert if (!BEV_IS_SOCKET(bufev))
677c43e99fdSEd Maste goto done;
678c43e99fdSEd Maste
679c43e99fdSEd Maste bufev->ev_base = base;
680c43e99fdSEd Maste
681c43e99fdSEd Maste res = event_base_set(base, &bufev->ev_read);
682c43e99fdSEd Maste if (res == -1)
683c43e99fdSEd Maste goto done;
684c43e99fdSEd Maste
685c43e99fdSEd Maste res = event_base_set(base, &bufev->ev_write);
686c43e99fdSEd Maste done:
687c43e99fdSEd Maste BEV_UNLOCK(bufev);
688c43e99fdSEd Maste return res;
689c43e99fdSEd Maste }
690c43e99fdSEd Maste
691c43e99fdSEd Maste static int
be_socket_ctrl(struct bufferevent * bev,enum bufferevent_ctrl_op op,union bufferevent_ctrl_data * data)692c43e99fdSEd Maste be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
693c43e99fdSEd Maste union bufferevent_ctrl_data *data)
694c43e99fdSEd Maste {
695c43e99fdSEd Maste switch (op) {
696c43e99fdSEd Maste case BEV_CTRL_SET_FD:
697c43e99fdSEd Maste be_socket_setfd(bev, data->fd);
698c43e99fdSEd Maste return 0;
699c43e99fdSEd Maste case BEV_CTRL_GET_FD:
700c43e99fdSEd Maste data->fd = event_get_fd(&bev->ev_read);
701c43e99fdSEd Maste return 0;
702c43e99fdSEd Maste case BEV_CTRL_GET_UNDERLYING:
703c43e99fdSEd Maste case BEV_CTRL_CANCEL_ALL:
704c43e99fdSEd Maste default:
705c43e99fdSEd Maste return -1;
706c43e99fdSEd Maste }
707c43e99fdSEd Maste }
708