-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
executable file
·423 lines (364 loc) · 10.3 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* comments:
* server.c - a concurrent server with i/o multiplexing, and two transport protocols
* author - Braha Petru Bogdan - <[email protected]> (c)
* compilation command: gcc server.c -std=c11 -pthread -o sv
* run command example: ./sv 'schedule.txt'
* run command example: ./sv
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include "include/communication.h"
#include "include/error.h"
#include "include/printer.h"
#include "include/route.h"
#include "include/server_txt.h" //!
#include "include/server_api.h"
typedef struct
{
fd_set container;
int count;
} rr_fd;
//------------------------------------------------
void check_txt(int *argc, char *argv[],
char *const path_txt);
bool running_condition();
int maintenance(pthread_t *th0, pthread_t *th1,
const int argc,
char *const path_txt);
// three threads
void *udp_communication(void *);
void *multiplexing(void *);
int main(int argc, char *argv[]);
//------------------------------------------------
#define path_thread "include/dev/key.txt"
#define path_stable "include/data/default schedule.txt"
#define path_binary "include/dev/write_txt" //!
#define name_random "random schedule.txt"
#define path_to_build "include/data/"
#define path_location "include/data/random schedule.txt"
rr_fd descriptors;
int sd_udp;
//------------------------------------------------
int main(int argc, char *argv[])
{
char path_txt[BYTES_PATH_MAX];
strcpy(path_txt, path_to_build);
check_txt(&argc, argv, path_txt);
read_txt(path_txt);
call(printf("%d routes are valid.\n", count_routes));
// server address
const uint16_t port = 2970;
struct sockaddr_in skadd_server;
skadd_server.sin_family = AF_INET;
skadd_server.sin_addr.s_addr = htonl(INADDR_ANY);
skadd_server.sin_port = htons(port);
FD_ZERO(&descriptors.container);
// tcp
const int ONE_CLIENT_ONLY = 1;
const int TWO_CLIENT_ONLY = 2;
const int COUNT_CLIENT_MAX = 1024;
int sd_listen =
socket(AF_INET,
SOCK_STREAM | SOCK_NONBLOCK, 0);
call_var(sd_listen);
call(bind(sd_listen,
(struct sockaddr *)&skadd_server,
sizeof(struct sockaddr)));
call(listen(sd_listen, COUNT_CLIENT_MAX));
int option = 1;
call(setsockopt(sd_listen,
SOL_SOCKET, SO_REUSEADDR,
&option, sizeof(option)));
// udp
sd_udp = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
call_var(sd_udp);
call(bind(sd_udp,
(struct sockaddr *)&skadd_server,
sizeof(struct sockaddr)));
// loops: i/o multiplexing and non-blocking accepts
pthread_t multiplexing_thread;
call0(pthread_create(&multiplexing_thread, NULL,
&multiplexing, NULL));
pthread_t udp_thread;
call0(pthread_create(&udp_thread, NULL,
&udp_communication, NULL));
call(printf("the server is online.\n\n"));
bool maintenance_flag = true;
for (; running_condition();)
{
// maintenance time
time_t t = time(NULL);
struct tm tm = *localtime(&t);
if (0 == tm.tm_hour && 0 == tm.tm_min)
{
if (maintenance_flag)
{
printf("maintenance started.\n");
if (ERR_CODE ==
maintenance(&multiplexing_thread,
&udp_thread, argc, path_txt))
error("maintenance() failed");
else
printf("maintenance finished.\n\n");
maintenance_flag = false;
}
}
else
maintenance_flag = true;
// additional error check
if (errno)
{
warning(strerror(errno));
errno = 0;
}
// accepted client
int sd_client = accept(sd_listen, NULL, NULL);
if (errno && EWOULDBLOCK != errno)
error(strerror(errno));
errno = 0;
if (ERR_CODE == sd_client)
continue;
if (ERR_CODE == ioctl(sd_client, FIONBIO, &option))
{
error(strerror(errno));
errno = 0;
continue;
}
// sets
FD_SET(sd_client, &descriptors.container);
if (sd_client >= descriptors.count)
descriptors.count = sd_client + 1;
}
// the server closes, an admin key was used
// using call() is now accepted
call0(pthread_join(multiplexing_thread, NULL));
call0(pthread_join(udp_thread, NULL));
for (int fd = 0; fd < descriptors.count; fd++)
if (FD_ISSET(fd, &descriptors.container))
{
warning("not closed socket");
close(fd);
}
call(close(sd_listen));
call(close(sd_udp));
call(printf("the server is offline.\n\n"));
return EXIT_SUCCESS;
}
// receives three bytes sends two bytes at minimum
void *udp_communication(void * ignore)
{
// todo received udp socket as parameter not global
unsigned char buffer[3];
struct sockaddr_in skaddr_client;
socklen_t length = sizeof(skaddr_client);
for (; running_condition();)
{
int bytes =
recvfrom(sd_udp, buffer,
3 * sizeof(char), NO_FLAG,
(struct sockaddr *)&skaddr_client,
&length);
if (ERR_CODE == bytes)
{
if (EWOULDBLOCK != errno)
error("recvfrom() failed");
errno = 0;
continue;
}
struct rr_route data[COUNT_ROUTES_MAX];
unsigned short count =
udp_parse(buffer[0], buffer[1],
buffer[2], data);
sendto(sd_udp, &count, sizeof(count), NO_FLAG,
(struct sockaddr *)&skaddr_client,
length);
sendto(sd_udp, data,
count * sizeof(struct rr_route),
NO_FLAG,
(struct sockaddr *)&skaddr_client,
length);
}
}
// receives four bytes, sends one byte
void *tcp_communication(const int sd)
{
if (!FD_ISSET(sd, &descriptors.container))
return NULL;
unsigned char command = 0, argument1 = 0;
unsigned short argument0 = 0;
ssize_t bytes = read_all(sd, &command, sizeof(command));
bytes += read_all(sd, &argument0, sizeof(argument0));
bytes += read_all(sd, &argument1, sizeof(argument1));
if (errno || bytes != 4)
{
warning("client disconnected while receving command");
if (ECONNRESET != errno && errno)
error(strerror(errno));
FD_CLR(sd, &descriptors.container);
if (ERR_CODE == close(sd))
error("close() failed");
errno = 0;
return NULL;
}
unsigned char outcome =
tcp_parse(command, argument0, argument1);
bytes = write_all(sd, &outcome, sizeof(outcome));
if (errno || bytes != sizeof(outcome))
{
warning("client disconnected while sending outcome");
if (ECONNRESET != errno && errno)
error(strerror(errno));
FD_CLR(sd, &descriptors.container);
if (ERR_CODE == close(sd))
error("close() failed");
errno = 0;
return NULL;
}
if (TCP_CODE_Q == command)
{
FD_CLR(sd, &descriptors.container);
if (ERR_CODE == close(sd))
error("close() failed");
}
return NULL;
}
void *multiplexing(void * ignore)
{
struct timeval TV = {1, 0};
for (; running_condition();)
{
fd_set tcp_fd;
memcpy(&tcp_fd, &descriptors.container,
sizeof(descriptors.container));
int count_selected =
select(descriptors.count, &tcp_fd,
NULL, NULL, &TV);
if (ERR_CODE == count_selected)
error("select() failed");
for (int sd = 4;
sd < descriptors.count &&
count_selected;
sd++)
if (FD_ISSET(sd, &tcp_fd))
{
tcp_communication(sd);
count_selected--;
}
}
return NULL;
}
//------------------------------------------------
void check_txt(int *argc, char *argv[],
char *const path_txt)
{
if (*argc > 2)
{
error("at most one txt file name is expected");
exit(EXIT_FAILURE);
}
if (1 == *argc)
{
strcat(path_txt, name_random);
if (EXIT_FAILURE ==
write_file(path_binary, path_location))
strcpy(path_txt, path_stable);
return;
}
// 2 == argc
char path_tmp[BYTES_PATH_MAX];
strcpy(path_tmp, path_txt);
strcat(path_tmp, argv[1]);
if (0 == test_txt(path_tmp))
{
strcat(path_txt, argv[1]);
return;
}
// random generation
*argc = 1; // later generations
strcat(path_txt, name_random);
if (EXIT_FAILURE ==
write_file(path_binary, path_location))
strcpy(path_txt, path_stable);
}
/* a server should always be online
* this should always return true
* provides error messages
*/
bool running_condition()
{
char key = '0';
int fd = open(path_thread, O_RDONLY);
if (ERR_CODE == fd ||
ERR_CODE == read_all(fd, &key, sizeof(key)) ||
ERR_CODE == close(fd))
{
error("running_condition() failed");
return false;
}
return '1' == key;
}
/* even though clients are not served at this time
* if something goes wrong here,
* the server shouldn't stop
* provides error message for restarting threads
* returns the count of routes newly accessed
*/
int maintenance(pthread_t *th0, pthread_t *th1,
const int argc,
char *const path_txt)
{
if (NULL == th0 || NULL == th1 || NULL == path_txt)
return ERR_CODE;
// stop serving clients
char key = '0'; // security--
int fd = open(path_thread, O_WRONLY);
if (ERR_CODE == fd ||
sizeof(key) != write_all(fd, &key, sizeof(key)) ||
ERR_CODE == close(fd))
return ERR_CODE;
if (pthread_join(*th0, NULL) ||
pthread_join(*th1, NULL))
return ERR_CODE;
for (int fd = 0; fd < descriptors.count; fd++)
if (FD_ISSET(fd, &descriptors.container))
{
warning("not closed socket");
close(fd);
}
// check the new schedule for today
if (1 == argc)
{
if (EXIT_FAILURE ==
write_file(path_binary, path_location))
strcpy(path_txt, path_stable);
}
read_txt(path_txt);
call(printf("%d routes are valid.\n", count_routes));
// restart threads
call0(pthread_create(th0, NULL,
&multiplexing, NULL));
call0(pthread_create(th1, NULL,
&udp_communication, NULL));
// restart serving clients
key = '1'; // security--
fd = open(path_thread, O_WRONLY);
if (ERR_CODE == fd ||
sizeof(key) != write_all(fd, &key, sizeof(key)) ||
ERR_CODE == close(fd))
return ERR_CODE;
// success
return count_routes;
}