buffers.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers.c
  8. * \brief Implements a generic buffer interface.
  9. *
  10. * A buf_t is a (fairly) opaque byte-oriented FIFO that can read to or flush
  11. * from memory, sockets, file descriptors, TLS connections, or another buf_t.
  12. * Buffers are implemented as linked lists of memory chunks.
  13. *
  14. * All socket-backed and TLS-based connection_t objects have a pair of
  15. * buffers: one for incoming data, and one for outcoming data. These are fed
  16. * and drained from functions in connection.c, trigged by events that are
  17. * monitored in main.c.
  18. **/
  19. #define BUFFERS_PRIVATE
  20. #include "orconfig.h"
  21. #include <stddef.h>
  22. #include "buffers.h"
  23. #include "compat.h"
  24. #include "compress.h"
  25. #include "util.h"
  26. #include "torint.h"
  27. #include "torlog.h"
  28. #include "tortls.h"
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. //#define PARANOIA
  33. #ifdef PARANOIA
  34. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  35. * <b>buf</b> is well-formed. */
  36. #define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
  37. #else
  38. #define check() STMT_NIL
  39. #endif
  40. /* Implementation notes:
  41. *
  42. * After flirting with memmove, and dallying with ring-buffers, we're finally
  43. * getting up to speed with the 1970s and implementing buffers as a linked
  44. * list of small chunks. Each buffer has such a list; data is removed from
  45. * the head of the list, and added at the tail. The list is singly linked,
  46. * and the buffer keeps a pointer to the head and the tail.
  47. *
  48. * Every chunk, except the tail, contains at least one byte of data. Data in
  49. * each chunk is contiguous.
  50. *
  51. * When you need to treat the first N characters on a buffer as a contiguous
  52. * string, use the buf_pullup function to make them so. Don't do this more
  53. * than necessary.
  54. *
  55. * The major free Unix kernels have handled buffers like this since, like,
  56. * forever.
  57. */
  58. /* Chunk manipulation functions */
  59. #define CHUNK_HEADER_LEN offsetof(chunk_t, mem[0])
  60. /* We leave this many NUL bytes at the end of the buffer. */
  61. #ifdef DISABLE_MEMORY_SENTINELS
  62. #define SENTINEL_LEN 0
  63. #else
  64. #define SENTINEL_LEN 4
  65. #endif
  66. /* Header size plus NUL bytes at the end */
  67. #define CHUNK_OVERHEAD (CHUNK_HEADER_LEN + SENTINEL_LEN)
  68. /** Return the number of bytes needed to allocate a chunk to hold
  69. * <b>memlen</b> bytes. */
  70. #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_OVERHEAD + (memlen))
  71. /** Return the number of usable bytes in a chunk allocated with
  72. * malloc(<b>memlen</b>). */
  73. #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_OVERHEAD)
  74. #define DEBUG_SENTINEL
  75. #if defined(DEBUG_SENTINEL) && !defined(DISABLE_MEMORY_SENTINELS)
  76. #define DBG_S(s) s
  77. #else
  78. #define DBG_S(s) (void)0
  79. #endif
  80. #ifdef DISABLE_MEMORY_SENTINELS
  81. #define CHUNK_SET_SENTINEL(chunk, alloclen) STMT_NIL
  82. #else
  83. #define CHUNK_SET_SENTINEL(chunk, alloclen) do { \
  84. uint8_t *a = (uint8_t*) &(chunk)->mem[(chunk)->memlen]; \
  85. DBG_S(uint8_t *b = &((uint8_t*)(chunk))[(alloclen)-SENTINEL_LEN]); \
  86. DBG_S(tor_assert(a == b)); \
  87. memset(a,0,SENTINEL_LEN); \
  88. } while (0)
  89. #endif
  90. /** Return the next character in <b>chunk</b> onto which data can be appended.
  91. * If the chunk is full, this might be off the end of chunk->mem. */
  92. static inline char *
  93. CHUNK_WRITE_PTR(chunk_t *chunk)
  94. {
  95. return chunk->data + chunk->datalen;
  96. }
  97. /** Return the number of bytes that can be written onto <b>chunk</b> without
  98. * running out of space. */
  99. static inline size_t
  100. CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
  101. {
  102. return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
  103. }
  104. /** Move all bytes stored in <b>chunk</b> to the front of <b>chunk</b>->mem,
  105. * to free up space at the end. */
  106. static inline void
  107. chunk_repack(chunk_t *chunk)
  108. {
  109. if (chunk->datalen && chunk->data != &chunk->mem[0]) {
  110. memmove(chunk->mem, chunk->data, chunk->datalen);
  111. }
  112. chunk->data = &chunk->mem[0];
  113. }
  114. /** Keep track of total size of allocated chunks for consistency asserts */
  115. static size_t total_bytes_allocated_in_chunks = 0;
  116. static void
  117. buf_chunk_free_unchecked(chunk_t *chunk)
  118. {
  119. if (!chunk)
  120. return;
  121. #ifdef DEBUG_CHUNK_ALLOC
  122. tor_assert(CHUNK_ALLOC_SIZE(chunk->memlen) == chunk->DBG_alloc);
  123. #endif
  124. tor_assert(total_bytes_allocated_in_chunks >=
  125. CHUNK_ALLOC_SIZE(chunk->memlen));
  126. total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
  127. tor_free(chunk);
  128. }
  129. static inline chunk_t *
  130. chunk_new_with_alloc_size(size_t alloc)
  131. {
  132. chunk_t *ch;
  133. ch = tor_malloc(alloc);
  134. ch->next = NULL;
  135. ch->datalen = 0;
  136. #ifdef DEBUG_CHUNK_ALLOC
  137. ch->DBG_alloc = alloc;
  138. #endif
  139. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  140. total_bytes_allocated_in_chunks += alloc;
  141. ch->data = &ch->mem[0];
  142. CHUNK_SET_SENTINEL(ch, alloc);
  143. return ch;
  144. }
  145. /** Expand <b>chunk</b> until it can hold <b>sz</b> bytes, and return a
  146. * new pointer to <b>chunk</b>. Old pointers are no longer valid. */
  147. static inline chunk_t *
  148. chunk_grow(chunk_t *chunk, size_t sz)
  149. {
  150. off_t offset;
  151. const size_t memlen_orig = chunk->memlen;
  152. const size_t orig_alloc = CHUNK_ALLOC_SIZE(memlen_orig);
  153. const size_t new_alloc = CHUNK_ALLOC_SIZE(sz);
  154. tor_assert(sz > chunk->memlen);
  155. offset = chunk->data - chunk->mem;
  156. chunk = tor_realloc(chunk, new_alloc);
  157. chunk->memlen = sz;
  158. chunk->data = chunk->mem + offset;
  159. #ifdef DEBUG_CHUNK_ALLOC
  160. tor_assert(chunk->DBG_alloc == orig_alloc);
  161. chunk->DBG_alloc = new_alloc;
  162. #endif
  163. total_bytes_allocated_in_chunks += new_alloc - orig_alloc;
  164. CHUNK_SET_SENTINEL(chunk, new_alloc);
  165. return chunk;
  166. }
  167. /** If a read onto the end of a chunk would be smaller than this number, then
  168. * just start a new chunk. */
  169. #define MIN_READ_LEN 8
  170. /** Every chunk should take up at least this many bytes. */
  171. #define MIN_CHUNK_ALLOC 256
  172. /** No chunk should take up more than this many bytes. */
  173. #define MAX_CHUNK_ALLOC 65536
  174. /** Return the allocation size we'd like to use to hold <b>target</b>
  175. * bytes. */
  176. STATIC size_t
  177. preferred_chunk_size(size_t target)
  178. {
  179. tor_assert(target <= SIZE_T_CEILING - CHUNK_OVERHEAD);
  180. if (CHUNK_ALLOC_SIZE(target) >= MAX_CHUNK_ALLOC)
  181. return CHUNK_ALLOC_SIZE(target);
  182. size_t sz = MIN_CHUNK_ALLOC;
  183. while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
  184. sz <<= 1;
  185. }
  186. return sz;
  187. }
  188. /** Collapse data from the first N chunks from <b>buf</b> into buf->head,
  189. * growing it as necessary, until buf->head has the first <b>bytes</b> bytes
  190. * of data from the buffer, or until buf->head has all the data in <b>buf</b>.
  191. *
  192. * Set *<b>head_out</b> to point to the first byte of available data, and
  193. * *<b>len_out</b> to the number of bytes of data available at
  194. * *<b>head_out</b>. Note that *<b>len_out</b> may be more or less than
  195. * <b>bytes</b>, depending on the number of bytes available.
  196. */
  197. void
  198. buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
  199. {
  200. chunk_t *dest, *src;
  201. size_t capacity;
  202. if (!buf->head) {
  203. *head_out = NULL;
  204. *len_out = 0;
  205. return;
  206. }
  207. check();
  208. if (buf->datalen < bytes)
  209. bytes = buf->datalen;
  210. capacity = bytes;
  211. if (buf->head->datalen >= bytes) {
  212. *head_out = buf->head->data;
  213. *len_out = buf->head->datalen;
  214. return;
  215. }
  216. if (buf->head->memlen >= capacity) {
  217. /* We don't need to grow the first chunk, but we might need to repack it.*/
  218. size_t needed = capacity - buf->head->datalen;
  219. if (CHUNK_REMAINING_CAPACITY(buf->head) < needed)
  220. chunk_repack(buf->head);
  221. tor_assert(CHUNK_REMAINING_CAPACITY(buf->head) >= needed);
  222. } else {
  223. chunk_t *newhead;
  224. size_t newsize;
  225. /* We need to grow the chunk. */
  226. chunk_repack(buf->head);
  227. newsize = CHUNK_SIZE_WITH_ALLOC(preferred_chunk_size(capacity));
  228. newhead = chunk_grow(buf->head, newsize);
  229. tor_assert(newhead->memlen >= capacity);
  230. if (newhead != buf->head) {
  231. if (buf->tail == buf->head)
  232. buf->tail = newhead;
  233. buf->head = newhead;
  234. }
  235. }
  236. dest = buf->head;
  237. while (dest->datalen < bytes) {
  238. size_t n = bytes - dest->datalen;
  239. src = dest->next;
  240. tor_assert(src);
  241. if (n >= src->datalen) {
  242. memcpy(CHUNK_WRITE_PTR(dest), src->data, src->datalen);
  243. dest->datalen += src->datalen;
  244. dest->next = src->next;
  245. if (buf->tail == src)
  246. buf->tail = dest;
  247. buf_chunk_free_unchecked(src);
  248. } else {
  249. memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
  250. dest->datalen += n;
  251. src->data += n;
  252. src->datalen -= n;
  253. tor_assert(dest->datalen == bytes);
  254. }
  255. }
  256. check();
  257. *head_out = buf->head->data;
  258. *len_out = buf->head->datalen;
  259. }
  260. #ifdef TOR_UNIT_TESTS
  261. /* Write sz bytes from cp into a newly allocated buffer buf.
  262. * Returns NULL when passed a NULL cp or zero sz.
  263. * Asserts on failure: only for use in unit tests.
  264. * buf must be freed using buf_free(). */
  265. buf_t *
  266. buf_new_with_data(const char *cp, size_t sz)
  267. {
  268. /* Validate arguments */
  269. if (!cp || sz <= 0) {
  270. return NULL;
  271. }
  272. tor_assert(sz < SSIZE_T_CEILING);
  273. /* Allocate a buffer */
  274. buf_t *buf = buf_new_with_capacity(sz);
  275. tor_assert(buf);
  276. assert_buf_ok(buf);
  277. tor_assert(!buf->head);
  278. /* Allocate a chunk that is sz bytes long */
  279. buf->head = chunk_new_with_alloc_size(CHUNK_ALLOC_SIZE(sz));
  280. buf->tail = buf->head;
  281. tor_assert(buf->head);
  282. assert_buf_ok(buf);
  283. tor_assert(buf_allocation(buf) >= sz);
  284. /* Copy the data and size the buffers */
  285. tor_assert(sz <= buf_slack(buf));
  286. tor_assert(sz <= CHUNK_REMAINING_CAPACITY(buf->head));
  287. memcpy(&buf->head->mem[0], cp, sz);
  288. buf->datalen = sz;
  289. buf->head->datalen = sz;
  290. buf->head->data = &buf->head->mem[0];
  291. assert_buf_ok(buf);
  292. /* Make sure everything is large enough */
  293. tor_assert(buf_allocation(buf) >= sz);
  294. tor_assert(buf_allocation(buf) >= buf_datalen(buf) + buf_slack(buf));
  295. /* Does the buffer implementation allocate more than the requested size?
  296. * (for example, by rounding up). If so, these checks will fail. */
  297. tor_assert(buf_datalen(buf) == sz);
  298. tor_assert(buf_slack(buf) == 0);
  299. return buf;
  300. }
  301. #endif
  302. /** Remove the first <b>n</b> bytes from buf. */
  303. void
  304. buf_remove_from_front(buf_t *buf, size_t n)
  305. {
  306. tor_assert(buf->datalen >= n);
  307. while (n) {
  308. tor_assert(buf->head);
  309. if (buf->head->datalen > n) {
  310. buf->head->datalen -= n;
  311. buf->head->data += n;
  312. buf->datalen -= n;
  313. return;
  314. } else {
  315. chunk_t *victim = buf->head;
  316. n -= victim->datalen;
  317. buf->datalen -= victim->datalen;
  318. buf->head = victim->next;
  319. if (buf->tail == victim)
  320. buf->tail = NULL;
  321. buf_chunk_free_unchecked(victim);
  322. }
  323. }
  324. check();
  325. }
  326. /** Create and return a new buf with default chunk capacity <b>size</b>.
  327. */
  328. buf_t *
  329. buf_new_with_capacity(size_t size)
  330. {
  331. buf_t *b = buf_new();
  332. b->default_chunk_size = preferred_chunk_size(size);
  333. return b;
  334. }
  335. /** Allocate and return a new buffer with default capacity. */
  336. buf_t *
  337. buf_new(void)
  338. {
  339. buf_t *buf = tor_malloc_zero(sizeof(buf_t));
  340. buf->magic = BUFFER_MAGIC;
  341. buf->default_chunk_size = 4096;
  342. return buf;
  343. }
  344. size_t
  345. buf_get_default_chunk_size(const buf_t *buf)
  346. {
  347. return buf->default_chunk_size;
  348. }
  349. /** Remove all data from <b>buf</b>. */
  350. void
  351. buf_clear(buf_t *buf)
  352. {
  353. chunk_t *chunk, *next;
  354. buf->datalen = 0;
  355. for (chunk = buf->head; chunk; chunk = next) {
  356. next = chunk->next;
  357. buf_chunk_free_unchecked(chunk);
  358. }
  359. buf->head = buf->tail = NULL;
  360. }
  361. /** Return the number of bytes stored in <b>buf</b> */
  362. MOCK_IMPL(size_t,
  363. buf_datalen, (const buf_t *buf))
  364. {
  365. return buf->datalen;
  366. }
  367. /** Return the total length of all chunks used in <b>buf</b>. */
  368. size_t
  369. buf_allocation(const buf_t *buf)
  370. {
  371. size_t total = 0;
  372. const chunk_t *chunk;
  373. for (chunk = buf->head; chunk; chunk = chunk->next) {
  374. total += CHUNK_ALLOC_SIZE(chunk->memlen);
  375. }
  376. return total;
  377. }
  378. /** Return the number of bytes that can be added to <b>buf</b> without
  379. * performing any additional allocation. */
  380. size_t
  381. buf_slack(const buf_t *buf)
  382. {
  383. if (!buf->tail)
  384. return 0;
  385. else
  386. return CHUNK_REMAINING_CAPACITY(buf->tail);
  387. }
  388. /** Release storage held by <b>buf</b>. */
  389. void
  390. buf_free(buf_t *buf)
  391. {
  392. if (!buf)
  393. return;
  394. buf_clear(buf);
  395. buf->magic = 0xdeadbeef;
  396. tor_free(buf);
  397. }
  398. /** Return a new copy of <b>in_chunk</b> */
  399. static chunk_t *
  400. chunk_copy(const chunk_t *in_chunk)
  401. {
  402. chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen));
  403. total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(in_chunk->memlen);
  404. #ifdef DEBUG_CHUNK_ALLOC
  405. newch->DBG_alloc = CHUNK_ALLOC_SIZE(in_chunk->memlen);
  406. #endif
  407. newch->next = NULL;
  408. if (in_chunk->data) {
  409. off_t offset = in_chunk->data - in_chunk->mem;
  410. newch->data = newch->mem + offset;
  411. }
  412. return newch;
  413. }
  414. /** Return a new copy of <b>buf</b> */
  415. buf_t *
  416. buf_copy(const buf_t *buf)
  417. {
  418. chunk_t *ch;
  419. buf_t *out = buf_new();
  420. out->default_chunk_size = buf->default_chunk_size;
  421. for (ch = buf->head; ch; ch = ch->next) {
  422. chunk_t *newch = chunk_copy(ch);
  423. if (out->tail) {
  424. out->tail->next = newch;
  425. out->tail = newch;
  426. } else {
  427. out->head = out->tail = newch;
  428. }
  429. }
  430. out->datalen = buf->datalen;
  431. return out;
  432. }
  433. /** Append a new chunk with enough capacity to hold <b>capacity</b> bytes to
  434. * the tail of <b>buf</b>. If <b>capped</b>, don't allocate a chunk bigger
  435. * than MAX_CHUNK_ALLOC. */
  436. static chunk_t *
  437. buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
  438. {
  439. chunk_t *chunk;
  440. if (CHUNK_ALLOC_SIZE(capacity) < buf->default_chunk_size) {
  441. chunk = chunk_new_with_alloc_size(buf->default_chunk_size);
  442. } else if (capped && CHUNK_ALLOC_SIZE(capacity) > MAX_CHUNK_ALLOC) {
  443. chunk = chunk_new_with_alloc_size(MAX_CHUNK_ALLOC);
  444. } else {
  445. chunk = chunk_new_with_alloc_size(preferred_chunk_size(capacity));
  446. }
  447. chunk->inserted_time = (uint32_t)monotime_coarse_absolute_msec();
  448. if (buf->tail) {
  449. tor_assert(buf->head);
  450. buf->tail->next = chunk;
  451. buf->tail = chunk;
  452. } else {
  453. tor_assert(!buf->head);
  454. buf->head = buf->tail = chunk;
  455. }
  456. check();
  457. return chunk;
  458. }
  459. /** Return the age of the oldest chunk in the buffer <b>buf</b>, in
  460. * milliseconds. Requires the current monotonic time, in truncated msec,
  461. * as its input <b>now</b>.
  462. */
  463. uint32_t
  464. buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
  465. {
  466. if (buf->head) {
  467. return now - buf->head->inserted_time;
  468. } else {
  469. return 0;
  470. }
  471. }
  472. size_t
  473. buf_get_total_allocation(void)
  474. {
  475. return total_bytes_allocated_in_chunks;
  476. }
  477. /** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
  478. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  479. * *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
  480. * and the number of bytes read otherwise. */
  481. static inline int
  482. read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
  483. int *reached_eof, int *socket_error)
  484. {
  485. ssize_t read_result;
  486. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  487. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  488. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  489. if (read_result < 0) {
  490. int e = tor_socket_errno(fd);
  491. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  492. #ifdef _WIN32
  493. if (e == WSAENOBUFS)
  494. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  495. #endif
  496. *socket_error = e;
  497. return -1;
  498. }
  499. return 0; /* would block. */
  500. } else if (read_result == 0) {
  501. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  502. *reached_eof = 1;
  503. return 0;
  504. } else { /* actually got bytes. */
  505. buf->datalen += read_result;
  506. chunk->datalen += read_result;
  507. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  508. (int)buf->datalen);
  509. tor_assert(read_result < INT_MAX);
  510. return (int)read_result;
  511. }
  512. }
  513. /** As read_to_chunk(), but return (negative) error code on error, blocking,
  514. * or TLS, and the number of bytes read otherwise. */
  515. static inline int
  516. read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  517. size_t at_most)
  518. {
  519. int read_result;
  520. tor_assert(CHUNK_REMAINING_CAPACITY(chunk) >= at_most);
  521. read_result = tor_tls_read(tls, CHUNK_WRITE_PTR(chunk), at_most);
  522. if (read_result < 0)
  523. return read_result;
  524. buf->datalen += read_result;
  525. chunk->datalen += read_result;
  526. return read_result;
  527. }
  528. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  529. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  530. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  531. * error; else return the number of bytes read.
  532. */
  533. /* XXXX indicate "read blocked" somehow? */
  534. int
  535. read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
  536. int *socket_error)
  537. {
  538. /* XXXX It's stupid to overload the return values for these functions:
  539. * "error status" and "number of bytes read" are not mutually exclusive.
  540. */
  541. int r = 0;
  542. size_t total_read = 0;
  543. check();
  544. tor_assert(reached_eof);
  545. tor_assert(SOCKET_OK(s));
  546. if (BUG(buf->datalen >= INT_MAX))
  547. return -1;
  548. if (BUG(buf->datalen >= INT_MAX - at_most))
  549. return -1;
  550. while (at_most > total_read) {
  551. size_t readlen = at_most - total_read;
  552. chunk_t *chunk;
  553. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  554. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  555. if (readlen > chunk->memlen)
  556. readlen = chunk->memlen;
  557. } else {
  558. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  559. chunk = buf->tail;
  560. if (cap < readlen)
  561. readlen = cap;
  562. }
  563. r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
  564. check();
  565. if (r < 0)
  566. return r; /* Error */
  567. tor_assert(total_read+r < INT_MAX);
  568. total_read += r;
  569. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  570. break;
  571. }
  572. }
  573. return (int)total_read;
  574. }
  575. /** As read_to_buf, but reads from a TLS connection, and returns a TLS
  576. * status value rather than the number of bytes read.
  577. *
  578. * Using TLS on OR connections complicates matters in two ways.
  579. *
  580. * First, a TLS stream has its own read buffer independent of the
  581. * connection's read buffer. (TLS needs to read an entire frame from
  582. * the network before it can decrypt any data. Thus, trying to read 1
  583. * byte from TLS can require that several KB be read from the network
  584. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  585. * Because the data hasn't been read by Tor (it's still inside the TLS),
  586. * this means that sometimes a connection "has stuff to read" even when
  587. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  588. * used in connection.c to detect TLS objects with non-empty internal
  589. * buffers and read from them again.
  590. *
  591. * Second, the TLS stream's events do not correspond directly to network
  592. * events: sometimes, before a TLS stream can read, the network must be
  593. * ready to write -- or vice versa.
  594. */
  595. int
  596. read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  597. {
  598. int r = 0;
  599. size_t total_read = 0;
  600. check_no_tls_errors();
  601. check();
  602. if (BUG(buf->datalen >= INT_MAX))
  603. return -1;
  604. if (BUG(buf->datalen >= INT_MAX - at_most))
  605. return -1;
  606. while (at_most > total_read) {
  607. size_t readlen = at_most - total_read;
  608. chunk_t *chunk;
  609. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  610. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  611. if (readlen > chunk->memlen)
  612. readlen = chunk->memlen;
  613. } else {
  614. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  615. chunk = buf->tail;
  616. if (cap < readlen)
  617. readlen = cap;
  618. }
  619. r = read_to_chunk_tls(buf, chunk, tls, readlen);
  620. check();
  621. if (r < 0)
  622. return r; /* Error */
  623. tor_assert(total_read+r < INT_MAX);
  624. total_read += r;
  625. if ((size_t)r < readlen) /* eof, block, or no more to read. */
  626. break;
  627. }
  628. return (int)total_read;
  629. }
  630. /** Helper for flush_buf(): try to write <b>sz</b> bytes from chunk
  631. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct
  632. * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  633. * written on success, 0 on blocking, -1 on failure.
  634. */
  635. static inline int
  636. flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz,
  637. size_t *buf_flushlen)
  638. {
  639. ssize_t write_result;
  640. if (sz > chunk->datalen)
  641. sz = chunk->datalen;
  642. write_result = tor_socket_send(s, chunk->data, sz, 0);
  643. if (write_result < 0) {
  644. int e = tor_socket_errno(s);
  645. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  646. #ifdef _WIN32
  647. if (e == WSAENOBUFS)
  648. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  649. #endif
  650. return -1;
  651. }
  652. log_debug(LD_NET,"write() would block, returning.");
  653. return 0;
  654. } else {
  655. *buf_flushlen -= write_result;
  656. buf_remove_from_front(buf, write_result);
  657. tor_assert(write_result < INT_MAX);
  658. return (int)write_result;
  659. }
  660. }
  661. /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from chunk
  662. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
  663. * more if there is a forced pending write size.) On success, deduct the
  664. * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  665. * written on success, and a TOR_TLS error code on failure or blocking.
  666. */
  667. static inline int
  668. flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  669. size_t sz, size_t *buf_flushlen)
  670. {
  671. int r;
  672. size_t forced;
  673. char *data;
  674. forced = tor_tls_get_forced_write_size(tls);
  675. if (forced > sz)
  676. sz = forced;
  677. if (chunk) {
  678. data = chunk->data;
  679. tor_assert(sz <= chunk->datalen);
  680. } else {
  681. data = NULL;
  682. tor_assert(sz == 0);
  683. }
  684. r = tor_tls_write(tls, data, sz);
  685. if (r < 0)
  686. return r;
  687. if (*buf_flushlen > (size_t)r)
  688. *buf_flushlen -= r;
  689. else
  690. *buf_flushlen = 0;
  691. buf_remove_from_front(buf, r);
  692. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  693. r,(int)*buf_flushlen,(int)buf->datalen);
  694. return r;
  695. }
  696. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  697. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  698. * the number of bytes actually written, and remove the written bytes
  699. * from the buffer. Return the number of bytes written on success,
  700. * -1 on failure. Return 0 if write() would block.
  701. */
  702. int
  703. flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  704. {
  705. /* XXXX It's stupid to overload the return values for these functions:
  706. * "error status" and "number of bytes flushed" are not mutually exclusive.
  707. */
  708. int r;
  709. size_t flushed = 0;
  710. tor_assert(buf_flushlen);
  711. tor_assert(SOCKET_OK(s));
  712. tor_assert(*buf_flushlen <= buf->datalen);
  713. tor_assert(sz <= *buf_flushlen);
  714. check();
  715. while (sz) {
  716. size_t flushlen0;
  717. tor_assert(buf->head);
  718. if (buf->head->datalen >= sz)
  719. flushlen0 = sz;
  720. else
  721. flushlen0 = buf->head->datalen;
  722. r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen);
  723. check();
  724. if (r < 0)
  725. return r;
  726. flushed += r;
  727. sz -= r;
  728. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  729. break;
  730. }
  731. tor_assert(flushed < INT_MAX);
  732. return (int)flushed;
  733. }
  734. /** As flush_buf(), but writes data to a TLS connection. Can write more than
  735. * <b>flushlen</b> bytes.
  736. */
  737. int
  738. flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen,
  739. size_t *buf_flushlen)
  740. {
  741. int r;
  742. size_t flushed = 0;
  743. ssize_t sz;
  744. tor_assert(buf_flushlen);
  745. tor_assert(*buf_flushlen <= buf->datalen);
  746. tor_assert(flushlen <= *buf_flushlen);
  747. sz = (ssize_t) flushlen;
  748. /* we want to let tls write even if flushlen is zero, because it might
  749. * have a partial record pending */
  750. check_no_tls_errors();
  751. check();
  752. do {
  753. size_t flushlen0;
  754. if (buf->head) {
  755. if ((ssize_t)buf->head->datalen >= sz)
  756. flushlen0 = sz;
  757. else
  758. flushlen0 = buf->head->datalen;
  759. } else {
  760. flushlen0 = 0;
  761. }
  762. r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen);
  763. check();
  764. if (r < 0)
  765. return r;
  766. flushed += r;
  767. sz -= r;
  768. if (r == 0) /* Can't flush any more now. */
  769. break;
  770. } while (sz > 0);
  771. tor_assert(flushed < INT_MAX);
  772. return (int)flushed;
  773. }
  774. /** Append <b>string_len</b> bytes from <b>string</b> to the end of
  775. * <b>buf</b>.
  776. *
  777. * Return the new length of the buffer on success, -1 on failure.
  778. */
  779. int
  780. write_to_buf(const char *string, size_t string_len, buf_t *buf)
  781. {
  782. if (!string_len)
  783. return (int)buf->datalen;
  784. check();
  785. if (BUG(buf->datalen >= INT_MAX))
  786. return -1;
  787. if (BUG(buf->datalen >= INT_MAX - string_len))
  788. return -1;
  789. while (string_len) {
  790. size_t copy;
  791. if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
  792. buf_add_chunk_with_capacity(buf, string_len, 1);
  793. copy = CHUNK_REMAINING_CAPACITY(buf->tail);
  794. if (copy > string_len)
  795. copy = string_len;
  796. memcpy(CHUNK_WRITE_PTR(buf->tail), string, copy);
  797. string_len -= copy;
  798. string += copy;
  799. buf->datalen += copy;
  800. buf->tail->datalen += copy;
  801. }
  802. check();
  803. tor_assert(buf->datalen < INT_MAX);
  804. return (int)buf->datalen;
  805. }
  806. /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
  807. * onto <b>string</b>.
  808. */
  809. void
  810. peek_from_buf(char *string, size_t string_len, const buf_t *buf)
  811. {
  812. chunk_t *chunk;
  813. tor_assert(string);
  814. /* make sure we don't ask for too much */
  815. tor_assert(string_len <= buf->datalen);
  816. /* assert_buf_ok(buf); */
  817. chunk = buf->head;
  818. while (string_len) {
  819. size_t copy = string_len;
  820. tor_assert(chunk);
  821. if (chunk->datalen < copy)
  822. copy = chunk->datalen;
  823. memcpy(string, chunk->data, copy);
  824. string_len -= copy;
  825. string += copy;
  826. chunk = chunk->next;
  827. }
  828. }
  829. /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
  830. * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
  831. * must be \<= the number of bytes on the buffer.
  832. */
  833. int
  834. fetch_from_buf(char *string, size_t string_len, buf_t *buf)
  835. {
  836. /* There must be string_len bytes in buf; write them onto string,
  837. * then memmove buf back (that is, remove them from buf).
  838. *
  839. * Return the number of bytes still on the buffer. */
  840. check();
  841. peek_from_buf(string, string_len, buf);
  842. buf_remove_from_front(buf, string_len);
  843. check();
  844. tor_assert(buf->datalen < INT_MAX);
  845. return (int)buf->datalen;
  846. }
  847. /** Move up to *<b>buf_flushlen</b> bytes from <b>buf_in</b> to
  848. * <b>buf_out</b>, and modify *<b>buf_flushlen</b> appropriately.
  849. * Return the number of bytes actually copied.
  850. */
  851. int
  852. move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
  853. {
  854. /* We can do way better here, but this doesn't turn up in any profiles. */
  855. char b[4096];
  856. size_t cp, len;
  857. if (BUG(buf_out->datalen >= INT_MAX))
  858. return -1;
  859. if (BUG(buf_out->datalen >= INT_MAX - *buf_flushlen))
  860. return -1;
  861. len = *buf_flushlen;
  862. if (len > buf_in->datalen)
  863. len = buf_in->datalen;
  864. cp = len; /* Remember the number of bytes we intend to copy. */
  865. tor_assert(cp < INT_MAX);
  866. while (len) {
  867. /* This isn't the most efficient implementation one could imagine, since
  868. * it does two copies instead of 1, but I kinda doubt that this will be
  869. * critical path. */
  870. size_t n = len > sizeof(b) ? sizeof(b) : len;
  871. fetch_from_buf(b, n, buf_in);
  872. write_to_buf(b, n, buf_out);
  873. len -= n;
  874. }
  875. *buf_flushlen -= cp;
  876. return (int)cp;
  877. }
  878. /** Internal structure: represents a position in a buffer. */
  879. typedef struct buf_pos_t {
  880. const chunk_t *chunk; /**< Which chunk are we pointing to? */
  881. int pos;/**< Which character inside the chunk's data are we pointing to? */
  882. size_t chunk_pos; /**< Total length of all previous chunks. */
  883. } buf_pos_t;
  884. /** Initialize <b>out</b> to point to the first character of <b>buf</b>.*/
  885. static void
  886. buf_pos_init(const buf_t *buf, buf_pos_t *out)
  887. {
  888. out->chunk = buf->head;
  889. out->pos = 0;
  890. out->chunk_pos = 0;
  891. }
  892. /** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
  893. * position of <b>out</b>, or later. Return -1 if no instances are found;
  894. * otherwise returns the absolute position of the character. */
  895. static off_t
  896. buf_find_pos_of_char(char ch, buf_pos_t *out)
  897. {
  898. const chunk_t *chunk;
  899. int pos;
  900. tor_assert(out);
  901. if (out->chunk) {
  902. if (out->chunk->datalen) {
  903. tor_assert(out->pos < (off_t)out->chunk->datalen);
  904. } else {
  905. tor_assert(out->pos == 0);
  906. }
  907. }
  908. pos = out->pos;
  909. for (chunk = out->chunk; chunk; chunk = chunk->next) {
  910. char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
  911. if (cp) {
  912. out->chunk = chunk;
  913. tor_assert(cp - chunk->data < INT_MAX);
  914. out->pos = (int)(cp - chunk->data);
  915. return out->chunk_pos + out->pos;
  916. } else {
  917. out->chunk_pos += chunk->datalen;
  918. pos = 0;
  919. }
  920. }
  921. return -1;
  922. }
  923. /** Advance <b>pos</b> by a single character, if there are any more characters
  924. * in the buffer. Returns 0 on success, -1 on failure. */
  925. static inline int
  926. buf_pos_inc(buf_pos_t *pos)
  927. {
  928. ++pos->pos;
  929. if (pos->pos == (off_t)pos->chunk->datalen) {
  930. if (!pos->chunk->next)
  931. return -1;
  932. pos->chunk_pos += pos->chunk->datalen;
  933. pos->chunk = pos->chunk->next;
  934. pos->pos = 0;
  935. }
  936. return 0;
  937. }
  938. /** Return true iff the <b>n</b>-character string in <b>s</b> appears
  939. * (verbatim) at <b>pos</b>. */
  940. static int
  941. buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
  942. {
  943. buf_pos_t p;
  944. if (!n)
  945. return 1;
  946. memcpy(&p, pos, sizeof(p));
  947. while (1) {
  948. char ch = p.chunk->data[p.pos];
  949. if (ch != *s)
  950. return 0;
  951. ++s;
  952. /* If we're out of characters that don't match, we match. Check this
  953. * _before_ we test incrementing pos, in case we're at the end of the
  954. * string. */
  955. if (--n == 0)
  956. return 1;
  957. if (buf_pos_inc(&p)<0)
  958. return 0;
  959. }
  960. }
  961. /** Return the first position in <b>buf</b> at which the <b>n</b>-character
  962. * string <b>s</b> occurs, or -1 if it does not occur. */
  963. int
  964. buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
  965. {
  966. buf_pos_t pos;
  967. buf_pos_init(buf, &pos);
  968. while (buf_find_pos_of_char(*s, &pos) >= 0) {
  969. if (buf_matches_at_pos(&pos, s, n)) {
  970. tor_assert(pos.chunk_pos + pos.pos < INT_MAX);
  971. return (int)(pos.chunk_pos + pos.pos);
  972. } else {
  973. if (buf_pos_inc(&pos)<0)
  974. return -1;
  975. }
  976. }
  977. return -1;
  978. }
  979. /** Return 1 iff <b>buf</b> starts with <b>cmd</b>. <b>cmd</b> must be a null
  980. * terminated string, of no more than PEEK_BUF_STARTSWITH_MAX bytes. */
  981. int
  982. peek_buf_startswith(const buf_t *buf, const char *cmd)
  983. {
  984. char tmp[PEEK_BUF_STARTSWITH_MAX];
  985. size_t clen = strlen(cmd);
  986. if (BUG(clen > sizeof(tmp)))
  987. return 0;
  988. if (buf->datalen < clen)
  989. return 0;
  990. peek_from_buf(tmp, clen, buf);
  991. return fast_memeq(tmp, cmd, clen);
  992. }
  993. /** Return the index within <b>buf</b> at which <b>ch</b> first appears,
  994. * or -1 if <b>ch</b> does not appear on buf. */
  995. static off_t
  996. buf_find_offset_of_char(buf_t *buf, char ch)
  997. {
  998. chunk_t *chunk;
  999. off_t offset = 0;
  1000. for (chunk = buf->head; chunk; chunk = chunk->next) {
  1001. char *cp = memchr(chunk->data, ch, chunk->datalen);
  1002. if (cp)
  1003. return offset + (cp - chunk->data);
  1004. else
  1005. offset += chunk->datalen;
  1006. }
  1007. return -1;
  1008. }
  1009. /** Try to read a single LF-terminated line from <b>buf</b>, and write it
  1010. * (including the LF), NUL-terminated, into the *<b>data_len</b> byte buffer
  1011. * at <b>data_out</b>. Set *<b>data_len</b> to the number of bytes in the
  1012. * line, not counting the terminating NUL. Return 1 if we read a whole line,
  1013. * return 0 if we don't have a whole line yet, and return -1 if the line
  1014. * length exceeds *<b>data_len</b>.
  1015. */
  1016. int
  1017. fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
  1018. {
  1019. size_t sz;
  1020. off_t offset;
  1021. if (!buf->head)
  1022. return 0;
  1023. offset = buf_find_offset_of_char(buf, '\n');
  1024. if (offset < 0)
  1025. return 0;
  1026. sz = (size_t) offset;
  1027. if (sz+2 > *data_len) {
  1028. *data_len = sz + 2;
  1029. return -1;
  1030. }
  1031. fetch_from_buf(data_out, sz+1, buf);
  1032. data_out[sz+1] = '\0';
  1033. *data_len = sz+1;
  1034. return 1;
  1035. }
  1036. /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
  1037. * compression state <b>state</b>, appending the result to <b>buf</b>. If
  1038. * <b>done</b> is true, flush the data in the state and finish the
  1039. * compression/uncompression. Return -1 on failure, 0 on success. */
  1040. int
  1041. write_to_buf_compress(buf_t *buf, tor_compress_state_t *state,
  1042. const char *data, size_t data_len,
  1043. const int done)
  1044. {
  1045. char *next;
  1046. size_t old_avail, avail;
  1047. int over = 0;
  1048. do {
  1049. int need_new_chunk = 0;
  1050. if (!buf->tail || ! CHUNK_REMAINING_CAPACITY(buf->tail)) {
  1051. size_t cap = data_len / 4;
  1052. buf_add_chunk_with_capacity(buf, cap, 1);
  1053. }
  1054. next = CHUNK_WRITE_PTR(buf->tail);
  1055. avail = old_avail = CHUNK_REMAINING_CAPACITY(buf->tail);
  1056. switch (tor_compress_process(state, &next, &avail,
  1057. &data, &data_len, done)) {
  1058. case TOR_COMPRESS_DONE:
  1059. over = 1;
  1060. break;
  1061. case TOR_COMPRESS_ERROR:
  1062. return -1;
  1063. case TOR_COMPRESS_OK:
  1064. if (data_len == 0) {
  1065. tor_assert_nonfatal(!done);
  1066. over = 1;
  1067. }
  1068. break;
  1069. case TOR_COMPRESS_BUFFER_FULL:
  1070. if (avail) {
  1071. /* The compression module says we need more room
  1072. * (TOR_COMPRESS_BUFFER_FULL). Start a new chunk automatically,
  1073. * whether were going to or not. */
  1074. need_new_chunk = 1;
  1075. }
  1076. if (data_len == 0 && !done) {
  1077. /* We've consumed all the input data, though, so there's no
  1078. * point in forging ahead right now. */
  1079. over = 1;
  1080. }
  1081. break;
  1082. }
  1083. buf->datalen += old_avail - avail;
  1084. buf->tail->datalen += old_avail - avail;
  1085. if (need_new_chunk) {
  1086. buf_add_chunk_with_capacity(buf, data_len/4, 1);
  1087. }
  1088. } while (!over);
  1089. check();
  1090. return 0;
  1091. }
  1092. /** Set *<b>output</b> to contain a copy of the data in *<b>input</b> */
  1093. int
  1094. buf_set_to_copy(buf_t **output,
  1095. const buf_t *input)
  1096. {
  1097. if (*output)
  1098. buf_free(*output);
  1099. *output = buf_copy(input);
  1100. return 0;
  1101. }
  1102. /** Log an error and exit if <b>buf</b> is corrupted.
  1103. */
  1104. void
  1105. assert_buf_ok(buf_t *buf)
  1106. {
  1107. tor_assert(buf);
  1108. tor_assert(buf->magic == BUFFER_MAGIC);
  1109. if (! buf->head) {
  1110. tor_assert(!buf->tail);
  1111. tor_assert(buf->datalen == 0);
  1112. } else {
  1113. chunk_t *ch;
  1114. size_t total = 0;
  1115. tor_assert(buf->tail);
  1116. for (ch = buf->head; ch; ch = ch->next) {
  1117. total += ch->datalen;
  1118. tor_assert(ch->datalen <= ch->memlen);
  1119. tor_assert(ch->data >= &ch->mem[0]);
  1120. tor_assert(ch->data <= &ch->mem[0]+ch->memlen);
  1121. if (ch->data == &ch->mem[0]+ch->memlen) {
  1122. static int warned = 0;
  1123. if (! warned) {
  1124. log_warn(LD_BUG, "Invariant violation in buf.c related to #15083");
  1125. warned = 1;
  1126. }
  1127. }
  1128. tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
  1129. if (!ch->next)
  1130. tor_assert(ch == buf->tail);
  1131. }
  1132. tor_assert(buf->datalen == total);
  1133. }
  1134. }