socks5proxy.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. * Slitheen - a decoy routing system for censorship resistance
  3. * Copyright (C) 2017 Cecylia Bocovich (cbocovic@uwaterloo.ca)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Additional permission under GNU GPL version 3 section 7
  18. *
  19. * If you modify this Program, or any covered work, by linking or combining
  20. * it with the OpenSSL library (or a modified version of that library),
  21. * containing parts covered by the terms of the OpenSSL Licence and the
  22. * SSLeay license, the licensors of this Program grant you additional
  23. * permission to convey the resulting work. Corresponding Source for a
  24. * non-source form of such a combination shall include the source code
  25. * for the parts of the OpenSSL library used as well as that of the covered
  26. * work.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <stdint.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <sys/socket.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <netinet/in.h>
  38. #include <netdb.h>
  39. #include <pthread.h>
  40. #include <fcntl.h>
  41. #include <arpa/inet.h>
  42. #include <openssl/bio.h>
  43. #include <openssl/evp.h>
  44. #include <openssl/buffer.h>
  45. #include <openssl/rand.h>
  46. #include "socks5proxy.h"
  47. #include "crypto.h"
  48. #include "tagging.h"
  49. #include "util.h"
  50. static connection_table *connections;
  51. typedef struct {
  52. int32_t in;
  53. int32_t out;
  54. } ous_pipes;
  55. int main(void){
  56. int listen_socket;
  57. struct sockaddr_in address;
  58. struct sockaddr_in remote_addr;
  59. socklen_t addr_size;
  60. connections = calloc(1, sizeof(connection_table));
  61. connections->first = NULL;
  62. int32_t ous_in[2];
  63. if(pipe(ous_in) < 0){
  64. printf("Failed to create pipe\n");
  65. return 1;
  66. }
  67. int32_t ous_out[2];
  68. if(pipe(ous_out) < 0){
  69. printf("Failed to create pipe\n");
  70. return 1;
  71. }
  72. ous_pipes pipes;
  73. pipes.in = ous_in[0];
  74. pipes.out = ous_out[1];
  75. /* Spawn a thread to listen for incomming OUS connections */
  76. /* Spawn a thread to do OUS IO */
  77. pthread_t *ous_thread = calloc(1, sizeof(pthread_t));
  78. pthread_create(ous_thread, NULL, ous_IO, (void *) &pipes);
  79. ous_pipes mux_pipes;
  80. mux_pipes.in = ous_in[1];
  81. mux_pipes.out = ous_out[0];
  82. /* Spawn a thread for multiplexing and demultiplexing */
  83. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  84. pthread_create(demux_thread, NULL, demultiplex_data, (void *) &mux_pipes);
  85. pthread_t *mux_thread = calloc(1, sizeof(pthread_t));
  86. pthread_create(mux_thread, NULL, multiplex_data, (void *) &mux_pipes);
  87. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  88. printf("Error creating socket\n");
  89. fflush(stdout);
  90. return 1;
  91. }
  92. address.sin_family = AF_INET;
  93. address.sin_addr.s_addr = INADDR_ANY;
  94. address.sin_port = htons(1080);
  95. int enable = 1;
  96. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  97. printf("Error setting sockopt\n");
  98. return 1;
  99. }
  100. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  101. printf("Error binding socket\n");
  102. fflush(stdout);
  103. return 1;
  104. }
  105. if(listen(listen_socket, 10) < 0){
  106. printf("Error listening\n");
  107. fflush(stdout);
  108. close(listen_socket);
  109. exit(1);
  110. }
  111. uint8_t last_id = 1;
  112. printf("Ready for listening\n");
  113. for(;;){
  114. addr_size = sizeof(remote_addr);
  115. int new_socket;
  116. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  117. &addr_size);
  118. if(new_socket < 0){
  119. perror("accept");
  120. exit(1);
  121. }
  122. printf("New connection\n");
  123. //assign a new stream_id and create a pipe for the session
  124. connection *new_conn = calloc(1, sizeof(connection));
  125. new_conn->stream_id = last_id++;
  126. new_conn->socket = new_socket;
  127. new_conn->state = NEW_STREAM;
  128. new_conn->next = NULL;
  129. if(connections->first == NULL){
  130. connections->first = new_conn;
  131. printf("Added first connection with id: %d\n", new_conn->stream_id);
  132. fflush(stdout);
  133. } else {
  134. connection *last = connections->first;
  135. while(last->next != NULL){
  136. last = last->next;
  137. }
  138. last->next = new_conn;
  139. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  140. fflush(stdout);
  141. }
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Responsible for communicating with the OUS. Upstream data is read from the pipes of individual
  147. * streams and sent to the OUS. Downstream data is read from the OUS, demultiplexed according t
  148. * stream ID, and sent to the corresponding stream.
  149. */
  150. void *ous_IO(void *args){
  151. ous_pipes *pipes = (ous_pipes *) args;
  152. int32_t ous_in = pipes->in;
  153. int32_t ous_out = pipes->out;
  154. //generate Slitheen ID
  155. uint8_t slitheen_id[SLITHEEN_ID_LEN];
  156. uint8_t shared_secret[16];
  157. generate_slitheen_id(slitheen_id, shared_secret);
  158. #ifdef DEBUG
  159. printf("Randomly generated slitheen id: ");
  160. int i;
  161. for(i=0; i< SLITHEEN_ID_LEN; i++){
  162. printf("%02x ", slitheen_id[i]);
  163. }
  164. printf("\n");
  165. #endif
  166. // Calculate super encryption keys
  167. generate_super_keys(shared_secret);
  168. printf("Generated super encrypt keys\n");
  169. char *encoded_bytes = NULL;
  170. base64_encode(slitheen_id, SLITHEEN_ID_LEN, &encoded_bytes);
  171. printf("Encoded ID\n");
  172. //give encoded slitheen ID to ous
  173. struct sockaddr_in ous_addr;
  174. ous_addr.sin_family = AF_INET;
  175. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  176. ous_addr.sin_port = htons(57173);
  177. int32_t ous = socket(AF_INET, SOCK_STREAM, 0);
  178. if(ous < 0){
  179. printf("Failed to make socket\n");
  180. pthread_exit(NULL);
  181. }
  182. int32_t error = connect(ous, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  183. if(error < 0){
  184. printf("Error connecting to OUS\n");
  185. pthread_exit(NULL);
  186. }
  187. printf("Connected to OUS\n");
  188. uint16_t len = htons(strlen(encoded_bytes));
  189. int32_t bytes_sent = send(ous, (unsigned char *) &len, sizeof(uint16_t), 0);
  190. bytes_sent += send(ous, encoded_bytes, ntohs(len), 0);
  191. printf("Wrote %d bytes to OUS_in: %x\n %s\n", bytes_sent, len, encoded_bytes);
  192. uint8_t *buffer = emalloc(BUFSIZ);
  193. int32_t buffer_len = BUFSIZ;
  194. uint8_t *input_buffer = NULL;
  195. uint32_t input_buffer_len;
  196. int32_t bytes_read;
  197. /* Select on proxy pipes, demux thread, and ous to send and receive data*/
  198. for(;;){
  199. fd_set read_fds;
  200. fd_set write_fds;
  201. int32_t nfds = ous;
  202. if(ous_in > nfds)
  203. nfds = ous_in;
  204. if(ous_out > nfds)
  205. nfds = ous_out;
  206. FD_ZERO(&read_fds);
  207. FD_ZERO(&write_fds);
  208. FD_SET(ous_in, &read_fds);
  209. FD_SET(ous, &read_fds);
  210. FD_SET(ous_out, &write_fds);
  211. FD_SET(ous, &write_fds);
  212. if(select(nfds+1, &read_fds, &write_fds, NULL, NULL) < 0){
  213. fprintf(stderr, "Select error\n");
  214. break;
  215. }
  216. if(FD_ISSET(ous_in, &read_fds) && FD_ISSET(ous, &write_fds)){
  217. bytes_read = read(ous_in, buffer, buffer_len);
  218. #ifdef DEBUG_IO
  219. printf("Received %d bytes from multiplexer\n", bytes_read);
  220. for(int i=0; i< bytes_read; i++){
  221. printf("%02x ", buffer[i]);
  222. }
  223. printf("\n");
  224. fflush(stdout);
  225. #endif
  226. if(bytes_read > 0){
  227. bytes_sent = send(ous, buffer, bytes_read, 0);
  228. #ifdef DEBUG_IO
  229. printf("Sent %d bytes to OUS\n", bytes_sent);
  230. for(int i=0; i< bytes_sent; i++){
  231. printf("%02x ", buffer[i]);
  232. }
  233. printf("\n");
  234. fflush(stdout);
  235. #endif
  236. if(bytes_sent <= 0){
  237. fprintf(stderr, "Connection to OUS closed\n");
  238. break;
  239. }
  240. } else if (bytes_read == 0) {
  241. fprintf(stderr, "Connection to multiplexer closed\n");
  242. break;
  243. } else {
  244. fprintf(stderr, "Error reading from multiplexer\n");
  245. break;
  246. }
  247. }
  248. if(FD_ISSET(ous, &read_fds) && FD_ISSET(ous_out, &write_fds)){
  249. bytes_read = recv(ous, (uint8_t *) &input_buffer_len, 4, 0);
  250. #ifdef DEBUG_IO
  251. printf("Received %d bytes from OUS\n", bytes_read);
  252. for(int i=0; i< bytes_read; i++){
  253. printf("%02x ", buffer[i]);
  254. }
  255. printf("\n");
  256. fflush(stdout);
  257. #endif
  258. if (bytes_read <= 0) {
  259. fprintf(stderr, "Connection to OUS closed\n");
  260. break;
  261. }
  262. uint32_t chunk_len = input_buffer_len;
  263. bytes_sent = write(ous_out, (uint8_t *) &input_buffer_len, bytes_read);
  264. //TODO: check return
  265. input_buffer = malloc(input_buffer_len);
  266. bytes_read = recv(ous, input_buffer, chunk_len, 0);
  267. #ifdef DEBUG_IO
  268. printf("Received %d bytes from OUS\n", bytes_read);
  269. for(int i=0; i< bytes_read; i++){
  270. printf("%02x ", input_buffer[i]);
  271. }
  272. printf("\n");
  273. fflush(stdout);
  274. #endif
  275. if(bytes_read > 0){
  276. bytes_sent = write(ous_out, input_buffer, bytes_read);
  277. #ifdef DEBUG_IO
  278. printf("Sent %d bytes to demultiplexer\n", bytes_sent);
  279. for(int i=0; i< bytes_sent; i++){
  280. printf("%02x ", input_buffer[i]);
  281. }
  282. printf("\n");
  283. fflush(stdout);
  284. #endif
  285. if(bytes_sent <= 0){
  286. fprintf(stderr, "Connection to demultiplexer closed\n");
  287. break;
  288. }
  289. } else if (bytes_read == 0) {
  290. fprintf(stderr, "Connection to OUS closed\n");
  291. break;
  292. } else {
  293. fprintf(stderr, "Error reading from OUS\n");
  294. break;
  295. }
  296. free(input_buffer);
  297. input_buffer = NULL;
  298. }
  299. }
  300. fprintf(stderr, "Closing OUS\n");
  301. close(ous);
  302. close(ous_in);
  303. close(ous_out);
  304. free(buffer);
  305. pthread_exit(NULL);
  306. }
  307. /*
  308. * Continuously read from all stream sockets and pass data to ous
  309. */
  310. void *multiplex_data(void *args){
  311. ous_pipes *pipes = (ous_pipes *) args;
  312. int32_t buffer_len = BUFSIZ;
  313. uint8_t *buffer = ecalloc(1, buffer_len);
  314. int32_t bytes_read;
  315. uint8_t *response = ecalloc(1, BUFSIZ);
  316. /* Select on stream sockets and ous_in pipe to send and receive data*/
  317. for(;;){
  318. fd_set read_fds;
  319. fd_set write_fds;
  320. int32_t nfds = 0;
  321. FD_ZERO(&read_fds);
  322. FD_ZERO(&write_fds);
  323. //add all stream sockets to read_fds
  324. connection *conn = connections->first;
  325. while(conn != NULL){
  326. if(conn->socket > nfds)
  327. nfds = conn->socket;
  328. FD_SET(conn->socket, &read_fds);
  329. conn = conn->next;
  330. }
  331. FD_SET(pipes->in, &write_fds);
  332. if(pipes->in > nfds)
  333. nfds = pipes->in;
  334. struct timeval tv;
  335. tv.tv_sec = 3;
  336. tv.tv_usec = 0;
  337. if(select(nfds+1, &read_fds, &write_fds, NULL, &tv) < 0){
  338. fprintf(stderr, "Select error\n");
  339. break;
  340. }
  341. struct slitheen_up_hdr *up_hdr;
  342. uint16_t len;
  343. char *encoded_bytes = NULL;
  344. conn = connections->first;
  345. while(conn != NULL){
  346. uint8_t stream_id = conn->stream_id;
  347. if(FD_ISSET(conn->socket, &read_fds) && FD_ISSET(pipes->in, &write_fds)){
  348. printf("Reading from stream %d\n", conn->stream_id);
  349. bytes_read = recv(conn->socket, buffer, buffer_len, 0);
  350. if(bytes_read < 0){
  351. close(conn->socket);
  352. conn = conn->next;
  353. remove_connection(stream_id);
  354. continue;
  355. } else if(bytes_read == 0){
  356. //socket is closed
  357. printf("Closing connection for stream %d sockfd.\n", conn->stream_id);
  358. fflush(stdout);
  359. if(conn->state == CONNECTED){
  360. //Send close message to slitheen proxy
  361. up_hdr = (struct slitheen_up_hdr *) buffer;
  362. up_hdr->stream_id = conn->stream_id;
  363. up_hdr->len = 0;
  364. base64_encode(buffer, 20, &encoded_bytes);
  365. len = htons(strlen(encoded_bytes));
  366. int32_t bytes_sent = write(pipes->in, (unsigned char *) &len, sizeof(uint16_t));
  367. bytes_sent += write(pipes->in, encoded_bytes, ntohs(len));
  368. printf("Wrote %d bytes to ous\n", bytes_sent);
  369. printf("Closing message: %s\n", encoded_bytes);
  370. free(encoded_bytes);
  371. encoded_bytes = NULL;
  372. }
  373. close(conn->socket);
  374. conn = conn->next;
  375. remove_connection(stream_id);
  376. continue;
  377. }
  378. switch(conn->state){
  379. case NEW_STREAM:
  380. printf("Received new stream data from stream %d\n", conn->stream_id);
  381. #ifdef DEBUG
  382. printf("Received %d bytes (id %d):\n", bytes_read, conn->stream_id);
  383. for(int i=0; i< bytes_read; i++){
  384. printf("%02x ", buffer[i]);
  385. }
  386. printf("\n");
  387. fflush(stdout);
  388. #endif
  389. //Respond to methods negotiation
  390. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  391. uint8_t *p = buffer + 2;
  392. if(clnt_meth->version != 0x05){
  393. close(conn->socket);
  394. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  395. fflush(stdout);
  396. conn = conn->next;
  397. remove_connection(stream_id);
  398. continue;
  399. }
  400. int responded = 0;
  401. int bytes_sent;
  402. for(int i=0; i< clnt_meth->num_methods; i++){
  403. if(p[0] == 0x00){//send response with METH= 0x00
  404. response[0] = 0x05;
  405. response[1] = 0x00;
  406. send(conn->socket, response, 2, 0);
  407. responded = 1;
  408. }
  409. p++;
  410. }
  411. if(!responded){//respond with METH= 0xFF
  412. response[0] = 0x05;
  413. response[1] = 0xFF;
  414. send(conn->socket, response, 2, 0);
  415. close(conn->socket);
  416. conn = conn->next;
  417. remove_connection(stream_id);
  418. continue;
  419. }
  420. conn->state = NEGOTIATED;
  421. break;
  422. case NEGOTIATED:
  423. printf("Received negotiation data from stream %d\n", conn->stream_id);
  424. #ifdef DEBUG
  425. printf("Received %d bytes (id %d):\n", bytes_read, conn->stream_id);
  426. for(int i=0; i< bytes_read; i++){
  427. printf("%02x ", buffer[i]);
  428. }
  429. printf("\n");
  430. fflush(stdout);
  431. #endif
  432. //Respond to say connection was accepted
  433. response[0] = 0x05;
  434. response[1] = 0x00;
  435. response[2] = 0x00;
  436. response[3] = 0x01;
  437. *((uint32_t *) (response + 4)) = 0;
  438. *((uint16_t *) (response + 8)) = 0;
  439. send(conn->socket, response, 10, 0); //TODO: add check for send
  440. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  441. up_hdr = (struct slitheen_up_hdr *) buffer;
  442. up_hdr->stream_id = conn->stream_id;
  443. up_hdr->len = htons(bytes_read);
  444. bytes_read+= sizeof(struct slitheen_up_hdr);
  445. base64_encode(buffer, bytes_read, &encoded_bytes);
  446. len = htons(strlen(encoded_bytes));
  447. bytes_sent = write(pipes->in, (unsigned char *) &len, sizeof(uint16_t));
  448. bytes_sent += write(pipes->in, encoded_bytes, ntohs(len));
  449. printf("Wrote %d bytes to ous\n", bytes_sent);
  450. free(encoded_bytes);
  451. encoded_bytes = NULL;
  452. conn->state = CONNECTED;
  453. break;
  454. case CONNECTED:
  455. printf("Received application data from stream %d\n", conn->stream_id);
  456. #ifdef DEBUG_UPSTREAM
  457. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, conn->stream_id);
  458. for(int i=0; i< bytes_read; i++){
  459. printf("%02x ", buffer[i]);
  460. }
  461. printf("\n");
  462. printf("%s\n", buffer);
  463. fflush(stdout);
  464. #endif
  465. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  466. up_hdr = (struct slitheen_up_hdr *) buffer;
  467. up_hdr->stream_id = conn->stream_id;
  468. up_hdr->len = htons(bytes_read);
  469. bytes_read+= sizeof(struct slitheen_up_hdr);
  470. base64_encode(buffer, bytes_read, &encoded_bytes);
  471. len = htons(strlen(encoded_bytes));
  472. bytes_sent = write(pipes->in, (unsigned char *) &len, sizeof(uint16_t));
  473. bytes_sent += write(pipes->in, encoded_bytes, ntohs(len));
  474. printf("Wrote %d bytes to ous\n", bytes_sent);
  475. #ifdef DEBUG_UPSTREAM
  476. printf("Sent to OUS (%d bytes): %x %s\n",bytes_sent, len, encoded_bytes);
  477. #endif
  478. free(encoded_bytes);
  479. encoded_bytes = NULL;
  480. break;
  481. default:
  482. fprintf(stderr, "Wrong connection state\n");
  483. close(conn->socket);
  484. conn = conn->next;
  485. remove_connection(stream_id);
  486. break;
  487. }
  488. }
  489. conn = conn->next;
  490. }
  491. }
  492. free(response);
  493. pthread_exit(NULL);
  494. }
  495. /* Read blocks of covert data from the OUS. Determine the stream id and the length of
  496. * the block and then write the data to the correct thread to be passed to the browser
  497. */
  498. void *demultiplex_data(void *args){
  499. ous_pipes *pipes = (ous_pipes *) args;
  500. uint8_t *buffer = NULL;
  501. uint8_t *p;
  502. uint8_t *partial_block = NULL;
  503. uint32_t partial_block_len = 0;
  504. uint64_t expected_next_count = 1;
  505. data_block *saved_data = NULL;
  506. for(;;){
  507. uint32_t chunk_len;
  508. int32_t bytes_read = read(pipes->out, (uint8_t *) &chunk_len, 4);
  509. fprintf(stdout, "Length of this chunk: %u\n", chunk_len);
  510. buffer = calloc(1, chunk_len);
  511. bytes_read = read(pipes->out, buffer, chunk_len);
  512. if(bytes_read <= 0){
  513. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  514. goto err;
  515. }
  516. if(bytes_read < chunk_len) {
  517. printf("Error: read %d out of %d bytes\n", bytes_read, chunk_len);
  518. }
  519. int32_t chunk_remaining = bytes_read;
  520. p = buffer;
  521. #ifdef DEBUG_PARSE
  522. printf("Received a new chunk of len %d bytes\n", chunk_remaining);
  523. #endif
  524. //didn't read a full slitheen block last time
  525. if(partial_block_len > 0){
  526. //process first part of slitheen info
  527. memmove(buffer+partial_block_len, buffer, bytes_read);
  528. memcpy(buffer, partial_block, partial_block_len);
  529. chunk_remaining += partial_block_len;
  530. free(partial_block);
  531. partial_block = NULL;
  532. partial_block_len = 0;
  533. }
  534. while(chunk_remaining > 0){
  535. /*TODO: investigate assumption that we only ever receive chunks
  536. * that contain full slitheen blocks */
  537. #ifdef DEBUG_PARSE
  538. printf("Chunk remaining: %d bytes\n", chunk_remaining);
  539. #endif
  540. if(chunk_remaining < SLITHEEN_HEADER_LEN){
  541. #ifdef DEBUG_PARSE
  542. printf("Incomplete Slitheen block: ");
  543. int i;
  544. for(i = 0; i< chunk_remaining; i++){
  545. printf("%02x ", p[i]);
  546. }
  547. printf("\n");
  548. #endif
  549. /*
  550. if(partial_block != NULL) printf("UH OH (PB)\n");
  551. partial_block = calloc(1, chunk_remaining);
  552. memcpy(partial_block, p, chunk_remaining);
  553. partial_block_len = chunk_remaining;
  554. chunk_remaining = 0;
  555. */
  556. break;
  557. }
  558. //decrypt header to see if we have entire block
  559. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  560. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  561. if(!peek_header(tmp_header)){
  562. printf("This chunk doesn't contain a Slitheen block\n");
  563. break;
  564. }
  565. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  566. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  567. #ifdef DEBUG
  568. printf("Slitheen header:\n");
  569. int i;
  570. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  571. printf("%02x ", tmp_header[i]);
  572. }
  573. printf("\n");
  574. #endif
  575. if(ntohs(sl_hdr->len) > chunk_remaining){
  576. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  577. printf("Saving in partial block\n");
  578. if(partial_block != NULL) printf("UH OH (PB)\n");
  579. partial_block = calloc(1, ntohs(sl_hdr->len));
  580. memcpy(partial_block, p, chunk_remaining);
  581. partial_block_len = chunk_remaining;
  582. chunk_remaining = 0;
  583. free(tmp_header);
  584. break;
  585. }
  586. super_decrypt(p);
  587. sl_hdr = (struct slitheen_hdr *) p;
  588. free(tmp_header);
  589. p += SLITHEEN_HEADER_LEN;
  590. chunk_remaining -= SLITHEEN_HEADER_LEN;
  591. if((!sl_hdr->len) && (sl_hdr->garbage)){
  592. #ifdef DEBUG_PARSE
  593. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  594. #endif
  595. //there might be more garbage bytes than we have chunk left
  596. p += ntohs(sl_hdr->garbage);
  597. chunk_remaining -= ntohs(sl_hdr->garbage);
  598. continue;
  599. }
  600. int32_t sock =-1;
  601. if(connections->first == NULL){
  602. printf("Error: there are no connections\n");
  603. } else {
  604. connection *last = connections->first;
  605. if (last->stream_id == sl_hdr->stream_id){
  606. sock = last->socket;
  607. }
  608. while(last->next != NULL){
  609. last = last->next;
  610. if (last->stream_id == sl_hdr->stream_id){
  611. sock = last->socket;
  612. }
  613. }
  614. }
  615. if(sock == -1){
  616. printf("No stream id exists. Possibly invalid header\n");
  617. break;
  618. }
  619. #ifdef DEBUG_PARSE
  620. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  621. #endif
  622. //figure out how much to skip
  623. int32_t padding = 0;
  624. if(ntohs(sl_hdr->len) %16){
  625. padding = 16 - ntohs(sl_hdr->len)%16;
  626. }
  627. p += 16; //IV
  628. //check counter to see if we are missing data
  629. if(sl_hdr->counter > expected_next_count){
  630. //save any future data
  631. printf("Received header with count %lu. Expected count %lu.\n",
  632. sl_hdr->counter, expected_next_count);
  633. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  634. data_block *new_block = malloc(sizeof(data_block));
  635. new_block->count = sl_hdr->counter;
  636. new_block->len = ntohs(sl_hdr->len);
  637. new_block->data = malloc(ntohs(sl_hdr->len));
  638. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  639. new_block->socket = sock;
  640. new_block->next = saved_data;
  641. saved_data = new_block;
  642. } else {
  643. data_block *last = saved_data;
  644. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  645. last = last->next;
  646. }
  647. data_block *new_block = malloc(sizeof(data_block));
  648. new_block->count = sl_hdr->counter;
  649. new_block->len = ntohs(sl_hdr->len);
  650. new_block->data = malloc(ntohs(sl_hdr->len));
  651. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  652. new_block->socket = sock;
  653. new_block->next = last->next;
  654. last->next = new_block;
  655. }
  656. } else {
  657. int32_t bytes_sent = send(sock, p, ntohs(sl_hdr->len), 0);
  658. if(bytes_sent <= 0){
  659. printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
  660. }
  661. //increment expected counter
  662. expected_next_count++;
  663. }
  664. //now check to see if there is saved data to write out
  665. if(saved_data != NULL){
  666. data_block *current_block = saved_data;
  667. while((current_block != NULL) && (expected_next_count == current_block->count)){
  668. int32_t bytes_sent = send(current_block->socket, current_block->data,
  669. current_block->len, 0);
  670. if(bytes_sent <= 0){
  671. printf("Error writing to socket for stream id %d\n", sl_hdr->stream_id);
  672. }
  673. expected_next_count++;
  674. saved_data = current_block->next;
  675. free(current_block->data);
  676. free(current_block);
  677. current_block = saved_data;
  678. }
  679. }
  680. p += ntohs(sl_hdr->len); //encrypted data
  681. p += 16; //mac
  682. p += padding;
  683. p += ntohs(sl_hdr->garbage);
  684. chunk_remaining -= ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  685. }
  686. free(buffer);
  687. buffer = NULL;
  688. }
  689. err:
  690. if (buffer != NULL) {
  691. free(buffer);
  692. }
  693. close(pipes->out);
  694. pthread_exit(NULL);
  695. }
  696. int remove_connection(uint16_t stream_id){
  697. connection *last = connections->first;
  698. connection *prev = last;
  699. while(last != NULL){
  700. if(last->stream_id == stream_id){
  701. if(last == connections->first){
  702. connections->first = last->next;
  703. } else {
  704. prev->next = last->next;
  705. }
  706. free(last);
  707. printf("Removed stream id %d from connections table\n", stream_id);
  708. break;
  709. }
  710. prev = last;
  711. last = last->next;
  712. }
  713. return 1;
  714. }