-
Notifications
You must be signed in to change notification settings - Fork 0
/
sercd.c
1561 lines (1356 loc) · 43 KB
/
sercd.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
/*
sercd: RFC 2217 compliant serial port redirector
Copyright 2003-2008 Peter Åstrand <[email protected]> for Cendio AB
Copyright (C) 1999 - 2003 InfoTecna s.r.l.
Copyright (C) 2001, 2002 Trustees of Columbia University
in the City of New York
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Current design issues:
. does not properly check implement BREAK handling. Need to figure
out how to turn a BREAK on and then off based upon receipt of
COM-PORT Subnegotiations
. Lack of login processing
. Lack of Telnet START_TLS to protect the data stream
. Lack of Telnet AUTHENTICATION
. LineState processing is not implemented
. The code probably won't compile on most versions of Unix due to the
highly platform dependent nature of the serial apis.
*/
/* Return NoError, which is 0, on success */
/* Standard library includes */
#include <stdio.h> /* snprintf */
#include <stdlib.h> /* atoi */
#include <string.h> /* strlen */
#include <unistd.h> /* close */
#include <errno.h> /* errno */
#include <time.h> /* CLOCKS_PER_SEC */
#include <fcntl.h> /* open */
#include <assert.h> /* assert */
#include "sercd.h"
#include "unix.h"
#include "win.h"
/* Buffer size */
#define BufferSize 2048
/* Cisco IOS bug compatibility */
Boolean CiscoIOSCompatible = False;
/* Log to stderr instead of syslog */
Boolean StdErrLogging = False;
/* Buffer structure */
typedef struct
{
unsigned char Buffer[BufferSize];
unsigned int RdPos;
unsigned int WrPos;
}
BufferType;
/* Complete lock file pathname */
static char *LockFileName;
/* Complete device file pathname */
static char *DeviceName;
/* Device file descriptor */
static PORTHANDLE *DeviceFd = NULL;
/* Network sockets */
static SERCD_SOCKET *InSocketFd = NULL;
static SERCD_SOCKET *OutSocketFd = NULL;
/* Com Port Control enabled flag */
Boolean PortControlEnable = True;
/* Maximum log level to log in the system log */
int MaxLogLevel = LOG_DEBUG + 1;
/* Status enumeration for IAC escaping and interpretation */
typedef enum
{ IACNormal, IACReceived, IACComReceiving }
IACState;
/* Effective status for IAC escaping and interpretation */
static IACState IACEscape = IACNormal;
/* Same as above during signature reception */
static IACState IACSigEscape;
/* Current IAC command begin received */
static unsigned char IACCommand[TmpStrLen];
/* Position of insertion into IACCommand[] */
static size_t IACPos;
/* Modem state mask set by the client */
static unsigned char ModemStateMask = ((unsigned char) 255);
/* Line state mask set by the client */
static unsigned char LineStateMask = ((unsigned char) 0);
#ifdef COMMENT
/* Current status of the line control lines */
static unsigned char LineState = ((unsigned char) 0);
#endif
/* Current status of the modem control lines */
static unsigned char ModemState = ((unsigned char) 0);
/* Break state flag */
Boolean BreakSignaled = False;
/* Input flow control flag */
Boolean InputFlow = True;
/* Telnet State Machine */
static struct _tnstate
{
int sent_will:1;
int sent_do:1;
int sent_wont:1;
int sent_dont:1;
int is_will:1;
int is_do:1;
}
tnstate[256];
/* Function prototypes */
/* initialize Telnet State Machine */
void InitTelnetStateMachine(void);
/* Initialize a buffer for operation */
void InitBuffer(BufferType * B);
/* Check if the buffer is empty */
Boolean IsBufferEmpty(BufferType * B);
/* Add a byte to a buffer */
void AddToBuffer(BufferType * B, unsigned char C);
/* Get a byte from a buffer */
unsigned char GetFromBuffer(BufferType * B);
/* Retrieves the port speed from PortFd */
unsigned long int GetPortSpeed(PORTHANDLE PortFd);
/* Retrieves the data size from PortFd */
unsigned char GetPortDataSize(PORTHANDLE PortFd);
/* Retrieves the parity settings from PortFd */
unsigned char GetPortParity(PORTHANDLE PortFd);
/* Retrieves the stop bits size from PortFd */
unsigned char GetPortStopSize(PORTHANDLE PortFd);
/* Retrieves the flow control status, including DTR and RTS status,
from PortFd */
unsigned char GetPortFlowControl(PORTHANDLE PortFd, unsigned char Which);
/* Return the status of the modem control lines (DCD, CTS, DSR, RNG) */
unsigned char GetModemState(PORTHANDLE PortFd, unsigned char PMState);
/* Set the serial port data size */
void SetPortDataSize(PORTHANDLE PortFd, unsigned char DataSize);
/* Set the serial port parity */
void SetPortParity(PORTHANDLE PortFd, unsigned char Parity);
/* Set the serial port stop bits size */
void SetPortStopSize(PORTHANDLE PortFd, unsigned char StopSize);
/* Set the port flow control and DTR and RTS status */
void SetPortFlowControl(PORTHANDLE PortFd, unsigned char How);
/* Set the serial port speed */
void SetPortSpeed(PORTHANDLE PortFd, unsigned long BaudRate);
/* Serial port break */
void SetBreak(PORTHANDLE PortFd, Boolean on);
/* Flush serial port */
void SetFlush(PORTHANDLE PortFd, int selector);
/* Init platform subsystems, such as the syslog */
void PlatformInit();
/* Initialize port */
int OpenPort(const char *DeviceName, const char *LockFileName, PORTHANDLE * PortFd);
/* Close and uninit port */
void ClosePort(PORTHANDLE PortFd, const char *LockFileName);
/* Send the signature Sig to the client */
void SendSignature(BufferType * B, char *Sig);
/* Write a char to SockFd performing IAC escaping */
void EscWriteChar(BufferType * B, unsigned char C);
/* Redirect char C to PortFd checking for IAC escape sequences */
void EscRedirectChar(BufferType * SockB, BufferType * DevB, PORTHANDLE PortFd, unsigned char C);
/* Send the specific telnet option to SockFd using Command as command */
void SendTelnetOption(BufferType * B, unsigned char Command, char Option);
/* Send a string to SockFd performing IAC escaping */
void SendStr(BufferType * B, char *Str);
/* Send the baud rate BR to SockFd */
void SendBaudRate(BufferType * B, unsigned long int BR);
/* Send the CPC command Command using Parm as parameter */
void SendCPCByteCommand(BufferType * B, unsigned char Command, unsigned char Parm);
/* Handling of COM Port Control specific commands */
void HandleCPCCommand(BufferType * B, PORTHANDLE PortFd, unsigned char *Command, size_t CSize);
/* Common telnet IAC commands handling */
void HandleIACCommand(BufferType * B, PORTHANDLE PortFd, unsigned char *Command, size_t CSize);
/* Write a buffer to SockFd with IAC escaping */
void EscWriteBuffer(BufferType * B, unsigned char *Buffer, unsigned int BSize);
/* initialize Telnet State Machine */
void
InitTelnetStateMachine(void)
{
int i;
for (i = 0; i < 256; i++) {
tnstate[i].sent_do = 0;
tnstate[i].sent_will = 0;
tnstate[i].sent_wont = 0;
tnstate[i].sent_dont = 0;
tnstate[i].is_do = 0;
tnstate[i].is_will = 0;
}
}
/* Setup sockets for low latency and automatic keepalive; doesn't
* check if anything fails because failure doesn't prevent correct
* functioning but only provides slightly worse behaviour
*/
void
SetSocketOptions(SERCD_SOCKET insocket, SERCD_SOCKET outsocket)
{
/* Socket setup flag */
int SockParmEnable = 1;
setsockopt(insocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &SockParmEnable,
sizeof(SockParmEnable));
setsockopt(insocket, SOL_SOCKET, SO_OOBINLINE, (char *) &SockParmEnable,
sizeof(SockParmEnable));
setsockopt(outsocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &SockParmEnable,
sizeof(SockParmEnable));
#ifndef WIN32
/* Generic socket parameter */
int SockParm;
SockParm = IPTOS_LOWDELAY;
setsockopt(insocket, SOL_IP, IP_TOS, &SockParm, sizeof(SockParm));
setsockopt(outsocket, SOL_IP, IP_TOS, &SockParm, sizeof(SockParm));
/* Make reads/writes non-blocking. In principle, non-blocking IO
is not necessary, since we are using select. However, the Linux
select man page BUGS section contains: "Under Linux, select()
may report a socket file descriptor as "ready for reading",
while nevertheless a subsequent read blocks." Besides, we need
to use non-blocking for the serial port anyway, and using
non-blocking means that we don't need to check the send buffer
size. */
ioctl(outsocket, FIONBIO, &SockParmEnable);
ioctl(insocket, FIONBIO, &SockParmEnable);
#endif
}
/* Initialize a buffer for operation */
void
InitBuffer(BufferType * B)
{
/* Set the initial buffer positions */
B->RdPos = 0;
B->WrPos = 0;
}
/* Return the length of the data in the buffer */
unsigned int
BufferLength(BufferType * B)
{
return (B->WrPos - B->RdPos + BufferSize) % BufferSize;
}
/* Return how much room is left */
unsigned int
BufferRoomLeft(BufferType * B)
{
/* -1 is for full/empty distinction */
return BufferSize - 1 - BufferLength(B);
}
/* Check if there's room for a number of additional bytes */
Boolean
BufferHasRoomFor(BufferType * B, unsigned int x)
{
return BufferRoomLeft(B) >= x;
}
/* Check if the buffer is empty */
Boolean
IsBufferEmpty(BufferType * B)
{
return BufferLength(B) == 0;
}
/* Add a byte to a buffer. */
void
AddToBuffer(BufferType * B, unsigned char C)
{
assert(BufferHasRoomFor(B, 1));
B->Buffer[B->WrPos] = C;
B->WrPos = (B->WrPos + 1) % BufferSize;
}
/* Get a byte from a buffer */
unsigned char
GetFromBuffer(BufferType * B)
{
unsigned char C = B->Buffer[B->RdPos];
B->RdPos = (B->RdPos + 1) % BufferSize;
return (C);
}
/* Get string from buffer, without removing it. Returns the length of
the string. */
unsigned char *
GetBufferString(BufferType * B, unsigned int *len)
{
if (B->RdPos <= B->WrPos)
*len = B->WrPos - B->RdPos;
else
*len = BufferSize - B->RdPos;
return &(B->Buffer[B->RdPos]);
}
/* Remove the number of read bytes specified */
void
BufferPopBytes(BufferType * B, unsigned int len)
{
B->RdPos += len;
B->RdPos %= BufferSize;
}
/* Function executed when the program exits */
void
ExitFunction(void)
{
DropConnection(DeviceFd, InSocketFd, OutSocketFd, LockFileName);
/* Program termination notification */
LogMsg(LOG_NOTICE, "sercd stopped.");
}
/* Function called on break signal */
/* Unimplemented yet */
void
BreakFunction(int unused)
{
#ifndef COMMENT
/* Just to avoid compilation warnings */
/* There's no performance penalty in doing this
because this function is almost never called */
unused = unused;
/* ExitFunction will be called through atexit */
exit(NoError);
#else /* COMMENT */
unsigned char LineState;
if (BreakSignaled == True) {
BreakSignaled = False;
LineState = 0;
}
else {
BreakSignaled = True;
LineState = 16;
}
/* Notify client of break change */
if ((LineStateMask & (unsigned char) 16) != 0) {
LogMsg(LOG_DEBUG, "Notifying break change.");
SendCPCByteCommand(&ToNetBuf, TNASC_NOTIFY_LINESTATE, LineState);
}
#endif /* COMMENT */
}
/* Send the signature Sig to the client. Sig must not be longer than
255 characters. */
#define SendSignature_bytes (6 + 2 * 255)
void
SendSignature(BufferType * B, char *Sig)
{
assert(strlen(Sig) <= 255);
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSB);
AddToBuffer(B, TNCOM_PORT_OPTION);
AddToBuffer(B, TNASC_SIGNATURE);
SendStr(B, Sig);
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSE);
}
/* Write a char to socket performing IAC escaping */
#define EscWriteChar_bytes 2
void
EscWriteChar(BufferType * B, unsigned char C)
{
/* Last received byte */
static unsigned char Last = 0;
if (C == TNIAC)
AddToBuffer(B, C);
else if (C != 0x0A && !tnstate[TN_TRANSMIT_BINARY].is_will && Last == 0x0D)
AddToBuffer(B, 0x00);
AddToBuffer(B, C);
/* Set last received byte */
Last = C;
}
/* Redirect char C to Device checking for IAC escape sequences */
#define EscRedirectChar_bytes_SockB HandleIACCommand_bytes
#define EscRedirectChar_bytes_DevB 1
void
EscRedirectChar(BufferType * SockB, BufferType * DevB, PORTHANDLE PortFd, unsigned char C)
{
/* Last received byte */
static unsigned char Last = 0;
/* Check the IAC escape status */
switch (IACEscape) {
/* Normal status */
case IACNormal:
if (C == TNIAC)
IACEscape = IACReceived;
else if (!tnstate[TN_TRANSMIT_BINARY].is_do && C == 0x00 && Last == 0x0D)
/* Swallow the NUL after a CR if not receiving BINARY */
break;
else
AddToBuffer(DevB, C);
break;
/* IAC previously received */
case IACReceived:
if (C == TNIAC) {
AddToBuffer(DevB, C);
IACEscape = IACNormal;
}
else {
IACCommand[0] = TNIAC;
IACCommand[1] = C;
IACPos = 2;
IACEscape = IACComReceiving;
IACSigEscape = IACNormal;
}
break;
/* IAC Command reception */
case IACComReceiving:
/* Telnet suboption, could be only CPC */
if (IACCommand[1] == TNSB) {
/* Get the suboption signature */
if (IACPos < 4) {
IACCommand[IACPos] = C;
IACPos++;
}
else {
/* Check which suboption we are dealing with */
switch (IACCommand[3]) {
/* Signature, which needs further escaping */
case TNCAS_SIGNATURE:
switch (IACSigEscape) {
case IACNormal:
if (C == TNIAC)
IACSigEscape = IACReceived;
else if (IACPos < sizeof(IACCommand)) {
IACCommand[IACPos] = C;
IACPos++;
}
break;
case IACComReceiving:
IACSigEscape = IACNormal;
break;
case IACReceived:
if (C == TNIAC) {
if (IACPos < sizeof(IACCommand)) {
IACCommand[IACPos] = C;
IACPos++;
}
IACSigEscape = IACNormal;
}
else {
if (IACPos < sizeof(IACCommand)) {
IACCommand[IACPos] = TNIAC;
IACPos++;
}
if (IACPos < sizeof(IACCommand)) {
IACCommand[IACPos] = C;
IACPos++;
}
HandleIACCommand(SockB, PortFd, IACCommand, IACPos);
IACEscape = IACNormal;
}
break;
}
break;
/* Set baudrate */
case TNCAS_SET_BAUDRATE:
IACCommand[IACPos] = C;
IACPos++;
if (IACPos == 10) {
HandleIACCommand(SockB, PortFd, IACCommand, IACPos);
IACEscape = IACNormal;
}
break;
/* Flow control command */
case TNCAS_FLOWCONTROL_SUSPEND:
case TNCAS_FLOWCONTROL_RESUME:
IACCommand[IACPos] = C;
IACPos++;
if (IACPos == 6) {
HandleIACCommand(SockB, PortFd, IACCommand, IACPos);
IACEscape = IACNormal;
}
break;
/* Normal CPC command with single byte parameter */
default:
IACCommand[IACPos] = C;
IACPos++;
if (IACPos == 7) {
HandleIACCommand(SockB, PortFd, IACCommand, IACPos);
IACEscape = IACNormal;
}
break;
}
}
}
else {
/* Normal 3 byte IAC option */
IACCommand[IACPos] = C;
IACPos++;
if (IACPos == 3) {
HandleIACCommand(SockB, PortFd, IACCommand, IACPos);
IACEscape = IACNormal;
}
}
break;
}
/* Set last received byte */
Last = C;
}
/* Send the specific telnet option to SockFd using Command as command */
#define SendTelnetOption_bytes 3
void
SendTelnetOption(BufferType * B, unsigned char Command, char Option)
{
unsigned char IAC = TNIAC;
AddToBuffer(B, IAC);
AddToBuffer(B, Command);
AddToBuffer(B, Option);
}
/* Send initial Telnet negotiations to the client */
#define SendTelnetInitialOptions_bytes (SendTelnetOption_bytes*3)
void
SendTelnetInitialOptions(BufferType * B)
{
SendTelnetOption(B, TNWILL, TN_TRANSMIT_BINARY);
tnstate[TN_TRANSMIT_BINARY].sent_will = 1;
SendTelnetOption(B, TNDO, TN_TRANSMIT_BINARY);
tnstate[TN_TRANSMIT_BINARY].sent_do = 1;
SendTelnetOption(B, TNWILL, TN_ECHO);
tnstate[TN_ECHO].sent_will = 1;
SendTelnetOption(B, TNWILL, TN_SUPPRESS_GO_AHEAD);
tnstate[TN_SUPPRESS_GO_AHEAD].sent_will = 1;
SendTelnetOption(B, TNDO, TN_SUPPRESS_GO_AHEAD);
tnstate[TN_SUPPRESS_GO_AHEAD].sent_do = 1;
SendTelnetOption(B, TNDO, TNCOM_PORT_OPTION);
tnstate[TNCOM_PORT_OPTION].sent_do = 1;
}
/* Send a string to SockFd performing IAC escaping
Max buffer fill: 2*len(Str) */
void
SendStr(BufferType * B, char *Str)
{
size_t I;
size_t L;
L = strlen(Str);
for (I = 0; I < L; I++)
EscWriteChar(B, (unsigned char) Str[I]);
}
/* Send the baud rate BR to Buffer */
#define SendBaudRate_bytes (6 + 2*sizeof(unsigned long int))
void
SendBaudRate(BufferType * B, unsigned long int BR)
{
unsigned char *p;
unsigned long int NBR;
int i;
NBR = htonl(BR);
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSB);
AddToBuffer(B, TNCOM_PORT_OPTION);
AddToBuffer(B, TNASC_SET_BAUDRATE);
p = (unsigned char *) &NBR;
for (i = 0; i < (int) sizeof(NBR); i++)
EscWriteChar(B, p[i]);
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSE);
}
/* Send the CPC command Command using Parm as parameter */
#define SendCPCByteCommand_bytes 8
void
SendCPCByteCommand(BufferType * B, unsigned char Command, unsigned char Parm)
{
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSB);
AddToBuffer(B, TNCOM_PORT_OPTION);
AddToBuffer(B, Command);
EscWriteChar(B, Parm);
AddToBuffer(B, TNIAC);
AddToBuffer(B, TNSE);
}
/* Handling of COM Port Control specific commands */
#define HandleCPCCommand_bytes \
MAX(SendSignature_bytes, MAX(SendBaudRate_bytes, SendCPCByteCommand_bytes))
void
HandleCPCCommand(BufferType * SockB, PORTHANDLE PortFd, unsigned char *Command, size_t CSize)
{
char LogStr[TmpStrLen];
char SigStr[255];
unsigned long int BaudRate;
unsigned char DataSize;
unsigned char Parity;
unsigned char StopSize;
unsigned char FlowControl;
/* Check wich command has been requested */
switch (Command[3]) {
/* Signature */
case TNCAS_SIGNATURE:
if (CSize == 6) {
/* Void signature, client is asking for our signature */
snprintf(SigStr, sizeof(SigStr), "sercd %s %s", VERSION, DeviceName);
LogStr[sizeof(SigStr) - 1] = '\0';
SendSignature(SockB, SigStr);
snprintf(LogStr, sizeof(LogStr), "Sent signature: %s", SigStr);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_INFO, LogStr);
}
else {
/* Received client signature */
strncpy(SigStr, (char *) &Command[4], MAX(CSize - 6, sizeof(SigStr) - 1));
snprintf(LogStr, sizeof(LogStr) - 1, "Received client signature: %s", SigStr);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_INFO, LogStr);
}
break;
/* Set serial baud rate */
case TNCAS_SET_BAUDRATE:
/* Retrieve the baud rate which is in network order */
BaudRate = ntohl(*((unsigned long int *) &Command[4]));
if (BaudRate == 0)
/* Client is asking for current baud rate */
LogMsg(LOG_DEBUG, "Baud rate notification received.");
else {
/* Change the baud rate */
snprintf(LogStr, sizeof(LogStr), "Port baud rate change to %lu requested.", BaudRate);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetPortSpeed(PortFd, BaudRate);
}
/* Send confirmation */
BaudRate = GetPortSpeed(PortFd);
SendBaudRate(SockB, BaudRate);
snprintf(LogStr, sizeof(LogStr), "Port baud rate: %lu", BaudRate);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
/* Set serial data size */
case TNCAS_SET_DATASIZE:
if (Command[4] == 0)
/* Client is asking for current data size */
LogMsg(LOG_DEBUG, "Data size notification requested.");
else {
/* Set the data size */
snprintf(LogStr, sizeof(LogStr),
"Port data size change to %u requested.", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetPortDataSize(PortFd, Command[4]);
}
/* Send confirmation */
DataSize = GetPortDataSize(PortFd);
SendCPCByteCommand(SockB, TNASC_SET_DATASIZE, DataSize);
snprintf(LogStr, sizeof(LogStr), "Port data size: %u", (unsigned int) DataSize);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
/* Set the serial parity */
case TNCAS_SET_PARITY:
if (Command[4] == 0)
/* Client is asking for current parity */
LogMsg(LOG_DEBUG, "Parity notification requested.");
else {
/* Set the parity */
snprintf(LogStr, sizeof(LogStr),
"Port parity change to %u requested", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetPortParity(PortFd, Command[4]);
}
/* Send confirmation */
Parity = GetPortParity(PortFd);
SendCPCByteCommand(SockB, TNASC_SET_PARITY, Parity);
snprintf(LogStr, sizeof(LogStr), "Port parity: %u", (unsigned int) Parity);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
/* Set the serial stop size */
case TNCAS_SET_STOPSIZE:
if (Command[4] == 0)
/* Client is asking for current stop size */
LogMsg(LOG_DEBUG, "Stop size notification requested.");
else {
/* Set the stop size */
snprintf(LogStr, sizeof(LogStr),
"Port stop size change to %u requested.", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetPortStopSize(PortFd, Command[4]);
}
/* Send confirmation */
StopSize = GetPortStopSize(PortFd);
SendCPCByteCommand(SockB, TNASC_SET_STOPSIZE, StopSize);
snprintf(LogStr, sizeof(LogStr), "Port stop size: %u", (unsigned int) StopSize);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
/* Flow control and DTR/RTS handling */
case TNCAS_SET_CONTROL:
switch (Command[4]) {
case TNCOM_CMD_FLOW_REQ:
case TNCOM_CMD_DTR_REQ:
case TNCOM_CMD_RTS_REQ:
case TNCOM_CMD_INFLOW_REQ:
/* Client is asking for current flow control or DTR/RTS status */
LogMsg(LOG_DEBUG, "Flow control notification requested.");
FlowControl = GetPortFlowControl(PortFd, Command[4]);
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, FlowControl);
snprintf(LogStr, sizeof(LogStr), "Port flow control: %u", (unsigned int) FlowControl);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
case TNCOM_CMD_BREAK_REQ:
if (BreakSignaled) {
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, TNCOM_CMD_BREAK_ON);
}
else {
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, TNCOM_CMD_BREAK_OFF);
}
break;
case TNCOM_CMD_BREAK_ON:
/* Break command */
SetBreak(PortFd, True);
BreakSignaled = True;
LogMsg(LOG_DEBUG, "Break Signal ON.");
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, TNCOM_CMD_BREAK_ON);
break;
case TNCOM_CMD_BREAK_OFF:
SetBreak(PortFd, False);
BreakSignaled = False;
LogMsg(LOG_DEBUG, "Break Signal OFF.");
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, TNCOM_CMD_BREAK_OFF);
break;
default:
/* Set the flow control */
snprintf(LogStr, sizeof(LogStr),
"Port flow control change to %u requested.", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetPortFlowControl(PortFd, Command[4]);
/* Flow control status confirmation */
if (CiscoIOSCompatible && Command[4] >= TNCOM_CMD_INFLOW_REQ
&& Command[4] <= TNCOM_CMD_INFLOW_HARDWARE)
/* INBOUND not supported separately.
Following the behavior of Cisco ISO 11.3
*/
FlowControl = 0;
else
/* Return the actual port flow control settings */
FlowControl = GetPortFlowControl(PortFd, TNCOM_CMD_FLOW_REQ);
SendCPCByteCommand(SockB, TNASC_SET_CONTROL, FlowControl);
snprintf(LogStr, sizeof(LogStr), "Port flow control: %u", (unsigned int) FlowControl);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
}
break;
/* Set the line state mask */
case TNCAS_SET_LINESTATE_MASK:
snprintf(LogStr, sizeof(LogStr), "Line state set to %u", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
/* Only break notification supported */
LineStateMask = Command[4] & (unsigned char) 16;
SendCPCByteCommand(SockB, TNASC_SET_LINESTATE_MASK, LineStateMask);
break;
/* Set the modem state mask */
case TNCAS_SET_MODEMSTATE_MASK:
snprintf(LogStr, sizeof(LogStr), "Modem state mask set to %u", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
ModemStateMask = Command[4];
SendCPCByteCommand(SockB, TNASC_SET_MODEMSTATE_MASK, ModemStateMask);
break;
/* Port flush requested */
case TNCAS_PURGE_DATA:
snprintf(LogStr, sizeof(LogStr), "Port flush %u requested.", (unsigned int) Command[4]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SetFlush(PortFd, Command[4]);
SendCPCByteCommand(SockB, TNASC_PURGE_DATA, Command[4]);
break;
/* Suspend output to the client */
case TNCAS_FLOWCONTROL_SUSPEND:
LogMsg(LOG_DEBUG, "Flow control suspend requested.");
InputFlow = False;
break;
/* Resume output to the client */
case TNCAS_FLOWCONTROL_RESUME:
LogMsg(LOG_DEBUG, "Flow control resume requested.");
InputFlow = True;
break;
/* Unknown request */
default:
snprintf(LogStr, sizeof(LogStr), "Unhandled request %u", (unsigned int) Command[3]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
}
}
/* Common telnet IAC commands handling */
#define HandleIACCommand_bytes MAX(HandleCPCCommand_bytes, SendTelnetOption_bytes)
void
HandleIACCommand(BufferType * SockB, PORTHANDLE PortFd, unsigned char *Command, size_t CSize)
{
char LogStr[TmpStrLen];
/* Check which command */
switch (Command[1]) {
/* Suboptions */
case TNSB:
if (!(tnstate[Command[2]].is_will || tnstate[Command[2]].is_do))
break;
switch (Command[2]) {
/* RFC 2217 COM Port Control Protocol option */
case TNCOM_PORT_OPTION:
HandleCPCCommand(SockB, PortFd, Command, CSize);
break;
default:
snprintf(LogStr, sizeof(LogStr), "Unknown suboption received: %u",
(unsigned int) Command[2]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
break;
}
break;
/* Requests for options */
case TNWILL:
switch (Command[2]) {
/* COM Port Control Option */
case TNCOM_PORT_OPTION:
LogMsg(LOG_INFO, "Telnet COM Port Control Enabled (WILL).");
PortControlEnable = True;
if (!tnstate[Command[2]].sent_do) {
SendTelnetOption(SockB, TNDO, Command[2]);
}
tnstate[Command[2]].is_do = 1;
break;
/* Telnet Binary mode */
case TN_TRANSMIT_BINARY:
LogMsg(LOG_INFO, "Telnet Binary Transfer Enabled (WILL).");
if (!tnstate[Command[2]].sent_do)
SendTelnetOption(SockB, TNDO, Command[2]);
tnstate[Command[2]].is_do = 1;
break;
/* Echo request not handled */
case TN_ECHO:
LogMsg(LOG_INFO, "Rejecting Telnet Echo Option (WILL).");
if (!tnstate[Command[2]].sent_do)
SendTelnetOption(SockB, TNDO, Command[2]);
tnstate[Command[2]].is_do = 1;
break;
/* No go ahead needed */
case TN_SUPPRESS_GO_AHEAD:
LogMsg(LOG_INFO, "Suppressing Go Ahead characters (WILL).");
if (!tnstate[Command[2]].sent_do)
SendTelnetOption(SockB, TNDO, Command[2]);
tnstate[Command[2]].is_do = 1;
break;
/* Reject everything else */
default:
snprintf(LogStr, sizeof(LogStr), "Rejecting option WILL: %u",
(unsigned int) Command[2]);
LogStr[sizeof(LogStr) - 1] = '\0';
LogMsg(LOG_DEBUG, LogStr);
SendTelnetOption(SockB, TNDONT, Command[2]);
tnstate[Command[2]].is_do = 0;
break;
}
tnstate[Command[2]].sent_do = 0;
tnstate[Command[2]].sent_dont = 0;
break;
/* Confirmations for options */
case TNDO:
switch (Command[2]) {
/* COM Port Control Option */
case TNCOM_PORT_OPTION: