-
Notifications
You must be signed in to change notification settings - Fork 1
/
comm.c
3347 lines (2998 loc) · 108 KB
/
comm.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
/*
* comm.c -- communications functions and more.
* Dwayne Fontenot (Jacques@TMI)
*/
#include "std.h"
#include "comm.h"
#include "main.h"
#include "socket_efuns.h"
#include "backend.h"
#include "socket_ctrl.h"
#include "debug.h"
#include "ed.h"
#include "file.h"
#include "master.h"
#include "add_action.h"
#include "eval.h"
#include "console.h"
#ifndef ENOSR
#define ENOSR 63
#endif
#ifndef ANSI_SUBSTITUTE
#define ANSI_SUBSTITUTE 0x20
#endif
#ifndef ADDRFAIL_NOTIFY
#define ADDRFAIL_NOTIFY 0
#endif
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#define TELOPT_MSSP 70
#define TELOPT_COMPRESS 85
#define TELOPT_COMPRESS2 86
#define TELOPT_MXP 91 // mud extension protocol
#define TELOPT_ZMP 93 // zenith mud protocol
#define TELOPT_GMCP 201 // something mud communication protocol, how many do we need?
#define MSSP_VAR 1
#define MSSP_VAL 2
#ifndef MAX
#define MAX(x,y) (((x)>(y))?(x):(y))
#endif
#ifndef ENV_FILLER
#define ENV_FILLER 0x1e
#endif
#define TELOPT_NEW_ENVIRON 39
#define NEW_ENV_IS 0
#define NEW_ENV_SEND 1
#define NEW_ENV_INFO 2
#define NEW_ENV_VAR 0
#define NEW_ENV_VALUE 1
#define NEW_ENV_ESC 2
#define NEW_ENV_USERVAR 3
static unsigned char telnet_break_response[] = { 28, IAC, WILL, TELOPT_TM };
static unsigned char telnet_ip_response[] = { 127, IAC, WILL, TELOPT_TM };
static unsigned char telnet_abort_response[] = { IAC, DM };
static unsigned char telnet_do_tm_response[] = { IAC, WILL, TELOPT_TM };
static unsigned char telnet_do_naws[] = { IAC, DO, TELOPT_NAWS };
static unsigned char telnet_dont_naws[] = { IAC, DONT, TELOPT_NAWS };
static unsigned char telnet_do_ttype[] = { IAC, DO, TELOPT_TTYPE };
static unsigned char telnet_term_query[] = { IAC, SB, TELOPT_TTYPE, TELQUAL_SEND, IAC, SE };
static unsigned char telnet_no_echo[] = { IAC, WONT, TELOPT_ECHO };
static unsigned char telnet_no_single[] = { IAC, WONT, TELOPT_SGA };
static unsigned char telnet_yes_echo[] = { IAC, WILL, TELOPT_ECHO };
static unsigned char telnet_yes_single[] = { IAC, WILL, TELOPT_SGA };
static unsigned char telnet_ga[] = { IAC, GA };
static unsigned char telnet_ayt_response[] = { '\n', '[', '-', 'Y', 'e', 's', '-', ']', ' ', '\n' };
static unsigned char telnet_line_mode[] = { IAC, DO, TELOPT_LINEMODE };
static unsigned char telnet_lm_mode[] = { IAC, SB, TELOPT_LINEMODE, LM_MODE, MODE_EDIT | MODE_TRAPSIG, IAC, SE };
static unsigned char telnet_char_mode[] = { IAC, DONT, TELOPT_LINEMODE };
static unsigned char slc_default_flags[] = { SLC_NOSUPPORT, SLC_CANTCHANGE, SLC_CANTCHANGE, SLC_CANTCHANGE, SLC_CANTCHANGE, SLC_NOSUPPORT,
SLC_NOSUPPORT, SLC_NOSUPPORT, SLC_CANTCHANGE, SLC_CANTCHANGE, SLC_NOSUPPORT, SLC_NOSUPPORT,
SLC_NOSUPPORT, SLC_NOSUPPORT, SLC_NOSUPPORT, SLC_NOSUPPORT, SLC_NOSUPPORT, SLC_NOSUPPORT };
static unsigned char slc_default_chars[] = { 0x00, BREAK, IP, AO, AYT, 0x00, 0x00, 0x00,
SUSP, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00 };
#ifdef HAVE_ZLIB
static unsigned char telnet_compress_send_request_v2[] = { IAC, WILL,
TELOPT_COMPRESS2 };
static unsigned char telnet_compress_send_request_v1[] = { IAC, WILL,
TELOPT_COMPRESS };
static unsigned char telnet_compress_v1_response[] = { IAC, SB,
TELOPT_COMPRESS, WILL,
SE };
static unsigned char telnet_compress_v2_response[] = { IAC, SB,
TELOPT_COMPRESS2, IAC,
SE };
#endif
static unsigned char telnet_do_mxp[] = { IAC, DO, TELOPT_MXP };
static unsigned char telnet_will_mxp[] = { IAC, SB, TELOPT_MXP, IAC, SE };
static unsigned char telnet_will_mssp[] = { IAC, WILL, TELOPT_MSSP };
static unsigned char telnet_start_mssp[] = { IAC, SB, TELOPT_MSSP };
static unsigned char telnet_mssp_value[] = {MSSP_VAR, '%', 's', MSSP_VAL, '%', 's', 0};
static unsigned char telnet_end_sub[] = {IAC, SE};
static unsigned char telnet_will_zmp[] = { IAC, WILL, TELOPT_ZMP};
static unsigned char telnet_start_zmp[] = { IAC, SB, TELOPT_ZMP};
static unsigned char telnet_do_newenv[] = { IAC, DO, TELOPT_NEW_ENVIRON };
static unsigned char telnet_send_uservar[] = { IAC, SB, TELOPT_NEW_ENVIRON, NEW_ENV_SEND, IAC, SE };
static unsigned char telnet_do_gmcp[] = {IAC, DO, TELOPT_GMCP};
static unsigned char telnet_start_gmcp[] = {IAC, SB, TELOPT_GMCP};
/*
* local function prototypes.
*/
#ifdef SIGNAL_FUNC_TAKES_INT
static void sigpipe_handler (int);
#else
static void sigpipe_handler (void);
#endif
static void hname_handler (void);
static void get_user_data (interactive_t *);
static char *get_user_command (void);
static char *first_cmd_in_buf (interactive_t *);
static int cmd_in_buf (interactive_t *);
static int call_function_interactive (interactive_t *, char *);
static void print_prompt (interactive_t *);
static void query_addr_name (object_t *);
static void got_addr_number (char *, char *);
#ifdef IPV6
static void add_ip_entry (struct in6_addr, char *);
#else
static void add_ip_entry (long, char *);
#endif
static void new_user_handler (int);
static void end_compression (interactive_t *);
static void start_compression (interactive_t *);
static int send_compressed (interactive_t *ip, unsigned char* data, int length);
#ifdef NO_SNOOP
# define handle_snoop(str, len, who)
#else
# define handle_snoop(str, len, who) if ((who)->snooped_by) receive_snoop(str, len, who->snooped_by)
static void receive_snoop (const char *, int, object_t * ob);
#endif
/*
* public local variables.
*/
fd_set readmask, writemask;
int num_user;
#ifdef F_SET_HIDE
int num_hidden_users = 0; /* for the O_HIDDEN flag. This counter must
* be kept up to date at all times! If you
* modify the O_HIDDEN flag in an object,
* make sure that you update this counter if
* the object is interactive. */
#endif
int add_message_calls = 0;
#ifdef F_NETWORK_STATS
int inet_out_packets = 0;
int inet_out_volume = 0;
int inet_in_packets = 0;
int inet_in_volume = 0;
#ifdef PACKAGE_SOCKETS
int inet_socket_in_packets = 0;
int inet_socket_in_volume = 0;
int inet_socket_out_packets = 0;
int inet_socket_out_volume = 0;
#endif
#endif
int inet_packets = 0;
int inet_volume = 0;
interactive_t **all_users = 0;
int max_users = 0;
#ifdef HAS_CONSOLE
int has_console = -1;
#endif
/*
* private local variables.
*/
static int addr_server_fd = -1;
static
void set_linemode (interactive_t * ip)
{
if (ip->iflags & USING_LINEMODE) {
add_binary_message(ip->ob, telnet_line_mode, sizeof(telnet_line_mode));
add_binary_message(ip->ob, telnet_lm_mode, sizeof(telnet_lm_mode));
} else {
add_binary_message(ip->ob, telnet_no_single, sizeof(telnet_no_single));
}
}
static
void set_charmode (interactive_t * ip)
{
if (ip->iflags & USING_LINEMODE) {
add_binary_message(ip->ob, telnet_char_mode, sizeof(telnet_char_mode));
} else {
add_binary_message(ip->ob, telnet_yes_single, sizeof(telnet_yes_single));
}
}
#ifndef NO_SNOOP
static void
receive_snoop (const char * buf, int len, object_t * snooper)
{
/* command giver no longer set to snooper */
#ifdef RECEIVE_SNOOP
char *str;
str = new_string(len, "receive_snoop");
memcpy(str, buf, len);
str[len] = 0;
push_malloced_string(str);
apply(APPLY_RECEIVE_SNOOP, snooper, 1, ORIGIN_DRIVER);
#else
/* snoop output is now % in all cases */
add_message(snooper, "%", 1);
add_message(snooper, buf, len);
#endif
}
#endif
/*
* Initialize new user connection socket.
*/
void init_user_conn()
{
#ifdef IPV6
struct sockaddr_in6 sin;
#else
struct sockaddr_in sin;
#endif
memset(&sin, 0, sizeof(sin));
socklen_t sin_len;
int optval;
int i;
int have_fd6;
int fd6_which = -1;
/* Check for fd #6 open as a valid socket */
optval = 1;
have_fd6 = (setsockopt(6, SOL_SOCKET, SO_REUSEADDR, (char *)&optval, sizeof(optval)) == 0);
for (i=0; i < 5; i++) {
#ifdef F_NETWORK_STATS
external_port[i].in_packets = 0;
external_port[i].in_volume = 0;
external_port[i].out_packets = 0;
external_port[i].out_volume = 0;
#endif
if (!external_port[i].port) {
#if defined(FD6_KIND) && defined(FD6_PORT)
if (!have_fd6) continue;
fd6_which = i;
have_fd6 = 0;
if (FD6_KIND == PORT_UNDEFINED || FD6_PORT < 1) {
debug_message("Socket passed to fd 6 ignored (support is disabled).\n");
continue;
}
debug_message("Accepting connections on fd 6 (port %d).\n", FD6_PORT);
external_port[i].kind = FD6_KIND;
external_port[i].port = FD6_PORT;
external_port[i].fd = 6;
#else
continue;
#endif
} else {
/*
* create socket of proper type.
*/
int sockflags = SOCK_STREAM;
#ifdef IPV6
if ((external_port[i].fd = socket(PF_INET6, sockflags, 0)) == -1) {
#else
if ((external_port[i].fd = socket(PF_INET, sockflags, 0)) == -1) {
#endif
debug_perror("init_user_conn: socket", 0);
exit(1);
}
/*
* enable local address reuse.
*/
optval = 1;
if (setsockopt(external_port[i].fd, SOL_SOCKET, SO_REUSEADDR,
(char *) &optval, sizeof(optval)) == -1) {
socket_perror("init_user_conn: setsockopt", 0);
exit(2);
}
#ifdef FD_CLOEXEC
fcntl(external_port[i].fd, F_SETFD, FD_CLOEXEC);
#endif
/*
* fill in socket address information.
*/
#ifdef IPV6
sin.sin6_family = AF_INET6;
if(MUD_IP[0])
inet_pton(AF_INET6, MUD_IP, &(sin.sin6_addr));
else
sin.sin6_addr = in6addr_any;
sin.sin6_port = htons((u_short) external_port[i].port);
#else
sin.sin_family = AF_INET;
if (MUD_IP[0])
sin.sin_addr.s_addr = inet_addr(MUD_IP);
else
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons((u_short) external_port[i].port);
#endif
/*
* bind name to socket.
*/
if (bind(external_port[i].fd, (struct sockaddr *) & sin,
sizeof(sin)) == -1) {
socket_perror("init_user_conn: bind", 0);
exit(3);
}
}
/*
* get socket name.
*/
sin_len = sizeof(sin);
if (getsockname(external_port[i].fd, (struct sockaddr *) & sin,
&sin_len) == -1) {
socket_perror("init_user_conn: getsockname", 0);
if (i != fd6_which) {
exit(4);
}
}
/*
* set socket non-blocking,
*/
if (set_socket_nonblocking(external_port[i].fd, 1) == -1) {
socket_perror("init_user_conn: set_socket_nonblocking 1", 0);
if (i != fd6_which) {
exit(8);
}
}
/*
* listen on socket for connections.
*/
if (listen(external_port[i].fd, 128) == -1) {
socket_perror("init_user_conn: listen", 0);
if (i != fd6_which) {
exit(10);
}
}
}
if (have_fd6) {
debug_message("No more ports available; fd #6 ignored.\n");
}
/*
* register signal handler for SIGPIPE.
*/
#if defined(SIGPIPE) && defined(SIGNAL_ERROR)
#ifdef SIG_IGN
if (signal(SIGPIPE, SIG_IGN) == SIGNAL_ERROR) {
debug_perror("init_user_conn: signal SIGPIPE",0);
exit(5);
}
#else
if (signal(SIGPIPE, sigpipe_handler) == SIGNAL_ERROR) {
debug_perror("init_user_conn: signal SIGPIPE",0);
exit(5);
}
#endif
#endif
}
/*
* Shut down new user accept file descriptor.
*/
void ipc_remove()
{
int i;
for (i = 0; i < 5; i++) {
if (!external_port[i].port) continue;
if (OS_socket_close(external_port[i].fd) == -1) {
socket_perror("ipc_remove: close", 0);
}
}
debug_message("closed external ports\n");
}
void init_addr_server (char * hostname, int addr_server_port)
{
#ifdef WIN32
WORD wVersionRequested = MAKEWORD(1,1);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
#endif
#ifdef IPV6
struct sockaddr_in6 server;
#else
struct sockaddr_in server;
#endif
#ifndef IPV6
struct hostent *hp;
int addr;
#endif
int server_fd;
int optval;
if (addr_server_fd >= 0)
return;
if (!hostname) return;
#ifdef IPV6
/*
* get network host data for hostname.
*/
struct addrinfo hints, *res;
hints.ai_family = AF_INET6;
hints.ai_socktype = 0;
hints.ai_protocol = 0;
#ifndef AI_V4MAPPED
hints.ai_flags = AI_CANONNAME;
#else
hints.ai_flags = AI_CANONNAME| AI_V4MAPPED;
#endif
if(getaddrinfo(hostname, "1234", &hints, &res)){
//failed
socket_perror("init_addr_server: getaddrinfo", 0);
return;
}
memcpy(&server, res->ai_addr, sizeof(server));
freeaddrinfo(res);
server.sin6_port = htons((u_short) addr_server_port);
//inet_pton(AF_INET6, hostname, &(server.sin6_addr));
//memcpy((char *) &server.sin6_addr, (char *) hp->h_addr, hp->h_length);
/*
* create socket of proper type.
*/
server_fd = socket(AF_INET6, SOCK_STREAM, 0);
#else
/*
* get network host data for hostname.
*/
if (hostname[0] >= '0' && hostname[0] <= '9' &&
(addr = inet_addr(hostname)) != -1) {
hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
} else {
hp = gethostbyname(hostname);
}
if (hp == NULL) {
socket_perror("init_addr_server: gethostbyname", 0);
return;
}
/*
* set up address information for server.
*/
server.sin_family = AF_INET;
server.sin_port = htons((u_short) addr_server_port);
server.sin_addr.s_addr = inet_addr(hostname);
memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
/*
* create socket of proper type.
*/
server_fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (server_fd == INVALID_SOCKET) { /* problem opening socket */
socket_perror("init_addr_server: socket", 0);
return;
}
/*
* enable local address reuse.
*/
optval = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &optval,
sizeof(optval)) == -1) {
socket_perror("init_addr_server: setsockopt", 0);
return;
}
/*
* connect socket to server address.
*/
if (connect(server_fd, (struct sockaddr *) & server, sizeof(server)) == -1) {
if(ADDRFAIL_NOTIFY){
if (socket_errno == ECONNREFUSED && ADDRFAIL_NOTIFY)
debug_message("Connection to address server (%s %d) refused.\n",
hostname, addr_server_port);
else
socket_perror("init_addr_server: connect", 0);
}
OS_socket_close(server_fd);
return;
}
addr_server_fd = server_fd;
debug_message("Connected to address server on %s port %d\n", hostname,
addr_server_port);
/*
* set socket non-blocking.
*/
if (set_socket_nonblocking(server_fd, 1) == -1) {
socket_perror("init_addr_server: set_socket_nonblocking 1", 0);
return;
}
#ifdef WIN32
WSACleanup();
#endif
}
/*
* If there is a shadow for this object, then the message should be
* sent to it. But only if catch_tell() is defined. Beware that one of the
* shadows may be the originator of the message, which means that we must
* not send the message to that shadow, or any shadows in the linked list
* before that shadow.
*
* Also note that we don't need to do this in the case of
* INTERACTIVE_CATCH_TELL, since catch_tell() was already called
* _instead of_ add_message(), and shadows got their chance then.
*/
#if !defined(INTERACTIVE_CATCH_TELL) && !defined(NO_SHADOWS)
#define SHADOW_CATCH_MESSAGE
#endif
#ifdef SHADOW_CATCH_MESSAGE
static int shadow_catch_message (object_t * ob, const char * str)
{
if (!ob->shadowed)
return 0;
while (ob->shadowed != 0 && ob->shadowed != current_object)
ob = ob->shadowed;
while (ob->shadowing) {
copy_and_push_string(str);
if (apply(APPLY_CATCH_TELL, ob, 1, ORIGIN_DRIVER))
/* this will work, since we know the */
/* function is defined */
return 1;
ob = ob->shadowing;
}
return 0;
}
#endif
/*
* Send a message to an interactive object. If that object is shadowed,
* special handling is done.
*/
void add_message (object_t * who, const char * data, int len)
{
interactive_t *ip;
const char *cp;
const char *end;
char *trans;
int translen;
/*
* if who->interactive is not valid, write message on stderr.
* (maybe)
*/
if (!who || (who->flags & O_DESTRUCTED) || !who->interactive ||
(who->interactive->iflags & (NET_DEAD | CLOSING))) {
#ifdef NONINTERACTIVE_STDERR_WRITE
putc(']', stderr);
fwrite(data, len, 1, stderr);
#endif
return;
}
ip = who->interactive;
trans = translate(ip->trans->outgoing, data, len, &translen);
#ifdef SHADOW_CATCH_MESSAGE
/*
* shadow handling.
*/
if (shadow_catch_message(who, data)) {
#ifdef SNOOP_SHADOWED
handle_snoop(data, len, ip);
#endif
return;
}
#endif /* NO_SHADOWS */
/*
* write message into ip->message_buf.
*/
end = trans + translen;
for (cp = trans; cp < end; cp++) {
if (ip->message_length == MESSAGE_BUF_SIZE) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message."));
return;
}
if (ip->message_length == MESSAGE_BUF_SIZE)
break;
}
if ((*cp == '\n' || *cp == -1)
#ifndef NO_BUFFER_TYPE
&& ip->connection_type != PORT_BINARY
#endif
) {
if (ip->message_length == (MESSAGE_BUF_SIZE - 1)) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message."));
return;
}
if (ip->message_length == (MESSAGE_BUF_SIZE - 1))
break;
}
ip->message_buf[ip->message_producer] = (*cp == '\n')?'\r':-1;
ip->message_producer = (ip->message_producer + 1)
% MESSAGE_BUF_SIZE;
ip->message_length++;
}
ip->message_buf[ip->message_producer] = *cp;
ip->message_producer = (ip->message_producer + 1) % MESSAGE_BUF_SIZE;
ip->message_length++;
}
handle_snoop(data, len, ip);
#ifdef FLUSH_OUTPUT_IMMEDIATELY
flush_message(ip);
#endif
add_message_calls++;
} /* add_message() */
/* WARNING: this can only handle results < LARGEST_PRINTABLE_STRING in size */
void add_vmessage (object_t *who, const char *format, ...)
{
int len;
interactive_t *ip;
char *cp, new_string_data[LARGEST_PRINTABLE_STRING + 1];
va_list args;
V_START(args, format);
V_VAR(object_t *, who, args);
V_VAR(char *, format, args);
/*
* if who->interactive is not valid, write message on stderr.
* (maybe)
*/
if (!who || (who->flags & O_DESTRUCTED) || !who->interactive ||
(who->interactive->iflags & (NET_DEAD | CLOSING))) {
#ifdef NONINTERACTIVE_STDERR_WRITE
putc(']', stderr);
vfprintf(stderr, format, args);
#endif
va_end(args);
return;
}
ip = who->interactive;
new_string_data[0] = '\0';
vsnprintf(new_string_data, LARGEST_PRINTABLE_STRING, format, args);
va_end(args);
len = strlen(new_string_data);
#ifdef SHADOW_CATCH_MESSAGE
/*
* shadow handling.
*/
if (shadow_catch_message(who, new_string_data)) {
#ifdef SNOOP_SHADOWED
handle_snoop(new_string_data, len, ip);
#endif
return;
}
#endif /* NO_SHADOWS */
/*
* write message into ip->message_buf.
*/
for (cp = new_string_data; *cp != '\0'; cp++) {
if (ip->message_length == MESSAGE_BUF_SIZE) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message."));
return;
}
if (ip->message_length == MESSAGE_BUF_SIZE)
break;
}
if (*cp == '\n') {
if (ip->message_length == (MESSAGE_BUF_SIZE - 1)) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message.\n"));
return;
}
if (ip->message_length == (MESSAGE_BUF_SIZE - 1))
break;
}
ip->message_buf[ip->message_producer] = '\r';
ip->message_producer = (ip->message_producer + 1)
% MESSAGE_BUF_SIZE;
ip->message_length++;
}
ip->message_buf[ip->message_producer] = *cp;
ip->message_producer = (ip->message_producer + 1) % MESSAGE_BUF_SIZE;
ip->message_length++;
}
if (ip->message_length != 0) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message.\n"));
return;
}
}
handle_snoop(new_string_data, len, ip);
#ifdef FLUSH_OUTPUT_IMMEDIATELY
flush_message(ip);
#endif
add_message_calls++;
} /* add_message() */
void add_binary_message (object_t * who, const unsigned char * data, int len)
{
interactive_t *ip;
const unsigned char *cp, *end;
/*
* if who->interactive is not valid, bail
*/
if (!who || (who->flags & O_DESTRUCTED) || !who->interactive ||
(who->interactive->iflags & (NET_DEAD | CLOSING))) {
return;
}
ip = who->interactive;
/*
* write message into ip->message_buf.
*/
end = data + len;
for (cp = data; cp < end; cp++) {
if (ip->message_length == MESSAGE_BUF_SIZE) {
if (!flush_message(ip)) {
debug(connections, ("Broken connection during add_message."));
return;
}
if (ip->message_length == MESSAGE_BUF_SIZE)
break;
}
ip->message_buf[ip->message_producer] = *cp;
ip->message_producer = (ip->message_producer + 1) % MESSAGE_BUF_SIZE;
ip->message_length++;
}
flush_message(ip);
add_message_calls++;
}
/*
* Flush outgoing message buffer of current interactive object.
*/
int flush_message (interactive_t * ip)
{
int length, num_bytes;
/*
* if ip is not valid, do nothing.
*/
if (!ip || !ip->ob || !IP_VALID(ip, ip->ob) ||
(ip->ob->flags & O_DESTRUCTED) || (ip->iflags & (NET_DEAD | CLOSING))){
//debug(connections, ("flush_message: invalid target!\n"));
return 0;
}
/*
* write ip->message_buf[] to socket.
*/
while (ip->message_length != 0) {
if (ip->message_consumer < ip->message_producer) {
length = ip->message_producer - ip->message_consumer;
} else {
length = MESSAGE_BUF_SIZE - ip->message_consumer;
}
/* Need to use send to get out of band data
num_bytes = write(ip->fd,ip->message_buf + ip->message_consumer,length);
*/
#ifdef HAVE_ZLIB
if (ip->compressed_stream) {
num_bytes = send_compressed(ip, (unsigned char *)ip->message_buf +
ip->message_consumer, length);
} else {
#endif
num_bytes = send(ip->fd, ip->message_buf + ip->message_consumer,
length, ip->out_of_band | MSG_NOSIGNAL);
#ifdef HAVE_ZLIB
}
#endif
if (!num_bytes) {
ip->iflags |= NET_DEAD;
return 0;
}
if (num_bytes == -1) {
#ifdef EWOULDBLOCK
if (socket_errno == EWOULDBLOCK) {
//debug(connections, ("flush_message: write: Operation would block\n"));
return 1;
#else
if (0) {
;
#endif
} else if (socket_errno == EINTR) {
//debug(connections, ("flush_message: write: Interrupted system call"));
return 1;
} else {
//socket_perror("flush_message: write", 0);
ip->iflags |= NET_DEAD;
return 0;
}
}
ip->message_consumer = (ip->message_consumer + num_bytes) %
MESSAGE_BUF_SIZE;
ip->message_length -= num_bytes;
ip->out_of_band = 0;
inet_packets++;
inet_volume += num_bytes;
#ifdef F_NETWORK_STATS
inet_out_packets++;
inet_out_volume += num_bytes;
external_port[ip->external_port].out_packets++;
external_port[ip->external_port].out_volume += num_bytes;
#endif
}
return 1;
} /* flush_message() */
static int send_mssp_val(mapping_t *map, mapping_node_t *el, void *obp){
object_t *ob = (object_t *)obp;
if(el->values[0].type == T_STRING && el->values[1].type == T_STRING){
char buf[1024];
int len = sprintf(buf, (char *)telnet_mssp_value, el->values[0].u.string, el->values[1].u.string);
add_binary_message(ob, (unsigned char *)buf, len);
} else if (el->values[0].type == T_STRING && el->values[1].type == T_ARRAY && el->values[1].u.arr->size > 0 && el->values[1].u.arr->item[0].type == T_STRING){
char buf[10240];
int len = sprintf(buf, (char *)telnet_mssp_value, el->values[0].u.string, el->values[1].u.arr->item[0].u.string);
add_binary_message(ob, (unsigned char *)buf, len);
array_t *ar = el->values[1].u.arr;
int i;
unsigned char val = MSSP_VAL;
for(i=1; i < ar->size; i++){
if(ar->item[i].type == T_STRING){
add_binary_message(ob, &val, 1);
add_binary_message(ob, (const unsigned char *)ar->item[i].u.string, strlen(ar->item[i].u.string));
}
}
}
return 0;
}
static void copy_chars (interactive_t * ip, char * from, int num_bytes)
{
int i, start, x;
unsigned char dont_response[3] = { IAC, DONT, 0 };
unsigned char wont_response[3] = { IAC, WONT, 0 };
start = ip->text_end;
for (i = 0; i < num_bytes; i++) {
switch (ip->state) {
case TS_DATA:
switch ((unsigned char)from[i]) {
case IAC:
ip->state = TS_IAC;
break;
#if defined(NO_ANSI) && defined(STRIP_BEFORE_PROCESS_INPUT)
case 0x1b:
ip->text[ip->text_end++] = ANSI_SUBSTITUTE;
break;
#endif
case 0x08:
case 0x7f:
if (ip->iflags & SINGLE_CHAR)
ip->text[ip->text_end++] = from[i];
else {
if (ip->text_end > 0)
ip->text_end--;
}
break;
default:
ip->text[ip->text_end++] = from[i];
break;
}
break;
case TS_IAC:
switch ((unsigned char)from[i]) {
case IAC:
ip->state = TS_DATA;
ip->text[ip->text_end++] = from[i];
break;
case WILL:
ip->state = TS_WILL;
break;
case WONT:
ip->state = TS_WONT;
break;
case DO:
ip->state = TS_DO;
break;
case DONT:
ip->state = TS_DONT;
break;
case SB:
ip->state = TS_SB;
ip->sb_pos = 0;
break;
case BREAK:
add_binary_message(ip->ob, telnet_break_response, sizeof(telnet_break_response));
break;
case IP: /* interrupt process */
add_binary_message(ip->ob, telnet_ip_response, sizeof(telnet_ip_response));
break;
case AYT: /* are you there? you bet */
add_binary_message(ip->ob, telnet_ayt_response, sizeof(telnet_ayt_response));
break;
case AO: /* abort output */
flush_message(ip);
ip->out_of_band = MSG_OOB;
add_binary_message(ip->ob, telnet_abort_response, sizeof(telnet_abort_response));
break;
default:
ip->state = TS_DATA;
break;
}
break;
case TS_WILL:
ip->iflags |= USING_TELNET;
switch ((unsigned char)from[i]) {
case TELOPT_TTYPE:
add_binary_message(ip->ob, telnet_term_query, sizeof(telnet_term_query));
break;
case TELOPT_LINEMODE:
/* Do linemode and set the mode: EDIT + TRAPSIG */
ip->iflags |= USING_LINEMODE;
set_linemode(ip);
break;
case TELOPT_ECHO:
case TELOPT_NAWS:
/* do nothing, but don't send a dont response */
break;
case TELOPT_MXP :
/* Mxp is enabled, tell the mudlib about it. */
apply(APPLY_MXP_ENABLE, ip->ob, 0, ORIGIN_DRIVER);
ip->iflags |= USING_MXP;
break;
case TELOPT_GMCP:
apply(APPLY_GMCP_ENABLE, ip->ob, 0, ORIGIN_DRIVER);
ip->iflags |= USING_GMCP;
break;
case TELOPT_NEW_ENVIRON :
add_binary_message(ip->ob, telnet_send_uservar, sizeof(telnet_send_uservar));
break;
default:
dont_response[2] = from[i];
add_binary_message(ip->ob, dont_response, sizeof(dont_response));
break;
}
ip->state = TS_DATA;
break;
case TS_WONT:
ip->iflags |= USING_TELNET;
switch ((unsigned char)from[i]) {
case TELOPT_LINEMODE:
/* If we're in single char mode, we just requested for
* linemode to be disabled, so don't remove our flag.
*/
if (!(ip->iflags & SINGLE_CHAR))
ip->iflags &= ~USING_LINEMODE;
break;
}
ip->state = TS_DATA;