-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
201 lines (158 loc) · 5.09 KB
/
server.c
File metadata and controls
201 lines (158 loc) · 5.09 KB
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
// A simple server in the internet domain using TCP
// The port number is passed as an argument
// implementation code has been taken from sockets Week 7 tutorial
#define _POSIX_C_SOURCE 200112L
#include <netdb.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <strings.h>
#include <string.h>
#include <signal.h>
#include "server-helper.h"
#define MULTITHREADED
#define IMPLEMENTS_IPV6
// max connections
#define BACKLOG 10
#define MAX_PATH 2048
// struct for passing thread arguments
struct serverPThread{
int nsockfd;
char *root_path;
};
// thread function for handling requests on a thread
void *thread_action(void* arg);
// starts server after receiving the protocol, port and root path
void start_server(char protocol, const char *port, char* rootpath);
int main(int argc, char** argv) {
if (argc < 4) {
fprintf(stderr, "ERROR, missing info\n");
exit(EXIT_FAILURE);
}
char protocol = *argv[1];
const char *port = argv[2];
char rootpath[MAX_PATH];
strcpy(rootpath,argv[3]);
// start the server by sending the protocol, port and rootpath
start_server(protocol, port, rootpath);
return 0;
}
// starts server after receiving the protocol, port and root path
void start_server(char protocol, const char *port, char* rootpath){
int sockfd, enable, s;
struct addrinfo hints, *res, *p;
struct sockaddr_storage client_addr;
socklen_t client_addr_size;
// Create address we're going to listen on (with given port number)
memset(&hints, 0, sizeof hints);
// IPv4
if(protocol=='4'){
hints.ai_family = AF_INET;
}
// IPv6
else if(protocol=='6'){
hints.ai_family = AF_INET6;
}
// TCP
hints.ai_socktype = SOCK_STREAM;
// for bind, listen, accept
hints.ai_flags = AI_PASSIVE;
// node (NULL means any interface), service (port), hints, res
s = getaddrinfo(NULL, port, &hints, &res);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
// for ipv4
if(protocol == '4'){
p = res;
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
}
// for ipv6
if(protocol == '6'){
for(p = res; p != NULL; p = p->ai_next){
if (p->ai_family == AF_INET6
&& (sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) < 0 ){
// socket creation was attempted but failed
perror("socket");
exit(EXIT_FAILURE);
}
}
// Create socket
}
if (sockfd < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// prevent SIGNPIPE signal error
signal(SIGPIPE, SIG_IGN);
// Reuse port if possible
enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
// Bind address to the socket
if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
close(sockfd);
perror("bind");
exit(EXIT_FAILURE);
}
// Listen on socket - means we're ready to accept connections,
// incoming connection requests will be queued, man 3 listen
if (listen(sockfd, BACKLOG) < 0) {
close(sockfd);
perror("listen");
exit(EXIT_FAILURE);
}
freeaddrinfo(res);
client_addr_size = sizeof client_addr;
while(1){
struct serverPThread *serverThread = malloc(sizeof(struct serverPThread));
if (serverThread == NULL) {
perror("thread");
exit(EXIT_FAILURE);
}
// Accept a connection - blocks until a connection is ready to be accepted
// Get back a new file descriptor to communicate on
// this file descriptor is passed to the server thread arguments
serverThread->nsockfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_addr_size);
if (serverThread->nsockfd< 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// store the root path in the server thread arguments
serverThread->root_path = rootpath;
// thread identifier
pthread_t tid;
// creates the thread
if(pthread_create(&tid, NULL, thread_action, serverThread)!=0){
perror("pthread_create() error");
}
printf("with thread %ld\n", (long int) tid);
}
}
// thread function to handle requests on a thread
void *thread_action(void* serverThread){
// detachs thread to free its resources/ agruments
pthread_detach(pthread_self());
int newsockfd;
char root[BUFFER_LEN];
char filepath[BUFFER_LEN];
// get the thread arguments
strcpy(root,((struct serverPThread *) serverThread)->root_path);
newsockfd = ((struct serverPThread *) serverThread)->nsockfd;
free(serverThread);
// parse and serves a valid response to a valid request
if(parseReq(newsockfd, filepath, root) > 0){
serveReq(newsockfd,root, filepath);
}
close(newsockfd);
serverThread = NULL;
pthread_exit(NULL);
return NULL;
}