Jump to content

Portmultiplex -Link TCP sessions together along with optionally COM port


TurionAltec
 Share

Recommended Posts

#cs
Portmultiplex V0.1
By:TurionAltec
This program will allow the linking of a combination of:
    -One Serial COM port
    -Up to two inbound TCP connections
    -One outbound TCP connection with adjustable delay (to allow, for example,
        time to establish a modem connection over the serial port before
        patching a remote telnet session in.)

DEPENDENCIES:
Serial communication requires the UDF and DLL files placed in the script /
compiled EXE folder:
http://www.autoitscript.com/forum/topic/128546-serial-port-com-port-udf/


If you have no use for serial and wish to disable functionality:
-Comment out the #include line
-Comment out the block of commands in the Initialize region
-Uncomment the "dummy" functions at the end of the program.

The program is set to compile as a console application. When compiled, input is
via console. Type the selection and press ENTER
or press ENTER to accept default.

As console input doesn't work in Scite, when not compiled it will prompt via
Inputboxes.

In both cases status will be sent to the Console.

The program has limited error checking, however
if an incoming TCP connection is lost it will try to listen for a new
connection. It does not attempt to enumerate COM ports,
and is preconfigured for the modem I have on COM3.

I have had problems getting ZModem in Hyperterminal to work across a telnet
link. Try using another file transfer protocol. I have experienced good
throughput (6MB/s) with low AutoIT CPU usage when transferring files.

This script does nothing to follow the specific Telnet protocol in RFC-854 so it
may not work with 100% compatibility. All it does is take raw input data and copy to other ports.

Console Input is accomplished using the method discussed in:
http://www.autoitscript.com/forum/topic/107951-help-with-winapi-createfile-function-and-raw-mode/

WHY
You may wonder why I made this program. Three reasons:
1)Remember the olden days when two users would connect by modem direct to each
other and chat by typing raw text in the terminal and send files with Kermit or
X/Y/Z Modem? Now you can recreate that with two inbound telnet sessions
using Hyper terminal, or Tera Term!

2) Connecting Legacy PCs by serial or modem connection to telnet sessions.
This is to essentially turn the legacy PC into a telnet client / dumb terminal (or telnet
server!) If you don't have a proper serial Null-modem cable, you can directly
connect the Two PC's modems without tying up an actual phone line. To do
this open the terminal program on each computer. One one end type "ATD" and
ENTER. On the other type "ATA" and ENTER. This is where the delay on the
outgoing TCP connection comes in:
-On host PC run PortMultiplex. Enable COM, inbound, outbound.
-On legacy PC issue ATD modem command.
-Launch a telnet session with localhost:3300 to issue ATA command and establish
modem connection. You can close your localhost:3300 session if you want or
keep it open to act as a monitor.
-After your selected time the outgoing TCP connection will be made to a
remote system.

If you are looking for something a little more polished, have a look at this
software. It's designed to mimic modem signals so you can run a BBS off your
old PC to act as a telnet server. You can even type "atdt remotehost:port"
on your legacy PC to establish a telnet connection.
http://www.jammingsignal.com/files/

3)This might be useful if you’re trying to link a couple serial devices
at remote sites to each other across a LAN or the internet.

#ce

#region
#AutoIt3Wrapper_Change2CUI=y
#endregion

#region ;Definition
#include <WinAPI.au3>
#include 'CommMG.au3' ;Comment out this line to disable serial use.
Global $TCPinport = 3300, $IPin = "127.0.0.1", $TCPinSocket1 = -1, $TCPinSocket2 = -1, $TCPinMainSocket
Global $TCPoutPort = 23, $IPout = "127.0.0.1", $TCPoutdelay = 10000, $TCPoutSocket = -1
Global $COMMinit = '3,$sportSetError,9600,8,"none",1,2', $sportSetError = ""
Global $COMMenable = False, $TCPinEnable = False, $TCPoutEnable = False, $TCPinActive1 = False, $TCPinActive2 = False, $TCPoutActive = False
Global $tempvar = ""
#endregion ;Definition

#region ;Get parameters
ConsoleWrite("Port Multiplex V0.1" & @CRLF)
$tempvar = _ConsoleInput("Enable COMM? [Y,_N_]:", "N")
If StringInStr($tempvar, "y") > 0 Then
    $COMMenable = True
    $COMMinit = _ConsoleInput("Enter Comm initialization string [" & $COMMinit & "]:", $COMMinit)
EndIf

$tempvar = _ConsoleInput("Enable inbound TCP? [_Y_,N]:", "y")
If StringInStr($tempvar, "y") > 0 Then
    $TCPinEnable = True
    $TCPinport = _ConsoleInput("Enter listening port [" & $TCPinport & "]:", $TCPinport)
    $IPin = _ConsoleInput("Enter listening IP [" & $IPin & "]:", $IPin)
EndIf

$tempvar = _ConsoleInput("Enable outbound TCP? [Y,_N_]:", "N")
If StringInStr($tempvar, "y") > 0 Then
    $TCPoutEnable = True
    $IPout = _ConsoleInput("Enter outbound IP/address [" & $IPout & "]:", $IPout)
    $TCPoutPort = _ConsoleInput("Enter outbound port [" & $TCPoutPort & "]:", $TCPoutPort)
    $TCPoutdelay = _ConsoleInput("Enter delay in ms before outbound connection [" & $TCPoutdelay & "]:", $TCPoutdelay)
EndIf
#endregion ;Get parameters

#region ;Initialize
;To disable serial comment out from here to the EndIf

If $COMMenable Then
    Execute("_CommSetPort(" & $COMMinit & ")")
    If @error Then
        ConsoleWrite("Error setting up COM port!" & @CRLF)
        Exit
    EndIf
    _CommSetXonXoffProperties(11, 13, 100, 100)
    _CommSetTimeouts(5, 5, 5, 5, 5)
    _CommSetRTS(1)
    _CommSetDTR(1)
    _CommSetRTS(0)
    _CommSetDTR(0)
    ConsoleWrite("COM port initialized and connected" & @CRLF)
EndIf
;End of comment out region for disabling serial.

TCPStartup()

If $TCPinEnable Then
    $TCPinMainSocket = TCPListen($IPin, $TCPinport, 2)
    If @error Then
        ConsoleWrite("Error Listening on inbound TCP port!" & @CRLF)
        Exit
    EndIf

    ConsoleWrite("Listening on inbound TCP port" & @CRLF)
EndIf

If $TCPoutEnable And $TCPoutdelay = 0 Then
    ConnectTCPout()
ElseIf $TCPoutEnable And $TCPoutdelay > 0 Then
    AdlibRegister("ConnectTCPout", $TCPoutdelay)    ;This might be a dirty use of Adlib, but it works.
EndIf
#endregion ;Initialize

#region ;Main loop
While 1
    Select
        Case Not $TCPinEnable
;           Int(1);No-op
        Case Not $TCPinActive1
            ConnectTCPin1()
        Case Not $TCPinActive2
            ConnectTCPin2()
    EndSelect
    If $TCPinActive1 Then ReadTCPin1()
    If $TCPinActive2 Then ReadTCPin2()
    If $TCPoutActive Then ReadTCPout()
    If $COMMenable Then ReadCOMM()
WEnd
#endregion ;Main loop
#region ;Connect Functions
Func ConnectTCPin1()
    $TCPinSocket1 = TCPAccept($TCPinMainSocket)
    If $TCPinSocket1 <> -1 Then
        $TCPinActive1=True
        ConsoleWrite("Inbound TCP 1 Connected"&@CRLF)
    EndIf
EndFunc

Func ConnectTCPin2()
    $TCPinSocket2 = TCPAccept($TCPinMainSocket)
    If $TCPinSocket2 <> -1 Then
        $TCPinActive2=True
        ConsoleWrite("Inbound TCP 2 Connected"&@CRLF)
    EndIf
EndFunc

Func ConnectTCPout()
    AdlibUnRegister("ConnectTCPout")
    $IPout=TCPNameToIP($IPout)
    $TCPoutSocket=TCPConnect($IPout,$TCPoutPort)
    If $TCPoutSocket=-1 Then
        ConsoleWrite("Error connecting outbound TCP"&@CRLF)
    Else
        $TCPoutActive=True
        ConsoleWrite("Outbound TCP connected"&@CRLF)
    EndIf
EndFunc
#endregion ;Connect Functions

#region ;Read Functions
Func ReadTCPin1()
    $tempvar=TCPRecv($TCPinSocket1, 2048)
    If @error=-1 Then
        $TCPinActive1=False
        ConsoleWrite("Inbound TCP 1 Connection lost"&@CRLF)
    EndIf

    If $tempvar="" Then Return
;   If $TCPinActive1 Then TCPSend($TCPinSocket1,$tempvar)
    If $TCPinActive2 Then TCPSend($TCPinSocket2,$tempvar)
    If $TCPoutActive Then TCPSend($TCPoutSocket,$tempvar)
    If $COMMenable Then _CommSendstring($tempvar)
EndFunc

Func ReadTCPin2()
    $tempvar=TCPRecv($TCPinSocket2, 2048)
    If @error=-1 Then
        $TCPinActive2=False
        ConsoleWrite("Inbound TCP 2 Connection lost"&@CRLF)
    EndIf

    If $tempvar="" Then Return
    If $TCPinActive1 Then TCPSend($TCPinSocket1,$tempvar)
;   If $TCPinActive2 Then TCPSend($TCPinSocket2,$tempvar)
    If $TCPoutActive Then TCPSend($TCPoutSocket,$tempvar)
    If $COMMenable Then _CommSendstring($tempvar)
EndFunc

Func ReadTCPout()
    $tempvar=TCPRecv($TCPoutSocket, 2048)
    If @error=-1 Then
        $TCPoutActive=False
        ConsoleWrite("Outbound TCP Connection lost"&@CRLF)
    EndIf

    If $tempvar="" Then Return
    If $TCPinActive1 Then TCPSend($TCPinSocket1,$tempvar)
    If $TCPinActive2 Then TCPSend($TCPinSocket2,$tempvar)
;   If $TCPoutActive Then TCPSend($TCPoutSocket,$tempvar)
    If $COMMenable Then _CommSendstring($tempvar)
EndFunc

Func ReadCOMM()
    $tempvar=_CommGetString()
    If $tempvar="" Then Return
    If $TCPinActive1 Then TCPSend($TCPinSocket1,$tempvar)
    If $TCPinActive2 Then TCPSend($TCPinSocket2,$tempvar)
    If $TCPoutActive Then TCPSend($TCPoutSocket,$tempvar)
;   If $COMMenable Then _CommSendstring($tempvar)
EndFunc

#endregion ;Read Functions

Func _ConsoleInput($consoleprompt, $promptdefault)
    Local $nRead, $sRet = ""
    If Not @Compiled Then
        Return InputBox("Input", $consoleprompt, $promptdefault)
    EndIf
    ConsoleWrite($consoleprompt)
    Local $tBuffer = DllStructCreate("char")
    Local $hFile = _WinAPI_CreateFile("CON", 2, 2)
    While 1
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 1, $nRead)
        If DllStructGetData($tBuffer, 1) = @CR Then ExitLoop
        If $nRead > 0 Then $sRet &= DllStructGetData($tBuffer, 1)
    WEnd
    _WinAPI_CloseHandle($hFile)
    If $sRet = "" Then $sRet = $promptdefault
    Return $sRet
EndFunc   ;==>_ConsoleInput
#region ;Dummy serial functions
;In addition to commenting out the #include line, Uncomment the below lines to disable serial functionality.
#cs
Func _CommSendstring($tempvar)
    Return
EndFunc
Func _CommGetString()
    Return
Endfunc
#ce
#endregion ;Dummy serial functions

This program will allow the linking of a combination of:

-One Serial COM port

-Up to two inbound TCP connections

-One outbound TCP connection with adjustable delay (to allow, for example,

time to establish a modem connection over the serial port before

patching a remote telnet session in.)

DEPENDACIES:

Serial communication requires the UDF and DLL files placed in the script /

compiled EXE folder:

WHY?

You may wonder why I made this program. Three reasons:

1)Remember the olden days when two users would connect by modem direct to each

other and chat by typing raw text in the terminal and send files with Kermit or

X/Y/Z Modem? Now you can recreate that with two inbound telnet sessions

using Hyper terminal, or Tera Term!

2) Connecting Legacy PCs by serial or modem connection to telnet sessions.

This is to essentially turn the legacy PC into a telnet client / dumb terminal (or telnet

server!) If you don't have a proper serial Null-modem cable, you can directly

connect the Two PC's modems without tying up an actual phone line. To do

this open the terminal program on each computer. One one end type "ATD" and

ENTER. On the other type "ATA" and ENTER. This is where the delay on the

outgoing TCP connection comes in:

-On host PC run PortMultiplex. Enable COM, inbound, outbound.

-On legacy PC issue ATD modem command.

-Launch a telnet session with localhost:3300 to issue ATA command and establish

modem connection. You can close your localhost:3300 session if you want or

keep it open to act as a monitor.

-After your selected time the outgoing TCP connection will be made to a

remote system.

If you are looking for something a little more polished, have a look at this

software. It's designed to mimic modem signals so you can run a BBS off your

old PC to act as a telnet server. You can even type "atdt remotehost:port"

on your legacy PC to establish a telnet connection.

http://www.jammingsignal.com/files/

3)This might be useful if you’re trying to link a couple serial devices

at remote sites to each other across a LAN or the internet.

Of course the interface (when compiled) is console based. Seems appropriate when you're dealing with legacy PC's.

Edited by TurionAltec
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...