2 * A simple socket-like package.
3 * This could undoubtedly be improved, since it does polling and busy-waiting.
4 * At least it uses asynch I/O and implements timeouts!
6 * Other funkiness includes the use of my own (possibly brain-damaged) error-handling infrastructure.
8 * -Roy Wood (roy@centricsystems.ca)
13 /* ====================================================================
14 * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
28 * 3. All advertising materials mentioning features or use of this
29 * software must display the following acknowledgment:
30 * "This product includes software developed by the OpenSSL Project
31 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
33 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
34 * endorse or promote products derived from this software without
35 * prior written permission. For written permission, please contact
36 * openssl-core@openssl.org.
38 * 5. Products derived from this software may not be called "OpenSSL"
39 * nor may "OpenSSL" appear in their names without prior written
40 * permission of the OpenSSL Project.
42 * 6. Redistributions of any form whatsoever must retain the following
44 * "This product includes software developed by the OpenSSL Project
45 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
47 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
48 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
51 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
54 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 * OF THE POSSIBILITY OF SUCH DAMAGE.
59 * ====================================================================
61 * This product includes cryptographic software written by Eric Young
62 * (eay@cryptsoft.com). This product includes software written by Tim
63 * Hudson (tjh@cryptsoft.com).
71 #include "MacSocket.h"
75 #include <OpenTransport.h>
76 #include <OpenTpTInternet.h>
77 #include <OpenTptClient.h>
81 #include "CPStringUtils.hpp"
82 #include "ErrorHandling.hpp"
85 // #define MACSOCKET_DEBUG 1
87 #ifdef MACSOCKET_DEBUG
96 #define kMaxNumSockets 4
103 Boolean mEndpointIsBound;
105 Boolean mLocalEndIsConnected;
106 Boolean mRemoteEndIsConnected;
108 Boolean mReceivedTOpenComplete;
109 Boolean mReceivedTBindComplete;
110 Boolean mReceivedTConnect;
111 Boolean mReceivedTListen;
112 Boolean mReceivedTPassCon;
113 Boolean mReceivedTDisconnect;
114 Boolean mReceivedTOrdRel;
115 Boolean mReceivedTDisconnectComplete;
118 long mOperationStartTicks;
120 MacSocket_IdleWaitCallback mIdleWaitCallback;
123 OTEventCode mExpectedCode;
124 OTResult mAsyncOperationResult;
126 EndpointRef mEndPointRef;
127 TBind *mBindRequestedAddrInfo;
128 TBind *mAssignedAddrInfo;
129 TCall *mRemoteAddrInfo;
131 Boolean mReadyToReadData;
132 Boolean mReadyToWriteData;
138 char mErrMessage[256];
141 typedef struct SocketStruct SocketStruct;
144 static SocketStruct sSockets[kMaxNumSockets];
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);
156 static void InitSocket(SocketStruct *ioSocket);
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);
166 void MacSocket_GetSocketErrorInfo(const int inSocketNum,int *outSocketErrCode,char *outSocketErrString,const int inSocketErrStringMaxLength)
168 if (outSocketErrCode != nil)
170 *outSocketErrCode = -1;
173 if (outSocketErrString != nil)
175 CopyCStrToCStr("",outSocketErrString,inSocketErrStringMaxLength);
179 if (SocketIndexIsValid(inSocketNum))
181 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
184 if (outSocketErrCode != nil)
186 *outSocketErrCode = theSocketStruct->mLastError;
189 if (outSocketErrString != nil)
191 CopyCStrToCStr(theSocketStruct->mErrMessage,outSocketErrString,inSocketErrStringMaxLength);
197 void MacSocket_SetUserRefPtr(const int inSocketNum,void *inNewRefPtr)
199 if (SocketIndexIsValid(inSocketNum))
201 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
203 theSocketStruct->mUserRefPtr = inNewRefPtr;
209 void MacSocket_GetLocalIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength)
211 if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
213 char tempString[256];
214 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
217 CopyCStrToCStr("",tempString,sizeof(tempString));
219 if (theSocketStruct->mAssignedAddrInfo != nil)
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);
236 ConcatCStrToCStr(":",tempString,sizeof(tempString));
237 ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString));
240 CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength);
246 void MacSocket_GetRemoteIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength)
248 if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
250 char tempString[256];
251 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
254 CopyCStrToCStr("",tempString,sizeof(tempString));
256 if (theSocketStruct->mRemoteAddrInfo != nil)
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);
273 ConcatCStrToCStr(":",tempString,sizeof(tempString));
274 ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString));
277 CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength);
283 Boolean MacSocket_RemoteEndIsClosing(const int inSocketNum)
285 Boolean theResult = false;
287 if (SocketIndexIsValid(inSocketNum))
289 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
291 theResult = theSocketStruct->mReceivedTOrdRel;
299 Boolean MacSocket_ListenCompleted(const int inSocketNum)
301 Boolean theResult = false;
303 if (SocketIndexIsValid(inSocketNum))
305 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
307 theResult = theSocketStruct->mReceivedTPassCon;
315 Boolean MacSocket_RemoteEndIsOpen(const int inSocketNum)
317 if (SocketIndexIsValid(inSocketNum))
319 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
321 return(theSocketStruct->mRemoteEndIsConnected);
332 Boolean MacSocket_LocalEndIsOpen(const int inSocketNum)
334 if (SocketIndexIsValid(inSocketNum))
336 SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
338 return(theSocketStruct->mLocalEndIsConnected);
349 static Boolean TimeoutElapsed(const SocketStruct *inSocket)
351 Boolean timeIsUp = false;
353 if (inSocket != nil && inSocket->mTimeoutTicks > 0 && ::TickCount() > inSocket->mOperationStartTicks + inSocket->mTimeoutTicks)
364 static Boolean SocketIndexIsValid(const int inSocketNum)
366 if (inSocketNum >= 0 && inSocketNum < kMaxNumSockets && sSockets[inSocketNum].mEndPointRef != kOTInvalidEndpointRef)
379 static void InitSocket(SocketStruct *ioSocket)
381 ioSocket->mIsInUse = false;
383 ioSocket->mEndpointIsBound = false;
385 ioSocket->mLocalEndIsConnected = false;
386 ioSocket->mRemoteEndIsConnected = false;
388 ioSocket->mReceivedTOpenComplete = false;
389 ioSocket->mReceivedTBindComplete = false;
390 ioSocket->mReceivedTConnect = false;
391 ioSocket->mReceivedTListen = false;
392 ioSocket->mReceivedTPassCon = false;
393 ioSocket->mReceivedTDisconnect = false;
394 ioSocket->mReceivedTOrdRel = false;
395 ioSocket->mReceivedTDisconnectComplete = false;
397 ioSocket->mTimeoutTicks = 30 * 60;
398 ioSocket->mOperationStartTicks = -1;
400 ioSocket->mIdleWaitCallback = nil;
401 ioSocket->mUserRefPtr = nil;
403 ioSocket->mExpectedCode = 0;
404 ioSocket->mAsyncOperationResult = noErr;
406 ioSocket->mEndPointRef = kOTInvalidEndpointRef;
408 ioSocket->mBindRequestedAddrInfo = nil;
409 ioSocket->mAssignedAddrInfo = nil;
410 ioSocket->mRemoteAddrInfo = nil;
412 ioSocket->mReadyToReadData = false;
413 ioSocket->mReadyToWriteData = true;
415 ioSocket->mReadBuffer = nil;
416 ioSocket->mWriteBuffer = nil;
418 ioSocket->mLastError = noErr;
419 CopyCStrToCStr("",ioSocket->mErrMessage,sizeof(ioSocket->mErrMessage));
424 static void PrepareForAsyncOperation(SocketStruct *ioSocket,const OTEventCode inExpectedCode)
426 ioSocket->mOperationStartTicks = ::TickCount();
428 ioSocket->mAsyncOperationResult = noErr;
430 ioSocket->mExpectedCode = inExpectedCode;
434 // The wait function....
436 static OSErr MyBusyWait(SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag)
438 OSErr errCode = noErr;
439 OTResult theOTResult = noErr;
442 SetErrorMessageAndBailIfNil(ioSocket,"MyBusyWait: Bad parameter, ioSocket = nil");
443 SetErrorMessageAndBailIfNil(inAsyncOperationCompleteFlag,"MyBusyWait: Bad parameter, inAsyncOperationCompleteFlag = nil");
447 if (*inAsyncOperationCompleteFlag)
449 theOTResult = ioSocket->mAsyncOperationResult;
454 if (ioSocket->mIdleWaitCallback != nil)
456 theOTResult = (*(ioSocket->mIdleWaitCallback))(ioSocket->mUserRefPtr);
458 if (theOTResult != noErr && returnImmediatelyOnError)
464 if (TimeoutElapsed(ioSocket))
466 theOTResult = kMacSocket_TimeoutErr;
475 if (outOTResult != nil)
477 *outOTResult = theOTResult;
485 // I used to do thread switching, but stopped. It could easily be rolled back in though....
487 static pascal void OTNonYieldingNotifier(void *contextPtr,OTEventCode code,OTResult result,void *cookie)
489 SocketStruct *theSocketStruct = (SocketStruct *) contextPtr;
491 if (theSocketStruct != nil)
493 if (theSocketStruct->mExpectedCode != 0 && code == theSocketStruct->mExpectedCode)
495 theSocketStruct->mAsyncOperationResult = result;
497 theSocketStruct->mExpectedCode = 0;
505 theSocketStruct->mReceivedTOpenComplete = true;
507 theSocketStruct->mEndPointRef = (EndpointRef) cookie;
515 theSocketStruct->mReceivedTBindComplete = true;
523 theSocketStruct->mReceivedTConnect = true;
525 theSocketStruct->mLocalEndIsConnected = true;
527 theSocketStruct->mRemoteEndIsConnected = true;
535 theSocketStruct->mReceivedTListen = true;
543 theSocketStruct->mReceivedTPassCon = true;
545 theSocketStruct->mLocalEndIsConnected = true;
547 theSocketStruct->mRemoteEndIsConnected = true;
555 theSocketStruct->mReadyToReadData = true;
562 theSocketStruct->mReadyToWriteData = true;
569 theSocketStruct->mReceivedTDisconnect = true;
571 theSocketStruct->mRemoteEndIsConnected = false;
573 theSocketStruct->mLocalEndIsConnected = false;
575 ::OTRcvDisconnect(theSocketStruct->mEndPointRef,nil);
582 theSocketStruct->mReceivedTOrdRel = true;
584 // We can still write data, so don't clear mRemoteEndIsConnected
586 ::OTRcvOrderlyDisconnect(theSocketStruct->mEndPointRef);
591 case T_DISCONNECTCOMPLETE:
593 theSocketStruct->mReceivedTDisconnectComplete = true;
595 theSocketStruct->mRemoteEndIsConnected = false;
597 theSocketStruct->mLocalEndIsConnected = false;
605 T_CONNECT OTRcvConnect
606 T_DATA OTRcv, OTRcvUData
607 T_DISCONNECT OTRcvDisconnect
608 T_ORDREL OTRcvOrderlyDisconnect
609 T_GODATA OTSnd, OTSndUData, OTLook
613 T_GOEXDATA OTSnd, OTLook
620 // Initialize the main socket data structure
622 OSErr MacSocket_Startup(void)
626 for (int i = 0;i < kMaxNumSockets;i++)
628 InitSocket(&(sSockets[i]));
631 ::InitOpenTransport();
633 sSocketsSetup = true;
642 // Cleanup before exiting
644 OSErr MacSocket_Shutdown(void)
648 for (int i = 0;i < kMaxNumSockets;i++)
650 SocketStruct *theSocketStruct = &(sSockets[i]);
652 if (theSocketStruct->mIsInUse)
654 if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef)
656 OTResult theOTResult;
659 // Since we're killing the endpoint, I don't bother to send the disconnect (sorry!)
662 if (theSocketStruct->mLocalEndIsConnected)
664 // This is an abortive action, so we do a hard disconnect instead of an OTSndOrderlyDisconnect
666 theOTResult = ::OTSndDisconnect(theSocketStruct->mEndPointRef, nil);
668 // Now we have to watch for T_DISCONNECTCOMPLETE event
670 theSocketStruct->mLocalEndIsConnected = false;
674 theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef);
677 theSocketStruct->mEndPointRef = kOTInvalidEndpointRef;
680 if (theSocketStruct->mBindRequestedAddrInfo != nil)
682 ::OTFree((void *) theSocketStruct->mBindRequestedAddrInfo,T_BIND);
684 theSocketStruct->mBindRequestedAddrInfo = nil;
687 if (theSocketStruct->mAssignedAddrInfo != nil)
689 ::OTFree((void *) theSocketStruct->mAssignedAddrInfo,T_BIND);
691 theSocketStruct->mAssignedAddrInfo = nil;
694 if (theSocketStruct->mRemoteAddrInfo != nil)
696 ::OTFree((void *) theSocketStruct->mRemoteAddrInfo,T_CALL);
698 theSocketStruct->mRemoteAddrInfo = nil;
705 ::CloseOpenTransport();
707 sSocketsSetup = false;
720 OSErr MacSocket_socket(int *outSocketNum,const Boolean inDoThreadSwitching,const long inTimeoutTicks,MacSocket_IdleWaitCallback inIdleWaitCallback,void *inUserRefPtr)
722 // Gotta roll support back in for threads eventually.....
724 #pragma unused(inDoThreadSwitching)
727 OSErr errCode = noErr;
730 SetErrorMessageAndBailIfNil(outSocketNum,"MacSocket_socket: Bad parameter, outSocketNum == nil");
735 // Find an unused socket
737 for (int i = 0;i < kMaxNumSockets;i++)
739 if (sSockets[i].mIsInUse == false)
741 OTResult theOTResult;
742 SocketStruct *theSocketStruct = &(sSockets[i]);
745 InitSocket(theSocketStruct);
747 theSocketStruct->mIdleWaitCallback = inIdleWaitCallback;
748 theSocketStruct->mUserRefPtr = inUserRefPtr;
750 theSocketStruct->mTimeoutTicks = inTimeoutTicks;
753 // Set up OT endpoint
755 PrepareForAsyncOperation(theSocketStruct,T_OPENCOMPLETE);
757 theOTResult = ::OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName),0,nil,OTNonYieldingNotifier,(void *) theSocketStruct);
759 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult);
761 BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOpenComplete)));
763 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult);
770 theSocketStruct->mIsInUse = true;
775 else if (i == kMaxNumSockets - 1)
777 SetErrorMessageAndBail("MacSocket_socket: No sockets available");
792 OSErr MacSocket_listen(const int inSocketNum,const int inPortNum)
794 OSErr errCode = noErr;
795 SocketStruct *theSocketStruct = nil;
798 if (!SocketIndexIsValid(inSocketNum))
800 SetErrorMessageAndBail("MacSocket_listen: Invalid socket number specified");
804 theSocketStruct = &(sSockets[inSocketNum]);
807 OTResult theOTResult;
810 if (theSocketStruct->mBindRequestedAddrInfo == nil)
812 theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
814 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
815 SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
818 if (theSocketStruct->mAssignedAddrInfo == nil)
820 theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
822 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
823 SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
826 if (theSocketStruct->mRemoteAddrInfo == nil)
828 theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult);
830 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult);
831 SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() returned nil");
835 if (!theSocketStruct->mEndpointIsBound)
837 InetInterfaceInfo theInetInterfaceInfo;
839 theOTResult = ::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface);
841 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't determine OT interface info, OTInetGetInterfaceInfo() = ",theOTResult);
844 InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mBindRequestedAddrInfo->addr.buf;
846 // theInetAddress->fAddressType = AF_INET;
847 // theInetAddress->fPort = inPortNum;
848 // theInetAddress->fHost = theInetInterfaceInfo.fAddress;
850 ::OTInitInetAddress(theInetAddress,inPortNum,theInetInterfaceInfo.fAddress);
852 theSocketStruct->mBindRequestedAddrInfo->addr.len = sizeof(InetAddress);
854 theSocketStruct->mBindRequestedAddrInfo->qlen = 1;
857 theOTResult = ::OTSetSynchronous(theSocketStruct->mEndPointRef);
859 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetSynchronous() = ",theOTResult);
861 theOTResult = NegotiateIPReuseAddrOption(theSocketStruct->mEndPointRef,true);
863 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT IP address reuse flag, NegotiateIPReuseAddrOption() = ",theOTResult);
865 theOTResult = ::OTSetAsynchronous(theSocketStruct->mEndPointRef);
867 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetAsynchronous() = ",theOTResult);
870 PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
872 theOTResult = ::OTBind(theSocketStruct->mEndPointRef,theSocketStruct->mBindRequestedAddrInfo,theSocketStruct->mAssignedAddrInfo);
874 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult);
876 BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete)));
878 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult);
881 theSocketStruct->mEndpointIsBound = true;
885 PrepareForAsyncOperation(theSocketStruct,T_LISTEN);
887 theOTResult = ::OTListen(theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo);
889 if (theOTResult == noErr)
891 PrepareForAsyncOperation(theSocketStruct,T_PASSCON);
893 theOTResult = ::OTAccept(theSocketStruct->mEndPointRef,theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo);
895 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't begin OT accept, OTAccept() = ",theOTResult);
897 BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTPassCon)));
899 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't accept OT connection, OTAccept() = ",theOTResult);
902 else if (theOTResult == kOTNoDataErr)
909 SetErrorMessageAndLongIntAndBail("MacSocket_listen: Can't begin OT listen, OTListen() = ",theOTResult);
918 if (theSocketStruct != nil)
920 theSocketStruct->mLastError = noErr;
922 CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
924 if (errCode != noErr)
926 theSocketStruct->mLastError = errCode;
928 CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
940 OSErr MacSocket_connect(const int inSocketNum,char *inTargetAddressAndPort)
942 OSErr errCode = noErr;
943 SocketStruct *theSocketStruct = nil;
946 if (!SocketIndexIsValid(inSocketNum))
948 SetErrorMessageAndBail("MacSocket_connect: Invalid socket number specified");
951 theSocketStruct = &(sSockets[inSocketNum]);
953 if (theSocketStruct->mEndpointIsBound)
955 SetErrorMessageAndBail("MacSocket_connect: Socket previously bound");
959 OTResult theOTResult;
961 theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
963 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
964 SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
967 theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
969 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
970 SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
973 theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult);
975 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult);
976 SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() returned nil");
979 PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
981 theOTResult = ::OTBind(theSocketStruct->mEndPointRef,nil,theSocketStruct->mAssignedAddrInfo);
983 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult);
985 BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete)));
987 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult);
989 theSocketStruct->mEndpointIsBound = true;
993 DNSAddress hostDNSAddress;
995 // Set up target address
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;
1016 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult);
1018 BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTConnect)));
1020 if (theOTResult == kMacSocket_TimeoutErr)
1022 SetErrorMessageAndBail("MacSocket_connect: Can't connect OT endpoint, OTConnect() = kMacSocket_TimeoutErr");
1027 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult);
1030 theOTResult = ::OTRcvConnect(theSocketStruct->mEndPointRef,nil);
1032 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't complete connect on OT endpoint, OTRcvConnect() = ",theOTResult);
1038 #ifdef MACSOCKET_DEBUG
1039 printf("MacSocket_connect: connect completed\n");
1044 if (theSocketStruct != nil)
1046 theSocketStruct->mLastError = noErr;
1048 CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1050 if (errCode != noErr)
1052 theSocketStruct->mLastError = errCode;
1054 CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1066 // Close a connection
1068 OSErr MacSocket_close(const int inSocketNum)
1070 OSErr errCode = noErr;
1071 SocketStruct *theSocketStruct = nil;
1074 if (!SocketIndexIsValid(inSocketNum))
1076 SetErrorMessageAndBail("MacSocket_close: Invalid socket number specified");
1080 theSocketStruct = &(sSockets[inSocketNum]);
1082 if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef)
1084 OTResult theOTResult = noErr;
1088 if (theSocketStruct->mReceivedTOrdRel)
1090 // Already did an OTRcvOrderlyDisconnect() in the notifier
1092 if (theSocketStruct->mLocalEndIsConnected)
1094 theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef);
1096 theSocketStruct->mLocalEndIsConnected = false;
1100 else if (theSocketStruct->mLocalEndIsConnected)
1102 theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef);
1104 theSocketStruct->mLocalEndIsConnected = false;
1106 // Wait for other end to hang up too!
1108 // PrepareForAsyncOperation(theSocketStruct,T_ORDREL);
1110 // errCode = MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOrdRel));
1114 if (theOTResult != noErr)
1116 ::OTCloseProvider(theSocketStruct->mEndPointRef);
1121 theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef);
1124 theSocketStruct->mEndPointRef = kOTInvalidEndpointRef;
1126 errCode = theOTResult;
1130 theSocketStruct->mIsInUse = false;
1135 if (theSocketStruct != nil)
1137 theSocketStruct->mLastError = noErr;
1139 CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1141 if (errCode != noErr)
1143 theSocketStruct->mLastError = errCode;
1145 CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1157 // Receive some bytes
1159 int MacSocket_recv(const int inSocketNum,void *outBuff,int outBuffLength,const Boolean inBlock)
1161 OSErr errCode = noErr;
1162 int totalBytesRead = 0;
1163 SocketStruct *theSocketStruct = nil;
1166 SetErrorMessageAndBailIfNil(outBuff,"MacSocket_recv: Bad parameter, outBuff = nil");
1168 if (outBuffLength <= 0)
1170 SetErrorMessageAndBail("MacSocket_recv: Bad parameter, outBuffLength <= 0");
1173 if (!SocketIndexIsValid(inSocketNum))
1175 SetErrorMessageAndBail("MacSocket_recv: Invalid socket number specified");
1178 theSocketStruct = &(sSockets[inSocketNum]);
1180 if (!theSocketStruct->mLocalEndIsConnected)
1182 SetErrorMessageAndBail("MacSocket_recv: Socket not connected");
1185 if (theSocketStruct->mReceivedTOrdRel)
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)
1219 SetErrorMessageAndLongIntAndBail("MacSocket_recv: Can't receive OT data, OTRcv() = ",theOTResult);
1223 totalBytesRead += bytesRead;
1226 if (totalBytesRead <= 0)
1228 if (theSocketStruct->mReceivedTOrdRel)
1233 // This seems pretty stupid to me now. Maybe I'll delete this blocking garbage.
1237 if (TimeoutElapsed(theSocketStruct))
1239 SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_recv: Receive operation timed-out");
1242 if (theSocketStruct->mIdleWaitCallback != nil)
1244 theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr);
1246 SetErrorMessageAndBailIfError(theOTResult,"MacSocket_recv: User cancelled operation");
1260 #ifdef MACSOCKET_DEBUG
1261 printf("MacSocket_recv: read %d bytes in total\n",totalBytesRead);
1267 if (theSocketStruct != nil)
1269 theSocketStruct->mLastError = noErr;
1271 CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1273 if (errCode != noErr)
1275 theSocketStruct->mLastError = errCode;
1277 CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1283 return(totalBytesRead);
1290 int MacSocket_send(const int inSocketNum,const void *inBuff,int inBuffLength)
1292 OSErr errCode = noErr;
1294 SocketStruct *theSocketStruct = nil;
1297 SetErrorMessageAndBailIfNil(inBuff,"MacSocket_send: Bad parameter, inBuff = nil");
1299 if (inBuffLength <= 0)
1301 SetErrorMessageAndBail("MacSocket_send: Bad parameter, inBuffLength <= 0");
1304 if (!SocketIndexIsValid(inSocketNum))
1306 SetErrorMessageAndBail("MacSocket_send: Invalid socket number specified");
1310 theSocketStruct = &(sSockets[inSocketNum]);
1312 if (!theSocketStruct->mLocalEndIsConnected)
1314 SetErrorMessageAndBail("MacSocket_send: Socket not connected");
1318 OTResult theOTResult;
1321 PrepareForAsyncOperation(theSocketStruct,0);
1323 while (bytesSent < inBuffLength)
1325 if (theSocketStruct->mIdleWaitCallback != nil)
1327 theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr);
1329 SetErrorMessageAndBailIfError(theOTResult,"MacSocket_send: User cancelled");
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))
1350 SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_send: Send timed-out")
1353 theOTResult = noErr;
1356 SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_send: Can't send OT data, OTSnd() = ",theOTResult);
1362 #ifdef MACSOCKET_DEBUG
1363 printf("MacSocket_send: sent %d bytes\n",bytesSent);
1369 if (theSocketStruct != nil)
1371 theSocketStruct->mLastError = noErr;
1373 CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
1375 if (errCode != noErr)
1377 theSocketStruct->mLastError = errCode;
1379 CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
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))
1408 SetErrorMessageAndBail("NegotiateIPReuseAddrOption: Open Transport endpoint is not synchronous");
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;
1454 // Some rough notes....
1459 // OTAckSends(ep) // enable AckSend option
1461 // buf = OTAllocMem( nbytes); // Allocate nbytes of memory from OT
1462 // OTSnd(ep, buf, nbytes, 0); // send a packet
1464 // NotifyProc( .... void* theParam) // Notifier Proc
1465 // case T_MEMORYRELEASED: // process event
1466 // OTFreeMem( theParam); // free up memory
1472 struct InetInterfaceInfo
1476 InetHost fBroadcastAddr;
1477 InetHost fDefaultGatewayAddr;
1483 UInt8* fReservedPtrs[2];
1484 InetDomainName fDomainName;
1485 UInt32 fIPSecondaryCount;
1486 UInt8 fReserved[252];
1488 typedef struct InetInterfaceInfo InetInterfaceInfo;
1492 ((InetAddress *) addr.buf)->fHost
1500 typedef struct TBind TBind;
1509 typedef struct TNetbuf TNetbuf;
1514 OTAddressType fAddressType; // always AF_INET
1515 InetPort fPort; // Port number
1516 InetHost fHost; // Host address in net byte order
1517 UInt8 fUnused[8]; // Traditional unused bytes
1519 typedef struct InetAddress InetAddress;
1525 static pascal void Notifier(void* context, OTEventCode event, OTResult result, void* cookie)
1527 EPInfo* epi = (EPInfo*) context;
1537 case T_ACCEPTCOMPLETE:
1539 if (result != kOTNoError)
1540 DBAlert1("Notifier: T_ACCEPTCOMPLETE - result %d",result);
1546 if (result != kOTNoError)
1548 DBAlert1("Notifier: T_PASSCON result %d", result);
1552 OTAtomicAdd32(1, &gCntrConnections);
1553 OTAtomicAdd32(1, &gCntrTotalConnections);
1554 OTAtomicAdd32(1, &gCntrIntervalConnects);
1556 if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 )
1566 if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 )
1582 DoRcvDisconnect(epi);
1586 case T_DISCONNECTCOMPLETE:
1588 if (result != kOTNoError)
1589 DBAlert1("Notifier: T_DISCONNECT_COMPLETE result %d",result);
1594 case T_MEMORYRELEASED:
1596 OTAtomicAdd32(-1, &epi->outstandingSends);
1602 DBAlert1("Notifier: unknown event <%x>", event);