-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
164 lines (140 loc) · 4.5 KB
/
main.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
// Wiktor Garbarek 291963
#define _DEFAULT_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/time.h>
#include <sys/select.h>
#include <fcntl.h>
#include <errno.h>
#include "utils.h"
void parse_args(int argc, char *argv[], uint16_t *port, char directory[][4096]){
if (argc != 3){
printf("Some arguments are missing - usage %s PORT DIRECTORY\n", argv[0]);
exit(EXIT_FAILURE);
}
*port = atoi(argv[PORT_I]);
struct stat checker;
if (stat(argv[DIRECTORY_I], &checker) == -1 || !S_ISDIR(checker.st_mode)){
printf("%s does not exist or is not a directory\n", argv[DIRECTORY_I]);
exit(EXIT_FAILURE);
}
if (sprintf((char *) directory, "%s", argv[DIRECTORY_I]) <= 0){
printf("Invalid directory name\n");
exit(EXIT_FAILURE);
}
}
int init_socket(uint16_t port){
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
handle_error("socket");
}
struct sockaddr_in server_address;
bzero (&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind (sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
handle_error("bind");
}
if (listen(sockfd, 64) < 0){
handle_error("listen");
}
return sockfd;
}
int accept_connection(int sockfd){
struct sockaddr_in client_address;
socklen_t len = sizeof(client_address);
int conn_sockfd = accept(sockfd, (struct sockaddr *) &client_address, &len);
if (conn_sockfd < 0){
handle_error("accept");
}
char ip_address[20];
inet_ntop (AF_INET, &client_address.sin_addr, ip_address, sizeof(ip_address));
printf("New client %s:%d\n", ip_address, ntohs(client_address.sin_port));
return conn_sockfd;
}
void set_timeout(struct timeval *timeout){
timeout->tv_sec = 1;
timeout->tv_usec = 0;
}
int is_still_alive(char *connection, struct timeval *timeout){
if(strcmp(connection, "close") == 0){
return 0;
} else {
set_timeout(timeout);
return 1;
}
}
int select_wrapper(int conn_sockfd, struct timeval *timeout){
fd_set descriptors;
FD_ZERO (&descriptors);
FD_SET (conn_sockfd, &descriptors);
int keep_alive = select(conn_sockfd+1, &descriptors, NULL, NULL, timeout);
if (keep_alive < 0){
handle_error("select");
}
return keep_alive;
}
static int sockfd;
void close_socket(){
printf("Stopping server\n");
if (close(sockfd) < 0){
handle_error("close");
}
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]){
uint16_t port;
char directory[4096];
parse_args(argc, argv, &port, &directory);
sockfd = init_socket(port);
struct sigaction act;
act.sa_handler = close_socket;
sigaction(SIGINT, &act, NULL);
for(;;){
int conn_sockfd = accept_connection(sockfd);
struct timeval timeout;
set_timeout(&timeout);
int keep_alive = 1;
while(keep_alive){
keep_alive = select_wrapper(conn_sockfd, &timeout);
if (keep_alive == 0){
break;
}
char buffer[IP_MAXPACKET];
bzero(buffer, IP_MAXPACKET);
int bytes_received = recv(conn_sockfd, buffer, IP_MAXPACKET, MSG_DONTWAIT);
if(bytes_received > 0){
struct request request;
bzero(&request, sizeof(request));
parse_request(&request, buffer);
printf("Parsed request:\n\t%s\n\t%s\n\t%s\n", request.endpoint, request.host, request.connection);
struct response response;
prepare_response(&response, &request, directory);
respond(&response, conn_sockfd);
printf("Responded with code %d\n", response.code);
keep_alive = is_still_alive(request.connection, &timeout);
}
if (bytes_received == 0){
keep_alive = 0;
} else if (bytes_received < 0 && errno != EWOULDBLOCK){
handle_error("recv");
}
}
if (close(conn_sockfd) < 0){
handle_error("close");
}
}
close_socket();
return 0;
}