buffers.c 19 KB

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