socks5proxy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /** SOCKSv5 proxy that listens on port 1080.
  2. *
  3. *
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <stdint.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <netinet/in.h>
  15. #include <netdb.h>
  16. #include <pthread.h>
  17. #include <fcntl.h>
  18. #include <arpa/inet.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/buffer.h>
  22. #include <openssl/rand.h>
  23. #include "socks5proxy.h"
  24. #include "crypto.h"
  25. #include "tagging.h"
  26. static connection_table *connections;
  27. int main(void){
  28. int listen_socket;
  29. struct sockaddr_in address;
  30. struct sockaddr_in remote_addr;
  31. socklen_t addr_size;
  32. mkfifo("OUS_out", 0666);
  33. //generate Slitheen ID using Telex tagging method
  34. uint8_t slitheen_id[SLITHEEN_ID_LEN];
  35. uint8_t shared_secret[16];
  36. generate_slitheen_id(slitheen_id, shared_secret);
  37. //RAND_bytes(slitheen_id, SLITHEEN_ID_LEN);
  38. printf("Randomly generated slitheen id: ");
  39. int i;
  40. for(i=0; i< SLITHEEN_ID_LEN; i++){
  41. printf("%02x ", slitheen_id[i]);
  42. }
  43. printf("\n");
  44. // Calculate super encryption keys
  45. generate_super_keys(shared_secret);
  46. //b64 encode slitheen ID
  47. char *encoded_bytes;
  48. BUF_MEM *buffer_ptr;
  49. BIO *bio, *b64;
  50. b64 = BIO_new(BIO_f_base64());
  51. bio = BIO_new(BIO_s_mem());
  52. bio = BIO_push(b64, bio);
  53. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  54. BIO_write(bio, slitheen_id, SLITHEEN_ID_LEN);
  55. BIO_flush(bio);
  56. BIO_get_mem_ptr(bio, &buffer_ptr);
  57. BIO_set_close(bio, BIO_NOCLOSE);
  58. BIO_free_all(bio);
  59. encoded_bytes = (*buffer_ptr).data;
  60. encoded_bytes[(*buffer_ptr).length] = '\0';
  61. //give encoded slitheen ID to ous
  62. struct sockaddr_in ous_addr;
  63. ous_addr.sin_family = AF_INET;
  64. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  65. ous_addr.sin_port = htons(8888);
  66. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  67. if(ous_in < 0){
  68. printf("Failed to make ous_in socket\n");
  69. return 1;
  70. }
  71. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  72. if(error < 0){
  73. printf("Error connecting\n");
  74. return 1;
  75. }
  76. char *message = calloc(1, BUFSIZ);
  77. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes), encoded_bytes);
  78. int32_t bytes_sent = send(ous_in, message, strlen(message), 0);
  79. printf("Wrote %d bytes to OUS_in:\n %s\n", bytes_sent, message);
  80. free(message);
  81. /* Spawn process to listen for incoming data from OUS
  82. int32_t demux_pipe[2];
  83. if(pipe(demux_pipe) < 0){
  84. printf("Failed to create pipe for new thread\n");
  85. return 1;
  86. }*/
  87. connections = calloc(1, sizeof(connection_table));
  88. connections->first = NULL;
  89. pthread_t *demux_thread = calloc(1, sizeof(pthread_t));
  90. pthread_create(demux_thread, NULL, demultiplex_data, NULL);
  91. if (!(listen_socket = socket(AF_INET, SOCK_STREAM, 0))){
  92. printf("Error creating socket\n");
  93. fflush(stdout);
  94. return 1;
  95. }
  96. address.sin_family = AF_INET;
  97. address.sin_addr.s_addr = INADDR_ANY;
  98. address.sin_port = htons(1080);
  99. int enable = 1;
  100. if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) <0 ){
  101. printf("Error setting sockopt\n");
  102. return 1;
  103. }
  104. if(bind(listen_socket, (struct sockaddr *) &address, sizeof(address))){
  105. printf("Error binding socket\n");
  106. fflush(stdout);
  107. return 1;
  108. }
  109. if(listen(listen_socket, 10) < 0){
  110. printf("Error listening\n");
  111. fflush(stdout);
  112. close(listen_socket);
  113. exit(1);
  114. }
  115. uint8_t last_id = 1;
  116. printf("Ready for listening\n");
  117. for(;;){
  118. addr_size = sizeof(remote_addr);
  119. int new_socket;
  120. new_socket = accept(listen_socket, (struct sockaddr *) &remote_addr,
  121. &addr_size);
  122. if(new_socket < 0){
  123. perror("accept");
  124. exit(1);
  125. }
  126. printf("New connection\n");
  127. //assign a new stream_id and create a pipe for the session
  128. connection *new_conn = calloc(1, sizeof(connection));
  129. new_conn->stream_id = last_id++;
  130. int32_t pipefd[2];
  131. if(pipe(pipefd) < 0){
  132. printf("Failed to create pipe\n");
  133. continue;
  134. }
  135. new_conn->pipe_fd = pipefd[1];
  136. new_conn->next = NULL;
  137. if(connections->first == NULL){
  138. connections->first = new_conn;
  139. printf("Added first connection with id: %d\n", new_conn->stream_id);
  140. fflush(stdout);
  141. } else {
  142. connection *last = connections->first;
  143. while(last->next != NULL){
  144. last = last->next;
  145. }
  146. last->next = new_conn;
  147. printf("Added connection with id: %d at %p\n", new_conn->stream_id, last->next);
  148. fflush(stdout);
  149. }
  150. int pid = fork();
  151. if(pid == 0){ //child
  152. close(listen_socket);
  153. proxy_data(new_socket, new_conn->stream_id, pipefd[0]);
  154. exit(0);
  155. }
  156. close(new_socket);
  157. }
  158. return 0;
  159. }
  160. //continuously read from the socket and look for a CONNECT message
  161. int proxy_data(int sockfd, uint16_t stream_id, int32_t ous_out){
  162. uint8_t *buffer = calloc(1, BUFSIZ);
  163. uint8_t *response = calloc(1, BUFSIZ);
  164. int32_t i;
  165. int bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  166. if (bytes_read < 0){
  167. printf("Error reading from socket (fd = %d)\n", sockfd);
  168. fflush(stdout);
  169. goto err;
  170. }
  171. #ifdef DEBUG
  172. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  173. for(i=0; i< bytes_read; i++){
  174. printf("%02x ", buffer[i]);
  175. }
  176. printf("\n");
  177. fflush(stdout);
  178. #endif
  179. //Respond to methods negotiation
  180. struct socks_method_req *clnt_meth = (struct socks_method_req *) buffer;
  181. uint8_t *p = buffer + 2;
  182. if(clnt_meth->version != 0x05){
  183. printf("Client supplied invalid version: %02x\n", clnt_meth->version);
  184. fflush(stdout);
  185. goto err;
  186. }
  187. int responded = 0;
  188. int bytes_sent;
  189. for(i=0; i< clnt_meth->num_methods; i++){
  190. if(p[0] == 0x00){//send response with METH= 0x00
  191. response[0] = 0x05;
  192. response[1] = 0x00;
  193. send(sockfd, response, 2, 0);
  194. responded = 1;
  195. }
  196. p++;
  197. }
  198. if(!responded){//respond with METH= 0xFF
  199. response[0] = 0x05;
  200. response[1] = 0xFF;
  201. send(sockfd, response, 2, 0);
  202. goto err;
  203. }
  204. //Now wait for a connect request
  205. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  206. if (bytes_read < 0){
  207. printf("Error reading from socket\n");
  208. fflush(stdout);
  209. goto err;
  210. }
  211. #ifdef DEBUG
  212. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  213. for(i=0; i< bytes_read; i++){
  214. printf("%02x ", buffer[i]);
  215. }
  216. printf("\n");
  217. fflush(stdout);
  218. #endif
  219. //Now respond
  220. response[0] = 0x05;
  221. response[1] = 0x00;
  222. response[2] = 0x00;
  223. response[3] = 0x01;
  224. *((uint32_t *) (response + 4)) = 0;
  225. *((uint16_t *) (response + 8)) = 0;
  226. send(sockfd, response, 10, 0);
  227. //wait for first upstream bytes
  228. bytes_read += recv(sockfd, buffer+bytes_read, BUFSIZ-bytes_read-3, 0);
  229. if (bytes_read < 0){
  230. printf("Error reading from socket\n");
  231. fflush(stdout);
  232. goto err;
  233. }
  234. #ifdef DEBUG_UPSTREAM
  235. printf("Received %d bytes (id %d):\n", bytes_read, stream_id);
  236. for(i=0; i< bytes_read; i++){
  237. printf("%02x ", buffer[i]);
  238. }
  239. printf("\n");
  240. fflush(stdout);
  241. #endif
  242. //pre-pend stream_id and length
  243. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read+1);
  244. struct slitheen_up_hdr *up_hdr = (struct slitheen_up_hdr *) buffer;
  245. up_hdr->stream_id = stream_id;
  246. up_hdr->len = htons(bytes_read);
  247. bytes_read+= sizeof(struct slitheen_up_hdr);
  248. //encode bytes for safe transport (b64)
  249. const char *encoded_bytes;
  250. BUF_MEM *buffer_ptr;
  251. BIO *bio, *b64;
  252. b64 = BIO_new(BIO_f_base64());
  253. bio = BIO_new(BIO_s_mem());
  254. bio = BIO_push(b64, bio);
  255. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  256. BIO_write(bio, buffer, bytes_read);
  257. BIO_flush(bio);
  258. BIO_get_mem_ptr(bio, &buffer_ptr);
  259. BIO_set_close(bio, BIO_NOCLOSE);
  260. BIO_free_all(bio);
  261. encoded_bytes = (*buffer_ptr).data;
  262. struct sockaddr_in ous_addr;
  263. ous_addr.sin_family = AF_INET;
  264. inet_pton(AF_INET, "127.0.0.1", &(ous_addr.sin_addr));
  265. ous_addr.sin_port = htons(8888);
  266. int32_t ous_in = socket(AF_INET, SOCK_STREAM, 0);
  267. if(ous_in < 0){
  268. printf("Failed to make ous_in socket\n");
  269. goto err;
  270. }
  271. int32_t error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  272. if(error < 0){
  273. printf("Error connecting\n");
  274. goto err;
  275. }
  276. char *message = calloc(1, BUFSIZ);
  277. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ", strlen(encoded_bytes)+1, encoded_bytes);
  278. bytes_sent = send(ous_in, message, strlen(message), 0);
  279. #ifdef DEBUG_UPSTREAM
  280. printf("Wrote %d bytes to OUS_in: %s\n", bytes_sent, message);
  281. #endif
  282. if(bytes_sent < 0){
  283. printf("Error writing to websocket\n");
  284. fflush(stdout);
  285. goto err;
  286. } else {
  287. close(ous_in);
  288. }
  289. p = buffer+sizeof(struct slitheen_up_hdr);
  290. #ifdef DEBUG_UPSTREAM
  291. for(i=0; i< bytes_read; i++){
  292. printf("%02x ", p[i]);
  293. }
  294. printf("\n");
  295. fflush(stdout);
  296. #endif
  297. struct socks_req *clnt_req = (struct socks_req *) p;
  298. p += 4;
  299. //see if it's a connect request
  300. if(clnt_req->cmd != 0x01){
  301. printf("Error: issued a non-connect command\n");
  302. fflush(stdout);
  303. goto err;
  304. }
  305. //now select on pipe (for downstream data) and the socket (for upstream data)
  306. for(;;){
  307. fd_set readfds;
  308. fd_set writefds;
  309. int32_t nfds = (sockfd > ous_out) ? sockfd +1 : ous_out + 1;
  310. FD_ZERO(&readfds);
  311. FD_ZERO(&writefds);
  312. FD_SET(sockfd, &readfds);
  313. FD_SET(ous_out, &readfds);
  314. FD_SET(sockfd, &writefds);
  315. if(select(nfds, &readfds, &writefds, NULL, NULL) <0){
  316. printf("Select error\n");
  317. fflush(stdout);
  318. continue;
  319. }
  320. if(FD_ISSET(sockfd, &readfds)){// && FD_ISSET(ous_in, &writefds)){
  321. bytes_read = recv(sockfd, buffer, BUFSIZ-1, 0);
  322. if (bytes_read < 0){
  323. printf("Error reading from socket (in for loop)\n");
  324. fflush(stdout);
  325. goto err;
  326. }
  327. if(bytes_read == 0){
  328. //socket is closed
  329. printf("Closing connection for stream %d sockfd.\n", stream_id);
  330. fflush(stdout);
  331. //Send close message to slitheen proxy
  332. up_hdr = (struct slitheen_up_hdr *) buffer;
  333. up_hdr->stream_id = stream_id;
  334. up_hdr->len = 0;
  335. bio = BIO_new(BIO_s_mem());
  336. b64 = BIO_new(BIO_f_base64());
  337. bio = BIO_push(b64, bio);
  338. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  339. BIO_write(bio, buffer, 20);
  340. BIO_flush(bio);
  341. BIO_get_mem_ptr(bio, &buffer_ptr);
  342. encoded_bytes = (*buffer_ptr).data;
  343. BIO_set_close(bio, BIO_NOCLOSE);
  344. BIO_free_all(bio);
  345. uint8_t *ebytes = calloc(1, (*buffer_ptr).length+1);
  346. memcpy(ebytes, (*buffer_ptr).data, (*buffer_ptr).length);
  347. ebytes[(*buffer_ptr).length] = '\0';
  348. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  349. if(ous_in < 0){
  350. printf("Failed to make ous_in socket\n");
  351. fflush(stdout);
  352. goto err;
  353. }
  354. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  355. if(error < 0){
  356. printf("Error connecting\n");
  357. fflush(stdout);
  358. goto err;
  359. }
  360. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  361. strlen( (char *)ebytes)+1, ebytes);
  362. free(ebytes);
  363. bytes_sent = send(ous_in, message, strlen(message), 0);
  364. printf("Closing message: %s\n", message);
  365. close(ous_in);
  366. goto err;
  367. }
  368. if(bytes_read > 0){
  369. #ifdef DEBUG_UPSTREAM
  370. printf("Received %d data bytes from sockfd (id %d):\n", bytes_read, stream_id);
  371. for(i=0; i< bytes_read; i++){
  372. printf("%02x ", buffer[i]);
  373. }
  374. printf("\n");
  375. printf("%s\n", buffer);
  376. fflush(stdout);
  377. #endif
  378. memmove(buffer+sizeof(struct slitheen_up_hdr), buffer, bytes_read);
  379. up_hdr = (struct slitheen_up_hdr *) buffer;
  380. up_hdr->stream_id = stream_id;
  381. up_hdr->len = htons(bytes_read);
  382. bytes_read+= sizeof(struct slitheen_up_hdr);
  383. bio = BIO_new(BIO_s_mem());
  384. b64 = BIO_new(BIO_f_base64());
  385. bio = BIO_push(b64, bio);
  386. BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
  387. BIO_write(bio, buffer, bytes_read);
  388. BIO_flush(bio);
  389. BIO_get_mem_ptr(bio, &buffer_ptr);
  390. BIO_set_close(bio, BIO_NOCLOSE);
  391. BIO_free_all(bio);
  392. encoded_bytes = (*buffer_ptr).data;
  393. ous_in = socket(AF_INET, SOCK_STREAM, 0);
  394. if(ous_in < 0){
  395. printf("Failed to make ous_in socket\n");
  396. return 1;
  397. }
  398. error = connect(ous_in, (struct sockaddr *) &ous_addr, sizeof (struct sockaddr));
  399. if(error < 0){
  400. printf("Error connecting\n");
  401. return 1;
  402. }
  403. sprintf(message, "POST / HTTP/1.1\r\nContent-Length: %zd\r\n\r\n%s ",
  404. strlen(encoded_bytes)+1, encoded_bytes);
  405. bytes_sent = send(ous_in, message, strlen(message), 0);
  406. #ifdef DEBUG_UPSTREAM
  407. printf("Sent to OUS (%d bytes):%s\n",bytes_sent, message);
  408. #endif
  409. close(ous_in);
  410. }
  411. } else if(FD_ISSET(ous_out, &readfds) && FD_ISSET(sockfd, &writefds)){
  412. bytes_read = read(ous_out, buffer, BUFSIZ-1);
  413. if (bytes_read <= 0){
  414. printf("Error reading from ous_out (in for loop)\n");
  415. fflush(stdout);
  416. goto err;
  417. }
  418. if(bytes_read > 0){
  419. #ifdef DEBUG_DOWNSTREAM
  420. printf("Stream id %d received %d bytes from ous_out:\n", stream_id, bytes_read);
  421. for(i=0; i< bytes_read; i++){
  422. printf("%02x ", buffer[i]);
  423. }
  424. printf("\n");
  425. printf("%s\n", buffer);
  426. fflush(stdout);
  427. #endif
  428. bytes_sent = send(sockfd, buffer, bytes_read, 0);
  429. if(bytes_sent <= 0){
  430. printf("Error sending bytes to browser for stream id %d\n", stream_id);
  431. }
  432. #ifdef DEBUG_DOWNSTREAM
  433. printf("Sent to browser (%d bytes from stream id %d):\n", bytes_sent, stream_id);
  434. for(i=0; i< bytes_sent; i++){
  435. printf("%02x ", buffer[i]);
  436. }
  437. printf("\n");
  438. fflush(stdout);
  439. #endif
  440. }
  441. }
  442. }
  443. err:
  444. //should also remove stream from table
  445. close(sockfd);
  446. free(buffer);
  447. free(response);
  448. exit(0);
  449. }
  450. /* Read blocks of covert data from OUS_out. Determine the stream id and the length of
  451. * the block and then write the data to the correct thread to be passed to the browser
  452. */
  453. void *demultiplex_data(){
  454. int32_t buffer_len = BUFSIZ;
  455. uint8_t *buffer = calloc(1, buffer_len);
  456. uint8_t *p;
  457. printf("Opening OUS_out... ");
  458. int32_t ous_fd = open("OUS_out", O_RDONLY);
  459. printf("done.\n");
  460. uint8_t *partial_block = NULL;
  461. uint32_t partial_block_len = 0;
  462. uint32_t resource_remaining = 0;
  463. uint64_t expected_next_count = 1;
  464. data_block *saved_data = NULL;
  465. for(;;){
  466. int32_t bytes_read = read(ous_fd, buffer, buffer_len-partial_block_len);
  467. if(bytes_read > 0){
  468. int32_t bytes_remaining = bytes_read;
  469. p = buffer;
  470. //didn't read a full slitheen block last time
  471. if(partial_block_len > 0){
  472. //process first part of slitheen info
  473. memmove(buffer+partial_block_len, buffer, bytes_read);
  474. memcpy(buffer, partial_block, partial_block_len);
  475. bytes_remaining += partial_block_len;
  476. free(partial_block);
  477. partial_block = NULL;
  478. partial_block_len = 0;
  479. }
  480. while(bytes_remaining > 0){
  481. if(resource_remaining <= 0){//we're at a new resource
  482. //the first value for a new resource will be the resource length,
  483. //followed by a newline
  484. uint8_t *end_ptr;
  485. resource_remaining = strtol((const char *) p, (char **) &end_ptr, 10);
  486. #ifdef DEBUG_PARSE
  487. printf("Starting new resource of len %d bytes\n", resource_remaining);
  488. printf("Resource len bytes:\n");
  489. int i;
  490. for(i=0; i< (end_ptr - p) + 1; i++){
  491. printf("%02x ", ((const char *) p)[i]);
  492. }
  493. printf("\n");
  494. #endif
  495. if(resource_remaining == 0){
  496. bytes_remaining -= (end_ptr - p) + 1;
  497. p += (end_ptr - p) + 1;
  498. } else {
  499. bytes_remaining -= (end_ptr - p) + 1;
  500. p += (end_ptr - p) + 1;
  501. }
  502. continue;
  503. }
  504. if(resource_remaining < SLITHEEN_HEADER_LEN){
  505. printf("ERROR: Resource remaining doesn't fit header len.\n");
  506. resource_remaining = 0;
  507. bytes_remaining = 0;
  508. break;
  509. }
  510. if(bytes_remaining < SLITHEEN_HEADER_LEN){
  511. #ifdef DEBUG_PARSE
  512. printf("Partial header: ");
  513. int i;
  514. for(i = 0; i< bytes_remaining; i++){
  515. printf("%02x ", p[i]);
  516. }
  517. printf("\n");
  518. #endif
  519. if(partial_block != NULL) printf("UH OH (PB)\n");
  520. partial_block = calloc(1, bytes_remaining);
  521. memcpy(partial_block, p, bytes_remaining);
  522. partial_block_len = bytes_remaining;
  523. bytes_remaining = 0;
  524. break;
  525. }
  526. //decrypt header to see if we have entire block
  527. uint8_t *tmp_header = malloc(SLITHEEN_HEADER_LEN);
  528. memcpy(tmp_header, p, SLITHEEN_HEADER_LEN);
  529. peek_header(tmp_header);
  530. struct slitheen_hdr *sl_hdr = (struct slitheen_hdr *) tmp_header;
  531. //first see if sl_hdr corresponds to a valid stream. If not, ignore rest of read bytes
  532. #ifdef DEBUG_PARSE
  533. printf("Slitheen header:\n");
  534. int i;
  535. for(i = 0; i< SLITHEEN_HEADER_LEN; i++){
  536. printf("%02x ", tmp_header[i]);
  537. }
  538. printf("\n");
  539. #endif
  540. if(ntohs(sl_hdr->len) > resource_remaining){
  541. printf("ERROR: slitheen block doesn't fit in resource remaining!\n");
  542. resource_remaining = 0;
  543. bytes_remaining = 0;
  544. break;
  545. }
  546. if(ntohs(sl_hdr->len) > bytes_remaining){
  547. if(partial_block != NULL) printf("UH OH (PB)\n");
  548. partial_block = calloc(1, ntohs(sl_hdr->len));
  549. memcpy(partial_block, p, bytes_remaining);
  550. partial_block_len = bytes_remaining;
  551. bytes_remaining = 0;
  552. free(tmp_header);
  553. break;
  554. }
  555. super_decrypt(p);
  556. sl_hdr = (struct slitheen_hdr *) p;
  557. free(tmp_header);
  558. p += SLITHEEN_HEADER_LEN;
  559. bytes_remaining -= SLITHEEN_HEADER_LEN;
  560. resource_remaining -= SLITHEEN_HEADER_LEN;
  561. if((!sl_hdr->len) && (sl_hdr->garbage)){
  562. #ifdef DEBUG_PARSE
  563. printf("%d Garbage bytes\n", ntohs(sl_hdr->garbage));
  564. #endif
  565. p += ntohs(sl_hdr->garbage);
  566. bytes_remaining -= ntohs(sl_hdr->garbage);
  567. resource_remaining -= ntohs(sl_hdr->garbage);
  568. continue;
  569. }
  570. int32_t pipe_fd =-1;
  571. if(connections->first == NULL){
  572. printf("Error: there are no connections\n");
  573. } else {
  574. connection *last = connections->first;
  575. if (last->stream_id == sl_hdr->stream_id){
  576. pipe_fd = last->pipe_fd;
  577. }
  578. while(last->next != NULL){
  579. last = last->next;
  580. if (last->stream_id == sl_hdr->stream_id){
  581. pipe_fd = last->pipe_fd;
  582. }
  583. }
  584. }
  585. if(pipe_fd == -1){
  586. printf("No stream id exists. Possibly invalid header\n");
  587. break;
  588. }
  589. #ifdef DEBUG_PARSE
  590. printf("Received information for stream id: %d of length: %u\n", sl_hdr->stream_id, ntohs(sl_hdr->len));
  591. #endif
  592. //figure out how much to skip
  593. int32_t padding = 0;
  594. if(ntohs(sl_hdr->len) %16){
  595. padding = 16 - ntohs(sl_hdr->len)%16;
  596. }
  597. p += 16; //IV
  598. //check counter to see if we are missing data
  599. if(sl_hdr->counter > expected_next_count){
  600. //save any future data
  601. printf("Received header with count %lu. Expected count %lu.\n",
  602. sl_hdr->counter, expected_next_count);
  603. if((saved_data == NULL) || (saved_data->count > sl_hdr->counter)){
  604. data_block *new_block = malloc(sizeof(data_block));
  605. new_block->count = sl_hdr->counter;
  606. new_block->len = ntohs(sl_hdr->len);
  607. new_block->data = malloc(ntohs(sl_hdr->len));
  608. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  609. new_block->pipe_fd = pipe_fd;
  610. new_block->next = saved_data;
  611. saved_data = new_block;
  612. } else {
  613. data_block *last = saved_data;
  614. while((last->next != NULL) && (last->next->count < sl_hdr->counter)){
  615. last = last->next;
  616. }
  617. data_block *new_block = malloc(sizeof(data_block));
  618. new_block->count = sl_hdr->counter;
  619. new_block->len = ntohs(sl_hdr->len);
  620. new_block->data = malloc(ntohs(sl_hdr->len));
  621. memcpy(new_block->data, p, ntohs(sl_hdr->len));
  622. new_block->pipe_fd = pipe_fd;
  623. new_block->next = last->next;
  624. last->next = new_block;
  625. }
  626. } else {
  627. int32_t bytes_sent = write(pipe_fd, p, ntohs(sl_hdr->len));
  628. if(bytes_sent <= 0){
  629. printf("Error reading to pipe for stream id %d\n",
  630. sl_hdr->stream_id);
  631. }
  632. //increment expected counter
  633. expected_next_count++;
  634. }
  635. //now check to see if there is saved data to write out
  636. if(saved_data != NULL){
  637. data_block *current_block = saved_data;
  638. while((current_block != NULL) &&
  639. (expected_next_count == current_block->count)){
  640. int32_t bytes_sent = write(current_block->pipe_fd,
  641. current_block->data, current_block->len);
  642. if(bytes_sent <= 0){
  643. printf("Error reading to pipe for stream id %d\n",
  644. sl_hdr->stream_id);
  645. }
  646. expected_next_count++;
  647. saved_data = current_block->next;
  648. free(current_block->data);
  649. free(current_block);
  650. current_block = saved_data;
  651. }
  652. }
  653. p += ntohs(sl_hdr->len); //encrypted data
  654. p += 16; //mac
  655. p += padding;
  656. p += ntohs(sl_hdr->garbage);
  657. bytes_remaining -=
  658. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  659. resource_remaining -=
  660. ntohs(sl_hdr->len) + 16 + padding + 16 + ntohs(sl_hdr->garbage);
  661. }
  662. } else {
  663. printf("Error: read %d bytes from OUS_out\n", bytes_read);
  664. printf("Re-opening OUS_out... ");
  665. close(ous_fd);
  666. ous_fd = open("OUS_out", O_RDONLY);
  667. printf("done.\n");
  668. }
  669. }
  670. free(buffer);
  671. close(ous_fd);
  672. }