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