-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
176 lines (158 loc) · 4.36 KB
/
server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#define HTTPPORT 80
char *findIP(char *host);
int main( int argc, char *argv[] ) {
int sockfd, newsockfd, portno, clilen, remSock, htmlStart, i = 0;
char buffer[8192], *tok_str, *ip, *host, *content, *request = "GET / HTTP/1.1\r\n\r\n";
char buf[BUFSIZ +1];
struct sockaddr_in serv_addr, cli_addr, rem_addr;
int n, m;
FILE *fp;
/* Creating a socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("ERROR while opening the socket");
exit(1);
}
/* Initializing the socket structure */
portno = 45455;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Binding the socket to the port*/
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("ERROR while binding");
exit(1);
}
/* Begin listening for incoming connections
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
/* Accept connection request from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) {
printf("ERROR on accept");
exit(1);
}
/* Begin communication */
bzero(buffer,8192);
n = read( newsockfd,buffer,8192 );
if (n < 0) {
printf("ERROR reading from socket");
exit(1);
}
tok_str = strtok(buffer, " /"); //buffer is the message read from the client
while ((tok_str != NULL)&& (i < 2))// ' ' and '/' are delimiters
{
//printf("%s\n", tok_str);
if (i == 1)
host = tok_str; //save the 2nd token as the webpage
tok_str = strtok(NULL, " /");
i++;
}
//printf("REQUESTED PAGE: %s\n", host);
fp = fopen (host, "r");
if (fp == NULL) //if file is not on server
{
fp = fopen(host, "w"); //open file for writing
printf("%s", request); //Print get request
ip = findIP(host); //find the ip of the host the client requested
printf("%s", ip);
remSock = socket(AF_INET, SOCK_STREAM, 0); //create a socket to connect to the remote server
if (remSock < 0)
{
printf("ERROR creating remote socket");
exit(1);
}
rem_addr.sin_family = AF_INET;
rem_addr.sin_addr.s_addr = inet_addr(ip);
rem_addr.sin_port = htons(HTTPPORT); //connect on port 80 for http request
if (connect(remSock, (struct sockaddr*)&rem_addr, sizeof(rem_addr)) < 0)
{
printf("ERROR attempting to connect to remote server");
exit(1);
}
n = write(remSock, request, strlen(request)); //send get request to remote server
if (n < 0)
{
printf("ERROR sending http request");
exit(1);
}
memset(buf, 0, sizeof(buf));
htmlStart = 1;
while ((n = recv(remSock, buf, BUFSIZ, 0)) > 0) //receive response from server
{
content = buf; //store response in content variable
if (htmlStart)
{
//printf("%s", content);
m = write(newsockfd, content, strlen(content)); //write content to client
fputs(content, fp); //write content to file for later access
fflush(fp);
}
memset (buf, 0, n);
}
if (n < 0)
{
printf("ERROR reading response");
exit(1);
}
close(remSock);
close(newsockfd);
close(sockfd);
}
else
{
content = (char*) malloc(BUFSIZ); //allocate buffer to read from file
while((fgets(content, BUFSIZ, fp)) != NULL)
{
//printf("%s", content);
n = write(newsockfd, content, strlen(content));
}
free(content);
}
if (n < 0) {
printf("ERROR writing to socket");
exit(1);
}
close(sockfd);
close(newsockfd);
fflush(fp);
fflush(stdout);
return 0;
}
//function to find the ip of a given host
char *findIP(char *host)
{
struct hostent *hent;
int iplen = 15;
char *ip = (char *) malloc(iplen + 1);
memset(ip, 0, iplen +1);
if ((hent = gethostbyname(host)) == NULL)
{
herror("Can't get host IP");
exit(1);
}
if (inet_ntop(AF_INET, (void *) hent->h_addr_list[0], ip, iplen) == NULL)
{
perror("Unable to resolve host");
exit(1);
}
return ip;
};