Advertisement
xerpi

psp load client alpha 1

Feb 1st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2014, xerpi
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13.  
  14. #define PSPLOAD_VER       "alpha 1"
  15. #define PSPLOAD_PORT      4299
  16. #define PSPLOAD_ENVVAR    "PSPLOAD"
  17. #define PSPLOAD_CHUNK_LEN 1024
  18. #define PSPLOAD_MAGIC_1   'D'
  19. #define PSPLOAD_MAGIC_2   'O'
  20. #define PSPLOAD_MAGIC_3   'G'
  21. #define PSPLOAD_MAGIC_4   'E'
  22.  
  23. /*
  24.   PROTOCOL:
  25.     1) Send header
  26.     2) Send args
  27.     3) Send file (splitted into PSPLOAD_CHUNK_LEN bytes packet)
  28. */
  29.  
  30. struct pspload_header {
  31.     uint8_t  magic[4];
  32.     uint32_t file_size;
  33.     int32_t  argc;
  34. } __attribute__((packed));
  35.  
  36. struct pspload_argv {
  37.     uint32_t size;
  38.     void     *data;
  39. } __attribute__((packed));
  40.  
  41. static char *pspload_envvar_str = NULL;
  42. static uint8_t pspload_buffer[PSPLOAD_CHUNK_LEN];
  43.  
  44. void print_usage();
  45. ssize_t pspload_send(int sock, void *data, uint32_t size);
  46.  
  47. int main(int argc, char *argv[])
  48. {
  49.     printf("pspload version "PSPLOAD_VER" by xerpi\n");
  50.    
  51.     if (argc < 2) {
  52.         print_usage();
  53.         goto exit_error;
  54.     }
  55.  
  56.     if (!(pspload_envvar_str = getenv(PSPLOAD_ENVVAR))) {
  57.         printf("Set PSPLOAD\n");
  58.         goto exit_error;
  59.     }
  60.     if (strncmp(pspload_envvar_str, "tcp:", 4)) {
  61.         printf("PSPLOAD seems invalid\n");
  62.         goto exit_error;
  63.     }
  64.  
  65.     FILE *fp;
  66.     if (!(fp = fopen(argv[1], "rb"))) {
  67.         perror("Error opening file");
  68.         goto exit_error;  
  69.     }
  70.    
  71.     fseek(fp, 0, SEEK_END);
  72.     long file_size = ftell(fp);
  73.     fseek(fp, 0, SEEK_SET);
  74.    
  75.     if (file_size == 0) {
  76.         printf("Empty file?\n");
  77.         goto exit_close_file;
  78.     }
  79.        
  80.     int sock;
  81.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  82.         perror("Error creating socket");
  83.         goto exit_close_file;
  84.     }
  85.  
  86.     struct sockaddr_in server;
  87.     server.sin_family = AF_INET;
  88.     server.sin_port   = htons(PSPLOAD_PORT);
  89.     server.sin_addr.s_addr = inet_addr(&pspload_envvar_str[4]);
  90.    
  91.     if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1) {
  92.         perror("Error connecting socket");
  93.         goto exit_close_file;
  94.     }
  95.     printf("Connected to the server\n");
  96.    
  97.     struct pspload_header header = {
  98.         .magic = {
  99.             [0] = PSPLOAD_MAGIC_1,
  100.             [1] = PSPLOAD_MAGIC_2,
  101.             [2] = PSPLOAD_MAGIC_3,
  102.             [3] = PSPLOAD_MAGIC_4
  103.         },
  104.         .file_size = file_size,
  105.         .argc = 0
  106.     };
  107.    
  108.     pspload_send(sock, &header, sizeof(header));
  109.     int i, total_bytes_sent = 0, bytes_read = 0, bytes_sent = 0;
  110.     //for (i = 0; i < header.argc) send args
  111.    
  112.     for (i = 0; total_bytes_sent < file_size; ++i) {
  113.         bytes_read = fread(pspload_buffer, 1, PSPLOAD_CHUNK_LEN, fp);
  114.         printf("Read %d bytes from file\n", bytes_read);
  115.         bytes_sent = pspload_send(sock, pspload_buffer, bytes_read);
  116.         printf("Sent %d bytes\n", bytes_sent);
  117.         total_bytes_sent += bytes_read;
  118.     }
  119.    
  120. exit_close_sock:
  121.     close(sock);
  122.     printf("Connection closed\n");
  123. exit_close_file:
  124.     fclose(fp);
  125. exit_error:
  126.     return 0;
  127. }
  128.  
  129. ssize_t pspload_send(int sock, void *data, uint32_t size)
  130. {
  131.     ssize_t ret = send(sock, data, size, 0);
  132.     if (ret == -1) {
  133.         perror("Error sending data");
  134.     }
  135.     return ret;
  136. }
  137.  
  138.  
  139. void print_usage()
  140. {
  141.     printf("Usage: pspload file\nwill send file to the PSP\n");
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement