buffers.c 34 KB

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