buffers.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  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-2011, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers.c
  8. * \brief Implements a generic interface buffer. Buffers are
  9. * fairly opaque string holders that can read to or flush from:
  10. * memory, file descriptors, or TLS connections.
  11. **/
  12. #define BUFFERS_PRIVATE
  13. #include "or.h"
  14. #include "buffers.h"
  15. #include "config.h"
  16. #include "connection_edge.h"
  17. #include "connection_or.h"
  18. #include "control.h"
  19. #include "reasons.h"
  20. #include "../common/util.h"
  21. #include "../common/torlog.h"
  22. #ifdef HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #ifdef HAVE_SYS_UIO_H
  26. #include <sys/uio.h>
  27. #endif
  28. //#define PARANOIA
  29. #ifdef PARANOIA
  30. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  31. * <b>buf</b> is well-formed. */
  32. #define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
  33. #else
  34. #define check() STMT_NIL
  35. #endif
  36. /* Implementation notes:
  37. *
  38. * After flirting with memmove, and dallying with ring-buffers, we're finally
  39. * getting up to speed with the 1970s and implementing buffers as a linked
  40. * list of small chunks. Each buffer has such a list; data is removed from
  41. * the head of the list, and added at the tail. The list is singly linked,
  42. * and the buffer keeps a pointer to the head and the tail.
  43. *
  44. * Every chunk, except the tail, contains at least one byte of data. Data in
  45. * each chunk is contiguous.
  46. *
  47. * When you need to treat the first N characters on a buffer as a contiguous
  48. * string, use the buf_pullup function to make them so. Don't do this more
  49. * than necessary.
  50. *
  51. * The major free Unix kernels have handled buffers like this since, like,
  52. * forever.
  53. */
  54. /* Chunk manipulation functions */
  55. /** A single chunk on a buffer or in a freelist. */
  56. typedef struct chunk_t {
  57. struct chunk_t *next; /**< The next chunk on the buffer or freelist. */
  58. size_t datalen; /**< The number of bytes stored in this chunk */
  59. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  60. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  61. char mem[1]; /**< The actual memory used for storage in this chunk. May be
  62. * more than one byte long. */
  63. } chunk_t;
  64. #define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
  65. /** Return the number of bytes needed to allocate a chunk to hold
  66. * <b>memlen</b> bytes. */
  67. #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_HEADER_LEN + (memlen))
  68. /** Return the number of usable bytes in a chunk allocated with
  69. * malloc(<b>memlen</b>). */
  70. #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_HEADER_LEN)
  71. /** Return the next character in <b>chunk</b> onto which data can be appended.
  72. * If the chunk is full, this might be off the end of chunk->mem. */
  73. static INLINE char *
  74. CHUNK_WRITE_PTR(chunk_t *chunk)
  75. {
  76. return chunk->data + chunk->datalen;
  77. }
  78. /** Return the number of bytes that can be written onto <b>chunk</b> without
  79. * running out of space. */
  80. static INLINE size_t
  81. CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
  82. {
  83. return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
  84. }
  85. /** Move all bytes stored in <b>chunk</b> to the front of <b>chunk</b>->mem,
  86. * to free up space at the end. */
  87. static INLINE void
  88. chunk_repack(chunk_t *chunk)
  89. {
  90. if (chunk->datalen && chunk->data != &chunk->mem[0]) {
  91. memmove(chunk->mem, chunk->data, chunk->datalen);
  92. }
  93. chunk->data = &chunk->mem[0];
  94. }
  95. #ifdef ENABLE_BUF_FREELISTS
  96. /** A freelist of chunks. */
  97. typedef struct chunk_freelist_t {
  98. size_t alloc_size; /**< What size chunks does this freelist hold? */
  99. int max_length; /**< Never allow more than this number of chunks in the
  100. * freelist. */
  101. int slack; /**< When trimming the freelist, leave this number of extra
  102. * chunks beyond lowest_length.*/
  103. int cur_length; /**< How many chunks on the freelist now? */
  104. int lowest_length; /**< What's the smallest value of cur_length since the
  105. * last time we cleaned this freelist? */
  106. uint64_t n_alloc;
  107. uint64_t n_free;
  108. uint64_t n_hit;
  109. chunk_t *head; /**< First chunk on the freelist. */
  110. } chunk_freelist_t;
  111. /** Macro to help define freelists. */
  112. #define FL(a,m,s) { a, m, s, 0, 0, 0, 0, 0, NULL }
  113. /** Static array of freelists, sorted by alloc_len, terminated by an entry
  114. * with alloc_size of 0. */
  115. static chunk_freelist_t freelists[] = {
  116. FL(4096, 256, 8), FL(8192, 128, 4), FL(16384, 64, 4), FL(32768, 32, 2),
  117. FL(0, 0, 0)
  118. };
  119. #undef FL
  120. /** How many times have we looked for a chunk of a size that no freelist
  121. * could help with? */
  122. static uint64_t n_freelist_miss = 0;
  123. static void assert_freelist_ok(chunk_freelist_t *fl);
  124. /** Return the freelist to hold chunks of size <b>alloc</b>, or NULL if
  125. * no freelist exists for that size. */
  126. static INLINE chunk_freelist_t *
  127. get_freelist(size_t alloc)
  128. {
  129. int i;
  130. for (i=0; freelists[i].alloc_size <= alloc; ++i) {
  131. if (freelists[i].alloc_size == alloc) {
  132. return &freelists[i];
  133. }
  134. }
  135. return NULL;
  136. }
  137. /** Deallocate a chunk or put it on a freelist */
  138. static void
  139. chunk_free_unchecked(chunk_t *chunk)
  140. {
  141. size_t alloc;
  142. chunk_freelist_t *freelist;
  143. alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
  144. freelist = get_freelist(alloc);
  145. if (freelist && freelist->cur_length < freelist->max_length) {
  146. chunk->next = freelist->head;
  147. freelist->head = chunk;
  148. ++freelist->cur_length;
  149. } else {
  150. if (freelist)
  151. ++freelist->n_free;
  152. tor_free(chunk);
  153. }
  154. }
  155. /** Allocate a new chunk with a given allocation size, or get one from the
  156. * freelist. Note that a chunk with allocation size A can actually hold only
  157. * CHUNK_SIZE_WITH_ALLOC(A) bytes in its mem field. */
  158. static INLINE chunk_t *
  159. chunk_new_with_alloc_size(size_t alloc)
  160. {
  161. chunk_t *ch;
  162. chunk_freelist_t *freelist;
  163. tor_assert(alloc >= sizeof(chunk_t));
  164. freelist = get_freelist(alloc);
  165. if (freelist && freelist->head) {
  166. ch = freelist->head;
  167. freelist->head = ch->next;
  168. if (--freelist->cur_length < freelist->lowest_length)
  169. freelist->lowest_length = freelist->cur_length;
  170. ++freelist->n_hit;
  171. } else {
  172. /* XXXX take advantage of tor_malloc_roundup, once we know how that
  173. * affects freelists. */
  174. if (freelist)
  175. ++freelist->n_alloc;
  176. else
  177. ++n_freelist_miss;
  178. ch = tor_malloc(alloc);
  179. }
  180. ch->next = NULL;
  181. ch->datalen = 0;
  182. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  183. ch->data = &ch->mem[0];
  184. return ch;
  185. }
  186. #else
  187. static void
  188. chunk_free_unchecked(chunk_t *chunk)
  189. {
  190. tor_free(chunk);
  191. }
  192. static INLINE chunk_t *
  193. chunk_new_with_alloc_size(size_t alloc)
  194. {
  195. chunk_t *ch;
  196. ch = tor_malloc_roundup(&alloc);
  197. ch->next = NULL;
  198. ch->datalen = 0;
  199. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  200. ch->data = &ch->mem[0];
  201. return ch;
  202. }
  203. #endif
  204. /** Expand <b>chunk</b> until it can hold <b>sz</b> bytes, and return a
  205. * new pointer to <b>chunk</b>. Old pointers are no longer valid. */
  206. static INLINE chunk_t *
  207. chunk_grow(chunk_t *chunk, size_t sz)
  208. {
  209. off_t offset;
  210. tor_assert(sz > chunk->memlen);
  211. offset = chunk->data - chunk->mem;
  212. chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz));
  213. chunk->memlen = sz;
  214. chunk->data = chunk->mem + offset;
  215. return chunk;
  216. }
  217. /** If a read onto the end of a chunk would be smaller than this number, then
  218. * just start a new chunk. */
  219. #define MIN_READ_LEN 8
  220. /** Every chunk should take up at least this many bytes. */
  221. #define MIN_CHUNK_ALLOC 256
  222. /** No chunk should take up more than this many bytes. */
  223. #define MAX_CHUNK_ALLOC 65536
  224. /** Return the allocation size we'd like to use to hold <b>target</b>
  225. * bytes. */
  226. static INLINE size_t
  227. preferred_chunk_size(size_t target)
  228. {
  229. size_t sz = MIN_CHUNK_ALLOC;
  230. while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
  231. sz <<= 1;
  232. }
  233. return sz;
  234. }
  235. /** Remove from the freelists most chunks that have not been used since the
  236. * last call to buf_shrink_freelists(). */
  237. void
  238. buf_shrink_freelists(int free_all)
  239. {
  240. #ifdef ENABLE_BUF_FREELISTS
  241. int i;
  242. disable_control_logging();
  243. for (i = 0; freelists[i].alloc_size; ++i) {
  244. int slack = freelists[i].slack;
  245. assert_freelist_ok(&freelists[i]);
  246. if (free_all || freelists[i].lowest_length > slack) {
  247. int n_to_free = free_all ? freelists[i].cur_length :
  248. (freelists[i].lowest_length - slack);
  249. int n_to_skip = freelists[i].cur_length - n_to_free;
  250. int orig_length = freelists[i].cur_length;
  251. int orig_n_to_free = n_to_free, n_freed=0;
  252. int orig_n_to_skip = n_to_skip;
  253. int new_length = n_to_skip;
  254. chunk_t **chp = &freelists[i].head;
  255. chunk_t *chunk;
  256. while (n_to_skip) {
  257. if (! (*chp)->next) {
  258. log_warn(LD_BUG, "I wanted to skip %d chunks in the freelist for "
  259. "%d-byte chunks, but only found %d. (Length %d)",
  260. orig_n_to_skip, (int)freelists[i].alloc_size,
  261. orig_n_to_skip-n_to_skip, freelists[i].cur_length);
  262. assert_freelist_ok(&freelists[i]);
  263. goto done;
  264. }
  265. // tor_assert((*chp)->next);
  266. chp = &(*chp)->next;
  267. --n_to_skip;
  268. }
  269. chunk = *chp;
  270. *chp = NULL;
  271. while (chunk) {
  272. chunk_t *next = chunk->next;
  273. tor_free(chunk);
  274. chunk = next;
  275. --n_to_free;
  276. ++n_freed;
  277. ++freelists[i].n_free;
  278. }
  279. if (n_to_free) {
  280. log_warn(LD_BUG, "Freelist length for %d-byte chunks may have been "
  281. "messed up somehow.", (int)freelists[i].alloc_size);
  282. log_warn(LD_BUG, "There were %d chunks at the start. I decided to "
  283. "keep %d. I wanted to free %d. I freed %d. I somehow think "
  284. "I have %d left to free.",
  285. freelists[i].cur_length, n_to_skip, orig_n_to_free,
  286. n_freed, n_to_free);
  287. }
  288. // tor_assert(!n_to_free);
  289. freelists[i].cur_length = new_length;
  290. log_info(LD_MM, "Cleaned freelist for %d-byte chunks: original "
  291. "length %d, kept %d, dropped %d.",
  292. (int)freelists[i].alloc_size, orig_length,
  293. orig_n_to_skip, orig_n_to_free);
  294. }
  295. freelists[i].lowest_length = freelists[i].cur_length;
  296. assert_freelist_ok(&freelists[i]);
  297. }
  298. done:
  299. enable_control_logging();
  300. #else
  301. (void) free_all;
  302. #endif
  303. }
  304. /** Describe the current status of the freelists at log level <b>severity</b>.
  305. */
  306. void
  307. buf_dump_freelist_sizes(int severity)
  308. {
  309. #ifdef ENABLE_BUF_FREELISTS
  310. int i;
  311. log(severity, LD_MM, "====== Buffer freelists:");
  312. for (i = 0; freelists[i].alloc_size; ++i) {
  313. uint64_t total = ((uint64_t)freelists[i].cur_length) *
  314. freelists[i].alloc_size;
  315. log(severity, LD_MM,
  316. U64_FORMAT" bytes in %d %d-byte chunks ["U64_FORMAT
  317. " misses; "U64_FORMAT" frees; "U64_FORMAT" hits]",
  318. U64_PRINTF_ARG(total),
  319. freelists[i].cur_length, (int)freelists[i].alloc_size,
  320. U64_PRINTF_ARG(freelists[i].n_alloc),
  321. U64_PRINTF_ARG(freelists[i].n_free),
  322. U64_PRINTF_ARG(freelists[i].n_hit));
  323. }
  324. log(severity, LD_MM, U64_FORMAT" allocations in non-freelist sizes",
  325. U64_PRINTF_ARG(n_freelist_miss));
  326. #else
  327. (void)severity;
  328. #endif
  329. }
  330. /** Magic value for buf_t.magic, to catch pointer errors. */
  331. #define BUFFER_MAGIC 0xB0FFF312u
  332. /** A resizeable buffer, optimized for reading and writing. */
  333. struct buf_t {
  334. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  335. * BUFFER_MAGIC. */
  336. size_t datalen; /**< How many bytes is this buffer holding right now? */
  337. size_t default_chunk_size; /**< Don't allocate any chunks smaller than
  338. * this for this buffer. */
  339. chunk_t *head; /**< First chunk in the list, or NULL for none. */
  340. chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
  341. };
  342. /** Collapse data from the first N chunks from <b>buf</b> into buf->head,
  343. * growing it as necessary, until buf->head has the first <b>bytes</b> bytes
  344. * of data from the buffer, or until buf->head has all the data in <b>buf</b>.
  345. *
  346. * If <b>nulterminate</b> is true, ensure that there is a 0 byte in
  347. * buf->head->mem right after all the data. */
  348. static void
  349. buf_pullup(buf_t *buf, size_t bytes, int nulterminate)
  350. {
  351. chunk_t *dest, *src;
  352. size_t capacity;
  353. if (!buf->head)
  354. return;
  355. check();
  356. if (buf->datalen < bytes)
  357. bytes = buf->datalen;
  358. if (nulterminate) {
  359. capacity = bytes + 1;
  360. if (buf->head->datalen >= bytes && CHUNK_REMAINING_CAPACITY(buf->head)) {
  361. *CHUNK_WRITE_PTR(buf->head) = '\0';
  362. return;
  363. }
  364. } else {
  365. capacity = bytes;
  366. if (buf->head->datalen >= bytes)
  367. return;
  368. }
  369. if (buf->head->memlen >= capacity) {
  370. /* We don't need to grow the first chunk, but we might need to repack it.*/
  371. if (CHUNK_REMAINING_CAPACITY(buf->head) < capacity-buf->datalen)
  372. chunk_repack(buf->head);
  373. tor_assert(CHUNK_REMAINING_CAPACITY(buf->head) >= capacity-buf->datalen);
  374. } else {
  375. chunk_t *newhead;
  376. size_t newsize;
  377. /* We need to grow the chunk. */
  378. chunk_repack(buf->head);
  379. newsize = CHUNK_SIZE_WITH_ALLOC(preferred_chunk_size(capacity));
  380. newhead = chunk_grow(buf->head, newsize);
  381. tor_assert(newhead->memlen >= capacity);
  382. if (newhead != buf->head) {
  383. if (buf->tail == buf->head)
  384. buf->tail = newhead;
  385. buf->head = newhead;
  386. }
  387. }
  388. dest = buf->head;
  389. while (dest->datalen < bytes) {
  390. size_t n = bytes - dest->datalen;
  391. src = dest->next;
  392. tor_assert(src);
  393. if (n > src->datalen) {
  394. memcpy(CHUNK_WRITE_PTR(dest), src->data, src->datalen);
  395. dest->datalen += src->datalen;
  396. dest->next = src->next;
  397. if (buf->tail == src)
  398. buf->tail = dest;
  399. chunk_free_unchecked(src);
  400. } else {
  401. memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
  402. dest->datalen += n;
  403. src->data += n;
  404. src->datalen -= n;
  405. tor_assert(dest->datalen == bytes);
  406. }
  407. }
  408. if (nulterminate) {
  409. tor_assert(CHUNK_REMAINING_CAPACITY(buf->head));
  410. *CHUNK_WRITE_PTR(buf->head) = '\0';
  411. }
  412. check();
  413. }
  414. /** Resize buf so it won't hold extra memory that we haven't been
  415. * using lately.
  416. */
  417. void
  418. buf_shrink(buf_t *buf)
  419. {
  420. (void)buf;
  421. }
  422. /** Remove the first <b>n</b> bytes from buf. */
  423. static INLINE void
  424. buf_remove_from_front(buf_t *buf, size_t n)
  425. {
  426. tor_assert(buf->datalen >= n);
  427. while (n) {
  428. tor_assert(buf->head);
  429. if (buf->head->datalen > n) {
  430. buf->head->datalen -= n;
  431. buf->head->data += n;
  432. buf->datalen -= n;
  433. return;
  434. } else {
  435. chunk_t *victim = buf->head;
  436. n -= victim->datalen;
  437. buf->datalen -= victim->datalen;
  438. buf->head = victim->next;
  439. if (buf->tail == victim)
  440. buf->tail = NULL;
  441. chunk_free_unchecked(victim);
  442. }
  443. }
  444. check();
  445. }
  446. /** Create and return a new buf with default chunk capacity <b>size</b>.
  447. */
  448. buf_t *
  449. buf_new_with_capacity(size_t size)
  450. {
  451. buf_t *b = buf_new();
  452. b->default_chunk_size = preferred_chunk_size(size);
  453. return b;
  454. }
  455. /** Allocate and return a new buffer with default capacity. */
  456. buf_t *
  457. buf_new(void)
  458. {
  459. buf_t *buf = tor_malloc_zero(sizeof(buf_t));
  460. buf->magic = BUFFER_MAGIC;
  461. buf->default_chunk_size = 4096;
  462. return buf;
  463. }
  464. /** Remove all data from <b>buf</b>. */
  465. void
  466. buf_clear(buf_t *buf)
  467. {
  468. chunk_t *chunk, *next;
  469. buf->datalen = 0;
  470. for (chunk = buf->head; chunk; chunk = next) {
  471. next = chunk->next;
  472. chunk_free_unchecked(chunk);
  473. }
  474. buf->head = buf->tail = NULL;
  475. }
  476. /** Return the number of bytes stored in <b>buf</b> */
  477. size_t
  478. buf_datalen(const buf_t *buf)
  479. {
  480. return buf->datalen;
  481. }
  482. /** Return the total length of all chunks used in <b>buf</b>. */
  483. size_t
  484. buf_allocation(const buf_t *buf)
  485. {
  486. size_t total = 0;
  487. const chunk_t *chunk;
  488. for (chunk = buf->head; chunk; chunk = chunk->next) {
  489. total += chunk->memlen;
  490. }
  491. return total;
  492. }
  493. /** Return the number of bytes that can be added to <b>buf</b> without
  494. * performing any additional allocation. */
  495. size_t
  496. buf_slack(const buf_t *buf)
  497. {
  498. if (!buf->tail)
  499. return 0;
  500. else
  501. return CHUNK_REMAINING_CAPACITY(buf->tail);
  502. }
  503. /** Release storage held by <b>buf</b>. */
  504. void
  505. buf_free(buf_t *buf)
  506. {
  507. if (!buf)
  508. return;
  509. buf_clear(buf);
  510. buf->magic = 0xdeadbeef;
  511. tor_free(buf);
  512. }
  513. /** Append a new chunk with enough capacity to hold <b>capacity</b> bytes to
  514. * the tail of <b>buf</b>. If <b>capped</b>, don't allocate a chunk bigger
  515. * than MAX_CHUNK_ALLOC. */
  516. static chunk_t *
  517. buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
  518. {
  519. chunk_t *chunk;
  520. if (CHUNK_ALLOC_SIZE(capacity) < buf->default_chunk_size) {
  521. chunk = chunk_new_with_alloc_size(buf->default_chunk_size);
  522. } else if (capped && CHUNK_ALLOC_SIZE(capacity) > MAX_CHUNK_ALLOC) {
  523. chunk = chunk_new_with_alloc_size(MAX_CHUNK_ALLOC);
  524. } else {
  525. chunk = chunk_new_with_alloc_size(preferred_chunk_size(capacity));
  526. }
  527. if (buf->tail) {
  528. tor_assert(buf->head);
  529. buf->tail->next = chunk;
  530. buf->tail = chunk;
  531. } else {
  532. tor_assert(!buf->head);
  533. buf->head = buf->tail = chunk;
  534. }
  535. check();
  536. return chunk;
  537. }
  538. /** If we're using readv and writev, how many chunks are we willing to
  539. * read/write at a time? */
  540. #define N_IOV 3
  541. /** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
  542. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  543. * *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
  544. * and the number of bytes read otherwise. */
  545. static INLINE int
  546. read_to_chunk(buf_t *buf, chunk_t *chunk, int fd, size_t at_most,
  547. int *reached_eof, int *socket_error)
  548. {
  549. ssize_t read_result;
  550. #if 0 && defined(HAVE_READV) && !defined(WIN32)
  551. struct iovec iov[N_IOV];
  552. int i;
  553. size_t remaining = at_most;
  554. for (i=0; chunk && i < N_IOV && remaining; ++i) {
  555. iov[i].iov_base = CHUNK_WRITE_PTR(chunk);
  556. if (remaining > CHUNK_REMAINING_CAPACITY(chunk))
  557. iov[i].iov_len = CHUNK_REMAINING_CAPACITY(chunk);
  558. else
  559. iov[i].iov_len = remaining;
  560. remaining -= iov[i].iov_len;
  561. chunk = chunk->next;
  562. }
  563. read_result = readv(fd, iov, i);
  564. #else
  565. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  566. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  567. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  568. #endif
  569. if (read_result < 0) {
  570. int e = tor_socket_errno(fd);
  571. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  572. #ifdef MS_WINDOWS
  573. if (e == WSAENOBUFS)
  574. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  575. #endif
  576. *socket_error = e;
  577. return -1;
  578. }
  579. return 0; /* would block. */
  580. } else if (read_result == 0) {
  581. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  582. *reached_eof = 1;
  583. return 0;
  584. } else { /* actually got bytes. */
  585. buf->datalen += read_result;
  586. #if 0 && defined(HAVE_READV) && !defined(WIN32)
  587. while ((size_t)read_result > CHUNK_REMAINING_CAPACITY(chunk)) {
  588. chunk->datalen += CHUNK_REMAINING_CAPACITY(chunk);
  589. read_result -= CHUNK_REMAINING_CAPACITY(chunk);
  590. chunk = chunk->next;
  591. tor_assert(chunk);
  592. }
  593. #endif
  594. chunk->datalen += read_result;
  595. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  596. (int)buf->datalen);
  597. tor_assert(read_result < INT_MAX);
  598. return (int)read_result;
  599. }
  600. }
  601. /** As read_to_chunk(), but return (negative) error code on error, blocking,
  602. * or TLS, and the number of bytes read otherwise. */
  603. static INLINE int
  604. read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  605. size_t at_most)
  606. {
  607. int read_result;
  608. tor_assert(CHUNK_REMAINING_CAPACITY(chunk) >= at_most);
  609. read_result = tor_tls_read(tls, CHUNK_WRITE_PTR(chunk), at_most);
  610. if (read_result < 0)
  611. return read_result;
  612. buf->datalen += read_result;
  613. chunk->datalen += read_result;
  614. return read_result;
  615. }
  616. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  617. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  618. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  619. * error; else return the number of bytes read.
  620. */
  621. /* XXXX023 indicate "read blocked" somehow? */
  622. int
  623. read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof,
  624. int *socket_error)
  625. {
  626. /* XXXX023 It's stupid to overload the return values for these functions:
  627. * "error status" and "number of bytes read" are not mutually exclusive.
  628. */
  629. int r = 0;
  630. size_t total_read = 0;
  631. check();
  632. tor_assert(reached_eof);
  633. tor_assert(s >= 0);
  634. while (at_most > total_read) {
  635. size_t readlen = at_most - total_read;
  636. chunk_t *chunk;
  637. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  638. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  639. if (readlen > chunk->memlen)
  640. readlen = chunk->memlen;
  641. } else {
  642. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  643. chunk = buf->tail;
  644. if (cap < readlen)
  645. readlen = cap;
  646. }
  647. r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
  648. check();
  649. if (r < 0)
  650. return r; /* Error */
  651. tor_assert(total_read+r < INT_MAX);
  652. total_read += r;
  653. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  654. break;
  655. }
  656. }
  657. return (int)total_read;
  658. }
  659. /** As read_to_buf, but reads from a TLS connection, and returns a TLS
  660. * status value rather than the number of bytes read.
  661. *
  662. * Using TLS on OR connections complicates matters in two ways.
  663. *
  664. * First, a TLS stream has its own read buffer independent of the
  665. * connection's read buffer. (TLS needs to read an entire frame from
  666. * the network before it can decrypt any data. Thus, trying to read 1
  667. * byte from TLS can require that several KB be read from the network
  668. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  669. * Because the data hasn't been read by Tor (it's still inside the TLS),
  670. * this means that sometimes a connection "has stuff to read" even when
  671. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  672. * used in connection.c to detect TLS objects with non-empty internal
  673. * buffers and read from them again.
  674. *
  675. * Second, the TLS stream's events do not correspond directly to network
  676. * events: sometimes, before a TLS stream can read, the network must be
  677. * ready to write -- or vice versa.
  678. */
  679. int
  680. read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  681. {
  682. int r = 0;
  683. size_t total_read = 0;
  684. check();
  685. while (at_most > total_read) {
  686. size_t readlen = at_most - total_read;
  687. chunk_t *chunk;
  688. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  689. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  690. if (readlen > chunk->memlen)
  691. readlen = chunk->memlen;
  692. } else {
  693. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  694. chunk = buf->tail;
  695. if (cap < readlen)
  696. readlen = cap;
  697. }
  698. r = read_to_chunk_tls(buf, chunk, tls, readlen);
  699. check();
  700. if (r < 0)
  701. return r; /* Error */
  702. tor_assert(total_read+r < INT_MAX);
  703. total_read += r;
  704. if ((size_t)r < readlen) /* eof, block, or no more to read. */
  705. break;
  706. }
  707. return (int)total_read;
  708. }
  709. /** Helper for flush_buf(): try to write <b>sz</b> bytes from chunk
  710. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct
  711. * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  712. * written on success, 0 on blocking, -1 on failure.
  713. */
  714. static INLINE int
  715. flush_chunk(int s, buf_t *buf, chunk_t *chunk, size_t sz,
  716. size_t *buf_flushlen)
  717. {
  718. ssize_t write_result;
  719. #if 0 && defined(HAVE_WRITEV) && !defined(WIN32)
  720. struct iovec iov[N_IOV];
  721. int i;
  722. size_t remaining = sz;
  723. for (i=0; chunk && i < N_IOV && remaining; ++i) {
  724. iov[i].iov_base = chunk->data;
  725. if (remaining > chunk->datalen)
  726. iov[i].iov_len = chunk->datalen;
  727. else
  728. iov[i].iov_len = remaining;
  729. remaining -= iov[i].iov_len;
  730. chunk = chunk->next;
  731. }
  732. write_result = writev(s, iov, i);
  733. #else
  734. if (sz > chunk->datalen)
  735. sz = chunk->datalen;
  736. write_result = tor_socket_send(s, chunk->data, sz, 0);
  737. #endif
  738. if (write_result < 0) {
  739. int e = tor_socket_errno(s);
  740. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  741. #ifdef MS_WINDOWS
  742. if (e == WSAENOBUFS)
  743. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  744. #endif
  745. return -1;
  746. }
  747. log_debug(LD_NET,"write() would block, returning.");
  748. return 0;
  749. } else {
  750. *buf_flushlen -= write_result;
  751. buf_remove_from_front(buf, write_result);
  752. tor_assert(write_result < INT_MAX);
  753. return (int)write_result;
  754. }
  755. }
  756. /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from chunk
  757. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
  758. * more if there is a forced pending write size.) On success, deduct the
  759. * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  760. * written on success, and a TOR_TLS error code on failure or blocking.
  761. */
  762. static INLINE int
  763. flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  764. size_t sz, size_t *buf_flushlen)
  765. {
  766. int r;
  767. size_t forced;
  768. char *data;
  769. forced = tor_tls_get_forced_write_size(tls);
  770. if (forced > sz)
  771. sz = forced;
  772. if (chunk) {
  773. data = chunk->data;
  774. tor_assert(sz <= chunk->datalen);
  775. } else {
  776. data = NULL;
  777. tor_assert(sz == 0);
  778. }
  779. r = tor_tls_write(tls, data, sz);
  780. if (r < 0)
  781. return r;
  782. if (*buf_flushlen > (size_t)r)
  783. *buf_flushlen -= r;
  784. else
  785. *buf_flushlen = 0;
  786. buf_remove_from_front(buf, r);
  787. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  788. r,(int)*buf_flushlen,(int)buf->datalen);
  789. return r;
  790. }
  791. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  792. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  793. * the number of bytes actually written, and remove the written bytes
  794. * from the buffer. Return the number of bytes written on success,
  795. * -1 on failure. Return 0 if write() would block.
  796. */
  797. int
  798. flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  799. {
  800. /* XXXX023 It's stupid to overload the return values for these functions:
  801. * "error status" and "number of bytes flushed" are not mutually exclusive.
  802. */
  803. int r;
  804. size_t flushed = 0;
  805. tor_assert(buf_flushlen);
  806. tor_assert(s >= 0);
  807. tor_assert(*buf_flushlen <= buf->datalen);
  808. tor_assert(sz <= *buf_flushlen);
  809. check();
  810. while (sz) {
  811. size_t flushlen0;
  812. tor_assert(buf->head);
  813. if (buf->head->datalen >= sz)
  814. flushlen0 = sz;
  815. else
  816. flushlen0 = buf->head->datalen;
  817. r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen);
  818. check();
  819. if (r < 0)
  820. return r;
  821. flushed += r;
  822. sz -= r;
  823. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  824. break;
  825. }
  826. tor_assert(flushed < INT_MAX);
  827. return (int)flushed;
  828. }
  829. /** As flush_buf(), but writes data to a TLS connection. Can write more than
  830. * <b>flushlen</b> bytes.
  831. */
  832. int
  833. flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen,
  834. size_t *buf_flushlen)
  835. {
  836. int r;
  837. size_t flushed = 0;
  838. ssize_t sz;
  839. tor_assert(buf_flushlen);
  840. tor_assert(*buf_flushlen <= buf->datalen);
  841. tor_assert(flushlen <= *buf_flushlen);
  842. sz = (ssize_t) flushlen;
  843. /* we want to let tls write even if flushlen is zero, because it might
  844. * have a partial record pending */
  845. check_no_tls_errors();
  846. check();
  847. do {
  848. size_t flushlen0;
  849. if (buf->head) {
  850. if ((ssize_t)buf->head->datalen >= sz)
  851. flushlen0 = sz;
  852. else
  853. flushlen0 = buf->head->datalen;
  854. } else {
  855. flushlen0 = 0;
  856. }
  857. r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen);
  858. check();
  859. if (r < 0)
  860. return r;
  861. flushed += r;
  862. sz -= r;
  863. if (r == 0) /* Can't flush any more now. */
  864. break;
  865. } while (sz > 0);
  866. tor_assert(flushed < INT_MAX);
  867. return (int)flushed;
  868. }
  869. /** Append <b>string_len</b> bytes from <b>string</b> to the end of
  870. * <b>buf</b>.
  871. *
  872. * Return the new length of the buffer on success, -1 on failure.
  873. */
  874. int
  875. write_to_buf(const char *string, size_t string_len, buf_t *buf)
  876. {
  877. if (!string_len)
  878. return (int)buf->datalen;
  879. check();
  880. while (string_len) {
  881. size_t copy;
  882. if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
  883. buf_add_chunk_with_capacity(buf, string_len, 1);
  884. copy = CHUNK_REMAINING_CAPACITY(buf->tail);
  885. if (copy > string_len)
  886. copy = string_len;
  887. memcpy(CHUNK_WRITE_PTR(buf->tail), string, copy);
  888. string_len -= copy;
  889. string += copy;
  890. buf->datalen += copy;
  891. buf->tail->datalen += copy;
  892. }
  893. check();
  894. tor_assert(buf->datalen < INT_MAX);
  895. return (int)buf->datalen;
  896. }
  897. /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
  898. * onto <b>string</b>.
  899. */
  900. static INLINE void
  901. peek_from_buf(char *string, size_t string_len, const buf_t *buf)
  902. {
  903. chunk_t *chunk;
  904. tor_assert(string);
  905. /* make sure we don't ask for too much */
  906. tor_assert(string_len <= buf->datalen);
  907. /* assert_buf_ok(buf); */
  908. chunk = buf->head;
  909. while (string_len) {
  910. size_t copy = string_len;
  911. tor_assert(chunk);
  912. if (chunk->datalen < copy)
  913. copy = chunk->datalen;
  914. memcpy(string, chunk->data, copy);
  915. string_len -= copy;
  916. string += copy;
  917. chunk = chunk->next;
  918. }
  919. }
  920. /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
  921. * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
  922. * must be \<= the number of bytes on the buffer.
  923. */
  924. int
  925. fetch_from_buf(char *string, size_t string_len, buf_t *buf)
  926. {
  927. /* There must be string_len bytes in buf; write them onto string,
  928. * then memmove buf back (that is, remove them from buf).
  929. *
  930. * Return the number of bytes still on the buffer. */
  931. check();
  932. peek_from_buf(string, string_len, buf);
  933. buf_remove_from_front(buf, string_len);
  934. check();
  935. tor_assert(buf->datalen < INT_MAX);
  936. return (int)buf->datalen;
  937. }
  938. /** Check <b>buf</b> for a variable-length cell according to the rules of link
  939. * protocol version <b>linkproto</b>. If one is found, pull it off the buffer
  940. * and assign a newly allocated var_cell_t to *<b>out</b>, and return 1.
  941. * Return 0 if whatever is on the start of buf_t is not a variable-length
  942. * cell. Return 1 and set *<b>out</b> to NULL if there seems to be the start
  943. * of a variable-length cell on <b>buf</b>, but the whole thing isn't there
  944. * yet. */
  945. int
  946. fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
  947. {
  948. char hdr[VAR_CELL_HEADER_SIZE];
  949. var_cell_t *result;
  950. uint8_t command;
  951. uint16_t length;
  952. /* If linkproto is unknown (0) or v2 (2), variable-length cells work as
  953. * implemented here. If it's 1, there are no variable-length cells. Tor
  954. * does not support other versions right now, and so can't negotiate them.
  955. */
  956. if (linkproto == 1)
  957. return 0;
  958. check();
  959. *out = NULL;
  960. if (buf->datalen < VAR_CELL_HEADER_SIZE)
  961. return 0;
  962. peek_from_buf(hdr, sizeof(hdr), buf);
  963. command = get_uint8(hdr+2);
  964. if (!(CELL_COMMAND_IS_VAR_LENGTH(command)))
  965. return 0;
  966. length = ntohs(get_uint16(hdr+3));
  967. if (buf->datalen < (size_t)(VAR_CELL_HEADER_SIZE+length))
  968. return 1;
  969. result = var_cell_new(length);
  970. result->command = command;
  971. result->circ_id = ntohs(get_uint16(hdr));
  972. buf_remove_from_front(buf, VAR_CELL_HEADER_SIZE);
  973. peek_from_buf((char*) result->payload, length, buf);
  974. buf_remove_from_front(buf, length);
  975. check();
  976. *out = result;
  977. return 1;
  978. }
  979. /** Move up to *<b>buf_flushlen</b> bytes from <b>buf_in</b> to
  980. * <b>buf_out</b>, and modify *<b>buf_flushlen</b> appropriately.
  981. * Return the number of bytes actually copied.
  982. */
  983. int
  984. move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
  985. {
  986. /* XXXX we can do way better here, but this doesn't turn up in any
  987. * profiles. */
  988. char b[4096];
  989. size_t cp, len;
  990. len = *buf_flushlen;
  991. if (len > buf_in->datalen)
  992. len = buf_in->datalen;
  993. cp = len; /* Remember the number of bytes we intend to copy. */
  994. tor_assert(cp < INT_MAX);
  995. while (len) {
  996. /* This isn't the most efficient implementation one could imagine, since
  997. * it does two copies instead of 1, but I kinda doubt that this will be
  998. * critical path. */
  999. size_t n = len > sizeof(b) ? sizeof(b) : len;
  1000. fetch_from_buf(b, n, buf_in);
  1001. write_to_buf(b, n, buf_out);
  1002. len -= n;
  1003. }
  1004. *buf_flushlen -= cp;
  1005. return (int)cp;
  1006. }
  1007. /** Internal structure: represents a position in a buffer. */
  1008. typedef struct buf_pos_t {
  1009. const chunk_t *chunk; /**< Which chunk are we pointing to? */
  1010. int pos;/**< Which character inside the chunk's data are we pointing to? */
  1011. size_t chunk_pos; /**< Total length of all previous chunks. */
  1012. } buf_pos_t;
  1013. /** Initialize <b>out</b> to point to the first character of <b>buf</b>.*/
  1014. static void
  1015. buf_pos_init(const buf_t *buf, buf_pos_t *out)
  1016. {
  1017. out->chunk = buf->head;
  1018. out->pos = 0;
  1019. out->chunk_pos = 0;
  1020. }
  1021. /** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
  1022. * position of <b>out</b>, or later. Return -1 if no instances are found;
  1023. * otherwise returns the absolute position of the character. */
  1024. static off_t
  1025. buf_find_pos_of_char(char ch, buf_pos_t *out)
  1026. {
  1027. const chunk_t *chunk;
  1028. int pos;
  1029. tor_assert(out);
  1030. if (out->chunk) {
  1031. if (out->chunk->datalen) {
  1032. tor_assert(out->pos < (off_t)out->chunk->datalen);
  1033. } else {
  1034. tor_assert(out->pos == 0);
  1035. }
  1036. }
  1037. pos = out->pos;
  1038. for (chunk = out->chunk; chunk; chunk = chunk->next) {
  1039. char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
  1040. if (cp) {
  1041. out->chunk = chunk;
  1042. tor_assert(cp - chunk->data < INT_MAX);
  1043. out->pos = (int)(cp - chunk->data);
  1044. return out->chunk_pos + out->pos;
  1045. } else {
  1046. out->chunk_pos += chunk->datalen;
  1047. pos = 0;
  1048. }
  1049. }
  1050. return -1;
  1051. }
  1052. /** Advance <b>pos</b> by a single character, if there are any more characters
  1053. * in the buffer. Returns 0 on success, -1 on failure. */
  1054. static INLINE int
  1055. buf_pos_inc(buf_pos_t *pos)
  1056. {
  1057. ++pos->pos;
  1058. if (pos->pos == (off_t)pos->chunk->datalen) {
  1059. if (!pos->chunk->next)
  1060. return -1;
  1061. pos->chunk_pos += pos->chunk->datalen;
  1062. pos->chunk = pos->chunk->next;
  1063. pos->pos = 0;
  1064. }
  1065. return 0;
  1066. }
  1067. /** Return true iff the <b>n</b>-character string in <b>s</b> appears
  1068. * (verbatim) at <b>pos</b>. */
  1069. static int
  1070. buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
  1071. {
  1072. buf_pos_t p;
  1073. if (!n)
  1074. return 1;
  1075. memcpy(&p, pos, sizeof(p));
  1076. while (1) {
  1077. char ch = p.chunk->data[p.pos];
  1078. if (ch != *s)
  1079. return 0;
  1080. ++s;
  1081. /* If we're out of characters that don't match, we match. Check this
  1082. * _before_ we test incrementing pos, in case we're at the end of the
  1083. * string. */
  1084. if (--n == 0)
  1085. return 1;
  1086. if (buf_pos_inc(&p)<0)
  1087. return 0;
  1088. }
  1089. }
  1090. /** Return the first position in <b>buf</b> at which the <b>n</b>-character
  1091. * string <b>s</b> occurs, or -1 if it does not occur. */
  1092. /*private*/ int
  1093. buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
  1094. {
  1095. buf_pos_t pos;
  1096. buf_pos_init(buf, &pos);
  1097. while (buf_find_pos_of_char(*s, &pos) >= 0) {
  1098. if (buf_matches_at_pos(&pos, s, n)) {
  1099. tor_assert(pos.chunk_pos + pos.pos < INT_MAX);
  1100. return (int)(pos.chunk_pos + pos.pos);
  1101. } else {
  1102. if (buf_pos_inc(&pos)<0)
  1103. return -1;
  1104. }
  1105. }
  1106. return -1;
  1107. }
  1108. /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
  1109. * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain NULs.)
  1110. * If a) the headers include a Content-Length field and all bytes in
  1111. * the body are present, or b) there's no Content-Length field and
  1112. * all headers are present, then:
  1113. *
  1114. * - strdup headers into <b>*headers_out</b>, and NUL-terminate it.
  1115. * - memdup body into <b>*body_out</b>, and NUL-terminate it.
  1116. * - Then remove them from <b>buf</b>, and return 1.
  1117. *
  1118. * - If headers or body is NULL, discard that part of the buf.
  1119. * - If a headers or body doesn't fit in the arg, return -1.
  1120. * (We ensure that the headers or body don't exceed max len,
  1121. * _even if_ we're planning to discard them.)
  1122. * - If force_complete is true, then succeed even if not all of the
  1123. * content has arrived.
  1124. *
  1125. * Else, change nothing and return 0.
  1126. */
  1127. int
  1128. fetch_from_buf_http(buf_t *buf,
  1129. char **headers_out, size_t max_headerlen,
  1130. char **body_out, size_t *body_used, size_t max_bodylen,
  1131. int force_complete)
  1132. {
  1133. char *headers, *p;
  1134. size_t headerlen, bodylen, contentlen;
  1135. int crlf_offset;
  1136. check();
  1137. if (!buf->head)
  1138. return 0;
  1139. crlf_offset = buf_find_string_offset(buf, "\r\n\r\n", 4);
  1140. if (crlf_offset > (int)max_headerlen ||
  1141. (crlf_offset < 0 && buf->datalen > max_headerlen)) {
  1142. log_debug(LD_HTTP,"headers too long.");
  1143. return -1;
  1144. } else if (crlf_offset < 0) {
  1145. log_debug(LD_HTTP,"headers not all here yet.");
  1146. return 0;
  1147. }
  1148. /* Okay, we have a full header. Make sure it all appears in the first
  1149. * chunk. */
  1150. if ((int)buf->head->datalen < crlf_offset + 4)
  1151. buf_pullup(buf, crlf_offset+4, 0);
  1152. headerlen = crlf_offset + 4;
  1153. headers = buf->head->data;
  1154. bodylen = buf->datalen - headerlen;
  1155. log_debug(LD_HTTP,"headerlen %d, bodylen %d.", (int)headerlen, (int)bodylen);
  1156. if (max_headerlen <= headerlen) {
  1157. log_warn(LD_HTTP,"headerlen %d larger than %d. Failing.",
  1158. (int)headerlen, (int)max_headerlen-1);
  1159. return -1;
  1160. }
  1161. if (max_bodylen <= bodylen) {
  1162. log_warn(LD_HTTP,"bodylen %d larger than %d. Failing.",
  1163. (int)bodylen, (int)max_bodylen-1);
  1164. return -1;
  1165. }
  1166. #define CONTENT_LENGTH "\r\nContent-Length: "
  1167. p = (char*) tor_memstr(headers, headerlen, CONTENT_LENGTH);
  1168. if (p) {
  1169. int i;
  1170. i = atoi(p+strlen(CONTENT_LENGTH));
  1171. if (i < 0) {
  1172. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  1173. "someone is trying to crash us.");
  1174. return -1;
  1175. }
  1176. contentlen = i;
  1177. /* if content-length is malformed, then our body length is 0. fine. */
  1178. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  1179. if (bodylen < contentlen) {
  1180. if (!force_complete) {
  1181. log_debug(LD_HTTP,"body not all here yet.");
  1182. return 0; /* not all there yet */
  1183. }
  1184. }
  1185. if (bodylen > contentlen) {
  1186. bodylen = contentlen;
  1187. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  1188. }
  1189. }
  1190. /* all happy. copy into the appropriate places, and return 1 */
  1191. if (headers_out) {
  1192. *headers_out = tor_malloc(headerlen+1);
  1193. fetch_from_buf(*headers_out, headerlen, buf);
  1194. (*headers_out)[headerlen] = 0; /* NUL terminate it */
  1195. }
  1196. if (body_out) {
  1197. tor_assert(body_used);
  1198. *body_used = bodylen;
  1199. *body_out = tor_malloc(bodylen+1);
  1200. fetch_from_buf(*body_out, bodylen, buf);
  1201. (*body_out)[bodylen] = 0; /* NUL terminate it */
  1202. }
  1203. check();
  1204. return 1;
  1205. }
  1206. /**
  1207. * Wait this many seconds before warning the user about using SOCKS unsafely
  1208. * again (requires that WarnUnsafeSocks is turned on). */
  1209. #define SOCKS_WARN_INTERVAL 5
  1210. /** Warn that the user application has made an unsafe socks request using
  1211. * protocol <b>socks_protocol</b> on port <b>port</b>. Don't warn more than
  1212. * once per SOCKS_WARN_INTERVAL, unless <b>safe_socks</b> is set. */
  1213. static void
  1214. log_unsafe_socks_warning(int socks_protocol, const char *address,
  1215. uint16_t port, int safe_socks)
  1216. {
  1217. static ratelim_t socks_ratelim = RATELIM_INIT(SOCKS_WARN_INTERVAL);
  1218. or_options_t *options = get_options();
  1219. char *m = NULL;
  1220. if (! options->WarnUnsafeSocks)
  1221. return;
  1222. if (safe_socks || (m = rate_limit_log(&socks_ratelim, approx_time()))) {
  1223. log_warn(LD_APP,
  1224. "Your application (using socks%d to port %d) is giving "
  1225. "Tor only an IP address. Applications that do DNS resolves "
  1226. "themselves may leak information. Consider using Socks4A "
  1227. "(e.g. via privoxy or socat) instead. For more information, "
  1228. "please see https://wiki.torproject.org/TheOnionRouter/"
  1229. "TorFAQ#SOCKSAndDNS.%s%s",
  1230. socks_protocol,
  1231. (int)port,
  1232. safe_socks ? " Rejecting." : "",
  1233. m ? m : "");
  1234. tor_free(m);
  1235. }
  1236. control_event_client_status(LOG_WARN,
  1237. "DANGEROUS_SOCKS PROTOCOL=SOCKS%d ADDRESS=%s:%d",
  1238. socks_protocol, address, (int)port);
  1239. }
  1240. /** Do not attempt to parse socks messages longer than this. This value is
  1241. * actually significantly higher than the longest possible socks message. */
  1242. #define MAX_SOCKS_MESSAGE_LEN 512
  1243. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  1244. * of the forms
  1245. * - socks4: "socksheader username\\0"
  1246. * - socks4a: "socksheader username\\0 destaddr\\0"
  1247. * - socks5 phase one: "version #methods methods"
  1248. * - socks5 phase two: "version command 0 addresstype..."
  1249. * If it's a complete and valid handshake, and destaddr fits in
  1250. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  1251. * assign to <b>req</b>, and return 1.
  1252. *
  1253. * If it's invalid or too big, return -1.
  1254. *
  1255. * Else it's not all there yet, leave buf alone and return 0.
  1256. *
  1257. * If you want to specify the socks reply, write it into <b>req->reply</b>
  1258. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  1259. *
  1260. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  1261. * the connection is possibly leaking DNS requests locally or not.
  1262. *
  1263. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  1264. *
  1265. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  1266. * undefined.
  1267. */
  1268. int
  1269. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  1270. int log_sockstype, int safe_socks)
  1271. {
  1272. unsigned int len;
  1273. char tmpbuf[TOR_ADDR_BUF_LEN+1];
  1274. tor_addr_t destaddr;
  1275. uint32_t destip;
  1276. uint8_t socksver;
  1277. enum {socks4, socks4a} socks4_prot = socks4a;
  1278. char *next, *startaddr;
  1279. struct in_addr in;
  1280. if (buf->datalen < 2) /* version and another byte */
  1281. return 0;
  1282. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, 0);
  1283. tor_assert(buf->head && buf->head->datalen >= 2);
  1284. socksver = *buf->head->data;
  1285. switch (socksver) { /* which version of socks? */
  1286. case 5: /* socks5 */
  1287. if (req->socks_version != 5) { /* we need to negotiate a method */
  1288. unsigned char nummethods = (unsigned char)*(buf->head->data+1);
  1289. tor_assert(!req->socks_version);
  1290. if (buf->datalen < 2u+nummethods)
  1291. return 0;
  1292. buf_pullup(buf, 2u+nummethods, 0);
  1293. if (!nummethods || !memchr(buf->head->data+2, 0, nummethods)) {
  1294. log_warn(LD_APP,
  1295. "socks5: offered methods don't include 'no auth'. "
  1296. "Rejecting.");
  1297. req->replylen = 2; /* 2 bytes of response */
  1298. req->reply[0] = 5;
  1299. req->reply[1] = '\xFF'; /* reject all methods */
  1300. return -1;
  1301. }
  1302. /* remove packet from buf. also remove any other extraneous
  1303. * bytes, to support broken socks clients. */
  1304. buf_clear(buf);
  1305. req->replylen = 2; /* 2 bytes of response */
  1306. req->reply[0] = 5; /* socks5 reply */
  1307. req->reply[1] = 0; /* tell client to use "none" auth method */
  1308. req->socks_version = 5; /* remember we've already negotiated auth */
  1309. log_debug(LD_APP,"socks5: accepted method 0");
  1310. return 0;
  1311. }
  1312. /* we know the method; read in the request */
  1313. log_debug(LD_APP,"socks5: checking request");
  1314. if (buf->datalen < 8) /* basic info plus >=2 for addr plus 2 for port */
  1315. return 0; /* not yet */
  1316. tor_assert(buf->head->datalen >= 8);
  1317. req->command = (unsigned char) *(buf->head->data+1);
  1318. if (req->command != SOCKS_COMMAND_CONNECT &&
  1319. req->command != SOCKS_COMMAND_RESOLVE &&
  1320. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  1321. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  1322. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  1323. req->command);
  1324. return -1;
  1325. }
  1326. switch (*(buf->head->data+3)) { /* address type */
  1327. case 1: /* IPv4 address */
  1328. case 4: /* IPv6 address */ {
  1329. const int is_v6 = *(buf->head->data+3) == 4;
  1330. const unsigned addrlen = is_v6 ? 16 : 4;
  1331. log_debug(LD_APP,"socks5: ipv4 address type");
  1332. if (buf->datalen < 6+addrlen) /* ip/port there? */
  1333. return 0; /* not yet */
  1334. if (is_v6)
  1335. tor_addr_from_ipv6_bytes(&destaddr, buf->head->data+4);
  1336. else
  1337. tor_addr_from_ipv4n(&destaddr, get_uint32(buf->head->data+4));
  1338. tor_addr_to_str(tmpbuf, &destaddr, sizeof(tmpbuf), 1);
  1339. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1340. log_warn(LD_APP,
  1341. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  1342. "Rejecting.",
  1343. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  1344. return -1;
  1345. }
  1346. strlcpy(req->address,tmpbuf,sizeof(req->address));
  1347. req->port = ntohs(get_uint16(buf->head->data+4+addrlen));
  1348. buf_remove_from_front(buf, 6+addrlen);
  1349. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  1350. !addressmap_have_mapping(req->address,0)) {
  1351. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  1352. if (safe_socks)
  1353. return -1;
  1354. }
  1355. return 1;
  1356. }
  1357. case 3: /* fqdn */
  1358. log_debug(LD_APP,"socks5: fqdn address type");
  1359. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  1360. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  1361. "hostname type. Rejecting.");
  1362. return -1;
  1363. }
  1364. len = (unsigned char)*(buf->head->data+4);
  1365. if (buf->datalen < 7+len) /* addr/port there? */
  1366. return 0; /* not yet */
  1367. buf_pullup(buf, 7+len, 0);
  1368. tor_assert(buf->head->datalen >= 7+len);
  1369. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  1370. log_warn(LD_APP,
  1371. "socks5 hostname is %d bytes, which doesn't fit in "
  1372. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  1373. return -1;
  1374. }
  1375. memcpy(req->address,buf->head->data+5,len);
  1376. req->address[len] = 0;
  1377. req->port = ntohs(get_uint16(buf->head->data+5+len));
  1378. buf_remove_from_front(buf, 5+len+2);
  1379. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1380. log_warn(LD_PROTOCOL,
  1381. "Your application (using socks5 to port %d) gave Tor "
  1382. "a malformed hostname: %s. Rejecting the connection.",
  1383. req->port, escaped(req->address));
  1384. return -1;
  1385. }
  1386. if (log_sockstype)
  1387. log_notice(LD_APP,
  1388. "Your application (using socks5 to port %d) gave "
  1389. "Tor a hostname, which means Tor will do the DNS resolve "
  1390. "for you. This is good.", req->port);
  1391. return 1;
  1392. default: /* unsupported */
  1393. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  1394. (int) *(buf->head->data+3));
  1395. return -1;
  1396. }
  1397. tor_assert(0);
  1398. case 4: /* socks4 */
  1399. /* http://archive.socks.permeo.com/protocol/socks4.protocol */
  1400. /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
  1401. req->socks_version = 4;
  1402. if (buf->datalen < SOCKS4_NETWORK_LEN) /* basic info available? */
  1403. return 0; /* not yet */
  1404. buf_pullup(buf, 1280, 0);
  1405. req->command = (unsigned char) *(buf->head->data+1);
  1406. if (req->command != SOCKS_COMMAND_CONNECT &&
  1407. req->command != SOCKS_COMMAND_RESOLVE) {
  1408. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  1409. * socks4.) */
  1410. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  1411. req->command);
  1412. return -1;
  1413. }
  1414. req->port = ntohs(get_uint16(buf->head->data+2));
  1415. destip = ntohl(get_uint32(buf->head->data+4));
  1416. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  1417. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  1418. return -1;
  1419. }
  1420. if (destip >> 8) {
  1421. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  1422. in.s_addr = htonl(destip);
  1423. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  1424. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1425. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  1426. (int)strlen(tmpbuf));
  1427. return -1;
  1428. }
  1429. log_debug(LD_APP,
  1430. "socks4: successfully read destip (%s)",
  1431. safe_str_client(tmpbuf));
  1432. socks4_prot = socks4;
  1433. }
  1434. next = memchr(buf->head->data+SOCKS4_NETWORK_LEN, 0,
  1435. buf->head->datalen-SOCKS4_NETWORK_LEN);
  1436. if (!next) {
  1437. if (buf->head->datalen >= 1024) {
  1438. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  1439. return -1;
  1440. }
  1441. log_debug(LD_APP,"socks4: Username not here yet.");
  1442. return 0;
  1443. }
  1444. tor_assert(next < CHUNK_WRITE_PTR(buf->head));
  1445. startaddr = NULL;
  1446. if (socks4_prot != socks4a &&
  1447. !addressmap_have_mapping(tmpbuf,0)) {
  1448. log_unsafe_socks_warning(4, tmpbuf, req->port, safe_socks);
  1449. if (safe_socks)
  1450. return -1;
  1451. }
  1452. if (socks4_prot == socks4a) {
  1453. if (next+1 == CHUNK_WRITE_PTR(buf->head)) {
  1454. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  1455. return 0;
  1456. }
  1457. startaddr = next+1;
  1458. next = memchr(startaddr, 0, CHUNK_WRITE_PTR(buf->head)-startaddr);
  1459. if (!next) {
  1460. if (buf->head->datalen >= 1024) {
  1461. log_debug(LD_APP,"socks4: Destaddr too long.");
  1462. return -1;
  1463. }
  1464. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  1465. return 0;
  1466. }
  1467. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  1468. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  1469. return -1;
  1470. }
  1471. // tor_assert(next < buf->cur+buf->datalen);
  1472. if (log_sockstype)
  1473. log_notice(LD_APP,
  1474. "Your application (using socks4a to port %d) gave "
  1475. "Tor a hostname, which means Tor will do the DNS resolve "
  1476. "for you. This is good.", req->port);
  1477. }
  1478. log_debug(LD_APP,"socks4: Everything is here. Success.");
  1479. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  1480. sizeof(req->address));
  1481. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1482. log_warn(LD_PROTOCOL,
  1483. "Your application (using socks4 to port %d) gave Tor "
  1484. "a malformed hostname: %s. Rejecting the connection.",
  1485. req->port, escaped(req->address));
  1486. return -1;
  1487. }
  1488. /* next points to the final \0 on inbuf */
  1489. buf_remove_from_front(buf, next - buf->head->data + 1);
  1490. return 1;
  1491. case 'G': /* get */
  1492. case 'H': /* head */
  1493. case 'P': /* put/post */
  1494. case 'C': /* connect */
  1495. strlcpy(req->reply,
  1496. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  1497. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  1498. "<html>\n"
  1499. "<head>\n"
  1500. "<title>Tor is not an HTTP Proxy</title>\n"
  1501. "</head>\n"
  1502. "<body>\n"
  1503. "<h1>Tor is not an HTTP Proxy</h1>\n"
  1504. "<p>\n"
  1505. "It appears you have configured your web browser to use Tor as an HTTP proxy."
  1506. "\n"
  1507. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  1508. "Please configure your client accordingly.\n"
  1509. "</p>\n"
  1510. "<p>\n"
  1511. "See <a href=\"https://www.torproject.org/documentation.html\">"
  1512. "https://www.torproject.org/documentation.html</a> for more "
  1513. "information.\n"
  1514. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  1515. " IE will be willing to display it. Comment comment comment comment "
  1516. " comment comment comment comment comment comment comment comment.-->\n"
  1517. "</p>\n"
  1518. "</body>\n"
  1519. "</html>\n"
  1520. , MAX_SOCKS_REPLY_LEN);
  1521. req->replylen = strlen(req->reply)+1;
  1522. /* fall through */
  1523. default: /* version is not socks4 or socks5 */
  1524. log_warn(LD_APP,
  1525. "Socks version %d not recognized. (Tor is not an http proxy.)",
  1526. *(buf->head->data));
  1527. {
  1528. char *tmp = tor_strndup(buf->head->data, 8); /*XXXX what if longer?*/
  1529. control_event_client_status(LOG_WARN,
  1530. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  1531. escaped(tmp));
  1532. tor_free(tmp);
  1533. }
  1534. return -1;
  1535. }
  1536. }
  1537. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  1538. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  1539. * incomplete response, 1 on success and -1 on error, in which case
  1540. * <b>reason</b> is set to a descriptive message (free() when finished
  1541. * with it).
  1542. *
  1543. * As a special case, 2 is returned when user/pass is required
  1544. * during SOCKS5 handshake and user/pass is configured.
  1545. */
  1546. int
  1547. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  1548. {
  1549. unsigned char *data;
  1550. size_t addrlen;
  1551. if (buf->datalen < 2)
  1552. return 0;
  1553. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, 0);
  1554. tor_assert(buf->head && buf->head->datalen >= 2);
  1555. data = (unsigned char *) buf->head->data;
  1556. switch (state) {
  1557. case PROXY_SOCKS4_WANT_CONNECT_OK:
  1558. /* Wait for the complete response */
  1559. if (buf->head->datalen < 8)
  1560. return 0;
  1561. if (data[1] != 0x5a) {
  1562. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  1563. return -1;
  1564. }
  1565. /* Success */
  1566. buf_remove_from_front(buf, 8);
  1567. return 1;
  1568. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  1569. /* we don't have any credentials */
  1570. if (data[1] != 0x00) {
  1571. *reason = tor_strdup("server doesn't support any of our "
  1572. "available authentication methods");
  1573. return -1;
  1574. }
  1575. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  1576. buf_clear(buf);
  1577. return 1;
  1578. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  1579. /* we have a username and password. return 1 if we can proceed without
  1580. * providing authentication, or 2 otherwise. */
  1581. switch (data[1]) {
  1582. case 0x00:
  1583. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  1584. "doesn't require authentication.");
  1585. buf_clear(buf);
  1586. return 1;
  1587. case 0x02:
  1588. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  1589. buf_clear(buf);
  1590. return 2;
  1591. /* fall through */
  1592. }
  1593. *reason = tor_strdup("server doesn't support any of our available "
  1594. "authentication methods");
  1595. return -1;
  1596. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  1597. /* handle server reply to rfc1929 authentication */
  1598. if (data[1] != 0x00) {
  1599. *reason = tor_strdup("authentication failed");
  1600. return -1;
  1601. }
  1602. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  1603. buf_clear(buf);
  1604. return 1;
  1605. case PROXY_SOCKS5_WANT_CONNECT_OK:
  1606. /* response is variable length. BND.ADDR, etc, isn't needed
  1607. * (don't bother with buf_pullup()), but make sure to eat all
  1608. * the data used */
  1609. /* wait for address type field to arrive */
  1610. if (buf->datalen < 4)
  1611. return 0;
  1612. switch (data[3]) {
  1613. case 0x01: /* ip4 */
  1614. addrlen = 4;
  1615. break;
  1616. case 0x04: /* ip6 */
  1617. addrlen = 16;
  1618. break;
  1619. case 0x03: /* fqdn (can this happen here?) */
  1620. if (buf->datalen < 5)
  1621. return 0;
  1622. addrlen = 1 + data[4];
  1623. break;
  1624. default:
  1625. *reason = tor_strdup("invalid response to connect request");
  1626. return -1;
  1627. }
  1628. /* wait for address and port */
  1629. if (buf->datalen < 6 + addrlen)
  1630. return 0;
  1631. if (data[1] != 0x00) {
  1632. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  1633. return -1;
  1634. }
  1635. buf_remove_from_front(buf, 6 + addrlen);
  1636. return 1;
  1637. }
  1638. /* shouldn't get here... */
  1639. tor_assert(0);
  1640. return -1;
  1641. }
  1642. /** Return 1 iff buf looks more like it has an (obsolete) v0 controller
  1643. * command on it than any valid v1 controller command. */
  1644. int
  1645. peek_buf_has_control0_command(buf_t *buf)
  1646. {
  1647. if (buf->datalen >= 4) {
  1648. char header[4];
  1649. uint16_t cmd;
  1650. peek_from_buf(header, sizeof(header), buf);
  1651. cmd = ntohs(get_uint16(header+2));
  1652. if (cmd <= 0x14)
  1653. return 1; /* This is definitely not a v1 control command. */
  1654. }
  1655. return 0;
  1656. }
  1657. /** Return the index within <b>buf</b> at which <b>ch</b> first appears,
  1658. * or -1 if <b>ch</b> does not appear on buf. */
  1659. static off_t
  1660. buf_find_offset_of_char(buf_t *buf, char ch)
  1661. {
  1662. chunk_t *chunk;
  1663. off_t offset = 0;
  1664. for (chunk = buf->head; chunk; chunk = chunk->next) {
  1665. char *cp = memchr(chunk->data, ch, chunk->datalen);
  1666. if (cp)
  1667. return offset + (cp - chunk->data);
  1668. else
  1669. offset += chunk->datalen;
  1670. }
  1671. return -1;
  1672. }
  1673. /** Try to read a single LF-terminated line from <b>buf</b>, and write it,
  1674. * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
  1675. * Set *<b>data_len</b> to the number of bytes in the line, not counting the
  1676. * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
  1677. * have a whole line yet, and return -1 if the line length exceeds
  1678. * *<b>data_len</b>.
  1679. */
  1680. int
  1681. fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
  1682. {
  1683. size_t sz;
  1684. off_t offset;
  1685. if (!buf->head)
  1686. return 0;
  1687. offset = buf_find_offset_of_char(buf, '\n');
  1688. if (offset < 0)
  1689. return 0;
  1690. sz = (size_t) offset;
  1691. if (sz+2 > *data_len) {
  1692. *data_len = sz + 2;
  1693. return -1;
  1694. }
  1695. fetch_from_buf(data_out, sz+1, buf);
  1696. data_out[sz+1] = '\0';
  1697. *data_len = sz+1;
  1698. return 1;
  1699. }
  1700. /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
  1701. * zlib state <b>state</b>, appending the result to <b>buf</b>. If
  1702. * <b>done</b> is true, flush the data in the state and finish the
  1703. * compression/uncompression. Return -1 on failure, 0 on success. */
  1704. int
  1705. write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  1706. const char *data, size_t data_len,
  1707. int done)
  1708. {
  1709. char *next;
  1710. size_t old_avail, avail;
  1711. int over = 0;
  1712. do {
  1713. int need_new_chunk = 0;
  1714. if (!buf->tail || ! CHUNK_REMAINING_CAPACITY(buf->tail)) {
  1715. size_t cap = data_len / 4;
  1716. buf_add_chunk_with_capacity(buf, cap, 1);
  1717. }
  1718. next = CHUNK_WRITE_PTR(buf->tail);
  1719. avail = old_avail = CHUNK_REMAINING_CAPACITY(buf->tail);
  1720. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  1721. case TOR_ZLIB_DONE:
  1722. over = 1;
  1723. break;
  1724. case TOR_ZLIB_ERR:
  1725. return -1;
  1726. case TOR_ZLIB_OK:
  1727. if (data_len == 0)
  1728. over = 1;
  1729. break;
  1730. case TOR_ZLIB_BUF_FULL:
  1731. if (avail) {
  1732. /* Zlib says we need more room (ZLIB_BUF_FULL). Start a new chunk
  1733. * automatically, whether were going to or not. */
  1734. need_new_chunk = 1;
  1735. }
  1736. break;
  1737. }
  1738. buf->datalen += old_avail - avail;
  1739. buf->tail->datalen += old_avail - avail;
  1740. if (need_new_chunk) {
  1741. buf_add_chunk_with_capacity(buf, data_len/4, 1);
  1742. }
  1743. } while (!over);
  1744. check();
  1745. return 0;
  1746. }
  1747. /** Log an error and exit if <b>buf</b> is corrupted.
  1748. */
  1749. void
  1750. assert_buf_ok(buf_t *buf)
  1751. {
  1752. tor_assert(buf);
  1753. tor_assert(buf->magic == BUFFER_MAGIC);
  1754. if (! buf->head) {
  1755. tor_assert(!buf->tail);
  1756. tor_assert(buf->datalen == 0);
  1757. } else {
  1758. chunk_t *ch;
  1759. size_t total = 0;
  1760. tor_assert(buf->tail);
  1761. for (ch = buf->head; ch; ch = ch->next) {
  1762. total += ch->datalen;
  1763. tor_assert(ch->datalen <= ch->memlen);
  1764. tor_assert(ch->data >= &ch->mem[0]);
  1765. tor_assert(ch->data < &ch->mem[0]+ch->memlen);
  1766. tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
  1767. if (!ch->next)
  1768. tor_assert(ch == buf->tail);
  1769. }
  1770. tor_assert(buf->datalen == total);
  1771. }
  1772. }
  1773. #ifdef ENABLE_BUF_FREELISTS
  1774. /** Log an error and exit if <b>fl</b> is corrupted.
  1775. */
  1776. static void
  1777. assert_freelist_ok(chunk_freelist_t *fl)
  1778. {
  1779. chunk_t *ch;
  1780. int n;
  1781. tor_assert(fl->alloc_size > 0);
  1782. n = 0;
  1783. for (ch = fl->head; ch; ch = ch->next) {
  1784. tor_assert(CHUNK_ALLOC_SIZE(ch->memlen) == fl->alloc_size);
  1785. ++n;
  1786. }
  1787. tor_assert(n == fl->cur_length);
  1788. tor_assert(n >= fl->lowest_length);
  1789. tor_assert(n <= fl->max_length);
  1790. }
  1791. #endif