socks5proxy.c 17 KB

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