nsa.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <arpa/inet.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <sys/stat.h>
  8. /// Prints usage message
  9. void usage(char* filename) {
  10. printf("\tNSA - yet another CIA network installer for FBI 2.0 or greater.\n"
  11. "\tUsage: %s ip-address 1st.cia [2nd.cia 3rd.cia ...]\n", filename);
  12. exit(0);
  13. }
  14. int main(int argc, char* argv[]) {
  15. // Print usage message if too few args are given.
  16. if(argc < 3)
  17. usage(argv[0]);
  18. // Check the files first.
  19. // Keep each file name with its size, which we'll need when sending the
  20. // file.
  21. struct file_w_size {
  22. char * file;
  23. unsigned long int size;
  24. };
  25. struct file_w_size filelist[argc-2];
  26. for( int i=2; i<argc; i++) {
  27. // Check that the file is readable
  28. if(access(argv[i], R_OK) == -1) {
  29. fprintf(stderr, "Cannot read file %s\n", argv[i]);
  30. return EXIT_FAILURE;
  31. }
  32. // Get the size and store it
  33. struct stat st;
  34. stat(argv[i], &st);
  35. filelist[i-2].size = st.st_size;
  36. filelist[i-2].file = argv[i];
  37. }
  38. // Create a TCP/IP socket
  39. int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  40. if(sock == -1) {
  41. fprintf(stderr, "Failed to create socket\n");
  42. return EXIT_FAILURE;
  43. }
  44. // Parse the 3DS's IP address as an unsigned long
  45. struct sockaddr_in serv_addr;
  46. if( inet_pton(AF_INET, argv[1], &serv_addr.sin_addr.s_addr) != 1) {
  47. fprintf(stderr, "Invalid IP\n");
  48. return EXIT_FAILURE;
  49. }
  50. // Set the rest of sockaddr_in
  51. serv_addr.sin_family = AF_INET;
  52. serv_addr.sin_port = htons(5000); // Port 5000
  53. memset(serv_addr.sin_zero, 0, sizeof(serv_addr.sin_zero));
  54. // Connect to the 3DS
  55. if(connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) {
  56. fprintf(stderr, "Failed to connect to socket\n");
  57. return EXIT_FAILURE;
  58. }
  59. // FBI first accepts the # of files being sent as a big endian uint
  60. unsigned int numfiles = argc-2;
  61. printf("Telling FBI we have %u files to send...\n", numfiles);
  62. // Swap to big endian
  63. numfiles = htonl(numfiles);
  64. send(sock, &numfiles, sizeof(unsigned int), 0);
  65. // Send each file
  66. // Allocate a 16MB buffer to hold what's read of the file
  67. char * buf = malloc(16000000 * sizeof(char));
  68. for(int i=0; i< argc-2; i++) {
  69. // Recieve 1 byte ACK from FBI
  70. recv(sock, NULL, 1, 0);
  71. // Send the file size as a big endian ulong int
  72. printf("DEBUG:: size before %lx", filelist[i].size);
  73. filelist[i].size = (filelist[i].size & 0x00000000FFFFFFFF) << 32 | (filelist[i].size & 0xFFFFFFFF00000000) >> 32;
  74. filelist[i].size = (filelist[i].size & 0x0000FFFF0000FFFF) << 16 | (filelist[i].size & 0xFFFF0000FFFF0000) >> 16;
  75. filelist[i].size = (filelist[i].size & 0x00FF00FF00FF00FF) << 8 | (filelist[i].size & 0xFF00FF00FF00FF00) >> 8;
  76. printf("Sending size of %s...\n", filelist[i].file);
  77. send(sock, &filelist[i].size, sizeof(unsigned long int), 0);
  78. // Open and send the file
  79. printf("Sending %s...\n", filelist[i].file);
  80. FILE *fp = fopen(filelist[i].file, "r");
  81. size_t bytes_read;
  82. do {
  83. bytes_read = fread(buf, 1, 16000000, fp);
  84. send(sock, buf, bytes_read, 0);
  85. }while(bytes_read);
  86. printf("Sent %s!\n", filelist[i].file);
  87. fclose(fp);
  88. }
  89. free(buf);
  90. close(sock);
  91. return EXIT_SUCCESS;
  92. }