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.

534 lines
11 KiB

  1. #include "stdafx.h"
  2. VOID
  3. IcqWriteCompletionRoutine
  4. (
  5. ULONG ErrorCode,
  6. ULONG BytesTransferred,
  7. PNH_BUFFER Bufferp
  8. )
  9. /*++
  10. Routine Description:
  11. A generic Write Completion Routine.
  12. Arguments:
  13. none.
  14. Return Value:
  15. --*/
  16. {
  17. PCNhSock Socketp = Bufferp->Socketp;
  18. ICQ_TRC(TM_IO, TL_DUMP, ("-- WRITE COMPLETION ROUTINE --"));
  19. NhReleaseBuffer(Bufferp);
  20. //Dereference Interface;
  21. DEREF_COMPONENT( Socketp, eRefIoWrite);
  22. DEREF_COMPONENT( g_IcqComponentReferencep, eRefIoWrite);
  23. }
  24. VOID
  25. ReadServerCompletionRoutine
  26. (
  27. ULONG ErrorCode,
  28. ULONG BytesTransferred,
  29. PNH_BUFFER Bufferp
  30. )
  31. /*++
  32. Routine Description:
  33. Reads the Server Responses to Client packets
  34. and then multiplexes this responses to the approprate client
  35. which was given in the Context information
  36. Arguments:
  37. none.
  38. Return Value:
  39. --*/
  40. {
  41. PICQ_CLIENT IcqClientp = NULL;
  42. PCNhSock Socketp = NULL;
  43. ULONG Error = NO_ERROR;
  44. ICQ_TRC(TM_IO, TL_ERROR, (" "));
  45. PROFILER(TM_IO, TL_TRACE, ("> UDP-SERVER - READ "));
  46. if(Bufferp is NULL) return;
  47. //
  48. // Get the Client Information
  49. Socketp = Bufferp->Socketp;
  50. IcqClientp = (PICQ_CLIENT)Bufferp->Context;
  51. ASSERT(IcqClientp != NULL);
  52. if(ErrorCode)
  53. {
  54. // Re-issue a read operation if it is not critical Error.
  55. ICQ_TRC(TM_IO, TL_ERROR,
  56. ("** !! READ ERROR in Server -> Client (UDP) !! **"));
  57. NhReleaseBuffer(Bufferp);
  58. //ErrorOut();
  59. }
  60. else // we need the context information
  61. {
  62. //
  63. // reissue read first
  64. //
  65. REF_COMPONENT( IcqClientp, eRefIoRead );
  66. Error = Socketp->NhReadDatagramSocket(g_IcqComponentReferencep,
  67. NULL,
  68. ReadServerCompletionRoutine,
  69. IcqClientp,
  70. NULL);
  71. if(Error)
  72. {
  73. ICQ_TRC(TM_IO, TL_ERROR, ("READ ERROR"));
  74. DEREF_COMPONENT( IcqClientp, eRefIoRead );
  75. }
  76. // multiplex the data
  77. Error = IcqClientp->ServerRead(Bufferp, 0, 0);
  78. if(Error)
  79. {
  80. NhReleaseBuffer( Bufferp );
  81. }
  82. }
  83. // DEREFERENCING
  84. DEREF_COMPONENT( Socketp, eRefIoRead );
  85. DEREF_COMPONENT( g_IcqComponentReferencep, eRefIoRead );
  86. DEREF_COMPONENT( IcqClientp, eRefIoRead );
  87. }
  88. VOID
  89. IcqReadClientUdpCompletionRoutine
  90. (
  91. ULONG ErrorCode,
  92. ULONG BytesTransferred,
  93. PNH_BUFFER Bufferp
  94. )
  95. /*++
  96. Routine Description:
  97. This is the READ - DISPATCHER for the UDP
  98. Clients sending packets to the server will be caught here..
  99. it will dispatch the packet to the appropriate ICQ client objects READER
  100. Arguments:
  101. none.
  102. Return Value:
  103. --*/
  104. {
  105. PCNhSock Socketp = Bufferp->Socketp;
  106. ULONG Error = NO_ERROR;
  107. // HANDLE DynamicRedirectHandle = (HANDLE) Bufferp->Context;
  108. ICQ_TRC(TM_IO, TL_ERROR, (" "));
  109. PROFILER(TM_IO, TL_TRACE,("UDP-CLIEN-READ DATA CAME from %s e(%hu)",
  110. INET_NTOA(Bufferp->ReadAddress.sin_addr.S_un.S_addr), ErrorCode));
  111. do
  112. {
  113. if(ErrorCode)
  114. {
  115. // Re-issue a read operation if it is not critical Error.
  116. ICQ_TRC(TM_IO, TL_ERROR,
  117. ("** !! CLIENT~SERVER READ ERROR - Deletin all the Client Entries"));
  118. Error = g_IcqPrxp->ReadFromClientCompletionRoutine(ErrorCode,
  119. BytesTransferred,
  120. Bufferp);
  121. break;
  122. }
  123. else
  124. {
  125. // Re-issue a read operation.
  126. Error = Socketp->NhReadDatagramSocket(g_IcqComponentReferencep,
  127. NULL,
  128. IcqReadClientUdpCompletionRoutine,
  129. NULL,// DynamicRedirectHandle,
  130. NULL);
  131. if(Error)
  132. {
  133. ICQ_TRC(TM_IO, TL_ERROR,("TREAD !!> REISsUE READ ERROR "));
  134. break;
  135. }
  136. g_IcqPrxp->ReadFromClientCompletionRoutine(ErrorCode,
  137. BytesTransferred,
  138. Bufferp);
  139. } // else for if (ErrorCode)
  140. } while ( FALSE );
  141. DEREF_COMPONENT( Socketp, eRefIoRead);
  142. DEREF_COMPONENT( g_IcqComponentReferencep, eRefIoRead);
  143. }
  144. //
  145. // CLIENT - RELATED IO COMPLETION ROUTINES
  146. //
  147. VOID
  148. IcqPeerConnectionCompletionRoutine
  149. (
  150. ULONG ErrorCode,
  151. ULONG BytesTransferred,
  152. PNH_BUFFER Bufferp
  153. )
  154. /*++
  155. Routine Description:
  156. Arguments:
  157. none.
  158. Return Value:
  159. --*/
  160. {
  161. PCNhSock Socketp = NULL;
  162. PICQ_PEER IcqPeerp = NULL;
  163. ULONG Error;
  164. // ASSERT(Bufferp);
  165. Socketp = Bufferp->Socketp;
  166. IcqPeerp = reinterpret_cast<PICQ_PEER>(Bufferp->Context);
  167. // IcqClientp = reinterpret_cast<PICQ_CLIENT>(Bufferp->Context2);
  168. ICQ_TRC(TM_IO, TL_TRACE, ("> IcqPeerConnectionCompletionRoutine"));
  169. // ASSERT on all of them.
  170. do
  171. {
  172. if(ErrorCode)
  173. {
  174. // Do - we need it?
  175. // Delete the Shadow mapping
  176. //
  177. // if can't connect then Delete the existing Socket to which we have
  178. // established connection.
  179. //
  180. if(IcqPeerp->MappingDirection is IcqFlagOutgoing)
  181. {
  182. STOP_COMPONENT(IcqPeerp->ToClientSocketp);
  183. //DEREFERENCE_COMPONENT(IcqPeerp->ToClientSocketp);
  184. }
  185. else // incoming
  186. {
  187. STOP_COMPONENT(IcqPeerp->ToPeerSocketp);
  188. //DEREFERENCE_COMPONENT(IcqPeerp->ToClientSocketp);
  189. }
  190. break;
  191. }
  192. else
  193. {
  194. ULONG srcIp;
  195. USHORT srcPort;
  196. Socketp->NhQueryRemoteEndpointSocket(&srcIp, &srcPort);
  197. //
  198. //
  199. ICQ_TRC(TM_IO, TL_TRACE,
  200. ("Connected to - Remote end is at %s:%hu",
  201. INET_NTOA(srcIp), htons(srcPort)));
  202. //
  203. // Activate The Connection
  204. // Issue the Read Operations on both Sockets with the Peer as their
  205. // Context
  206. //
  207. if(IcqPeerp->MappingDirection is IcqFlagOutgoing)
  208. {
  209. IcqPeerp->ToPeerSocketp = Socketp;
  210. ICQ_TRC(TM_MSG, TL_DUMP,
  211. ("Mapping is outGoing.. this socket is to the peer"));
  212. }
  213. else
  214. {
  215. IcqPeerp->ToClientSocketp = Socketp;
  216. }
  217. ICQ_TRC(TM_IO, TL_TRACE, ("Issuing firstRead"));
  218. Socketp = IcqPeerp->ToClientSocketp;
  219. REF_COMPONENT( IcqPeerp, eRefIoRead );
  220. Error = Socketp->NhReadStreamSocket
  221. (
  222. g_IcqComponentReferencep,
  223. NULL,
  224. ICQ_BUFFER_SIZE, // we should give a size
  225. 0,
  226. IcqPeerReadCompletionRoutine,
  227. IcqPeerp,
  228. NULL
  229. );
  230. if(Error)
  231. {
  232. ErrorOut();
  233. ICQ_TRC(TM_MSG, TL_ERROR, (" ERROR - Read failed on ToClient Sock"));
  234. DEREF_COMPONENT( IcqPeerp, eRefIoRead );
  235. break;
  236. }
  237. //
  238. // The other read operation
  239. //
  240. ICQ_TRC(TM_IO, TL_TRACE, ("Issuing second Read"));
  241. Socketp = IcqPeerp->ToPeerSocketp;
  242. REF_COMPONENT( IcqPeerp, eRefIoRead );
  243. Error = Socketp->NhReadStreamSocket
  244. (
  245. g_IcqComponentReferencep,
  246. NULL,
  247. ICQ_BUFFER_SIZE,
  248. 0,
  249. IcqPeerReadCompletionRoutine,
  250. IcqPeerp,
  251. NULL
  252. );
  253. if(Error)
  254. {
  255. ErrorOut();
  256. ICQ_TRC(TM_MSG, TL_ERROR, (" ERROR - Read failed on ToPeer Sock"));
  257. STOP_COMPONENT(IcqPeerp->ToClientSocketp); // reference will be cleared later
  258. DEREF_COMPONENT( IcqPeerp, eRefIoRead);
  259. DEREF_COMPONENT( IcqPeerp, eRefIoRead );
  260. break;
  261. }
  262. //
  263. // Reference the ICQ_PEER twice due to two read issues
  264. IcqPeerp->bActivated = TRUE;
  265. }
  266. } while (FALSE);
  267. DEREF_COMPONENT( Bufferp->Socketp, eRefIoConnect );
  268. DEREF_COMPONENT( IcqPeerp, eRefIoConnect);
  269. DEREF_COMPONENT( g_IcqComponentReferencep, eRefIoConnect );
  270. }
  271. VOID
  272. IcqPeerReadCompletionRoutine
  273. (
  274. ULONG ErrorCode,
  275. ULONG BytesTransferred,
  276. PNH_BUFFER Bufferp
  277. )
  278. /*++
  279. Routine Description:
  280. Reads TCP data between the peers. Within these flows there will be
  281. a place where the Data channel is negotiated.
  282. Arguments:
  283. none.
  284. Return Value:
  285. --*/
  286. {
  287. PICQ_PEER IcqPeerp = NULL;
  288. PCNhSock Socketp = NULL;
  289. PCNhSock OtherSocketp = NULL;
  290. ULONG Error = NO_ERROR;
  291. // ASSeRT(Bufferp);
  292. Socketp = Bufferp->Socketp;
  293. IcqPeerp = reinterpret_cast<PICQ_PEER>(Bufferp->Context);
  294. ICQ_TRC(TM_IO, TL_TRACE,
  295. ("> IcqPeerReadCompletionRoutine E:(%X) D:(%u)",
  296. ErrorCode, BytesTransferred));
  297. do
  298. {
  299. if( (ErrorCode != 0) ||
  300. ((ErrorCode== 0) && (BytesTransferred == 0))
  301. )
  302. {
  303. ICQ_TRC(TM_IO, TL_ERROR, ("Stoppping the Peer SESSION"));
  304. IcqPeerp->EndPeerSessionForClient(Socketp);
  305. break;
  306. }
  307. else
  308. {
  309. if(BytesTransferred)
  310. {
  311. OtherSocketp = IcqPeerp->ToClientSocketp;
  312. ICQ_TRC(TM_IO, TL_TRACE,
  313. (" Regular Data from a Peer(%u)/Client(%s) Socket",
  314. IcqPeerp->PeerUIN, // Peer-UIN
  315. INET_NTOA(IcqPeerp->iKey1))); // Client-IP
  316. //
  317. // Determine which direction this Socket is from..
  318. //
  319. if(OtherSocketp is Socketp)
  320. {
  321. //
  322. // NOTE: if it is from the ToClientSocketp then Process it
  323. //
  324. IcqPeerp->ProcessOutgoingPeerMessage(Bufferp->Buffer,
  325. Bufferp->BytesTransferred);
  326. OtherSocketp = IcqPeerp->ToPeerSocketp;
  327. }
  328. // then forward it to the other side by simply writing it .
  329. Error = OtherSocketp->NhWriteStreamSocket(g_IcqComponentReferencep,
  330. Bufferp,
  331. BytesTransferred,
  332. 0,
  333. IcqWriteCompletionRoutine,
  334. NULL,
  335. NULL);
  336. if(Error)
  337. {
  338. ICQ_TRC(TM_MSG, TL_ERROR,
  339. ("Error - In writing to the other side"));
  340. }
  341. }
  342. //
  343. // Issue another Read operation
  344. //
  345. REF_COMPONENT( IcqPeerp, eRefIoRead );
  346. Error = Socketp->NhReadStreamSocket(g_IcqComponentReferencep,
  347. NULL,
  348. ICQ_BUFFER_SIZE,
  349. 0,
  350. IcqPeerReadCompletionRoutine,
  351. IcqPeerp,
  352. NULL);
  353. if(Error)
  354. {
  355. DEREF_COMPONENT( IcqPeerp, eRefIoRead);
  356. ICQ_TRC(TM_MSG, TL_ERROR, ("Peer connection no more??"));
  357. }
  358. }
  359. } while (FALSE);
  360. DEREF_COMPONENT( Socketp, eRefIoRead );
  361. DEREF_COMPONENT( IcqPeerp, eRefIoRead );
  362. DEREF_COMPONENT( g_IcqComponentReferencep, eRefIoRead );
  363. } // End of *IcqPeerReadCompletionRoutine*