From 2fb31f77ac7332aad38afa217cc298222a243d5f Mon Sep 17 00:00:00 2001 From: Troy Rollo Date: Tue, 15 Nov 1994 00:28:00 +1000 Subject: [PATCH] TwinSock 1.0 --- commands.c | 669 +++++++++++++++++ copying | 341 +++++++++ makefile | 18 + makefile.unx | 4 + packet.c | 532 ++++++++++++++ packet.h | 41 ++ readme.txt | 132 ++++ term.c | 39 + tshost.c | 407 ++++++++++ twinsock.c | 338 +++++++++ twinsock.h | 162 ++++ twinsock.ico | Bin 0 -> 766 bytes twinsock.ini | 16 + twinsock.rc | 2 + twinsock.res | Bin 0 -> 792 bytes tx.h | 45 ++ winsock.c | 1997 ++++++++++++++++++++++++++++++++++++++++++++++++++ winsock.def | 65 ++ wserror.h | 53 ++ 19 files changed, 4861 insertions(+) create mode 100644 commands.c create mode 100644 copying create mode 100644 makefile create mode 100644 makefile.unx create mode 100644 packet.c create mode 100644 packet.h create mode 100644 readme.txt create mode 100644 term.c create mode 100644 tshost.c create mode 100644 twinsock.c create mode 100644 twinsock.h create mode 100644 twinsock.ico create mode 100644 twinsock.ini create mode 100644 twinsock.rc create mode 100644 twinsock.res create mode 100644 tx.h create mode 100644 winsock.c create mode 100644 winsock.def create mode 100644 wserror.h diff --git a/commands.c b/commands.c new file mode 100644 index 0000000..36c3624 --- /dev/null +++ b/commands.c @@ -0,0 +1,669 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 +#include +#include +#include +#include +#include +#include "twinsock.h" +#include "tx.h" +#include "wserror.h" + +struct +{ + int iErrnoHost; + int iErrnoDos; +} error_mappings[] = +{ + { 0, 0 }, + { EINTR, WSAEINTR }, + { EBADF, WSAEBADF }, + { EINVAL, WSAEINVAL }, + { EMFILE, WSAEMFILE }, + { EWOULDBLOCK, WSAEWOULDBLOCK }, + { EINPROGRESS, WSAEINPROGRESS }, + { EALREADY, WSAEALREADY }, + { ENOTSOCK, WSAENOTSOCK }, + { EDESTADDRREQ, WSAEDESTADDRREQ }, + { EMSGSIZE, WSAEMSGSIZE }, + { EPROTOTYPE, WSAEPROTOTYPE }, + { ENOPROTOOPT, WSAENOPROTOOPT }, + { EPROTONOSUPPORT, WSAEPROTONOSUPPORT }, + { ESOCKTNOSUPPORT, WSAESOCKTNOSUPPORT }, + { EOPNOTSUPP, WSAEOPNOTSUPP }, + { EPFNOSUPPORT, WSAEPFNOSUPPORT }, + { EAFNOSUPPORT, WSAEAFNOSUPPORT }, + { EADDRINUSE, WSAEADDRINUSE }, + { EADDRNOTAVAIL,WSAEADDRNOTAVAIL }, + { ENETDOWN, WSAENETDOWN }, + { ENETUNREACH, WSAENETUNREACH }, + { ENETRESET, WSAENETRESET }, + { ECONNABORTED, WSAECONNABORTED }, + { ECONNRESET, WSAECONNRESET }, + { ENOBUFS, WSAENOBUFS }, + { EISCONN, WSAEISCONN }, + { ENOTCONN, WSAENOTCONN }, + { ESHUTDOWN, WSAESHUTDOWN }, + { ETOOMANYREFS, WSAETOOMANYREFS }, + { ETIMEDOUT, WSAETIMEDOUT }, + { ECONNREFUSED, WSAECONNREFUSED }, + { ELOOP, WSAELOOP, }, + { ENAMETOOLONG, WSAENAMETOOLONG }, + { EHOSTDOWN, WSAEHOSTDOWN }, + { EHOSTUNREACH, WSAEHOSTUNREACH }, + { -1, -1 } +}, h_error_mappings[] = +{ + { 0, 0 }, + { HOST_NOT_FOUND, WSAHOST_NOT_FOUND }, + { TRY_AGAIN, WSATRY_AGAIN }, + { NO_RECOVERY, WSANO_RECOVERY }, + { NO_DATA, WSANO_DATA }, + { -1, -1 } +}; + +/* some functions to allow us to copy integer data + * to and from unaligned addresses + */ + +static short +ToShort(char *pchData) +{ + short n; + + memcpy(&n, pchData, sizeof(short)); + return n; +} + +static long +ToLong(char *pchData) +{ + long n; + + memcpy(&n, pchData, sizeof(long)); + return n; +} + +static void +FromShort(char *pchData, short n) +{ + memcpy(pchData, &n, sizeof(short)); +} + +static void +FromLong(char *pchData, long n) +{ + memcpy(pchData, &n, sizeof(long)); +} + +static int +GetIntVal(struct func_arg *pfa) +{ + switch(pfa->at) + { + case AT_Int16: + case AT_Int16Ptr: + return ToShort(pfa->pvData); + + case AT_Int32: + case AT_Int32Ptr: + return ToLong(pfa->pvData); + } +} + +void +SetIntVal(struct func_arg *pfa, int iVal) +{ + switch(pfa->at) + { + case AT_Int16: + case AT_Int16Ptr: + FromShort(pfa->pvData,(short) iVal); + return; + + case AT_Int32: + case AT_Int32Ptr: + FromLong(pfa->pvData, iVal); + return; + } +}; + +struct sockaddr * +ConvertSA(struct func_arg *pfa, struct sockaddr_in *sin) +{ + memcpy(sin, pfa->pvData, sizeof(*sin)); + sin->sin_family = ntohs(sin->sin_family); + return (struct sockaddr *) sin; +} + +int +MapError(int iError) +{ + int i; + + for (i = 0; error_mappings[i].iErrnoHost != -1; i++) + { + if (error_mappings[i].iErrnoHost == iError) + return error_mappings[i].iErrnoDos; + } + return WSAEFAULT; +} + +int +MapHError(int iError) +{ + int i; + + for (i = 0; h_error_mappings[i].iErrnoHost != -1; i++) + { + if (h_error_mappings[i].iErrnoHost == iError) + return h_error_mappings[i].iErrnoDos; + } + return WSAEFAULT; +} + +int +CopyString(void *pvData, char *pchString, int iMax) +{ + char *pchData; + int iLen; + + pchData = (char *) pvData; + iLen = strlen(pchString); + if (iLen + 1 > iMax - 1) + return 0; + strcpy(pchData, pchString); + return iLen + 1; +} + +void +CopyHostEnt(void *pvData, struct hostent *phe) +{ + int iLocation; + char *pchData; + int i; + + pchData = (char *) pvData; + FromShort(pchData, htons(phe->h_addrtype)); + FromShort(pchData + sizeof(short), htons(phe->h_length)); + for (i = 0; phe->h_addr_list[i]; i++); + FromShort(pchData + sizeof(short) * 2, htons(i)); + iLocation = sizeof(short) * 3; + for (i = 0; phe->h_addr_list[i]; i++) + { + memcpy(pchData + iLocation, phe->h_addr_list[i], 4); + iLocation += 4; + } + iLocation += CopyString(pchData + iLocation, + phe->h_name, + MAX_HOST_ENT - iLocation); + for (i = 0; phe->h_aliases[i]; i++) + { + iLocation += CopyString(pchData + iLocation, + phe->h_aliases[i], + MAX_HOST_ENT - iLocation); + } + pchData[iLocation] = 0; +} + +void +CopyNetEnt(void *pvData, struct netent *pne) +{ + int iLocation; + char *pchData; + int i; + + pchData = (char *) pvData; + FromShort(pchData, htons(pne->n_addrtype)); + FromLong(pchData + sizeof(short), pne->n_net); + iLocation = sizeof(short) + sizeof(long); + iLocation += CopyString(pchData + iLocation, + pne->n_name, + MAX_HOST_ENT - iLocation); + for (i = 0; pne->n_aliases[i]; i++) + { + iLocation += CopyString(pchData + iLocation, + pne->n_aliases[i], + MAX_HOST_ENT - iLocation); + } + pchData[iLocation] = 0; +} + +void +CopyServEnt(void *pvData, struct servent *pse) +{ + int iLocation; + char *pchData; + int i; + + pchData = (char *) pvData; + FromShort(pchData, pse->s_port); /* No htons - already in network order */ + iLocation = sizeof(short); + iLocation += CopyString(pchData + iLocation, + pse->s_proto, + MAX_HOST_ENT - iLocation); + iLocation += CopyString(pchData + iLocation, + pse->s_name, + MAX_HOST_ENT - iLocation); + for (i = 0; pse->s_aliases[i]; i++) + { + iLocation += CopyString(pchData + iLocation, + pse->s_aliases[i], + MAX_HOST_ENT - iLocation); + } + pchData[iLocation] = 0; +} + +void +CopyProtoEnt(void *pvData, struct protoent *ppe) +{ + int iLocation; + char *pchData; + int i; + + pchData = (char *) pvData; + FromShort(pchData, htons(ppe->p_proto)); + iLocation = sizeof(short); + iLocation += CopyString(pchData + iLocation, + ppe->p_name, + MAX_HOST_ENT - iLocation); + for (i = 0; ppe->p_aliases[i]; i++) + { + iLocation += CopyString(pchData + iLocation, + ppe->p_aliases[i], + MAX_HOST_ENT - iLocation); + } + pchData[iLocation] = 0; +} + +void +SwapSockOptIn( struct func_arg *pfa, + int iOpt) +{ + int iValue; + char *pchData; + + pchData = (char *) pfa->pvData; + if (iOpt == SO_LINGER) + { + FromShort(pchData, ntohs(ToShort(pchData))); + FromShort(pchData + sizeof(short), + ntohs(ToShort(pchData + sizeof(short)))); + } + else + { + FromLong(pchData, ntohl(ToLong(pchData))); + } +} + +void +SwapSockOptOut( struct func_arg *pfa, + int iOpt) +{ + int iValue; + char *pchData; + + pchData = (char *) pfa->pvData; + if (iOpt == SO_LINGER) + { + FromShort(pchData, htons(ToShort(pchData))); + FromShort(pchData + sizeof(short), + htons(ToShort(pchData + sizeof(short)))); + } + else + { + FromLong(pchData, htonl(ToLong(pchData))); + } +} + +void +ResponseReceived(struct tx_request *ptxr_) +{ + enum Functions ft; + short nArgs; + short nLen; + short id; + int iLen; + int iValue; + int iSocket; + int nOptVal; + short nError; + struct tx_request *ptxr; + struct func_arg *pfaArgs; + struct func_arg faResult; + struct sockaddr_in sin; + char *pchData; + int i; + int iErrorSent; + int iOffset; + struct hostent *phe; + struct netent *pne; + struct servent *pse; + struct protoent *ppe; + + nLen = ntohs(ptxr_->nLen); + ptxr = (struct tx_request *) malloc(nLen); + memcpy(ptxr, ptxr_, nLen); + ft = (enum Functions) ntohs(ptxr->iType); + nArgs = ntohs(ptxr->nArgs); + + pfaArgs = (struct func_arg *) malloc(sizeof(struct func_arg) * nArgs); + pchData = ptxr->pchData; + for (i = 0; i < nArgs; i++) + { + pfaArgs[i].at = (enum arg_type) ntohs(ToShort(pchData)); + pchData += sizeof(short); + pfaArgs[i].iLen = ntohs(ToShort(pchData)); + pchData += sizeof(short); + pfaArgs[i].pvData = pchData; + pchData += pfaArgs[i].iLen; + } + faResult.at = (enum arg_type) ntohs(ToShort(pchData)); + pchData += sizeof(short); + faResult.iLen = ntohs(ToShort(pchData)); + pchData += sizeof(short); + faResult.pvData = pchData; + + iErrorSent = 0; + errno = 0; + h_errno = 0; + + switch(ft) + { + case FN_IOCtl: + case FN_Accept: + case FN_Select: + case FN_Data: + ptxr->nError = htons(WSAEOPNOTSUPP); + ptxr->nLen = htons(sizeof(short) * 5); + PacketTransmitData(ptxr, sizeof(short) * 5, -1); + iErrorSent = 1; + break; + + case FN_Send: + SetIntVal(&faResult, + send(GetIntVal(&pfaArgs[0]), + pfaArgs[1].pvData, + GetIntVal(&pfaArgs[2]), + GetIntVal(&pfaArgs[3]))); + break; + + case FN_SendTo: + SetIntVal(&faResult, + sendto(GetIntVal(&pfaArgs[0]), + pfaArgs[1].pvData, + GetIntVal(&pfaArgs[2]), + GetIntVal(&pfaArgs[3]), + ConvertSA(&pfaArgs[4], &sin), + GetIntVal(&pfaArgs[5]))); + break; + + case FN_Bind: + SetIntVal(&faResult, + bind(GetIntVal(&pfaArgs[0]), + ConvertSA(&pfaArgs[1], &sin), + GetIntVal(&pfaArgs[2]))); + break; + + case FN_Connect: + iSocket = GetIntVal(&pfaArgs[0]); + iValue = connect(iSocket, + ConvertSA(&pfaArgs[1], &sin), + GetIntVal(&pfaArgs[2])); + SetIntVal(&faResult, iValue); + if (iValue != -1) + BumpLargestFD(iSocket); + break; + + case FN_Close: + SetIntVal(&faResult, + close(GetIntVal(&pfaArgs[0]))); + FlushStream(GetIntVal(&pfaArgs[0])); + SetClosed(GetIntVal(&pfaArgs[0])); + break; + + case FN_Shutdown: + SetIntVal(&faResult, + shutdown(GetIntVal(&pfaArgs[0]), + GetIntVal(&pfaArgs[1]))); + if (GetIntVal(&pfaArgs[1]) != 1) + SetClosed(GetIntVal(&pfaArgs[0])); + break; + + case FN_Listen: + iSocket = GetIntVal(&pfaArgs[0]); + iValue = listen(iSocket, + GetIntVal(&pfaArgs[1])); + SetIntVal(&faResult, iValue); + if (iValue != -1) + { + BumpLargestFD(iSocket); + SetListener(iSocket); + } + break; + + case FN_Socket: + iSocket = socket(GetIntVal(&pfaArgs[0]), + GetIntVal(&pfaArgs[1]), + GetIntVal(&pfaArgs[2])); + SetIntVal(&faResult,iSocket); + if (iSocket != -1) + { + BumpLargestFD(iSocket); + nOptVal = 1; + iLen = sizeof(nOptVal); + setsockopt(iSocket, + SOL_SOCKET, + SO_OOBINLINE, + (char *) &nOptVal, + &iLen); + errno = 0; + } + break; + + case FN_GetPeerName: + iLen = GetIntVal(&pfaArgs[2]); + iValue = getpeername(GetIntVal(&pfaArgs[0]), + (struct sockaddr *) pfaArgs[1].pvData, + &iLen); + if (iValue != -1) + { + SetIntVal(&faResult, iValue); + SetIntVal(&pfaArgs[2], iValue); + iValue = ((struct sockaddr *) pfaArgs[1].pvData)-> + sa_family; + iValue = htons((short) iValue); + ((struct sockaddr *) pfaArgs[1].pvData)->sa_family = + (short) iValue; + } + break; + + case FN_GetSockName: + iLen = GetIntVal(&pfaArgs[2]); + iValue = getsockname(GetIntVal(&pfaArgs[0]), + (struct sockaddr *) pfaArgs[1].pvData, + &iLen); + if (iValue != -1) + { + SetIntVal(&faResult, iValue); + SetIntVal(&pfaArgs[2], iValue); + iValue = ((struct sockaddr *) pfaArgs[1].pvData)-> + sa_family; + iValue = htons((short) iValue); + ((struct sockaddr *) pfaArgs[1].pvData)->sa_family = + (short) iValue; + } + break; + + case FN_GetSockOpt: + iLen = GetIntVal(&pfaArgs[4]); + iValue = getsockopt( GetIntVal(&pfaArgs[0]), + GetIntVal(&pfaArgs[1]), + GetIntVal(&pfaArgs[2]), + (char *) pfaArgs[3].pvData, + &iLen); + if (iValue != -1) + { + SwapSockOptOut(&pfaArgs[3], + GetIntVal(&pfaArgs[2])); + SetIntVal(&pfaArgs[4], iLen); + } + SetIntVal(&faResult, iValue); + break; + + case FN_SetSockOpt: + iLen = GetIntVal(&pfaArgs[4]); + SwapSockOptIn(&pfaArgs[3], + GetIntVal(&pfaArgs[2])); + iValue = setsockopt( GetIntVal(&pfaArgs[0]), + SOL_SOCKET, + GetIntVal(&pfaArgs[2]), + (char *) pfaArgs[3].pvData, + &iLen); + SwapSockOptOut(&pfaArgs[3], + GetIntVal(&pfaArgs[2])); + SetIntVal(&faResult, iValue); + break; + + case FN_GetHostName: + SetIntVal(&faResult, + gethostname((char *) pfaArgs[0].pvData, + GetIntVal(&pfaArgs[1]))); + break; + + case FN_HostByAddr: + phe = gethostbyaddr((char *) pfaArgs[0].pvData, + GetIntVal(&pfaArgs[1]), + GetIntVal(&pfaArgs[2])); + if (phe) + { + h_errno = 0; + CopyHostEnt(faResult.pvData, phe); + } + break; + + case FN_HostByName: + phe = gethostbyname((char *) pfaArgs[0].pvData); + if (phe) + { + h_errno = 0; + CopyHostEnt(faResult.pvData, phe); + } + else if (!h_errno) + { + h_errno = TRY_AGAIN; + } + break; + + case FN_ServByPort: + pse = getservbyport(GetIntVal(&pfaArgs[0]), + (char *) pfaArgs[1].pvData); + if (pse) + { + h_errno = 0; + CopyServEnt(faResult.pvData, pse); + } + else + { + h_errno = NO_DATA; + } + break; + + case FN_ServByName: + pse = getservbyname((char *) pfaArgs[0].pvData, + (char *) pfaArgs[1].pvData); + if (pse) + { + h_errno = 0; + CopyServEnt(faResult.pvData, pse); + } + else + { + h_errno = NO_DATA; + } + break; + + case FN_ProtoByNumber: + ppe = getprotobynumber(GetIntVal(&pfaArgs[0])); + if (ppe) + { + h_errno = 0; + CopyProtoEnt(faResult.pvData, ppe); + } + else + { + h_errno = NO_DATA; + } + break; + + case FN_ProtoByName: + ppe = getprotobyname((char *) pfaArgs[0].pvData); + if (ppe) + { + h_errno = 0; + CopyProtoEnt(faResult.pvData, ppe); + } + else + { + h_errno = NO_DATA; + } + break; + } + if (!iErrorSent) + { + if (ft >= FN_HostByAddr || ft <= FN_ProtoByName) + ptxr->nError = htons(MapHError(h_errno)); + else + ptxr->nError = htons(MapError(errno)); + PacketTransmitData(ptxr, nLen, -1); + } + free(ptxr); + free(pfaArgs); +} + +void +SendSocketData(int iSocket, + void *pvData, + int iLen, + struct sockaddr_in *psa, + int iAddrLen, + enum function_type ft) +{ + struct tx_request *ptxr; + int iDataLen; + struct sockaddr_in sa; + + iDataLen = sizeof(struct sockaddr_in) + iLen; + sa = *psa; + sa.sin_family = htons(sa.sin_family); + ptxr = (struct tx_request *) malloc(sizeof(short) * 5 + iDataLen); + ptxr->nLen = htons(iDataLen + sizeof(short) * 5); + ptxr->id = htons(iSocket); + ptxr->nArgs = 0; + ptxr->nError = 0; + ptxr->iType = htons(ft); + memcpy(ptxr->pchData, &sa, sizeof(sa)); + memcpy(ptxr->pchData + sizeof(sa), pvData, iLen); + PacketTransmitData(ptxr, sizeof(short) * 5 + iDataLen, iSocket); + free(ptxr); +} + \ No newline at end of file diff --git a/copying b/copying new file mode 100644 index 0000000..cc896b6 --- /dev/null +++ b/copying @@ -0,0 +1,341 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + 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. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + + \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..21e04d3 --- /dev/null +++ b/makefile @@ -0,0 +1,18 @@ +all: twinsock.exe + +winsock.dll: winsock.c + bcc -WD -ml -v -w- -lc -lC winsock.c + +winsock.lib : winsock.dll + implib winsock.lib winsock.dll + +packet.obj: packet.c + bcc -WE -ml -v -w- -c packet.c + +twinsock.obj: twinsock.c + bcc -WE -ml -v -w- -c twinsock.c + +twinsock.exe: twinsock.obj packet.obj winsock.lib + bcc -WE -lc -lC -ml -v twinsock.obj packet.obj winsock.lib + rc twinsock + \ No newline at end of file diff --git a/makefile.unx b/makefile.unx new file mode 100644 index 0000000..e9c76b3 --- /dev/null +++ b/makefile.unx @@ -0,0 +1,4 @@ +CFLAGS=-g + +tshost: packet.o tshost.o term.o commands.o + cc $(CFLAGS) -o tshost packet.o tshost.o term.o commands.o diff --git a/packet.c b/packet.c new file mode 100644 index 0000000..14fe463 --- /dev/null +++ b/packet.c @@ -0,0 +1,532 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 +#ifdef __MSDOS__ +#include +#include +#else +#include +#include +#endif +#include "packet.h" + +#define MAX_STREAMS 256 + +short nInSeq = 0; +short nOutSeq = 0; + +extern int SendData(void *pvData, int nBytes); + +static unsigned long crc_32_tab[] = { /* CRC polynomial 0xedb88320 */ +0x00000000l, 0x77073096l, 0xee0e612cl, 0x990951bal, 0x076dc419l, 0x706af48fl, 0xe963a535l, 0x9e6495a3l, +0x0edb8832l, 0x79dcb8a4l, 0xe0d5e91el, 0x97d2d988l, 0x09b64c2bl, 0x7eb17cbdl, 0xe7b82d07l, 0x90bf1d91l, +0x1db71064l, 0x6ab020f2l, 0xf3b97148l, 0x84be41del, 0x1adad47dl, 0x6ddde4ebl, 0xf4d4b551l, 0x83d385c7l, +0x136c9856l, 0x646ba8c0l, 0xfd62f97al, 0x8a65c9ecl, 0x14015c4fl, 0x63066cd9l, 0xfa0f3d63l, 0x8d080df5l, +0x3b6e20c8l, 0x4c69105el, 0xd56041e4l, 0xa2677172l, 0x3c03e4d1l, 0x4b04d447l, 0xd20d85fdl, 0xa50ab56bl, +0x35b5a8fal, 0x42b2986cl, 0xdbbbc9d6l, 0xacbcf940l, 0x32d86ce3l, 0x45df5c75l, 0xdcd60dcfl, 0xabd13d59l, +0x26d930acl, 0x51de003al, 0xc8d75180l, 0xbfd06116l, 0x21b4f4b5l, 0x56b3c423l, 0xcfba9599l, 0xb8bda50fl, +0x2802b89el, 0x5f058808l, 0xc60cd9b2l, 0xb10be924l, 0x2f6f7c87l, 0x58684c11l, 0xc1611dabl, 0xb6662d3dl, +0x76dc4190l, 0x01db7106l, 0x98d220bcl, 0xefd5102al, 0x71b18589l, 0x06b6b51fl, 0x9fbfe4a5l, 0xe8b8d433l, +0x7807c9a2l, 0x0f00f934l, 0x9609a88el, 0xe10e9818l, 0x7f6a0dbbl, 0x086d3d2dl, 0x91646c97l, 0xe6635c01l, +0x6b6b51f4l, 0x1c6c6162l, 0x856530d8l, 0xf262004el, 0x6c0695edl, 0x1b01a57bl, 0x8208f4c1l, 0xf50fc457l, +0x65b0d9c6l, 0x12b7e950l, 0x8bbeb8eal, 0xfcb9887cl, 0x62dd1ddfl, 0x15da2d49l, 0x8cd37cf3l, 0xfbd44c65l, +0x4db26158l, 0x3ab551cel, 0xa3bc0074l, 0xd4bb30e2l, 0x4adfa541l, 0x3dd895d7l, 0xa4d1c46dl, 0xd3d6f4fbl, +0x4369e96al, 0x346ed9fcl, 0xad678846l, 0xda60b8d0l, 0x44042d73l, 0x33031de5l, 0xaa0a4c5fl, 0xdd0d7cc9l, +0x5005713cl, 0x270241aal, 0xbe0b1010l, 0xc90c2086l, 0x5768b525l, 0x206f85b3l, 0xb966d409l, 0xce61e49fl, +0x5edef90el, 0x29d9c998l, 0xb0d09822l, 0xc7d7a8b4l, 0x59b33d17l, 0x2eb40d81l, 0xb7bd5c3bl, 0xc0ba6cadl, +0xedb88320l, 0x9abfb3b6l, 0x03b6e20cl, 0x74b1d29al, 0xead54739l, 0x9dd277afl, 0x04db2615l, 0x73dc1683l, +0xe3630b12l, 0x94643b84l, 0x0d6d6a3el, 0x7a6a5aa8l, 0xe40ecf0bl, 0x9309ff9dl, 0x0a00ae27l, 0x7d079eb1l, +0xf00f9344l, 0x8708a3d2l, 0x1e01f268l, 0x6906c2fel, 0xf762575dl, 0x806567cbl, 0x196c3671l, 0x6e6b06e7l, +0xfed41b76l, 0x89d32be0l, 0x10da7a5al, 0x67dd4accl, 0xf9b9df6fl, 0x8ebeeff9l, 0x17b7be43l, 0x60b08ed5l, +0xd6d6a3e8l, 0xa1d1937el, 0x38d8c2c4l, 0x4fdff252l, 0xd1bb67f1l, 0xa6bc5767l, 0x3fb506ddl, 0x48b2364bl, +0xd80d2bdal, 0xaf0a1b4cl, 0x36034af6l, 0x41047a60l, 0xdf60efc3l, 0xa867df55l, 0x316e8eefl, 0x4669be79l, +0xcb61b38cl, 0xbc66831al, 0x256fd2a0l, 0x5268e236l, 0xcc0c7795l, 0xbb0b4703l, 0x220216b9l, 0x5505262fl, +0xc5ba3bbel, 0xb2bd0b28l, 0x2bb45a92l, 0x5cb36a04l, 0xc2d7ffa7l, 0xb5d0cf31l, 0x2cd99e8bl, 0x5bdeae1dl, +0x9b64c2b0l, 0xec63f226l, 0x756aa39cl, 0x026d930al, 0x9c0906a9l, 0xeb0e363fl, 0x72076785l, 0x05005713l, +0x95bf4a82l, 0xe2b87a14l, 0x7bb12bael, 0x0cb61b38l, 0x92d28e9bl, 0xe5d5be0dl, 0x7cdcefb7l, 0x0bdbdf21l, +0x86d3d2d4l, 0xf1d4e242l, 0x68ddb3f8l, 0x1fda836el, 0x81be16cdl, 0xf6b9265bl, 0x6fb077e1l, 0x18b74777l, +0x88085ae6l, 0xff0f6a70l, 0x66063bcal, 0x11010b5cl, 0x8f659effl, 0xf862ae69l, 0x616bffd3l, 0x166ccf45l, +0xa00ae278l, 0xd70dd2eel, 0x4e048354l, 0x3903b3c2l, 0xa7672661l, 0xd06016f7l, 0x4969474dl, 0x3e6e77dbl, +0xaed16a4al, 0xd9d65adcl, 0x40df0b66l, 0x37d83bf0l, 0xa9bcae53l, 0xdebb9ec5l, 0x47b2cf7fl, 0x30b5ffe9l, +0xbdbdf21cl, 0xcabac28al, 0x53b39330l, 0x24b4a3a6l, 0xbad03605l, 0xcdd70693l, 0x54de5729l, 0x23d967bfl, +0xb3667a2el, 0xc4614ab8l, 0x5d681b02l, 0x2a6f2b94l, 0xb40bbe37l, 0xc30c8ea1l, 0x5a05df1bl, 0x2d02ef8dl +}; + +#define UPDC32(octet, crc) (crc_32_tab[((crc) ^ (octet)) & 0xff] ^ ((crc) >> 8)) + +static short +CalcCRC(data, size) +char *data; +int size; +{ + unsigned long crc = 0xffff; + + while (size--) + { + crc = UPDC32(*data++, crc); + } + crc = ~crc; + return (crc & 0xffff); +} + +struct packet_queue +{ + int idPacket; + int iPacketLen; + int iStream; + struct packet *pkt; + struct packet_queue *ppqNext; +}; + +struct packet_queue *ppqList = 0; +int iInitialised = 0; +struct packet_queue *appqStreams[MAX_STREAMS]; +char aiStreams[MAX_STREAMS / 8]; + +#define STREAM_BIT(x) (1 << (((x) + 1) % 8)) +#define STREAM_BYTE(x) aiStreams[((x) + 1) / 8] + +#define SET_STREAM(x) (STREAM_BYTE(x) |= STREAM_BIT(x)) +#define CLR_STREAM(x) (STREAM_BYTE(x) &= ~STREAM_BIT(x)) +#define GET_STREAM(x) ((STREAM_BYTE(x) & STREAM_BIT(x)) != 0) + +static char ach6bit[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./"; + +int TransmitData(void *pvData, int iDataLen) +{ + char *pchDataIn; + char *pchDataOut; + char c; + int iIn, iOut; + int nBits; + int nBitsLeft; + int nBitsNow; + int nDataOut; + char cNow, cTmp; + + nDataOut = iDataLen * 4 / 3 + 1; + if (iDataLen % 6) + nDataOut++; + pchDataIn = (char *) pvData; + pchDataOut = (char *) malloc(nDataOut); + nBits = nBitsLeft = 0; + cNow = 0; + pchDataOut[0] = '@'; /* Signals the receiving end to realign to bit 0 */ + for (iIn = 0, iOut = 1; iOut < nDataOut;) + { + if (nBitsLeft) + { + cTmp = c & ((1 << nBitsLeft) - 1); + nBitsNow = 6 - nBits; + if (nBitsLeft < nBitsNow) + nBitsNow = nBitsLeft; + cNow <<= nBitsNow; + cTmp >>= nBitsLeft - nBitsNow; + cTmp &= ((1 << nBitsNow) - 1); + cNow |= cTmp; + nBits += nBitsNow; + nBitsLeft -= nBitsNow; + if (nBits == 6) + { + pchDataOut[iOut++] = ach6bit[cNow]; + cNow = 0; + nBits = 0; + } + } + else + { + if (iIn < iDataLen) + c = pchDataIn[iIn++]; + else + c = 0; + nBitsLeft = 8; + } + } + nDataOut = SendData(pchDataOut, nDataOut); + free(pchDataOut); + return nDataOut; +} + +void TransmitHead(void) +{ + TransmitData(ppqList->pkt, ppqList->iPacketLen); + SetTransmitTimeout(); +} + +void TimeoutReceived(void) +{ + TransmitHead(); +} + +void InitHead() +{ + struct packet *pkt; + short nCRC; + + ppqList->idPacket = nOutSeq; + pkt = ppqList->pkt; + pkt->iPacketID = htons(nOutSeq); + nCRC = CalcCRC(pkt, ppqList->iPacketLen); + pkt->nCRC = htons(nCRC); + nOutSeq++; +} + +void InsertInQueue(struct packet_queue **pppqNext, struct packet_queue *ppq) +{ + for (; *pppqNext; pppqNext = &((*pppqNext)->ppqNext)); + *pppqNext = ppq; +} + +void FlushStream(int iStream) +{ + struct packet_queue *ppqPop; + + while ((ppqPop = appqStreams[iStream + 1]) != 0) + { + appqStreams[iStream + 1] = ppqPop->ppqNext; + free(ppqPop->pkt); + free(ppqPop); + } +} + +void PutInQueue(struct packet_queue *ppq, int iStream) +{ + if (iStream == -2) /* Urgent */ + { + InsertInQueue(&ppqList, ppq); + } + else if (GET_STREAM(iStream)) + { + InsertInQueue(appqStreams + iStream + 1, ppq); + } + else + { + SET_STREAM(iStream); + InsertInQueue(&ppqList, ppq); + } + if (ppqList == ppq) + { + InitHead(); + TransmitHead(); + } +} + +void PopListHead(void) +{ + int iStream; + struct packet_queue *ppqNext, *ppqPop; + + iStream = ppqList->iStream; + ppqNext = ppqList->ppqNext; + free(ppqList->pkt); + free(ppqList); + ppqList = ppqNext; + + if (iStream != -2) + { + if (appqStreams[iStream + 1]) + { + ppqPop = appqStreams[iStream + 1]; + appqStreams[iStream + 1] = ppqPop->ppqNext; + ppqPop->ppqNext = 0; + InsertInQueue(&ppqList, ppqPop); + } + else + { + CLR_STREAM(iStream); + } + } + + if (ppqList) + { + InitHead(); + TransmitHead(); + } +} + +void SendPacket(void *pvData, int iDataLen, int iStream) +{ + struct packet *pkt; + struct packet_queue *ppq; + short nCRC; + + if (!iInitialised) + { + iInitialised = 1; + memset(aiStreams, 0, sizeof(aiStreams)); + memset(appqStreams, 0, sizeof(appqStreams)); + } + + pkt = (struct packet *) malloc(sizeof(struct packet)); + ppq = (struct packet_queue *) malloc(sizeof(struct packet_queue)); + + ppq->iPacketLen = iDataLen + sizeof(short) * 4; + ppq->iStream = iStream; + ppq->pkt = pkt; + ppq->ppqNext = 0; + + pkt->iPacketLen = htons(iDataLen); + pkt->nCRC = 0; + pkt->nType = htons((short) PT_Data); + memcpy(pkt->achData, pvData, iDataLen); + + PutInQueue(ppq, iStream); +} + +void TransmitAck(short id) +{ + struct packet pkt; + int nCRC; + + pkt.iPacketLen = 0; + pkt.nCRC = 0; + pkt.nType = htons((short) PT_Ack); + pkt.iPacketID = htons(id); + nCRC = CalcCRC(&pkt, sizeof(short) * 4); + pkt.nCRC = htons(nCRC); + TransmitData(&pkt, sizeof(short) * 4); +} + +void ProcessData(void *pvData, int nDataLen) +{ + static struct packet pkt; + static int nBytes = 0; + int nToCopy; + int nData; + enum packet_type pt; + short iLen; + short nCRC; + short id; + short iLocation; + + if (!pvData) /* Receive timeout */ + { + nBytes = 0; + return; + } + + while (nDataLen) + { + if (nBytes < sizeof(short) * 4) + { + nToCopy = sizeof(short) * 4 - nBytes; + if (nToCopy > nDataLen) + nToCopy = nDataLen; + memcpy((char *) &pkt + nBytes, pvData, nToCopy); + pvData = (char *) pvData + nToCopy; + nDataLen -= nToCopy; + nBytes += nToCopy; + } + if (nBytes < sizeof(short) * 4) + break; + pt = (enum packet_type) ntohs(pkt.nType); + iLen = ntohs(pkt.iPacketLen); + nCRC = ntohs(pkt.nCRC); + id = ntohs(pkt.iPacketID); + if (iLen > PACKET_MAX || iLen < 0) /* Sanity check */ + { + nBytes = 0; + nDataLen = 0; + KillReceiveTimeout(); + if (ppqList) + KillTransmitTimeout(); + FlushInput(); + if (ppqList) + SetTransmitTimeout(); + return; + } + if (nBytes < sizeof(short) * 4 + iLen) + { + nToCopy = sizeof(short) * 4 + iLen - nBytes; + if (nDataLen < nToCopy) + nToCopy = nDataLen; + memcpy((char *) &pkt + nBytes, pvData, nToCopy); + pvData = (char *) pvData + nToCopy; + nDataLen -= nToCopy; + nBytes += nToCopy; + } + if (nBytes == sizeof(short) * 4 + iLen) + { + nBytes = 0; + pkt.nCRC = 0; + if (CalcCRC(&pkt, iLen + sizeof(short) * 4) == nCRC) + { + switch(pt) + { + case PT_Data: + iLocation = nInSeq - id; + if (iLocation & 0x8000) + { + /* We're in trouble */ + Shutdown(); + } + else + { + TransmitAck(id); + if (!iLocation) + { + nInSeq++; + DataReceived(pkt.achData, iLen); + } + } + break; + + case PT_Nak: + if (ppqList && + id == ppqList->idPacket) + TransmitHead(); + break; + + case PT_Ack: + if (ppqList && + id == ppqList->idPacket) + { + KillTransmitTimeout(); + PopListHead(); + } + break; + + case PT_Shutdown: + Shutdown(0); + break; + } + } + else + { + /* If we flush input we should also + * reset any tranmit timeout, otherwise + * we may resend the packet while flushing, + * and flush the Ack. We also kill any + * receive timeout because we have already + * "flushed" any existing input. + */ + KillReceiveTimeout(); + if (ppqList) + KillTransmitTimeout(); + FlushInput(); + if (ppqList) + SetTransmitTimeout(); + return; + } + } + } + if (nBytes) + SetReceiveTimeout(); +} + +void PacketReceiveData(void *pvData, int nDataLen) +{ + static int nBits = 0; + static char c = 0; + static int nCtlX = 0; + char cIn; + char cTmp; + int nBitsLeft = 0; + int iOut = 0; + int nBitsNow; + char *pchDataOut; + char *pchDataIn; + + if (!pvData) + { + ProcessData(0, nDataLen); + nBits = nBitsLeft = 0; + return; + } + + KillReceiveTimeout(); + + pchDataIn = (char *) pvData; + pchDataOut = (char *) malloc(nDataLen); + while (nDataLen || nBitsLeft) + { + if (nBitsLeft) + { + nBitsNow = 8 - nBits; + if (nBitsLeft < nBitsNow) + nBitsNow = nBitsLeft; + c <<= nBitsNow; + cTmp = cIn >> (nBitsLeft - nBitsNow); + cTmp &= ((1 << nBitsNow) - 1); + c |= cTmp; + nBits += nBitsNow; + nBitsLeft -= nBitsNow; + if (nBits == 8) + { + pchDataOut[iOut++] = c; + nBits = 0; + } + } + else + { + cIn = *pchDataIn++; + nDataLen--; + if (cIn == '\030') /* ^X */ + { + nCtlX++; + if (nCtlX >= 5) + Shutdown(); + continue; + } + else + { + nCtlX = 0; + } + if (cIn == '@') + { + cIn = c = 0; + nBitsLeft = 0; + nBits = 0; + } + else + { + if (cIn >= 'A' && cIn <= 'Z') + cIn -= 'A'; + else if (cIn >= 'a' && cIn <= 'z') + cIn = cIn - 'a' + 26; + else if (cIn >= '0' && cIn <= '9') + cIn = cIn - '0' + 52; + else if (cIn == '.') + cIn = 62; + else if (cIn == '/') + cIn = 63; + else + continue; + nBitsLeft = 6; + } + } + } + ProcessData(pchDataOut, iOut); + free(pchDataOut); +} + +void PacketTransmitData(void *pvData, int iDataLen, int iStream) +{ + iStream = -2; + while (iDataLen > PACKET_MAX) + { + SendPacket(pvData, PACKET_MAX, iStream); + pvData = (char *) pvData + PACKET_MAX; + iDataLen -= PACKET_MAX; + } + SendPacket(pvData, iDataLen, iStream); +} + \ No newline at end of file diff --git a/packet.h b/packet.h new file mode 100644 index 0000000..384202b --- /dev/null +++ b/packet.h @@ -0,0 +1,41 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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. + */ + +enum packet_type +{ + PT_Data, + PT_Ack, + PT_Nak, + PT_Shutdown +}; + +#define PACKET_MAX 512 + +struct packet +{ + short iPacketID; + short iPacketLen; + short nCRC; + short nType; + char achData[PACKET_MAX]; +}; + + + \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..e2002eb --- /dev/null +++ b/readme.txt @@ -0,0 +1,132 @@ + TwinSock 1.0 + ============ + Troy's Windows Sockets + Copyright 1994 Troy Rollo + +What is TwinSock? +----------------- + +TwinSock is a free implementation of proxy sockets for Windows. + +Other Windows Sockets drivers use a network card, or a well known Internet +over serial lines protocol, such as SLIP, C-SLIP or PPP. These drivers may +access the network card or communications card directly, or via a VxD or DOS +based TCP/IP stack. their uses are limited to cases where either the machine +is directly connected to a network, or the host at the other end of the phone +line supports the same serial line internet protocol. + +The other shortcoming of these drivers is that they require an official IP +address to operate, and frequently you will not be able to connect very far +beyond the host you connect directly to. + +TwinSock, on the other hand, makes use of the IP address of the host to +provide socket services to the client. When an application running under +Windows requests socket services of TwinSock, TwinSock will transparently +pass these requests on to the TwinSock Host program running on the remote +machine for processing. The result is that you have all the same networking +capabilities as you would if your Windows machine were physically connected +to the network in place of thte host machine. + +Licensing +--------- + +TwinSock 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. + +Installing +---------- + + 1. Copy WINSOCK.DLL, TWINSOCK.EXE and TWINSOCK.INI to your Windows + directory. + + 2. Edit TWINSOCK.INI and make sure all the parameters match up with + what you need for your modem. + + 3. Copy the following files to a directory on your UNIX host, + renaming makefile.unx to Makefile. + + makefile.unx + tshost.c + packet.c + commands.c + term.c + packet.h + twinsock.h + tx.h + wserror.h + + 4. Type "make" to build the server. If it doesn't compile first + off, try to modify it until it does. The files you will probably + need to touch are (in decreasing order of probability): + + term.c + tshost.c + commands.c + + You should avoid touching packet.c if possible. + + 5. Using your favourite terminal program, start tshost. + You should see a message telling you to start your TwinSock + client. + + 6. Start the TwinSock client. + + 7. Start your favourite Windows Sockets applications. + +Shutting down +------------- + + 1. Stop the TwinSock client. + + 2. Start your terminal program. + + 3. Type ^X (control-X) five times. + +Enhancements and Bug Reports +---------------------------- + + Enhancements and bug reports should be directed to: + + twinsock@cbme.unsw.EDU.AU + +TODO & BUGS +----------- + + The protocol used over the serial connection converts everything + to base 64 using the characters A-Z, a-z, '.' and '/' in order to + get past the most obstinate terminal servers. This is probably + overkill in almost all cases, and there should be an option to + fix this. + + Out Of Band data should be handled properly. + + The protocol over the serial line was kept simple to ensure a + quick release (9 days after the project was started). It could + benefit from any number of advanced features. + + The internals don't clean up properly if an application exits + without cleaning up itself. + + There are some potentially annoying bugs, although this version + is usable with most Windows Sockets applications. + + TwinSock Host should be ported to more hosts, including non + UNIX hosts. + +History +------- + + 05-Nov-1994 Project initiated + 14-Nov-1994 Version 1.0 released + \ No newline at end of file diff --git a/term.c b/term.c new file mode 100644 index 0000000..63b4d0f --- /dev/null +++ b/term.c @@ -0,0 +1,39 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 + +struct sgttyb Old; +struct sgttyb New; + +void InitTerm(void) +{ + ioctl(0, TIOCGETP, &Old); + New = Old; + New.sg_flags |= CBREAK | LITOUT | PASS8 | DECCTQ | RAW; + New.sg_flags &= ~(ECHO | CRMOD | TILDE | NOHANG | CTLECH); + ioctl(0, TIOCSETP, &New); +} + +void UnInitTerm(void) +{ + ioctl(0, TIOCSETP, &Old); +} + \ No newline at end of file diff --git a/tshost.c b/tshost.c new file mode 100644 index 0000000..cd45f84 --- /dev/null +++ b/tshost.c @@ -0,0 +1,407 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 +#include +#include +#include +#include +#include "twinsock.h" +#include "tx.h" + +#define BUFFER_SIZE 1024 + +static fd_set fdsActive; +static fd_set fdsListener; +static int nLargestFD; +static char achBuffer[BUFFER_SIZE]; +static int bFlushing = 0; +void FlushInput(void); + +int GetNextExpiry(struct timeval *ptvValue); +void CheckTimers(void); +extern char *sys_errlist[]; + +#define TIMER_ID_SEND 0 +#define TIMER_ID_RECEIVE 1 +#define TIMER_ID_FLUSH 2 + +int BumpLargestFD(int iValue) +{ + if (iValue > nLargestFD) + nLargestFD = iValue; + FD_SET(iValue, &fdsActive); +}; + +int SetListener(int iValue) +{ + FD_SET(iValue, &fdsListener); +} + +int SetClosed(int iValue) +{ + FD_CLR(iValue, &fdsListener); + FD_CLR(iValue, &fdsActive); +} + +main(int argc, char **argv) +{ + fd_set fdsRead, fdsWrite, fdsExcept, fdsDummy; + int nRead; + int i; + int iResult; + struct sockaddr_in saSource; + int nSourceLen; + int s; + struct timeval tvZero; + struct timeval tv; + + fprintf(stderr, "TwinSock Host 1.0\n"); + fprintf(stderr, "Copyright 1994 Troy Rollo\n"); + fprintf(stderr, "This program is free software\n"); + fprintf(stderr, "See the file COPYING for details\n"); + fprintf(stderr, "\nStart your TwinSock client now\n"); + + InitTerm(); + nLargestFD = 0; + FD_ZERO(&fdsActive); + FD_ZERO(&fdsWrite); + FD_ZERO(&fdsExcept); + + FD_SET(0, &fdsActive); + + while(1) + { + fdsRead = fdsActive; + iResult = select(nLargestFD + 1, + &fdsRead, + &fdsWrite, + &fdsExcept, + GetNextExpiry(&tv) ? &tv : 0); + CheckTimers(); + if (iResult <= 0) + continue; + if (FD_ISSET(0, &fdsRead)) + { + nRead = read(0, achBuffer, BUFFER_SIZE); + if (nRead > 0) + { + if (bFlushing) + FlushInput(); + else + PacketReceiveData(achBuffer, nRead); + } + } + for (i = 3; i <= nLargestFD; i++) + { + if (FD_ISSET(i, &fdsRead)) + { + FD_ZERO(&fdsDummy); + FD_SET(i, &fdsDummy); + tvZero.tv_sec = tvZero.tv_usec = 0; + if (select(i + 1, + &fdsDummy, + &fdsWrite, + &fdsExcept, + &tvZero) != 1) + continue; /* Select lied */ + + nSourceLen = sizeof(saSource); + if (FD_ISSET(i, &fdsListener)) + { + s = accept(i, + &saSource, + &nSourceLen); + if (s == -1) + continue; + s = htonl(s); + BumpLargestFD(s); + SendSocketData(i, + &s, + sizeof(s), + &saSource, + nSourceLen, + FN_Accept); + } + else + { + nRead = recvfrom(i, + achBuffer, + BUFFER_SIZE, + 0, + &saSource, + &nSourceLen); + if (nRead >= 0) + SendSocketData(i, + achBuffer, + nRead, + &saSource, + nSourceLen, + FN_Data); + if (nRead == 0) + SetClosed(i); + } + } + } + } +} + + +void +SetTransmitTimeout(void) +{ + KillTimer(TIMER_ID_SEND); + SetTimer(TIMER_ID_SEND, 3000); +} + +void +KillTransmitTimeout(void) +{ + KillTimer(TIMER_ID_SEND); +} + +void SetReceiveTimeout(void) +{ + KillTimer(TIMER_ID_RECEIVE); + SetTimer(TIMER_ID_RECEIVE, 1500); +} + +void KillReceiveTimeout(void) +{ + KillTimer(TIMER_ID_RECEIVE); +} + +static struct timeval atvTimers[3]; +static int iTimersOn; +static int iTimerRunning; +static int iInTimer = 0; + + +int FireTimer(int iTimer) +{ + switch(iTimer) + { + case TIMER_ID_SEND: + TimeoutReceived(); + break; + + case TIMER_ID_RECEIVE: + PacketReceiveData(0, 0); + break; + + case TIMER_ID_FLUSH: + bFlushing = 0; + break; + } +} + +int GetNextExpiry(struct timeval *ptvValue) +{ + struct timeval tvNow; + struct timezone tzDummy; + struct timeval tvGo; + int i; + + if (!iTimersOn) + return 0; + + tvGo.tv_sec = 0x7fffffff; + tvGo.tv_usec = 0; + for (i = 0; i < 3; i++) + { + if (!(iTimersOn & (1 << i))) + continue; + if (atvTimers[i].tv_sec < tvGo.tv_sec || + (atvTimers[i].tv_sec == tvGo.tv_sec && + atvTimers[i].tv_usec < tvGo.tv_usec)) + { + tvGo = atvTimers[i]; + iTimerRunning = i; + } + } + gettimeofday(&tvNow, &tzDummy); + ptvValue->tv_sec = tvGo.tv_sec - tvNow.tv_sec; + ptvValue->tv_usec = tvGo.tv_usec - tvNow.tv_usec; + while (ptvValue->tv_usec < 0) + { + ptvValue->tv_usec += 1000000l; + ptvValue->tv_sec--; + } + while (ptvValue->tv_usec >= 1000000l) + { + ptvValue->tv_usec -= 1000000l; + ptvValue->tv_sec++; + } + if (ptvValue->tv_sec < 0) + { + ptvValue->tv_sec = 0; + ptvValue->tv_usec = 100; + } + if (!ptvValue->tv_sec && + ptvValue->tv_usec < 100) + ptvValue->tv_usec = 100; + return 1; +} + +void CheckTimers(void) +{ + struct timeval tvNow; + struct timezone tzDummy; + int i; + + gettimeofday(&tvNow, &tzDummy); + for (i = 0; i < 3; i++) + { + if (!(iTimersOn & (1 << i))) + continue; + if (atvTimers[i].tv_sec < tvNow.tv_sec || + (atvTimers[i].tv_sec == tvNow.tv_sec && + atvTimers[i].tv_usec < tvNow.tv_usec)) + { + KillTimer(i); + FireTimer(i); + } + } +} + +int SetTimer(int idTimer, int iTime) +{ + struct timeval tvNow; + struct timezone tzDummy; + + gettimeofday(&tvNow, &tzDummy); + atvTimers[idTimer] = tvNow; + atvTimers[idTimer].tv_usec += (long) iTime % 1000l * 1000l; + atvTimers[idTimer].tv_sec += iTime / 1000; + while (atvTimers[idTimer].tv_usec > 1000000l) + { + atvTimers[idTimer].tv_sec++; + atvTimers[idTimer].tv_usec -= 1000000l; + } + iTimersOn |= (1 << idTimer); +} + +int KillTimer(int idTimer) +{ + iTimersOn &= ~(1 << idTimer); +} + +void Shutdown(void) +{ + UnInitTerm(); + fprintf(stderr, "\nTwinSock Host Finished\n"); + exit(0); +} + +int +SendData(void *pvData, int iDataLen) +{ + int iLen; + int nWritten; + + if (bFlushing) + return iDataLen; /* Lie */ + iLen = iDataLen; + + while (iLen > 0) + { + nWritten = write(1, pvData, iDataLen); + if (nWritten > 0) + iLen -= nWritten; + } + return iDataLen; +} + +void +FlushInput(void) +{ + bFlushing = 1; + SetTimer(TIMER_ID_FLUSH, 1500); +} + +static void +SendInitResponse(void) +{ + struct tx_request txr; + + txr.iType = htons(FN_Init); + txr.nArgs = 0; + txr.nLen = htons(10); + txr.id = -1; + txr.nError = 0; + PacketTransmitData(&txr, 10, -2); +} + +void +DataReceived(void *pvData, int iLen) +{ + static struct tx_request *ptxr = 0; + static struct tx_request txrHeader; + static int nBytes = 0; + short nPktLen; + enum Functions ft; + int nCopy; + + while (iLen) + { + if (nBytes < 10) + { + nCopy = 10 - nBytes; + if (nCopy > iLen) + nCopy = iLen; + memcpy((char *) &txrHeader + nBytes, pvData, nCopy); + nBytes += nCopy; + pvData = (char *) pvData + nCopy; + iLen -= nCopy; + if (nBytes == 10) + { + nPktLen = ntohs(txrHeader.nLen); + ptxr = (struct tx_request *) malloc(sizeof(struct tx_request) + nPktLen - 10); + memcpy(ptxr, &txrHeader, 10); + } + } + if (nBytes >= 10) + { + nPktLen = ntohs(txrHeader.nLen); + ft = (enum Functions) ntohs(txrHeader.iType); + nCopy = nPktLen - nBytes; + if (nCopy > iLen) + nCopy = iLen; + if (nCopy) + { + memcpy((char *) ptxr + nBytes, pvData, nCopy); + nBytes += nCopy; + pvData = (char *) pvData + nCopy; + iLen -= nCopy; + } + if (nBytes == nPktLen) + { + if (ft == FN_Init) + SendInitResponse(); + else + ResponseReceived(ptxr); + free(ptxr); + ptxr = 0; + nBytes = 0; + } + } + } +} + \ No newline at end of file diff --git a/twinsock.c b/twinsock.c new file mode 100644 index 0000000..3e9dc3e --- /dev/null +++ b/twinsock.c @@ -0,0 +1,338 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 +#include +#include "twinsock.h" +#include "tx.h" + +extern RegisterManager(HWND hwnd); + +#define READ_MAX 1024 +#define TIMER_ID_SEND 1 +#define TIMER_ID_RECEIVE 2 +#define TIMER_ID_FLUSH 3 + +static int idComm; +static HWND hwnd; +static BOOL bFlushing = FALSE; + +extern void PacketReceiveData(void *pvData, int iLen); + +void SetTransmitTimeout(void) +{ + KillTimer(hwnd, TIMER_ID_SEND); + SetTimer(hwnd, TIMER_ID_SEND, 3000, 0); +} + +void KillTransmitTimeout(void) +{ + KillTimer(hwnd, TIMER_ID_SEND); +} + +void SetReceiveTimeout(void) +{ + KillTimer(hwnd, TIMER_ID_RECEIVE); + SetTimer(hwnd, TIMER_ID_RECEIVE, 1500, 0); +} + +void KillReceiveTimeout(void) +{ + KillTimer(hwnd, TIMER_ID_RECEIVE); +} + +void FlushInput(void) +{ + KillTimer(hwnd, TIMER_ID_FLUSH); + bFlushing = TRUE; + SetTimer(hwnd, TIMER_ID_FLUSH, 1500, 0); +} + +static void DoReading(void) +{ + static char achBuffer[READ_MAX]; + int nRead; + COMSTAT cs; + + do + { + nRead = ReadComm(idComm, achBuffer, READ_MAX); + if (nRead <= 0) + { + GetCommError(idComm, &cs); + nRead = -nRead; + } + if (nRead) + { + if (bFlushing) + FlushInput(); + else + PacketReceiveData(achBuffer, nRead); + } + } while (nRead); +} + +int SendData(void *pvData, int iDataLen) +{ + int nWritten; + COMSTAT cs; + int iLen; + + if (bFlushing) + return iDataLen; /* Lie */ + iLen = iDataLen; + do + { + nWritten = WriteComm(idComm, pvData, iLen); + if (nWritten < 0) + { + GetCommError(idComm, &cs); + nWritten = -nWritten; + } + iLen -= nWritten; + pvData = (char *) pvData + nWritten; + } while (iLen); + return iDataLen; +} + +LRESULT CALLBACK _export +WindowProc( HWND hWnd, + UINT wMsg, + WPARAM wParam, + LPARAM lParam) +{ + + switch(wMsg) + { + case WM_SYSCOMMAND: + if (wParam == SC_MAXIMIZE || + wParam == SC_RESTORE) + return 0; + break; + + case WM_COMMNOTIFY: + switch(LOWORD(lParam)) + { + case CN_RECEIVE: + DoReading(); + break; + } + break; + + case WM_TIMER: + switch(wParam) + { + case TIMER_ID_SEND: + TimeoutReceived(TIMER_ID_SEND); + break; + + case TIMER_ID_RECEIVE: + KillTimer(hWnd, TIMER_ID_RECEIVE); + PacketReceiveData(0, 0); + break; + + case TIMER_ID_FLUSH: + KillTimer(hWnd, TIMER_ID_FLUSH); + bFlushing = FALSE; + break; + } + break; + + case WM_USER: + PacketTransmitData((void *) lParam, wParam, 0); + DoReading(); + break; + + case WM_CLOSE: + PostQuitMessage(0); + break; + } + return DefWindowProc(hWnd, wMsg, wParam, lParam); +} + +void +DataReceived(void *pvData, int iLen) +{ + static struct tx_request *ptxr = 0; + static struct tx_request txrHeader; + static int nBytes = 0; + short nPktLen; + enum Functions ft; + int nCopy; + + while (iLen) + { + if (nBytes < 10) + { + nCopy = 10 - nBytes; + if (nCopy > iLen) + nCopy = iLen; + memcpy((char *) &txrHeader + nBytes, pvData, nCopy); + nBytes += nCopy; + pvData = (char *) pvData + nCopy; + iLen -= nCopy; + if (nBytes == 10) + { + nPktLen = ntohs(txrHeader.nLen); + ptxr = (struct tx_request *) malloc(sizeof(struct tx_request) + nPktLen - 1); + memcpy(ptxr, &txrHeader, 10); + } + } + if (nBytes >= 10) + { + nPktLen = ntohs(txrHeader.nLen); + ft = (enum Functions) ntohs(txrHeader.iType); + nCopy = nPktLen - nBytes; + if (nCopy > iLen) + nCopy = iLen; + if (nCopy) + { + memcpy((char *) ptxr + nBytes, pvData, nCopy); + nBytes += nCopy; + pvData = (char *) pvData + nCopy; + iLen -= nCopy; + } + if (nBytes == nPktLen) + { + if (ft == FN_Init) + SetInitialised(); + else + ResponseReceived(ptxr); + free(ptxr); + ptxr = 0; + nBytes = 0; + } + } + } +} + +static UINT +GetConfigInt(char const *pchItem, UINT nDefault) +{ + return GetPrivateProfileInt("Config", pchItem, nDefault, "TWINSOCK.INI"); +} + +static void +SendInitRequest(void) +{ + struct tx_request txr; + + txr.iType = htons(FN_Init); + txr.nArgs = 0; + txr.nLen = htons(10); + txr.id = -1; + txr.nError = 0; + PacketTransmitData(&txr, 10, 0); +} + +void +Shutdown(void) +{ + PostQuitMessage(0); +} + +#pragma argsused +int far pascal +WinMain(HINSTANCE hInstance, + HINSTANCE hPrec, + LPSTR lpCmdLine, + int nShow) +{ + WNDCLASS wc; + MSG msg; + DCB dcb; + char achProfileEntry[256]; + + wc.style = 0; + wc.lpfnWndProc = WindowProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = hInstance; + wc.hIcon = LoadIcon(hInstance, "TSICON"); + wc.hCursor = 0; + wc.hbrBackground = 0; + wc.lpszMenuName = 0; + wc.lpszClassName = "TwinSock Communications"; + RegisterClass(&wc); + hwnd = CreateWindow( "TwinSock Communications", + "TwinSock Communications", + WS_OVERLAPPEDWINDOW | + WS_MINIMIZE | + WS_VISIBLE, + CW_USEDEFAULT, + 0, + CW_USEDEFAULT, + 0, + 0, + 0, + hInstance, + 0); + ShowWindow(hwnd, SW_SHOW); + RegisterManager(hwnd); + + GetPrivateProfileString("Config", "Port", "COM1", achProfileEntry, 256, "TWINSOCK.INI"); + idComm = OpenComm(achProfileEntry, 1024, 1024); + if (idComm < 0) + exit(1); + dcb.Id = idComm; + dcb.BaudRate = GetConfigInt("Speed", 19200); + dcb.ByteSize = GetConfigInt("Databits", 8); + dcb.Parity = GetConfigInt("Parity", NOPARITY); + dcb.StopBits = GetConfigInt("StopBits", ONESTOPBIT); + dcb.RlsTimeout = GetConfigInt("RlsTimeout", 0); + dcb.CtsTimeout = GetConfigInt("CtsTimeout", 0); + dcb.DsrTimeout = GetConfigInt("DsrTimeout", 0); + dcb.fBinary = TRUE; + dcb.fRtsDisable = GetConfigInt("fRtsDisable", TRUE); + dcb.fParity = GetConfigInt("fParity", FALSE); + dcb.fOutxCtsFlow = GetConfigInt("OutxCtsFlow", FALSE); + dcb.fOutxDsrFlow = GetConfigInt("OutxDsrFlow", FALSE); + dcb.fDummy = 0; + dcb.fDtrDisable = GetConfigInt("fDtrDisable", TRUE); + dcb.fOutX = GetConfigInt("fOutX", TRUE); + dcb.fInX = GetConfigInt("fInX", FALSE); + dcb.fPeChar = 0; + dcb.fNull = 0; + dcb.fChEvt = 0; + dcb.fDtrflow = GetConfigInt("fDtrFlow", FALSE); + dcb.fRtsflow = GetConfigInt("fRtsFlow", FALSE); + dcb.fDummy2 = 0; + dcb.XonChar = '\021'; + dcb.XoffChar = '\023'; + dcb.XonLim = 100; + dcb.XoffLim = 900; + dcb.PeChar = 0; + dcb.EofChar = 0; + dcb.EvtChar = 0; + dcb.TxDelay = 0; + + SetCommState(&dcb); + EnableCommNotification(idComm, hwnd, 1, 0); + SendInitRequest(); + + while (GetMessage(&msg, 0, 0, 0)) + { + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } +} + \ No newline at end of file diff --git a/twinsock.h b/twinsock.h new file mode 100644 index 0000000..0070f43 --- /dev/null +++ b/twinsock.h @@ -0,0 +1,162 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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. + */ + +enum arg_type +{ + AT_Int16 = 1, + AT_Int32, + AT_Int16Ptr, + AT_Int32Ptr, + AT_Char, + AT_String, + AT_GenPtr, +#ifdef __MSDOS__ + AT_Int = AT_Int16, + AT_IntPtr = AT_Int16 +#else +#ifdef apollo +#define AT_Int AT_Int32 +#define AT_IntPtr AT_Int32 +#else + AT_Int = AT_Int32, + AT_IntPtr = AT_Int +#endif +#endif +}; + +enum Functions +{ + FN_Init = 0, + FN_Accept, + FN_Bind, + FN_Close, + FN_Connect, + FN_IOCtl, + FN_GetPeerName, + FN_GetSockName, + FN_GetSockOpt, + FN_Listen, + FN_Select, + FN_Send, + FN_SendTo, + FN_SetSockOpt, + FN_Shutdown, + FN_Socket, + FN_Data, + FN_GetHostName, + FN_HostByAddr, + FN_HostByName, + FN_ServByPort, + FN_ServByName, + FN_ProtoByNumber, + FN_ProtoByName +}; + +struct func_arg +{ + enum arg_type at; + void *pvData; + int iLen; +#ifdef _Windows + BOOL bConstant; +#endif +}; + +struct transmit_function +{ + enum Functions fn; + int nArgs; + struct func_arg *pfaList; + struct func_arg *pfaResult; +}; + +#define MAX_HOST_ENT 1024 +#define MAX_ALTERNATES 20 + +#ifdef _Windows +struct data +{ + int iLen; + int nUsed; + struct sockaddr_in sin; + char *pchData; + struct data *pdNext; +}; + +struct per_task +{ + HTASK htask; + char achAddress[16]; + struct per_task *pptNext; + int iErrno; + FARPROC lpBlockFunc; + BOOL bCancel; + BOOL bBlocking; + struct hostent he; + struct servent se; + struct protoent pe; + char achHostEnt[MAX_HOST_ENT]; + char *apchHostAlii[MAX_ALTERNATES]; + char *apchHostAddresses[MAX_ALTERNATES]; + char achServEnt[MAX_HOST_ENT]; + char *apchServAlii[MAX_ALTERNATES]; + char achProtoEnt[MAX_HOST_ENT]; + char *apchProtoAlii[MAX_ALTERNATES]; +}; + +struct per_socket +{ + SOCKET s; + unsigned short iFlags; + struct data *pdIn; + struct data *pdOut; + HTASK htaskOwner; + struct per_socket *ppsNext; + long iEvents; + HWND hWnd; + unsigned wMsg; +}; + +#define PSF_ACCEPT 0x0001 +#define PSF_CONNECT 0x0002 +#define PSF_SHUTDOWN 0x0004 +#define PSF_NONBLOCK 0x0008 +#define PSF_CLOSED 0x0010 + +#define INIT_ARGS(args, type, data, size) \ + ( args.at = type, \ + args.pvData = (void *) data, \ + args.iLen = size, \ + args.bConstant = FALSE ) + +#define INIT_CARGS(args, type, data, size) \ + ( args.at = type, \ + args.pvData = (void *) data, \ + args.iLen = size, \ + args.bConstant = TRUE ) + +#define INIT_TF(tf, func, count, args, retval) \ + ( tf.fn = func, \ + tf.nArgs = count, \ + tf.pfaList = args, \ + tf.pfaResult = &retval ) + +#endif + \ No newline at end of file diff --git a/twinsock.ico b/twinsock.ico new file mode 100644 index 0000000000000000000000000000000000000000..b87050bf9e8fb759aa6591550a913c63d44757f9 GIT binary patch literal 766 zcmc(cF_yz13`FI9Dblf3YL_`ijz&r6EBQDnDe06y2#0JoeS{?#jgW+Fwy@Uq*Ch8J zvkUtbUC|r5*a>Tv;XX@-gvmKJEJrlkzV8s;KXca!WyvLsG#Ss`%Dm%xmUr^DZQvxQ z1ezN)fG}*DbcB>At%E5+NHt4QAaFq@ReLX^bn<3fOInIh#r%ThY!gRA6GzF72z-PN z?0^^CND)sRJrdUGdR8mdTPgh}G<(9QQCea~bqs-!qLwWpdnIxluWaPkI)-rIK4q>Q gSeYgNaq$v84qjmX9_p3oxzkGUhjyPMR_kAt2NF!iVE_OC literal 0 HcmV?d00001 diff --git a/twinsock.ini b/twinsock.ini new file mode 100644 index 0000000..419625f --- /dev/null +++ b/twinsock.ini @@ -0,0 +1,16 @@ +[Config] +Port=COM1 +Speed=19200 +Databits=8 + +;0 = None +;1 = Odd +;2 = Even +;3 = Mark +;4 = Space +Parity=0 + +; 0 => 1, 1 => 1.5, 2 => 2 +StopBits=0 + + \ No newline at end of file diff --git a/twinsock.rc b/twinsock.rc new file mode 100644 index 0000000..31d64ef --- /dev/null +++ b/twinsock.rc @@ -0,0 +1,2 @@ +TSICON ICON "twinsock.ico" + \ No newline at end of file diff --git a/twinsock.res b/twinsock.res new file mode 100644 index 0000000000000000000000000000000000000000..bbfa04ad2fe561705bf880ebeeb18f9fa516bf0c GIT binary patch literal 792 zcmc&zF;>Gc4E>=UI^hgGKt@K6zuaTq;%6aPyNZh;*+R{Zs4Nmd*? zz$VP|=L7HrVBvh>oN%NDGVOm$CP^mecu{l7nXzpf3G)YLT5)p7Bur^imR(Ez#PuZW z;IXd6Nk$22?x+ET$)-VvOZld?fC7Y6AVq?}g)*q>we-}<8rGIHVbaT3%R7$m0iti-NccM+LL}HI>ZvtJ4N=A5kCdSxL*%)7A zZ^Dkf%iOkOMV9==#Y)uJd4chHn6E_5PAkA2Z8lo0(*ITL5g*sL*ULMe=d)Kb@xCw1 K?6+Qj!h|ol$;^%b literal 0 HcmV?d00001 diff --git a/tx.h b/tx.h new file mode 100644 index 0000000..2c7478e --- /dev/null +++ b/tx.h @@ -0,0 +1,45 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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. + */ + +struct tx_request +{ + short iType; + short nArgs; + short nLen; + short id; + short nError; + char pchData[1]; +}; + +#ifdef _Windows +struct tx_queue +{ + struct tx_request *ptxr; + short id; + struct tx_queue *ptxqNext; + BOOL bDone; + char *pchLocation; + HWND hwnd; + u_int wMsg; + enum function_type ft; + HTASK htask; +}; +#endif + \ No newline at end of file diff --git a/winsock.c b/winsock.c new file mode 100644 index 0000000..1396fd7 --- /dev/null +++ b/winsock.c @@ -0,0 +1,1997 @@ +/* + * TwinSock - "Troy's Windows Sockets" + * + * Copyright (C) 1994 Troy Rollo + * + * 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 +#include +#include +#include +#include "twinsock.h" +#include "tx.h" + +static int iErrno = 0; +static short idNext = 0; + +static struct per_task *pptList = 0; +static struct per_socket *ppsList = 0; +static struct tx_queue *ptxqList = 0; + +HWND hwndManager = 0; +BOOL bEstablished = 0; + +static void FireAsyncRequest(struct tx_queue *ptxq); + +void _export +RegisterManager(HWND hwnd) +{ + hwndManager = hwnd; +} + +void _export +SetInitialised(void) +{ + bEstablished = TRUE; +} + + +void +CopyDataIn( void *pvSource, + enum arg_type at, + void *pvDest, + int nLen) +{ + switch(at) + { + case AT_Int16: + *(short *) pvDest = ntohs(*(short *) pvSource); + break; + + case AT_Int32: + *(short *) pvDest = ntohs(*(short *) pvSource); + break; + + case AT_Char: + *(char *) pvDest = *(char *) pvSource; + break; + + case AT_Int16Ptr: + case AT_Int32Ptr: + case AT_GenPtr: + case AT_String: + memcpy(pvDest, pvSource, nLen); + break; + } +} + +void +CopyDataOut( void *pvDest, + enum arg_type at, + void *pvSource, + int nLen) +{ + switch(at) + { + case AT_Int16: + *(short *) pvDest = htons(*(short *) pvSource); + break; + + case AT_Int32: + *(short *) pvDest = htons(*(short *) pvSource); + break; + + case AT_Char: + *(char *) pvDest = *(char *) pvSource; + break; + + case AT_Int16Ptr: + case AT_Int32Ptr: + case AT_GenPtr: + case AT_String: + memcpy(pvDest, pvSource, nLen); + break; + } +} + +static struct per_task * +GetAnotherTaskInfo(HTASK htask) +{ + struct per_task *ppt; + + for (ppt = pptList; ppt; ppt = ppt->pptNext) + { + if (ppt->htask == htask) + return ppt; + } + iErrno = WSANOTINITIALISED; + return 0; +} + +static struct per_task * +GetTaskInfo(void) +{ + return GetAnotherTaskInfo(GetCurrentTask()); +} + + +static struct per_socket * +GetSocketInfo(SOCKET s) +{ + struct per_socket *pps; + + + for (pps = ppsList; pps; pps = pps->ppsNext) + { + if (pps->s == s) + return pps; + } + iErrno = WSAENOTSOCK; + return 0; +} + +static void +Notify(struct per_socket *pps, + int iCode) +{ + if (pps->iEvents & iCode) + PostMessage(pps->hWnd, pps->wMsg, pps->s, WSAMAKESELECTREPLY(iCode, 0)); +} + +static struct per_socket * +NewSocket(struct per_task *ppt, SOCKET s) +{ + struct per_socket *ppsNew; + + ppsNew = (struct per_socket *) malloc(sizeof(struct per_socket)); + ppsNew->s = s; + ppsNew->iFlags = 0; + ppsNew->pdIn = 0; + ppsNew->pdOut = 0; + ppsNew->htaskOwner = ppt->htask; + ppsNew->ppsNext = ppsList; + ppsNew->iEvents = 0; + ppsList = ppsNew; + return ppsNew; + +} + +static void SendEarlyClose(SOCKET s); + +static void +RemoveSocket(struct per_socket *pps) +{ + struct per_socket **ppps, *ppsParent; + struct data **ppd, *pd; + + /* If our parent has noticed we're here, we need to remove ourselves + * from the list of sockets awaiting acception. + */ + for (ppsParent = ppsList; ppsParent; ppsParent = ppsParent->ppsNext) + { + if (!(ppsParent->iFlags & PSF_ACCEPT)) + continue; + for (ppd = &ppsParent->pdIn; *ppd; ppd = &(*ppd)->pdNext) + { + if ((*ppd)->pchData == (char *) pps) + { + pd = *ppd; + *ppd = pd->pdNext; + free(pd); + } + } + } + + /* Find our own position in the list */ + for (ppps = &ppsList; *ppps; ppps = &(*ppps)->ppsNext) + { + if (*ppps == pps) + { + *ppps = pps->ppsNext; + + /* If we have unacknowledged children, kill them */ + while (pps->pdIn) + { + pd = pps->pdIn; + pps->pdIn = pd->pdNext; + if (pps->iFlags & PSF_ACCEPT) + { + SendEarlyClose(((struct per_socket *) pd->pchData)->s); + RemoveSocket((struct per_socket *) pd->pchData); + } + else + { + free(pd->pchData); + } + free(pd); + } + free(pps); + return; + } + } +} + +static void +DataReceived(SOCKET s, void *pvData, int nLen, enum function_type ft) +{ + struct data *pdNew, **ppdList; + struct per_socket *pps; + struct per_task *ppt; + int ns; + + pps = GetSocketInfo(s); + if (!pps) + { + if (ft == FN_Accept) + { + nLen -= sizeof(struct sockaddr_in); + pvData = (char *) pvData + sizeof(struct sockaddr_in); + if (nLen == sizeof(long)) + ns = ntohl(*(long *) pvData); + else + ns = ntohs(*(short*) pvData); + SendEarlyClose(ns); + } + return; + } + for (ppdList = &pps->pdIn; *ppdList; ppdList = &(*ppdList)->pdNext); + + pdNew = (struct data *) malloc(sizeof(struct data)); + pdNew->sin = *(struct sockaddr_in *) pvData; + pdNew->sin.sin_family = ntohs(pdNew->sin.sin_family); + + nLen -= sizeof(struct sockaddr_in); + pvData = (char *) pvData + sizeof(struct sockaddr_in); + + pdNew->pdNext = 0; + pdNew->nUsed = 0; + *ppdList = pdNew; + + if (pps->iFlags & PSF_ACCEPT) + { + pdNew->iLen = 0; + if (nLen == sizeof(long)) + ns = ntohl(*(long *) pvData); + else + ns = ntohs(*(short*) pvData); + ppt = GetAnotherTaskInfo(pps->htaskOwner); + pdNew->pchData = (char *) NewSocket(ppt, ns); + if (pdNew == pps->pdIn) + Notify(pps, FD_ACCEPT); + } + else + { + pdNew->iLen = nLen; + pdNew->pchData = (char *) malloc(nLen); + memcpy(pdNew->pchData, pvData, nLen); + + if (pdNew == pps->pdIn) + Notify(pps, nLen ? FD_READ : FD_CLOSE); + } +} + +static BOOL +StartBlocking(struct per_task *ppt) +{ + if (ppt->bBlocking) + { + iErrno = WSAEINPROGRESS; + return FALSE; + } + else + { + ppt->bBlocking = TRUE; + ppt->bCancel = FALSE; + return TRUE; + } +} + +static void +EndBlocking(struct per_task *ppt) +{ + ppt->bBlocking = FALSE; +} + +static BOOL +FlushMessages(struct per_task *ppt) +{ + MSG msg; + BOOL ret; + + if (ppt->lpBlockFunc) + return ((BOOL far pascal (*)()) ppt->lpBlockFunc)(); + + ret = (BOOL) PeekMessage(&msg,0,0,0,PM_REMOVE); + if (ret) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + /* Some poorly behaved applications quit without + * checking the return value of WSACancel. It + * *can* fail. + */ + if (msg.message == WM_QUIT) + ppt->bCancel = TRUE; + } + return ret; +} + +static void +RemoveTXQ(struct tx_queue *ptxq) +{ + struct tx_queue **pptxq; + + for (pptxq = &ptxqList; *pptxq; pptxq = &((*pptxq)->ptxqNext)) + { + if (*pptxq == ptxq) + { + *pptxq = ptxq->ptxqNext; + free(ptxq->ptxr); + free(ptxq); + return; + } + } +} + +static void +RemoveTask(struct per_task *ppt) +{ + struct per_task **pppt; + + for (pppt = &pptList; *pppt; pppt = &((*pppt)->pptNext)) + { + if (*pppt == ppt) + { + *pppt = ppt->pptNext; + free(ppt); + } + } +}; + +void far _export +ResponseReceived(struct tx_request *ptxr) +{ + int nLen; + int id; + struct tx_queue *ptxq; + enum Functions ft; + + ft = (enum Functions) ntohs(ptxr->iType); + id = ntohs(ptxr->id); + nLen = ntohs(ptxr->nLen); + if (ft == FN_Data || ft == FN_Accept) + { + DataReceived(id, ptxr->pchData, nLen - sizeof(short) * 5, ft); + return; + } + for (ptxq = ptxqList; ptxq; ptxq = ptxq->ptxqNext) + { + if (ptxq->id == id) + { + memcpy(ptxq->ptxr, ptxr, nLen); + ptxq->bDone = TRUE; + if (ptxq->pchLocation) + FireAsyncRequest(ptxq); + return; + } + } +} + +static struct tx_queue * +TransmitFunction(struct transmit_function *ptf) +{ + int i; + int nSize = sizeof(short) * 5; + struct tx_request *ptxr; + struct tx_queue *ptxq, **pptxq; + int iOffset; + + for (i = 0; i < ptf->nArgs; i++) + nSize += ptf->pfaList[i].iLen + sizeof(short) * 2; + nSize += ptf->pfaResult->iLen + sizeof(short) * 2; + ptxr = (struct tx_request *) malloc(nSize); + ptxr->iType = htons((short) ptf->fn); + ptxr->nArgs = htons((short) ptf->nArgs); + ptxr->nError = 0; + ptxr->nLen = htons((short) nSize); + ptxr->id = htons(idNext); + for (iOffset = i = 0; i < ptf->nArgs; i++) + { + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].at); + iOffset += 2; + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].iLen); + iOffset += 2; + CopyDataOut( ptxr->pchData + iOffset, + ptf->pfaList[i].at, + ptf->pfaList[i].pvData, + ptf->pfaList[i].iLen); + iOffset += ptf->pfaList[i].iLen; + } + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->at); + iOffset += 2; + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->iLen); + iOffset += 2; + CopyDataOut( ptxr->pchData + iOffset, + ptf->pfaResult->at, + ptf->pfaResult->pvData, + ptf->pfaResult->iLen); + + iOffset += ptf->pfaResult->iLen; + + ptxq = (struct tx_queue *) malloc(sizeof(struct tx_queue)); + ptxq->ptxqNext = 0; + ptxq->id = idNext; + ptxq->ptxr = ptxr; + ptxq->bDone = 0; + ptxq->hwnd = 0; + ptxq->pchLocation = 0; + ptxq->wMsg = 0; + idNext++; + for (pptxq = &ptxqList; *pptxq; pptxq = &((*pptxq)->ptxqNext)); + *pptxq = ptxq; + SendMessage(hwndManager, WM_USER, nSize, (LPARAM) ptxr); + return ptxq; +}; + +static void +SendEarlyClose(SOCKET s) +{ + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + int nReturn; + + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Close, 1, pfaArgs, pfaReturn); + RemoveTXQ(TransmitFunction(&tf)); +} + +static void +SetErrorResult(struct func_arg *pfaResult) +{ + switch (pfaResult->at) + { + case AT_Int16: + *(short *) pfaResult->pvData = -1; + break; + + case AT_Int32: + *(long *) pfaResult->pvData = -1; + break; + + case AT_Char: + *(char *) pfaResult->pvData = -1; + break; + + case AT_Int16Ptr: + case AT_Int32Ptr: + case AT_String: + case AT_GenPtr: + *(void **) pfaResult->pvData = 0; + break; + } +} + +static int TransmitFunctionAndBlock( + struct per_task *ppt, + struct transmit_function *ptf) +{ + struct tx_queue *ptxq; + struct tx_request *ptxr; + int i, iOffset; + BOOL bError = FALSE; + + if (!StartBlocking(ppt)) + { + iErrno = WSAEINPROGRESS; + SetErrorResult(ptf->pfaResult); + return 0; + } + ptxq = TransmitFunction(ptf); + ptxr = ptxq->ptxr; + while (!ptxq->bDone) + { + FlushMessages(ppt); + if (ppt->bCancel) + { + iErrno = WSAEINTR; + RemoveTXQ(ptxq); + SetErrorResult(ptf->pfaResult); + EndBlocking(ppt); + return 0; + } + } + if (ptxq->ptxr->nError) + { + iErrno = ntohs(ptxq->ptxr->nError); + SetErrorResult(ptf->pfaResult); + bError = TRUE; + } + else + { + for (iOffset = i = 0; i < ptf->nArgs; i++) + { + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].at); + iOffset += 2; + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaList[i].iLen); + iOffset += 2; + if (!ptf->pfaList[i].bConstant) + CopyDataIn( ptxr->pchData + iOffset, + ptf->pfaList[i].at, + ptf->pfaList[i].pvData, + ptf->pfaList[i].iLen); + iOffset += ptf->pfaList[i].iLen; + } + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->at); + iOffset += 2; + *(short *) (ptxr->pchData + iOffset) = htons((short) ptf->pfaResult->iLen); + iOffset += 2; + CopyDataIn( ptxr->pchData + iOffset, + ptf->pfaResult->at, + ptf->pfaResult->pvData, + ptf->pfaResult->iLen); + + } + RemoveTXQ(ptxq); + EndBlocking(ppt); + return bError ? 0 : 1; +} + +static void +CopyHostEnt(struct per_task *ppt) +{ + int iLocation; + int i; + int nAddrs; + + ppt->he.h_addrtype = ntohs(*(short *) ppt->achHostEnt); + ppt->he.h_length = ntohs(*(short *) (ppt->achHostEnt + sizeof(short))); + nAddrs = ntohs(*(short *) (ppt->achHostEnt + sizeof(short) * 2)); + iLocation = sizeof(short) * 3; + for (i = 0; i < nAddrs; i++) + { + if (i < MAX_ALTERNATES - 1) + ppt->apchHostAddresses[i] = ppt->achHostEnt + iLocation; + iLocation += 4; + } + if (nAddrs < MAX_ALTERNATES - 1) + ppt->apchHostAddresses[nAddrs] = 0; + else + ppt->apchHostAddresses[MAX_ALTERNATES - 1] = 0; + ppt->he.h_addr_list = ppt->apchHostAddresses; + ppt->he.h_name = ppt->achHostEnt + iLocation; + iLocation += strlen(ppt->achHostEnt + iLocation)+ 1; + for (i = 0; ppt->achHostEnt[iLocation] && i < MAX_ALTERNATES - 1; i++) + { + ppt->apchHostAlii[i] = ppt->achHostEnt + iLocation; + iLocation += strlen(ppt->achHostEnt + iLocation)+ 1; + } + ppt->apchHostAlii[i] = 0; + ppt->he.h_aliases = ppt->apchHostAlii; + ppt->he.h_addr_list = ppt->apchHostAddresses; +} + +static int +CopyHostEntTo(struct per_task *ppt, char *pchData) +{ + int i; + int nAddrs; + struct hostent *phe; + char *pchOld; + int nAlii; + + CopyHostEnt(ppt); + + phe = (struct hostent *) pchData; + memcpy(phe, &ppt->he, sizeof(*phe)); + + pchData += sizeof(*phe); + + for (nAddrs = 0; phe->h_addr_list[nAddrs]; nAddrs++); + for (nAlii = 0; phe->h_aliases[nAlii]; nAlii++); + + memcpy(pchData, phe->h_addr_list, sizeof(char *) * (nAddrs + 1)); + phe->h_addr_list = (char **) pchData; + pchData += sizeof(char *) * (nAddrs + 1); + + memcpy(pchData, phe->h_aliases, sizeof(char *) * (nAddrs + 1)); + phe->h_aliases = (char **) pchData; + pchData += sizeof(char *) * (nAddrs + 1); + + pchOld = phe->h_addr_list[0]; + memcpy(pchData, pchOld, 4 * nAddrs); + for (i = 0; i < nAddrs; i++) + phe->h_addr_list[i] = phe->h_addr_list[i] - pchOld + pchData; + pchData += 4 * nAddrs; + + strcpy(pchData, phe->h_name); + phe->h_name = pchData; + pchData += strlen(pchData) + 1; + + for (i = 0; i < nAlii; i++) + { + strcpy(pchData, phe->h_aliases[i]); + phe->h_aliases[i] = pchData; + pchData += strlen(pchData) + 1; + } + return (pchData - (char *) phe); +} + +static void +CopyServEnt(struct per_task *ppt) +{ + int iLocation; + int i; + + /* Note that s_port is needed in network byte order */ + ppt->se.s_port = *(short *) ppt->achServEnt; + iLocation = sizeof(short); + ppt->se.s_proto = ppt->achServEnt + iLocation; + iLocation += strlen(ppt->achServEnt + iLocation) + 1; + ppt->se.s_name = ppt->achServEnt + iLocation; + iLocation += strlen(ppt->achServEnt + iLocation) + 1; + for (i = 0; ppt->achServEnt[iLocation] && i < MAX_ALTERNATES - 1; i++) + { + ppt->apchServAlii[i] = ppt->achServEnt + iLocation; + iLocation += strlen(ppt->achServEnt + iLocation) + 1; + } + ppt->apchServAlii[i] = 0; + ppt->se.s_aliases = ppt->apchServAlii; +} + +static int +CopyServEntTo(struct per_task *ppt, char *pchData) +{ + int i; + int nAlii; + struct servent *pse; + + CopyServEnt(ppt); + + pse = (struct servent *) pchData; + memcpy(pse, &ppt->se, sizeof(*pse)); + pchData += sizeof(struct servent *); + + for (nAlii = 0; pse->s_aliases[nAlii]; nAlii++); + memcpy(pchData, pse->s_aliases, sizeof(char *) * (nAlii + 1)); + pse->s_aliases = (char **) pchData; + pchData += sizeof(char *) * (nAlii + 1); + + strcpy(pchData, ppt->se.s_name); + ppt->se.s_name = pchData; + pchData += strlen(pchData) + 1; + + strcpy(pchData, ppt->se.s_proto); + ppt->se.s_proto = pchData; + pchData += strlen(pchData) + 1; + + for (i = 0; i < nAlii; i++) + { + strcpy(pchData, pse->s_aliases[i]); + pse->s_aliases[i] = pchData; + pchData += strlen(pchData) + 1; + } + return (pchData - (char *) pse); +} + +static void +CopyProtoEnt(struct per_task *ppt) +{ + int iLocation; + int i; + int nAddrs; + + ppt->pe.p_proto = ntohs(*(short *) ppt->achProtoEnt); + iLocation = sizeof(short); + ppt->pe.p_name = ppt->achProtoEnt + iLocation; + iLocation += strlen(ppt->achProtoEnt + iLocation) + 1; + for (i = 0; ppt->achProtoEnt[iLocation] && i < MAX_ALTERNATES - 1; i++) + { + ppt->apchProtoAlii[i] = ppt->achProtoEnt + iLocation; + iLocation += strlen(ppt->achProtoEnt + iLocation) + 1; + } + ppt->apchProtoAlii[i] = 0; + ppt->pe.p_aliases = ppt->apchProtoAlii; +} + +static int +CopyProtoEntTo(struct per_task *ppt, char *pchData) +{ + int iLocation; + int i; + int nAlii; + struct protoent *ppe; + + CopyProtoEnt(ppt); + + ppe = (struct protoent *) pchData; + memcpy(ppe, &ppt->se, sizeof(*ppe)); + pchData += sizeof(*ppe); + + for (nAlii = 0; ppe->p_aliases[nAlii]; nAlii++); + memcpy(pchData, ppe->p_aliases, sizeof(char *) * (nAlii + 1)); + ppe->p_aliases = (char **) pchData; + pchData += sizeof(char *) * (nAlii + 1); + + strcpy(pchData, ppe->p_name); + ppe->p_name = pchData; + pchData += strlen(pchData) + 1; + + for (i = 0; i < nAlii; i++) + { + strcpy(pchData, ppe->p_aliases[i]); + ppe->p_aliases[i] = pchData; + pchData += strlen(pchData) + 1; + } + return (pchData - (char *) ppe); +} + +SOCKET pascal far _export +accept(SOCKET s, struct sockaddr FAR *addr, + int FAR *addrlen) +{ + struct per_task *ppt; + struct per_socket *pps, *ppsNew; + struct data *pd; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + if (!(pps->iFlags & PSF_ACCEPT)) + { + iErrno = WSAEINVAL; + return -1; + } + if (!pps->pdIn && (pps->iFlags & PSF_NONBLOCK)) + { + iErrno = WSAEWOULDBLOCK; + return -1; + } + if (!StartBlocking(ppt)) + return -1; + while (!pps->pdIn) + { + FlushMessages(ppt); + if (ppt->bCancel) + { + iErrno = WSAEINTR; + EndBlocking(ppt); + return -1; + } + } + + pd = pps->pdIn; + if (addr && addrlen) + { + memcpy(addr, &pd->sin, sizeof(pd->sin)); + *addrlen = sizeof(pd->sin); + } + ppsNew = (struct per_socket *) pd->pchData; + + pps->pdIn = pd->pdNext; + free(pd); + if (pps->pdIn) + Notify(pps, FD_ACCEPT); + + EndBlocking(ppt); + return ppsNew->s; +} + +int pascal far _export +bind(SOCKET s, const struct sockaddr FAR *addr, int namelen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct sockaddr *psa; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!GetSocketInfo(s)) + return -1; + psa = (struct sockaddr *) malloc(namelen); + memcpy(psa, addr, namelen); + psa->sa_family = htons(psa->sa_family); + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_CARGS(pfaArgs[1], AT_GenPtr, psa, namelen ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, &namelen, sizeof(namelen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Bind, 3, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + free(psa); + return nReturn; +} + +int pascal far _export +closesocket(SOCKET s) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Close, 1, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + RemoveSocket(pps); /* Assume success. Is this valid? */ + return nReturn; +} + +int pascal far _export +connect(SOCKET s, const struct sockaddr FAR *name, int namelen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct sockaddr *psa; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + psa = (struct sockaddr *) malloc(namelen); + memcpy(psa, name, namelen); + psa->sa_family = htons(psa->sa_family); + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_CARGS(pfaArgs[1], AT_GenPtr, psa, namelen ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, &namelen, sizeof(namelen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Connect, 3, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + free(psa); + if (nReturn != -1) + pps->iFlags |= PSF_CONNECT; + return nReturn; +} + +int pascal far _export +ioctlsocket(SOCKET s, long cmd, u_long far * arg) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + switch(cmd) + { + case FIONBIO: + if (*arg) + pps->iFlags |= PSF_NONBLOCK; + else + pps->iFlags &= ~PSF_NONBLOCK; + break; + + case FIONREAD: + if (pps->pdIn) + *arg = pps->pdIn->iLen - pps->pdIn->nUsed; + else + *arg = 0; + break; + + case SIOCATMARK: + *(BOOL *) arg = 0; + break; + } + return 0; +} + +int pascal far _export getpeername (SOCKET s, struct sockaddr FAR *name, + int FAR * namelen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!GetSocketInfo(s)) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_GenPtr, name, *namelen ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, namelen, sizeof(*namelen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_GetPeerName, 3, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + if (nReturn != -1) + name->sa_family = ntohs(name->sa_family); + return nReturn; +} + +int pascal far _export getsockname (SOCKET s, struct sockaddr FAR *name, + int FAR * namelen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!GetSocketInfo(s)) + return -1; + *namelen = sizeof(struct sockaddr_in); /* Just in case */ + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_GenPtr, name, *namelen ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, namelen, sizeof(*namelen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_GetSockName, 3, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + if (nReturn != -1) + name->sa_family = ntohs(name->sa_family); + return nReturn; +} + +int pascal far _export getsockopt (SOCKET s, int level, int optname, + char FAR * optval, int FAR *optlen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[5]; + struct func_arg pfaReturn; + struct transmit_function tf; + long iOptVal; + short iOptLen; + + iOptLen = sizeof(long); + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!GetSocketInfo(s)) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_Int, &level, sizeof(level) ); + INIT_ARGS(pfaArgs[2], AT_Int, &optname, sizeof(optname) ); + if (optname == SO_LINGER) + INIT_ARGS(pfaArgs[3], AT_GenPtr, optval, iOptLen ); + else + INIT_ARGS(pfaArgs[3], AT_Int32Ptr, &iOptVal, iOptLen ); + INIT_ARGS(pfaArgs[4], AT_IntPtr, &iOptLen, sizeof(iOptLen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_GetSockOpt, 5, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + if (optname == SO_LINGER) + { + *optlen = sizeof(struct linger); + } + else + { + *optlen = sizeof(int); + *optval = iOptVal; + } + } + return nReturn; +} + +u_long pascal far _export htonl (u_long hostlong) +{ + char *pchValue = (char *) &hostlong; + char c; + + c = pchValue[0]; + pchValue[0] = pchValue[3]; + pchValue[3] = c; + c = pchValue[1]; + pchValue[1] = pchValue[2]; + pchValue[2] = c; + return hostlong; +} + +u_short pascal far _export htons (u_short hostshort) +{ + char *pchValue = (char *) &hostshort; + char c; + + c = pchValue[0]; + pchValue[0] = pchValue[1]; + pchValue[1] = c; + return hostshort; +} + +unsigned long pascal far _export inet_addr (const char FAR * cp) +{ + unsigned long iValue; + char *pchValue = (char *) &iValue; + + if (!GetTaskInfo()) + return -1; + pchValue[0] = atoi(cp); + cp = strchr(cp, '.'); + if (cp) + { + cp++; + pchValue[1] = atoi(cp); + cp = strchr(cp, '.'); + if (cp) + { + cp++; + pchValue[2] = atoi(cp); + cp = strchr(cp, '.'); + if (cp) + { + cp++; + pchValue[3] = atoi(cp); + cp = strchr(cp, '.'); + if (!cp) + { + return iValue; + } + } + } + } + return -1; +} + +char FAR * pascal far _export inet_ntoa (struct in_addr in) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + + sprintf(ppt->achAddress, "%d.%d.%d.%d", + (int) in.S_un.S_un_b.s_b1, + (int) in.S_un.S_un_b.s_b2, + (int) in.S_un.S_un_b.s_b3, + (int) in.S_un.S_un_b.s_b4); + return ppt->achAddress; +} + +int pascal far _export listen (SOCKET s, int backlog) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + if (pps->iFlags & PSF_CONNECT) + { + iErrno = WSAEISCONN; + return -1; + } + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_Int, &backlog, sizeof(backlog) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Listen, 2, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + if (nReturn != -1) + pps->iFlags |= PSF_ACCEPT; + return nReturn; +} + +u_long pascal far _export ntohl (u_long netlong) +{ + char *pchValue = (char *) &netlong; + char c; + + c = pchValue[0]; + pchValue[0] = pchValue[3]; + pchValue[3] = c; + c = pchValue[1]; + pchValue[1] = pchValue[2]; + pchValue[2] = c; + return netlong; +} + +u_short pascal far _export ntohs (u_short netshort) +{ + char *pchValue = (char *) &netshort; + char c; + + c = pchValue[0]; + pchValue[0] = pchValue[1]; + pchValue[1] = c; + return netshort; +} + +int pascal far _export recv (SOCKET s, char FAR * buf, int len, int flags) +{ + return recvfrom(s, buf, len, flags, 0, 0); +} + +int pascal far _export recvfrom (SOCKET s, char FAR * buf, int len, int flags, + struct sockaddr FAR *from, int FAR * fromlen) +{ + struct per_task *ppt; + struct per_socket *pps; + struct data *pd; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + if (pps->iFlags & PSF_CLOSED) + { + /* Haven't you got the message yet? */ + iErrno = WSAENOTCONN; + return -1; + } + if (pps->iFlags & PSF_ACCEPT) + { + iErrno = WSAENOTCONN; + return -1; + } + if (pps->iFlags & PSF_SHUTDOWN) + { + iErrno = WSAESHUTDOWN; + return -1; + } + if (!pps->pdIn && (pps->iFlags & PSF_NONBLOCK)) + { + iErrno = WSAEWOULDBLOCK; + return -1; + } + if (!StartBlocking(ppt)) + return -1; + while (!pps->pdIn) + { + FlushMessages(ppt); + if (ppt->bCancel) + { + iErrno = WSAEINTR; + EndBlocking(ppt); + return -1; + } + } + + pd = pps->pdIn; + if (from && fromlen) + { + memcpy(from, &pd->sin, sizeof(pd->sin)); + *fromlen = sizeof(pd->sin); + } + if (len > pd->iLen - pd->nUsed) + len = pd->iLen - pd->nUsed; + memcpy(buf, pd->pchData + pd->nUsed, len); + pd->nUsed += len; + if (pd->nUsed == pd->iLen) + { + pps->pdIn = pd->pdNext; + free(pd->pchData); + free(pd); + if (pps->pdIn) + Notify(pps, pps->pdIn->iLen ? FD_READ : FD_CLOSE); + } + else + { + Notify(pps, FD_READ); + } + + EndBlocking(ppt); + return len; +} + +#define PPS_ERROR ((struct per_socket *) -1) + +static struct per_socket ** +GetPPS(fd_set *fds) +{ + struct per_socket **pps; + int i; + + if (!fds || !fds->fd_count) + return 0; + pps = (struct per_socket *) malloc(sizeof(struct per_socket *) * + fds->fd_count); + for (i = 0; i < fds->fd_count; i++) + { + pps[i] = GetSocketInfo(fds->fd_array[i]); + if (!pps[i]) + { + free(pps); + return PPS_ERROR; + } + } + return pps; +} + + +int pascal far _export select (int nfds, fd_set *readfds, fd_set far *writefds, + fd_set *exceptfds, const struct timeval far *timeout) +{ + struct per_task *ppt; + struct per_socket **ppsRead, **ppsWrite, **ppsExcept; + BOOL bOneOK = FALSE; + BOOL bTimedOut = FALSE; + int iOld, iNew; + unsigned long tExpire; + int i; + + if (timeout) + { + tExpire = GetTickCount(); + tExpire += timeout->tv_usec / 1000 + timeout->tv_sec * 1000; + } + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!StartBlocking(ppt)) + return -1; + + ppsRead = GetPPS(readfds); + if (ppsRead == PPS_ERROR) + return -1; + ppsWrite = GetPPS(writefds); + if (ppsWrite == PPS_ERROR) + { + free(ppsRead); + return -1; + } + ppsExcept = GetPPS(exceptfds); + if (ppsExcept == PPS_ERROR) + { + free(ppsRead); + free(ppsWrite); + return -1; + } + + while (!bOneOK && !bTimedOut && !ppt->bCancel) + { + FlushMessages(ppt); + if (ppsWrite) + bOneOK = TRUE; + if (ppsRead) + { + for (i = 0; i < readfds->fd_count; i++) + { + if (ppsRead[i]->pdIn) + bOneOK = TRUE; + } + } + if (timeout && GetTickCount() >= tExpire) + bTimedOut = TRUE; + } + + nfds = 0; + if (readfds) + { + for (iOld = iNew = 0; iOld < readfds->fd_count; iOld++) + { + if (iOld != iNew) + readfds->fd_array[iNew] = + readfds->fd_array[iOld]; + if (ppsRead[iOld]->pdIn) + iNew++; + } + readfds->fd_count = iNew; + nfds += iNew; + } + if (writefds) + nfds += writefds->fd_count; + if (exceptfds) + exceptfds->fd_count = 0; + + EndBlocking(ppt); + if (ppsRead) + free(ppsRead); + if (ppsWrite) + free(ppsWrite); + if (ppsExcept) + free(ppsExcept); + if (ppt->bCancel && !nfds) + { + iErrno = WSAEINTR; + return -1; + } + else + { + return nfds; + } +} + +int pascal far _export send (SOCKET s, const char FAR * buf, int len, int flags) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[4]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_CARGS(pfaArgs[1], AT_GenPtr, buf, len ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, &len, sizeof(len) ); + INIT_ARGS(pfaArgs[3], AT_Int, &flags, sizeof(flags) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Send, 4, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + Notify(pps, FD_WRITE); + return nReturn; +} + +int pascal far _export sendto (SOCKET s, const char FAR * buf, int len, int flags, + const struct sockaddr FAR *to, int tolen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[6]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct sockaddr *psa; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + psa = (struct sockaddr *) malloc(tolen); + memcpy(psa, to, tolen); + psa->sa_family = htons(psa->sa_family); + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_CARGS(pfaArgs[1], AT_GenPtr, buf, len ); + INIT_ARGS(pfaArgs[2], AT_IntPtr, &len, sizeof(len) ); + INIT_ARGS(pfaArgs[3], AT_Int, &flags, sizeof(flags) ); + INIT_CARGS(pfaArgs[4], AT_GenPtr, to, tolen ); + INIT_ARGS(pfaArgs[5], AT_Int, &tolen, sizeof(tolen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Send, 6, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + free(psa); + Notify(pps, FD_WRITE); + return nReturn; +} + +int pascal far _export setsockopt (SOCKET s, int level, int optname, + const char FAR * optval, int optlen) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[5]; + struct func_arg pfaReturn; + struct transmit_function tf; + long iOptVal; + short iOptLen; + + iOptLen = sizeof(long); + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (!GetSocketInfo(s)) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_Int, &level, sizeof(level) ); + INIT_ARGS(pfaArgs[2], AT_Int, &optname, sizeof(optname) ); + if (optname == SO_LINGER) + { + INIT_ARGS(pfaArgs[3], AT_GenPtr, optval, iOptLen ); + } + else + { + iOptVal = *optval; + INIT_ARGS(pfaArgs[3], AT_Int32Ptr, &iOptVal, iOptLen ); + } + INIT_ARGS(pfaArgs[4], AT_Int, &iOptLen, sizeof(iOptLen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_SetSockOpt, 5, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + return nReturn; +} + +int pascal far _export shutdown (SOCKET s, int how) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &s, sizeof(s) ); + INIT_ARGS(pfaArgs[1], AT_Int, &how, sizeof(how) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Shutdown, 2, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + if (nReturn != -1 && (how == 0 || how == 2)) + pps->iFlags |= PSF_SHUTDOWN; + return nReturn; +} + +SOCKET pascal far _export socket (int af, int type, int protocol) +{ + struct per_task *ppt; + struct per_socket *pps; + int nReturn; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + INIT_ARGS(pfaArgs[0], AT_Int, &af, sizeof(af) ); + INIT_ARGS(pfaArgs[1], AT_Int, &type, sizeof(type) ); + INIT_ARGS(pfaArgs[2], AT_Int, &protocol, sizeof(protocol) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_Socket, 3, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + if (nReturn != -1) + NewSocket(ppt, nReturn); + return nReturn; +} + +struct hostent FAR * pascal far _export gethostbyaddr(const char FAR * addr, + int len, int type) +{ + struct per_task *ppt; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_GenPtr, addr, len ); + INIT_ARGS(pfaArgs[1], AT_Int, &len, sizeof(len) ); + INIT_ARGS(pfaArgs[2], AT_Int, &type, sizeof(type) ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_HostByAddr, 3, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyHostEnt(ppt); + return &ppt->he; + } + else + { + return 0; + } +} + +struct hostent FAR * pascal far _export gethostbyname(const char FAR * name) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_String, name, strlen(name) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_HostByName, 1, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyHostEnt(ppt); + return &ppt->he; + } + else + { + return 0; + } +} + +struct servent FAR * pascal far _export getservbyport(int port, const char FAR * proto) +{ + struct per_task *ppt; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_ARGS(pfaArgs[0], AT_Int, &port, sizeof(port) ); + INIT_CARGS(pfaArgs[1], AT_String, proto, strlen(proto) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achServEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ServByPort, 2, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyServEnt(ppt); + return &ppt->se; + } + else + { + return 0; + } +} + +struct servent FAR * pascal far _export getservbyname(const char FAR * name, + const char FAR * proto) +{ + struct per_task *ppt; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_String, name, strlen(name) + 1 ); + INIT_CARGS(pfaArgs[1], AT_String, proto, strlen(proto) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achServEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ServByName, 2, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyServEnt(ppt); + return &ppt->se; + } + else + { + return 0; + } +} + +struct protoent FAR * pascal far _export getprotobynumber(int proto) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_ARGS(pfaArgs[0], AT_Int, &proto, sizeof(proto) ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achProtoEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ProtoByNumber, 1, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyProtoEnt(ppt); + return &ppt->pe; + } + else + { + return 0; + } +} + +struct protoent FAR * pascal far _export getprotobyname(const char FAR * name) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_String, name, strlen(name) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achProtoEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ProtoByName, 1, pfaArgs, pfaReturn); + if (TransmitFunctionAndBlock(ppt, &tf)) + { + CopyProtoEnt(ppt); + return &ppt->pe; + } + else + { + return 0; + } +} + +int pascal far _export +gethostname(char *name, int namelen) +{ + struct per_task *ppt; + int nReturn; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + INIT_ARGS(pfaArgs[0], AT_String, name, namelen ); + INIT_ARGS(pfaArgs[1], AT_Int, &namelen, sizeof(namelen) ); + INIT_ARGS(pfaReturn, AT_Int, &nReturn, sizeof(nReturn) ); + INIT_TF(tf, FN_GetHostName, 2, pfaArgs, pfaReturn); + TransmitFunctionAndBlock(ppt, &tf); + return nReturn; +} + +int pascal far _export WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData) +{ + struct per_task *pptNew; + + lpWSAData->wVersion = 0x0101; + lpWSAData->wHighVersion = 0x0101; + strcpy(lpWSAData->szDescription, + "TwinSock 1.0 - Proxy sockets system. " + "Copyright 1994 Troy Rollo. " + "This library is free software. " + "See the file \"COPYING.LIB\" from the " + "distribution for details."); + if (!hwndManager) + strcpy(lpWSAData->szSystemStatus, "Not Initialised."); + else if (bEstablished) + strcpy(lpWSAData->szSystemStatus, "Ready."); + else + strcpy(lpWSAData->szSystemStatus, "Initialising."); + lpWSAData->iMaxSockets = 256; + lpWSAData->iMaxUdpDg = 512; + lpWSAData->lpVendorInfo = 0; + if (wVersionRequired == 0x0001) + return WSAVERNOTSUPPORTED; + if (!bEstablished) + return WSASYSNOTREADY; + pptNew = malloc(sizeof(struct per_task)); + pptNew->htask = GetCurrentTask(); + pptNew->pptNext = pptList; + pptNew->lpBlockFunc = 0; + pptNew->bCancel = FALSE; + pptNew->bBlocking = FALSE; + pptList = pptNew; + return 0; +} + +int pascal far _export WSACleanup(void) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (ppt->bBlocking) + { + iErrno = WSAEINPROGRESS; + RemoveTask(ppt); + return -1; + } + return 0; +} + +void pascal far _export WSASetLastError(int iError) +{ + if (!GetTaskInfo()) + return -1; + iErrno = iError; +} + +int pascal far _export WSAGetLastError(void) +{ + return iErrno; +} + +BOOL pascal far _export WSAIsBlocking(void) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + return ppt->bBlocking; +} + +int pascal far _export WSAUnhookBlockingHook(void) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + ppt->lpBlockFunc = 0; + return 0; +} + +FARPROC pascal far _export WSASetBlockingHook(FARPROC lpBlockFunc) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + ppt->lpBlockFunc = lpBlockFunc; + return -1; +} + +int pascal far _export WSACancelBlockingCall(void) +{ + struct per_task *ppt; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if (ppt->bBlocking) + { + ppt->bCancel = TRUE; + return 0; + } + else + { + iErrno = WSAEINVAL; + return -1; + } +} + +static void +FireAsyncRequest(struct tx_queue *ptxq) +{ + int nLen; + char *pchData; + struct per_task *ppt; + + if (ptxq->ptxr->nError) + { + PostMessage(ptxq->hwnd, ptxq->wMsg, ptxq->id | 0x4000, + WSAMAKEASYNCREPLY(0, ntohs(ptxq->ptxr->nError))); + RemoveTXQ(ptxq); + return; + } + + ppt = GetAnotherTaskInfo(ptxq->htask); + + if (!ppt) + { + /* How **Rude** */ + RemoveTXQ(ptxq); + return; + } + + pchData = ptxq->ptxr->pchData + + ntohs(ptxq->ptxr->nLen) - + MAX_HOST_ENT - + sizeof(short) * 5; + + switch(ptxq->ft) + { + case FN_HostByName: + case FN_HostByAddr: + memcpy(ppt->achHostEnt, pchData, MAX_HOST_ENT); + nLen = CopyHostEntTo(ppt, ptxq->pchLocation); + break; + + case FN_ServByName: + case FN_ServByPort: + memcpy(ppt->achServEnt, pchData, MAX_HOST_ENT); + nLen = CopyServEntTo(ppt, ptxq->pchLocation); + break; + + case FN_ProtoByName: + case FN_ProtoByNumber: + memcpy(ppt->achProtoEnt, pchData, MAX_HOST_ENT); + nLen = CopyProtoEntTo(ppt, ptxq->pchLocation); + break; + } + + PostMessage(ptxq->hwnd, ptxq->wMsg, ptxq->id | 0x4000, + WSAMAKEASYNCREPLY(nLen, 0)); + RemoveTXQ(ptxq); +} + +HANDLE pascal far _export WSAAsyncGetServByName(HWND hWnd, u_int wMsg, + const char FAR * name, + const char FAR * proto, + char FAR * buf, int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_String, name, strlen(name) + 1 ); + INIT_CARGS(pfaArgs[1], AT_String, proto, strlen(proto) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ServByName, 2, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_ServByName; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +HANDLE pascal far _export WSAAsyncGetServByPort(HWND hWnd, u_int wMsg, int port, + const char FAR * proto, char FAR * buf, + int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[2]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_Int, &port, sizeof(port) ); + INIT_CARGS(pfaArgs[1], AT_String, proto, strlen(proto) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ServByPort, 2, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_ServByPort; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +HANDLE pascal far _export WSAAsyncGetProtoByName(HWND hWnd, u_int wMsg, + const char FAR * name, char FAR * buf, + int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_String, name, strlen(name) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ProtoByName, 1, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_ProtoByName; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +HANDLE pascal far _export WSAAsyncGetProtoByNumber(HWND hWnd, u_int wMsg, + int number, char FAR * buf, + int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_Int, &number, sizeof(number) ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_ProtoByNumber, 1, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_ProtoByNumber; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +HANDLE pascal far _export WSAAsyncGetHostByName(HWND hWnd, u_int wMsg, + const char FAR * name, char FAR * buf, + int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[1]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_GenPtr, name, strlen(name) + 1 ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_HostByName, 1, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_HostByName; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +HANDLE pascal far _export WSAAsyncGetHostByAddr(HWND hWnd, u_int wMsg, + const char FAR * addr, int len, int type, + char FAR * buf, int buflen) +{ + struct per_task *ppt; + struct func_arg pfaArgs[3]; + struct func_arg pfaReturn; + struct transmit_function tf; + struct tx_queue *txq; + + if ((ppt = GetTaskInfo()) == 0) + return 0; + INIT_CARGS(pfaArgs[0], AT_GenPtr, addr, len ); + INIT_CARGS(pfaArgs[1], AT_Int, &len, sizeof(len) ); + INIT_CARGS(pfaArgs[2], AT_Int, &type, sizeof(type) ); + INIT_ARGS(pfaReturn, AT_GenPtr, ppt->achHostEnt,MAX_HOST_ENT ); + INIT_TF(tf, FN_HostByAddr, 3, pfaArgs, pfaReturn); + txq = TransmitFunction(&tf); + txq->hwnd = hWnd; + txq->pchLocation = buf; + txq->wMsg = wMsg; + txq->ft = FN_HostByAddr; + txq->htask = ppt->htask; + return (txq->id | 0x4000); +} + +int pascal far _export WSACancelAsyncRequest(HANDLE hAsyncTaskHandle) +{ + struct tx_queue *ptxq; + + if (!GetTaskInfo()) + return -1; + + for (ptxq = ptxqList; ptxq; ptxq = ptxq->ptxqNext) + { + if ((HANDLE) (ptxq->id | 0x4000) == hAsyncTaskHandle) + { + RemoveTXQ(ptxq); + return 0; + } + } + iErrno = WSAEINVAL; + return -1; +} + +int pascal far _export WSAAsyncSelect(SOCKET s, HWND hWnd, u_int wMsg, + long lEvent) +{ + struct per_task *ppt; + struct per_socket *pps; + + if ((ppt = GetTaskInfo()) == 0) + return -1; + if ((pps = GetSocketInfo(s)) == 0) + return -1; + if (lEvent) + pps->iFlags |= PSF_NONBLOCK; + pps->hWnd = hWnd; + pps->wMsg = wMsg; + pps->iEvents = lEvent; + Notify(pps, FD_WRITE); + return 0; +} + +int FAR PASCAL _export _WSAFDIsSet(SOCKET s, fd_set FAR *pfds) +{ + int i; + + if (!GetTaskInfo()) + return -1; + if (!GetSocketInfo(s)) + return -1; + for (i = 0; i < pfds->fd_count; i++) + { + if (pfds->fd_array[i] == s) + return TRUE; + } + return FALSE; +} + + + \ No newline at end of file diff --git a/winsock.def b/winsock.def new file mode 100644 index 0000000..971296c --- /dev/null +++ b/winsock.def @@ -0,0 +1,65 @@ +LIBRARY WINSOCK +DESCRIPTION 'BSD Socket API for Windows' +EXETYPE WINDOWS +CODE PRELOAD FIXED +HEAPSIZE 1024 + +EXPORTS + ACCEPT @1 + BIND @2 + CLOSESOCKET @3 + CONNECT @4 + GETPEERNAME @5 + GETSOCKNAME @6 + GETSOCKOPT @7 + HTONL @8 + HTONS @9 + INET_ADDR @10 + INET_NTOA @11 + IOCTLSOCKET @12 + LISTEN @13 + NTOHL @14 + NTOHS @15 + RECV @16 + RECVFROM @17 + SELECT @18 + SEND @19 + SENDTO @20 + SETSOCKOPT @21 + SHUTDOWN @22 + SOCKET @23 + + GETHOSTBYADDR @51 + GETHOSTBYNAME @52 + GETPROTOBYNAME @53 + GETPROTOBYNUMBER @54 + GETSERVBYNAME @55 + GETSERVBYPORT @56 + GETHOSTNAME @57 + + WSAASYNCSELECT @101 + WSAASYNCGETHOSTBYADDR @102 + WSAASYNCGETHOSTBYNAME @103 + WSAASYNCGETPROTOBYNUMBER @104 + WSAASYNCGETPROTOBYNAME @105 + WSAASYNCGETSERVBYPORT @106 + WSAASYNCGETSERVBYNAME @107 + WSACANCELASYNCREQUEST @108 + WSASETBLOCKINGHOOK @109 + WSAUNHOOKBLOCKINGHOOK @110 + WSAGETLASTERROR @111 + WSASETLASTERROR @112 + WSACANCELBLOCKINGCALL @113 + WSAISBLOCKING @114 + WSASTARTUP @115 + WSACLEANUP @116 + + _WSAFDISSET @151 + + WEP @500 RESIDENTNAME + + _ResponseReceived @10000 + _RegisterManager @10001 + _SetInitialised @10002 + + \ No newline at end of file diff --git a/wserror.h b/wserror.h new file mode 100644 index 0000000..ae7d5bc --- /dev/null +++ b/wserror.h @@ -0,0 +1,53 @@ +#define WSABASEERR 10000 +#define WSAEINTR (WSABASEERR+4) +#define WSAEBADF (WSABASEERR+9) +#define WSAEACCES (WSABASEERR+13) +#define WSAEFAULT (WSABASEERR+14) +#define WSAEINVAL (WSABASEERR+22) +#define WSAEMFILE (WSABASEERR+24) +#define WSAEWOULDBLOCK (WSABASEERR+35) +#define WSAEINPROGRESS (WSABASEERR+36) +#define WSAEALREADY (WSABASEERR+37) +#define WSAENOTSOCK (WSABASEERR+38) +#define WSAEDESTADDRREQ (WSABASEERR+39) +#define WSAEMSGSIZE (WSABASEERR+40) +#define WSAEPROTOTYPE (WSABASEERR+41) +#define WSAENOPROTOOPT (WSABASEERR+42) +#define WSAEPROTONOSUPPORT (WSABASEERR+43) +#define WSAESOCKTNOSUPPORT (WSABASEERR+44) +#define WSAEOPNOTSUPP (WSABASEERR+45) +#define WSAEPFNOSUPPORT (WSABASEERR+46) +#define WSAEAFNOSUPPORT (WSABASEERR+47) +#define WSAEADDRINUSE (WSABASEERR+48) +#define WSAEADDRNOTAVAIL (WSABASEERR+49) +#define WSAENETDOWN (WSABASEERR+50) +#define WSAENETUNREACH (WSABASEERR+51) +#define WSAENETRESET (WSABASEERR+52) +#define WSAECONNABORTED (WSABASEERR+53) +#define WSAECONNRESET (WSABASEERR+54) +#define WSAENOBUFS (WSABASEERR+55) +#define WSAEISCONN (WSABASEERR+56) +#define WSAENOTCONN (WSABASEERR+57) +#define WSAESHUTDOWN (WSABASEERR+58) +#define WSAETOOMANYREFS (WSABASEERR+59) +#define WSAETIMEDOUT (WSABASEERR+60) +#define WSAECONNREFUSED (WSABASEERR+61) +#define WSAELOOP (WSABASEERR+62) +#define WSAENAMETOOLONG (WSABASEERR+63) +#define WSAEHOSTDOWN (WSABASEERR+64) +#define WSAEHOSTUNREACH (WSABASEERR+65) +#define WSAENOTEMPTY (WSABASEERR+66) +#define WSAEPROCLIM (WSABASEERR+67) +#define WSAEUSERS (WSABASEERR+68) +#define WSAEDQUOT (WSABASEERR+69) +#define WSAESTALE (WSABASEERR+70) +#define WSAEREMOTE (WSABASEERR+71) +#define WSAEDISCON (WSABASEERR+101) +#define WSASYSNOTREADY (WSABASEERR+91) +#define WSAVERNOTSUPPORTED (WSABASEERR+92) +#define WSANOTINITIALISED (WSABASEERR+93) +#define WSAHOST_NOT_FOUND (WSABASEERR+1001) +#define WSATRY_AGAIN (WSABASEERR+1002) +#define WSANO_RECOVERY (WSABASEERR+1003) +#define WSANO_DATA (WSABASEERR+1004) +#define WSANO_ADDRESS WSANO_DATA