-
Notifications
You must be signed in to change notification settings - Fork 9
/
hasses.c
1300 lines (1167 loc) · 38.5 KB
/
hasses.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
/* Asynchronous SSE Server
* Author: Peter Deak ([email protected])
* License: GPL
*/
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <pwd.h>
#include "cdata.h"
#include "chat.h"
#include "hasses.h"
#include "cio.h"
/* Example chat:
-> GET /sse?subscribe=XXX HTTP/1.1
-> Host: 192.168.1.100:80
-> User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0
-> Accept: text/event-stream
-> Accept-Language: en-US,en;q=0.5
-> DNT: 1
-> Referer: http://192.168.1.200/sse2/
-> Origin: http://192.168.1.200
-> Connection: keep-alive
-> Pragma: no-cache
-> Cache-Control: no-cache
<- HTTP/1.1 200 OK
<- Date: Wed, 14 Jan 2015 21:01:35 GMT
<- Server: Apache/2.2.22 (Debian)
<- X-Powered-By: PHP/5.4.35-0+deb7u2
<- Cache-Control: no-cache
<- Keep-Alive: timeout=5, max=100
<- Connection: Keep-Alive
<- Transfer-Encoding: chunked
<- Content-Type: text/event-stream
<-
<- e8
<- id: 1421269313
<- data: Hello this is the first
*/
struct Hasses_Settings hsettings;
struct Hasses_Statistics stats;
//other
char *input = NULL;
time_t last_cli_ttl_check = 0;
int epoll_descriptor;
time_t log_oldrawtime=1;
char log_timebuf[80];
struct CommCli *commFirst = NULL; //First Communication client
void beforeExit(void)
{
if(strlen(hsettings.pidfile) > 0)
if(unlink(hsettings.pidfile) != 0)
toLog(0,"Warning: Cannot delete pid file!\n");
}
void sigint_handler(int sig)
{
beforeExit();
toLog(0,"Received INT/TERM signal, Exiting...\n");
exit(0);
}
int name_to_uid(char const *name)
{
if(name==NULL || strlen(name) <= 0)
return -1;
struct passwd *pwd = getpwnam(name);
if(pwd != NULL)
return pwd->pw_uid;
return -1;
}
int create_and_bind(int port)
{
char portstr[10];
struct addrinfo hints;
struct addrinfo *result, *rp;
int s, sfd,optval=1;
toLog(2,"Create/bind listening socket (%d)...\n",port);
memset(&hints, 0, sizeof (struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
snprintf(portstr,10,"%d",port);
s = getaddrinfo (NULL,portstr, &hints, &result);
if(s != 0)
{
toLog(0,"Error in getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
s = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,&optval,sizeof(optval));
if(s != 0)
{
toLog(0,"Error, setsockopt() failure\n");
return -1;
}
s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
if (s == 0)
{
/* We managed to bind successfully! */
toLog(2,"Bind to port:%s <%d>\n",portstr,sfd);
break;
}
close(sfd);
}
if (rp == NULL)
{
toLog(0,"Error, could not bind socket on port %d\n",port);
return -1;
}
freeaddrinfo(result);
toLog(2,"Socket created and bound.\n");
return sfd;
}
int make_socket_non_blocking(int sfd)
{
int flags, s;
flags = fcntl(sfd, F_GETFL, 0);
if (flags == -1)
{
toLog(0,"Error, fcntl() failure in make_socket_non_blocking (1)\n");
return 1;
}
flags |= O_NONBLOCK;
s = fcntl(sfd, F_SETFL, flags);
if (s == -1)
{
toLog(0,"Error, fcntl() in make_socket_non_blocking (2)\n");
return 1;
}
return 0;
}
int close_client(int d)
{
/* Closing the descriptor will make epoll remove it
from the set of descriptors which are monitored.
this below may unnecessary */
epoll_ctl(epoll_descriptor,EPOLL_CTL_DEL,d,NULL);
cio_client_close(client_get(d));
close(d);
//Remove from my list
client_del(d);
toLog(1,"Closed connection (Remaining clients: %d) <%d>\n",client_count(),d);
return 0;
}
int close_communication_client(int d)
{
/* Closing the descriptor will make epoll remove it
from the set of descriptors which are monitored.
this below may unnecessary */
epoll_ctl(epoll_descriptor,EPOLL_CTL_DEL,d,NULL);
close(d);
commclient_del(d);
toLog(2,"Closed communication connection <%d>\n",client_count(),d);
return 0;
}
int printversion(void)
{
printf("Hyper's async SSE (Server Sent Event) server\n"
"Version: %s\n"
"Compiled: %s\n"
"Author: Peter Deak ([email protected])\n"
"License: GPL\n",VERSION, __DATE__);
return 0;
}
int printhelp(void)
{
printf("Hyper's async SSE (Server Sent Event) server\n"
"Usage:\n hasses -p=<SSE_PORT> -murl=<MATCHING_URL>\n"
" [-cp=<COMM_PORT>] [-fifo=<FIFOFILE>] [-sep=<delimiter>]\n"
" [-q|-debug] [-l=<LOGFILE>] [-pidfile=<PIDFILE>]\n"
" [-ssl] [-cert-file=<PEMFILE>] [-privatekey-file=<KEYFILE>]\n"
" [-cors-base=<URL>] [-ra] [-user=<USER>] [-nodaemon]\n"
"Commands on communication channel or fifo file:\n"
" \"status\" - Print status/statistics to the log\n"
" \"tcpstatus\" - Send status/statistics back to the sender\n"
" \"clientlist\" - List clients to the log\n"
" \"loglevel_quiet\" - Set loglevel to minimal\n"
" \"loglevel_normal\" - Set loglevel to normal\n"
" \"loglevel_debug\" - Set loglevel to maximum\n"
" \"numberofclients\" - Print number of clients to the log\n"
" \"tcpnumberofclients\" - Send number of clients back to the sender\n"
" \"reinit_enable\" - Enable re-initialize opened connections\n"
" \"reinit_disable\" - Enable re-initialize opened connections\n"
" \"subscribedclients:<sub>\" - Print number of the subscribed clients to the log\n"
" \"tcpsubscribedclients:<sub>\" - Send number of the subscribed clients back to the sender\n"
" \"<token>=<message>\" - Send message to the subscribers of <token>\n"
" \"<token>=<message>;<token2>=<message2>\" - Send more messages\n"
" \"<token>-<rId>=<message>\" - Send message to the subscribers of <token> except <rId>\n"
" \"*=<message>;\" - Send message to all clients\n\n");
return 0;
}
void attach_signal_handler(void)
{
struct sigaction sa;
sa.sa_handler = sigint_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if(sigaction(SIGINT, &sa, NULL) == -1)
{
toLog(0,"Error, Cannot set signal handler: sigaction() failure. Exiting...\n");
beforeExit();
exit(1);
}
if(sigaction(SIGTERM, &sa, NULL) == -1)
{
toLog(0,"Error, Cannot set signal handler: sigaction() failure. Exiting...\n");
beforeExit();
exit(1);
}
}
int create_open_fifo(const char *fifofile)
{
int s;
int fifo;
toLog(2,"Create/open fifo file: %s\n",fifofile);
//Create the FIFO (ignore the error if it already exists).
if( mkfifo(fifofile, 0666 ) < 0 )
{
if(errno != EEXIST )
{
toLog(0,"Error creating fifo file:%s mkfifo() failure. Exiting...\n",fifofile);
beforeExit();
exit(1);
}
}
fifo = open(fifofile,O_RDWR);
if( fifo < 0 )
{
toLog(0,"Error opening fifo file:%s Exiting...\n",fifofile);
beforeExit();
exit(1);
}
toLog(2,"FIFO opened.\n" );
s = make_socket_non_blocking (fifo);
if(s != 0)
{
toLog(0,"Cannot setup fifo device, Exiting...\n");
beforeExit();
exit(1);
}
toLog(2,"FIFO is non blocking.\n");
return fifo;
}
int my_cio_high_read(struct CliConn *client,char *buffer)
{
if(strlen(buffer) > 0)
{
toLog(2,"Received from %s <%d>:\n",client->info,client->descr);
chat_received(client,buffer,hsettings.match_url);
}
return 0;
}
int my_cio_low_write(struct CliConn *client,char *buffer,int length)
{
int s;
if(client->err)
return 1;
s=write(client->descr,buffer,length);
if(s != length)
{
client->err = 1;
return 1;
}
return 0;
}
int main(int argi,char **argc)
{
int s;
int sfd = -1;
int commfd = -1;
int port = 0;
int commport = 0;
hsettings.loglevel = 1;
hsettings.use_ssl = 0;
hsettings.reinit_allowed = 0;
hsettings.paramuid=-1;
hsettings.nodaemon=0;
h_strlcpy(hsettings.pidfile,"",128);
h_strlcpy(hsettings.match_url,"",64);
h_strlcpy(hsettings.fifofile,"",128);
h_strlcpy(hsettings.logfile,"/var/log/hasses.log",128);
h_strlcpy(hsettings.paramuser,"",64);
h_strlcpy(hsettings.certfile,"",128);
h_strlcpy(hsettings.pkeyfile,"",128);
h_strlcpy(hsettings.corsbase,"*",128);
h_strlcpy(hsettings.delimiter,";",2);
stats.startDaemon = 0;
stats.maxclients = 0;
stats.allclient = 0;
stats.allreinit = 0;
stats.allmessage = 0;
stats.allsmessage = 0;
h_strlcpy(log_timebuf,"error:",80);
if(argi <= 1)
{
printhelp();
return 0;
}
int p;
for(p = 1 ; p < argi ; ++p)
{
if(!strcmp(argc[p],"-h") || !strcmp(argc[p],"-help"))
return printhelp();
if(!strcmp(argc[p],"-v") || !strcmp(argc[p],"-V") || !strcmp(argc[p],"-version"))
return printversion();
if(!strcmp(argc[p],"-q"))
{
hsettings.loglevel = 0;
continue;
}
if(!strcmp(argc[p],"-debug"))
{
hsettings.loglevel = 2;
continue;
}
if(!strcmp(argc[p],"-ra"))
{
hsettings.reinit_allowed = 1;
continue;
}
if(!strcmp(argc[p],"-nodaemon"))
{
hsettings.nodaemon = 1;
continue;
}
if(!strcmp(argc[p],"-ssl"))
{
hsettings.use_ssl = 1;
continue;
}
if(!strncmp(argc[p],"-murl=",6) && strlen(argc[p]) > 6)
{
h_strlcpy(hsettings.match_url,argc[p]+6,63);
continue;
}
if(!strncmp(argc[p],"-p=",3) && strlen(argc[p]) > 3)
{
if(sscanf(argc[p]+3,"%d",&port) == 1 && port > 0)
continue;
}
if(!strncmp(argc[p],"-cp=",4) && strlen(argc[p]) > 4)
{
if(sscanf(argc[p]+4,"%d",&commport) == 1 && commport > 0)
continue;
}
if(!strncmp(argc[p],"-l=",3) && strlen(argc[p]) > 3)
{
h_strlcpy(hsettings.logfile,argc[p]+3,127);
continue;
}
if(!strncmp(argc[p],"-fifo=",6) && strlen(argc[p]) > 6)
{
h_strlcpy(hsettings.fifofile,argc[p]+6,127);
continue;
}
if(!strncmp(argc[p],"-sep=",5) && strlen(argc[p]) > 5)
{
hsettings.delimiter[0] = argc[p][5];
hsettings.delimiter[1] = '\0';
continue;
}
if(!strncmp(argc[p],"-pidfile=",9) && strlen(argc[p]) > 9)
{
h_strlcpy(hsettings.pidfile,argc[p]+9,127);
continue;
}
if(!strncmp(argc[p],"-cert-file=",11) && strlen(argc[p]) > 11)
{
h_strlcpy(hsettings.certfile,argc[p]+11,127);
continue;
}
if(!strncmp(argc[p],"-privatekey-file=",17) && strlen(argc[p]) > 17)
{
h_strlcpy(hsettings.pkeyfile,argc[p]+17,127);
continue;
}
if(!strncmp(argc[p],"-cors-base=",11) && strlen(argc[p]) > 11)
{
h_strlcpy(hsettings.corsbase,argc[p]+11,127);
continue;
}
if(!strncmp(argc[p],"-user=",6) && strlen(argc[p]) > 6)
{
h_strlcpy(hsettings.paramuser,argc[p]+6,63);
hsettings.paramuid = name_to_uid(hsettings.paramuser);
if(hsettings.paramuid == -1)
{
fprintf(stderr,"Error unknown user/Cannot setuid to user: \"%s\"\n",hsettings.paramuser);
exit(1);
}
continue;
}
if(!strncmp(argc[p],"-",1))
{
fprintf(stderr,"Error, unknown switch or incorrect use: \"%s\"\n",argc[p]);
return 1;
}
}
if(strlen(hsettings.match_url) == 0 ||
(strlen(hsettings.fifofile) == 0 && commport == 0) ||
port == 0 )
{
fprintf(stderr,"Error, Neither FIFO file nor communication port is specified!\n\n");
printhelp();
return 0;
}
if(hsettings.logfile[0] != '/' ||
(strlen(hsettings.fifofile) > 0 && hsettings.fifofile[0] != '/') ||
(strlen(hsettings.pidfile) > 0 && hsettings.pidfile[0] != '/'))
{
fprintf(stderr,"WARNING: Use absolute path to specify fifo, log or pid files!\n");
return 0;
}
stats.startDaemon = time(NULL);
toLog(1,"\n=== daemon starting ===\n");
toLog(1,"Pid: %d\n",getpid());
if(hsettings.loglevel > 1)
{
toLog(2,"Parameters:\n");
toLog(2," loglevel: %d\n",hsettings.loglevel);
toLog(2," Match url: %s\n",hsettings.match_url);
toLog(2," TCP Port (SSE): %d\n",port);
if(commport > 0)
toLog(2," TCP Port (Communication): %d\n",commport);
toLog(2," FIFO file: %s\n",strlen(hsettings.fifofile) > 0 ? hsettings.fifofile : "-none-");
toLog(2," Delimiter: %s\n",hsettings.delimiter);
toLog(2," Mode: %s\n",(hsettings.use_ssl?"SSL (https)":"Normal (http)"));
if(hsettings.use_ssl)
{
toLog(2," SSL Cert key: %s\n",hsettings.certfile);
toLog(2," SSL Prvt key: %s\n",hsettings.pkeyfile);
}
toLog(2," Log file: %s\n",hsettings.logfile);
toLog(2," Pid file: %s\n",hsettings.pidfile);
toLog(2," Set daemon user: %s\n",hsettings.paramuser);
printf("Parameters:\n");
printf(" loglevel: %d\n",hsettings.loglevel);
printf(" Match url: %s\n",hsettings.match_url);
printf(" TCP Port (SSE): %d\n",port);
if(commport > 0)
printf(" TCP Port (Communication): %d\n",commport);
printf(" Fifo file: %s\n",strlen(hsettings.fifofile) > 0 ? hsettings.fifofile : "-none-");
printf(" Mode: %s\n",(hsettings.use_ssl?"SSL (https)":"Normal (http)"));
if(hsettings.use_ssl)
{
printf(" SSL Cert key: %s\n",hsettings.certfile);
printf(" SSL Prvt key: %s\n",hsettings.pkeyfile);
}
printf(" Log file: %s\n",hsettings.logfile);
printf(" Pid file: %s\n",hsettings.pidfile);
printf(" Set daemon user: %s\n",hsettings.paramuser);
fflush(stdout);
}
if(!hsettings.nodaemon)
{
if(hsettings.loglevel > 0)
toLog(1,"Started, Entering daemon mode...\n");
if(daemon(0,0) == -1)
{
toLog(0,"Error, daemon() failure, Exiting...\n");
exit(1);
}
toLog(2,"Entered daemon mode.\n");
}
else
{
toLog(2,"\nWARNING: Daemon mode is disabled by -nodaemon switch!\n"
"All messages written to the standard output!\n"
"THE HASSES DOES NOT USE THE LOG FILE!\n\n");
}
if(hsettings.paramuid >= 0)
{
if(setuid(hsettings.paramuid) == 0)
{
toLog(2,"SetUid done to uid: %d (%s)\n",
hsettings.paramuid,hsettings.paramuser);
}
else
{
toLog(2,"SetUid done to uid: %d (%s) failed! Exiting...\n",
hsettings.paramuid,hsettings.paramuser);
exit(1);
}
}
if(strlen(hsettings.pidfile) > 0)
{
FILE *pidf = NULL;
if((pidf = fopen(hsettings.pidfile,"w")) != NULL)
{
fprintf(pidf,"%d",getpid());
fclose(pidf);
}
else
toLog(0,"Error, Cannot open pid file!\n");
}
attach_signal_handler();
int fifo = -1;
if(strlen(hsettings.fifofile) > 0)
fifo = create_open_fifo(hsettings.fifofile);
struct epoll_event event;
struct epoll_event *events;
input = (char *)malloc(sizeof(char) * MAX_READ_SIZE);
cio_high_read_SET(my_cio_high_read);
cio_low_write_SET(my_cio_low_write);
client_init();
chat_init(&hsettings,&stats);
if(cio_init(hsettings.use_ssl, hsettings.certfile, hsettings.pkeyfile))
{
toLog(0,"Exiting due to previous error...\n");
beforeExit();
exit(1);
}
toLog(2,"Open SSE port to listen...\n");
sfd = create_and_bind(port);
if (sfd == -1)
{
toLog(0,"Exiting due to previous error...\n");
beforeExit();
exit(1);
}
s = make_socket_non_blocking(sfd);
if(s == -1)
{
toLog(0,"Exiting due to previous error...\n");
beforeExit();
exit(1);
}
s = listen (sfd, SOMAXCONN);
if(s == -1)
{
toLog(0,"Error, listen() failure. Exiting...\n");
beforeExit();
exit(1);
}
if(commport > 0)
{
toLog(2,"Open communication port to listen...\n");
commfd = create_and_bind(commport);
if (commfd == -1)
{
toLog(0,"Exiting due to previous error...\n");
beforeExit();
exit(1);
}
s = make_socket_non_blocking(commfd);
if(s == -1)
{
toLog(0,"Exiting due to previous error...\n");
beforeExit();
exit(1);
}
s = listen (commfd, SOMAXCONN);
if(s == -1)
{
toLog(0,"Error, listen() failure. Exiting...\n");
beforeExit();
exit(1);
}
}
toLog(2,"Creating epoll...\n");
epoll_descriptor = epoll_create1(0);
if (epoll_descriptor == -1)
{
toLog(0,"Error, epoll_create1() failure, Exiting...\n");
beforeExit();
exit(1);
}
if(fifo >= 0)
{
event.data.fd = fifo;
event.events = EPOLLIN | EPOLLET;
s = epoll_ctl(epoll_descriptor, EPOLL_CTL_ADD, fifo, &event);
if(s == -1)
{
toLog(0,"Error, epoll_ctl() add fifo failure (1) Exiting...\n");
beforeExit();
exit(1);
}
}
event.data.fd = sfd;
event.events = EPOLLIN | EPOLLET;
s = epoll_ctl(epoll_descriptor, EPOLL_CTL_ADD, sfd, &event);
if(s == -1)
{
toLog(0,"Error, epoll_ctl() add sse socket failure (2) Exiting...\n");
beforeExit();
exit(1);
}
if(commfd >= 0)
{
event.data.fd = commfd;
event.events = EPOLLIN | EPOLLET;
s = epoll_ctl(epoll_descriptor, EPOLL_CTL_ADD, commfd, &event);
if(s == -1)
{
toLog(0,"Error, epoll_ctl() add communication socket failure (3) Exiting...\n");
beforeExit();
exit(1);
}
}
toLog(2,"Epoll created.\n");
/* Buffer where events are returned */
events = calloc (MAXEVENTS, sizeof event);
/* The event loop */
toLog(2,"Starting main event loop...\n");
while(1)
{
int n,i;
checkTimeouts();
n = epoll_wait (epoll_descriptor, events, MAXEVENTS, -1);
for (i = 0; i < n; i++)
{
if ((events[i].events & EPOLLERR) ||
(events[i].events & EPOLLHUP) ||
(!(events[i].events & EPOLLIN)))
{
/* An error has occured on this fd, or the socket is not
ready for reading (why were we notified then?) */
if(commclient_check(events[i].data.fd)) //communication client
{
toLog(2,"Communication client HUP/ERR or shutdown, Closing...\n");
close_communication_client(events[i].data.fd);
}
else //sse client
{
toLog(1, "SSE client HUP/ERR or shutdown, Closing...\n");
close_client(events[i].data.fd);
}
continue;
}
else if (sfd == events[i].data.fd)
{
/* We have a notification on the listening socket, which
means one or more incoming connections. */
while (1)
{
struct sockaddr in_addr;
socklen_t in_len;
int infd;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
in_len = sizeof(in_addr);
infd = accept(sfd, &in_addr, &in_len);
if(infd == -1)
{
if ((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
/* We have processed all incoming connections. */
break;
}
else
{
toLog(1, "Error, accept() failure!\n");
break;
}
}
s = getnameinfo (&in_addr, in_len,
hbuf, sizeof hbuf,
sbuf, sizeof sbuf,
NI_NUMERICHOST | NI_NUMERICSERV);
if(s == 0)
toLog(1,"Connect from %s on port %s as <%d>\n",hbuf,sbuf,infd);
else
{
toLog(1,"Error in getnameinfo() <%d>\n",infd);
}
/* Make the incoming socket non-blocking and add it to the
list of fds to monitor. */
s = make_socket_non_blocking(infd);
if (s == -1)
{
toLog(1,"Cannot set new accepted socket to non blocking. Closing socket!\n");
close(infd);
break;
}
event.data.fd = infd;
event.events = EPOLLIN | EPOLLET;
s = epoll_ctl(epoll_descriptor, EPOLL_CTL_ADD, infd, &event);
if (s == -1)
{
toLog(0,"Error, epoll_ctl() add failure (3)\n");
beforeExit();
exit(1);
}
//Add to my list
int ccount,sn_r;
client_add(infd);
sn_r = snprintf(client_current()->info,63,"%s:%s",hbuf,sbuf);
if(sn_r < 0)
h_strlcpy(client_current()->info,"truncated",64); //probably never happend that ip and port is greater than 63
client_current()->status = STATUS_NEW;
ccount = client_count();
toLog(2,"Added to the list (%d).\n",ccount);
if(stats.maxclients < ccount)
stats.maxclients = ccount;
}
continue;
}
else if (commfd == events[i].data.fd)
{
/* We have a notification on the communication listening socket, which
means one or more incoming connections for internal communication. */
while (1)
{
struct sockaddr in_addr;
socklen_t in_len;
int infd;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
in_len = sizeof(in_addr);
infd = accept(commfd, &in_addr, &in_len);
if(infd == -1)
{
if ((errno == EAGAIN) ||
(errno == EWOULDBLOCK))
{
/* We have processed all incoming connections. */
break;
}
else
{
toLog(1, "Error, accept() failure!\n");
break;
}
}
if(hsettings.loglevel >= 2)
{
s = getnameinfo (&in_addr, in_len,
hbuf, sizeof hbuf,
sbuf, sizeof sbuf,
NI_NUMERICHOST | NI_NUMERICSERV);
if(s == 0)
toLog(2,"Communication connect from %s on port %s as <%d>\n",hbuf,sbuf,infd);
else
{
toLog(1,"Error in getnameinfo() <%d>\n",infd);
}
}
/* Make the incoming socket non-blocking and add it to the
list of fds to monitor. */
s = make_socket_non_blocking(infd);
if (s == -1)
{
toLog(1,"Cannot set new accepted socket to non blocking. Closing socket!\n");
close(infd);
break;
}
event.data.fd = infd;
event.events = EPOLLIN | EPOLLET;
s = epoll_ctl(epoll_descriptor, EPOLL_CTL_ADD, infd, &event);
if (s == -1)
{
toLog(0,"Error, epoll_ctl() add failure (3)\n");
beforeExit();
exit(1);
}
commclient_add(infd);
}
continue;
}
else if (fifo == events[i].data.fd)
{
input[0] = '\0';
char *input_p = input;
while(1)
{
ssize_t count;
count = read(events[i].data.fd, input_p, MAX_READ_SIZE-strlen(input));
if(count == -1)
{
/* If errno == EAGAIN, that means we have read all
data. So go back to the main loop. */
if (errno != EAGAIN)
{
toLog(1,"Error, read() failure (1)\n");
}
break;
}
else if (count == 0)
{
break;
}
input_p[count]='\0';
input_p = input_p + count;
}
chop(input);
toLog(2,"#FIFO received message: \"%s\"\n",input);
parse_comm_messages(input, NON_TCP_SENDER);
}
else
{
/* We have data on the fd waiting to be read. Read and
display it. We must read whatever data is available
completely, as we are running in edge-triggered mode
and won't get a notification again for the same
data. */
int done = 0;
input[0] = '\0';
char *input_p = input;
ssize_t fullcount=0;
ssize_t count;
while(1)
{
count = read(events[i].data.fd,input_p, MAX_READ_SIZE-strlen(input));
if(count == -1)
{
/* If errno == EAGAIN, that means we have read all
data. So go back to the main loop. */
if (errno != EAGAIN)
{
toLog(1,"Error, read() failure (2) closing client...\n");
done = 1;
}
break;
}
else if (count == 0)
{
done = 1;
break;
}
fullcount += count;
input_p = input_p + count;
}
input[fullcount]='\0';
if(commclient_check(events[i].data.fd)) //communication client
{
if(done)
{
toLog(2,"Communication connection closed by peer:\n");
if(strlen(input) > 3)
{
chop(input);
toLog(2,"#COMM-TCP last received message: \"%s\"\n",input);
parse_comm_messages(input, events[i].data.fd);
}
close_communication_client(events[i].data.fd);
}
else
{
chop(input);
toLog(2,"#COMM-TCP received message: \"%s\"\n",input);
parse_comm_messages(input, events[i].data.fd);
}
}
else //sse client
{
if(done)
{
toLog(2,"Connection closed by peer:\n");
close_client(events[i].data.fd);
}
else
{
cio_low_read(client_get(events[i].data.fd),input,fullcount);
}
}
}
}
}
free (events);
close (sfd);
return 0;
}
void commclient_add(int fd)
{
struct CommCli *n;
struct CommCli *ccli = (struct CommCli *)malloc(sizeof(struct CommCli));
ccli->fd = fd;
ccli->next = NULL;
if(commFirst == NULL)
commFirst = ccli;
else
{
for(n = commFirst;n->next != NULL;n = n->next);
n->next = ccli;
}
}