A clone of btpd with my configuration changes.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

450 satır
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. va_list aq;
  115. for (;;) {
  116. buffer = buf->buffer + buf->off;
  117. space = buf->totallen - buf->misalign - buf->off;
  118. va_copy(aq, ap);
  119. #ifdef WIN32
  120. sz = vsnprintf(buffer, space - 1, fmt, aq);
  121. buffer[space - 1] = '\0';
  122. #else
  123. sz = vsnprintf(buffer, space, fmt, aq);
  124. #endif
  125. va_end(aq);
  126. if (sz == -1)
  127. return (-1);
  128. if (sz < space) {
  129. buf->off += sz;
  130. if (buf->cb != NULL)
  131. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  132. return (sz);
  133. }
  134. if (evbuffer_expand(buf, sz + 1) == -1)
  135. return (-1);
  136. }
  137. /* NOTREACHED */
  138. }
  139. int
  140. evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
  141. {
  142. int res = -1;
  143. va_list ap;
  144. va_start(ap, fmt);
  145. res = evbuffer_add_vprintf(buf, fmt, ap);
  146. va_end(ap);
  147. return (res);
  148. }
  149. /* Reads data from an event buffer and drains the bytes read */
  150. int
  151. evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen)
  152. {
  153. size_t nread = datlen;
  154. if (nread >= buf->off)
  155. nread = buf->off;
  156. memcpy(data, buf->buffer, nread);
  157. evbuffer_drain(buf, nread);
  158. return (nread);
  159. }
  160. /*
  161. * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
  162. * The returned buffer needs to be freed by the called.
  163. */
  164. char *
  165. evbuffer_readline(struct evbuffer *buffer)
  166. {
  167. u_char *data = EVBUFFER_DATA(buffer);
  168. size_t len = EVBUFFER_LENGTH(buffer);
  169. char *line;
  170. unsigned int i;
  171. for (i = 0; i < len; i++) {
  172. if (data[i] == '\r' || data[i] == '\n')
  173. break;
  174. }
  175. if (i == len)
  176. return (NULL);
  177. if ((line = malloc(i + 1)) == NULL) {
  178. fprintf(stderr, "%s: out of memory\n", __func__);
  179. evbuffer_drain(buffer, i);
  180. return (NULL);
  181. }
  182. memcpy(line, data, i);
  183. line[i] = '\0';
  184. /*
  185. * Some protocols terminate a line with '\r\n', so check for
  186. * that, too.
  187. */
  188. if ( i < len - 1 ) {
  189. char fch = data[i], sch = data[i+1];
  190. /* Drain one more character if needed */
  191. if ( (sch == '\r' || sch == '\n') && sch != fch )
  192. i += 1;
  193. }
  194. evbuffer_drain(buffer, i + 1);
  195. return (line);
  196. }
  197. /* Adds data to an event buffer */
  198. static inline void
  199. evbuffer_align(struct evbuffer *buf)
  200. {
  201. memmove(buf->orig_buffer, buf->buffer, buf->off);
  202. buf->buffer = buf->orig_buffer;
  203. buf->misalign = 0;
  204. }
  205. /* Expands the available space in the event buffer to at least datlen */
  206. int
  207. evbuffer_expand(struct evbuffer *buf, size_t datlen)
  208. {
  209. size_t need = buf->misalign + buf->off + datlen;
  210. /* If we can fit all the data, then we don't have to do anything */
  211. if (buf->totallen >= need)
  212. return (0);
  213. /*
  214. * If the misalignment fulfills our data needs, we just force an
  215. * alignment to happen. Afterwards, we have enough space.
  216. */
  217. if (buf->misalign >= datlen) {
  218. evbuffer_align(buf);
  219. } else {
  220. void *newbuf;
  221. size_t length = buf->totallen;
  222. if (length < 256)
  223. length = 256;
  224. while (length < need)
  225. length <<= 1;
  226. if (buf->orig_buffer != buf->buffer)
  227. evbuffer_align(buf);
  228. if ((newbuf = realloc(buf->buffer, length)) == NULL)
  229. return (-1);
  230. buf->orig_buffer = buf->buffer = newbuf;
  231. buf->totallen = length;
  232. }
  233. return (0);
  234. }
  235. int
  236. evbuffer_add(struct evbuffer *buf, void *data, size_t datlen)
  237. {
  238. size_t need = buf->misalign + buf->off + datlen;
  239. size_t oldoff = buf->off;
  240. if (buf->totallen < need) {
  241. if (evbuffer_expand(buf, datlen) == -1)
  242. return (-1);
  243. }
  244. memcpy(buf->buffer + buf->off, data, datlen);
  245. buf->off += datlen;
  246. if (datlen && buf->cb != NULL)
  247. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  248. return (0);
  249. }
  250. void
  251. evbuffer_drain(struct evbuffer *buf, size_t len)
  252. {
  253. size_t oldoff = buf->off;
  254. if (len >= buf->off) {
  255. buf->off = 0;
  256. buf->buffer = buf->orig_buffer;
  257. buf->misalign = 0;
  258. goto done;
  259. }
  260. buf->buffer += len;
  261. buf->misalign += len;
  262. buf->off -= len;
  263. done:
  264. /* Tell someone about changes in this buffer */
  265. if (buf->off != oldoff && buf->cb != NULL)
  266. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  267. }
  268. /*
  269. * Reads data from a file descriptor into a buffer.
  270. */
  271. #define EVBUFFER_MAX_READ 4096
  272. int
  273. evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
  274. {
  275. u_char *p;
  276. size_t oldoff = buf->off;
  277. int n = EVBUFFER_MAX_READ;
  278. #ifdef WIN32
  279. DWORD dwBytesRead;
  280. #endif
  281. #ifdef FIONREAD
  282. if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
  283. n = EVBUFFER_MAX_READ;
  284. } else if (n > EVBUFFER_MAX_READ && n > howmuch) {
  285. /*
  286. * It's possible that a lot of data is available for
  287. * reading. We do not want to exhaust resources
  288. * before the reader has a chance to do something
  289. * about it. If the reader does not tell us how much
  290. * data we should read, we artifically limit it.
  291. */
  292. if (n > buf->totallen << 2)
  293. n = buf->totallen << 2;
  294. if (n < EVBUFFER_MAX_READ)
  295. n = EVBUFFER_MAX_READ;
  296. }
  297. #endif
  298. if (howmuch < 0 || howmuch > n)
  299. howmuch = n;
  300. /* If we don't have FIONREAD, we might waste some space here */
  301. if (evbuffer_expand(buf, howmuch) == -1)
  302. return (-1);
  303. /* We can append new data at this point */
  304. p = buf->buffer + buf->off;
  305. #ifndef WIN32
  306. n = read(fd, p, howmuch);
  307. if (n == -1)
  308. return (-1);
  309. if (n == 0)
  310. return (0);
  311. #else
  312. n = ReadFile((HANDLE)fd, p, howmuch, &dwBytesRead, NULL);
  313. if (n == 0)
  314. return (-1);
  315. if (dwBytesRead == 0)
  316. return (0);
  317. n = dwBytesRead;
  318. #endif
  319. buf->off += n;
  320. /* Tell someone about changes in this buffer */
  321. if (buf->off != oldoff && buf->cb != NULL)
  322. (*buf->cb)(buf, oldoff, buf->off, buf->cbarg);
  323. return (n);
  324. }
  325. int
  326. evbuffer_write(struct evbuffer *buffer, int fd)
  327. {
  328. int n;
  329. #ifdef WIN32
  330. DWORD dwBytesWritten;
  331. #endif
  332. #ifndef WIN32
  333. n = write(fd, buffer->buffer, buffer->off);
  334. if (n == -1)
  335. return (-1);
  336. if (n == 0)
  337. return (0);
  338. #else
  339. n = WriteFile((HANDLE)fd, buffer->buffer, buffer->off, &dwBytesWritten, NULL);
  340. if (n == 0)
  341. return (-1);
  342. if (dwBytesWritten == 0)
  343. return (0);
  344. n = dwBytesWritten;
  345. #endif
  346. evbuffer_drain(buffer, n);
  347. return (n);
  348. }
  349. u_char *
  350. evbuffer_find(struct evbuffer *buffer, const u_char *what, size_t len)
  351. {
  352. size_t remain = buffer->off;
  353. u_char *search = buffer->buffer;
  354. u_char *p;
  355. while ((p = memchr(search, *what, remain)) != NULL && remain >= len) {
  356. if (memcmp(p, what, len) == 0)
  357. return (p);
  358. search = p + 1;
  359. remain = buffer->off - (size_t)(search - buffer->buffer);
  360. }
  361. return (NULL);
  362. }
  363. void evbuffer_setcb(struct evbuffer *buffer,
  364. void (*cb)(struct evbuffer *, size_t, size_t, void *),
  365. void *cbarg)
  366. {
  367. buffer->cb = cb;
  368. buffer->cbarg = cbarg;
  369. }