buffers.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  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-2013, 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 "addressmap.h"
  15. #include "buffers.h"
  16. #include "config.h"
  17. #include "connection_edge.h"
  18. #include "connection_or.h"
  19. #include "control.h"
  20. #include "reasons.h"
  21. #include "ext_orport.h"
  22. #include "../common/util.h"
  23. #include "../common/torlog.h"
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. //#define PARANOIA
  28. #ifdef PARANOIA
  29. /** Helper: If PARANOIA is defined, assert that the buffer in local variable
  30. * <b>buf</b> is well-formed. */
  31. #define check() STMT_BEGIN assert_buf_ok(buf); STMT_END
  32. #else
  33. #define check() STMT_NIL
  34. #endif
  35. /* Implementation notes:
  36. *
  37. * After flirting with memmove, and dallying with ring-buffers, we're finally
  38. * getting up to speed with the 1970s and implementing buffers as a linked
  39. * list of small chunks. Each buffer has such a list; data is removed from
  40. * the head of the list, and added at the tail. The list is singly linked,
  41. * and the buffer keeps a pointer to the head and the tail.
  42. *
  43. * Every chunk, except the tail, contains at least one byte of data. Data in
  44. * each chunk is contiguous.
  45. *
  46. * When you need to treat the first N characters on a buffer as a contiguous
  47. * string, use the buf_pullup function to make them so. Don't do this more
  48. * than necessary.
  49. *
  50. * The major free Unix kernels have handled buffers like this since, like,
  51. * forever.
  52. */
  53. static int parse_socks(const char *data, size_t datalen, socks_request_t *req,
  54. int log_sockstype, int safe_socks, ssize_t *drain_out,
  55. size_t *want_length_out);
  56. static int parse_socks_client(const uint8_t *data, size_t datalen,
  57. int state, char **reason,
  58. ssize_t *drain_out);
  59. #define DEBUG_CHUNK_ALLOC
  60. /* Chunk manipulation functions */
  61. /** A single chunk on a buffer or in a freelist. */
  62. typedef struct chunk_t {
  63. struct chunk_t *next; /**< The next chunk on the buffer or freelist. */
  64. size_t datalen; /**< The number of bytes stored in this chunk */
  65. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  66. #ifdef DEBUG_CHUNK_ALLOC
  67. size_t DBG_alloc;
  68. #endif
  69. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  70. uint32_t inserted_time; /**< Timestamp in truncated ms since epoch
  71. * when this chunk was inserted. */
  72. char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
  73. * this chunk. */
  74. } chunk_t;
  75. #define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])
  76. /** Return the number of bytes needed to allocate a chunk to hold
  77. * <b>memlen</b> bytes. */
  78. #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_HEADER_LEN + (memlen))
  79. /** Return the number of usable bytes in a chunk allocated with
  80. * malloc(<b>memlen</b>). */
  81. #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_HEADER_LEN)
  82. /** Return the next character in <b>chunk</b> onto which data can be appended.
  83. * If the chunk is full, this might be off the end of chunk->mem. */
  84. static INLINE char *
  85. CHUNK_WRITE_PTR(chunk_t *chunk)
  86. {
  87. return chunk->data + chunk->datalen;
  88. }
  89. /** Return the number of bytes that can be written onto <b>chunk</b> without
  90. * running out of space. */
  91. static INLINE size_t
  92. CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
  93. {
  94. return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
  95. }
  96. /** Move all bytes stored in <b>chunk</b> to the front of <b>chunk</b>->mem,
  97. * to free up space at the end. */
  98. static INLINE void
  99. chunk_repack(chunk_t *chunk)
  100. {
  101. if (chunk->datalen && chunk->data != &chunk->mem[0]) {
  102. memmove(chunk->mem, chunk->data, chunk->datalen);
  103. }
  104. chunk->data = &chunk->mem[0];
  105. }
  106. #if defined(ENABLE_BUF_FREELISTS) || defined(RUNNING_DOXYGEN)
  107. /** A freelist of chunks. */
  108. typedef struct chunk_freelist_t {
  109. size_t alloc_size; /**< What size chunks does this freelist hold? */
  110. int max_length; /**< Never allow more than this number of chunks in the
  111. * freelist. */
  112. int slack; /**< When trimming the freelist, leave this number of extra
  113. * chunks beyond lowest_length.*/
  114. int cur_length; /**< How many chunks on the freelist now? */
  115. int lowest_length; /**< What's the smallest value of cur_length since the
  116. * last time we cleaned this freelist? */
  117. uint64_t n_alloc;
  118. uint64_t n_free;
  119. uint64_t n_hit;
  120. chunk_t *head; /**< First chunk on the freelist. */
  121. } chunk_freelist_t;
  122. /** Macro to help define freelists. */
  123. #define FL(a,m,s) { a, m, s, 0, 0, 0, 0, 0, NULL }
  124. /** Static array of freelists, sorted by alloc_len, terminated by an entry
  125. * with alloc_size of 0. */
  126. static chunk_freelist_t freelists[] = {
  127. FL(4096, 256, 8), FL(8192, 128, 4), FL(16384, 64, 4), FL(32768, 32, 2),
  128. FL(0, 0, 0)
  129. };
  130. #undef FL
  131. /** How many times have we looked for a chunk of a size that no freelist
  132. * could help with? */
  133. static uint64_t n_freelist_miss = 0;
  134. /** DOCDOC */
  135. static size_t total_bytes_allocated_in_chunks = 0;
  136. static void assert_freelist_ok(chunk_freelist_t *fl);
  137. /** Return the freelist to hold chunks of size <b>alloc</b>, or NULL if
  138. * no freelist exists for that size. */
  139. static INLINE chunk_freelist_t *
  140. get_freelist(size_t alloc)
  141. {
  142. int i;
  143. for (i=0; (freelists[i].alloc_size <= alloc &&
  144. freelists[i].alloc_size); ++i ) {
  145. if (freelists[i].alloc_size == alloc) {
  146. return &freelists[i];
  147. }
  148. }
  149. return NULL;
  150. }
  151. /** Deallocate a chunk or put it on a freelist */
  152. static void
  153. chunk_free_unchecked(chunk_t *chunk)
  154. {
  155. size_t alloc;
  156. chunk_freelist_t *freelist;
  157. alloc = CHUNK_ALLOC_SIZE(chunk->memlen);
  158. freelist = get_freelist(alloc);
  159. if (freelist && freelist->cur_length < freelist->max_length) {
  160. chunk->next = freelist->head;
  161. freelist->head = chunk;
  162. ++freelist->cur_length;
  163. } else {
  164. if (freelist)
  165. ++freelist->n_free;
  166. #ifdef DEBUG_CHUNK_ALLOC
  167. tor_assert(alloc == chunk->DBG_alloc);
  168. #endif
  169. tor_assert(total_bytes_allocated_in_chunks >= alloc);
  170. total_bytes_allocated_in_chunks -= alloc;
  171. tor_free(chunk);
  172. }
  173. }
  174. /** Allocate a new chunk with a given allocation size, or get one from the
  175. * freelist. Note that a chunk with allocation size A can actually hold only
  176. * CHUNK_SIZE_WITH_ALLOC(A) bytes in its mem field. */
  177. static INLINE chunk_t *
  178. chunk_new_with_alloc_size(size_t alloc)
  179. {
  180. chunk_t *ch;
  181. chunk_freelist_t *freelist;
  182. tor_assert(alloc >= sizeof(chunk_t));
  183. freelist = get_freelist(alloc);
  184. if (freelist && freelist->head) {
  185. ch = freelist->head;
  186. freelist->head = ch->next;
  187. if (--freelist->cur_length < freelist->lowest_length)
  188. freelist->lowest_length = freelist->cur_length;
  189. ++freelist->n_hit;
  190. } else {
  191. if (freelist)
  192. ++freelist->n_alloc;
  193. else
  194. ++n_freelist_miss;
  195. ch = tor_malloc(alloc);
  196. #ifdef DEBUG_CHUNK_ALLOC
  197. ch->DBG_alloc = alloc;
  198. #endif
  199. total_bytes_allocated_in_chunks += alloc;
  200. }
  201. ch->next = NULL;
  202. ch->datalen = 0;
  203. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  204. ch->data = &ch->mem[0];
  205. return ch;
  206. }
  207. #else
  208. static void
  209. chunk_free_unchecked(chunk_t *chunk)
  210. {
  211. if (!chunk)
  212. return;
  213. #ifdef DEBUG_CHUNK_ALLOC
  214. tor_assert(CHUNK_ALLOC_SIZE(chunk->memlen) == chunk->DBG_alloc);
  215. #endif
  216. tor_assert(total_bytes_allocated_in_chunks >=
  217. CHUNK_ALLOC_SIZE(chunk->memlen));
  218. total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
  219. tor_free(chunk);
  220. }
  221. static INLINE chunk_t *
  222. chunk_new_with_alloc_size(size_t alloc)
  223. {
  224. chunk_t *ch;
  225. ch = tor_malloc(alloc);
  226. ch->next = NULL;
  227. ch->datalen = 0;
  228. #ifdef DEBUG_CHUNK_ALLOC
  229. ch->DBG_alloc = alloc;
  230. #endif
  231. ch->memlen = CHUNK_SIZE_WITH_ALLOC(alloc);
  232. total_bytes_allocated_in_chunks += alloc;
  233. ch->data = &ch->mem[0];
  234. return ch;
  235. }
  236. #endif
  237. /** Expand <b>chunk</b> until it can hold <b>sz</b> bytes, and return a
  238. * new pointer to <b>chunk</b>. Old pointers are no longer valid. */
  239. static INLINE chunk_t *
  240. chunk_grow(chunk_t *chunk, size_t sz)
  241. {
  242. off_t offset;
  243. size_t memlen_orig = chunk->memlen;
  244. tor_assert(sz > chunk->memlen);
  245. offset = chunk->data - chunk->mem;
  246. chunk = tor_realloc(chunk, CHUNK_ALLOC_SIZE(sz));
  247. chunk->memlen = sz;
  248. chunk->data = chunk->mem + offset;
  249. #ifdef DEBUG_CHUNK_ALLOC
  250. tor_assert(chunk->DBG_alloc == CHUNK_ALLOC_SIZE(memlen_orig));
  251. chunk->DBG_alloc = CHUNK_ALLOC_SIZE(sz);
  252. #endif
  253. total_bytes_allocated_in_chunks +=
  254. CHUNK_ALLOC_SIZE(sz) - CHUNK_ALLOC_SIZE(memlen_orig);
  255. return chunk;
  256. }
  257. /** If a read onto the end of a chunk would be smaller than this number, then
  258. * just start a new chunk. */
  259. #define MIN_READ_LEN 8
  260. /** Every chunk should take up at least this many bytes. */
  261. #define MIN_CHUNK_ALLOC 256
  262. /** No chunk should take up more than this many bytes. */
  263. #define MAX_CHUNK_ALLOC 65536
  264. /** Return the allocation size we'd like to use to hold <b>target</b>
  265. * bytes. */
  266. static INLINE size_t
  267. preferred_chunk_size(size_t target)
  268. {
  269. size_t sz = MIN_CHUNK_ALLOC;
  270. while (CHUNK_SIZE_WITH_ALLOC(sz) < target) {
  271. sz <<= 1;
  272. }
  273. return sz;
  274. }
  275. /** Remove from the freelists most chunks that have not been used since the
  276. * last call to buf_shrink_freelists(). Return the amount of memory
  277. * freed. */
  278. size_t
  279. buf_shrink_freelists(int free_all)
  280. {
  281. #ifdef ENABLE_BUF_FREELISTS
  282. int i;
  283. size_t total_freed = 0;
  284. disable_control_logging();
  285. for (i = 0; freelists[i].alloc_size; ++i) {
  286. int slack = freelists[i].slack;
  287. assert_freelist_ok(&freelists[i]);
  288. if (free_all || freelists[i].lowest_length > slack) {
  289. int n_to_free = free_all ? freelists[i].cur_length :
  290. (freelists[i].lowest_length - slack);
  291. int n_to_skip = freelists[i].cur_length - n_to_free;
  292. int orig_length = freelists[i].cur_length;
  293. int orig_n_to_free = n_to_free, n_freed=0;
  294. int orig_n_to_skip = n_to_skip;
  295. int new_length = n_to_skip;
  296. chunk_t **chp = &freelists[i].head;
  297. chunk_t *chunk;
  298. while (n_to_skip) {
  299. if (! (*chp)->next) {
  300. log_warn(LD_BUG, "I wanted to skip %d chunks in the freelist for "
  301. "%d-byte chunks, but only found %d. (Length %d)",
  302. orig_n_to_skip, (int)freelists[i].alloc_size,
  303. orig_n_to_skip-n_to_skip, freelists[i].cur_length);
  304. assert_freelist_ok(&freelists[i]);
  305. goto done;
  306. }
  307. // tor_assert((*chp)->next);
  308. chp = &(*chp)->next;
  309. --n_to_skip;
  310. }
  311. chunk = *chp;
  312. *chp = NULL;
  313. while (chunk) {
  314. chunk_t *next = chunk->next;
  315. #ifdef DEBUG_CHUNK_ALLOC
  316. tor_assert(chunk->DBG_alloc == CHUNK_ALLOC_SIZE(chunk->memlen));
  317. #endif
  318. tor_assert(total_bytes_allocated_in_chunks >=
  319. CHUNK_ALLOC_SIZE(chunk->memlen));
  320. total_bytes_allocated_in_chunks -= CHUNK_ALLOC_SIZE(chunk->memlen);
  321. total_freed += CHUNK_ALLOC_SIZE(chunk->memlen);
  322. tor_free(chunk);
  323. chunk = next;
  324. --n_to_free;
  325. ++n_freed;
  326. ++freelists[i].n_free;
  327. }
  328. if (n_to_free) {
  329. log_warn(LD_BUG, "Freelist length for %d-byte chunks may have been "
  330. "messed up somehow.", (int)freelists[i].alloc_size);
  331. log_warn(LD_BUG, "There were %d chunks at the start. I decided to "
  332. "keep %d. I wanted to free %d. I freed %d. I somehow think "
  333. "I have %d left to free.",
  334. freelists[i].cur_length, n_to_skip, orig_n_to_free,
  335. n_freed, n_to_free);
  336. }
  337. // tor_assert(!n_to_free);
  338. freelists[i].cur_length = new_length;
  339. tor_assert(orig_n_to_skip == new_length);
  340. log_info(LD_MM, "Cleaned freelist for %d-byte chunks: original "
  341. "length %d, kept %d, dropped %d. New length is %d",
  342. (int)freelists[i].alloc_size, orig_length,
  343. orig_n_to_skip, orig_n_to_free, new_length);
  344. }
  345. freelists[i].lowest_length = freelists[i].cur_length;
  346. assert_freelist_ok(&freelists[i]);
  347. }
  348. done:
  349. enable_control_logging();
  350. return total_freed;
  351. #else
  352. (void) free_all;
  353. return 0;
  354. #endif
  355. }
  356. /** Describe the current status of the freelists at log level <b>severity</b>.
  357. */
  358. void
  359. buf_dump_freelist_sizes(int severity)
  360. {
  361. #ifdef ENABLE_BUF_FREELISTS
  362. int i;
  363. tor_log(severity, LD_MM, "====== Buffer freelists:");
  364. for (i = 0; freelists[i].alloc_size; ++i) {
  365. uint64_t total = ((uint64_t)freelists[i].cur_length) *
  366. freelists[i].alloc_size;
  367. tor_log(severity, LD_MM,
  368. U64_FORMAT" bytes in %d %d-byte chunks ["U64_FORMAT
  369. " misses; "U64_FORMAT" frees; "U64_FORMAT" hits]",
  370. U64_PRINTF_ARG(total),
  371. freelists[i].cur_length, (int)freelists[i].alloc_size,
  372. U64_PRINTF_ARG(freelists[i].n_alloc),
  373. U64_PRINTF_ARG(freelists[i].n_free),
  374. U64_PRINTF_ARG(freelists[i].n_hit));
  375. }
  376. tor_log(severity, LD_MM, U64_FORMAT" allocations in non-freelist sizes",
  377. U64_PRINTF_ARG(n_freelist_miss));
  378. #else
  379. (void)severity;
  380. #endif
  381. }
  382. /** Magic value for buf_t.magic, to catch pointer errors. */
  383. #define BUFFER_MAGIC 0xB0FFF312u
  384. /** A resizeable buffer, optimized for reading and writing. */
  385. struct buf_t {
  386. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  387. * BUFFER_MAGIC. */
  388. size_t datalen; /**< How many bytes is this buffer holding right now? */
  389. size_t default_chunk_size; /**< Don't allocate any chunks smaller than
  390. * this for this buffer. */
  391. chunk_t *head; /**< First chunk in the list, or NULL for none. */
  392. chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
  393. };
  394. /** Collapse data from the first N chunks from <b>buf</b> into buf->head,
  395. * growing it as necessary, until buf->head has the first <b>bytes</b> bytes
  396. * of data from the buffer, or until buf->head has all the data in <b>buf</b>.
  397. *
  398. * If <b>nulterminate</b> is true, ensure that there is a 0 byte in
  399. * buf->head->mem right after all the data. */
  400. STATIC void
  401. buf_pullup(buf_t *buf, size_t bytes, int nulterminate)
  402. {
  403. /* XXXX nothing uses nulterminate; remove it. */
  404. chunk_t *dest, *src;
  405. size_t capacity;
  406. if (!buf->head)
  407. return;
  408. check();
  409. if (buf->datalen < bytes)
  410. bytes = buf->datalen;
  411. if (nulterminate) {
  412. capacity = bytes + 1;
  413. if (buf->head->datalen >= bytes && CHUNK_REMAINING_CAPACITY(buf->head)) {
  414. *CHUNK_WRITE_PTR(buf->head) = '\0';
  415. return;
  416. }
  417. } else {
  418. capacity = bytes;
  419. if (buf->head->datalen >= bytes)
  420. return;
  421. }
  422. if (buf->head->memlen >= capacity) {
  423. /* We don't need to grow the first chunk, but we might need to repack it.*/
  424. size_t needed = capacity - buf->head->datalen;
  425. if (CHUNK_REMAINING_CAPACITY(buf->head) < needed)
  426. chunk_repack(buf->head);
  427. tor_assert(CHUNK_REMAINING_CAPACITY(buf->head) >= needed);
  428. } else {
  429. chunk_t *newhead;
  430. size_t newsize;
  431. /* We need to grow the chunk. */
  432. chunk_repack(buf->head);
  433. newsize = CHUNK_SIZE_WITH_ALLOC(preferred_chunk_size(capacity));
  434. newhead = chunk_grow(buf->head, newsize);
  435. tor_assert(newhead->memlen >= capacity);
  436. if (newhead != buf->head) {
  437. if (buf->tail == buf->head)
  438. buf->tail = newhead;
  439. buf->head = newhead;
  440. }
  441. }
  442. dest = buf->head;
  443. while (dest->datalen < bytes) {
  444. size_t n = bytes - dest->datalen;
  445. src = dest->next;
  446. tor_assert(src);
  447. if (n > src->datalen) {
  448. memcpy(CHUNK_WRITE_PTR(dest), src->data, src->datalen);
  449. dest->datalen += src->datalen;
  450. dest->next = src->next;
  451. if (buf->tail == src)
  452. buf->tail = dest;
  453. chunk_free_unchecked(src);
  454. } else {
  455. memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
  456. dest->datalen += n;
  457. src->data += n;
  458. src->datalen -= n;
  459. tor_assert(dest->datalen == bytes);
  460. }
  461. }
  462. if (nulterminate) {
  463. tor_assert(CHUNK_REMAINING_CAPACITY(buf->head));
  464. *CHUNK_WRITE_PTR(buf->head) = '\0';
  465. }
  466. check();
  467. }
  468. #ifdef TOR_UNIT_TESTS
  469. void
  470. buf_get_first_chunk_data(const buf_t *buf, const char **cp, size_t *sz)
  471. {
  472. if (!buf || !buf->head) {
  473. *cp = NULL;
  474. *sz = 0;
  475. } else {
  476. *cp = buf->head->data;
  477. *sz = buf->head->datalen;
  478. }
  479. }
  480. #endif
  481. /** Resize buf so it won't hold extra memory that we haven't been
  482. * using lately.
  483. */
  484. void
  485. buf_shrink(buf_t *buf)
  486. {
  487. (void)buf;
  488. }
  489. /** Remove the first <b>n</b> bytes from buf. */
  490. static INLINE void
  491. buf_remove_from_front(buf_t *buf, size_t n)
  492. {
  493. tor_assert(buf->datalen >= n);
  494. while (n) {
  495. tor_assert(buf->head);
  496. if (buf->head->datalen > n) {
  497. buf->head->datalen -= n;
  498. buf->head->data += n;
  499. buf->datalen -= n;
  500. return;
  501. } else {
  502. chunk_t *victim = buf->head;
  503. n -= victim->datalen;
  504. buf->datalen -= victim->datalen;
  505. buf->head = victim->next;
  506. if (buf->tail == victim)
  507. buf->tail = NULL;
  508. chunk_free_unchecked(victim);
  509. }
  510. }
  511. check();
  512. }
  513. /** Create and return a new buf with default chunk capacity <b>size</b>.
  514. */
  515. buf_t *
  516. buf_new_with_capacity(size_t size)
  517. {
  518. buf_t *b = buf_new();
  519. b->default_chunk_size = preferred_chunk_size(size);
  520. return b;
  521. }
  522. /** Allocate and return a new buffer with default capacity. */
  523. buf_t *
  524. buf_new(void)
  525. {
  526. buf_t *buf = tor_malloc_zero(sizeof(buf_t));
  527. buf->magic = BUFFER_MAGIC;
  528. buf->default_chunk_size = 4096;
  529. return buf;
  530. }
  531. size_t
  532. buf_get_default_chunk_size(const buf_t *buf)
  533. {
  534. return buf->default_chunk_size;
  535. }
  536. /** Remove all data from <b>buf</b>. */
  537. void
  538. buf_clear(buf_t *buf)
  539. {
  540. chunk_t *chunk, *next;
  541. buf->datalen = 0;
  542. for (chunk = buf->head; chunk; chunk = next) {
  543. next = chunk->next;
  544. chunk_free_unchecked(chunk);
  545. }
  546. buf->head = buf->tail = NULL;
  547. }
  548. /** Return the number of bytes stored in <b>buf</b> */
  549. size_t
  550. buf_datalen(const buf_t *buf)
  551. {
  552. return buf->datalen;
  553. }
  554. /** Return the total length of all chunks used in <b>buf</b>. */
  555. size_t
  556. buf_allocation(const buf_t *buf)
  557. {
  558. size_t total = 0;
  559. const chunk_t *chunk;
  560. for (chunk = buf->head; chunk; chunk = chunk->next) {
  561. total += CHUNK_ALLOC_SIZE(chunk->memlen);
  562. }
  563. return total;
  564. }
  565. /** Return the number of bytes that can be added to <b>buf</b> without
  566. * performing any additional allocation. */
  567. size_t
  568. buf_slack(const buf_t *buf)
  569. {
  570. if (!buf->tail)
  571. return 0;
  572. else
  573. return CHUNK_REMAINING_CAPACITY(buf->tail);
  574. }
  575. /** Release storage held by <b>buf</b>. */
  576. void
  577. buf_free(buf_t *buf)
  578. {
  579. if (!buf)
  580. return;
  581. buf_clear(buf);
  582. buf->magic = 0xdeadbeef;
  583. tor_free(buf);
  584. }
  585. /** Return a new copy of <b>in_chunk</b> */
  586. static chunk_t *
  587. chunk_copy(const chunk_t *in_chunk)
  588. {
  589. chunk_t *newch = tor_memdup(in_chunk, CHUNK_ALLOC_SIZE(in_chunk->memlen));
  590. total_bytes_allocated_in_chunks += CHUNK_ALLOC_SIZE(in_chunk->memlen);
  591. #ifdef DEBUG_CHUNK_ALLOC
  592. newch->DBG_alloc = CHUNK_ALLOC_SIZE(in_chunk->memlen);
  593. #endif
  594. newch->next = NULL;
  595. if (in_chunk->data) {
  596. off_t offset = in_chunk->data - in_chunk->mem;
  597. newch->data = newch->mem + offset;
  598. }
  599. return newch;
  600. }
  601. /** Return a new copy of <b>buf</b> */
  602. buf_t *
  603. buf_copy(const buf_t *buf)
  604. {
  605. chunk_t *ch;
  606. buf_t *out = buf_new();
  607. out->default_chunk_size = buf->default_chunk_size;
  608. for (ch = buf->head; ch; ch = ch->next) {
  609. chunk_t *newch = chunk_copy(ch);
  610. if (out->tail) {
  611. out->tail->next = newch;
  612. out->tail = newch;
  613. } else {
  614. out->head = out->tail = newch;
  615. }
  616. }
  617. out->datalen = buf->datalen;
  618. return out;
  619. }
  620. /** Append a new chunk with enough capacity to hold <b>capacity</b> bytes to
  621. * the tail of <b>buf</b>. If <b>capped</b>, don't allocate a chunk bigger
  622. * than MAX_CHUNK_ALLOC. */
  623. static chunk_t *
  624. buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
  625. {
  626. chunk_t *chunk;
  627. struct timeval now;
  628. if (CHUNK_ALLOC_SIZE(capacity) < buf->default_chunk_size) {
  629. chunk = chunk_new_with_alloc_size(buf->default_chunk_size);
  630. } else if (capped && CHUNK_ALLOC_SIZE(capacity) > MAX_CHUNK_ALLOC) {
  631. chunk = chunk_new_with_alloc_size(MAX_CHUNK_ALLOC);
  632. } else {
  633. chunk = chunk_new_with_alloc_size(preferred_chunk_size(capacity));
  634. }
  635. tor_gettimeofday_cached_monotonic(&now);
  636. chunk->inserted_time = (uint32_t)tv_to_msec(&now);
  637. if (buf->tail) {
  638. tor_assert(buf->head);
  639. buf->tail->next = chunk;
  640. buf->tail = chunk;
  641. } else {
  642. tor_assert(!buf->head);
  643. buf->head = buf->tail = chunk;
  644. }
  645. check();
  646. return chunk;
  647. }
  648. /** Return the age of the oldest chunk in the buffer <b>buf</b>, in
  649. * milliseconds. Requires the current time, in truncated milliseconds since
  650. * the epoch, as its input <b>now</b>.
  651. */
  652. uint32_t
  653. buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
  654. {
  655. if (buf->head) {
  656. return now - buf->head->inserted_time;
  657. } else {
  658. return 0;
  659. }
  660. }
  661. size_t
  662. buf_get_total_allocation(void)
  663. {
  664. return total_bytes_allocated_in_chunks;
  665. }
  666. /** Read up to <b>at_most</b> bytes from the socket <b>fd</b> into
  667. * <b>chunk</b> (which must be on <b>buf</b>). If we get an EOF, set
  668. * *<b>reached_eof</b> to 1. Return -1 on error, 0 on eof or blocking,
  669. * and the number of bytes read otherwise. */
  670. static INLINE int
  671. read_to_chunk(buf_t *buf, chunk_t *chunk, tor_socket_t fd, size_t at_most,
  672. int *reached_eof, int *socket_error)
  673. {
  674. ssize_t read_result;
  675. if (at_most > CHUNK_REMAINING_CAPACITY(chunk))
  676. at_most = CHUNK_REMAINING_CAPACITY(chunk);
  677. read_result = tor_socket_recv(fd, CHUNK_WRITE_PTR(chunk), at_most, 0);
  678. if (read_result < 0) {
  679. int e = tor_socket_errno(fd);
  680. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  681. #ifdef _WIN32
  682. if (e == WSAENOBUFS)
  683. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  684. #endif
  685. *socket_error = e;
  686. return -1;
  687. }
  688. return 0; /* would block. */
  689. } else if (read_result == 0) {
  690. log_debug(LD_NET,"Encountered eof on fd %d", (int)fd);
  691. *reached_eof = 1;
  692. return 0;
  693. } else { /* actually got bytes. */
  694. buf->datalen += read_result;
  695. chunk->datalen += read_result;
  696. log_debug(LD_NET,"Read %ld bytes. %d on inbuf.", (long)read_result,
  697. (int)buf->datalen);
  698. tor_assert(read_result < INT_MAX);
  699. return (int)read_result;
  700. }
  701. }
  702. /** As read_to_chunk(), but return (negative) error code on error, blocking,
  703. * or TLS, and the number of bytes read otherwise. */
  704. static INLINE int
  705. read_to_chunk_tls(buf_t *buf, chunk_t *chunk, tor_tls_t *tls,
  706. size_t at_most)
  707. {
  708. int read_result;
  709. tor_assert(CHUNK_REMAINING_CAPACITY(chunk) >= at_most);
  710. read_result = tor_tls_read(tls, CHUNK_WRITE_PTR(chunk), at_most);
  711. if (read_result < 0)
  712. return read_result;
  713. buf->datalen += read_result;
  714. chunk->datalen += read_result;
  715. return read_result;
  716. }
  717. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  718. * <b>at_most</b> bytes, growing the buffer as necessary. If recv() returns 0
  719. * (because of EOF), set *<b>reached_eof</b> to 1 and return 0. Return -1 on
  720. * error; else return the number of bytes read.
  721. */
  722. /* XXXX024 indicate "read blocked" somehow? */
  723. int
  724. read_to_buf(tor_socket_t s, size_t at_most, buf_t *buf, int *reached_eof,
  725. int *socket_error)
  726. {
  727. /* XXXX024 It's stupid to overload the return values for these functions:
  728. * "error status" and "number of bytes read" are not mutually exclusive.
  729. */
  730. int r = 0;
  731. size_t total_read = 0;
  732. check();
  733. tor_assert(reached_eof);
  734. tor_assert(SOCKET_OK(s));
  735. while (at_most > total_read) {
  736. size_t readlen = at_most - total_read;
  737. chunk_t *chunk;
  738. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  739. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  740. if (readlen > chunk->memlen)
  741. readlen = chunk->memlen;
  742. } else {
  743. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  744. chunk = buf->tail;
  745. if (cap < readlen)
  746. readlen = cap;
  747. }
  748. r = read_to_chunk(buf, chunk, s, readlen, reached_eof, socket_error);
  749. check();
  750. if (r < 0)
  751. return r; /* Error */
  752. tor_assert(total_read+r < INT_MAX);
  753. total_read += r;
  754. if ((size_t)r < readlen) { /* eof, block, or no more to read. */
  755. break;
  756. }
  757. }
  758. return (int)total_read;
  759. }
  760. /** As read_to_buf, but reads from a TLS connection, and returns a TLS
  761. * status value rather than the number of bytes read.
  762. *
  763. * Using TLS on OR connections complicates matters in two ways.
  764. *
  765. * First, a TLS stream has its own read buffer independent of the
  766. * connection's read buffer. (TLS needs to read an entire frame from
  767. * the network before it can decrypt any data. Thus, trying to read 1
  768. * byte from TLS can require that several KB be read from the network
  769. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  770. * Because the data hasn't been read by Tor (it's still inside the TLS),
  771. * this means that sometimes a connection "has stuff to read" even when
  772. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  773. * used in connection.c to detect TLS objects with non-empty internal
  774. * buffers and read from them again.
  775. *
  776. * Second, the TLS stream's events do not correspond directly to network
  777. * events: sometimes, before a TLS stream can read, the network must be
  778. * ready to write -- or vice versa.
  779. */
  780. int
  781. read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  782. {
  783. int r = 0;
  784. size_t total_read = 0;
  785. check_no_tls_errors();
  786. check();
  787. while (at_most > total_read) {
  788. size_t readlen = at_most - total_read;
  789. chunk_t *chunk;
  790. if (!buf->tail || CHUNK_REMAINING_CAPACITY(buf->tail) < MIN_READ_LEN) {
  791. chunk = buf_add_chunk_with_capacity(buf, at_most, 1);
  792. if (readlen > chunk->memlen)
  793. readlen = chunk->memlen;
  794. } else {
  795. size_t cap = CHUNK_REMAINING_CAPACITY(buf->tail);
  796. chunk = buf->tail;
  797. if (cap < readlen)
  798. readlen = cap;
  799. }
  800. r = read_to_chunk_tls(buf, chunk, tls, readlen);
  801. check();
  802. if (r < 0)
  803. return r; /* Error */
  804. tor_assert(total_read+r < INT_MAX);
  805. total_read += r;
  806. if ((size_t)r < readlen) /* eof, block, or no more to read. */
  807. break;
  808. }
  809. return (int)total_read;
  810. }
  811. /** Helper for flush_buf(): try to write <b>sz</b> bytes from chunk
  812. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. On success, deduct
  813. * the bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  814. * written on success, 0 on blocking, -1 on failure.
  815. */
  816. static INLINE int
  817. flush_chunk(tor_socket_t s, buf_t *buf, chunk_t *chunk, size_t sz,
  818. size_t *buf_flushlen)
  819. {
  820. ssize_t write_result;
  821. if (sz > chunk->datalen)
  822. sz = chunk->datalen;
  823. write_result = tor_socket_send(s, chunk->data, sz, 0);
  824. if (write_result < 0) {
  825. int e = tor_socket_errno(s);
  826. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  827. #ifdef _WIN32
  828. if (e == WSAENOBUFS)
  829. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  830. #endif
  831. return -1;
  832. }
  833. log_debug(LD_NET,"write() would block, returning.");
  834. return 0;
  835. } else {
  836. *buf_flushlen -= write_result;
  837. buf_remove_from_front(buf, write_result);
  838. tor_assert(write_result < INT_MAX);
  839. return (int)write_result;
  840. }
  841. }
  842. /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes from chunk
  843. * <b>chunk</b> of buffer <b>buf</b> onto socket <b>s</b>. (Tries to write
  844. * more if there is a forced pending write size.) On success, deduct the
  845. * bytes written from *<b>buf_flushlen</b>. Return the number of bytes
  846. * written on success, and a TOR_TLS error code on failure or blocking.
  847. */
  848. static INLINE int
  849. flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
  850. size_t sz, size_t *buf_flushlen)
  851. {
  852. int r;
  853. size_t forced;
  854. char *data;
  855. forced = tor_tls_get_forced_write_size(tls);
  856. if (forced > sz)
  857. sz = forced;
  858. if (chunk) {
  859. data = chunk->data;
  860. tor_assert(sz <= chunk->datalen);
  861. } else {
  862. data = NULL;
  863. tor_assert(sz == 0);
  864. }
  865. r = tor_tls_write(tls, data, sz);
  866. if (r < 0)
  867. return r;
  868. if (*buf_flushlen > (size_t)r)
  869. *buf_flushlen -= r;
  870. else
  871. *buf_flushlen = 0;
  872. buf_remove_from_front(buf, r);
  873. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  874. r,(int)*buf_flushlen,(int)buf->datalen);
  875. return r;
  876. }
  877. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  878. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  879. * the number of bytes actually written, and remove the written bytes
  880. * from the buffer. Return the number of bytes written on success,
  881. * -1 on failure. Return 0 if write() would block.
  882. */
  883. int
  884. flush_buf(tor_socket_t s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  885. {
  886. /* XXXX024 It's stupid to overload the return values for these functions:
  887. * "error status" and "number of bytes flushed" are not mutually exclusive.
  888. */
  889. int r;
  890. size_t flushed = 0;
  891. tor_assert(buf_flushlen);
  892. tor_assert(SOCKET_OK(s));
  893. tor_assert(*buf_flushlen <= buf->datalen);
  894. tor_assert(sz <= *buf_flushlen);
  895. check();
  896. while (sz) {
  897. size_t flushlen0;
  898. tor_assert(buf->head);
  899. if (buf->head->datalen >= sz)
  900. flushlen0 = sz;
  901. else
  902. flushlen0 = buf->head->datalen;
  903. r = flush_chunk(s, buf, buf->head, flushlen0, buf_flushlen);
  904. check();
  905. if (r < 0)
  906. return r;
  907. flushed += r;
  908. sz -= r;
  909. if (r == 0 || (size_t)r < flushlen0) /* can't flush any more now. */
  910. break;
  911. }
  912. tor_assert(flushed < INT_MAX);
  913. return (int)flushed;
  914. }
  915. /** As flush_buf(), but writes data to a TLS connection. Can write more than
  916. * <b>flushlen</b> bytes.
  917. */
  918. int
  919. flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t flushlen,
  920. size_t *buf_flushlen)
  921. {
  922. int r;
  923. size_t flushed = 0;
  924. ssize_t sz;
  925. tor_assert(buf_flushlen);
  926. tor_assert(*buf_flushlen <= buf->datalen);
  927. tor_assert(flushlen <= *buf_flushlen);
  928. sz = (ssize_t) flushlen;
  929. /* we want to let tls write even if flushlen is zero, because it might
  930. * have a partial record pending */
  931. check_no_tls_errors();
  932. check();
  933. do {
  934. size_t flushlen0;
  935. if (buf->head) {
  936. if ((ssize_t)buf->head->datalen >= sz)
  937. flushlen0 = sz;
  938. else
  939. flushlen0 = buf->head->datalen;
  940. } else {
  941. flushlen0 = 0;
  942. }
  943. r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen);
  944. check();
  945. if (r < 0)
  946. return r;
  947. flushed += r;
  948. sz -= r;
  949. if (r == 0) /* Can't flush any more now. */
  950. break;
  951. } while (sz > 0);
  952. tor_assert(flushed < INT_MAX);
  953. return (int)flushed;
  954. }
  955. /** Append <b>string_len</b> bytes from <b>string</b> to the end of
  956. * <b>buf</b>.
  957. *
  958. * Return the new length of the buffer on success, -1 on failure.
  959. */
  960. int
  961. write_to_buf(const char *string, size_t string_len, buf_t *buf)
  962. {
  963. if (!string_len)
  964. return (int)buf->datalen;
  965. check();
  966. while (string_len) {
  967. size_t copy;
  968. if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
  969. buf_add_chunk_with_capacity(buf, string_len, 1);
  970. copy = CHUNK_REMAINING_CAPACITY(buf->tail);
  971. if (copy > string_len)
  972. copy = string_len;
  973. memcpy(CHUNK_WRITE_PTR(buf->tail), string, copy);
  974. string_len -= copy;
  975. string += copy;
  976. buf->datalen += copy;
  977. buf->tail->datalen += copy;
  978. }
  979. check();
  980. tor_assert(buf->datalen < INT_MAX);
  981. return (int)buf->datalen;
  982. }
  983. /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
  984. * onto <b>string</b>.
  985. */
  986. static INLINE void
  987. peek_from_buf(char *string, size_t string_len, const buf_t *buf)
  988. {
  989. chunk_t *chunk;
  990. tor_assert(string);
  991. /* make sure we don't ask for too much */
  992. tor_assert(string_len <= buf->datalen);
  993. /* assert_buf_ok(buf); */
  994. chunk = buf->head;
  995. while (string_len) {
  996. size_t copy = string_len;
  997. tor_assert(chunk);
  998. if (chunk->datalen < copy)
  999. copy = chunk->datalen;
  1000. memcpy(string, chunk->data, copy);
  1001. string_len -= copy;
  1002. string += copy;
  1003. chunk = chunk->next;
  1004. }
  1005. }
  1006. /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
  1007. * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
  1008. * must be \<= the number of bytes on the buffer.
  1009. */
  1010. int
  1011. fetch_from_buf(char *string, size_t string_len, buf_t *buf)
  1012. {
  1013. /* There must be string_len bytes in buf; write them onto string,
  1014. * then memmove buf back (that is, remove them from buf).
  1015. *
  1016. * Return the number of bytes still on the buffer. */
  1017. check();
  1018. peek_from_buf(string, string_len, buf);
  1019. buf_remove_from_front(buf, string_len);
  1020. check();
  1021. tor_assert(buf->datalen < INT_MAX);
  1022. return (int)buf->datalen;
  1023. }
  1024. /** True iff the cell command <b>command</b> is one that implies a
  1025. * variable-length cell in Tor link protocol <b>linkproto</b>. */
  1026. static INLINE int
  1027. cell_command_is_var_length(uint8_t command, int linkproto)
  1028. {
  1029. /* If linkproto is v2 (2), CELL_VERSIONS is the only variable-length cells
  1030. * work as implemented here. If it's 1, there are no variable-length cells.
  1031. * Tor does not support other versions right now, and so can't negotiate
  1032. * them.
  1033. */
  1034. switch (linkproto) {
  1035. case 1:
  1036. /* Link protocol version 1 has no variable-length cells. */
  1037. return 0;
  1038. case 2:
  1039. /* In link protocol version 2, VERSIONS is the only variable-length cell */
  1040. return command == CELL_VERSIONS;
  1041. case 0:
  1042. case 3:
  1043. default:
  1044. /* In link protocol version 3 and later, and in version "unknown",
  1045. * commands 128 and higher indicate variable-length. VERSIONS is
  1046. * grandfathered in. */
  1047. return command == CELL_VERSIONS || command >= 128;
  1048. }
  1049. }
  1050. /** Check <b>buf</b> for a variable-length cell according to the rules of link
  1051. * protocol version <b>linkproto</b>. If one is found, pull it off the buffer
  1052. * and assign a newly allocated var_cell_t to *<b>out</b>, and return 1.
  1053. * Return 0 if whatever is on the start of buf_t is not a variable-length
  1054. * cell. Return 1 and set *<b>out</b> to NULL if there seems to be the start
  1055. * of a variable-length cell on <b>buf</b>, but the whole thing isn't there
  1056. * yet. */
  1057. int
  1058. fetch_var_cell_from_buf(buf_t *buf, var_cell_t **out, int linkproto)
  1059. {
  1060. char hdr[VAR_CELL_MAX_HEADER_SIZE];
  1061. var_cell_t *result;
  1062. uint8_t command;
  1063. uint16_t length;
  1064. const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
  1065. const int circ_id_len = get_circ_id_size(wide_circ_ids);
  1066. const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
  1067. check();
  1068. *out = NULL;
  1069. if (buf->datalen < header_len)
  1070. return 0;
  1071. peek_from_buf(hdr, header_len, buf);
  1072. command = get_uint8(hdr + circ_id_len);
  1073. if (!(cell_command_is_var_length(command, linkproto)))
  1074. return 0;
  1075. length = ntohs(get_uint16(hdr + circ_id_len + 1));
  1076. if (buf->datalen < (size_t)(header_len+length))
  1077. return 1;
  1078. result = var_cell_new(length);
  1079. result->command = command;
  1080. if (wide_circ_ids)
  1081. result->circ_id = ntohl(get_uint32(hdr));
  1082. else
  1083. result->circ_id = ntohs(get_uint16(hdr));
  1084. buf_remove_from_front(buf, header_len);
  1085. peek_from_buf((char*) result->payload, length, buf);
  1086. buf_remove_from_front(buf, length);
  1087. check();
  1088. *out = result;
  1089. return 1;
  1090. }
  1091. #ifdef USE_BUFFEREVENTS
  1092. /** Try to read <b>n</b> bytes from <b>buf</b> at <b>pos</b> (which may be
  1093. * NULL for the start of the buffer), copying the data only if necessary. Set
  1094. * *<b>data_out</b> to a pointer to the desired bytes. Set <b>free_out</b>
  1095. * to 1 if we needed to malloc *<b>data</b> because the original bytes were
  1096. * noncontiguous; 0 otherwise. Return the number of bytes actually available
  1097. * at *<b>data_out</b>.
  1098. */
  1099. static ssize_t
  1100. inspect_evbuffer(struct evbuffer *buf, char **data_out, size_t n,
  1101. int *free_out, struct evbuffer_ptr *pos)
  1102. {
  1103. int n_vecs, i;
  1104. if (evbuffer_get_length(buf) < n)
  1105. n = evbuffer_get_length(buf);
  1106. if (n == 0)
  1107. return 0;
  1108. n_vecs = evbuffer_peek(buf, n, pos, NULL, 0);
  1109. tor_assert(n_vecs > 0);
  1110. if (n_vecs == 1) {
  1111. struct evbuffer_iovec v;
  1112. i = evbuffer_peek(buf, n, pos, &v, 1);
  1113. tor_assert(i == 1);
  1114. *data_out = v.iov_base;
  1115. *free_out = 0;
  1116. return v.iov_len;
  1117. } else {
  1118. ev_ssize_t copied;
  1119. *data_out = tor_malloc(n);
  1120. *free_out = 1;
  1121. copied = evbuffer_copyout(buf, *data_out, n);
  1122. tor_assert(copied >= 0 && (size_t)copied == n);
  1123. return copied;
  1124. }
  1125. }
  1126. /** As fetch_var_cell_from_buf, buf works on an evbuffer. */
  1127. int
  1128. fetch_var_cell_from_evbuffer(struct evbuffer *buf, var_cell_t **out,
  1129. int linkproto)
  1130. {
  1131. char *hdr = NULL;
  1132. int free_hdr = 0;
  1133. size_t n;
  1134. size_t buf_len;
  1135. uint8_t command;
  1136. uint16_t cell_length;
  1137. var_cell_t *cell;
  1138. int result = 0;
  1139. const int wide_circ_ids = linkproto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
  1140. const int circ_id_len = get_circ_id_size(wide_circ_ids);
  1141. const unsigned header_len = get_var_cell_header_size(wide_circ_ids);
  1142. *out = NULL;
  1143. buf_len = evbuffer_get_length(buf);
  1144. if (buf_len < header_len)
  1145. return 0;
  1146. n = inspect_evbuffer(buf, &hdr, header_len, &free_hdr, NULL);
  1147. tor_assert(n >= header_len);
  1148. command = get_uint8(hdr + circ_id_len);
  1149. if (!(cell_command_is_var_length(command, linkproto))) {
  1150. goto done;
  1151. }
  1152. cell_length = ntohs(get_uint16(hdr + circ_id_len + 1));
  1153. if (buf_len < (size_t)(header_len+cell_length)) {
  1154. result = 1; /* Not all here yet. */
  1155. goto done;
  1156. }
  1157. cell = var_cell_new(cell_length);
  1158. cell->command = command;
  1159. if (wide_circ_ids)
  1160. cell->circ_id = ntohl(get_uint32(hdr));
  1161. else
  1162. cell->circ_id = ntohs(get_uint16(hdr));
  1163. evbuffer_drain(buf, header_len);
  1164. evbuffer_remove(buf, cell->payload, cell_length);
  1165. *out = cell;
  1166. result = 1;
  1167. done:
  1168. if (free_hdr && hdr)
  1169. tor_free(hdr);
  1170. return result;
  1171. }
  1172. #endif
  1173. /** Move up to *<b>buf_flushlen</b> bytes from <b>buf_in</b> to
  1174. * <b>buf_out</b>, and modify *<b>buf_flushlen</b> appropriately.
  1175. * Return the number of bytes actually copied.
  1176. */
  1177. int
  1178. move_buf_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
  1179. {
  1180. /* We can do way better here, but this doesn't turn up in any profiles. */
  1181. char b[4096];
  1182. size_t cp, len;
  1183. len = *buf_flushlen;
  1184. if (len > buf_in->datalen)
  1185. len = buf_in->datalen;
  1186. cp = len; /* Remember the number of bytes we intend to copy. */
  1187. tor_assert(cp < INT_MAX);
  1188. while (len) {
  1189. /* This isn't the most efficient implementation one could imagine, since
  1190. * it does two copies instead of 1, but I kinda doubt that this will be
  1191. * critical path. */
  1192. size_t n = len > sizeof(b) ? sizeof(b) : len;
  1193. fetch_from_buf(b, n, buf_in);
  1194. write_to_buf(b, n, buf_out);
  1195. len -= n;
  1196. }
  1197. *buf_flushlen -= cp;
  1198. return (int)cp;
  1199. }
  1200. /** Internal structure: represents a position in a buffer. */
  1201. typedef struct buf_pos_t {
  1202. const chunk_t *chunk; /**< Which chunk are we pointing to? */
  1203. int pos;/**< Which character inside the chunk's data are we pointing to? */
  1204. size_t chunk_pos; /**< Total length of all previous chunks. */
  1205. } buf_pos_t;
  1206. /** Initialize <b>out</b> to point to the first character of <b>buf</b>.*/
  1207. static void
  1208. buf_pos_init(const buf_t *buf, buf_pos_t *out)
  1209. {
  1210. out->chunk = buf->head;
  1211. out->pos = 0;
  1212. out->chunk_pos = 0;
  1213. }
  1214. /** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
  1215. * position of <b>out</b>, or later. Return -1 if no instances are found;
  1216. * otherwise returns the absolute position of the character. */
  1217. static off_t
  1218. buf_find_pos_of_char(char ch, buf_pos_t *out)
  1219. {
  1220. const chunk_t *chunk;
  1221. int pos;
  1222. tor_assert(out);
  1223. if (out->chunk) {
  1224. if (out->chunk->datalen) {
  1225. tor_assert(out->pos < (off_t)out->chunk->datalen);
  1226. } else {
  1227. tor_assert(out->pos == 0);
  1228. }
  1229. }
  1230. pos = out->pos;
  1231. for (chunk = out->chunk; chunk; chunk = chunk->next) {
  1232. char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
  1233. if (cp) {
  1234. out->chunk = chunk;
  1235. tor_assert(cp - chunk->data < INT_MAX);
  1236. out->pos = (int)(cp - chunk->data);
  1237. return out->chunk_pos + out->pos;
  1238. } else {
  1239. out->chunk_pos += chunk->datalen;
  1240. pos = 0;
  1241. }
  1242. }
  1243. return -1;
  1244. }
  1245. /** Advance <b>pos</b> by a single character, if there are any more characters
  1246. * in the buffer. Returns 0 on success, -1 on failure. */
  1247. static INLINE int
  1248. buf_pos_inc(buf_pos_t *pos)
  1249. {
  1250. ++pos->pos;
  1251. if (pos->pos == (off_t)pos->chunk->datalen) {
  1252. if (!pos->chunk->next)
  1253. return -1;
  1254. pos->chunk_pos += pos->chunk->datalen;
  1255. pos->chunk = pos->chunk->next;
  1256. pos->pos = 0;
  1257. }
  1258. return 0;
  1259. }
  1260. /** Return true iff the <b>n</b>-character string in <b>s</b> appears
  1261. * (verbatim) at <b>pos</b>. */
  1262. static int
  1263. buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
  1264. {
  1265. buf_pos_t p;
  1266. if (!n)
  1267. return 1;
  1268. memcpy(&p, pos, sizeof(p));
  1269. while (1) {
  1270. char ch = p.chunk->data[p.pos];
  1271. if (ch != *s)
  1272. return 0;
  1273. ++s;
  1274. /* If we're out of characters that don't match, we match. Check this
  1275. * _before_ we test incrementing pos, in case we're at the end of the
  1276. * string. */
  1277. if (--n == 0)
  1278. return 1;
  1279. if (buf_pos_inc(&p)<0)
  1280. return 0;
  1281. }
  1282. }
  1283. /** Return the first position in <b>buf</b> at which the <b>n</b>-character
  1284. * string <b>s</b> occurs, or -1 if it does not occur. */
  1285. STATIC int
  1286. buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
  1287. {
  1288. buf_pos_t pos;
  1289. buf_pos_init(buf, &pos);
  1290. while (buf_find_pos_of_char(*s, &pos) >= 0) {
  1291. if (buf_matches_at_pos(&pos, s, n)) {
  1292. tor_assert(pos.chunk_pos + pos.pos < INT_MAX);
  1293. return (int)(pos.chunk_pos + pos.pos);
  1294. } else {
  1295. if (buf_pos_inc(&pos)<0)
  1296. return -1;
  1297. }
  1298. }
  1299. return -1;
  1300. }
  1301. /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
  1302. * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain NULs.)
  1303. * If a) the headers include a Content-Length field and all bytes in
  1304. * the body are present, or b) there's no Content-Length field and
  1305. * all headers are present, then:
  1306. *
  1307. * - strdup headers into <b>*headers_out</b>, and NUL-terminate it.
  1308. * - memdup body into <b>*body_out</b>, and NUL-terminate it.
  1309. * - Then remove them from <b>buf</b>, and return 1.
  1310. *
  1311. * - If headers or body is NULL, discard that part of the buf.
  1312. * - If a headers or body doesn't fit in the arg, return -1.
  1313. * (We ensure that the headers or body don't exceed max len,
  1314. * _even if_ we're planning to discard them.)
  1315. * - If force_complete is true, then succeed even if not all of the
  1316. * content has arrived.
  1317. *
  1318. * Else, change nothing and return 0.
  1319. */
  1320. int
  1321. fetch_from_buf_http(buf_t *buf,
  1322. char **headers_out, size_t max_headerlen,
  1323. char **body_out, size_t *body_used, size_t max_bodylen,
  1324. int force_complete)
  1325. {
  1326. char *headers, *p;
  1327. size_t headerlen, bodylen, contentlen;
  1328. int crlf_offset;
  1329. check();
  1330. if (!buf->head)
  1331. return 0;
  1332. crlf_offset = buf_find_string_offset(buf, "\r\n\r\n", 4);
  1333. if (crlf_offset > (int)max_headerlen ||
  1334. (crlf_offset < 0 && buf->datalen > max_headerlen)) {
  1335. log_debug(LD_HTTP,"headers too long.");
  1336. return -1;
  1337. } else if (crlf_offset < 0) {
  1338. log_debug(LD_HTTP,"headers not all here yet.");
  1339. return 0;
  1340. }
  1341. /* Okay, we have a full header. Make sure it all appears in the first
  1342. * chunk. */
  1343. if ((int)buf->head->datalen < crlf_offset + 4)
  1344. buf_pullup(buf, crlf_offset+4, 0);
  1345. headerlen = crlf_offset + 4;
  1346. headers = buf->head->data;
  1347. bodylen = buf->datalen - headerlen;
  1348. log_debug(LD_HTTP,"headerlen %d, bodylen %d.", (int)headerlen, (int)bodylen);
  1349. if (max_headerlen <= headerlen) {
  1350. log_warn(LD_HTTP,"headerlen %d larger than %d. Failing.",
  1351. (int)headerlen, (int)max_headerlen-1);
  1352. return -1;
  1353. }
  1354. if (max_bodylen <= bodylen) {
  1355. log_warn(LD_HTTP,"bodylen %d larger than %d. Failing.",
  1356. (int)bodylen, (int)max_bodylen-1);
  1357. return -1;
  1358. }
  1359. #define CONTENT_LENGTH "\r\nContent-Length: "
  1360. p = (char*) tor_memstr(headers, headerlen, CONTENT_LENGTH);
  1361. if (p) {
  1362. int i;
  1363. i = atoi(p+strlen(CONTENT_LENGTH));
  1364. if (i < 0) {
  1365. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  1366. "someone is trying to crash us.");
  1367. return -1;
  1368. }
  1369. contentlen = i;
  1370. /* if content-length is malformed, then our body length is 0. fine. */
  1371. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  1372. if (bodylen < contentlen) {
  1373. if (!force_complete) {
  1374. log_debug(LD_HTTP,"body not all here yet.");
  1375. return 0; /* not all there yet */
  1376. }
  1377. }
  1378. if (bodylen > contentlen) {
  1379. bodylen = contentlen;
  1380. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  1381. }
  1382. }
  1383. /* all happy. copy into the appropriate places, and return 1 */
  1384. if (headers_out) {
  1385. *headers_out = tor_malloc(headerlen+1);
  1386. fetch_from_buf(*headers_out, headerlen, buf);
  1387. (*headers_out)[headerlen] = 0; /* NUL terminate it */
  1388. }
  1389. if (body_out) {
  1390. tor_assert(body_used);
  1391. *body_used = bodylen;
  1392. *body_out = tor_malloc(bodylen+1);
  1393. fetch_from_buf(*body_out, bodylen, buf);
  1394. (*body_out)[bodylen] = 0; /* NUL terminate it */
  1395. }
  1396. check();
  1397. return 1;
  1398. }
  1399. #ifdef USE_BUFFEREVENTS
  1400. /** As fetch_from_buf_http, buf works on an evbuffer. */
  1401. int
  1402. fetch_from_evbuffer_http(struct evbuffer *buf,
  1403. char **headers_out, size_t max_headerlen,
  1404. char **body_out, size_t *body_used, size_t max_bodylen,
  1405. int force_complete)
  1406. {
  1407. struct evbuffer_ptr crlf, content_length;
  1408. size_t headerlen, bodylen, contentlen;
  1409. /* Find the first \r\n\r\n in the buffer */
  1410. crlf = evbuffer_search(buf, "\r\n\r\n", 4, NULL);
  1411. if (crlf.pos < 0) {
  1412. /* We didn't find one. */
  1413. if (evbuffer_get_length(buf) > max_headerlen)
  1414. return -1; /* Headers too long. */
  1415. return 0; /* Headers not here yet. */
  1416. } else if (crlf.pos > (int)max_headerlen) {
  1417. return -1; /* Headers too long. */
  1418. }
  1419. headerlen = crlf.pos + 4; /* Skip over the \r\n\r\n */
  1420. bodylen = evbuffer_get_length(buf) - headerlen;
  1421. if (bodylen > max_bodylen)
  1422. return -1; /* body too long */
  1423. /* Look for the first occurrence of CONTENT_LENGTH insize buf before the
  1424. * crlfcrlf */
  1425. content_length = evbuffer_search_range(buf, CONTENT_LENGTH,
  1426. strlen(CONTENT_LENGTH), NULL, &crlf);
  1427. if (content_length.pos >= 0) {
  1428. /* We found a content_length: parse it and figure out if the body is here
  1429. * yet. */
  1430. struct evbuffer_ptr eol;
  1431. char *data = NULL;
  1432. int free_data = 0;
  1433. int n, i;
  1434. n = evbuffer_ptr_set(buf, &content_length, strlen(CONTENT_LENGTH),
  1435. EVBUFFER_PTR_ADD);
  1436. tor_assert(n == 0);
  1437. eol = evbuffer_search_eol(buf, &content_length, NULL, EVBUFFER_EOL_CRLF);
  1438. tor_assert(eol.pos > content_length.pos);
  1439. tor_assert(eol.pos <= crlf.pos);
  1440. inspect_evbuffer(buf, &data, eol.pos - content_length.pos, &free_data,
  1441. &content_length);
  1442. i = atoi(data);
  1443. if (free_data)
  1444. tor_free(data);
  1445. if (i < 0) {
  1446. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  1447. "someone is trying to crash us.");
  1448. return -1;
  1449. }
  1450. contentlen = i;
  1451. /* if content-length is malformed, then our body length is 0. fine. */
  1452. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  1453. if (bodylen < contentlen) {
  1454. if (!force_complete) {
  1455. log_debug(LD_HTTP,"body not all here yet.");
  1456. return 0; /* not all there yet */
  1457. }
  1458. }
  1459. if (bodylen > contentlen) {
  1460. bodylen = contentlen;
  1461. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  1462. }
  1463. }
  1464. if (headers_out) {
  1465. *headers_out = tor_malloc(headerlen+1);
  1466. evbuffer_remove(buf, *headers_out, headerlen);
  1467. (*headers_out)[headerlen] = '\0';
  1468. }
  1469. if (body_out) {
  1470. tor_assert(headers_out);
  1471. tor_assert(body_used);
  1472. *body_used = bodylen;
  1473. *body_out = tor_malloc(bodylen+1);
  1474. evbuffer_remove(buf, *body_out, bodylen);
  1475. (*body_out)[bodylen] = '\0';
  1476. }
  1477. return 1;
  1478. }
  1479. #endif
  1480. /**
  1481. * Wait this many seconds before warning the user about using SOCKS unsafely
  1482. * again (requires that WarnUnsafeSocks is turned on). */
  1483. #define SOCKS_WARN_INTERVAL 5
  1484. /** Warn that the user application has made an unsafe socks request using
  1485. * protocol <b>socks_protocol</b> on port <b>port</b>. Don't warn more than
  1486. * once per SOCKS_WARN_INTERVAL, unless <b>safe_socks</b> is set. */
  1487. static void
  1488. log_unsafe_socks_warning(int socks_protocol, const char *address,
  1489. uint16_t port, int safe_socks)
  1490. {
  1491. static ratelim_t socks_ratelim = RATELIM_INIT(SOCKS_WARN_INTERVAL);
  1492. const or_options_t *options = get_options();
  1493. if (! options->WarnUnsafeSocks)
  1494. return;
  1495. if (safe_socks) {
  1496. log_fn_ratelim(&socks_ratelim, LOG_WARN, LD_APP,
  1497. "Your application (using socks%d to port %d) is giving "
  1498. "Tor only an IP address. Applications that do DNS resolves "
  1499. "themselves may leak information. Consider using Socks4A "
  1500. "(e.g. via privoxy or socat) instead. For more information, "
  1501. "please see https://wiki.torproject.org/TheOnionRouter/"
  1502. "TorFAQ#SOCKSAndDNS.%s",
  1503. socks_protocol,
  1504. (int)port,
  1505. safe_socks ? " Rejecting." : "");
  1506. }
  1507. control_event_client_status(LOG_WARN,
  1508. "DANGEROUS_SOCKS PROTOCOL=SOCKS%d ADDRESS=%s:%d",
  1509. socks_protocol, address, (int)port);
  1510. }
  1511. /** Do not attempt to parse socks messages longer than this. This value is
  1512. * actually significantly higher than the longest possible socks message. */
  1513. #define MAX_SOCKS_MESSAGE_LEN 512
  1514. /** Return a new socks_request_t. */
  1515. socks_request_t *
  1516. socks_request_new(void)
  1517. {
  1518. return tor_malloc_zero(sizeof(socks_request_t));
  1519. }
  1520. /** Free all storage held in the socks_request_t <b>req</b>. */
  1521. void
  1522. socks_request_free(socks_request_t *req)
  1523. {
  1524. if (!req)
  1525. return;
  1526. if (req->username) {
  1527. memwipe(req->username, 0x10, req->usernamelen);
  1528. tor_free(req->username);
  1529. }
  1530. if (req->password) {
  1531. memwipe(req->password, 0x04, req->passwordlen);
  1532. tor_free(req->password);
  1533. }
  1534. memwipe(req, 0xCC, sizeof(socks_request_t));
  1535. tor_free(req);
  1536. }
  1537. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  1538. * of the forms
  1539. * - socks4: "socksheader username\\0"
  1540. * - socks4a: "socksheader username\\0 destaddr\\0"
  1541. * - socks5 phase one: "version #methods methods"
  1542. * - socks5 phase two: "version command 0 addresstype..."
  1543. * If it's a complete and valid handshake, and destaddr fits in
  1544. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  1545. * assign to <b>req</b>, and return 1.
  1546. *
  1547. * If it's invalid or too big, return -1.
  1548. *
  1549. * Else it's not all there yet, leave buf alone and return 0.
  1550. *
  1551. * If you want to specify the socks reply, write it into <b>req->reply</b>
  1552. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  1553. *
  1554. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  1555. * the connection is possibly leaking DNS requests locally or not.
  1556. *
  1557. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  1558. *
  1559. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  1560. * undefined.
  1561. */
  1562. int
  1563. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  1564. int log_sockstype, int safe_socks)
  1565. {
  1566. int res;
  1567. ssize_t n_drain;
  1568. size_t want_length = 128;
  1569. if (buf->datalen < 2) /* version and another byte */
  1570. return 0;
  1571. do {
  1572. n_drain = 0;
  1573. buf_pullup(buf, want_length, 0);
  1574. tor_assert(buf->head && buf->head->datalen >= 2);
  1575. want_length = 0;
  1576. res = parse_socks(buf->head->data, buf->head->datalen, req, log_sockstype,
  1577. safe_socks, &n_drain, &want_length);
  1578. if (n_drain < 0)
  1579. buf_clear(buf);
  1580. else if (n_drain > 0)
  1581. buf_remove_from_front(buf, n_drain);
  1582. } while (res == 0 && buf->head && want_length < buf->datalen &&
  1583. buf->datalen >= 2);
  1584. return res;
  1585. }
  1586. #ifdef USE_BUFFEREVENTS
  1587. /* As fetch_from_buf_socks(), but targets an evbuffer instead. */
  1588. int
  1589. fetch_from_evbuffer_socks(struct evbuffer *buf, socks_request_t *req,
  1590. int log_sockstype, int safe_socks)
  1591. {
  1592. char *data;
  1593. ssize_t n_drain;
  1594. size_t datalen, buflen, want_length;
  1595. int res;
  1596. buflen = evbuffer_get_length(buf);
  1597. if (buflen < 2)
  1598. return 0;
  1599. {
  1600. /* See if we can find the socks request in the first chunk of the buffer.
  1601. */
  1602. struct evbuffer_iovec v;
  1603. int i;
  1604. n_drain = 0;
  1605. i = evbuffer_peek(buf, -1, NULL, &v, 1);
  1606. tor_assert(i == 1);
  1607. data = v.iov_base;
  1608. datalen = v.iov_len;
  1609. want_length = 0;
  1610. res = parse_socks(data, datalen, req, log_sockstype,
  1611. safe_socks, &n_drain, &want_length);
  1612. if (n_drain < 0)
  1613. evbuffer_drain(buf, evbuffer_get_length(buf));
  1614. else if (n_drain > 0)
  1615. evbuffer_drain(buf, n_drain);
  1616. if (res)
  1617. return res;
  1618. }
  1619. /* Okay, the first chunk of the buffer didn't have a complete socks request.
  1620. * That means that either we don't have a whole socks request at all, or
  1621. * it's gotten split up. We're going to try passing parse_socks() bigger
  1622. * and bigger chunks until either it says "Okay, I got it", or it says it
  1623. * will need more data than we currently have. */
  1624. /* Loop while we have more data that we haven't given parse_socks() yet. */
  1625. do {
  1626. int free_data = 0;
  1627. const size_t last_wanted = want_length;
  1628. n_drain = 0;
  1629. data = NULL;
  1630. datalen = inspect_evbuffer(buf, &data, want_length, &free_data, NULL);
  1631. want_length = 0;
  1632. res = parse_socks(data, datalen, req, log_sockstype,
  1633. safe_socks, &n_drain, &want_length);
  1634. if (free_data)
  1635. tor_free(data);
  1636. if (n_drain < 0)
  1637. evbuffer_drain(buf, evbuffer_get_length(buf));
  1638. else if (n_drain > 0)
  1639. evbuffer_drain(buf, n_drain);
  1640. if (res == 0 && n_drain == 0 && want_length <= last_wanted) {
  1641. /* If we drained nothing, and we didn't ask for more than last time,
  1642. * then we probably wanted more data than the buffer actually had,
  1643. * and we're finding out that we're not satisified with it. It's
  1644. * time to break until we have more data. */
  1645. break;
  1646. }
  1647. buflen = evbuffer_get_length(buf);
  1648. } while (res == 0 && want_length <= buflen && buflen >= 2);
  1649. return res;
  1650. }
  1651. #endif
  1652. /** The size of the header of an Extended ORPort message: 2 bytes for
  1653. * COMMAND, 2 bytes for BODYLEN */
  1654. #define EXT_OR_CMD_HEADER_SIZE 4
  1655. /** Read <b>buf</b>, which should contain an Extended ORPort message
  1656. * from a transport proxy. If well-formed, create and populate
  1657. * <b>out</b> with the Extended ORport message. Return 0 if the
  1658. * buffer was incomplete, 1 if it was well-formed and -1 if we
  1659. * encountered an error while parsing it. */
  1660. int
  1661. fetch_ext_or_command_from_buf(buf_t *buf, ext_or_cmd_t **out)
  1662. {
  1663. char hdr[EXT_OR_CMD_HEADER_SIZE];
  1664. uint16_t len;
  1665. check();
  1666. if (buf->datalen < EXT_OR_CMD_HEADER_SIZE)
  1667. return 0;
  1668. peek_from_buf(hdr, sizeof(hdr), buf);
  1669. len = ntohs(get_uint16(hdr+2));
  1670. if (buf->datalen < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
  1671. return 0;
  1672. *out = ext_or_cmd_new(len);
  1673. (*out)->cmd = ntohs(get_uint16(hdr));
  1674. (*out)->len = len;
  1675. buf_remove_from_front(buf, EXT_OR_CMD_HEADER_SIZE);
  1676. fetch_from_buf((*out)->body, len, buf);
  1677. return 1;
  1678. }
  1679. #ifdef USE_BUFFEREVENTS
  1680. /** Read <b>buf</b>, which should contain an Extended ORPort message
  1681. * from a transport proxy. If well-formed, create and populate
  1682. * <b>out</b> with the Extended ORport message. Return 0 if the
  1683. * buffer was incomplete, 1 if it was well-formed and -1 if we
  1684. * encountered an error while parsing it. */
  1685. int
  1686. fetch_ext_or_command_from_evbuffer(struct evbuffer *buf, ext_or_cmd_t **out)
  1687. {
  1688. char hdr[EXT_OR_CMD_HEADER_SIZE];
  1689. uint16_t len;
  1690. size_t buf_len = evbuffer_get_length(buf);
  1691. if (buf_len < EXT_OR_CMD_HEADER_SIZE)
  1692. return 0;
  1693. evbuffer_copyout(buf, hdr, EXT_OR_CMD_HEADER_SIZE);
  1694. len = ntohs(get_uint16(hdr+2));
  1695. if (buf_len < (unsigned)len + EXT_OR_CMD_HEADER_SIZE)
  1696. return 0;
  1697. *out = ext_or_cmd_new(len);
  1698. (*out)->cmd = ntohs(get_uint16(hdr));
  1699. (*out)->len = len;
  1700. evbuffer_drain(buf, EXT_OR_CMD_HEADER_SIZE);
  1701. evbuffer_remove(buf, (*out)->body, len);
  1702. return 1;
  1703. }
  1704. #endif
  1705. /** Implementation helper to implement fetch_from_*_socks. Instead of looking
  1706. * at a buffer's contents, we look at the <b>datalen</b> bytes of data in
  1707. * <b>data</b>. Instead of removing data from the buffer, we set
  1708. * <b>drain_out</b> to the amount of data that should be removed (or -1 if the
  1709. * buffer should be cleared). Instead of pulling more data into the first
  1710. * chunk of the buffer, we set *<b>want_length_out</b> to the number of bytes
  1711. * we'd like to see in the input buffer, if they're available. */
  1712. static int
  1713. parse_socks(const char *data, size_t datalen, socks_request_t *req,
  1714. int log_sockstype, int safe_socks, ssize_t *drain_out,
  1715. size_t *want_length_out)
  1716. {
  1717. unsigned int len;
  1718. char tmpbuf[TOR_ADDR_BUF_LEN+1];
  1719. tor_addr_t destaddr;
  1720. uint32_t destip;
  1721. uint8_t socksver;
  1722. char *next, *startaddr;
  1723. unsigned char usernamelen, passlen;
  1724. struct in_addr in;
  1725. if (datalen < 2) {
  1726. /* We always need at least 2 bytes. */
  1727. *want_length_out = 2;
  1728. return 0;
  1729. }
  1730. if (req->socks_version == 5 && !req->got_auth) {
  1731. /* See if we have received authentication. Strictly speaking, we should
  1732. also check whether we actually negotiated username/password
  1733. authentication. But some broken clients will send us authentication
  1734. even if we negotiated SOCKS_NO_AUTH. */
  1735. if (*data == 1) { /* username/pass version 1 */
  1736. /* Format is: authversion [1 byte] == 1
  1737. usernamelen [1 byte]
  1738. username [usernamelen bytes]
  1739. passlen [1 byte]
  1740. password [passlen bytes] */
  1741. usernamelen = (unsigned char)*(data + 1);
  1742. if (datalen < 2u + usernamelen + 1u) {
  1743. *want_length_out = 2u + usernamelen + 1u;
  1744. return 0;
  1745. }
  1746. passlen = (unsigned char)*(data + 2u + usernamelen);
  1747. if (datalen < 2u + usernamelen + 1u + passlen) {
  1748. *want_length_out = 2u + usernamelen + 1u + passlen;
  1749. return 0;
  1750. }
  1751. req->replylen = 2; /* 2 bytes of response */
  1752. req->reply[0] = 1; /* authversion == 1 */
  1753. req->reply[1] = 0; /* authentication successful */
  1754. log_debug(LD_APP,
  1755. "socks5: Accepted username/password without checking.");
  1756. if (usernamelen) {
  1757. req->username = tor_memdup(data+2u, usernamelen);
  1758. req->usernamelen = usernamelen;
  1759. }
  1760. if (passlen) {
  1761. req->password = tor_memdup(data+3u+usernamelen, passlen);
  1762. req->passwordlen = passlen;
  1763. }
  1764. *drain_out = 2u + usernamelen + 1u + passlen;
  1765. req->got_auth = 1;
  1766. *want_length_out = 7; /* Minimal socks5 sommand. */
  1767. return 0;
  1768. } else if (req->auth_type == SOCKS_USER_PASS) {
  1769. /* unknown version byte */
  1770. log_warn(LD_APP, "Socks5 username/password version %d not recognized; "
  1771. "rejecting.", (int)*data);
  1772. return -1;
  1773. }
  1774. }
  1775. socksver = *data;
  1776. switch (socksver) { /* which version of socks? */
  1777. case 5: /* socks5 */
  1778. if (req->socks_version != 5) { /* we need to negotiate a method */
  1779. unsigned char nummethods = (unsigned char)*(data+1);
  1780. int have_user_pass, have_no_auth;
  1781. int r=0;
  1782. tor_assert(!req->socks_version);
  1783. if (datalen < 2u+nummethods) {
  1784. *want_length_out = 2u+nummethods;
  1785. return 0;
  1786. }
  1787. if (!nummethods)
  1788. return -1;
  1789. req->replylen = 2; /* 2 bytes of response */
  1790. req->reply[0] = 5; /* socks5 reply */
  1791. have_user_pass = (memchr(data+2, SOCKS_USER_PASS, nummethods) !=NULL);
  1792. have_no_auth = (memchr(data+2, SOCKS_NO_AUTH, nummethods) !=NULL);
  1793. if (have_user_pass && !(have_no_auth && req->socks_prefer_no_auth)) {
  1794. req->auth_type = SOCKS_USER_PASS;
  1795. req->reply[1] = SOCKS_USER_PASS; /* tell client to use "user/pass"
  1796. auth method */
  1797. req->socks_version = 5; /* remember we've already negotiated auth */
  1798. log_debug(LD_APP,"socks5: accepted method 2 (username/password)");
  1799. r=0;
  1800. } else if (have_no_auth) {
  1801. req->reply[1] = SOCKS_NO_AUTH; /* tell client to use "none" auth
  1802. method */
  1803. req->socks_version = 5; /* remember we've already negotiated auth */
  1804. log_debug(LD_APP,"socks5: accepted method 0 (no authentication)");
  1805. r=0;
  1806. } else {
  1807. log_warn(LD_APP,
  1808. "socks5: offered methods don't include 'no auth' or "
  1809. "username/password. Rejecting.");
  1810. req->reply[1] = '\xFF'; /* reject all methods */
  1811. r=-1;
  1812. }
  1813. /* Remove packet from buf. Some SOCKS clients will have sent extra
  1814. * junk at this point; let's hope it's an authentication message. */
  1815. *drain_out = 2u + nummethods;
  1816. return r;
  1817. }
  1818. if (req->auth_type != SOCKS_NO_AUTH && !req->got_auth) {
  1819. log_warn(LD_APP,
  1820. "socks5: negotiated authentication, but none provided");
  1821. return -1;
  1822. }
  1823. /* we know the method; read in the request */
  1824. log_debug(LD_APP,"socks5: checking request");
  1825. if (datalen < 7) {/* basic info plus >=1 for addr plus 2 for port */
  1826. *want_length_out = 7;
  1827. return 0; /* not yet */
  1828. }
  1829. req->command = (unsigned char) *(data+1);
  1830. if (req->command != SOCKS_COMMAND_CONNECT &&
  1831. req->command != SOCKS_COMMAND_RESOLVE &&
  1832. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  1833. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  1834. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  1835. req->command);
  1836. return -1;
  1837. }
  1838. switch (*(data+3)) { /* address type */
  1839. case 1: /* IPv4 address */
  1840. case 4: /* IPv6 address */ {
  1841. const int is_v6 = *(data+3) == 4;
  1842. const unsigned addrlen = is_v6 ? 16 : 4;
  1843. log_debug(LD_APP,"socks5: ipv4 address type");
  1844. if (datalen < 6+addrlen) {/* ip/port there? */
  1845. *want_length_out = 6+addrlen;
  1846. return 0; /* not yet */
  1847. }
  1848. if (is_v6)
  1849. tor_addr_from_ipv6_bytes(&destaddr, data+4);
  1850. else
  1851. tor_addr_from_ipv4n(&destaddr, get_uint32(data+4));
  1852. tor_addr_to_str(tmpbuf, &destaddr, sizeof(tmpbuf), 1);
  1853. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1854. log_warn(LD_APP,
  1855. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  1856. "Rejecting.",
  1857. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  1858. return -1;
  1859. }
  1860. strlcpy(req->address,tmpbuf,sizeof(req->address));
  1861. req->port = ntohs(get_uint16(data+4+addrlen));
  1862. *drain_out = 6+addrlen;
  1863. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  1864. !addressmap_have_mapping(req->address,0)) {
  1865. log_unsafe_socks_warning(5, req->address, req->port, safe_socks);
  1866. if (safe_socks)
  1867. return -1;
  1868. }
  1869. return 1;
  1870. }
  1871. case 3: /* fqdn */
  1872. log_debug(LD_APP,"socks5: fqdn address type");
  1873. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  1874. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  1875. "hostname type. Rejecting.");
  1876. return -1;
  1877. }
  1878. len = (unsigned char)*(data+4);
  1879. if (datalen < 7+len) { /* addr/port there? */
  1880. *want_length_out = 7+len;
  1881. return 0; /* not yet */
  1882. }
  1883. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  1884. log_warn(LD_APP,
  1885. "socks5 hostname is %d bytes, which doesn't fit in "
  1886. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  1887. return -1;
  1888. }
  1889. memcpy(req->address,data+5,len);
  1890. req->address[len] = 0;
  1891. req->port = ntohs(get_uint16(data+5+len));
  1892. *drain_out = 5+len+2;
  1893. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1894. log_warn(LD_PROTOCOL,
  1895. "Your application (using socks5 to port %d) gave Tor "
  1896. "a malformed hostname: %s. Rejecting the connection.",
  1897. req->port, escaped(req->address));
  1898. return -1;
  1899. }
  1900. if (log_sockstype)
  1901. log_notice(LD_APP,
  1902. "Your application (using socks5 to port %d) instructed "
  1903. "Tor to take care of the DNS resolution itself if "
  1904. "necessary. This is good.", req->port);
  1905. return 1;
  1906. default: /* unsupported */
  1907. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  1908. (int) *(data+3));
  1909. return -1;
  1910. }
  1911. tor_assert(0);
  1912. case 4: { /* socks4 */
  1913. enum {socks4, socks4a} socks4_prot = socks4a;
  1914. const char *authstart, *authend;
  1915. /* http://ss5.sourceforge.net/socks4.protocol.txt */
  1916. /* http://ss5.sourceforge.net/socks4A.protocol.txt */
  1917. req->socks_version = 4;
  1918. if (datalen < SOCKS4_NETWORK_LEN) {/* basic info available? */
  1919. *want_length_out = SOCKS4_NETWORK_LEN;
  1920. return 0; /* not yet */
  1921. }
  1922. // buf_pullup(buf, 1280, 0);
  1923. req->command = (unsigned char) *(data+1);
  1924. if (req->command != SOCKS_COMMAND_CONNECT &&
  1925. req->command != SOCKS_COMMAND_RESOLVE) {
  1926. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  1927. * socks4.) */
  1928. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  1929. req->command);
  1930. return -1;
  1931. }
  1932. req->port = ntohs(get_uint16(data+2));
  1933. destip = ntohl(get_uint32(data+4));
  1934. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  1935. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  1936. return -1;
  1937. }
  1938. if (destip >> 8) {
  1939. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  1940. in.s_addr = htonl(destip);
  1941. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  1942. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  1943. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  1944. (int)strlen(tmpbuf));
  1945. return -1;
  1946. }
  1947. log_debug(LD_APP,
  1948. "socks4: successfully read destip (%s)",
  1949. safe_str_client(tmpbuf));
  1950. socks4_prot = socks4;
  1951. }
  1952. authstart = data + SOCKS4_NETWORK_LEN;
  1953. next = memchr(authstart, 0,
  1954. datalen-SOCKS4_NETWORK_LEN);
  1955. if (!next) {
  1956. if (datalen >= 1024) {
  1957. log_debug(LD_APP, "Socks4 user name too long; rejecting.");
  1958. return -1;
  1959. }
  1960. log_debug(LD_APP,"socks4: Username not here yet.");
  1961. *want_length_out = datalen+1024; /* More than we need, but safe */
  1962. return 0;
  1963. }
  1964. authend = next;
  1965. tor_assert(next < data+datalen);
  1966. startaddr = NULL;
  1967. if (socks4_prot != socks4a &&
  1968. !addressmap_have_mapping(tmpbuf,0)) {
  1969. log_unsafe_socks_warning(4, tmpbuf, req->port, safe_socks);
  1970. if (safe_socks)
  1971. return -1;
  1972. }
  1973. if (socks4_prot == socks4a) {
  1974. if (next+1 == data+datalen) {
  1975. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  1976. *want_length_out = datalen + 1024; /* More than we need, but safe */
  1977. return 0;
  1978. }
  1979. startaddr = next+1;
  1980. next = memchr(startaddr, 0, data + datalen - startaddr);
  1981. if (!next) {
  1982. if (datalen >= 1024) {
  1983. log_debug(LD_APP,"socks4: Destaddr too long.");
  1984. return -1;
  1985. }
  1986. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  1987. *want_length_out = datalen + 1024; /* More than we need, but safe */
  1988. return 0;
  1989. }
  1990. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  1991. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  1992. return -1;
  1993. }
  1994. // tor_assert(next < buf->cur+buf->datalen);
  1995. if (log_sockstype)
  1996. log_notice(LD_APP,
  1997. "Your application (using socks4a to port %d) instructed "
  1998. "Tor to take care of the DNS resolution itself if "
  1999. "necessary. This is good.", req->port);
  2000. }
  2001. log_debug(LD_APP,"socks4: Everything is here. Success.");
  2002. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  2003. sizeof(req->address));
  2004. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  2005. log_warn(LD_PROTOCOL,
  2006. "Your application (using socks4 to port %d) gave Tor "
  2007. "a malformed hostname: %s. Rejecting the connection.",
  2008. req->port, escaped(req->address));
  2009. return -1;
  2010. }
  2011. if (authend != authstart) {
  2012. req->got_auth = 1;
  2013. req->usernamelen = authend - authstart;
  2014. req->username = tor_memdup(authstart, authend - authstart);
  2015. }
  2016. /* next points to the final \0 on inbuf */
  2017. *drain_out = next - data + 1;
  2018. return 1;
  2019. }
  2020. case 'G': /* get */
  2021. case 'H': /* head */
  2022. case 'P': /* put/post */
  2023. case 'C': /* connect */
  2024. strlcpy((char*)req->reply,
  2025. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  2026. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  2027. "<html>\n"
  2028. "<head>\n"
  2029. "<title>Tor is not an HTTP Proxy</title>\n"
  2030. "</head>\n"
  2031. "<body>\n"
  2032. "<h1>Tor is not an HTTP Proxy</h1>\n"
  2033. "<p>\n"
  2034. "It appears you have configured your web browser to use Tor as an HTTP proxy."
  2035. "\n"
  2036. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  2037. "Please configure your client accordingly.\n"
  2038. "</p>\n"
  2039. "<p>\n"
  2040. "See <a href=\"https://www.torproject.org/documentation.html\">"
  2041. "https://www.torproject.org/documentation.html</a> for more "
  2042. "information.\n"
  2043. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  2044. " IE will be willing to display it. Comment comment comment comment "
  2045. " comment comment comment comment comment comment comment comment.-->\n"
  2046. "</p>\n"
  2047. "</body>\n"
  2048. "</html>\n"
  2049. , MAX_SOCKS_REPLY_LEN);
  2050. req->replylen = strlen((char*)req->reply)+1;
  2051. /* fall through */
  2052. default: /* version is not socks4 or socks5 */
  2053. log_warn(LD_APP,
  2054. "Socks version %d not recognized. (Tor is not an http proxy.)",
  2055. *(data));
  2056. {
  2057. /* Tell the controller the first 8 bytes. */
  2058. char *tmp = tor_strndup(data, datalen < 8 ? datalen : 8);
  2059. control_event_client_status(LOG_WARN,
  2060. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  2061. escaped(tmp));
  2062. tor_free(tmp);
  2063. }
  2064. return -1;
  2065. }
  2066. }
  2067. /** Inspect a reply from SOCKS server stored in <b>buf</b> according
  2068. * to <b>state</b>, removing the protocol data upon success. Return 0 on
  2069. * incomplete response, 1 on success and -1 on error, in which case
  2070. * <b>reason</b> is set to a descriptive message (free() when finished
  2071. * with it).
  2072. *
  2073. * As a special case, 2 is returned when user/pass is required
  2074. * during SOCKS5 handshake and user/pass is configured.
  2075. */
  2076. int
  2077. fetch_from_buf_socks_client(buf_t *buf, int state, char **reason)
  2078. {
  2079. ssize_t drain = 0;
  2080. int r;
  2081. if (buf->datalen < 2)
  2082. return 0;
  2083. buf_pullup(buf, MAX_SOCKS_MESSAGE_LEN, 0);
  2084. tor_assert(buf->head && buf->head->datalen >= 2);
  2085. r = parse_socks_client((uint8_t*)buf->head->data, buf->head->datalen,
  2086. state, reason, &drain);
  2087. if (drain > 0)
  2088. buf_remove_from_front(buf, drain);
  2089. else if (drain < 0)
  2090. buf_clear(buf);
  2091. return r;
  2092. }
  2093. #ifdef USE_BUFFEREVENTS
  2094. /** As fetch_from_buf_socks_client, buf works on an evbuffer */
  2095. int
  2096. fetch_from_evbuffer_socks_client(struct evbuffer *buf, int state,
  2097. char **reason)
  2098. {
  2099. ssize_t drain = 0;
  2100. uint8_t *data;
  2101. size_t datalen;
  2102. int r;
  2103. /* Linearize the SOCKS response in the buffer, up to 128 bytes.
  2104. * (parse_socks_client shouldn't need to see anything beyond that.) */
  2105. datalen = evbuffer_get_length(buf);
  2106. if (datalen > MAX_SOCKS_MESSAGE_LEN)
  2107. datalen = MAX_SOCKS_MESSAGE_LEN;
  2108. data = evbuffer_pullup(buf, datalen);
  2109. r = parse_socks_client(data, datalen, state, reason, &drain);
  2110. if (drain > 0)
  2111. evbuffer_drain(buf, drain);
  2112. else if (drain < 0)
  2113. evbuffer_drain(buf, evbuffer_get_length(buf));
  2114. return r;
  2115. }
  2116. #endif
  2117. /** Implementation logic for fetch_from_*_socks_client. */
  2118. static int
  2119. parse_socks_client(const uint8_t *data, size_t datalen,
  2120. int state, char **reason,
  2121. ssize_t *drain_out)
  2122. {
  2123. unsigned int addrlen;
  2124. *drain_out = 0;
  2125. if (datalen < 2)
  2126. return 0;
  2127. switch (state) {
  2128. case PROXY_SOCKS4_WANT_CONNECT_OK:
  2129. /* Wait for the complete response */
  2130. if (datalen < 8)
  2131. return 0;
  2132. if (data[1] != 0x5a) {
  2133. *reason = tor_strdup(socks4_response_code_to_string(data[1]));
  2134. return -1;
  2135. }
  2136. /* Success */
  2137. *drain_out = 8;
  2138. return 1;
  2139. case PROXY_SOCKS5_WANT_AUTH_METHOD_NONE:
  2140. /* we don't have any credentials */
  2141. if (data[1] != 0x00) {
  2142. *reason = tor_strdup("server doesn't support any of our "
  2143. "available authentication methods");
  2144. return -1;
  2145. }
  2146. log_info(LD_NET, "SOCKS 5 client: continuing without authentication");
  2147. *drain_out = -1;
  2148. return 1;
  2149. case PROXY_SOCKS5_WANT_AUTH_METHOD_RFC1929:
  2150. /* we have a username and password. return 1 if we can proceed without
  2151. * providing authentication, or 2 otherwise. */
  2152. switch (data[1]) {
  2153. case 0x00:
  2154. log_info(LD_NET, "SOCKS 5 client: we have auth details but server "
  2155. "doesn't require authentication.");
  2156. *drain_out = -1;
  2157. return 1;
  2158. case 0x02:
  2159. log_info(LD_NET, "SOCKS 5 client: need authentication.");
  2160. *drain_out = -1;
  2161. return 2;
  2162. /* fall through */
  2163. }
  2164. *reason = tor_strdup("server doesn't support any of our available "
  2165. "authentication methods");
  2166. return -1;
  2167. case PROXY_SOCKS5_WANT_AUTH_RFC1929_OK:
  2168. /* handle server reply to rfc1929 authentication */
  2169. if (data[1] != 0x00) {
  2170. *reason = tor_strdup("authentication failed");
  2171. return -1;
  2172. }
  2173. log_info(LD_NET, "SOCKS 5 client: authentication successful.");
  2174. *drain_out = -1;
  2175. return 1;
  2176. case PROXY_SOCKS5_WANT_CONNECT_OK:
  2177. /* response is variable length. BND.ADDR, etc, isn't needed
  2178. * (don't bother with buf_pullup()), but make sure to eat all
  2179. * the data used */
  2180. /* wait for address type field to arrive */
  2181. if (datalen < 4)
  2182. return 0;
  2183. switch (data[3]) {
  2184. case 0x01: /* ip4 */
  2185. addrlen = 4;
  2186. break;
  2187. case 0x04: /* ip6 */
  2188. addrlen = 16;
  2189. break;
  2190. case 0x03: /* fqdn (can this happen here?) */
  2191. if (datalen < 5)
  2192. return 0;
  2193. addrlen = 1 + data[4];
  2194. break;
  2195. default:
  2196. *reason = tor_strdup("invalid response to connect request");
  2197. return -1;
  2198. }
  2199. /* wait for address and port */
  2200. if (datalen < 6 + addrlen)
  2201. return 0;
  2202. if (data[1] != 0x00) {
  2203. *reason = tor_strdup(socks5_response_code_to_string(data[1]));
  2204. return -1;
  2205. }
  2206. *drain_out = 6 + addrlen;
  2207. return 1;
  2208. }
  2209. /* shouldn't get here... */
  2210. tor_assert(0);
  2211. return -1;
  2212. }
  2213. /** Return 1 iff buf looks more like it has an (obsolete) v0 controller
  2214. * command on it than any valid v1 controller command. */
  2215. int
  2216. peek_buf_has_control0_command(buf_t *buf)
  2217. {
  2218. if (buf->datalen >= 4) {
  2219. char header[4];
  2220. uint16_t cmd;
  2221. peek_from_buf(header, sizeof(header), buf);
  2222. cmd = ntohs(get_uint16(header+2));
  2223. if (cmd <= 0x14)
  2224. return 1; /* This is definitely not a v1 control command. */
  2225. }
  2226. return 0;
  2227. }
  2228. #ifdef USE_BUFFEREVENTS
  2229. int
  2230. peek_evbuffer_has_control0_command(struct evbuffer *buf)
  2231. {
  2232. int result = 0;
  2233. if (evbuffer_get_length(buf) >= 4) {
  2234. int free_out = 0;
  2235. char *data = NULL;
  2236. size_t n = inspect_evbuffer(buf, &data, 4, &free_out, NULL);
  2237. uint16_t cmd;
  2238. tor_assert(n >= 4);
  2239. cmd = ntohs(get_uint16(data+2));
  2240. if (cmd <= 0x14)
  2241. result = 1;
  2242. if (free_out)
  2243. tor_free(data);
  2244. }
  2245. return result;
  2246. }
  2247. #endif
  2248. /** Return the index within <b>buf</b> at which <b>ch</b> first appears,
  2249. * or -1 if <b>ch</b> does not appear on buf. */
  2250. static off_t
  2251. buf_find_offset_of_char(buf_t *buf, char ch)
  2252. {
  2253. chunk_t *chunk;
  2254. off_t offset = 0;
  2255. for (chunk = buf->head; chunk; chunk = chunk->next) {
  2256. char *cp = memchr(chunk->data, ch, chunk->datalen);
  2257. if (cp)
  2258. return offset + (cp - chunk->data);
  2259. else
  2260. offset += chunk->datalen;
  2261. }
  2262. return -1;
  2263. }
  2264. /** Try to read a single LF-terminated line from <b>buf</b>, and write it
  2265. * (including the LF), NUL-terminated, into the *<b>data_len</b> byte buffer
  2266. * at <b>data_out</b>. Set *<b>data_len</b> to the number of bytes in the
  2267. * line, not counting the terminating NUL. Return 1 if we read a whole line,
  2268. * return 0 if we don't have a whole line yet, and return -1 if the line
  2269. * length exceeds *<b>data_len</b>.
  2270. */
  2271. int
  2272. fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
  2273. {
  2274. size_t sz;
  2275. off_t offset;
  2276. if (!buf->head)
  2277. return 0;
  2278. offset = buf_find_offset_of_char(buf, '\n');
  2279. if (offset < 0)
  2280. return 0;
  2281. sz = (size_t) offset;
  2282. if (sz+2 > *data_len) {
  2283. *data_len = sz + 2;
  2284. return -1;
  2285. }
  2286. fetch_from_buf(data_out, sz+1, buf);
  2287. data_out[sz+1] = '\0';
  2288. *data_len = sz+1;
  2289. return 1;
  2290. }
  2291. /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
  2292. * zlib state <b>state</b>, appending the result to <b>buf</b>. If
  2293. * <b>done</b> is true, flush the data in the state and finish the
  2294. * compression/uncompression. Return -1 on failure, 0 on success. */
  2295. int
  2296. write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  2297. const char *data, size_t data_len,
  2298. int done)
  2299. {
  2300. char *next;
  2301. size_t old_avail, avail;
  2302. int over = 0;
  2303. do {
  2304. int need_new_chunk = 0;
  2305. if (!buf->tail || ! CHUNK_REMAINING_CAPACITY(buf->tail)) {
  2306. size_t cap = data_len / 4;
  2307. buf_add_chunk_with_capacity(buf, cap, 1);
  2308. }
  2309. next = CHUNK_WRITE_PTR(buf->tail);
  2310. avail = old_avail = CHUNK_REMAINING_CAPACITY(buf->tail);
  2311. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  2312. case TOR_ZLIB_DONE:
  2313. over = 1;
  2314. break;
  2315. case TOR_ZLIB_ERR:
  2316. return -1;
  2317. case TOR_ZLIB_OK:
  2318. if (data_len == 0)
  2319. over = 1;
  2320. break;
  2321. case TOR_ZLIB_BUF_FULL:
  2322. if (avail) {
  2323. /* Zlib says we need more room (ZLIB_BUF_FULL). Start a new chunk
  2324. * automatically, whether were going to or not. */
  2325. need_new_chunk = 1;
  2326. }
  2327. break;
  2328. }
  2329. buf->datalen += old_avail - avail;
  2330. buf->tail->datalen += old_avail - avail;
  2331. if (need_new_chunk) {
  2332. buf_add_chunk_with_capacity(buf, data_len/4, 1);
  2333. }
  2334. } while (!over);
  2335. check();
  2336. return 0;
  2337. }
  2338. #ifdef USE_BUFFEREVENTS
  2339. int
  2340. write_to_evbuffer_zlib(struct evbuffer *buf, tor_zlib_state_t *state,
  2341. const char *data, size_t data_len,
  2342. int done)
  2343. {
  2344. char *next;
  2345. size_t old_avail, avail;
  2346. int over = 0, n;
  2347. struct evbuffer_iovec vec[1];
  2348. do {
  2349. {
  2350. size_t cap = data_len / 4;
  2351. if (cap < 128)
  2352. cap = 128;
  2353. /* XXXX NM this strategy is fragmentation-prone. We should really have
  2354. * two iovecs, and write first into the one, and then into the
  2355. * second if the first gets full. */
  2356. n = evbuffer_reserve_space(buf, cap, vec, 1);
  2357. tor_assert(n == 1);
  2358. }
  2359. next = vec[0].iov_base;
  2360. avail = old_avail = vec[0].iov_len;
  2361. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  2362. case TOR_ZLIB_DONE:
  2363. over = 1;
  2364. break;
  2365. case TOR_ZLIB_ERR:
  2366. return -1;
  2367. case TOR_ZLIB_OK:
  2368. if (data_len == 0)
  2369. over = 1;
  2370. break;
  2371. case TOR_ZLIB_BUF_FULL:
  2372. if (avail) {
  2373. /* Zlib says we need more room (ZLIB_BUF_FULL). Start a new chunk
  2374. * automatically, whether were going to or not. */
  2375. }
  2376. break;
  2377. }
  2378. /* XXXX possible infinite loop on BUF_FULL. */
  2379. vec[0].iov_len = old_avail - avail;
  2380. evbuffer_commit_space(buf, vec, 1);
  2381. } while (!over);
  2382. check();
  2383. return 0;
  2384. }
  2385. #endif
  2386. /** Set *<b>output</b> to contain a copy of the data in *<b>input</b> */
  2387. int
  2388. generic_buffer_set_to_copy(generic_buffer_t **output,
  2389. const generic_buffer_t *input)
  2390. {
  2391. #ifdef USE_BUFFEREVENTS
  2392. struct evbuffer_ptr ptr;
  2393. size_t remaining = evbuffer_get_length(input);
  2394. if (*output) {
  2395. evbuffer_drain(*output, evbuffer_get_length(*output));
  2396. } else {
  2397. if (!(*output = evbuffer_new()))
  2398. return -1;
  2399. }
  2400. evbuffer_ptr_set((struct evbuffer*)input, &ptr, 0, EVBUFFER_PTR_SET);
  2401. while (remaining) {
  2402. struct evbuffer_iovec v[4];
  2403. int n_used, i;
  2404. n_used = evbuffer_peek((struct evbuffer*)input, -1, &ptr, v, 4);
  2405. if (n_used < 0)
  2406. return -1;
  2407. for (i=0;i<n_used;++i) {
  2408. evbuffer_add(*output, v[i].iov_base, v[i].iov_len);
  2409. tor_assert(v[i].iov_len <= remaining);
  2410. remaining -= v[i].iov_len;
  2411. evbuffer_ptr_set((struct evbuffer*)input,
  2412. &ptr, v[i].iov_len, EVBUFFER_PTR_ADD);
  2413. }
  2414. }
  2415. #else
  2416. if (*output)
  2417. buf_free(*output);
  2418. *output = buf_copy(input);
  2419. #endif
  2420. return 0;
  2421. }
  2422. /** Log an error and exit if <b>buf</b> is corrupted.
  2423. */
  2424. void
  2425. assert_buf_ok(buf_t *buf)
  2426. {
  2427. tor_assert(buf);
  2428. tor_assert(buf->magic == BUFFER_MAGIC);
  2429. if (! buf->head) {
  2430. tor_assert(!buf->tail);
  2431. tor_assert(buf->datalen == 0);
  2432. } else {
  2433. chunk_t *ch;
  2434. size_t total = 0;
  2435. tor_assert(buf->tail);
  2436. for (ch = buf->head; ch; ch = ch->next) {
  2437. total += ch->datalen;
  2438. tor_assert(ch->datalen <= ch->memlen);
  2439. tor_assert(ch->data >= &ch->mem[0]);
  2440. tor_assert(ch->data < &ch->mem[0]+ch->memlen);
  2441. tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
  2442. if (!ch->next)
  2443. tor_assert(ch == buf->tail);
  2444. }
  2445. tor_assert(buf->datalen == total);
  2446. }
  2447. }
  2448. #ifdef ENABLE_BUF_FREELISTS
  2449. /** Log an error and exit if <b>fl</b> is corrupted.
  2450. */
  2451. static void
  2452. assert_freelist_ok(chunk_freelist_t *fl)
  2453. {
  2454. chunk_t *ch;
  2455. int n;
  2456. tor_assert(fl->alloc_size > 0);
  2457. n = 0;
  2458. for (ch = fl->head; ch; ch = ch->next) {
  2459. tor_assert(CHUNK_ALLOC_SIZE(ch->memlen) == fl->alloc_size);
  2460. ++n;
  2461. }
  2462. tor_assert(n == fl->cur_length);
  2463. tor_assert(n >= fl->lowest_length);
  2464. tor_assert(n <= fl->max_length);
  2465. }
  2466. #endif