-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.c
299 lines (244 loc) · 7.96 KB
/
util.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* CSci 4061 F2014 Assignment 5
* Section 3
* Date: 12/9/2014
* Name: Jialun Jiang, Yaozhang Xu, Shang Ju
* ID: 4467970, 4944346, 4455103
*/
#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
static int master_fd = -1;
pthread_mutex_t accept_con_mutex = PTHREAD_MUTEX_INITIALIZER;
// this function takes a hostname and returns the IP address
int lookup_host (const char *host)
{
struct addrinfo hints, *res;
int errcode;
char addrstr[100];
void *ptr;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo (host, NULL, &hints, &res);
if (errcode != 0)
{
perror ("getaddrinfo");
return -1;
}
printf ("Host: %s\n", host);
while (res)
{
inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr, 100);
switch (res->ai_family)
{
case AF_INET:
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
break;
case AF_INET6:
ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
break;
}
inet_ntop (res->ai_family, ptr, addrstr, 100);
printf ("IPv%d address: %s (%s)\n", res->ai_family == PF_INET6 ? 6 : 4,
addrstr, res->ai_canonname);
res = res->ai_next;
}
return 0;
}
int makeargv(const char *s, const char *delimiters, char ***argvp) {
int error;
int i;
int numtokens;
const char *snew;
char *t;
if ((s == NULL) || (delimiters == NULL) || (argvp == NULL)) {
errno = EINVAL;
return -1;
}
*argvp = NULL;
snew = s + strspn(s, delimiters);
if ((t = malloc(strlen(snew) + 1)) == NULL)
return -1;
strcpy(t,snew);
numtokens = 0;
if (strtok(t, delimiters) != NULL)
for (numtokens = 1; strtok(NULL, delimiters) != NULL; numtokens++) ;
if ((*argvp = malloc((numtokens + 1)*sizeof(char *))) == NULL) {
error = errno;
free(t);
errno = error;
return -1;
}
if (numtokens == 0)
free(t);
else {
strcpy(t,snew);
**argvp = strtok(t,delimiters);
for (i=1; i<numtokens; i++)
*((*argvp) +i) = strtok(NULL,delimiters);
}
*((*argvp) + numtokens) = NULL;
return numtokens;
}
void freemakeargv(char **argv) {
if (argv == NULL)
return;
if (*argv != NULL)
free(*argv);
free(argv);
}
/**********************************************
* init
- port is the number of the port you want the server to be
started on
- initializes the connection acception/handling system
- YOU MUST CALL THIS EXACTLY ONCE (not once per thread,
but exactly one time, in the main thread of your program)
BEFORE USING ANY OF THE FUNCTIONS BELOW
- if init encounters any errors, it will call exit().
************************************************/
enum boolean {FALSE, TRUE};
void init(int port) {
struct sockaddr_in address;
int enable = 1;
if ((master_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("ERROR: Failed to open socket.");
exit(-1);
}
if (setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, (char*) &enable, sizeof(int)) == -1) {
perror("ERROR: Failed to set socket options.");
exit(-1);
}
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
if (bind(master_fd, (struct sockaddr*) &address, sizeof(address)) == -1) {
perror("ERROR: Failed to bind address.");
exit(-1);
}
if(listen(master_fd, 5) == -1) {
perror("ERROR: Failed to start listening.");
exit(-1);
}
}
/**********************************************
* accept_connection - takes no parameters
- returns a file descriptor for further request processing.
DO NOT use the file descriptor on your own -- use
get_request() instead.
- if the return value is negative, the thread calling
accept_connection must exit by calling pthread_exit().
***********************************************/
int accept_connection(void) {
struct sockaddr address;
int sSock, addressLen;
addressLen = sizeof(address);
if(pthread_mutex_lock(&accept_con_mutex) != 0) {
perror("ERROR: Failed to lock mutex.");
exit(-1);
}
sSock = accept(master_fd, (struct sockaddr*) &address, &addressLen);
if(pthread_mutex_unlock(&accept_con_mutex) != 0) {
perror("ERROR: Failed to unlock mutex.");
exit(-1);
}
if (sSock == -1) {
perror("ERROR: Failed to accept connection");
exit(-1);
}
return sSock;
}
/**********************************************
* get_request
- parameters:
- fd is the file descriptor obtained by accept_connection()
from where you wish to get a request
- filename is the location of a character buffer in which
this function should store the requested filename. (Buffer
should be of size 1024 bytes.)
- returns 0 on success, nonzero on failure. You must account
for failures because some connections might send faulty
requests. This is a recoverable error - you must not exit
inside the thread that called get_request. After an error, you
must NOT use a return_request or return_error function for that
specific 'connection'.
************************************************/
int get_request(int fd, char *filename) {
FILE* fin;
size_t length;
char* fname;
char** argv;
if ((fin = fdopen(fd, "r")) == NULL) {
return -1;
}
if (getline(&fname, &length, fin) == -1) {
return -1;
}
if (makeargv(fname, " ", &argv) < 1) {
return -1;
}
strncpy(filename, argv[1], 1024);
freemakeargv(argv);
return 0;
}
/**********************************************
* return_result
- returns the contents of a file to the requesting client
- parameters:
- fd is the file descriptor obtained by accept_connection()
to where you wish to return the result of a request
- content_type is a pointer to a string that indicates the
type of content being returned. possible types include
"text/html", "text/plain", "image/gif", "image/jpeg" cor-
responding to .html, .txt, .gif, .jpg files.
- buf is a pointer to a memory location where the requested
file has been read into memory (the heap). return_result
will use this memory location to return the result to the
user. (remember to use -D_REENTRANT for CFLAGS.) you may
safely deallocate the memory after the call to
return_result (if it will not be cached).
- numbytes is the number of bytes the file takes up in buf
- returns 0 on success, nonzero on failure.
************************************************/
int return_result(int fd, char *content_type, char *buf, int numbytes) {
char* result;
if ((result = (char*)calloc(numbytes + 1000, sizeof(char))) == NULL) {
return -1;
}
sprintf(result, "HTTP/1.1 200 OK\nContent-Type: %s\nContent-Length: %d\nConnection: Close\n\n", content_type, numbytes);
write(fd, result, strlen(result));
write(fd, buf, numbytes);
close(fd);
return 0;
}
/**********************************************
* return_error
- returns an error message in response to a bad request
- parameters:
- fd is the file descriptor obtained by accept_connection()
to where you wish to return the error
- buf is a pointer to the location of the error text
- returns 0 on success, nonzero on failure.
************************************************/
int return_error(int fd, char *buf) {
char* result;
int numbytes = strlen(buf);
if ((result = (char*)calloc(numbytes + 1000, sizeof(char))) == NULL) {
return -1;
}
sprintf(result, "HTTP/1.1 404 Not Found\nContent-Type: text/html\nContent-Length: %d\nConnection: Close\n\n", numbytes);
strncat(result, buf, numbytes);
write(fd, result, strlen(result));
return 0;
}