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.

252 lines
6.0 KiB

  1. #include "stdafx.h"
  2. ULONG
  3. _ICQ_PEER::CreateShadowMappingPriorToConnection(PDispatchReply DispatchReplyp)
  4. /*++
  5. Routine Description:
  6. Creates the ShadowMapping According to the given values
  7. Arguments:
  8. none.
  9. Return Value:
  10. --*/
  11. {
  12. ULONG Error = NO_ERROR;
  13. ULONG DstIp = 0, NewDstIp = 0, SrcIp = 0, NewSrcIp = 0;
  14. USHORT DstPort = 0, NewDstPort = 0, SrcPort = 0, NewSrcPort = 0;
  15. ICQ_TRC(TM_MSG, TL_TRACE, (" Creating Shadow mapping "));
  16. //
  17. // Create Shadow mapping
  18. DstIp = NewDstIp = DispatchReplyp->dispatch.dstIp;
  19. DstPort = NewDstPort = DispatchReplyp->dispatch.dstPort;
  20. Error = g_IAlgServicesp->GetBestSourceAddressForDestinationAddress(DstIp,
  21. TRUE,
  22. &SrcIp);
  23. if( FAILED(Error) )
  24. {
  25. ASSERT(FALSE);
  26. }
  27. if(this->MappingDirection is IcqFlagIncoming)
  28. {
  29. this->ToClientSocketp->NhQueryLocalEndpointSocket
  30. (
  31. NULL,
  32. &SrcPort
  33. );
  34. this->ToPeerSocketp->NhQueryRemoteEndpointSocket
  35. (
  36. &NewSrcIp,
  37. &NewSrcPort
  38. );
  39. Error = g_IAlgServicesp->PrepareSourceModifiedProxyConnection(eALG_TCP,
  40. SrcIp,
  41. SrcPort,
  42. DstIp,
  43. DstPort,
  44. NewSrcIp,
  45. NewSrcPort,
  46. &ShadowRedirectp);
  47. }
  48. else // if outgoing
  49. {
  50. this->ToPeerSocketp ->NhQueryLocalEndpointSocket(NULL, &SrcPort);
  51. NewSrcPort = SrcPort;
  52. Error = g_IAlgServicesp->PrepareProxyConnection(eALG_TCP,
  53. SrcIp,
  54. SrcPort,
  55. DstIp,
  56. DstPort,
  57. FALSE,
  58. &ShadowRedirectp);
  59. }
  60. if( FAILED(Error) )
  61. {
  62. ICQ_TRC(TM_MSG, TL_ERROR,
  63. ("** !! ShadowRedirect has Failed"));
  64. }
  65. else
  66. {
  67. ICQ_TRC(TM_MSG, TL_INFO, ("** Peer Shadow - Mapping Report **"));
  68. ICQ_TRC(TM_MSG, TL_INFO, (" DstIp-DstPort %s:%hu",
  69. INET_NTOA(DstIp), htons(DstPort)));
  70. ICQ_TRC(TM_MSG, TL_INFO, (" SrcIp-SrcPort %s:%hu",
  71. INET_NTOA(SrcIp), htons(SrcPort)));
  72. ICQ_TRC(TM_MSG, TL_INFO, (" NewDstIp-NewDstPort %s:%hu",
  73. INET_NTOA(NewDstIp), htons(NewDstPort)));
  74. ICQ_TRC(TM_MSG, TL_INFO, (" NewSrcIp-NewSrcPort %s:%hu",
  75. INET_NTOA(NewSrcIp), htons(NewSrcPort)));
  76. }
  77. return Error;
  78. }
  79. ULONG
  80. _ICQ_PEER::InitiatePeerConnection(PDispatchReply DispatchReplyp)
  81. /*++
  82. Routine Description:
  83. Should create the Shadow mapping and then try to fire the
  84. connection Request stuff.
  85. Arguments:
  86. none.
  87. Return Value:
  88. --*/
  89. {
  90. PCNhSock tempSocketp = NULL;
  91. PCNhSock * sockHolder;
  92. ULONG Error = NO_ERROR;
  93. ULONG DstIp=0;
  94. USHORT DstPort = 0;
  95. PICQ_PEER IcqPeerp = this;
  96. ICQ_TRC(TM_MSG, TL_TRACE, (" > Initiate Peer CONNECTION"));
  97. NEW_OBJECT(tempSocketp, CNhSock);
  98. //tempSocketp = new CNhSock();
  99. if(tempSocketp is NULL)
  100. {
  101. return E_FAIL;
  102. }
  103. //
  104. // Create Socket but first get the best IP address for this.
  105. Error =
  106. g_IAlgServicesp->GetBestSourceAddressForDestinationAddress(DispatchReplyp->dispatch.dstIp,
  107. FALSE,
  108. &DstIp);
  109. if ( FAILED(DstIp) )
  110. {
  111. ICQ_TRC(TM_MSG, TL_ERROR, ("Error Can't get the best source addr"));
  112. DstIp = g_MyPublicIp;
  113. }
  114. Error = tempSocketp->NhCreateStreamSocket(DstIp,
  115. 0,
  116. NULL);
  117. if ( Error )
  118. {
  119. ICQ_TRC(TM_MSG, TL_ERROR, ("Error - Connection Socket can't be created"));
  120. DEREF_COMPONENT( tempSocketp, eRefInitialization );
  121. return E_INVALIDARG;
  122. }
  123. //
  124. //
  125. //
  126. if ( this->MappingDirection is IcqFlagOutgoing )
  127. {
  128. this->ToPeerSocketp = tempSocketp;
  129. sockHolder = &this->ToPeerSocketp ;
  130. }
  131. else
  132. {
  133. this->ToClientSocketp = tempSocketp;
  134. sockHolder = & this->ToClientSocketp ;
  135. }
  136. DstIp = DispatchReplyp->dispatch.dstIp;
  137. DstPort = DispatchReplyp->dispatch.dstPort;
  138. if( CreateShadowMappingPriorToConnection(DispatchReplyp) )
  139. {
  140. ICQ_TRC(TM_MSG, TL_ERROR, ("Error - Shadow Mapping Creation Error"));
  141. *sockHolder = NULL;
  142. DEREF_COMPONENT( tempSocketp, eRefInitialization );
  143. return E_INVALIDARG;
  144. }
  145. ICQ_TRC(TM_MSG, TL_TRACE, (" ISSUE CONNECTion to %s:%hu",
  146. INET_NTOA(DstIp), htons(DstPort)));
  147. //
  148. // Reference "this" due to the CONNECT
  149. //
  150. REF_COMPONENT( IcqPeerp, eRefIoConnect ); //this
  151. //
  152. // Call the Connection Request
  153. //
  154. Error = tempSocketp->NhConnectStreamSocket
  155. (
  156. g_IcqComponentReferencep,
  157. DstIp,
  158. DstPort,
  159. NULL,
  160. IcqPeerConnectionCompletionRoutine,
  161. NULL,
  162. IcqPeerp, //this
  163. NULL
  164. );
  165. if(Error)
  166. {
  167. ICQ_TRC(TM_MSG, TL_ERROR,
  168. ("** !! Error - Connect to Peer/Client has failed"));
  169. ErrorOut();
  170. this->ShadowRedirectp->Cancel();
  171. this->ShadowRedirectp->Release();
  172. this->ShadowRedirectp = NULL;
  173. DEREF_COMPONENT( IcqPeerp, eRefIoConnect ); //this
  174. STOP_COMPONENT( tempSocketp );
  175. DEREF_COMPONENT( tempSocketp, eRefInitialization );
  176. *sockHolder = NULL;
  177. return Error;
  178. }
  179. return Error;
  180. }