leviathan0117

Untitled

Jun 24th, 2022 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10. #include <signal.h>
  11.  
  12. static void signal_action(int signo, siginfo_t *si, void *ctx) {
  13.     exit(0);
  14. }
  15.  
  16. size_t compress(const char* prev_buf, char* data, size_t n, char* cur_buf) {
  17.     return 15;
  18. }
  19.  
  20. size_t get_compress_buf_size() {
  21.     return 10000;
  22. }
  23.  
  24.  
  25. int main(int argc, char *argv[]) {
  26.     // EXIT PROPERLY BULLSHT
  27.    
  28.     struct sigaction sig_a;
  29.    
  30.     memset(&sig_a, 0, sizeof(sig_a));
  31.    
  32.     sig_a.sa_flags = SA_SIGINFO | SA_RESTART /*| SA_NODEFER*/;
  33.     sig_a.sa_sigaction = signal_action;
  34.  
  35.     if (sigaction(SIGTERM, &sig_a, NULL) < 0) {
  36.         perror("Sigaction fault");
  37.         fflush(NULL);
  38.         return 1;
  39.     }
  40.     if (sigaction(SIGPIPE, &sig_a, NULL) < 0) {
  41.         perror("Sigaction fault");
  42.         fflush(NULL);
  43.         return 1;
  44.     }
  45.    
  46.     // EXIT PROPERLY BULLSHT - END
  47.  
  48.    
  49.     int BLOCKSIZE = atoi(argv[2]);
  50.    
  51.     // START SERVER BULLSHT
  52.     int fd = socket(PF_INET, SOCK_STREAM, 0);
  53.     if (fd < 0) {
  54.         perror("Couldn't start server\n");
  55.         exit(1);
  56.     }
  57.  
  58.     int val = 1;
  59.     setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
  60.     setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val));
  61.  
  62.     struct sockaddr_in sa;
  63.     sa.sin_family = AF_INET;
  64.     sa.sin_addr.s_addr = INADDR_ANY;
  65.     sa.sin_port = htons(atoi(argv[1]));
  66.     if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
  67.         perror("Couldn't bind");
  68.         exit(1);
  69.     }
  70.  
  71.     if (listen(fd, 1) < 0) {
  72.         perror("Cound't listen");
  73.         exit(1);
  74.     }
  75.     // START SERVER BULLSHT - END
  76.    
  77.     char buf[BLOCKSIZE];
  78.    
  79.     char prev_buf[get_compress_buf_size()];
  80.     char data[get_compress_buf_size()];
  81.     char cur_buf[get_compress_buf_size()];
  82.  
  83.     while (1) {
  84.         struct sockaddr_in ss;
  85.         socklen_t sz = sizeof(ss);
  86.         int afd = accept(fd, (void*) &ss, &sz);
  87.         if (afd < 0) {
  88.             perror("Couldn't accept");
  89.             continue;
  90.         }
  91.  
  92.         // printf("client: %s, %d\n", inet_ntoa(ss.sin_addr), ntohs(ss.sin_port));
  93.        
  94.         int msg_size = 0;
  95.         int total_size = 0;
  96.         size_t compressed_size = 0;
  97.         int ok = 1;
  98.  
  99.         while ((msg_size = read(afd, buf, BLOCKSIZE)) > 0) {
  100.             total_size += msg_size;
  101.             if (total_size > BLOCKSIZE) {
  102.                 int tmp = htonl(0);
  103.                 if (write(afd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
  104.                     perror("Couldn't print");
  105.                     continue;
  106.                 }
  107.                 fflush(NULL);
  108.                 ok = 0;
  109.                 break;
  110.             } else {
  111.                 memcpy(data, buf, msg_size);
  112.                 compressed_size += compress(prev_buf, data, msg_size, cur_buf);
  113.                 memcpy(prev_buf, cur_buf, get_compress_buf_size());
  114.             }
  115.         }
  116.         if (ok) {
  117.             compressed_size = htonl(compressed_size);
  118.             if (write(afd, &compressed_size, sizeof(compressed_size)) != sizeof(compressed_size)) {
  119.                 perror("Couldn't print");
  120.                 continue;
  121.             }
  122.             fflush(NULL);
  123.         }
  124.        
  125.         close(afd);
  126.     }
  127.    
  128.     return 0;
  129. }
  130.  
Add Comment
Please, Sign In to add comment