forked from msantos/sredird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sredird.c
1875 lines (1602 loc) · 50.5 KB
/
sredird.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
/*
sredird: RFC 2217 compliant serial port redirector
Version 2.2.1, 20 February 2004
Copyright (C) 1999 - 2003 InfoTecna s.r.l.
Copyright (C) 2001, 2002 Trustees of Columbia University
in the City of New York
Copyright (C) 2020-2021 Michael Santos <michael.santos@gmail.com>
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.
*/
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <syslog.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include "restrict_process.h"
/* Version id */
#define VersionId "2.2.1"
#define SRedirdVersionId "Version " VersionId ", 20 February 2004"
/* Maximum length of temporary strings */
#define TmpStrLen 255
/* Buffer size */
#define BufferSize 2048
/* Base Telnet protocol constants (STD 8) */
#define TNSE 240
#define TNNOP 241
#define TNSB 250
#define TNWILL 251
#define TNWONT 252
#define TNDO 253
#define TNDONT 254
#define TNIAC 255
/* Base Telnet protocol options constants (STD 27, STD 28, STD 29) */
#define TN_TRANSMIT_BINARY 0
#define TN_ECHO 1
#define TN_SUPPRESS_GO_AHEAD 3
/* Base Telnet Com Port Control (CPC) protocol constants (RFC 2217) */
#define TNCOM_PORT_OPTION 44
/* CPC Client to Access Server constants */
#define TNCAS_SIGNATURE 0
#define TNCAS_SET_BAUDRATE 1
#define TNCAS_SET_DATASIZE 2
#define TNCAS_SET_PARITY 3
#define TNCAS_SET_STOPSIZE 4
#define TNCAS_SET_CONTROL 5
#define TNCAS_NOTIFY_LINESTATE 6
#define TNCAS_NOTIFY_MODEMSTATE 7
#define TNCAS_FLOWCONTROL_SUSPEND 8
#define TNCAS_FLOWCONTROL_RESUME 9
#define TNCAS_SET_LINESTATE_MASK 10
#define TNCAS_SET_MODEMSTATE_MASK 11
#define TNCAS_PURGE_DATA 12
/* CPC Access Server to Client constants */
#define TNASC_SIGNATURE 100
#define TNASC_SET_BAUDRATE 101
#define TNASC_SET_DATASIZE 102
#define TNASC_SET_PARITY 103
#define TNASC_SET_STOPSIZE 104
#define TNASC_SET_CONTROL 105
#define TNASC_NOTIFY_LINESTATE 106
#define TNASC_NOTIFY_MODEMSTATE 107
#define TNASC_FLOWCONTROL_SUSPEND 108
#define TNASC_FLOWCONTROL_RESUME 109
#define TNASC_SET_LINESTATE_MASK 110
#define TNASC_SET_MODEMSTATE_MASK 111
#define TNASC_PURGE_DATA 112
/* Modem state effective change mask */
#define ModemStateECMask 255
#define LineStateECMask 255
/* Default modem state polling in milliseconds (100 msec should be enough) */
#define ModemStatePolling 100
#define COUNT(_array) (sizeof(_array) / sizeof(_array[0]))
#define DEVICE_FILENO 2
/* Standard boolean definition */
typedef enum { False, True } Boolean;
/* Cisco IOS bug compatibility */
static Boolean CiscoIOSCompatible = False;
/* Buffer structure */
typedef struct {
unsigned char Buffer[BufferSize];
unsigned int RdPos;
unsigned int WrPos;
} BufferType;
/* Complete device file pathname */
static const char *DeviceName;
/* Device file descriptor */
static int DeviceFd = -1;
/* Com Port Control enabled flag */
static Boolean TCPCEnabled = False;
/* True after retrieving the initial settings from the serial port */
static Boolean InitPortRetrieved = False;
/* Initial serial port settings */
static struct termios InitialPortSettings;
/* Maximum log level to log in the system log */
static 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 = 255;
/* Line state mask set by the client */
static unsigned char LineStateMask = 0;
#ifdef COMMENT
/* Current status of the line control lines */
static unsigned char LineState = 0;
#endif
/* Current status of the modem control lines */
static unsigned char ModemState = 0;
/* Break state flag */
static Boolean BreakSignaled = False;
/* Input flow control flag */
static Boolean InputFlow = True;
/* Telnet State Machine */
static struct _tnstate {
unsigned int sent_will : 1;
unsigned int sent_do : 1;
unsigned int sent_wont : 1;
unsigned int sent_dont : 1;
unsigned int is_will : 1;
unsigned 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 */
static Boolean IsBufferEmpty(BufferType *B);
/* Check if the buffer is full */
static Boolean IsBufferFull(BufferType *B);
/* Add a byte to a buffer */
void AddToBuffer(BufferType *B, unsigned char C);
/* Push a byte to a buffer */
void PushToBuffer(BufferType *B, unsigned char C);
/* Get a byte from a buffer */
unsigned char GetFromBuffer(BufferType *B);
/* Generic log function with log level control. Uses the same log levels
of the syslog(3) system call */
void LogMsg(int LogLevel, const char *const fmt, ...);
/* Function executed when the program exits */
static noreturn void ExitFunction(void);
/* Function called on many signals */
static noreturn void SignalFunction(int unused);
/* Function called on break signal */
static noreturn void BreakFunction(int unused);
/* Retrieves the port speed from PortFd */
unsigned long int GetPortSpeed(int PortFd);
/* Retrieves the data size from PortFd */
unsigned char GetPortDataSize(int PortFd);
/* Retrieves the parity settings from PortFd */
unsigned char GetPortParity(int PortFd);
/* Retrieves the stop bits size from PortFd */
unsigned char GetPortStopSize(int PortFd);
/* Retrieves the flow control status, including DTR and RTS status,
from PortFd */
unsigned char GetPortFlowControl(int PortFd, unsigned char Which);
/* Return the status of the modem control lines (DCD, CTS, DSR, RNG) */
unsigned char GetModemState(int PortFd, unsigned char PMState);
/* Set the serial port data size */
void SetPortDataSize(int PortFd, unsigned char DataSize);
/* Set the serial port parity */
void SetPortParity(int PortFd, unsigned char Parity);
/* Set the serial port stop bits size */
void SetPortStopSize(int PortFd, unsigned char StopSize);
/* Set the port flow control and DTR and RTS status */
void SetPortFlowControl(int PortFd, unsigned char How);
/* Set the serial port speed */
void SetPortSpeed(int PortFd, unsigned long BaudRate);
/* 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, int 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 flow control command Command */
void SendCPCFlowCommand(BufferType *B, unsigned char Command);
/* 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, int PortFd, unsigned char *Command,
size_t CSize);
/* Common telnet IAC commands handling */
void HandleIACCommand(BufferType *B, int 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);
/* Usage */
void Usage(void);
static const struct option long_options[] = {
{"timeout", required_argument, NULL, 't'},
{"cisco-compatibility", no_argument, NULL, 'i'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}};
/* 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;
}
}
/* Initialize a buffer for operation */
void InitBuffer(BufferType *B) {
/* Set the initial buffer positions */
B->RdPos = 0;
B->WrPos = 0;
}
/* Check if the buffer is empty */
Boolean IsBufferEmpty(BufferType *B) { return ((Boolean)B->RdPos == B->WrPos); }
/* Check if the buffer is full */
Boolean IsBufferFull(BufferType *B) {
/* We consider the buffer to be filled when there are 100 bytes left
This is so even a full buffer can safely have escaped characters
added to it.
*/
return ((Boolean)B->WrPos == (B->RdPos + BufferSize - 101) % BufferSize);
}
/* Add a byte to a buffer */
void AddToBuffer(BufferType *B, unsigned char C) {
B->Buffer[B->WrPos] = C;
B->WrPos = (B->WrPos + 1) % BufferSize;
}
void PushToBuffer(BufferType *B, unsigned char C) {
if (B->RdPos > 0)
B->RdPos--;
else
B->RdPos = BufferSize - 1;
B->Buffer[B->RdPos] = C;
}
/* 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;
}
/* Generic log function with log level control. Uses the same log levels
of the syslog(3) system call */
void LogMsg(int LogLevel, const char *const fmt, ...) {
va_list ap;
if (LogLevel <= MaxLogLevel) {
(void)fprintf(stderr, "%s: ", DeviceName);
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
}
}
/* Function executed when the program exits */
static noreturn void ExitFunction(void) {
const char *message = "SRedird stopped.\n";
/* Restores initial port settings */
if (DeviceFd > -1) {
if (InitPortRetrieved == True)
tcsetattr(DeviceFd, TCSANOW, &InitialPortSettings);
}
/* Program termination notification */
if (MaxLogLevel >= LOG_NOTICE) {
/* warning: ignoring return value of ‘write’, declared with attribute
* warn_unused_result [-Wunused-result] */
if (write(STDERR_FILENO, message, strlen(message)) == -1) {
}
}
_exit(0);
}
/* Function called on many signals */
static noreturn void SignalFunction(int unused) {
(void)unused;
/* Same as the exit function */
ExitFunction();
}
/* Function called on break signal */
/* Unimplemented yet */
static noreturn void BreakFunction(int unused) {
#ifndef COMMENT
(void)unused;
/* Same as the exit function */
ExitFunction();
#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 */
}
/* Retrieves the port speed from PortFd */
unsigned long int GetPortSpeed(int PortFd) {
struct termios PortSettings;
speed_t Speed;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
Speed = cfgetospeed(&PortSettings);
switch (Speed) {
case B50:
return 50UL;
case B75:
return 75UL;
case B110:
return 110UL;
case B134:
return 134UL;
case B150:
return 150UL;
case B200:
return 200UL;
case B300:
return 300UL;
case B600:
return 600UL;
case B1200:
return 1200UL;
case B1800:
return 1800UL;
case B2400:
return 2400UL;
case B4800:
return 4800UL;
case B9600:
return 9600UL;
case B19200:
return 19200UL;
case B38400:
return 38400UL;
case B57600:
return 57600UL;
case B115200:
return 115200UL;
case B230400:
return 230400UL;
#ifdef B460800
case B460800:
return 460800UL;
#endif
default:
return 0UL;
}
}
/* Retrieves the data size from PortFd */
unsigned char GetPortDataSize(int PortFd) {
struct termios PortSettings;
tcflag_t DataSize;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgettattr");
DataSize = PortSettings.c_cflag & CSIZE;
switch (DataSize) {
case CS5:
return 5;
case CS6:
return 6;
case CS7:
return 7;
case CS8:
return 8;
default:
return 0;
}
}
/* Retrieves the parity settings from PortFd */
unsigned char GetPortParity(int PortFd) {
struct termios PortSettings;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
if ((PortSettings.c_cflag & PARENB) == 0)
return 1;
if ((PortSettings.c_cflag & PARENB) != 0 &&
(PortSettings.c_cflag & PARODD) != 0)
return 2;
return 3;
}
/* Retrieves the stop bits size from PortFd */
unsigned char GetPortStopSize(int PortFd) {
struct termios PortSettings;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
if ((PortSettings.c_cflag & CSTOPB) == 0)
return 1;
return 2;
}
/* Retrieves the flow control status, including DTR and RTS status,
from PortFd */
unsigned char GetPortFlowControl(int PortFd, unsigned char Which) {
struct termios PortSettings;
int MLines;
/* Gets the basic informations from the port */
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
if (ioctl(PortFd, TIOCMGET, &MLines) < 0)
err(EXIT_FAILURE, "ioctl(TIOCMGET)");
/* Check which kind of information is requested */
switch (Which) {
/* Com Port Flow Control Setting (outbound/both) */
case 0:
if (PortSettings.c_iflag & IXON)
return 2;
if (PortSettings.c_cflag & CRTSCTS)
return 3;
return 1;
/* BREAK State */
case 4:
if (BreakSignaled == True)
return 5;
return 6;
/* DTR Signal State */
case 7:
if (MLines & TIOCM_DTR)
return 8;
return 9;
/* RTS Signal State */
case 10:
if (MLines & TIOCM_RTS)
return 11;
return 12;
/* Com Port Flow Control Setting (inbound) */
case 13:
if (PortSettings.c_iflag & IXOFF)
return 15;
if (PortSettings.c_cflag & CRTSCTS)
return 16;
return 14;
default:
if (PortSettings.c_iflag & IXON)
return 2;
if (PortSettings.c_cflag & CRTSCTS)
return 3;
return 1;
}
}
/* Return the status of the modem control lines (DCD, CTS, DSR, RNG) */
unsigned char GetModemState(int PortFd, unsigned char PMState) {
int MLines;
unsigned char MState = 0;
if (ioctl(PortFd, TIOCMGET, &MLines) < 0)
err(EXIT_FAILURE, "ioctl(TIOCMGET)");
if ((MLines & TIOCM_CAR) != 0)
MState += 128;
if ((MLines & TIOCM_RNG) != 0)
MState += 64;
if ((MLines & TIOCM_DSR) != 0)
MState += 32;
if ((MLines & TIOCM_CTS) != 0)
MState += 16;
if ((MState & 128) != (PMState & 128))
MState += 8;
if ((MState & 64) != (PMState & 64))
MState += 4;
if ((MState & 32) != (PMState & 32))
MState += 2;
if ((MState & 16) != (PMState & 16))
MState += 1;
return MState;
}
/* Set the serial port data size */
void SetPortDataSize(int PortFd, unsigned char DataSize) {
struct termios PortSettings;
tcflag_t PDataSize;
switch (DataSize) {
case 5:
PDataSize = CS5;
break;
case 6:
PDataSize = CS6;
break;
case 7:
PDataSize = CS7;
break;
case 8:
PDataSize = CS8;
break;
default:
PDataSize = CS8;
break;
}
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
PortSettings.c_cflag &= ~CSIZE;
PortSettings.c_cflag |= PDataSize & CSIZE;
if (tcsetattr(PortFd, TCSADRAIN, &PortSettings) < 0)
err(EXIT_FAILURE, "tcsetattr");
}
/* Set the serial port parity */
void SetPortParity(int PortFd, unsigned char Parity) {
struct termios PortSettings;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
switch (Parity) {
case 1:
PortSettings.c_cflag = PortSettings.c_cflag & ~PARENB;
break;
case 2:
PortSettings.c_cflag = PortSettings.c_cflag | PARENB | PARODD;
break;
case 3:
PortSettings.c_cflag = (PortSettings.c_cflag | PARENB) & ~PARODD;
break;
/* There's no support for MARK and SPACE parity so sets no parity */
default:
LogMsg(LOG_WARNING, "Requested unsupported parity, set to no parity.");
PortSettings.c_cflag = PortSettings.c_cflag & ~PARENB;
break;
}
if (tcsetattr(PortFd, TCSADRAIN, &PortSettings) < 0)
err(EXIT_FAILURE, "tcsetattr");
}
/* Set the serial port stop bits size */
void SetPortStopSize(int PortFd, unsigned char StopSize) {
struct termios PortSettings;
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
switch (StopSize) {
case 1:
PortSettings.c_cflag = PortSettings.c_cflag & ~CSTOPB;
break;
case 2:
PortSettings.c_cflag = PortSettings.c_cflag | CSTOPB;
break;
case 3:
PortSettings.c_cflag = PortSettings.c_cflag & ~CSTOPB;
LogMsg(LOG_WARNING,
"Requested unsupported 1.5 bits stop size, set to 1 bit stop size.");
break;
default:
PortSettings.c_cflag = PortSettings.c_cflag & ~CSTOPB;
break;
}
if (tcsetattr(PortFd, TCSADRAIN, &PortSettings) < 0)
err(EXIT_FAILURE, "tcsetattr");
}
/* Set the port flow control and DTR and RTS status */
void SetPortFlowControl(int PortFd, unsigned char How) {
struct termios PortSettings;
int MLines;
/* Gets the base status from the port */
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
if (ioctl(PortFd, TIOCMGET, &MLines) < 0)
err(EXIT_FAILURE, "ioctl(TIOCMGET)");
/* Check which settings to change */
switch (How) {
/* No Flow Control (outbound/both) */
case 1:
PortSettings.c_iflag = PortSettings.c_iflag & ~IXON;
PortSettings.c_iflag = PortSettings.c_iflag & ~IXOFF;
PortSettings.c_cflag = PortSettings.c_cflag & ~CRTSCTS;
break;
/* XON/XOFF Flow Control (outbound/both) */
case 2:
PortSettings.c_iflag = PortSettings.c_iflag | IXON;
PortSettings.c_iflag = PortSettings.c_iflag | IXOFF;
PortSettings.c_cflag = PortSettings.c_cflag & ~CRTSCTS;
break;
/* HARDWARE Flow Control (outbound/both) */
case 3:
PortSettings.c_iflag = PortSettings.c_iflag & ~IXON;
PortSettings.c_iflag = PortSettings.c_iflag & ~IXOFF;
PortSettings.c_cflag = PortSettings.c_cflag | CRTSCTS;
break;
/* BREAK State ON */
case 5:
if (tcsendbreak(PortFd, 1) < 0)
err(EXIT_FAILURE, "tcsendbreak");
BreakSignaled = True;
break;
/* BREAK State OFF */
case 6:
/* Should not send another break */
/* tcsendbreak(PortFd,0); */
BreakSignaled = False;
break;
/* DTR Signal State ON */
case 8:
MLines = MLines | TIOCM_DTR;
break;
/* DTR Signal State OFF */
case 9:
MLines = MLines & ~TIOCM_DTR;
break;
/* RTS Signal State ON */
case 11:
MLines = MLines | TIOCM_RTS;
break;
/* RTS Signal State OFF */
case 12:
MLines = MLines & ~TIOCM_RTS;
break;
/* INBOUND FLOW CONTROL is ignored */
/* No Flow Control (inbound) */
case 14:
/* XON/XOFF Flow Control (inbound) */
case 15:
/* HARDWARE Flow Control (inbound) */
case 16:
LogMsg(LOG_WARNING, "Inbound flow control ignored.");
break;
default:
LogMsg(LOG_WARNING, "Requested unsupported flow control.");
break;
}
if (tcsetattr(PortFd, TCSADRAIN, &PortSettings) < 0)
err(EXIT_FAILURE, "tcsetattr");
if (ioctl(PortFd, TIOCMSET, &MLines) < 0)
err(EXIT_FAILURE, "ioctl(TIOCMSET)");
}
/* Set the serial port speed */
void SetPortSpeed(int PortFd, unsigned long BaudRate) {
struct termios PortSettings;
speed_t Speed;
switch (BaudRate) {
case 50UL:
Speed = B50;
break;
case 75UL:
Speed = B75;
break;
case 110UL:
Speed = B110;
break;
case 134UL:
Speed = B134;
break;
case 150UL:
Speed = B150;
break;
case 200UL:
Speed = B200;
break;
case 300UL:
Speed = B300;
break;
case 600UL:
Speed = B600;
break;
case 1200UL:
Speed = B1200;
break;
case 1800UL:
Speed = B1800;
break;
case 2400UL:
Speed = B2400;
break;
case 4800UL:
Speed = B4800;
break;
case 9600UL:
Speed = B9600;
break;
case 19200UL:
Speed = B19200;
break;
case 38400UL:
Speed = B38400;
break;
case 57600UL:
Speed = B57600;
break;
case 115200UL:
Speed = B115200;
break;
case 230400UL:
Speed = B230400;
break;
#ifdef B460800
case 460800UL:
Speed = B460800;
break;
#endif
default:
LogMsg(LOG_WARNING, "Unknown baud rate requested, setting to 9600.");
Speed = B9600;
break;
}
if (tcgetattr(PortFd, &PortSettings) < 0)
err(EXIT_FAILURE, "tcgetattr");
if (cfsetospeed(&PortSettings, Speed) < 0)
err(EXIT_FAILURE, "cfsetospeed");
if (cfsetispeed(&PortSettings, Speed) < 0)
err(EXIT_FAILURE, "cfsetispeed");
if (tcsetattr(PortFd, TCSADRAIN, &PortSettings) < 0)
err(EXIT_FAILURE, "tcsetattr");
}
/* Send the signature Sig to the client */
void SendSignature(BufferType *B, char *Sig) {
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 */
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 */
void EscRedirectChar(BufferType *SockB, BufferType *DevB, int 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 < TmpStrLen) {
IACCommand[IACPos] = C;
IACPos++;
}
break;
case IACComReceiving:
IACSigEscape = IACNormal;
break;
case IACReceived:
if (C == TNIAC) {
if (IACPos < TmpStrLen) {
IACCommand[IACPos] = C;
IACPos++;
}
IACSigEscape = IACNormal;
} else {
if (IACPos < TmpStrLen) {
IACCommand[IACPos] = TNIAC;
IACPos++;
}
if (IACPos < TmpStrLen) {
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;