-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtaskibc.c
1826 lines (1595 loc) · 51.1 KB
/
taskibc.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
/*****************************************************************************
*
* $Id: taskibc.c,v 1.132 2007/02/11 20:29:53 mbse Exp $
* Purpose ...............: mbtask - Internet BBS Chat (but it looks like...)
*
*****************************************************************************
* Copyright (C) 1997-2006
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* MBSE BBS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
#include "../config.h"
#include "../lib/mbselib.h"
#include "taskstat.h"
#include "taskutil.h"
#include "taskchat.h"
#include "taskibc.h"
/* Globals */
_ncs_list ncs_list[MAXIBC_NCS];
_srv_list srv_list[MAXIBC_SRV];
_usr_list usr_list[MAXIBC_USR];
_chn_list chn_list[MAXIBC_CHN];
extern int internet; /* Internet status */
time_t scfg_time = (time_t)0; /* Servers config time */
time_t now; /* Current time */
int callchg = FALSE; /* Is call state changed */
int srvchg = FALSE; /* Is serverlist changed */
int usrchg = FALSE; /* Is userlist changed */
int chnchg = FALSE; /* Is channellist changed */
int banchg = FALSE; /* Is banned users changed */
int nickchg = FALSE; /* Is nicknames changed */
time_t resettime; /* Time to reset all */
int do_reset = FALSE; /* Reset init */
int link_reset = FALSE; /* Reset one link */
extern struct sockaddr_in clientaddr_in; /* IBC remote socket */
#define PING_PONG_LOG 1
typedef enum {NCS_INIT, NCS_CALL, NCS_WAITPWD, NCS_CONNECT, NCS_HANGUP, NCS_FAIL, NCS_DEAD} NCSTYPE;
static char *ncsstate[] = {
(char *)"init", (char *)"call", (char *)"waitpwd", (char *)"connect",
(char *)"hangup", (char *)"fail", (char *)"dead"
};
static char *mon[] = {
(char *)"Jan",(char *)"Feb",(char *)"Mar",
(char *)"Apr",(char *)"May",(char *)"Jun",
(char *)"Jul",(char *)"Aug",(char *)"Sep",
(char *)"Oct",(char *)"Nov",(char *)"Dec"
};
/*
* Internal prototypes
*/
void fill_ncslist(char *, char *, char *, int, unsigned int);
void dump_ncslist(void);
int add_server(char *, int, char *, char *, char *, char *);
void del_server(char *);
void del_router(char *);
int send_msg(int, char *);
void broadcast(char *, char *);
void check_servers(void);
int command_pass(int, char *, char *);
int command_server(char *, char *);
int command_squit(int, char *, char *);
int command_user(int, char *, char *);
int command_quit(int, char *, char *);
int command_nick(int, char *, char *);
int command_join(int, char *, char *);
int command_part(int, char *, char *);
int command_topic(int, char *, char *);
int command_privmsg(int, char *, char *);
/*
* Add a server to the neighbour serverlist
*/
void fill_ncslist(char *server, char *myname, char *passwd, int dyndns, unsigned int crc)
{
int i;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) == 0) {
strncpy(ncs_list[i].server, server, 63);
strncpy(ncs_list[i].resolved, server, 63);
strncpy(ncs_list[i].myname, myname, 63);
strncpy(ncs_list[i].passwd, passwd, 15);
ncs_list[i].state = NCS_INIT;
ncs_list[i].action = now;
ncs_list[i].last = (time_t)0;
ncs_list[i].version = 0;
ncs_list[i].remove = FALSE;
ncs_list[i].socket = -1;
ncs_list[i].token = 0;
ncs_list[i].gotpass = FALSE;
ncs_list[i].gotserver = FALSE;
ncs_list[i].dyndns = dyndns;
ncs_list[i].halfdead = 0;
ncs_list[i].crc = crc;
return;
}
}
WriteError("IBC: ncs_list is full");
}
void dump_ncslist(void)
{
char temp1[128], temp2[128], buf[21];
struct tm ptm;
time_t tnow;
int i, first;
if (!callchg && !srvchg && !usrchg && !chnchg && !banchg && !nickchg)
return;
if (callchg) {
first = FALSE;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server)) {
if (! first) {
Syslog('r', "IBC: Idx Server State Del Pwd Srv Dyn 1/2 Next action");
Syslog('r', "IBC: --- ------------------------------ ------- --- --- --- --- --- -----------");
first = TRUE;
}
snprintf(temp1, 30, "%s", ncs_list[i].server);
Syslog('r', "IBC: %3d %-30s %-7s %s %s %s %s %3d %d", i, temp1, ncsstate[ncs_list[i].state],
ncs_list[i].remove ? "yes":"no ", ncs_list[i].gotpass ? "yes":"no ",
ncs_list[i].gotserver ? "yes":"no ", ncs_list[i].dyndns ? "yes":"no ",
ncs_list[i].halfdead, (int)ncs_list[i].action - (int)now);
}
}
if (! first) {
Syslog('r', "IBC: No servers configured");
}
}
if (srvchg) {
first = FALSE;
for (i = 0; i < MAXIBC_SRV; i++) {
if (strlen(srv_list[i].server)) {
if (! first) {
Syslog('+', "IBC: Idx Server Router Hops User Connect time");
Syslog('+', "IBC: --- ------------------------- ------------------------- ---- ---- --------------------");
first = TRUE;
}
snprintf(temp1, 25, "%s", srv_list[i].server);
snprintf(temp2, 25, "%s", srv_list[i].router);
tnow = (time_t)srv_list[i].connected;
localtime_r(&tnow, &ptm);
snprintf(buf, 21, "%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
Syslog('+', "IBC: %3d %-25s %-25s %4d %4d %s", i, temp1, temp2, srv_list[i].hops, srv_list[i].users, buf);
}
}
if (! first) {
Syslog('+', "IBC: Servers list is empty");
}
}
if (usrchg) {
first = FALSE;
for (i = 0; i < MAXIBC_USR; i++) {
if (strlen(usr_list[i].server)) {
if (! first) {
Syslog('+', "IBC: Idx Server User Name/Nick Channel Sys Connect time");
Syslog('+', "IBC: --- -------------------- ---------------- --------- ------------- --- --------------------");
first = TRUE;
}
snprintf(temp1, 20, "%s", usr_list[i].server);
snprintf(temp2, 16, "%s", usr_list[i].realname);
tnow = (time_t)usr_list[i].connected;
localtime_r(&tnow, &ptm);
snprintf(buf, 21, "%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
Syslog('+', "IBC: %3d %-20s %-16s %-9s %-13s %s %s", i, temp1, temp2, usr_list[i].nick, usr_list[i].channel,
usr_list[i].sysop ? "yes":"no ", buf);
}
}
if (! first) {
Syslog('+', "IBC: Users list is empty");
}
}
if (chnchg) {
first = FALSE;
for (i = 0; i < MAXIBC_CHN; i++) {
if (strlen(chn_list[i].server)) {
if (! first) {
Syslog('+', "IBC: Idx Channel Owner Topic Usr Created");
Syslog('+', "IBC: --- -------------------- --------- ------------------------------- --- --------------------");
first = TRUE;
}
tnow = (time_t)chn_list[i].created;
localtime_r(&tnow, &ptm);
snprintf(buf, 21, "%02d-%s-%04d %02d:%02d:%02d", ptm.tm_mday, mon[ptm.tm_mon], ptm.tm_year+1900,
ptm.tm_hour, ptm.tm_min, ptm.tm_sec);
Syslog('+', "IBC: %3d %-20s %-9s %-31s %3d %s", i, chn_list[i].name,
chn_list[i].owner, chn_list[i].topic, chn_list[i].users, buf);
}
}
if (! first) {
Syslog('+', "IBC: Channels list is empty");
}
}
callchg = srvchg = usrchg = chnchg = banchg = nickchg = FALSE;
}
/*
* Add one user to the userlist. Returns:
* 0 = Ok
* 1 = User already registered.
* 2 = Too many users.
*/
int add_user(char *server, char *name, char *realname)
{
int i, j, Found = FALSE;
for (j = 0; j < MAXIBC_USR; j++) {
if ((strcmp(usr_list[j].server, server) == 0) && (strcmp(usr_list[j].realname, realname) == 0)) {
Syslog('!', "IBC: add_user(%s, %s, %s), already registered", server, name, realname);
return 1;
}
}
for (i = 0; i < MAXIBC_SRV; i++) {
if (strcmp(srv_list[i].server, server) == 0) {
Found = TRUE;
break;
}
}
if (!Found) {
Syslog('!', "IBC: add_user(%s, %s, %s), unknown server", server, name, realname);
return 1;
}
for (j = 0; j < MAXIBC_USR; j++) {
if (strlen(usr_list[j].server) == 0) {
strncpy(usr_list[j].server, server, 63);
strncpy(usr_list[j].name, name, 9);
strncpy(usr_list[j].nick, name, 9);
strncpy(usr_list[j].realname, realname, 36);
usr_list[j].connected = now;
srv_list[i].users++;
srvchg = usrchg = TRUE;
Syslog('r', "IBC: add_user (%s, %s, %s) slot=%d", server, name, realname, i);
return 0;
}
}
WriteError("IBC: add_user(%s, %s, %s), user list is full", server, name, realname);
return 2;
}
/*
* Delete user from channel
*/
void del_userchannel(char *channel)
{
int i;
for (i = 0; i < MAXIBC_CHN; i++) {
if (strcmp(chn_list[i].name, channel) == 0 && chn_list[i].users) {
chnchg = TRUE;
chn_list[i].users--;
Syslog('r', "IBC: del_userchannel(%s), %d users left", channel, chn_list[i].users);
if (chn_list[i].users == 0) {
Syslog('+', "IBC: deleted empty channel %s", channel);
del_channel(channel);
}
break;
}
}
}
/*
* Delete one user. If name == NULL then delete all users of a server.
*/
void del_user(char *server, char *name)
{
int i, count = 0;
for (i = 0; i < MAXIBC_USR; i++) {
if (name && (strcmp(usr_list[i].server, server) == 0) && (strcmp(usr_list[i].name, name) == 0)) {
if (strlen(usr_list[i].channel))
del_userchannel(usr_list[i].channel);
Syslog('r', "IBC: del_user(%s, %s) from slot %d", server, printable(name, 0), i);
memset(&usr_list[i], 0, sizeof(_usr_list));
usrchg = TRUE;
count++;
} else if ((name == NULL) && (strcmp(usr_list[i].server, server) == 0)) {
if (strlen(usr_list[i].channel))
del_userchannel(usr_list[i].channel);
Syslog('r', "IBC: del_user(%s, %s) user %s from slot %d", server, printable(name, 0), usr_list[i].name, i);
memset(&usr_list[i], 0, sizeof(_usr_list));
usrchg = TRUE;
count++;
}
}
/*
* Update users list of the server
*/
for (i = 0; i < MAXIBC_SRV; i++) {
if ((strcmp(srv_list[i].server, server) == 0) && srv_list[i].users) {
srv_list[i].users -= count;
if (srv_list[i].users < 0)
srv_list[i].users = 0; /* Just in case, nothing is perfect */
srvchg = TRUE;
}
}
}
/*
* Add channel with owner, returns:
* 0 = Success
* 1 = Already registered
* 2 = Too many channels
*/
int add_channel(char *name, char *owner, char *server)
{
int i;
for (i = 0; i < MAXIBC_CHN; i++) {
if ((strcmp(chn_list[i].name, name) == 0) && (strcmp(chn_list[i].owner, owner) == 0) &&
(strcmp(chn_list[i].server, server) == 0)) {
Syslog('!', "IBC: add_channel(%s, %s, %s), already registered", name, owner, server);
return 1;
}
}
for (i = 0; i < MAXIBC_CHN; i++) {
if (strlen(chn_list[i].server) == 0) {
strncpy(chn_list[i].name, name, 20);
strncpy(chn_list[i].owner, owner, 9);
strncpy(chn_list[i].server, server, 63);
chn_list[i].users = 1;
chn_list[i].created = now;
chnchg = TRUE;
Syslog('r', "IBC: add_channel (%s, %s, %s) slot=%d", name, owner, server, i);
return 0;
}
}
WriteError("IBC: add_channel(%s, %s, %s), too many channels", name, owner, server);
return 2;
}
void del_channel(char *name)
{
int i;
for (i = 0; i < MAXIBC_CHN; i++) {
if (strlen(chn_list[i].server) && (strcmp(chn_list[i].name, name) == 0)) {
Syslog('r', "IBC: del_channel(%s), slot=%d", name, i);
memset(&chn_list[i], 0, sizeof(_chn_list));
return;
}
}
}
/*
* Add a server to the list, returns
* 0 = error
* 1 = success
*/
int add_server(char *name, int hops, char *prod, char *vers, char *fullname, char *router)
{
int i, haverouter = FALSE;
for (i = 0; i < MAXIBC_SRV; i++) {
if (strlen(srv_list[i].server) && (strcmp(srv_list[i].server, name) == 0)) {
Syslog('r', "IBC: add_server %s %d %s %s \"%s\" %s, duplicate, ignore", name, hops, prod, vers, fullname, router);
return 0;
}
}
/*
* If name <> router it's a remote server, then check if we have a router in our list.
*/
if (strcmp(name, router) && hops) {
for (i = 0; i < MAXIBC_SRV; i++) {
if (strcmp(srv_list[i].server, router) == 0) {
haverouter = TRUE;
break;
}
}
if (! haverouter) {
Syslog('r', "IBC: add_server %s %d %s %s \"%s\" %s, no router, ignore", name, hops, prod, vers, fullname, router);
return 0;
}
}
for (i = 0; i < MAXIBC_SRV; i++) {
if (strlen(srv_list[i].server) == 0) {
strncpy(srv_list[i].server, name, 63);
strncpy(srv_list[i].router, router, 63);
strncpy(srv_list[i].prod, prod, 20);
strncpy(srv_list[i].vers, vers, 20);
strncpy(srv_list[i].fullname, fullname, 35);
srv_list[i].connected = now;
srv_list[i].users = 0;
srv_list[i].hops = hops;
srvchg = TRUE;
Syslog('r', "IBC: add_server %s %d %s %s \"%s\" %s, slot=%d", name, hops, prod, vers, fullname, router, i);
return 1;
}
}
WriteError("IBC: add_server %s %d %s %s \"%s\" %s, serverlist full", name, hops, prod, vers, fullname, router);
return 0;
}
/*
* Delete server.
*/
void del_server(char *name)
{
int i;
for (i = 0; i < MAXIBC_SRV; i++) {
if (strcmp(srv_list[i].server, name) == 0) {
Syslog('r', "IBC: del_server(%s), slot %d", name, i);
del_user(srv_list[i].server, NULL);
memset(&srv_list[i], 0, sizeof(_srv_list));
srvchg = TRUE;
}
}
}
/*
* Delete router.
*/
void del_router(char *name)
{
int i;
for (i = 0; i < MAXIBC_SRV; i++) {
if (strcmp(srv_list[i].router, name) == 0) {
Syslog('r', "IBC: del_router(%s) slot %d", name, i);
del_user(srv_list[i].server, NULL);
memset(&srv_list[i], 0, sizeof(_srv_list));
srvchg = TRUE;
}
}
}
/*
* Send a message to all servers
*/
void send_all(char *msg)
{
int i;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) && (ncs_list[i].state == NCS_CONNECT)) {
send_msg(i, msg);
}
}
}
/*
* Send command message to each connected neighbour server and use the correct own fqdn.
*/
void send_at(char *cmd, char *nick, char *param)
{
int i;
char *p;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) && (ncs_list[i].state == NCS_CONNECT)) {
p = xstrcpy(cmd);
p = xstrcat(p, (char *)" ");
p = xstrcat(p, nick);
p = xstrcat(p, (char *)"@");
p = xstrcat(p, ncs_list[i].myname);
p = xstrcat(p, (char *)" ");
p = xstrcat(p, param);
p = xstrcat(p, (char *)"\r\n");
send_msg(i, p);
free(p);
}
}
}
void send_nick(char *nick, char *name, char *realname)
{
int i;
char *p;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) && (ncs_list[i].state == NCS_CONNECT)) {
p = xstrcpy((char *)"NICK ");
p = xstrcat(p, nick);
p = xstrcat(p, (char *)" ");
p = xstrcat(p, name);
p = xstrcat(p, (char *)" ");
p = xstrcat(p, ncs_list[i].myname);
p = xstrcat(p, (char *)" ");
p = xstrcat(p, realname);
p = xstrcat(p, (char *)"\r\n");
send_msg(i, p);
free(p);
}
}
}
/*
* Broadcast a message to all servers except the originating server
*/
void broadcast(char *origin, char *msg)
{
int i;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) && (ncs_list[i].state == NCS_CONNECT) && (strcmp(origin, ncs_list[i].server))) {
send_msg(i, msg);
}
}
}
/*
* Send message to a server
*/
int send_msg(int slot, char *msg)
{
char *p;
#ifndef PING_PONG_LOG
if (strcmp(msg, "PING\r\n") && strcmp(msg, "PONG\r\n")) {
#endif
p = xstrcpy((char *)"IBC: > ");
p = xstrcat(p, ncs_list[slot].server);
p = xstrcat(p, (char *)": ");
p = xstrcat(p, printable(msg, 0));
Syslogp('r', p);
free(p);
#ifndef PING_PONG_LOG
}
#endif
if (sendto(ncs_list[slot].socket, msg, strlen(msg), 0,
(struct sockaddr *)&ncs_list[slot].servaddr_in, sizeof(struct sockaddr_in)) == -1) {
WriteError("$IBC: can't send message");
return -1;
}
return 0;
}
void check_servers(void)
{
char *errmsg, *p, scfgfn[PATH_MAX];
FILE *fp;
int i, j, inlist, Remove, local_reset, conf_changed;
int a1, a2, a3, a4, users = 0, channels = 0;
unsigned int crc;
struct servent *se;
struct hostent *he;
now = time(NULL);
snprintf(scfgfn, PATH_MAX, "%s/etc/ibcsrv.data", getenv("MBSE_ROOT"));
/*
* Check if we reached the global reset time
*/
if (((int)now > (int)resettime) && !do_reset) {
resettime = time(NULL) + (time_t)86400;
do_reset = TRUE;
Syslog('+', "IBC: global reset time reached, preparing reset");
}
local_reset = conf_changed = FALSE;
for (i = 0; i < MAXIBC_USR; i++)
if (strlen(usr_list[i].server))
users++;
for (i = 0; i < MAXIBC_CHN; i++)
if (strlen(chn_list[i].server))
channels++;
if (!users && !channels && do_reset) {
Syslog('+', "IBC: no channels and users, performing reset");
local_reset = TRUE;
do_reset = FALSE;
}
if (file_time(scfgfn) != scfg_time) {
conf_changed = TRUE;
Syslog('+', "IBC: %s filetime changed, rereading configuration", scfgfn);
}
/*
* Check if configuration is changed, if so then apply the changes.
*/
if (conf_changed || local_reset || link_reset) {
if (strlen(srv_list[0].server) == 0) {
/*
* First add this server name to the servers database.
*/
add_server(CFG.myfqdn, 0, (char *)"mbsebbs", (char *)VERSION, CFG.bbs_name, (char *)"none");
}
/*
* Local reset, make all crc's invalid so the connections will restart.
*/
if (local_reset) {
for (i = 0; i < MAXIBC_NCS; i++)
if (strlen(ncs_list[i].server))
ncs_list[i].crc--;
}
if (link_reset) {
Syslog('r', "IBC: link_reset starting");
link_reset = FALSE;
}
if ((fp = fopen(scfgfn, "r"))) {
fread(&ibcsrvhdr, sizeof(ibcsrvhdr), 1, fp);
/*
* Check for neighbour servers to delete
*/
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server)) {
fseek(fp, ibcsrvhdr.hdrsize, SEEK_SET);
inlist = FALSE;
while (fread(&ibcsrv, ibcsrvhdr.recsize, 1, fp)) {
crc = 0xffffffff;
crc = upd_crc32((char *)&ibcsrv, crc, sizeof(ibcsrv));
if ((strcmp(ncs_list[i].server, ibcsrv.server) == 0) && ibcsrv.Active && (ncs_list[i].crc == crc)) {
inlist = TRUE;
}
}
if (!inlist) {
if (local_reset || link_reset)
Syslog('+', "IBC: server %s connection reset", ncs_list[i].server);
else
Syslog('+', "IBC: server %s configuration changed or removed", ncs_list[i].server);
ncs_list[i].remove = TRUE;
ncs_list[i].action = now;
srvchg = TRUE;
callchg = TRUE;
}
}
}
/*
* Start removing servers
*/
Remove = FALSE;
for (i = 0; i < MAXIBC_NCS; i++) {
if (ncs_list[i].remove) {
Remove = TRUE;
Syslog('r', "IBC: Remove server %s", ncs_list[i].server);
if (ncs_list[i].state == NCS_CONNECT) {
p = calloc(512, sizeof(char));
if (local_reset) {
snprintf(p, 512, "SQUIT %s Reset connection\r\n", ncs_list[i].server);
broadcast(ncs_list[i].server, p);
snprintf(p, 512, "SQUIT %s Your system connection is reset\r\n", ncs_list[i].myname);
send_msg(i, p);
} else {
snprintf(p, 512, "SQUIT %s Removed from configuration\r\n", ncs_list[i].server);
broadcast(ncs_list[i].server, p);
snprintf(p, 512, "SQUIT %s Your system is removed from configuration\r\n", ncs_list[i].myname);
send_msg(i, p);
}
free(p);
del_router(ncs_list[i].server);
}
if (ncs_list[i].socket != -1) {
Syslog('r', "IBC: Closing socket %d", ncs_list[i].socket);
shutdown(ncs_list[i].socket, SHUT_WR);
ncs_list[i].socket = -1;
ncs_list[i].state = NCS_HANGUP;
}
callchg = TRUE;
srvchg = TRUE;
}
}
dump_ncslist();
if (link_reset) {
link_reset = FALSE;
}
/*
* If a neighbour is removed by configuration, remove it from the list.
*/
if (Remove) {
Syslog('r', "IBC: Starting remove list");
for (i = 0; i < MAXIBC_NCS; i++) {
if (ncs_list[i].remove) {
Syslog('r', " do %s", ncs_list[i].server);
memset(&ncs_list[i], 0, sizeof(_ncs_list));
callchg = TRUE;
}
}
}
dump_ncslist();
/*
* Changed or deleted servers are now removed, add new
* configured servers or changed servers.
*/
fseek(fp, ibcsrvhdr.hdrsize, SEEK_SET);
while (fread(&ibcsrv, ibcsrvhdr.recsize, 1, fp)) {
/*
* Check for new configured servers
*/
if (ibcsrv.Active && strlen(ibcsrv.myname) && strlen(ibcsrv.server) && strlen(ibcsrv.passwd)) {
inlist = FALSE;
for (i = 0; i < MAXIBC_NCS; i++) {
if (strcmp(ncs_list[i].server, ibcsrv.server) == 0) {
inlist = TRUE;
}
}
for (j = 0; j < MAXIBC_SRV; j++) {
if ((strcmp(srv_list[j].server, ibcsrv.server) == 0) && (strcmp(srv_list[j].router, ibcsrv.server))) {
inlist = TRUE;
Syslog('+', "IBC: can't add new configured server %s: already connected via %s",
ibcsrv.server, srv_list[j].router);
}
}
if (!inlist ) {
crc = 0xffffffff;
crc = upd_crc32((char *)&ibcsrv, crc, sizeof(ibcsrv));
fill_ncslist(ibcsrv.server, ibcsrv.myname, ibcsrv.passwd, ibcsrv.Dyndns, crc);
srvchg = TRUE;
callchg = TRUE;
Syslog('+', "IBC: new configured Internet BBS Chatserver: %s", ibcsrv.server);
}
}
}
fclose(fp);
}
scfg_time = file_time(scfgfn);
}
dump_ncslist();
/*
* Check if we need to make state changes
*/
for (i = 0; i < MAXIBC_NCS; i++) {
if (strlen(ncs_list[i].server) == 0)
continue;
if (((int)ncs_list[i].action - (int)now) <= 0) {
switch (ncs_list[i].state) {
case NCS_INIT: if (internet) {
/*
* Internet is available, setup the connection.
* Get IP address for the hostname.
* Set default next action to 60 seconds.
*/
ncs_list[i].action = now + (time_t)60;
// Gives SIGBUS on Sparc memset(&ncs_list[i].servaddr_in, 0, sizeof(struct sockaddr_in));
se = getservbyname("fido", "udp");
ncs_list[i].servaddr_in.sin_family = AF_INET;
ncs_list[i].servaddr_in.sin_port = se->s_port;
if (sscanf(ncs_list[i].server,"%d.%d.%d.%d",&a1,&a2,&a3,&a4) == 4)
ncs_list[i].servaddr_in.sin_addr.s_addr = inet_addr(ncs_list[i].server);
else if ((he = gethostbyname(ncs_list[i].server)))
memcpy(&ncs_list[i].servaddr_in.sin_addr, he->h_addr, he->h_length);
else {
switch (h_errno) {
case HOST_NOT_FOUND: errmsg = (char *)"Authoritative: Host not found"; break;
case TRY_AGAIN: errmsg = (char *)"Non-Authoritive: Host not found"; break;
case NO_RECOVERY: errmsg = (char *)"Non recoverable errors"; break;
default: errmsg = (char *)"Unknown error"; break;
}
Syslog('!', "IBC: no IP address for %s: %s", ncs_list[i].server, errmsg);
ncs_list[i].action = now + (time_t)120;
ncs_list[i].state = NCS_FAIL;
callchg = TRUE;
break;
}
if (ncs_list[i].socket == -1) {
ncs_list[i].socket = socket(AF_INET, SOCK_DGRAM, 0);
if (ncs_list[i].socket == -1) {
Syslog('!', "$IBC: can't create socket for %s", ncs_list[i].server);
ncs_list[i].state = NCS_FAIL;
ncs_list[i].action = now + (time_t)120;
callchg = TRUE;
break;
}
Syslog('r', "IBC: socket %d created for %s", ncs_list[i].socket, ncs_list[i].server);
} else {
Syslog('r', "IBC: socket %d reused for %s", ncs_list[i].socket, ncs_list[i].server);
}
ncs_list[i].state = NCS_CALL;
ncs_list[i].action = now + (time_t)1;
callchg = TRUE;
} else {
/*
* No internet, just wait
*/
ncs_list[i].action = now + (time_t)10;
}
break;
case NCS_CALL: /*
* In this state we accept PASS and SERVER commands from
* the remote with the same token as we have sent.
*/
Syslog('r', "IBC: %s call", ncs_list[i].server);
if (strlen(ncs_list[i].passwd) == 0) {
Syslog('!', "IBC: no password configured for %s", ncs_list[i].server);
ncs_list[i].state = NCS_FAIL;
ncs_list[i].action = now + (time_t)300;
callchg = TRUE;
break;
}
ncs_list[i].token = gettoken();
p = calloc(512, sizeof(char));
snprintf(p, 512, "PASS %s 0100 %s\r\n", ncs_list[i].passwd, ncs_list[i].compress ? "Z":"");
send_msg(i, p);
snprintf(p, 512, "SERVER %s 0 %d mbsebbs %s %s\r\n", ncs_list[i].myname, ncs_list[i].token,
VERSION, CFG.bbs_name);
send_msg(i, p);
free(p);
ncs_list[i].action = now + (time_t)10;
ncs_list[i].state = NCS_WAITPWD;
callchg = TRUE;
break;
case NCS_WAITPWD: /*
* This state can be left by before the timeout is reached
* by a reply from the remote if the connection is accepted.
*/
Syslog('r', "IBC: %s waitpwd", ncs_list[i].server);
ncs_list[i].token = 0;
ncs_list[i].state = NCS_CALL;
while (TRUE) {
j = 1+(int) (1.0 * CFG.dialdelay * rand() / (RAND_MAX + 1.0));
if ((j > (CFG.dialdelay / 10)) && (j > 9))
break;
}
Syslog('r', "IBC: next call in %d %d seconds", CFG.dialdelay, j);
ncs_list[i].action = now + (time_t)j;
callchg = TRUE;
break;
case NCS_CONNECT: /*
* In this state we check if the connection is still alive
*/
j = (int)now - (int)ncs_list[i].last;
if (ncs_list[i].halfdead > 5) {
/*
* Halfdead means 5 times received a PASS while we are in
* connected state. This means the other side "thinks" it's
* not connected and tries to connect. This can be caused by
* temporary internet problems.
* Reset our side of the connection.
*/
Syslog('+', "IBC: server %s connection is half dead", ncs_list[i].server);
ncs_list[i].state = NCS_DEAD;
ncs_list[i].action = now + (time_t)60; // 1 minute delay before calling again.
ncs_list[i].gotpass = FALSE;
ncs_list[i].gotserver = FALSE;
ncs_list[i].token = 0;
ncs_list[i].halfdead = 0;
p = calloc(81, sizeof(char));
snprintf(p, 81, "SQUIT %s Connection died\r\n", ncs_list[i].server);
broadcast(ncs_list[i].server, p);
free(p);
callchg = TRUE;
srvchg = TRUE;
system_shout("*** NETWORK SPLIT, lost connection with server %s", ncs_list[i].server);
del_router(ncs_list[i].server);
break;
}
if (((int)now - (int)ncs_list[i].last) > 130) {
/*
* Missed 3 PING replies
*/
Syslog('+', "IBC: server %s connection is dead", ncs_list[i].server);
ncs_list[i].state = NCS_DEAD;
ncs_list[i].action = now + (time_t)120; // 2 minutes delay before calling again.
ncs_list[i].gotpass = FALSE;
ncs_list[i].gotserver = FALSE;
ncs_list[i].token = 0;
ncs_list[i].halfdead = 0;
p = calloc(81, sizeof(char));
snprintf(p, 81, "SQUIT %s Connection died\r\n", ncs_list[i].server);
broadcast(ncs_list[i].server, p);
free(p);
callchg = TRUE;
srvchg = TRUE;
system_shout("*** NETWORK SPLIT, lost connection with server %s", ncs_list[i].server);
del_router(ncs_list[i].server);
break;
}
/*
* Ping at 60, 90 and 120 seconds
*/
if (((int)now - (int)ncs_list[i].last) > 120) {
Syslog('r', "IBC: sending 3rd PING at 120 seconds");
send_msg(i, (char *)"PING\r\n");
} else if (((int)now - (int)ncs_list[i].last) > 90) {
Syslog('r', "IBC: sending 2nd PING at 90 seconds");
send_msg(i, (char *)"PING\r\n");
} else if (((int)now - (int)ncs_list[i].last) > 60) {
send_msg(i, (char *)"PING\r\n");
}
ncs_list[i].action = now + (time_t)10;
break;
case NCS_HANGUP: Syslog('r', "IBC: %s hangup => call", ncs_list[i].server);
ncs_list[i].action = now + (time_t)1;
ncs_list[i].state = NCS_CALL;
callchg = TRUE;
srvchg = TRUE;
break;
case NCS_DEAD: Syslog('r', "IBC: %s dead -> call", ncs_list[i].server);
ncs_list[i].action = now + (time_t)1;
ncs_list[i].state = NCS_CALL;
callchg = TRUE;
srvchg = TRUE;
break;
case NCS_FAIL: Syslog('r', "IBC: %s fail => init", ncs_list[i].server);
ncs_list[i].action = now + (time_t)1;
ncs_list[i].state = NCS_INIT;
callchg = TRUE;
srvchg = TRUE;
break;
}
}
}
dump_ncslist();
}