Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.68 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <stdlib.h>
  5. #include <netinet/in.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <netdb.h>
  9.  
  10. #define BUF_SIZE 4096
  11.  
  12. struct params {
  13.         int n;
  14.         int c;
  15. };
  16.  
  17. size_t Read(int fd, char *buf, size_t buf_size) {
  18.         int nread;
  19.         nread = read(fd, buf, buf_size);
  20.         if (nread > 0) {
  21.                 buf[nread] = '\0';
  22.                 printf("< %s", buf);
  23.         }
  24.         return nread;
  25. }
  26.  
  27. struct params ReadUntilParams(int fd, char *buf, size_t buf_size) {
  28.         int nread;
  29.         struct params ret;
  30.  
  31.         while (1) {
  32.                 nread = Read(fd, buf, buf_size);
  33.                 if (nread == -1) {
  34.                         perror("read");
  35.                         exit(EXIT_FAILURE);
  36.                 }
  37.                 if (nread == 0) {
  38.                         ret.n = 0;
  39.                         ret.c = 0;
  40.                         return ret;
  41.                 }
  42.                 if (!(sscanf(buf, "N=%d C=%d\n", &(ret.n), &(ret.c)) <= 0)) {
  43.                         break;
  44.                 }
  45.         }
  46.         return ret;
  47. }
  48.  
  49. struct range_result {
  50.         int nwritten;
  51.         int last_item;
  52. };
  53.  
  54. struct range_result Range(int low, int high, char *buf) {
  55.         int index = 0;
  56.         int nwritten;
  57.  
  58.         struct range_result result;
  59.  
  60.         for (int num = low; num <= high; num++) {
  61.                 nwritten = snprintf(buf + index, BUF_SIZE - index, "%d ", num);
  62.                 // This checks for >= instead of just > because snprintf() might
  63.                 // truncate the result, putting \0 at buf[BUF_SIZE]. We don't care
  64.                 // are about the the \0 byte later on, but truncated means that we
  65.                 // swallowed a ' '.
  66.                 if (index + nwritten >= BUF_SIZE) {
  67.                         // Buffer not big enough.
  68.                         result.nwritten = index;
  69.                         result.last_item = num - 1;
  70.                         return result;
  71.                 }
  72.                 index += nwritten;
  73.         }
  74.  
  75.         result.nwritten = index;
  76.         result.last_item = high;
  77.         return result;
  78. }
  79.  
  80. void PlayRound();
  81. int main() {
  82.         int sfd;
  83.         struct addrinfo hints;
  84.         struct addrinfo *result;
  85.         int nread;
  86.  
  87.         memset(&hints, 0, sizeof(struct addrinfo));
  88.         hints.ai_family = AF_INET;
  89.         hints.ai_socktype = SOCK_STREAM;
  90.         int s = getaddrinfo("pwnable.kr", "9007", &hints, &result);
  91.         if (s != 0) {
  92.                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
  93.                 exit(EXIT_FAILURE);
  94.         }
  95.  
  96.         struct addrinfo *rp;
  97.         for (rp = result; rp != NULL; rp = rp->ai_next) {
  98.                 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  99.                 if (sfd == -1) {
  100.                         continue;
  101.                 }
  102.                 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
  103.                         break;                  /* Success */
  104.                 }
  105.                 close(sfd);
  106.         }
  107.  
  108.         if (rp == NULL) {
  109.                 fprintf(stderr, "Could not connect\n");
  110.                 exit(EXIT_FAILURE);
  111.         }
  112.  
  113.         freeaddrinfo(result);
  114.         // -------
  115.  
  116.  
  117.         while (1) {
  118.                 if (!PlayRound(sfd)) {
  119.                         break;
  120.                 }
  121.         }
  122. }
  123.  
  124.  
  125. void PlayRound(int sfd) {
  126.         char buf[BUF_SIZE];
  127.  
  128.         struct params params = ReadUntilParams(sfd, buf, BUF_SIZE - 1);
  129.         if (params.n == 0) {
  130.                 return false;
  131.         }
  132.  
  133.         int low, high;
  134.         low = 0;
  135.         high = params.n - 1;
  136.  
  137.         while (low != high) {
  138.                 int mid = (low + high) / 2;
  139.  
  140.                 int send_low = low;
  141.                 int send_high = mid;
  142.  
  143.                 while (1) {
  144.                         struct range_result result = Range(send_low, send_high, buf);
  145. //                      printf("[debug] last_item = %d\n", result.last_item);
  146. //                      printf("[debug] nwritten = %d\n", result.nwritten);
  147.                         if (result.nwritten == BUF_SIZE) {
  148.                                 if (buf[BUF_SIZE] == '\0' && buf[BUF_SIZE - 1] != ' ') {
  149.                                         printf("ITS THE NULL");
  150.                                 }
  151.                         }
  152.  
  153.                         int nwritten;
  154.                         nwritten = write(sfd, buf, result.nwritten);
  155.                         if (nwritten < 0) {
  156.                                 perror("write");
  157.                                 exit(EXIT_FAILURE);
  158.                         }
  159.  
  160.                         printf("> %.*s", result.nwritten, buf);
  161.                         fflush(stdout);
  162.  
  163.                         if (result.last_item == mid) {
  164.                                 break;
  165.                         }
  166.  
  167.                         send_low = result.last_item + 1;
  168.                 }
  169.  
  170.                 buf[0] = '\n';
  171.  
  172.                 int nwritten;
  173.                 nwritten = write(sfd, buf, 1);
  174.                 printf("\n");
  175.  
  176.                 Read(sfd, buf, BUF_SIZE - 1);
  177.  
  178.                 int weight = atoi(buf);
  179.                 if (weight % 10 == 0) {
  180.                         low = mid + 1;
  181.                 } else {
  182.                         high = mid;
  183.                 }
  184.         }
  185.  
  186.         while (1) {
  187.                 sprintf(buf, "%d\n", low);
  188.                 write(sfd, buf, strlen(buf));
  189.                 printf("> %s", buf);
  190.  
  191.                 Read(sfd, buf, BUF_SIZE - 1);
  192.  
  193.                 if (strncmp(buf, "Correct!", strlen("Correct!")) == 0) {
  194.                         break;
  195.                 }
  196.         }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement