1
0
mirror of https://github.com/UzixLS/picocom.git synced 2025-07-19 07:21:18 +03:00

Add -T option for RFC 2217 telnet devices

Add support for connecting to remote COM-PORT servers using RFC 2217.

This commit adds a term_ops provider that translates all termios
and modem ioctls into TELNET operations.

It supports:
 - IPv4 and IPv6 hosts and ports numbers, per getaddrinfo()
 - baud rate changes up to 115200
 - data bit size, parity, stop bits
 - sending BREAK
 - control over remote hardware flow control mode
 - control over remote signals: RTS, DTR
 - visibility of remote signals: CD, RI, DSR, CTS
 - flushing remote rx/tx buffers

Limitations
 - There will be some delay between some operations (eg DTR toggle)
   so rapid toggling can result in a temporarily inconsistent local
   state.  However, it will eventually synchronise with the remote
   state if allowed to settle.
 - The terminal's FD is the raw TCP connection, so sx/rx likely won't
   work.
 - More baud rates could be supported.
 - Remote parity modes SPACE and MARK are not supported.
 - Stop bit size 1.5 is not supported.
 - In-protocol flow control is not supported.
 - BREAK is implemented with a local usleep of 250ms, which may be
   eaten up or extended by the network.
This commit is contained in:
David Leonard
2018-02-18 20:07:42 +10:00
committed by Nick Patavalis
parent fa87795a89
commit 5017addb62
6 changed files with 1191 additions and 5 deletions

View File

@ -50,15 +50,17 @@ linenoise-1.0/linenoise.o : linenoise-1.0/linenoise.c linenoise-1.0/linenoise.h
OBJS += picocom.o term.o fdio.o split.o termios2.o custbaud_bsd.o
OBJS += tn2217.o
picocom : $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
picocom.o : picocom.c term.h fdio.h split.h custbaud.h
picocom.o : picocom.c term.h fdio.h split.h custbaud.h tn2217.h
term.o : term.c term.h termint.h termios2.h custbaud_bsd.h custbaud.h
split.o : split.c split.h
fdio.o : fdio.c fdio.h
termios2.o : termios2.c termios2.h termbits2.h custbaud.h
custbaud_bsd.o : custbaud_bsd.c custbaud_bsd.h custbaud.h
tn2217.o : tn2217.c tn2217.h tncomport.h
.c.o :
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<

View File

@ -457,6 +457,11 @@ Picocom accepts the following command-line options.
options. Picocom's version, compile-time options, and enabled
features are also shown.
**--telnet** | **-T**
: Interpret the _device_ as the name of an RFC 2217 service,
with an optional port: _hostname_[,_port_]
# DISPLAY OF OPTIONS AND PORT SETTINGS

View File

@ -56,6 +56,7 @@
#endif
#include "custbaud.h"
#include "tn2217.h"
/**********************************************************************/
@ -219,6 +220,7 @@ struct {
int raise_rts;
int raise_dtr;
int quiet;
int telnet;
} opts = {
.port = NULL,
.baud = 9600,
@ -1656,6 +1658,7 @@ show_usage(char *name)
printf(" --lower-dtr\n");
printf(" --raise-dtr\n");
printf(" --<q>uiet\n");
printf(" --telnet | -T\n");
printf(" --<h>elp\n");
printf("<map> is a comma-separated list of one or more of:\n");
printf(" crlf : map CR --> LF\n");
@ -1715,6 +1718,7 @@ parse_args(int argc, char *argv[])
{"raise-rts", no_argument, 0, 3},
{"raise-dtr", no_argument, 0, 4},
{"quiet", no_argument, 0, 'q'},
{"telnet", no_argument, 0, 'T'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
@ -1729,7 +1733,7 @@ parse_args(int argc, char *argv[])
/* no default error messages printed. */
opterr = 0;
c = getopt_long(argc, argv, "hirulcqXnv:s:r:e:f:b:y:d:p:g:t:x:",
c = getopt_long(argc, argv, "hirulcqXnTv:s:r:e:f:b:y:d:p:g:t:x:",
longOptions, &optionIndex);
if (c < 0)
@ -1907,6 +1911,9 @@ parse_args(int argc, char *argv[])
case 'q':
opts.quiet = 1;
break;
case 'T':
opts.telnet = 1;
break;
case 'h':
show_usage(argv[0]);
exit(EXIT_SUCCESS);
@ -2066,19 +2073,23 @@ main (int argc, char *argv[])
fatal("cannot open %s: %s", opts.log_filename, strerror(errno));
}
tty_fd = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (opts.telnet) {
tty_fd = tn2217_open(opts.port);
} else
tty_fd = open(opts.port, O_RDWR | O_NONBLOCK | O_NOCTTY);
if (tty_fd < 0)
fatal("cannot open %s: %s", opts.port, strerror(errno));
#ifdef USE_FLOCK
if ( ! opts.nolock ) {
if ( ! opts.nolock && !opts.telnet ) {
r = flock(tty_fd, LOCK_EX | LOCK_NB);
if ( r < 0 )
fatal("cannot lock %s: %s", opts.port, strerror(errno));
}
#endif
r = term_add(tty_fd, NULL);
r = term_add(tty_fd, opts.telnet ? &tn2217_ops : NULL);
if ( r >= 0 && ! opts.noinit ) {
r = term_set(tty_fd,
1, /* raw mode. */

1066
tn2217.c Normal file

File diff suppressed because it is too large Load Diff

14
tn2217.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef TN2217_H
#define TN2217_H
/* Open a socket to a telnet service.
* The port string will be of the form "<hostname>[,<port>]".
* If the port is omitted, uses "telnet" from /etc/servcies.
* The tn2217_ops should be used to interact with the returned fd.
* Returns -1 on failure, and prints an error message to stderr. */
int tn2217_open(const char *port);
struct term_ops;
extern const struct term_ops tn2217_ops;
#endif /* of TN2217_H */

88
tncomport.h Normal file
View File

@ -0,0 +1,88 @@
#ifndef TNCOMPORT_H
#define TNCOMPORT_H
/* RFC2217 COM-PORT-OPTION constant definitions */
#define TELOPT_COMPORT 44
/* Suboption codes */
#define COMPORT_SIGNATURE 0 /* <text> */
#define COMPORT_SET_BAUDRATE 1 /* <value(4)> */
#define COMPORT_SET_DATASIZE 2 /* <value> */
#define COMPORT_SET_PARITY 3 /* <value> */
#define COMPORT_SET_STOPSIZE 4 /* <value> */
#define COMPORT_SET_CONTROL 5 /* <value> */
#define COMPORT_NOTIFY_LINESTATE 6 /* <value> */
#define COMPORT_NOTIFY_MODEMSTATE 7 /* <value> */
#define COMPORT_FLOWCONTROL_SUSPEND 8 /* replaces RFC1372 */
#define COMPORT_FLOWCONTROL_RESUME 9 /* replaces RFC1372 */
#define COMPORT_SET_LINESTATE_MASK 10 /* <value> */
#define COMPORT_SET_MODEMSTATE_MASK 11 /* <value> */
#define COMPORT_PURGE_DATA 12 /* <value> */
#define COMPORT_SERVER_BASE 100 /* server message offset */
#define COMPORT_BAUDRATE_REQUEST 0 /* max baudrate is 4.2 Gbaud */
#define COMPORT_DATASIZE_REQUEST 0
#define COMPORT_DATASIZE_5 5
#define COMPORT_DATASIZE_6 6
#define COMPORT_DATASIZE_7 7
#define COMPORT_DATASIZE_8 8
#define COMPORT_PARITY_REQUEST 0
#define COMPORT_PARITY_NONE 1
#define COMPORT_PARITY_ODD 2
#define COMPORT_PARITY_EVEN 3
#define COMPORT_PARITY_MARK 4
#define COMPORT_PARITY_SPACE 5
#define COMPORT_STOPSIZE_REQUEST 0
#define COMPORT_STOPSIZE_1 1
#define COMPORT_STOPSIZE_2 2
#define COMPORT_STOPSIZE_1_5 3 /* when datasize = 5 */
#define COMPORT_CONTROL_FC_REQUEST 0 /* request FC state */
#define COMPORT_CONTROL_FC_NONE 1
#define COMPORT_CONTROL_FC_XONOFF 2
#define COMPORT_CONTROL_FC_HARDWARE 3
#define COMPORT_CONTROL_FC_DCD 17
#define COMPORT_CONTROL_FC_DSR 19
#define COMPORT_CONTROL_BREAK_REQUEST 4 /* request BREAK state */
#define COMPORT_CONTROL_BREAK_ON 5
#define COMPORT_CONTROL_BREAK_OFF 6
#define COMPORT_CONTROL_DTR_REQUEST 7 /* request DTR state */
#define COMPORT_CONTROL_DTR_ON 8
#define COMPORT_CONTROL_DTR_OFF 9
#define COMPORT_CONTROL_RTS_REQUEST 10 /* request RTS state */
#define COMPORT_CONTROL_RTS_ON 11
#define COMPORT_CONTROL_RTS_OFF 12
#define COMPORT_CONTROL_FCI_REQUEST 13 /* request inbound FC state */
#define COMPORT_CONTROL_FCI_NONE 14
#define COMPORT_CONTROL_FCI_XONOFF 15
#define COMPORT_CONTROL_FCI_HARDWARE 16
#define COMPORT_CONTROL_FCI_DTR 18
#define COMPORT_LINE_TMOUT 128
#define COMPORT_LINE_TSRE 64
#define COMPORT_LINE_THRE 32
#define COMPORT_LINE_BI 16
#define COMPORT_LINE_FE 8
#define COMPORT_LINE_PE 4
#define COMPORT_LINE_OE 2
#define COMPORT_LINE_DR 1
#define COMPORT_MODEM_CD 128
#define COMPORT_MODEM_RI 64
#define COMPORT_MODEM_DSR 32
#define COMPORT_MODEM_CTS 16
#define COMPORT_MODEM_DCD 8
#define COMPORT_MODEM_TERI 4
#define COMPORT_MODEM_DDSR 2
#define COMPORT_MODEM_DCTS 1
#define COMPORT_PURGE_RX 1
#define COMPORT_PURGE_TX 2
#define COMPORT_PURGE_RXTX 3
#endif /* for TNCOMPORT_H */