buffers.c 17 KB

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