Commit a965cd54 authored by John van Groningen's avatar John van Groningen
Browse files

port to 64 bits, changes in layout

parent 316cf70a
...@@ -4,10 +4,6 @@ ...@@ -4,10 +4,6 @@
for the Windows platform. for the Windows platform.
********************************************************************************************/ ********************************************************************************************/
/********************************************************************************************
About this module:
Routines related to printing.
********************************************************************************************/
//#define FD_SETSIZE "maximal number of sockets to select on" (default is 64) //#define FD_SETSIZE "maximal number of sockets to select on" (default is 64)
#include <winsock.h> #include <winsock.h>
...@@ -25,34 +21,34 @@ Naming convention: functions, which are called from Clean end with a "C". Functi ...@@ -25,34 +21,34 @@ Naming convention: functions, which are called from Clean end with a "C". Functi
//************************************************ //************************************************
// functions, which are called from Clean (semantic is explained in tcp.icl or ostcp.icl) // functions, which are called from Clean (semantic is explained in tcp.icl or ostcp.icl)
int os_eom(SOCKET endpointRef); int os_eom (SOCKET endpointRef);
int os_disconnected(SOCKET endpointRef); int os_disconnected (SOCKET endpointRef);
int os_connectrequestavailable(SOCKET endpointRef); int os_connectrequestavailable (SOCKET endpointRef);
void abortedHost_syncC(CleanString inetAddr, int *errCode, int *ipAddr); void abortedHost_syncC(CleanString inetAddr, int *errCode, int *ipAddr);
void abortedHost_asyncC(CleanString inetAddr, int *errCode, HANDLE *endpointRef); void abortedHost_asyncC(CleanString inetAddr, int *errCode, HANDLE *endpointRef);
void openTCP_ListenerC(int portNum, int *pErrCode, SOCKET *pEndpointRef); void openTCP_ListenerC(int portNum, int *pErrCode, SOCKET *pEndpointRef);
void acceptC(SOCKET endpointRef, int *pErrCode, int *pInetHost, SOCKET *pEndpointRef); void acceptC (SOCKET endpointRef, int *pErrCode, int *pInetHost, SOCKET *pEndpointRef);
void os_connectTCPC(int isIOProg, int block, int doTimeout, unsigned int stopTime, void os_connectTCPC (int isIOProg, int block, int doTimeout, unsigned int stopTime,
int ipAddr, int portnum, int ipAddr, int portnum,
int *errCode, int *timeoutExpiredP, int *endpointRefP int *errCode, int *timeoutExpiredP, int *endpointRefP
); );
void sendC(SOCKET endpointRef, CleanString data, int begin, int nBytes, int *pErrCode, int *pSentBytes); void sendC (SOCKET endpointRef, CleanString data, int begin, int nBytes, int *pErrCode, int *pSentBytes);
void receiveC(SOCKET endpointRef, int maxSize, CleanString *data); void receiveC(SOCKET endpointRef, int maxSize, CleanString *data);
int getRcvBuffSizeC(); int getRcvBuffSizeC();
void resizeStringC(CleanString string, int newSize); void resizeStringC(CleanString string, int newSize);
int data_availableC(SOCKET endpointRef); int data_availableC(SOCKET endpointRef);
void disconnectGracefulC(SOCKET endpointRef); void disconnectGracefulC (SOCKET endpointRef);
void disconnectBrutalC(SOCKET endpointRef); void disconnectBrutalC (SOCKET endpointRef);
void garbageCollectEndpointC(SOCKET endpointRef); void garbageCollectEndpointC (SOCKET endpointRef);
void os_select_inetevents(SOCKET endpointRef, int receiverCategory, void os_select_inetevents (SOCKET endpointRef, int receiverCategory,
int referenceCount, int getReceiveEvents, int getSendEvents, int referenceCount, int getReceiveEvents, int getSendEvents,
int aborted int aborted
); );
void selectChC(int justForMac, int nonBlocking, int doTimeout, unsigned int stopTime, void selectChC (int justForMac, int nonBlocking, int doTimeout, unsigned int stopTime,
SOCKET *pRChannels, int *justForMac2, SOCKET *pSChannels, SOCKET *pRChannels, int *justForMac2, SOCKET *pSChannels,
int *pErrCode int *pErrCode
); );
int tcpPossibleC(void); int tcpPossibleC (void);
//************************************************ //************************************************
// other functions // other functions
...@@ -72,10 +68,10 @@ int insertNewDictionaryItem(SOCKET endpointRef); ...@@ -72,10 +68,10 @@ int insertNewDictionaryItem(SOCKET endpointRef);
// adds it to the dictionary. returns error code: 0==ok, 1==not ok // adds it to the dictionary. returns error code: 0==ok, 1==not ok
dictitem* lookup(SOCKET endpointRef); dictitem* lookup(SOCKET endpointRef);
// lookup entry (CFN) // lookup entry (CFN)
void setEndpointDataC(int endpointRef, int referenceCount, void setEndpointDataC (SOCKET endpointRef, int referenceCount,
int hasReceiveNotifier, int hasSendableNotifier, int aborted); int hasReceiveNotifier, int hasSendableNotifier, int aborted);
// set the corresponding fields of the entry // set the corresponding fields of the entry
void getEndpointDataC(int endpointRef, int *referenceCount, void getEndpointDataC (SOCKET endpointRef, int *referenceCount,
int *hasReceiveNotifier, int *hasSendableNotifier, int *aborted); int *hasReceiveNotifier, int *hasSendableNotifier, int *aborted);
// returns the corresponding fields of the entry // returns the corresponding fields of the entry
void removeDictionaryItem(SOCKET endpointRef); void removeDictionaryItem(SOCKET endpointRef);
...@@ -100,19 +96,19 @@ extern void (*exit_tcpip_function)(); // the function will be called, when the C ...@@ -100,19 +96,19 @@ extern void (*exit_tcpip_function)(); // the function will be called, when the C
//--------------------- FUNCTION IMPLEMENTATION ----------------------------------- //--------------------- FUNCTION IMPLEMENTATION -----------------------------------
int tcpPossibleC(void) int tcpPossibleC (void)
{ {
StartUp(FALSE); StartUp (FALSE);
return tcpStartedUp; return tcpStartedUp;
} }
int os_eom(SOCKET endpointRef) int os_eom (SOCKET endpointRef)
{ {
int err, err2; int err, err2;
char dummyBuffer[1]; char dummyBuffer[1];
dictitem *pDictitem; dictitem *pDictitem;
err = recv( endpointRef, dummyBuffer, 1, MSG_PEEK); err = recv (endpointRef, dummyBuffer, 1, MSG_PEEK);
err2 = WSAGetLastError(); err2 = WSAGetLastError();
if (err>0) if (err>0)
return FALSE; return FALSE;
...@@ -128,7 +124,7 @@ int os_eom(SOCKET endpointRef) ...@@ -128,7 +124,7 @@ int os_eom(SOCKET endpointRef)
return TRUE; return TRUE;
} }
int os_disconnected(SOCKET endpointRef) int os_disconnected (SOCKET endpointRef)
{ {
int err; int err;
char string[1]; char string[1];
...@@ -147,7 +143,7 @@ int os_disconnected(SOCKET endpointRef) ...@@ -147,7 +143,7 @@ int os_disconnected(SOCKET endpointRef)
// only this error can happen with sockets that can still send // only this error can happen with sockets that can still send
} }
int os_connectrequestavailable(SOCKET endpointRef) int os_connectrequestavailable (SOCKET endpointRef)
{ {
FD_SET readSet; FD_SET readSet;
TIMEVAL timeout; TIMEVAL timeout;
...@@ -228,50 +224,48 @@ void StartUp(int abort) ...@@ -228,50 +224,48 @@ void StartUp(int abort)
}; };
} }
void lookupHost_syncC (CleanString inetAddr, int *errCode, int *ipAddrP)
void lookupHost_syncC(CleanString inetAddr, int *errCode, int *ipAddrP)
// error code: 0 ok, 1 error (also: addr doesn't exist) // error code: 0 ok, 1 error (also: addr doesn't exist)
{ {
HOSTENT *hostentP; HOSTENT *hostentP;
unsigned long ipAddr; unsigned long ipAddr;
StartUp(TRUE); StartUp (TRUE);
ipAddr = inet_addr(CleanStringCharacters(inetAddr)); ipAddr = inet_addr (CleanStringCharacters(inetAddr));
if (ipAddr!=INADDR_NONE) if (ipAddr!=INADDR_NONE){
{
*errCode = 0; *errCode = 0;
*ipAddrP = ntohl(ipAddr); *ipAddrP = ntohl(ipAddr);
return; return;
}; }
*errCode = 1; *errCode = 1;
hostentP = gethostbyname(CleanStringCharacters(inetAddr)); // string is alphanumerical hostentP = gethostbyname (CleanStringCharacters (inetAddr)); // string is alphanumerical
if (hostentP!=NULL) if (hostentP!=NULL){
{ *ipAddrP = ntohl(((DWORD *)(*(hostentP->h_addr_list)))[0]); *ipAddrP = ntohl (((DWORD *)(*(hostentP->h_addr_list)))[0]);
if (*ipAddrP!=0) if (*ipAddrP!=0)
*errCode = 0; *errCode = 0;
}; }
} }
void lookupHost_asyncC(CleanString inetAddr, int *errCode, HANDLE *endpointRef) void lookupHost_asyncC (CleanString inetAddr, int *errCode, HANDLE *endpointRef)
// errCode: 0 ok, 1 not ok // errCode: 0 ok, 1 not ok
{ {
DNSInfo *newPtr; DNSInfo *newPtr;
HANDLE dnsHdl; HANDLE dnsHdl;
StartUp(TRUE); StartUp (TRUE);
*errCode = 1; *errCode = 1;
newPtr = (DNSInfo*) LocalAlloc(LMEM_FIXED,sizeof(DNSInfo)); newPtr = (DNSInfo*) LocalAlloc (LMEM_FIXED,sizeof(DNSInfo));
if (newPtr==NULL) { if (newPtr==NULL){
*errCode = 1; *errCode = 1;
return; return;
}; }
newPtr->next = DNSInfoList; newPtr->next = DNSInfoList;
DNSInfoList = newPtr; DNSInfoList = newPtr;
// and fill the fields and initiate DNS lookup. // and fill the fields and initiate DNS lookup.
dnsHdl = WSAAsyncGetHostByName(ghMainWindow,PM_DNS_EVENT,CleanStringCharacters(inetAddr), dnsHdl = WSAAsyncGetHostByName (ghMainWindow,PM_DNS_EVENT,CleanStringCharacters(inetAddr),
DNSInfoList->junion.freeSpace, DNSInfoList->junion.freeSpace,
MAXGETHOSTSTRUCT); MAXGETHOSTSTRUCT);
// this will cause the sending of a PM_DNS_EVENT message to the main window. // this will cause the sending of a PM_DNS_EVENT message to the main window.
...@@ -321,10 +315,10 @@ void openTCP_ListenerC(int portNum, int *pErrCode, SOCKET *pEndpointRef) ...@@ -321,10 +315,10 @@ void openTCP_ListenerC(int portNum, int *pErrCode, SOCKET *pEndpointRef)
if (*pErrCode) if (*pErrCode)
return; return;
setEndpointDataC(s, 1,0,0,0); setEndpointDataC (s, 1,0,0,0);
} }
void acceptC(SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRef) void acceptC (SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRef)
// errCode: 0:ok; otherwise:not ok // errCode: 0:ok; otherwise:not ok
{ {
SOCKET endpointRef; SOCKET endpointRef;
...@@ -334,7 +328,7 @@ void acceptC(SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRe ...@@ -334,7 +328,7 @@ void acceptC(SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRe
clientAdrSize = sizeof(clientAdr); clientAdrSize = sizeof(clientAdr);
endpointRef = accept(listener,(LPSOCKADDR) &clientAdr, &clientAdrSize); endpointRef = accept(listener,(LPSOCKADDR) &clientAdr, &clientAdrSize);
tru = TRUE; tru = TRUE;
ioctlsocket(endpointRef, FIONBIO, &tru); // set mode to non blocking ioctlsocket (endpointRef, FIONBIO, &tru); // set mode to non blocking
*pErrCode = endpointRef==INVALID_SOCKET; *pErrCode = endpointRef==INVALID_SOCKET;
if (*pErrCode) if (*pErrCode)
return; return;
...@@ -346,12 +340,12 @@ void acceptC(SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRe ...@@ -346,12 +340,12 @@ void acceptC(SOCKET listener, int *pErrCode, int *pInetHost, SOCKET *pEndpointRe
if (*pErrCode) if (*pErrCode)
return; return;
setEndpointDataC(endpointRef,2,0,0,0); setEndpointDataC (endpointRef,2,0,0,0);
} }
void os_connectTCPC(int onlyForMac, int block, int doTimeout, unsigned int stopTime, void os_connectTCPC (int onlyForMac, int block, int doTimeout, unsigned int stopTime,
int ipAddr, int portnum, int ipAddr, int portnum,
int *errCodeP, int *timeoutExpiredP, int *endpointRefP) int *errCodeP, int *timeoutExpiredP, size_t *endpointRefP)
// errCode: 0 ok; 1 not ok // errCode: 0 ok; 1 not ok
{ {
SOCKET client; SOCKET client;
...@@ -370,11 +364,10 @@ void os_connectTCPC(int onlyForMac, int block, int doTimeout, unsigned int stopT ...@@ -370,11 +364,10 @@ void os_connectTCPC(int onlyForMac, int block, int doTimeout, unsigned int stopT
clientAdr.sin_port = 0; // the winsock library will choose a free number between 1024 and 5000 clientAdr.sin_port = 0; // the winsock library will choose a free number between 1024 and 5000
err = bind(client, (LPSOCKADDR) &clientAdr, sizeof(clientAdr)); err = bind(client, (LPSOCKADDR) &clientAdr, sizeof(clientAdr));
if (err) if (err){
{
closesocket(client); closesocket(client);
return; return;
}; }
srvAdr.sin_family = AF_INET; // of course internet adress family srvAdr.sin_family = AF_INET; // of course internet adress family
srvAdr.sin_addr.s_addr = htonl(ipAddr); srvAdr.sin_addr.s_addr = htonl(ipAddr);
...@@ -382,121 +375,106 @@ void os_connectTCPC(int onlyForMac, int block, int doTimeout, unsigned int stopT ...@@ -382,121 +375,106 @@ void os_connectTCPC(int onlyForMac, int block, int doTimeout, unsigned int stopT
tru = TRUE; tru = TRUE;
////////////////////////////////////////////////////////////////////////// if (block && doTimeout){
if (block && doTimeout)
{
ioctlsocket(client, FIONBIO, &tru); // set mode to non blocking ioctlsocket(client, FIONBIO, &tru); // set mode to non blocking
err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr)); err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr));
if (!err) { if (!err) {
*errCodeP = 0; *errCodeP = 0;
*timeoutExpiredP = FALSE; *timeoutExpiredP = FALSE;
*endpointRefP = client; *endpointRefP = client;
} } else if (WSAGetLastError()!=WSAEWOULDBLOCK) {
else if (WSAGetLastError()!=WSAEWOULDBLOCK) { closesocket(client);
return;
} else {
FD_SET writeSet, exptnSet;
TIMEVAL timeout;
unsigned int now;
int noOfWritableSockets, timeoutTicks;
FD_ZERO(&writeSet);
FD_SET(client,&writeSet);
FD_ZERO(&exptnSet);
FD_SET(client,&exptnSet);
now = GetTickCount();
timeoutTicks = ((int)stopTime) - ((int)now);
if (timeoutTicks<=0)
{ // timeout expired
closesocket(client); closesocket(client);
*timeoutExpiredP = TRUE;
return; return;
}
else
{
FD_SET writeSet, exptnSet;
TIMEVAL timeout;
unsigned int now;
int noOfWritableSockets, timeoutTicks;
FD_ZERO(&writeSet);
FD_SET(client,&writeSet);
FD_ZERO(&exptnSet);
FD_SET(client,&exptnSet);
now = GetTickCount();
timeoutTicks = ((int)stopTime) - ((int)now);
if (timeoutTicks<=0)
{ // timeout expired
closesocket(client);
*timeoutExpiredP = TRUE;
return;
};
timeout.tv_sec = timeoutTicks / 1000; // Timeout in sec's
timeout.tv_usec = (timeoutTicks % 1000)*1000; // Timeout in microsec's
noOfWritableSockets = select(0,NULL,&writeSet,&exptnSet,&timeout);
*errCodeP = noOfWritableSockets<0
|| (noOfWritableSockets>0 && FD_ISSET(client,&exptnSet));
*timeoutExpiredP = noOfWritableSockets==0;
*endpointRefP = client;
if (*errCodeP || *timeoutExpiredP) {
closesocket(client);
return;
};
}; };
}; timeout.tv_sec = timeoutTicks / 1000; // Timeout in sec's
/////////////////////////////////////////////////////////////////////////// timeout.tv_usec = (timeoutTicks % 1000)*1000; // Timeout in microsec's
if (block && !doTimeout) noOfWritableSockets = select(0,NULL,&writeSet,&exptnSet,&timeout);
{ *errCodeP = noOfWritableSockets<0
|| (noOfWritableSockets>0 && FD_ISSET(client,&exptnSet));
*timeoutExpiredP = noOfWritableSockets==0;
*endpointRefP = client;
if (*errCodeP || *timeoutExpiredP) {
closesocket(client);
return;
}
}
}
if (block && !doTimeout){
err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr)); err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr));
if (err) if (err){
{
closesocket(client); closesocket(client);
return; return;
}; }
ioctlsocket(client, FIONBIO, &tru); // set mode to non blocking ioctlsocket(client, FIONBIO, &tru); // set mode to non blocking
*errCodeP = 0; *errCodeP = 0;
*timeoutExpiredP = FALSE; *timeoutExpiredP = FALSE;
*endpointRefP = client; *endpointRefP = client;
}; }
////////////////////////////////////////////////////////////////////////////
if (!block) if (!block){
{
err = WSAAsyncSelect(client,ghMainWindow,PM_SOCKET_EVENT,FD_CONNECT); err = WSAAsyncSelect(client,ghMainWindow,PM_SOCKET_EVENT,FD_CONNECT);
if (err) if (err){
{
closesocket(client); closesocket(client);
return; return;
}; }
err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr)); err = connect(client, (LPSOCKADDR) &srvAdr, sizeof(srvAdr));
if (err==SOCKET_ERROR) if (err==SOCKET_ERROR){
{
err = WSAGetLastError(); // a WSAEWOULDBLOCK error is a pretty harmless thing err = WSAGetLastError(); // a WSAEWOULDBLOCK error is a pretty harmless thing
if (err!=WSAEWOULDBLOCK) if (err!=WSAEWOULDBLOCK){
{
closesocket(client); closesocket(client);
return; return;
}; }
}; }
*errCodeP = 0; *errCodeP = 0;
*timeoutExpiredP = FALSE; *timeoutExpiredP = FALSE;
*endpointRefP = client; *endpointRefP = client;
}; }
//////////////////////////////////////////////////////////////////////////////
*errCodeP = insertNewDictionaryItem(client); *errCodeP = insertNewDictionaryItem(client);
if (*errCodeP) if (*errCodeP){
{
closesocket(client); closesocket(client);
return; return;
}; }
if (block) if (block)
setEndpointDataC(client,2,0,0,0); setEndpointDataC (client,2,0,0,0);
else else {
{
dictitem *ptr; dictitem *ptr;
ptr = lookup(client); ptr = lookup(client);
ptr->referenceCount = 1; ptr->referenceCount = 1;
ptr->hasReceiveNotifier = 0; ptr->hasReceiveNotifier = 0;
ptr->hasSendableNotifier = 1; ptr->hasSendableNotifier = 1;
ptr->aborted = 0; ptr->aborted = 0;
}; }
} }
void sendC(SOCKET endpointRef, CleanString data, int begin, int nBytes, void sendC (SOCKET endpointRef, CleanString data, int begin, int nBytes,
int *pErrCode, int *pSentBytes) int *pErrCode, int *pSentBytes)
{ {
int sentBytes; int sentBytes;
*pErrCode = 0; *pErrCode = 0;
sentBytes = send(endpointRef, CleanStringCharacters(data)+begin,nBytes, 0); sentBytes = send (endpointRef, CleanStringCharacters(data)+begin,nBytes, 0);
if (sentBytes==SOCKET_ERROR) { if (sentBytes==SOCKET_ERROR) {
int err; int err;
sentBytes = 0; sentBytes = 0;
...@@ -514,31 +492,29 @@ void sendC(SOCKET endpointRef, CleanString data, int begin, int nBytes, ...@@ -514,31 +492,29 @@ void sendC(SOCKET endpointRef, CleanString data, int begin, int nBytes,
} }
void receiveC(SOCKET endpointRef, int maxSize, CleanString *pReceived) void receiveC (SOCKET endpointRef, int maxSize, CleanString *pReceived)
{ {
int size, received; int size, received;
dictitem *pDictitem; dictitem *pDictitem;
*pReceived = (CleanString) pRcvBuff; *pReceived = (CleanString) pRcvBuff;
size = maxSize<=0 ? rcvBuffSize : maxSize; size = maxSize<=0 ? rcvBuffSize : maxSize;
received = recv( endpointRef, CleanStringCharacters(pRcvBuff), size, 0); received = recv (endpointRef, CleanStringCharacters(pRcvBuff), size, 0);
pDictitem = lookup(endpointRef); pDictitem = lookup(endpointRef);
trace( if (!pDictitem) trace( if (!pDictitem)
rMessageBox(NULL, MB_APPLMODAL, "in receiveC", "ERROR");) rMessageBox(NULL, MB_APPLMODAL, "in receiveC", "ERROR");)
if (received>0) { if (received>0){
pDictitem->availByteValid = 0; pDictitem->availByteValid = 0;
CleanStringLength(pRcvBuff) = received; CleanStringLength(pRcvBuff) = received;
} } else if (pDictitem->availByteValid) {
else if (pDictitem->availByteValid) { CleanStringCharacters(pRcvBuff)[0] = pDictitem->availByte;
CleanStringCharacters(pRcvBuff)[0] = pDictitem->availByte; pDictitem->availByteValid = 0;
pDictitem->availByteValid = 0; CleanStringLength(pRcvBuff) = 1;
CleanStringLength(pRcvBuff) = 1; } else
} CleanStringLength(pRcvBuff) = 0;
else
CleanStringLength(pRcvBuff) = 0;
} }
int data_availableC(SOCKET endpointRef) int data_availableC (SOCKET endpointRef)
{ {