-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibmpsocket.c
1418 lines (1199 loc) · 35.3 KB
/
libmpsocket.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define _GNU_SOURCE /* to enable gcc magic in dlfcn.h for shared libs */
#include "libmpsocket.h"
#include "mpsock_socket.h"
#include "mpsock_connection.h"
#include "mpsock_dns.h"
#include "mpsock_interface.h"
#include "mpsock_buffer.h"
#include "mpsock_misc.h"
/*
* Global variables
*/
// user input
static int initial_chunk_size;
static int conns;
static char *intf_list;
static char *ip_list;
static int max_req_con = MAX_REQ_CON;
static int max_req_serv = MAX_REQ_SERV;
static int max_req_mpsocket = MAX_REQ_MPSOCKET;
static int initial_alpha = INITIAL_ALPHA;
int version = SCHEDULER_VERSION_DEFAULT;
int alpha_max = ALPHA_MAX_DEFAULT;
int processing_skips = PROCESSING_SKIPS_DEFAULT;
int use_initial_second_path = USE_INITIAL_SECOND_PATH_DEFAULT;
int use_random_path = USE_RANDOM_INTERFACE;
int log_decisions = DO_LOG_SYNC;
int log_traffic = DO_LOG_TRAFFIC;
int log_metrics = DO_LOG_METRICS;
int scheduler_algo = SCHEDULER_ALGO_DEFAULT;
// TODO: verify if static would be better
// hash tables
mpsock_socket *mpsock_socket_table = NULL; // all mpsockets are stored here
mpsock_interface *mpsock_interface_table = NULL; // all interfaces are stored here -> a pointer to this pointer is given to each socket
mpsock_addrs *mpsock_address_table = NULL; // all dns resolvings are stored here -> a pointer of this pointer is given to each socket
// buffer
mpsock_buffer *mpsock_data_buffer = NULL; // our buffer for payload data
/*
* Renamed hooked function pointers
*/
// sys/socket.h interface
static int (*o_socket)(int, int, int);
static int (*o_connect)(int, const struct sockaddr *, socklen_t);
static int (*o_send)(int, const void *, size_t, int);
static int (*o_recv)(int, void *, size_t, int);
static int (*o_bind)(int, const struct sockaddr *, socklen_t);
static int (*o_getpeername)(int, struct sockaddr *, socklen_t *);
static int (*o_shutdown)(int, int);
static int (*o_setsockopt)(int, int, int, const void *, socklen_t);
static int (*o_getsockopt)(int, int, int, void *, socklen_t *);
static int (*o_accept)(int, struct sockaddr *, socklen_t *);
static int (*o_getsockname)(int, struct sockaddr *, socklen_t *);
static int (*o_listen)(int, int);
static int (*o_socketpair)(int, int, int, int socket_vector[2]);
static ssize_t (*o_recvfrom)(int, void *, size_t, int, struct sockaddr *, socklen_t *);
static ssize_t (*o_recvmsg)(int, struct msghdr *, int);
static ssize_t (*o_sendmsg)(int, const struct msghdr *, int);
static ssize_t (*o_sendto)(int, const void *, size_t, int, const struct sockaddr *, socklen_t);
// netdb.h interface
static struct hostent *(*o_gethostbyname)(const char *);
static int (*o_getaddrinfo)(const char *, const char *, const struct addrinfo *, struct addrinfo **);
// unistd.h interface
static size_t (*o_write)(int, const void *, size_t);
static ssize_t (*o_read)(int, void *, size_t);
static int (*o_close)(int);
static int (*o_select)(int, fd_set *, fd_set *, fd_set *, struct timeval *);
static int (*o_pselect)(int, fd_set *, fd_set *, fd_set *, const struct timespec *, const sigset_t *);
/*
* hooked function pass through wrappers
*/
// sys/socket.h interface
int f_socket(int domain, int type, int protocol)
{
return o_socket(domain,type,protocol);
}
int f_connect(int sd, const struct sockaddr *serv_addr, socklen_t addrlen)
{
return o_connect(sd,serv_addr,addrlen);
}
int f_send(int s, const void *msg, size_t len, int flags)
{
return o_send(s,msg,len,flags);
}
int f_recv(int fd, void *buf, size_t count, int flags)
{
return o_recv(fd,buf,count,flags);
}
int f_bind(int fd, const struct sockaddr *address, socklen_t len)
{
return o_bind(fd,address,len);
}
int f_getpeername(int fd, struct sockaddr *address, socklen_t *len)
{
return o_getpeername(fd,address,len);
}
int f_shutdown(int fd, int how)
{
return o_shutdown(fd,how);
}
int f_setsockopt(int fd, int level, int option_name, const void *option_value, socklen_t option_len)
{
return o_setsockopt(fd,level,option_name,option_value,option_len);
}
int f_getsockopt(int fd, int level, int option_name, void *option_value, socklen_t *option_len)
{
return o_getsockopt(fd,level,option_name,option_value,option_len);
}
int f_accept(int fd, struct sockaddr *address, socklen_t *address_len)
{
return o_accept(fd,address,address_len);
}
int f_getsockname(int fd, struct sockaddr *address, socklen_t *address_len)
{
return o_getsockname(fd,address,address_len);
}
int f_listen(int fd, int backlog)
{
return o_listen(fd,backlog);
}
int f_socketpair(int domain, int type, int protocol, int socket_vector[2])
{
return o_socketpair(domain,type,protocol,socket_vector);
}
ssize_t f_recvfrom(int fd, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len)
{
return o_recvfrom(fd,buffer,length,flags,address,address_len);
}
ssize_t f_recvmsg(int fd, struct msghdr *message, int flags)
{
return o_recvmsg(fd,message,flags);
}
ssize_t f_sendmsg(int fd, const struct msghdr *message, int flags)
{
return o_sendmsg(fd,message,flags);
}
ssize_t f_sendto(int fd, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
{
return o_sendto(fd,message,length,flags,dest_addr,dest_len);
}
// netdb.h interface
struct hostent *f_gethostbyname(const char *name)
{
return o_gethostbyname(name);
}
int f_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
{
return o_getaddrinfo(node,service,hints,res);
}
// unistd.h interface
size_t f_write(int fd, const void* buf, size_t count)
{
return o_write(fd,buf,count);
}
ssize_t f_read(int fd, void* buf, size_t count)
{
return o_read(fd,buf,count);
}
int f_close(int fd)
{
return o_close(fd);
}
int f_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
return o_select(nfds,readfds,writefds,exceptfds,timeout);
}
int f_pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask)
{
return o_pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask);
}
/* Subtract the `struct timeval' values X and Y,
storing the result in RESULT.
Return 1 if the difference is negative, otherwise 0. */
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
{
/* Perform the carry for the later subtraction by updating y. */
if(x->tv_usec < y->tv_usec)
{
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if(x->tv_usec - y->tv_usec > 1000000)
{
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
tv_usec is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
//void delete_parser(http_parsers *p) {
// LOG_INFO("delete_parser: sd=%d fd=%d\n", p->sd, p->fd);
// HASH_DEL(parsers, p);
// free(p->parser);
// p->parser = NULL;
//}
void signalhandler(int signum)
{
LOG_FATAL("%sSIGSEV Error Occurred",FATAL_EVENT);
signal(signum,SIG_DFL);
kill(getpid(),signum);
}
void signalhandlerAsserts(int signum)
{
LOG_FATAL("%sSIGABRT (Assertion) Error Occurred",FATAL_EVENT);
signal(signum,SIG_DFL);
kill(getpid(),signum);
}
static void __attribute__ ((constructor))
init(void)
{
LOG_INFO("%sinitialize libmpsocket",FUN_EVENT);
srand(time(NULL));
//signal(SIGSEGV,signalhandler);
//signal(SIGABRT,signalhandlerAsserts);
// Parse input environment variables
const char *s;
//
// //
// // The choice of a scheduler version is disabled (jkim)
// // a scheduler version is only relevant to time_chunk_algorithm
// //
//
// s = getenv("SCHEDULER_VERSION");
//
// if (s)
// version = atoi(s);
//
// if (version == -1)
// version = SCHEDULER_VERSION_DEFAULT;
//
// if (USE_ASSERTS) assert(version >= 0 && version < 3);
//
// printf("[mHTTP]\tusing scheduler version %d"version);
//
s = getenv("INITIAL_CHUNK_SIZE_IN_KB");
if(s)
{
initial_chunk_size = atoi(s) * 1024;
}
else
{
initial_chunk_size = INITIAL_CHUNK_SIZE;
}
printf("set initial_chunk_size = %d kB\n",initial_chunk_size/1024);
//
// //
// // The maximum requests limit is disabled (jkim)
// //
//
// s = getenv("MAX_REQ_CON");
//
// if(s)
// {
// max_req_con = atoi(s);
// }
// else
// {
// max_req_con = MAX_REQ_CON;
// }
//
// LOG_INFO("%sset maximum requests per connection to %d",COND_EVENT,max_req_con);
//
// //
// // The choice of a scheduler algorithm is disabled (jkim)
// //
//
// s = getenv("SCHEDULER_ALGO");
//
// if (s)
// scheduler_algo = atoi(s);
//
// printf("using scheduler algorithm %d\n",scheduler_algo);
//
//
// //
// // The maximum requests limit is disabled (jkim)
// //
//
// s = getenv("MAX_REQ_SERV");
//
// if(s)
// {
// max_req_serv = atoi(s);
// }
// else
// {
// max_req_serv = MAX_REQ_SERV;
// }
//
// LOG_DEBUG("%sset maximum requests per server to %d",COND_EVENT,max_req_serv);
//s = getenv("INITIAL_SECOND_PATH");
//
// if(s)
// {
// use_initial_second_path = atoi(s);
// }
//
// LOG_INFO("%sintial second path set to %d",COND_EVENT,use_initial_second_path);
// s = getenv("RANDOM_PATH");
//
// if(s)
// {
// use_random_path = atoi(s);
// }
//
// LOG_INFO("%srandom path set to %d",COND_EVENT,use_random_path);
//
// //
// // Logging is temporarily disabled, but it should be done to a file later.
// //
//
// s = getenv("LOG_DECISIONS");
//
// if(s)
// {
// log_decisions = atoi(s);
// }
// s = getenv("LOG_TRAFFIC");
//
// if(s)
// {
// log_traffic = atoi(s);
// }
// s = getenv("LOG_METRICS");
//
// if(s)
// {
// log_metrics = atoi(s);
// }
//
// //
// // The maximum requests limit is disabled (jkim)
// //
//
// s = getenv("MAX_REQ_MPSOCKET");
//
// if(s)
// {
// max_req_mpsocket = atoi(s);
// }
// else
// {
// max_req_mpsocket = MAX_REQ_MPSOCKET;
// }
//
// LOG_INFO("%sset maximum requests per mpsocket to %d",COND_EVENT,max_req_mpsocket);
//
// //
// // The choice of an initial alpha is hard-coded (jkim)
// //
//
// s = getenv("INITIAL_ALPHA");
//
// if(s)
// {
// initial_alpha = atoi(s);
// }
// else
// {
// initial_alpha = INITIAL_ALPHA;
// }
//
// printf("set initial_alpha to %d\n",initial_alpha);
//
// //
// // alpha_max is hard-coded (jkim)
// // Only relevant to time_chunk_algorithm
// //
//
// s = getenv("ALPHA_MAX");
//
// if (s)
// alpha_max = atoi(s);
//
// if (alpha_max == -1)
// alpha_max = ALPHA_MAX_DEFAULT;
//
// printf("set alpha_max to %d\n",alpha_max);
//
//
// //
// // processing_skips is disabled (and will eventually deleted) (jkim)
// //
//
// s = getenv("PROCESSING_SKIPS");
//
// if(s)
// {
// processing_skips = atoi(s);
// }
//
// if(processing_skips = -1)
// {
// processing_skips = PROCESSING_SKIPS_DEFAULT;
// }
//
// LOG_INFO("%sset processing_skips to %d",COND_EVENT,processing_skips);
const char *p;
p = getenv("CONNECTIONS");
if(p)
{
conns = atoi(p);
}
else
{
usage();
exit(0);
}
// create the ringbuffer
mpsock_data_buffer = create_buffer(MPSOCK_BUFFER_SIZE);
// For the manual configuration of IP and interface list
intf_list = getenv("INTERFACES");
ip_list = getenv("IPADDRS");
if(strcmp(intf_list, "0") == 0) intf_list = NULL;
if(strcmp(ip_list, "0") == 0) ip_list = NULL;
// set global start ts
gettimeofday(&global_start_ts,NULL);
// set/verify hooks
#define GET_SYM(sym) \
o_##sym = dlsym(RTLD_NEXT, #sym); \
if (o_##sym == NULL) { \
fprintf(stderr, "mn:" "dlsym(%s) failed: %s\n", #sym, dlerror()); \
}
GET_SYM(socket);
GET_SYM(connect);
GET_SYM(write);
GET_SYM(read);
GET_SYM(send);
GET_SYM(recv);
GET_SYM(close);
GET_SYM(gethostbyname);
GET_SYM(getaddrinfo);
GET_SYM(bind);
GET_SYM(getpeername);
GET_SYM(shutdown);
GET_SYM(setsockopt);
GET_SYM(getsockopt);
GET_SYM(accept);
GET_SYM(getsockname);
GET_SYM(listen);
GET_SYM(socketpair);
GET_SYM(recvfrom);
GET_SYM(recvmsg);
GET_SYM(sendmsg);
GET_SYM(sendto);
GET_SYM(select);
GET_SYM(pselect);
#undef GET_SYM
}
int socket(int domain, int type, int protocol)
{
if(PASS_THROUGH)
{
LOG_INFO("%ssocket(domain=%d, type=%d, protocol=%d)",FUN_EVENT,domain,type,protocol);
return f_socket(domain,type,protocol);
}
// ==================================================================
// we return a fake descriptor to the application. Meaning, the
// application believes that it gets a socket descriptor, but what it
// really gets is the file descriptor made by our wrapper function.
// ==================================================================
int sd = f_socket(domain, type, protocol);
int rt = sd;
// check if we got a tcp socket
if(type == SOCK_STREAM)
{
LOG_INFO("%shooked socket(domain=%d, type=%d, protocol=%d)",FUN_EVENT,domain,type,protocol);
// create fake socket
mpsock_socket *sock = create_mpsocket(sd, initial_chunk_size, conns, mpsock_data_buffer, max_req_con, max_req_serv, max_req_mpsocket, initial_alpha, version, alpha_max, processing_skips, use_initial_second_path, use_random_path, log_decisions, log_traffic, log_metrics, scheduler_algo);
rt = sock->m_sd;
LOG_INFO("%screated mhttp-socket: %d",RESULT_EVENT,rt);
// setup the available interfaces
if(intf_list == NULL)
{
collect_interfaces(sd); // collect available network interfaces
}
else
{
insert_interfaces(intf_list); // create the list of interface by the user
}
}
else
{
LOG_INFO("%snormal socket(domain=%d, type=%d, protocol=%d)",FUN_EVENT,domain,type,protocol);
LOG_INFO("%screated normal-socket: %d",RESULT_EVENT,rt);
}
return rt;
}
int connect(int sd, const struct sockaddr *serv_addr, socklen_t addrlen)
{
if(PASS_THROUGH)
{
LOG_INFO("%sconnect(sd=%d)",FUN_EVENT,sd);
return f_connect(sd,serv_addr,addrlen);
}
if(!is_mpsocket(sd))
{
LOG_INFO("%snormal connect(m_sd=%d)",FUN_EVENT,sd);
return f_connect(sd,serv_addr,addrlen);
}
LOG_INFO("%shooked connect(m_sd=%d)",FUN_EVENT,sd);
mpsock_socket *mpsock = find_mpsocket(sd);
if(USE_ASSERTS) assert(mpsock != NULL);
mpsock_connection *conn = NULL;
struct sockaddr_in *sa = (struct sockaddr_in *) serv_addr;
// check if we got a http connection request
if(htons(sa->sin_port) == 80 || htons(sa->sin_port) == 8080)
{
// set the port
set_port(mpsock,htons(sa->sin_port));
conn = get_random_free_connection(mpsock->pool);
// Force to connect the first IP address of the list
//if(ip_list)
//{
// // TODO: verify
// LOG_DEBUG("%sForce IP address",COND_EVENT);
// sa->sin_addr.s_addr = scheduler_get_first_ip(conn->scheduler,sa->sin_addr.s_addr);
//}
if(USE_ASSERTS) assert(conn != NULL);
mpsock_interface *intf = scheduler_get_interface(conn->scheduler);
if(setsockopt(conn->sd, SOL_SOCKET, SO_BINDTODEVICE, intf->name, strlen(intf->name)) < 0)
{
perror("setsockopt error");
exit(1);
}
set_index_and_ip(conn,(unsigned long)sa->sin_addr.s_addr);
}
else if(htons(sa->sin_port) == 443 || htons(sa->sin_port) == 8443)
{
// https not supported yet -> strip mpsocket wrapper from socket descriptor
//LOG_INFO("%shttps connection detected -> strip mpsock wrapper",COND_EVENT);
if(PRINT_DEFAULT_OUT)
{
printf("\n[mHTTP]\tSSL Connection");
}
strip_mpsock_from_socket(mpsock);
}
else
{
LOG_FATAL("%sunsupported port detected %d",FATAL_EVENT,htons(sa->sin_port));
perror("shutting down due to unsupported port");
exit(1);
}
if(conn != NULL)
{
int i=0;
for(i=0; i<conn->adrs->count; i++)
{
if(conn->adrs->ipset[i].ip == (unsigned long)sa->sin_addr.s_addr)
{
conn->adrs->ipset[i].inuse = TRUE;
break;
}
}
if(USE_ASSERTS) assert(conn->adrs->ipset[i].ip == (unsigned long)sa->sin_addr.s_addr);
if(USE_ASSERTS) assert(conn->adrs->ipset[i].inuse == TRUE);
LOG_INFO("%ssd#%d using host %zu",RESULT_EVENT,conn->sd,(unsigned long)sa->sin_addr.s_addr);
}
return f_connect(sd, serv_addr, addrlen);
//return (is_http_connection) ? f_connect(conn->sd, serv_addr, addrlen) : f_connect(sd, serv_addr, addrlen);
/*
else
{
int is_http = FALSE;
http_connection *item;
struct sockaddr_in *sa = (struct sockaddr_in *) serv_addr;
// TODO: verify
if(htons(sa->sin_port) == 80 || htons(sa->sin_port) == 8080 || htons(sa->sin_port) == 443 || htons(sa->sin_port) == 8443)
{
LOG_DEBUG("%sis http connection",COND_EVENT);
is_http = TRUE;
item = find_connection_by_fd(sd);
if(item == NULL)
{
LOG_FATAL("Could not find parser for fd=%d",sd);
exit(0);
}
// Force to connect the first IP address of the list
if(ip_list)
{
LOG_DEBUG("%sForce IP address",COND_EVENT);
sa->sin_addr.s_addr = get_first_ip(sa->sin_addr.s_addr);
}
mpsock_interface *intf = get_interface(item->sd);
if(setsockopt(item->sd, SOL_SOCKET, SO_BINDTODEVICE, intf->name, strlen(intf->name)) < 0)
{
perror("setsockopt error");
exit(1);
}
// disabling the nagle algorithm; changed nothing;
//int tcp = 1;
//if (setsockopt(item->sd, IPPROTO_TCP, TCP_NODELAY, (char*)&tcp, sizeof(int))) {
// perror("nagle's algorithm error");
// exit(1);
//}
http_parser *parser = (http_parser*) malloc(sizeof(http_parser));
parser->fd = item->fd; // renamed from master_fd
parser->sd = item->sd; // renamed from fd
parser->chunk_size = initial_chunk_size;
init_mhttp(parser);
parser->parser_status = PARSER_INIT;
parser->used_port = htons(sa->sin_port);
// Registering the parser
item->parser = parser;
mpsock_data_pool *pool = create_data_pool(parser);
// TODO: verify
//int err;
//if((err = pthread_create(&(pool->pid), NULL, &run_thread, (void*) pool)) != 0)
//{
// perror("pthread_create() error");
// exit(0);
//}
//LOG_INFO("%spid=%d",RESULT_EVENT,pool->pid);
//pool->is_thread_running = TRUE;
}
else
{
LOG_INFO("%sconnect(fd=%d) --> NO mhttp",FUN_EVENT,sd);
}
return (is_http) ? f_connect(item->sd, serv_addr, addrlen) : f_connect(sd, serv_addr, addrlen);
}
*/
}
size_t write(int fd, const char* buf, size_t count)
{
// TODO: what if this is not http?
if(PASS_THROUGH)
{
LOG_INFO("%swrite(fd=%d)",FUN_EVENT,fd);
return f_write(fd,buf,count);
}
if(!is_mpsocket(fd))
{
// not an mpsocket -> normal write
LOG_DEBUG("%snormal write(fd=%d)",FUN_EVENT,fd);
return f_write(fd,buf,count);
}
LOG_DEBUG("%shooked write(fd=%d)",FUN_EVENT,fd);
// TODO: verify
// extract request
size_t req_size = count-2;
char *request = (char*)malloc(req_size);
memcpy(request, buf, req_size);
// find pool and set request
mpsock_pool *pool = find_mpsocket(fd)->pool;
if(USE_ASSERTS) assert(pool->pos_buffer_read == 0);
if(USE_ASSERTS) assert(pool->pos_buffer_save == 0);
if(USE_ASSERTS) assert(pool->current_response_size == 0);
if(USE_ASSERTS) assert(!pool->is_response_created);
if(USE_ASSERTS) assert(pool->next_start_byte == 0);
if(USE_ASSERTS) assert(pool->req_start.tv_sec == 0);
// set request
pool->current_request = request;
pool->current_request_size = req_size;
// TODO: verify
//mpsock_connection *conn = get_first_connection(pool);
mpsock_connection *conn = get_best_connection(pool);
if(USE_ASSERTS) assert(conn->is_used == FALSE);
if(USE_ASSERTS) assert(conn->pool == pool);
// start first connection
gettimeofday(&(pool->req_start),NULL);
start_connection(conn);
// TODO: verify
return count;
//return f_write(p->sd, tmp, strlen(tmp)); // returning bytes must be the size of the original data
/*
else
{
if(no_connection(fd))
{
// no mpsock -> pass through
LOG_INFO("%sno connection for write request fd=%d -> use normal write instead",COND_EVENT,fd);
return f_write(fd, buf, count);
}
// Assuming this is an HTTP request from the application.
//char tmp[HTTP_MAX_HEADER_SIZE];
//size_t req_size = strlen(buf);
//LOG_INFO("%scount=%d, req_size=%d",FUN_EVENT,count,req_size);
// TODO: better
size_t req_size = count-2;
char *request = (char*)malloc(req_size);
//memset(tmp, 0, HTTP_MAX_HEADER_SIZE);
memcpy(request, buf, req_size);
// find parser and create request
http_parser *p = find_connection_by_fd(fd)->parser;
p->original_request = request;
p->original_request_size = req_size;
LOG_DEBUG("%s==================================\n%s",FUN_EVENT,request);
// convert to mhttp request
char *tmp = (char*)malloc(HTTP_MAX_HEADER_SIZE);
memset(tmp,0,HTTP_MAX_HEADER_SIZE);
memcpy(tmp,buf,count);
parse_message(p, tmp, count);
// if collector thread is not running (because this is a follow up request) -> start it again
mpsock_data_pool *pool = find_data_pool(fd);
if(!pool->is_thread_running)
{
int err;
if((err = pthread_create(&(pool->pid), NULL, &run_thread, (void*) pool)) != 0)
{
perror("pthread_create() error");
exit(0);
}
pool->is_thread_running = TRUE;
}
//LOG_INFO("%swrite request with parser->sd=%d, parser->status=%d:\n===================================================\n%s",RESULT_EVENT,p->sd,p->parser_status,request);
return f_write(p->sd, tmp, strlen(tmp)); // returning bytes must be the size of the original data
}
*/
}
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
LOG_DEBUG("%sselect(nfds)",FUN_EVENT);
if(PASS_THROUGH)
{
return f_select(nfds,readfds,writefds,exceptfds,timeout);
}
// check if there are fake descriptors to take care of
if(HASH_COUNT(mpsock_socket_table) == 0)
{
// no fake sockets -> return normal function
return f_select(nfds,readfds,writefds,exceptfds,timeout);
}
LOG_DEBUG("%shooked select(nfds)",FUN_EVENT);
// keep track of passed time
struct timeval start_exec, cur_time, diff_time;
gettimeofday(&start_exec,NULL);
// check if one of our fake descriptors is ready to read -> and if not remove it from given fd_set
int num_fake_sockets_not_ready = 0;
mpsock_socket *tracked_sockets[HASH_COUNT(mpsock_socket_table)];
mpsock_socket *current_sock;
// TODO: why is HASH_ITER not working here????????????
for(current_sock = mpsock_socket_table; current_sock != NULL; current_sock = current_sock->hh.next)
{
// TODO: also check other fd_set lists!
// check if main socket is in this list
if(readfds != NULL && current_sock->m_sd < nfds)
{
if(FD_ISSET(current_sock->m_sd,readfds))
{
// unset our main socket descriptor from the set
FD_CLR(current_sock->m_sd,readfds);
tracked_sockets[num_fake_sockets_not_ready] = current_sock;
num_fake_sockets_not_ready++;
}
}
}
int ret = 0;
if(num_fake_sockets_not_ready)
{
// we modified the set_list -> now we have to keep track of changes ourselves
struct timeval sleep, time_result;
sleep.tv_sec = 0;
sleep.tv_usec = 1; //1000; // TODO: find optimal value
gettimeofday(&cur_time,NULL);
timeval_subtract(&diff_time,&cur_time,&start_exec);
int i;
int timeout_not_passed = 1;
while(ret == 0 && timeout_not_passed)
{
// check normal sockets
// TODO: weird behaviour here
ret = f_select(nfds,readfds,writefds,exceptfds,&sleep);
// check fake sockets
for(i = 0; i<num_fake_sockets_not_ready; i++)
{
if(!DO_THREAD_ADVANCE_POINTER)
{
// advance save pointers of this socket
advance_socket_save_pointer(tracked_sockets[i]);
}
if(socket_bytes_ready(tracked_sockets[i]))
{
// this pool has something to read now!
ret++;
FD_SET(tracked_sockets[i]->m_sd,readfds);
}
}
// update times
gettimeofday(&cur_time,NULL);
timeval_subtract(&diff_time,&cur_time,&start_exec);
// get difference between timout and passed time
timeout_not_passed = timeval_subtract(&time_result,&diff_time,timeout);
LOG_DEBUG("%sdiff_time=%zu s",RESULT_EVENT,diff_time.tv_sec);
}
}
else
{
// we did not fiddle with anything -> make normal call
ret = f_select(nfds,readfds,writefds,exceptfds,timeout);
}
LOG_DEBUG("%sselect() returned: %d",FUN_EVENT,ret);
return ret;
/*
if(PASS_THROUGH)
{
int ret = f_select(nfds,readfds,writefds,exceptfds,timeout);
LOG_DEBUG("%sselect() returned: %d",FUN_EVENT,ret);
return ret;
}
// keep track of passed time
struct timeval start_exec, cur_time, diff_time;
gettimeofday(&start_exec,NULL);
// check if one of our fake descriptors is ready to read -> and if not remove it from given fd_set
int num_fake_sockets_not_ready = 0;
mpsock_data_pool *tracked_pools[HASH_COUNT(data_pools)];
mpsock_data_pool *pool;
for(pool=data_pools; pool != NULL; pool=pool->hh.next)
{
// TODO: also check other fd_set lists!
if(readfds != NULL && pool->fd < nfds)
{
if(FD_ISSET(pool->fd,readfds))
{
// check if our fake descriptor is ready to read
if(pool->pos_buffer_save == pool->pos_buffer_read)
{
// no new data -> unset fd
FD_CLR(pool->fd,readfds);
tracked_pools[num_fake_sockets_not_ready] = pool;
num_fake_sockets_not_ready++;
}
}
}
}
int ret = 0;
if(num_fake_sockets_not_ready)
{
// we modified the set_list -> now we have to keep track of changes ourselves
struct timeval sleep, time_result;
sleep.tv_sec = 0;
sleep.tv_usec = 1000;//1000; // TODO: find optimal value
gettimeofday(&cur_time,NULL);
timeval_subtract(&diff_time,&cur_time,&start_exec);
int i;
int timeout_not_passed = 1;
while(ret == 0 && timeout_not_passed)
{
ret = f_select(nfds,readfds,writefds,exceptfds,&sleep);
for(i = 0; i<num_fake_sockets_not_ready; i++)
{
if(tracked_pools[i]->pos_buffer_save > tracked_pools[i]->pos_buffer_read)
{
// this pool has something to read now!
ret++;
FD_SET(tracked_pools[i]->fd,readfds);
}
}
// update times
gettimeofday(&cur_time,NULL);
timeval_subtract(&diff_time,&cur_time,&start_exec);
// get difference between timout and passed time
timeout_not_passed = timeval_subtract(&time_result,&diff_time,timeout);
LOG_DEBUG("%sdiff_time=%d s",RESULT_EVENT,diff_time.tv_sec);
}
}
else
{