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