| 6 |
| * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 |
| 28 | #include "config.h" |
| 29 |
| 30 | #ifdef WIN32 |
| 31 | #include <winsock2.h> |
| 32 | #include <windows.h> |
| 33 | #endif |
| 34 |
| 35 | #if defined(HAVE_VASPRINTF) && !defined(_GNU_SOURCE) |
| 36 | /* If we have vasprintf, we need to define this before we include stdio.h. */ |
| 37 | #define _GNU_SOURCE |
| 38 | #endif |
| 39 |
| 40 | #include <sys/types.h> |
| 41 |
| 42 | #ifdef HAVE_SYS_TIME_H |
| 43 | #include <sys/time.h> |
| 44 | #endif |
| 45 |
| 46 | #ifdef HAVE_SYS_IOCTL_H |
| 47 | #include <sys/ioctl.h> |
| 48 | #endif |
| 49 |
| 50 | #include <assert.h> |
| 51 | #include <errno.h> |
| 52 | #include <stdio.h> |
| 53 | #include <stdlib.h> |
| 54 | #include <string.h> |
| 55 | #ifdef HAVE_STDARG_H |
| 56 | #include <stdarg.h> |
| 57 | #endif |
| 58 | #ifdef HAVE_UNISTD_H |
| 59 | #include <unistd.h> |
| 60 | #endif |
| 61 |
| 62 | #include "event.h" |
| 63 | #include "config.h" |
| 64 | #include "evutil.h" |
| 65 | #include "./log.h" |
| 66 |
| 67 | struct evbuffer * |
| 68 | evbuffer_new(void) |
| 69 | { |
| 70 | struct evbuffer *buffer; |
| 71 |
| 72 | buffer = calloc(1, sizeof(struct evbuffer)); |
| 73 |
| 74 | return (buffer); |
| 75 | } |
| 76 |
| 77 | void |
| 78 | evbuffer_free(struct evbuffer *buffer) |
| 79 | { |
| 80 | if (buffer->orig_buffer != NULL) |
| 81 | free(buffer->orig_buffer); |
| 82 | free(buffer); |
| 83 | } |
| 84 |
| 85 | /* |
| 86 | * This is a destructive add. The data from one buffer moves into |
| 87 | * the other buffer. |
| 88 | */ |
| 89 |
| 90 | #define SWAP(x,y) do { \ |
| 91 | (x)->buffer = (y)->buffer; \ |
| 92 | (x)->orig_buffer = (y)->orig_buffer; \ |
| 93 | (x)->misalign = (y)->misalign; \ |
| 94 | (x)->totallen = (y)->totallen; \ |
| 95 | (x)->off = (y)->off; \ |
| 96 | } while (0) |
| 97 |
| 98 | int |
| 99 | evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) |
| 100 | { |
| 101 | int res; |
| 102 |
| 103 | /* Short cut for better performance */ |
| 104 | if (outbuf->off == 0) { |
| 105 | struct evbuffer tmp; |
| 106 | size_t oldoff = inbuf->off; |
| 107 |
| 108 | /* Swap them directly */ |
| 109 | SWAP(&tmp, outbuf); |
| 110 | SWAP(outbuf, inbuf); |
| 111 | SWAP(inbuf, &tmp); |
| 112 |
| 113 | /* |
| 114 | * Optimization comes with a price; we need to notify the |
| 115 | * buffer if necessary of the changes. oldoff is the amount |
| 116 | * of data that we transfered from inbuf to outbuf |
| 117 | */ |
| 118 | if (inbuf->off != oldoff && inbuf->cb != NULL) |
| 119 | (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg); |
| 120 | if (oldoff && outbuf->cb != NULL) |
| 121 | (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg); |
| 122 |
| 123 | return (0); |
| 124 | } |
| 125 |
| 126 | res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off); |
| 127 | if (res == 0) { |
| 128 | /* We drain the input buffer on success */ |
| 129 | evbuffer_drain(inbuf, inbuf->off); |
| 130 | } |
| 131 |
| 132 | return (res); |
| 133 | } |
| 134 |
| 135 | int |
| 136 | evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap) |
| 137 | { |
| 138 | char *buffer; |
| 139 | size_t space; |
| 140 | size_t oldoff = buf->off; |
| 141 | int sz; |
| 142 | va_list aq; |
| 143 |
| 144 | /* make sure that at least some space is available */ |
| 145 | if (evbuffer_expand(buf, 64) < 0) |
| 146 | return (-1); |
| 147 | for (;;) { |
| 148 | size_t used = buf->misalign + buf->off; |
| 149 | buffer = (char *)buf->buffer + buf->off; |
| 150 | assert(buf->totallen >= used); |
| 151 | space = buf->totallen - used; |
| 152 |
| 153 | #ifndef va_copy |
| 154 | #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list)) |
| 155 | #endif |
| 156 | va_copy(aq, ap); |
| 157 |
| 158 | sz = evutil_vsnprintf(buffer, space, fmt, aq); |
| 159 |
| 160 | va_end(aq); |
| 161 |
| 162 | if (sz < 0) |
| 163 | return (-1); |
| 164 | if ((size_t)sz < space) { |
| 165 | buf->off += sz; |
| 166 | if (buf->cb != NULL) |
| 167 | (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); |
| 168 | return (sz); |
| 169 | } |
| 170 | if (evbuffer_expand(buf, sz + 1) == -1) |
| 171 | return (-1); |
| 172 |
| 173 | } |
| 174 | /* NOTREACHED */ |
| 175 | } |
| 176 |
| 177 | int |
| 178 | evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) |
| 179 | { |
| 180 | int res = -1; |
| 181 | va_list ap; |
| 182 |
| 183 | va_start(ap, fmt); |
| 184 | res = evbuffer_add_vprintf(buf, fmt, ap); |
| 185 | va_end(ap); |
| 186 |
| 187 | return (res); |
| 188 | } |
| 189 |
| 190 | /* Reads data from an event buffer and drains the bytes read */ |
| 191 |
| 192 | int |
| 193 | evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen) |
| 194 | { |
| 195 | size_t nread = datlen; |
| 196 | if (nread >= buf->off) |
| 197 | nread = buf->off; |
| 198 |
| 199 | memcpy(data, buf->buffer, nread); |
| 200 | evbuffer_drain(buf, nread); |
| 201 |
| 202 | return (nread); |
| 203 | } |
| 204 |
| 205 | /* |
| 206 | * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'. |
| 207 | * The returned buffer needs to be freed by the called. |
| 208 | */ |
| 209 |
| 210 | char * |
| 211 | evbuffer_readline(struct evbuffer *buffer) |
| 212 | { |
| 213 | u_char *data = EVBUFFER_DATA(buffer); |
| 214 | size_t len = EVBUFFER_LENGTH(buffer); |
| 215 | char *line; |
| 216 | unsigned int i; |
| 217 |
| 218 | for (i = 0; i < len; i++) { |
| 219 | if (data[i] == '\r' || data[i] == '\n') |
| 220 | break; |
| 221 | } |
| 222 |
| 223 | if (i == len) |
| 224 | return (NULL); |
| 225 |
| 226 | if ((line = malloc(i + 1)) == NULL) { |
| 227 | fprintf(stderr, "%s: out of memory\n", __func__); |
| 228 | return (NULL); |
| 229 | } |
| 230 |
| 231 | memcpy(line, data, i); |
| 232 | line[i] = '\0'; |
| 233 |
| 234 | /* |
| 235 | * Some protocols terminate a line with '\r\n', so check for |
| 236 | * that, too. |
| 237 | */ |
| 238 | if ( i < len - 1 ) { |
| 239 | char fch = data[i], sch = data[i+1]; |
| 240 |
| 241 | /* Drain one more character if needed */ |
| 242 | if ( (sch == '\r' || sch == '\n') && sch != fch ) |
| 243 | i += 1; |
| 244 | } |
| 245 |
| 246 | evbuffer_drain(buffer, i + 1); |
| 247 |
| 248 | return (line); |
| 249 | } |
| 250 |
| 251 |
| 252 | char * |
| 253 | evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, |
| 254 | enum evbuffer_eol_style eol_style) |
| 255 | { |
| 256 | u_char *data = EVBUFFER_DATA(buffer); |
| 257 | u_char *start_of_eol, *end_of_eol; |
| 258 | size_t len = EVBUFFER_LENGTH(buffer); |
| 259 | char *line; |
| 260 | unsigned int i, n_to_copy, n_to_drain; |
| 261 |
| 262 | if (n_read_out) |
| 263 | *n_read_out = 0; |
| 264 |
| 265 | /* depending on eol_style, set start_of_eol to the first character |
| 266 | * in the newline, and end_of_eol to one after the last character. */ |
| 267 | switch (eol_style) { |
| 268 | case EVBUFFER_EOL_ANY: |
| 269 | for (i = 0; i < len; i++) { |
| 270 | if (data[i] == '\r' || data[i] == '\n') |
| 271 | break; |
| 272 | } |
| 273 | if (i == len) |
| 274 | return (NULL); |
| 275 | start_of_eol = data+i; |
| 276 | ++i; |
| 277 | for ( ; i < len; i++) { |
| 278 | if (data[i] != '\r' && data[i] != '\n') |
| 279 | break; |
| 280 | } |
| 281 | end_of_eol = data+i; |
| 282 | break; |
| 283 | case EVBUFFER_EOL_CRLF: |
| 284 | end_of_eol = memchr(data, '\n', len); |
| 285 | if (!end_of_eol) |
| 286 | return (NULL); |
| 287 | if (end_of_eol > data && *(end_of_eol-1) == '\r') |
| 288 | start_of_eol = end_of_eol - 1; |
| 289 | else |
| 290 | start_of_eol = end_of_eol; |
| 291 | end_of_eol++; /*point to one after the LF. */ |
| 292 | break; |
| 293 | case EVBUFFER_EOL_CRLF_STRICT: { |
| 294 | u_char *cp = data; |
| 295 | while ((cp = memchr(cp, '\r', len-(cp-data)))) { |
| 296 | if (cp < data+len-1 && *(cp+1) == '\n') |
| 297 | break; |
| 298 | if (++cp >= data+len) { |
| 299 | cp = NULL; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | if (!cp) |
| 304 | return (NULL); |
| 305 | start_of_eol = cp; |
| 306 | end_of_eol = cp+2; |
| 307 | break; |
| 308 | } |
| 309 | case EVBUFFER_EOL_LF: |
| 310 | start_of_eol = memchr(data, '\n', len); |
| 311 | if (!start_of_eol) |
| 312 | return (NULL); |
| 313 | end_of_eol = start_of_eol + 1; |
| 314 | break; |
| 315 | default: |
| 316 | return (NULL); |
| 317 | } |
| 318 |
| 319 | n_to_copy = start_of_eol - data; |
| 320 | n_to_drain = end_of_eol - data; |
| 321 |
| 322 | if ((line = malloc(n_to_copy+1)) == NULL) { |
| 323 | event_warn("%s: out of memory\n", __func__); |
| 324 | return (NULL); |
| 325 | } |
| 326 |
| 327 | memcpy(line, data, n_to_copy); |
| 328 | line[n_to_copy] = '\0'; |
| 329 |
| 330 | evbuffer_drain(buffer, n_to_drain); |
| 331 | if (n_read_out) |
| 332 | *n_read_out = (size_t)n_to_copy; |
| 333 |
| 334 | return (line); |
| 335 | } |
| 336 |
| 337 | /* Adds data to an event buffer */ |
| 338 |
| 339 | static void |
| 340 | evbuffer_align(struct evbuffer *buf) |
| 341 | { |
| 342 | memmove(buf->orig_buffer, buf->buffer, buf->off); |
| 343 | buf->buffer = buf->orig_buffer; |
| 344 | buf->misalign = 0; |
| 345 | } |
| 346 |
| 347 | #ifndef SIZE_MAX |
| 348 | #define SIZE_MAX ((size_t)-1) |
| 349 | #endif |
| 350 |
| 351 | /* Expands the available space in the event buffer to at least datlen */ |
| 352 |
| 353 | int |
| 354 | evbuffer_expand(struct evbuffer *buf, size_t datlen) |
| 355 | { |
| 356 | size_t used = buf->misalign + buf->off; |
| 357 |
| 358 | assert(buf->totallen >= used); |
| 359 |
| 360 | /* If we can fit all the data, then we don't have to do anything */ |
| 361 | if (buf->totallen - used >= datlen) |
| 362 | return (0); |
| 363 | /* If we would need to overflow to fit this much data, we can't |
| 364 | * do anything. */ |
| 365 | if (datlen > SIZE_MAX - buf->off) |
| 366 | return (-1); |
| 367 |
| 368 | /* |
| 369 | * If the misalignment fulfills our data needs, we just force an |
| 370 | * alignment to happen. Afterwards, we have enough space. |
| 371 | */ |
| 372 | if (buf->totallen - buf->off >= datlen) { |
| 373 | evbuffer_align(buf); |
| 374 | } else { |
| 375 | void *newbuf; |
| 376 | size_t length = buf->totallen; |
| 377 | size_t need = buf->off + datlen; |
| 378 |
| 379 | if (length < 256) |
| 380 | length = 256; |
| 381 | if (need < SIZE_MAX / 2) { |
| 382 | while (length < need) { |
| 383 | length <<= 1; |
| 384 | } |
| 385 | } else { |
| 386 | length = need; |
| 387 | } |
| 388 |
| 389 | if (buf->orig_buffer != buf->buffer) |
| 390 | evbuffer_align(buf); |
| 391 | if ((newbuf = realloc(buf->buffer, length)) == NULL) |
| 392 | return (-1); |
| 393 |
| 394 | buf->orig_buffer = buf->buffer = newbuf; |
| 395 | buf->totallen = length; |
| 396 | } |
| 397 |
| 398 | return (0); |
| 399 | } |
| 400 |
| 401 | int |
| 402 | evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen) |
| 403 | { |
| 404 | size_t used = buf->misalign + buf->off; |
| 405 | size_t oldoff = buf->off; |
| 406 |
| 407 | if (buf->totallen - used < datlen) { |
| 408 | if (evbuffer_expand(buf, datlen) == -1) |
| 409 | return (-1); |
| 410 | } |
| 411 |
| 412 | memcpy(buf->buffer + buf->off, data, datlen); |
| 413 | buf->off += datlen; |
| 414 |
| 415 | if (datlen && buf->cb != NULL) |
| 416 | (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); |
| 417 |
| 418 | return (0); |
| 419 | } |
| 420 |
| 421 | void |
| 422 | evbuffer_drain(struct evbuffer *buf, size_t len) |
| 423 | { |
| 424 | size_t oldoff = buf->off; |
| 425 |
| 426 | if (len >= buf->off) { |
| 427 | buf->off = 0; |
| 428 | buf->buffer = buf->orig_buffer; |
| 429 | buf->misalign = 0; |
| 430 | goto done; |
| 431 | } |
| 432 |
| 433 | buf->buffer += len; |
| 434 | buf->misalign += len; |
| 435 |
| 436 | buf->off -= len; |
| 437 |
| 438 | done: |
| 439 | /* Tell someone about changes in this buffer */ |
| 440 | if (buf->off != oldoff && buf->cb != NULL) |
| 441 | (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); |
| 442 |
| 443 | } |
| 444 |
| 445 | /* |
| 446 | * Reads data from a file descriptor into a buffer. |
| 447 | */ |
| 448 |
| 449 | #define EVBUFFER_MAX_READ 4096 |
| 450 |
| 451 | int |
| 452 | evbuffer_read(struct evbuffer *buf, int fd, int howmuch) |
| 453 | { |
| 454 | u_char *p; |
| 455 | size_t oldoff = buf->off; |
| 456 | int n = EVBUFFER_MAX_READ; |
| 457 |
| 458 | #if defined(FIONREAD) |
| 459 | #ifdef WIN32 |
| 460 | long lng = n; |
| 461 | if (ioctlsocket(fd, FIONREAD, &lng) == -1 || (n=lng) <= 0) { |
| 462 | #else |
| 463 | if (ioctl(fd, FIONREAD, &n) == -1 || n <= 0) { |
| 464 | #endif |
| 465 | n = EVBUFFER_MAX_READ; |
| 466 | } else if (n > EVBUFFER_MAX_READ && n > howmuch) { |
| 467 | /* |
| 468 | * It's possible that a lot of data is available for |
| 469 | * reading. We do not want to exhaust resources |
| 470 | * before the reader has a chance to do something |
| 471 | * about it. If the reader does not tell us how much |
| 472 | * data we should read, we artifically limit it. |
| 473 | */ |
| 474 | if ((size_t)n > buf->totallen << 2) |
| 475 | n = buf->totallen << 2; |
| 476 | if (n < EVBUFFER_MAX_READ) |
| 477 | n = EVBUFFER_MAX_READ; |
| 478 | } |
| 479 | #endif |
| 480 | if (howmuch < 0 || howmuch > n) |
| 481 | howmuch = n; |
| 482 |
| 483 | /* If we don't have FIONREAD, we might waste some space here */ |
| 484 | if (evbuffer_expand(buf, howmuch) == -1) |
| 485 | return (-1); |
| 486 |
| 487 | /* We can append new data at this point */ |
| 488 | p = buf->buffer + buf->off; |
| 489 |
| 490 | #ifndef WIN32 |
| 491 | n = read(fd, p, howmuch); |
| 492 | #else |
| 493 | n = recv(fd, p, howmuch, 0); |
| 494 | #endif |
| 495 | if (n == -1) |
| 496 | return (-1); |
| 497 | if (n == 0) |
| 498 | return (0); |
| 499 |
| 500 | buf->off += n; |
| 501 |
| 502 | /* Tell someone about changes in this buffer */ |
| 503 | if (buf->off != oldoff && buf->cb != NULL) |
| 504 | (*buf->cb)(buf, oldoff, buf->off, buf->cbarg); |
| 505 |
| 506 | return (n); |
| 507 | } |
| 508 |
| 509 | int |
| 510 | evbuffer_write(struct evbuffer *buffer, int fd) |
| 511 | { |
| 512 | int n; |
| 513 |
| 514 | #ifndef WIN32 |
| 515 | n = write(fd, buffer->buffer, buffer->off); |
| 516 | #else |
| 517 | n = send(fd, buffer->buffer, buffer->off, 0); |
| 518 | #endif |
| 519 | if (n == -1) |
| 520 | return (-1); |
| 521 | if (n == 0) |
| 522 | return (0); |
| 523 | evbuffer_drain(buffer, n); |
| 524 |
| 525 | return (n); |
| 526 | } |
| 527 |
| 528 | u_char * |
| 529 | evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len) |
| 530 | { |
| 531 | u_char *search = buffer->buffer, *end = search + buffer->off; |
| 532 | u_char *p; |
| 533 |
| 534 | while (search < end && |
| 535 | (p = memchr(search, *what, end - search)) != NULL) { |
| 536 | if (p + len > end) |
| 537 | break; |
| 538 | if (memcmp(p, what, len) == 0) |
| 539 | return (p); |
| 540 | search = p + 1; |
| 541 | } |
| 542 |
| 543 | return (NULL); |
| 544 | } |
| 545 |
| 546 | void evbuffer_setcb(struct evbuffer *buffer, |
| 547 | void (*cb)(struct evbuffer *, size_t, size_t, void *), |
| 548 | void *cbarg) |
| 549 | { |
| 550 | buffer->cb = cb; |
| 551 | buffer->cbarg = cbarg; |
| 552 | } |
| 553 |