75 #include <OpenTransport.h>
76 #include <OpenTpTInternet.h>
77 #include <OpenTptClient.h>
87 #ifdef MACSOCKET_DEBUG
96 #define kMaxNumSockets 4
145 static Boolean sSocketsSetup =
false;
150 static OSErr MyBusyWait(
SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag);
152 static pascal
void OTNonYieldingNotifier(
void *contextPtr,OTEventCode
code,OTResult result,
void *cookie);
154 static Boolean SocketIndexIsValid(
const int inSocketNum);
158 static void PrepareForAsyncOperation(
SocketStruct *ioSocket,
const OTEventCode inExpectedCode);
160 static Boolean TimeoutElapsed(
const SocketStruct *inSocket);
162 static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,
const Boolean inEnableReuseIP);
168 if (outSocketErrCode != nil)
170 *outSocketErrCode = -1;
173 if (outSocketErrString != nil)
179 if (SocketIndexIsValid(inSocketNum))
181 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
184 if (outSocketErrCode != nil)
186 *outSocketErrCode = theSocketStruct->
mLastError;
189 if (outSocketErrString != nil)
199 if (SocketIndexIsValid(inSocketNum))
201 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
211 if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
213 char tempString[256];
214 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
221 InetAddress *theInetAddress = (InetAddress *) theSocketStruct->
mAssignedAddrInfo->addr.buf;
222 InetHost theInetHost = theInetAddress->fHost;
224 if (theInetHost == 0)
226 InetInterfaceInfo theInetInterfaceInfo;
228 if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr)
230 theInetHost = theInetInterfaceInfo.fAddress;
234 ::OTInetHostToString(theInetHost,tempString);
248 if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
250 char tempString[256];
251 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
258 InetAddress *theInetAddress = (InetAddress *) theSocketStruct->
mRemoteAddrInfo->addr.buf;
259 InetHost theInetHost = theInetAddress->fHost;
261 if (theInetHost == 0)
263 InetInterfaceInfo theInetInterfaceInfo;
265 if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr)
267 theInetHost = theInetInterfaceInfo.fAddress;
271 ::OTInetHostToString(theInetHost,tempString);
285 Boolean theResult =
false;
287 if (SocketIndexIsValid(inSocketNum))
289 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
301 Boolean theResult =
false;
303 if (SocketIndexIsValid(inSocketNum))
305 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
317 if (SocketIndexIsValid(inSocketNum))
319 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
334 if (SocketIndexIsValid(inSocketNum))
336 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
349 static Boolean TimeoutElapsed(
const SocketStruct *inSocket)
351 Boolean timeIsUp =
false;
364 static Boolean SocketIndexIsValid(
const int inSocketNum)
424 static void PrepareForAsyncOperation(
SocketStruct *ioSocket,
const OTEventCode inExpectedCode)
436 static OSErr MyBusyWait(
SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag)
438 OSErr errCode = noErr;
439 OTResult theOTResult = noErr;
447 if (*inAsyncOperationCompleteFlag)
458 if (theOTResult != noErr && returnImmediatelyOnError)
464 if (TimeoutElapsed(ioSocket))
475 if (outOTResult != nil)
477 *outOTResult = theOTResult;
487 static pascal
void OTNonYieldingNotifier(
void *contextPtr,OTEventCode
code,OTResult result,
void *cookie)
491 if (theSocketStruct != nil)
586 ::OTRcvOrderlyDisconnect(theSocketStruct->
mEndPointRef);
591 case T_DISCONNECTCOMPLETE:
628 InitSocket(&(sSockets[i]));
631 ::InitOpenTransport();
633 sSocketsSetup =
true;
654 if (theSocketStruct->
mEndPointRef != kOTInvalidEndpointRef)
656 OTResult theOTResult;
674 theOTResult = ::OTCloseProvider(theSocketStruct->
mEndPointRef);
705 ::CloseOpenTransport();
707 sSocketsSetup =
false;
724 #pragma unused(inDoThreadSwitching)
727 OSErr errCode = noErr;
741 OTResult theOTResult;
745 InitSocket(theSocketStruct);
755 PrepareForAsyncOperation(theSocketStruct,T_OPENCOMPLETE);
757 theOTResult = ::OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName),0,nil,OTNonYieldingNotifier,(
void *) theSocketStruct);
775 else if (i == kMaxNumSockets - 1)
794 OSErr errCode = noErr;
798 if (!SocketIndexIsValid(inSocketNum))
804 theSocketStruct = &(sSockets[inSocketNum]);
807 OTResult theOTResult;
837 InetInterfaceInfo theInetInterfaceInfo;
839 theOTResult = ::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface);
850 ::OTInitInetAddress(theInetAddress,inPortNum,theInetInterfaceInfo.fAddress);
857 theOTResult = ::OTSetSynchronous(theSocketStruct->
mEndPointRef);
861 theOTResult = NegotiateIPReuseAddrOption(theSocketStruct->
mEndPointRef,
true);
865 theOTResult = ::OTSetAsynchronous(theSocketStruct->
mEndPointRef);
870 PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
885 PrepareForAsyncOperation(theSocketStruct,T_LISTEN);
889 if (theOTResult == noErr)
891 PrepareForAsyncOperation(theSocketStruct,T_PASSCON);
902 else if (theOTResult == kOTNoDataErr)
918 if (theSocketStruct != nil)
924 if (errCode != noErr)
942 OSErr errCode = noErr;
946 if (!SocketIndexIsValid(inSocketNum))
951 theSocketStruct = &(sSockets[inSocketNum]);
959 OTResult theOTResult;
979 PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
993 DNSAddress hostDNSAddress;
997 sndCall.addr.buf = (UInt8 *) &hostDNSAddress;
998 sndCall.addr.len = ::OTInitDNSAddress(&hostDNSAddress,inTargetAddressAndPort);
999 sndCall.opt.buf = nil;
1000 sndCall.opt.len = 0;
1001 sndCall.udata.buf = nil;
1002 sndCall.udata.len = 0;
1003 sndCall.sequence = 0;
1007 PrepareForAsyncOperation(theSocketStruct,T_CONNECT);
1009 theOTResult = ::OTConnect(theSocketStruct->
mEndPointRef,&sndCall,nil);
1011 if (theOTResult == kOTNoDataErr)
1013 theOTResult = noErr;
1022 SetErrorMessageAndBail(
"MacSocket_connect: Can't connect OT endpoint, OTConnect() = kMacSocket_TimeoutErr");
1030 theOTResult = ::OTRcvConnect(theSocketStruct->
mEndPointRef,nil);
1038 #ifdef MACSOCKET_DEBUG
1039 printf(
"MacSocket_connect: connect completed\n");
1044 if (theSocketStruct != nil)
1050 if (errCode != noErr)
1070 OSErr errCode = noErr;
1074 if (!SocketIndexIsValid(inSocketNum))
1080 theSocketStruct = &(sSockets[inSocketNum]);
1082 if (theSocketStruct->
mEndPointRef != kOTInvalidEndpointRef)
1084 OTResult theOTResult = noErr;
1094 theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->
mEndPointRef);
1102 theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->
mEndPointRef);
1114 if (theOTResult != noErr)
1121 theOTResult = ::OTCloseProvider(theSocketStruct->
mEndPointRef);
1126 errCode = theOTResult;
1135 if (theSocketStruct != nil)
1141 if (errCode != noErr)
1159 int MacSocket_recv(
const int inSocketNum,
void *outBuff,
int outBuffLength,
const Boolean inBlock)
1161 OSErr errCode = noErr;
1162 int totalBytesRead = 0;
1168 if (outBuffLength <= 0)
1173 if (!SocketIndexIsValid(inSocketNum))
1178 theSocketStruct = &(sSockets[inSocketNum]);
1193 PrepareForAsyncOperation(theSocketStruct,0);
1198 OTResult theOTResult;
1201 theOTResult = ::OTRcv(theSocketStruct->
mEndPointRef,(
void *) ((
unsigned long) outBuff + (
unsigned long) totalBytesRead),outBuffLength - totalBytesRead,nil);
1203 if (theOTResult >= 0)
1205 bytesRead = theOTResult;
1207 #ifdef MACSOCKET_DEBUG
1208 printf(
"MacSocket_recv: read %d bytes in part\n",bytesRead);
1212 else if (theOTResult == kOTNoDataErr)
1223 totalBytesRead += bytesRead;
1226 if (totalBytesRead <= 0)
1237 if (TimeoutElapsed(theSocketStruct))
1260 #ifdef MACSOCKET_DEBUG
1261 printf(
"MacSocket_recv: read %d bytes in total\n",totalBytesRead);
1267 if (theSocketStruct != nil)
1273 if (errCode != noErr)
1283 return(totalBytesRead);
1292 OSErr errCode = noErr;
1299 if (inBuffLength <= 0)
1304 if (!SocketIndexIsValid(inSocketNum))
1310 theSocketStruct = &(sSockets[inSocketNum]);
1318 OTResult theOTResult;
1321 PrepareForAsyncOperation(theSocketStruct,0);
1323 while (bytesSent < inBuffLength)
1333 theOTResult = ::OTSnd(theSocketStruct->
mEndPointRef,(
void *) ((
unsigned long) inBuff + bytesSent),inBuffLength - bytesSent,0);
1335 if (theOTResult >= 0)
1337 bytesSent += theOTResult;
1339 theOTResult = noErr;
1343 PrepareForAsyncOperation(theSocketStruct,0);
1346 if (theOTResult == kOTFlowErr)
1348 if (TimeoutElapsed(theSocketStruct))
1353 theOTResult = noErr;
1362 #ifdef MACSOCKET_DEBUG
1363 printf(
"MacSocket_send: sent %d bytes\n",bytesSent);
1369 if (theSocketStruct != nil)
1375 if (errCode != noErr)
1383 if (errCode != noErr)
1397 static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,
const Boolean inEnableReuseIP)
1400 UInt8 buf[kOTFourByteOptionSize];
1401 TOption* theOTOption;
1402 TOptMgmt theOTRequest;
1403 TOptMgmt theOTResult;
1406 if (!OTIsSynchronous(inEndpoint))
1411 theOTRequest.opt.buf = buf;
1412 theOTRequest.opt.len =
sizeof(buf);
1413 theOTRequest.flags = T_NEGOTIATE;
1415 theOTResult.opt.buf = buf;
1416 theOTResult.opt.maxlen = kOTFourByteOptionSize;
1419 theOTOption = (TOption *) buf;
1421 theOTOption->level = INET_IP;
1422 theOTOption->name = IP_REUSEADDR;
1423 theOTOption->len = kOTFourByteOptionSize;
1424 theOTOption->status = 0;
1425 *((UInt32 *) (theOTOption->value)) = inEnableReuseIP;
1427 errCode = ::OTOptionManagement(inEndpoint,&theOTRequest,&theOTResult);
1429 if (errCode == kOTNoError)
1431 if (theOTOption->status != T_SUCCESS)
1433 errCode = theOTOption->status;
1438 errCode = kOTNoError;