-
Notifications
You must be signed in to change notification settings - Fork 20
/
bsd.c
2783 lines (2541 loc) · 87.6 KB
/
bsd.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
/* bsd.c */
#ifdef SOLARIS
/* Solaris has broken header declarations */
char *index(const char *, int);
void bzero(void *, int);
#endif
#include "copyright.h"
#include "autoconf.h"
#ifdef VMS
#include "multinet_root:[multinet.include.sys]file.h"
#include "multinet_root:[multinet.include.sys]ioctl.h"
#include "multinet_root:[multinet.include]errno.h"
#else
#include <sys/file.h>
#include <sys/ioctl.h>
#ifdef SYSWAIT
#include <sys/wait.h>
#else
#include <wait.h>
#endif
#endif
#include <signal.h>
/* For MTU/MSS additions */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
/*hack*/
/* #define NSIG 32 */
#ifndef NSIG
#define NSIG 32
#endif
#include "mudconf.h"
#include "config.h"
#include "db.h"
#include "file_c.h"
#include "externs.h"
#include "interface.h"
#include "flags.h"
#include "alloc.h"
#include "command.h"
#include "attrs.h"
#include "mail.h"
#include "rhost_ansi.h"
#include "rhost_utf8.h"
#include "local.h"
#include "door.h"
#include "debug.h"
#define FILENUM BSD_C
#include <math.h>
#ifdef TLI
#include <sys/stream.h>
#include <sys/tiuser.h>
#include <sys/tihdr.h>
#include <stropts.h>
#include <poll.h>
#define shutdown(x,y) 0
extern int t_errno;
extern char *t_errlist[];
#endif
#if defined BSD_LIKE || defined TLI || defined SOLARIS
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#endif
extern void NDECL(dispatch);
void NDECL(pcache_sync);
static int sock;
int ndescriptors = 0;
DESC *descriptor_list = NULL;
DESC *desc_in_use = NULL;
DESC *FDECL(initializesock, (int, struct sockaddr_in *, char *, int));
DESC *FDECL(new_connection, (int));
int FDECL(process_output, (DESC *));
int FDECL(process_input, (DESC *));
extern void FDECL(broadcast_monitor, (dbref, int, char *, char *, char *, int, int, int, char *));
extern int FDECL(lookup, (char *, char *, int, int *));
extern CF_HAND(cf_site);
extern double NDECL(next_timer);
extern int FDECL(alarm_msec, (double));
extern int NDECL(alarm_stop);
extern unsigned int CRC32_ProcessBuffer(unsigned int, const void *, unsigned int);
int signal_depth;
void populate_tor_seed(void)
{
struct sockaddr_in *p_sock2;
void *v_sock;
struct addrinfo hints, *res, *p_res;
char ipstr[INET_ADDRSTRLEN + 1], *tbuff, *tbuff2, *tpr2, *strtok, *mystrtok_r;
char *tok1, *tok2, *tok3, *tok4, *tokptr;
int i_validip;
if ( !*(mudconf.tor_localhost) ) {
strcpy(mudstate.tor_localcache, (char *)"1.0.0.127");
return;
}
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
tbuff = alloc_lbuf("populate_tor_seed");
tpr2 = tbuff2 = alloc_lbuf("populate_tor_seed2");
memset(tbuff2, '\0', LBUF_SIZE);
strcpy(tbuff, mudconf.tor_localhost);
strtok = strtok_r(tbuff, " \t", &mystrtok_r);
i_validip = 0;
while ( strtok ) {
if ((getaddrinfo(strtok, NULL, &hints, &res)) == 0) {
for( p_res = res; p_res != NULL; p_res = p_res->ai_next) {
p_sock2 = (struct sockaddr_in *)p_res->ai_addr;
v_sock = &(p_sock2->sin_addr);
inet_ntop(p_res->ai_family, v_sock, ipstr, sizeof(ipstr)-1);
tok1 = tok2 = tok3 = tok4 = NULL;
tok1 = strtok_r(ipstr, ".", &tokptr);
if ( tok1 ) {
tok2 = strtok_r(NULL, ".", &tokptr);
if ( tok2 ) {
tok3 = strtok_r(NULL, ".", &tokptr);
if ( tok3 ) {
tok4 = strtok_r(NULL, ".", &tokptr);
}
}
}
if ( !tok4 ) {
continue;
}
if ( i_validip )
safe_chr(' ', tbuff2, &tpr2);
safe_str(tok4, tbuff2, &tpr2);
safe_chr('.', tbuff2, &tpr2);
safe_str(tok3, tbuff2, &tpr2);
safe_chr('.', tbuff2, &tpr2);
safe_str(tok2, tbuff2, &tpr2);
safe_chr('.', tbuff2, &tpr2);
safe_str(tok1, tbuff2, &tpr2);
i_validip = 1;
}
freeaddrinfo(res);
}
strtok = strtok_r(NULL, " \t", &mystrtok_r);
}
tpr2 = tbuff2 + 999;
while ( tpr2 && *tpr2 && !isspace(*tpr2) )
tpr2--;
*tpr2 = '\0';
if ( !*tbuff2 ) {
strcpy(mudstate.tor_localcache, (char *)"1.0.0.127");
} else {
strncpy(mudstate.tor_localcache, tbuff2, 999);
}
free_lbuf(tbuff);
free_lbuf(tbuff2);
}
/*********************************************************************************************
* 1. Need a mudstate.tor_ipaddr 1000 char buffer to house the tor-translated addresses
* 2. Need to write a mudconf.tor_localhost to mudstate.tor_ipaddr cache routine
* 3. Need to loop for each entry in tor_ipaddr for a tor lookup *bleh*
* 4. Thi function needs re-arrangement to minimalize as much as we can on overhead
*/
int check_tor(struct in_addr a_remote, int i_port) {
char *s_reverselocal, *s_reverseremote, *s_tordns, *tortok, *tortokptr,
*s_tmp, *s_tok1, *s_tok2, *s_tok3, *s_tok4, *s_tokptr, *s_tmp2,
ipstr[INET_ADDRSTRLEN + 1];
int i_found;
struct addrinfo hints, *res, *p_res;
struct sockaddr_in *p_sock2;
void *v_sock;
if ( !*(mudstate.tor_localcache) )
populate_tor_seed();
s_tmp = alloc_lbuf("check_tor_local");
s_reverselocal = alloc_sbuf("check_tor_1");
s_reverseremote = alloc_sbuf("check_tor_2");
s_tordns = alloc_mbuf("check_tor_3");
memset(s_tmp, '\0', LBUF_SIZE);
strcpy(s_tmp, mudstate.tor_localcache);
tortok = strtok_r(s_tmp, " \t", &tortokptr);
i_found = 0;
while (tortok) {
sprintf(s_reverselocal, "%s", tortok);
s_tmp2 = inet_ntoa(a_remote);
s_tok1 = s_tok2 = s_tok3 = s_tok4 = NULL;
s_tok1 = strtok_r(s_tmp2, ".", &s_tokptr);
if ( s_tok1 ) {
s_tok2 = strtok_r(NULL, ".", &s_tokptr);
if ( s_tok2 ) {
s_tok3 = strtok_r(NULL, ".", &s_tokptr);
if ( s_tok3 ) {
s_tok4 = strtok_r(NULL, ".", &s_tokptr);
}
}
}
if ( s_tok4 ) {
sprintf(s_reverseremote, "%s.%s.%s.%s", s_tok4, s_tok3, s_tok2, s_tok1);
sprintf(s_tordns, "%s.%d.%s.ip-port.exitlist.torproject.org", s_reverseremote, i_port, s_reverselocal);
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if ((getaddrinfo(s_tordns, NULL, &hints, &res)) == 0) {
memset(ipstr, '\0', sizeof(ipstr));
for( p_res = res; p_res != NULL; p_res = p_res->ai_next) {
p_sock2 = (struct sockaddr_in *)p_res->ai_addr;
v_sock = &(p_sock2->sin_addr);
inet_ntop(p_res->ai_family, v_sock, ipstr, sizeof(ipstr)-1);
if ( strcmp(ipstr, "127.0.0.2") == 0 ) {
i_found = 1;
break;
}
}
freeaddrinfo(res);
if ( i_found )
break;
}
}
tortok = strtok_r(NULL, " \t", &tortokptr);
}
free_lbuf(s_tmp);
free_sbuf(s_reverselocal);
free_sbuf(s_reverseremote);
free_mbuf(s_tordns);
return i_found;
}
int make_socket(int port, char* address)
{
int s, opt;
FILE *f_fptr;
#ifdef TLI
struct t_bind *bindreq, *bindret;
struct sockaddr_in *serverreq, *serverret;
#else
struct sockaddr_in server;
#endif
DPUSH; /* #1 */
#ifdef TLI
s = t_open(TLI_TCP, (O_RDWR | O_NDELAY), NULL);
#else
s = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (s < 0) {
log_perror("NET", "FAIL", NULL, "creating master socket");
DPOP; /* #1 */
exit(3);
}
#ifdef TLI
bindreq = (struct t_bind *) t_alloc(s, T_BIND, T_ALL);
if (!bindreq) {
log_perror("NET", "FAIL", NULL, "t_alloc(req)");
close(s);
DPOP; /* #1 */
exit(4);
}
bindret = (struct t_bind *) t_alloc(s, T_BIND, T_ALL);
if (!bindret) {
log_perror("NET", "FAIL", NULL, "t_alloc(ret)");
close(s);
DPOP; /* #1 */
exit(4);
}
serverreq = (struct sockaddr_in *) bindreq->addr.buf;
serverreq->sin_family = AF_INET;
if(inet_addr((const char*)address) != -1)
serverreq->sin_addr.s_addr = inet_addr((const char*)address);
else
serverreq->sin_addr.s_addr = INADDR_ANY;
serverreq->sin_port = htons(port);
bindreq->addr.len = sizeof(struct sockaddr_in);
bindret->addr.maxlen = sizeof(struct sockaddr_in);
bindreq->qlen = 5;
if (t_bind(s, bindreq, bindret) < 0) {
log_perror("NET", "FAIL", NULL, "t_bind");
close(s);
DPOP; /* #1 */
exit(5);
}
serverret = (struct sockaddr_in *) bindret->addr.buf;
if (serverreq->sin_port != serverret->sin_port) {
log_perror("NET", "FAIL", NULL, "t_bind(portinuse)");
close(s);
DPOP; /* #1 */
exit(6);
}
#else
opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
(char *) &opt, sizeof(opt)) < 0) {
log_perror("NET", "FAIL", NULL, "setsockopt");
}
server.sin_family = AF_INET;
if(inet_addr((const char*)address) != -1)
server.sin_addr.s_addr = inet_addr((const char*)address);
else
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(s, (struct sockaddr *) &server, sizeof(server))) {
log_perror("NET", "FAIL", NULL, "bind");
close(s);
DPOP; /* #1 */
exit(4);
}
listen(s, 5);
#endif
STARTLOG(LOG_STARTUP, "INI", "BSD")
log_text(unsafe_tprintf("RhostMUSH is now listening on port <%d>", port));
ENDLOG
if ( (f_fptr = fopen("netrhost.pid", "w")) != NULL ) {
fprintf(f_fptr, "%d\n", (int)getpid());
fclose(f_fptr);
}
RETURN(s); /* #1 */
}
#ifndef HAVE_GETTIMEOFDAY
#define get_tod(x) { (x)->tv_sec = time(NULL); (x)->tv_usec = 0; }
#else
#define get_tod(x) gettimeofday(x, (struct timezone *)0)
#endif
int maxd;
void
shovechars(int port,char* address)
{
fd_set input_set, output_set;
struct timeval last_slice, current_time, next_slice, timeout;
int found, check, new_connection_error_count = 0;
DESC *d, *dnext, *newd;
CMDENT *cmdp = NULL;
int avail_descriptors, maxfds, active_auths, aflags2, temp1, temp2;
int sitecntr, i_oldlasttime, i_oldlastcnt, flagkeep, i_len, i_cntr;
dbref aowner2;
char *logbuff, *progatr, all[10], tsitebuff[1001], *ptsitebuff, s_cutter[6],
s_cutter2[8], *progatr_str, *progatr_strptr, *s_progatr, *b_progatr,
*t_progatr, *b_progatrptr, *tstrtokr;
#ifdef ZENTY_ANSI
char *s_buff, *s_buffptr, *s_buff2, *s_buff2ptr, *s_buff3, *s_buff3ptr;
#endif
FILE *f;
int silent, i_progatr, anum;
unsigned int ulCRC32;
ATTR *ap;
#ifdef TLI
struct pollfd *fds;
int i;
#define CheckInput(x) ((fds[x].revents & POLLIN) == POLLIN)
#define CheckOutput(x) ((fds[x].revents & POLLOUT) == POLLOUT)
#else
#define CheckInput(x) FD_ISSET(x, &input_set)
#define CheckOutput(x) FD_ISSET(x, &output_set)
#endif
DPUSH; /* #2 */
mudstate.debug_cmd = (char *) "< shovechars >";
sock = make_socket(port, address);
maxd = sock + 1;
get_tod(&last_slice);
flagkeep = i_oldlasttime = i_oldlastcnt = 0;
f = fopen("reboot.silent","r");
silent=0;
found = -1;
if(f != NULL) {
silent=1;
memset(tsitebuff, '\0', sizeof(tsitebuff));
fgets(tsitebuff, sizeof(tsitebuff)-1, f);
fclose(f);
if ( *tsitebuff )
found = atoi(tsitebuff);
remove("reboot.silent");
}
/* we may be rebooting, so recalc maxd */
DESC_ITER_ALL(d) {
if( d->descriptor >= maxd ) {
maxd = d->descriptor + 1;
}
if ((d->flags & DS_HAS_DOOR) && (d->door_desc >= maxd)) {
maxd = d->door_desc + 1;
}
if( d->flags & DS_CONNECTED ) {
if(!silent) {
queue_string(d, "Game: New server image successfully loaded.\r\n");
} else if ( d->player == found ) {
queue_string(d, "Game: Finished Rebooting Silently.\r\n");
}
strncpy(all, Name(d->player), 5);
*(all + 5) = '\0';
if (!stricmp(all, "guest")) {
temp1 = atoi((Name(d->player) + 5));
temp2 = 0x00000001;
temp2 <<= (temp1 - 1);
mudstate.guest_status |= temp2;
mudstate.guest_num++;
}
if ( strlen(mudconf.guest_namelist) > 0 ) {
memset(tsitebuff, 0, sizeof(tsitebuff));
strncpy(tsitebuff, mudconf.guest_namelist, 1000);
ptsitebuff = strtok_r(tsitebuff, " \t", &tstrtokr);
sitecntr = 1;
while ( (ptsitebuff != NULL) && (sitecntr < 32) ) {
if ( lookup_player(NOTHING, ptsitebuff, 0) == d->player ) {
temp1 = sitecntr;
temp2 = 0x00000001;
temp2 <<= (temp1 - 1);
mudstate.guest_status |= temp2;
mudstate.guest_num++;
}
ptsitebuff = strtok_r(NULL, " \t", &tstrtokr);
sitecntr++;
}
}
}
if ( Good_obj(d->player) && InProgram(d->player) ) {
if ( (mudconf.login_to_prog && !(ProgCon(d->player))) ||
(!mudconf.login_to_prog && ProgCon(d->player)) ) {
queue_string(d, "You are still in a @program.\r\n");
progatr = atr_get(d->player, A_PROGPROMPTBUF, &aowner2, &aflags2);
if ( progatr && *progatr ) {
if ( strcmp(progatr, "NULL") != 0 ) {
#ifdef ZENTY_ANSI
s_buffptr = s_buff = alloc_lbuf("parse_ansi_prompt");
s_buff2ptr = s_buff2 = alloc_lbuf("parse_ansi_prompt2");
s_buff3ptr = s_buff3 = alloc_lbuf("parse_ansi_prompt3");
parse_ansi((char *) progatr, s_buff, &s_buffptr, s_buff2, &s_buff2ptr, s_buff3, &s_buff3ptr);
queue_string(d, unsafe_tprintf("%s%s%s \377\371", ANSI_HILITE, s_buff, ANSI_NORMAL));
free_lbuf(s_buff);
free_lbuf(s_buff2);
free_lbuf(s_buff3);
#else
queue_string(d, unsafe_tprintf("%s%s%s \377\371", ANSI_HILITE, progatr, ANSI_NORMAL));
#endif
}
} else {
queue_string(d, unsafe_tprintf("%s>%s \377\371", ANSI_HILITE, ANSI_NORMAL));
}
free_lbuf(progatr);
} else {
queue_string(d, "Your @program was aborted from the @reboot.\r\n");
s_Flags4(d->player, (Flags4(d->player) & (~INPROGRAM)));
queue_string(d, "\377\371");
mudstate.shell_program = 0;
atr_clr(d->player, A_PROGBUFFER);
atr_clr(d->player, A_PROGPROMPTBUF);
}
}
}
#ifdef HAVE_GETDTABLESIZE
maxfds = getdtablesize();
#else
maxfds = sysconf(_SC_OPEN_MAX);
#endif
#ifdef TLI
fds = (struct pollfd *) malloc(sizeof(struct pollfd) * maxfds);
#endif
avail_descriptors = maxfds - 36;
STARTLOG(LOG_STARTUP, "INI", "BSD")
log_text((char *) "Max Descriptors: ");
log_text((char *) unsafe_tprintf("%d", avail_descriptors));
mudstate.max_logins_allowed = avail_descriptors - 10;
ENDLOG
while (mudstate.shutdown_flag == 0) {
/* Beginning of a new cycle - free unsafe_tprint buffers */
freeTrackedBuffers();
get_tod(¤t_time);
last_slice = update_quotas(last_slice, current_time);
process_commands();
#ifdef ENABLE_DOORS
door_processInternalDoors();
#endif
local_tick();
if (mudstate.shutdown_flag || mudstate.reboot_flag)
break;
/* test for events */
dispatch();
/* any queued robot commands waiting? */
double next = roundf(que_next() * 10) / 10;
timeout.tv_sec = floor(next);
timeout.tv_usec = floor(1000000 * fmod(next,1.0)); ;
next_slice = msec_add(last_slice, mudconf.timeslice);
timeval_sub(next_slice, current_time);
#ifdef TLI
for (i = 0; i < maxfds; i++)
fds[i].fd = -1;
#else
FD_ZERO(&input_set);
FD_ZERO(&output_set);
#endif
/* Listen for new connections if there are free descriptors */
if (ndescriptors < avail_descriptors) {
#ifdef TLI
fds[sock].fd = sock;
fds[sock].events = POLLIN;
#else
FD_SET(sock, &input_set);
#endif
}
active_auths = 0;
/* Mark sockets that we want to test for change in status */
DESC_ITER_ALL(d) {
#ifdef TLI
if (!d->input_head || d->output_head) {
fds[d->descriptor].fd = d->descriptor;
fds[d->descriptor].events = 0;
if (!d->input_head)
fds[d->descriptor].events |= POLLIN;
if (d->output_head)
fds[d->descriptor].events |= POLLOUT;
}
#else
if (!d->input_head && (d->flags & DS_AUTH_IN_PROGRESS) == 0)
FD_SET(d->descriptor, &input_set);
if (d->output_head)
FD_SET(d->descriptor, &output_set);
if (d->flags & DS_AUTH_IN_PROGRESS) {
active_auths++;
if (d->authdescriptor >= maxd)
maxd = d->authdescriptor + 1;
if (d->flags & (DS_NEED_AUTH_WRITE|DS_AUTH_CONNECTING))
FD_SET(d->authdescriptor, &output_set);
FD_SET(d->authdescriptor, &input_set);
}
if (d->flags & DS_HAS_DOOR) {
if (d->door_desc >= maxd)
maxd = d->door_desc + 1;
if (!d->door_input_head)
FD_SET(d->door_desc, &input_set);
if (d->door_output_head)
FD_SET(d->door_desc, &output_set);
}
#endif
}
door_registerInternalDoorDescriptors(&input_set, &output_set, &maxd);
/* Enable AUTH timeout processing */
if( active_auths > 0 ) {
timeout.tv_sec = MIN(timeout.tv_sec,1);
}
/* Wait for something to happen */
#ifdef TLI
found = poll(fds, maxfds, timeout.tv_sec);
#else
found = select(maxd, &input_set, &output_set, (fd_set *) NULL,
&timeout);
#endif
if (found < 0) {
if (errno != EINTR) {
(mudstate.scheck)++;
switch(errno) {
case EBADF:
fprintf(stderr,"BAD FILE DESCRIPTOR\n");
break;
case EINVAL:
fprintf(stderr,"N is NEGATIVE\n");
break;
case ENOMEM:
fprintf(stderr,"NO MEMORY\n");
}
log_perror("NET", "FAIL",
"checking for activity, clearing decriptor queues", "select");
DESC_ITER_ALL(d) {
freeqs(d,0);
}
if (mudstate.scheck > 2) {
log_perror("NET","FAIL","Repeated select failures. Shutting down.","select");
break;
}
}
}
/* NOTE: Some firewalls discard AUTH connection attempts
** instead of refusing the connection. This can cause
** the connection to hang with no activity on the port.
** That is why we must check for timeouts here before
** we detect if there is socket activity. -Thorin 04/2002
*/
if( active_auths > 0 ) {
DESC_SAFEITER_ALL(d, dnext) {
if( (d->flags & DS_AUTH_IN_PROGRESS) &&
(time(NULL) - d->connected_at >= 3) ) {
d->flags &= ~DS_AUTH_IN_PROGRESS;
shutdown(d->authdescriptor, 2);
close(d->authdescriptor);
strcpy(d->userid,"");
STARTLOG(LOG_NET, "NET", "FAIL")
logbuff = alloc_mbuf("shovechars.LOG.authtimeout");
sprintf(logbuff,
"[%d/%s] Auth request timed out",
d->descriptor,
d->addr);
log_text(logbuff);
free_mbuf(logbuff);
ENDLOG
}
}
}
if (found < 0) {
continue;
}
mudstate.scheck = 0;
/* if !found then time for robot commands */
if (!found) {
do_top(mudconf.queue_chunk);
continue;
} else {
do_top(mudconf.active_q_chunk);
}
/* Check for new connection requests */
check = CheckInput(sock);
if (check) {
newd = new_connection(sock);
if (!newd) {
#ifdef TLI
check = (errno && (errno != ENFILE));
#else
check = (errno && (errno != EINTR) &&
(errno != EMFILE) &&
(errno != ENFILE));
#endif
if (check) {
if( ++new_connection_error_count == 10 ) {
log_perror("NET", "FAIL", NULL,
"new_connection/stop");
}
else if( new_connection_error_count < 10 ) {
log_perror("NET", "FAIL", NULL,
"new_connection");
}
}
} else {
if (newd->descriptor >= maxd)
maxd = newd->descriptor + 1;
}
}
/* Check for activity on user sockets */
DESC_SAFEITER_ALL(d, dnext) {
/* Check for Auth */
if (d->flags & DS_AUTH_IN_PROGRESS) {
if ((d->flags & DS_AUTH_CONNECTING) &&
CheckOutput(d->authdescriptor)) {
check_auth_connect(d);
}
else if ((d->flags & DS_NEED_AUTH_WRITE) &&
CheckOutput(d->authdescriptor)) {
write_auth(d);
}
else if( CheckInput(d->authdescriptor) ) {
check_auth(d);
}
}
if (d->flags & DS_HAS_DOOR) {
if (CheckInput(d->door_desc)) {
d->last_time = mudstate.now;
if (!process_door_input(d)) {
closeDoorWithId(d, d->door_num);
}
}
}
/* Lensy:
* Additional check needed because the above call might close the conny
*/
if (d->flags & DS_HAS_DOOR) {
if (CheckOutput(d->door_desc)) {
if (!process_door_output(d)) {
closeDoorWithId(d, d->door_num);
}
}
}
/* Process input from sockets with pending input */
check = CheckInput(d->descriptor);
if (check) {
/* Undo autodark */
i_oldlasttime = d->last_time;
flagkeep = d->flags;
if ( Good_obj(d->player) && !TogHideIdle(d->player) ) {
d->last_time = mudstate.now;
if (d->flags & DS_AUTODARK) {
d->flags &= ~DS_AUTODARK;
s_Flags(d->player,
Flags(d->player) & ~DARK);
}
if (d->flags & DS_AUTOUNF) {
d->flags &= ~DS_AUTOUNF;
s_Flags2(d->player,
Flags2(d->player) & ~UNFINDABLE);
}
} else if ( d->last_time == 0 ) {
d->last_time = mudstate.now;
}
/* Process received data */
i_oldlastcnt = d->input_tot;
if (!process_input(d)) {
shutdownsock(d, R_SOCKDIED);
continue;
}
/* Idle stamp checking for command typed */
if ( mudconf.idle_stamp && (d->flags & DS_CONNECTED) && d->input_head && (char *)(d->input_head->cmd) ) {
ulCRC32 = 0;
i_len = strlen(d->input_head->cmd);
ulCRC32 = CRC32_ProcessBuffer(ulCRC32, d->input_head->cmd, i_len);
anum = mkattr("_IDLESTAMP");
if ( anum > 0 ) {
ap = atr_num(anum);
if (ap) {
attr_wizhidden("_IDLESTAMP");
progatr = atr_get(d->player, ap->number, &aowner2, &aflags2);
if ( progatr ) {
progatr_str = progatr;
i_progatr = 0;
while ( progatr_str && *progatr_str ) {
if ( *progatr_str == ' ' )
i_progatr++;
progatr_str++;
}
s_progatr = alloc_sbuf("idle_stamp");
sprintf(s_progatr, "%u", ulCRC32);
if ( (i_progatr >= (mudconf.idle_stamp_max-1)) && (strstr(progatr, s_progatr) == NULL) ) {
progatr_str = strtok_r(progatr, " ", &progatr_strptr);
if ( progatr_str )
progatr_str = strtok_r(NULL, " ", &progatr_strptr);
/* Let's catch up to the current value if i_progatr - 1 still > mudconf.idle_stamp_max */
if ( ((i_progatr - 1) >= (mudconf.idle_stamp_max-1)) && progatr_str )
progatr_str = strtok_r(NULL, " ", &progatr_strptr);
} else {
progatr_str = strtok_r(progatr, " ", &progatr_strptr);
}
b_progatrptr = b_progatr = alloc_lbuf("idle_stamp");
i_progatr = 0;
anum = 0;
i_cntr = 0;
if ( progatr_str ) {
t_progatr = alloc_lbuf("idle_stamp_movetoend");
while ( progatr_str ) {
if ( strstr(progatr_str, s_progatr) != NULL ) {
anum = 1;
if ( strchr(progatr_str, ':') != NULL ) {
i_progatr = atoi(strchr(progatr_str, ':')+1);
i_progatr++;
} else {
i_progatr = 1;
}
if ( i_progatr > 1 ) {
sprintf(t_progatr, "%u:%d", ulCRC32, i_progatr);
} else {
sprintf(s_progatr, "%u:%d", ulCRC32, i_progatr);
if ( i_cntr > 0 )
safe_chr(' ', b_progatr, &b_progatrptr);
safe_str(s_progatr, b_progatr, &b_progatrptr);
i_cntr++;
}
} else {
if ( i_cntr > 0 )
safe_chr(' ', b_progatr, &b_progatrptr);
safe_str(progatr_str, b_progatr, &b_progatrptr);
i_progatr = 1;
i_cntr++;
}
progatr_str = strtok_r(NULL, " ", &progatr_strptr);
}
/* Move the last command issued to end of the list if existed */
if ( *t_progatr ) {
if ( i_cntr > 0 ) {
safe_chr(' ', b_progatr, &b_progatrptr);
}
safe_str(t_progatr, b_progatr, &b_progatrptr);
}
free_lbuf(t_progatr);
}
if ( !anum ) {
if ( i_progatr )
safe_chr(' ', b_progatr, &b_progatrptr);
sprintf(s_progatr, "%u:1", ulCRC32);
safe_str(s_progatr, b_progatr, &b_progatrptr);
}
atr_add_raw(d->player, ap->number, b_progatr);
free_sbuf(s_progatr);
free_lbuf(b_progatr);
}
free_lbuf(progatr);
}
}
}
if ( (d->flags & DS_CONNECTED) && d->input_head && (char *)(d->input_head->cmd) ) {
memcpy(s_cutter, d->input_head->cmd, 5);
memcpy(s_cutter2, d->input_head->cmd, 7);
s_cutter[5] = '\0';
s_cutter2[7] = '\0';
if ( (Good_obj(d->player) && Wizard(d->player) && (stricmp(s_cutter, "idle ") == 0)) ||
(stricmp(s_cutter, "idle") == 0) ||
(stricmp(s_cutter2, "idle @@") == 0) ) {
cmdp = (CMDENT *) hashfind("idle", &mudstate.command_htab);
if ( cmdp && check_access(d->player, cmdp->perms, cmdp->perms2, 0)) {
if ( !(CmdCheck(d->player) && cmdtest(d->player, "idle")) ) {
d->last_time = i_oldlasttime;
d->flags = d->flags | flagkeep;
if ( d->flags & DS_AUTOUNF )
s_Flags2(d->player, Flags2(d->player) | UNFINDABLE);
if ( d->flags & DS_AUTODARK )
s_Flags(d->player, Flags(d->player) | DARK);
}
}
}
}
/* Ignore Null Input */
if ( (d->input_tot <= (i_oldlastcnt + 2)) && d->input_head && (char *)(d->input_head->cmd) &&
((*(d->input_head->cmd) == '\r') || (*(d->input_head->cmd) == '\n')) ) {
d->last_time = i_oldlasttime;
}
}
/* Process output for sockets with pending output */
check = CheckOutput(d->descriptor);
if (check) {
if (!process_output(d)) {
shutdownsock(d, R_SOCKDIED);
}
}
}
door_checkInternalDoorDescriptors(&input_set, &output_set);
}
DPOPCONDITIONAL; /* #2 */
}
const char *
addrout(struct in_addr a)
{
extern char *inet_ntoa();
char *retval;
char *logbuff;
DPUSH; /* #3 */
if (mudconf.use_hostname) {
struct hostent *he;
/* check the nodns_site list */
if( site_check(a, mudstate.special_list, 1, 0, H_NODNS) & H_NODNS ) {
retval = inet_ntoa(a);
RETURN(retval); /* #3 */
}
he = gethostbyaddr((char *) &a.s_addr, sizeof(a.s_addr), AF_INET);
if (he) {
retval = he->h_name;
if ( !mudconf.nospam_connect ) {
STARTLOG(LOG_NET, "NET", "DNS")
logbuff = alloc_lbuf("bsd.addrout");
sprintf(logbuff, "%s = %s", inet_ntoa(a), retval);
log_text(logbuff);
free_lbuf(logbuff);
ENDLOG
}
RETURN(retval); /* #3 */
}
else {
retval = inet_ntoa(a);
RETURN(retval); /* 3 */
}
}
retval = inet_ntoa(a);
RETURN(retval); /* 3 */
}
#ifdef TLI
static void
log_tli_error(const char *key, const char *routine,
int err, int t_err)
{
char *buff;
DPUSH; /* #4 */
STARTLOG(LOG_NET, "NET", "BIND")
buff = alloc_mbuf("log_tli_error");
sprintf(buff, "%s failed, errno=%d(%s), t_errno=%d(%s)",
routine, err, sys_errlist[err],
t_err, t_errlist[t_err]);
log_text(buff);
free_mbuf(buff);
ENDLOG
DPOP; /* #4 */
}
#endif
#ifdef TLI
struct t_call *nc_call = (struct t_call *) NULL;
#endif
DESC *
new_connection(int sock)
{
int newsock, maxsitecon, maxtsitecon, cur_port, i_retvar = -1;
char *buff, *buff1, *cmdsave, tchbuff[LBUF_SIZE], *tsite_buff;
DESC *d, *dchk, *dchknext;
struct sockaddr_in addr;
static int spam_log = 0;
char *logbuff, *addroutbuf;
int myerrno = 0, i_chktor = 0, i_chksite = -1, i_forbid = 0, i_proxychk;
#ifndef __MACH__
#ifndef CYGWIN
#ifndef BROKEN_PROXY
int i_mtu, i_mtulen, i_mss, i_msslen;
#endif
#endif
#endif
#ifndef TLI
int addr_len;
#else
int ret;
#endif
DPUSH; /* #5 */
cur_port = i_proxychk = 0;
cmdsave = mudstate.debug_cmd;
mudstate.debug_cmd = (char *) "< new_connection >";
#ifdef TLI
if (!nc_call) {
nc_call = (struct t_call *) t_alloc(sock, T_CALL, T_ALL);
if (!nc_call) {
RETURN(0); /* #5 */
}