-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
571 lines (525 loc) · 17.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mp3split.h"
#include "mp3stream.h"
#include <errno.h>
#include <signal.h>
#include <sys/select.h>
#include <sys/time.h>
#if HAVE_GETOPT_H
#include <getopt.h>
#endif
unsigned int client_count = 0;
void _usage(char *name)
{
fprintf(stdout,"Usage: %s [options]\nOptions:\n", name);
fprintf(stdout," --help\tDisplay this information\n");
#ifdef PACKAGE_NAME
fprintf(stdout," --version\tDisplay %s version information\n", PACKAGE_NAME);
#endif
fprintf(stdout," --v4only\tUse IPv4 sockets even if IPv6 is available\n");
fprintf(stdout," --v6only\tUse IPv6 sockets even if IPv4 is available\n");
}
#define DUMMY_BUFSIZE 8
/**
* Adds one client to the linked list
* @param sockfd Socket of the client to add
* @param head_client First element of the linked list
* @param cur_frame The current element that is being streamed
*/
static inline void clientloop(int sockfd, struct Client* const head_client, struct FrameBufferElement* const cur_frame)
{
int client = -1;
int8_t buffer[DUMMY_BUFSIZE];
struct sockaddr_storage client_addr;
socklen_t client_len = sizeof(client_addr);
client = accept(sockfd, (struct sockaddr*) &client_addr,
&client_len);
int flags = fcntl(client, F_GETFL, 0);
if(flags == -1)
{
perror(strerror(errno));
close(client);
return;
}
if(fcntl(client, F_SETFL, flags | O_NONBLOCK) == -1)
{
perror(strerror(errno));
close(client);
return;
}
//fprintf(stderr, "Got new client!\n");
if (client < 0) {
//fprintf(stderr, "Something went wrong while accepting the client!\n");
exit(1);
}
//fprintf(stderr, "allocationg new client with fd %d\n", client);
struct Client* new_client = malloc(sizeof(struct Client));
if(!new_client) {
perror(strerror(errno));
exit(1);
}
new_client->next = NULL;
new_client->sent = 0;
new_client->fbe = cur_frame;
new_client->frame_id = cur_frame->id;
new_client->skipped_frames = 0;
struct Client* cur_client = head_client;
while (cur_client->next != NULL) { // get the last client struct
cur_client = cur_client->next;
}
new_client->sock = client;
//fprintf(stderr, "adding new client\n");
cur_client->next = new_client;
new_client->prev = cur_client;
//fprintf(stderr, "added new client! %p ->* %p -> next (new client) %p\n", head_client, cur_client, new_client);
socklen_t addr_len = sizeof(client_addr);
int err = getnameinfo((struct sockaddr*)&client_addr,addr_len,new_client->ip,sizeof(new_client->ip),
0,0,NI_NUMERICHOST);
fprintf(stderr, "Client %d (%s) connected, clients: %d\n",
new_client->sock,new_client->ip , ++client_count);
read(client, buffer, DUMMY_BUFSIZE); // lawl we don't really care...
ssize_t n = write(client,
"HTTP/1.1 200 OK\r\n\r\n", 19);
//icy-metaint: 8192\r\n\r\n", 38);
if (n < 0) {
remove_client(new_client);
//fprintf(stderr, "Life is pointless because errno: %d\n", errno);
}
}
int main(int argc, char *argv[]) {
if (argc < 1) {
//fprintf(stderr, "NO U >:V giev host and port\n");
exit(1);
}
signal(SIGPIPE, SIG_IGN);
struct Client* head_client = malloc(sizeof(struct Client));
if(!head_client) {
perror(strerror(errno));
exit(1);
}
head_client->sock = -1;
head_client->prev = NULL;
head_client->next = NULL;
int server4 = -1, server6 = -1, v4only = 0, v6only = 0, opts;
unsigned int port = 8080;
socklen_t client_len;
struct sockaddr_storage client_addr;
struct addrinfo hints, *res;
char port_str[5];
int c, error = 0, option_index = 0;
#ifdef HAVE_GETOPT_LONG
struct option long_options[] = {
{"v4only", no_argument, &v4only, 1},
{"v6only", no_argument, &v6only, 1},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}
};
#endif
while(1) {
#ifdef HAVE_GETOPT_LONG
c = getopt_long(argc, argv, "46Vh",
long_options, &option_index);
#else
c = getopt(argc, argv, "46Vh");
#endif
if(c == -1)
break;
switch(c) {
case '4':
if(v6only) {
fprintf(stderr, "%s: cannot specify -4 and -6 together\n", argv[0]);
exit(1);
}
v4only = 1;
break;
case '6':
if(v4only) {
fprintf(stderr, "%s: cannot specify -6 and -4 together\n", argv[0]);
exit(1);
}
v6only = 1;
break;
case 'V':
#ifdef PACKAGE_STRING
fprintf(stdout, "%s\n", PACKAGE_STRING);
#endif
exit(0);
break;
case 'h':
#ifdef PACKAGE_STRING
fprintf(stdout, "%s\n", PACKAGE_STRING);
#endif
_usage(argv[0]);
exit(0);
break;
case '?':
fprintf(stderr,
"Unrecognized option: -%c\n", optopt);
exit(1);
break;
}
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
hints.ai_socktype = SOCK_STREAM;
snprintf(port_str, sizeof(port_str), "%d", port);
error = getaddrinfo(NULL, port_str, &hints, &res);
if (error) {
perror(gai_strerror(error));
exit(1);
}
if (!v4only && (server6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
if(errno != EAFNOSUPPORT
#ifdef EPFNOSUPPORT
&& errno != EPFNOSUPPORT
#endif
) {
perror(strerror(errno));
exit(1);
}
else
v4only = 1;
}
if(!v6only && (server4 = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
if(errno != EAFNOSUPPORT
#ifdef EPFNOSUPPORT
&& errno != EPFNOSUPPORT
#endif
) {
perror(strerror(errno));
exit(1);
}
else
v6only = 1;
}
int on = 1;
if (server4 > 0 && setsockopt(server4, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
//fprintf(stderr, "Something went wrong while setting up the server!\n");
perror(strerror(errno));
exit(1);
}
if (server6 > 0 && setsockopt(server6, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
//fprintf(stderr, "Something went wrong while setting up the server!\n");
perror(strerror(errno));
exit(1);
}
#ifdef IPV6_V6ONLY
/* on supported operating systems, make the IPv6 socket listen only for IPv6 connections */
if (server6 > 0 && setsockopt(server6, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) != 0) {
perror(strerror(errno));
exit(1);
}
#endif
/* make IPv4 listening socket non-blocking
if(server4 > 0) {
if((opts = fcntl(server4, F_GETFL)) == -1)
{
perror(strerror(errno));
exit(1);
}
opts = (opts | O_NONBLOCK);
if (fcntl(server4, F_SETFL, opts) < 0)
{
perror(strerror(errno));
exit(1);
}
}
*/
/* make IPv6 listening socket non-blocking
if(server6 > 0) {
if((opts = fcntl(server6, F_GETFL)) == -1)
{
perror(strerror(errno));
exit(1);
}
opts = (opts | O_NONBLOCK);
if (fcntl(server6, F_SETFL, opts) < 0)
{
perror(strerror(errno));
exit(1);
}
}
*/
for(struct addrinfo *iter = res; iter; iter = iter->ai_next) {
int server = (iter->ai_family == PF_INET6 ? server6 : server4);
if (server > 0 && bind(server, iter->ai_addr,
iter->ai_addrlen) < 0) {
close(server);
server = -1;
fprintf(stderr, "Something went wrong while binding the server! %s\n",
strerror(errno));
}
}
freeaddrinfo(res);
res = NULL;
struct FrameBufferElement* FB_head = malloc(sizeof(
struct FrameBufferElement));
if(!FB_head) {
perror(strerror(errno));
exit(1);
}
FB_head->frame = NULL;
FB_head->next = NULL;
FB_head->prev = NULL;
FB_head->id = 0;
struct FrameBufferElement* prev_fbe = FB_head;
struct FrameBufferElement* cur_frame;
int cur_frame_id;
for (cur_frame_id = 1; cur_frame_id < RINGBUFSIZE; cur_frame_id++) {
cur_frame = malloc(sizeof(struct FrameBufferElement));
if(!cur_frame) {
perror(strerror(errno));
exit(1);
}
cur_frame->id = cur_frame_id;
cur_frame->frame = NULL;
cur_frame->prev = prev_fbe;
cur_frame->latest = 0;
prev_fbe->next = cur_frame;
prev_fbe = cur_frame;
}
cur_frame->next = FB_head;
FB_head->prev = cur_frame;
fd_set master_fds, rfds, wfds;
int retval;
FD_ZERO(&master_fds);
// we make the first client the client that serves our mp3 stream.
if (server4 > 0) { listen(server4, 1); FD_SET(server4, &master_fds); }
if (server6 > 0) { listen(server6, 1); FD_SET(server6, &master_fds); }
//memcpy(&rfds, &master_fds, sizeof(master_fds));
rfds = master_fds;
retval = select(max(server4, server6) + 1, &rfds, NULL, NULL, NULL);
if (retval == -1) exit(1);
client_len = sizeof(client_addr);
int mp3_stream = -1;
if (FD_ISSET(server4, &rfds)) {
mp3_stream = accept(server4, (struct sockaddr*) &client_addr,
&client_len);
} else if (FD_ISSET(server6, &rfds)) {
mp3_stream = accept(server6, (struct sockaddr*) &client_addr,
&client_len);
}
unsigned int frame_number = 0;
for (;;) {
// prepare fds for select
memcpy(&rfds, &master_fds, sizeof(master_fds));
FD_ZERO(&wfds);
if (server4 > 0) listen(server4, 10);
if (server6 > 0) listen(server6, 10);
FD_SET(mp3_stream, &rfds);
struct Client* cur_client = head_client->next;
while (cur_client != NULL) {
FD_SET(cur_client->sock, &wfds);
cur_client = cur_client->next;
}
retval = select(max(max(server4, server6), mp3_stream) + 1, &rfds, NULL, NULL, NULL);
if (retval <= 0) {
//fprintf(stderr, "select() failed, abandon process!\n");
exit(1);
} else if (retval) {
if (FD_ISSET(server6, &rfds)) clientloop(server6, head_client, cur_frame);
if (FD_ISSET(server4, &rfds)) clientloop(server4, head_client, cur_frame);
if (FD_ISSET(mp3_stream, &rfds)) { // can read mp3_stream
if(!add_frame(mp3_stream, cur_frame)) { // frame is NULL --> EOF
clear_FB(cur_frame);
clear_clients(head_client);
close(mp3_stream);
exit(0);
}
cur_frame = cur_frame->next;
//fprintf(stderr, "Frame: %d, Buffer: %d\n", frame_number,
// cur_frame->id);
frame_number++;
}
cur_client = head_client->next; // get the first "real" client
while (cur_client != NULL) { // select() through our clients
if (FD_ISSET(cur_client->sock, &wfds)) { // client can be written
// fprintf(stderr, "Client: %d, Frame: %d\n", cur_client->sock,
// cur_client->frame_id);
write_data(cur_client);
}
cur_client = cur_client->next;
}
}
}
}
/**
* Adds one frame from the stream to the buffer and puts it behind the currently
* streamed frame in the buffer.
* @param in The stream we are taking our frame from
* @param cur_frame The current frame that is being streamed
* @return Returns whether or not the frame we got is valid (1) or not (0)
*/
int add_frame(int in, struct FrameBufferElement* cur_frame) {
int id = (cur_frame->id + 1) % (RINGBUFSIZE + 1);
cur_frame->latest = 0;
cur_frame = cur_frame->next;
if (cur_frame->frame != NULL)
free(cur_frame->frame->content);
free(cur_frame->frame);
cur_frame->frame = get_frame(in);
cur_frame->id = id;
cur_frame->latest = 1;
return cur_frame->frame != NULL;
}
/**
* Removes one client from the client list
* @param client The client to be removed.
*/
void remove_client(struct Client* client) {
fprintf(stderr, "Client %d (%s) dropped, clients: %d\n", client->sock,
client->ip, --client_count);
if (client->prev != NULL) { // we're not the head
client->prev->next = client->next;
if (client->next != NULL) // we're not the last element
client->next->prev = client->prev;
if (client->sock != -1)
close(client->sock);
free(client);
} else { // we're the head
if (client->next != NULL) { // we're not alone!
client->next = client->next->next;
client->sock = client->next->sock;
free(client->next);
} else { // we're everything that's left
if (client->sock != -1)
close(client->sock);
free(client);
}
}
}
/**
* Returns the maximum of two given integers
* @param a The first value
* @param b The second value
* @return The greater value
*/
int max(int a, int b) {
return a > b ? a : b;
}
/**
* Writes data to one client, this is not trivial.
* The function checks if there is data to be sent to the client, by checking
* if the current frame pointed to by the client is the most recent one. If not
* one frame is sent to the client.
* @param client The client we are sending data to.
*/
void write_data(struct Client* client) {
if (client->fbe->frame == NULL) {
// WTF? no frame, drop the client
remove_client(client);
}
if (client->frame_id != client->fbe->id) {
// client will skip, lagged behind too long in the ringbuffer
fprintf(stderr, "Client %d lagging behind %d frames.\n", client->sock,
client->skipped_frames);
if (client->skipped_frames++ > RINGBUFSIZE * 2) {
// assume the client has quit
remove_client(client);
return;
}
} else {
client->skipped_frames = 0;
}
size_t frame_length;
ssize_t sent_tmp;
uint32_t header_nl;
while (!client->fbe->latest) {
frame_length = get_frame_length(client->fbe->frame->header);
sent_tmp = 0;
if (client->sent < 4) { // we're not done sending the header
//fprintf(stderr, "Sent Header ");
header_nl = htonl(client->fbe->frame->header);
sent_tmp = write(client->sock, &header_nl + client->sent,
4 - client->sent);
client->sent += sent_tmp;
//fprintf(stderr, "%d bytes\n", sent_tmp);
sent_tmp = 0;
}
//fprintf(stderr, "Sent data ");
sent_tmp = write(client->sock, client->fbe->frame->content
+ client->sent - 4, frame_length - client->sent);
if (sent_tmp < 0) { // write failed, check for error
if (errno == EPIPE) // client quit
remove_client(client);
return;
}
client->sent += sent_tmp;
//fprintf(stderr, "%d bytes\n");
if (client->sent == frame_length) {
client->fbe = client->fbe->next;
client->frame_id = client->fbe->id;
client->sent = 0;
}
}
}
/**
* Prints pointers of all framebuffer elements
* @param head The head of the list, actually any element from the list, since
* it is a ringbuffer list
*/
void print_FB(struct FrameBufferElement* head)
{
struct FrameBufferElement* cur_fbe = head->next;
while (cur_fbe != head) {
fprintf(stderr, "%d->", cur_fbe->id);
cur_fbe = cur_fbe->next;
}
fprintf(stderr, "%d\n\n", cur_fbe->id);
cur_fbe = head->prev;
while (cur_fbe != head) {
fprintf(stderr, "%d<-", cur_fbe->id);
cur_fbe = cur_fbe->prev;
}
fprintf(stderr, "%d\n\n", cur_fbe->id);
}
/**
* Cleans up the Ringbuffer
* @param elem Any element from the ringbuffer
*/
void clear_FB(struct FrameBufferElement* elem) {
struct FrameBufferElement* next;
elem->prev->next = NULL;
while (elem != NULL) {
next = elem->next;
if (elem->frame != NULL) {
free(elem->frame->content);
}
free(elem->frame);
free(elem);
elem = next;
}
}
/**
* Cleans up the client list
* @param elem The head client
*/
void clear_clients(struct Client* elem) {
struct Client* next;
while (elem != NULL) {
next = elem->next;
if (elem->sock != -1)
close(elem->sock);
free(elem);
elem = next;
}
}