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) 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | @file buffer_iocp.c |
michael@0 | 29 | |
michael@0 | 30 | This module implements overlapped read and write functions for evbuffer |
michael@0 | 31 | objects on Windows. |
michael@0 | 32 | */ |
michael@0 | 33 | |
michael@0 | 34 | #include "event2/buffer.h" |
michael@0 | 35 | #include "event2/buffer_compat.h" |
michael@0 | 36 | #include "event2/util.h" |
michael@0 | 37 | #include "event2/thread.h" |
michael@0 | 38 | #include "event2/event-config.h" |
michael@0 | 39 | #include "util-internal.h" |
michael@0 | 40 | #include "evthread-internal.h" |
michael@0 | 41 | #include "evbuffer-internal.h" |
michael@0 | 42 | #include "iocp-internal.h" |
michael@0 | 43 | #include "mm-internal.h" |
michael@0 | 44 | |
michael@0 | 45 | #include <winsock2.h> |
michael@0 | 46 | #include <windows.h> |
michael@0 | 47 | #include <stdio.h> |
michael@0 | 48 | |
michael@0 | 49 | #define MAX_WSABUFS 16 |
michael@0 | 50 | |
michael@0 | 51 | /** An evbuffer that can handle overlapped IO. */ |
michael@0 | 52 | struct evbuffer_overlapped { |
michael@0 | 53 | struct evbuffer buffer; |
michael@0 | 54 | /** The socket that we're doing overlapped IO on. */ |
michael@0 | 55 | evutil_socket_t fd; |
michael@0 | 56 | |
michael@0 | 57 | /** pending I/O type */ |
michael@0 | 58 | unsigned read_in_progress : 1; |
michael@0 | 59 | unsigned write_in_progress : 1; |
michael@0 | 60 | |
michael@0 | 61 | /** The first pinned chain in the buffer. */ |
michael@0 | 62 | struct evbuffer_chain *first_pinned; |
michael@0 | 63 | |
michael@0 | 64 | /** How many chains are pinned; how many of the fields in buffers |
michael@0 | 65 | * are we using. */ |
michael@0 | 66 | int n_buffers; |
michael@0 | 67 | WSABUF buffers[MAX_WSABUFS]; |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | /** Given an evbuffer, return the correponding evbuffer structure, or NULL if |
michael@0 | 71 | * the evbuffer isn't overlapped. */ |
michael@0 | 72 | static inline struct evbuffer_overlapped * |
michael@0 | 73 | upcast_evbuffer(struct evbuffer *buf) |
michael@0 | 74 | { |
michael@0 | 75 | if (!buf || !buf->is_overlapped) |
michael@0 | 76 | return NULL; |
michael@0 | 77 | return EVUTIL_UPCAST(buf, struct evbuffer_overlapped, buffer); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | /** Unpin all the chains noted as pinned in 'eo'. */ |
michael@0 | 81 | static void |
michael@0 | 82 | pin_release(struct evbuffer_overlapped *eo, unsigned flag) |
michael@0 | 83 | { |
michael@0 | 84 | int i; |
michael@0 | 85 | struct evbuffer_chain *next, *chain = eo->first_pinned; |
michael@0 | 86 | |
michael@0 | 87 | for (i = 0; i < eo->n_buffers; ++i) { |
michael@0 | 88 | EVUTIL_ASSERT(chain); |
michael@0 | 89 | next = chain->next; |
michael@0 | 90 | _evbuffer_chain_unpin(chain, flag); |
michael@0 | 91 | chain = next; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | evbuffer_commit_read(struct evbuffer *evbuf, ev_ssize_t nBytes) |
michael@0 | 97 | { |
michael@0 | 98 | struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf); |
michael@0 | 99 | struct evbuffer_chain **chainp; |
michael@0 | 100 | size_t remaining, len; |
michael@0 | 101 | unsigned i; |
michael@0 | 102 | |
michael@0 | 103 | EVBUFFER_LOCK(evbuf); |
michael@0 | 104 | EVUTIL_ASSERT(buf->read_in_progress && !buf->write_in_progress); |
michael@0 | 105 | EVUTIL_ASSERT(nBytes >= 0); /* XXXX Can this be false? */ |
michael@0 | 106 | |
michael@0 | 107 | evbuffer_unfreeze(evbuf, 0); |
michael@0 | 108 | |
michael@0 | 109 | chainp = evbuf->last_with_datap; |
michael@0 | 110 | if (!((*chainp)->flags & EVBUFFER_MEM_PINNED_R)) |
michael@0 | 111 | chainp = &(*chainp)->next; |
michael@0 | 112 | remaining = nBytes; |
michael@0 | 113 | for (i = 0; remaining > 0 && i < (unsigned)buf->n_buffers; ++i) { |
michael@0 | 114 | EVUTIL_ASSERT(*chainp); |
michael@0 | 115 | len = buf->buffers[i].len; |
michael@0 | 116 | if (remaining < len) |
michael@0 | 117 | len = remaining; |
michael@0 | 118 | (*chainp)->off += len; |
michael@0 | 119 | evbuf->last_with_datap = chainp; |
michael@0 | 120 | remaining -= len; |
michael@0 | 121 | chainp = &(*chainp)->next; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | pin_release(buf, EVBUFFER_MEM_PINNED_R); |
michael@0 | 125 | |
michael@0 | 126 | buf->read_in_progress = 0; |
michael@0 | 127 | |
michael@0 | 128 | evbuf->total_len += nBytes; |
michael@0 | 129 | evbuf->n_add_for_cb += nBytes; |
michael@0 | 130 | |
michael@0 | 131 | evbuffer_invoke_callbacks(evbuf); |
michael@0 | 132 | |
michael@0 | 133 | _evbuffer_decref_and_unlock(evbuf); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | void |
michael@0 | 137 | evbuffer_commit_write(struct evbuffer *evbuf, ev_ssize_t nBytes) |
michael@0 | 138 | { |
michael@0 | 139 | struct evbuffer_overlapped *buf = upcast_evbuffer(evbuf); |
michael@0 | 140 | |
michael@0 | 141 | EVBUFFER_LOCK(evbuf); |
michael@0 | 142 | EVUTIL_ASSERT(buf->write_in_progress && !buf->read_in_progress); |
michael@0 | 143 | evbuffer_unfreeze(evbuf, 1); |
michael@0 | 144 | evbuffer_drain(evbuf, nBytes); |
michael@0 | 145 | pin_release(buf,EVBUFFER_MEM_PINNED_W); |
michael@0 | 146 | buf->write_in_progress = 0; |
michael@0 | 147 | _evbuffer_decref_and_unlock(evbuf); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | struct evbuffer * |
michael@0 | 151 | evbuffer_overlapped_new(evutil_socket_t fd) |
michael@0 | 152 | { |
michael@0 | 153 | struct evbuffer_overlapped *evo; |
michael@0 | 154 | |
michael@0 | 155 | evo = mm_calloc(1, sizeof(struct evbuffer_overlapped)); |
michael@0 | 156 | if (!evo) |
michael@0 | 157 | return NULL; |
michael@0 | 158 | |
michael@0 | 159 | TAILQ_INIT(&evo->buffer.callbacks); |
michael@0 | 160 | evo->buffer.refcnt = 1; |
michael@0 | 161 | evo->buffer.last_with_datap = &evo->buffer.first; |
michael@0 | 162 | |
michael@0 | 163 | evo->buffer.is_overlapped = 1; |
michael@0 | 164 | evo->fd = fd; |
michael@0 | 165 | |
michael@0 | 166 | return &evo->buffer; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | int |
michael@0 | 170 | evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t at_most, |
michael@0 | 171 | struct event_overlapped *ol) |
michael@0 | 172 | { |
michael@0 | 173 | struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); |
michael@0 | 174 | int r = -1; |
michael@0 | 175 | int i; |
michael@0 | 176 | struct evbuffer_chain *chain; |
michael@0 | 177 | DWORD bytesSent; |
michael@0 | 178 | |
michael@0 | 179 | if (!buf) { |
michael@0 | 180 | /* No buffer, or it isn't overlapped */ |
michael@0 | 181 | return -1; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | EVBUFFER_LOCK(buf); |
michael@0 | 185 | EVUTIL_ASSERT(!buf_o->read_in_progress); |
michael@0 | 186 | if (buf->freeze_start || buf_o->write_in_progress) |
michael@0 | 187 | goto done; |
michael@0 | 188 | if (!buf->total_len) { |
michael@0 | 189 | /* Nothing to write */ |
michael@0 | 190 | r = 0; |
michael@0 | 191 | goto done; |
michael@0 | 192 | } else if (at_most < 0 || (size_t)at_most > buf->total_len) { |
michael@0 | 193 | at_most = buf->total_len; |
michael@0 | 194 | } |
michael@0 | 195 | evbuffer_freeze(buf, 1); |
michael@0 | 196 | |
michael@0 | 197 | buf_o->first_pinned = NULL; |
michael@0 | 198 | buf_o->n_buffers = 0; |
michael@0 | 199 | memset(buf_o->buffers, 0, sizeof(buf_o->buffers)); |
michael@0 | 200 | |
michael@0 | 201 | chain = buf_o->first_pinned = buf->first; |
michael@0 | 202 | |
michael@0 | 203 | for (i=0; i < MAX_WSABUFS && chain; ++i, chain=chain->next) { |
michael@0 | 204 | WSABUF *b = &buf_o->buffers[i]; |
michael@0 | 205 | b->buf = (char*)( chain->buffer + chain->misalign ); |
michael@0 | 206 | _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_W); |
michael@0 | 207 | |
michael@0 | 208 | if ((size_t)at_most > chain->off) { |
michael@0 | 209 | /* XXXX Cast is safe for now, since win32 has no |
michael@0 | 210 | mmaped chains. But later, we need to have this |
michael@0 | 211 | add more WSAbufs if chain->off is greater than |
michael@0 | 212 | ULONG_MAX */ |
michael@0 | 213 | b->len = (unsigned long)chain->off; |
michael@0 | 214 | at_most -= chain->off; |
michael@0 | 215 | } else { |
michael@0 | 216 | b->len = (unsigned long)at_most; |
michael@0 | 217 | ++i; |
michael@0 | 218 | break; |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | buf_o->n_buffers = i; |
michael@0 | 223 | _evbuffer_incref(buf); |
michael@0 | 224 | if (WSASend(buf_o->fd, buf_o->buffers, i, &bytesSent, 0, |
michael@0 | 225 | &ol->overlapped, NULL)) { |
michael@0 | 226 | int error = WSAGetLastError(); |
michael@0 | 227 | if (error != WSA_IO_PENDING) { |
michael@0 | 228 | /* An actual error. */ |
michael@0 | 229 | pin_release(buf_o, EVBUFFER_MEM_PINNED_W); |
michael@0 | 230 | evbuffer_unfreeze(buf, 1); |
michael@0 | 231 | evbuffer_free(buf); /* decref */ |
michael@0 | 232 | goto done; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | buf_o->write_in_progress = 1; |
michael@0 | 237 | r = 0; |
michael@0 | 238 | done: |
michael@0 | 239 | EVBUFFER_UNLOCK(buf); |
michael@0 | 240 | return r; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | int |
michael@0 | 244 | evbuffer_launch_read(struct evbuffer *buf, size_t at_most, |
michael@0 | 245 | struct event_overlapped *ol) |
michael@0 | 246 | { |
michael@0 | 247 | struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); |
michael@0 | 248 | int r = -1, i; |
michael@0 | 249 | int nvecs; |
michael@0 | 250 | int npin=0; |
michael@0 | 251 | struct evbuffer_chain *chain=NULL, **chainp; |
michael@0 | 252 | DWORD bytesRead; |
michael@0 | 253 | DWORD flags = 0; |
michael@0 | 254 | struct evbuffer_iovec vecs[MAX_WSABUFS]; |
michael@0 | 255 | |
michael@0 | 256 | if (!buf_o) |
michael@0 | 257 | return -1; |
michael@0 | 258 | EVBUFFER_LOCK(buf); |
michael@0 | 259 | EVUTIL_ASSERT(!buf_o->write_in_progress); |
michael@0 | 260 | if (buf->freeze_end || buf_o->read_in_progress) |
michael@0 | 261 | goto done; |
michael@0 | 262 | |
michael@0 | 263 | buf_o->first_pinned = NULL; |
michael@0 | 264 | buf_o->n_buffers = 0; |
michael@0 | 265 | memset(buf_o->buffers, 0, sizeof(buf_o->buffers)); |
michael@0 | 266 | |
michael@0 | 267 | if (_evbuffer_expand_fast(buf, at_most, MAX_WSABUFS) == -1) |
michael@0 | 268 | goto done; |
michael@0 | 269 | evbuffer_freeze(buf, 0); |
michael@0 | 270 | |
michael@0 | 271 | nvecs = _evbuffer_read_setup_vecs(buf, at_most, |
michael@0 | 272 | vecs, MAX_WSABUFS, &chainp, 1); |
michael@0 | 273 | for (i=0;i<nvecs;++i) { |
michael@0 | 274 | WSABUF_FROM_EVBUFFER_IOV( |
michael@0 | 275 | &buf_o->buffers[i], |
michael@0 | 276 | &vecs[i]); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | buf_o->n_buffers = nvecs; |
michael@0 | 280 | buf_o->first_pinned = chain = *chainp; |
michael@0 | 281 | |
michael@0 | 282 | npin=0; |
michael@0 | 283 | for ( ; chain; chain = chain->next) { |
michael@0 | 284 | _evbuffer_chain_pin(chain, EVBUFFER_MEM_PINNED_R); |
michael@0 | 285 | ++npin; |
michael@0 | 286 | } |
michael@0 | 287 | EVUTIL_ASSERT(npin == nvecs); |
michael@0 | 288 | |
michael@0 | 289 | _evbuffer_incref(buf); |
michael@0 | 290 | if (WSARecv(buf_o->fd, buf_o->buffers, nvecs, &bytesRead, &flags, |
michael@0 | 291 | &ol->overlapped, NULL)) { |
michael@0 | 292 | int error = WSAGetLastError(); |
michael@0 | 293 | if (error != WSA_IO_PENDING) { |
michael@0 | 294 | /* An actual error. */ |
michael@0 | 295 | pin_release(buf_o, EVBUFFER_MEM_PINNED_R); |
michael@0 | 296 | evbuffer_unfreeze(buf, 0); |
michael@0 | 297 | evbuffer_free(buf); /* decref */ |
michael@0 | 298 | goto done; |
michael@0 | 299 | } |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | buf_o->read_in_progress = 1; |
michael@0 | 303 | r = 0; |
michael@0 | 304 | done: |
michael@0 | 305 | EVBUFFER_UNLOCK(buf); |
michael@0 | 306 | return r; |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | evutil_socket_t |
michael@0 | 310 | _evbuffer_overlapped_get_fd(struct evbuffer *buf) |
michael@0 | 311 | { |
michael@0 | 312 | struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); |
michael@0 | 313 | return buf_o ? buf_o->fd : -1; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | void |
michael@0 | 317 | _evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd) |
michael@0 | 318 | { |
michael@0 | 319 | struct evbuffer_overlapped *buf_o = upcast_evbuffer(buf); |
michael@0 | 320 | EVBUFFER_LOCK(buf); |
michael@0 | 321 | /* XXX is this right?, should it cancel current I/O operations? */ |
michael@0 | 322 | if (buf_o) |
michael@0 | 323 | buf_o->fd = fd; |
michael@0 | 324 | EVBUFFER_UNLOCK(buf); |
michael@0 | 325 | } |