Leaked source code of windows server 2003
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.

2548 lines
72 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ras.cpp
  5. Abstract:
  6. The RAS client functionality (Transport/session mangement)
  7. Author:
  8. Nikhil Bobde (NikhilB)
  9. Revision History:
  10. --*/
  11. #include "globals.h"
  12. #include "q931obj.h"
  13. #include "line.h"
  14. #include "q931pdu.h"
  15. #include "ras.h"
  16. #define OID_ELEMENT_LAST(Value) { NULL, Value }
  17. #define OID_ELEMENT(Index,Value) { (ASN1objectidentifier_s *) &_OID_H225ProtocolIdentifierV1 [Index], Value },
  18. // this stores an unrolled constant linked list
  19. const ASN1objectidentifier_s _OID_H225ProtocolIdentifierV1 [] = {
  20. OID_ELEMENT (1, 0) // 0 = ITU-T
  21. OID_ELEMENT (2, 0) // 0 = Recommendation
  22. OID_ELEMENT (3, 8) // 8 = H Series
  23. OID_ELEMENT (4, 2250) // 2250 = H.225.0
  24. OID_ELEMENT (5, 0) // 0 = Version
  25. OID_ELEMENT_LAST (1) // 1 = V1
  26. };
  27. #undef OID_ELEMENT
  28. #define OID_ELEMENT(Index,Value) { (ASN1objectidentifier_s *) &_OID_H225ProtocolIdentifierV2 [Index], Value },
  29. // this stores an unrolled constant linked list
  30. const ASN1objectidentifier_s _OID_H225ProtocolIdentifierV2 [] = {
  31. OID_ELEMENT (1, 0) // 0 = ITU-T
  32. OID_ELEMENT (2, 0) // 0 = Recommendation
  33. OID_ELEMENT (3, 8) // 8 = H Series
  34. OID_ELEMENT (4, 2250) // 2250 = H.225.0
  35. OID_ELEMENT (5, 0) // 0 = Version
  36. OID_ELEMENT_LAST (2) // 2 = V2
  37. };
  38. RAS_CLIENT g_RasClient;
  39. static LONG RasSequenceNumber;
  40. PH323_ALIASNAMES
  41. RASGetRegisteredAliasList()
  42. {
  43. return g_RasClient.GetRegisteredAliasList();
  44. }
  45. HRESULT RasStart (void)
  46. {
  47. H323DBG(( DEBUG_LEVEL_TRACE, "RasStart - entered." ));
  48. if( (g_RegistrySettings.saGKAddr.sin_addr.s_addr == NULL) ||
  49. (g_RegistrySettings.saGKAddr.sin_addr.s_addr == INADDR_NONE )
  50. )
  51. {
  52. return E_FAIL;
  53. }
  54. if (g_RegistrySettings.fIsGKEnabled)
  55. {
  56. if( !g_RasClient.Initialize (&g_RegistrySettings.saGKAddr) )
  57. return E_OUTOFMEMORY;
  58. //send the rrq message
  59. if( !g_RasClient.SendRRQ( NOT_RESEND_SEQ_NUM, NULL ) )
  60. {
  61. H323DBG(( DEBUG_LEVEL_TRACE, "couldn't send rrq." ));
  62. //m_RegisterState = RAS_REGISTER_STATE_IDLE;
  63. return E_FAIL;
  64. }
  65. }
  66. H323DBG(( DEBUG_LEVEL_TRACE, "RasStart - exited." ));
  67. return S_OK;
  68. }
  69. void RasStop (void)
  70. {
  71. H323DBG(( DEBUG_LEVEL_TRACE, "RasStop - entered." ));
  72. DWORD dwState = g_RasClient.GetRegState();
  73. if( (dwState==RAS_REGISTER_STATE_REGISTERED) ||
  74. (dwState==RAS_REGISTER_STATE_RRQSENT) )
  75. {
  76. //send urq
  77. g_RasClient.SendURQ( NOT_RESEND_SEQ_NUM, NULL );
  78. }
  79. g_RasClient.Shutdown();
  80. H323DBG(( DEBUG_LEVEL_TRACE, "RasStop - exited." ));
  81. }
  82. BOOL RasIsRegistered (void)
  83. {
  84. return g_RasClient.GetRegState() == RAS_REGISTER_STATE_REGISTERED;
  85. }
  86. HRESULT RasGetLocalAddress (
  87. OUT SOCKADDR_IN * ReturnLocalAddress)
  88. {
  89. return g_RasClient.GetLocalAddress (ReturnLocalAddress);
  90. }
  91. USHORT RasAllocSequenceNumber (void)
  92. {
  93. USHORT SequenceNumber;
  94. H323DBG(( DEBUG_LEVEL_TRACE, "RasAllocSequenceNumber - entered." ));
  95. do
  96. {
  97. SequenceNumber = (USHORT) InterlockedIncrement (&RasSequenceNumber);
  98. } while( SequenceNumber == 0 );
  99. H323DBG(( DEBUG_LEVEL_TRACE, "RasAllocSequenceNumber - exited." ));
  100. return SequenceNumber;
  101. }
  102. HRESULT RasEncodeSendMessage (
  103. IN RasMessage * pRasMessage)
  104. {
  105. return g_RasClient.IssueSend( pRasMessage ) ? S_OK : E_FAIL;
  106. }
  107. HRESULT RasGetEndpointID (
  108. OUT EndpointIdentifier * ReturnEndpointID)
  109. {
  110. return g_RasClient.GetEndpointID (ReturnEndpointID);
  111. }
  112. void RasHandleRegistryChange()
  113. {
  114. g_RasClient.HandleRegistryChange();
  115. }
  116. RAS_CLIENT::RAS_CLIENT()
  117. {
  118. //create the timer queue
  119. m_hRegTimer = NULL;
  120. m_hRegTTLTimer = NULL;
  121. m_hUnRegTimer = NULL;
  122. m_RegisterState = RAS_REGISTER_STATE_IDLE;
  123. m_IoRefCount = 0;
  124. m_dwState = RAS_CLIENT_STATE_NONE;
  125. m_pAliasList = NULL;
  126. m_Socket = INVALID_SOCKET;
  127. InitializeListHead( &m_sendPendingList );
  128. InitializeListHead( &m_sendFreeList );
  129. InitializeListHead( &m_aliasChangeRequestList );
  130. m_dwSendFreeLen = 0;
  131. m_lastRegisterSeqNum = 0;
  132. m_wTTLSeqNumber = 0;
  133. m_UnRegisterSeqNum = 0;
  134. m_wRASSeqNum = 0;
  135. m_dwRegRetryCount = 0;
  136. m_dwUnRegRetryCount = 0;
  137. m_dwCallsInProgress = 0;
  138. m_dwRegTimeToLive = 0;
  139. m_pRRQExpireContext = NULL;
  140. m_pURQExpireContext = NULL;
  141. ZeroMemory( (PVOID)&m_GKAddress, sizeof(SOCKADDR_IN) );
  142. ZeroMemory( (PVOID)&m_ASNCoderInfo, sizeof(ASN1_CODER_INFO) );
  143. ZeroMemory( (PVOID)&m_PendingURQ, sizeof(PENDINGURQ) );
  144. ZeroMemory( (PVOID)&m_RASEndpointID, sizeof(ENDPOINT_ID) );
  145. // No need to check the result of this one since this object is
  146. // not allocated on heap, right when the DLL is loaded.
  147. InitializeCriticalSectionAndSpinCount( &m_CriticalSection, 0x80000000 );
  148. }
  149. RAS_CLIENT::~RAS_CLIENT (void)
  150. {
  151. //free the various lists
  152. FreeSendList( &m_sendFreeList );
  153. FreeSendList( &m_sendPendingList );
  154. FreeSendList( &m_aliasChangeRequestList );
  155. DeleteCriticalSection( &m_CriticalSection );
  156. }
  157. HRESULT
  158. RAS_CLIENT::GetEndpointID(
  159. OUT EndpointIdentifier * ReturnEndpointID )
  160. {
  161. HRESULT hr;
  162. H323DBG(( DEBUG_LEVEL_TRACE, "GetEndpointID - entered." ));
  163. Lock();
  164. if (m_RegisterState == RAS_REGISTER_STATE_REGISTERED)
  165. {
  166. ReturnEndpointID->length = m_RASEndpointID.length;
  167. //m_RASEndpointID.value is an array and not a pointer.
  168. //so explicit assignment of each field is required
  169. ReturnEndpointID->value = m_RASEndpointID.value;
  170. hr = S_OK;
  171. }
  172. else
  173. {
  174. hr = S_OK;
  175. }
  176. Unlock();
  177. H323DBG(( DEBUG_LEVEL_TRACE, "GetEndpointID - exited." ));
  178. return hr;
  179. }
  180. //the addr and port are in network byte order
  181. BOOL
  182. RAS_CLIENT::Initialize(
  183. IN SOCKADDR_IN* psaGKAddr
  184. )
  185. {
  186. DWORD dwSize;
  187. int rc;
  188. H323DBG(( DEBUG_LEVEL_TRACE, "RAS Initialize entered:%p.",this ));
  189. //set the m_GKAddress here
  190. dwSize = sizeof m_RASEndpointID.value;
  191. GetComputerNameW( m_RASEndpointID.value, &dwSize );
  192. m_RASEndpointID.length = (WORD)wcslen(m_RASEndpointID.value);
  193. m_pAliasList = new H323_ALIASNAMES;
  194. if( m_pAliasList == NULL )
  195. {
  196. goto error2;
  197. }
  198. ZeroMemory( (PVOID)m_pAliasList, sizeof(H323_ALIASNAMES) );
  199. if( g_RegistrySettings.fIsGKLogOnPhoneEnabled )
  200. {
  201. if(!AddAliasItem( m_pAliasList,
  202. g_RegistrySettings.wszGKLogOnPhone,
  203. e164_chosen ))
  204. {
  205. goto error3;
  206. }
  207. }
  208. if( g_RegistrySettings.fIsGKLogOnAccountEnabled )
  209. {
  210. if(!AddAliasItem( m_pAliasList,
  211. g_RegistrySettings.wszGKLogOnAccount,
  212. h323_ID_chosen ))
  213. {
  214. goto error3;
  215. }
  216. }
  217. if( m_pAliasList->wCount == 0 )
  218. {
  219. //add the machine name as he default alias
  220. if(!AddAliasItem( m_pAliasList,
  221. m_RASEndpointID.value,
  222. h323_ID_chosen ))
  223. {
  224. goto error3;
  225. }
  226. }
  227. rc = InitASNCoder();
  228. if( rc != ASN1_SUCCESS )
  229. {
  230. H323DBG((DEBUG_LEVEL_TRACE, "RAS_InitCoder() returned: %d ", rc));
  231. goto error3;
  232. }
  233. m_GKAddress = *psaGKAddr;
  234. if(!InitializeIo() )
  235. {
  236. goto error4;
  237. }
  238. H323DBG((DEBUG_LEVEL_TRACE, "GK addr:%lx.", m_GKAddress.sin_addr.s_addr ));
  239. m_dwState = RAS_CLIENT_STATE_INITIALIZED;
  240. m_RegisterState = RAS_REGISTER_STATE_IDLE;
  241. H323DBG(( DEBUG_LEVEL_TRACE, "RAS Initialize exited:%p.",this ));
  242. return TRUE;
  243. error4:
  244. TermASNCoder();
  245. error3:
  246. FreeAliasNames( m_pAliasList );
  247. m_pAliasList = NULL;
  248. error2:
  249. return FALSE;
  250. }
  251. void
  252. RAS_CLIENT::Shutdown(void)
  253. {
  254. H323DBG(( DEBUG_LEVEL_TRACE, "RAS Shutdown entered:%p.",this ));
  255. Lock();
  256. switch (m_dwState)
  257. {
  258. case RAS_CLIENT_STATE_NONE:
  259. // nothing to do
  260. break;
  261. case RAS_CLIENT_STATE_INITIALIZED:
  262. if( m_Socket != INVALID_SOCKET )
  263. {
  264. closesocket(m_Socket);
  265. m_Socket = INVALID_SOCKET;
  266. }
  267. //free alias list
  268. FreeAliasNames( m_pAliasList );
  269. m_pAliasList = NULL;
  270. TermASNCoder();
  271. m_dwState = RAS_CLIENT_STATE_NONE;
  272. //delete if any timers
  273. if( m_hRegTTLTimer )
  274. {
  275. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTTLTimer, NULL );
  276. m_hRegTTLTimer = NULL;
  277. }
  278. if( m_hUnRegTimer != NULL )
  279. {
  280. DeleteTimerQueueTimer( H323TimerQueue, m_hUnRegTimer, NULL );
  281. m_hUnRegTimer = NULL;
  282. m_dwUnRegRetryCount = 0;
  283. }
  284. if( m_hRegTimer != NULL )
  285. {
  286. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL );
  287. m_hRegTimer = NULL;
  288. }
  289. if( m_pRRQExpireContext != NULL )
  290. {
  291. delete m_pRRQExpireContext;
  292. m_pRRQExpireContext = NULL;
  293. }
  294. if( m_pURQExpireContext != NULL )
  295. {
  296. delete m_pURQExpireContext;
  297. m_pURQExpireContext = NULL;
  298. }
  299. m_RegisterState = RAS_REGISTER_STATE_IDLE;
  300. m_dwSendFreeLen = 0;
  301. m_dwRegRetryCount = 0;
  302. m_dwUnRegRetryCount = 0;
  303. m_dwCallsInProgress = 0;
  304. m_lastRegisterSeqNum = 0;
  305. m_wTTLSeqNumber = 0;
  306. m_UnRegisterSeqNum = 0;
  307. m_wRASSeqNum = 0;
  308. m_dwRegTimeToLive = 0;
  309. ZeroMemory( (PVOID)&m_GKAddress, sizeof(SOCKADDR_IN) );
  310. ZeroMemory( (PVOID)&m_ASNCoderInfo, sizeof(ASN1_CODER_INFO) );
  311. ZeroMemory( (PVOID)&m_PendingURQ, sizeof(PENDINGURQ) );
  312. ZeroMemory( (PVOID)&m_RASEndpointID, sizeof(ENDPOINT_ID) );
  313. break;
  314. default:
  315. _ASSERTE(FALSE);
  316. break;
  317. }
  318. Unlock();
  319. H323DBG(( DEBUG_LEVEL_TRACE, "RAS Shutdown exited:%p.",this ));
  320. }
  321. BOOL
  322. RAS_CLIENT::FreeSendList(
  323. PLIST_ENTRY pListHead
  324. )
  325. {
  326. PLIST_ENTRY pLE;
  327. RAS_SEND_CONTEXT * pSendContext;
  328. H323DBG(( DEBUG_LEVEL_ERROR, "FreeSendList entered." ));
  329. // process list until empty
  330. while( IsListEmpty(pListHead) == FALSE )
  331. {
  332. // retrieve first entry
  333. pLE = RemoveHeadList(pListHead);
  334. // convert list entry to structure pointer
  335. pSendContext = CONTAINING_RECORD( pLE, RAS_SEND_CONTEXT, ListEntry );
  336. // release memory
  337. if( pSendContext != NULL )
  338. {
  339. delete pSendContext;
  340. pSendContext = NULL;
  341. }
  342. }
  343. H323DBG(( DEBUG_LEVEL_ERROR, "FreeSendList exited:%p.", this ));
  344. // success
  345. return TRUE;
  346. }
  347. //!!always called from a lock
  348. RAS_SEND_CONTEXT *
  349. RAS_CLIENT::AllocSendBuffer(void)
  350. {
  351. RAS_SEND_CONTEXT *pSendBuf;
  352. H323DBG(( DEBUG_LEVEL_TRACE, "AllocSendBuffer entered:%p.",this ));
  353. if( m_dwSendFreeLen )
  354. {
  355. m_dwSendFreeLen--;
  356. _ASSERTE( IsListEmpty(&m_sendFreeList) == FALSE );
  357. LIST_ENTRY *pLE = RemoveHeadList( &m_sendFreeList );
  358. pSendBuf = CONTAINING_RECORD( pLE, RAS_SEND_CONTEXT, ListEntry );
  359. }
  360. else
  361. {
  362. pSendBuf = (RAS_SEND_CONTEXT*)new RAS_SEND_CONTEXT;
  363. }
  364. H323DBG(( DEBUG_LEVEL_TRACE, "AllocSendBuffer exited:%p.",this ));
  365. return pSendBuf;
  366. }
  367. //!!always called from a lock
  368. void
  369. RAS_CLIENT::FreeSendBuffer(
  370. IN RAS_SEND_CONTEXT * pBuffer
  371. )
  372. {
  373. H323DBG(( DEBUG_LEVEL_TRACE, "FreeSendBuffer entered:%p.",this ));
  374. if(m_dwSendFreeLen < RASIO_SEND_BUFFER_LIST_MAX )
  375. {
  376. m_dwSendFreeLen++;
  377. InsertHeadList( &m_sendFreeList, &pBuffer->ListEntry );
  378. }
  379. else
  380. {
  381. delete pBuffer;
  382. }
  383. H323DBG(( DEBUG_LEVEL_TRACE, "FreeSendBuffer exited:%p.",this ));
  384. }
  385. //RAS client functions
  386. void
  387. NTAPI RAS_CLIENT::RegExpiredCallback (
  388. IN PVOID ContextParameter, // ExpContext
  389. IN BOOLEAN TimerFired) // not used
  390. {
  391. EXPIRE_CONTEXT * pExpireContext;
  392. RAS_CLIENT * This;
  393. H323DBG(( DEBUG_LEVEL_TRACE, "RegExpiredCallback - entered." ));
  394. _ASSERTE( ContextParameter );
  395. pExpireContext = (EXPIRE_CONTEXT *)ContextParameter;
  396. _ASSERTE( m_pRRQExpireContext == pExpireContext );
  397. _ASSERTE(pExpireContext -> RasClient);
  398. This = (RAS_CLIENT *) pExpireContext -> RasClient;
  399. This -> RegExpired (pExpireContext -> seqNumber);
  400. H323DBG(( DEBUG_LEVEL_TRACE, "RegExpiredCallback - exited." ));
  401. delete pExpireContext;
  402. }
  403. void
  404. NTAPI RAS_CLIENT::UnregExpiredCallback(
  405. IN PVOID ContextParameter, // ExpContext
  406. IN BOOLEAN TimerFired) // not used
  407. {
  408. EXPIRE_CONTEXT * pExpireContext;
  409. RAS_CLIENT * This;
  410. H323DBG(( DEBUG_LEVEL_TRACE, "UnregExpiredCallback - entered." ));
  411. _ASSERTE(ContextParameter);
  412. pExpireContext = (EXPIRE_CONTEXT *) ContextParameter;
  413. _ASSERTE( m_pURQExpireContext == pExpireContext );
  414. _ASSERTE( pExpireContext -> RasClient );
  415. This = (RAS_CLIENT *) pExpireContext -> RasClient;
  416. This -> UnregExpired( pExpireContext -> seqNumber );
  417. H323DBG(( DEBUG_LEVEL_TRACE, "UnregExpiredCallback - exited." ));
  418. delete pExpireContext;
  419. }
  420. void
  421. NTAPI RAS_CLIENT::TTLExpiredCallback(
  422. IN PVOID ContextParameter, // ExpContext
  423. IN BOOLEAN TimerFired) // not used
  424. {
  425. RAS_CLIENT * This;
  426. _ASSERTE(ContextParameter);
  427. This = (RAS_CLIENT*)ContextParameter;
  428. _ASSERTE(This == &g_RasClient);
  429. This -> TTLExpired();
  430. }
  431. //If we have already sent RRQ to this GK then this RRQ is supposed to rplace
  432. //the original alias list with the new list
  433. BOOL
  434. RAS_CLIENT::SendRRQ(
  435. IN long seqNumber,
  436. IN PALIASCHANGE_REQUEST pAliasChangeRequest
  437. )
  438. {
  439. RasMessage rasMessage;
  440. RegistrationRequest * RRQ;
  441. SOCKADDR_IN sockAddr;
  442. EXPIRE_CONTEXT * pExpireContext = NULL;
  443. RegistrationRequest_callSignalAddress CallSignalAddressSequence;
  444. RegistrationRequest_rasAddress RasAddressSequence;
  445. H323DBG(( DEBUG_LEVEL_TRACE, "SendRRQ entered:%p.",this ));
  446. pExpireContext = new EXPIRE_CONTEXT;
  447. if( pExpireContext == NULL )
  448. {
  449. return FALSE;
  450. }
  451. Lock();
  452. // initialize the structure
  453. ZeroMemory (&rasMessage, sizeof rasMessage);
  454. rasMessage.choice = registrationRequest_chosen;
  455. RRQ = &rasMessage.u.registrationRequest;
  456. RRQ -> bit_mask = 0;
  457. RRQ -> protocolIdentifier = OID_H225ProtocolIdentifierV2;
  458. // get sequence number
  459. if( seqNumber != NOT_RESEND_SEQ_NUM )
  460. {
  461. RRQ -> requestSeqNum = (WORD)seqNumber;
  462. }
  463. else
  464. {
  465. RRQ -> requestSeqNum = GetNextSeqNum();
  466. if( pAliasChangeRequest == NULL )
  467. {
  468. m_lastRegisterSeqNum = RRQ -> requestSeqNum;
  469. }
  470. else
  471. {
  472. pAliasChangeRequest->wRequestSeqNum = RRQ -> requestSeqNum;
  473. }
  474. }
  475. H323DBG(( DEBUG_LEVEL_TRACE, "RRQ seqNum:%d.", RRQ -> requestSeqNum ));
  476. sockAddr.sin_family = AF_INET;
  477. sockAddr.sin_port =
  478. htons(LOWORD(g_RegistrySettings.dwQ931ListenPort));
  479. //we are listening for Q931 conns on all local interfaces
  480. //so specify just one of the local IP addresses
  481. sockAddr.sin_addr.s_addr = m_sockAddr.sin_addr.s_addr;
  482. SetTransportAddress( &sockAddr, &CallSignalAddressSequence.value);
  483. CallSignalAddressSequence.next = NULL;
  484. RRQ -> callSignalAddress = &CallSignalAddressSequence;
  485. // ras address. The UDP socket for this GK
  486. RasAddressSequence.next = NULL;
  487. RasAddressSequence.value = m_transportAddress;
  488. RRQ -> rasAddress = &RasAddressSequence;
  489. // fill in endpoint type
  490. RRQ -> terminalType.bit_mask |= terminal_present;
  491. RRQ -> terminalType.terminal.bit_mask = 0;
  492. //fill in terminal alias list
  493. if( pAliasChangeRequest && pAliasChangeRequest->pAliasList )
  494. {
  495. RRQ -> terminalAlias = (RegistrationRequest_terminalAlias *)
  496. SetMsgAddressAlias( pAliasChangeRequest->pAliasList );
  497. if (NULL == RRQ -> terminalAlias)
  498. {
  499. goto cleanup;
  500. }
  501. RRQ -> bit_mask |= RegistrationRequest_terminalAlias_present;
  502. RRQ -> bit_mask |= RegistrationRequest_endpointIdentifier_present;
  503. RRQ->endpointIdentifier.length = pAliasChangeRequest->rasEndpointID.length;
  504. RRQ->endpointIdentifier.value = pAliasChangeRequest->rasEndpointID.value;
  505. }
  506. else if( m_pAliasList && m_pAliasList->wCount )
  507. {
  508. RRQ -> terminalAlias = (RegistrationRequest_terminalAlias *)
  509. SetMsgAddressAlias( m_pAliasList );
  510. if (NULL == RRQ -> terminalAlias)
  511. {
  512. goto cleanup;
  513. }
  514. RRQ -> bit_mask |= RegistrationRequest_terminalAlias_present;
  515. }
  516. else
  517. {
  518. _ASSERTE(0);
  519. }
  520. //endpointVendor
  521. CopyVendorInfo( &(RRQ -> endpointVendor) );
  522. //a few random booleans
  523. RRQ -> discoveryComplete = FALSE;
  524. RRQ -> keepAlive = FALSE;
  525. RRQ -> willSupplyUUIEs = FALSE;
  526. // encode and send
  527. if( !IssueSend(&rasMessage) )
  528. {
  529. goto cleanup;
  530. }
  531. //delete if any previous TTL timer
  532. if( m_hRegTTLTimer )
  533. {
  534. DeleteTimerQueueTimer(H323TimerQueue, m_hRegTTLTimer, NULL );
  535. m_hRegTTLTimer = NULL;
  536. }
  537. //delete if any previous RRQ sent timer
  538. if( m_hRegTimer != NULL )
  539. {
  540. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL );
  541. m_hRegTimer = NULL;
  542. }
  543. pExpireContext -> RasClient = this;
  544. pExpireContext -> seqNumber = RRQ -> requestSeqNum;
  545. if( !CreateTimerQueueTimer(
  546. &m_hRegTimer,
  547. H323TimerQueue,
  548. RAS_CLIENT::RegExpiredCallback,
  549. pExpireContext,
  550. REG_EXPIRE_TIME, 0,
  551. WT_EXECUTEINIOTHREAD | WT_EXECUTEONLYONCE ) )
  552. {
  553. goto cleanup;
  554. }
  555. if( RRQ -> bit_mask & RegistrationRequest_terminalAlias_present )
  556. {
  557. FreeAddressAliases( (PSetup_UUIE_destinationAddress)
  558. RRQ -> terminalAlias);
  559. }
  560. m_dwRegRetryCount++;
  561. if( pAliasChangeRequest == NULL )
  562. {
  563. m_RegisterState = RAS_REGISTER_STATE_RRQSENT;
  564. }
  565. Unlock();
  566. H323DBG(( DEBUG_LEVEL_TRACE, "SendRRQ exited:%p.", this ));
  567. return TRUE;
  568. cleanup:
  569. if( RRQ -> bit_mask & RegistrationRequest_terminalAlias_present )
  570. {
  571. FreeAddressAliases( (PSetup_UUIE_destinationAddress)
  572. RRQ -> terminalAlias);
  573. }
  574. if( pExpireContext != NULL )
  575. {
  576. delete pExpireContext;
  577. }
  578. Unlock();
  579. H323DBG(( DEBUG_LEVEL_TRACE, "SendRRQ error:%p.",this ));
  580. return FALSE;
  581. }
  582. BOOL
  583. RAS_CLIENT::SendURQ(
  584. IN long seqNumber,
  585. IN EndpointIdentifier * pEndpointID
  586. )
  587. {
  588. RasMessage rasMessage;
  589. UnregistrationRequest * URQ;
  590. SOCKADDR_IN sockAddr;
  591. EXPIRE_CONTEXT * pExpireContext = NULL;
  592. UnregistrationRequest_callSignalAddress CallSignalAddressSequence;
  593. H323DBG(( DEBUG_LEVEL_TRACE, "SendURQ entered:%p.",this ));
  594. Lock();
  595. if( m_RegisterState == RAS_REGISTER_STATE_RRQSENT )
  596. {
  597. //store the seqNumber of this RRQ and send URQ if we recv RCF
  598. m_PendingURQ.RRQSeqNumber = m_lastRegisterSeqNum;
  599. Unlock();
  600. H323DBG(( DEBUG_LEVEL_TRACE, "rrq sent:so pending urq." ));
  601. return TRUE;
  602. }
  603. else if( m_RegisterState != RAS_REGISTER_STATE_REGISTERED )
  604. {
  605. //if already unregistered or URQ sent then return success
  606. Unlock();
  607. H323DBG(( DEBUG_LEVEL_TRACE, "current state:%d.", m_RegisterState ));
  608. return TRUE;
  609. }
  610. pExpireContext = new EXPIRE_CONTEXT;
  611. if( pExpireContext == NULL )
  612. {
  613. goto cleanup;
  614. }
  615. ZeroMemory (&rasMessage, sizeof RasMessage);
  616. rasMessage.choice = unregistrationRequest_chosen;
  617. URQ = &rasMessage.u.unregistrationRequest;
  618. // get sequence number
  619. if( seqNumber != NOT_RESEND_SEQ_NUM )
  620. {
  621. URQ -> requestSeqNum = (WORD)seqNumber;
  622. }
  623. else
  624. {
  625. m_UnRegisterSeqNum = GetNextSeqNum();
  626. URQ -> requestSeqNum = (WORD)m_UnRegisterSeqNum;
  627. }
  628. H323DBG(( DEBUG_LEVEL_TRACE, "RRQ seqNum:%d.", URQ -> requestSeqNum ));
  629. sockAddr.sin_family = AF_INET;
  630. sockAddr.sin_port =
  631. htons(LOWORD(g_RegistrySettings.dwQ931ListenPort));
  632. //we are listening for Q931 conns on all local interfaces
  633. //so specify just one of the local IP addresses
  634. sockAddr.sin_addr.s_addr = m_sockAddr.sin_addr.s_addr;
  635. SetTransportAddress( &sockAddr, &CallSignalAddressSequence.value);
  636. CallSignalAddressSequence.next = NULL;
  637. URQ -> callSignalAddress = &CallSignalAddressSequence;
  638. //get endpointidentifier by using GetComputerNameW
  639. URQ -> bit_mask |= UnregistrationRequest_endpointIdentifier_present;
  640. if( pEndpointID != NULL )
  641. {
  642. URQ->endpointIdentifier.length = pEndpointID ->length;
  643. URQ->endpointIdentifier.value = pEndpointID -> value;
  644. }
  645. else
  646. {
  647. URQ->endpointIdentifier.length = m_RASEndpointID.length;
  648. URQ->endpointIdentifier.value = m_RASEndpointID.value;
  649. }
  650. // encode and send
  651. if( !IssueSend( &rasMessage ) )
  652. {
  653. goto cleanup;
  654. }
  655. pExpireContext -> RasClient = this;
  656. pExpireContext -> seqNumber = URQ -> requestSeqNum;
  657. //delete if any previous RRQ sent timer
  658. if( m_hUnRegTimer != NULL )
  659. {
  660. DeleteTimerQueueTimer( H323TimerQueue, m_hUnRegTimer, NULL );
  661. m_hUnRegTimer = NULL;
  662. m_dwUnRegRetryCount = 0;
  663. }
  664. if( !CreateTimerQueueTimer(
  665. &m_hUnRegTimer,
  666. H323TimerQueue,
  667. RAS_CLIENT::UnregExpiredCallback,
  668. pExpireContext,
  669. REG_EXPIRE_TIME, 0,
  670. WT_EXECUTEINIOTHREAD | WT_EXECUTEONLYONCE) )
  671. {
  672. goto cleanup;
  673. }
  674. //delete if any TTL timer
  675. if( m_hRegTTLTimer )
  676. {
  677. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTTLTimer, NULL );
  678. m_hRegTTLTimer = NULL;
  679. }
  680. m_dwUnRegRetryCount++;
  681. m_RegisterState = RAS_REGISTER_STATE_URQSENT;
  682. Unlock();
  683. H323DBG(( DEBUG_LEVEL_TRACE, "SendURQ exited:%p.",this ));
  684. return TRUE;
  685. cleanup:
  686. if( pExpireContext != NULL )
  687. {
  688. delete pExpireContext;
  689. }
  690. Unlock();
  691. return FALSE;
  692. }
  693. //!!always called from a lock
  694. BOOL
  695. RAS_CLIENT::SendUCF(
  696. IN WORD seqNumber
  697. )
  698. {
  699. RasMessage rasMessage;
  700. UnregistrationConfirm * UCF;
  701. H323DBG(( DEBUG_LEVEL_TRACE, "SendUCF entered:%p.",this ));
  702. // initialize the structure
  703. ZeroMemory (&rasMessage, sizeof rasMessage);
  704. rasMessage.choice = unregistrationConfirm_chosen;
  705. UCF = &rasMessage.u.unregistrationConfirm;
  706. UCF -> bit_mask = 0;
  707. UCF -> requestSeqNum = seqNumber;
  708. if( !IssueSend( &rasMessage ) )
  709. {
  710. return FALSE;
  711. }
  712. H323DBG(( DEBUG_LEVEL_TRACE, "SendUCF exited:%p.",this ));
  713. return TRUE;
  714. }
  715. //!!always called from a lock
  716. BOOL
  717. RAS_CLIENT::SendURJ(
  718. IN WORD seqNumber,
  719. IN DWORD dwReason
  720. )
  721. {
  722. RasMessage rasMessage;
  723. UnregistrationReject * URJ;
  724. H323DBG(( DEBUG_LEVEL_TRACE, "SendURJ entered:%p.",this ));
  725. // initialize the structure
  726. ZeroMemory (&rasMessage, sizeof rasMessage);
  727. rasMessage.choice = unregistrationReject_chosen;
  728. URJ = &rasMessage.u.unregistrationReject;
  729. URJ -> bit_mask = 0;
  730. URJ -> requestSeqNum = seqNumber;
  731. URJ -> rejectReason.choice = (WORD)dwReason;
  732. if( !IssueSend( &rasMessage ) )
  733. {
  734. return FALSE;
  735. }
  736. H323DBG(( DEBUG_LEVEL_TRACE, "SendURJ exited:%p.",this ));
  737. return TRUE;
  738. }
  739. //!!always called from a lock
  740. void
  741. RAS_CLIENT::ProcessRasMessage(
  742. IN RasMessage *pRasMessage
  743. )
  744. {
  745. PH323_CALL pCall = NULL;
  746. ASN1decoding_t pASN1decInfo;
  747. H323DBG(( DEBUG_LEVEL_TRACE, "RAS: processing RasMessage" ));
  748. //Verify that the RCF came from the expected gatekeeper
  749. switch( pRasMessage -> choice )
  750. {
  751. case registrationReject_chosen:
  752. OnRegistrationReject( &pRasMessage -> u.registrationReject );
  753. break;
  754. case registrationConfirm_chosen:
  755. OnRegistrationConfirm( &pRasMessage -> u.registrationConfirm );
  756. break;
  757. case unregistrationRequest_chosen:
  758. OnUnregistrationRequest( &pRasMessage -> u.unregistrationRequest );
  759. break;
  760. case unregistrationReject_chosen:
  761. OnUnregistrationReject( &pRasMessage -> u.unregistrationReject );
  762. break;
  763. case unregistrationConfirm_chosen:
  764. OnUnregistrationConfirm( &pRasMessage -> u.unregistrationConfirm );
  765. break;
  766. case infoRequest_chosen:
  767. pASN1decInfo = m_ASNCoderInfo.pDecInfo;
  768. //This function should be always called in
  769. //a lock and it unlocks the the ras client
  770. OnInfoRequest( &pRasMessage -> u.infoRequest );
  771. ASN1_FreeDecoded( pASN1decInfo, pRasMessage, RasMessage_PDU );
  772. //return here since we have already unlocked and freed the buffer
  773. return;
  774. default:
  775. pASN1decInfo = m_ASNCoderInfo.pDecInfo;
  776. //Don't loclk the RAS client while locking the call object.
  777. Unlock();
  778. HandleRASCallMessage( pRasMessage );
  779. ASN1_FreeDecoded( pASN1decInfo, pRasMessage, RasMessage_PDU );
  780. //return here since we have already unlocked and freed the buffer
  781. return;
  782. }
  783. ASN1_FreeDecoded( m_ASNCoderInfo.pDecInfo, pRasMessage, RasMessage_PDU );
  784. Unlock();
  785. H323DBG(( DEBUG_LEVEL_TRACE, "ProcessRasMessage exited:%p.",this ));
  786. }
  787. //!!always called from a lock
  788. void
  789. HandleRASCallMessage(
  790. IN RasMessage *pRasMessage
  791. )
  792. {
  793. PH323_CALL pCall = NULL;
  794. H323DBG(( DEBUG_LEVEL_TRACE, "RAS: processing RASCallMessage" ));
  795. switch( pRasMessage -> choice )
  796. {
  797. case admissionConfirm_chosen:
  798. pCall = g_pH323Line -> FindCallByARQSeqNumAndLock(
  799. pRasMessage -> u.admissionConfirm.requestSeqNum);
  800. if( pCall != NULL )
  801. {
  802. pCall -> OnAdmissionConfirm( &pRasMessage->u.admissionConfirm );
  803. pCall -> Unlock();
  804. }
  805. else
  806. H323DBG(( DEBUG_LEVEL_ERROR, "acf:call not found:%d.",
  807. pRasMessage -> u.admissionConfirm.requestSeqNum ));
  808. break;
  809. case admissionReject_chosen:
  810. pCall = g_pH323Line -> FindCallByARQSeqNumAndLock(
  811. pRasMessage -> u.admissionReject.requestSeqNum );
  812. if( pCall != NULL )
  813. {
  814. pCall -> OnAdmissionReject( &pRasMessage->u.admissionReject );
  815. pCall -> Unlock();
  816. }
  817. else
  818. H323DBG(( DEBUG_LEVEL_ERROR, "arj:call not found:%d.",
  819. pRasMessage -> u.admissionReject.requestSeqNum ));
  820. break;
  821. case disengageRequest_chosen:
  822. pCall = g_pH323Line -> FindCallByCallRefAndLock(
  823. pRasMessage -> u.disengageRequest.callReferenceValue );
  824. if( pCall != NULL )
  825. {
  826. pCall -> OnDisengageRequest( &pRasMessage -> u.disengageRequest );
  827. pCall -> Unlock();
  828. }
  829. else
  830. H323DBG(( DEBUG_LEVEL_ERROR, "drq:call not found:%d.",
  831. pRasMessage -> u.disengageRequest.callReferenceValue ));
  832. break;
  833. case disengageConfirm_chosen:
  834. pCall = g_pH323Line -> FindCallByDRQSeqNumAndLock(
  835. pRasMessage -> u.disengageConfirm.requestSeqNum );
  836. if( pCall != NULL )
  837. {
  838. pCall -> OnDisengageConfirm( &pRasMessage -> u.disengageConfirm );
  839. pCall -> Unlock();
  840. }
  841. else
  842. H323DBG(( DEBUG_LEVEL_ERROR, "dcf:call not found:%d.",
  843. pRasMessage -> u.disengageConfirm.requestSeqNum ));
  844. break;
  845. case disengageReject_chosen:
  846. pCall = g_pH323Line -> FindCallByDRQSeqNumAndLock(
  847. pRasMessage -> u.disengageReject.requestSeqNum );
  848. if( pCall != NULL )
  849. {
  850. pCall -> OnDisengageReject( &pRasMessage -> u.disengageReject );
  851. pCall -> Unlock();
  852. }
  853. else
  854. H323DBG(( DEBUG_LEVEL_ERROR, "drj:call not found:%d.",
  855. pRasMessage -> u.disengageReject.requestSeqNum));
  856. break;
  857. case requestInProgress_chosen:
  858. pCall = g_pH323Line -> FindCallByARQSeqNumAndLock(
  859. pRasMessage->u.requestInProgress.requestSeqNum );
  860. if( pCall != NULL )
  861. {
  862. pCall -> OnRequestInProgress( &pRasMessage->u.requestInProgress );
  863. pCall -> Unlock();
  864. }
  865. else
  866. H323DBG(( DEBUG_LEVEL_ERROR, "rip:call not found:%d.",
  867. pRasMessage->u.requestInProgress.requestSeqNum ));
  868. break;
  869. default:
  870. _ASSERTE(0);
  871. H323DBG(( DEBUG_LEVEL_ERROR, "ProcessRASMessage: wrong message:%d",
  872. pRasMessage -> choice));
  873. break;
  874. }
  875. H323DBG(( DEBUG_LEVEL_TRACE, "HandleRASCallMessage exited" ));
  876. }
  877. void
  878. RAS_CLIENT::RegExpired(
  879. IN WORD seqNumber
  880. )
  881. {
  882. H323DBG(( DEBUG_LEVEL_TRACE, "RegExpired entered:%p.", this ));
  883. Lock();
  884. m_pRRQExpireContext = NULL;
  885. if( m_hRegTimer != NULL )
  886. {
  887. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL );
  888. m_hRegTimer = NULL;
  889. }
  890. /*
  891. 1. Registered:ignore this timeout
  892. 2. RRQ sent: (the endpoint has initiated the process of registering but
  893. hasn't heard from the GK yet) Resend the RRQ
  894. 3. Unregistered: ignore this imeout
  895. 4. URQ sent: (the endpoint has initiated the process of unregistering but
  896. hasn't heard from the GK yet) ignore this timeout
  897. 5. Idle: The GK client object has just been initialized and hasn't done
  898. anything. This should not happen.
  899. */
  900. switch( m_RegisterState )
  901. {
  902. case RAS_REGISTER_STATE_RRQSENT:
  903. if( m_dwRegRetryCount < REG_RETRY_MAX )
  904. {
  905. if( !SendRRQ( (long)seqNumber, NULL ) )
  906. {
  907. m_RegisterState = RAS_REGISTER_STATE_RRQEXPIRED;
  908. }
  909. }
  910. else
  911. {
  912. m_RegisterState = RAS_REGISTER_STATE_RRQEXPIRED;
  913. }
  914. break;
  915. case RAS_REGISTER_STATE_REGISTERED:
  916. case RAS_REGISTER_STATE_URQSENT:
  917. case RAS_REGISTER_STATE_UNREGISTERED:
  918. break;
  919. case RAS_REGISTER_STATE_IDLE:
  920. _ASSERTE(0);
  921. break;
  922. }
  923. Unlock();
  924. H323DBG(( DEBUG_LEVEL_TRACE, "RegExpired exited:%p.",this ));
  925. }
  926. void
  927. RAS_CLIENT::TTLExpired()
  928. {
  929. RasMessage rasMessage;
  930. RegistrationRequest * RRQ;
  931. H323DBG(( DEBUG_LEVEL_TRACE, "TTLExpired entered:%p.",this ));
  932. Lock();
  933. if( m_RegisterState == RAS_REGISTER_STATE_REGISTERED )
  934. {
  935. //send light weight RRQ
  936. // initialize the structure
  937. ZeroMemory (&rasMessage, sizeof rasMessage);
  938. rasMessage.choice = registrationRequest_chosen;
  939. RRQ = &rasMessage.u.registrationRequest;
  940. RRQ -> bit_mask = 0;
  941. RRQ -> protocolIdentifier = OID_H225ProtocolIdentifierV2;
  942. RRQ -> bit_mask |= keepAlive_present;
  943. RRQ -> keepAlive = TRUE;
  944. //copy TTL
  945. RRQ -> bit_mask |= RegistrationRequest_timeToLive_present;
  946. RRQ -> timeToLive = m_dwRegTimeToLive;
  947. //endpoint identifier
  948. RRQ -> bit_mask |= RegistrationRequest_endpointIdentifier_present;
  949. RRQ->endpointIdentifier.length = m_RASEndpointID.length;
  950. RRQ->endpointIdentifier.value = m_RASEndpointID.value;
  951. //seqNumber
  952. m_wTTLSeqNumber = GetNextSeqNum();
  953. RRQ -> requestSeqNum = (WORD)m_wTTLSeqNumber;
  954. //what about gatekeeperIdentifier, tokens?
  955. // encode and send
  956. if( !IssueSend(&rasMessage) )
  957. {
  958. H323DBG(( DEBUG_LEVEL_TRACE, "SendLwtRRQ error:%p.",this ));
  959. Unlock();
  960. return;
  961. }
  962. }
  963. Unlock();
  964. H323DBG(( DEBUG_LEVEL_TRACE, "TTLExpired exited:%p.",this ));
  965. }
  966. void
  967. RAS_CLIENT::UnregExpired(
  968. IN WORD seqNumber
  969. )
  970. {
  971. H323DBG(( DEBUG_LEVEL_TRACE, "UnregExpired entered:%p.",this ));
  972. Lock();
  973. m_pURQExpireContext = NULL;
  974. if( m_hUnRegTimer != NULL )
  975. {
  976. DeleteTimerQueueTimer( H323TimerQueue, m_hUnRegTimer, NULL);
  977. m_hUnRegTimer = NULL;
  978. m_dwUnRegRetryCount = 0;
  979. }
  980. switch( m_RegisterState )
  981. {
  982. case RAS_REGISTER_STATE_URQSENT:
  983. if( m_dwUnRegRetryCount < URQ_RETRY_MAX )
  984. {
  985. SendURQ( (long)seqNumber, NULL );
  986. }
  987. else
  988. {
  989. m_RegisterState = RAS_REGISTER_STATE_URQEXPIRED;
  990. }
  991. break;
  992. case RAS_REGISTER_STATE_REGISTERED:
  993. case RAS_REGISTER_STATE_RRQSENT:
  994. case RAS_REGISTER_STATE_UNREGISTERED:
  995. break;
  996. case RAS_REGISTER_STATE_IDLE:
  997. _ASSERTE(0);
  998. break;
  999. }
  1000. Unlock();
  1001. H323DBG(( DEBUG_LEVEL_TRACE, "UnregExpired exited:%p.",this ));
  1002. }
  1003. void
  1004. RAS_CLIENT::OnUnregistrationRequest(
  1005. IN UnregistrationRequest *URQ
  1006. )
  1007. {
  1008. H323DBG(( DEBUG_LEVEL_TRACE, "OnUnregistrationRequest entered:%p.", this ));
  1009. _ASSERTE( m_RegisterState != RAS_REGISTER_STATE_IDLE );
  1010. if( (m_RegisterState == RAS_REGISTER_STATE_UNREGISTERED) ||
  1011. (m_RegisterState == RAS_REGISTER_STATE_RRJ) )
  1012. {
  1013. SendURJ( URQ -> requestSeqNum, notCurrentlyRegistered_chosen );
  1014. }
  1015. else if( m_dwCallsInProgress )
  1016. {
  1017. SendURJ( URQ -> requestSeqNum, callInProgress_chosen );
  1018. }
  1019. else
  1020. {
  1021. m_RegisterState = RAS_REGISTER_STATE_UNREGISTERED;
  1022. SendUCF( URQ -> requestSeqNum );
  1023. //try to register again
  1024. if( !SendRRQ( NOT_RESEND_SEQ_NUM, NULL ) )
  1025. {
  1026. H323DBG(( DEBUG_LEVEL_ERROR,
  1027. "couldn't send rrq on urq request." ));
  1028. }
  1029. }
  1030. H323DBG(( DEBUG_LEVEL_TRACE, "OnUnregistrationRequest exited:%p.",this ));
  1031. }
  1032. //!!always called from a lock
  1033. void
  1034. RAS_CLIENT::OnUnregistrationConfirm(
  1035. IN UnregistrationConfirm *UCF
  1036. )
  1037. {
  1038. H323DBG((DEBUG_LEVEL_TRACE, "OnUnregistrationConfirm entered:%p.",this));
  1039. if( UCF -> requestSeqNum != m_UnRegisterSeqNum )
  1040. return;
  1041. _ASSERTE( m_hUnRegTimer );
  1042. if( m_hUnRegTimer != NULL )
  1043. {
  1044. DeleteTimerQueueTimer( H323TimerQueue, m_hUnRegTimer, NULL);
  1045. m_hUnRegTimer = NULL;
  1046. m_dwUnRegRetryCount = 0;
  1047. }
  1048. if( (m_RegisterState == RAS_REGISTER_STATE_URQSENT) ||
  1049. (m_RegisterState == RAS_REGISTER_STATE_URQEXPIRED) )
  1050. {
  1051. //delete if any TTL timer
  1052. if( m_hRegTTLTimer )
  1053. {
  1054. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTTLTimer, NULL );
  1055. m_hRegTTLTimer = NULL;
  1056. }
  1057. m_RegisterState = RAS_REGISTER_STATE_UNREGISTERED;
  1058. }
  1059. H323DBG(( DEBUG_LEVEL_TRACE, "OnUnregistrationConfirm exited:%p.",this));
  1060. }
  1061. //!!always called from a lock
  1062. void
  1063. RAS_CLIENT::OnUnregistrationReject(
  1064. IN UnregistrationReject *URJ
  1065. )
  1066. {
  1067. H323DBG(( DEBUG_LEVEL_TRACE, "OnUnregistrationReject entered:%p.",this ));
  1068. if( URJ -> requestSeqNum != m_UnRegisterSeqNum )
  1069. {
  1070. return;
  1071. }
  1072. if( m_hUnRegTimer != NULL )
  1073. {
  1074. DeleteTimerQueueTimer( H323TimerQueue, m_hUnRegTimer, NULL);
  1075. m_hUnRegTimer = NULL;
  1076. m_dwUnRegRetryCount = 0;
  1077. }
  1078. if( (m_RegisterState == RAS_REGISTER_STATE_URQSENT) ||
  1079. (m_RegisterState == RAS_REGISTER_STATE_URQEXPIRED) )
  1080. {
  1081. m_RegisterState = RAS_REGISTER_STATE_UNREGISTERED;
  1082. }
  1083. H323DBG(( DEBUG_LEVEL_TRACE, "OnUnregistrationReject exited:%p.",this ));
  1084. }
  1085. //!!always called from a lock
  1086. void
  1087. RAS_CLIENT::OnRegistrationReject(
  1088. IN RegistrationReject * RRJ
  1089. )
  1090. {
  1091. H323DBG(( DEBUG_LEVEL_TRACE, "OnRegistrationReject entered:%p.",this ));
  1092. if( RRJ -> requestSeqNum == m_wTTLSeqNumber )
  1093. {
  1094. //Keep alive failed. So start registration process again.
  1095. //This will change the RAS registration state to RRQSENT from REGISTERED.
  1096. if( !SendRRQ( NOT_RESEND_SEQ_NUM, NULL ) )
  1097. {
  1098. H323DBG(( DEBUG_LEVEL_ERROR,
  1099. "couldn't send rrq on Keep alive failure." ));
  1100. m_RegisterState = RAS_REGISTER_STATE_UNREGISTERED;
  1101. }
  1102. }
  1103. else if( RRJ -> requestSeqNum == m_lastRegisterSeqNum )
  1104. {
  1105. if( m_hRegTimer != NULL )
  1106. {
  1107. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL);
  1108. m_hRegTimer = NULL;
  1109. m_dwRegRetryCount = 0;
  1110. }
  1111. if( (m_RegisterState == RAS_REGISTER_STATE_RRQSENT) ||
  1112. (m_RegisterState == RAS_REGISTER_STATE_RRQEXPIRED) )
  1113. {
  1114. m_RegisterState = RAS_REGISTER_STATE_RRJ;
  1115. }
  1116. }
  1117. H323DBG(( DEBUG_LEVEL_TRACE, "OnRegistrationReject exitd:%p.",this ));
  1118. }
  1119. //!!always called in lock
  1120. void
  1121. RAS_CLIENT::OnRegistrationConfirm(
  1122. IN RegistrationConfirm * RCF )
  1123. {
  1124. LIST_ENTRY *pListEntry = NULL;
  1125. PALIASCHANGE_REQUEST pAliasChangeRequest = NULL;
  1126. H323DBG(( DEBUG_LEVEL_TRACE, "OnRegistrationConfirm entered:%p.", this ));
  1127. if( RCF -> requestSeqNum == m_PendingURQ.RRQSeqNumber )
  1128. {
  1129. //The timer could have been startd with the last RRQ,
  1130. //irrespective of the current state of registration.
  1131. if( m_hRegTimer != NULL )
  1132. {
  1133. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL);
  1134. m_hRegTimer = NULL;
  1135. m_dwRegRetryCount = 0;
  1136. }
  1137. //send URQ with the endpointIdentifier
  1138. SendURQ( NOT_RESEND_SEQ_NUM, &RCF->endpointIdentifier );
  1139. H323DBG(( DEBUG_LEVEL_TRACE, "sending pending URQ for RRQ:%d",
  1140. RCF->requestSeqNum ));
  1141. return;
  1142. }
  1143. else if( RCF -> requestSeqNum == m_lastRegisterSeqNum )
  1144. {
  1145. //The timer could have been startd with the last RRQ,
  1146. //irrespective of the current state of registration.
  1147. if( m_hRegTimer != NULL )
  1148. {
  1149. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL);
  1150. m_hRegTimer = NULL;
  1151. m_dwRegRetryCount = 0;
  1152. }
  1153. switch (m_RegisterState)
  1154. {
  1155. case RAS_REGISTER_STATE_REGISTERED:
  1156. if (RCF->requestSeqNum == m_wTTLSeqNumber)
  1157. {
  1158. H323DBG(( DEBUG_LEVEL_TRACE, "RCF for TTL-RRQ." ));
  1159. }
  1160. else
  1161. {
  1162. H323DBG(( DEBUG_LEVEL_WARNING,
  1163. "warning: received RCF, but was already registered-ignoring"));
  1164. }
  1165. break;
  1166. case RAS_REGISTER_STATE_RRQEXPIRED:
  1167. H323DBG(( DEBUG_LEVEL_TRACE,
  1168. "received RCF, but registration already expired, send URQ" ));
  1169. SendURQ (NOT_RESEND_SEQ_NUM, &RCF->endpointIdentifier);
  1170. break;
  1171. case RAS_REGISTER_STATE_RRQSENT:
  1172. //expecting RRQ. gatekeeper has responded.
  1173. m_RegisterState = RAS_REGISTER_STATE_REGISTERED;
  1174. CopyMemory( (PVOID)m_RASEndpointID.value,
  1175. (PVOID)RCF -> endpointIdentifier.value,
  1176. RCF -> endpointIdentifier.length * sizeof(WCHAR) );
  1177. m_RASEndpointID.value[RCF -> endpointIdentifier.length] = L'\0';
  1178. m_RASEndpointID.length = (WORD)RCF -> endpointIdentifier.length;
  1179. InitializeTTLTimer( RCF );
  1180. break;
  1181. default:
  1182. H323DBG(( DEBUG_LEVEL_TRACE,
  1183. "RAS: received RRQ, but was in unexpected state"));
  1184. break;
  1185. }
  1186. }
  1187. else if( RCF -> requestSeqNum == m_wTTLSeqNumber )
  1188. {
  1189. //The timer could have been startd with the last RRQ,
  1190. //irrespective of the current state of registration.
  1191. if( m_hRegTimer != NULL )
  1192. {
  1193. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL);
  1194. m_hRegTimer = NULL;
  1195. m_dwRegRetryCount = 0;
  1196. }
  1197. //look for the change in keepalive interval.
  1198. InitializeTTLTimer( RCF );
  1199. }
  1200. else
  1201. {
  1202. //Try to find if this is a alias change request.
  1203. for( pListEntry = m_aliasChangeRequestList.Flink;
  1204. pListEntry != &m_aliasChangeRequestList;
  1205. pListEntry = pListEntry -> Flink )
  1206. {
  1207. pAliasChangeRequest = CONTAINING_RECORD( pListEntry,
  1208. ALIASCHANGE_REQUEST, listEntry );
  1209. if( pAliasChangeRequest -> wRequestSeqNum == RCF -> requestSeqNum )
  1210. {
  1211. break;
  1212. }
  1213. }
  1214. if( pListEntry != &m_aliasChangeRequestList )
  1215. {
  1216. //The timer could have been startd with the last RRQ,
  1217. //irrespective of the current state of registration.
  1218. if( m_hRegTimer != NULL )
  1219. {
  1220. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTimer, NULL);
  1221. m_hRegTimer = NULL;
  1222. m_dwRegRetryCount = 0;
  1223. }
  1224. //if registration has changed since this request was made then
  1225. //ignore the message.
  1226. if( memcmp( (PVOID)pAliasChangeRequest -> rasEndpointID.value,
  1227. m_RASEndpointID.value, m_RASEndpointID.length * sizeof(WCHAR) )
  1228. == 0 )
  1229. {
  1230. //update the alias list.
  1231. FreeAliasNames( m_pAliasList );
  1232. m_pAliasList = pAliasChangeRequest->pAliasList;
  1233. RemoveEntryList( &pAliasChangeRequest->listEntry );
  1234. delete pAliasChangeRequest;
  1235. }
  1236. }
  1237. }
  1238. H323DBG(( DEBUG_LEVEL_TRACE, "OnRegistrationConfirm exited:%p.", this ));
  1239. }
  1240. //!!always called in a lock.
  1241. BOOL
  1242. RAS_CLIENT::InitializeTTLTimer(
  1243. IN RegistrationConfirm * RCF )
  1244. {
  1245. H323DBG(( DEBUG_LEVEL_TRACE, "InitializeTTLTimer - entered." ));
  1246. if( (RCF->bit_mask & RegistrationConfirm_timeToLive_present) &&
  1247. ( (m_dwRegTimeToLive != RCF->timeToLive) || (m_hRegTTLTimer == NULL) )
  1248. )
  1249. {
  1250. m_dwRegTimeToLive = RCF->timeToLive;
  1251. H323DBG(( DEBUG_LEVEL_TRACE, "timetolive value:%d.",
  1252. m_dwRegTimeToLive ));
  1253. //delete if any previous TTL timer
  1254. if( m_hRegTTLTimer )
  1255. {
  1256. DeleteTimerQueueTimer( H323TimerQueue, m_hRegTTLTimer, NULL );
  1257. m_hRegTTLTimer = NULL;
  1258. }
  1259. //start a timer to send lightweight RRQ afetr given time
  1260. if( !CreateTimerQueueTimer(
  1261. &m_hRegTTLTimer,
  1262. H323TimerQueue,
  1263. RAS_CLIENT::TTLExpiredCallback,
  1264. this,
  1265. (m_dwRegTimeToLive - REG_TTL_MARGIN)*1000,
  1266. (m_dwRegTimeToLive - REG_TTL_MARGIN)*1000,
  1267. WT_EXECUTEINIOTHREAD | WT_EXECUTEONLYONCE ) )
  1268. {
  1269. H323DBG ((DEBUG_LEVEL_ERROR, "failed to create timer queue timer"));
  1270. m_hRegTTLTimer = NULL;
  1271. return FALSE;
  1272. }
  1273. }
  1274. H323DBG(( DEBUG_LEVEL_TRACE, "InitializeTTLTimer - exited." ));
  1275. return TRUE;
  1276. }
  1277. void
  1278. RAS_CLIENT::OnInfoRequest (
  1279. IN InfoRequest * IRQ)
  1280. {
  1281. PH323_CALL pCall;
  1282. SOCKADDR_IN ReplyAddress;
  1283. InfoRequestResponse_perCallInfo CallInfoList;
  1284. InfoRequestResponse_perCallInfo * pCallInfoList = NULL;
  1285. InfoRequestResponse_perCallInfo_Seq * CallInfo;
  1286. HRESULT hr;
  1287. int iIndex, jIndex;
  1288. int iNumCalls = 0;
  1289. int iCallTableSize;
  1290. H323DBG(( DEBUG_LEVEL_TRACE, "OnInfoRequest - entered." ));
  1291. if (m_RegisterState != RAS_REGISTER_STATE_REGISTERED)
  1292. {
  1293. H323DBG ((DEBUG_LEVEL_ERROR, "RAS: received InfoRequest, but was not registered"));
  1294. Unlock();
  1295. return;
  1296. }
  1297. if (IRQ->bit_mask & replyAddress_present)
  1298. {
  1299. if (!GetTransportAddress (&IRQ -> replyAddress, &ReplyAddress))
  1300. {
  1301. H323DBG ((DEBUG_LEVEL_ERROR, "RAS: received InfoRequest, but replyAddress was malformed"));
  1302. Unlock();
  1303. return;
  1304. }
  1305. }
  1306. else
  1307. {
  1308. ReplyAddress = m_GKAddress;
  1309. }
  1310. //Don't loclk the RAS client while locking the call object.
  1311. Unlock();
  1312. if( IRQ -> callReferenceValue )
  1313. {
  1314. //query is for a specific call. So find the call and then send IRR
  1315. pCall = g_pH323Line ->
  1316. FindCallByCallRefAndLock( IRQ -> callReferenceValue );
  1317. if( pCall )
  1318. {
  1319. CallInfo = &CallInfoList.value;
  1320. CallInfoList.next = NULL;
  1321. ZeroMemory (CallInfo, sizeof (InfoRequestResponse_perCallInfo_Seq));
  1322. CallInfo -> callIdentifier.guid.length = sizeof (GUID);
  1323. CallInfo -> conferenceID.length = sizeof (GUID);
  1324. hr = pCall -> GetCallInfo (
  1325. (GUID *) &CallInfo -> callIdentifier.guid.value,
  1326. (GUID *) &CallInfo -> conferenceID.value);
  1327. if( hr != S_OK )
  1328. {
  1329. H323DBG ((DEBUG_LEVEL_ERROR,
  1330. "RAS: call is disconnected for crv (%04XH).",
  1331. IRQ -> callReferenceValue));
  1332. return;
  1333. }
  1334. pCall -> Unlock();
  1335. CallInfo -> callReferenceValue = IRQ -> callReferenceValue;
  1336. CallInfo -> callType.choice = pointToPoint_chosen;
  1337. CallInfo -> callModel.choice = direct_chosen;
  1338. }
  1339. else
  1340. {
  1341. H323DBG(( DEBUG_LEVEL_ERROR,
  1342. "RAS: received InfoRequest for nonexistent crv (%04XH).",
  1343. IRQ -> callReferenceValue));
  1344. return;
  1345. }
  1346. SendInfoRequestResponse (&ReplyAddress, IRQ -> requestSeqNum, &CallInfoList);
  1347. }
  1348. else
  1349. {
  1350. //send the info about all the active calls.
  1351. iNumCalls = g_pH323Line->GetNoOfCalls();
  1352. if( iNumCalls != 0 )
  1353. {
  1354. pCallInfoList = new InfoRequestResponse_perCallInfo[iNumCalls];
  1355. }
  1356. if( pCallInfoList != NULL )
  1357. {
  1358. //lock the call table
  1359. g_pH323Line -> LockCallTable();
  1360. iCallTableSize = g_pH323Line->GetCallTableSize();
  1361. //lock the call so that nobody else would be able to delete the call
  1362. for( jIndex=0, iIndex=0;
  1363. (iIndex < iCallTableSize) && (jIndex < iNumCalls);
  1364. iIndex++ )
  1365. {
  1366. pCall = g_pH323Line->GetCallAtIndex(iIndex);
  1367. if( pCall != NULL )
  1368. {
  1369. pCall -> Lock();
  1370. CallInfo = &(pCallInfoList[jIndex++].value);
  1371. ZeroMemory( CallInfo, sizeof (InfoRequestResponse_perCallInfo_Seq) );
  1372. CallInfo -> callIdentifier.guid.length = sizeof( GUID );
  1373. CallInfo -> conferenceID.length = sizeof( GUID );
  1374. pCall -> GetCallInfo(
  1375. (GUID *) &CallInfo -> callIdentifier.guid.value,
  1376. (GUID *) &CallInfo -> conferenceID.value);
  1377. CallInfo -> callReferenceValue = pCall->GetCallRef();
  1378. pCall -> Unlock();
  1379. CallInfo -> callType.choice = pointToPoint_chosen;
  1380. CallInfo -> callModel.choice = direct_chosen;
  1381. }
  1382. }
  1383. for( iIndex=0; iIndex < jIndex-1; iIndex++ )
  1384. {
  1385. pCallInfoList[iIndex].next = &(pCallInfoList[iIndex+1]);
  1386. }
  1387. pCallInfoList[iIndex].next = NULL;
  1388. //unlock the call table
  1389. g_pH323Line -> UnlockCallTable();
  1390. }
  1391. SendInfoRequestResponse( &ReplyAddress, IRQ -> requestSeqNum,
  1392. pCallInfoList );
  1393. if( pCallInfoList != NULL )
  1394. {
  1395. delete pCallInfoList;
  1396. }
  1397. }
  1398. H323DBG(( DEBUG_LEVEL_TRACE, "OnInfoRequest - exited." ));
  1399. }
  1400. HRESULT RAS_CLIENT::SendInfoRequestResponse (
  1401. IN SOCKADDR_IN * RasAddress,
  1402. IN USHORT SequenceNumber,
  1403. IN InfoRequestResponse_perCallInfo * CallInfoList)
  1404. {
  1405. RasMessage RasMessage;
  1406. InfoRequestResponse * IRR;
  1407. SOCKADDR_IN SocketAddress;
  1408. HRESULT hr;
  1409. H323DBG(( DEBUG_LEVEL_TRACE, "SendInfoRequestResponse - entered." ));
  1410. Lock();
  1411. if( m_RegisterState == RAS_REGISTER_STATE_REGISTERED )
  1412. {
  1413. InfoRequestResponse_callSignalAddress CallSignalAddressSequence;
  1414. H323DBG(( DEBUG_LEVEL_TRACE, "SendIRR entered:%p.",this ));
  1415. ZeroMemory (&RasMessage, sizeof RasMessage);
  1416. RasMessage.choice = infoRequestResponse_chosen;
  1417. IRR = &RasMessage.u.infoRequestResponse;
  1418. IRR -> requestSeqNum = SequenceNumber;
  1419. // we are listening for Q931 conns on all local interfaces
  1420. // so specify just one of the local IP addresses
  1421. // -XXX- fix for multihomed support later
  1422. SocketAddress.sin_family = AF_INET;
  1423. SocketAddress.sin_port = htons (LOWORD(g_RegistrySettings.dwQ931ListenPort));
  1424. SocketAddress.sin_addr.s_addr = m_sockAddr.sin_addr.s_addr;
  1425. // callSignalAddress
  1426. SetTransportAddress (&SocketAddress, &CallSignalAddressSequence.value);
  1427. CallSignalAddressSequence.next = NULL;
  1428. IRR -> callSignalAddress = &CallSignalAddressSequence;
  1429. // rasAddress
  1430. IRR -> rasAddress = m_transportAddress;
  1431. // endpointIdentifier
  1432. IRR -> endpointIdentifier.length = m_RASEndpointID.length;
  1433. IRR -> endpointIdentifier.value = m_RASEndpointID.value;
  1434. // fill in endpoint type
  1435. IRR -> endpointType.bit_mask |= terminal_present;
  1436. IRR -> endpointType.terminal.bit_mask = 0;
  1437. if( CallInfoList )
  1438. {
  1439. IRR -> bit_mask |= perCallInfo_present;
  1440. IRR -> perCallInfo = CallInfoList;
  1441. }
  1442. // send the pdu
  1443. hr = EncodeSendMessage (&RasMessage);
  1444. }
  1445. else
  1446. {
  1447. hr = E_FAIL;
  1448. }
  1449. Unlock();
  1450. H323DBG(( DEBUG_LEVEL_TRACE, "SendInfoRequestResponse - exited." ));
  1451. return TRUE;
  1452. }
  1453. //!!always called from a lock
  1454. BOOL
  1455. RAS_CLIENT::InitializeIo (void)
  1456. {
  1457. DWORD dwFlags = 0;
  1458. int AddressLength;
  1459. H323DBG(( DEBUG_LEVEL_TRACE, "InitializeIo entered:%p.",this ));
  1460. m_Socket = WSASocket (AF_INET,
  1461. SOCK_DGRAM,
  1462. IPPROTO_UDP,
  1463. NULL, 0,
  1464. WSA_FLAG_OVERLAPPED);
  1465. if( m_Socket == INVALID_SOCKET )
  1466. {
  1467. WSAGetLastError();
  1468. return FALSE;
  1469. }
  1470. if( !H323BindIoCompletionCallback ( (HANDLE)m_Socket,
  1471. RAS_CLIENT::IoCompletionCallback, 0))
  1472. {
  1473. GetLastError();
  1474. goto cleanup;
  1475. }
  1476. m_sockAddr.sin_family = AF_INET;
  1477. m_sockAddr.sin_port = htons (0);
  1478. m_sockAddr.sin_addr.s_addr =
  1479. GetLocalIPAddress( m_GKAddress.sin_addr.S_un.S_addr );
  1480. H323DBG(( DEBUG_LEVEL_TRACE,
  1481. "gk sock addr:%lx.", m_sockAddr.sin_addr.s_addr ));
  1482. if( bind( m_Socket, (SOCKADDR *)&m_sockAddr, sizeof(m_sockAddr) )
  1483. == SOCKET_ERROR )
  1484. {
  1485. H323DBG(( DEBUG_LEVEL_ERROR,
  1486. "Couldn't bind the RAS socket:%d, %p", WSAGetLastError(), this ));
  1487. goto cleanup;
  1488. }
  1489. // now that we've bound to a dynamic UDP port,
  1490. // query that port from the stack and store it.
  1491. AddressLength = sizeof m_sockAddr;
  1492. if( getsockname(m_Socket, (SOCKADDR *)&m_sockAddr, &AddressLength)
  1493. == SOCKET_ERROR )
  1494. {
  1495. H323DBG(( DEBUG_LEVEL_ERROR,
  1496. "getsockname failed :%d, %p", WSAGetLastError(), this ));
  1497. goto cleanup;
  1498. }
  1499. _ASSERTE( ntohs(m_sockAddr.sin_port) );
  1500. // fill in the IoTransportAddress structure.
  1501. // this structure is the ASN.1-friendly transport
  1502. // address of this client's endpoint.
  1503. SetTransportAddress( &m_sockAddr, &m_transportAddress );
  1504. // initiate i/o
  1505. ZeroMemory( (PVOID)&m_recvOverlapped, sizeof(RAS_RECV_CONTEXT) );
  1506. if( !IssueRecv() )
  1507. {
  1508. goto cleanup;
  1509. }
  1510. H323DBG(( DEBUG_LEVEL_TRACE, "InitializeIo exited:%p.",this ));
  1511. return TRUE;
  1512. cleanup:
  1513. closesocket(m_Socket);
  1514. m_Socket = INVALID_SOCKET;
  1515. return FALSE;
  1516. }
  1517. DWORD GetLocalIPAddress(
  1518. IN DWORD dwRemoteAddr
  1519. )
  1520. {
  1521. DWORD dwLocalAddr = INADDR_ANY;
  1522. SOCKADDR_IN sRemoteAddr;
  1523. SOCKADDR_IN sLocalAddr;
  1524. DWORD dwNumBytesReturned = 0;
  1525. SOCKET querySocket;
  1526. H323DBG(( DEBUG_LEVEL_TRACE, "GetLocalIPAddress - entered." ));
  1527. ZeroMemory( (PVOID)&sRemoteAddr, sizeof(SOCKADDR_IN) );
  1528. ZeroMemory( (PVOID)&sLocalAddr, sizeof(SOCKADDR_IN) );
  1529. sRemoteAddr.sin_family = AF_INET;
  1530. sRemoteAddr.sin_addr = *(struct in_addr *) &dwRemoteAddr;
  1531. querySocket = WSASocket(
  1532. AF_INET, // int af
  1533. SOCK_DGRAM, // int type
  1534. IPPROTO_UDP, // int protocol
  1535. NULL, // LPWSAPROTOCOL_INFO lpProtocolInfo
  1536. 0, // GROUP g
  1537. WSA_FLAG_OVERLAPPED // DWORD dwFlags
  1538. );
  1539. if( querySocket == INVALID_SOCKET )
  1540. {
  1541. H323DBG(( DEBUG_LEVEL_ERROR, "getlocalIP wsasocket:%d.",
  1542. WSAGetLastError() ));
  1543. return dwLocalAddr;
  1544. }
  1545. if( WSAIoctl(
  1546. querySocket, // SOCKET s
  1547. SIO_ROUTING_INTERFACE_QUERY, // DWORD dwIoControlCode
  1548. &sRemoteAddr, // LPVOID lpvInBuffer
  1549. sizeof(SOCKADDR_IN), // DWORD cbInBuffer
  1550. &sLocalAddr, // LPVOID lpvOUTBuffer
  1551. sizeof(SOCKADDR_IN), // DWORD cbOUTBuffer
  1552. &dwNumBytesReturned, // LPDWORD lpcbBytesReturned
  1553. NULL, // LPWSAOVERLAPPED lpOverlapped
  1554. NULL // LPWSAOVERLAPPED_COMPLETION_ROUTINE lpComplROUTINE
  1555. ) == SOCKET_ERROR)
  1556. {
  1557. H323DBG(( DEBUG_LEVEL_ERROR, "getlocalIP error wsaioctl:%d.",
  1558. WSAGetLastError() ));
  1559. }
  1560. else
  1561. {
  1562. dwLocalAddr = *(DWORD *)&sLocalAddr.sin_addr;
  1563. //if the remote address is on the same machine then...
  1564. H323DBG(( DEBUG_LEVEL_ERROR, "dwLocalAddr:%x.", dwLocalAddr ));
  1565. if( dwLocalAddr == NET_LOCAL_IP_ADDR_INTERFACE )
  1566. {
  1567. dwLocalAddr = dwRemoteAddr;
  1568. }
  1569. _ASSERTE( dwLocalAddr );
  1570. }
  1571. closesocket( querySocket );
  1572. H323DBG(( DEBUG_LEVEL_TRACE, "GetLocalIPAddress - exited." ));
  1573. return dwLocalAddr;
  1574. }
  1575. //!!always called from a lock
  1576. BOOL
  1577. RAS_CLIENT::IssueRecv(void)
  1578. {
  1579. int iError;
  1580. WSABUF BufferArray [1];
  1581. H323DBG(( DEBUG_LEVEL_TRACE, "IssueRecv entered:%p.",this ));
  1582. _ASSERTE(!m_recvOverlapped.IsPending);
  1583. if(m_Socket == INVALID_SOCKET)
  1584. {
  1585. return FALSE;
  1586. }
  1587. BufferArray [0].buf = (char *)(m_recvOverlapped.arBuf);
  1588. BufferArray [0].len = IO_BUFFER_SIZE;
  1589. ZeroMemory (&m_recvOverlapped.Overlapped, sizeof(OVERLAPPED));
  1590. m_recvOverlapped.Type = OVERLAPPED_TYPE_RECV;
  1591. m_recvOverlapped.RasClient = this;
  1592. m_recvOverlapped.AddressLength = sizeof (SOCKADDR_IN);
  1593. m_recvOverlapped.Flags = 0;
  1594. if( WSARecvFrom(m_Socket,
  1595. BufferArray, 1,
  1596. &m_recvOverlapped.BytesTransferred,
  1597. &m_recvOverlapped.Flags,
  1598. (SOCKADDR*)&m_recvOverlapped.Address,
  1599. &m_recvOverlapped.AddressLength,
  1600. &m_recvOverlapped.Overlapped,
  1601. NULL) == SOCKET_ERROR )
  1602. {
  1603. iError = WSAGetLastError();
  1604. if( iError == WSA_IO_PENDING )
  1605. {
  1606. m_recvOverlapped.IsPending = TRUE;
  1607. m_IoRefCount++;
  1608. }
  1609. else if( iError == WSAEMSGSIZE )
  1610. {
  1611. //We don't handle this condition right now as it should not happen
  1612. //In future with changes in the protocol this might be invoked and
  1613. //should be fixed
  1614. _ASSERTE( FALSE );
  1615. }
  1616. else if( iError == WSAECONNRESET )
  1617. {
  1618. //On a UPD-datagram socket this error would indicate that a
  1619. //previous send operation resulted in an ICMP "Port Unreachable"
  1620. //message. This will happen if GK is not listening on the specified
  1621. //port. This case would need special handling.
  1622. _ASSERTE( FALSE );
  1623. return FALSE;
  1624. }
  1625. else
  1626. {
  1627. //fatal error on the socket. shutdown the RAS client
  1628. return FALSE;
  1629. }
  1630. }
  1631. else
  1632. {
  1633. //data recvd immediately. IsPending is set beacause anyway a
  1634. //SendComplete event will be sent which will reset this BOOL
  1635. m_recvOverlapped.IsPending = TRUE;
  1636. m_IoRefCount++;
  1637. }
  1638. H323DBG(( DEBUG_LEVEL_TRACE, "IssueRecv exited:%p.",this ));
  1639. return TRUE;
  1640. }
  1641. HRESULT
  1642. RAS_CLIENT::EncodeSendMessage(
  1643. IN RasMessage * RasMessage)
  1644. {
  1645. RAS_SEND_CONTEXT * SendContext;
  1646. ASN1error_e AsnError;
  1647. WSABUF BufferArray [1];
  1648. DWORD dwStatus;
  1649. H323DBG(( DEBUG_LEVEL_TRACE, "EncodeSendMessage entered:%p.",this ));
  1650. Lock();
  1651. if (m_Socket == INVALID_SOCKET)
  1652. {
  1653. Unlock();
  1654. return E_FAIL;
  1655. }
  1656. SendContext = AllocSendBuffer();
  1657. if( SendContext == NULL )
  1658. {
  1659. Unlock();
  1660. return E_OUTOFMEMORY;
  1661. }
  1662. ZeroMemory( &SendContext -> Overlapped, sizeof (OVERLAPPED) );
  1663. AsnError = ASN1_Encode (
  1664. m_ASNCoderInfo.pEncInfo,
  1665. RasMessage,
  1666. RasMessage_PDU,
  1667. ASN1ENCODE_SETBUFFER,
  1668. SendContext -> arBuf,
  1669. IO_BUFFER_SIZE);
  1670. if (ASN1_FAILED (AsnError))
  1671. {
  1672. H323DBG ((DEBUG_LEVEL_ERROR, "RAS: failed to encode RAS PDU (%d).", AsnError));
  1673. FreeSendBuffer (SendContext);
  1674. Unlock();
  1675. return E_FAIL;
  1676. }
  1677. BufferArray [0].buf = (char *) SendContext -> arBuf;
  1678. BufferArray [0].len = m_ASNCoderInfo.pEncInfo -> len;
  1679. SendContext -> Type = OVERLAPPED_TYPE_SEND;
  1680. SendContext -> RasClient = this;
  1681. SendContext -> Address = m_GKAddress;
  1682. if( WSASendTo (m_Socket,
  1683. BufferArray,
  1684. 1,
  1685. &SendContext -> BytesTransferred,
  1686. 0,
  1687. (SOCKADDR *)&SendContext -> Address,
  1688. sizeof (SOCKADDR_IN),
  1689. &SendContext->Overlapped,
  1690. NULL) == SOCKET_ERROR
  1691. && WSAGetLastError() != WSA_IO_PENDING)
  1692. {
  1693. dwStatus = WSAGetLastError();
  1694. H323DBG(( DEBUG_LEVEL_ERROR, "failed to issue async send on RAS socket" ));
  1695. DumpError (dwStatus);
  1696. //fatal error: shut down the client
  1697. FreeSendBuffer (SendContext);
  1698. Unlock();
  1699. return HRESULT_FROM_WIN32 (dwStatus);
  1700. }
  1701. InsertHeadList( &m_sendPendingList, &SendContext -> ListEntry );
  1702. m_IoRefCount++;
  1703. Unlock();
  1704. H323DBG(( DEBUG_LEVEL_TRACE, "EncodeSendMessage exited:%p.",this ));
  1705. return S_OK;
  1706. }
  1707. // static
  1708. void
  1709. NTAPI RAS_CLIENT::IoCompletionCallback(
  1710. IN DWORD dwStatus,
  1711. IN DWORD BytesTransferred,
  1712. IN OVERLAPPED * Overlapped
  1713. )
  1714. {
  1715. RAS_OVERLAPPED * RasOverlapped;
  1716. RAS_CLIENT * pRASClient;
  1717. H323DBG(( DEBUG_LEVEL_TRACE, "ras-IoCompletionCallback entered." ));
  1718. _ASSERTE( Overlapped );
  1719. RasOverlapped = CONTAINING_RECORD( Overlapped, RAS_OVERLAPPED, Overlapped );
  1720. pRASClient = RasOverlapped -> RasClient;
  1721. switch (RasOverlapped -> Type)
  1722. {
  1723. case OVERLAPPED_TYPE_SEND:
  1724. pRASClient -> OnSendComplete( dwStatus,
  1725. static_cast<RAS_SEND_CONTEXT *> (RasOverlapped));
  1726. break;
  1727. case OVERLAPPED_TYPE_RECV:
  1728. RasOverlapped -> BytesTransferred = BytesTransferred;
  1729. pRASClient -> OnRecvComplete( dwStatus,
  1730. static_cast<RAS_RECV_CONTEXT *> (RasOverlapped));
  1731. break;
  1732. default:
  1733. _ASSERTE(FALSE);
  1734. }
  1735. H323DBG(( DEBUG_LEVEL_TRACE, "ras-IoCompletionCallback exited." ));
  1736. }
  1737. void
  1738. RAS_CLIENT::OnSendComplete(
  1739. IN DWORD dwStatus,
  1740. IN RAS_SEND_CONTEXT * pSendContext
  1741. )
  1742. {
  1743. H323DBG(( DEBUG_LEVEL_TRACE, "OnSendComplete entered:%p.",this ));
  1744. if( dwStatus != ERROR_SUCCESS )
  1745. {
  1746. return;
  1747. }
  1748. Lock();
  1749. m_IoRefCount--;
  1750. //if the RAS client is already shutdown, then reduce the I/O refcount and return.
  1751. if( m_dwState == RAS_CLIENT_STATE_NONE )
  1752. {
  1753. Unlock();
  1754. return;
  1755. }
  1756. //this buffer may have already been freed
  1757. if( IsInList( &m_sendPendingList, &pSendContext->ListEntry ) )
  1758. {
  1759. RemoveEntryList( &pSendContext->ListEntry );
  1760. FreeSendBuffer( pSendContext );
  1761. }
  1762. Unlock();
  1763. H323DBG(( DEBUG_LEVEL_TRACE, "OnSendComplete exited:%p.",this ));
  1764. }
  1765. void
  1766. RAS_CLIENT::OnRecvComplete(
  1767. IN DWORD dwStatus,
  1768. IN RAS_RECV_CONTEXT * RecvContext )
  1769. {
  1770. RasMessage * RasMessage = NULL;
  1771. ASN1error_e AsnError;
  1772. H323DBG(( DEBUG_LEVEL_TRACE, "OnRecvComplete enterd:%p.",this ));
  1773. Lock();
  1774. m_IoRefCount--;
  1775. //if the RAS client is already shutdown, then reduce the I/O refcount and return.
  1776. if( m_dwState == RAS_CLIENT_STATE_NONE )
  1777. {
  1778. Unlock();
  1779. return;
  1780. }
  1781. _ASSERTE(m_recvOverlapped.IsPending);
  1782. m_recvOverlapped.IsPending = FALSE;
  1783. switch( dwStatus )
  1784. {
  1785. case ERROR_SUCCESS:
  1786. //asn decode and process the message
  1787. if( m_recvOverlapped.BytesTransferred != 0 )
  1788. {
  1789. AsnError = ASN1_Decode (
  1790. m_ASNCoderInfo.pDecInfo, // ptr to encoder info
  1791. (PVOID *) &RasMessage, // pdu data structure
  1792. RasMessage_PDU, // pdu id
  1793. ASN1DECODE_SETBUFFER, // flags
  1794. m_recvOverlapped.arBuf, // buffer to decode
  1795. m_recvOverlapped.BytesTransferred); // size of buffer to decode
  1796. //issue another read
  1797. IssueRecv();
  1798. if( ASN1_SUCCEEDED(AsnError) )
  1799. {
  1800. _ASSERTE(RasMessage);
  1801. //This function should be always called in
  1802. //a lock and it unlocks the the ras client
  1803. ProcessRasMessage( RasMessage );
  1804. return;
  1805. }
  1806. else
  1807. {
  1808. H323DBG(( DEBUG_LEVEL_ERROR, "RAS ASNDecode returned error:%d.",
  1809. AsnError ));
  1810. H323DUMPBUFFER( (BYTE*)m_recvOverlapped.arBuf,
  1811. (DWORD)m_recvOverlapped.BytesTransferred);
  1812. }
  1813. }
  1814. break;
  1815. case STATUS_PORT_UNREACHABLE:
  1816. case STATUS_CANCELLED:
  1817. IssueRecv();
  1818. break;
  1819. default:
  1820. H323DBG ((DEBUG_LEVEL_ERROR, "failed to recv data on socket"));
  1821. DumpError (dwStatus);
  1822. break;
  1823. }
  1824. Unlock();
  1825. H323DBG(( DEBUG_LEVEL_TRACE, "OnRecvComplete exited:%p.",this ));
  1826. }
  1827. int
  1828. RAS_CLIENT::InitASNCoder(void)
  1829. {
  1830. int rc;
  1831. H323DBG((DEBUG_LEVEL_TRACE, "InitASNCoder entered: %p.", this ));
  1832. memset((PVOID)&m_ASNCoderInfo, 0, sizeof(m_ASNCoderInfo));
  1833. if( H225ASN_Module == NULL)
  1834. {
  1835. return ASN1_ERR_BADARGS;
  1836. }
  1837. rc = ASN1_CreateEncoder(
  1838. H225ASN_Module, // ptr to mdule
  1839. &(m_ASNCoderInfo.pEncInfo), // ptr to encoder info
  1840. NULL, // buffer ptr
  1841. 0, // buffer size
  1842. NULL); // parent ptr
  1843. if (rc == ASN1_SUCCESS)
  1844. {
  1845. _ASSERTE(m_ASNCoderInfo.pEncInfo );
  1846. rc = ASN1_CreateDecoder(
  1847. H225ASN_Module, // ptr to mdule
  1848. &(m_ASNCoderInfo.pDecInfo), // ptr to decoder info
  1849. NULL, // buffer ptr
  1850. 0, // buffer size
  1851. NULL); // parent ptr
  1852. _ASSERTE(m_ASNCoderInfo.pDecInfo );
  1853. }
  1854. if (rc != ASN1_SUCCESS)
  1855. {
  1856. TermASNCoder();
  1857. }
  1858. H323DBG((DEBUG_LEVEL_TRACE, "InitASNCoder exited: %p.", this ));
  1859. return rc;
  1860. }
  1861. //!!always called in a lock
  1862. int
  1863. RAS_CLIENT::TermASNCoder(void)
  1864. {
  1865. H323DBG(( DEBUG_LEVEL_TRACE, "RAS TermASNCoder entered:%p.",this ));
  1866. if (H225ASN_Module == NULL)
  1867. {
  1868. return ASN1_ERR_BADARGS;
  1869. }
  1870. ASN1_CloseEncoder(m_ASNCoderInfo.pEncInfo);
  1871. ASN1_CloseDecoder(m_ASNCoderInfo.pDecInfo);
  1872. memset( (PVOID)&m_ASNCoderInfo, 0, sizeof(m_ASNCoderInfo));
  1873. H323DBG(( DEBUG_LEVEL_TRACE, "RAS TermASNCoder exited:%p.",this ));
  1874. return ASN1_SUCCESS;
  1875. }
  1876. void
  1877. RAS_CLIENT::HandleRegistryChange()
  1878. {
  1879. PH323_ALIASNAMES pAliasList = NULL;
  1880. PH323_ALIASITEM pAliasItem = NULL;
  1881. PALIASCHANGE_REQUEST pAliasChangeRequest = NULL;
  1882. int iIndex;
  1883. H323DBG(( DEBUG_LEVEL_TRACE, "RAS HandleRegistryChange entered:%p.",this ));
  1884. //If line is not in listening mode then, return
  1885. if( g_pH323Line -> GetState() != H323_LINESTATE_LISTENING )
  1886. return;
  1887. Lock();
  1888. __try
  1889. {
  1890. //If registered with a GK, and GK disabled then send URQ.
  1891. if( g_RegistrySettings.fIsGKEnabled == FALSE )
  1892. {
  1893. RasStop();
  1894. }
  1895. else
  1896. {
  1897. switch( m_RegisterState )
  1898. {
  1899. //If not registered then send RRQ to the GK.
  1900. case RAS_REGISTER_STATE_IDLE:
  1901. //No need to send URQ.
  1902. //Shutdown the object.
  1903. g_RasClient.Shutdown();
  1904. RasStart();
  1905. break;
  1906. case RAS_REGISTER_STATE_REGISTERED:
  1907. case RAS_REGISTER_STATE_RRQSENT:
  1908. if( g_RegistrySettings.saGKAddr.sin_addr.s_addr !=
  1909. m_GKAddress.sin_addr.s_addr )
  1910. {
  1911. //change of GK address
  1912. //send URQ to the old GK and shutdown the RASClinet object.
  1913. RasStop();
  1914. //Initialize the GK object with new settings and send RRQ to the new GK.
  1915. RasStart();
  1916. }
  1917. else
  1918. {
  1919. //check for change in alias list.
  1920. for( iIndex=0; iIndex < m_pAliasList->wCount; iIndex++ )
  1921. {
  1922. pAliasItem = &(m_pAliasList->pItems[iIndex]);
  1923. if( pAliasItem->wType == e164_chosen )
  1924. {
  1925. if( g_RegistrySettings.fIsGKLogOnPhoneEnabled == FALSE )
  1926. {
  1927. break;
  1928. }
  1929. else if( memcmp(
  1930. (PVOID)g_RegistrySettings.wszGKLogOnPhone,
  1931. pAliasItem->pData,
  1932. (pAliasItem->wDataLength+1) * sizeof(WCHAR) ) != 0 )
  1933. {
  1934. break;
  1935. }
  1936. }
  1937. else if( pAliasItem->wType == h323_ID_chosen )
  1938. {
  1939. if( g_RegistrySettings.fIsGKLogOnAccountEnabled==FALSE )
  1940. {
  1941. break;
  1942. }
  1943. else if( memcmp(
  1944. (PVOID)g_RegistrySettings.wszGKLogOnAccount,
  1945. pAliasItem->pData,
  1946. (pAliasItem->wDataLength+1) * sizeof(WCHAR) ) != 0 )
  1947. {
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. if( (iIndex < m_pAliasList->wCount ) ||
  1953. ( m_pAliasList->wCount !=
  1954. (g_RegistrySettings.fIsGKLogOnPhoneEnabled +
  1955. g_RegistrySettings.fIsGKLogOnAccountEnabled
  1956. )
  1957. )
  1958. )
  1959. {
  1960. //create the new alias list.
  1961. pAliasList = new H323_ALIASNAMES;
  1962. if( pAliasList != NULL )
  1963. {
  1964. ZeroMemory( (PVOID)pAliasList, sizeof(H323_ALIASNAMES) );
  1965. if( g_RegistrySettings.fIsGKLogOnPhoneEnabled )
  1966. {
  1967. if( !AddAliasItem( pAliasList,
  1968. g_RegistrySettings.wszGKLogOnPhone,
  1969. e164_chosen ) )
  1970. {
  1971. goto cleanup;
  1972. }
  1973. }
  1974. if( g_RegistrySettings.fIsGKLogOnAccountEnabled )
  1975. {
  1976. if( !AddAliasItem( pAliasList,
  1977. g_RegistrySettings.wszGKLogOnAccount,
  1978. h323_ID_chosen ) )
  1979. {
  1980. goto cleanup;
  1981. }
  1982. }
  1983. //queue the alias change request in the list
  1984. pAliasChangeRequest = new ALIASCHANGE_REQUEST;
  1985. if( pAliasChangeRequest == NULL )
  1986. {
  1987. goto cleanup;
  1988. }
  1989. pAliasChangeRequest->rasEndpointID.length = m_RASEndpointID.length;
  1990. CopyMemory( (PVOID)pAliasChangeRequest->rasEndpointID.value,
  1991. m_RASEndpointID.value,
  1992. (pAliasChangeRequest->rasEndpointID.length+1)*sizeof(WCHAR) );
  1993. pAliasChangeRequest->wRequestSeqNum = 0;
  1994. pAliasChangeRequest->pAliasList = pAliasList;
  1995. //Send RRQ with the new alias list.
  1996. if( !SendRRQ(NOT_RESEND_SEQ_NUM, pAliasChangeRequest) )
  1997. {
  1998. goto cleanup;
  1999. }
  2000. InsertHeadList( &m_aliasChangeRequestList,
  2001. &pAliasChangeRequest->listEntry );
  2002. }
  2003. }
  2004. }
  2005. break;
  2006. default:
  2007. //Shutdown the RASClinet object. Send RRQ to new GK.
  2008. RasStop();
  2009. RasStart();
  2010. break;
  2011. }
  2012. }
  2013. }
  2014. __except(1)
  2015. {
  2016. H323DBG(( DEBUG_LEVEL_TRACE, "except in HandleRegistryChange :%p.", this ));
  2017. _ASSERTE(0);
  2018. }
  2019. Unlock();
  2020. H323DBG(( DEBUG_LEVEL_TRACE, "RAS HandleRegistryChange exited:%p.", this ));
  2021. return;
  2022. cleanup:
  2023. if( pAliasList != NULL )
  2024. {
  2025. FreeAliasNames( pAliasList );
  2026. }
  2027. if( pAliasChangeRequest != NULL )
  2028. {
  2029. delete pAliasChangeRequest;
  2030. }
  2031. Unlock();
  2032. }