Skip to content

Commit

Permalink
TwinSock 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Rollo authored and Julius Schwartzenberg committed Oct 7, 2017
1 parent 2fb31f7 commit 181e468
Show file tree
Hide file tree
Showing 21 changed files with 1,267 additions and 667 deletions.
55 changes: 55 additions & 0 deletions about.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* TwinSock - "Troy's Windows Sockets"
*
* Copyright (C) 1994 Troy Rollo <[email protected]>
*
* 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 <windows.h>

extern HINSTANCE hinst;

BOOL CALLBACK
AboutDlgProc( HWND hDlg,
UINT wMsg,
WPARAM wParam,
LPARAM lParam)
{
if (wMsg == WM_COMMAND &&
(wParam == IDOK || wParam == IDCANCEL))
{
EndDialog(hDlg, TRUE);
return TRUE;
}
else if (wMsg == WM_INITDIALOG)
{
return TRUE;
}
else
{
return FALSE;
}
}

void
About(HWND hwndParent)
{
FARPROC fpDlgProc;

fpDlgProc = MakeProcInstance((FARPROC) AboutDlgProc, hinst);
DialogBox(hinst, "ABOUT_DLG", hwndParent, fpDlgProc);
FreeProcInstance(fpDlgProc);
}

146 changes: 146 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/sh
# Remove all carriage returns and Control-Zs
rm -f a.out test.c
rm -f *.o tshost
echo "Cleaning the source code of MS-DOS control characters"
for i in *.c *.h
do
tr -d '\015\032' < $i > tempfile
mv tempfile $i
done

# Test for a few things we need to know about

echo "Testing for sys/select.h"
if [ -f /usr/include/sys/select.h ]
then
SELECT_H=-DNEED_SELECT_H
echo "You have sys/select.h"
fi

echo "Testing for sys/ttold.h"
if [ -f /usr/include/sys/ttold.h ]
then
TTOLD_H=-DNEED_TTOLD_H
echo "You have sys/ttold.h"
fi

echo "Testing for sgtty.h"
if [ -f /usr/include/sgtty.h ]
then
echo "You have it"
else
echo "You don't have it - I will use ioctl.h instead"
SGTTY_H=-DNO_SGTTY_H
fi

# Try to find a C compiler that does ANSI.
# Note that just testing for no error exit is not sufficient
# because what we find may not be a compiler, so we test for
# an a.out file.

echo "main(int argc, char **argv) { return (int) argv[argc]; }" > test.c
echo "Attempting to find a compiler that will work"
for i in bsdcc ucbcc cc acc gcc /usr/local/bin/gcc
do
( $i test.c ) </dev/null >/dev/null 2>&1
if [ -f a.out ]
then
CC=$i
break
fi
done

case "$CC" in
"")
echo "Unable to find an ANSI C compiler"
exit 1
;;
esac

echo "Using $CC as the C compiler"

echo "main() {}" > test.c

echo "Testing for -lsocket"
if $CC test.c -lsocket > /dev/null 2>/dev/null
then
echo "You will need -lsocket"
L_SOCKET=-lsocket
else
echo "You don't need it"
fi

echo "Testing for -lresolv"
if $CC test.c -lresolv ${L_SOCKET} > /dev/null 2>/dev/null
then
echo "You will need -lresolv"
L_RESOLV=-lresolv
else
if [ -f /lib/resolv.so ]
then
echo "Found the resolver libraries in /lib/resolv.so"
L_RESOLV=/lib/resolv.so
else
if [ -f /usr/lib/resolv.so ]
then
echo "Found the resolver libraries in /usr/lib/resolv.so"
L_RESOLV=/usr/lib/resolv.so
else
echo "You don't appear to need resolver libraries"
fi
fi
fi

echo "Testing for -lnsl"
if $CC test.c -lnsl ${L_RESOLV} -lresolv ${L_SOCKET} > /dev/null 2>/dev/null
then
echo "You will need -lnsl"
L_NSL=-lnsl
else
echo "You don't appear to need -lnsl"
fi

echo "main() {char *pch1, *pch2; memcpy(pch1, pch2, 10); memset(pch1, 0, 10); }" > test.c
if $CC test.c > /dev/null 2>&1
then
echo "You have memcpy and memset"
else
echo "We will use TwinSock's memcpy and memset"
NEED_MEM=mem.o
fi

echo "Testing for h_errno"
echo "#include <netdb.h>
main() { return h_errno; }" > test.c
if $CC test.c > /dev/null 2>&1
then
echo "h_errno is where it should be"
else
echo "extern int h_errno; main() { return h_errno; }" > test.c
if $CC test.c > /dev/null 2>&1
then
echo "h_errno is not declared in netdb.h, but exists"
H_ERRNO=-DNEED_H_ERRNO
else
echo "h_errno does not exist, using errno"
H_ERRNO=-DNO_H_ERRNO
fi
fi

rm -f a.out test.c

OBJECTS="tshost.o packet.o commands.o term.o $NEED_MEM"

echo "Building makefile"
echo ".c.o:" > Makefile
echo " ${CC} ${SELECT_H} ${TTOLD_H} ${SGTTY_H} ${H_ERRNO} -c "'$*.c' >> Makefile
echo >> Makefile
echo "tshost: ${OBJECTS}" >> Makefile
echo " ${CC} -o tshost ${OBJECTS} ${L_NSL} ${L_RESOLV} ${L_SOCKET}" >> Makefile

echo
echo 'Running "make"'
echo
exec make

46 changes: 30 additions & 16 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
#include "tx.h"
#include "wserror.h"

#ifdef NEED_H_ERRNO
extern int h_errno;
#endif
#ifdef NO_H_ERRNO
#define h_errno errno
#endif

extern void PacketTransmitData (void *pvData, int iDataLen, int iStream);

struct
{
int iErrnoHost;
Expand Down Expand Up @@ -85,7 +94,7 @@ struct
* to and from unaligned addresses
*/

static short
static unsigned short
ToShort(char *pchData)
{
short n;
Expand All @@ -104,7 +113,7 @@ ToLong(char *pchData)
}

static void
FromShort(char *pchData, short n)
FromShort(char *pchData, unsigned short n)
{
memcpy(pchData, &n, sizeof(short));
}
Expand All @@ -115,34 +124,34 @@ FromLong(char *pchData, long n)
memcpy(pchData, &n, sizeof(long));
}

static int
static long
GetIntVal(struct func_arg *pfa)
{
switch(pfa->at)
{
case AT_Int16:
case AT_Int16Ptr:
return ToShort(pfa->pvData);
return ntohs(ToShort(pfa->pvData));

case AT_Int32:
case AT_Int32Ptr:
return ToLong(pfa->pvData);
return ntohl(ToLong(pfa->pvData));
}
}

void
SetIntVal(struct func_arg *pfa, int iVal)
SetIntVal(struct func_arg *pfa, long iVal)
{
switch(pfa->at)
{
case AT_Int16:
case AT_Int16Ptr:
FromShort(pfa->pvData,(short) iVal);
FromShort(pfa->pvData,htons((short) iVal));
return;

case AT_Int32:
case AT_Int32Ptr:
FromLong(pfa->pvData, iVal);
FromLong(pfa->pvData, htonl((short) iVal));
return;
}
};
Expand Down Expand Up @@ -395,7 +404,7 @@ ResponseReceived(struct tx_request *ptxr_)
case FN_Data:
ptxr->nError = htons(WSAEOPNOTSUPP);
ptxr->nLen = htons(sizeof(short) * 5);
PacketTransmitData(ptxr, sizeof(short) * 5, -1);
PacketTransmitData(ptxr, sizeof(short) * 5, -2);
iErrorSent = 1;
break;

Expand Down Expand Up @@ -475,7 +484,7 @@ ResponseReceived(struct tx_request *ptxr_)
SOL_SOCKET,
SO_OOBINLINE,
(char *) &nOptVal,
&iLen);
iLen);
errno = 0;
}
break;
Expand Down Expand Up @@ -538,7 +547,7 @@ ResponseReceived(struct tx_request *ptxr_)
SOL_SOCKET,
GetIntVal(&pfaArgs[2]),
(char *) pfaArgs[3].pvData,
&iLen);
iLen);
SwapSockOptOut(&pfaArgs[3],
GetIntVal(&pfaArgs[2]));
SetIntVal(&faResult, iValue);
Expand Down Expand Up @@ -575,7 +584,11 @@ ResponseReceived(struct tx_request *ptxr_)
break;

case FN_ServByPort:
pse = getservbyport(GetIntVal(&pfaArgs[0]),
if (pfaArgs[0].at == AT_Int16)
iValue = *(short *) pfaArgs[0].pvData;
else
iValue = *(long *) pfaArgs[0].pvData;
pse = getservbyport(iValue,
(char *) pfaArgs[1].pvData);
if (pse)
{
Expand Down Expand Up @@ -630,11 +643,11 @@ ResponseReceived(struct tx_request *ptxr_)
}
if (!iErrorSent)
{
if (ft >= FN_HostByAddr || ft <= FN_ProtoByName)
if (ft >= FN_HostByAddr && ft <= FN_ProtoByName)
ptxr->nError = htons(MapHError(h_errno));
else
ptxr->nError = htons(MapError(errno));
PacketTransmitData(ptxr, nLen, -1);
PacketTransmitData(ptxr, nLen, -2);
}
free(ptxr);
free(pfaArgs);
Expand All @@ -646,7 +659,7 @@ SendSocketData(int iSocket,
int iLen,
struct sockaddr_in *psa,
int iAddrLen,
enum function_type ft)
enum Functions ft)
{
struct tx_request *ptxr;
int iDataLen;
Expand All @@ -663,7 +676,8 @@ SendSocketData(int iSocket,
ptxr->iType = htons(ft);
memcpy(ptxr->pchData, &sa, sizeof(sa));
memcpy(ptxr->pchData + sizeof(sa), pvData, iLen);
PacketTransmitData(ptxr, sizeof(short) * 5 + iDataLen, iSocket);
PacketTransmitData(ptxr, sizeof(short) * 5 + iDataLen,
(ft == FN_Data) ? iSocket : -2);
free(ptxr);
}

Loading

0 comments on commit 181e468

Please sign in to comment.