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.

1103 lines
30 KiB

  1. /****************************************************************************
  2. *
  3. * $Archive: S:\sturgeon\src\q931\vcs\utils.c_v $
  4. *
  5. * INTEL Corporation Prorietary Information
  6. *
  7. * This listing is supplied under the terms of a license agreement
  8. * with INTEL Corporation and may not be copied nor disclosed except
  9. * in accordance with the terms of that agreement.
  10. *
  11. * Copyright (c) 1993-1996 Intel Corporation.
  12. *
  13. * $Revision: 1.33 $
  14. * $Date: 23 Jan 1997 20:42:54 $
  15. * $Author: SBELL1 $
  16. *
  17. * Deliverable:
  18. *
  19. * Abstract:
  20. *
  21. *
  22. * Notes:
  23. *
  24. ***************************************************************************/
  25. #pragma warning ( disable : 4115 4201 4214 4514 )
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #include <nt.h>
  30. #include <ntrtl.h>
  31. #include <nturtl.h>
  32. #include <windows.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. #include "common.h"
  36. #include "q931.h"
  37. #include "isrg.h"
  38. #include "utils.h"
  39. #include "linkapi.h"
  40. //====================================================================================
  41. //===============/=====================================================================
  42. void
  43. Q931MakePhysicalID(DWORD *pdwPhysicalID)
  44. {
  45. *pdwPhysicalID = INVALID_PHYS_ID;
  46. }
  47. //====================================================================================
  48. //====================================================================================
  49. WORD
  50. ADDRToInetPort(CC_ADDR *pAddr)
  51. {
  52. WORD HostPort = 0;
  53. switch (pAddr->nAddrType)
  54. {
  55. case CC_IP_DOMAIN_NAME:
  56. HostPort = pAddr->Addr.IP_DomainName.wPort;
  57. break;
  58. case CC_IP_DOT:
  59. HostPort = pAddr->Addr.IP_Dot.wPort;
  60. break;
  61. case CC_IP_BINARY:
  62. HostPort = pAddr->Addr.IP_Binary.wPort;
  63. break;
  64. }
  65. return htons(HostPort);
  66. }
  67. //====================================================================================
  68. //====================================================================================
  69. DWORD
  70. ADDRToInetAddr(CC_ADDR *pAddr)
  71. {
  72. struct hostent *pHostEnt;
  73. switch (pAddr->nAddrType)
  74. {
  75. case CC_IP_DOMAIN_NAME:
  76. {
  77. char buf[sizeof(pAddr->Addr.IP_DomainName.cAddr) / sizeof(WCHAR)];
  78. WideCharToMultiByte(CP_ACP, 0, pAddr->Addr.IP_DomainName.cAddr, -1, buf, sizeof(buf), NULL, NULL);
  79. pHostEnt = gethostbyname(buf);
  80. }
  81. if (pHostEnt == NULL || pHostEnt->h_addr_list == NULL)
  82. {
  83. return htonl(0L);
  84. }
  85. return *((DWORD *)pHostEnt->h_addr_list[0]);
  86. case CC_IP_DOT:
  87. {
  88. char buf[sizeof(pAddr->Addr.IP_Dot.cAddr) / sizeof(WCHAR)];
  89. WideCharToMultiByte(CP_ACP, 0, pAddr->Addr.IP_Dot.cAddr, -1, buf, sizeof(buf), NULL, NULL);
  90. return inet_addr(buf);
  91. }
  92. case CC_IP_BINARY:
  93. return htonl(pAddr->Addr.IP_Binary.dwAddr);
  94. }
  95. return 0L;
  96. }
  97. //====================================================================================
  98. // If Port 0 is passed in, use default listen port.
  99. //====================================================================================
  100. void
  101. SetDefaultPort(CC_ADDR *pAddr)
  102. {
  103. switch (pAddr->nAddrType)
  104. {
  105. case CC_IP_DOMAIN_NAME:
  106. if (pAddr->Addr.IP_DomainName.wPort == 0)
  107. {
  108. pAddr->Addr.IP_DomainName.wPort = CC_H323_HOST_CALL;
  109. }
  110. return;
  111. case CC_IP_DOT:
  112. if (pAddr->Addr.IP_Dot.wPort == 0)
  113. {
  114. pAddr->Addr.IP_Dot.wPort = CC_H323_HOST_CALL;
  115. }
  116. return;
  117. case CC_IP_BINARY:
  118. if (pAddr->Addr.IP_Binary.wPort == 0)
  119. {
  120. pAddr->Addr.IP_Binary.wPort = CC_H323_HOST_CALL;
  121. }
  122. return;
  123. }
  124. return;
  125. }
  126. //====================================================================================
  127. //====================================================================================
  128. BOOL
  129. MakeBinaryADDR(CC_ADDR *pInAddr, CC_ADDR *pOutAddr)
  130. {
  131. if (pOutAddr == NULL)
  132. {
  133. return FALSE;
  134. }
  135. memset(pOutAddr, 0, sizeof(CC_ADDR));
  136. if (pInAddr == NULL)
  137. {
  138. return FALSE;
  139. }
  140. pOutAddr->nAddrType = CC_IP_BINARY;
  141. pOutAddr->bMulticast = pInAddr->bMulticast;
  142. switch (pInAddr->nAddrType)
  143. {
  144. case CC_IP_DOMAIN_NAME:
  145. {
  146. struct hostent *pHostEnt;
  147. DWORD net_addr;
  148. {
  149. char buf[sizeof(pInAddr->Addr.IP_DomainName.cAddr) / sizeof(WCHAR)];
  150. WideCharToMultiByte(CP_ACP, 0, pInAddr->Addr.IP_DomainName.cAddr, -1, buf, sizeof(buf), NULL, NULL);
  151. if (buf[0] == '\0')
  152. {
  153. return FALSE;
  154. }
  155. pHostEnt = gethostbyname(buf);
  156. }
  157. if (pHostEnt == NULL || pHostEnt->h_addr_list == NULL)
  158. {
  159. return FALSE;
  160. }
  161. net_addr = *((DWORD *)pHostEnt->h_addr_list[0]);
  162. pOutAddr->Addr.IP_Binary.wPort = pInAddr->Addr.IP_DomainName.wPort;
  163. pOutAddr->Addr.IP_Binary.dwAddr = ntohl(net_addr);
  164. }
  165. break;
  166. case CC_IP_DOT:
  167. {
  168. char buf[sizeof(pInAddr->Addr.IP_Dot.cAddr) / sizeof(WCHAR)];
  169. WideCharToMultiByte(CP_ACP, 0, pInAddr->Addr.IP_Dot.cAddr, -1, buf, sizeof(buf), NULL, NULL);
  170. if (buf[0] == '\0')
  171. {
  172. return FALSE;
  173. }
  174. pOutAddr->Addr.IP_Binary.dwAddr = ntohl(inet_addr(buf));
  175. }
  176. pOutAddr->Addr.IP_Binary.wPort = pInAddr->Addr.IP_Dot.wPort;
  177. break;
  178. default:
  179. pOutAddr->Addr.IP_Binary.wPort = pInAddr->Addr.IP_Binary.wPort;
  180. pOutAddr->Addr.IP_Binary.dwAddr = pInAddr->Addr.IP_Binary.dwAddr;
  181. break;
  182. }
  183. return TRUE;
  184. }
  185. //====================================================================================
  186. //====================================================================================
  187. void
  188. GetDomainAddr(CC_ADDR *pAddr)
  189. {
  190. WORD wTemp;
  191. char szHostName[80];
  192. if (gethostname(szHostName, sizeof(szHostName)) != SOCKET_ERROR)
  193. {
  194. wTemp = pAddr->Addr.IP_Binary.wPort;
  195. pAddr->nAddrType = CC_IP_DOMAIN_NAME;
  196. MultiByteToWideChar(CP_ACP, 0, szHostName, -1,
  197. pAddr->Addr.IP_DomainName.cAddr,
  198. sizeof(pAddr->Addr.IP_DomainName.cAddr) /
  199. sizeof(pAddr->Addr.IP_DomainName.cAddr[0]));
  200. pAddr->Addr.IP_DomainName.wPort = wTemp;
  201. }
  202. }
  203. //====================================================================================
  204. //====================================================================================
  205. CS_STATUS
  206. Q931ValidateAddr(PCC_ADDR pAddr)
  207. {
  208. if (pAddr == NULL)
  209. {
  210. return CS_OK;
  211. }
  212. if ((pAddr->nAddrType != CC_IP_DOMAIN_NAME) &&
  213. (pAddr->nAddrType != CC_IP_DOT) &&
  214. (pAddr->nAddrType != CC_IP_BINARY))
  215. {
  216. return CS_BAD_PARAM;
  217. }
  218. if (pAddr->nAddrType == CC_IP_DOT)
  219. {
  220. WCHAR *p = pAddr->Addr.IP_Dot.cAddr;
  221. while (*p)
  222. {
  223. if (wcschr((const WCHAR *)CC_ODOTTO_CHARS, *p) == NULL)
  224. {
  225. return CS_BAD_PARAM;
  226. }
  227. p++;
  228. }
  229. }
  230. if (pAddr->bMulticast == TRUE)
  231. {
  232. return CS_BAD_PARAM;
  233. }
  234. return CS_OK;
  235. }
  236. //====================================================================================
  237. //====================================================================================
  238. CS_STATUS
  239. Q931ValidateAliasItem(PCC_ALIASITEM pSource)
  240. {
  241. register unsigned int y;
  242. if (pSource)
  243. {
  244. if ((pSource->pData == NULL) || (pSource->wDataLength == 0))
  245. {
  246. return CS_BAD_PARAM;
  247. }
  248. if (pSource->pPrefix != NULL)
  249. {
  250. if (pSource->wPrefixLength == 0)
  251. {
  252. return CS_BAD_PARAM;
  253. }
  254. }
  255. else if (pSource->wPrefixLength != 0)
  256. {
  257. return CS_BAD_PARAM;
  258. }
  259. switch (pSource->wType)
  260. {
  261. case CC_ALIAS_H323_ID:
  262. if ((pSource->wDataLength + pSource->wPrefixLength) > CC_ALIAS_MAX_H323_ID)
  263. {
  264. return CS_BAD_PARAM;
  265. }
  266. break;
  267. case CC_ALIAS_H323_PHONE:
  268. if ((pSource->wDataLength + pSource->wPrefixLength +1) > CC_ALIAS_MAX_H323_PHONE)
  269. {
  270. return CS_BAD_PARAM;
  271. }
  272. for (y = 0; y < pSource->wDataLength; ++y)
  273. {
  274. if (wcschr((const WCHAR *)CC_ALIAS_H323_PHONE_CHARS, pSource->pData[y]) == NULL)
  275. {
  276. return CS_BAD_PARAM;
  277. }
  278. }
  279. if (pSource->pPrefix != NULL)
  280. {
  281. for (y = 0; y < pSource->wPrefixLength; ++y)
  282. {
  283. if (wcschr((const WCHAR *)CC_ALIAS_H323_PHONE_CHARS, pSource->pPrefix[y]) == NULL)
  284. {
  285. return CS_BAD_PARAM;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. return CS_OK;
  292. }
  293. //====================================================================================
  294. //====================================================================================
  295. CS_STATUS
  296. Q931CopyAliasItem(PCC_ALIASITEM *ppTarget, PCC_ALIASITEM pSource)
  297. {
  298. PCC_ALIASITEM pNewItem = NULL;
  299. if (ppTarget == NULL)
  300. {
  301. return CS_BAD_PARAM;
  302. }
  303. *ppTarget = NULL;
  304. if (pSource == NULL)
  305. {
  306. return CS_OK;
  307. }
  308. pNewItem = (PCC_ALIASITEM)Malloc(sizeof(CC_ALIASITEM));
  309. if (pNewItem == NULL)
  310. {
  311. return CS_NO_MEMORY;
  312. }
  313. pNewItem->wType = pSource->wType;
  314. if ((pSource->wPrefixLength != 0) && (pSource->pPrefix != NULL))
  315. {
  316. pNewItem->wPrefixLength = pSource->wPrefixLength;
  317. pNewItem->pPrefix = (LPWSTR)Malloc(pSource->wPrefixLength * sizeof(pNewItem->pPrefix[0]));
  318. if (pNewItem->pPrefix == NULL)
  319. {
  320. Free(pNewItem);
  321. return CS_NO_MEMORY;
  322. }
  323. memcpy(pNewItem->pPrefix, pSource->pPrefix, pSource->wPrefixLength * sizeof(WCHAR));
  324. }
  325. else
  326. {
  327. pNewItem->wPrefixLength = 0;
  328. pNewItem->pPrefix = NULL;
  329. }
  330. if ((pSource->wDataLength != 0) && (pSource->pData != NULL))
  331. {
  332. pNewItem->wDataLength = pSource->wDataLength;
  333. pNewItem->pData = (LPWSTR)Malloc(pSource->wDataLength * sizeof(pNewItem->pData[0]));
  334. if (pNewItem->pData == NULL)
  335. {
  336. if (pNewItem->pPrefix)
  337. {
  338. Free(pNewItem->pPrefix);
  339. }
  340. Free(pNewItem);
  341. return CS_NO_MEMORY;
  342. }
  343. memcpy(pNewItem->pData, pSource->pData, pSource->wDataLength * sizeof(WCHAR));
  344. }
  345. else
  346. {
  347. pNewItem->wDataLength = 0;
  348. pNewItem->pData = NULL;
  349. }
  350. *ppTarget = pNewItem;
  351. return CS_OK;
  352. }
  353. //====================================================================================
  354. //====================================================================================
  355. CS_STATUS
  356. Q931FreeAliasItem(PCC_ALIASITEM pSource)
  357. {
  358. if (pSource)
  359. {
  360. if ((pSource->pPrefix) != NULL)
  361. {
  362. Free(pSource->pPrefix);
  363. }
  364. if ((pSource->pData) != NULL)
  365. {
  366. Free(pSource->pData);
  367. }
  368. Free(pSource);
  369. }
  370. return CS_OK;
  371. }
  372. //====================================================================================
  373. //====================================================================================
  374. CS_STATUS
  375. Q931ValidateAliasNames(PCC_ALIASNAMES pSource)
  376. {
  377. CS_STATUS TempResult = CS_OK;
  378. WORD x;
  379. if (pSource)
  380. {
  381. for (x = 0; x < pSource->wCount; x++)
  382. {
  383. TempResult = Q931ValidateAliasItem(&(pSource->pItems[x]));
  384. if (TempResult != CS_OK)
  385. {
  386. return TempResult;
  387. }
  388. }
  389. }
  390. return CS_OK;
  391. }
  392. //====================================================================================
  393. //====================================================================================
  394. CS_STATUS
  395. Q931CopyAliasNames(PCC_ALIASNAMES *ppTarget, PCC_ALIASNAMES pSource)
  396. {
  397. if (ppTarget == NULL)
  398. {
  399. return CS_BAD_PARAM;
  400. }
  401. *ppTarget = NULL;
  402. if (pSource == NULL)
  403. {
  404. return CS_OK;
  405. }
  406. if (pSource->wCount)
  407. {
  408. WORD x;
  409. *ppTarget = (PCC_ALIASNAMES)Malloc(sizeof(CC_ALIASNAMES));
  410. if (*ppTarget == NULL)
  411. {
  412. return CS_NO_MEMORY;
  413. }
  414. (*ppTarget)->pItems = (PCC_ALIASITEM)Malloc(pSource->wCount *
  415. sizeof(CC_ALIASITEM));
  416. if ((*ppTarget)->pItems == NULL)
  417. {
  418. Free(*ppTarget);
  419. *ppTarget = NULL;
  420. return CS_NO_MEMORY;
  421. }
  422. (*ppTarget)->wCount = pSource->wCount;
  423. {
  424. PCC_ALIASITEM p = (*ppTarget)->pItems;
  425. for (x = 0; x < pSource->wCount; x++)
  426. {
  427. p[x].wType = pSource->pItems[x].wType;
  428. if ((pSource->pItems[x].wPrefixLength != 0) &&
  429. (pSource->pItems[x].pPrefix != NULL))
  430. {
  431. p[x].wPrefixLength = pSource->pItems[x].wPrefixLength;
  432. p[x].pPrefix = (LPWSTR)Malloc(pSource->pItems[x].wPrefixLength * sizeof(p[x].pPrefix[0]));
  433. if (p[x].pPrefix == NULL)
  434. {
  435. // Free everything that has been allocated so far...
  436. int y;
  437. for (y = 0; y < x; y++)
  438. {
  439. if (p[y].pPrefix)
  440. {
  441. Free(p[y].pPrefix);
  442. }
  443. if (p[y].pData)
  444. {
  445. Free(p[y].pData);
  446. }
  447. }
  448. Free(p);
  449. Free(*ppTarget);
  450. *ppTarget = NULL;
  451. return CS_NO_MEMORY;
  452. }
  453. memcpy(p[x].pPrefix, pSource->pItems[x].pPrefix,
  454. pSource->pItems[x].wPrefixLength * sizeof(WCHAR));
  455. }
  456. else
  457. {
  458. p[x].wPrefixLength = 0;
  459. p[x].pPrefix = NULL;
  460. }
  461. if ((pSource->pItems[x].wDataLength != 0) &&
  462. (pSource->pItems[x].pData != NULL))
  463. {
  464. p[x].wDataLength = pSource->pItems[x].wDataLength;
  465. p[x].pData = (LPWSTR)Malloc(pSource->pItems[x].wDataLength * sizeof(p[x].pData[0]));
  466. if (p[x].pData == NULL)
  467. {
  468. // Free everything that has been allocated so far...
  469. int y;
  470. if (p[x].pPrefix)
  471. {
  472. Free(p[x].pPrefix);
  473. }
  474. for (y = 0; y < x; y++)
  475. {
  476. if (p[y].pPrefix)
  477. {
  478. Free(p[y].pPrefix);
  479. }
  480. if (p[y].pData)
  481. {
  482. Free(p[y].pData);
  483. }
  484. }
  485. Free(p);
  486. Free(*ppTarget);
  487. *ppTarget = NULL;
  488. return CS_NO_MEMORY;
  489. }
  490. memcpy(p[x].pData, pSource->pItems[x].pData,
  491. pSource->pItems[x].wDataLength * sizeof(WCHAR));
  492. }
  493. else
  494. {
  495. p[x].wDataLength = 0;
  496. p[x].pData = NULL;
  497. }
  498. }
  499. }
  500. }
  501. return CS_OK;
  502. }
  503. //====================================================================================
  504. //====================================================================================
  505. CS_STATUS
  506. Q931FreeAliasNames(PCC_ALIASNAMES pSource)
  507. {
  508. if (pSource && (pSource->wCount))
  509. {
  510. // Free everything that has been allocated so far...
  511. int x;
  512. for (x = 0; x < pSource->wCount; x++)
  513. {
  514. if ((pSource->pItems[x].pPrefix) != NULL)
  515. {
  516. Free(pSource->pItems[x].pPrefix);
  517. }
  518. if ((pSource->pItems[x].pData) != NULL)
  519. {
  520. Free(pSource->pItems[x].pData);
  521. }
  522. }
  523. if (pSource->pItems != NULL)
  524. {
  525. Free(pSource->pItems);
  526. }
  527. if (pSource != NULL)
  528. {
  529. Free(pSource);
  530. }
  531. }
  532. return CS_OK;
  533. }
  534. //====================================================================================
  535. //====================================================================================
  536. CS_STATUS
  537. Q931ValidateDisplay(LPWSTR pszDisplay)
  538. {
  539. if (pszDisplay == NULL)
  540. {
  541. return CS_OK;
  542. }
  543. if (wcslen(pszDisplay) > CC_MAX_DISPLAY_LENGTH)
  544. {
  545. return CS_BAD_PARAM;
  546. }
  547. #if 0 // turn this on to validate display field against IA5 characters...
  548. while (*pszDisplay)
  549. {
  550. if (wcschr(CC_UNICODE_IA5_CHARS, *pszDisplay) == NULL)
  551. {
  552. return CS_BAD_PARAM;
  553. }
  554. pszDisplay++;
  555. }
  556. #endif
  557. return CS_OK;
  558. }
  559. //====================================================================================
  560. //====================================================================================
  561. CS_STATUS
  562. Q931ValidatePartyNumber(LPWSTR pszPartyNumber)
  563. {
  564. if (pszPartyNumber == NULL)
  565. {
  566. return CS_OK;
  567. }
  568. if (wcslen(pszPartyNumber) > CC_MAX_PARTY_NUMBER_LEN)
  569. {
  570. return CS_BAD_PARAM;
  571. }
  572. #if 0 // turn this on to validate party number field against IA5 characters...
  573. while (*pszPartyNumber)
  574. {
  575. if (wcschr(CC_UNICODE_IA5_CHARS, *pszPartyNumber) == NULL)
  576. {
  577. return CS_BAD_PARAM;
  578. }
  579. pszPartyNumber++;
  580. }
  581. #endif
  582. return CS_OK;
  583. }
  584. //====================================================================================
  585. //====================================================================================
  586. CS_STATUS
  587. Q931CopyDisplay(LPWSTR *ppDest, LPWSTR pSource)
  588. {
  589. if (ppDest == NULL)
  590. {
  591. ASSERT(FALSE);
  592. return CS_BAD_PARAM;
  593. }
  594. if (pSource == NULL)
  595. {
  596. *ppDest = NULL;
  597. return CS_OK;
  598. }
  599. *ppDest = (LPWSTR)Malloc((wcslen(pSource) + 1) * sizeof(WCHAR));
  600. if (*ppDest == NULL)
  601. {
  602. return CS_NO_MEMORY;
  603. }
  604. wcscpy(*ppDest, pSource);
  605. return CS_OK;
  606. }
  607. //====================================================================================
  608. //====================================================================================
  609. CS_STATUS
  610. Q931FreeDisplay(LPWSTR pszDisplay)
  611. {
  612. if (pszDisplay == NULL)
  613. {
  614. return CS_OK;
  615. }
  616. Free(pszDisplay);
  617. return CS_OK;
  618. }
  619. //====================================================================================
  620. //====================================================================================
  621. CS_STATUS
  622. Q931ValidateVendorInfo(PCC_VENDORINFO pVendorInfo)
  623. {
  624. if (pVendorInfo == NULL)
  625. {
  626. return CS_OK;
  627. }
  628. if (pVendorInfo->pProductNumber)
  629. {
  630. if (pVendorInfo->pProductNumber->wOctetStringLength)
  631. {
  632. if (pVendorInfo->pProductNumber->wOctetStringLength > CC_MAX_PRODUCT_LENGTH)
  633. {
  634. return CS_BAD_PARAM;
  635. }
  636. if (pVendorInfo->pProductNumber->pOctetString == NULL)
  637. {
  638. return CS_BAD_PARAM;
  639. }
  640. }
  641. else if (pVendorInfo->pProductNumber->pOctetString)
  642. {
  643. return CS_BAD_PARAM;
  644. }
  645. }
  646. if (pVendorInfo->pVersionNumber)
  647. {
  648. if (pVendorInfo->pVersionNumber->wOctetStringLength)
  649. {
  650. if (pVendorInfo->pVersionNumber->wOctetStringLength > CC_MAX_VERSION_LENGTH)
  651. {
  652. return CS_BAD_PARAM;
  653. }
  654. if (pVendorInfo->pVersionNumber->pOctetString == NULL)
  655. {
  656. return CS_BAD_PARAM;
  657. }
  658. }
  659. else if (pVendorInfo->pVersionNumber->pOctetString)
  660. {
  661. return CS_BAD_PARAM;
  662. }
  663. }
  664. return CS_OK;
  665. }
  666. //====================================================================================
  667. //====================================================================================
  668. CS_STATUS
  669. Q931CopyVendorInfo(PCC_VENDORINFO *ppDest, PCC_VENDORINFO pSource)
  670. {
  671. if (ppDest == NULL)
  672. {
  673. ASSERT(FALSE);
  674. return CS_BAD_PARAM;
  675. }
  676. if (pSource == NULL)
  677. {
  678. *ppDest = NULL;
  679. return CS_OK;
  680. }
  681. *ppDest = (PCC_VENDORINFO)Malloc(sizeof(CC_VENDORINFO));
  682. if (*ppDest == NULL)
  683. {
  684. return CS_NO_MEMORY;
  685. }
  686. memset(*ppDest, 0, sizeof(CC_VENDORINFO));
  687. (*ppDest)->bCountryCode = pSource->bCountryCode;
  688. (*ppDest)->bExtension = pSource->bExtension;
  689. (*ppDest)->wManufacturerCode = pSource->wManufacturerCode;
  690. if ((pSource->pProductNumber == NULL) ||
  691. (pSource->pProductNumber->pOctetString == NULL) ||
  692. (pSource->pProductNumber->wOctetStringLength == 0))
  693. {
  694. (*ppDest)->pProductNumber = NULL;
  695. }
  696. else
  697. {
  698. (*ppDest)->pProductNumber = (PCC_OCTETSTRING)Malloc(sizeof(CC_OCTETSTRING));
  699. if ((*ppDest)->pProductNumber == NULL)
  700. {
  701. Q931FreeVendorInfo(*ppDest);
  702. *ppDest = NULL;
  703. return CS_NO_MEMORY;
  704. }
  705. memset((*ppDest)->pProductNumber, 0, sizeof(CC_OCTETSTRING));
  706. (*ppDest)->pProductNumber->pOctetString =
  707. (BYTE *)Malloc(pSource->pProductNumber->wOctetStringLength);
  708. if ((*ppDest)->pProductNumber->pOctetString == NULL)
  709. {
  710. Q931FreeVendorInfo(*ppDest);
  711. *ppDest = NULL;
  712. return CS_NO_MEMORY;
  713. }
  714. (*ppDest)->pProductNumber->wOctetStringLength =
  715. pSource->pProductNumber->wOctetStringLength;
  716. memcpy((*ppDest)->pProductNumber->pOctetString,
  717. pSource->pProductNumber->pOctetString,
  718. pSource->pProductNumber->wOctetStringLength);
  719. }
  720. if ((pSource->pVersionNumber == NULL) ||
  721. (pSource->pVersionNumber->pOctetString == NULL) ||
  722. (pSource->pVersionNumber->wOctetStringLength == 0))
  723. {
  724. (*ppDest)->pVersionNumber = NULL;
  725. }
  726. else
  727. {
  728. (*ppDest)->pVersionNumber = (PCC_OCTETSTRING)Malloc(sizeof(CC_OCTETSTRING));
  729. if ((*ppDest)->pVersionNumber == NULL)
  730. {
  731. Q931FreeVendorInfo(*ppDest);
  732. *ppDest = NULL;
  733. return CS_NO_MEMORY;
  734. }
  735. memset((*ppDest)->pVersionNumber, 0, sizeof(CC_OCTETSTRING));
  736. (*ppDest)->pVersionNumber->pOctetString =
  737. (BYTE *)Malloc(pSource->pVersionNumber->wOctetStringLength);
  738. if ((*ppDest)->pVersionNumber->pOctetString == NULL)
  739. {
  740. Q931FreeVendorInfo(*ppDest);
  741. *ppDest = NULL;
  742. return CS_NO_MEMORY;
  743. }
  744. (*ppDest)->pVersionNumber->wOctetStringLength =
  745. pSource->pVersionNumber->wOctetStringLength;
  746. memcpy((*ppDest)->pVersionNumber->pOctetString,
  747. pSource->pVersionNumber->pOctetString,
  748. pSource->pVersionNumber->wOctetStringLength);
  749. }
  750. return CS_OK;
  751. }
  752. //====================================================================================
  753. //====================================================================================
  754. CS_STATUS
  755. Q931FreeVendorInfo(PCC_VENDORINFO pVendorInfo)
  756. {
  757. if (pVendorInfo == NULL)
  758. {
  759. return CS_OK;
  760. }
  761. if (pVendorInfo->pProductNumber != NULL)
  762. {
  763. if (pVendorInfo->pProductNumber->pOctetString != NULL)
  764. {
  765. Free(pVendorInfo->pProductNumber->pOctetString);
  766. }
  767. Free(pVendorInfo->pProductNumber);
  768. }
  769. if (pVendorInfo->pVersionNumber != NULL)
  770. {
  771. if (pVendorInfo->pVersionNumber->pOctetString != NULL)
  772. {
  773. Free(pVendorInfo->pVersionNumber->pOctetString);
  774. }
  775. Free(pVendorInfo->pVersionNumber);
  776. }
  777. Free(pVendorInfo);
  778. return CS_OK;
  779. }
  780. //====================================================================================
  781. //====================================================================================
  782. CS_STATUS
  783. Q931ValidateNonStandardData(PCC_NONSTANDARDDATA pNonStandardData)
  784. {
  785. if (pNonStandardData)
  786. {
  787. if ((pNonStandardData->sData.pOctetString == NULL) ||
  788. (pNonStandardData->sData.wOctetStringLength == 0))
  789. {
  790. return CS_BAD_PARAM;
  791. }
  792. }
  793. return CS_OK;
  794. }
  795. //====================================================================================
  796. //====================================================================================
  797. CS_STATUS
  798. Q931CopyNonStandardData(PCC_NONSTANDARDDATA *ppDest, PCC_NONSTANDARDDATA pSource)
  799. {
  800. if (ppDest == NULL)
  801. {
  802. ASSERT(FALSE);
  803. return CS_BAD_PARAM;
  804. }
  805. if (pSource == NULL)
  806. {
  807. *ppDest = NULL;
  808. return CS_OK;
  809. }
  810. *ppDest = (PCC_NONSTANDARDDATA)Malloc(sizeof(CC_NONSTANDARDDATA));
  811. if (*ppDest == NULL)
  812. {
  813. return CS_NO_MEMORY;
  814. }
  815. (*ppDest)->bCountryCode = pSource->bCountryCode;
  816. (*ppDest)->bExtension = pSource->bExtension;
  817. (*ppDest)->wManufacturerCode = pSource->wManufacturerCode;
  818. (*ppDest)->sData.wOctetStringLength = pSource->sData.wOctetStringLength;
  819. if (pSource->sData.pOctetString == NULL)
  820. {
  821. (*ppDest)->sData.pOctetString = NULL;
  822. }
  823. else
  824. {
  825. (*ppDest)->sData.pOctetString = (void *)Malloc(pSource->sData.wOctetStringLength);
  826. if ((*ppDest)->sData.pOctetString == NULL)
  827. {
  828. Free(*ppDest);
  829. *ppDest = NULL;
  830. return CS_NO_MEMORY;
  831. }
  832. memcpy((*ppDest)->sData.pOctetString, pSource->sData.pOctetString,
  833. pSource->sData.wOctetStringLength);
  834. }
  835. return CS_OK;
  836. }
  837. //====================================================================================
  838. //====================================================================================
  839. CS_STATUS
  840. Q931FreeNonStandardData(PCC_NONSTANDARDDATA pNonStandardData)
  841. {
  842. if (pNonStandardData == NULL)
  843. {
  844. return CS_OK;
  845. }
  846. if (pNonStandardData->sData.pOctetString != NULL)
  847. {
  848. Free(pNonStandardData->sData.pOctetString);
  849. }
  850. Free(pNonStandardData);
  851. return CS_OK;
  852. }
  853. #if defined(DBG) && !defined(ISRDBG)
  854. DWORD g_dwQ931DbgLevel = 0;
  855. BOOL g_fQ931DbgInitialized = FALSE;
  856. void Q931DbgInit() {
  857. #define H323_REGKEY_ROOT \
  858. TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\H323TSP")
  859. #define H323_REGVAL_DEBUGLEVEL \
  860. TEXT("DebugLevel")
  861. #define H323_REGVAL_Q931DEBUGLEVEL \
  862. TEXT("Q931DebugLevel")
  863. HKEY hKey;
  864. LONG lStatus;
  865. DWORD dwValue;
  866. DWORD dwValueSize;
  867. DWORD dwValueType;
  868. LPSTR pszValue;
  869. LPSTR pszKey = H323_REGKEY_ROOT;
  870. // only call this once
  871. g_fQ931DbgInitialized = TRUE;
  872. // open registry subkey
  873. lStatus = RegOpenKeyEx(
  874. HKEY_LOCAL_MACHINE,
  875. pszKey,
  876. 0,
  877. KEY_READ,
  878. &hKey
  879. );
  880. // validate return code
  881. if (lStatus != ERROR_SUCCESS) {
  882. return; // bail...
  883. }
  884. // initialize values
  885. dwValueType = REG_DWORD;
  886. dwValueSize = sizeof(DWORD);
  887. // retrieve q931 debug level
  888. pszValue = H323_REGVAL_Q931DEBUGLEVEL;
  889. // query for registry value
  890. lStatus = RegQueryValueEx(
  891. hKey,
  892. pszValue,
  893. NULL,
  894. &dwValueType,
  895. (LPBYTE)&dwValue,
  896. &dwValueSize
  897. );
  898. // validate return code
  899. if (lStatus != ERROR_SUCCESS) {
  900. // initialize values
  901. dwValueType = REG_DWORD;
  902. dwValueSize = sizeof(DWORD);
  903. // retrieve tsp debug level
  904. pszValue = H323_REGVAL_DEBUGLEVEL;
  905. // query for registry value
  906. lStatus = RegQueryValueEx(
  907. hKey,
  908. pszValue,
  909. NULL,
  910. &dwValueType,
  911. (LPBYTE)&dwValue,
  912. &dwValueSize
  913. );
  914. }
  915. // validate return code
  916. if (lStatus == ERROR_SUCCESS) {
  917. // update debug level
  918. g_dwQ931DbgLevel = dwValue;
  919. }
  920. // close key
  921. RegCloseKey(hKey);
  922. }
  923. void Q931DbgPrint(DWORD dwLevel,
  924. #ifdef UNICODE_TRACE
  925. LPTSTR pszFormat,
  926. #else
  927. LPSTR pszFormat,
  928. #endif
  929. ...)
  930. {
  931. #define DEBUG_FORMAT_HEADER "Q931 "
  932. #define DEBUG_FORMAT_TIMESTAMP "[%02u:%02u:%02u.%03u"
  933. #define DEBUG_FORMAT_THREADID ",tid=%x] "
  934. #define MAXDBG_STRLEN 512
  935. va_list Args;
  936. SYSTEMTIME SystemTime;
  937. char szDebugMessage[MAXDBG_STRLEN+1];
  938. int nLengthRemaining;
  939. int nLength = 0;
  940. // make sure initialized
  941. if (g_fQ931DbgInitialized == FALSE) {
  942. Q931DbgInit();
  943. }
  944. // verify debug log level
  945. if (dwLevel > g_dwQ931DbgLevel) {
  946. return; // bail...
  947. }
  948. // retrieve local time
  949. GetLocalTime(&SystemTime);
  950. // add component header to the debug message
  951. nLength += sprintf(&szDebugMessage[nLength],
  952. DEBUG_FORMAT_HEADER
  953. );
  954. // add timestamp to the debug message
  955. nLength += sprintf(&szDebugMessage[nLength],
  956. DEBUG_FORMAT_TIMESTAMP,
  957. SystemTime.wHour,
  958. SystemTime.wMinute,
  959. SystemTime.wSecond,
  960. SystemTime.wMilliseconds
  961. );
  962. // add thread id to the debug message
  963. nLength += sprintf(&szDebugMessage[nLength],
  964. DEBUG_FORMAT_THREADID,
  965. GetCurrentThreadId()
  966. );
  967. // point at first argument
  968. va_start(Args, pszFormat);
  969. // determine number of bytes left in buffer
  970. nLengthRemaining = sizeof(szDebugMessage) - nLength;
  971. // add user specified debug message
  972. _vsnprintf(&szDebugMessage[nLength],
  973. nLengthRemaining,
  974. pszFormat,
  975. Args
  976. );
  977. // release pointer
  978. va_end(Args);
  979. // output message to specified sink
  980. OutputDebugString(szDebugMessage);
  981. OutputDebugString("\n");
  982. }
  983. #endif
  984. #ifdef __cplusplus
  985. }
  986. #endif