Skip to content

Instantly share code, notes, and snippets.

@SergiyOsadchyy
Created February 4, 2017 12:16
Show Gist options
  • Save SergiyOsadchyy/dc8720620fb9af63d19d4115f94457ed to your computer and use it in GitHub Desktop.
Save SergiyOsadchyy/dc8720620fb9af63d19d4115f94457ed to your computer and use it in GitHub Desktop.
Linux_Lab_41
// Linux_Lab_41
//
// Created by Sergiy on 28.01.17.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#define HOST "www.google.com.ua"
#define PAGE "/"
#define PORT 80
int createTCPsocket();
char *getIP(char *host);
int main(int argc, const char * argv[])
{
char *my_URL;
char *tokenPage;
size_t length_URL_without_page;
char *my_URL_without_page;
char *tokenPort;
size_t length_URL_without_page_and_port;
char *my_URL_without_page_and_port;
char *host;
char *port_text;
char *port_text_temp;
char *page;
int port;
char *query;
char *queryTemplate;
int sock;
char *ip;
struct sockaddr_in *server;
int tmpres;
char buf[BUFSIZ+1];
// Build query -START-
if (argc == 1)
{
printf("For usage you have to provide URL (for example: www.gu.net:8101/leased_r.html)\n");
exit(2);
}
my_URL = argv[1];
//my_URL = "www.google.com";
//my_URL = "www.linux.die.net/man/3/strchr";
printf("The URL is %s\n", my_URL);
// Take page from URL -START-
tokenPage = strchr(my_URL, '/');
if (tokenPage == NULL)
{
printf("There is no path provided.\n");
page = "/";
printf("Default path is %s\n", page);
}
else
{
page = malloc(strlen(tokenPage));
memcpy(page, tokenPage, strlen(tokenPage));
}
printf("The page is %s\n", page);
// Take page from URL -END-
//----------------
// define URL without page -START-
if (tokenPage == NULL)
{
my_URL_without_page = malloc(strlen(my_URL));
memcpy(my_URL_without_page, my_URL, strlen(my_URL));
}
else
{
length_URL_without_page = strlen(my_URL) - strlen(tokenPage);
my_URL_without_page = malloc(length_URL_without_page);
memcpy(my_URL_without_page, my_URL, length_URL_without_page);
}
//printf("URL without page is %s\n", my_URL_without_page);
// define URL without page -END-
//-------------
// take port_text from URL -START-
tokenPort = strchr(my_URL_without_page, ':');
if (tokenPort == NULL)
{
printf("There is no port provided.\n");
port_text = "";
port = 80;
printf("Default port is %d\n", port);
}
else
{
port_text = malloc(strlen(tokenPort));
memcpy(port_text, tokenPort, strlen(tokenPort));
//get port from port_text
port_text_temp = port_text +1;
port = atoi(port_text_temp);
printf("The port is %d\n", port);
}
printf("The port(text) is %s\n", port_text);
// take port_text from URL -END-
//-------------
// define URL without port -START-
if (tokenPort == NULL)
{
my_URL_without_page_and_port = malloc(strlen(my_URL_without_page));
memcpy(my_URL_without_page_and_port, my_URL_without_page, strlen(my_URL_without_page));
}
else
{
length_URL_without_page_and_port = strlen(my_URL_without_page) - strlen(tokenPort);
my_URL_without_page_and_port = malloc(length_URL_without_page_and_port);
memcpy(my_URL_without_page_and_port, my_URL_without_page, length_URL_without_page_and_port);
}
//printf("URL without page and port is %s\n", my_URL_without_page_and_port);
// define URL without port -END-
//-------------
// take host from URL -START-
host = my_URL_without_page_and_port;
printf("host is %s\n", host);
// take host from URL -END-
//-------------
queryTemplate = "GET %s HTTP/1.1\r\nHost: %s%s\r\n\r\n";
query = (char *)malloc(strlen(queryTemplate) + strlen(page) + strlen(host) + strlen(port_text) - 5);
sprintf(query, queryTemplate, page, host, port_text);
fprintf(stderr, "Query is:\n<<START>>\n\n%s<<END>>\n", query);
// Build query -END-
//-----------
// Create socket -START-
sock = createTCPsocket();
//printf("File descriptor that refers to endpoint is %d\n\n", sock);
// Create socket -END-
//-----------
// Get IP -START-
ip = getIP(host);
fprintf(stderr, "IP is %s\n", ip);
// Get IP -END-
//---------
// Setup server -START-
server = (struct sockaddr_in *)malloc(sizeof(struct sockaddr_in *)) ;
server->sin_family = AF_INET;
//server->sin_addr.s_addr = INADDR_ANY;
server->sin_port = htons(port);
tmpres = inet_pton(AF_INET, ip, (void *)(&(server->sin_addr.s_addr)));
if (tmpres < 0)
{
perror("Can't set remote->sin_addr.s_addr");
exit(1);
}
else if (tmpres == 0)
{
fprintf("%s is not a valid IP address\n", ip);
exit(1);
}
// Setup server -END-
//-----------
// Connect -START-
if (connect(sock, (struct sockaddr *)server, sizeof(struct sockaddr)) < 0)
{
perror("Could not connect");
exit(1);
}
// Connect -END-
//-----------
// Send the query to the server -START-
int sent = 0;
while (sent < strlen(query))
{
tmpres = send(sock, query+sent, strlen(query) - sent, 0);
if (tmpres == -1)
{
perror("Can't send query");
exit(1);
}
sent += tmpres;
}
// Send the query to the server -END-
//receive the page -START-
memset(buf, 0, sizeof(buf));
int htmlstart = 0;
char *htmlcontent;
while ((tmpres = recv(sock, buf, BUFSIZ, 0)) > 0)
{
if (htmlstart == 0)
{
// under certain conditions this will not work.
// If the \r\n\r\n part is splitted into two messages
// it will fail to detect the beginning of HTML content
htmlcontent = strstr(buf, "\r\n\r\n");
if (htmlcontent != NULL)
{
htmlstart = 1;
htmlcontent += 4;
}
}
else
{
htmlcontent = buf;
}
if (htmlstart)
{
fprintf(stdout, htmlcontent);
}
memset(buf, 0, tmpres);
}
if (tmpres < 0)
{
perror("Error receiving data");
}
//receive the page -END-
//------------
//free(page);
//free(my_URL_without_page);
free(query);
free(server);
free(ip);
close(sock);
return 0;
}
int createTCPsocket()
{
int sock;
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0 )
{
perror("Failed to create socket\n");
exit(1);
}
return sock;
}
char *getIP(char *host)
{
struct hostent *hent;
int iplen = 15; //XXX.XXX.XXX.XXX
char *ip = (char *)malloc(iplen + 1);
memset(ip, 0, iplen+1);
char *host_without_www = host + 4;
//printf("\nHost without www is %s\n", host_without_www);
if ((hent = gethostbyname(host_without_www)) == NULL)
{
perror("Can't get IP\n");
exit(1);
}
if ((inet_ntop(AF_INET, (void *)hent->h_addr_list[0], ip, iplen)) == NULL)
{
perror("\nCan't resolve host\n");
printf("\n");
exit(1);
}
return ip;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment