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.

272 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 2001-2002 Microsoft Corporation
  3. Module Name:
  4. teredo.c
  5. Abstract:
  6. Teredo commands.
  7. Author:
  8. Mohit Talwar (mohitt) Wed Nov 07 22:18:53 2001
  9. --*/
  10. #include "precomp.h"
  11. //
  12. // The following enums and defines should be kept consistent with
  13. // teredo.h in 6to4svc.
  14. //
  15. //
  16. // TEREDO_TYPE
  17. //
  18. // Define the type of the teredo service.
  19. //
  20. typedef enum {
  21. TEREDO_DEFAULT = 0,
  22. TEREDO_CLIENT,
  23. TEREDO_SERVER,
  24. TEREDO_DISABLED,
  25. TEREDO_AUTOMATIC,
  26. TEREDO_MAXIMUM,
  27. } TEREDO_TYPE, *PTEREDO_TYPE;
  28. #define KEY_TEREDO_REFRESH_INTERVAL L"RefreshInterval"
  29. #define KEY_TEREDO_TYPE L"Type"
  30. #define KEY_TEREDO_SERVER_NAME L"ServerName"
  31. #define KEY_TEREDO L"System\\CurrentControlSet\\Services\\Teredo"
  32. PWCHAR pwszTypeString[] = {
  33. TOKEN_VALUE_DEFAULT,
  34. TOKEN_VALUE_CLIENT,
  35. TOKEN_VALUE_SERVER,
  36. TOKEN_VALUE_DISABLED,
  37. TOKEN_VALUE_AUTOMATIC,
  38. };
  39. /////////////////////////////////////////////////////////////////////////////
  40. // Commands related to teredo
  41. /////////////////////////////////////////////////////////////////////////////
  42. DWORD
  43. ShowTeredo(
  44. IN FORMAT Format
  45. )
  46. {
  47. HKEY hKey = INVALID_HANDLE_VALUE;
  48. TEREDO_TYPE tyType;
  49. WCHAR pwszServerName[NI_MAXHOST] = TOKEN_VALUE_DEFAULT;
  50. ULONG ulRefreshInterval;
  51. (VOID) RegOpenKeyEx(
  52. HKEY_LOCAL_MACHINE, KEY_TEREDO, 0, KEY_READ, &hKey);
  53. tyType = GetInteger(hKey, KEY_TEREDO_TYPE, TEREDO_DEFAULT);
  54. if (tyType >= TEREDO_MAXIMUM) {
  55. tyType = TEREDO_DEFAULT;
  56. }
  57. GetString(hKey, KEY_TEREDO_SERVER_NAME, pwszServerName, NI_MAXHOST);
  58. ulRefreshInterval = GetInteger(hKey, KEY_TEREDO_REFRESH_INTERVAL, 0);
  59. if (hKey != INVALID_HANDLE_VALUE) {
  60. RegCloseKey(hKey);
  61. }
  62. if (Format == FORMAT_DUMP) {
  63. if ((tyType != TEREDO_DEFAULT) ||
  64. (wcscmp(pwszServerName, TOKEN_VALUE_DEFAULT) != 0) ||
  65. (ulRefreshInterval != 0)) {
  66. DisplayMessageT(DMP_IPV6_SET_TEREDO);
  67. if (tyType != TEREDO_DEFAULT) {
  68. DisplayMessageT(DMP_STRING_ARG, TOKEN_TYPE,
  69. pwszTypeString[tyType]);
  70. }
  71. if (wcscmp(pwszServerName, TOKEN_VALUE_DEFAULT) != 0) {
  72. DisplayMessageT(DMP_STRING_ARG, TOKEN_SERVERNAME,
  73. pwszServerName);
  74. }
  75. if (ulRefreshInterval != 0) {
  76. DisplayMessageT(
  77. DMP_INTEGER_ARG, TOKEN_REFRESH_INTERVAL, ulRefreshInterval);
  78. }
  79. DisplayMessage(g_hModule, MSG_NEWLINE);
  80. }
  81. } else {
  82. DisplayMessage(
  83. g_hModule,
  84. MSG_TEREDO_PARAMETERS,
  85. pwszTypeString[tyType],
  86. pwszServerName);
  87. if (ulRefreshInterval == 0) {
  88. DisplayMessage(g_hModule, MSG_STRING, TOKEN_VALUE_DEFAULT);
  89. } else {
  90. DisplayMessage(g_hModule, MSG_SECONDS, ulRefreshInterval);
  91. }
  92. }
  93. return NO_ERROR;
  94. }
  95. DWORD
  96. HandleSetTeredo(
  97. IN LPCWSTR pwszMachine,
  98. IN OUT LPWSTR *ppwcArguments,
  99. IN DWORD dwCurrentIndex,
  100. IN DWORD dwArgCount,
  101. IN DWORD dwFlags,
  102. IN LPCVOID pvData,
  103. OUT BOOL *pbDone
  104. )
  105. {
  106. DWORD dwErr = NO_ERROR;
  107. HKEY hKey = INVALID_HANDLE_VALUE;
  108. TEREDO_TYPE tyType = 0;
  109. PWCHAR pwszServerName = NULL;
  110. ULONG ulRefreshInterval = 0;
  111. BOOL bType, bServer, bRefreshInterval;
  112. TAG_TYPE pttTags[] = {{TOKEN_TYPE, FALSE, FALSE},
  113. {TOKEN_SERVERNAME, FALSE, FALSE},
  114. {TOKEN_REFRESH_INTERVAL, FALSE, FALSE}};
  115. DWORD rgdwTagType[sizeof(pttTags) / sizeof(TAG_TYPE)];
  116. TOKEN_VALUE rgtvEnums[] = {
  117. { TOKEN_VALUE_DEFAULT, TEREDO_DEFAULT },
  118. { TOKEN_VALUE_CLIENT, TEREDO_CLIENT },
  119. { TOKEN_VALUE_SERVER, TEREDO_SERVER },
  120. { TOKEN_VALUE_DISABLED, TEREDO_DISABLED },
  121. { TOKEN_VALUE_AUTOMATIC, TEREDO_AUTOMATIC },
  122. };
  123. DWORD i;
  124. bType = bServer = bRefreshInterval = FALSE;
  125. // Parse arguments
  126. dwErr = PreprocessCommand(g_hModule,
  127. ppwcArguments,
  128. dwCurrentIndex,
  129. dwArgCount,
  130. pttTags,
  131. sizeof(pttTags)/sizeof(TAG_TYPE),
  132. 1,
  133. sizeof(pttTags)/sizeof(TAG_TYPE),
  134. rgdwTagType );
  135. if (dwErr != NO_ERROR) {
  136. return dwErr;
  137. }
  138. for (i = 0; i < (dwArgCount - dwCurrentIndex); i++) {
  139. switch(rgdwTagType[i]) {
  140. case 0: // TYPE
  141. dwErr = MatchEnumTag(NULL,
  142. ppwcArguments[dwCurrentIndex + i],
  143. NUM_TOKENS_IN_TABLE(rgtvEnums),
  144. rgtvEnums,
  145. (PDWORD) &tyType);
  146. if (dwErr != NO_ERROR) {
  147. dwErr = ERROR_INVALID_PARAMETER;
  148. break;
  149. }
  150. bType = TRUE;
  151. break;
  152. case 1: // SERVERNAME
  153. pwszServerName = ppwcArguments[dwCurrentIndex + i];
  154. bServer = TRUE;
  155. break;
  156. case 2: // REFRESHINTERVAL
  157. ulRefreshInterval = wcstoul(
  158. ppwcArguments[dwCurrentIndex + i], NULL, 10);
  159. bRefreshInterval = TRUE;
  160. break;
  161. default:
  162. dwErr = ERROR_INVALID_SYNTAX;
  163. break;
  164. }
  165. if (dwErr != NO_ERROR) {
  166. return dwErr;
  167. }
  168. }
  169. // Now do the sets
  170. dwErr = RegCreateKeyEx(HKEY_LOCAL_MACHINE, KEY_TEREDO, 0, NULL, 0,
  171. KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
  172. if (dwErr != NO_ERROR) {
  173. return dwErr;
  174. }
  175. if (bType) { // 0 (TEREDO_DEFAULT) resets to default.
  176. dwErr = SetInteger(hKey, KEY_TEREDO_TYPE, tyType);
  177. if (dwErr != NO_ERROR)
  178. goto Bail;
  179. }
  180. if (bServer) { // "default" resets to default.
  181. dwErr = SetString(hKey, KEY_TEREDO_SERVER_NAME, pwszServerName);
  182. if (dwErr != NO_ERROR)
  183. goto Bail;
  184. }
  185. if (bRefreshInterval) { // 0 resets to default.
  186. dwErr = SetInteger(
  187. hKey, KEY_TEREDO_REFRESH_INTERVAL, ulRefreshInterval);
  188. if (dwErr != NO_ERROR)
  189. goto Bail;
  190. }
  191. Bail:
  192. RegCloseKey(hKey);
  193. Ip6to4PokeService();
  194. return ERROR_OKAY;
  195. }
  196. DWORD
  197. HandleShowTeredo(
  198. IN LPCWSTR pwszMachine,
  199. IN OUT LPWSTR *ppwcArguments,
  200. IN DWORD dwCurrentIndex,
  201. IN DWORD dwArgCount,
  202. IN DWORD dwFlags,
  203. IN LPCVOID pvData,
  204. OUT BOOL *pbDone
  205. )
  206. {
  207. return ShowTeredo(FORMAT_NORMAL);
  208. }
  209. DWORD
  210. ResetTeredo(
  211. VOID
  212. )
  213. {
  214. DWORD dwErr;
  215. // Nuke teredo parameters.
  216. dwErr = ResetKey(HKEY_LOCAL_MACHINE, KEY_TEREDO);
  217. if ((dwErr != NO_ERROR) && (dwErr != ERROR_FILE_NOT_FOUND)) {
  218. return dwErr;
  219. }
  220. // Poke the teredo service.
  221. Ip6to4PokeService();
  222. return ERROR_OKAY;
  223. }