Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <stdlib.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <sys/types.h>
- #include <netdb.h>
- #define BUF_SIZE 4096
- struct params {
- int n;
- int c;
- };
- size_t Read(int fd, char *buf, size_t buf_size) {
- int nread;
- nread = read(fd, buf, buf_size);
- if (nread > 0) {
- buf[nread] = '\0';
- printf("< %s", buf);
- }
- return nread;
- }
- struct params ReadUntilParams(int fd, char *buf, size_t buf_size) {
- int nread;
- struct params ret;
- while (1) {
- nread = Read(fd, buf, buf_size);
- if (nread == -1) {
- perror("read");
- exit(EXIT_FAILURE);
- }
- if (nread == 0) {
- ret.n = 0;
- ret.c = 0;
- return ret;
- }
- if (!(sscanf(buf, "N=%d C=%d\n", &(ret.n), &(ret.c)) <= 0)) {
- break;
- }
- }
- return ret;
- }
- struct range_result {
- int nwritten;
- int last_item;
- };
- struct range_result Range(int low, int high, char *buf) {
- int index = 0;
- int nwritten;
- struct range_result result;
- for (int num = low; num <= high; num++) {
- nwritten = snprintf(buf + index, BUF_SIZE - index, "%d ", num);
- // This checks for >= instead of just > because snprintf() might
- // truncate the result, putting \0 at buf[BUF_SIZE]. We don't care
- // are about the the \0 byte later on, but truncated means that we
- // swallowed a ' '.
- if (index + nwritten >= BUF_SIZE) {
- // Buffer not big enough.
- result.nwritten = index;
- result.last_item = num - 1;
- return result;
- }
- index += nwritten;
- }
- result.nwritten = index;
- result.last_item = high;
- return result;
- }
- void PlayRound();
- int main() {
- int sfd;
- struct addrinfo hints;
- struct addrinfo *result;
- int nread;
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- int s = getaddrinfo("pwnable.kr", "9007", &hints, &result);
- if (s != 0) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
- exit(EXIT_FAILURE);
- }
- struct addrinfo *rp;
- for (rp = result; rp != NULL; rp = rp->ai_next) {
- sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
- if (sfd == -1) {
- continue;
- }
- if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
- break; /* Success */
- }
- close(sfd);
- }
- if (rp == NULL) {
- fprintf(stderr, "Could not connect\n");
- exit(EXIT_FAILURE);
- }
- freeaddrinfo(result);
- // -------
- while (1) {
- if (!PlayRound(sfd)) {
- break;
- }
- }
- }
- void PlayRound(int sfd) {
- char buf[BUF_SIZE];
- struct params params = ReadUntilParams(sfd, buf, BUF_SIZE - 1);
- if (params.n == 0) {
- return false;
- }
- int low, high;
- low = 0;
- high = params.n - 1;
- while (low != high) {
- int mid = (low + high) / 2;
- int send_low = low;
- int send_high = mid;
- while (1) {
- struct range_result result = Range(send_low, send_high, buf);
- // printf("[debug] last_item = %d\n", result.last_item);
- // printf("[debug] nwritten = %d\n", result.nwritten);
- if (result.nwritten == BUF_SIZE) {
- if (buf[BUF_SIZE] == '\0' && buf[BUF_SIZE - 1] != ' ') {
- printf("ITS THE NULL");
- }
- }
- int nwritten;
- nwritten = write(sfd, buf, result.nwritten);
- if (nwritten < 0) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- printf("> %.*s", result.nwritten, buf);
- fflush(stdout);
- if (result.last_item == mid) {
- break;
- }
- send_low = result.last_item + 1;
- }
- buf[0] = '\n';
- int nwritten;
- nwritten = write(sfd, buf, 1);
- printf("\n");
- Read(sfd, buf, BUF_SIZE - 1);
- int weight = atoi(buf);
- if (weight % 10 == 0) {
- low = mid + 1;
- } else {
- high = mid;
- }
- }
- while (1) {
- sprintf(buf, "%d\n", low);
- write(sfd, buf, strlen(buf));
- printf("> %s", buf);
- Read(sfd, buf, BUF_SIZE - 1);
- if (strncmp(buf, "Correct!", strlen("Correct!")) == 0) {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement