cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # (C) 2009 Chris Liechti <cliechti@gmx.net> |
| 4 | # redirect data from a TCP/IP connection to a serial port and vice versa |
| 5 | # using RFC 2217 |
| 6 | |
| 7 | |
| 8 | import sys |
| 9 | import os |
| 10 | import threading |
| 11 | import time |
| 12 | import socket |
| 13 | import serial |
| 14 | import serial.rfc2217 |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 15 | import logging |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 16 | |
| 17 | class Redirector: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 18 | def __init__(self, serial_instance, socket, debug=None): |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 19 | self.serial = serial_instance |
| 20 | self.socket = socket |
| 21 | self._write_lock = threading.Lock() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 22 | self.rfc2217 = serial.rfc2217.PortManager( |
| 23 | self.serial, |
| 24 | self, |
| 25 | debug_output = (debug and logging.getLogger('rfc2217.server')) |
| 26 | ) |
| 27 | self.log = logging.getLogger('redirector') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 28 | |
| 29 | def statusline_poller(self): |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 30 | self.log.debug('status line poll thread started') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 31 | while self.alive: |
| 32 | time.sleep(1) |
| 33 | self.rfc2217.check_modem_lines() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 34 | self.log.debug('status line poll thread terminated') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 35 | |
| 36 | def shortcut(self): |
| 37 | """connect the serial port to the TCP port by copying everything |
| 38 | from one side to the other""" |
| 39 | self.alive = True |
| 40 | self.thread_read = threading.Thread(target=self.reader) |
| 41 | self.thread_read.setDaemon(True) |
| 42 | self.thread_read.setName('serial->socket') |
| 43 | self.thread_read.start() |
| 44 | self.thread_poll = threading.Thread(target=self.statusline_poller) |
| 45 | self.thread_poll.setDaemon(True) |
| 46 | self.thread_poll.setName('status line poll') |
| 47 | self.thread_poll.start() |
| 48 | self.writer() |
| 49 | |
| 50 | def reader(self): |
| 51 | """loop forever and copy serial->socket""" |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 52 | self.log.debug('reader thread started') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 53 | while self.alive: |
| 54 | try: |
| 55 | data = self.serial.read(1) # read one, blocking |
| 56 | n = self.serial.inWaiting() # look if there is more |
| 57 | if n: |
| 58 | data = data + self.serial.read(n) # and get as much as possible |
| 59 | if data: |
| 60 | # escape outgoing data when needed (Telnet IAC (0xff) character) |
| 61 | data = serial.to_bytes(self.rfc2217.escape(data)) |
| 62 | self._write_lock.acquire() |
| 63 | try: |
| 64 | self.socket.sendall(data) # send it over TCP |
| 65 | finally: |
| 66 | self._write_lock.release() |
| 67 | except socket.error, msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 68 | self.log.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 69 | # probably got disconnected |
| 70 | break |
| 71 | self.alive = False |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 72 | self.log.debug('reader thread terminated') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 73 | |
| 74 | def write(self, data): |
| 75 | """thread safe socket write with no data escaping. used to send telnet stuff""" |
| 76 | self._write_lock.acquire() |
| 77 | try: |
| 78 | self.socket.sendall(data) |
| 79 | finally: |
| 80 | self._write_lock.release() |
| 81 | |
| 82 | def writer(self): |
| 83 | """loop forever and copy socket->serial""" |
| 84 | while self.alive: |
| 85 | try: |
| 86 | data = self.socket.recv(1024) |
| 87 | if not data: |
| 88 | break |
| 89 | self.serial.write(serial.to_bytes(self.rfc2217.filter(data))) |
| 90 | except socket.error, msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 91 | self.log.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 92 | # probably got disconnected |
| 93 | break |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 94 | self.stop() |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 95 | |
| 96 | def stop(self): |
| 97 | """Stop copying""" |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 98 | self.log.debug('stopping') |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 99 | if self.alive: |
| 100 | self.alive = False |
| 101 | self.thread_read.join() |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 102 | self.thread_poll.join() |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 103 | |
| 104 | |
| 105 | if __name__ == '__main__': |
| 106 | import optparse |
| 107 | |
| 108 | parser = optparse.OptionParser( |
cliechti | 3453f12 | 2009-08-10 22:56:34 +0000 | [diff] [blame] | 109 | usage = "%prog [options] port", |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 110 | description = "RFC 2217 Serial to Network (TCP/IP) redirector.", |
| 111 | epilog = """\ |
| 112 | NOTE: no security measures are implemented. Anyone can remotely connect |
| 113 | to this service over the network. |
| 114 | |
| 115 | Only one connection at once is supported. When the connection is terminated |
| 116 | it waits for the next connect. |
| 117 | """) |
| 118 | |
| 119 | parser.add_option("-p", "--localport", |
| 120 | dest = "local_port", |
| 121 | action = "store", |
| 122 | type = 'int', |
| 123 | help = "local TCP port", |
| 124 | default = 2217 |
| 125 | ) |
| 126 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 127 | parser.add_option("-v", "--verbose", |
| 128 | dest = "verbosity", |
| 129 | action = "count", |
| 130 | help = "print more diagnostic messages (option can be given multiple times)", |
| 131 | default = 0 |
| 132 | ) |
| 133 | |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 134 | (options, args) = parser.parse_args() |
| 135 | |
| 136 | if len(args) != 1: |
| 137 | parser.error('serial port name required as argument') |
| 138 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 139 | if options.verbosity > 3: |
| 140 | options.verbosity = 3 |
| 141 | level = ( |
| 142 | logging.WARNING, |
| 143 | logging.INFO, |
| 144 | logging.DEBUG, |
| 145 | logging.NOTSET, |
| 146 | )[options.verbosity] |
| 147 | logging.basicConfig(level=logging.INFO) |
| 148 | logging.getLogger('rfc2217').setLevel(level) |
| 149 | |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 150 | # connect to serial port |
| 151 | ser = serial.Serial() |
| 152 | ser.port = args[0] |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 153 | ser.timeout = 3 # required so that the reader thread can exit |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 154 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 155 | logging.info("RFC 2217 TCP/IP to Serial redirector - type Ctrl-C / BREAK to quit") |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 156 | |
| 157 | try: |
| 158 | ser.open() |
| 159 | except serial.SerialException, e: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 160 | logging.error("Could not open serial port %s: %s" % (ser.portstr, e)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 161 | sys.exit(1) |
| 162 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 163 | logging.info("Serving serial port: %s" % (ser.portstr,)) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 164 | settings = ser.getSettingsDict() |
| 165 | # reset contol line as no _remote_ "terminal" has been connected yet |
| 166 | ser.setDTR(False) |
| 167 | ser.setRTS(False) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 168 | |
| 169 | srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 170 | srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 171 | srv.bind( ('', options.local_port) ) |
| 172 | srv.listen(1) |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 173 | logging.info("TCP/IP port: %s" % (options.local_port,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 174 | while True: |
| 175 | try: |
| 176 | connection, addr = srv.accept() |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 177 | logging.info('Connected by %s:%s' % (addr[0], addr[1])) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 178 | connection.setsockopt( socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) |
| 179 | ser.setRTS(True) |
| 180 | ser.setDTR(True) |
| 181 | # enter network <-> serial loop |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 182 | r = Redirector( |
| 183 | ser, |
| 184 | connection, |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 185 | options.verbosity > 0 |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 186 | ) |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 187 | try: |
| 188 | r.shortcut() |
| 189 | finally: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 190 | logging.info('Disconnected') |
cliechti | d9a06ce | 2009-08-10 01:30:53 +0000 | [diff] [blame] | 191 | r.stop() |
| 192 | connection.close() |
| 193 | ser.setDTR(False) |
| 194 | ser.setRTS(False) |
| 195 | # Restore port settings (may have been changed by RFC 2217 capable |
| 196 | # client) |
| 197 | ser.applySettingsDict(settings) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 198 | except KeyboardInterrupt: |
| 199 | break |
| 200 | except socket.error, msg: |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 201 | logging.error('%s' % (msg,)) |
cliechti | ef39b8b | 2009-08-07 18:22:49 +0000 | [diff] [blame] | 202 | |
cliechti | 5cc3eb1 | 2009-08-11 23:04:30 +0000 | [diff] [blame^] | 203 | logging.info('--- exit ---') |