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.

236 lines
5.1 KiB

  1. //
  2. // constr.cpp
  3. //
  4. // Implementation of CRdpConnectionString
  5. //
  6. // CRdpConnectionString implements a generic connection string
  7. // that can specify a server name and optionally a port and other
  8. // connection parameters
  9. //
  10. // Copyright(C) Microsoft Corporation 2002
  11. // Author: Nadim Abdo (nadima)
  12. //
  13. #include "stdafx.h"
  14. #define TRC_GROUP TRC_GROUP_UI
  15. #define TRC_FILE "constr.cpp"
  16. #include <atrcapi.h>
  17. #include "constr.h"
  18. #include "autil.h"
  19. CRdpConnectionString::CRdpConnectionString()
  20. {
  21. DC_BEGIN_FN("CRdpConnectionString");
  22. memset(_szFullConnectionString, 0, sizeof(_szFullConnectionString));
  23. DC_END_FN();
  24. }
  25. CRdpConnectionString::CRdpConnectionString(LPCTSTR szConString)
  26. {
  27. DC_BEGIN_FN("CRdpConnectionString");
  28. SetFullConnectionString(szConString);
  29. DC_END_FN();
  30. }
  31. CRdpConnectionString::~CRdpConnectionString()
  32. {
  33. DC_BEGIN_FN("~CRdpConnectionString");
  34. DC_END_FN();
  35. }
  36. HRESULT
  37. CRdpConnectionString::SetFullConnectionString(
  38. IN LPCTSTR szConString
  39. )
  40. {
  41. HRESULT hr;
  42. DC_BEGIN_FN("SetFullConnectionString");
  43. hr = StringCchCopy(
  44. _szFullConnectionString,
  45. SIZE_TCHARS(_szFullConnectionString),
  46. szConString
  47. );
  48. DC_END_FN();
  49. return hr;
  50. }
  51. LPTSTR
  52. CRdpConnectionString::GetFullConnectionString(
  53. )
  54. {
  55. DC_BEGIN_FN("GetFullConnectionString");
  56. DC_END_FN();
  57. return _szFullConnectionString;
  58. }
  59. //
  60. // Retreive the server portion of the connect string e.g. if
  61. //
  62. // 'nadima3:3389 /connect" then get "nadima3:3389"
  63. //
  64. HRESULT
  65. CRdpConnectionString::GetServerPortion(
  66. OUT LPTSTR szServerPortion,
  67. IN ULONG cchServerPortionLen
  68. )
  69. {
  70. HRESULT hr;
  71. DC_BEGIN_FN("GetServerPortion");
  72. hr = CUT::GetCanonicalServerNameFromConnectString(
  73. _szFullConnectionString,
  74. szServerPortion,
  75. cchServerPortionLen
  76. );
  77. DC_END_FN();
  78. return hr;
  79. }
  80. //
  81. // Retreive the server name portion of the connect string e.g. if
  82. //
  83. // 'nadima3:3389 /connect" then get "nadima3"
  84. //
  85. HRESULT
  86. CRdpConnectionString::GetServerNamePortion(
  87. OUT LPTSTR szServerPortion,
  88. IN ULONG cchServerPortionLen
  89. )
  90. {
  91. HRESULT hr;
  92. TCHAR szServerPort[TSC_MAX_ADDRESS_LENGTH];
  93. DC_BEGIN_FN("GetServerNamePortion");
  94. hr = GetServerPortion(
  95. szServerPort,
  96. SIZE_TCHARS(szServerPort)
  97. );
  98. if (SUCCEEDED(hr)) {
  99. CUT::GetServerNameFromFullAddress(
  100. szServerPort,
  101. szServerPortion,
  102. cchServerPortionLen
  103. );
  104. }
  105. DC_END_FN();
  106. return hr;
  107. }
  108. //
  109. // Retreive the args portion of the connect string e.g. if
  110. //
  111. // 'nadima3:3389 /connect" then get "/connect"
  112. //
  113. HRESULT
  114. CRdpConnectionString::GetArgumentsPortion(
  115. OUT LPTSTR szArguments,
  116. IN ULONG cchArgLen
  117. )
  118. {
  119. HRESULT hr = E_FAIL;
  120. TCHAR szServerPortion[TSC_MAX_ADDRESS_LENGTH];
  121. DC_BEGIN_FN("GetArgumentsPortion");
  122. if (cchArgLen) {
  123. memset(szArguments, 0, sizeof(szArguments));
  124. }
  125. else {
  126. hr = E_INVALIDARG;
  127. DC_QUIT;
  128. }
  129. hr = GetServerPortion(
  130. szServerPortion,
  131. SIZE_TCHARS(szServerPortion)
  132. );
  133. if (SUCCEEDED(hr)) {
  134. ULONG cchLenServerPortion = _tcslen(szServerPortion);
  135. ULONG cchLenFull = _tcslen(_szFullConnectionString);
  136. if (cchLenFull > cchLenServerPortion) {
  137. //
  138. // There is stuff after the server name.
  139. // Return it as the arguments
  140. //
  141. LPTSTR szArgStart = _szFullConnectionString + cchLenServerPortion;
  142. hr = StringCchCopy(szArguments, cchArgLen, szArgStart);
  143. }
  144. }
  145. if (FAILED(hr) && cchArgLen) {
  146. szArguments[0] = 0;
  147. }
  148. DC_EXIT_POINT:
  149. DC_END_FN();
  150. return hr;
  151. }
  152. //
  153. // Validate the server portion
  154. //
  155. BOOL
  156. CRdpConnectionString::ValidateServerPart(
  157. IN LPTSTR szConnectionString
  158. )
  159. {
  160. HRESULT hr;
  161. BOOL fIsValid = FALSE;
  162. CRdpConnectionString stringToTest;
  163. DC_BEGIN_FN("ValidateServerPart");
  164. if (NULL == szConnectionString[0]) {
  165. TRC_ERR((TB,_T("0 length server string")));
  166. DC_QUIT;
  167. }
  168. TCHAR szServer[TSC_MAX_ADDRESS_LENGTH];
  169. hr = stringToTest.SetFullConnectionString(
  170. szConnectionString
  171. );
  172. if (SUCCEEDED(hr)) {
  173. hr = stringToTest.GetServerPortion(
  174. szServer,
  175. SIZE_TCHARS(szServer)
  176. );
  177. if (FAILED(hr)) {
  178. TRC_ERR((TB,_T("Fail to get server portion")));
  179. DC_QUIT;
  180. }
  181. if(CUT::ValidateServerName( szServer, TRUE)) {
  182. fIsValid = TRUE;
  183. }
  184. else {
  185. TRC_ERR((TB,_T("ValidateServerName failed")));
  186. }
  187. }
  188. else {
  189. TRC_ERR((TB,_T("Fail to setfull conn string:0x%x"), hr));
  190. }
  191. DC_END_FN();
  192. DC_EXIT_POINT:
  193. return fIsValid;
  194. }