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