Created
September 4, 2012 11:59
-
-
Save butterflysky/3620600 to your computer and use it in GitHub Desktop.
my attempt at level0 on vortex wargame at www.overthewire.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/ip.h> | |
#include <netdb.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#include <byteswap.h> | |
/* TODO: | |
* replace static return codes with constants | |
* factor helper functions into separate companion library | |
* replace reliance on __builtin_bswap32 with more portable function | |
*/ | |
/* prototypes for socket helper functions */ | |
int senddata (int sockfd, const void *msg, int len); | |
int recvdata (int sockfd, void *buf, int len); | |
int main (int argc, char **argv) { | |
int status, sockfd; | |
unsigned int sum, buf[4], i; | |
char ip[INET_ADDRSTRLEN], credsbuf[1024]; | |
struct addrinfo hints, *servinfo, *p; | |
memset(&hints, 0, sizeof hints); | |
memset(credsbuf, 0, sizeof credsbuf); | |
hints.ai_family = AF_UNSPEC; | |
hints.ai_socktype = SOCK_STREAM; | |
if ((status = getaddrinfo("vortex.labs.overthewire.org", "5842", &hints, &servinfo)) != 0) { | |
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |
exit(1); | |
} | |
for (p = servinfo; p != NULL; p = p->ai_next) { | |
inet_ntop(AF_INET, ((struct sockaddr_in *) p->ai_addr)->sin_addr, ip, INET_ADDRSTRLEN); | |
printf("Connecting to %s ...\n",ip); | |
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { | |
perror("socket"); | |
continue; | |
} | |
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { | |
close(sockfd); | |
perror("connect"); | |
continue; | |
} | |
break; | |
} | |
if (p == NULL) { | |
fprintf(stderr, "failed to connect\n"); | |
exit(2); | |
} | |
printf("success. reading 4 unsigned integers\n"); | |
for (i=0; i < 4; i++) { | |
recvdata(sockfd, buf + i, sizeof buf[i]); | |
buf[i] = __builtin_bswap32(buf[i]); | |
} | |
printf("The numbers returned were: %u\t%u\t%u\t%u\n", buf[0], buf[1], buf[2], buf[3]); | |
sum = __builtin_bswap32(buf[0] + buf[1] + buf[2] + buf[3]); | |
status = 0; | |
printf("Sending sum of %u back\n", sum); | |
do { | |
status = senddata(sockfd, &sum + status, (sizeof sum) - status); | |
} while (status < sizeof sum); | |
printf("Receiving feedback:\n"); | |
recvdata(sockfd, credsbuf, sizeof credsbuf); | |
printf("%s\n", credsbuf); | |
freeaddrinfo(servinfo); | |
return 0; | |
} | |
int senddata (int sockfd, const void *msg, int len) { | |
int status; | |
status = send(sockfd, msg, len, 0); | |
if (status == -1) { | |
fprintf(stderr, "send failed; %s\n", strerror(errno)); | |
exit(5); | |
} | |
return status; | |
} | |
int recvdata (int sockfd, void *buf, int len) { | |
int status; | |
status = recv(sockfd, buf, len, 0); | |
if (status == -1 || status == 0) { | |
fprintf(stderr, "recv failed; %s\n", strerror(errno)); | |
exit(errno); | |
} | |
/* | |
if (status == 0) { | |
fprintf(stderr, "connection closed by remote host\n"); | |
exit(4); | |
} | |
*/ | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment