Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2896 lines
72 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. dgtrans.cxx
  5. Abstract:
  6. Common code for winsock-based datagram transports.
  7. Author:
  8. Dave Steckler (davidst) 15-Mar-1993
  9. Jeff Roberts (jroberts) 02-Dec-1994
  10. Mario Goertzel (mariogo) 10-Apr-1996
  11. Michael Burton (t-mburt) 05-Sep-1997
  12. Charlie Wickham (charlwi) 01-Oct-1997
  13. Revision History:
  14. Dave wrote a version.
  15. Connie changed it but forgot to add her name.
  16. Jeff made it work.
  17. Mario rewrote most of it for NT and io completion ports.
  18. MarioGo 12/10/1996 Changes for async support, added client
  19. t-mburt 09/05/1997 Added prelim support for clusters
  20. charlwi 10/01/1997 Finished cluster work
  21. --*/
  22. #include <precomp.hxx>
  23. #include <trans.hxx>
  24. #include <dgtrans.hxx>
  25. #include <wswrap.hxx>
  26. #ifdef NCADG_MQ_ON
  27. #include "mqtrans.hxx"
  28. #endif
  29. //
  30. // If a datagram send doesn't complete within 5 seconds, abort it.
  31. //
  32. #define DG_SEND_TIMEOUT (5000)
  33. // Cluster SOCKADDR_CLUSTER initialization routine
  34. inline void
  35. CDP_InitLocalAddress(
  36. SOCKADDR_CLUSTER *Address,
  37. unsigned short Endpoint
  38. )
  39. {
  40. Address->sac_family = AF_CLUSTER;
  41. Address->sac_node = CLUSADDR_ANY;
  42. Address->sac_port = Endpoint;
  43. Address->sac_zero = 0;
  44. }
  45. extern RPC_STATUS CDP_InitializeSockAddr(char *Endpoint, WS_SOCKADDR *);
  46. extern RPC_STATUS UDP_InitializeSockAddr(char *Endpoint, WS_SOCKADDR *);
  47. #ifdef IPX_ON
  48. extern RPC_STATUS IPX_InitializeSockAddr(char *Endpoint, WS_SOCKADDR *);
  49. #endif
  50. const DG_TRANS_INFO DgTransportTable[] =
  51. {
  52. // UDP
  53. {
  54. AF_INET,
  55. SOCK_DGRAM,
  56. IPPROTO_UDP,
  57. 0x40000,
  58. 0x10000,
  59. UDP_InitializeSockAddr
  60. },
  61. #ifdef IPX_ON
  62. // IPX
  63. {
  64. AF_IPX,
  65. SOCK_DGRAM,
  66. NSPROTO_IPX,
  67. 0x40000,
  68. 0x10000,
  69. IPX_InitializeSockAddr
  70. },
  71. #else
  72. // IPX
  73. {
  74. 0,
  75. 0,
  76. 0,
  77. 0,
  78. 0,
  79. 0
  80. },
  81. #endif
  82. // CDP
  83. {
  84. AF_CLUSTER,
  85. SOCK_DGRAM,
  86. CLUSPROTO_CDP,
  87. 0x40000,
  88. 0x10000,
  89. CDP_InitializeSockAddr
  90. }
  91. };
  92. inline const DG_TRANS_INFO *GetDgTransportInfo(PROTOCOL_ID id)
  93. {
  94. #ifdef IPX_ON
  95. ASSERT(id == UDP || id == IPX || id == CDP);
  96. #else
  97. ASSERT(id == UDP || id == CDP);
  98. #endif
  99. return &DgTransportTable[id - UDP];
  100. }
  101. typedef const DG_TRANS_INFO *PDG_TRANS_INFO;
  102. // may be TRUE only for rpcss. For all others it's FALSE
  103. BOOL fWSARecvMsgFnPtrInitialized = FALSE;
  104. const UUID WSARecvMsgFnPtrUuid = WSAID_WSARECVMSG;
  105. ////////////////////////////////////////////////////////////////////////
  106. //
  107. // Generic datagram (winsock and NT based) routines.
  108. //
  109. RPC_STATUS
  110. DG_SubmitReceive(IN PWS_DATAGRAM_ENDPOINT pEndpoint,
  111. IN PWS_DATAGRAM pDatagram)
  112. /*++
  113. Arguments:
  114. pEndpoint - The endpoint on which the receive should be posted.
  115. pDatagram - The datagram object to manage the receive.
  116. Return Value:
  117. RPC_P_IO_PENDING - OK
  118. RPC_S_OUT_OF_MEMORY
  119. RPC_S_OUT_OF_RESOURCES
  120. --*/
  121. {
  122. RPC_STATUS status;
  123. NTSTATUS NtStatus;
  124. DWORD bytes, flags;
  125. int err;
  126. if (pDatagram->Packet.buf == 0)
  127. {
  128. status = I_RpcTransDatagramAllocate(pEndpoint,
  129. (BUFFER *)&pDatagram->Packet.buf,
  130. (PUINT) &pDatagram->Packet.len,
  131. &pDatagram->AddressPair);
  132. if (status != RPC_S_OK)
  133. {
  134. return(RPC_S_OUT_OF_MEMORY);
  135. }
  136. pDatagram->AddressPair->LocalAddress
  137. = WSA_CMSG_DATA(&pDatagram->MessageAncillaryData)
  138. + FIELD_OFFSET(in_pktinfo, ipi_addr);
  139. ASSERT( pDatagram->Packet.buf );
  140. }
  141. ASSERT(*(PDWORD)pDatagram->Packet.buf = 0xDEADF00D);
  142. bytes = flags = 0;
  143. if (!fWSARecvMsgFnPtrInitialized)
  144. {
  145. pDatagram->cRecvAddr = sizeof(WS_SOCKADDR);
  146. err = WSARecvFrom(((WS_DATAGRAM_ENDPOINT*)(pDatagram->pEndpoint))->Socket,
  147. &pDatagram->Packet,
  148. 1,
  149. &bytes,
  150. &flags,
  151. &((WS_SOCKADDR *)pDatagram->AddressPair->RemoteAddress)->generic,
  152. &pDatagram->cRecvAddr,
  153. &pDatagram->Read.ol,
  154. 0);
  155. }
  156. else
  157. {
  158. pDatagram->Msg.lpBuffers = &pDatagram->Packet;
  159. pDatagram->Msg.name = &((WS_SOCKADDR *)pDatagram->AddressPair->RemoteAddress)->generic;
  160. pDatagram->Msg.namelen = sizeof(WS_SOCKADDR);
  161. pDatagram->Msg.dwBufferCount = 1;
  162. pDatagram->Msg.Control.buf = (char *)pDatagram->MessageAncillaryData;
  163. pDatagram->Msg.Control.len = sizeof(pDatagram->MessageAncillaryData);
  164. pDatagram->Msg.dwFlags = 0;
  165. err = WSARecvMsg(((WS_DATAGRAM_ENDPOINT*)(pDatagram->pEndpoint))->Socket,
  166. &pDatagram->Msg,
  167. &bytes,
  168. &pDatagram->Read.ol,
  169. 0);
  170. }
  171. #if 0
  172. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  173. DPFLTR_WARNING_LEVEL,
  174. RPCTRANS "ERROR: RecvFrom: %p buf %p (%d bytes) status %d\n",
  175. pDatagram,
  176. pDatagram->Packet.buf,
  177. pDatagram->Packet.len,
  178. err == 0 ? 0 : GetLastError()));
  179. #endif
  180. if (err == NO_ERROR)
  181. {
  182. return(RPC_P_IO_PENDING);
  183. }
  184. status = GetLastError();
  185. if ( status == ERROR_IO_PENDING
  186. || status == WSAEMSGSIZE )
  187. {
  188. // WSAEMSGSIZE will be handled in complete.cxx. This is like "NO_ERROR"
  189. return(RPC_P_IO_PENDING);
  190. }
  191. RpcpErrorAddRecord( EEInfoGCWinsock,
  192. status,
  193. EEInfoDLWinsockDatagramSubmitReceive10,
  194. ((WS_DATAGRAM_ENDPOINT*)(pDatagram->pEndpoint))->Socket
  195. );
  196. if (WSAECONNRESET == status)
  197. {
  198. return RPC_P_PORT_DOWN;
  199. }
  200. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  201. DPFLTR_WARNING_LEVEL,
  202. RPCTRANS "WSARecvFrom failed %p\n",
  203. GetLastError()));
  204. return(RPC_S_OUT_OF_RESOURCES);
  205. }
  206. void
  207. DG_SubmitReceives(
  208. BASE_ADDRESS *ThisEndpoint
  209. )
  210. /*++
  211. Routine Description:
  212. Helper function called when the pending IO count
  213. on an address is too low.
  214. Arguments:
  215. ThisEndpoint - The address to submit IOs on.
  216. Return Value:
  217. None
  218. --*/
  219. {
  220. PWS_DATAGRAM pDg;
  221. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  222. if (pEndpoint->Socket == 0)
  223. {
  224. //
  225. // The address is currently deactivated, don't submit more I/O
  226. //
  227. return;
  228. }
  229. do
  230. {
  231. BOOL fIoSubmitted;
  232. fIoSubmitted = FALSE;
  233. // Only one thread should be trying to submit IOs at a time.
  234. // This saves locking each DATAGRAM object.
  235. // Simple lock - but requires a loop. See the comment at the end
  236. // of the loop.
  237. if (pEndpoint->fSubmittingIos != 0)
  238. break;
  239. if (InterlockedIncrement(&pEndpoint->fSubmittingIos) != 1)
  240. break;
  241. // Submit new IOs on all the idle datagram objects
  242. for (int i = 0; i < pEndpoint->cMaximumIos; i++)
  243. {
  244. pDg = &pEndpoint->aDatagrams[i];
  245. if (pDg->Busy)
  246. {
  247. continue;
  248. }
  249. // Must be all set for the IO to complete before trying
  250. // to submit the IO.
  251. InterlockedIncrement(&pEndpoint->cPendingIos);
  252. pDg->Busy = TRUE;
  253. if (DG_SubmitReceive(pEndpoint, pDg) == RPC_P_IO_PENDING)
  254. {
  255. fIoSubmitted = TRUE;
  256. }
  257. else
  258. {
  259. pDg->Busy = FALSE;
  260. InterlockedDecrement(&pEndpoint->cPendingIos);
  261. break;
  262. }
  263. }
  264. // Release the "lock" on the endpoint object.
  265. pEndpoint->fSubmittingIos = 0;
  266. if (!fIoSubmitted && pEndpoint->cPendingIos == 0)
  267. {
  268. // It appears that no IO is pending on the endpoint.
  269. COMMON_AddressManager(pEndpoint);
  270. return;
  271. }
  272. // Even if we submitted new IOs, they may all have completed
  273. // already. Which means we may need to loop and submit more
  274. // IOs. This is needed since the thread which completed the
  275. // last IO may have run into our lock and returned.
  276. }
  277. while (pEndpoint->cPendingIos == 0);
  278. return;
  279. }
  280. RPC_STATUS RPC_ENTRY
  281. DG_SendPacket(
  282. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  283. IN DG_TRANSPORT_ADDRESS pAddress,
  284. IN BUFFER pHeader,
  285. IN unsigned cHeader,
  286. IN BUFFER pBody,
  287. IN unsigned cBody,
  288. IN BUFFER pTrailer,
  289. IN unsigned cTrailer
  290. )
  291. /*++
  292. Routine Description:
  293. Sends a packet to an address.
  294. The routine will send a packet built out of the three buffers supplied.
  295. All the buffers are optional, the actual packet sent will be built from
  296. all the buffers actually supplied. In each call at least buffer should
  297. NOT be null.
  298. Arguments:
  299. ThisEndpoint - Endpoint to send from.
  300. pAddress - Address to send to.
  301. pHeader - First data buffer
  302. cHeader - Size of the first data buffer or 0.
  303. pBody - Second data buffer
  304. cBody - Size of the second data buffer or 0.
  305. pTrailer - Third data buffer.
  306. cTrailer - Size of the first data buffer or 0.
  307. Return Value:
  308. RPC_S_OK
  309. RPC_S_OUT_OF_RESOURCES
  310. RPC_P_SEND_FAILED
  311. --*/
  312. {
  313. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  314. WS_SOCKADDR* pSockAddr = (WS_SOCKADDR *)pAddress;
  315. WSABUF buffers[3];
  316. int cBuffers;
  317. HANDLE hIoEvent;
  318. DWORD Status = 0;
  319. if (pHeader)
  320. {
  321. CallTestHook( TH_X_DG_SEND, pHeader, &Status );
  322. }
  323. else
  324. {
  325. CallTestHook( TH_X_DG_SEND, pBody, &Status );
  326. }
  327. if (Status)
  328. {
  329. return Status;
  330. }
  331. hIoEvent = I_RpcTransGetThreadEvent();
  332. cBuffers = 0;
  333. if (cHeader)
  334. {
  335. buffers[cBuffers].len = cHeader;
  336. buffers[cBuffers].buf = (PCHAR) pHeader;
  337. cBuffers++;
  338. }
  339. if (cBody)
  340. {
  341. buffers[cBuffers].len = cBody;
  342. buffers[cBuffers].buf = (PCHAR) pBody;
  343. cBuffers++;
  344. }
  345. if (cTrailer)
  346. {
  347. buffers[cBuffers].len = cTrailer;
  348. buffers[cBuffers].buf = (PCHAR) pTrailer;
  349. cBuffers++;
  350. }
  351. ASSERT(cBuffers);
  352. // All RPC packets have version 4.
  353. //
  354. ASSERT( buffers[0].buf[0] == 4 );
  355. OVERLAPPED ol;
  356. ol.hEvent = (HANDLE)(ULONG_PTR(hIoEvent) | 1);
  357. DWORD bytes;
  358. if ( WSASendTo(pEndpoint->Socket,
  359. buffers,
  360. cBuffers,
  361. &bytes,
  362. 0,
  363. &pSockAddr->generic,
  364. WsTransportTable[pEndpoint->id].SockAddrSize,
  365. &ol,
  366. 0) != 0)
  367. {
  368. DWORD Status = GetLastError();
  369. RpcpErrorAddRecord( EEInfoGCWinsock,
  370. Status,
  371. EEInfoDLWinsockDatagramSend10,
  372. PULONG(&pSockAddr->generic)[0],
  373. PULONG(&pSockAddr->generic)[1]
  374. );
  375. if (WSAENETUNREACH == Status)
  376. {
  377. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  378. DPFLTR_INFO_LEVEL,
  379. RPCTRANS "WSASendTo failed with net unreachable\n",
  380. GetLastError()));
  381. return RPC_P_PORT_DOWN;
  382. }
  383. if (WSAEHOSTDOWN == Status)
  384. {
  385. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  386. DPFLTR_INFO_LEVEL,
  387. RPCTRANS "WSASendTo failed with host down\n",
  388. GetLastError()));
  389. return RPC_P_HOST_DOWN;
  390. }
  391. if (Status != WSA_IO_PENDING)
  392. {
  393. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  394. DPFLTR_INFO_LEVEL,
  395. RPCTRANS "WSASendTo failed %d\n",
  396. GetLastError()));
  397. return(RPC_P_SEND_FAILED);
  398. }
  399. if (WAIT_OBJECT_0 != WaitForSingleObject( hIoEvent, DG_SEND_TIMEOUT ))
  400. {
  401. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  402. DPFLTR_INFO_LEVEL,
  403. RPCTRANS "Dg Send timed out\n"));
  404. //
  405. // Cancel the send and wait for it to complete.
  406. //
  407. CancelIo( (HANDLE)pEndpoint->Socket );
  408. GetOverlappedResult((HANDLE)pEndpoint->Socket,
  409. &ol,
  410. &bytes,
  411. TRUE);
  412. return(RPC_P_SEND_FAILED);
  413. }
  414. BOOL b = GetOverlappedResult((HANDLE)pEndpoint->Socket,
  415. &ol,
  416. &bytes,
  417. TRUE);
  418. if (!b)
  419. {
  420. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  421. DPFLTR_INFO_LEVEL,
  422. RPCTRANS "Dg Send wait failed %d\n",
  423. GetLastError()));
  424. return(RPC_P_SEND_FAILED);
  425. }
  426. }
  427. ASSERT(bytes == cHeader + cBody + cTrailer);
  428. if (pEndpoint->cMinimumIos &&
  429. pEndpoint->cPendingIos <= pEndpoint->cMinimumIos)
  430. {
  431. // It's ok if this fails, this is just a performance optimization.
  432. // Right after a send there often "idle" time waiting for the response
  433. // so this is a good time to submit receives.
  434. DG_SubmitReceives(pEndpoint);
  435. }
  436. return(RPC_S_OK);
  437. }
  438. RPC_STATUS RPC_ENTRY
  439. DG_ForwardPacket(
  440. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  441. IN BUFFER pHeader,
  442. IN unsigned cHeader,
  443. IN BUFFER pBody,
  444. IN unsigned cBody,
  445. IN BUFFER pTrailer,
  446. IN unsigned cTrailer,
  447. IN CHAR * pszPort
  448. )
  449. /*++
  450. Routine Description:
  451. Sends a packet to the server it was originally destined for (that
  452. is, the client had a dynamic endpoint it wished the enpoint mapper
  453. to resolve and forward the packet to).
  454. Arguments:
  455. ThisEndpoint - The endpoint to forward the packet from.
  456. // Buffer like DG_SendPacket
  457. pszPort - Pointer to the server port num to forward to.
  458. This is in an Ansi string.
  459. Return Value:
  460. RPC_S_CANT_CREATE_ENDPOINT - pEndpoint invalid.
  461. results of SendPacket().
  462. --*/
  463. {
  464. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  465. WS_SOCKADDR SockAddr;
  466. PDG_TRANS_INFO pInfo = GetDgTransportInfo(pEndpoint->id);
  467. ASSERT(pEndpoint->type | SERVER);
  468. if (pInfo->EndpointToAddr(pszPort, &SockAddr) != RPC_S_OK)
  469. {
  470. return RPC_S_CANT_CREATE_ENDPOINT;
  471. }
  472. return ( DG_SendPacket(ThisEndpoint,
  473. (PVOID)&SockAddr,
  474. pHeader,
  475. cHeader,
  476. pBody,
  477. cBody,
  478. pTrailer,
  479. cTrailer) );
  480. }
  481. RPC_STATUS
  482. RPC_ENTRY
  483. DG_ReceivePacket(
  484. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  485. OUT DG_TRANSPORT_ADDRESS *pReplyAddress,
  486. OUT PUINT pBufferLength,
  487. OUT BUFFER *pBuffer,
  488. IN LONG Timeout
  489. )
  490. /*++
  491. Routine Description:
  492. Used to wait for a datagram from a server. Returns the data
  493. returned and the address of the machine which replied.
  494. This is a blocking API. It should only be called during sync
  495. client RPC threads.
  496. Arguments:
  497. Endpoint - The endpoint to receive from.
  498. ReplyAddress - Contain the source address of the datagram if
  499. successful.
  500. BufferLength - The size of Buffer on input, the size of the
  501. datagram received on output.
  502. Timeout - Milliseconds to wait for a datagram.
  503. Return Value:
  504. RPC_S_OK
  505. RPC_P_OVERSIZE_PACKET - Datagram > BufferLength arrived,
  506. first BufferLength bytes of Buffer contain the partial datagram.
  507. RPC_P_RECEIVE_FAILED
  508. RPC_P_TIMEOUT
  509. --*/
  510. {
  511. RPC_STATUS status;
  512. BOOL b;
  513. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  514. PWS_DATAGRAM pDatagram = &pEndpoint->aDatagrams[0];
  515. DWORD bytes;
  516. DWORD flags;
  517. int err;
  518. ASSERT((pEndpoint->type & TYPE_MASK) == CLIENT);
  519. ASSERT(pEndpoint->aDatagrams[0].Read.ol.hEvent);
  520. DWORD Status = 0;
  521. CallTestHook( TH_X_DG_SYNC_RECV, ThisEndpoint, &Status );
  522. if (Status)
  523. {
  524. return Status;
  525. }
  526. if (pDatagram->Busy == 0)
  527. {
  528. if (pEndpoint->aDatagrams[0].Packet.buf == 0)
  529. {
  530. status = I_RpcTransDatagramAllocate(pEndpoint,
  531. (BUFFER *)&pDatagram->Packet.buf,
  532. (PUINT) &pDatagram->Packet.len,
  533. &pDatagram->AddressPair);
  534. if (status != RPC_S_OK)
  535. {
  536. return(RPC_S_OUT_OF_MEMORY);
  537. }
  538. pDatagram->cRecvAddr = sizeof(WS_SOCKADDR);
  539. pDatagram->AddressPair->LocalAddress
  540. = WSA_CMSG_DATA(&pDatagram->MessageAncillaryData)
  541. + FIELD_OFFSET(in_pktinfo, ipi_addr);
  542. ASSERT( pDatagram->Packet.buf );
  543. }
  544. bytes = flags = 0;
  545. if (!fWSARecvMsgFnPtrInitialized)
  546. {
  547. err = WSARecvFrom(pEndpoint->Socket,
  548. &pDatagram->Packet,
  549. 1,
  550. &bytes,
  551. &flags,
  552. &((WS_SOCKADDR *)pDatagram->AddressPair->RemoteAddress)->generic,
  553. &pDatagram->cRecvAddr,
  554. &pDatagram->Read.ol,
  555. 0);
  556. }
  557. else
  558. {
  559. pDatagram->Msg.lpBuffers = &pDatagram->Packet;
  560. pDatagram->Msg.name = &((WS_SOCKADDR *)pDatagram->AddressPair->RemoteAddress)->generic;
  561. pDatagram->Msg.namelen = sizeof(WS_SOCKADDR);
  562. pDatagram->Msg.dwBufferCount = 1;
  563. pDatagram->Msg.Control.buf = (char *)&pDatagram->MessageAncillaryData;
  564. pDatagram->Msg.Control.len = sizeof(pDatagram->MessageAncillaryData);
  565. pDatagram->Msg.dwFlags = 0;
  566. err = WSARecvMsg(((WS_DATAGRAM_ENDPOINT*)(pDatagram->pEndpoint))->Socket,
  567. &pDatagram->Msg,
  568. &bytes,
  569. &pDatagram->Read.ol,
  570. 0);
  571. }
  572. if ( err != 0)
  573. {
  574. status = GetLastError();
  575. if (status != WSA_IO_PENDING)
  576. {
  577. RpcpErrorAddRecord( EEInfoGCWinsock,
  578. status,
  579. EEInfoDLWinsockDatagramReceive10,
  580. pEndpoint->Socket
  581. );
  582. if (status == WSAEMSGSIZE)
  583. {
  584. status = RPC_P_OVERSIZE_PACKET;
  585. }
  586. else if (status == WSAECONNRESET)
  587. {
  588. return RPC_P_PORT_DOWN;
  589. }
  590. else
  591. {
  592. // No need to free the packet now.
  593. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  594. DPFLTR_INFO_LEVEL,
  595. RPCTRANS "WSARecvFrom failed %d\n",
  596. status));
  597. ASSERT(pDatagram->Busy == 0);
  598. return(RPC_P_RECEIVE_FAILED);
  599. }
  600. }
  601. else
  602. {
  603. status = RPC_P_IO_PENDING;
  604. }
  605. }
  606. else
  607. {
  608. status = RPC_S_OK;
  609. }
  610. pDatagram->Busy = TRUE;
  611. }
  612. else
  613. {
  614. ASSERT(pDatagram->Busy);
  615. ASSERT(pDatagram->Packet.buf);
  616. status = RPC_P_IO_PENDING;
  617. }
  618. // Wait for IO to complete or timeout
  619. if (status == RPC_P_IO_PENDING)
  620. {
  621. status = WaitForSingleObjectEx(pDatagram->Read.ol.hEvent,
  622. Timeout,
  623. TRUE);
  624. if (status != STATUS_WAIT_0)
  625. {
  626. // In the timeout case we just want to return and
  627. // leave. We'll finish the receive on the next call.
  628. if (status == WAIT_IO_COMPLETION)
  629. {
  630. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  631. DPFLTR_WARNING_LEVEL,
  632. RPCTRANS "DG received cancelled (%p)\n",
  633. pDatagram));
  634. }
  635. else
  636. {
  637. ASSERT(status == STATUS_TIMEOUT);
  638. }
  639. ASSERT(pDatagram->Busy);
  640. return(RPC_P_TIMEOUT);
  641. }
  642. BOOL b = GetOverlappedResult((HANDLE)pEndpoint->Socket,
  643. &pDatagram->Read.ol,
  644. &bytes,
  645. FALSE);
  646. if (!b)
  647. {
  648. RpcpErrorAddRecord( EEInfoGCWinsock,
  649. GetLastError(),
  650. EEInfoDLWinsockDatagramSend20,
  651. pEndpoint->Socket
  652. );
  653. switch (GetLastError())
  654. {
  655. case WSAEMSGSIZE:
  656. case ERROR_MORE_DATA:
  657. ASSERT(bytes == pDatagram->Packet.len);
  658. status = RPC_P_OVERSIZE_PACKET;
  659. break;
  660. case ERROR_PORT_UNREACHABLE:
  661. pDatagram->Busy = 0;
  662. return RPC_P_PORT_DOWN;
  663. break;
  664. case STATUS_TIMEOUT:
  665. ASSERT(0);
  666. case ERROR_OPERATION_ABORTED:
  667. // ERROR_OPERATION_ABORTED can occur if one thread
  668. // tried to make a call and failed, leaving a pending
  669. // receive. That thread dies. Then the endpoint is
  670. // reused by a different thread and the IO is aborted.
  671. // Returning receive failed will cause the runtime to
  672. // retransmit which will do the right thing.
  673. default:
  674. TransDbgDetail((DPFLTR_RPCPROXY_ID,
  675. DPFLTR_INFO_LEVEL,
  676. RPCTRANS "DG sync recv failed %d\n",
  677. GetLastError()));
  678. pDatagram->Busy = 0;
  679. return(RPC_P_RECEIVE_FAILED);
  680. break;
  681. }
  682. }
  683. else
  684. {
  685. status = RPC_S_OK;
  686. }
  687. }
  688. ASSERT( status == RPC_S_OK
  689. || status == RPC_P_OVERSIZE_PACKET);
  690. ASSERT(pDatagram->Busy);
  691. ASSERT(pDatagram->Packet.buf);
  692. ASSERT(bytes <= pDatagram->Packet.len);
  693. *pBuffer = (BUFFER)pDatagram->Packet.buf;
  694. *pBufferLength = bytes;
  695. *pReplyAddress = pDatagram->AddressPair->RemoteAddress;
  696. pDatagram->Packet.buf = 0;
  697. pDatagram->Busy = 0;
  698. return(status);
  699. }
  700. RPC_STATUS
  701. DG_CreateEndpoint(
  702. OUT WS_DATAGRAM_ENDPOINT *pEndpoint
  703. )
  704. /*++
  705. Routine Description:
  706. Creates a new endpoint.
  707. Arguments:
  708. pEndpoint - The runtime allocated endpoint structure to
  709. filled in.
  710. pSockAddr - An initialized sockaddr with the correct
  711. (or no) endpoint.
  712. id - The id of the protocol to use in creating the address.
  713. fClient - If TRUE this is a client endpoint
  714. fAsync - If TRUE this endpoint is "async" which means that
  715. a) It should be added to the IO completion port and
  716. b) that the transport should pend a number of receives
  717. on the endpoint automatically.
  718. EndpointFlags - used in allocation IP ports.
  719. Return Value:
  720. RPC_S_OK
  721. RPC_S_OUT_OF_MEMORY
  722. RPC_S_OUT_OF_RESOURCES
  723. RPC_S_CANT_CREATE_ENDPOINT
  724. RPC_S_DUPLICATE_ENDPOINT
  725. --*/
  726. {
  727. PWS_DATAGRAM pDatagram;
  728. int i, err;
  729. int length;
  730. RPC_STATUS status = RPC_S_OK;
  731. SOCKET sock = 0;
  732. PDG_TRANS_INFO pInfo = GetDgTransportInfo(pEndpoint->id);
  733. BOOL fClient = pEndpoint->fClient;
  734. BOOL fAsync = pEndpoint->fAsync;
  735. LPFN_WSARECVMSG WSARecvMsgFnPtr;
  736. DWORD dwBytesReturned;
  737. // Common stuff
  738. pEndpoint->type = DATAGRAM | ADDRESS;
  739. pEndpoint->Socket = 0;
  740. pEndpoint->Endpoint = 0;
  741. pEndpoint->pAddressVector = 0;
  742. pEndpoint->SubmitListen = DG_SubmitReceives;
  743. pEndpoint->InAddressList = NotInList;
  744. pEndpoint->pNext = 0;
  745. pEndpoint->fSubmittingIos = 0;
  746. pEndpoint->cPendingIos = 0;
  747. pEndpoint->cMinimumIos = 0;
  748. pEndpoint->cMaximumIos = 0;
  749. pEndpoint->aDatagrams = 0;
  750. pEndpoint->pFirstAddress = pEndpoint;
  751. pEndpoint->pNextAddress = 0;
  752. pEndpoint->fAborted = 0;
  753. if (fClient)
  754. {
  755. pEndpoint->type |= CLIENT;
  756. }
  757. else
  758. {
  759. pEndpoint->type |= SERVER;
  760. }
  761. //
  762. // Check if we can use a wrapper around the AFD send/recv
  763. // IOCTLs instead WSASendTo.
  764. //
  765. TryUsingAfd();
  766. //
  767. // Create the socket.
  768. //
  769. sock = WSASocketT(pInfo->AddressFamily,
  770. pInfo->SocketType,
  771. pInfo->Protocol,
  772. 0,
  773. 0,
  774. WSA_FLAG_OVERLAPPED);
  775. if (sock == INVALID_SOCKET)
  776. {
  777. RpcpErrorAddRecord( EEInfoGCWinsock,
  778. GetLastError(),
  779. EEInfoDLWinsockDatagramCreate10,
  780. (ULONG) pInfo->AddressFamily,
  781. (ULONG) pInfo->Protocol
  782. );
  783. switch(GetLastError())
  784. {
  785. case WSAEAFNOSUPPORT:
  786. case WSAEPROTONOSUPPORT:
  787. case WSAENETDOWN:
  788. case WSAESOCKTNOSUPPORT:
  789. status = RPC_S_PROTSEQ_NOT_SUPPORTED;
  790. break;
  791. case WSAENOBUFS:
  792. case WSAEMFILE:
  793. case WSA_NOT_ENOUGH_MEMORY:
  794. status = RPC_S_OUT_OF_MEMORY;
  795. break;
  796. default:
  797. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  798. DPFLTR_WARNING_LEVEL,
  799. RPCTRANS "DG socket() returned 0x%lx\n",
  800. GetLastError()));
  801. ASSERT(0);
  802. // no break
  803. case WSAEPROVIDERFAILEDINIT:
  804. status = RPC_S_OUT_OF_RESOURCES;
  805. break;
  806. }
  807. return(status);
  808. }
  809. if (fWSARecvMsgFnPtrInitialized == FALSE)
  810. {
  811. // if the AddressChangeFn is non-default (i.e. we are in RPCSS),
  812. // use WSARecvMsg so that we can retrieve the local address as
  813. // well
  814. if (AddressChangeFn && (AddressChangeFn != NullAddressChangeFn))
  815. {
  816. // retrieve the WSARecvMsg function pointer
  817. err = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER, (void *) &WSARecvMsgFnPtrUuid,
  818. sizeof(UUID), (void *) &WSARecvMsgFnPtr, sizeof(void *), &dwBytesReturned,
  819. NULL, NULL);
  820. if (err == SOCKET_ERROR)
  821. {
  822. closesocket(sock);
  823. return RPC_S_PROTSEQ_NOT_SUPPORTED;
  824. }
  825. WFT.pWSARecvMsg = WSARecvMsgFnPtr;
  826. fWSARecvMsgFnPtrInitialized = TRUE;
  827. }
  828. }
  829. //
  830. // Make the handle non-inheritable so it goes away when we close it.
  831. //
  832. if (FALSE == SetHandleInformation( (HANDLE) sock, HANDLE_FLAG_INHERIT, 0))
  833. {
  834. closesocket(sock);
  835. return RPC_S_OUT_OF_RESOURCES;
  836. }
  837. //
  838. // Protect the socket to prevent another server from using our port.
  839. //
  840. WS_ProtectListeningSocket(sock, TRUE);
  841. //
  842. // Bind the socket to the endpoint (or to a dynamic endpoint)
  843. //
  844. status = WS_Bind(sock,
  845. &pEndpoint->ListenAddr,
  846. (pEndpoint->id == UDP),
  847. pEndpoint->EndpointFlags);
  848. if (status != RPC_S_OK)
  849. {
  850. closesocket(sock);
  851. return(status);
  852. }
  853. pEndpoint->Socket = sock;
  854. //
  855. // Turn on ring buffering for server- and client-side async endpoints.
  856. // Any error is ignored.
  857. //
  858. if (fAsync || !fClient)
  859. {
  860. DWORD BytesReturned;
  861. if (0 != WSAIoctl( sock, SIO_ENABLE_CIRCULAR_QUEUEING, 0, 0, 0, 0, &BytesReturned, 0, 0 ))
  862. {
  863. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  864. DPFLTR_WARNING_LEVEL,
  865. RPCTRANS "DG couldn't enable circular queueing 0x%lx\n",
  866. GetLastError()));
  867. }
  868. }
  869. if (!fClient)
  870. {
  871. //
  872. // Set server socket recv buffer size..
  873. //
  874. int size;
  875. int PacketInfoOn = TRUE;
  876. if (gfServerPlatform == TRUE && gPhysicalMemorySize >= 40)
  877. {
  878. size = pInfo->ServerBufferSize;
  879. }
  880. else
  881. {
  882. size = pInfo->WorkstationBufferSize;
  883. }
  884. if (fWSARecvMsgFnPtrInitialized)
  885. {
  886. err = setsockopt(sock,
  887. IPPROTO_IP,
  888. IP_PKTINFO,
  889. (char *)&PacketInfoOn,
  890. sizeof(PacketInfoOn)
  891. );
  892. if (err != 0)
  893. {
  894. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  895. DPFLTR_WARNING_LEVEL,
  896. RPCTRANS "DG couldn't set packet info %d\n",
  897. GetLastError()));
  898. closesocket(sock);
  899. return(RPC_S_OUT_OF_MEMORY);
  900. }
  901. }
  902. err = setsockopt(sock,
  903. SOL_SOCKET,
  904. SO_RCVBUF,
  905. (char *) &size,
  906. sizeof(size)
  907. );
  908. #if DBG
  909. if (err != 0)
  910. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  911. DPFLTR_WARNING_LEVEL,
  912. RPCTRANS "DG couldn't set buffer size %d\n",
  913. GetLastError()));
  914. #endif
  915. }
  916. else
  917. {
  918. // Enable broadcast send on the client
  919. DWORD option = TRUE;
  920. err = setsockopt(sock,
  921. SOL_SOCKET,
  922. SO_BROADCAST,
  923. (PCHAR)&option,
  924. sizeof(option));
  925. ASSERT(err == 0);
  926. }
  927. //
  928. // If the endpoint is going to async initialize async part
  929. // and add the socket to the IO completion port.
  930. //
  931. if (status == RPC_S_OK)
  932. {
  933. int cMaxIos;
  934. int cMinIos;
  935. ASSERT(fAsync || fClient);
  936. // Step one, figure out the high and low mark for ios.
  937. if (fAsync)
  938. {
  939. cMinIos = 1;
  940. cMaxIos = 2;
  941. if (gPhysicalMemorySize >= 40) // megabytes
  942. {
  943. cMaxIos = 2
  944. + (gfServerPlatform == TRUE) * 2
  945. + (fClient == FALSE) * gNumberOfProcessors;
  946. // This should be larger than zero so that we'll generally submit new
  947. // recvs during idle time rather then just after receiving a datagram.
  948. cMinIos = 1 + (fClient == FALSE ) * (gNumberOfProcessors/2);
  949. }
  950. }
  951. else
  952. {
  953. // For sync endpoints we need to allocate a single datagram
  954. // object for the receive.
  955. cMinIos = 0;
  956. cMaxIos = 1;
  957. }
  958. ASSERT(cMinIos < cMaxIos);
  959. pEndpoint->cMinimumIos = cMinIos;
  960. pEndpoint->cMaximumIos = cMaxIos;
  961. // Allocate a chunk on memory to hold the array of datagrams
  962. // PERF: For clients, allocate larger array but don't submit all
  963. // the IOs unless we determine that the port is "really" active.
  964. pEndpoint->aDatagrams = new WS_DATAGRAM[cMaxIos];
  965. if (pEndpoint->aDatagrams)
  966. {
  967. UINT type;
  968. type = DATAGRAM | RECEIVE;
  969. type |= (fClient) ? CLIENT : SERVER;
  970. for (i = 0; i < cMaxIos; i++)
  971. {
  972. pDatagram = &pEndpoint->aDatagrams[i];
  973. pDatagram->id = pEndpoint->id;
  974. pDatagram->type = type;
  975. pDatagram->pEndpoint = pEndpoint;
  976. pDatagram->Busy = 0;
  977. pDatagram->Packet.buf = 0;
  978. memset(&pDatagram->Read, 0, sizeof(pDatagram->Read));
  979. pDatagram->Read.pAsyncObject = pDatagram;
  980. }
  981. if (fAsync)
  982. {
  983. status = COMMON_PrepareNewHandle((HANDLE)sock);
  984. }
  985. else
  986. {
  987. // The receive operation on sync endpoints will may span
  988. // several receives. This means it can't use the thread
  989. // event, so allocate an event for the receive.
  990. HANDLE hEvent = CreateEventW(0, TRUE, FALSE, 0);
  991. if (!hEvent)
  992. {
  993. status = RPC_S_OUT_OF_RESOURCES;
  994. }
  995. else
  996. {
  997. ASSERT(pDatagram == &pEndpoint->aDatagrams[0]);
  998. pDatagram->Read.ol.hEvent = hEvent;
  999. }
  1000. }
  1001. }
  1002. else
  1003. {
  1004. status = RPC_S_OUT_OF_MEMORY;
  1005. }
  1006. }
  1007. // If adding a new failure case here, add code to close the sync receive event.
  1008. if (status != RPC_S_OK)
  1009. {
  1010. closesocket(sock);
  1011. delete pEndpoint->aDatagrams;
  1012. return(status);
  1013. }
  1014. TransportProtocol::AddObjectToProtocolList((BASE_ASYNC_OBJECT *) pEndpoint);
  1015. if (!fClient)
  1016. {
  1017. TransportProtocol::FunctionalProtocolDetected(pEndpoint->id);
  1018. }
  1019. return(RPC_S_OK);
  1020. }
  1021. VOID
  1022. DG_DeactivateAddress (
  1023. IN WS_DATAGRAM_ENDPOINT *pEndpoint
  1024. )
  1025. /*++
  1026. Function Name:DG_DeactivateAddress
  1027. Parameters:
  1028. Description:
  1029. Returns:
  1030. --*/
  1031. {
  1032. if (InterlockedIncrement(&pEndpoint->fAborted) != 1)
  1033. {
  1034. return;
  1035. }
  1036. if (pEndpoint->Socket)
  1037. {
  1038. closesocket(pEndpoint->Socket);
  1039. pEndpoint->Socket = 0;
  1040. }
  1041. }
  1042. RPC_STATUS
  1043. DG_ReactivateAddress (
  1044. IN WS_DATAGRAM_ENDPOINT *pEndpoint
  1045. )
  1046. /*++
  1047. Function Name:DG_DeactivateAddress
  1048. Parameters:
  1049. Description:
  1050. Returns:
  1051. --*/
  1052. {
  1053. RPC_STATUS Status;
  1054. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1055. //
  1056. // If the endpoint is dynamic, clear out the endpoint
  1057. //
  1058. if (pEndpoint->fDynamicEndpoint)
  1059. {
  1060. //
  1061. // Clear out the listenaddr
  1062. //
  1063. switch (pEndpoint->id)
  1064. {
  1065. #ifdef IPX_ON
  1066. case IPX:
  1067. addr->ipxaddr.sa_socket = 0;
  1068. break;
  1069. #endif
  1070. case CDP:
  1071. CDP_InitLocalAddress(&addr->clusaddr, 0);
  1072. break;
  1073. case UDP:
  1074. addr->inetaddr.sin_port = 0;
  1075. break;
  1076. default:
  1077. ASSERT(0);
  1078. }
  1079. }
  1080. Status = DG_CreateEndpoint(pEndpoint);
  1081. if (Status == RPC_S_OK)
  1082. {
  1083. pEndpoint->fAborted = 0;
  1084. DG_SubmitReceives(pEndpoint);
  1085. }
  1086. return Status;
  1087. }
  1088. void RPC_ENTRY
  1089. DG_ServerAbortListen(
  1090. IN DG_TRANSPORT_ENDPOINT ThisEndpoint
  1091. )
  1092. /*++
  1093. Routine Description:
  1094. Callback after DG_CreateEndpoint has completed successfully
  1095. but the runtime for some reason is not going to be able to
  1096. listen on the endpoint.
  1097. --*/
  1098. {
  1099. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1100. ASSERT(pEndpoint->cPendingIos == 0);
  1101. ASSERT(pEndpoint->Socket);
  1102. ASSERT(pEndpoint->pNext == 0);
  1103. ASSERT(pEndpoint->type & SERVER);
  1104. delete pEndpoint->pAddressVector;
  1105. delete pEndpoint->aDatagrams;
  1106. closesocket(pEndpoint->Socket);
  1107. return;
  1108. }
  1109. RPC_STATUS RPC_ENTRY
  1110. DG_ClientCloseEndpoint(
  1111. IN DG_TRANSPORT_ENDPOINT ThisEndpoint
  1112. )
  1113. /*++
  1114. Routine Description:
  1115. Called on sync client endpoints when they are no longer needed.
  1116. Arguments:
  1117. ThisEndpoint
  1118. Return Value:
  1119. RPC_S_OK
  1120. --*/
  1121. {
  1122. BOOL b;
  1123. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1124. PWS_DATAGRAM pDatagram = &pEndpoint->aDatagrams[0];
  1125. ASSERT((pEndpoint->type & TYPE_MASK) == CLIENT);
  1126. ASSERT(pEndpoint->Socket); // Open must have worked
  1127. ASSERT(pEndpoint->cMinimumIos == 0);
  1128. ASSERT(pEndpoint->cMaximumIos == 1); // Must not be async!
  1129. ASSERT(pEndpoint->aDatagrams);
  1130. ASSERT(pEndpoint->aDatagrams[0].Read.ol.hEvent);
  1131. ASSERT(pEndpoint->Endpoint == 0);
  1132. ASSERT(pEndpoint->pAddressVector == 0);
  1133. ASSERT(pEndpoint->pNext == 0);
  1134. // If there is a pending receive, closing the socket will cancel the IO.
  1135. closesocket(pEndpoint->Socket);
  1136. // Wait for the pending receive to actually complete.
  1137. if (pDatagram->Busy)
  1138. {
  1139. DWORD bytes;
  1140. ASSERT(pDatagram->Busy);
  1141. ASSERT(pDatagram->Packet.buf);
  1142. GetOverlappedResult((HANDLE)pEndpoint->Socket,
  1143. &pDatagram->Read.ol,
  1144. &bytes,
  1145. TRUE);
  1146. if (GetLastError() != ERROR_OPERATION_ABORTED)
  1147. {
  1148. // Overactive output, the receive may have completed normally..
  1149. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  1150. DPFLTR_WARNING_LEVEL,
  1151. RPCTRANS "DG receive completed %d on %p after closed\n",
  1152. GetLastError(),
  1153. pDatagram));
  1154. }
  1155. }
  1156. b = CloseHandle(pEndpoint->aDatagrams[0].Read.ol.hEvent);
  1157. ASSERT(b);
  1158. TransportProtocol::RemoveObjectFromProtocolList(pEndpoint);
  1159. // Free the receive buffer if allocated
  1160. if (pDatagram->Packet.buf)
  1161. {
  1162. I_RpcTransDatagramFree(pEndpoint,
  1163. (BUFFER)pDatagram->Packet.buf
  1164. );
  1165. }
  1166. delete pDatagram;
  1167. return(RPC_S_OK);
  1168. }
  1169. RPC_STATUS
  1170. RPC_ENTRY
  1171. DG_GetEndpointStats(
  1172. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1173. OUT DG_ENDPOINT_STATS * pStats
  1174. )
  1175. {
  1176. DWORD Status;
  1177. DWORD Data;
  1178. int Length;
  1179. BOOL Ok;
  1180. PWS_DATAGRAM_ENDPOINT Endpoint = (PWS_DATAGRAM_ENDPOINT) ThisEndpoint;
  1181. Length = sizeof(DWORD);
  1182. Status = getsockopt(Endpoint->Socket, SOL_SOCKET, SO_MAX_MSG_SIZE, (char *) &Data, &Length);
  1183. if (Status)
  1184. {
  1185. return GetLastError();
  1186. }
  1187. //
  1188. // jroberts, 10-Jan-2001 : I believe that getsockopt is returning 0xffffffff occasionally.
  1189. // This is an attempt to catch it.
  1190. //
  1191. if (Endpoint->id == UDP)
  1192. {
  1193. ASSERT( Data < 0x10000 );
  1194. }
  1195. Data &= ~7UL;
  1196. pStats->MaxPduSize = Data;
  1197. Length = sizeof(DWORD);
  1198. Status = getsockopt(Endpoint->Socket, SOL_SOCKET, SO_RCVBUF, (char *) &Data, &Length);
  1199. if (Status)
  1200. {
  1201. return GetLastError();
  1202. }
  1203. Data &= ~7UL;
  1204. pStats->ReceiveBufferSize = Data;
  1205. return RPC_S_OK;
  1206. }
  1207. ////////////////////////////////////////////////////////////////////////
  1208. //
  1209. // CDP/IP specific functions.
  1210. //
  1211. RPC_STATUS RPC_ENTRY
  1212. CDP_ServerListen(
  1213. IN OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1214. IN RPC_CHAR *NetworkAddress,
  1215. IN OUT RPC_CHAR **pPort,
  1216. IN void *pSecurityDescriptor,
  1217. IN ULONG EndpointFlags,
  1218. IN ULONG NICFlags,
  1219. OUT NETWORK_ADDRESS_VECTOR **ppNetworkAddressVector
  1220. )
  1221. /*++
  1222. Routine Description:
  1223. Creates a server endpoint object to receive packets. New
  1224. packets won't actually arrive until CompleteListen is
  1225. called.
  1226. Arguments:
  1227. ThisEndpoint - Storage for the server endpoint object.
  1228. pPort - The endpoint to listen on or a pointer to 0 if
  1229. the transport should choose the address.
  1230. Contains the endpoint listened to on output. The
  1231. caller should free this.
  1232. pSecurityDiscriptor - Security to attach to this endpoint (not
  1233. used by this transport).
  1234. EndpointFlags - Application flags passed into RPC via
  1235. RpcServerUseProtseq*Ex.
  1236. NICFlags - Application flags passed into RPC via
  1237. RpcServerUseProtseq*Ex.
  1238. pNetworkAddresses - A vector of the network addresses
  1239. listened on by this call. This vector does
  1240. not need to be freed.
  1241. Return Value:
  1242. RPC_S_OK
  1243. RPC_S_OUT_OF_MEMORY
  1244. RPC_S_OUT_OF_RESOURCES
  1245. RPC_S_CANT_CREATE_ENDPOINT
  1246. RPC_S_INVALID_ENDPOINT_FORMAT
  1247. RPC_S_DUPLICATE_ENDPOINT
  1248. --*/
  1249. {
  1250. RPC_STATUS status;
  1251. NTSTATUS NtStatus;
  1252. USHORT port;
  1253. UNICODE_STRING UnicodeString;
  1254. ANSI_STRING AsciiString;
  1255. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1256. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1257. NETWORK_ADDRESS_VECTOR * ServerAddress;
  1258. // Figure out the port to listen on.
  1259. if (*pPort)
  1260. {
  1261. status = EndpointToPortNumber(*pPort, port);
  1262. if (status != RPC_S_OK)
  1263. {
  1264. return(status);
  1265. }
  1266. pEndpoint->fDynamicEndpoint = 0;
  1267. CDP_InitLocalAddress( &addr->clusaddr, port );
  1268. }
  1269. else
  1270. {
  1271. return RPC_S_INVALID_ENDPOINT_FORMAT;
  1272. }
  1273. pEndpoint->id = CDP;
  1274. pEndpoint->fClient = FALSE;
  1275. pEndpoint->fAsync = TRUE;
  1276. pEndpoint->EndpointFlags = 0;
  1277. //
  1278. // Actually create the endpoint
  1279. //
  1280. status =
  1281. DG_CreateEndpoint(pEndpoint);
  1282. if (status != RPC_S_OK)
  1283. {
  1284. return(status);
  1285. }
  1286. // Figure out the network addresses.
  1287. // The only way we can determine our cluster
  1288. // address is to read it out of the registry
  1289. status = CDP_BuildAddressVector(&pEndpoint->pAddressVector);
  1290. if (status != RPC_S_OK)
  1291. {
  1292. DG_ServerAbortListen(ThisEndpoint);
  1293. return(status);
  1294. }
  1295. *ppNetworkAddressVector = pEndpoint->pAddressVector;
  1296. return RPC_S_OK;
  1297. }
  1298. RPC_STATUS
  1299. CDP_QueryEndpoint
  1300. (
  1301. IN void * pOriginalEndpoint,
  1302. OUT RPC_CHAR * pClientEndpoint
  1303. )
  1304. {
  1305. WS_SOCKADDR * pSockAddr = (WS_SOCKADDR *)pOriginalEndpoint;
  1306. unsigned NativeSocket = pSockAddr->clusaddr.sac_port;
  1307. char AnsiBuffer[CDP_MAXIMUM_ENDPOINT];
  1308. char * pAnsi = AnsiBuffer;
  1309. RPC_CHAR * pUni = pClientEndpoint;
  1310. //
  1311. // Convert endpoint to an ASCII string, and thence to Unicode.
  1312. //
  1313. _ultoa(NativeSocket, AnsiBuffer, 10);
  1314. while ( *pUni++ = *pAnsi++ );
  1315. return RPC_S_OK;
  1316. }
  1317. RPC_STATUS
  1318. CDP_QueryAddress
  1319. (
  1320. IN void * pOriginalEndpoint,
  1321. OUT RPC_CHAR * pClientAddress
  1322. )
  1323. {
  1324. PSOCKADDR_CLUSTER pSockAddr = (PSOCKADDR_CLUSTER) pOriginalEndpoint;
  1325. _ultow(pSockAddr->sac_node, pClientAddress, 10);
  1326. return(RPC_S_OK);
  1327. }
  1328. RPC_STATUS
  1329. RPC_ENTRY
  1330. CDP_ClientInitializeAddress
  1331. (
  1332. OUT DG_TRANSPORT_ADDRESS Address,
  1333. IN RPC_CHAR *NetworkAddress,
  1334. IN RPC_CHAR *pPort,
  1335. IN BOOL fUseCache,
  1336. IN BOOL fBroadcast
  1337. )
  1338. /*++
  1339. Routine Description:
  1340. Initializes a address object for sending to a server.
  1341. Arguments:
  1342. Address - Storage for the address
  1343. NetworkAddress - The address of the server or 0 if local
  1344. Endpoint - The endpoint of the server
  1345. fUseCache - If TRUE then the transport may use a cached
  1346. value from a previous call on the same NetworkAddress.
  1347. fBroadcast - If TRUE, NetworkAddress is ignored and a broadcast
  1348. address is used.
  1349. Return Value:
  1350. RPC_S_OK - Success, name resolved and, optionally, added to cache.
  1351. RPC_P_FOUND_IN_CACHE - Success, returned only if fUseCache is TRUE
  1352. and the was name found in local cache.
  1353. RPC_P_MATCHED_CACHE - Partial success, fUseCache is FALSE and the
  1354. result of the lookup was the same as the value previously
  1355. in the cache.
  1356. RPC_S_OUT_OF_MEMORY
  1357. RPC_S_OUT_OF_RESOURCES
  1358. RPC_S_INVALID_ENDPOINT_FORMAT
  1359. RPC_S_SERVER_UNAVAILABLE
  1360. --*/
  1361. {
  1362. WS_SOCKADDR *pAddr = (WS_SOCKADDR *)Address;
  1363. ULONG HostAddr;
  1364. ULONG Endpoint;
  1365. int i;
  1366. USHORT port;
  1367. RPC_STATUS status;
  1368. // Figure out the destination port
  1369. status = EndpointToPortNumber(pPort, port);
  1370. if (RPC_S_OK != status)
  1371. {
  1372. ASSERT( 0 );
  1373. return(status);
  1374. }
  1375. CDP_InitLocalAddress( &pAddr->clusaddr, port );
  1376. // Resolve the network address - CDP addresses are
  1377. // numbers representing a member ID in the cluster.
  1378. pAddr->clusaddr.sac_node = _wtol( NetworkAddress );
  1379. return(status);
  1380. }
  1381. RPC_STATUS
  1382. RPC_ENTRY
  1383. CDP_ClientOpenEndpoint(
  1384. OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1385. IN BOOL fAsync,
  1386. DWORD Flags
  1387. )
  1388. {
  1389. RPC_STATUS Status;
  1390. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1391. // We don't care what port we bind to, we also don't care what
  1392. // port we bind to.
  1393. // I think he's trying to say that we don't care what port
  1394. // we bind to.
  1395. CDP_InitLocalAddress(&pEndpoint->ListenAddr.clusaddr, 0);
  1396. pEndpoint->id = CDP;
  1397. pEndpoint->fClient = TRUE;
  1398. pEndpoint->fAsync = fAsync;
  1399. pEndpoint->EndpointFlags = 0;
  1400. pEndpoint->fDynamicEndpoint = 1;
  1401. Status = DG_CreateEndpoint(pEndpoint);
  1402. return Status;
  1403. }
  1404. RPC_STATUS
  1405. CDP_InitializeSockAddr(
  1406. IN char *Endpoint,
  1407. OUT WS_SOCKADDR *pSockAddr
  1408. )
  1409. /*++
  1410. Routine Description:
  1411. Initialized the sockaddr to be a loopback address to the
  1412. endpoint specified. Used to forward packets locally.
  1413. Arguments:
  1414. Endpoint - The string value of the servers endpoint.
  1415. pSockAddr - The sockaddr to fill in.
  1416. Return Value:
  1417. RPC_S_INVALID_ENDPOINT_FORMAT
  1418. RPC_S_OK
  1419. --*/
  1420. {
  1421. long port;
  1422. port = atol(Endpoint);
  1423. if (port <= 0 || port > 0xFFFF)
  1424. {
  1425. ASSERT( 0 );
  1426. return(RPC_S_INVALID_ENDPOINT_FORMAT);
  1427. }
  1428. CDP_InitLocalAddress(&pSockAddr->clusaddr, (unsigned short) port);
  1429. return(RPC_S_OK);
  1430. }
  1431. RPC_STATUS
  1432. RPC_ENTRY
  1433. CDP_GetEndpointStats(
  1434. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1435. OUT DG_ENDPOINT_STATS * pStats
  1436. )
  1437. {
  1438. RPC_STATUS Status;
  1439. Status = DG_GetEndpointStats(ThisEndpoint, pStats);
  1440. if (Status)
  1441. {
  1442. pStats->MaxPduSize = 1452;
  1443. pStats->MaxPacketSize = 1452;
  1444. pStats->PreferredPduSize = 1452;
  1445. pStats->ReceiveBufferSize= 8192;
  1446. return Status;
  1447. }
  1448. //
  1449. // ethernet frame (1500) - UDP/IP headers (28) - CNP/CDP headers (20)
  1450. //
  1451. pStats->MaxPacketSize = 1452;
  1452. pStats->PreferredPduSize = 4096;
  1453. if (pStats->PreferredPduSize > pStats->MaxPduSize)
  1454. {
  1455. pStats->PreferredPduSize = pStats->MaxPduSize;
  1456. }
  1457. return RPC_S_OK;
  1458. }
  1459. ////////////////////////////////////////////////////////////////////////
  1460. //
  1461. // UDP/IP specific functions.
  1462. //
  1463. RPC_STATUS RPC_ENTRY
  1464. UDP_ServerListen(
  1465. IN OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1466. IN RPC_CHAR *NetworkAddress,
  1467. IN OUT RPC_CHAR **pPort,
  1468. IN void *pSecurityDescriptor,
  1469. IN ULONG EndpointFlags,
  1470. IN ULONG NICFlags,
  1471. OUT NETWORK_ADDRESS_VECTOR **ppNetworkAddressVector
  1472. )
  1473. /*++
  1474. Routine Description:
  1475. Creates a server endpoint object to receive packets. New
  1476. packets won't actually arrive until CompleteListen is
  1477. called.
  1478. Arguments:
  1479. ThisEndpoint - Storage for the server endpoint object.
  1480. pPort - The endpoint to listen on or a pointer to 0 if
  1481. the transport should choose the address.
  1482. Contains the endpoint listened to on output. The
  1483. caller should free this.
  1484. pSecurityDiscriptor - Security to attach to this endpoint (not
  1485. used by UDP).
  1486. EndpointFlags - Application flags passed into RPC via
  1487. RpcServerUseProtseq*Ex.
  1488. NICFlags - Application flags passed into RPC via
  1489. RpcServerUseProtseq*Ex.
  1490. pNetworkAddresses - A vector of the network addresses
  1491. listened on by this call. This vector does
  1492. not need to be freed.
  1493. Return Value:
  1494. RPC_S_OK
  1495. RPC_S_OUT_OF_MEMORY
  1496. RPC_S_OUT_OF_RESOURCES
  1497. RPC_S_CANT_CREATE_ENDPOINT
  1498. RPC_S_INVALID_ENDPOINT_FORMAT
  1499. RPC_S_DUPLICATE_ENDPOINT
  1500. --*/
  1501. {
  1502. RPC_STATUS status;
  1503. NTSTATUS NtStatus;
  1504. USHORT port;
  1505. UNICODE_STRING UnicodeString;
  1506. ANSI_STRING AsciiString;
  1507. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1508. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1509. addr->inetaddr.sin_family = AF_INET;
  1510. if (NetworkAddress)
  1511. {
  1512. IP_ADDRESS_RESOLVER resolver(NetworkAddress,
  1513. cosServer,
  1514. ipvtuIPv4 // IP version to use
  1515. );
  1516. // Loop until success, fatal failure or we run out of addresses.
  1517. status = resolver.NextAddress(&addr->inetaddr);
  1518. if (status != RPC_S_OK)
  1519. {
  1520. return RPC_S_INVALID_NET_ADDR;
  1521. }
  1522. }
  1523. else
  1524. {
  1525. addr->inetaddr.sin_addr.s_addr = INADDR_ANY;
  1526. }
  1527. // Figure out the port to listen on.
  1528. if (*pPort)
  1529. {
  1530. status = EndpointToPortNumber(*pPort, port);
  1531. if (status != RPC_S_OK)
  1532. {
  1533. return(status);
  1534. }
  1535. pEndpoint->fDynamicEndpoint = 0;
  1536. addr->inetaddr.sin_port = htons(port);
  1537. }
  1538. else
  1539. {
  1540. addr->inetaddr.sin_port = 0;
  1541. pEndpoint->fDynamicEndpoint = 1;
  1542. }
  1543. pEndpoint->id = UDP;
  1544. pEndpoint->fClient = FALSE;
  1545. pEndpoint->fAsync = TRUE;
  1546. pEndpoint->EndpointFlags = EndpointFlags;
  1547. //
  1548. // Actually create the endpoint
  1549. //
  1550. status =
  1551. DG_CreateEndpoint(pEndpoint);
  1552. if (status != RPC_S_OK)
  1553. {
  1554. return(status);
  1555. }
  1556. // If needed, figure out the dynamically allocated endpoint.
  1557. if (!*pPort)
  1558. {
  1559. *pPort = new RPC_CHAR[IP_MAXIMUM_ENDPOINT];
  1560. if (!*pPort)
  1561. {
  1562. DG_ServerAbortListen(ThisEndpoint);
  1563. return(RPC_S_OUT_OF_MEMORY);
  1564. }
  1565. port = ntohs(addr->inetaddr.sin_port);
  1566. PortNumberToEndpoint(port, *pPort);
  1567. }
  1568. // Figure out the network addresses
  1569. status = IP_BuildAddressVector(&pEndpoint->pAddressVector,
  1570. RPC_C_BIND_TO_ALL_NICS,
  1571. NULL,
  1572. NULL);
  1573. if (status != RPC_S_OK)
  1574. {
  1575. DG_ServerAbortListen(ThisEndpoint);
  1576. return(status);
  1577. }
  1578. *ppNetworkAddressVector = pEndpoint->pAddressVector;
  1579. return RPC_S_OK;
  1580. }
  1581. RPC_STATUS
  1582. UDP_QueryEndpoint
  1583. (
  1584. IN void * pOriginalEndpoint,
  1585. OUT RPC_CHAR * pClientEndpoint
  1586. )
  1587. {
  1588. WS_SOCKADDR * pSockAddr = (WS_SOCKADDR *)pOriginalEndpoint;
  1589. unsigned NativeSocket = ntohs(pSockAddr->inetaddr.sin_port);
  1590. char AnsiBuffer[IP_MAXIMUM_ENDPOINT];
  1591. char * pAnsi = AnsiBuffer;
  1592. RPC_CHAR * pUni = pClientEndpoint;
  1593. //
  1594. // Convert endpoint to an ASCII string, and thence to Unicode.
  1595. //
  1596. _ultoa(NativeSocket, AnsiBuffer, 10);
  1597. while ( *pUni++ = *pAnsi++ );
  1598. return RPC_S_OK;
  1599. }
  1600. RPC_STATUS
  1601. UDP_QueryAddress
  1602. (
  1603. IN void * pOriginalEndpoint,
  1604. OUT RPC_CHAR * pClientAddress
  1605. )
  1606. {
  1607. UNICODE_STRING UnicodeString;
  1608. ANSI_STRING AsciiString;
  1609. WS_SOCKADDR *pSockAddr = (WS_SOCKADDR *)pOriginalEndpoint;
  1610. NTSTATUS NtStatus;
  1611. UnicodeString.Buffer = pClientAddress;
  1612. UnicodeString.Length = 0;
  1613. UnicodeString.MaximumLength = IP_MAXIMUM_RAW_NAME * sizeof(RPC_CHAR);
  1614. char *t = inet_ntoa(pSockAddr->inetaddr.sin_addr);
  1615. ASSERT(t);
  1616. RtlInitAnsiString(&AsciiString, t);
  1617. ASSERT(AsciiString.Length < IP_MAXIMUM_RAW_NAME);
  1618. NtStatus = RtlAnsiStringToUnicodeString(&UnicodeString,
  1619. &AsciiString,
  1620. FALSE);
  1621. if (!NT_SUCCESS(NtStatus))
  1622. {
  1623. ASSERT(0);
  1624. return(RPC_S_OUT_OF_RESOURCES);
  1625. }
  1626. return(RPC_S_OK);
  1627. }
  1628. RPC_STATUS
  1629. RPC_ENTRY
  1630. UDP_ClientInitializeAddress
  1631. (
  1632. OUT DG_TRANSPORT_ADDRESS Address,
  1633. IN RPC_CHAR *NetworkAddress,
  1634. IN RPC_CHAR *pPort,
  1635. IN BOOL fUseCache,
  1636. IN BOOL fBroadcast
  1637. )
  1638. /*++
  1639. Routine Description:
  1640. Initializes a address object for sending to a server.
  1641. Arguments:
  1642. Address - Storage for the address
  1643. NetworkAddress - The address of the server or 0 if local
  1644. Endpoint - The endpoint of the server
  1645. fUseCache - If TRUE then the transport may use a cached
  1646. value from a previous call on the same NetworkAddress.
  1647. fBroadcast - If TRUE, NetworkAddress is ignored and a broadcast
  1648. address is used.
  1649. Return Value:
  1650. RPC_S_OK - Success, name resolved and, optionally, added to cache.
  1651. RPC_P_FOUND_IN_CACHE - Success, returned only if fUseCache is TRUE
  1652. and the was name found in local cache.
  1653. RPC_P_MATCHED_CACHE - Partial success, fUseCache is FALSE and the
  1654. result of the lookup was the same as the value previously
  1655. in the cache.
  1656. RPC_S_OUT_OF_MEMORY
  1657. RPC_S_OUT_OF_RESOURCES
  1658. RPC_S_INVALID_ENDPOINT_FORMAT
  1659. RPC_S_SERVER_UNAVAILABLE
  1660. --*/
  1661. {
  1662. WS_SOCKADDR *pAddr = (WS_SOCKADDR *)Address;
  1663. ULONG HostAddr;
  1664. ULONG Endpoint;
  1665. int i;
  1666. USHORT port;
  1667. RPC_STATUS status;
  1668. // Contant part of address
  1669. memset(pAddr->inetaddr.sin_zero, 0, 8);
  1670. pAddr->inetaddr.sin_family = AF_INET;
  1671. // Figure out the destination port
  1672. status = EndpointToPortNumber(pPort, port);
  1673. if (RPC_S_OK != status)
  1674. {
  1675. return(status);
  1676. }
  1677. pAddr->inetaddr.sin_port = htons(port);
  1678. // Resolve the network address
  1679. if (fBroadcast)
  1680. {
  1681. pAddr->inetaddr.sin_addr.s_addr = INADDR_BROADCAST;
  1682. return(RPC_S_OK);
  1683. }
  1684. // Multiple server address support for UDP/IP is not available.
  1685. IP_ADDRESS_RESOLVER resolver(NetworkAddress,
  1686. cosClient,
  1687. ipvtuIPv4 // IP version to use
  1688. );
  1689. status = resolver.NextAddress(&pAddr->inetaddr);
  1690. if (status)
  1691. {
  1692. RpcpErrorAddRecord( EEInfoGCWinsock,
  1693. status,
  1694. EEInfoDLWinsockDatagramResolve10,
  1695. NetworkAddress
  1696. );
  1697. }
  1698. return(status);
  1699. }
  1700. RPC_STATUS
  1701. RPC_ENTRY
  1702. UDP_ClientOpenEndpoint(
  1703. OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1704. IN BOOL fAsync,
  1705. DWORD EndpointFlags
  1706. )
  1707. {
  1708. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1709. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1710. // We don't care what port we bind to, we also don't care what
  1711. // port we bind to.
  1712. memset(addr, 0, sizeof(*addr));
  1713. addr->inetaddr.sin_family = AF_INET;
  1714. pEndpoint->id = UDP;
  1715. pEndpoint->fClient = TRUE;
  1716. pEndpoint->fAsync = fAsync;
  1717. pEndpoint->EndpointFlags = EndpointFlags;
  1718. pEndpoint->fDynamicEndpoint = 1;
  1719. return(DG_CreateEndpoint(pEndpoint));
  1720. }
  1721. RPC_STATUS
  1722. UDP_InitializeSockAddr(
  1723. IN char *Endpoint,
  1724. OUT WS_SOCKADDR *pSockAddr
  1725. )
  1726. /*++
  1727. Routine Description:
  1728. Initialized the sockaddr to be a loopback address to the
  1729. endpoint specified. Used to forward packets locally.
  1730. Arguments:
  1731. Endpoint - The string value of the servers endpoint.
  1732. pSockAddr - The sockaddr to fill in.
  1733. Return Value:
  1734. RPC_S_INVALID_ENDPOINT_FORMAT
  1735. RPC_S_OK
  1736. --*/
  1737. {
  1738. int len;
  1739. long port;
  1740. pSockAddr->generic.sa_family = AF_INET;
  1741. pSockAddr->inetaddr.sin_addr.s_addr = 0x0100007F; // byte swapped, 127.0.0.1
  1742. port = atol(Endpoint);
  1743. if (port <= 0 || port > 0xFFFF)
  1744. {
  1745. return(RPC_S_INVALID_ENDPOINT_FORMAT);
  1746. }
  1747. pSockAddr->inetaddr.sin_port = htons((USHORT) port);
  1748. return(RPC_S_OK);
  1749. }
  1750. RPC_STATUS
  1751. RPC_ENTRY
  1752. UDP_GetEndpointStats(
  1753. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1754. OUT DG_ENDPOINT_STATS * pStats
  1755. )
  1756. {
  1757. RPC_STATUS Status;
  1758. Status = DG_GetEndpointStats(ThisEndpoint, pStats);
  1759. if (Status)
  1760. {
  1761. pStats->MaxPduSize = 1472;
  1762. pStats->MaxPacketSize = 1472;
  1763. pStats->PreferredPduSize = 1472;
  1764. pStats->ReceiveBufferSize= 8192;
  1765. return Status;
  1766. }
  1767. pStats->MaxPacketSize = 1472;
  1768. pStats->PreferredPduSize = 4096;
  1769. if (pStats->PreferredPduSize > pStats->MaxPduSize)
  1770. {
  1771. pStats->PreferredPduSize = pStats->MaxPduSize;
  1772. }
  1773. return RPC_S_OK;
  1774. }
  1775. #ifdef IPX_ON
  1776. ////////////////////////////////////////////////////////////////////////
  1777. //
  1778. // IPX specific functions.
  1779. //
  1780. RPC_STATUS RPC_ENTRY
  1781. IPX_ServerListen(
  1782. IN OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1783. IN RPC_CHAR *NetworkAddress,
  1784. IN OUT RPC_CHAR **pPort,
  1785. IN void *pSecurityDescriptor,
  1786. IN ULONG EndpointFlags,
  1787. IN ULONG NICFlags,
  1788. OUT NETWORK_ADDRESS_VECTOR **ppNetworkAddressVector
  1789. )
  1790. /*++
  1791. Routine Description:
  1792. Creates a server endpoint object to receive packets. New
  1793. packets won't actually arrive until CompleteListen is
  1794. called.
  1795. Arguments:
  1796. ThisEndpoint - Storage for the server endpoint object.
  1797. pPort - The endpoint to listen on or a pointer to 0 if
  1798. the transport should choose the address.
  1799. Contains the endpoint listened to on output. The
  1800. caller should free this.
  1801. pSecurityDiscriptor - Security to attach to this endpoint (not
  1802. used by the IPX transport).
  1803. EndpointFlags - Application flags passed into RPC via
  1804. RpcServerUseProtseq*Ex.
  1805. NICFlags - Application flags passed into RPC via
  1806. RpcServerUseProtseq*Ex.
  1807. pNetworkAddresses - A vector of the network addresses
  1808. listened on by this call. This vector does
  1809. not need to be freed.
  1810. Return Value:
  1811. RPC_S_OK
  1812. RPC_S_OUT_OF_MEMORY
  1813. RPC_S_OUT_OF_RESOURCES
  1814. RPC_S_CANT_CREATE_ENDPOINT
  1815. RPC_S_INVALID_ENDPOINT_FORMAT
  1816. RPC_S_DUPLICATE_ENDPOINT
  1817. --*/
  1818. {
  1819. RPC_STATUS status;
  1820. NTSTATUS NtStatus;
  1821. USHORT port;
  1822. UNICODE_STRING UnicodeString;
  1823. ANSI_STRING AsciiString;
  1824. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1825. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1826. //
  1827. // Figure out what port to listen on.
  1828. //
  1829. if (*pPort)
  1830. {
  1831. status = EndpointToPortNumber(*pPort, port);
  1832. if (status != RPC_S_OK)
  1833. {
  1834. return(status);
  1835. }
  1836. pEndpoint->fDynamicEndpoint = 0;
  1837. }
  1838. else
  1839. {
  1840. port = 0;
  1841. pEndpoint->fDynamicEndpoint = 1;
  1842. }
  1843. addr->generic.sa_family = AF_IPX;
  1844. addr->ipxaddr.sa_socket = htons(port);
  1845. pEndpoint->id = IPX;
  1846. pEndpoint->fClient = FALSE;
  1847. pEndpoint->fAsync = TRUE;
  1848. pEndpoint->EndpointFlags = 0;
  1849. //
  1850. // Actually create the endpoint
  1851. //
  1852. status =
  1853. DG_CreateEndpoint(pEndpoint);
  1854. if (status != RPC_S_OK)
  1855. {
  1856. return(status);
  1857. }
  1858. // If needed, figure out the dynamically allocated endpoint.
  1859. if (!*pPort)
  1860. {
  1861. *pPort = new RPC_CHAR[IP_MAXIMUM_ENDPOINT];
  1862. if (!*pPort)
  1863. {
  1864. DG_ServerAbortListen(ThisEndpoint);
  1865. return(RPC_S_OUT_OF_MEMORY);
  1866. }
  1867. port = ntohs(addr->ipxaddr.sa_socket);
  1868. PortNumberToEndpoint(port, *pPort);
  1869. }
  1870. // Update the local address cache
  1871. //
  1872. // Since there is only one addess no lock is required.
  1873. //
  1874. memcpy(IpxAddr.sa_netnum, addr->ipxaddr.sa_netnum, sizeof(IpxAddr.sa_netnum));
  1875. memcpy(IpxAddr.sa_nodenum, addr->ipxaddr.sa_nodenum, sizeof(IpxAddr.sa_nodenum));
  1876. fIpxAddrValid = TRUE;
  1877. //
  1878. // Figure out our server's raw IPX address.
  1879. //
  1880. status = IPX_BuildAddressVector(ppNetworkAddressVector);
  1881. if (status != RPC_S_OK)
  1882. {
  1883. DG_ServerAbortListen(ThisEndpoint);
  1884. delete *pPort;
  1885. return(status);
  1886. }
  1887. pEndpoint->pAddressVector = *ppNetworkAddressVector;
  1888. return RPC_S_OK;
  1889. }
  1890. RPC_STATUS
  1891. IPX_QueryEndpoint
  1892. (
  1893. IN void * pOriginalEndpoint,
  1894. OUT RPC_CHAR * pClientEndpoint
  1895. )
  1896. {
  1897. WS_SOCKADDR * pSockAddr = (WS_SOCKADDR *)pOriginalEndpoint;
  1898. unsigned NativeSocket = ntohs(pSockAddr->ipxaddr.sa_socket);
  1899. char AnsiBuffer[IPX_MAXIMUM_ENDPOINT];
  1900. char * pAnsi = AnsiBuffer;
  1901. RPC_CHAR * pUni = pClientEndpoint;
  1902. //
  1903. // Convert endpoint to an ASCII string, and thence to Unicode.
  1904. //
  1905. _ultoa(NativeSocket, AnsiBuffer, 10);
  1906. while ( *pUni++ = *pAnsi++ );
  1907. return RPC_S_OK;
  1908. }
  1909. RPC_STATUS
  1910. IPX_QueryAddress
  1911. (
  1912. IN void * pOriginalEndpoint,
  1913. OUT RPC_CHAR * pString
  1914. )
  1915. {
  1916. WS_SOCKADDR *pSockAddr = (WS_SOCKADDR *) pOriginalEndpoint;
  1917. IPX_AddressToName(&pSockAddr->ipxaddr, pString);
  1918. return RPC_S_OK;
  1919. }
  1920. RPC_STATUS
  1921. RPC_ENTRY
  1922. IPX_ClientInitializeAddress
  1923. (
  1924. OUT DG_TRANSPORT_ADDRESS Address,
  1925. IN RPC_CHAR *NetworkAddress,
  1926. IN RPC_CHAR *pPort,
  1927. IN BOOL fUseCache,
  1928. IN BOOL fBroadcast
  1929. )
  1930. /*++
  1931. Routine Description:
  1932. Initializes a address object for sending to a server.
  1933. Arguments:
  1934. Address - Storage for the address
  1935. NetworkAddress - The address of the server or 0 if local
  1936. pPort - The endpoint of the server
  1937. fUseCache - If TRUE then the transport may use a cached
  1938. value from a previous call on the same NetworkAddress.
  1939. fBroadcast - If TRUE, NetworkAddress is ignored and a broadcast
  1940. address is used.
  1941. Return Value:
  1942. RPC_S_OK - Success, name resolved and, optionally, added to cache.
  1943. RPC_P_FOUND_IN_CACHE - Success, returned only if fUseCache is TRUE
  1944. and the was name found in local cache.
  1945. RPC_P_MATCHED_CACHE - Partial success, fUseCache is FALSE and the
  1946. result of the lookup was the same as the value previously
  1947. in the cache.
  1948. RPC_S_OUT_OF_MEMORY
  1949. RPC_S_OUT_OF_RESOURCES
  1950. RPC_S_INVALID_ENDPOINT_FORMAT
  1951. RPC_S_SERVER_UNAVAILABLE
  1952. --*/
  1953. {
  1954. WS_SOCKADDR *pAddr = (WS_SOCKADDR *)Address;
  1955. RPC_STATUS status = RPC_S_OK;
  1956. USHORT port = 0;
  1957. pAddr->ipxaddr.sa_family = AF_IPX;
  1958. pAddr->ipxaddr.sa_socket = 0;
  1959. // Convert unicode endpoint to port number
  1960. status = EndpointToPortNumber(pPort, port);
  1961. if (status != RPC_S_OK)
  1962. {
  1963. return(status);
  1964. }
  1965. //
  1966. // Convert unicode network address to ipx address
  1967. //
  1968. if (FALSE == fBroadcast)
  1969. {
  1970. status = IPX_NameToAddress(NetworkAddress,
  1971. fUseCache,
  1972. &pAddr->ipxaddr
  1973. );
  1974. }
  1975. else
  1976. {
  1977. memset(pAddr->ipxaddr.sa_netnum, 0, sizeof(pAddr->ipxaddr.sa_netnum));
  1978. memset(pAddr->ipxaddr.sa_nodenum, 0xFF, sizeof(pAddr->ipxaddr.sa_nodenum));
  1979. }
  1980. pAddr->ipxaddr.sa_socket = htons(port);
  1981. return(status);
  1982. }
  1983. RPC_STATUS
  1984. RPC_ENTRY
  1985. IPX_ClientOpenEndpoint(
  1986. OUT DG_TRANSPORT_ENDPOINT ThisEndpoint,
  1987. IN BOOL fAsync,
  1988. DWORD Flags
  1989. )
  1990. {
  1991. RPC_STATUS status;
  1992. PWS_DATAGRAM_ENDPOINT pEndpoint = (PWS_DATAGRAM_ENDPOINT)ThisEndpoint;
  1993. WS_SOCKADDR *addr = &pEndpoint->ListenAddr;
  1994. // We don't care what port we bind to, we also don't care what
  1995. // port we bind to.
  1996. memset(addr, 0, sizeof(*addr));
  1997. addr->ipxaddr.sa_family = AF_IPX;
  1998. pEndpoint->id = IPX;
  1999. pEndpoint->fClient = TRUE ;
  2000. pEndpoint->fAsync = fAsync;
  2001. pEndpoint->EndpointFlags = 0;
  2002. pEndpoint->fDynamicEndpoint = 1;
  2003. status = DG_CreateEndpoint(pEndpoint);
  2004. if (status == RPC_S_OK)
  2005. {
  2006. // Update cache
  2007. memcpy(IpxAddr.sa_netnum, addr->ipxaddr.sa_netnum, sizeof(IpxAddr.sa_netnum));
  2008. memcpy(IpxAddr.sa_nodenum, addr->ipxaddr.sa_nodenum, sizeof(IpxAddr.sa_nodenum));
  2009. fIpxAddrValid = TRUE;
  2010. }
  2011. return(status);
  2012. }
  2013. RPC_STATUS
  2014. IPX_InitializeSockAddr(
  2015. IN char *Endpoint,
  2016. OUT WS_SOCKADDR *pSockAddr
  2017. )
  2018. /*++
  2019. Routine Description:
  2020. Initialized the sockaddr to be a loopback address to the
  2021. endpoint specified. Used to forward packets locally.
  2022. Arguments:
  2023. Endpoint - The string value of the servers endpoint.
  2024. pSockAddr - The sockaddr to fill in.
  2025. Return Value:
  2026. RPC_S_INVALID_ENDPOINT_FORMAT
  2027. RPC_S_OK
  2028. --*/
  2029. {
  2030. int len;
  2031. long port;
  2032. pSockAddr->generic.sa_family = AF_IPX;
  2033. port = atol(Endpoint);
  2034. if (port <= 0 || port > 0xFFFF)
  2035. {
  2036. return(RPC_S_INVALID_ENDPOINT_FORMAT);
  2037. }
  2038. pSockAddr->ipxaddr.sa_socket = htons((USHORT)port);
  2039. //
  2040. // In order to get this far this server must have
  2041. // alrady listened to IPX.
  2042. //
  2043. ASSERT(fIpxAddrValid);
  2044. memcpy(pSockAddr->ipxaddr.sa_netnum, IpxAddr.sa_netnum, sizeof(pSockAddr->ipxaddr.sa_netnum));
  2045. memcpy(pSockAddr->ipxaddr.sa_nodenum, IpxAddr.sa_nodenum, sizeof(pSockAddr->ipxaddr.sa_nodenum));
  2046. return(RPC_S_OK);
  2047. }
  2048. RPC_STATUS
  2049. RPC_ENTRY
  2050. IPX_GetEndpointStats(
  2051. IN DG_TRANSPORT_ENDPOINT ThisEndpoint,
  2052. OUT DG_ENDPOINT_STATS * pStats
  2053. )
  2054. {
  2055. RPC_STATUS Status;
  2056. Status = DG_GetEndpointStats(ThisEndpoint, pStats);
  2057. if (Status)
  2058. {
  2059. pStats->MaxPduSize = 1478;
  2060. pStats->MaxPacketSize = 1478;
  2061. pStats->PreferredPduSize = 1478;
  2062. pStats->ReceiveBufferSize= 8192;
  2063. return Status;
  2064. }
  2065. pStats->MaxPacketSize = 1478;
  2066. pStats->PreferredPduSize = pStats->MaxPduSize;
  2067. pStats->MaxPacketSize = pStats->MaxPduSize;
  2068. return RPC_S_OK;
  2069. }
  2070. #endif
  2071. ////////////////////////////////////////////////////////////////////////
  2072. //
  2073. // Transport interface structures and loader
  2074. //
  2075. const RPC_DATAGRAM_TRANSPORT
  2076. UDP_TransportInterface = {
  2077. RPC_TRANSPORT_INTERFACE_VERSION,
  2078. UDP_TOWER_ID,
  2079. IP_ADDRESS_ID,
  2080. RPC_STRING_LITERAL("ncadg_ip_udp"),
  2081. "135",
  2082. COMMON_ProcessCalls,
  2083. COMMON_StartPnpNotifications,
  2084. COMMON_ListenForPNPNotifications,
  2085. COMMON_TowerConstruct,
  2086. COMMON_TowerExplode,
  2087. COMMON_PostRuntimeEvent,
  2088. TRUE,
  2089. sizeof(WS_DATAGRAM_ENDPOINT),
  2090. sizeof(WS_DATAGRAM_ENDPOINT),
  2091. sizeof(WS_SOCKADDR),
  2092. IP_MAXIMUM_ENDPOINT,
  2093. IP_MAXIMUM_PRETTY_NAME,
  2094. 1024,
  2095. 1472,
  2096. DG_SendPacket,
  2097. UDP_ClientOpenEndpoint,
  2098. UDP_ClientInitializeAddress,
  2099. DG_ClientCloseEndpoint,
  2100. DG_ReceivePacket,
  2101. UDP_ServerListen,
  2102. DG_ServerAbortListen,
  2103. COMMON_ServerCompleteListen,
  2104. DG_ForwardPacket,
  2105. UDP_QueryAddress,
  2106. UDP_QueryEndpoint,
  2107. UDP_GetEndpointStats,
  2108. FALSE, // fIsMessageTransport (TRUE/FALSE).
  2109. 0, // OptionSize
  2110. 0, // InitOptions()
  2111. 0, // SetOption()
  2112. 0, // InqOption()
  2113. 0, // ImplementOptions()
  2114. 0, // AllowReceives()
  2115. 0 // InquireAuthClient()
  2116. };
  2117. const RPC_DATAGRAM_TRANSPORT
  2118. CDP_TransportInterface = {
  2119. RPC_TRANSPORT_INTERFACE_VERSION,
  2120. CDP_TOWER_ID,
  2121. IP_ADDRESS_ID,
  2122. RPC_STRING_LITERAL("ncadg_cluster"),
  2123. NULL,
  2124. COMMON_ProcessCalls,
  2125. COMMON_StartPnpNotifications,
  2126. COMMON_ListenForPNPNotifications,
  2127. COMMON_TowerConstruct,
  2128. COMMON_TowerExplode,
  2129. COMMON_PostRuntimeEvent,
  2130. TRUE,
  2131. sizeof(WS_DATAGRAM_ENDPOINT),
  2132. sizeof(WS_DATAGRAM_ENDPOINT),
  2133. sizeof(WS_SOCKADDR),
  2134. IP_MAXIMUM_ENDPOINT,
  2135. IP_MAXIMUM_PRETTY_NAME,
  2136. 1024,
  2137. 1452,
  2138. DG_SendPacket,
  2139. CDP_ClientOpenEndpoint,
  2140. CDP_ClientInitializeAddress,
  2141. DG_ClientCloseEndpoint,
  2142. DG_ReceivePacket,
  2143. CDP_ServerListen,
  2144. DG_ServerAbortListen,
  2145. COMMON_ServerCompleteListen,
  2146. DG_ForwardPacket,
  2147. CDP_QueryAddress,
  2148. CDP_QueryEndpoint,
  2149. CDP_GetEndpointStats,
  2150. FALSE, // fIsMessageTransport (TRUE/FALSE).
  2151. 0, // OptionSize
  2152. 0, // InitOptions()
  2153. 0, // SetOption()
  2154. 0, // InqOption()
  2155. 0, // ImplementOptions()
  2156. 0, // AllowReceives()
  2157. 0 // InquireAuthClient()
  2158. };
  2159. #ifdef IPX_ON
  2160. const RPC_DATAGRAM_TRANSPORT
  2161. IPX_TransportInterface = {
  2162. RPC_TRANSPORT_INTERFACE_VERSION,
  2163. IPX_TOWER_ID,
  2164. IPX_ADDRESS_ID,
  2165. RPC_STRING_LITERAL("ncadg_ipx"),
  2166. "34280",
  2167. COMMON_ProcessCalls,
  2168. COMMON_StartPnpNotifications,
  2169. COMMON_ListenForPNPNotifications,
  2170. COMMON_TowerConstruct,
  2171. COMMON_TowerExplode,
  2172. COMMON_PostRuntimeEvent,
  2173. TRUE,
  2174. sizeof(WS_DATAGRAM_ENDPOINT),
  2175. sizeof(WS_DATAGRAM_ENDPOINT),
  2176. sizeof(WS_SOCKADDR),
  2177. IPX_MAXIMUM_ENDPOINT,
  2178. IPX_MAXIMUM_PRETTY_NAME,
  2179. 1024,
  2180. 1464,
  2181. DG_SendPacket,
  2182. IPX_ClientOpenEndpoint,
  2183. IPX_ClientInitializeAddress,
  2184. DG_ClientCloseEndpoint,
  2185. DG_ReceivePacket,
  2186. IPX_ServerListen,
  2187. DG_ServerAbortListen,
  2188. COMMON_ServerCompleteListen,
  2189. DG_ForwardPacket,
  2190. IPX_QueryAddress,
  2191. IPX_QueryEndpoint,
  2192. IPX_GetEndpointStats,
  2193. FALSE, // fIsMessageTransport (TRUE/FALSE).
  2194. 0, // OptionSize
  2195. 0, // InitOptions()
  2196. 0, // SetOption()
  2197. 0, // InqOption()
  2198. 0, // ImplementOptions()
  2199. 0, // AllowReceives()
  2200. 0 // InquireAuthClient()
  2201. };
  2202. #endif
  2203. #ifdef NCADG_MQ_ON
  2204. const RPC_DATAGRAM_TRANSPORT
  2205. MQ_TransportInterface = {
  2206. RPC_TRANSPORT_INTERFACE_VERSION,
  2207. MQ_TOWER_ID,
  2208. MQ_ADDRESS_ID,
  2209. RPC_STRING_LITERAL("ncadg_mq"),
  2210. "EpMapper",
  2211. COMMON_ProcessCalls,
  2212. COMMON_StartPnpNotifications,
  2213. COMMON_ListenForPNPNotifications,
  2214. COMMON_TowerConstruct,
  2215. COMMON_TowerExplode,
  2216. COMMON_PostRuntimeEvent,
  2217. TRUE,
  2218. sizeof(MQ_DATAGRAM_ENDPOINT),
  2219. sizeof(MQ_DATAGRAM_ENDPOINT),
  2220. sizeof(MQ_ADDRESS),
  2221. MQ_MAXIMUM_ENDPOINT,
  2222. MQ_MAXIMUM_PRETTY_NAME,
  2223. MQ_MAX_PDU_SIZE,
  2224. MQ_PREFERRED_PDU_SIZE,
  2225. MQ_SendPacket,
  2226. MQ_ClientOpenEndpoint,
  2227. MQ_ClientInitializeAddress,
  2228. MQ_ClientCloseEndpoint,
  2229. MQ_ReceivePacket,
  2230. MQ_ServerListen,
  2231. MQ_ServerAbortListen,
  2232. COMMON_ServerCompleteListen,
  2233. MQ_ForwardPacket,
  2234. MQ_QueryAddress,
  2235. MQ_QueryEndpoint,
  2236. MQ_GetEndpointStats,
  2237. TRUE, // fIsMessageTransport (TRUE/FALSE).
  2238. sizeof(MQ_OPTIONS), // OptionSize
  2239. MQ_InitOptions, // InitOptions()
  2240. MQ_SetOption, // SetOption()
  2241. MQ_InqOption, // InqOption()
  2242. MQ_ImplementOptions, // ImplementOptions()
  2243. MQ_AllowReceives, // AllowReceives()
  2244. MQ_InquireAuthClient
  2245. };
  2246. #endif // NCADG_MQ_ON
  2247. const RPC_DATAGRAM_TRANSPORT *
  2248. DG_TransportLoad (
  2249. IN PROTOCOL_ID index
  2250. )
  2251. /*++
  2252. Routine Description:
  2253. Loads a datagram protocol and returns the transport interface
  2254. information for the protocol.
  2255. Arguments:
  2256. index - the PROTOCOL_ID value of the protocol to load.
  2257. Return Value:
  2258. 0 - failure
  2259. !0 - success
  2260. --*/
  2261. {
  2262. const RPC_DATAGRAM_TRANSPORT *pInfo;
  2263. if (fWinsockLoaded == FALSE)
  2264. {
  2265. if (RPC_WSAStartup() == FALSE)
  2266. {
  2267. return(0);
  2268. }
  2269. fWinsockLoaded = TRUE;
  2270. }
  2271. switch(index)
  2272. {
  2273. case UDP:
  2274. pInfo = &UDP_TransportInterface;
  2275. break;
  2276. #ifdef IPX_ON
  2277. case IPX:
  2278. if (InitializeIpxNameCache() != RPC_S_OK)
  2279. {
  2280. pInfo = 0;
  2281. }
  2282. else
  2283. {
  2284. pInfo = &IPX_TransportInterface;
  2285. }
  2286. break;
  2287. #endif
  2288. #ifdef NCADG_MQ_ON
  2289. case MSMQ:
  2290. if (!MQ_Initialize())
  2291. {
  2292. return(0);
  2293. }
  2294. else
  2295. {
  2296. pInfo = &MQ_TransportInterface;
  2297. }
  2298. break;
  2299. #endif // NCADG_MQ_ON
  2300. case CDP:
  2301. pInfo = &CDP_TransportInterface;
  2302. break;
  2303. default:
  2304. TransDbgPrint((DPFLTR_RPCPROXY_ID,
  2305. DPFLTR_WARNING_LEVEL,
  2306. RPCTRANS "DG_TransportLoad called with index: %d\n",
  2307. index));
  2308. ASSERT(0);
  2309. pInfo = 0;
  2310. break;
  2311. }
  2312. // PERFBUG: Add code to lookup real PDU sizes
  2313. return(pInfo);
  2314. }