Compare commits

..

10 Commits

7 changed files with 218 additions and 4 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
main

View File

@@ -1,4 +1,4 @@
main: main.c
gcc main.c -o main -L/usr/lib -lXm -lXt -lX11 -l:libXmHTML.so.1.1.10
gcc main.c url_manipulation.c easysock.c -o main -L/usr/lib -lXm -lXt -lX11 -l:libXmHTML.so.1.1.10

View File

@@ -77,13 +77,36 @@ int create_local (int network, char transport, char* address, int port,struct so
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct) {
struct addrinfo hints; /* Used to tell getaddrinfo what kind of address we want */
struct addrinfo* results; /* Used by getaddrinfo to store the addresses */
if (check_ip_ver(address) < 0) { /* If the address is a domain name */
int err_code;
char* port_str = malloc(10 * sizeof(char));
sprintf(port_str,"%d",port); /* getaddrinfo expects a string for its port */
memset(&hints,'\0',sizeof(hints));
hints.ai_socktype = char_to_socktype(transport);
err_code = getaddrinfo(address,port_str,&hints,&results);
if (err_code != 0) {
exit(err_code);
}
remote_addr_struct = results->ai_addr;
network = inet_to_int(results->ai_family);
} else {
create_addr(network,address,port,remote_addr_struct);
}
int socket = create_socket(network,transport);
if (socket < 0) {
exit(errno);
}
create_addr(network,address,port,remote_addr_struct);
int addrlen;
int addrlen;
if (network == 4) {
addrlen = sizeof(struct sockaddr_in);
} else if (network == 6) {
@@ -102,3 +125,45 @@ int create_remote (int network,char transport,char* address,int port,struct sock
}
return socket;
}
int check_ip_ver(char* address) {
char buffer[16]; /* 16 chars - 128 bits - is enough to hold an ipv6 address */
if (inet_pton(AF_INET,address,buffer) == 1) {
return 4;
} else if (inet_pton(AF_INET6,address,buffer) == 1) {
return 6;
} else {
return -1;
}
}
int int_to_inet(int network) {
if (network == 4) {
return AF_INET;
} else if (network == 6) {
return AF_INET6;
} else {
exit(207);
}
}
int inet_to_int(int af_type) {
if (af_type == AF_INET) {
return 4;
} else if (af_type == AF_INET6) {
return 6;
} else {
exit(207);
}
}
int char_to_socktype(char transport) {
if (transport == 'T') {
return SOCK_STREAM;
} else if (transport == 'U') {
return SOCK_DGRAM;
} else {
exit(250);
}
}

View File

@@ -2,7 +2,9 @@
#define EASYSOCK_H_
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
@@ -50,5 +52,24 @@ It prints the error returned by 'connect' if something went wrong, and exits wit
int create_remote (int network,char transport,char* address,int port,struct sockaddr* remote_addr_struct);
/* check_ip_ver - This function checks if the given string is an IPv4 address (returns 4),
IPv6 address (returns 6) or neither (returns -1). */
int check_ip_ver(char* address);
/* int_to_inet - Takes an int value (4 for IPv4, 6 for IPv6) and returns AF_INET or
AF_INET6 respectively. */
int int_to_inet(int network);
/* char_to_socktype - Takes a character that represents a transport-layer protocol
(currently only supports 'T' for TCP or 'U' for UDP - exits with error code 250 if
the given characters is neither of these) and return the appropriate SOCKTYPE value. */
int char_to_socktype(char transport);
/* inet_to_int - Takes an int value that corresponds to AF_INET or AF_INET6,
and returns the appropriate int value. */
int inet_to_int(int af_type);
#endif

30
main.c
View File

@@ -6,6 +6,7 @@
#include <Xm/Form.h>
#include <XmHTML/XmHTML.h>
#include "easysock.h"
#include "url_manipulation.h"
void testFunc(Widget,XtPointer,XmPushButtonCallbackStruct *);
@@ -60,8 +61,35 @@ void testFunc(Widget w, XtPointer client_data, XmPushButtonCallbackStruct *callb
Widget* widget_list = (Widget *)client_data;
Widget result_widget = widget_list[1];
Widget text_widget = widget_list[0];
int port;
char* url = XmTextFieldGetString(text_widget);
char* protocol = url_to_proto(url); /* Extract the protocol string from the URL input */
if (strcmp(protocol,"http") == 0) {
port = 80;
} else if (strcmp(protocol,"https") == 0) {
port = 443;
} else {
exit(50); /* I haven't added support for other protocols yet */
}
XmHTMLTextSetString(result_widget,"");
/* We call the create_remote function to create an address, bind it to a socket,
and make the connection for us. It returns the socket, so that the HTTP request can be made.
First parameter is IP version - set to -1 because we don't know yet if the address is IPv4 or IPv6. */
char* request = "GET /~fdc/sample.html HTTP/1.1\nHost: www.columbia.edu\n\n"
char* response = malloc(sizeof(char) * 5000);
struct sockaddr* address_struct;
int remote_socket = create_remote(-1,'T',url_to_hostname(url),port,address_struct);
send(remote_socket,request,strlen(request),0);
recv(remote_socket,response,sizeof(response),0);
XmHTMLTextSetString(result_widget,response);
// printf("%d\n",val);
// exit(2);
}

83
url_manipulation.c Normal file
View File

@@ -0,0 +1,83 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define DEFAULT_PATH "/"
char* url_to_proto(char* url) {
size_t length = strlen(url);
int sentinel = 0;
char* to_return = malloc(sizeof(char) * 10);
for (int i=0;i<length;i++) {
if (*(url + (sizeof(char) * i)) == ':') { /* If the character is ':' ...*/
sentinel = i; /* ...Then we stop scanning the string */
}
}
strncpy(to_return,url,sentinel);
return to_return;
}
char* url_to_hostname(char* url) {
/* This function relies on a hack-y assumption: that, given a string
that represents a URL (e.g. https://www.example.com/index.html), the length of the host portion
of the address is the distance between the second slash (:/'/') and the third ('/'index.html),
or the second slash and the end of the string, if the path isn't present. */
size_t length = strlen(url);
char* protocol = url_to_proto(url);
char* temp_substring = malloc(sizeof(char) * 256);
char* to_return = malloc(sizeof(char) * 256);
/* This is a very roundabout way of saying that, starting from a certain point in the
URL (length of protocol string + 3 (':','/','/'), we are copying the rest of the string into another
substring. */
strncpy(temp_substring,url+(strlen(protocol) + 3),(length - (strlen(protocol) + 3)));
/* Now we need to substring _this_ substring, by returning only the part before the next slash. */
int sentinel = 0;
for (int i=0;i<strlen(temp_substring);i++) {
if (*(temp_substring+(sizeof(char) * i)) == '/') {
sentinel = i;
}
}
if (sentinel == 0) { /* If there are no more slashes */
return temp_substring;
}
strncpy(to_return,temp_substring,sentinel);
free(temp_substring);
return to_return;
}
char* url_to_path(char* url) {
/* Assumption: The path starts from the third slash to the end of the string. */
int str_len = strlen(url);
int sentinel = 0;
int num_slashes = 0;
char* to_return = malloc(sizeof(char) * 512);
for (int i=0;i<strlen(url);i++) {
if (*(url + (sizeof(char) * i)) == '/') {
num_slashes++;
sentinel = i;
}
if (num_slashes == 3) {
break;
}
}
if (num_slashes < 3 || sentinel == str_len) { /* If we don't find the requisite number of slashes after
parsing the string OR the third slash is at the end of the string
("e.g. www.example.com/")... */
strcpy(to_return,DEFAULT_PATH); /* ...Then return the default path - "/" */
return to_return;
}
/* Copy all bytes of the string, starting from the third slash */
strncpy(to_return, url+(sizeof(char) * sentinel), strlen(url)-(sizeof(char) * sentinel));
return to_return;
}

16
url_manipulation.h Normal file
View File

@@ -0,0 +1,16 @@
#include <string.h>
/* url_to_proto - This takes in a URL, and returns the protocol.
Example - 'https://example.org' would return 'https'. */
char* url_to_proto(char* url);
/* url_to_hostname - This takes in a URL, and returns the hostname.
Example - 'https://example.org' would return 'example.org'. */
char* url_to_hostname(char* url);
/* url_to_path - Returns the path portion of a URL.
Example: 'https://example.org/index.html' will return '/index.html'
Any empty path will also return '/index.html' */
char* url_to_path(char* url);