-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
253 lines (203 loc) · 7.97 KB
/
Copy pathserver.c
File metadata and controls
253 lines (203 loc) · 7.97 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
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
#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <memory.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <unistd.h>
#include "common.h"
#include "server_conf.h"
int setup_listener(char const* port) {
int listener_fd;
int yes = 1;
struct addrinfo hint;
struct addrinfo* server_info;
int exit_code;
memset(&hint, 0, sizeof(hint));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;
hint.ai_flags = AI_PASSIVE;
ERR_WRAPPER((exit_code = getaddrinfo(NULL, port, &hint, &server_info)) != 0, gai_strerror(exit_code), error)
for(struct addrinfo* p = server_info; p != NULL; p = p->ai_next) {
listener_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener_fd < 0) {
continue;
}
setsockopt(listener_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); // for no error when restarting
if (bind(listener_fd, p->ai_addr, p->ai_addrlen) < 0) {
close(listener_fd);
continue;
}
if (listen(listener_fd, BACKLOG) == -1) {
close(listener_fd);
continue;
}
freeaddrinfo(server_info);
return listener_fd;
}
error:
freeaddrinfo(server_info);
return -1;
}
void dump_client(struct sockaddr_in client_addr, int n) {
char addr[INET_ADDRSTRLEN];
inet_ntop(client_addr.sin_family, &client_addr.sin_addr, addr, sizeof addr);
printf("+ Thread %d: Got connection from %s\n", n, addr);
}
void send_response(int socket, int res) {
if (send(socket, &res, sizeof(res), 0) == -1) {
perror("- Couldn't send response");
close(socket);
}
}
void signal_error_and_close(int socket, char const* msg) {
perror(msg);
send_response(socket, -1);
close(socket);
}
#define SIGCLS_WRAPPER(predicate, sock, msg, label) \
if (predicate) { \
signal_error_and_close(sock, msg); \
goto label; \
}
ssize_t recv_size(int socket) {
uint32_t size;
ssize_t read = recv(socket, &size, sizeof(uint32_t), 0);
SIGCLS_WRAPPER(read == -1, socket, "- Couldn't receive (size)", error)
if (read == 0) { // client is already dead
close(socket);
return -1;
}
size = ntohl(size);
return size;
error:
return -1;
}
int recv_all(int socket, char* buf, size_t len) {
size_t total = 0;
size_t left = len;
size_t n;
while(total < len) {
n = recv(socket, buf + total, left, 0);
SIGCLS_WRAPPER(n == -1, socket, "- Couldn't receive (content)", error)
total += n;
left -= n;
}
return 0;
error:
return -1;
}
_Noreturn void* server(void* data_) {
struct thread_data* data = data_;
int thread_num = data->num;
int listener_fd = data->listener_fd;
int epoll_fd = data->epoll_fd;
char const* path = data->path;
free(data);
struct epoll_event event;
struct epoll_event events[MAX_EVENTS];
int client_fd;
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
uint32_t seed = time(0) + thread_num; // for thread-safe random
for (;;) {
int event_cnt = epoll_wait(epoll_fd, events, MAX_EVENTS, TIMEOUT);
for (int i = 0; i < event_cnt; ++i) {
if (events[i].data.fd == listener_fd) {
client_fd = accept(listener_fd, (struct sockaddr*)&client_addr, &len);
ERRNO_WRAPPER(client_fd == -1, "- Couldn't accept connection (accept)", continue_)
event.events = EPOLLIN | EPOLLONESHOT | EPOLLET;
event.data.fd = client_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &event) == -1) {
perror("- Couldn't accept connection (epoll_ctl)");
close(client_fd);
continue;
}
dump_client(client_addr, thread_num);
} else {
client_fd = events[i].data.fd;
ssize_t filename_size = recv_size(client_fd);
if (filename_size == -1) { continue; }
char* filename = malloc(filename_size + 1);
SIGCLS_WRAPPER(filename == NULL, client_fd, "- Couldn't allocate memory (for user's filename)", continue_)
ERR_WRAPPER_NOMSG(recv_all(client_fd, filename, filename_size) == -1, cleanup_filename)
filename[filename_size] = '\0';
ssize_t size = recv_size(client_fd);
ERR_WRAPPER_NOMSG(size == -1, cleanup_filename)
// len(path) + len('/') + len(max int) + len('_') + len(filename) + len('\0')
char* file = malloc(strlen(path) + 1 + 10 + 1 + strlen(filename) + 1);
SIGCLS_WRAPPER(file == NULL, client_fd, "- Couldn't allocate memory (for full filename)", cleanup_filename)
sprintf(file, "%s/%d_%s", path, rand_r(&seed), filename);
int fd = open(file, O_RDWR | O_CREAT, S_IRWXU);
SIGCLS_WRAPPER(fd == -1, client_fd, "- Couldn't create file (open)", cleanup_file)
SIGCLS_WRAPPER(ftruncate(fd, size) == -1, client_fd, "- Couldn't create file (truncate)", cleanup_file)
char* buf = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
SIGCLS_WRAPPER(buf == MAP_FAILED, client_fd, "- Couldn't mmap file", cleanup_file)
ERR_WRAPPER_NOMSG(recv_all(client_fd, buf, size) == -1, cleanup_mmap)
printf("+ Thread %d: Got file %s\n", thread_num, file);
send_response(client_fd, 0);
event.events = EPOLLIN | EPOLLONESHOT | EPOLLET;
event.data.fd = client_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client_fd, &event) == -1) {
perror("- Couldn't rearm connection");
close(client_fd);
}
cleanup_mmap:
munmap(buf, size);
cleanup_file:
free(file);
cleanup_filename:
free(filename);
}
continue_: ;
}
}
}
int main(int argc, char** argv) {
char const* port = DEFAULT_PORT;
char const* path = DEFAULT_PATH;
if (argc >= 2) {
port = argv[1];
if (argc == 3) {
path = argv[2];
}
}
int listener_fd = setup_listener(port);
ERR_WRAPPER(listener_fd == -1, "- Couldn't initialize server (socket)", error);
int epoll_fd = epoll_create1(0);
ERRNO_WRAPPER(epoll_fd == -1, "- Couldn't initialize server (epoll)", cleanup_listener)
pthread_t thread_pool[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; ++i) {
struct thread_data* data = malloc(sizeof(struct thread_data)); // thread is responsible for freeing
ERR_WRAPPER(data == NULL, "- Couldn't initialize server (threads)", cleanup_epoll)
data->num = i;
data->listener_fd = listener_fd;
data->epoll_fd = epoll_fd;
data->path = path;
ERRNO_WRAPPER(pthread_create(&thread_pool[i], NULL, server, data) != 0,
"- Couldn't initialize server (threads)", cleanup_epoll) // created threads will die when main() returns
}
struct thread_data* data = malloc(sizeof(struct thread_data));
ERR_WRAPPER(data == NULL, "- Couldn't initialize server (threads)", cleanup_epoll)
data->num = NUM_THREADS;
data->listener_fd = listener_fd;
data->epoll_fd = epoll_fd;
data->path = path;
struct epoll_event event;
event.events = EPOLLIN | EPOLLEXCLUSIVE;
event.data.fd = listener_fd;
ERRNO_WRAPPER(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listener_fd, &event) == -1,
"- Couldn't initialize server (epoll_ctl)", cleanup_epoll)
printf("+ Started on port %s\n", port);
server(data); // no return
cleanup_epoll:
close(epoll_fd);
cleanup_listener:
close(listener_fd);
error:
return ERROR;
}