buffers.c 17 KB

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