From 1c469ad845a9e6fc05f8913ad46d25d9170e977c Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Tue, 28 Aug 2018 00:08:03 -0400 Subject: [PATCH] Add connecting to socket --- Makefile | 2 +- client/http_client.c | 6 ++++ client/http_client.h | 2 -- client/http_socket.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ client/http_socket.h | 3 ++ 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c6a67ad..54a385e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc CFLAGS = -Wall -g -c -std=gnu99 -LDFLAGS = +LDFLAGS = -lm BUILD_DIR = build COMMON_OBJ_FILES = $(patsubst %.c,$(BUILD_DIR)/%.o,$(wildcard common/*.c)) CLIENT_OBJ_FILES = $(patsubst %.c,$(BUILD_DIR)/%.o,$(wildcard client/*.c)) diff --git a/client/http_client.c b/client/http_client.c index 93cbc81..144d29b 100644 --- a/client/http_client.c +++ b/client/http_client.c @@ -1,5 +1,6 @@ #include "http_client.h" #include "../common/http_message.h" +#include "http_socket.h" #include #include #include @@ -31,6 +32,11 @@ int main(int argc, char* argv[]) { } get_parts(dest, path_buffer, host_buffer); printf("%s\n%s\n", path_buffer, host_buffer); + struct http_message req = build_basic_request("GET", host_buffer, path_buffer, port_num); + char * req_result = send_request(req); + printf("%s", req_result); + free(req_result); + free(req.contents); free(host_buffer); free(path_buffer); } diff --git a/client/http_client.h b/client/http_client.h index 8811152..94eacd1 100644 --- a/client/http_client.h +++ b/client/http_client.h @@ -4,8 +4,6 @@ #define RTT_FLAG "-p" #define USAGE_STRING "Usage: ./http_client [-p] server_url port_number\n" #define PORT_ERROR "Port must be a number in the range 1-65535" -#define MIN_PORT 1 -#define MAX_PORT 65535 void get_parts(const char *dest, char *path_buffer, char *host_buffer); diff --git a/client/http_socket.c b/client/http_socket.c index d9a3561..8fe480a 100644 --- a/client/http_socket.c +++ b/client/http_socket.c @@ -1,7 +1,13 @@ #include "http_socket.h" +#include "../common/socket_helper.h" +#include +#include +#include #include #include #include +#include +#include /** * Gets the buffer size to allocate based on the given components. @@ -40,3 +46,62 @@ struct http_message build_basic_request(const char *method, const char *host, co return message; } + + +/** + * Get the address info for a given hostname and port. + * + * @param hostname The hostname to read + * @param port The port to get the address info on + * @param info A pointer to a location where getaddrinfo can store data + * + * @return The result of getadddrinfo. See `man getaddrinfo` for details. + */ +static int get_addr_info(const char *hostname, int port, struct addrinfo **info) { + struct addrinfo hints; + // Force all hints to be null + memset(&hints, 0, sizeof(struct addrinfo)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + // Get the number of chars in the port + int port_length = floor(log10(port)) + 1; + char *port_string = malloc((port_length + 1) * sizeof(char)); + sprintf(port_string, "%d", port); + + int addr_status = getaddrinfo(hostname, port_string, &hints, info); + free(port_string); + + return addr_status; +} + +/** + * Send a given HTTP request + * + * @param req The HTTP request to send + * + * @return The data returned from the socket - must be freed by caller. + */ +char *send_request(struct http_message req) { + struct addrinfo *addr_info; + int addr_status = get_addr_info(req.address, req.port, &addr_info); + if (addr_status != 0) { + return NULL; + } + + int socket_fd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol); + if (socket_fd < 0) { + return NULL; + } + + int connection_status = connect(socket_fd, addr_info->ai_addr, addr_info->ai_addrlen); + if (connection_status != 0) { + return NULL; + } + + send(socket_fd, req.contents, strlen(req.contents), 0); + char * remote_parts = get_all_remote_parts(socket_fd); + close(socket_fd); + + return remote_parts; +} diff --git a/client/http_socket.h b/client/http_socket.h index d870056..fbee906 100644 --- a/client/http_socket.h +++ b/client/http_socket.h @@ -4,7 +4,10 @@ #include "../common/http_message.h" #define HTTP_VERSION "HTTP/1.1" +#define MIN_PORT 1 +#define MAX_PORT 65535 struct http_message build_basic_request(const char *method, const char *host, const char *path, int port); +char *send_request(struct http_message req); #endif