A clone of btpd with my configuration changes.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

447 linhas
9.6 KiB

  1. /*
  2. * Copyright (c) 2002, 2003 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #ifdef HAVE_VASPRINTF
  31. /* If we have vasprintf, we need to define this before we include stdio.h. */
  32. #define _GNU_SOURCE
  33. #endif
  34. #include <sys/types.h>
  35. #ifdef HAVE_SYS_TIME_H
  36. #include <sys/time.h>
  37. #endif
  38. #ifdef HAVE_SYS_IOCTL_H
  39. #include <sys/ioctl.h>
  40. #endif
  41. #include <errno.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #ifdef HAVE_STDARG_H
  46. #include <stdarg.h>
  47. #endif
  48. #ifdef HAVE_UNISTD_H
  49. #include <unistd.h>
  50. #endif
  51. #include "event.h"
  52. struct evbuffer *
  53. evbuffer_new(void)
  54. {
  55. struct evbuffer *buffer;
  56. buffer = calloc(1, sizeof(struct evbuffer));
  57. return (buffer);
  58. }
  59. void
  60. evbuffer_free(struct evbuffer *buffer)
  61. {
  62. if (buffer->orig_buffer != NULL)
  63. free(buffer->orig_buffer);
  64. free(buffer);
  65. }
  66. /*
  67. * This is a destructive add. The data from one buffer moves into
  68. * the other buffer.
  69. */
  70. #define SWAP(x,y) do { \
  71. (x)->buffer = (y)->buffer; \
  72. (x)->orig_buffer = (y)->orig_buffer; \
  73. (x)->misalign = (y)->misalign; \
  74. (x)->totallen = (y)->totallen; \
  75. (x)->off = (y)->off; \
  76. } while (0)
  77. int
  78. evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
  79. {
  80. int res;
  81. /* Short cut for better performance */
  82. if (outbuf->off == 0) {
  83. struct evbuffer tmp;
  84. size_t oldoff = inbuf->off;
  85. /* Swap them directly */
  86. SWAP(&tmp, outbuf);
  87. SWAP(outbuf, inbuf);
  88. SWAP(inbuf, &tmp);
  89. /*
  90. * Optimization comes with a price; we need to notify the
  91. * buffer if necessary of the changes. oldoff is the amount
  92. * of data that we tranfered from inbuf to outbuf
  93. */
  94. if (inbuf->off != oldoff && inbuf->cb != NULL)
  95. (*inbuf->cb)(inbuf, oldoff, inbuf->off, inbuf->cbarg);
  96. if (oldoff && outbuf->cb != NULL)
  97. (*outbuf->cb)(outbuf, 0, oldoff, outbuf->cbarg);
  98. return (0);
  99. }
  100. res = evbuffer_add(outbuf, inbuf->buffer, inbuf->off);
  101. if (res == 0) {
  102. /* We drain the input buffer on success */
  103. evbuffer_drain(inbuf, inbuf->off);
  104. }
  105. return (res);
  106. }
  107. int
  108. evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
  109. {
  110. char *buffer;
  111. size_t space;
  112. size_t oldoff = buf->off;
  113. int sz;
  114. for (;;) {
  115. buffer = buf->buffer + buf->off;
  116. space = buf->totallen - buf->misalign - buf->off;
  117. #ifdef WIN32
  118. sz = vsnprintf(buffer, space - 1, fmt, ap);
  119. buffer[space - 1] = '\0';
  120. #else
  121. sz = vsnprintf(buffer, space, fmt, ap);
  122. #endif
  123. if (sz == -1)
  124. return (-1);
  125. if (sz < space) {
  126. buf->off += sz;
  127. if (buf->cb != NULL)
  128. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  129. return (sz);
  130. }
  131. if (evbuffer_expand(buf, sz + 1) == -1)
  132. return (-1);
  133. }
  134. /* NOTREACHED */
  135. }
  136. int
  137. evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
  138. {
  139. int res = -1;
  140. va_list ap;
  141. va_start(ap, fmt);
  142. res = evbuffer_add_vprintf(buf, fmt, ap);
  143. va_end(ap);
  144. return (res);
  145. }
  146. /* Reads data from an event buffer and drains the bytes read */
  147. int
  148. evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen)
  149. {
  150. size_t nread = datlen;
  151. if (nread >= buf->off)
  152. nread = buf->off;
  153. memcpy(data, buf->buffer, nread);
  154. evbuffer_drain(buf, nread);
  155. return (nread);
  156. }
  157. /*
  158. * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
  159. * The returned buffer needs to be freed by the called.
  160. */
  161. char *
  162. evbuffer_readline(struct evbuffer *buffer)
  163. {
  164. u_char *data = EVBUFFER_DATA(buffer);
  165. size_t len = EVBUFFER_LENGTH(buffer);
  166. char *line;
  167. unsigned int i;
  168. for (i = 0; i < len; i++) {
  169. if (data[i] == '\r' || data[i] == '\n')
  170. break;
  171. }
  172. if (i == len)
  173. return (NULL);
  174. if ((line = malloc(i + 1)) == NULL) {
  175. fprintf(stderr, "%s: out of memory\n", __func__);
  176. evbuffer_drain(buffer, i);
  177. return (NULL);
  178. }
  179. memcpy(line, data, i);
  180. line[i] = '\0';
  181. /*
  182. * Some protocols terminate a line with '\r\n', so check for
  183. * that, too.
  184. */
  185. if ( i < len - 1 ) {
  186. char fch = data[i], sch = data[i+1];
  187. /* Drain one more character if needed */
  188. if ( (sch == '\r' || sch == '\n') && sch != fch )
  189. i += 1;
  190. }
  191. evbuffer_drain(buffer, i + 1);
  192. return (line);
  193. }
  194. /* Adds data to an event buffer */
  195. static inline void
  196. evbuffer_align(struct evbuffer *buf)
  197. {
  198. memmove(buf->orig_buffer, buf->buffer, buf->off);
  199. buf->buffer = buf->orig_buffer;
  200. buf->misalign = 0;
  201. }
  202. /* Expands the available space in the event buffer to at least datlen */
  203. int
  204. evbuffer_expand(struct evbuffer *buf, size_t datlen)
  205. {
  206. size_t need = buf->misalign + buf->off + datlen;
  207. /* If we can fit all the data, then we don't have to do anything */
  208. if (buf->totallen >= need)
  209. return (0);
  210. /*
  211. * If the misalignment fulfills our data needs, we just force an
  212. * alignment to happen. Afterwards, we have enough space.
  213. */
  214. if (buf->misalign >= datlen) {
  215. evbuffer_align(buf);
  216. } else {
  217. void *newbuf;
  218. size_t length = buf->totallen;
  219. if (length < 256)
  220. length = 256;
  221. while (length < need)
  222. length <<= 1;
  223. if (buf->orig_buffer != buf->buffer)
  224. evbuffer_align(buf);
  225. if ((newbuf = realloc(buf->buffer, length)) == NULL)
  226. return (-1);
  227. buf->orig_buffer = buf->buffer = newbuf;
  228. buf->totallen = length;
  229. }
  230. return (0);
  231. }
  232. int
  233. evbuffer_add(struct evbuffer *buf, void *data, size_t datlen)
  234. {
  235. size_t need = buf->misalign + buf->off + datlen;
  236. size_t oldoff = buf->off;
  237. if (buf->totallen < need) {
  238. if (evbuffer_expand(buf, datlen) == -1)
  239. return (-1);
  240. }
  241. memcpy(buf->buffer + buf->off, data, datlen);
  242. buf->off += datlen;
  243. if (datlen && buf->cb != NULL)
  244. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  245. return (0);
  246. }
  247. void
  248. evbuffer_drain(struct evbuffer *buf, size_t len)
  249. {
  250. size_t oldoff = buf->off;
  251. if (len >= buf->off) {
  252. buf->off = 0;
  253. buf->buffer = buf->orig_buffer;
  254. buf->misalign = 0;
  255. goto done;
  256. }
  257. buf->buffer += len;
  258. buf->misalign += len;
  259. buf->off -= len;
  260. done:
  261. /* Tell someone about changes in this buffer */
  262. if (buf->off != oldoff && buf->cb != NULL)
  263. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  264. }
  265. /*
  266. * Reads data from a file descriptor into a buffer.
  267. */
  268. #define EVBUFFER_MAX_READ 4096
  269. int
  270. evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
  271. {
  272. u_char *p;
  273. size_t oldoff = buf->off;
  274. int n = EVBUFFER_MAX_READ;
  275. #ifdef WIN32
  276. DWORD dwBytesRead;
  277. #endif
  278. #ifdef FIONREAD
  279. if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
  280. n = EVBUFFER_MAX_READ;
  281. } else if (n > EVBUFFER_MAX_READ && n > howmuch) {
  282. /*
  283. * It's possible that a lot of data is available for
  284. * reading. We do not want to exhaust resources
  285. * before the reader has a chance to do something
  286. * about it. If the reader does not tell us how much
  287. * data we should read, we artifically limit it.
  288. */
  289. if (n > buf->totallen << 2)
  290. n = buf->totallen << 2;
  291. if (n < EVBUFFER_MAX_READ)
  292. n = EVBUFFER_MAX_READ;
  293. }
  294. #endif
  295. if (howmuch < 0 || howmuch > n)
  296. howmuch = n;
  297. /* If we don't have FIONREAD, we might waste some space here */
  298. if (evbuffer_expand(buf, howmuch) == -1)
  299. return (-1);
  300. /* We can append new data at this point */
  301. p = buf->buffer + buf->off;
  302. #ifndef WIN32
  303. n = read(fd, p, howmuch);
  304. if (n == -1)
  305. return (-1);
  306. if (n == 0)
  307. return (0);
  308. #else
  309. n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL);
  310. if (n == 0)
  311. return (-1);
  312. if (dwBytesRead == 0)
  313. return (0);
  314. n = dwBytesRead;
  315. #endif
  316. buf->off += n;
  317. /* Tell someone about changes in this buffer */
  318. if (buf->off != oldoff && buf->cb != NULL)
  319. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  320. return (n);
  321. }
  322. int
  323. evbuffer_write(struct evbuffer *buffer, int fd)
  324. {
  325. int n;
  326. #ifdef WIN32
  327. DWORD dwBytesWritten;
  328. #endif
  329. #ifndef WIN32
  330. n = write(fd, buffer->buffer, buffer->off);
  331. if (n == -1)
  332. return (-1);
  333. if (n == 0)
  334. return (0);
  335. #else
  336. n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL);
  337. if (n == 0)
  338. return (-1);
  339. if (dwBytesWritten == 0)
  340. return (0);
  341. n = dwBytesWritten;
  342. #endif
  343. evbuffer_drain(buffer, n);
  344. return (n);
  345. }
  346. u_char *
  347. evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len)
  348. {
  349. size_t remain = buffer->off;
  350. u_char *search = buffer->buffer;
  351. u_char *p;
  352. while ((p = memchr(search, *what, remain)) != NULL && remain >= len) {
  353. if (memcmp(p, what, len) == 0)
  354. return (p);
  355. search = p + 1;
  356. remain = buffer->off - (size_t)(search - buffer->buffer);
  357. }
  358. return (NULL);
  359. }
  360. void evbuffer_setcb(struct evbuffer *buffer,
  361. void (*cb)(struct evbuffer *, size_t, size_t, void *),
  362. void *cbarg)
  363. {
  364. buffer->cb = cb;
  365. buffer->cbarg = cbarg;
  366. }