-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_tmxr.c
5772 lines (5099 loc) · 241 KB
/
sim_tmxr.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
/* sim_tmxr.c: Telnet terminal multiplexer library
Copyright (c) 2001-2011, Robert M Supnik
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Robert M Supnik shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Robert M Supnik.
Based on the original DZ11 simulator by Thord Nilson, as updated by
Arthur Krewat.
12-Oct-12 MP Revised serial port support to not require changes to
any code in TMXR library using code. Added support
for per line listener ports and outgoing tcp connections.
02-Jun-11 MP Fixed telnet option negotiation loop with some clients
Added Option Negotiation and Debugging Support
17-Jan-11 MP Added Buffered line capabilities
16-Jan-11 MP Made option negotiation more reliable
20-Nov-08 RMS Added three new standardized SHOW routines
05-Nov-08 JDB Moved logging call after connection check in tmxr_putc_ln
03-Nov-08 JDB Added TMXR null check to tmxr_find_ldsc
07-Oct-08 JDB Added initial serial port support
30-Sep-08 JDB Reverted tmxr_find_ldsc to original implementation
27-May-08 JDB Added line connection order to tmxr_poll_conn,
added tmxr_set_lnorder and tmxr_show_lnorder
14-May-08 JDB Print device and line to which connection was made
11-Apr-07 JDB Worked around Telnet negotiation problem with QCTerm
16-Aug-05 RMS Fixed C++ declaration and cast problems
29-Jun-05 RMS Extended tmxr_dscln to support unit array devices
Fixed bug in SET LOG/NOLOG
04-Jan-04 RMS Changed TMXR ldsc to be pointer to linedesc array
Added tmxr_linemsg, circular output pointers, logging
(from Mark Pizzolato)
29-Dec-03 RMS Added output stall support
01-Nov-03 RMS Cleaned up attach routine
09-Mar-03 RMS Fixed bug in SHOW CONN
22-Dec-02 RMS Fixed bugs in IAC+IAC receive and transmit sequences
Added support for received break (all from by Mark Pizzolato)
Fixed bug in attach
31-Oct-02 RMS Fixed bug in 8b (binary) support
22-Aug-02 RMS Added tmxr_open_master, tmxr_close_master
30-Dec-01 RMS Added tmxr_fstats, tmxr_dscln, renamed tmxr_fstatus
03-Dec-01 RMS Changed tmxr_fconns for extended SET/SHOW
20-Oct-01 RMS Fixed bugs in read logic (found by Thord Nilson).
Added tmxr_rqln, tmxr_tqln
This library includes:
tmxr_poll_conn - poll for connection
tmxr_reset_ln - reset line (drops Telnet/tcp and serial connections)
tmxr_detach_ln - reset line and close per line listener and outgoing destination
tmxr_getc_ln - get character for line
tmxr_get_packet_ln - get packet from line
tmxr_get_packet_ln_ex - get packet from line with separater byte
tmxr_poll_rx - poll receive
tmxr_putc_ln - put character for line
tmxr_put_packet_ln - put packet on line
tmxr_put_packet_ln_ex - put packet on line with separator byte
tmxr_poll_tx - poll transmit
tmxr_send_buffered_data - transmit buffered data
tmxr_set_modem_control_passthru - enable modem control on a multiplexer
tmxr_clear_modem_control_passthru - disable modem control on a multiplexer
tmxr_set_port_speed_control - Declare that tmxr_set_config_line is used
tmxr_clear_port_speed_control - Declare that tmxr_set_config_line is not used
tmxr_set_line_port_speed_control - Declare that tmxr_set_config_line is used for line
tmxr_clear_line_port_speed_control - Declare that tmxr_set_config_line is not used for line
tmxr_set_get_modem_bits - set and/or get a line modem bits
tmxr_set_line_loopback - enable or disable loopback mode on a line
tmxr_get_line_loopback - returns the current loopback status of a line
tmxr_set_line_halfduplex - enable or disable halfduplex mode on a line
tmxr_get_line_halfduplex - returns the current halfduplex status of a line
tmxr_set_config_line - set port speed, character size, parity and stop bits
tmxr_open_master - open master connection
tmxr_close_master - close master connection
tmxr_attach - attach terminal multiplexor to listening port
tmxr_detach - detach terminal multiplexor to listening port
tmxr_attach_help - help routine for attaching multiplexer devices
tmxr_set_line_unit - set the unit which polls for input for a given line
tmxr_ex - (null) examine
tmxr_dep - (null) deposit
tmxr_msg - send message to socket
tmxr_linemsg - send message to line
tmxr_linemsgf - send formatted message to line
tmxr_fconns - output connection status
tmxr_fstats - output connection statistics
tmxr_set_log - enable logging for line
tmxr_set_nolog - disable logging for line
tmxr_show_log - show logging status for line
tmxr_dscln - disconnect line (SET routine)
tmxr_rqln - number of available characters for line
tmxr_tqln - number of buffered characters for line
tmxr_tpqln - number of buffered packet characters for line
tmxr_tpbusyln - transmit packet busy status for line
tmxr_set_lnorder - set line connection order
tmxr_show_lnorder - show line connection order
tmxr_show_summ - show connection summary
tmxr_show_cstat - show line connections or status
tmxr_show_lines - show number of lines
tmxr_show_open_devices - show info about all open tmxr devices
All routines are OS-independent.
This library supports the simulation of multiple-line terminal multiplexers.
It may also be used to create single-line "multiplexers" to provide
additional terminals beyond the simulation console. It may also be used to
create single-line or multi-line simulated synchronous (BiSync) devices.
Multiplexer lines may be connected to terminal emulators supporting the
Telnet protocol via sockets, or to hardware terminals via host serial
ports. Concurrent Telnet and serial connections may be mixed on a given
multiplexer.
When connecting via sockets, the simulated multiplexer is attached to a
listening port on the host system:
sim> attach MUX 23
Listening on port 23
Once attached, the listening port must be polled for incoming connections.
When a connection attempt is received, it will be associated with the next
multiplexer line in the user-specified line order, or with the next line in
sequence if no order has been specified. Individual lines may be connected
to serial ports or remote systems via TCP (telnet or not as desired), OR
they may have separate listening TCP ports.
Logging of Multiplexer Line output:
The traffic going out multiplexer lines can be logged to files. A single
line multiplexer can log it's traffic with the following command:
sim> atta MUX 23,Log=LogFileName
sim> atta MUX Connect=ser0,Log=LogFileName
Specifying a Log value for a multi-line multiplexer is specifying a
template filename. The actual file name used for each line will be
the indicated filename with _n appended (n being the line number).
Buffered Multiplexer Line:
A Multiplexer Line Buffering has been implemented. A Buffered Line will
have a copy of the last 'buffer size' bytes of output retained in a line
specific buffer. The contents of this buffer will be transmitted out any
new connection on that line when a new telnet session is established.
This capability is most useful for the Console Telnet session. When a
Console Telnet session is Buffered, a simulator will start (via BOOT CPU
or whatever is appropriate for a particular simulator) without needing to
have an active telnet connection. When a Telnet connection comes along
for the telnet port, the contents of the saved buffer (which wraps on
overflow) are presented on the telnet session as output before session
traffic. This allows the connecting telnet client to see what happened
before he connected since the likely reason he might be connecting to the
console of a background simulator is to troubleshoot unusual behavior,
the details of which may have already been sent to the console.
Serial Port support:
Serial ports may be specified as an operating system specific device names
or using simh generic serial names. simh generic names are of the form
serN, where N is from 0 thru one less than the maximum number of serial
ports on the local system. The mapping of simh generic port names to OS
specific names can be displayed using the following command:
sim> show serial
Serial devices:
ser0 COM1 (\Device\Serial0)
ser1 COM3 (Winachcf0)
sim> attach MUX Line=2,Connect=ser0
or equivalently
sim> attach MUX Line=2,Connect=COM1
An optional configuration string may be present after the port name. If
present, it must be separated from the port name with a semicolon and has
this form:
<rate>-<charsize><parity><stopbits>
where:
rate = communication rate in bits per second
charsize = character size in bits (5-8, including optional parity)
parity = parity designator (N/E/O/M/S for no/even/odd/mark/space parity)
stopbits = number of stop bits (1, 1.5, or 2)
As an example:
9600-8n1
The supported rates, sizes, and parity options are host-specific. If
a configuration string is not supplied, then the default of 9600-8N1
is used.
An attachment to a serial port with the '-V' switch will cause a
connection message to be output to the connected serial port.
This will help to confirm the correct port has been connected and
that the port settings are reasonable for the connected device.
This would be done as:
sim> attach -V MUX Connect=SerN
Line specific tcp listening ports are supported. These are configured
using commands of the form:
sim> attach MUX Line=2,port{;notelnet}|{;nomessage}
Direct computer to computer connections (Virutal Null Modem cables) may
be established using the telnet protocol or via raw tcp sockets.
sim> attach MUX Line=2,Connect=host:port{;notelnet}
Computer to computer virtual connections can be one way (as illustrated
above) or symmetric. A symmetric connection is configured by combining
a one way connection with a tcp listening port on the same line:
sim> attach MUX Line=2,Connect=host:port,listenport
When symmetric virtual connections are configured, incoming connections
on the specified listening port are checked to assure that they actually
come from the specified connection destination host system.
The command syntax for a single line device (MX) is:
sim> attach MX port{;notelnet}|{;nomessage}
sim> attach MX Connect=serN{;config}
sim> attach MX Connect=COM9{;config}
sim> attach MX Connect=host:port{;notelnet}|{;nomessage}
The command syntax for ANY multi-line device is:
sim> attach MX port{;notelnet}|{;nomessage} ; Defines the master listening port for the mux and optionally allows non-telnet (i.e. raw socket) operation for all lines.
sim> attach MX Line=n,port{;notelnet}|{;nomessage} ; Defines a line specific listen port for a particular line. Each line can have a separate listen port and the mux can have its own as well. Optionally disable telnet wire protocol (i.e. raw socket)
sim> attach MX Line=n,Connect=serN{;config} ; Connects line n to simh generic serial port N (port list visible with the sim> SHOW SERIAL command), the optional ";config" data specifies the speed, parity and stop bits for the connection
; DTR (and RTS) will be raised at attach time and will drop at detach/disconnect time
sim> attach MX Line=n,Connect=host:port{;notelnet} ; Causes a connection to be established to the designated host:port. The actual connection will happen in a non-blocking fashion and will be completed and/or re-established by the normal tmxr_poll_conn activities
All connections configured for any multiplexer device are unconfigured by:
sim> detach MX ; detaches ALL connections/ports/sessions on the MUX.
Console serial connections are achieved by:
sim> set console serial=serN{;config}
or
sim> set console serial=COM2{;config}
A line specific listening port (12366) can be specified by the following:
sim> attach MUX Line=2,12366
A line specific remote telnet (or raw tcp) destination can be specified
by the following:
sim> attach MUX Line=2,Connect=remotehost:port
If a connection to a remotehost:port wants a raw binary data channel
(instead of a telnet session) the following would be used:
sim> attach MUX Line=2,Connect=remotehost:port;notelnet
A single line multiplexor can indicate any of the above line options
without specifying a line number:
sim> attach MUX Connect=ser0;9600-8N1
sim> attach MUX 12366{;notelnet}|{;nomessage}
sim> attach MUX Connect=remotehost:port
sim> attach MUX Connect=remotehost:port;notelnet
A multiplexor can disconnect all (telnet, serial and outgoing) previous
attachments with:
sim> detach MUX
A device emulation may choose to implement a command interface to
disconnect specific individual lines. This would usually be done via
a Unit Modifier table entry (MTAB) which dispatches the command
"SET dev DISCONNECT[=line]" to tmxr_dscln. This will cause a telnet
connection to be closed, but a serial port will normally have DTR
dropped for 500ms and raised again (thus hanging up a modem on that
serial port).
sim> set MUX disconnect=2
Full Modem Control serial port support.
This library supports devices which wish to emulate full modem
control/signalling for serial ports. Any device emulation which wishes
to support this functionality for attached serial ports must call
"tmxr_set_modem_control_passthru" before any call to tmxr_attach.
This disables automatic DTR (&RTS) manipulation by this library.
Responsibility for manipulating DTR falls on the simulated operating
system. Calling tmxr_set_modem_control_passthru would usually be in
a device reset routine. It may also be called by a device attach
routine based on user specified options.
Once support for full modem control has been declared by a device
emulation for a particular TMXR device, this library will make no
direct effort to manipulate modem bits while connected to serial ports.
The "tmxr_set_get_modem_bits" API exists to allow the device emulation
layer to query and control modem signals. The "tmxr_set_config_line"
API exists to allow the device emulation layer to change port settings
(baud rate, parity and stop bits). A modem_control enabled line
merely passes the VM's port status bits, data and settings through to
and from the serial port.
The "tmxr_set_get_modem_bits" and "tmxr_set_config_line" APIs will
ONLY work on a modem control enabled TMXR device.
*/
#define NOT_MUX_USING_CODE /* sim_tmxr library define */
#include "sim_defs.h"
#include "sim_scp_private.h"
#include "sim_serial.h"
#include "sim_sock.h"
#include "sim_timer.h"
#include "sim_tmxr.h"
#include "sim_ether.h"
#include "scp.h"
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
/* Telnet protocol constants - negatives are for init'ing signed char data */
/* Commands */
#define TN_IAC 0xFFu /* -1 */ /* protocol delim */
#define TN_DONT 0xFEu /* -2 */ /* dont */
#define TN_DO 0xFDu /* -3 */ /* do */
#define TN_WONT 0xFCu /* -4 */ /* wont */
#define TN_WILL 0xFBu /* -5 */ /* will */
#define TN_SB 0xFAu /* -6 */ /* sub-option negotiation */
#define TN_GA 0xF9u /* -7 */ /* go ahead */
#define TN_EL 0xF8u /* -8 */ /* erase line */
#define TN_EC 0xF7u /* -9 */ /* erase character */
#define TN_AYT 0xF6u /* -10 */ /* are you there */
#define TN_AO 0xF5u /* -11 */ /* abort output */
#define TN_IP 0xF4u /* -12 */ /* interrupt process */
#define TN_BRK 0xF3u /* -13 */ /* break */
#define TN_DATAMK 0xF2u /* -14 */ /* data mark */
#define TN_NOP 0xF1u /* -15 */ /* no operation */
#define TN_SE 0xF0u /* -16 */ /* end sub-option negot */
/* Options */
#define TN_BIN 0 /* bin */
#define TN_ECHO 1 /* echo */
#define TN_SGA 3 /* sga */
#define TN_STATUS 5 /* option status query */
#define TN_TIMING 6 /* Timing Mark */
#define TN_NAOCRD 10 /* Output Carriage-Return Disposition */
#define TN_NAOHTS 11 /* Output Horizontal Tab Stops */
#define TN_NAOHTD 12 /* Output Horizontal Tab Stop Disposition */
#define TN_NAOFFD 13 /* Output Forfeed Disposition */
#define TN_NAOVTS 14 /* Output Vertical Tab Stop */
#define TN_NAOVTD 15 /* Output Vertical Tab Stop Disposition */
#define TN_NAOLFD 16 /* Output Linefeed Disposition */
#define TN_EXTEND 17 /* Extended Ascii */
#define TN_LOGOUT 18 /* Logout */
#define TN_BM 19 /* Byte Macro */
#define TN_DET 20 /* Data Entry Terminal */
#define TN_SENDLO 23 /* Send Location */
#define TN_TERMTY 24 /* Terminal Type */
#define TN_ENDREC 25 /* Terminal Type */
#define TN_TUID 26 /* TACACS User Identification */
#define TN_OUTMRK 27 /* Output Marking */
#define TN_TTYLOC 28 /* Terminal Location Number */
#define TN_3270 29 /* 3270 Regime */
#define TN_X3PAD 30 /* X.3 PAD */
#define TN_NAWS 31 /* Negotiate About Window Size */
#define TN_TERMSP 32 /* Terminal Speed */
#define TN_TOGFLO 33 /* Remote Flow Control */
#define TN_LINE 34 /* line mode */
#define TN_XDISPL 35 /* X Display Location */
#define TN_ENVIRO 36 /* Environment */
#define TN_AUTH 37 /* Authentication */
#define TN_ENCRYP 38 /* Data Encryption */
#define TN_NEWENV 39 /* New Environment */
#define TN_TN3270 40 /* TN3270 Enhancements */
#define TN_CHARST 42 /* CHARSET */
#define TN_COMPRT 44 /* Com Port Control */
#define TN_KERMIT 47 /* KERMIT */
#define TN_CR 015 /* carriage return */
#define TN_LF 012 /* line feed */
#define TN_NUL 000 /* null */
/* Telnet line states */
#define TNS_NORM 000 /* normal */
#define TNS_IAC 001 /* IAC seen */
#define TNS_WILL 002 /* WILL seen */
#define TNS_WONT 003 /* WONT seen */
#define TNS_SKIP 004 /* skip next cmd */
#define TNS_CRPAD 005 /* CR padding */
#define TNS_DO 006 /* DO request pending rejection */
/* Telnet Option Sent Flags */
#define TNOS_DONT 001 /* Don't has been sent */
#define TNOS_WONT 002 /* Won't has been sent */
/* Lifted from ddcmp.c in the framer firmware */
struct status_msg_t
{
uint8 dc1;
uint8 on; /* "on" flags */
uint16 mflags;
uint32 speed;
uint32 txspeed;
uint32 rxframes;
uint32 rxbytes;
uint32 txframes;
uint32 txbytes;
uint32 hcrc_err;
uint32 crc_err;
uint32 len_err;
uint32 nobuf_err;
uint32 last_cmd_sts; /* Response code from last command */
uint32 freq; /* Measured frequency */
char version[64];
};
#define ON_ACT 1
#define ON_SYN 2
#define ON_CLKOK 4
/* This struct is internal to sim_tmxr, used when a line is attached
* to a DDCMP synchronous framer device (a USB peripheral that looks
* like an Ethernet interface).
*/
typedef struct framer_data {
ETH_DEV *eth; /* Ethernet device pointer if framer */
uint16 fmode; /* Framer mode from attach command */
uint32 fspeed; /* Framer link speed from attach command */
struct status_msg_t status; /* Last received status message */
int status_cnt; /* Count of status messages seen */
t_bool connect_pending; /* True if connected not yet reported */
} FRAMER;
static BITFIELD tmxr_modem_bits[] = {
BIT(DTR), /* Data Terminal Ready */
BIT(RTS), /* Request To Send */
BIT(DCD), /* Data Carrier Detect */
BIT(RNG), /* Ring Indicator */
BIT(CTS), /* Clear To Send */
BIT(DSR), /* Data Set Ready */
ENDBITS
};
static u_char mantra[] = { /* Telnet Option Negotiation Mantra */
TN_IAC, TN_WILL, TN_LINE,
TN_IAC, TN_WILL, TN_SGA,
TN_IAC, TN_WILL, TN_ECHO,
TN_IAC, TN_WILL, TN_BIN,
TN_IAC, TN_DO, TN_BIN
};
#define TMXR_GUARD ((int32)(lp->serport ? 1 : sizeof(mantra)))/* buffer guard */
#define TMXR_LINE_DISABLED (-1)
/* Local routines */
static void tmxr_setup_framer(TMLN *line, ETH_PACK *packet, int len);
static int tmxr_framer_read (TMLN *line, char *buf, int nbytes);
static int tmxr_framer_write (TMLN *line, const char *buf, int32 length);
static void tmxr_add_to_open_list (TMXR* mux);
/* Initialize the line state.
Reset the line state to represent an idle line. Note that we do not clear
all of the line structure members, so a connected line remains connected
after this call.
Because a line break is represented by a flag in the "receive break status"
array, we must zero that array in order to clear any pending break
indications.
*/
static void tmxr_init_line (TMLN *lp)
{
lp->tsta = 0; /* init telnet state */
lp->xmte = 1; /* enable transmit */
lp->dstb = 0; /* default bin mode */
lp->rxbpr = lp->rxbpi = lp->rxcnt = lp->rxpcnt = 0; /* init receive indexes */
if (!lp->txbfd || lp->notelnet) /* if not buffered telnet */
lp->txbpr = lp->txbpi = lp->txcnt = lp->txpcnt = 0; /* init transmit indexes */
lp->txdrp = lp->txstall = 0;
tmxr_set_get_modem_bits (lp, 0, 0, NULL);
if (lp->mp && (!lp->mp->buffered) && (!lp->txbfd)) {
lp->txbfd = 0;
lp->txbsz = TMXR_MAXBUF;
lp->txb = (char *)realloc (lp->txb, lp->txbsz);
lp->rxbsz = TMXR_MAXBUF;
lp->rxb = (char *)realloc(lp->rxb, lp->rxbsz);
lp->rbr = (char *)realloc(lp->rbr, lp->rxbsz);
}
if (lp->loopback) {
lp->lpbsz = lp->rxbsz;
lp->lpb = (char *)realloc(lp->lpb, lp->lpbsz);
lp->lpbcnt = lp->lpbpi = lp->lpbpr = 0;
}
if (lp->rxpb) {
lp->rxpboffset = lp->rxpbsize = 0;
free (lp->rxpb);
lp->rxpb = NULL;
}
if (lp->txpb) {
lp->txpbsize = lp->txppsize = lp->txppoffset = 0;
free (lp->txpb);
lp->txpb = NULL;
}
memset (lp->rbr, 0, lp->rxbsz); /* clear break status array */
}
/* Report a connection to a line.
If the indicated line (lp) is speaking the telnet wire protocol, a
notification of the form:
Connected to the <sim> simulator <dev> device, line <n>
is sent to the newly connected line. If the device has only one line, the
"line <n>" part is omitted. If the device has not been defined, the "<dev>
device" part is omitted.
*/
static void tmxr_report_connection (TMXR *mp, TMLN *lp)
{
int32 unwritten, psave;
char cmsg[160];
char dmsg[80] = "";
char lmsg[80] = "";
char msgbuf[512] = "";
if (((!lp->notelnet) && (!lp->nomessage)) || (sim_switches & SWMASK ('V'))) {
sprintf (cmsg, "\n\r\nConnected to the %s simulator ", sim_name);
if (mp->dptr) { /* device defined? */
sprintf (dmsg, "%s device", /* report device name */
sim_dname (mp->dptr));
if (mp->lines > 1) /* more than one line? */
sprintf (lmsg, ", line %d", (int)(lp-mp->ldsc));/* report the line number */
}
sprintf (msgbuf, "%s%s%s\r\n\n", cmsg, dmsg, lmsg);
}
if (!mp->buffered) {
lp->txbpi = 0; /* init buf pointers */
lp->txbpr = (int32)(lp->txbsz - strlen (msgbuf));
lp->rxcnt = lp->txcnt = lp->txdrp = lp->txstall = 0;/* init counters */
lp->rxpcnt = lp->txpcnt = 0;
}
else
if (lp->txcnt > lp->txbsz)
lp->txbpr = (lp->txbpi + 1) % lp->txbsz;
else
lp->txbpr = (int32)(lp->txbsz - strlen (msgbuf));
psave = lp->txbpi; /* save insertion pointer */
lp->txbpi = lp->txbpr; /* insert connection message */
if ((lp->serport) && (!sim_is_running)) {
sim_os_ms_sleep (TMXR_DTR_DROP_TIME); /* Wait for DTR to be noticed */
lp->ser_connect_pending = FALSE; /* Mark line as ready for action */
lp->conn = TRUE;
}
tmxr_linemsg (lp, msgbuf); /* beginning of buffer */
lp->txbpi = psave; /* restore insertion pointer */
unwritten = tmxr_send_buffered_data (lp); /* send the message */
if ((lp->serport) && (!sim_is_running)) {
lp->ser_connect_pending = TRUE; /* Mark line as not yet ready for action */
lp->conn = FALSE;
}
if (unwritten == 0) /* buffer now empty? */
lp->xmte = 1; /* reenable transmission if paused */
lp->txcnt -= (int32)strlen (msgbuf); /* adjust statistics */
}
/* Report a disconnection to a line.
A notification of the form:
Disconnected from the <sim> simulator
is sent to the line about to be disconnected. We do not flush the buffer
here, because the disconnect routines will do that just after calling us.
*/
static void tmxr_report_disconnection (TMLN *lp)
{
if (lp->notelnet || lp->nomessage)
return;
tmxr_linemsgf (lp, "\r\nDisconnected from the %s simulator\r\n\n", sim_name);/* report disconnection */
}
static int32 loop_write_ex (TMLN *lp, char *buf, int32 length, t_bool prefix_datagram)
{
int32 written = 0;
int32 loopfree = lp->lpbsz - lp->lpbcnt;
if (lp->datagram && prefix_datagram) {
if ((size_t)loopfree < (size_t)(length + sizeof(length)))
return written;
loop_write_ex (lp, (char *)&length, sizeof(length), FALSE);
}
while (length) {
int32 chunksize;
loopfree = lp->lpbsz - lp->lpbcnt;
if (loopfree == 0)
break;
if (loopfree < length)
length = loopfree;
if (lp->lpbpi >= lp->lpbpr)
chunksize = lp->lpbsz - lp->lpbpi;
else
chunksize = lp->lpbpr - lp->lpbpi;
if (chunksize > length)
chunksize = length;
memcpy (&lp->lpb[lp->lpbpi], buf, chunksize);
buf += chunksize;
length -= chunksize;
written += chunksize;
lp->lpbpi = (lp->lpbpi + chunksize) % lp->lpbsz;
}
lp->lpbcnt += written;
return written;
}
static int32 loop_write (TMLN *lp, char *buf, int32 length)
{
return loop_write_ex (lp, buf, length, TRUE);
}
static int32 loop_read_ex (TMLN *lp, char *buf, int32 bufsize)
{
int32 bytesread = 0;
while (bufsize > 0) {
int32 chunksize;
int32 loopused = lp->lpbcnt;
if (loopused < bufsize)
bufsize = loopused;
if (loopused == 0)
break;
if (lp->lpbpi > lp->lpbpr)
chunksize = lp->lpbpi - lp->lpbpr;
else
chunksize = lp->lpbsz - lp->lpbpr;
if (chunksize > bufsize)
chunksize = bufsize;
memcpy (buf, &lp->lpb[lp->lpbpr], chunksize);
buf += chunksize;
bufsize -= chunksize;
bytesread += chunksize;
lp->lpbpr = (lp->lpbpr + chunksize) % lp->lpbsz;
}
lp->lpbcnt -= bytesread;
return bytesread;
}
static int32 loop_read (TMLN *lp, char *buf, int32 bufsize)
{
if (lp->datagram) {
int32 pktsize;
if (lp->lpbcnt < (int32)sizeof(pktsize))
return 0;
if ((sizeof(pktsize) != loop_read_ex (lp, (char *)&pktsize, sizeof(pktsize))) ||
(pktsize > bufsize))
return -1;
bufsize = pktsize;
}
return loop_read_ex (lp, buf, bufsize);
}
/* Read from a line.
Up to "length" characters are read into the character buffer associated with
line "lp". The actual number of characters read is returned. If no
characters are available, 0 is returned. If an error occurred while reading,
-1 is returned.
If a line break was detected on serial input, the associated receive break
status flag will be set. Line break indication for Telnet connections is
embedded in the Telnet protocol and must be determined externally.
*/
static int32 tmxr_read (TMLN *lp, int32 length)
{
int32 i = lp->rxbpi;
if (lp->loopback)
return loop_read (lp, &(lp->rxb[i]), length);
if (lp->serport) /* serial port connection? */
return sim_read_serial (lp->serport, &(lp->rxb[i]), length, &(lp->rbr[i]));
else {
if (lp->framer)
return tmxr_framer_read (lp, &(lp->rxb[i]), length);
else /* Telnet connection */
return sim_read_sock (lp->sock, &(lp->rxb[i]), length);
}
}
/* Write to a line.
Up to "length" characters are written from the character buffer associated
with "lp". The actual number of characters written is returned. If an error
occurred while writing, -1 is returned.
*/
static int32 tmxr_write (TMLN *lp, int32 length)
{
int32 written = 0;
int32 i = lp->txbpr;
if ((lp->txbps) && (sim_gtime () < lp->txnexttime) && (sim_is_running))
return 0;
if (lp->loopback)
return loop_write (lp, &(lp->txb[i]), length);
if (lp->serport) { /* serial port connection? */
written = sim_write_serial (lp->serport, &(lp->txb[i]), length);
}
else {
if (lp->framer)
written = tmxr_framer_write (lp, &(lp->txb[i]), length);
else {
if (lp->sock) { /* Telnet connection */
written = sim_write_sock (lp->sock, &(lp->txb[i]), length);
if (written == SOCKET_ERROR) { /* did an error occur? */
lp->txdone = TRUE;
if (lp->datagram)
return written; /* ignore errors on datagram sockets */
else
return -1; /* return error indication */
}
}
else {
if ((lp->conn == TMXR_LINE_DISABLED) ||
((lp->conn == 0) && lp->txbfd)){
written = length; /* Count here output timing is correct */
if (lp->conn == TMXR_LINE_DISABLED)
lp->txdrp += length; /* Record as having been dropped on the floor */
}
}
}
}
if (written > 0) {
lp->txdone = FALSE;
if ((lp->txbps) && (sim_is_running))
lp->txnexttime = floor (sim_gtime () + ((written * lp->txdeltausecs * sim_timer_inst_per_sec ()) / USECS_PER_SECOND));
}
return written;
}
/* Remove a character from the read buffer.
The character at position "p" in the read buffer associated with line "lp" is
removed by moving all of the following received characters down one position.
The receive break status array is adjusted accordingly.
*/
static void tmxr_rmvrc (TMLN *lp, int32 p)
{
for ( ; p < lp->rxbpi; p++) { /* work from "p" through end of buffer */
lp->rxb[p] = lp->rxb[p + 1]; /* slide following character down */
lp->rbr[p] = lp->rbr[p + 1]; /* adjust break status too */
}
lp->rbr[p] = 0; /* clear potential break from vacated slot */
lp->rxbpi = lp->rxbpi - 1; /* drop buffer insert index */
}
/* Find a line descriptor indicated by unit or number.
If "uptr" is NULL, then the line descriptor is determined by the line number
passed in "val". If "uptr" is not NULL, then it must point to a unit
associated with a line, and the line descriptor is determined by the unit
number, which is derived by the position of the unit in the device's unit
array.
Note: This routine may be called with a UNIT that does not belong to the
device indicated in the TMXR structure. That is, the multiplexer lines may
belong to a device other than the one attached to the socket (the HP 2100 MUX
device is one example). Therefore, we must look up the device from the unit
at each call, rather than depending on the DEVICE pointer stored in the TMXR.
*/
static TMLN *tmxr_find_ldsc (UNIT *uptr, int32 val, const TMXR *mp)
{
if (mp == NULL) /* invalid multiplexer descriptor? */
return NULL; /* programming error! */
if (uptr) { /* called from SET? */
DEVICE *dptr = find_dev_from_unit (uptr); /* find device */
if (dptr == NULL) /* what?? */
return NULL;
val = (int32) (uptr - dptr->units); /* implicit line # */
}
if ((val < 0) || (val >= mp->lines)) /* invalid line? */
return NULL;
return mp->ldsc + val; /* line descriptor */
}
/* Get a line descriptor indicated by a string or unit.
A pointer to the line descriptor associated with multiplexer "mp" and unit
"uptr" or specified by string "cptr" is returned. If "uptr" is non-null,
then the unit number within its associated device implies the line number.
If "uptr" is null, then the string "cptr" is parsed for a decimal line
number. If the line number is missing, malformed, or outside of the range of
line numbers associated with "mp", then NULL is returned with status set to
SCPE_ARG.
Implementation note:
1. A return status of SCPE_IERR implies a programming error (passing an
invalid pointer or an invalid unit).
*/
static TMLN *tmxr_get_ldsc (UNIT *uptr, const char *cptr, TMXR *mp, t_stat *status)
{
t_value ln;
TMLN *lp = NULL;
t_stat code = SCPE_OK;
if (mp == NULL) /* missing mux descriptor? */
code = SCPE_IERR; /* programming error! */
else if (uptr) { /* implied line form? */
lp = tmxr_find_ldsc (uptr, mp->lines, mp); /* determine line from unit */
if (lp == NULL) /* invalid line number? */
code = SCPE_IERR; /* programming error! */
}
else if (cptr == NULL) /* named line form, parameter supplied? */
code = SCPE_MISVAL; /* no, so report missing */
else {
ln = get_uint (cptr, 10, mp->lines - 1, &code); /* get line number */
if (code == SCPE_OK) /* line number OK? */
lp = mp->ldsc + (int32) ln; /* use as index to determine line */
}
if (status) /* return value pointer supplied? */
*status = code; /* store return status value */
return lp; /* return pointer to line descriptor */
}
/* Generate the Attach string which will fully configure the multiplexer
Inputs:
old = pointer to the original configuration string which will be replaced
*mp = pointer to multiplexer
Output:
a complete attach string for the current state of the multiplexer
*/
static char *growstring(char **string, size_t growth)
{
*string = (char *)realloc (*string, 1 + (*string ? strlen (*string) : 0) + growth);
return *string + strlen(*string);
}
static char *tmxr_mux_attach_string(char *old, TMXR *mp)
{
char* tptr = NULL;
int32 i;
TMLN *lp;
free (old);
tptr = (char *) calloc (1, 1);
if (tptr == NULL) /* no more mem? */
return tptr;
if (mp->port) { /* copy port */
sprintf (growstring(&tptr, 33 + strlen (mp->port)), "%s%s", mp->port,
mp->notelnet ? ";notelnet" :
(mp->nomessage ? ";nomessage" :
""));
if (mp->acl) { /* copy acl in pieces */
char gbuf[CBUFSIZE];
const char *c = mp->acl;
while (*c != '\0') {
c = get_glyph_nc (c, gbuf, ',');
sprintf (growstring(&tptr, 9 + strlen (gbuf)), ";%s=%s", (gbuf[0] == '+') ? "Accept" : "Reject", gbuf + 1);
}
}
}
if (mp->logfiletmpl[0]) /* logfile info */
sprintf (growstring(&tptr, 7 + strlen (mp->logfiletmpl)), ",Log=%s", mp->logfiletmpl);
if (mp->buffered)
sprintf (growstring(&tptr, 10 + 10), ",Buffered=%d", mp->buffered);
while ((*tptr == ',') || (*tptr == ' '))
memmove (tptr, tptr+1, strlen(tptr+1)+1);
for (i=0; i<mp->lines; ++i) {
char *lptr;
lp = mp->ldsc + i;
lptr = tmxr_line_attach_string(lp);
if (lptr) {
sprintf (growstring(&tptr, 10+strlen(lptr)), "%s%s", *tptr ? "," : "", lptr);
free (lptr);
}
}
if (mp->lines == 1)
while ((*tptr == ',') || (*tptr == ' '))
memmove (tptr, tptr+1, strlen(tptr+1)+1);
if (*tptr == '\0') {
free (tptr);
tptr = NULL;
}
return tptr;
}
/* Global routines */
/* Return the Line specific attach setup currently configured for a given line
Inputs:
*lp = pointer to terminal line descriptor
Outputs:
a string which can be used to reconfigure the line,
NULL if the line isn't configured
Note: The returned string is dynamically allocated memory and must be freed
when it is no longer needed by calling free
*/
char *tmxr_line_attach_string(TMLN *lp)
{
char* tptr = NULL;
tptr = (char *) calloc (1, 1);
if (tptr == NULL) /* no more mem? */
return tptr;
if (lp->destination || lp->port || lp->txlogname || (lp->conn == TMXR_LINE_DISABLED)) {
if ((lp->mp->lines > 1) || (lp->port))
sprintf (growstring(&tptr, 32), "Line=%d", (int)(lp-lp->mp->ldsc));
if (lp->conn == TMXR_LINE_DISABLED)
sprintf (growstring(&tptr, 32), ",Disabled");
if (lp->modem_control != lp->mp->modem_control)
sprintf (growstring(&tptr, 32), ",%s", lp->modem_control ? "Modem" : "NoModem");
if (lp->txbfd && (lp->txbsz != lp->mp->buffered))
sprintf (growstring(&tptr, 32), ",Buffered=%d", lp->txbsz);
if (!lp->txbfd && (lp->mp->buffered > 0))
sprintf (growstring(&tptr, 32), ",UnBuffered");
if (lp->mp->datagram != lp->datagram)
sprintf (growstring(&tptr, 8), ",%s", lp->datagram ? "UDP" : "TCP");
if (lp->mp->packet != lp->packet)
sprintf (growstring(&tptr, 8), ",Packet");
if (lp->port) {