buffers.c 81 KB

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