@@ -0,0 +1 @@ | |||
*.out |
@@ -0,0 +1,11 @@ | |||
CC = gcc | |||
CFLAGS = -O0 | |||
SRC = *.c | |||
all: $(patsubst %.c, %.out, $(wildcard *.c)) | |||
%.out: %.c Makefile | |||
$(CC) $(CFLAGS) $< -o $@ | |||
clean: | |||
rm *.out |
@@ -0,0 +1,58 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <unistd.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
// TODO: fix over buf overflow | |||
void fillHttpReq(char *buf, char *host, size_t size) { | |||
char *req = "GET / HTTP/1.1\r\nHost: \0"; | |||
memcpy(buf, req, strlen(req)); | |||
memcpy(buf + strlen(buf), host, strlen(host)); | |||
memcpy(buf + strlen(buf), "\r\n\r\n", strlen("\r\n\r\n")); | |||
} | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
if (argc != 2) { | |||
fprintf(stderr, "Usage: %s <hostname>", argv[0]); | |||
exit(1); | |||
} | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo(argv[1], "80", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s = socket(servinfo->ai_family, servinfo->ai_socktype, | |||
servinfo->ai_protocol); | |||
printf("%d\n", s); | |||
int connection; | |||
if ((connection = connect(s, servinfo->ai_addr, servinfo->ai_addrlen)) == | |||
-1) { | |||
fprintf(stderr, "connection error: %d\n", connection); | |||
exit(1); | |||
} | |||
char buf[10000]; | |||
fillHttpReq(buf, argv[1], 10000); | |||
printf("%s\n", buf); | |||
// TODO: send throw a loop | |||
send(s, buf, 10000, 0); | |||
memset(buf, 0, 10000); | |||
recv(s, buf, 10000, 0); | |||
printf("%s\n", buf); | |||
close(s); | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,72 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
#define BACKLOG 10 | |||
int main(int argc, char **argv) { | |||
int status; | |||
struct addrinfo hints; | |||
struct addrinfo *servinfo; | |||
struct sockaddr_storage their_addr; | |||
socklen_t addr_size; | |||
memset(&hints, 0, sizeof hints); | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
hints.ai_flags = AI_PASSIVE; | |||
if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
int s; | |||
if ((s = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) == -1) { | |||
fprintf(stderr, "socket error: %d\n", status); | |||
exit(1); | |||
} | |||
printf("%d\n", s); | |||
int yes=1; | |||
if ((status = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes)) == -1) { | |||
fprintf(stderr, "setsockopt error: %d\n", status); | |||
exit(1); | |||
} | |||
if((status = bind(s, servinfo->ai_addr, servinfo->ai_addrlen)) == -1) { | |||
fprintf(stderr, "bind error: %d\n", status); | |||
exit(1); | |||
} | |||
listen(s, BACKLOG); | |||
addr_size = sizeof their_addr; | |||
int new_fd = accept(s, (struct sockaddr *)&their_addr, &addr_size); | |||
printf("%d\n", new_fd); | |||
int t = 10; | |||
char c; | |||
while (t--) { | |||
char *msg = "Hi mom!\0"; | |||
int len = strlen(msg), bytes_sent; | |||
if ((bytes_sent = send(new_fd, msg, len, 0)) == -1) { | |||
fprintf(stderr, "send error: %d\n", status); | |||
exit(1); | |||
} | |||
} | |||
char *msg = "Ja!\0"; | |||
int len = strlen(msg), bytes_sent; | |||
if ((bytes_sent = send(new_fd, msg, len, 0)) == -1) { | |||
fprintf(stderr, "send error: %d\n", status); | |||
exit(1); | |||
} | |||
freeaddrinfo(servinfo); | |||
} |
@@ -0,0 +1,53 @@ | |||
#include <arpa/inet.h> | |||
#include <netdb.h> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
int main(int argc, char **argv) { | |||
if (argc != 2) { | |||
fprintf(stderr, "Usage: %s <hostname>", argv[0]); | |||
exit(1); | |||
} | |||
int status; | |||
struct addrinfo hints; | |||
memset(&hints, 0, sizeof hints); | |||
struct addrinfo *servinfo; | |||
hints.ai_family = AF_UNSPEC; | |||
hints.ai_socktype = SOCK_STREAM; | |||
if ((status = getaddrinfo(argv[1], "80", &hints, &servinfo)) != 0) { | |||
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); | |||
exit(1); | |||
} | |||
struct addrinfo *cur = servinfo; | |||
while (cur) { | |||
void *addr; | |||
char *ipver; | |||
char ipstr[INET6_ADDRSTRLEN]; | |||
if (cur->ai_family == AF_INET) { | |||
struct sockaddr_in *temp = (struct sockaddr_in *)cur->ai_addr; | |||
addr = &(temp->sin_addr); | |||
ipver = "IPv4"; | |||
} else if (cur->ai_family == AF_INET6) { | |||
struct sockaddr_in6 *temp = (struct sockaddr_in6 *)cur->ai_addr; | |||
addr = &(temp->sin6_addr); | |||
ipver = "IPv6"; | |||
} | |||
inet_ntop(cur->ai_family, addr, ipstr, sizeof ipstr); | |||
printf("%s: %s\n", ipver, ipstr); | |||
cur = cur->ai_next; | |||
} | |||
freeaddrinfo(servinfo); | |||
} |