buffers.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295
  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-2010, 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. //#define PARANOIA
  26. #ifdef PARANOIA
  27. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  28. * <b>buf</b> is well-formed. */
  29. #define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
  30. #else
  31. #define check() STMT_NIL
  32. #endif
  33. /* Implementation notes:
  34. *
  35. * After flirting with memmove, and dallying with ring-buffers, we're finally
  36. * getting up to speed with the 1970s and implementing buffers as a linked
  37. * list of small chunks. Each buffer has such a list; data is removed from
  38. * the head of the list, and added at the tail. The list is singly linked,
  39. * and the buffer keeps a pointer to the head and the tail.
  40. *
  41. * Every chunk, except the tail, contains at least one byte of data. Data in
  42. * each chunk is contiguous.
  43. *
  44. * When you need to treat the first N characters on a buffer as a contiguous
  45. * string, use the buf_pullup function to make them so. Don't do this more
  46. * than necessary.
  47. *
  48. * The major free Unix kernels have handled buffers like this since, like,
  49. * forever.
  50. */
  51. static int parse_socks(const char *data, size_t datalen, socks_request_t *req,
  52. int log_sockstype, int safe_socks, ssize_t *drain_out,
  53. size_t *want_length_out);
  54. static int parse_socks_client(const uint8_t *data, size_t datalen,
  55. int state, char **reason,
  56. ssize_t *drain_out);
  57. /* Chunk manipulation functions */
  58. /** A single chunk on a buffer or in a freelist. */
  59. typedef struct chunk_t {
  60. struct chunk_t *next; /**< The next chunk on the buffer or freelist. */
  61. size_t datalen; /**< The number of bytes stored in this chunk */
  62. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  63. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  64. char mem[1]; /**< The actual memory used for storage in this chunk. May be
  65. * more than one byte long. */
  66. } chunk_t;
  67. #define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
  68. /** Return the number of bytes needed to allocate a chunk to hold
  69. * <b>memlen</b> bytes. */
  70. #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_HEADER_LEN + (memlen))
  71. /** Return the number of usable bytes in a chunk allocated with
  72. * malloc(<b>memlen</b>). */
  73. #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_HEADER_LEN)
  74. /** Return the next character in <b>chunk</b> onto which data can be appended.
  75. * If the chunk is full, this might be off the end of chunk->mem. */
  76. static INLINE char *
  77. CHUNK_WRITE_PTR(chunk_t *chunk)
  78. {
  79. return chunk->data + chunk->datalen;
  80. }
  81. /** Return the number of bytes that can be written onto <b>chunk</b> without
  82. * running out of space. */
  83. static INLINE size_t
  84. CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
  85. {
  86. return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
  87. }
  88. /** Move all bytes stored in <b>chunk</b> to the front of <b>chunk</b>->mem,
  89. * to free up space at the end. */
  90. static INLINE void
  91. chunk_repack(chunk_t *chunk)
  92. {
  93. if (chunk->datalen && chunk->data != &chunk->mem[0]) {
  94. memmove(chunk->mem, chunk->data, chunk->datalen);
  95. }
  96. chunk->data = &chunk->mem[0];
  97. }
  98. #ifdef ENABLE_BUF_FREELISTS
  99. /** A freelist of chunks. */
  100. typedef struct chunk_freelist_t {
  101. size_t alloc_size; /**< What size chunks does this freelist hold? */
  102. int max_length; /**< Never allow more than this number of chunks in the
  103. * freelist. */
  104. int slack; /**< When trimming the freelist, leave this number of extra
  105. * chunks beyond lowest_length.*/
  106. int cur_length; /**< How many chunks on the freelist now? */
  107. int lowest_length; /**< What's the smallest value of cur_length since the
  108. * last time we cleaned this freelist? */
  109. uint64_t n_alloc;
  110. uint64_t n_free;
  111. uint64_t n_hit;
  112. chunk_t *head; /**< First chunk on the freelist. */
  113. } chunk_freelist_t;
  114. /** Macro to help define freelists. */
  115. #define FL(a,m,s) { a, m, s, 0, 0, 0, 0, 0, NULL }
  116. /** Static array of freelists, sorted by alloc_len, terminated by an entry
  117. * with alloc_size of 0. */
  118. static chunk_freelist_t freelists[] = {
  119. FL(4096, 256, 8), FL(8192, 128, 4), FL(16384, 64, 4), FL(32768, 32, 2),
  120. FL(0, 0, 0)
  121. };
  122. #undef FL
  123. /** How many times have we looked for a chunk of a size that no freelist
  124. * could help with? */
  125. static uint64_t n_freelist_miss = 0;
  126. static void assert_freelist_ok(chunk_freelist_t *fl);
  127. /** Return the freelist to hold chunks of size <b>alloc</b>, or NULL if
  128. * no freelist exists for that size. */
  129. static INLINE chunk_freelist_t *
  130. get_freelist(size_t alloc)
  131. {
  132. int i;
  133. for (i=0; freelists[i].alloc_size <= alloc; ++i) {
  134. if (freelists[i].alloc_size == alloc) {
  135. return &freelists[i];
  136. }
  137. }
  138. return NULL;
  139. }
  140. /** Deallocate a chunk or put it on a freelist */
  141. static void
  142. chunk_free_unchecked(chunk_t *chunk)
  143. {
  144. size_t alloc;
  145. chunk_freelist_t *freelist;
  146. alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
  147. freelist = get_freelist(alloc);
  148. if (freelist && freelist->cur_length < freelist->max_length) {
  149. chunk->next = freelist->head;
  150. freelist->head = chunk;
  151. ++freelist->cur_length;
  152. } else {
  153. if (freelist)
  154. ++freelist->n_free;
  155. tor_free(chunk);
  156. }
  157. }
  158. /** Allocate a new chunk with a given allocation size, or get one from the
  159. * freelist. Note that a chunk with allocation size A can actually hold only
  160. * CHUNK_SIZE_WITH_ALLOC(A) bytes in its mem field. */
  161. static INLINE chunk_t *
  162. chunk_new_with_alloc_size(size_t alloc)
  163. {
  164. chunk_t *ch;
  165. chunk_freelist_t *freelist;
  166. tor_assert(alloc >= sizeof(chunk_t));
  167. freelist = get_freelist(alloc);
  168. if (freelist && freelist->head) {
  169. ch = freelist->head;
  170. freelist->head = ch->next;
  171. if (--freelist->cur_length < freelist->lowest_length)
  172. freelist->lowest_length = freelist->cur_length;
  173. ++freelist->n_hit;
  174. } else {
  175. /* XXXX take advantage of tor_malloc_roundup, once we know how that
  176. * affects freelists. */
  177. if (freelist)
  178. ++freelist->n_alloc;
  179. else
  180. ++n_freelist_miss;
  181. ch = tor_malloc(alloc);
  182. }
  183. ch->next = NULL;
  184. ch->datalen = 0;
  185. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  186. ch->data = &ch->mem[0];
  187. return ch;
  188. }
  189. #else
  190. static void
  191. chunk_free_unchecked(chunk_t *chunk)
  192. {
  193. tor_free(chunk);
  194. }
  195. static INLINE chunk_t *
  196. chunk_new_with_alloc_size(size_t alloc)
  197. {
  198. chunk_t *ch;
  199. ch = tor_malloc_roundup(&alloc);
  200. ch->next = NULL;
  201. ch->datalen = 0;
  202. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  203. ch->data = &ch->mem[0];
  204. return ch;
  205. }
  206. #endif
  207. /** Expand <b>chunk</b> until it can hold <b>sz</b> bytes, and return a
  208. * new pointer to <b>chunk</b>. Old pointers are no longer valid. */
  209. static INLINE chunk_t *
  210. chunk_grow(chunk_t *chunk, size_t sz)
  211. {
  212. off_t offset;
  213. tor_assert(sz > chunk->memlen);
  214. offset = chunk->data - chunk->mem;
  215. chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz));
  216. chunk->memlen = sz;
  217. chunk->data = chunk->mem + offset;
  218. return chunk;
  219. }
  220. /** If a read onto the end of a chunk would be smaller than this number, then
  221. * just start a new chunk. */
  222. #define MIN_READ_LEN 8
  223. /** Every chunk should take up at least this many bytes. */
  224. #define MIN_CHUNK_ALLOC 256
  225. /** No chunk should take up more than this many bytes. */
  226. #define MAX_CHUNK_ALLOC 65536
  227. /** Return the allocation size we'd like to use to hold <b>target</b>
  228. * bytes. */
  229. static INLINE size_t
  230. preferred_chunk_size(size_t target)
  231. {
  232. size_t sz = MIN_CHUNK_ALLOC;
  233. while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
  234. sz <<= 1;
  235. }
  236. return sz;
  237. }
  238. /** Remove from the freelists most chunks that have not been used since the
  239. * last call to buf_shrink_freelists(). */
  240. void
  241. buf_shrink_freelists(int free_all)
  242. {
  243. #ifdef ENABLE_BUF_FREELISTS
  244. int i;
  245. for (i = 0; freelists[i].alloc_size; ++i) {
  246. int slack = freelists[i].slack;
  247. assert_freelist_ok(&freelists[i]);
  248. if (free_all || freelists[i].lowest_length > slack) {
  249. int n_to_free = free_all ? freelists[i].cur_length :
  250. (freelists[i].lowest_length - slack);
  251. int n_to_skip = freelists[i].cur_length - n_to_free;
  252. int orig_n_to_free = n_to_free, n_freed=0;
  253. int orig_n_to_skip = n_to_skip;
  254. int new_length = n_to_skip;
  255. chunk_t **chp = &freelists[i].head;
  256. chunk_t *chunk;
  257. log_info(LD_MM, "Cleaning freelist for %d-byte chunks: length %d, "
  258. "keeping %d, dropping %d.",
  259. (int)freelists[i].alloc_size, freelists[i].cur_length,
  260. n_to_skip, n_to_free);
  261. tor_assert(n_to_skip + n_to_free == freelists[i].cur_length);
  262. while (n_to_skip) {
  263. if (! (*chp)->next) {
  264. log_warn(LD_BUG, "I wanted to skip %d chunks in the freelist for "
  265. "%d-byte chunks, but only found %d. (Length %d)",
  266. orig_n_to_skip, (int)freelists[i].alloc_size,
  267. orig_n_to_skip-n_to_skip, freelists[i].cur_length);
  268. assert_freelist_ok(&freelists[i]);
  269. return;
  270. }
  271. // tor_assert((*chp)->next);
  272. chp = &(*chp)->next;
  273. --n_to_skip;
  274. }
  275. chunk = *chp;
  276. *chp = NULL;
  277. while (chunk) {
  278. chunk_t *next = chunk->next;
  279. tor_free(chunk);
  280. chunk = next;
  281. --n_to_free;
  282. ++n_freed;
  283. ++freelists[i].n_free;
  284. }
  285. if (n_to_free) {
  286. log_warn(LD_BUG, "Freelist length for %d-byte chunks may have been "
  287. "messed up somehow.", (int)freelists[i].alloc_size);
  288. log_warn(LD_BUG, "There were %d chunks at the start. I decided to "
  289. "keep %d. I wanted to free %d. I freed %d. I somehow think "
  290. "I have %d left to free.",
  291. freelists[i].cur_length, n_to_skip, orig_n_to_free,
  292. n_freed, n_to_free);
  293. }
  294. // tor_assert(!n_to_free);
  295. freelists[i].cur_length = new_length;
  296. }
  297. freelists[i].lowest_length = freelists[i].cur_length;
  298. assert_freelist_ok(&freelists[i]);
  299. }
  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. /** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
  539. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  540. * *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
  541. * and the number of bytes read otherwise. */
  542. static INLINE int
  543. read_to_chunk(buf_t *buf, chunk_t *chunk, int fd, size_t at_most,
  544. int *reached_eof, int *socket_error)
  545. {
  546. ssize_t read_result;
  547. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  548. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  549. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  550. if (read_result < 0) {
  551. int e = tor_socket_errno(fd);
  552. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  553. #ifdef MS_WINDOWS
  554. if (e == WSAENOBUFS)
  555. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  556. #endif
  557. *socket_error = e;
  558. return -1;
  559. }
  560. return 0; /* would block. */
  561. } else if (read_result == 0) {
  562. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  563. *reached_eof = 1;
  564. return 0;
  565. } else { /* actually got bytes. */
  566. buf->datalen += read_result;
  567. chunk->datalen += read_result;
  568. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  569. (int)buf->datalen);
  570. tor_assert(read_result < INT_MAX);
  571. return (int)read_result;
  572. }
  573. }
  574. /** As read_to_chunk(), but return (negative) error code on error, blocking,
  575. * or TLS, and the number of bytes read otherwise. */
  576. static INLINE int
  577. read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  578. size_t at_most)
  579. {
  580. int read_result;
  581. tor_assert(CHUNK_REMAINING_CAPACITY(chunk) >= at_most);
  582. read_result = tor_tls_read(tls, CHUNK_WRITE_PTR(chunk), at_most);
  583. if (read_result < 0)
  584. return read_result;
  585. buf->datalen += read_result;
  586. chunk->datalen += read_result;
  587. return read_result;
  588. }
  589. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  590. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  591. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  592. * error; else return the number of bytes read.
  593. */
  594. /* XXXX021 indicate "read blocked" somehow? */
  595. int
  596. read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof,
  597. int *socket_error)
  598. {
  599. /* XXXX021 It's stupid to overload the return values for these functions:
  600. * "error status" and "number of bytes read" are not mutually exclusive.
  601. */
  602. int r = 0;
  603. size_t total_read = 0;
  604. check();
  605. tor_assert(reached_eof);
  606. tor_assert(s >= 0);
  607. while (at_most > total_read) {
  608. size_t readlen = at_most - total_read;
  609. chunk_t *chunk;
  610. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  611. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  612. if (readlen > chunk->memlen)
  613. readlen = chunk->memlen;
  614. } else {
  615. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  616. chunk = buf->tail;
  617. if (cap < readlen)
  618. readlen = cap;
  619. }
  620. r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
  621. check();
  622. if (r < 0)
  623. return r; /* Error */
  624. tor_assert(total_read+r < INT_MAX);
  625. total_read += r;
  626. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  627. break;
  628. }
  629. }
  630. return (int)total_read;
  631. }
  632. /** As read_to_buf, but reads from a TLS connection, and returns a TLS
  633. * status value rather than the number of bytes read.
  634. *
  635. * Using TLS on OR connections complicates matters in two ways.
  636. *
  637. * First, a TLS stream has its own read buffer independent of the
  638. * connection's read buffer. (TLS needs to read an entire frame from
  639. * the network before it can decrypt any data. Thus, trying to read 1
  640. * byte from TLS can require that several KB be read from the network
  641. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  642. * Because the data hasn't been read by Tor (it's still inside the TLS),
  643. * this means that sometimes a connection "has stuff to read" even when
  644. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  645. * used in connection.c to detect TLS objects with non-empty internal
  646. * buffers and read from them again.
  647. *
  648. * Second, the TLS stream's events do not correspond directly to network
  649. * events: sometimes, before a TLS stream can read, the network must be
  650. * ready to write -- or vice versa.
  651. */
  652. int
  653. read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  654. {
  655. int r = 0;
  656. size_t total_read = 0;
  657. check();
  658. while (at_most > total_read) {
  659. size_t readlen = at_most - total_read;
  660. chunk_t *chunk;
  661. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  662. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  663. if (readlen > chunk->memlen)
  664. readlen = chunk->memlen;
  665. } else {
  666. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  667. chunk = buf->tail;
  668. if (cap < readlen)
  669. readlen = cap;
  670. }
  671. r = read_to_chunk_tls(buf, chunk, tls, readlen);
  672. check();
  673. if (r < 0)
  674. return r; /* Error */
  675. tor_assert(total_read+r < INT_MAX);
  676. total_read += r;
  677. if ((size_t)r < readlen) /* eof, block, or no more to read. */
  678. break;
  679. }
  680. return (int)total_read;
  681. }
  682. /** Helper for flush_buf(): try to write <b>sz</b> bytes from chunk
  683. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct
  684. * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  685. * written on success, 0 on blocking, -1 on failure.
  686. */
  687. static INLINE int
  688. flush_chunk(int s, buf_t *buf, chunk_t *chunk, size_t sz,
  689. size_t *buf_flushlen)
  690. {
  691. ssize_t write_result;
  692. if (sz > chunk->datalen)
  693. sz = chunk->datalen;
  694. write_result = tor_socket_send(s, chunk->data, sz, 0);
  695. if (write_result < 0) {
  696. int e = tor_socket_errno(s);
  697. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  698. #ifdef MS_WINDOWS
  699. if (e == WSAENOBUFS)
  700. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  701. #endif
  702. return -1;
  703. }
  704. log_debug(LD_NET,"write() would block, returning.");
  705. return 0;
  706. } else {
  707. *buf_flushlen -= write_result;
  708. buf_remove_from_front(buf, write_result);
  709. tor_assert(write_result < INT_MAX);
  710. return (int)write_result;
  711. }
  712. }
  713. /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from chunk
  714. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
  715. * more if there is a forced pending write size.) On success, deduct the
  716. * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  717. * written on success, and a TOR_TLS error code on failure or blocking.
  718. */
  719. static INLINE int
  720. flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  721. size_t sz, size_t *buf_flushlen)
  722. {
  723. int r;
  724. size_t forced;
  725. char *data;
  726. forced = tor_tls_get_forced_write_size(tls);
  727. if (forced > sz)
  728. sz = forced;
  729. if (chunk) {
  730. data = chunk->data;
  731. tor_assert(sz <= chunk->datalen);
  732. } else {
  733. data = NULL;
  734. tor_assert(sz == 0);
  735. }
  736. r = tor_tls_write(tls, data, sz);
  737. if (r < 0)
  738. return r;
  739. if (*buf_flushlen > (size_t)r)
  740. *buf_flushlen -= r;
  741. else
  742. *buf_flushlen = 0;
  743. buf_remove_from_front(buf, r);
  744. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  745. r,(int)*buf_flushlen,(int)buf->datalen);
  746. return r;
  747. }
  748. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  749. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  750. * the number of bytes actually written, and remove the written bytes
  751. * from the buffer. Return the number of bytes written on success,
  752. * -1 on failure. Return 0 if write() would block.
  753. */
  754. int
  755. flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  756. {
  757. /* XXXX021 It's stupid to overload the return values for these functions:
  758. * "error status" and "number of bytes flushed" are not mutually exclusive.
  759. */
  760. int r;
  761. size_t flushed = 0;
  762. tor_assert(buf_flushlen);
  763. tor_assert(s >= 0);
  764. tor_assert(*buf_flushlen <= buf->datalen);
  765. tor_assert(sz <= *buf_flushlen);
  766. check();
  767. while (sz) {
  768. size_t flushlen0;
  769. tor_assert(buf->head);
  770. if (buf->head->datalen >= sz)
  771. flushlen0 = sz;
  772. else
  773. flushlen0 = buf->head->datalen;
  774. r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen);
  775. check();
  776. if (r < 0)
  777. return r;
  778. flushed += r;
  779. sz -= r;
  780. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  781. break;
  782. }
  783. tor_assert(flushed < INT_MAX);
  784. return (int)flushed;
  785. }
  786. /** As flush_buf(), but writes data to a TLS connection. Can write more than
  787. * <b>flushlen</b> bytes.
  788. */
  789. int
  790. flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen,
  791. size_t *buf_flushlen)
  792. {
  793. int r;
  794. size_t flushed = 0;
  795. ssize_t sz;
  796. tor_assert(buf_flushlen);
  797. tor_assert(*buf_flushlen <= buf->datalen);
  798. tor_assert(flushlen <= *buf_flushlen);
  799. sz = (ssize_t) flushlen;
  800. /* we want to let tls write even if flushlen is zero, because it might
  801. * have a partial record pending */
  802. check_no_tls_errors();
  803. check();
  804. do {
  805. size_t flushlen0;
  806. if (buf->head) {
  807. if ((ssize_t)buf->head->datalen >= sz)
  808. flushlen0 = sz;
  809. else
  810. flushlen0 = buf->head->datalen;
  811. } else {
  812. flushlen0 = 0;
  813. }
  814. r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen);
  815. check();
  816. if (r < 0)
  817. return r;
  818. flushed += r;
  819. sz -= r;
  820. if (r == 0) /* Can't flush any more now. */
  821. break;
  822. } while (sz > 0);
  823. tor_assert(flushed < INT_MAX);
  824. return (int)flushed;
  825. }
  826. /** Append <b>string_len</b> bytes from <b>string</b> to the end of
  827. * <b>buf</b>.
  828. *
  829. * Return the new length of the buffer on success, -1 on failure.
  830. */
  831. int
  832. write_to_buf(const char *string, size_t string_len, buf_t *buf)
  833. {
  834. if (!string_len)
  835. return (int)buf->datalen;
  836. check();
  837. while (string_len) {
  838. size_t copy;
  839. if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
  840. buf_add_chunk_with_capacity(buf, string_len, 1);
  841. copy = CHUNK_REMAINING_CAPACITY(buf->tail);
  842. if (copy > string_len)
  843. copy = string_len;
  844. memcpy(CHUNK_WRITE_PTR(buf->tail), string, copy);
  845. string_len -= copy;
  846. string += copy;
  847. buf->datalen += copy;
  848. buf->tail->datalen += copy;
  849. }
  850. check();
  851. tor_assert(buf->datalen < INT_MAX);
  852. return (int)buf->datalen;
  853. }
  854. /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
  855. * onto <b>string</b>.
  856. */
  857. static INLINE void
  858. peek_from_buf(char *string, size_t string_len, const buf_t *buf)
  859. {
  860. chunk_t *chunk;
  861. tor_assert(string);
  862. /* make sure we don't ask for too much */
  863. tor_assert(string_len <= buf->datalen);
  864. /* assert_buf_ok(buf); */
  865. chunk = buf->head;
  866. while (string_len) {
  867. size_t copy = string_len;
  868. tor_assert(chunk);
  869. if (chunk->datalen < copy)
  870. copy = chunk->datalen;
  871. memcpy(string, chunk->data, copy);
  872. string_len -= copy;
  873. string += copy;
  874. chunk = chunk->next;
  875. }
  876. }
  877. /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
  878. * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
  879. * must be \<= the number of bytes on the buffer.
  880. */
  881. int
  882. fetch_from_buf(char *string, size_t string_len, buf_t *buf)
  883. {
  884. /* There must be string_len bytes in buf; write them onto string,
  885. * then memmove buf back (that is, remove them from buf).
  886. *
  887. * Return the number of bytes still on the buffer. */
  888. check();
  889. peek_from_buf(string, string_len, buf);
  890. buf_remove_from_front(buf, string_len);
  891. check();
  892. tor_assert(buf->datalen < INT_MAX);
  893. return (int)buf->datalen;
  894. }
  895. /** Check <b>buf</b> for a variable-length cell according to the rules of link
  896. * protocol version <b>linkproto</b>. If one is found, pull it off the buffer
  897. * and assign a newly allocated var_cell_t to *<b>out</b>, and return 1.
  898. * Return 0 if whatever is on the start of buf_t is not a variable-length
  899. * cell. Return 1 and set *<b>out</b> to NULL if there seems to be the start
  900. * of a variable-length cell on <b>buf</b>, but the whole thing isn't there
  901. * yet. */
  902. int
  903. fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
  904. {
  905. char hdr[VAR_CELL_HEADER_SIZE];
  906. var_cell_t *result;
  907. uint8_t command;
  908. uint16_t length;
  909. /* If linkproto is unknown (0) or v2 (2), variable-length cells work as
  910. * implemented here. If it's 1, there are no variable-length cells. Tor
  911. * does not support other versions right now, and so can't negotiate them.
  912. */
  913. if (linkproto == 1)
  914. return 0;
  915. check();
  916. *out = NULL;
  917. if (buf->datalen < VAR_CELL_HEADER_SIZE)
  918. return 0;
  919. peek_from_buf(hdr, sizeof(hdr), buf);
  920. command = get_uint8(hdr+2);
  921. if (!(CELL_COMMAND_IS_VAR_LENGTH(command)))
  922. return 0;
  923. length = ntohs(get_uint16(hdr+3));
  924. if (buf->datalen < (size_t)(VAR_CELL_HEADER_SIZE+length))
  925. return 1;
  926. result = var_cell_new(length);
  927. result->command = command;
  928. result->circ_id = ntohs(get_uint16(hdr));
  929. buf_remove_from_front(buf, VAR_CELL_HEADER_SIZE);
  930. peek_from_buf(result->payload, length, buf);
  931. buf_remove_from_front(buf, length);
  932. check();
  933. *out = result;
  934. return 1;
  935. }
  936. #ifdef USE_BUFFEREVENTS
  937. /** Try to read <b>n</b> bytes from <b>buf</b> at <b>pos</b> (which may be
  938. * NULL for the start fo the buffer), copying the data only if necessary. Set
  939. * *<b>data</b> to a pointer to the desired bytes. Set <b>free_out</b> to 1
  940. * if we needed to malloc *<b>data</b> because the original bytes were
  941. * noncontiguous; 0 otherwise. Return the number of bytes actually available
  942. * at <b>data</b>.
  943. */
  944. static size_t
  945. inspect_evbuffer(struct evbuffer *buf, char **data, size_t n, int *free_out,
  946. struct evbuffer_ptr *pos)
  947. {
  948. int n_vecs, i;
  949. if (evbuffer_get_length(buf) < n)
  950. n = evbuffer_get_length(buf);
  951. if (n == 0)
  952. return 0;
  953. n_vecs = evbuffer_peek(buf, n, pos, NULL, 0);
  954. if (n_vecs <= 0)
  955. return -1;
  956. if (n_vecs == 1) {
  957. struct evbuffer_iovec v;
  958. i = evbuffer_peek(buf, n, pos, &v, 1);
  959. tor_assert(i == 1);
  960. *data = v.iov_base;
  961. *free_out = 0;
  962. return v.iov_len;
  963. } else {
  964. struct evbuffer_iovec *vecs =
  965. tor_malloc(sizeof(struct evbuffer_iovec)*n_vecs);
  966. size_t copied = 0;
  967. i = evbuffer_peek(buf, n, NULL, vecs, n_vecs);
  968. tor_assert(i == n_vecs);
  969. *data = tor_malloc(n);
  970. for (i=0; i < n_vecs; ++i) {
  971. size_t copy = n - copied;
  972. if (copy > vecs[i].iov_len)
  973. copy = vecs[i].iov_len;
  974. tor_assert(copied+copy <= n);
  975. memcpy(data+copied, vecs[i].iov_base, copy);
  976. copied += copy;
  977. }
  978. *free_out = 0;
  979. return copied;
  980. }
  981. }
  982. /** As fetch_var_cell_from_buf, buf works on an evbuffer. */
  983. int
  984. fetch_var_cell_from_evbuffer(struct evbuffer *buf, var_cell_t **out,
  985. int linkproto)
  986. {
  987. char *hdr = NULL;
  988. int free_hdr = 0;
  989. size_t n;
  990. size_t buf_len;
  991. uint8_t command;
  992. uint16_t cell_length;
  993. var_cell_t *cell;
  994. int result;
  995. if (linkproto == 1)
  996. return 0;
  997. *out = NULL;
  998. buf_len = evbuffer_get_length(buf);
  999. if (buf_len < VAR_CELL_HEADER_SIZE) {
  1000. result = 0;
  1001. goto done;
  1002. }
  1003. n = inspect_evbuffer(buf, &hdr, VAR_CELL_HEADER_SIZE, &free_hdr, NULL);
  1004. tor_assert(n >= VAR_CELL_HEADER_SIZE);
  1005. command = get_uint8(hdr+2);
  1006. if (!(CELL_COMMAND_IS_VAR_LENGTH(command))) {
  1007. result = 0;
  1008. goto done;
  1009. }
  1010. cell_length = ntohs(get_uint16(hdr+3));
  1011. if (buf_len < (size_t)(VAR_CELL_HEADER_SIZE+cell_length)) {
  1012. result = 1; /* Not all here yet. */
  1013. goto done;
  1014. }
  1015. cell = var_cell_new(cell_length);
  1016. cell->command = command;
  1017. cell->circ_id = ntohs(get_uint16(hdr));
  1018. evbuffer_drain(buf, VAR_CELL_HEADER_SIZE);
  1019. evbuffer_remove(buf, cell->payload, cell_length);
  1020. *out = cell;
  1021. result = 1;
  1022. done:
  1023. if (free_hdr && hdr)
  1024. tor_free(hdr);
  1025. return result;
  1026. }
  1027. #endif
  1028. /** Move up to *<b>buf_flushlen</b> bytes from <b>buf_in</b> to
  1029. * <b>buf_out</b>, and modify *<b>buf_flushlen</b> appropriately.
  1030. * Return the number of bytes actually copied.
  1031. */
  1032. int
  1033. move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
  1034. {
  1035. /* XXXX we can do way better here, but this doesn't turn up in any
  1036. * profiles. */
  1037. char b[4096];
  1038. size_t cp, len;
  1039. len = *buf_flushlen;
  1040. if (len > buf_in->datalen)
  1041. len = buf_in->datalen;
  1042. cp = len; /* Remember the number of bytes we intend to copy. */
  1043. tor_assert(cp < INT_MAX);
  1044. while (len) {
  1045. /* This isn't the most efficient implementation one could imagine, since
  1046. * it does two copies instead of 1, but I kinda doubt that this will be
  1047. * critical path. */
  1048. size_t n = len > sizeof(b) ? sizeof(b) : len;
  1049. fetch_from_buf(b, n, buf_in);
  1050. write_to_buf(b, n, buf_out);
  1051. len -= n;
  1052. }
  1053. *buf_flushlen -= cp;
  1054. return (int)cp;
  1055. }
  1056. /** Internal structure: represents a position in a buffer. */
  1057. typedef struct buf_pos_t {
  1058. const chunk_t *chunk; /**< Which chunk are we pointing to? */
  1059. int pos;/**< Which character inside the chunk's data are we pointing to? */
  1060. size_t chunk_pos; /**< Total length of all previous chunks. */
  1061. } buf_pos_t;
  1062. /** Initialize <b>out</b> to point to the first character of <b>buf</b>.*/
  1063. static void
  1064. buf_pos_init(const buf_t *buf, buf_pos_t *out)
  1065. {
  1066. out->chunk = buf->head;
  1067. out->pos = 0;
  1068. out->chunk_pos = 0;
  1069. }
  1070. /** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
  1071. * position of <b>out</b>, or later. Return -1 if no instances are found;
  1072. * otherwise returns the absolute position of the character. */
  1073. static off_t
  1074. buf_find_pos_of_char(char ch, buf_pos_t *out)
  1075. {
  1076. const chunk_t *chunk;
  1077. int pos;
  1078. tor_assert(out);
  1079. if (out->chunk) {
  1080. if (out->chunk->datalen) {
  1081. tor_assert(out->pos < (off_t)out->chunk->datalen);
  1082. } else {
  1083. tor_assert(out->pos == 0);
  1084. }
  1085. }
  1086. pos = out->pos;
  1087. for (chunk = out->chunk; chunk; chunk = chunk->next) {
  1088. char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
  1089. if (cp) {
  1090. out->chunk = chunk;
  1091. tor_assert(cp - chunk->data < INT_MAX);
  1092. out->pos = (int)(cp - chunk->data);
  1093. return out->chunk_pos + out->pos;
  1094. } else {
  1095. out->chunk_pos += chunk->datalen;
  1096. pos = 0;
  1097. }
  1098. }
  1099. return -1;
  1100. }
  1101. /** Advance <b>pos</b> by a single character, if there are any more characters
  1102. * in the buffer. Returns 0 on success, -1 on failure. */
  1103. static INLINE int
  1104. buf_pos_inc(buf_pos_t *pos)
  1105. {
  1106. ++pos->pos;
  1107. if (pos->pos == (off_t)pos->chunk->datalen) {
  1108. if (!pos->chunk->next)
  1109. return -1;
  1110. pos->chunk_pos += pos->chunk->datalen;
  1111. pos->chunk = pos->chunk->next;
  1112. pos->pos = 0;
  1113. }
  1114. return 0;
  1115. }
  1116. /** Return true iff the <b>n</b>-character string in <b>s</b> appears
  1117. * (verbatim) at <b>pos</b>. */
  1118. static int
  1119. buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
  1120. {
  1121. buf_pos_t p;
  1122. if (!n)
  1123. return 1;
  1124. memcpy(&p, pos, sizeof(p));
  1125. while (1) {
  1126. char ch = p.chunk->data[p.pos];
  1127. if (ch != *s)
  1128. return 0;
  1129. ++s;
  1130. /* If we're out of characters that don't match, we match. Check this
  1131. * _before_ we test incrementing pos, in case we're at the end of the
  1132. * string. */
  1133. if (--n == 0)
  1134. return 1;
  1135. if (buf_pos_inc(&p)<0)
  1136. return 0;
  1137. }
  1138. }
  1139. /** Return the first position in <b>buf</b> at which the <b>n</b>-character
  1140. * string <b>s</b> occurs, or -1 if it does not occur. */
  1141. /*private*/ int
  1142. buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
  1143. {
  1144. buf_pos_t pos;
  1145. buf_pos_init(buf, &pos);
  1146. while (buf_find_pos_of_char(*s, &pos) >= 0) {
  1147. if (buf_matches_at_pos(&pos, s, n)) {
  1148. tor_assert(pos.chunk_pos + pos.pos < INT_MAX);
  1149. return (int)(pos.chunk_pos + pos.pos);
  1150. } else {
  1151. if (buf_pos_inc(&pos)<0)
  1152. return -1;
  1153. }
  1154. }
  1155. return -1;
  1156. }
  1157. /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
  1158. * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain NULs.)
  1159. * If a) the headers include a Content-Length field and all bytes in
  1160. * the body are present, or b) there's no Content-Length field and
  1161. * all headers are present, then:
  1162. *
  1163. * - strdup headers into <b>*headers_out</b>, and NUL-terminate it.
  1164. * - memdup body into <b>*body_out</b>, and NUL-terminate it.
  1165. * - Then remove them from <b>buf</b>, and return 1.
  1166. *
  1167. * - If headers or body is NULL, discard that part of the buf.
  1168. * - If a headers or body doesn't fit in the arg, return -1.
  1169. * (We ensure that the headers or body don't exceed max len,
  1170. * _even if_ we're planning to discard them.)
  1171. * - If force_complete is true, then succeed even if not all of the
  1172. * content has arrived.
  1173. *
  1174. * Else, change nothing and return 0.
  1175. */
  1176. int
  1177. fetch_from_buf_http(buf_t *buf,
  1178. char **headers_out, size_t max_headerlen,
  1179. char **body_out, size_t *body_used, size_t max_bodylen,
  1180. int force_complete)
  1181. {
  1182. char *headers, *p;
  1183. size_t headerlen, bodylen, contentlen;
  1184. int crlf_offset;
  1185. check();
  1186. if (!buf->head)
  1187. return 0;
  1188. crlf_offset = buf_find_string_offset(buf, "\r\n\r\n", 4);
  1189. if (crlf_offset > (int)max_headerlen ||
  1190. (crlf_offset < 0 && buf->datalen > max_headerlen)) {
  1191. log_debug(LD_HTTP,"headers too long.");
  1192. return -1;
  1193. } else if (crlf_offset < 0) {
  1194. log_debug(LD_HTTP,"headers not all here yet.");
  1195. return 0;
  1196. }
  1197. /* Okay, we have a full header. Make sure it all appears in the first
  1198. * chunk. */
  1199. if ((int)buf->head->datalen < crlf_offset + 4)
  1200. buf_pullup(buf, crlf_offset+4, 0);
  1201. headerlen = crlf_offset + 4;
  1202. headers = buf->head->data;
  1203. bodylen = buf->datalen - headerlen;
  1204. log_debug(LD_HTTP,"headerlen %d, bodylen %d.", (int)headerlen, (int)bodylen);
  1205. if (max_headerlen <= headerlen) {
  1206. log_warn(LD_HTTP,"headerlen %d larger than %d. Failing.",
  1207. (int)headerlen, (int)max_headerlen-1);
  1208. return -1;
  1209. }
  1210. if (max_bodylen <= bodylen) {
  1211. log_warn(LD_HTTP,"bodylen %d larger than %d. Failing.",
  1212. (int)bodylen, (int)max_bodylen-1);
  1213. return -1;
  1214. }
  1215. #define CONTENT_LENGTH "\r\nContent-Length: "
  1216. p = (char*) tor_memstr(headers, headerlen, CONTENT_LENGTH);
  1217. if (p) {
  1218. int i;
  1219. i = atoi(p+strlen(CONTENT_LENGTH));
  1220. if (i < 0) {
  1221. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  1222. "someone is trying to crash us.");
  1223. return -1;
  1224. }
  1225. contentlen = i;
  1226. /* if content-length is malformed, then our body length is 0. fine. */
  1227. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  1228. if (bodylen < contentlen) {
  1229. if (!force_complete) {
  1230. log_debug(LD_HTTP,"body not all here yet.");
  1231. return 0; /* not all there yet */
  1232. }
  1233. }
  1234. if (bodylen > contentlen) {
  1235. bodylen = contentlen;
  1236. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  1237. }
  1238. }
  1239. /* all happy. copy into the appropriate places, and return 1 */
  1240. if (headers_out) {
  1241. *headers_out = tor_malloc(headerlen+1);
  1242. fetch_from_buf(*headers_out, headerlen, buf);
  1243. (*headers_out)[headerlen] = 0; /* NUL terminate it */
  1244. }
  1245. if (body_out) {
  1246. tor_assert(body_used);
  1247. *body_used = bodylen;
  1248. *body_out = tor_malloc(bodylen+1);
  1249. fetch_from_buf(*body_out, bodylen, buf);
  1250. (*body_out)[bodylen] = 0; /* NUL terminate it */
  1251. }
  1252. check();
  1253. return 1;
  1254. }
  1255. #ifdef USE_BUFFEREVENTS
  1256. int
  1257. fetch_from_evbuffer_http(struct evbuffer *buf,
  1258. char **headers_out, size_t max_headerlen,
  1259. char **body_out, size_t *body_used, size_t max_bodylen,
  1260. int force_complete)
  1261. {
  1262. struct evbuffer_ptr crlf, content_length;
  1263. size_t headerlen, bodylen, contentlen;
  1264. crlf = evbuffer_search(buf, "\r\n\r\n", 4, NULL);
  1265. if (crlf.pos < 0) {
  1266. if (evbuffer_get_length(buf) > max_headerlen)
  1267. return -1; /* Headers too long. */
  1268. return 0; /* Headers not here yet. */
  1269. } else if (crlf.pos > (int)max_headerlen)
  1270. return -1; /* Headers too long. */
  1271. headerlen = crlf.pos + 4;
  1272. bodylen = evbuffer_get_length(buf) - headerlen;
  1273. if (bodylen > max_bodylen)
  1274. return -1; /* body too long */
  1275. content_length = evbuffer_search_range(buf, CONTENT_LENGTH,
  1276. strlen(CONTENT_LENGTH), NULL, &crlf);
  1277. if (content_length.pos >= 0) {
  1278. struct evbuffer_ptr eol;
  1279. char *data = NULL;
  1280. int free_data = 0;
  1281. int n, i;
  1282. n = evbuffer_ptr_set(buf, &content_length, strlen(CONTENT_LENGTH),
  1283. EVBUFFER_PTR_ADD);
  1284. tor_assert(n == 0);
  1285. eol = evbuffer_search_eol(buf, &content_length, NULL, EVBUFFER_EOL_CRLF);
  1286. tor_assert(eol.pos > content_length.pos);
  1287. tor_assert(eol.pos <= crlf.pos);
  1288. inspect_evbuffer(buf, &data, eol.pos - content_length.pos, &free_data,
  1289. &content_length);
  1290. i = atoi(data);
  1291. if (free_data)
  1292. tor_free(data);
  1293. if (i < 0) {
  1294. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  1295. "someone is trying to crash us.");
  1296. return -1;
  1297. }
  1298. contentlen = i;
  1299. /* if content-length is malformed, then our body length is 0. fine. */
  1300. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  1301. if (bodylen < contentlen) {
  1302. if (!force_complete) {
  1303. log_debug(LD_HTTP,"body not all here yet.");
  1304. return 0; /* not all there yet */
  1305. }
  1306. }
  1307. if (bodylen > contentlen) {
  1308. bodylen = contentlen;
  1309. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  1310. }
  1311. }
  1312. if (headers_out) {
  1313. *headers_out = tor_malloc(headerlen+1);
  1314. evbuffer_remove(buf, *headers_out, headerlen);
  1315. (*headers_out)[headerlen] = '\0';
  1316. }
  1317. if (body_out) {
  1318. tor_assert(headers_out);
  1319. tor_assert(body_used);
  1320. *body_used = bodylen;
  1321. *body_out = tor_malloc(bodylen+1);
  1322. evbuffer_remove(buf, *body_out, bodylen);
  1323. (*body_out)[bodylen] = '\0';
  1324. }
  1325. return 1;
  1326. }
  1327. #endif
  1328. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  1329. * of the forms
  1330. * - socks4: "socksheader username\\0"
  1331. * - socks4a: "socksheader username\\0 destaddr\\0"
  1332. * - socks5 phase one: "version #methods methods"
  1333. * - socks5 phase two: "version command 0 addresstype..."
  1334. * If it's a complete and valid handshake, and destaddr fits in
  1335. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  1336. * assign to <b>req</b>, and return 1.
  1337. *
  1338. * If it's invalid or too big, return -1.
  1339. *
  1340. * Else it's not all there yet, leave buf alone and return 0.
  1341. *
  1342. * If you want to specify the socks reply, write it into <b>req->reply</b>
  1343. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  1344. *
  1345. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  1346. * the connection is possibly leaking DNS requests locally or not.
  1347. *
  1348. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  1349. *
  1350. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  1351. * undefined.
  1352. */
  1353. int
  1354. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  1355. int log_sockstype, int safe_socks)
  1356. {
  1357. int res;
  1358. ssize_t n_drain;
  1359. size_t want_length = 128;
  1360. if (buf->datalen < 2) /* version and another byte */
  1361. return 0;
  1362. do {
  1363. n_drain = 0;
  1364. buf_pullup(buf, want_length, 0);
  1365. tor_assert(buf->head && buf->head->datalen >= 2);
  1366. want_length = 0;
  1367. res = parse_socks(buf->head->data, buf->head->datalen, req, log_sockstype,
  1368. safe_socks, &n_drain, &want_length);
  1369. if (n_drain < 0)
  1370. buf_clear(buf);
  1371. else if (n_drain > 0)
  1372. buf_remove_from_front(buf, n_drain);
  1373. } while (res == 0 && buf->head &&
  1374. buf->datalen > buf->head->datalen &&
  1375. want_length < buf->head->datalen);
  1376. return res;
  1377. }
  1378. #ifdef USE_BUFFEREVENTS
  1379. /* As fetch_from_buf_socks(), but targets an evbuffer instead. */
  1380. int
  1381. fetch_from_evbuffer_socks(struct evbuffer *buf, socks_request_t *req,
  1382. int log_sockstype, int safe_socks)
  1383. {
  1384. char *data;
  1385. ssize_t n_drain;
  1386. size_t datalen, buflen, want_length;
  1387. int res;
  1388. buflen = evbuffer_get_length(buf);
  1389. if (buflen < 2)
  1390. return 0;
  1391. {
  1392. struct evbuffer_iovec v;
  1393. int i;
  1394. want_length = evbuffer_get_contiguous_space(buf);
  1395. n_drain = 0;
  1396. i = evbuffer_peek(buf, want_length, NULL, &v, 1);
  1397. tor_assert(i == 1);
  1398. data = v.iov_base;
  1399. datalen = v.iov_len;
  1400. res = parse_socks(data, datalen, req, log_sockstype,
  1401. safe_socks, &n_drain, &want_length);
  1402. if (n_drain < 0)
  1403. evbuffer_drain(buf, evbuffer_get_length(buf));
  1404. else if (n_drain > 0)
  1405. evbuffer_drain(buf, n_drain);
  1406. if (res)
  1407. return res;
  1408. }
  1409. while (evbuffer_get_length(buf) > datalen) {
  1410. int free_data = 0;
  1411. n_drain = 0;
  1412. data = NULL;
  1413. datalen = inspect_evbuffer(buf, &data, want_length, &free_data, NULL);
  1414. res = parse_socks(data, datalen, req, log_sockstype,
  1415. safe_socks, &n_drain, &want_length);
  1416. if (free_data)
  1417. tor_free(data);
  1418. if (n_drain < 0)
  1419. evbuffer_drain(buf, evbuffer_get_length(buf));
  1420. else if (n_drain > 0)
  1421. evbuffer_drain(buf, n_drain);
  1422. if (res)
  1423. return res;
  1424. if (want_length <= datalen)
  1425. break;
  1426. }
  1427. return res;
  1428. }
  1429. #endif
  1430. /** Implementation helper to implement fetch_from_*_socks. Instead of looking
  1431. * at a buffer's contents, we look at the <b>datalen</b> bytes of data in
  1432. * <b>data</b>. Instead of removing data from the buffer, we set
  1433. * <b>drain_out</b> to the amount of data that should be removed (or -1 if the
  1434. * buffer should be cleared. Instead of pulling more data into the first
  1435. * chunk of the buffer, we set *<b>want_length_out</b> to the number of bytes
  1436. * we'd like to see in the input buffer. */
  1437. static int
  1438. parse_socks(const char *data, size_t datalen, socks_request_t *req,
  1439. int log_sockstype, int safe_socks, ssize_t *drain_out,
  1440. size_t *want_length_out)
  1441. {
  1442. unsigned int len;
  1443. char tmpbuf[TOR_ADDR_BUF_LEN+1];
  1444. tor_addr_t destaddr;
  1445. uint32_t destip;
  1446. uint8_t socksver;
  1447. enum {socks4, socks4a} socks4_prot = socks4a;
  1448. char *next, *startaddr;
  1449. struct in_addr in;
  1450. /* If the user connects with socks4 or the wrong variant of socks5,
  1451. * then log a warning to let him know that it might be unwise. */
  1452. static int have_warned_about_unsafe_socks = 0;
  1453. socksver = *data;
  1454. switch (socksver) { /* which version of socks? */
  1455. case 5: /* socks5 */
  1456. if (req->socks_version != 5) { /* we need to negotiate a method */
  1457. unsigned char nummethods = (unsigned char)*(data+1);
  1458. tor_assert(!req->socks_version);
  1459. if (datalen < 2u+nummethods) {
  1460. *want_length_out = 2u+nummethods;
  1461. return 0;
  1462. }
  1463. if (!nummethods || !memchr(data+2, 0, nummethods)) {
  1464. log_warn(LD_APP,
  1465. "socks5: offered methods don't include 'no auth'. "
  1466. "Rejecting.");
  1467. req->replylen = 2; /* 2 bytes of response */
  1468. req->reply[0] = 5;
  1469. req->reply[1] = '\xFF'; /* reject all methods */
  1470. return -1;
  1471. }
  1472. /* remove packet from buf. also remove any other extraneous
  1473. * bytes, to support broken socks clients. */
  1474. *drain_out = -1;
  1475. req->replylen = 2; /* 2 bytes of response */
  1476. req->reply[0] = 5; /* socks5 reply */
  1477. req->reply[1] = 0; /* tell client to use "none" auth method */
  1478. req->socks_version = 5; /* remember we've already negotiated auth */
  1479. log_debug(LD_APP,"socks5: accepted method 0");
  1480. return 0;
  1481. }
  1482. /* we know the method; read in the request */
  1483. log_debug(LD_APP,"socks5: checking request");
  1484. if (datalen < 8) {/* basic info plus >=2 for addr plus 2 for port */
  1485. *want_length_out = 8;
  1486. return 0; /* not yet */
  1487. }
  1488. req->command = (unsigned char) *(data+1);
  1489. if (req->command != SOCKS_COMMAND_CONNECT &&
  1490. req->command != SOCKS_COMMAND_RESOLVE &&
  1491. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  1492. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  1493. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  1494. req->command);
  1495. return -1;
  1496. }
  1497. switch (*(data+3)) { /* address type */
  1498. case 1: /* IPv4 address */
  1499. case 4: /* IPv6 address */ {
  1500. const int is_v6 = *(data+3) == 4;
  1501. const unsigned addrlen = is_v6 ? 16 : 4;
  1502. log_debug(LD_APP,"socks5: ipv4 address type");
  1503. if (datalen < 6+addrlen) {/* ip/port there? */
  1504. *want_length_out = 6+addrlen;
  1505. return 0; /* not yet */
  1506. }
  1507. if (is_v6)
  1508. tor_addr_from_ipv6_bytes(&destaddr, data+4);
  1509. else
  1510. tor_addr_from_ipv4n(&destaddr, get_uint32(data+4));
  1511. tor_addr_to_str(tmpbuf, &destaddr, sizeof(tmpbuf), 1);
  1512. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1513. log_warn(LD_APP,
  1514. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  1515. "Rejecting.",
  1516. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  1517. return -1;
  1518. }
  1519. strlcpy(req->address,tmpbuf,sizeof(req->address));
  1520. req->port = ntohs(get_uint16(data+4+addrlen));
  1521. *drain_out = 6+addrlen;
  1522. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  1523. !addressmap_have_mapping(req->address,0) &&
  1524. !have_warned_about_unsafe_socks) {
  1525. if (get_options()->WarnUnsafeSocks) {
  1526. log_warn(LD_APP,
  1527. "Your application (using socks5 to port %d) is giving "
  1528. "Tor only an IP address. Applications that do DNS resolves "
  1529. "themselves may leak information. Consider using Socks4A "
  1530. "(e.g. via privoxy or socat) instead. For more information, "
  1531. "please see https://wiki.torproject.org/TheOnionRouter/"
  1532. "TorFAQ#SOCKSAndDNS.%s", req->port,
  1533. safe_socks ? " Rejecting." : "");
  1534. /*have_warned_about_unsafe_socks = 1;*/
  1535. /*(for now, warn every time)*/
  1536. control_event_client_status(LOG_WARN,
  1537. "DANGEROUS_SOCKS PROTOCOL=SOCKS5 ADDRESS=%s:%d",
  1538. req->address, req->port);
  1539. }
  1540. if (safe_socks)
  1541. return -1;
  1542. }
  1543. return 1;
  1544. }
  1545. case 3: /* fqdn */
  1546. log_debug(LD_APP,"socks5: fqdn address type");
  1547. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  1548. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  1549. "hostname type. Rejecting.");
  1550. return -1;
  1551. }
  1552. len = (unsigned char)*(data+4);
  1553. if (datalen < 7+len) { /* addr/port there? */
  1554. *want_length_out = 7+len;
  1555. return 0; /* not yet */
  1556. }
  1557. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  1558. log_warn(LD_APP,
  1559. "socks5 hostname is %d bytes, which doesn't fit in "
  1560. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  1561. return -1;
  1562. }
  1563. memcpy(req->address,data+5,len);
  1564. req->address[len] = 0;
  1565. req->port = ntohs(get_uint16(data+5+len));
  1566. *drain_out = 5+len+2;
  1567. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1568. log_warn(LD_PROTOCOL,
  1569. "Your application (using socks5 to port %d) gave Tor "
  1570. "a malformed hostname: %s. Rejecting the connection.",
  1571. req->port, escaped(req->address));
  1572. return -1;
  1573. }
  1574. if (log_sockstype)
  1575. log_notice(LD_APP,
  1576. "Your application (using socks5 to port %d) gave "
  1577. "Tor a hostname, which means Tor will do the DNS resolve "
  1578. "for you. This is good.", req->port);
  1579. return 1;
  1580. default: /* unsupported */
  1581. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  1582. (int) *(data+3));
  1583. return -1;
  1584. }
  1585. tor_assert(0);
  1586. case 4: /* socks4 */
  1587. /* http://archive.socks.permeo.com/protocol/socks4.protocol */
  1588. /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
  1589. req->socks_version = 4;
  1590. if (datalen < SOCKS4_NETWORK_LEN) {/* basic info available? */
  1591. *want_length_out = SOCKS4_NETWORK_LEN;
  1592. return 0; /* not yet */
  1593. }
  1594. // buf_pullup(buf, 1280, 0);
  1595. req->command = (unsigned char) *(data+1);
  1596. if (req->command != SOCKS_COMMAND_CONNECT &&
  1597. req->command != SOCKS_COMMAND_RESOLVE) {
  1598. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  1599. * socks4.) */
  1600. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  1601. req->command);
  1602. return -1;
  1603. }
  1604. req->port = ntohs(get_uint16(data+2));
  1605. destip = ntohl(get_uint32(data+4));
  1606. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  1607. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  1608. return -1;
  1609. }
  1610. if (destip >> 8) {
  1611. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  1612. in.s_addr = htonl(destip);
  1613. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  1614. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1615. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  1616. (int)strlen(tmpbuf));
  1617. return -1;
  1618. }
  1619. log_debug(LD_APP,
  1620. "socks4: successfully read destip (%s)",
  1621. safe_str_client(tmpbuf));
  1622. socks4_prot = socks4;
  1623. }
  1624. next = memchr(data+SOCKS4_NETWORK_LEN, 0,
  1625. datalen-SOCKS4_NETWORK_LEN);
  1626. if (!next) {
  1627. if (datalen >= 1024) {
  1628. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  1629. return -1;
  1630. }
  1631. log_debug(LD_APP,"socks4: Username not here yet.");
  1632. *want_length_out = datalen+1024; /* ???? */
  1633. return 0;
  1634. }
  1635. tor_assert(next < data+datalen);
  1636. startaddr = NULL;
  1637. if (socks4_prot != socks4a &&
  1638. !addressmap_have_mapping(tmpbuf,0) &&
  1639. !have_warned_about_unsafe_socks) {
  1640. if (get_options()->WarnUnsafeSocks) {
  1641. log_warn(LD_APP,
  1642. "Your application (using socks4 to port %d) is giving Tor "
  1643. "only an IP address. Applications that do DNS resolves "
  1644. "themselves may leak information. Consider using Socks4A "
  1645. "(e.g. via privoxy or socat) instead. For more information, "
  1646. "please see https://wiki.torproject.org/TheOnionRouter/"
  1647. "TorFAQ#SOCKSAndDNS.%s", req->port,
  1648. safe_socks ? " Rejecting." : "");
  1649. /*have_warned_about_unsafe_socks = 1;*/
  1650. /*(for now, warn every time)*/
  1651. control_event_client_status(LOG_WARN,
  1652. "DANGEROUS_SOCKS PROTOCOL=SOCKS4 ADDRESS=%s:%d",
  1653. tmpbuf, req->port);
  1654. }
  1655. if (safe_socks)
  1656. return -1;
  1657. }
  1658. if (socks4_prot == socks4a) {
  1659. if (next+1 == data+datalen) {
  1660. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  1661. *want_length_out = datalen + 1024; /* ???? */
  1662. return 0;
  1663. }
  1664. startaddr = next+1;
  1665. next = memchr(startaddr, 0, data + datalen - startaddr);
  1666. if (!next) {
  1667. if (datalen >= 1024) {
  1668. log_debug(LD_APP,"socks4: Destaddr too long.");
  1669. return -1;
  1670. }
  1671. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  1672. *want_length_out = datalen + 1024;
  1673. return 0;
  1674. }
  1675. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  1676. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  1677. return -1;
  1678. }
  1679. // tor_assert(next < buf->cur+buf->datalen);
  1680. if (log_sockstype)
  1681. log_notice(LD_APP,
  1682. "Your application (using socks4a to port %d) gave "
  1683. "Tor a hostname, which means Tor will do the DNS resolve "
  1684. "for you. This is good.", req->port);
  1685. }
  1686. log_debug(LD_APP,"socks4: Everything is here. Success.");
  1687. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  1688. sizeof(req->address));
  1689. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1690. log_warn(LD_PROTOCOL,
  1691. "Your application (using socks4 to port %d) gave Tor "
  1692. "a malformed hostname: %s. Rejecting the connection.",
  1693. req->port, escaped(req->address));
  1694. return -1;
  1695. }
  1696. /* next points to the final \0 on inbuf */
  1697. *drain_out = next - data + 1;
  1698. return 1;
  1699. case 'G': /* get */
  1700. case 'H': /* head */
  1701. case 'P': /* put/post */
  1702. case 'C': /* connect */
  1703. strlcpy(req->reply,
  1704. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  1705. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  1706. "<html>\n"
  1707. "<head>\n"
  1708. "<title>Tor is not an HTTP Proxy</title>\n"
  1709. "</head>\n"
  1710. "<body>\n"
  1711. "<h1>Tor is not an HTTP Proxy</h1>\n"
  1712. "<p>\n"
  1713. "It appears you have configured your web browser to use Tor as an HTTP proxy."
  1714. "\n"
  1715. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  1716. "Please configure your client accordingly.\n"
  1717. "</p>\n"
  1718. "<p>\n"
  1719. "See <a href=\"https://www.torproject.org/documentation.html\">"
  1720. "https://www.torproject.org/documentation.html</a> for more "
  1721. "information.\n"
  1722. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  1723. " IE will be willing to display it. Comment comment comment comment "
  1724. " comment comment comment comment comment comment comment comment.-->\n"
  1725. "</p>\n"
  1726. "</body>\n"
  1727. "</html>\n"
  1728. , MAX_SOCKS_REPLY_LEN);
  1729. req->replylen = strlen(req->reply)+1;
  1730. /* fall through */
  1731. default: /* version is not socks4 or socks5 */
  1732. log_warn(LD_APP,
  1733. "Socks version %d not recognized. (Tor is not an http proxy.)",
  1734. *(data));
  1735. {
  1736. char *tmp = tor_strndup(data, 8); /*XXXX what if longer?*/
  1737. control_event_client_status(LOG_WARN,
  1738. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  1739. escaped(tmp));
  1740. tor_free(tmp);
  1741. }
  1742. return -1;
  1743. }
  1744. }
  1745. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  1746. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  1747. * incomplete response, 1 on success and -1 on error, in which case
  1748. * <b>reason</b> is set to a descriptive message (free() when finished
  1749. * with it).
  1750. *
  1751. * As a special case, 2 is returned when user/pass is required
  1752. * during SOCKS5 handshake and user/pass is configured.
  1753. */
  1754. int
  1755. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  1756. {
  1757. ssize_t drain = 0;
  1758. int r;
  1759. if (buf->datalen < 2)
  1760. return 0;
  1761. buf_pullup(buf, 128, 0);
  1762. tor_assert(buf->head && buf->head->datalen >= 2);
  1763. r = parse_socks_client((uint8_t*)buf->head->data, buf->head->datalen,
  1764. state, reason, &drain);
  1765. if (drain > 0)
  1766. buf_remove_from_front(buf, drain);
  1767. else if (drain < 0)
  1768. buf_clear(buf);
  1769. return r;
  1770. }
  1771. #ifdef USE_BUFFEREVENTS
  1772. /** As fetch_from_buf_socks_client, buf works on an evbuffer */
  1773. int
  1774. fetch_from_evbuffer_socks_client(struct evbuffer *buf, int state,
  1775. char **reason)
  1776. {
  1777. ssize_t drain = 0;
  1778. uint8_t *data;
  1779. size_t datalen = evbuffer_get_contiguous_space(buf);
  1780. int r;
  1781. data = evbuffer_pullup(buf, 128);
  1782. r = parse_socks_client(data, datalen, state, reason, &drain);
  1783. if (drain > 0)
  1784. evbuffer_drain(buf, drain);
  1785. else
  1786. evbuffer_drain(buf, evbuffer_get_length(buf));
  1787. return r;
  1788. }
  1789. #endif
  1790. /** Implementation logic for fetch_from_*_socks_client. */
  1791. static int
  1792. parse_socks_client(const uint8_t *data, size_t datalen,
  1793. int state, char **reason,
  1794. ssize_t *drain_out)
  1795. {
  1796. unsigned int addrlen;
  1797. *drain_out = 0;
  1798. if (datalen < 2)
  1799. return 0;
  1800. switch (state) {
  1801. case PROXY_SOCKS4_WANT_CONNECT_OK:
  1802. /* Wait for the complete response */
  1803. if (datalen < 8)
  1804. return 0;
  1805. if (data[1] != 0x5a) {
  1806. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  1807. return -1;
  1808. }
  1809. /* Success */
  1810. *drain_out = 8;
  1811. return 1;
  1812. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  1813. /* we don't have any credentials */
  1814. if (data[1] != 0x00) {
  1815. *reason = tor_strdup("server doesn't support any of our "
  1816. "available authentication methods");
  1817. return -1;
  1818. }
  1819. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  1820. *drain_out = -1;
  1821. return 1;
  1822. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  1823. /* we have a username and password. return 1 if we can proceed without
  1824. * providing authentication, or 2 otherwise. */
  1825. switch (data[1]) {
  1826. case 0x00:
  1827. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  1828. "doesn't require authentication.");
  1829. *drain_out = -1;
  1830. return 1;
  1831. case 0x02:
  1832. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  1833. *drain_out = -1;
  1834. return 2;
  1835. /* fall through */
  1836. }
  1837. *reason = tor_strdup("server doesn't support any of our available "
  1838. "authentication methods");
  1839. return -1;
  1840. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  1841. /* handle server reply to rfc1929 authentication */
  1842. if (data[1] != 0x00) {
  1843. *reason = tor_strdup("authentication failed");
  1844. return -1;
  1845. }
  1846. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  1847. *drain_out = -1;
  1848. return 1;
  1849. case PROXY_SOCKS5_WANT_CONNECT_OK:
  1850. /* response is variable length. BND.ADDR, etc, isn't needed
  1851. * (don't bother with buf_pullup()), but make sure to eat all
  1852. * the data used */
  1853. /* wait for address type field to arrive */
  1854. if (datalen < 4)
  1855. return 0;
  1856. switch (data[3]) {
  1857. case 0x01: /* ip4 */
  1858. addrlen = 4;
  1859. break;
  1860. case 0x04: /* ip6 */
  1861. addrlen = 16;
  1862. break;
  1863. case 0x03: /* fqdn (can this happen here?) */
  1864. if (datalen < 5)
  1865. return 0;
  1866. addrlen = 1 + data[4];
  1867. break;
  1868. default:
  1869. *reason = tor_strdup("invalid response to connect request");
  1870. return -1;
  1871. }
  1872. /* wait for address and port */
  1873. if (datalen < 6 + addrlen)
  1874. return 0;
  1875. if (data[1] != 0x00) {
  1876. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  1877. return -1;
  1878. }
  1879. *drain_out = 6 + addrlen;
  1880. return 1;
  1881. }
  1882. /* shouldn't get here... */
  1883. tor_assert(0);
  1884. return -1;
  1885. }
  1886. /** Return 1 iff buf looks more like it has an (obsolete) v0 controller
  1887. * command on it than any valid v1 controller command. */
  1888. int
  1889. peek_buf_has_control0_command(buf_t *buf)
  1890. {
  1891. if (buf->datalen >= 4) {
  1892. char header[4];
  1893. uint16_t cmd;
  1894. peek_from_buf(header, sizeof(header), buf);
  1895. cmd = ntohs(get_uint16(header+2));
  1896. if (cmd <= 0x14)
  1897. return 1; /* This is definitely not a v1 control command. */
  1898. }
  1899. return 0;
  1900. }
  1901. #ifdef USE_BUFFEREVENTS
  1902. int
  1903. peek_evbuffer_has_control0_command(struct evbuffer *buf)
  1904. {
  1905. int result = 0;
  1906. if (evbuffer_get_length(buf) >= 4) {
  1907. int free_out = 0;
  1908. char *data = NULL;
  1909. size_t n = inspect_evbuffer(buf, &data, 4, &free_out, NULL);
  1910. uint16_t cmd;
  1911. tor_assert(n >= 4);
  1912. cmd = ntohs(get_uint16(data+2));
  1913. if (cmd <= 0x14)
  1914. result = 1;
  1915. if (free_out)
  1916. tor_free(data);
  1917. }
  1918. return result;
  1919. }
  1920. #endif
  1921. /** Return the index within <b>buf</b> at which <b>ch</b> first appears,
  1922. * or -1 if <b>ch</b> does not appear on buf. */
  1923. static off_t
  1924. buf_find_offset_of_char(buf_t *buf, char ch)
  1925. {
  1926. chunk_t *chunk;
  1927. off_t offset = 0;
  1928. for (chunk = buf->head; chunk; chunk = chunk->next) {
  1929. char *cp = memchr(chunk->data, ch, chunk->datalen);
  1930. if (cp)
  1931. return offset + (cp - chunk->data);
  1932. else
  1933. offset += chunk->datalen;
  1934. }
  1935. return -1;
  1936. }
  1937. /** Try to read a single LF-terminated line from <b>buf</b>, and write it
  1938. * (including the LF), NUL-terminated, into the *<b>data_len</b> byte buffer
  1939. * at <b>data_out</b>. Set *<b>data_len</b> to the number of bytes in the
  1940. * line, not counting the terminating NUL. Return 1 if we read a whole line,
  1941. * return 0 if we don't have a whole line yet, and return -1 if the line
  1942. * length exceeds *<b>data_len</b>.
  1943. */
  1944. int
  1945. fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
  1946. {
  1947. size_t sz;
  1948. off_t offset;
  1949. if (!buf->head)
  1950. return 0;
  1951. offset = buf_find_offset_of_char(buf, '\n');
  1952. if (offset < 0)
  1953. return 0;
  1954. sz = (size_t) offset;
  1955. if (sz+2 > *data_len) {
  1956. *data_len = sz + 2;
  1957. return -1;
  1958. }
  1959. fetch_from_buf(data_out, sz+1, buf);
  1960. data_out[sz+1] = '\0';
  1961. *data_len = sz+1;
  1962. return 1;
  1963. }
  1964. /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
  1965. * zlib state <b>state</b>, appending the result to <b>buf</b>. If
  1966. * <b>done</b> is true, flush the data in the state and finish the
  1967. * compression/uncompression. Return -1 on failure, 0 on success. */
  1968. int
  1969. write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  1970. const char *data, size_t data_len,
  1971. int done)
  1972. {
  1973. char *next;
  1974. size_t old_avail, avail;
  1975. int over = 0;
  1976. do {
  1977. int need_new_chunk = 0;
  1978. if (!buf->tail || ! CHUNK_REMAINING_CAPACITY(buf->tail)) {
  1979. size_t cap = data_len / 4;
  1980. buf_add_chunk_with_capacity(buf, cap, 1);
  1981. }
  1982. next = CHUNK_WRITE_PTR(buf->tail);
  1983. avail = old_avail = CHUNK_REMAINING_CAPACITY(buf->tail);
  1984. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  1985. case TOR_ZLIB_DONE:
  1986. over = 1;
  1987. break;
  1988. case TOR_ZLIB_ERR:
  1989. return -1;
  1990. case TOR_ZLIB_OK:
  1991. if (data_len == 0)
  1992. over = 1;
  1993. break;
  1994. case TOR_ZLIB_BUF_FULL:
  1995. if (avail) {
  1996. /* Zlib says we need more room (ZLIB_BUF_FULL). Start a new chunk
  1997. * automatically, whether were going to or not. */
  1998. need_new_chunk = 1;
  1999. }
  2000. break;
  2001. }
  2002. buf->datalen += old_avail - avail;
  2003. buf->tail->datalen += old_avail - avail;
  2004. if (need_new_chunk) {
  2005. buf_add_chunk_with_capacity(buf, data_len/4, 1);
  2006. }
  2007. } while (!over);
  2008. check();
  2009. return 0;
  2010. }
  2011. #ifdef USE_BUFFEREVENTS
  2012. int
  2013. write_to_evbuffer_zlib(struct evbuffer *buf, tor_zlib_state_t *state,
  2014. const char *data, size_t data_len,
  2015. int done)
  2016. {
  2017. char *next;
  2018. size_t old_avail, avail;
  2019. int over = 0, n;
  2020. struct evbuffer_iovec vec[1];
  2021. do {
  2022. int need_new_chunk = 0;
  2023. {
  2024. size_t cap = data_len / 4;
  2025. if (cap < 128)
  2026. cap = 128;
  2027. /* XXXX NM this strategy is fragmentation-prone. We should really have
  2028. * two iovecs, and write first into the one, and then into the
  2029. * second if the first gets full. */
  2030. n = evbuffer_reserve_space(buf, cap, vec, 1);
  2031. tor_assert(n == 1);
  2032. }
  2033. next = vec[0].iov_base;
  2034. avail = old_avail = vec[0].iov_len;
  2035. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  2036. case TOR_ZLIB_DONE:
  2037. over = 1;
  2038. break;
  2039. case TOR_ZLIB_ERR:
  2040. return -1;
  2041. case TOR_ZLIB_OK:
  2042. if (data_len == 0)
  2043. over = 1;
  2044. break;
  2045. case TOR_ZLIB_BUF_FULL:
  2046. if (avail) {
  2047. /* Zlib says we need more room (ZLIB_BUF_FULL). Start a new chunk
  2048. * automatically, whether were going to or not. */
  2049. need_new_chunk = 1;
  2050. }
  2051. break;
  2052. }
  2053. /* XXXX possible infinite loop on BUF_FULL. */
  2054. vec[0].iov_len = old_avail - avail;
  2055. evbuffer_commit_space(buf, vec, 1);
  2056. } while (!over);
  2057. check();
  2058. return 0;
  2059. }
  2060. #endif
  2061. /** Log an error and exit if <b>buf</b> is corrupted.
  2062. */
  2063. void
  2064. assert_buf_ok(buf_t *buf)
  2065. {
  2066. tor_assert(buf);
  2067. tor_assert(buf->magic == BUFFER_MAGIC);
  2068. if (! buf->head) {
  2069. tor_assert(!buf->tail);
  2070. tor_assert(buf->datalen == 0);
  2071. } else {
  2072. chunk_t *ch;
  2073. size_t total = 0;
  2074. tor_assert(buf->tail);
  2075. for (ch = buf->head; ch; ch = ch->next) {
  2076. total += ch->datalen;
  2077. tor_assert(ch->datalen <= ch->memlen);
  2078. tor_assert(ch->data >= &ch->mem[0]);
  2079. tor_assert(ch->data < &ch->mem[0]+ch->memlen);
  2080. tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
  2081. if (!ch->next)
  2082. tor_assert(ch == buf->tail);
  2083. }
  2084. tor_assert(buf->datalen == total);
  2085. }
  2086. }
  2087. #ifdef ENABLE_BUF_FREELISTS
  2088. /** Log an error and exit if <b>fl</b> is corrupted.
  2089. */
  2090. static void
  2091. assert_freelist_ok(chunk_freelist_t *fl)
  2092. {
  2093. chunk_t *ch;
  2094. int n;
  2095. tor_assert(fl->alloc_size > 0);
  2096. n = 0;
  2097. for (ch = fl->head; ch; ch = ch->next) {
  2098. tor_assert(CHUNK_ALLOC_SIZE(ch->memlen) == fl->alloc_size);
  2099. ++n;
  2100. }
  2101. tor_assert(n == fl->cur_length);
  2102. tor_assert(n >= fl->lowest_length);
  2103. tor_assert(n <= fl->max_length);
  2104. }
  2105. #endif