buffers.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file buffers.c
  6. * \brief Abstractions for buffered IO.
  7. **/
  8. #include "or.h"
  9. #define BUFFER_MAGIC 0xB0FFF312u
  10. struct buf_t {
  11. uint32_t magic; /**< Magic cookie for debugging: Must be set to BUFFER_MAGIC */
  12. char *mem; /**< Storage for data in the buffer */
  13. size_t len; /**< Maximum amount of data that 'mem' can hold. */
  14. size_t datalen; /**< Number of bytes currently in 'mem'. */
  15. };
  16. /** Size, in bytes, for newly allocated buffers. Should be a power of 2. */
  17. #define INITIAL_BUF_SIZE (4*1024)
  18. /** Maximum size, in bytes, for resized buffers. */
  19. #define MAX_BUF_SIZE (1024*1024*10)
  20. /** Size, in bytes, for minimum 'shrink' size for buffers. Buffers may start
  21. * out smaller than this, but they will never autoshrink to less
  22. * than this size. */
  23. #define MIN_BUF_SHRINK_SIZE (16*1024)
  24. /** Change a buffer's capacity. new_capacity must be \<= buf->datalen. */
  25. static INLINE void buf_resize(buf_t *buf, size_t new_capacity)
  26. {
  27. tor_assert(buf->datalen <= new_capacity);
  28. tor_assert(new_capacity);
  29. buf->mem = tor_realloc(buf->mem, new_capacity);
  30. buf->len = new_capacity;
  31. }
  32. /** If the buffer is not large enough to hold "capacity" bytes, resize
  33. * it so that it can. (The new size will be a power of 2 times the old
  34. * size.)
  35. */
  36. static INLINE int buf_ensure_capacity(buf_t *buf, size_t capacity)
  37. {
  38. size_t new_len;
  39. if (buf->len >= capacity) /* Don't grow if we're already big enough. */
  40. return 0;
  41. if (capacity > MAX_BUF_SIZE) /* Don't grow past the maximum. */
  42. return -1;
  43. /* Find the smallest new_len equal to (2**X)*len for some X; such that
  44. * new_len is at least capacity.
  45. */
  46. new_len = buf->len*2;
  47. while (new_len < capacity)
  48. new_len *= 2;
  49. /* Resize the buffer. */
  50. log_fn(LOG_DEBUG,"Growing buffer from %d to %d bytes.",
  51. (int)buf->len, (int)new_len);
  52. buf_resize(buf,new_len);
  53. return 0;
  54. }
  55. /** If the buffer is at least 2*MIN_BUF_SHRINK_SIZE bytes in capacity,
  56. * and if the buffer is less than 1/4 full, shrink the buffer until
  57. * one of the above no longer holds. (We shrink the buffer by
  58. * dividing by powers of 2.)
  59. */
  60. static INLINE void buf_shrink_if_underfull(buf_t *buf) {
  61. size_t new_len;
  62. /* If the buffer is at least .25 full, or if shrinking the buffer would
  63. * put it onder MIN_BUF_SHRINK_SIZE, don't do it. */
  64. if (buf->datalen >= buf->len/4 || buf->len < 2*MIN_BUF_SHRINK_SIZE)
  65. return;
  66. /* Shrink new_len by powers of 2 until: datalen is at least 1/4 of
  67. * new_len, OR shrinking new_len more would put it under
  68. * MIN_BUF_SHRINK_SIZE.
  69. */
  70. new_len = buf->len / 2;
  71. while (buf->datalen < new_len/4 && new_len/2 > MIN_BUF_SHRINK_SIZE)
  72. new_len /= 2;
  73. log_fn(LOG_DEBUG,"Shrinking buffer from %d to %d bytes.",
  74. (int)buf->len, (int)new_len);
  75. buf_resize(buf, new_len);
  76. }
  77. /** Remove the first 'n' bytes from buf.
  78. */
  79. static INLINE void buf_remove_from_front(buf_t *buf, size_t n) {
  80. tor_assert(buf->datalen >= n);
  81. buf->datalen -= n;
  82. memmove(buf->mem, buf->mem+n, buf->datalen);
  83. buf_shrink_if_underfull(buf);
  84. }
  85. /** Find the first instance of the str_len byte string 'sr' on the
  86. * buf_len byte string 'bufstr'. Strings are not necessary
  87. * NUL-terminated. If none exists, return -1. Otherwise, return index
  88. * of the first character in bufstr _after_ the first instance of str.
  89. */
  90. /* XXXX The way this function is used, we could always get away with
  91. * XXXX assuming that str is NUL terminated, and use strstr instead. */
  92. static int find_mem_in_mem(const char *str, int str_len,
  93. const char *bufstr, int buf_len)
  94. {
  95. const char *location;
  96. const char *last_possible = bufstr + buf_len - str_len;
  97. tor_assert(str && str_len > 0 && bufstr);
  98. if(buf_len < str_len)
  99. return -1;
  100. for(location = bufstr; location <= last_possible; location++)
  101. if((*location == *str) && !memcmp(location+1, str+1, str_len-1))
  102. return location-bufstr+str_len;
  103. return -1;
  104. }
  105. /** Create and return a new buf with capacity 'size'.
  106. */
  107. buf_t *buf_new_with_capacity(size_t size) {
  108. buf_t *buf;
  109. buf = tor_malloc(sizeof(buf_t));
  110. buf->magic = BUFFER_MAGIC;
  111. buf->mem = tor_malloc(size);
  112. buf->len = size;
  113. buf->datalen = 0;
  114. // memset(buf->mem,0,size);
  115. assert_buf_ok(buf);
  116. return buf;
  117. }
  118. /** Allocate and return a new buffer with default capacity. */
  119. buf_t *buf_new()
  120. {
  121. return buf_new_with_capacity(INITIAL_BUF_SIZE);
  122. }
  123. /** Remove all data from 'buf' */
  124. void buf_clear(buf_t *buf)
  125. {
  126. buf->datalen = 0;
  127. }
  128. /** Return the number of bytes stored in 'buf' */
  129. size_t buf_datalen(const buf_t *buf)
  130. {
  131. return buf->datalen;
  132. }
  133. /** Return the maximum bytes that can be stored in 'buf' before buf
  134. * needs to resize. */
  135. size_t buf_capacity(const buf_t *buf)
  136. {
  137. return buf->len;
  138. }
  139. /** For testing only: Return a pointer to the raw memory stored in 'buf'.
  140. */
  141. const char *_buf_peek_raw_buffer(const buf_t *buf)
  142. {
  143. return buf->mem;
  144. }
  145. /** Release storage held by 'buf'.
  146. */
  147. void buf_free(buf_t *buf) {
  148. assert_buf_ok(buf);
  149. buf->magic = 0xDEADBEEF;
  150. tor_free(buf->mem);
  151. tor_free(buf);
  152. }
  153. /** Read from socket s, writing onto end of buf. Read at most
  154. * 'at_most' bytes, resizing the buffer as necessary. If read()
  155. * returns 0, set *reached_eof to 1 and return 0. Return -1 on error;
  156. * else return the number of bytes read. Return 0 if read() would
  157. * block.
  158. */
  159. int read_to_buf(int s, size_t at_most, buf_t *buf, int *reached_eof) {
  160. int read_result;
  161. assert_buf_ok(buf);
  162. tor_assert(reached_eof && (s>=0));
  163. if (buf_ensure_capacity(buf,buf->datalen+at_most))
  164. return -1;
  165. if(at_most + buf->datalen > buf->len)
  166. at_most = buf->len - buf->datalen; /* take the min of the two */
  167. if(at_most == 0)
  168. return 0; /* we shouldn't read anything */
  169. // log_fn(LOG_DEBUG,"reading at most %d bytes.",at_most);
  170. read_result = recv(s, buf->mem+buf->datalen, at_most, 0);
  171. if (read_result < 0) {
  172. if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
  173. return -1;
  174. }
  175. return 0; /* would block. */
  176. } else if (read_result == 0) {
  177. log_fn(LOG_DEBUG,"Encountered eof");
  178. *reached_eof = 1;
  179. return 0;
  180. } else { /* we read some bytes */
  181. buf->datalen += read_result;
  182. log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf.",read_result,
  183. (int)buf->datalen);
  184. return read_result;
  185. }
  186. }
  187. /** As read_to_buf, but reads from a TLS connection.
  188. */
  189. int read_to_buf_tls(tor_tls *tls, size_t at_most, buf_t *buf) {
  190. int r;
  191. tor_assert(tls);
  192. assert_buf_ok(buf);
  193. log_fn(LOG_DEBUG,"start: %d on buf, %d pending, at_most %d.",
  194. (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
  195. (int)at_most);
  196. if (buf_ensure_capacity(buf, at_most+buf->datalen))
  197. return TOR_TLS_ERROR;
  198. if (at_most + buf->datalen > buf->len)
  199. at_most = buf->len - buf->datalen;
  200. if (at_most == 0)
  201. return 0;
  202. log_fn(LOG_DEBUG,"before: %d on buf, %d pending, at_most %d.",
  203. (int)buf_datalen(buf), (int)tor_tls_get_pending_bytes(tls),
  204. (int)at_most);
  205. assert_no_tls_errors();
  206. r = tor_tls_read(tls, buf->mem+buf->datalen, at_most);
  207. if (r<0)
  208. return r;
  209. buf->datalen += r;
  210. log_fn(LOG_DEBUG,"Read %d bytes. %d on inbuf; %d pending",r,
  211. (int)buf->datalen,(int)tor_tls_get_pending_bytes(tls));
  212. return r;
  213. }
  214. /** Write data from 'buf' to the socket 's'. Write at most
  215. * *buf_flushlen bytes, and decrement *buf_flushlen by the number of
  216. * bytes actually written. Return the number of bytes written on
  217. * success, -1 on failure. Return 0 if write() would block.
  218. */
  219. int flush_buf(int s, buf_t *buf, int *buf_flushlen)
  220. {
  221. /* push from buf onto s
  222. * then memmove to front of buf
  223. * return -1 or how many bytes you just flushed */
  224. int write_result;
  225. assert_buf_ok(buf);
  226. tor_assert(buf_flushlen && (s>=0) && ((unsigned)*buf_flushlen <= buf->datalen));
  227. if(*buf_flushlen == 0) /* nothing to flush */
  228. return 0;
  229. write_result = send(s, buf->mem, *buf_flushlen, 0);
  230. if (write_result < 0) {
  231. if(!ERRNO_IS_EAGAIN(tor_socket_errno(s))) { /* it's a real error */
  232. /* get a stack trace to find epipe bugs */
  233. tor_assert(tor_socket_errno(s) != EPIPE);
  234. return -1;
  235. }
  236. log_fn(LOG_DEBUG,"write() would block, returning.");
  237. return 0;
  238. } else {
  239. *buf_flushlen -= write_result;
  240. buf_remove_from_front(buf, write_result);
  241. log_fn(LOG_DEBUG,"%d: flushed %d bytes, %d ready to flush, %d remain.",
  242. s,write_result,*buf_flushlen,(int)buf->datalen);
  243. return write_result;
  244. }
  245. }
  246. /** As flush_buf, but writes data to a TLS connection.
  247. */
  248. int flush_buf_tls(tor_tls *tls, buf_t *buf, int *buf_flushlen)
  249. {
  250. int r;
  251. assert_buf_ok(buf);
  252. tor_assert(tls && buf_flushlen);
  253. /* we want to let tls write even if flushlen is zero, because it might
  254. * have a partial record pending */
  255. r = tor_tls_write(tls, buf->mem, *buf_flushlen);
  256. if (r < 0) {
  257. return r;
  258. }
  259. *buf_flushlen -= r;
  260. buf_remove_from_front(buf, r);
  261. log_fn(LOG_DEBUG,"flushed %d bytes, %d ready to flush, %d remain.",
  262. r,*buf_flushlen,(int)buf->datalen);
  263. return r;
  264. }
  265. /** Append string_len bytes from 'string' to the end of 'buf'.
  266. * Return the new length of the buffer on success, -1 on failure.
  267. */
  268. int write_to_buf(const char *string, int string_len, buf_t *buf) {
  269. /* append string to buf (growing as needed, return -1 if "too big")
  270. * return total number of bytes on the buf
  271. */
  272. tor_assert(string);
  273. assert_buf_ok(buf);
  274. if (buf_ensure_capacity(buf, buf->datalen+string_len)) {
  275. log_fn(LOG_WARN, "buflen too small, can't hold %d bytes.", (int)buf->datalen+string_len);
  276. return -1;
  277. }
  278. memcpy(buf->mem+buf->datalen, string, string_len);
  279. buf->datalen += string_len;
  280. log_fn(LOG_DEBUG,"added %d bytes to buf (now %d total).",string_len, (int)buf->datalen);
  281. return buf->datalen;
  282. }
  283. /* Remove string_len bytes from the front of 'buf', and store them
  284. * into 'string'. Return the new buffer size. string_len must be <=
  285. * the number of bytes on the buffer.
  286. */
  287. int fetch_from_buf(char *string, size_t string_len, buf_t *buf) {
  288. /* There must be string_len bytes in buf; write them onto string,
  289. * then memmove buf back (that is, remove them from buf).
  290. *
  291. * Return the number of bytes still on the buffer. */
  292. tor_assert(string);
  293. tor_assert(string_len <= buf->datalen); /* make sure we don't ask for too much */
  294. assert_buf_ok(buf);
  295. memcpy(string,buf->mem,string_len);
  296. buf_remove_from_front(buf, string_len);
  297. return buf->datalen;
  298. }
  299. /** There is a (possibly incomplete) http statement on *buf, of the
  300. * form "\%s\\r\\n\\r\\n\%s", headers, body. (body may contain nuls.)
  301. * If a) the headers include a Content-Length field and all bytes in
  302. * the body are present, or b) there's no Content-Length field and
  303. * all headers are present, then:
  304. *
  305. * - strdup headers into *headers_out, and nul-terminate it.
  306. * - memdup body into *body_out, and nul-terminate it.
  307. * - Then remove them from buf, and return 1.
  308. *
  309. * - If headers or body is NULL, discard that part of the buf.
  310. * - If a headers or body doesn't fit in the arg, return -1.
  311. *
  312. * Else, change nothing and return 0.
  313. */
  314. int fetch_from_buf_http(buf_t *buf,
  315. char **headers_out, int max_headerlen,
  316. char **body_out, int *body_used, int max_bodylen) {
  317. char *headers, *body;
  318. int i;
  319. int headerlen, bodylen, contentlen;
  320. assert_buf_ok(buf);
  321. headers = buf->mem;
  322. i = find_mem_in_mem("\r\n\r\n", 4, buf->mem, buf->datalen);
  323. if(i < 0) {
  324. log_fn(LOG_DEBUG,"headers not all here yet.");
  325. return 0;
  326. }
  327. body = buf->mem+i;
  328. headerlen = body-headers; /* includes the CRLFCRLF */
  329. bodylen = buf->datalen - headerlen;
  330. log_fn(LOG_DEBUG,"headerlen %d, bodylen %d.", headerlen, bodylen);
  331. if(headers_out && max_headerlen <= headerlen) {
  332. log_fn(LOG_WARN,"headerlen %d larger than %d. Failing.", headerlen, max_headerlen-1);
  333. return -1;
  334. }
  335. if(body_out && max_bodylen <= bodylen) {
  336. log_fn(LOG_WARN,"bodylen %d larger than %d. Failing.", bodylen, max_bodylen-1);
  337. return -1;
  338. }
  339. #define CONTENT_LENGTH "\r\nContent-Length: "
  340. i = find_mem_in_mem(CONTENT_LENGTH, strlen(CONTENT_LENGTH),
  341. headers, headerlen);
  342. if(i > 0) {
  343. contentlen = atoi(headers+i);
  344. /* if content-length is malformed, then our body length is 0. fine. */
  345. log_fn(LOG_DEBUG,"Got a contentlen of %d.",contentlen);
  346. if(bodylen < contentlen) {
  347. log_fn(LOG_DEBUG,"body not all here yet.");
  348. return 0; /* not all there yet */
  349. }
  350. if(bodylen > contentlen) {
  351. bodylen = contentlen;
  352. log_fn(LOG_DEBUG,"bodylen reduced to %d.",bodylen);
  353. }
  354. }
  355. /* all happy. copy into the appropriate places, and return 1 */
  356. if(headers_out) {
  357. *headers_out = tor_malloc(headerlen+1);
  358. memcpy(*headers_out,buf->mem,headerlen);
  359. (*headers_out)[headerlen] = 0; /* null terminate it */
  360. }
  361. if(body_out) {
  362. tor_assert(body_used);
  363. *body_used = bodylen;
  364. *body_out = tor_malloc(bodylen+1);
  365. memcpy(*body_out,buf->mem+headerlen,bodylen);
  366. (*body_out)[bodylen] = 0; /* null terminate it */
  367. }
  368. buf_remove_from_front(buf, headerlen+bodylen);
  369. return 1;
  370. }
  371. /** There is a (possibly incomplete) socks handshake on buf, of one
  372. * of the forms
  373. * - socks4: "socksheader username\\0"
  374. * - socks4a: "socksheader username\\0 destaddr\\0"
  375. * - socks5 phase one: "version #methods methods"
  376. * - socks5 phase two: "version command 0 addresstype..."
  377. * If it's a complete and valid handshake, and destaddr fits in
  378. * MAX_SOCKS_ADDR_LEN bytes, then pull the handshake off the buf,
  379. * assign to req, and return 1.
  380. *
  381. * If it's invalid or too big, return -1.
  382. *
  383. * Else it's not all there yet, leave buf alone and return 0.
  384. *
  385. * If you want to specify the socks reply, write it into req->reply
  386. * and set req->replylen, else leave req->replylen alone.
  387. *
  388. * If returning 0 or -1, req->address and req->port are undefined.
  389. */
  390. int fetch_from_buf_socks(buf_t *buf, socks_request_t *req) {
  391. unsigned char len;
  392. char *tmpbuf=NULL;
  393. uint32_t destip;
  394. enum {socks4, socks4a} socks4_prot = socks4a;
  395. char *next, *startaddr;
  396. struct in_addr in;
  397. if(buf->datalen < 2) /* version and another byte */
  398. return 0;
  399. switch(*(buf->mem)) { /* which version of socks? */
  400. case 5: /* socks5 */
  401. if(req->socks_version != 5) { /* we need to negotiate a method */
  402. unsigned char nummethods = (unsigned char)*(buf->mem+1);
  403. tor_assert(!req->socks_version);
  404. if(buf->datalen < 2u+nummethods)
  405. return 0;
  406. if(!nummethods || !memchr(buf->mem+2, 0, nummethods)) {
  407. log_fn(LOG_WARN,"socks5: offered methods don't include 'no auth'. Rejecting.");
  408. req->replylen = 2; /* 2 bytes of response */
  409. req->reply[0] = 5; /* socks5 reply */
  410. req->reply[1] = '\xFF'; /* reject all methods */
  411. return -1;
  412. }
  413. buf_remove_from_front(buf,2+nummethods);/* remove packet from buf */
  414. req->replylen = 2; /* 2 bytes of response */
  415. req->reply[0] = 5; /* socks5 reply */
  416. req->reply[1] = 0; /* choose the 'no auth' method */
  417. req->socks_version = 5; /* remember that we've already negotiated auth */
  418. log_fn(LOG_DEBUG,"socks5: accepted method 0");
  419. return 0;
  420. }
  421. /* we know the method; read in the request */
  422. log_fn(LOG_DEBUG,"socks5: checking request");
  423. if(buf->datalen < 8) /* basic info plus >=2 for addr plus 2 for port */
  424. return 0; /* not yet */
  425. if(*(buf->mem+1) != 1) { /* not a connect? we don't support it. */
  426. log_fn(LOG_WARN,"socks5: command %d not '1'. Rejecting.",*(buf->mem+1));
  427. return -1;
  428. }
  429. switch(*(buf->mem+3)) { /* address type */
  430. case 1: /* IPv4 address */
  431. log_fn(LOG_DEBUG,"socks5: ipv4 address type");
  432. if(buf->datalen < 10) /* ip/port there? */
  433. return 0; /* not yet */
  434. destip = ntohl(*(uint32_t*)(buf->mem+4));
  435. in.s_addr = htonl(destip);
  436. tmpbuf = inet_ntoa(in);
  437. if(strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  438. log_fn(LOG_WARN,"socks5 IP takes %d bytes, which doesn't fit in %d. Rejecting.",
  439. (int)strlen(tmpbuf)+1,(int)MAX_SOCKS_ADDR_LEN);
  440. return -1;
  441. }
  442. strcpy(req->address,tmpbuf);
  443. req->port = ntohs(*(uint16_t*)(buf->mem+8));
  444. buf_remove_from_front(buf, 10);
  445. return 1;
  446. case 3: /* fqdn */
  447. log_fn(LOG_DEBUG,"socks5: fqdn address type");
  448. len = (unsigned char)*(buf->mem+4);
  449. if(buf->datalen < 7u+len) /* addr/port there? */
  450. return 0; /* not yet */
  451. if(len+1 > MAX_SOCKS_ADDR_LEN) {
  452. log_fn(LOG_WARN,"socks5 hostname is %d bytes, which doesn't fit in %d. Rejecting.",
  453. len+1,MAX_SOCKS_ADDR_LEN);
  454. return -1;
  455. }
  456. memcpy(req->address,buf->mem+5,len);
  457. req->address[len] = 0;
  458. req->port = ntohs(get_uint16(buf->mem+5+len));
  459. buf_remove_from_front(buf, 5+len+2);
  460. return 1;
  461. default: /* unsupported */
  462. log_fn(LOG_WARN,"socks5: unsupported address type %d. Rejecting.",*(buf->mem+3));
  463. return -1;
  464. }
  465. tor_assert(0);
  466. case 4: /* socks4 */
  467. /* http://archive.socks.permeo.com/protocol/socks4.protocol */
  468. /* http://archive.socks.permeo.com/protocol/socks4a.protocol */
  469. req->socks_version = 4;
  470. if(buf->datalen < SOCKS4_NETWORK_LEN) /* basic info available? */
  471. return 0; /* not yet */
  472. if(*(buf->mem+1) != 1) { /* not a connect? we don't support it. */
  473. log_fn(LOG_WARN,"socks4: command %d not '1'. Rejecting.",*(buf->mem+1));
  474. return -1;
  475. }
  476. req->port = ntohs(*(uint16_t*)(buf->mem+2));
  477. destip = ntohl(*(uint32_t*)(buf->mem+4));
  478. if(!req->port || !destip) {
  479. log_fn(LOG_WARN,"socks4: Port or DestIP is zero. Rejecting.");
  480. return -1;
  481. }
  482. if(destip >> 8) {
  483. log_fn(LOG_DEBUG,"socks4: destip not in form 0.0.0.x.");
  484. in.s_addr = htonl(destip);
  485. tmpbuf = inet_ntoa(in);
  486. if(strlen(tmpbuf)+1 > MAX_SOCKS_ADDR_LEN) {
  487. log_fn(LOG_WARN,"socks4 addr (%d bytes) too long. Rejecting.",
  488. (int)strlen(tmpbuf));
  489. return -1;
  490. }
  491. log_fn(LOG_DEBUG,"socks4: successfully read destip (%s)", tmpbuf);
  492. socks4_prot = socks4;
  493. }
  494. next = memchr(buf->mem+SOCKS4_NETWORK_LEN, 0, buf->datalen);
  495. if(!next) {
  496. log_fn(LOG_DEBUG,"socks4: Username not here yet.");
  497. return 0;
  498. }
  499. startaddr = next+1;
  500. if(socks4_prot == socks4a) {
  501. next = memchr(startaddr, 0, buf->mem+buf->datalen-startaddr);
  502. if(!next) {
  503. log_fn(LOG_DEBUG,"socks4: Destaddr not here yet.");
  504. return 0;
  505. }
  506. if(MAX_SOCKS_ADDR_LEN <= next-startaddr) {
  507. log_fn(LOG_WARN,"socks4: Destaddr too long. Rejecting.");
  508. return -1;
  509. }
  510. }
  511. log_fn(LOG_DEBUG,"socks4: Everything is here. Success.");
  512. strcpy(req->address, socks4_prot == socks4 ? tmpbuf : startaddr);
  513. /* XXX on very old netscapes (socks4) the next line triggers an
  514. * assert, because next-buf->mem+1 is greater than buf->datalen.
  515. */
  516. buf_remove_from_front(buf, next-buf->mem+1); /* next points to the final \0 on inbuf */
  517. return 1;
  518. case 'G': /* get */
  519. case 'H': /* head */
  520. case 'P': /* put/post */
  521. case 'C': /* connect */
  522. strcpy(req->reply,
  523. "HTTP/1.0 501 Tor is not an HTTP Proxy\r\n"
  524. "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"
  525. "<html>\n"
  526. "<head>\n"
  527. "<title>Tor is not an HTTP Proxy</title>\n"
  528. "</head>\n"
  529. "<body>\n"
  530. "<h1>Tor is not an HTTP Proxy</h1>\n"
  531. "<p>\n"
  532. "It appears you have configured your web browser to use Tor as an HTTP Proxy.\n"
  533. "This is not correct: Tor provides a SOCKS proxy. Please configure your\n"
  534. "client accordingly.\n"
  535. "</p>\n"
  536. "<p>\n"
  537. "See <a href=\"http://freehaven.net/tor/cvs/INSTALL\">http://freehaven.net/tor/cvs/INSTALL</a> for more information.\n"
  538. "<!-- Plus this comment, to make the body response more than 512 bytes, so IE will be willing to display it. Comment comment comment comment comment comment comment comment comment comment comment comment.-->\n"
  539. "</p>\n"
  540. "</body>\n"
  541. "</html>\n"
  542. );
  543. req->replylen = strlen(req->reply)+1;
  544. /* fall through */
  545. default: /* version is not socks4 or socks5 */
  546. log_fn(LOG_WARN,"Socks version %d not recognized. (Tor is not an http proxy.)",
  547. *(buf->mem));
  548. return -1;
  549. }
  550. }
  551. /** Log an error and exit if 'buf' is corrupted.
  552. */
  553. void assert_buf_ok(buf_t *buf)
  554. {
  555. tor_assert(buf);
  556. tor_assert(buf->magic == BUFFER_MAGIC);
  557. tor_assert(buf->mem);
  558. tor_assert(buf->datalen <= buf->len);
  559. }
  560. /*
  561. Local Variables:
  562. mode:c
  563. indent-tabs-mode:nil
  564. c-basic-offset:2
  565. End:
  566. */