-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomm1.c
More file actions
1626 lines (1488 loc) · 38.9 KB
/
Copy pathcomm1.c
File metadata and controls
1626 lines (1488 loc) · 38.9 KB
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
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#define TELOPTS
#include <arpa/telnet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <memory.h>
#ifndef sun
#include <fcntl.h>
#endif
#ifdef _AIX
#include <sys/select.h>
#endif
#include "config.h"
#include "lint.h"
#include "interpret.h"
#include "comm.h"
#include "object.h"
#include "sent.h"
#include "patchlevel.h"
#include "wiz_list.h"
#ifdef MUDWHO
#include "mudwho.h"
#endif
#ifdef MSDOS
#include "msdos/pcomm.h"
#endif
int socket PROT((int, int, int));
#ifndef sgi
#ifdef NeXT
int setsockopt PROT((int, int, int, void *, int));
#else
int setsockopt PROT((int, int, int, char *, int));
#endif
int bind PROT((int, struct sockaddr *, int));
int listen PROT((int, int));
int accept PROT((int, struct sockaddr *, int *));
#endif /* sgi */
int select PROT((int, fd_set *, fd_set *, fd_set *, struct timeval *));
void bzero PROT((char *, int));
void telnet_neg PROT((char *, char *));
void set_prompt PROT((char *));
char *query_ip_number PROT((struct object *));
static void add_ip_entry PROT((long, char *));
extern char *xalloc(), *string_copy(), *unshared_str_copy();
extern int d_flag;
extern int current_time;
char * first_cmd_in_buf PROT((struct interactive *));
void next_cmd_in_buf PROT((struct interactive *));
char * skip_eols PROT((struct interactive *, char *));
void remove_flush_entry PROT((struct interactive *ip));
void remove_interactive(), add_ref();
extern void debug_message(), fatal(), free_sentence();
struct interactive *all_players[MAX_PLAYERS];
extern int errno;
void new_player();
void flush_all_player_mess();
fd_set readfds;
int nfds = 0;
int num_player;
FILE *f_ip_demon = NULL, *f_ip_demon_wr;
#ifdef ACCESS_RESTRICTED
extern void *allow_host_access ();
extern void release_host_access ();
#endif
static struct object *first_player_for_flush=(struct object *)NULL;
/*
* Interprocess communication interface to the backend.
*/
static int s;
extern int port_number;
#ifdef COMM_STAT
int add_message_calls=0;
int inet_packets=0;
int inet_volume=0;
#endif
void prepare_ipc() {
#ifndef MSDOS
struct sockaddr_in sin;
struct hostent *hp;
int tmp;
char host_name[100];
#ifdef MUDWHO
char buff[100];
#endif
if (gethostname(host_name, sizeof host_name) == -1) {
perror("gethostname");
fatal("Error in gethostname()\n");
}
hp = gethostbyname(host_name);
if (hp == 0) {
(void)fprintf(stderr, "gethostbyname: unknown host.\n");
exit(1);
}
memset((char *)&sin, '\0', sizeof sin);
memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length);
sin.sin_port = htons((u_short)port_number);
sin.sin_family = hp->h_addrtype;
sin.sin_addr.s_addr = INADDR_ANY;
s = socket(hp->h_addrtype, SOCK_STREAM, 0);
if (s == -1) {
perror("socket");
abort();
}
tmp = 1;
if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR,
(char *) &tmp, sizeof (tmp)) < 0) {
perror ("setsockopt");
exit (1);
}
if (bind(s, (struct sockaddr *)&sin, sizeof sin) == -1) {
if (errno == EADDRINUSE) {
fprintf(stderr, "Socket already bound!\n");
debug_message("Socket already bound!\n");
exit(errno);
} else {
perror("bind");
abort();
}
}
if (listen(s, 5) == -1) {
perror("listen");
abort();
}
tmp = 1;
#if defined(sun) || defined(M_UNIX) || defined(NeXT)
if (ioctl(s, FIONBIO, &tmp) == -1) {
perror("ioctl socket FIONBIO");
abort();
}
#else /* sun */
if (fcntl(s, F_SETFL, FNDELAY) == -1) {
perror("ioctl socket FIONBIO");
abort();
}
#endif /* sun */
signal(SIGPIPE, SIG_IGN);
#ifdef MUDWHO
sprintf(buff, "3.00.%02d", PATCH_LEVEL);
rwhocli_setup(MUDWHO_SERVER,MUDWHO_PASSWORD,MUDWHO_NAME,buff);
#endif
#else
c_listen();
#endif
}
/*
* This one is called when shutting down the MUD.
*/
void ipc_remove() {
(void)printf("Shutting down ipc...\n");
#ifndef MSDOS
close(s);
#ifdef MUDWHO
rwhocli_shutdown();
#endif
#else
c_shutdown();
#endif
}
/*
* Send a message to a player. If that player is shadowed, special
* care hs tobe taken.
*/
/*VARARGS1*/
void add_message(fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *fmt;
int a1, a2, a3, a4, a5, a6, a7, a8, a9;
{
char buff[10000]; /* Kludgy! Hope this is enough ! */
char buff2[MAX_SOCKET_PACKET_SIZE+1];
struct interactive *ip;
int n, chunk, length;
int from, to;
int min_length;
int old_message_length;
if (command_giver == 0 || (command_giver->flags & O_DESTRUCTED) ||
command_giver->interactive == 0 ||
command_giver->interactive->do_close) {
putchar(']');
if ( fmt != MESSAGE_FLUSH )
printf(fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
fflush(stdout);
return;
}
ip = command_giver->interactive;
old_message_length = ip->message_length;
if ( fmt == MESSAGE_FLUSH ) {
min_length = 1;
strncpy ( buff, ip->message_buf, length=old_message_length );
buff[length] = '\0';
} else {
#ifdef COMM_STAT
add_message_calls++; /* We want to know how many packets the old
version would have sent */
#endif
min_length = DESIRED_SOCKET_PACKET_SIZE;
(void)sprintf(buff+old_message_length,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
length = old_message_length + strlen(buff+old_message_length);
/*
* Always check that your arrays are big enough ! :-)
*/
if (length > sizeof buff)
fatal("To long message!\n");
if (shadow_catch_message(command_giver, buff+old_message_length))
return;
if (ip->snoop_by) {
struct object *save = command_giver;
command_giver = ip->snoop_by->ob;
add_message("%%%s", buff+old_message_length);
command_giver = save;
}
if ( length >= min_length ) {
strncpy ( buff, ip->message_buf, old_message_length );
} else {
strcpy( ip->message_buf+old_message_length,
buff+old_message_length );
}
}
if (d_flag > 1)
debug_message("[%s(%d)]: %s", command_giver->name, length, buff);
/*
* Insert CR after all NL.
*/
to = 0;
#ifdef PORTALS
if (ip->out_portal) {
buff2[0] = ']';
to = 1;
}
#endif
for (from = 0; length-from >= min_length; to = 0 ) {
for ( ; to < (sizeof buff2)-1 && buff[from] != '\0';) {
if (buff[from] == '\n')
buff2[to++] = '\r';
#if 0
if (isprint(buff[from]) || isspace(buff[from]))
buff2[to++] = buff[from++];
else
from++;
#else
buff2[to++] = buff[from++];
#endif
}
if ( to == sizeof(buff2) ) {
to -= 2;
from--;
}
chunk = to;
/*
* We split up the message into something smaller than the max size.
*/
if ((n = write(ip->socket, buff2, chunk)) == -1) {
if (errno == EMSGSIZE) {
fprintf(stderr, "comm1: write EMSGSIZE.\n");
return;
}
if (errno == EINVAL) {
fprintf(stderr, "comm1: write EINVAL.\n");
if (old_message_length) remove_flush_entry(ip);
ip->do_close = 1;
return;
}
if (errno == ENETUNREACH) {
fprintf(stderr, "comm1: write ENETUNREACH.\n");
if (old_message_length) remove_flush_entry(ip);
ip->do_close = 1;
return;
}
if (errno == EHOSTUNREACH) {
fprintf(stderr, "comm1: write EHOSTUNREACH.\n");
if (old_message_length) remove_flush_entry(ip);
ip->do_close = 1;
return;
}
if (errno == EPIPE) {
fprintf(stderr, "comm1: write EPIPE detected\n");
if (old_message_length) remove_flush_entry(ip);
ip->do_close = 1;
return;
}
if (errno == EWOULDBLOCK) {
fprintf(stderr, "comm1: write EWOULDBLOCK. Message discarded.\n");
if (old_message_length) remove_flush_entry(ip);
/* ip->do_close = 1; -- LA */
return;
}
fprintf(stderr, "write: unknown errno %d\n", errno);
perror("write");
if (old_message_length) remove_flush_entry(ip);
ip->do_close = 1;
return;
}
#ifdef COMM_STAT
inet_packets++;
inet_volume += n;
#endif
if (n != chunk)
fprintf(stderr, "write socket: wrote %d, should be %d.\n",
n, chunk);
continue;
}
length -= from;
ip->message_length=length;
if (from)
strncpy( ip->message_buf, buff+from, length );
if ( length && !old_message_length ) { /* buffer became 'dirty' */
if ( ip->next_player_for_flush = first_player_for_flush ) {
first_player_for_flush->interactive->previous_player_for_flush =
command_giver;
}
ip->previous_player_for_flush = 0;
first_player_for_flush = command_giver;
}
if ( !length && old_message_length ) { /* buffer has become empty */
remove_flush_entry(ip);
}
}
void remove_flush_entry(ip)
struct interactive *ip;
{
ip->message_length=0;
if ( ip->previous_player_for_flush ) {
ip->previous_player_for_flush->interactive->next_player_for_flush
= ip->next_player_for_flush;
} else {
first_player_for_flush = ip->next_player_for_flush;
}
if ( ip->next_player_for_flush ) {
ip->next_player_for_flush->interactive->previous_player_for_flush
= ip->previous_player_for_flush;
}
}
void flush_all_player_mess() {
struct object *p,*np;
struct object *save = command_giver;
for ( p = first_player_for_flush; p; p=np) {
np = p->interactive->next_player_for_flush;
/* beware of side-effects when calling add_message the first time! */
command_giver = p;
add_message(MESSAGE_FLUSH);
}
command_giver=save;
}
/*
* Copy a string, replacing newlines with '\0'. Also add an extra
* space and back space for every newline. This trick will allow
* otherwise empty lines, as multiple newlines would be replaced by
* multiple zeroes only.
*/
static int copy_chars(from, to, n)
char *from, *to;
int n;
{
int i;
char *start = to;
for (i=0; i<n; i++) {
if (from[i] == '\r')
continue;
if (from[i] == '\n') {
*to++ = ' ';
*to++ = '\b';
*to++ = '\0';
continue;
}
*to++ = from[i];
}
return to - start;
}
/*
* Get a message from any player. For all players without a completed
* cmd in their input buffer, read their socket. Then, return the first
* cmd of the next player in sequence that has a complete cmd in their buffer.
* CmdsGiven is used to allow people in ED to send more cmds (if they have
* them queued up) than normal players. If we get a heartbeat, still read
* all sockets; if the next cmd giver is -1, we have already cycled and
* can go back to do the heart beat.
*/
#define StartCmdGiver (MAX_PLAYERS-1) /* the one after heartbeat */
#define IncCmdGiver NextCmdGiver = (NextCmdGiver < 0? StartCmdGiver: \
NextCmdGiver - 1)
int NextCmdGiver = StartCmdGiver;
int CmdsGiven = 0; /* -1 is used to poll heart beat. */
int twait = 0; /* wait time for select() */
extern int time_to_call_heart_beat, comm_time_to_call_heart_beat;
int get_message(buff, size)
char *buff;
int size;
{
int i, res;
struct interactive * ip = 0;
char *p;
/*
* Stay in this loop until we have a message from a player.
*/
while(1) {
int new_socket;
struct sockaddr_in addr;
int length;
struct timeval timeout;
/* First, try to get a new player... */
length = sizeof addr;
new_socket = accept(s, (struct sockaddr *)&addr, &length);
if (new_socket != -1)
new_player(new_socket, &addr, length);
else if (new_socket == -1 && errno != EWOULDBLOCK && errno != EINTR) {
perror("accept");
abort();
}
nfds = 0;
FD_ZERO(&readfds);
for (i=0; i<MAX_PLAYERS; i++) {
ip = all_players[i];
if (!ip)
continue;
if (ip->do_close) {
ip->do_close = 0;
remove_interactive(ip->ob);
continue;
}
if (!first_cmd_in_buf(ip)) {
FD_SET(ip->socket, &readfds);
if (ip->socket >= nfds)
nfds = ip->socket+1;
#ifdef PORTALS
if (ip->out_portal) {
FD_SET(ip->portal_socket, &readfds);
if (ip->portal_socket >= nfds)
nfds = ip->portal_socket + 1;
}
#endif /* PORTALS */
}
}
if (f_ip_demon != NULL) {
FD_SET(fileno(f_ip_demon), &readfds);
}
timeout.tv_sec = twait; /* avoid busy waiting when no buffered cmds */
timeout.tv_usec = 0;
res = select(nfds, &readfds, 0, 0, &timeout);
#ifdef MSDOS
if (timer_expired()) {
twait = 0;
goto return_next_command;
}
#endif
if (res == -1) {
twait = 0;
if (errno == EINTR) /* if we got an alarm, finish the round */
goto return_next_command;
perror("select");
return 0;
}
if (res) { /* waiting packets */
if (f_ip_demon != NULL && FD_ISSET(fileno(f_ip_demon), &readfds)) {
char buf[200], *pp, *q;
long laddr;
if (fgets(buf, sizeof buf, f_ip_demon)) {
/*printf("hname says: %s\n", buf);*/
laddr=inet_addr(buf);
if (laddr != -1) {
pp = strchr(buf, ' ');
if (pp) {
pp++;
q = strchr(buf, '\n');
if (q) {
*q = 0;
add_ip_entry(laddr, pp);
}
}
}
}
}
for (i=0; i<MAX_PLAYERS; i++) { /* read all pending sockets */
ip = all_players[i];
if (ip == 0)
continue;
if (FD_ISSET(ip->socket, &readfds)) { /* read this player */
int l;
/*
* Don't overfill their buffer.
* Use a very conservative estimate on how much we can
* read.
*/
l = (MAX_TEXT - ip->text_end - 1)/3;
if (l < size)
size = l;
if ((l = read(ip->socket, buff, size)) == -1) {
if (errno == ENETUNREACH) {
debug_message("Net unreachable detected.\n");
remove_interactive(ip->ob);
continue;
}
if (errno == EHOSTUNREACH) {
debug_message("Host unreachable detected.\n");
remove_interactive(ip->ob);
continue;
}
if (errno == ETIMEDOUT) {
debug_message("Connection timed out detected.\n");
remove_interactive(ip->ob);
continue;
}
if (errno == ECONNRESET) {
debug_message("Connection reset by peer detected.\n");
remove_interactive(ip->ob);
continue;
}
if (errno == EWOULDBLOCK) {
debug_message("read would block socket %d!\n",
ip->socket);
remove_interactive(ip->ob);
continue;
}
if (errno == EMSGSIZE) {
debug_message("read EMSGSIZE !\n");
continue;
}
perror("read");
debug_message("Unknown errno %d\n", errno);
remove_interactive(ip->ob);
continue;
}
#ifdef PORTALS
/*
* IF the data goes through a portal, send it,
* but don't return any data.
*/
if (ip->out_portal) {
if (ip->text_end) { /* pending text first */
write(ip->portal_socket, ip->text, ip->text_end);
ip->text_end = 0;
}
if (l)
write(ip->portal_socket, buff, l);
continue;
}
#endif /* PORTALS */
if (l == 0) {
if (ip->closing)
fatal("Tried to read from closing socket.\n");
remove_interactive(ip->ob);
return 0;
}
buff[l] = '\0';
/* replace newlines by nulls and catenate to buffer */
ip->text_end +=
copy_chars(buff, ip->text + ip->text_end, l);
#if 0
for (x=0; x<l; x++) {
if (buff[x] == '\n' || buff[x] == '\r')
buff[x] = '\0';
}
memcpy(ip->text+ip->text_end, buff, l+1);
ip->text_end += l;
#endif
/* now, text->end is just after the last char read. If last */
/* char was a nl, char *before* text_end will be null. */
ip->text[ip->text_end] = '\0';
}
}
}
/*
* we have read the sockets; now find and return a command
*/
return_next_command:
twait = 0;
ip = 0;
for (i = 0; i < MAX_PLAYERS + 1; i++) {
if (NextCmdGiver == -1) { /* we have cycled around all players */
CmdsGiven = 0; /* check heart beat */
IncCmdGiver;
if (comm_time_to_call_heart_beat) {
time_to_call_heart_beat = 1; /* twait stays 0! */
return 0;
}
}
ip = all_players[NextCmdGiver];
if (ip && (p = first_cmd_in_buf(ip))) /* wont respond to partials */
break;
CmdsGiven = 0; /* new player, no cmds issued */
IncCmdGiver;
}
if ((!ip)||!p) { /* no cmds found; loop and select (on timeout) again */
if (comm_time_to_call_heart_beat) { /* may as well do it now */
time_to_call_heart_beat = 1; /* no cmds, do heart beat */
NextCmdGiver = StartCmdGiver;/* do a complete poll next time */
CmdsGiven = 0;
return(0);
}
/* no heart beat to do and no cmds pending - avoid busy wait on select */
twait = 1;
continue; /* else await another cmd */
}
/*
* we have a player cmd - return it. If he is in ed, count his
* cmds, else only allow 1 cmd. If he has only one partially
* completed cmd left after * this, move it to the start of his
* buffer; new stuff will be appended.
*/
command_giver = ip->ob;
telnet_neg(buff, p);
next_cmd_in_buf(ip); /* move on buffer pointers */
/* if he is not in ed, dont let him issue another till the poll comes again */
if (ip->ed_buffer && CmdsGiven < ALLOWED_ED_CMDS)
CmdsGiven++;
else {
IncCmdGiver;
CmdsGiven = 0;
}
/* manage snooping - should the snooper see type ahead? Well, he doesn't here
*/
if (ip->snoop_by && !ip->noecho) {
command_giver = ip->snoop_by->ob;
add_message("%% %s\n", buff);
}
command_giver = ip->ob;
if (ip->noecho) {
/* Must not enable echo before the user input is received. */
add_message("%c%c%c", IAC, WONT, TELOPT_ECHO);
}
ip->noecho = 0;
ip->last_time = current_time;
return 1;
}
}
/*
* find the first character of the next complete cmd in a buffer, 0 if no
* completed cmd. There is a completed cmd if there is a null between
* text_start and text_end. Zero length commands are discarded (as occur
* between <cr> and <lf>). Update text_start if we have to skip leading
* nulls.
*/
char * first_cmd_in_buf(ip)
struct interactive *ip;
{
char * p, *q;
p = ip->text_start + ip->text;
while ((p < (ip->text_end + ip->text)) && !*p) /* skip null input */
p++;
ip->text_start = p - ip->text;
if (ip->text_start >= ip->text_end) {
ip->text_start = ip->text_end = 0;
ip->text[0] = '\0';
return(0);
}
while ((p < (ip->text_end + ip->text)) && *p) /* find end of cmd */
p++;
if (p < ip->text + ip->text_end) /* null terminated, was command */
return(ip->text + ip->text_start);
/* have a partial command at end of buffer; move it to start, return null */
/* if it can't move down, truncate it and return it as cmd. */
p = ip->text + ip->text_start;
q = ip->text;
while (p < (ip->text + ip->text_end))
*(q++) = *(p++);
ip->text_end -= ip->text_start;
ip->text_start = 0;
if (ip->text_end > MAX_TEXT - 2) {
ip->text[ip->text_end-2] = '\0'; /* nulls to truncate */
ip->text[ip->text_end-1] = '\0'; /* nulls to truncate */
ip->text_end--;
return(ip->text);
}
/* buffer not full and no newline - no cmd. */
return(0);
}
/*
* move pointers to next cmd, or clear buf.
*/
void next_cmd_in_buf(ip)
struct interactive *ip;
{
char * p = ip->text + ip->text_start;
while (*p && p < ip->text + ip->text_end)
p++;
/* skip past any nulls at the end */
while (!*p && p < ip->text + ip->text_end)
p++;
if (p < ip->text + ip->text_end)
ip->text_start = p - ip->text;
else {
ip->text_start = ip->text_end = 0;
ip->text[0] = '\0';
}
}
/*
* Remove an interactive player immediately.
*/
void remove_interactive(ob)
struct object *ob;
{
struct object *save = command_giver;
int i;
for (i=0; i<MAX_PLAYERS; i++) {
if (all_players[i] != ob->interactive)
continue;
if (ob->interactive->closing)
fatal("Double call to remove_interactive()\n");
ob->interactive->closing = 1;
if (ob->interactive->snoop_by) {
ob->interactive->snoop_by->snoop_on = 0;
ob->interactive->snoop_by = 0;
}
if (ob->interactive->snoop_on) {
ob->interactive->snoop_on->snoop_by = 0;
ob->interactive->snoop_on = 0;
}
command_giver = ob;
add_message("Closing down.\n");
if (ob->interactive->ed_buffer) {
extern void save_ed_buffer();
save_ed_buffer();
}
add_message(MESSAGE_FLUSH);
(void)shutdown(ob->interactive->socket, 2);
close(ob->interactive->socket);
#ifdef ACCESS_RESTRICTED
release_host_access (ob->interactive->access_class);
#endif
num_player--;
if (ob->interactive->input_to) {
free_object(ob->interactive->input_to->ob, "remove_interactive");
free_sentence(ob->interactive->input_to);
ob->interactive->input_to = 0;
}
free((char *)ob->interactive);
ob->interactive = 0;
all_players[i] = 0;
free_object(ob, "remove_interactive");
command_giver = save;
return;
}
(void)fprintf(stderr, "Could not find and remove player %s\n", ob->name);
abort();
}
#ifndef MSDOS
#ifndef ACCESS_RESTRICTED
int
allow_host_access(new_socket)
int new_socket;
{
struct sockaddr_in apa;
int len = sizeof apa;
char * ipname, *calloc(), *xalloc(), *index();
static int read_access_list = 0;
static struct access_list {
int addr_len;
char * addr, *name, *comment;
struct access_list * next;
} * access_list;
register struct access_list * ap;
if(!read_access_list) {
FILE * f = fopen("ACCESS.DENY", "r");
char buf[1024], ipn[50], hname[100], comment[1024], *p1, *p2;
struct access_list * na;
struct hostent * hent;
read_access_list = 1;
if(f) {
while(fgets(buf, sizeof buf - 1, f)) {
if(*buf != '#') {
ipn[0] = hname[0] = comment[0] = 0;
if(p1 = index(buf, ':')) *p1 = 0;
if(buf[0] && buf[0] != '\n')
strncpy(ipn, buf, sizeof ipn - 1);
if((p2 = p1) && *++p2) {
if(p1 = index(p2, ':')) *p1 = 0;
if(p2[0] && p2[0] != '\n')
strcpy(hname, p2);
if(p1 && p1[1] && p1[1] != '\n')
strcpy(comment, p1+1);
}
if(!(na = (struct access_list *)xalloc(sizeof na[0]))) {
fatal("Out of mem.\n");
}
na->addr = na->name = na->comment = 0;
na->next = 0;
if(*ipn && (!(na->addr = xalloc(strlen(ipn) + 1)) ||
!strcpy(na->addr, ipn)))
fatal("Out of mem.\n");
if(*hname && (!(na->name = xalloc(strlen(hname) + 1)) ||
!strcpy(na->name, hname)))
fatal("Out of mem.\n");
if(*comment && (!(na->comment=xalloc(strlen(comment)+1))||
!strcpy(na->comment, comment)))
fatal("Out of mem.\n");
if((!(int)*ipn)
&&
((!*hname)
|| (!(hent = gethostbyname(hname))) ||
(!(na->addr =
xalloc(hent->h_length+1)))||
!strcpy(na->addr,
inet_ntoa(*(struct in_addr *)hent->h_addr)))) {
if(na->name) free(na->name);
if(na->comment) free(na->comment);
free((char *)na);
continue;
}
if(!(na->addr_len = strlen(na->addr)))
continue;
/* printf("disabling: %s:%s:%s\n", na->addr,
na->name?na->name:"no name",
na->comment?na->comment:"no comment"); */
na->next = access_list;
access_list = na;
}
}
fclose(f);
}
}
if (!access_list)
return 0;
if(getpeername(new_socket, (struct sockaddr *)&apa, &len) == -1) {
close(new_socket);
perror("getpeername");
return -1;
}
ipname = inet_ntoa(apa.sin_addr);
for(ap = access_list; ap; ap = ap->next)
if(!strncmp(ipname, ap->addr, ap->addr_len)){
if(ap->comment) (void) write(new_socket, ap->comment,
strlen(ap->comment));
printf("Stopping: %s:%s\n", ap->addr, ap->name?ap->name:"no name");
close(new_socket);
return -1;
}
return 0;
}
#endif /* not ACCESS_RESTRICTED */
#endif
/*
* get the I'th player object from the interactive list, i starts at 0
* and can go to num_player - 1. For users(), etc.
*/
struct object * get_interactive_object(i)
int i;
{
int n;
if (i >= num_player) /* love them ASSERTS() :-) */
fatal("Get interactive (%d) with only %d players!", i, num_player);
for (n = 0; n < MAX_PLAYERS; n++)
if (all_players[n])
if (!(i--))
return(all_players[n]->ob);
fatal("Get interactive: player %d not found! (num_players = %d)",
i, num_player);
return 0; /* Just to satisfy some compiler warnings */
}
void new_player(new_socket, addr, len)
int new_socket;
struct sockaddr_in *addr;
int len;
{
int i;
char *p;
#ifndef MSDOS
#ifdef ACCESS_RESTRICTED
void *class;
if (!(class = allow_host_access (new_socket, new_socket)))
return;
#else
if(allow_host_access(new_socket))
return;
#endif
#endif
if (d_flag > 1)
debug_message("New player at socket %d.\n", new_socket);
for (i=0; i<MAX_PLAYERS; i++) {
struct object *ob;
struct svalue *ret;
extern struct object *master_ob;
if (all_players[i] != 0)
continue;
assert_master_ob_loaded();
command_giver = master_ob;
master_ob->interactive =
(struct interactive *)xalloc(sizeof (struct interactive));
master_ob->interactive->default_err_message = 0;
master_ob->flags |= O_ONCE_INTERACTIVE;
/* This initialization is not pretty. */
master_ob->interactive->ob = master_ob;
master_ob->interactive->text[0] = '\0';
master_ob->interactive->input_to = 0;
master_ob->interactive->closing = 0;
master_ob->interactive->snoop_on = 0;
master_ob->interactive->snoop_by = 0;
#ifdef PORTALS
master_ob->interactive->out_portal = 0;
master_ob->interactive->portal_socket = 0;
master_ob->interactive->from_portal = 0;
#endif /* PORTALS */
master_ob->interactive->text_end = 0;
master_ob->interactive->text_start = 0;
master_ob->interactive->do_close = 0;
master_ob->interactive->noecho = 0;
master_ob->interactive->trace_level = 0;
master_ob->interactive->trace_prefix = 0;
master_ob->interactive->last_time = current_time;
master_ob->interactive->ed_buffer = 0;
master_ob->interactive->message_length=0;
#ifdef MUDWHO
master_ob->interactive->login_time = current_time;
#endif
all_players[i] = master_ob->interactive;
all_players[i]->socket = new_socket;
set_prompt("> ");
#if 1
memcpy((char *)&all_players[i]->addr, (char *)addr, len);
#else
getpeername(new_socket, (struct sockaddr *)&all_players[i]->addr,
&len);
#endif
#ifdef ACCESS_RESTRICTED
all_players[i]->access_class = class;