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.

173 lines
4.5 KiB

  1. //##--------------------------------------------------------------
  2. //
  3. // File: portparser.cpp
  4. //
  5. // Synopsis: Implementation of CPortParser class responsible
  6. // for obtaining port information for the Radius
  7. // protocol component
  8. //
  9. //
  10. // History: 10/22/98 MKarki Created
  11. //
  12. // Copyright (C) 1997-98 Microsoft Corporation
  13. // All rights reserved.
  14. //
  15. //----------------------------------------------------------------
  16. #include <ias.h>
  17. #include <winsock2.h>
  18. #include <portparser.h>
  19. //
  20. // delimiters defined here
  21. //
  22. const WCHAR TOKEN_DELIMITER = L':';
  23. const WCHAR OBJECT_DELIMITER = L';';
  24. const WCHAR PORT_DELIMITER = L',';
  25. const WCHAR NUL = L'\0';
  26. //
  27. // maximum port value
  28. //
  29. const DWORD MAX_PORT_VALUE = 0xffff;
  30. //++--------------------------------------------------------------
  31. //
  32. // Function: GetIPAddress
  33. //
  34. // Synopsis: This is CPortParser class public method which
  35. // gets the IP Address from the string
  36. //
  37. // Arguments: [in] PDWORD - return IP Address
  38. //
  39. // Returns: HRESULT
  40. //
  41. // History: MKarki Created 10/22/97
  42. //
  43. //----------------------------------------------------------------
  44. HRESULT CPortParser::GetIPAddress (PDWORD pdwIPAddress) throw ()
  45. {
  46. HRESULT hr = S_OK;
  47. _ASSERT (current && pdwIPAddress);
  48. if (NUL == *m_pEnd) {return (S_FALSE);}
  49. try
  50. {
  51. current = m_pObjstart = m_pEnd;
  52. for (; NUL != *m_pEnd; ++m_pEnd)
  53. {
  54. if (OBJECT_DELIMITER == *m_pEnd)
  55. {
  56. ++m_pEnd;
  57. break;
  58. }
  59. }
  60. if ((NUL == *seekNext (TOKEN_DELIMITER)) || (current >= m_pEnd))
  61. {
  62. //
  63. // no IP address
  64. //
  65. *pdwIPAddress = INADDR_ANY;
  66. m_pPort = m_pObjstart;
  67. }
  68. else
  69. {
  70. //
  71. // convert the IP address from WideChar to MultiByte
  72. //
  73. INT iSize = static_cast <INT> (current - m_pObjstart);
  74. CHAR szIPAddress[ADDRESS_BUFFER_SIZE +1];
  75. INT iRetVal = ::WideCharToMultiByte (
  76. CP_ACP,
  77. 0,
  78. reinterpret_cast <LPCWSTR> (m_pObjstart),
  79. iSize,
  80. szIPAddress,
  81. ADDRESS_BUFFER_SIZE,
  82. NULL,
  83. NULL
  84. );
  85. if (0 != iRetVal)
  86. {
  87. szIPAddress[iSize] = NUL;
  88. *pdwIPAddress = inet_addr (szIPAddress);
  89. if (INADDR_NONE == *pdwIPAddress)
  90. {
  91. IASTracePrintf (
  92. "Unable to obtain IP address through inet_addr "
  93. "while parsing interface and port info"
  94. );
  95. hr = E_FAIL;
  96. }
  97. }
  98. else
  99. {
  100. IASTracePrintf (
  101. "Unable to convert IP Address to multi-byte string"
  102. );
  103. hr = E_FAIL;
  104. }
  105. m_pPort = (PWCHAR)(++current);
  106. }
  107. }
  108. catch (...)
  109. {
  110. }
  111. current = m_pObjstart;
  112. return (hr);
  113. } // end of CPortParser::GetIPAddress method
  114. //++--------------------------------------------------------------
  115. //
  116. // Function: GetNextPort
  117. //
  118. // Synopsis: This is CPortParser class public method which
  119. // gets the Next Port number from the string
  120. //
  121. // Arguments: [in] PDWORD - return Port
  122. //
  123. // Returns: HRESULT
  124. //
  125. // History: MKarki Created 10/22/97
  126. //
  127. //----------------------------------------------------------------
  128. HRESULT CPortParser::GetNextPort (PWORD pwPort) throw ()
  129. {
  130. HRESULT hr = S_FALSE;
  131. _ASSERT (current && pwPort);
  132. try
  133. {
  134. if ((NUL != *m_pPort) && (m_pPort != m_pEnd))
  135. {
  136. current = m_pPort;
  137. DWORD dwValue = extractUnsignedLong ();
  138. if (dwValue <= MAX_PORT_VALUE)
  139. {
  140. *pwPort = static_cast <USHORT> (dwValue);
  141. m_pPort = ((PORT_DELIMITER == *current) ||
  142. (OBJECT_DELIMITER == *current))?
  143. (PWCHAR)(++current):
  144. (PWCHAR)current;
  145. hr = S_OK;
  146. }
  147. else
  148. {
  149. hr = E_FAIL;
  150. }
  151. }
  152. }
  153. catch (...)
  154. {
  155. }
  156. current = m_pObjstart;
  157. return (hr);
  158. } // end of CPortParser::GetNextPort method