buffers.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char buffers_c_id[] =
  7. "$Id$";
  8. /**
  9. * \file buffers.c
  10. * \brief Implements a generic buffer interface. Buffers are
  11. * fairly opaque string holders that can read to or flush from:
  12. * memory, file descriptors, or TLS connections.
  13. **/
  14. #include "or.h"
  15. #define SENTINELS
  16. #undef CHECK_AFTER_RESIZE
  17. #undef PARANOIA
  18. #undef NOINLINE
  19. #ifdef SENTINELS
  20. /* If SENTINELS is defined, check for attempts to write beyond the
  21. * end/before the start of the buffer.
  22. */
  23. #define START_MAGIC 0x70370370u
  24. #define END_MAGIC 0xA0B0C0D0u
  25. #define RAW_MEM(m) ((void*)(((char*)m)-4))
  26. #define GUARDED_MEM(m) ((void*)(((char*)m)+4))
  27. #define ALLOC_LEN(ln) ((ln)+8)
  28. #define SET_GUARDS(m, ln) \
  29. do { set_uint32((m)-4,START_MAGIC); set_uint32((m)+ln,END_MAGIC); } while (0)
  30. #else
  31. #define RAW_MEM(m) (m)
  32. #define GUARDED_MEM(m) (m)
  33. #define ALLOC_LEN(ln) (ln)
  34. #define SET_GUARDS(m,ln) do {} while (0)
  35. #endif
  36. #ifdef PARANOIA
  37. #define check() do { assert_buf_ok(buf); } while (0)
  38. #else
  39. #define check() do { } while (0)
  40. #endif
  41. #ifdef NOINLINE
  42. #undef INLINE
  43. #define INLINE
  44. #endif
  45. #define BUFFER_MAGIC 0xB0FFF312u
  46. /** A resizeable buffer, optimized for reading and writing. */
  47. struct buf_t {
  48. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  49. * BUFFER_MAGIC */
  50. char *mem; /**< Storage for data in the buffer */
  51. char *cur; /**< The first byte used for storing data in the buffer. */
  52. size_t highwater; /**< Largest observed datalen since last buf_shrink */
  53. size_t len; /**< Maximum amount of data that <b>mem</b> can hold. */
  54. size_t memsize; /**< How many bytes did we actually allocate? Can be less
  55. * than 'len' if we shortened 'len' by a few bytes to make
  56. * zlib wrap around more easily. */
  57. size_t datalen; /**< Number of bytes currently in <b>mem</b>. */
  58. };
  59. /** How many bytes, total, are used in all buffers? */
  60. uint64_t buf_total_used = 0;
  61. /** How many bytes, total, are allocated in all buffers? */
  62. uint64_t buf_total_alloc = 0;
  63. /** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
  64. #define INITIAL_BUF_SIZE (4*1024)
  65. /** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
  66. * out smaller than this, but they will never autoshrink to less
  67. * than this size. */
  68. #define MIN_LAZY_SHRINK_SIZE (4*1024)
  69. static INLINE void peek_from_buf(char *string, size_t string_len, buf_t *buf);
  70. /** If the contents of buf wrap around the end of the allocated space,
  71. * malloc a new buf and copy the contents in starting at the
  72. * beginning. This operation is relatively expensive, so it shouldn't
  73. * be used e.g. for every single read or write.
  74. */
  75. static void
  76. buf_normalize(buf_t *buf)
  77. {
  78. check();
  79. if (buf->cur + buf->datalen <= buf->mem+buf->len) {
  80. return;
  81. } else {
  82. char *newmem, *oldmem;
  83. size_t sz = (buf->mem+buf->len)-buf->cur;
  84. log_warn(LD_BUG, "Unexpected non-normalized buffer.");
  85. newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(buf->memsize)));
  86. SET_GUARDS(newmem, buf->memsize);
  87. memcpy(newmem, buf->cur, sz);
  88. memcpy(newmem+sz, buf->mem, buf->datalen-sz);
  89. oldmem = RAW_MEM(buf->mem);
  90. tor_free(oldmem); /* Can't use tor_free directly. */
  91. buf->mem = buf->cur = newmem;
  92. buf->len = buf->memsize;
  93. check();
  94. }
  95. }
  96. /** Return the point in the buffer where the next byte will get stored. */
  97. static INLINE char *
  98. _buf_end(buf_t *buf)
  99. {
  100. char *next = buf->cur + buf->datalen;
  101. char *end = buf->mem + buf->len;
  102. return (next < end) ? next : (next - buf->len);
  103. }
  104. /** If the pointer <b>cp</b> has passed beyond the end of the buffer, wrap it
  105. * around. */
  106. static INLINE char *
  107. _wrap_ptr(buf_t *buf, char *cp)
  108. {
  109. return (cp >= buf->mem + buf->len) ? (cp - buf->len) : cp;
  110. }
  111. /** Return the offset of <b>cp</b> within the buffer. */
  112. static INLINE int
  113. _buf_offset(buf_t *buf, char *cp)
  114. {
  115. if (cp >= buf->cur)
  116. return cp - buf->cur;
  117. else
  118. /* return (cp - buf->mem) + buf->mem+buf->len - buf->cur */
  119. return cp + buf->len - buf->cur;
  120. }
  121. /** If the range of *<b>len</b> bytes starting at <b>at</b> wraps around the
  122. * end of the buffer, then set *<b>len</b> to the number of bytes starting
  123. * at <b>at</b>, and set *<b>more_len</b> to the number of bytes starting
  124. * at <b>buf-&gt;mem</b>. Otherwise, set *<b>more_len</b> to 0.
  125. */
  126. static INLINE void
  127. _split_range(buf_t *buf, char *at, size_t *len,
  128. size_t *more_len)
  129. {
  130. char *eos = at + *len;
  131. check();
  132. if (eos >= (buf->mem + buf->len)) {
  133. *more_len = eos - (buf->mem + buf->len);
  134. *len -= *more_len;
  135. } else {
  136. *more_len = 0;
  137. }
  138. }
  139. /** Change a buffer's capacity. <b>new_capacity</b> must be \>=
  140. * buf->datalen. */
  141. static void
  142. buf_resize(buf_t *buf, size_t new_capacity)
  143. {
  144. off_t offset;
  145. #ifdef CHECK_AFTER_RESIZE
  146. char *tmp, *tmp2;
  147. #endif
  148. tor_assert(buf->datalen <= new_capacity);
  149. tor_assert(new_capacity);
  150. #ifdef CHECK_AFTER_RESIZE
  151. assert_buf_ok(buf);
  152. tmp = tor_malloc(buf->datalen);
  153. tmp2 = tor_malloc(buf->datalen);
  154. peek_from_buf(tmp, buf->datalen, buf);
  155. #endif
  156. if (buf->len == new_capacity)
  157. return;
  158. offset = buf->cur - buf->mem;
  159. if (offset + buf->datalen > new_capacity) {
  160. /* We need to move stuff before we shrink. */
  161. if (offset + buf->datalen > buf->len) {
  162. /* We have:
  163. *
  164. * mem[0] ... mem[datalen-(len-offset)] (end of data)
  165. * mem[offset] ... mem[len-1] (the start of the data)
  166. *
  167. * We're shrinking the buffer by (len-new_capacity) bytes, so we need
  168. * to move the start portion back by that many bytes.
  169. */
  170. memmove(buf->cur-(buf->len-new_capacity), buf->cur,
  171. (size_t)(buf->len-offset));
  172. offset -= (buf->len-new_capacity);
  173. } else {
  174. /* The data doesn't wrap around, but it does extend beyond the new
  175. * buffer length:
  176. * mem[offset] ... mem[offset+datalen-1] (the data)
  177. */
  178. memmove(buf->mem, buf->cur, buf->datalen);
  179. offset = 0;
  180. }
  181. }
  182. /* XXX Some play code to throw away old buffers sometimes rather
  183. * than constantly reallocing them; just in case this is our memory
  184. * problem. It looks for now like it isn't, so disabled. -RD */
  185. if (0 && new_capacity == MIN_LAZY_SHRINK_SIZE &&
  186. !buf->datalen &&
  187. buf->len >= 1<<16) {
  188. /* don't realloc; free and malloc */
  189. char *oldmem, *newmem = GUARDED_MEM(tor_malloc(ALLOC_LEN(new_capacity)));
  190. SET_GUARDS(newmem, new_capacity);
  191. oldmem = RAW_MEM(buf->mem);
  192. tor_free(oldmem);
  193. buf->mem = buf->cur = newmem;
  194. } else {
  195. buf->mem = GUARDED_MEM(tor_realloc(RAW_MEM(buf->mem),
  196. ALLOC_LEN(new_capacity)));
  197. SET_GUARDS(buf->mem, new_capacity);
  198. buf->cur = buf->mem+offset;
  199. }
  200. buf_total_alloc += new_capacity;
  201. buf_total_alloc -= buf->len;
  202. if (offset + buf->datalen > buf->len) {
  203. /* We need to move data now that we are done growing. The buffer
  204. * now contains:
  205. *
  206. * mem[0] ... mem[datalen-(len-offset)] (end of data)
  207. * mem[offset] ... mem[len-1] (the start of the data)
  208. * mem[len]...mem[new_capacity] (empty space)
  209. *
  210. * We're growing by (new_capacity-len) bytes, so we need to move the
  211. * end portion forward by that many bytes.
  212. */
  213. memmove(buf->cur+(new_capacity-buf->len), buf->cur,
  214. (size_t)(buf->len-offset));
  215. buf->cur += new_capacity-buf->len;
  216. }
  217. buf->memsize = buf->len = new_capacity;
  218. #ifdef CHECK_AFTER_RESIZE
  219. assert_buf_ok(buf);
  220. peek_from_buf(tmp2, buf->datalen, buf);
  221. if (memcmp(tmp, tmp2, buf->datalen)) {
  222. tor_assert(0);
  223. }
  224. tor_free(tmp);
  225. tor_free(tmp2);
  226. #endif
  227. }
  228. /** If the buffer is not large enough to hold <b>capacity</b> bytes, resize
  229. * it so that it can. (The new size will be a power of 2 times the old
  230. * size.)
  231. */
  232. static INLINE int
  233. buf_ensure_capacity(buf_t *buf, size_t capacity)
  234. {
  235. size_t new_len, min_len;
  236. if (buf->len >= capacity) /* Don't grow if we're already big enough. */
  237. return 0;
  238. if (capacity > MAX_BUF_SIZE) /* Don't grow past the maximum. */
  239. return -1;
  240. /* Find the smallest new_len equal to (2**X) for some X; such that
  241. * new_len is at least capacity, and at least 2*buf->len.
  242. */
  243. min_len = buf->len*2;
  244. new_len = 16;
  245. while (new_len < min_len)
  246. new_len *= 2;
  247. while (new_len < capacity)
  248. new_len *= 2;
  249. /* Resize the buffer. */
  250. log_debug(LD_MM,"Growing buffer from %d to %d bytes.",
  251. (int)buf->len, (int)new_len);
  252. buf_resize(buf,new_len);
  253. return 0;
  254. }
  255. /** Resize buf so it won't hold extra memory that we haven't been
  256. * using lately (that is, since the last time we called buf_shrink).
  257. * Try to shrink the buf until it is the largest factor of two that
  258. * can contain <b>buf</b>-&gt;highwater, but never smaller than
  259. * MIN_LAZY_SHRINK_SIZE.
  260. */
  261. void
  262. buf_shrink(buf_t *buf)
  263. {
  264. size_t new_len;
  265. new_len = buf->len;
  266. while (buf->highwater < (new_len>>2) && new_len > MIN_LAZY_SHRINK_SIZE*2)
  267. new_len >>= 1;
  268. buf->highwater = buf->datalen;
  269. if (new_len == buf->len)
  270. return;
  271. log_debug(LD_MM,"Shrinking buffer from %d to %d bytes.",
  272. (int)buf->len, (int)new_len);
  273. buf_resize(buf, new_len);
  274. }
  275. /** Remove the first <b>n</b> bytes from buf. */
  276. static INLINE void
  277. buf_remove_from_front(buf_t *buf, size_t n)
  278. {
  279. tor_assert(buf->datalen >= n);
  280. buf->datalen -= n;
  281. buf_total_used -= n;
  282. if (buf->datalen) {
  283. buf->cur = _wrap_ptr(buf, buf->cur+n);
  284. } else {
  285. buf->cur = buf->mem;
  286. }
  287. check();
  288. }
  289. /** Make sure that the memory in buf ends with a zero byte. */
  290. static INLINE int
  291. buf_nul_terminate(buf_t *buf)
  292. {
  293. if (buf_ensure_capacity(buf,buf->datalen+1)<0)
  294. return -1;
  295. *_buf_end(buf) = '\0';
  296. return 0;
  297. }
  298. /** Create and return a new buf with capacity <b>size</b>. */
  299. buf_t *
  300. buf_new_with_capacity(size_t size)
  301. {
  302. buf_t *buf;
  303. buf = tor_malloc_zero(sizeof(buf_t));
  304. buf->magic = BUFFER_MAGIC;
  305. buf->cur = buf->mem = GUARDED_MEM(tor_malloc(ALLOC_LEN(size)));
  306. SET_GUARDS(buf->mem, size);
  307. buf->len = buf->memsize = size;
  308. buf_total_alloc += size;
  309. assert_buf_ok(buf);
  310. return buf;
  311. }
  312. /** Allocate and return a new buffer with default capacity. */
  313. buf_t *
  314. buf_new(void)
  315. {
  316. return buf_new_with_capacity(INITIAL_BUF_SIZE);
  317. }
  318. /** Remove all data from <b>buf</b>. */
  319. void
  320. buf_clear(buf_t *buf)
  321. {
  322. buf_total_used -= buf->datalen;
  323. buf->datalen = 0;
  324. buf->cur = buf->mem;
  325. buf->len = buf->memsize;
  326. }
  327. /** Return the number of bytes stored in <b>buf</b> */
  328. size_t
  329. buf_datalen(const buf_t *buf)
  330. {
  331. return buf->datalen;
  332. }
  333. /** Return the maximum bytes that can be stored in <b>buf</b> before buf
  334. * needs to resize. */
  335. size_t
  336. buf_capacity(const buf_t *buf)
  337. {
  338. return buf->len;
  339. }
  340. /** For testing only: Return a pointer to the raw memory stored in
  341. * <b>buf</b>. */
  342. const char *
  343. _buf_peek_raw_buffer(const buf_t *buf)
  344. {
  345. return buf->cur;
  346. }
  347. /** Release storage held by <b>buf</b>. */
  348. void
  349. buf_free(buf_t *buf)
  350. {
  351. char *oldmem;
  352. assert_buf_ok(buf);
  353. buf->magic = 0xDEADBEEF;
  354. oldmem = RAW_MEM(buf->mem);
  355. tor_free(oldmem);
  356. buf_total_alloc -= buf->len;
  357. buf_total_used -= buf->datalen;
  358. tor_free(buf);
  359. }
  360. /** Helper for read_to_buf(): read no more than at_most bytes from
  361. * socket s into buffer buf, starting at the position pos. (Does not
  362. * check for overflow.) Set *reached_eof to true on EOF. Return
  363. * number of bytes read on success, 0 if the read would block, -1 on
  364. * failure.
  365. */
  366. static INLINE int
  367. read_to_buf_impl(int s, size_t at_most, buf_t *buf,
  368. char *pos, int *reached_eof)
  369. {
  370. int read_result;
  371. // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
  372. read_result = tor_socket_recv(s, pos, at_most, 0);
  373. if (read_result < 0) {
  374. int e = tor_socket_errno(s);
  375. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  376. #ifdef MS_WINDOWS
  377. if (e == WSAENOBUFS)
  378. log_warn(LD_NET,"recv() failed: WSAENOBUFS. Not enough ram?");
  379. #endif
  380. return -1;
  381. }
  382. return 0; /* would block. */
  383. } else if (read_result == 0) {
  384. log_debug(LD_NET,"Encountered eof");
  385. *reached_eof = 1;
  386. return 0;
  387. } else { /* we read some bytes */
  388. buf->datalen += read_result;
  389. buf_total_used += read_result;
  390. if (buf->datalen > buf->highwater)
  391. buf->highwater = buf->datalen;
  392. log_debug(LD_NET,"Read %d bytes. %d on inbuf.",read_result,
  393. (int)buf->datalen);
  394. return read_result;
  395. }
  396. }
  397. /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
  398. * <b>at_most</b> bytes, resizing the buffer as necessary. If recv()
  399. * returns 0, set *<b>reached_eof</b> to 1 and return 0. Return -1 on error;
  400. * else return the number of bytes read. Return 0 if recv() would
  401. * block.
  402. */
  403. int
  404. read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof)
  405. {
  406. int r;
  407. char *next;
  408. size_t at_start;
  409. /* assert_buf_ok(buf); */
  410. tor_assert(reached_eof);
  411. tor_assert(s>=0);
  412. if (buf_ensure_capacity(buf,buf->datalen+at_most))
  413. return -1;
  414. if (at_most + buf->datalen > buf->len)
  415. at_most = buf->len - buf->datalen; /* take the min of the two */
  416. if (at_most == 0)
  417. return 0; /* we shouldn't read anything */
  418. next = _buf_end(buf);
  419. _split_range(buf, next, &at_most, &at_start);
  420. r = read_to_buf_impl(s, at_most, buf, next, reached_eof);
  421. check();
  422. if (r < 0 || (size_t)r < at_most) {
  423. return r; /* Either error, eof, block, or no more to read. */
  424. }
  425. if (at_start) {
  426. int r2;
  427. tor_assert(_buf_end(buf) == buf->mem);
  428. r2 = read_to_buf_impl(s, at_start, buf, buf->mem, reached_eof);
  429. check();
  430. if (r2 < 0) {
  431. return r2;
  432. } else {
  433. r += r2;
  434. }
  435. }
  436. return r;
  437. }
  438. /** Helper for read_to_buf_tls(): read no more than <b>at_most</b>
  439. * bytes from the TLS connection <b>tls</b> into buffer <b>buf</b>,
  440. * starting at the position <b>next</b>. (Does not check for overflow.)
  441. * Return number of bytes read on success, 0 if the read would block,
  442. * -1 on failure.
  443. */
  444. static INLINE int
  445. read_to_buf_tls_impl(tor_tls_t *tls, size_t at_most, buf_t *buf, char *next)
  446. {
  447. int r;
  448. log_debug(LD_NET,"before: %d on buf, %d pending, at_most %d.",
  449. (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
  450. (int)at_most);
  451. r = tor_tls_read(tls, next, at_most);
  452. if (r<0)
  453. return r;
  454. buf->datalen += r;
  455. buf_total_used += r;
  456. if (buf->datalen > buf->highwater)
  457. buf->highwater = buf->datalen;
  458. log_debug(LD_NET,"Read %d bytes. %d on inbuf; %d pending",r,
  459. (int)buf->datalen,(int)tor_tls_get_pending_bytes(tls));
  460. return r;
  461. }
  462. /** As read_to_buf, but reads from a TLS connection.
  463. *
  464. * Using TLS on OR connections complicates matters in two ways.
  465. *
  466. * First, a TLS stream has its own read buffer independent of the
  467. * connection's read buffer. (TLS needs to read an entire frame from
  468. * the network before it can decrypt any data. Thus, trying to read 1
  469. * byte from TLS can require that several KB be read from the network
  470. * and decrypted. The extra data is stored in TLS's decrypt buffer.)
  471. * Because the data hasn't been read by Tor (it's still inside the TLS),
  472. * this means that sometimes a connection "has stuff to read" even when
  473. * poll() didn't return POLLIN. The tor_tls_get_pending_bytes function is
  474. * used in connection.c to detect TLS objects with non-empty internal
  475. * buffers and read from them again.
  476. *
  477. * Second, the TLS stream's events do not correspond directly to network
  478. * events: sometimes, before a TLS stream can read, the network must be
  479. * ready to write -- or vice versa.
  480. */
  481. int
  482. read_to_buf_tls(tor_tls_t *tls, size_t at_most, buf_t *buf)
  483. {
  484. int r;
  485. char *next;
  486. size_t at_start;
  487. tor_assert(tls);
  488. assert_buf_ok(buf);
  489. log_debug(LD_NET,"start: %d on buf, %d pending, at_most %d.",
  490. (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
  491. (int)at_most);
  492. if (buf_ensure_capacity(buf, at_most+buf->datalen))
  493. return TOR_TLS_ERROR;
  494. if (at_most + buf->datalen > buf->len)
  495. at_most = buf->len - buf->datalen;
  496. if (at_most == 0)
  497. return 0;
  498. next = _buf_end(buf);
  499. _split_range(buf, next, &at_most, &at_start);
  500. r = read_to_buf_tls_impl(tls, at_most, buf, next);
  501. check();
  502. if (r < 0 || (size_t)r < at_most)
  503. return r; /* Either error, eof, block, or no more to read. */
  504. if (at_start) {
  505. int r2;
  506. tor_assert(_buf_end(buf) == buf->mem);
  507. r2 = read_to_buf_tls_impl(tls, at_start, buf, buf->mem);
  508. check();
  509. if (r2 < 0)
  510. return r2;
  511. else
  512. r += r2;
  513. }
  514. return r;
  515. }
  516. /** Helper for flush_buf(): try to write <b>sz</b> bytes from buffer
  517. * <b>buf</b> onto socket <b>s</b>. On success, deduct the bytes written
  518. * from *<b>buf_flushlen</b>.
  519. * Return the number of bytes written on success, -1 on failure.
  520. */
  521. static INLINE int
  522. flush_buf_impl(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  523. {
  524. int write_result;
  525. write_result = tor_socket_send(s, buf->cur, sz, 0);
  526. if (write_result < 0) {
  527. int e = tor_socket_errno(s);
  528. if (!ERRNO_IS_EAGAIN(e)) { /* it's a real error */
  529. #ifdef MS_WINDOWS
  530. if (e == WSAENOBUFS)
  531. log_warn(LD_NET,"write() failed: WSAENOBUFS. Not enough ram?");
  532. #endif
  533. return -1;
  534. }
  535. log_debug(LD_NET,"write() would block, returning.");
  536. return 0;
  537. } else {
  538. *buf_flushlen -= write_result;
  539. buf_remove_from_front(buf, write_result);
  540. return write_result;
  541. }
  542. }
  543. /** Write data from <b>buf</b> to the socket <b>s</b>. Write at most
  544. * <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by
  545. * the number of bytes actually written, and remove the written bytes
  546. * from the buffer. Return the number of bytes written on success,
  547. * -1 on failure. Return 0 if write() would block.
  548. */
  549. int
  550. flush_buf(int s, buf_t *buf, size_t sz, size_t *buf_flushlen)
  551. {
  552. int r;
  553. size_t flushed = 0;
  554. size_t flushlen0, flushlen1;
  555. /* assert_buf_ok(buf); */
  556. tor_assert(buf_flushlen);
  557. tor_assert(s>=0);
  558. tor_assert(*buf_flushlen <= buf->datalen);
  559. tor_assert(sz <= *buf_flushlen);
  560. if (sz == 0) /* nothing to flush */
  561. return 0;
  562. flushlen0 = sz;
  563. _split_range(buf, buf->cur, &flushlen0, &flushlen1);
  564. r = flush_buf_impl(s, buf, flushlen0, buf_flushlen);
  565. check();
  566. log_debug(LD_NET,"%d: flushed %d bytes, %d ready to flush, %d remain.",
  567. s,r,(int)*buf_flushlen,(int)buf->datalen);
  568. if (r < 0 || (size_t)r < flushlen0)
  569. return r; /* Error, or can't flush any more now. */
  570. flushed = r;
  571. if (flushlen1) {
  572. tor_assert(buf->cur == buf->mem);
  573. r = flush_buf_impl(s, buf, flushlen1, buf_flushlen);
  574. check();
  575. log_debug(LD_NET,"%d: flushed %d bytes, %d ready to flush, %d remain.",
  576. s,r,(int)*buf_flushlen,(int)buf->datalen);
  577. if (r<0)
  578. return r;
  579. flushed += r;
  580. }
  581. return flushed;
  582. }
  583. /** Helper for flush_buf_tls(): try to write <b>sz</b> bytes (or more if
  584. * required by a previous write) from buffer <b>buf</b> onto TLS object
  585. * <b>tls</b>. On success, deduct the bytes written from
  586. * *<b>buf_flushlen</b>. Return the number of bytes written on success, -1 on
  587. * failure.
  588. */
  589. static INLINE int
  590. flush_buf_tls_impl(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen)
  591. {
  592. int r;
  593. size_t forced;
  594. forced = tor_tls_get_forced_write_size(tls);
  595. if (forced > sz)
  596. sz = forced;
  597. r = tor_tls_write(tls, buf->cur, sz);
  598. if (r < 0) {
  599. return r;
  600. }
  601. *buf_flushlen -= r;
  602. buf_remove_from_front(buf, r);
  603. log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.",
  604. r,(int)*buf_flushlen,(int)buf->datalen);
  605. return r;
  606. }
  607. /** As flush_buf(), but writes data to a TLS connection.
  608. */
  609. int
  610. flush_buf_tls(tor_tls_t *tls, buf_t *buf, size_t sz, size_t *buf_flushlen)
  611. {
  612. int r;
  613. size_t flushed=0;
  614. size_t flushlen0, flushlen1;
  615. /* assert_buf_ok(buf); */
  616. tor_assert(tls);
  617. tor_assert(buf_flushlen);
  618. tor_assert(*buf_flushlen <= buf->datalen);
  619. tor_assert(sz <= *buf_flushlen);
  620. /* we want to let tls write even if flushlen is zero, because it might
  621. * have a partial record pending */
  622. check_no_tls_errors();
  623. flushlen0 = sz;
  624. _split_range(buf, buf->cur, &flushlen0, &flushlen1);
  625. if (flushlen1) {
  626. size_t forced = tor_tls_get_forced_write_size(tls);
  627. tor_assert(forced <= flushlen0);
  628. }
  629. r = flush_buf_tls_impl(tls, buf, flushlen0, buf_flushlen);
  630. check();
  631. if (r < 0 || (size_t)r < flushlen0)
  632. return r; /* Error, or can't flush any more now. */
  633. flushed = r;
  634. if (flushlen1) {
  635. tor_assert(buf->cur == buf->mem);
  636. r = flush_buf_tls_impl(tls, buf, flushlen1, buf_flushlen);
  637. check();
  638. if (r<0)
  639. return r;
  640. flushed += r;
  641. }
  642. return flushed;
  643. }
  644. /** Append <b>string_len</b> bytes from <b>string</b> to the end of
  645. * <b>buf</b>.
  646. *
  647. * Return the new length of the buffer on success, -1 on failure.
  648. */
  649. int
  650. write_to_buf(const char *string, size_t string_len, buf_t *buf)
  651. {
  652. char *next;
  653. size_t len2;
  654. /* append string to buf (growing as needed, return -1 if "too big")
  655. * return total number of bytes on the buf
  656. */
  657. tor_assert(string);
  658. /* assert_buf_ok(buf); */
  659. if (buf_ensure_capacity(buf, buf->datalen+string_len)) {
  660. log_warn(LD_MM, "buflen too small, can't hold %d bytes.",
  661. (int)(buf->datalen+string_len));
  662. return -1;
  663. }
  664. next = _buf_end(buf);
  665. _split_range(buf, next, &string_len, &len2);
  666. memcpy(next, string, string_len);
  667. buf->datalen += string_len;
  668. buf_total_used += string_len;
  669. if (len2) {
  670. tor_assert(_buf_end(buf) == buf->mem);
  671. memcpy(buf->mem, string+string_len, len2);
  672. buf->datalen += len2;
  673. buf_total_used += len2;
  674. }
  675. if (buf->datalen > buf->highwater)
  676. buf->highwater = buf->datalen;
  677. log_debug(LD_NET,"added %d bytes to buf (now %d total).",
  678. (int)string_len, (int)buf->datalen);
  679. check();
  680. return buf->datalen;
  681. }
  682. /** Helper: copy the first <b>string_len</b> bytes from <b>buf</b>
  683. * onto <b>string</b>.
  684. */
  685. static INLINE void
  686. peek_from_buf(char *string, size_t string_len, buf_t *buf)
  687. {
  688. size_t len2;
  689. /* There must be string_len bytes in buf; write them onto string,
  690. * then memmove buf back (that is, remove them from buf).
  691. *
  692. * Return the number of bytes still on the buffer. */
  693. tor_assert(string);
  694. /* make sure we don't ask for too much */
  695. tor_assert(string_len <= buf->datalen);
  696. /* assert_buf_ok(buf); */
  697. _split_range(buf, buf->cur, &string_len, &len2);
  698. memcpy(string, buf->cur, string_len);
  699. if (len2) {
  700. memcpy(string+string_len,buf->mem,len2);
  701. }
  702. }
  703. /** Remove <b>string_len</b> bytes from the front of <b>buf</b>, and store
  704. * them into <b>string</b>. Return the new buffer size. <b>string_len</b>
  705. * must be \<= the number of bytes on the buffer.
  706. */
  707. int
  708. fetch_from_buf(char *string, size_t string_len, buf_t *buf)
  709. {
  710. /* There must be string_len bytes in buf; write them onto string,
  711. * then memmove buf back (that is, remove them from buf).
  712. *
  713. * Return the number of bytes still on the buffer. */
  714. check();
  715. peek_from_buf(string, string_len, buf);
  716. buf_remove_from_front(buf, string_len);
  717. check();
  718. return buf->datalen;
  719. }
  720. /** There is a (possibly incomplete) http statement on <b>buf</b>, of the
  721. * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain nuls.)
  722. * If a) the headers include a Content-Length field and all bytes in
  723. * the body are present, or b) there's no Content-Length field and
  724. * all headers are present, then:
  725. *
  726. * - strdup headers into <b>*headers_out</b>, and nul-terminate it.
  727. * - memdup body into <b>*body_out</b>, and nul-terminate it.
  728. * - Then remove them from <b>buf</b>, and return 1.
  729. *
  730. * - If headers or body is NULL, discard that part of the buf.
  731. * - If a headers or body doesn't fit in the arg, return -1.
  732. * (We ensure that the headers or body don't exceed max len,
  733. * _even if_ we're planning to discard them.)
  734. * - If force_complete is true, then succeed even if not all of the
  735. * content has arrived.
  736. *
  737. * Else, change nothing and return 0.
  738. */
  739. int
  740. fetch_from_buf_http(buf_t *buf,
  741. char **headers_out, size_t max_headerlen,
  742. char **body_out, size_t *body_used, size_t max_bodylen,
  743. int force_complete)
  744. {
  745. char *headers, *body, *p;
  746. size_t headerlen, bodylen, contentlen;
  747. /* assert_buf_ok(buf); */
  748. buf_normalize(buf);
  749. if (buf_nul_terminate(buf)<0) {
  750. log_warn(LD_BUG,"Couldn't nul-terminate buffer");
  751. return -1;
  752. }
  753. headers = buf->cur;
  754. body = strstr(headers,"\r\n\r\n");
  755. if (!body) {
  756. log_debug(LD_HTTP,"headers not all here yet.");
  757. return 0;
  758. }
  759. body += 4; /* Skip the the CRLFCRLF */
  760. headerlen = body-headers; /* includes the CRLFCRLF */
  761. bodylen = buf->datalen - headerlen;
  762. log_debug(LD_HTTP,"headerlen %d, bodylen %d.", (int)headerlen, (int)bodylen);
  763. if (max_headerlen <= headerlen) {
  764. log_warn(LD_HTTP,"headerlen %d larger than %d. Failing.",
  765. (int)headerlen, (int)max_headerlen-1);
  766. return -1;
  767. }
  768. if (max_bodylen <= bodylen) {
  769. log_warn(LD_HTTP,"bodylen %d larger than %d. Failing.",
  770. (int)bodylen, (int)max_bodylen-1);
  771. return -1;
  772. }
  773. #define CONTENT_LENGTH "\r\nContent-Length: "
  774. p = strstr(headers, CONTENT_LENGTH);
  775. if (p) {
  776. int i;
  777. i = atoi(p+strlen(CONTENT_LENGTH));
  778. if (i < 0) {
  779. log_warn(LD_PROTOCOL, "Content-Length is less than zero; it looks like "
  780. "someone is trying to crash us.");
  781. return -1;
  782. }
  783. contentlen = i;
  784. /* if content-length is malformed, then our body length is 0. fine. */
  785. log_debug(LD_HTTP,"Got a contentlen of %d.",(int)contentlen);
  786. if (bodylen < contentlen) {
  787. if (!force_complete) {
  788. log_debug(LD_HTTP,"body not all here yet.");
  789. return 0; /* not all there yet */
  790. }
  791. }
  792. if (bodylen > contentlen) {
  793. bodylen = contentlen;
  794. log_debug(LD_HTTP,"bodylen reduced to %d.",(int)bodylen);
  795. }
  796. }
  797. /* all happy. copy into the appropriate places, and return 1 */
  798. if (headers_out) {
  799. *headers_out = tor_malloc(headerlen+1);
  800. memcpy(*headers_out,buf->cur,headerlen);
  801. (*headers_out)[headerlen] = 0; /* nul terminate it */
  802. }
  803. if (body_out) {
  804. tor_assert(body_used);
  805. *body_used = bodylen;
  806. *body_out = tor_malloc(bodylen+1);
  807. memcpy(*body_out,buf->cur+headerlen,bodylen);
  808. (*body_out)[bodylen] = 0; /* nul terminate it */
  809. }
  810. buf_remove_from_front(buf, headerlen+bodylen);
  811. return 1;
  812. }
  813. /** There is a (possibly incomplete) socks handshake on <b>buf</b>, of one
  814. * of the forms
  815. * - socks4: "socksheader username\\0"
  816. * - socks4a: "socksheader username\\0 destaddr\\0"
  817. * - socks5 phase one: "version #methods methods"
  818. * - socks5 phase two: "version command 0 addresstype..."
  819. * If it's a complete and valid handshake, and destaddr fits in
  820. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  821. * assign to <b>req</b>, and return 1.
  822. *
  823. * If it's invalid or too big, return -1.
  824. *
  825. * Else it's not all there yet, leave buf alone and return 0.
  826. *
  827. * If you want to specify the socks reply, write it into <b>req->reply</b>
  828. * and set <b>req->replylen</b>, else leave <b>req->replylen</b> alone.
  829. *
  830. * If <b>log_sockstype</b> is non-zero, then do a notice-level log of whether
  831. * the connection is possibly leaking DNS requests locally or not.
  832. *
  833. * If <b>safe_socks</b> is true, then reject unsafe socks protocols.
  834. *
  835. * If returning 0 or -1, <b>req->address</b> and <b>req->port</b> are
  836. * undefined.
  837. */
  838. int
  839. fetch_from_buf_socks(buf_t *buf, socks_request_t *req,
  840. int log_sockstype, int safe_socks)
  841. {
  842. unsigned char len;
  843. char tmpbuf[INET_NTOA_BUF_LEN];
  844. uint32_t destip;
  845. enum {socks4, socks4a} socks4_prot = socks4a;
  846. char *next, *startaddr;
  847. struct in_addr in;
  848. /* If the user connects with socks4 or the wrong variant of socks5,
  849. * then log a warning to let him know that it might be unwise. */
  850. static int have_warned_about_unsafe_socks = 0;
  851. if (buf->datalen < 2) /* version and another byte */
  852. return 0;
  853. buf_normalize(buf);
  854. switch (*(buf->cur)) { /* which version of socks? */
  855. case 5: /* socks5 */
  856. if (req->socks_version != 5) { /* we need to negotiate a method */
  857. unsigned char nummethods = (unsigned char)*(buf->cur+1);
  858. tor_assert(!req->socks_version);
  859. if (buf->datalen < 2u+nummethods)
  860. return 0;
  861. if (!nummethods || !memchr(buf->cur+2, 0, nummethods)) {
  862. log_warn(LD_APP,
  863. "socks5: offered methods don't include 'no auth'. "
  864. "Rejecting.");
  865. req->replylen = 2; /* 2 bytes of response */
  866. req->reply[0] = 5;
  867. req->reply[1] = '\xFF'; /* reject all methods */
  868. return -1;
  869. }
  870. /* remove packet from buf. also remove any other extraneous
  871. * bytes, to support broken socks clients. */
  872. buf_clear(buf);
  873. req->replylen = 2; /* 2 bytes of response */
  874. req->reply[0] = 5; /* socks5 reply */
  875. req->reply[1] = SOCKS5_SUCCEEDED;
  876. req->socks_version = 5; /* remember we've already negotiated auth */
  877. log_debug(LD_APP,"socks5: accepted method 0");
  878. return 0;
  879. }
  880. /* we know the method; read in the request */
  881. log_debug(LD_APP,"socks5: checking request");
  882. if (buf->datalen < 8) /* basic info plus >=2 for addr plus 2 for port */
  883. return 0; /* not yet */
  884. req->command = (unsigned char) *(buf->cur+1);
  885. if (req->command != SOCKS_COMMAND_CONNECT &&
  886. req->command != SOCKS_COMMAND_CONNECT_DIR &&
  887. req->command != SOCKS_COMMAND_RESOLVE &&
  888. req->command != SOCKS_COMMAND_RESOLVE_PTR) {
  889. /* not a connect or resolve or a resolve_ptr? we don't support it. */
  890. log_warn(LD_APP,"socks5: command %d not recognized. Rejecting.",
  891. req->command);
  892. return -1;
  893. }
  894. switch (*(buf->cur+3)) { /* address type */
  895. case 1: /* IPv4 address */
  896. log_debug(LD_APP,"socks5: ipv4 address type");
  897. if (buf->datalen < 10) /* ip/port there? */
  898. return 0; /* not yet */
  899. destip = ntohl(*(uint32_t*)(buf->cur+4));
  900. in.s_addr = htonl(destip);
  901. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  902. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  903. log_warn(LD_APP,
  904. "socks5 IP takes %d bytes, which doesn't fit in %d. "
  905. "Rejecting.",
  906. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  907. return -1;
  908. }
  909. strlcpy(req->address,tmpbuf,sizeof(req->address));
  910. req->port = ntohs(*(uint16_t*)(buf->cur+8));
  911. buf_remove_from_front(buf, 10);
  912. if (req->command != SOCKS_COMMAND_RESOLVE_PTR &&
  913. !addressmap_have_mapping(req->address) &&
  914. !have_warned_about_unsafe_socks) {
  915. log_warn(LD_APP,
  916. "Your application (using socks5 on port %d) is giving "
  917. "Tor only an IP address. Applications that do DNS resolves "
  918. "themselves may leak information. Consider using Socks4A "
  919. "(e.g. via privoxy or socat) instead. For more information, "
  920. "please see http://wiki.noreply.org/noreply/TheOnionRouter/"
  921. "TorFAQ#SOCKSAndDNS.%s", req->port,
  922. safe_socks ? " Rejecting." : "");
  923. // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
  924. control_event_client_status(LOG_WARN,
  925. "DANGEROUS_SOCKS PROTOCOL=SOCKS5 ADDRESS=%s:%d",
  926. req->address, req->port);
  927. if (safe_socks)
  928. return -1;
  929. }
  930. return 1;
  931. case 3: /* fqdn */
  932. log_debug(LD_APP,"socks5: fqdn address type");
  933. len = (unsigned char)*(buf->cur+4);
  934. if (buf->datalen < 7u+len) /* addr/port there? */
  935. return 0; /* not yet */
  936. if (len+1 > MAX_SOCKS_ADDR_LEN) {
  937. log_warn(LD_APP,
  938. "socks5 hostname is %d bytes, which doesn't fit in "
  939. "%d. Rejecting.", len+1,MAX_SOCKS_ADDR_LEN);
  940. return -1;
  941. }
  942. if (req->command == SOCKS_COMMAND_RESOLVE_PTR) {
  943. log_warn(LD_APP, "socks5 received RESOLVE_PTR command with "
  944. "hostname type. Rejecting.");
  945. return -1;
  946. }
  947. memcpy(req->address,buf->cur+5,len);
  948. req->address[len] = 0;
  949. req->port = ntohs(get_uint16(buf->cur+5+len));
  950. buf_remove_from_front(buf, 5+len+2);
  951. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  952. log_warn(LD_PROTOCOL,
  953. "Your application (using socks5 on port %d) gave Tor "
  954. "a malformed hostname: %s. Rejecting the connection.",
  955. req->port, escaped(req->address));
  956. return -1;
  957. }
  958. if (log_sockstype)
  959. log_notice(LD_APP,
  960. "Your application (using socks5 on port %d) gave "
  961. "Tor a hostname, which means Tor will do the DNS resolve "
  962. "for you. This is good.", req->port);
  963. return 1;
  964. default: /* unsupported */
  965. log_warn(LD_APP,"socks5: unsupported address type %d. Rejecting.",
  966. *(buf->cur+3));
  967. return -1;
  968. }
  969. tor_assert(0);
  970. case 4: /* socks4 */
  971. /* http://archive.socks.permeo.com/protocol/socks4.protocol */
  972. /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
  973. req->socks_version = 4;
  974. if (buf->datalen < SOCKS4_NETWORK_LEN) /* basic info available? */
  975. return 0; /* not yet */
  976. req->command = (unsigned char) *(buf->cur+1);
  977. if (req->command != SOCKS_COMMAND_CONNECT &&
  978. req->command != SOCKS_COMMAND_CONNECT_DIR &&
  979. req->command != SOCKS_COMMAND_RESOLVE) {
  980. /* not a connect or resolve? we don't support it. (No resolve_ptr with
  981. * socks4.) */
  982. log_warn(LD_APP,"socks4: command %d not recognized. Rejecting.",
  983. req->command);
  984. return -1;
  985. }
  986. req->port = ntohs(*(uint16_t*)(buf->cur+2));
  987. destip = ntohl(*(uint32_t*)(buf->mem+4));
  988. if ((!req->port && req->command!=SOCKS_COMMAND_RESOLVE) || !destip) {
  989. log_warn(LD_APP,"socks4: Port or DestIP is zero. Rejecting.");
  990. return -1;
  991. }
  992. if (destip >> 8) {
  993. log_debug(LD_APP,"socks4: destip not in form 0.0.0.x.");
  994. in.s_addr = htonl(destip);
  995. tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  996. if (strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  997. log_debug(LD_APP,"socks4 addr (%d bytes) too long. Rejecting.",
  998. (int)strlen(tmpbuf));
  999. return -1;
  1000. }
  1001. log_debug(LD_APP,
  1002. "socks4: successfully read destip (%s)", safe_str(tmpbuf));
  1003. socks4_prot = socks4;
  1004. }
  1005. next = memchr(buf->cur+SOCKS4_NETWORK_LEN, 0,
  1006. buf->datalen-SOCKS4_NETWORK_LEN);
  1007. if (!next) {
  1008. log_debug(LD_APP,"socks4: Username not here yet.");
  1009. return 0;
  1010. }
  1011. tor_assert(next < buf->cur+buf->datalen);
  1012. startaddr = NULL;
  1013. if (socks4_prot != socks4a &&
  1014. !addressmap_have_mapping(tmpbuf) &&
  1015. !have_warned_about_unsafe_socks) {
  1016. log_warn(LD_APP,
  1017. "Your application (using socks4 on port %d) is giving Tor "
  1018. "only an IP address. Applications that do DNS resolves "
  1019. "themselves may leak information. Consider using Socks4A "
  1020. "(e.g. via privoxy or socat) instead. For more information, "
  1021. "please see http://wiki.noreply.org/noreply/TheOnionRouter/"
  1022. "TorFAQ#SOCKSAndDNS.%s", req->port,
  1023. safe_socks ? " Rejecting." : "");
  1024. // have_warned_about_unsafe_socks = 1; // (for now, warn every time)
  1025. control_event_client_status(LOG_WARN,
  1026. "DANGEROUS_SOCKS PROTOCOL=SOCKS4 ADDRESS=%s:%d",
  1027. tmpbuf, req->port);
  1028. if (safe_socks)
  1029. return -1;
  1030. }
  1031. if (socks4_prot == socks4a) {
  1032. if (next+1 == buf->cur+buf->datalen) {
  1033. log_debug(LD_APP,"socks4: No part of destaddr here yet.");
  1034. return 0;
  1035. }
  1036. startaddr = next+1;
  1037. next = memchr(startaddr, 0, buf->cur+buf->datalen-startaddr);
  1038. if (!next) {
  1039. log_debug(LD_APP,"socks4: Destaddr not all here yet.");
  1040. return 0;
  1041. }
  1042. if (MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  1043. log_warn(LD_APP,"socks4: Destaddr too long. Rejecting.");
  1044. return -1;
  1045. }
  1046. tor_assert(next < buf->cur+buf->datalen);
  1047. if (log_sockstype)
  1048. log_notice(LD_APP,
  1049. "Your application (using socks4a on port %d) gave "
  1050. "Tor a hostname, which means Tor will do the DNS resolve "
  1051. "for you. This is good.", req->port);
  1052. }
  1053. log_debug(LD_APP,"socks4: Everything is here. Success.");
  1054. strlcpy(req->address, startaddr ? startaddr : tmpbuf,
  1055. sizeof(req->address));
  1056. if (!tor_strisprint(req->address) || strchr(req->address,'\"')) {
  1057. log_warn(LD_PROTOCOL,
  1058. "Your application (using socks4 on port %d) gave Tor "
  1059. "a malformed hostname: %s. Rejecting the connection.",
  1060. req->port, escaped(req->address));
  1061. return -1;
  1062. }
  1063. /* next points to the final \0 on inbuf */
  1064. buf_remove_from_front(buf, next-buf->cur+1);
  1065. return 1;
  1066. case 'G': /* get */
  1067. case 'H': /* head */
  1068. case 'P': /* put/post */
  1069. case 'C': /* connect */
  1070. strlcpy(req->reply,
  1071. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  1072. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  1073. "<html>\n"
  1074. "<head>\n"
  1075. "<title>Tor is not an HTTP Proxy</title>\n"
  1076. "</head>\n"
  1077. "<body>\n"
  1078. "<h1>Tor is not an HTTP Proxy</h1>\n"
  1079. "<p>\n"
  1080. "It appears you have configured your web browser to use Tor as an HTTP proxy."
  1081. "\n"
  1082. "This is not correct: Tor is a SOCKS proxy, not an HTTP proxy.\n"
  1083. "Please configure your client accordingly.\n"
  1084. "</p>\n"
  1085. "<p>\n"
  1086. "See <a href=\"http://tor.eff.org/documentation.html\">"
  1087. "http://tor.eff.org/documentation.html</a> for more information.\n"
  1088. "<!-- Plus this comment, to make the body response more than 512 bytes, so "
  1089. " IE will be willing to display it. Comment comment comment comment "
  1090. " comment comment comment comment comment comment comment comment.-->\n"
  1091. "</p>\n"
  1092. "</body>\n"
  1093. "</html>\n"
  1094. , MAX_SOCKS_REPLY_LEN);
  1095. req->replylen = strlen(req->reply)+1;
  1096. /* fall through */
  1097. default: /* version is not socks4 or socks5 */
  1098. log_warn(LD_APP,
  1099. "Socks version %d not recognized. (Tor is not an http proxy.)",
  1100. *(buf->cur));
  1101. {
  1102. char *tmp = tor_strndup(buf->cur, 8);
  1103. control_event_client_status(LOG_WARN,
  1104. "SOCKS_UNKNOWN_PROTOCOL DATA=\"%s\"",
  1105. escaped(tmp));
  1106. tor_free(tmp);
  1107. }
  1108. return -1;
  1109. }
  1110. }
  1111. /** If there is a complete version 0 control message waiting on buf, then store
  1112. * its contents into *<b>type_out</b>, store its body's length into
  1113. * *<b>len_out</b>, allocate and store a string for its body into
  1114. * *<b>body_out</b>, and return 1. (body_out will always be NUL-terminated,
  1115. * even if the control message body doesn't end with NUL.)
  1116. *
  1117. * If there is not a complete control message waiting, return 0.
  1118. *
  1119. * Return -1 on error; return -2 on "seems to be control protocol v1."
  1120. */
  1121. int
  1122. fetch_from_buf_control0(buf_t *buf, uint32_t *len_out, uint16_t *type_out,
  1123. char **body_out, int check_for_v1)
  1124. {
  1125. uint32_t msglen;
  1126. uint16_t type;
  1127. char tmp[4];
  1128. tor_assert(buf);
  1129. tor_assert(len_out);
  1130. tor_assert(type_out);
  1131. tor_assert(body_out);
  1132. *len_out = 0;
  1133. *body_out = NULL;
  1134. if (buf->datalen < 4)
  1135. return 0;
  1136. peek_from_buf(tmp, 4, buf);
  1137. msglen = ntohs(get_uint16(tmp));
  1138. type = ntohs(get_uint16(tmp+2));
  1139. if (type > 255 && check_for_v1)
  1140. return -2;
  1141. if (buf->datalen < 4 + (unsigned)msglen)
  1142. return 0;
  1143. *len_out = msglen;
  1144. *type_out = type;
  1145. buf_remove_from_front(buf, 4);
  1146. if (msglen) {
  1147. *body_out = tor_malloc(msglen+1);
  1148. fetch_from_buf(*body_out, msglen, buf);
  1149. (*body_out)[msglen] = '\0';
  1150. }
  1151. return 1;
  1152. }
  1153. /** Helper: return a pointer to the first instance of <b>c</b> in the
  1154. * <b>len</b>characters after <b>start</b> on <b>buf</b>. Return NULL if the
  1155. * character isn't found. */
  1156. static char *
  1157. find_char_on_buf(buf_t *buf, char *start, size_t len, char c)
  1158. {
  1159. size_t len_rest;
  1160. char *cp;
  1161. _split_range(buf, start, &len, &len_rest);
  1162. cp = memchr(start, c, len);
  1163. if (cp || !len_rest)
  1164. return cp;
  1165. return memchr(buf->mem, c, len_rest);
  1166. }
  1167. /** Helper: return a pointer to the first CRLF after cp on <b>buf</b>. Return
  1168. * NULL if no CRLF is found. */
  1169. static char *
  1170. find_crlf_on_buf(buf_t *buf, char *cp)
  1171. {
  1172. char *next;
  1173. while (1) {
  1174. size_t remaining = buf->datalen - _buf_offset(buf,cp);
  1175. cp = find_char_on_buf(buf, cp, remaining, '\r');
  1176. if (!cp)
  1177. return NULL;
  1178. next = _wrap_ptr(buf, cp+1);
  1179. if (next == _buf_end(buf))
  1180. return NULL;
  1181. if (*next == '\n')
  1182. return cp;
  1183. cp = next;
  1184. }
  1185. }
  1186. /** Try to read a single CRLF-terminated line from <b>buf</b>, and write it,
  1187. * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
  1188. * Set *<b>data_len</b> to the number of bytes in the line, not counting the
  1189. * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
  1190. * have a whole line yet, and return -1 if we we need to grow the buffer.
  1191. */
  1192. int
  1193. fetch_from_buf_line(buf_t *buf, char *data_out, size_t *data_len)
  1194. {
  1195. char *eol;
  1196. size_t sz;
  1197. /* Look for a CRLF. */
  1198. if (!(eol = find_crlf_on_buf(buf, buf->cur))) {
  1199. return 0;
  1200. }
  1201. sz = _buf_offset(buf, eol);
  1202. if (sz+3 > *data_len) {
  1203. *data_len = sz+3;
  1204. return -1;
  1205. }
  1206. fetch_from_buf(data_out, sz+2, buf);
  1207. data_out[sz+2] = '\0';
  1208. *data_len = sz+2;
  1209. return 1;
  1210. }
  1211. /** Try to read a single LF-terminated line from <b>buf</b>, and write it,
  1212. * NUL-terminated, into the *<b>data_len</b> byte buffer at <b>data_out</b>.
  1213. * Set *<b>data_len</b> to the number of bytes in the line, not counting the
  1214. * terminating NUL. Return 1 if we read a whole line, return 0 if we don't
  1215. * have a whole line yet, and return -1 if the line length exceeds
  1216. *<b>data_len</b>.
  1217. */
  1218. int
  1219. fetch_from_buf_line_lf(buf_t *buf, char *data_out, size_t *data_len)
  1220. {
  1221. char *cp;
  1222. size_t sz;
  1223. size_t remaining = buf->datalen - _buf_offset(buf,buf->cur);
  1224. cp = find_char_on_buf(buf, buf->cur, remaining, '\n');
  1225. if (!cp)
  1226. return 0;
  1227. sz = _buf_offset(buf, cp);
  1228. if (sz+2 > *data_len) {
  1229. *data_len = sz+2;
  1230. return -1;
  1231. }
  1232. fetch_from_buf(data_out, sz+1, buf);
  1233. data_out[sz+1] = '\0';
  1234. *data_len = sz+1;
  1235. return 1;
  1236. }
  1237. /** Compress on uncompress the <b>data_len</b> bytes in <b>data</b> using the
  1238. * zlib state <b>state</b>, appending the result to <b>buf</b>. If
  1239. * <b>done</b> is true, flush the data in the state and finish the
  1240. * compression/uncompression. Return -1 on failure, 0 on success. */
  1241. int
  1242. write_to_buf_zlib(buf_t *buf, tor_zlib_state_t *state,
  1243. const char *data, size_t data_len,
  1244. int done)
  1245. {
  1246. char *next;
  1247. size_t old_avail, avail;
  1248. int over = 0;
  1249. do {
  1250. buf_ensure_capacity(buf, buf->datalen + 1024);
  1251. next = _buf_end(buf);
  1252. if (next < buf->cur)
  1253. old_avail = avail = buf->cur - next;
  1254. else
  1255. old_avail = avail = (buf->mem + buf->len) - next;
  1256. switch (tor_zlib_process(state, &next, &avail, &data, &data_len, done)) {
  1257. case TOR_ZLIB_DONE:
  1258. over = 1;
  1259. break;
  1260. case TOR_ZLIB_ERR:
  1261. return -1;
  1262. case TOR_ZLIB_OK:
  1263. if (data_len == 0)
  1264. over = 1;
  1265. break;
  1266. case TOR_ZLIB_BUF_FULL:
  1267. if (avail && buf->len >= 1024 + buf->datalen) {
  1268. /* Zlib says we need more room (ZLIB_BUF_FULL), and we're not about
  1269. * to wrap around (avail != 0), and resizing won't actually make us
  1270. * un-full: we're at the end of the buffer, and zlib refuses to
  1271. * append more here, but there's a pile of free space at the start
  1272. * of the buffer (about 1K). So chop a few characters off the
  1273. * end of the buffer. This feels silly; anybody got a better hack?
  1274. *
  1275. * (We don't just want to expand the buffer nevertheless. Consider a
  1276. * 1/3 full buffer with a single byte free at the end. zlib will
  1277. * often refuse to append to that, and so we want to use the
  1278. * beginning, not double the buffer to be just 1/6 full.)
  1279. */
  1280. tor_assert(next >= buf->cur);
  1281. buf->len -= avail;
  1282. }
  1283. break;
  1284. }
  1285. buf->datalen += old_avail - avail;
  1286. if (buf->datalen > buf->highwater)
  1287. buf->highwater = buf->datalen;
  1288. buf_total_used += old_avail - avail;
  1289. } while (!over);
  1290. return 0;
  1291. }
  1292. /** Log an error and exit if <b>buf</b> is corrupted.
  1293. */
  1294. void
  1295. assert_buf_ok(buf_t *buf)
  1296. {
  1297. tor_assert(buf);
  1298. tor_assert(buf->magic == BUFFER_MAGIC);
  1299. tor_assert(buf->mem);
  1300. tor_assert(buf->highwater <= buf->len);
  1301. tor_assert(buf->datalen <= buf->highwater);
  1302. #ifdef SENTINELS
  1303. {
  1304. uint32_t u32 = get_uint32(buf->mem - 4);
  1305. tor_assert(u32 == START_MAGIC);
  1306. u32 = get_uint32(buf->mem + buf->memsize);
  1307. tor_assert(u32 == END_MAGIC);
  1308. }
  1309. #endif
  1310. }