buffers.c 15 KB

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