socks5proxy.c 19 KB

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