Skip to content

Commit

Permalink
TwinSock 1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Rollo authored and jschwartzenberg committed Jul 10, 2024
1 parent 3f52e86 commit 24bfa5d
Show file tree
Hide file tree
Showing 14 changed files with 1,011 additions and 134 deletions.
16 changes: 14 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ if [ -f /usr/include/sys/select.h ]
then
SELECT_H=-DNEED_SELECT_H
echo "You have sys/select.h"
if grep 'Santa Cruz Operation' /usr/include/sys/select.h >/dev/null
then
SELECT_H=
echo "but we won't use it since the SCO select.h is broken ..."
fi
fi

echo "Testing for sys/ttold.h"
Expand Down Expand Up @@ -137,9 +142,16 @@ else
TERM_OBJECT=term.o
fi

case "`uname -s`" in
OSF*)
echo "OSF doesn't like POSIX terminals, so we'd better use BSD ones."
TERM_OBJECT=term.o
;;
esac

rm -f a.out test.c

OBJECTS="tshost.o packet.o commands.o ${TERM_OBJECT} $NEED_MEM"
OBJECTS="tshost.o packet.o getentry.o commands.o ${TERM_OBJECT} $NEED_MEM"

echo "Building makefile"
echo ".c.o:" > Makefile
Expand All @@ -152,4 +164,4 @@ echo
echo 'Running "make -f Makefile"'
echo
exec make -f Makefile


139 changes: 133 additions & 6 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ static int HostSocketProtocol[] =
70, 71, 72, 73, 74, 75, 76, IPPROTO_ND, 78, 79
};


#ifndef SO_ACCEPTCONN
#define SO_ACCEPTCONN 0
#endif
#ifndef SO_USELOOPBACK
#define SO_USELOOPBACK 0
#endif

static struct
{
int iWindows;
Expand Down Expand Up @@ -339,8 +347,12 @@ SetIntVal(struct func_arg *pfa, long iVal)
struct sockaddr *
ConvertSA(struct func_arg *pfa, struct sockaddr_in *sin)
{
u_short tmp;

memcpy(sin, pfa->pvData, sizeof(*sin));
sin->sin_family = GetFamily(ntohs(sin->sin_family));
tmp = *(u_short *) sin;
*(u_short *) sin = 0;
sin->sin_family = GetFamily(ntohs(tmp));
return (struct sockaddr *) sin;
}

Expand Down Expand Up @@ -524,13 +536,113 @@ SwapSockOptOut( struct func_arg *pfa,
}
}


int
CompressArg( struct tx_request *ptxr,
struct func_arg *pfaArgs,
int nArgs,
struct func_arg *pfaResult,
int iArg)
{
struct func_arg *pfaNow;
char *pchNewData, *pchData;
int i, j;
int nLen;

pchData = pchNewData = ptxr->pchData;
for (i = 0; i <= nArgs; i++)
{
if (i < nArgs)
pfaNow = pfaArgs + i;
else
pfaNow = pfaResult;
for (j = 0; j < pfaNow->iLen + 4; j++)
pchNewData[j] = pchData[j];
pchData += pfaNow->iLen + 4;
if (i == iArg)
{
pfaNow->iLen = 0;
*(short *) (pchNewData + 2) = 0;
}
pchNewData += pfaNow->iLen + 4;
}
nLen = pchNewData - ptxr->pchData + 10;
ptxr->nLen = htons((short) nLen);
return nLen;
}


void
GetPortRange( short iInPort,
short *piStartPort,
short *piEndPort)
{
char achBuffer[1024];
char achPort[20];
char *pchComma;

sprintf(achPort, "%d", (int) iInPort);
GetTwinSockSetting("Mappings",
achPort,
"",
achBuffer,
sizeof(achBuffer));
if (!*achBuffer || !(pchComma = (char *) strchr(achBuffer, ',')))
{
*piStartPort = *piEndPort = iInPort;
return;
}
*pchComma++ = 0;
*piStartPort = atoi(achBuffer);
*piEndPort = atoi(pchComma);
}

void
SendRemapMessage( short iFrom,
short iTo)
{
char achBuffer[1024];
char achPortDesc[50];
char achPort[20];
struct tx_request *ptxr;

sprintf(achPortDesc, "Port %d", (int) iFrom);
sprintf(achPort, "%d", (int) iFrom);
GetTwinSockSetting("PortNames",
achPort,
achPortDesc,
achBuffer,
sizeof(achBuffer));
strcat(achBuffer, " remapped to ");
sprintf(achPortDesc, "Port %d", (int) iTo);
sprintf(achPort, "%d", (int) iTo);
GetTwinSockSetting("PortNames",
achPort,
achPortDesc,
achBuffer + strlen(achBuffer),
sizeof(achBuffer) - strlen(achBuffer));
ptxr = (struct tx_request *) malloc(11 + strlen(achBuffer));
ptxr->iType = htons(FN_Message);
ptxr->nArgs = 0;
ptxr->nLen = htons(11 + strlen(achBuffer));
ptxr->id = -1;
ptxr->nError = 0;
strcpy(ptxr->pchData, achBuffer);
PacketTransmitData(ptxr, 10 + strlen(achBuffer) + 1, -2);
free(ptxr);
}

void
ResponseReceived(struct tx_request *ptxr_)
{
enum Functions ft;
short nArgs;
short nLen;
short id;
short iInPort;
short iStartPort;
short iEndPort;
short iPort;
int iLen;
int iValue;
int iSocket;
Expand Down Expand Up @@ -595,6 +707,7 @@ ResponseReceived(struct tx_request *ptxr_)
pfaArgs[1].pvData,
GetIntVal(&pfaArgs[2]),
GetIntVal(&pfaArgs[3])));
nLen = CompressArg(ptxr, pfaArgs, nArgs, &faResult, 1);
break;

case FN_SendTo:
Expand All @@ -605,13 +718,28 @@ ResponseReceived(struct tx_request *ptxr_)
GetIntVal(&pfaArgs[3]),
ConvertSA(&pfaArgs[4], &sin),
GetIntVal(&pfaArgs[5])));
nLen = CompressArg(ptxr, pfaArgs, nArgs, &faResult, 1);
break;

case FN_Bind:
SetIntVal(&faResult,
bind(GetIntVal(&pfaArgs[0]),
ConvertSA(&pfaArgs[1], &sin),
GetIntVal(&pfaArgs[2])));
ConvertSA(&pfaArgs[1], &sin);
iInPort = ntohs(sin.sin_port);
GetPortRange(iInPort, &iStartPort, &iEndPort);
for (iPort = iStartPort; iPort <= iEndPort; iPort++)
{
errno = 0;
sin.sin_port = htons(iPort);
iValue = bind(GetIntVal(&pfaArgs[0]),
(struct sockaddr *) &sin,
GetIntVal(&pfaArgs[2]));
if (!iValue)
{
if (iPort != iInPort)
SendRemapMessage(iInPort, iPort);
break;
}
}
SetIntVal(&faResult, iValue);
break;

case FN_Connect:
Expand Down Expand Up @@ -865,4 +993,3 @@ SendSocketData(int iSocket,
(ft == FN_Data) ? iSocket : -2);
free(ptxr);
}

85 changes: 54 additions & 31 deletions comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "twinsock.h"

extern HINSTANCE hinst;
int iPortChanged = 0;
Expand All @@ -24,13 +26,20 @@ GetConfigInt(char const *pchItem, UINT nDefault)
return GetPrivateProfileInt("Config", pchItem, nDefault, "TWINSOCK.INI");
}

enum LineSpeed { B110, B300, B600, B1200, B2400, B4800, B9600,
B14400, B19200, B38400, B56000, B128000, B256000,
BBOGUS };
unsigned GetSpeedIndex(enum LineSpeed lsSpeed);

void
InitComm(int idComm)
{
DCB dcb;
unsigned speed;

dcb.Id = idComm;
dcb.BaudRate = GetConfigInt("Speed", 19200);
speed = GetConfigInt("Speed", B19200);
dcb.BaudRate = GetSpeedIndex((enum LineSpeed) speed);
dcb.ByteSize = GetConfigInt("Databits", 8);
dcb.Parity = GetConfigInt("Parity", NOPARITY);
dcb.StopBits = GetConfigInt("StopBits", ONESTOPBIT);
Expand Down Expand Up @@ -66,26 +75,43 @@ InitComm(int idComm)

static struct
{
unsigned iValue;
long iSpeed;
unsigned iValue;
char *strSpeed;
enum LineSpeed lsSpeed;
} aSpeeds[] =
{
{ CBR_110, 110 },
{ CBR_300, 300 },
{ CBR_600, 600 },
{ CBR_1200, 1200 },
{ CBR_2400, 2400 },
{ CBR_4800, 4800 },
{ CBR_9600, 9600 },
{ CBR_14400, 14400 },
{ CBR_19200, 19200 },
{ CBR_38400, 38400 },
{ CBR_56000, 56000 },
{ CBR_128000, 128000 },
{ CBR_256000, 2256000 },
{ 0, -1 },
{ CBR_110, "110", B110 },
{ CBR_300, "300", B300 },
{ CBR_600, "600", B600 },
{ CBR_1200, "1200", B1200 },
{ CBR_2400, "2400", B2400 },
{ CBR_4800, "4800", B4800 },
{ CBR_9600, "9600", B9600 },
{ CBR_14400, "14400",B14400 },
{ CBR_19200, "19200",B19200 },
{ CBR_38400, "38400",B38400 },
{ CBR_56000, "57600",B56000 },
{ CBR_128000, "128000",B128000},
{ CBR_256000, "256000",B256000},
{ 0, "BOGUS",BBOGUS },
};

/*
* Returns the appropriate port speed index value
* necessary to setup the COM port
*/
unsigned GetSpeedIndex(enum LineSpeed lsSpeed)
{
int i;

for (i = 0; aSpeeds[i].lsSpeed != BBOGUS; ++i)
{
if (lsSpeed == aSpeeds[i].lsSpeed)
break;
}
return (aSpeeds[i].iValue);
}

static char *apchPorts[] =
{
"COM1",
Expand Down Expand Up @@ -131,16 +157,13 @@ FillCommsDialog(HWND hDlg)
strcpy(achStartPort, achTmp);
for (i = 0; apchPorts[i]; i++)
SendDlgItemMessage(hDlg, CE_PORT, CB_ADDSTRING, 0, (LPARAM) apchPorts[i]);
iSpeedNow = GetPrivateProfileInt("Config", "Speed", 19200, "TWINSOCK.INI");
for (i = 0; aSpeeds[i].iSpeed != -1; i++)
iSpeedNow = GetPrivateProfileInt("Config", "Speed", (int) B19200, "TWINSOCK.INI");
for (i = 0; aSpeeds[i].lsSpeed != BBOGUS; ++i)
{
sprintf(achTmp, "%ld", aSpeeds[i].iSpeed);
sprintf(achTmp, "%s", aSpeeds[i].strSpeed);
SendDlgItemMessage(hDlg, CE_SPEED, CB_ADDSTRING, 0, (LPARAM) achTmp);
if (aSpeeds[i].iSpeed == iSpeedNow ||
aSpeeds[i].iValue == iSpeedNow)
{
if (aSpeeds[i].lsSpeed == iSpeedNow)
SendDlgItemMessage(hDlg, CE_SPEED, CB_SETCURSEL, i, 0);
}
}

for (i = 5; i <= 8; i++)
Expand All @@ -165,7 +188,7 @@ FillCommsDialog(HWND hDlg)
void
ReadCommsDialog(HWND hDlg)
{
int i;
LRESULT i;
char achTemp[100];

GetDlgItemText(hDlg, CE_PORT, achTemp, 100);
Expand All @@ -174,7 +197,7 @@ ReadCommsDialog(HWND hDlg)
iPortChanged = 1;

i = SendDlgItemMessage(hDlg, CE_SPEED, CB_GETCURSEL, 0, 0);
i = aSpeeds[i].iValue;
i = aSpeeds[i].lsSpeed;
sprintf(achTemp, "%d", i);
WritePrivateProfileString("Config", "Speed", achTemp, "TWINSOCK.INI");

Expand Down Expand Up @@ -247,8 +270,8 @@ DialDlgProc( HWND hDlg,
WPARAM wParam,
LPARAM lParam)
{
char achNumber[84];
int iMethod;
char achNumber[80];

switch(wMsg)
{
Expand All @@ -265,14 +288,14 @@ DialDlgProc( HWND hDlg,
switch(wParam)
{
case IDOK:
GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80);
WritePrivateProfileString("Config", "LastNumber", achNumber, "TWINSOCK.INI");
GetDlgItemText(hDlg, DL_NUMBER, achNumber + 4, 76);
WritePrivateProfileString("Config", "LastNumber", achNumber + 4, "TWINSOCK.INI");
iMethod = SendMessage(GetDlgItem(hDlg, DL_TONE), BM_GETCHECK, 0, 0);
WritePrivateProfileString("Config", "DialingMethod", iMethod ? "1" : "0", "TWINSOCK.INI");
EndDialog(hDlg, TRUE);
SendData(iMethod ? "ATDT" : "ATDP", 4);
strncpy(achNumber, iMethod ? "ATDT" : "ATDP", 4);
strcat(achNumber, "\r");
SendData(achNumber, strlen(achNumber));
SendData("\r", 1);
break;

case IDCANCEL:
Expand Down
Loading

0 comments on commit 24bfa5d

Please sign in to comment.