MacSocket.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /*
  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!
  5. *
  6. * Other funkiness includes the use of my own (possibly brain-damaged) error-handling infrastructure.
  7. *
  8. * -Roy Wood (roy@centricsystems.ca)
  9. *
  10. */
  11. /* ====================================================================
  12. * Copyright (c) 1998-1999 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * openssl-core@openssl.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include "MacSocket.h"
  65. #include <Threads.h>
  66. #include <OpenTransport.h>
  67. #include <OpenTpTInternet.h>
  68. #include <OpenTptClient.h>
  69. #include "CPStringUtils.hpp"
  70. #include "ErrorHandling.hpp"
  71. // #define MACSOCKET_DEBUG 1
  72. #ifdef MACSOCKET_DEBUG
  73. #include <stdio.h>
  74. #endif
  75. extern int errno;
  76. #define kMaxNumSockets 4
  77. struct SocketStruct
  78. {
  79. Boolean mIsInUse;
  80. Boolean mEndpointIsBound;
  81. Boolean mLocalEndIsConnected;
  82. Boolean mRemoteEndIsConnected;
  83. Boolean mReceivedTOpenComplete;
  84. Boolean mReceivedTBindComplete;
  85. Boolean mReceivedTConnect;
  86. Boolean mReceivedTListen;
  87. Boolean mReceivedTPassCon;
  88. Boolean mReceivedTDisconnect;
  89. Boolean mReceivedTOrdRel;
  90. Boolean mReceivedTDisconnectComplete;
  91. long mTimeoutTicks;
  92. long mOperationStartTicks;
  93. MacSocket_IdleWaitCallback mIdleWaitCallback;
  94. void *mUserRefPtr;
  95. OTEventCode mExpectedCode;
  96. OTResult mAsyncOperationResult;
  97. EndpointRef mEndPointRef;
  98. TBind *mBindRequestedAddrInfo;
  99. TBind *mAssignedAddrInfo;
  100. TCall *mRemoteAddrInfo;
  101. Boolean mReadyToReadData;
  102. Boolean mReadyToWriteData;
  103. Ptr mReadBuffer;
  104. Ptr mWriteBuffer;
  105. int mLastError;
  106. char mErrMessage[256];
  107. };
  108. typedef struct SocketStruct SocketStruct;
  109. static SocketStruct sSockets[kMaxNumSockets];
  110. static Boolean sSocketsSetup = false;
  111. static OSErr MyBusyWait(SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag);
  112. static pascal void OTNonYieldingNotifier(void *contextPtr,OTEventCode code,OTResult result,void *cookie);
  113. static Boolean SocketIndexIsValid(const int inSocketNum);
  114. static void InitSocket(SocketStruct *ioSocket);
  115. static void PrepareForAsyncOperation(SocketStruct *ioSocket,const OTEventCode inExpectedCode);
  116. static Boolean TimeoutElapsed(const SocketStruct *inSocket);
  117. static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,const Boolean inEnableReuseIP);
  118. void MacSocket_GetSocketErrorInfo(const int inSocketNum,int *outSocketErrCode,char *outSocketErrString,const int inSocketErrStringMaxLength)
  119. {
  120. if (outSocketErrCode != nil)
  121. {
  122. *outSocketErrCode = -1;
  123. }
  124. if (outSocketErrString != nil)
  125. {
  126. CopyCStrToCStr("",outSocketErrString,inSocketErrStringMaxLength);
  127. }
  128. if (SocketIndexIsValid(inSocketNum))
  129. {
  130. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  131. if (outSocketErrCode != nil)
  132. {
  133. *outSocketErrCode = theSocketStruct->mLastError;
  134. }
  135. if (outSocketErrString != nil)
  136. {
  137. CopyCStrToCStr(theSocketStruct->mErrMessage,outSocketErrString,inSocketErrStringMaxLength);
  138. }
  139. }
  140. }
  141. void MacSocket_SetUserRefPtr(const int inSocketNum,void *inNewRefPtr)
  142. {
  143. if (SocketIndexIsValid(inSocketNum))
  144. {
  145. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  146. theSocketStruct->mUserRefPtr = inNewRefPtr;
  147. }
  148. }
  149. void MacSocket_GetLocalIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength)
  150. {
  151. if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
  152. {
  153. char tempString[256];
  154. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  155. CopyCStrToCStr("",tempString,sizeof(tempString));
  156. if (theSocketStruct->mAssignedAddrInfo != nil)
  157. {
  158. InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mAssignedAddrInfo->addr.buf;
  159. InetHost theInetHost = theInetAddress->fHost;
  160. if (theInetHost == 0)
  161. {
  162. InetInterfaceInfo theInetInterfaceInfo;
  163. if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr)
  164. {
  165. theInetHost = theInetInterfaceInfo.fAddress;
  166. }
  167. }
  168. ::OTInetHostToString(theInetHost,tempString);
  169. ConcatCStrToCStr(":",tempString,sizeof(tempString));
  170. ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString));
  171. }
  172. CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength);
  173. }
  174. }
  175. void MacSocket_GetRemoteIPAndPort(const int inSocketNum,char *outIPAndPort,const int inIPAndPortLength)
  176. {
  177. if (outIPAndPort != nil && SocketIndexIsValid(inSocketNum))
  178. {
  179. char tempString[256];
  180. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  181. CopyCStrToCStr("",tempString,sizeof(tempString));
  182. if (theSocketStruct->mRemoteAddrInfo != nil)
  183. {
  184. InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mRemoteAddrInfo->addr.buf;
  185. InetHost theInetHost = theInetAddress->fHost;
  186. if (theInetHost == 0)
  187. {
  188. InetInterfaceInfo theInetInterfaceInfo;
  189. if (::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface) == noErr)
  190. {
  191. theInetHost = theInetInterfaceInfo.fAddress;
  192. }
  193. }
  194. ::OTInetHostToString(theInetHost,tempString);
  195. ConcatCStrToCStr(":",tempString,sizeof(tempString));
  196. ConcatLongIntToCStr(theInetAddress->fPort,tempString,sizeof(tempString));
  197. }
  198. CopyCStrToCStr(tempString,outIPAndPort,inIPAndPortLength);
  199. }
  200. }
  201. Boolean MacSocket_RemoteEndIsClosing(const int inSocketNum)
  202. {
  203. Boolean theResult = false;
  204. if (SocketIndexIsValid(inSocketNum))
  205. {
  206. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  207. theResult = theSocketStruct->mReceivedTOrdRel;
  208. }
  209. return(theResult);
  210. }
  211. Boolean MacSocket_ListenCompleted(const int inSocketNum)
  212. {
  213. Boolean theResult = false;
  214. if (SocketIndexIsValid(inSocketNum))
  215. {
  216. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  217. theResult = theSocketStruct->mReceivedTPassCon;
  218. }
  219. return(theResult);
  220. }
  221. Boolean MacSocket_RemoteEndIsOpen(const int inSocketNum)
  222. {
  223. if (SocketIndexIsValid(inSocketNum))
  224. {
  225. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  226. return(theSocketStruct->mRemoteEndIsConnected);
  227. }
  228. else
  229. {
  230. return(false);
  231. }
  232. }
  233. Boolean MacSocket_LocalEndIsOpen(const int inSocketNum)
  234. {
  235. if (SocketIndexIsValid(inSocketNum))
  236. {
  237. SocketStruct *theSocketStruct = &(sSockets[inSocketNum]);
  238. return(theSocketStruct->mLocalEndIsConnected);
  239. }
  240. else
  241. {
  242. return(false);
  243. }
  244. }
  245. static Boolean TimeoutElapsed(const SocketStruct *inSocket)
  246. {
  247. Boolean timeIsUp = false;
  248. if (inSocket != nil && inSocket->mTimeoutTicks > 0 && ::TickCount() > inSocket->mOperationStartTicks + inSocket->mTimeoutTicks)
  249. {
  250. timeIsUp = true;
  251. }
  252. return(timeIsUp);
  253. }
  254. static Boolean SocketIndexIsValid(const int inSocketNum)
  255. {
  256. if (inSocketNum >= 0 && inSocketNum < kMaxNumSockets && sSockets[inSocketNum].mEndPointRef != kOTInvalidEndpointRef)
  257. {
  258. return(true);
  259. }
  260. else
  261. {
  262. return(false);
  263. }
  264. }
  265. static void InitSocket(SocketStruct *ioSocket)
  266. {
  267. ioSocket->mIsInUse = false;
  268. ioSocket->mEndpointIsBound = false;
  269. ioSocket->mLocalEndIsConnected = false;
  270. ioSocket->mRemoteEndIsConnected = false;
  271. ioSocket->mReceivedTOpenComplete = false;
  272. ioSocket->mReceivedTBindComplete = false;
  273. ioSocket->mReceivedTConnect = false;
  274. ioSocket->mReceivedTListen = false;
  275. ioSocket->mReceivedTPassCon = false;
  276. ioSocket->mReceivedTDisconnect = false;
  277. ioSocket->mReceivedTOrdRel = false;
  278. ioSocket->mReceivedTDisconnectComplete = false;
  279. ioSocket->mTimeoutTicks = 30 * 60;
  280. ioSocket->mOperationStartTicks = -1;
  281. ioSocket->mIdleWaitCallback = nil;
  282. ioSocket->mUserRefPtr = nil;
  283. ioSocket->mExpectedCode = 0;
  284. ioSocket->mAsyncOperationResult = noErr;
  285. ioSocket->mEndPointRef = kOTInvalidEndpointRef;
  286. ioSocket->mBindRequestedAddrInfo = nil;
  287. ioSocket->mAssignedAddrInfo = nil;
  288. ioSocket->mRemoteAddrInfo = nil;
  289. ioSocket->mReadyToReadData = false;
  290. ioSocket->mReadyToWriteData = true;
  291. ioSocket->mReadBuffer = nil;
  292. ioSocket->mWriteBuffer = nil;
  293. ioSocket->mLastError = noErr;
  294. CopyCStrToCStr("",ioSocket->mErrMessage,sizeof(ioSocket->mErrMessage));
  295. }
  296. static void PrepareForAsyncOperation(SocketStruct *ioSocket,const OTEventCode inExpectedCode)
  297. {
  298. ioSocket->mOperationStartTicks = ::TickCount();
  299. ioSocket->mAsyncOperationResult = noErr;
  300. ioSocket->mExpectedCode = inExpectedCode;
  301. }
  302. // The wait function....
  303. static OSErr MyBusyWait(SocketStruct *ioSocket,Boolean returnImmediatelyOnError,OTResult *outOTResult,Boolean *inAsyncOperationCompleteFlag)
  304. {
  305. OSErr errCode = noErr;
  306. OTResult theOTResult = noErr;
  307. SetErrorMessageAndBailIfNil(ioSocket,"MyBusyWait: Bad parameter, ioSocket = nil");
  308. SetErrorMessageAndBailIfNil(inAsyncOperationCompleteFlag,"MyBusyWait: Bad parameter, inAsyncOperationCompleteFlag = nil");
  309. for (;;)
  310. {
  311. if (*inAsyncOperationCompleteFlag)
  312. {
  313. theOTResult = ioSocket->mAsyncOperationResult;
  314. break;
  315. }
  316. if (ioSocket->mIdleWaitCallback != nil)
  317. {
  318. theOTResult = (*(ioSocket->mIdleWaitCallback))(ioSocket->mUserRefPtr);
  319. if (theOTResult != noErr && returnImmediatelyOnError)
  320. {
  321. break;
  322. }
  323. }
  324. if (TimeoutElapsed(ioSocket))
  325. {
  326. theOTResult = kMacSocket_TimeoutErr;
  327. break;
  328. }
  329. }
  330. EXITPOINT:
  331. if (outOTResult != nil)
  332. {
  333. *outOTResult = theOTResult;
  334. }
  335. return(errCode);
  336. }
  337. // I used to do thread switching, but stopped. It could easily be rolled back in though....
  338. static pascal void OTNonYieldingNotifier(void *contextPtr,OTEventCode code,OTResult result,void *cookie)
  339. {
  340. SocketStruct *theSocketStruct = (SocketStruct *) contextPtr;
  341. if (theSocketStruct != nil)
  342. {
  343. if (theSocketStruct->mExpectedCode != 0 && code == theSocketStruct->mExpectedCode)
  344. {
  345. theSocketStruct->mAsyncOperationResult = result;
  346. theSocketStruct->mExpectedCode = 0;
  347. }
  348. switch (code)
  349. {
  350. case T_OPENCOMPLETE:
  351. {
  352. theSocketStruct->mReceivedTOpenComplete = true;
  353. theSocketStruct->mEndPointRef = (EndpointRef) cookie;
  354. break;
  355. }
  356. case T_BINDCOMPLETE:
  357. {
  358. theSocketStruct->mReceivedTBindComplete = true;
  359. break;
  360. }
  361. case T_CONNECT:
  362. {
  363. theSocketStruct->mReceivedTConnect = true;
  364. theSocketStruct->mLocalEndIsConnected = true;
  365. theSocketStruct->mRemoteEndIsConnected = true;
  366. break;
  367. }
  368. case T_LISTEN:
  369. {
  370. theSocketStruct->mReceivedTListen = true;
  371. break;
  372. }
  373. case T_PASSCON:
  374. {
  375. theSocketStruct->mReceivedTPassCon = true;
  376. theSocketStruct->mLocalEndIsConnected = true;
  377. theSocketStruct->mRemoteEndIsConnected = true;
  378. break;
  379. }
  380. case T_DATA:
  381. {
  382. theSocketStruct->mReadyToReadData = true;
  383. break;
  384. }
  385. case T_GODATA:
  386. {
  387. theSocketStruct->mReadyToWriteData = true;
  388. break;
  389. }
  390. case T_DISCONNECT:
  391. {
  392. theSocketStruct->mReceivedTDisconnect = true;
  393. theSocketStruct->mRemoteEndIsConnected = false;
  394. theSocketStruct->mLocalEndIsConnected = false;
  395. ::OTRcvDisconnect(theSocketStruct->mEndPointRef,nil);
  396. break;
  397. }
  398. case T_ORDREL:
  399. {
  400. theSocketStruct->mReceivedTOrdRel = true;
  401. // We can still write data, so don't clear mRemoteEndIsConnected
  402. ::OTRcvOrderlyDisconnect(theSocketStruct->mEndPointRef);
  403. break;
  404. }
  405. case T_DISCONNECTCOMPLETE:
  406. {
  407. theSocketStruct->mReceivedTDisconnectComplete = true;
  408. theSocketStruct->mRemoteEndIsConnected = false;
  409. theSocketStruct->mLocalEndIsConnected = false;
  410. break;
  411. }
  412. }
  413. }
  414. /*
  415. T_LISTEN OTListen
  416. T_CONNECT OTRcvConnect
  417. T_DATA OTRcv, OTRcvUData
  418. T_DISCONNECT OTRcvDisconnect
  419. T_ORDREL OTRcvOrderlyDisconnect
  420. T_GODATA OTSnd, OTSndUData, OTLook
  421. T_PASSCON none
  422. T_EXDATA OTRcv
  423. T_GOEXDATA OTSnd, OTLook
  424. T_UDERR OTRcvUDErr
  425. */
  426. }
  427. // Initialize the main socket data structure
  428. OSErr MacSocket_Startup(void)
  429. {
  430. if (!sSocketsSetup)
  431. {
  432. for (int i = 0;i < kMaxNumSockets;i++)
  433. {
  434. InitSocket(&(sSockets[i]));
  435. }
  436. ::InitOpenTransport();
  437. sSocketsSetup = true;
  438. }
  439. return(noErr);
  440. }
  441. // Cleanup before exiting
  442. OSErr MacSocket_Shutdown(void)
  443. {
  444. if (sSocketsSetup)
  445. {
  446. for (int i = 0;i < kMaxNumSockets;i++)
  447. {
  448. SocketStruct *theSocketStruct = &(sSockets[i]);
  449. if (theSocketStruct->mIsInUse)
  450. {
  451. if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef)
  452. {
  453. OTResult theOTResult;
  454. // Since we're killing the endpoint, I don't bother to send the disconnect (sorry!)
  455. /*
  456. if (theSocketStruct->mLocalEndIsConnected)
  457. {
  458. // This is an abortive action, so we do a hard disconnect instead of an OTSndOrderlyDisconnect
  459. theOTResult = ::OTSndDisconnect(theSocketStruct->mEndPointRef, nil);
  460. // Now we have to watch for T_DISCONNECTCOMPLETE event
  461. theSocketStruct->mLocalEndIsConnected = false;
  462. }
  463. */
  464. theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef);
  465. theSocketStruct->mEndPointRef = kOTInvalidEndpointRef;
  466. }
  467. if (theSocketStruct->mBindRequestedAddrInfo != nil)
  468. {
  469. ::OTFree((void *) theSocketStruct->mBindRequestedAddrInfo,T_BIND);
  470. theSocketStruct->mBindRequestedAddrInfo = nil;
  471. }
  472. if (theSocketStruct->mAssignedAddrInfo != nil)
  473. {
  474. ::OTFree((void *) theSocketStruct->mAssignedAddrInfo,T_BIND);
  475. theSocketStruct->mAssignedAddrInfo = nil;
  476. }
  477. if (theSocketStruct->mRemoteAddrInfo != nil)
  478. {
  479. ::OTFree((void *) theSocketStruct->mRemoteAddrInfo,T_CALL);
  480. theSocketStruct->mRemoteAddrInfo = nil;
  481. }
  482. }
  483. }
  484. ::CloseOpenTransport();
  485. sSocketsSetup = false;
  486. }
  487. return(noErr);
  488. }
  489. // Allocate a socket
  490. OSErr MacSocket_socket(int *outSocketNum,const Boolean inDoThreadSwitching,const long inTimeoutTicks,MacSocket_IdleWaitCallback inIdleWaitCallback,void *inUserRefPtr)
  491. {
  492. // Gotta roll support back in for threads eventually.....
  493. #pragma unused(inDoThreadSwitching)
  494. OSErr errCode = noErr;
  495. SetErrorMessageAndBailIfNil(outSocketNum,"MacSocket_socket: Bad parameter, outSocketNum == nil");
  496. *outSocketNum = -1;
  497. // Find an unused socket
  498. for (int i = 0;i < kMaxNumSockets;i++)
  499. {
  500. if (sSockets[i].mIsInUse == false)
  501. {
  502. OTResult theOTResult;
  503. SocketStruct *theSocketStruct = &(sSockets[i]);
  504. InitSocket(theSocketStruct);
  505. theSocketStruct->mIdleWaitCallback = inIdleWaitCallback;
  506. theSocketStruct->mUserRefPtr = inUserRefPtr;
  507. theSocketStruct->mTimeoutTicks = inTimeoutTicks;
  508. // Set up OT endpoint
  509. PrepareForAsyncOperation(theSocketStruct,T_OPENCOMPLETE);
  510. theOTResult = ::OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName),0,nil,OTNonYieldingNotifier,(void *) theSocketStruct);
  511. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult);
  512. BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOpenComplete)));
  513. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_socket: Can't create OT endpoint, OTAsyncOpenEndpoint() = ",theOTResult);
  514. *outSocketNum = i;
  515. errCode = noErr;
  516. theSocketStruct->mIsInUse = true;
  517. break;
  518. }
  519. else if (i == kMaxNumSockets - 1)
  520. {
  521. SetErrorMessageAndBail("MacSocket_socket: No sockets available");
  522. }
  523. }
  524. EXITPOINT:
  525. errno = errCode;
  526. return(errCode);
  527. }
  528. OSErr MacSocket_listen(const int inSocketNum,const int inPortNum)
  529. {
  530. OSErr errCode = noErr;
  531. SocketStruct *theSocketStruct = nil;
  532. if (!SocketIndexIsValid(inSocketNum))
  533. {
  534. SetErrorMessageAndBail("MacSocket_listen: Invalid socket number specified");
  535. }
  536. theSocketStruct = &(sSockets[inSocketNum]);
  537. OTResult theOTResult;
  538. if (theSocketStruct->mBindRequestedAddrInfo == nil)
  539. {
  540. theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
  541. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
  542. SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
  543. }
  544. if (theSocketStruct->mAssignedAddrInfo == nil)
  545. {
  546. theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
  547. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
  548. SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_listen: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
  549. }
  550. if (theSocketStruct->mRemoteAddrInfo == nil)
  551. {
  552. theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult);
  553. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult);
  554. SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_listen: Can't allocate OT T_CALL structure, OTAlloc() returned nil");
  555. }
  556. if (!theSocketStruct->mEndpointIsBound)
  557. {
  558. InetInterfaceInfo theInetInterfaceInfo;
  559. theOTResult = ::OTInetGetInterfaceInfo(&theInetInterfaceInfo,kDefaultInetInterface);
  560. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't determine OT interface info, OTInetGetInterfaceInfo() = ",theOTResult);
  561. InetAddress *theInetAddress = (InetAddress *) theSocketStruct->mBindRequestedAddrInfo->addr.buf;
  562. // theInetAddress->fAddressType = AF_INET;
  563. // theInetAddress->fPort = inPortNum;
  564. // theInetAddress->fHost = theInetInterfaceInfo.fAddress;
  565. ::OTInitInetAddress(theInetAddress,inPortNum,theInetInterfaceInfo.fAddress);
  566. theSocketStruct->mBindRequestedAddrInfo->addr.len = sizeof(InetAddress);
  567. theSocketStruct->mBindRequestedAddrInfo->qlen = 1;
  568. theOTResult = ::OTSetSynchronous(theSocketStruct->mEndPointRef);
  569. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetSynchronous() = ",theOTResult);
  570. theOTResult = NegotiateIPReuseAddrOption(theSocketStruct->mEndPointRef,true);
  571. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT IP address reuse flag, NegotiateIPReuseAddrOption() = ",theOTResult);
  572. theOTResult = ::OTSetAsynchronous(theSocketStruct->mEndPointRef);
  573. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't set OT endpoint mode, OTSetAsynchronous() = ",theOTResult);
  574. PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
  575. theOTResult = ::OTBind(theSocketStruct->mEndPointRef,theSocketStruct->mBindRequestedAddrInfo,theSocketStruct->mAssignedAddrInfo);
  576. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult);
  577. BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete)));
  578. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't bind OT endpoint, OTBind() = ",theOTResult);
  579. theSocketStruct->mEndpointIsBound = true;
  580. }
  581. PrepareForAsyncOperation(theSocketStruct,T_LISTEN);
  582. theOTResult = ::OTListen(theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo);
  583. if (theOTResult == noErr)
  584. {
  585. PrepareForAsyncOperation(theSocketStruct,T_PASSCON);
  586. theOTResult = ::OTAccept(theSocketStruct->mEndPointRef,theSocketStruct->mEndPointRef,theSocketStruct->mRemoteAddrInfo);
  587. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't begin OT accept, OTAccept() = ",theOTResult);
  588. BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTPassCon)));
  589. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_listen: Can't accept OT connection, OTAccept() = ",theOTResult);
  590. }
  591. else if (theOTResult == kOTNoDataErr)
  592. {
  593. theOTResult = noErr;
  594. }
  595. else
  596. {
  597. SetErrorMessageAndLongIntAndBail("MacSocket_listen: Can't begin OT listen, OTListen() = ",theOTResult);
  598. }
  599. errCode = noErr;
  600. EXITPOINT:
  601. if (theSocketStruct != nil)
  602. {
  603. theSocketStruct->mLastError = noErr;
  604. CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  605. if (errCode != noErr)
  606. {
  607. theSocketStruct->mLastError = errCode;
  608. CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  609. }
  610. }
  611. errno = errCode;
  612. return(errCode);
  613. }
  614. OSErr MacSocket_connect(const int inSocketNum,char *inTargetAddressAndPort)
  615. {
  616. OSErr errCode = noErr;
  617. SocketStruct *theSocketStruct = nil;
  618. if (!SocketIndexIsValid(inSocketNum))
  619. {
  620. SetErrorMessageAndBail("MacSocket_connect: Invalid socket number specified");
  621. }
  622. theSocketStruct = &(sSockets[inSocketNum]);
  623. if (theSocketStruct->mEndpointIsBound)
  624. {
  625. SetErrorMessageAndBail("MacSocket_connect: Socket previously bound");
  626. }
  627. OTResult theOTResult;
  628. theSocketStruct->mBindRequestedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
  629. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
  630. SetErrorMessageAndBailIfNil(theSocketStruct->mBindRequestedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
  631. theSocketStruct->mAssignedAddrInfo = (TBind *) ::OTAlloc(theSocketStruct->mEndPointRef,T_BIND,T_ADDR,&theOTResult);
  632. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() = ",theOTResult);
  633. SetErrorMessageAndBailIfNil(theSocketStruct->mAssignedAddrInfo,"MacSocket_connect: Can't allocate OT T_BIND structure, OTAlloc() returned nil");
  634. theSocketStruct->mRemoteAddrInfo = (TCall *) ::OTAlloc(theSocketStruct->mEndPointRef,T_CALL,T_ADDR,&theOTResult);
  635. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() = ",theOTResult);
  636. SetErrorMessageAndBailIfNil(theSocketStruct->mRemoteAddrInfo,"MacSocket_connect: Can't allocate OT T_CALL structure, OTAlloc() returned nil");
  637. PrepareForAsyncOperation(theSocketStruct,T_BINDCOMPLETE);
  638. theOTResult = ::OTBind(theSocketStruct->mEndPointRef,nil,theSocketStruct->mAssignedAddrInfo);
  639. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult);
  640. BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTBindComplete)));
  641. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't bind OT endpoint, OTBind() = ",theOTResult);
  642. theSocketStruct->mEndpointIsBound = true;
  643. TCall sndCall;
  644. DNSAddress hostDNSAddress;
  645. // Set up target address
  646. sndCall.addr.buf = (UInt8 *) &hostDNSAddress;
  647. sndCall.addr.len = ::OTInitDNSAddress(&hostDNSAddress,inTargetAddressAndPort);
  648. sndCall.opt.buf = nil;
  649. sndCall.opt.len = 0;
  650. sndCall.udata.buf = nil;
  651. sndCall.udata.len = 0;
  652. sndCall.sequence = 0;
  653. // Connect!
  654. PrepareForAsyncOperation(theSocketStruct,T_CONNECT);
  655. theOTResult = ::OTConnect(theSocketStruct->mEndPointRef,&sndCall,nil);
  656. if (theOTResult == kOTNoDataErr)
  657. {
  658. theOTResult = noErr;
  659. }
  660. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult);
  661. BailIfError(MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTConnect)));
  662. if (theOTResult == kMacSocket_TimeoutErr)
  663. {
  664. SetErrorMessageAndBail("MacSocket_connect: Can't connect OT endpoint, OTConnect() = kMacSocket_TimeoutErr");
  665. }
  666. else
  667. {
  668. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't connect OT endpoint, OTConnect() = ",theOTResult);
  669. }
  670. theOTResult = ::OTRcvConnect(theSocketStruct->mEndPointRef,nil);
  671. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_connect: Can't complete connect on OT endpoint, OTRcvConnect() = ",theOTResult);
  672. errCode = noErr;
  673. #ifdef MACSOCKET_DEBUG
  674. printf("MacSocket_connect: connect completed\n");
  675. #endif
  676. EXITPOINT:
  677. if (theSocketStruct != nil)
  678. {
  679. theSocketStruct->mLastError = noErr;
  680. CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  681. if (errCode != noErr)
  682. {
  683. theSocketStruct->mLastError = errCode;
  684. CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  685. }
  686. }
  687. errno = errCode;
  688. return(errCode);
  689. }
  690. // Close a connection
  691. OSErr MacSocket_close(const int inSocketNum)
  692. {
  693. OSErr errCode = noErr;
  694. SocketStruct *theSocketStruct = nil;
  695. if (!SocketIndexIsValid(inSocketNum))
  696. {
  697. SetErrorMessageAndBail("MacSocket_close: Invalid socket number specified");
  698. }
  699. theSocketStruct = &(sSockets[inSocketNum]);
  700. if (theSocketStruct->mEndPointRef != kOTInvalidEndpointRef)
  701. {
  702. OTResult theOTResult = noErr;
  703. // Try to play nice
  704. if (theSocketStruct->mReceivedTOrdRel)
  705. {
  706. // Already did an OTRcvOrderlyDisconnect() in the notifier
  707. if (theSocketStruct->mLocalEndIsConnected)
  708. {
  709. theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef);
  710. theSocketStruct->mLocalEndIsConnected = false;
  711. }
  712. }
  713. else if (theSocketStruct->mLocalEndIsConnected)
  714. {
  715. theOTResult = ::OTSndOrderlyDisconnect(theSocketStruct->mEndPointRef);
  716. theSocketStruct->mLocalEndIsConnected = false;
  717. // Wait for other end to hang up too!
  718. // PrepareForAsyncOperation(theSocketStruct,T_ORDREL);
  719. //
  720. // errCode = MyBusyWait(theSocketStruct,false,&theOTResult,&(theSocketStruct->mReceivedTOrdRel));
  721. }
  722. if (theOTResult != noErr)
  723. {
  724. ::OTCloseProvider(theSocketStruct->mEndPointRef);
  725. }
  726. else
  727. {
  728. theOTResult = ::OTCloseProvider(theSocketStruct->mEndPointRef);
  729. }
  730. theSocketStruct->mEndPointRef = kOTInvalidEndpointRef;
  731. errCode = theOTResult;
  732. }
  733. theSocketStruct->mIsInUse = false;
  734. EXITPOINT:
  735. if (theSocketStruct != nil)
  736. {
  737. theSocketStruct->mLastError = noErr;
  738. CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  739. if (errCode != noErr)
  740. {
  741. theSocketStruct->mLastError = errCode;
  742. CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  743. }
  744. }
  745. errno = errCode;
  746. return(errCode);
  747. }
  748. // Receive some bytes
  749. int MacSocket_recv(const int inSocketNum,void *outBuff,int outBuffLength,const Boolean inBlock)
  750. {
  751. OSErr errCode = noErr;
  752. int totalBytesRead = 0;
  753. SocketStruct *theSocketStruct = nil;
  754. SetErrorMessageAndBailIfNil(outBuff,"MacSocket_recv: Bad parameter, outBuff = nil");
  755. if (outBuffLength <= 0)
  756. {
  757. SetErrorMessageAndBail("MacSocket_recv: Bad parameter, outBuffLength <= 0");
  758. }
  759. if (!SocketIndexIsValid(inSocketNum))
  760. {
  761. SetErrorMessageAndBail("MacSocket_recv: Invalid socket number specified");
  762. }
  763. theSocketStruct = &(sSockets[inSocketNum]);
  764. if (!theSocketStruct->mLocalEndIsConnected)
  765. {
  766. SetErrorMessageAndBail("MacSocket_recv: Socket not connected");
  767. }
  768. if (theSocketStruct->mReceivedTOrdRel)
  769. {
  770. totalBytesRead = 0;
  771. goto EXITPOINT;
  772. }
  773. PrepareForAsyncOperation(theSocketStruct,0);
  774. for (;;)
  775. {
  776. int bytesRead;
  777. OTResult theOTResult;
  778. theOTResult = ::OTRcv(theSocketStruct->mEndPointRef,(void *) ((unsigned long) outBuff + (unsigned long) totalBytesRead),outBuffLength - totalBytesRead,nil);
  779. if (theOTResult >= 0)
  780. {
  781. bytesRead = theOTResult;
  782. #ifdef MACSOCKET_DEBUG
  783. printf("MacSocket_recv: read %d bytes in part\n",bytesRead);
  784. #endif
  785. }
  786. else if (theOTResult == kOTNoDataErr)
  787. {
  788. bytesRead = 0;
  789. }
  790. else
  791. {
  792. SetErrorMessageAndLongIntAndBail("MacSocket_recv: Can't receive OT data, OTRcv() = ",theOTResult);
  793. }
  794. totalBytesRead += bytesRead;
  795. if (totalBytesRead <= 0)
  796. {
  797. if (theSocketStruct->mReceivedTOrdRel)
  798. {
  799. break;
  800. }
  801. // This seems pretty stupid to me now. Maybe I'll delete this blocking garbage.
  802. if (inBlock)
  803. {
  804. if (TimeoutElapsed(theSocketStruct))
  805. {
  806. SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_recv: Receive operation timed-out");
  807. }
  808. if (theSocketStruct->mIdleWaitCallback != nil)
  809. {
  810. theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr);
  811. SetErrorMessageAndBailIfError(theOTResult,"MacSocket_recv: User cancelled operation");
  812. }
  813. continue;
  814. }
  815. }
  816. break;
  817. }
  818. errCode = noErr;
  819. #ifdef MACSOCKET_DEBUG
  820. printf("MacSocket_recv: read %d bytes in total\n",totalBytesRead);
  821. #endif
  822. EXITPOINT:
  823. if (theSocketStruct != nil)
  824. {
  825. theSocketStruct->mLastError = noErr;
  826. CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  827. if (errCode != noErr)
  828. {
  829. theSocketStruct->mLastError = errCode;
  830. CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  831. }
  832. }
  833. errno = errCode;
  834. return(totalBytesRead);
  835. }
  836. // Send some bytes
  837. int MacSocket_send(const int inSocketNum,const void *inBuff,int inBuffLength)
  838. {
  839. OSErr errCode = noErr;
  840. int bytesSent = 0;
  841. SocketStruct *theSocketStruct = nil;
  842. SetErrorMessageAndBailIfNil(inBuff,"MacSocket_send: Bad parameter, inBuff = nil");
  843. if (inBuffLength <= 0)
  844. {
  845. SetErrorMessageAndBail("MacSocket_send: Bad parameter, inBuffLength <= 0");
  846. }
  847. if (!SocketIndexIsValid(inSocketNum))
  848. {
  849. SetErrorMessageAndBail("MacSocket_send: Invalid socket number specified");
  850. }
  851. theSocketStruct = &(sSockets[inSocketNum]);
  852. if (!theSocketStruct->mLocalEndIsConnected)
  853. {
  854. SetErrorMessageAndBail("MacSocket_send: Socket not connected");
  855. }
  856. OTResult theOTResult;
  857. PrepareForAsyncOperation(theSocketStruct,0);
  858. while (bytesSent < inBuffLength)
  859. {
  860. if (theSocketStruct->mIdleWaitCallback != nil)
  861. {
  862. theOTResult = (*(theSocketStruct->mIdleWaitCallback))(theSocketStruct->mUserRefPtr);
  863. SetErrorMessageAndBailIfError(theOTResult,"MacSocket_send: User cancelled");
  864. }
  865. theOTResult = ::OTSnd(theSocketStruct->mEndPointRef,(void *) ((unsigned long) inBuff + bytesSent),inBuffLength - bytesSent,0);
  866. if (theOTResult >= 0)
  867. {
  868. bytesSent += theOTResult;
  869. theOTResult = noErr;
  870. // Reset timer....
  871. PrepareForAsyncOperation(theSocketStruct,0);
  872. }
  873. if (theOTResult == kOTFlowErr)
  874. {
  875. if (TimeoutElapsed(theSocketStruct))
  876. {
  877. SetErrorCodeAndMessageAndBail(kMacSocket_TimeoutErr,"MacSocket_send: Send timed-out")
  878. }
  879. theOTResult = noErr;
  880. }
  881. SetErrorMessageAndLongIntAndBailIfError(theOTResult,"MacSocket_send: Can't send OT data, OTSnd() = ",theOTResult);
  882. }
  883. errCode = noErr;
  884. #ifdef MACSOCKET_DEBUG
  885. printf("MacSocket_send: sent %d bytes\n",bytesSent);
  886. #endif
  887. EXITPOINT:
  888. if (theSocketStruct != nil)
  889. {
  890. theSocketStruct->mLastError = noErr;
  891. CopyCStrToCStr("",theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  892. if (errCode != noErr)
  893. {
  894. theSocketStruct->mLastError = errCode;
  895. CopyCStrToCStr(GetErrorMessage(),theSocketStruct->mErrMessage,sizeof(theSocketStruct->mErrMessage));
  896. }
  897. }
  898. if (errCode != noErr)
  899. {
  900. ::SysBeep(1);
  901. }
  902. errno = errCode;
  903. return(bytesSent);
  904. }
  905. static OSStatus NegotiateIPReuseAddrOption(EndpointRef inEndpoint,const Boolean inEnableReuseIP)
  906. {
  907. OSStatus errCode;
  908. UInt8 buf[kOTFourByteOptionSize];
  909. TOption* theOTOption;
  910. TOptMgmt theOTRequest;
  911. TOptMgmt theOTResult;
  912. if (!OTIsSynchronous(inEndpoint))
  913. {
  914. SetErrorMessageAndBail("NegotiateIPReuseAddrOption: Open Transport endpoint is not synchronous");
  915. }
  916. theOTRequest.opt.buf = buf;
  917. theOTRequest.opt.len = sizeof(buf);
  918. theOTRequest.flags = T_NEGOTIATE;
  919. theOTResult.opt.buf = buf;
  920. theOTResult.opt.maxlen = kOTFourByteOptionSize;
  921. theOTOption = (TOption *) buf;
  922. theOTOption->level = INET_IP;
  923. theOTOption->name = IP_REUSEADDR;
  924. theOTOption->len = kOTFourByteOptionSize;
  925. theOTOption->status = 0;
  926. *((UInt32 *) (theOTOption->value)) = inEnableReuseIP;
  927. errCode = ::OTOptionManagement(inEndpoint,&theOTRequest,&theOTResult);
  928. if (errCode == kOTNoError)
  929. {
  930. if (theOTOption->status != T_SUCCESS)
  931. {
  932. errCode = theOTOption->status;
  933. }
  934. else
  935. {
  936. errCode = kOTNoError;
  937. }
  938. }
  939. EXITPOINT:
  940. errno = errCode;
  941. return(errCode);
  942. }
  943. // Some rough notes....
  944. // OTAckSends(ep);
  945. // OTAckSends(ep) // enable AckSend option
  946. // ......
  947. // buf = OTAllocMem( nbytes); // Allocate nbytes of memory from OT
  948. // OTSnd(ep, buf, nbytes, 0); // send a packet
  949. // ......
  950. // NotifyProc( .... void* theParam) // Notifier Proc
  951. // case T_MEMORYRELEASED: // process event
  952. // OTFreeMem( theParam); // free up memory
  953. // break;
  954. /*
  955. struct InetInterfaceInfo
  956. {
  957. InetHost fAddress;
  958. InetHost fNetmask;
  959. InetHost fBroadcastAddr;
  960. InetHost fDefaultGatewayAddr;
  961. InetHost fDNSAddr;
  962. UInt16 fVersion;
  963. UInt16 fHWAddrLen;
  964. UInt8* fHWAddr;
  965. UInt32 fIfMTU;
  966. UInt8* fReservedPtrs[2];
  967. InetDomainName fDomainName;
  968. UInt32 fIPSecondaryCount;
  969. UInt8 fReserved[252];
  970. };
  971. typedef struct InetInterfaceInfo InetInterfaceInfo;
  972. ((InetAddress *) addr.buf)->fHost
  973. struct TBind
  974. {
  975. TNetbuf addr;
  976. OTQLen qlen;
  977. };
  978. typedef struct TBind TBind;
  979. struct TNetbuf
  980. {
  981. size_t maxlen;
  982. size_t len;
  983. UInt8* buf;
  984. };
  985. typedef struct TNetbuf TNetbuf;
  986. struct InetAddress
  987. {
  988. OTAddressType fAddressType; // always AF_INET
  989. InetPort fPort; // Port number
  990. InetHost fHost; // Host address in net byte order
  991. UInt8 fUnused[8]; // Traditional unused bytes
  992. };
  993. typedef struct InetAddress InetAddress;
  994. */
  995. /*
  996. static pascal void Notifier(void* context, OTEventCode event, OTResult result, void* cookie)
  997. {
  998. EPInfo* epi = (EPInfo*) context;
  999. switch (event)
  1000. {
  1001. case T_LISTEN:
  1002. {
  1003. DoListenAccept();
  1004. return;
  1005. }
  1006. case T_ACCEPTCOMPLETE:
  1007. {
  1008. if (result != kOTNoError)
  1009. DBAlert1("Notifier: T_ACCEPTCOMPLETE - result %d",result);
  1010. return;
  1011. }
  1012. case T_PASSCON:
  1013. {
  1014. if (result != kOTNoError)
  1015. {
  1016. DBAlert1("Notifier: T_PASSCON result %d", result);
  1017. return;
  1018. }
  1019. OTAtomicAdd32(1, &gCntrConnections);
  1020. OTAtomicAdd32(1, &gCntrTotalConnections);
  1021. OTAtomicAdd32(1, &gCntrIntervalConnects);
  1022. if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 )
  1023. {
  1024. ReadData(epi);
  1025. }
  1026. return;
  1027. }
  1028. case T_DATA:
  1029. {
  1030. if ( OTAtomicSetBit(&epi->stateFlags, kPassconBit) != 0 )
  1031. {
  1032. ReadData(epi);
  1033. }
  1034. return;
  1035. }
  1036. case T_GODATA:
  1037. {
  1038. SendData(epi);
  1039. return;
  1040. }
  1041. case T_DISCONNECT:
  1042. {
  1043. DoRcvDisconnect(epi);
  1044. return;
  1045. }
  1046. case T_DISCONNECTCOMPLETE:
  1047. {
  1048. if (result != kOTNoError)
  1049. DBAlert1("Notifier: T_DISCONNECT_COMPLETE result %d",result);
  1050. return;
  1051. }
  1052. case T_MEMORYRELEASED:
  1053. {
  1054. OTAtomicAdd32(-1, &epi->outstandingSends);
  1055. return;
  1056. }
  1057. default:
  1058. {
  1059. DBAlert1("Notifier: unknown event <%x>", event);
  1060. return;
  1061. }
  1062. }
  1063. }
  1064. */