forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomm.c
9124 lines (7749 loc) · 278 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
/*---------------------------------------------------------------------------
* Gamedriver Communications module.
*
*---------------------------------------------------------------------------
* Throughout the module the fact is used that valid socket numbers
* are always >= 0. Unused sockets are therefore marked with negative
* numbers.
*
* All information needed for an interactive object are stored in
* a 'interactive_t'. This struct is linked to by the shadow sentence
* of the interactive object.
*
* Sending data is performed through the function add_message().
* The function collects the data in interactive.message_buf[] until
* it is filled (or a special 'flush' message is passed), then the
* data is written en-bloc to the network.
*
* Incoming data is collected in interactive.text[]. interactive.text_end
* indexes the first free character in the buffer where new data is to
* be appended. The new data is not passed directly to the command parser,
* instead it is processed by a dfa implementing the important parts
* of the telnet protocol. The dfa analyses the data read, interprets
* any telnet commands and stores the remaining 'pure' data starting
* from the beginning of .text[].
*
* Initialized to start working in the state TS_DATA, the dfa does its
* thing until it either reaches a line end or the end of the current
* data. If it is a line end, it terminates the pure data collected so
* far with a '\0', goes into state TS_READY, and lets .tn_end index
* the next unprocessed raw data char. If it is the end of the current
* data, the dfa stays in whatever state it was and indexes the current end
* of the pure data gathered so far with interactive.command_end. Once
* a new chunk of data has been read, the dfa simply continues where it
* took off.
*
* There are some variations to this scheme, but you get the idea. It is
* possible to do all telnet negotiations on mudlib level, but it must
* be either the driver or the mudlib, not both.
*
* To understand get_message() itself fully, think of it as a coroutine
* with its own state. It does not really return to the caller (though
* that is how it is implemented), in merely yields control back to
* caller in order to process the found command or the pending heartbeat.
* TODO: Obvious possibility for implementation multi-threading.
*
* Timing is implemented this way: The driver usually stays in the input
* loop, waiting in 1 second intervals for incoming data. An alarm() is
* triggered by backend.c every 2 seconds and sets the flag variable
* comm_time_to_call_heart_beat and comm_return_to_backend. The loop checks
* the later variable every second and, if it is set, aborts its input loop
* and returns to the backend. To mark the cause of the return, the variable
* time_to_call_heart_beat is set before return when
* comm_time_to_call_heart_beat was set as well.
*
* TODO: The noecho/charmode logic, especially in combination with
* TODO:: the telnet machine is frustratingly underdocumented.
*
* The data that can not written directly to the sockets is put instead into
* an intermediate buffer, from which the backend loop will continue writing
* when possible. The buffers are stored in a linked list in the interactive_s
* structure.
*
* TODO: Fiona says: The telnet code is frustrating. It would be better if
* TODO:: the common handling of e.g. TELNET_NAWS is offered by hooks,
* TODO:: as the Anarres version of MudOS does. This would mean a rewrite.
*---------------------------------------------------------------------------
*/
#define SAVE_NOECHO /* TODO: Define to enable safe NOECHO mode */
#define SIMULATE_CHARMODE /* TODO: Even linemode clients stay in charmode */
#include "driver.h"
#include "typedefs.h"
#include "my-alloca.h"
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include <stdarg.h>
#include <stddef.h>
#include <sys/ioctl.h>
#include <poll.h>
#define TELOPTS
#include "../mudlib/sys/telnet.h"
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h> /* inet_ functions / structs */
#endif
#ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h> /* DNS HEADER struct */
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#include <resolv.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#if defined(_AIX)
# include <sys/select.h>
#endif
#ifdef HAVE_FCNTL
# include <fcntl.h>
#endif
#ifdef SOCKET_INC
# include SOCKET_INC
#endif
#include "comm.h"
#include "access_check.h"
#include "actions.h"
#include "array.h"
#include "closure.h"
#include "ed.h"
#include "exec.h"
#include "filestat.h"
#include "gcollect.h"
#include "interpret.h"
#include "main.h"
#include "mstrings.h"
#include "object.h"
#include "pkg-mccp.h"
#include "pkg-pgsql.h"
#include "pkg-python.h"
#ifdef USE_TLS
#include "pkg-tls.h"
#endif
#include "sent.h"
#include "simulate.h"
#include "stdstrings.h"
#include "svalue.h"
#include "swap.h"
#include "wiz_list.h"
#include "xalloc.h"
#include "i-current_object.h"
#include "i-eval_cost.h"
#include "../mudlib/sys/comm.h"
#include "../mudlib/sys/configuration.h"
#include "../mudlib/sys/driver_hook.h"
#include "../mudlib/sys/input_to.h"
#include "../mudlib/sys/interactive_info.h"
/* if driver is compiled for ERQ demon then include the necessary file
*/
#ifdef ERQ_DEMON
# include <sys/wait.h>
# ifdef ERQ_INCLUDE
# include ERQ_INCLUDE
# else
# include "util/erq/erq.h"
# endif
#endif
/* When no special networking code is needed, define the
* socket function to their normal Unix names.
*/
#if !defined (SOCKET_LIB) && !defined(SOCKET_INC)
# define socket_number(s) (s)
# define socket_ioctl ioctl
# ifndef hpux
# define socket_select select
# else
# define socket_select(n,r,w,x,t) select(n, (int *)r, (int *)w, (int *)x, t)
/* Silences the compiler */
# endif
# define socket_read read
# define socket_write write
# define socket_close close
#endif /* SOCKET_LIB */
#if defined(_AIX)
typedef unsigned long length_t;
#elif defined(__INTEL_COMPILER) || defined (__GNUC__)
typedef socklen_t length_t;
#else
typedef int length_t;
#endif
#if defined(CYGWIN)
extern int socketpair(int, int, int, int[2]);
#endif
#ifndef EPROTO
# define EPROTO EINTR
#endif
#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 64
#endif
#ifndef INET_ADDRSTRLEN
# define INET_ADDRSTRLEN 16
#endif
/* Amazing how complicated networking can be, hm? */
/*-------------------------------------------------------------------------*/
/* Types */
/* --- struct input_to_s: input_to() datastructure
*
* input-to structures describe a pending input_to() for a given
* interactive object. It is a specialization of input_s,
* which therefore must be the first element.
*/
typedef struct input_to_s input_to_t;
struct input_to_s {
input_t input;
callback_t fun; /* The function to call, and its args */
p_uint eval_nr; /* The thread number where this started. */
};
/*-------------------------------------------------------------------------*/
/* Variables */
interactive_t *all_players[MAX_PLAYERS];
/* Pointers to the structures of the interactive users.
* Unused entries are NULL.
* TODO: A list would be nicer
*/
static int max_player = -1;
/* Index of the last used entry in all_players[]. */
int num_player = 0;
/* The current number of active users */
p_int write_buffer_max_size = WRITE_BUFFER_MAX_SIZE;
/* Amount of data held pending in the write fifo queue.
* 0: No queue.
* -1: Infinite queue.
*/
char default_player_encoding[sizeof(((interactive_t*)0)->encoding)] = "ISO-8859-1//TRANSLIT";
/* Default encoding for interactives. */
static char udp_buf[65536];
/* Buffer for incoming UDP datagrams in get_message().
* If it is too small, the rest of the to be received datagram will be
* discarded. As UDP datagrams get be as large as (65536 - UDP-Header -
* IP-Header) we will just play safe here.
* static (file-scope) buffer, because we don't want to allocate such a
* large buffer on the stack or by xalloc (it is used often, we're not
* multi-threaded nor is get_message() re-entrant).
* Note for IPv6: IPv6 supports so-called jumbograms with payloads up to
* 2^32-1 octets in length (rfc2675). Obviously, we don't support that. ;-)
*/
#ifdef COMM_STAT
/* The statistics were originally introduced to measure the efficiency
* of the message buffering in comparison to the unbuffered sending of
* data. Nowadays, it's just interesting to know how much bandwidth you
* use.
*/
statcounter_t add_message_calls = 0;
/* Number of calls to add_message() */
statcounter_t inet_packets = 0;
/* Number packets sent to the users */
statcounter_t inet_volume = 0;
/* Amount of data sent to the users */
statcounter_t inet_packets_in = 0;
/* Number packets received from the users */
statcounter_t inet_volume_in = 0;
/* Amount of data received from the users */
#endif
/*-------------------------------------------------------------------------*/
#ifdef ERQ_DEMON
#define MAX_PENDING_ERQ 32 /* Max number of pending requests */
#define MAX_ERQ_PIDS 5 /* Maximum number of ERQ processes to wait. */
#define FLAG_NO_ERQ -2 /* No ERQ connected */
#define FLAG_ERQ_STOP -1 /* Severing connection */
static pid_t erq_pids[MAX_ERQ_PIDS];
/* PIDs of child processes we started. To prevent zombie processes we
* have to wait() for them after they terminated. And because there may
* be some overlap between starting a new one and killing the old one
* we remember more than one PID.
*/
static SOCKET_T erq_demon = FLAG_NO_ERQ;
/* Socket of the connection to the erq demon. */
static SOCKET_T erq_proto_demon = -1;
/* Socket to hold the connection to an aspiring new erq demon
* while the connection to the current one is being severed.
*/
static char buf_from_erq[ERQ_MAX_REPLY];
/* Buffer for the data received from the erq */
static char * input_from_erq = &buf_from_erq[0];
/* Pointer to the first free byte in buf_from_erq. */
static unsigned long erq_pending_len = 0;
/* erq_pending_len is used by send_erq(), but needs
* to be cleared by stop_erq_demon().
*/
typedef struct erq_callback_s {
svalue_t fun;
Bool string_arg;
} erq_callback_t;
static erq_callback_t pending_erq[MAX_PENDING_ERQ+1];
/* ERQ callback handles. The last one is reserved for callback-free
* requests.
* .fun is the callback closure, .string_arg is TRUE if the closure
* takes its data as a string instead of an array as argument.
*
* The free entries are organised in a singly linked list of
* T_INVALID .fun svalues, using the .fun.u.generic to point to the next
* free entry.
*/
static erq_callback_t *free_erq;
/* The first free entry in the freelist in pending_erq[] */
/* The size of the IPTABLE depends on the number of users,
* and is at least 200.
*/
#if MAX_PLAYERS > 700
# define IPSIZE MAX_PLAYERS
#else
# if MAX_PLAYERS > 100
# define IPSIZE (MAX_PLAYERS*2)
# else
# define IPSIZE 200
# endif
#endif
static struct ipentry {
struct in_addr addr; /* The address (only .s_addr is significant) */
string_t *name; /* tabled string with the hostname for <addr> */
} iptable[IPSIZE];
/* Cache of known names for given IP addresses.
* It is used as a ringbuffer, indexed by ipcur.
* TODO: Instead of a simple circular buffer, the lookup should be
* TODO:: hashed over the IP address. Worst case would still be O(IPSIZE),
* TODO:: but best case would be O(1).
* TODO: Allow unlimited numbers of entries, but give them a max lifetime
* TODO:: of a day. After that, even existing entries need to be
* TODO:: re-resolved. Maybe an additional size limit. (suggested by Coogan)
*/
static int ipcur = 0;
/* Index of the next entry to use in the iptable[].
*/
#endif /* ERQ_DEMON */
/*-------------------------------------------------------------------------*/
/* --- Communication sockets --- */
static SOCKET_T sos[MAXNUMPORTS];
/* The login sockets.
*/
static SOCKET_T udp_s = -1;
/* The UDP socket */
/* --- Networking information --- */
static char host_name[MAXHOSTNAMELEN+1];
/* This computer's hostname, used for query_host_name() efun.
*/
static struct in_addr host_ip_number;
/* This computer's numeric IP address only, used for
* the query_host_ip_number() efun.
*/
static struct sockaddr_in host_ip_addr_template;
/* The template address of this computer. It is copied locally
* and augmented with varying port numbers to open the driver's ports.
*/
char * domain_name = NULL;
/* This computer's domain name, as needed by lex.c::get_domainname().
*/
static int min_nfds = 0;
/* The number of fds used by the driver's sockets (udp, erq, login).
* It is the number of the highest fd plus one.
*/
/* --- Telnet handling --- */
static void (*telopts_do [NTELOPTS])(int);
static void (*telopts_dont[NTELOPTS])(int);
static void (*telopts_will[NTELOPTS])(int);
static void (*telopts_wont[NTELOPTS])(int);
/* Tables with the telnet statemachine handlers.
*/
enum telnet_states {
TS_DATA = 0, /* Initial state, we expect regular data or the begin of a telnet sequence. */
TS_IAC, /* Received an IAC, expect telnet command or another IAC. */
TS_WILL, /* Received IAC WILL, expect telnet option code. */
TS_WONT, /* Received IAC WONT, expect telnet option code. */
TS_DO, /* Received IAC DO, expect telnet option code. */
TS_DONT, /* Received IAC DONT, expect telnet option code. */
TS_SB, /* Received IAC SB and any number of bytes != IAC for the subnegotiation. */
TS_SB_IAC, /* Received IAC SB <contents> IAC. */
TS_READY, /* Received a full line, that is now in the .command[] buffer (without the newline). */
TS_CHAR_READY, /* Received at least one character in charmode, no line endings, yet. */
TS_SYNCH, /* Received SIGURG (out-of-band data) on the TCP channel and shall discard
all data until an IAC DM is received. */
TS_INVALID
};
/* Telnet states
*/
static inline int TN_START_VALID(int x)
{
return x == TS_SB || x == TS_SB_IAC;
}
/* --- Misc --- */
static volatile Bool urgent_data = MY_FALSE;
/* Flag set when a SIGURG/SIGIO announces the arrival of
* OOB data.
*/
static volatile mp_int urgent_data_time;
/* The backend::current_time when urgent_data was set last.
*/
static interactive_t *first_player_for_flush = NULL;
/* First interactive user object to flush. Marks the head
* of the list formed by interactive.{next,previous}_player_for_flush
*/
/* Bitflags for interactive.do_close
*
* Putting PROTO_ERQ into do_close looks strange, but actually makes
* sense because some of the steps to be taken for both are the
* same.
*/
#define FLAG_DO_CLOSE 0x1
#define FLAG_PROTO_ERQ 0x2
/*-------------------------------------------------------------------------*/
/* Outgoing connections in-progress */
typedef enum {
ocNotUsed /* Entry not used */
, ocUsed /* Entry holds pending connection */
, ocLoggingOn /* Entry is doing the LPC logon protocol.
* This value should not appear outside of
* check_for_out_connections(), if it does, it
* means that LPC logon threw an error.
*/
} OutConnStatus;
struct OutConn {
struct sockaddr_in target; /* Address connected to (allocated) */
object_t * curr_obj; /* Associated object */
int socket; /* Socket on our side */
OutConnStatus status; /* Status of this entry */
} outconn[MAX_OUTCONN];
/*-------------------------------------------------------------------------*/
/* Forward declarations */
static void mccp_telnet_neg(int);
static void free_input_to(input_to_t *);
static void free_input_handler(input_t *);
static void telnet_neg(interactive_t *);
static void telnet_neg_reset(interactive_t *);
static void send_will(int);
static void send_wont(int);
static void send_do(int);
static void send_dont(int);
static void add_flush_entry(interactive_t *ip);
static void remove_flush_entry(interactive_t *ip);
static void clear_message_buf(interactive_t *ip);
static void new_player(object_t *receiver, SOCKET_T new_socket, struct sockaddr_in *addr, size_t len, int login_port);
#ifdef ERQ_DEMON
static long read_32(char *);
static Bool send_erq(int handle, int request, const char *arg, size_t arglen);
static void shutdown_erq_demon(void);
static void stop_erq_demon(Bool);
static string_t * lookup_ip_entry (struct in_addr addr, Bool useErq);
static void add_ip_entry(struct in_addr addr, const char *name);
#ifdef USE_IPV6
static void update_ip_entry(const char *oldname, const char *newname);
#endif
#endif /* ERQ_DEMON */
static INLINE ssize_t comm_send_buf(char *msg, size_t size, interactive_t *ip);
#ifdef USE_IPV6
/*-------------------------------------------------------------------------*/
/* Not every IPv6 supporting platform has all the defines (like AIX 4.3) */
#ifndef IPV6_ADDR_SCOPE_GLOBAL
# define IPV6_ADDR_SCOPE_GLOBAL 0x0e
#endif
#ifndef s6_addr8
# define s6_addr8 __u6_addr.__u6_addr8
#endif
#ifndef s6_addr16
# define s6_addr16 __u6_addr.__u6_addr16
#endif
#ifndef s6_addr32
# define s6_addr32 __u6_addr.__u6_addr32
#endif
static inline void CREATE_IPV6_MAPPED(struct in_addr *v6, uint32 v4) {
v6->s6_addr32[0] = 0;
v6->s6_addr32[1] = 0;
v6->s6_addr32[2] = htonl(0x0000ffff);
v6->s6_addr32[3] = v4;
}
/* These are the typical IPv6 structures - we use them transparently.
*
* --- arpa/inet.h ---
*
* struct in6_addr {
* union {
* u_int32_t u6_addr32[4];
* #ifdef notyet
* u_int64_t u6_addr64[2];
* #endif
* u_int16_t u6_addr16[8];
* u_int8_t u6_addr8[16];
* } u6_addr;
* };
* #define s6_addr32 u6_addr.u6_addr32
* #ifdef notyet
* #define s6_addr64 u6_addr.u6_addr64
* #endif
* #define s6_addr16 u6_addr.u6_addr16
* #define s6_addr8 u6_addr.u6_addr8
* #define s6_addr u6_addr.u6_addr8
*
* --- netinet/in.h ---
*
* struct sockaddr_in6 {
* u_char sin6_len;
* u_char sin6_family;
* u_int16_t sin6_port;
* u_int32_t sin6_flowinfo;
* struct in6_addr sin6_addr;
* };
*
*/
/*-------------------------------------------------------------------------*/
static char *
inet6_ntoa (struct in6_addr in)
/* Convert the ipv6 address <in> into a string and return it.
* Note: the string is stored in a local buffer.
*/
{
static char str[INET6_ADDRSTRLEN+1];
if (NULL == inet_ntop(AF_INET6, &in, str, INET6_ADDRSTRLEN))
{
perror("inet_ntop");
}
return str;
} /* inet6_ntoa() */
/*-------------------------------------------------------------------------*/
static struct in6_addr
inet6_addr (const char *to_host)
/* Convert the name <to_host> into a ipv6 address and return it.
*/
{
struct in6_addr addr;
inet_pton(AF_INET6, to_host, &addr);
return addr;
} /* inet6_addr() */
#endif /* USE_IPV6 */
/*-------------------------------------------------------------------------*/
static char *
decode_noecho (char noecho)
/* Decode the <noecho> flag byte into a string.
* Result is a pointer to a static buffer.
*/
{
static char buf[100];
strcpy(buf, "(");
if (noecho & NOECHO_REQ) strcat(buf, "NOECHO_REQ, ");
if (noecho & CHARMODE_REQ) strcat(buf, "CHARMODE_REQ, ");
if (noecho & NOECHO) strcat(buf, "NOECHO, ");
if (noecho & CHARMODE) strcat(buf, "CHARMODE, ");
if (noecho & NOECHO_ACK) strcat(buf, "NOECHO_ACK, ");
if (noecho & CHARMODE_ACK) strcat(buf, "CHARMODE_ACK, ");
#ifdef SAVE_NOECHO
if (noecho & NOECHO_DELAYED) strcat(buf, "NOECHO_DELAYED, ");
#endif
if (noecho & NOECHO_STALE) strcat(buf, "NOECHO_STALE, ");
if (noecho & IGNORE_BANG) strcat(buf, "IGNORE_BANG");
strcat(buf, ")");
return buf;
} /* decode_noecho() */
/*-------------------------------------------------------------------------*/
static void
dump_bytes (void * data, size_t length, int indent)
/* Write the datablock starting at <data> of size <length> to stderr.
* If it spans more than one line, indent the following lines by <indent>.
*/
{
int cur_indent = 0;
unsigned char * datap = (unsigned char *)data;
while (length > 0)
{
size_t count;
if (cur_indent)
fprintf(stderr, "%*.*s", cur_indent, cur_indent, " ");
else
cur_indent = indent;
fprintf(stderr, " %p:", datap);
for (count = 0; count < 16 && length > 0; ++count, --length, ++datap)
{
fprintf(stderr, " %02x", *datap);
}
putc('\n', stderr);
}
} /* dump_bytes() */
/*-------------------------------------------------------------------------*/
static void comm_fatal (interactive_t *ip, char *fmt, ...)
FORMATDEBUG(printf,2,3) ;
static void
comm_fatal (interactive_t *ip, char *fmt, ...)
/* The telnet code ran into a fatal error.
* Dump the data from the current interactive structure and disconnect
* the user (we have to assume that the interactive structure is
* irrecoverably hosed).
* TODO: Make similar functions comm_error(), comm_perror() which prefix
* TODO:: the error message with the ip %p and obj name.
*/
{
va_list va;
static Bool in_fatal = MY_FALSE;
char * ts;
char * msg = "\r\n=== Internal communications error in mud driver.\r\n"
"=== Please log back in and inform the administration.\r\n"
"\r\n";
/* Prevent double fatal. */
if (in_fatal)
fatal("Recursive call to comm_fatal().");
in_fatal = MY_TRUE;
ts = time_stamp();
/* Print the error message */
va_start(va, fmt);
fflush(stdout);
fprintf(stderr, "%s ", ts);
vfprintf(stderr, fmt, va);
fflush(stderr);
debug_message("%s ", ts);
vdebug_message(fmt, va);
if (current_object.type == T_OBJECT)
{
fprintf(stderr, "%s Current object was %s\n"
, ts, current_object.u.ob->name
? get_txt(current_object.u.ob->name) : "<null>");
debug_message("%s Current object was %s\n"
, ts, current_object.u.ob->name
? get_txt(current_object.u.ob->name) : "<null>");
}
else if (current_object.type == T_LWOBJECT)
{
fprintf(stderr, "%s Current object was lightweight from %s\n"
, ts, get_txt(current_object.u.ob->name));
debug_message("%s Current object was lightweight from %s\n"
, ts, get_txt(current_object.u.lwob->prog->name));
}
debug_message("%s Dump of the call chain:\n", ts);
(void)dump_trace(MY_TRUE, NULL, NULL); fflush(stdout);
va_end(va);
/* Dump the interactive structure */
fprintf(stderr, "--- Dump of current interactive structure (%p..%p) --- \n"
, ip, ip + sizeof(*ip) - 1);
fprintf(stderr, " .socket: %d\n", ip->socket);
fprintf(stderr, " .ob: %p", ip->ob);
if (ip->ob) fprintf(stderr, " (%s)", get_txt(ip->ob->name));
putc('\n', stderr);
fprintf(stderr, " .input_handler: %p\n", ip->input_handler);
fprintf(stderr, " .modify_command: %p", ip->modify_command);
if (ip->modify_command) fprintf(stderr, " (%s)", get_txt(ip->modify_command->name));
putc('\n', stderr);
fprintf(stderr, " .prompt: ");
dump_bytes(&(ip->prompt), sizeof(ip->prompt), 21);
fprintf(stderr, " .addr: ");
dump_bytes(&(ip->addr), sizeof(ip->addr), 21);
fprintf(stderr, " .closing: %02hhx\n", (unsigned char)ip->closing);
fprintf(stderr, " .tn_enabled: %02hhx\n", (unsigned char)ip->tn_enabled);
fprintf(stderr, " .do_close: %02hhx", (unsigned char)ip->do_close);
if (ip->do_close & (FLAG_DO_CLOSE|FLAG_PROTO_ERQ)) fprintf(stderr, " (");
if (ip->do_close & FLAG_DO_CLOSE) fprintf(stderr, "DO_CLOSE");
if (ip->do_close & (FLAG_DO_CLOSE|FLAG_PROTO_ERQ)) fprintf(stderr, ", ");
if (ip->do_close & FLAG_PROTO_ERQ) fprintf(stderr, "PROTO_ERQ");
if (ip->do_close & (FLAG_DO_CLOSE|FLAG_PROTO_ERQ)) fprintf(stderr, ")");
putc('\n', stderr);
fprintf(stderr, " .noecho: %02hhx", (unsigned char)ip->noecho);
fprintf(stderr, " %s\n", decode_noecho(ip->noecho));
fprintf(stderr, " .tn_state: %hhd", ip->tn_state);
switch(ip->tn_state) {
case TS_DATA: fprintf(stderr, " (TS_DATA)\n"); break;
case TS_IAC: fprintf(stderr, " (TS_IAC)\n"); break;
case TS_WILL: fprintf(stderr, " (TS_WILL)\n"); break;
case TS_WONT: fprintf(stderr, " (TS_WONT)\n"); break;
case TS_DO: fprintf(stderr, " (TS_DO)\n"); break;
case TS_DONT: fprintf(stderr, " (TS_DONT)\n"); break;
case TS_SB: fprintf(stderr, " (TS_SB)\n"); break;
case TS_SB_IAC: fprintf(stderr, " (TS_SB_IAC)\n"); break;
case TS_READY: fprintf(stderr, " (TS_READY)\n"); break;
case TS_CHAR_READY: fprintf(stderr, " (TS_CHAR_READY)\n"); break;
case TS_SYNCH: fprintf(stderr, " (TS_SYNCH)\n"); break;
case TS_INVALID: fprintf(stderr, " (TS_INVALID)\n"); break;
default: putc('\n', stderr);
}
fprintf(stderr, " .supress_go_ahead: %02hhx\n", (unsigned char)ip->supress_go_ahead);
fprintf(stderr, " .text_prefix: %hd (%p)\n", ip->text_prefix, ip->text+ip->text_prefix);
fprintf(stderr, " .tn_start: %hd (%p)\n", ip->tn_start, ip->text+ip->tn_start);
fprintf(stderr, " .tn_end: %hd (%p)\n", ip->tn_end, ip->text+ip->tn_end);
fprintf(stderr, " .text_end: %hd (%p)\n", ip->text_end, ip->text+ip->text_end);
fprintf(stderr, " .command_start: %hd (%p)\n", ip->command_start, ip->command+ip->command_start);
fprintf(stderr, " .command_end: %hd (%p)\n", ip->command_end, ip->command+ip->command_end);
fprintf(stderr, " .command_unprc_end: %hd (%p)\n", ip->command_unprocessed_end, ip->command+ip->command_unprocessed_end);
fprintf(stderr, " .command_printed: %hd (%p)\n", ip->command_printed, ip->command+ip->command_printed);
fprintf(stderr, " .snoop_on: %p", ip->snoop_on);
if (ip->snoop_on && ip->snoop_on->ob) fprintf(stderr, " (%s)", get_txt(ip->snoop_on->ob->name));
putc('\n', stderr);
fprintf(stderr, " .snoop_by: %p", ip->snoop_by);
if (ip->snoop_by) fprintf(stderr, " (%s)", get_txt(ip->snoop_by->name));
putc('\n', stderr);
fprintf(stderr, " .last_time: %"PRIdMPINT"\n", ip->last_time);
fprintf(stderr, " .numCmds: %ld\n", ip->numCmds);
fprintf(stderr, " .maxNumCmds: %ld\n", ip->maxNumCmds);
fprintf(stderr, " .trace_level: %d\n", ip->trace_level);
fprintf(stderr, " .trace_prefix: %p", ip->trace_prefix);
if (ip->trace_prefix) fprintf(stderr, " '%s'", get_txt(ip->trace_prefix));
putc('\n', stderr);
fprintf(stderr, " .message_length: %d (%p)\n", ip->message_length, ip->message_buf+ip->message_length);
fprintf(stderr, " .next_for_flush: %p", ip->next_player_for_flush);
if (ip->next_player_for_flush) fprintf(stderr, " (%s)", get_txt(ip->next_player_for_flush->ob->name));
putc('\n', stderr);
fprintf(stderr, " .prev_for_flush: %p", ip->previous_player_for_flush);
if (ip->previous_player_for_flush) fprintf(stderr, " (%s)", get_txt(ip->previous_player_for_flush->ob->name));
putc('\n', stderr);
fprintf(stderr, " .access_class: %ld\n", ip->access_class);
fprintf(stderr, " .charset: ");
dump_bytes(&(ip->charset), sizeof(ip->charset), 21);
fprintf(stderr, " .combine_cset: ");
dump_bytes(&(ip->combine_cset), sizeof(ip->combine_cset), 21);
fprintf(stderr, " .quote_iac: %02hhx\n", (unsigned char)ip->quote_iac);
fprintf(stderr, " .catch_tell_activ: %02hhx\n", (unsigned char)ip->catch_tell_activ);
fprintf(stderr, " .gobble_char: %02hhx\n", (unsigned char)ip->gobble_char);
fprintf(stderr, " .syncing: %02hhx\n", (unsigned char)ip->syncing);
fprintf(stderr, " .text: ");
dump_bytes(&(ip->text), sizeof(ip->text), 21);
fprintf(stderr, " .command: ");
dump_bytes(&(ip->command), sizeof(ip->command), 21);
fprintf(stderr, " .message_buf: ");
dump_bytes(&(ip->message_buf), sizeof(ip->message_buf), 21);
fprintf(stderr, "------\n");
/* Disconnect the user */
comm_send_buf(msg, strlen(msg), ip);
remove_interactive(ip->ob, MY_TRUE);
/* Unset mutex */
in_fatal = MY_FALSE;
} /* comm_fatal() */
/*-------------------------------------------------------------------------*/
static void
set_socket_nosigpipe (SOCKET_T new_socket)
/* Set the <new_socket> to not generate SIGPIPE signals (we anyway ignore them).
* Failure is acceptable, the option is only available on some systems.
*/
{
#ifdef SO_NOSIGPIPE
const int nopipe = 1;
if (setsockopt(new_socket, SOL_SOCKET, SO_NOSIGPIPE, &nopipe, sizeof(nopipe)) != 0)
{
perror("Failure setting SO_NOSIGPIPE");
}
#endif
}
/*-------------------------------------------------------------------------*/
static void
set_socket_nonblocking (SOCKET_T new_socket)
/* Set the <new_socket> into non-blocking mode.
* Abort on error.
*/
{
int tmp;
tmp = 1;
# ifdef USE_IOCTL_FIONBIO
if (socket_ioctl(new_socket, FIONBIO, &tmp) == -1) {
perror("ioctl socket FIONBIO");
abort();
}
# else /* !USE_IOCTL_FIONBIO */
# ifdef USE_FCNTL_O_NDELAY
if (fcntl(new_socket, F_SETFL, O_NDELAY) == -1) {
# else
if (fcntl(new_socket, F_SETFL, FNDELAY) == -1) {
# endif
perror("fcntl socket FNDELAY");
abort();
}
# endif /* !USE_IOCTL_FIONBIO */
} /* set_socket_nonblocking() */
/*-------------------------------------------------------------------------*/
static void
set_close_on_exec (SOCKET_T new_socket)
/* Set that <new_socket> is closed when the driver performs an exec()
* (i.e. when starting the ERQ).
* Failure is acceptable as this is just a nicety.
*/
{
#ifdef HAVE_FCNTL
fcntl(new_socket, F_SETFD, 1L);
#endif
} /* set_close_on_exec() */
/*-------------------------------------------------------------------------*/
static void
set_socket_own (SOCKET_T new_socket)
/* Enable OOB communication on <new_socket>: the driver is set to
* receive SIGIO and SIGURG signals, and OOBINLINE is enabled.
* Failure is acceptable as both facilities are not available everywhere.
*/
{
#if defined(F_SETOWN) && defined(USE_FCNTL_SETOWN)
if (0 > fcntl(new_socket, F_SETOWN, getpid()))
{
perror("fcntl SETOWN");
}
#endif
#if defined(SO_OOBINLINE) && defined(USE_OOBINLINE)
{
int on = 1;
if (0 > setsockopt(new_socket, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof on))
{
perror("setsockopt SO_OOBINLINE");
}
}
#endif
new_socket = 0; /* Prevent 'not used' warning */
} /* set_socket_own() */
/*-------------------------------------------------------------------------*/
void
initialize_host_name (const char *hname)
/* This function is called at an early stage of the driver startup in order
* to initialise the global host_name with a useful value (specifically so
* that we can open the debug.log file).
* If <hname> is given, the hostname is parsed from the string, otherwise it
* is queried from the system.
*
* The value set in this function will later be overwritten by the
* call to initialize_host_ip_number().
*
* exit() on failure.
*/
{
char *domain;
/* Get the (possibly qualified) hostname */
if (hname != NULL)
{
if (strlen(hname) > MAXHOSTNAMELEN)
{
fprintf(stderr, "%s Given hostname '%s' too long.\n"
, time_stamp(), hname);
exit(1);
}
else
strcpy(host_name, hname);
}
else
{
if (gethostname(host_name, sizeof host_name) == -1) {
herror("gethostname");
exit(1);
}
}
/* Cut off the domain name part of the hostname, if any.
*/
domain = strchr(host_name, '.');
if (domain)
*domain = '\0';
} /* initialize_host_name() */
/*-------------------------------------------------------------------------*/
void
initialize_host_ip_number (const char *hname, const char * haddr)
/* Initialise the globals host_ip_number and host_ip_addr_template.
* If <hname> or <haddr> are given, the hostname/hostaddr are parsed
* from the strings, otherwise they are queried from the system.
*
* Open the UDP port if requested so that it can be used in inaugurate_master().
* exit() on failure.
*/
{
char *domain;
length_t tmp;
/* Get the (possibly qualified) hostname */
if (hname != NULL)
{
if (strlen(hname) > MAXHOSTNAMELEN)
{
fprintf(stderr, "%s Given hostname '%s' too long.\n"