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.

245 lines
5.4 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <ntddser.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <memory.h>
  9. #include <malloc.h>
  10. #include <rasman.h>
  11. #include <dim.h>
  12. #include <routprot.h>
  13. #include <ipxrtdef.h>
  14. // [pmay] this will no longer be neccessary when the ipx router
  15. // is converted to use MprInfo api's.
  16. typedef RTR_INFO_BLOCK_HEADER IPX_INFO_BLOCK_HEADER, *PIPX_INFO_BLOCK_HEADER;
  17. typedef struct _IF_CONFIG {
  18. BOOL Empty;
  19. WCHAR InterfaceName[48];
  20. HANDLE DimIfHandle;
  21. ROUTER_INTERFACE_TYPE IfType;
  22. ULONG IfSize;
  23. } IF_CONFIG, *PIF_CONFIG;
  24. FILE *ConfigFile;
  25. typedef struct _IF_TYPE_TRANSLATION {
  26. ROUTER_INTERFACE_TYPE DimType;
  27. ULONG IpxMibType;
  28. } IF_TYPE_TRANSLATION, *PIF_TYPE_TRANSLATION;
  29. IF_TYPE_TRANSLATION IfTypeTranslation[] = {
  30. { ROUTER_IF_TYPE_FULL_ROUTER, IF_TYPE_WAN_ROUTER },
  31. { ROUTER_IF_TYPE_HOME_ROUTER, IF_TYPE_PERSONAL_WAN_ROUTER },
  32. { ROUTER_IF_TYPE_DEDICATED, IF_TYPE_LAN },
  33. { ROUTER_IF_TYPE_CLIENT, IF_TYPE_WAN_WORKSTATION },
  34. { ROUTER_IF_TYPE_INTERNAL, IF_TYPE_INTERNAL }
  35. };
  36. #define MAX_IF_TYPES sizeof(IfTypeTranslation)/sizeof(IF_TYPE_TRANSLATION)
  37. VOID
  38. SaveInterface(PDIM_ROUTER_INTERFACE dimifp,
  39. PIPX_INTERFACE IpxIfp);
  40. VOID
  41. SaveConfiguration(PDIM_ROUTER_INTERFACE dimifp)
  42. {
  43. DWORD rc;
  44. IPX_MIB_GET_INPUT_DATA MibGetInputData;
  45. DWORD IfSize;
  46. IPX_INTERFACE IpxIf;
  47. IF_CONFIG IfConfig;
  48. // open configuration file
  49. if((ConfigFile = fopen("c:\\test\\config.bin", "w+b")) == NULL) {
  50. printf("SaveConfiguration: cannot open config file\n");
  51. return;
  52. }
  53. // get all configured interfaces from the rtrmgr and save them
  54. IfSize = sizeof(IPX_INTERFACE);
  55. MibGetInputData.TableId = IPX_INTERFACE_TABLE;
  56. MibGetInputData.MibIndex.InterfaceTableIndex.InterfaceIndex = 0;
  57. rc = (*(dimifp->MIBEntryGetFirst))(
  58. IPX_PROTOCOL_BASE,
  59. sizeof(IPX_MIB_GET_INPUT_DATA),
  60. &MibGetInputData,
  61. &IfSize,
  62. &IpxIf);
  63. if(rc != NO_ERROR) {
  64. printf("MIBEntryGetFirst: failed rc = %d\n", rc);
  65. return;
  66. }
  67. SaveInterface(dimifp, &IpxIf);
  68. for(;;)
  69. {
  70. MibGetInputData.MibIndex.InterfaceTableIndex.InterfaceIndex = IpxIf.InterfaceIndex;
  71. rc = (*(dimifp->MIBEntryGetNext))(
  72. IPX_PROTOCOL_BASE,
  73. sizeof(IPX_MIB_GET_INPUT_DATA),
  74. &MibGetInputData,
  75. &IfSize,
  76. &IpxIf);
  77. if(rc != NO_ERROR) {
  78. printf("EnumerateIpxInterfaces: MIBEntryGetNext failed rc= 0x%x\n", rc);
  79. break;
  80. }
  81. SaveInterface(dimifp, &IpxIf);
  82. }
  83. // done
  84. IfConfig.Empty = TRUE;
  85. fwrite(&IfConfig, sizeof(IF_CONFIG), 1, ConfigFile);
  86. fclose(ConfigFile);
  87. }
  88. VOID
  89. SaveInterface(PDIM_ROUTER_INTERFACE dimifp,
  90. PIPX_INTERFACE IpxIfp)
  91. {
  92. DWORD rc;
  93. ULONG IfSize = 0;
  94. ULONG InFtSize = 0;
  95. ULONG OutFtSize = 0;
  96. LPVOID IfInfop;
  97. IF_CONFIG IfConfig;
  98. ANSI_STRING AnsiString;
  99. UNICODE_STRING UnicodeString;
  100. int i;
  101. PIF_TYPE_TRANSLATION ittp;
  102. rc = (*dimifp->GetInterfaceInfo)((HANDLE)(IpxIfp->InterfaceIndex),
  103. NULL,
  104. &IfSize,
  105. NULL,
  106. &InFtSize,
  107. NULL,
  108. &OutFtSize);
  109. if(rc != ERROR_INSUFFICIENT_BUFFER) {
  110. printf("SaveInterface: GetInterfaceInfo failed rc = %d\n", rc);
  111. return;
  112. }
  113. IfInfop = GlobalAlloc(GPTR, IfSize);
  114. rc = (*dimifp->GetInterfaceInfo)((HANDLE)(IpxIfp->InterfaceIndex),
  115. IfInfop,
  116. &IfSize,
  117. NULL,
  118. &InFtSize,
  119. NULL,
  120. &OutFtSize);
  121. if(rc != NO_ERROR) {
  122. printf("SaveInterface: GetInterfaceInfo failed rc = %d\n", rc);
  123. return;
  124. }
  125. // write an IfConfig item followed by the if info
  126. RtlInitAnsiString(&AnsiString, IpxIfp->InterfaceName);
  127. RtlAnsiStringToUnicodeString(&UnicodeString, &AnsiString, TRUE);
  128. memcpy(IfConfig.InterfaceName, UnicodeString.Buffer, UnicodeString.MaximumLength);
  129. IfConfig.Empty = FALSE;
  130. IfConfig.DimIfHandle = (HANDLE)IpxIfp->InterfaceIndex;
  131. IfConfig.IfSize = IfSize;
  132. for(i=0, ittp=IfTypeTranslation; i<MAX_IF_TYPES; i++, ittp++) {
  133. if(IpxIfp->InterfaceType == ittp->IpxMibType) {
  134. IfConfig.IfType = ittp->DimType;
  135. break;
  136. }
  137. }
  138. printf("Saving if index %d size %d\n", IpxIfp->InterfaceIndex, IfSize);
  139. fwrite(&IfConfig, sizeof(IF_CONFIG), 1, ConfigFile);
  140. fwrite(IfInfop, IfSize, 1, ConfigFile);
  141. GlobalFree(IfInfop);
  142. RtlFreeUnicodeString(&UnicodeString);
  143. }
  144. VOID
  145. RestoreConfiguration(PDIM_ROUTER_INTERFACE dimifp)
  146. {
  147. IF_CONFIG IfConfig;
  148. LPVOID IfInfop;
  149. HANDLE IfIndex;
  150. PIF_TYPE_TRANSLATION ittp;
  151. DWORD rc;
  152. // open configuration file
  153. if((ConfigFile = fopen("c:\\test\\config.bin", "r+b")) == NULL) {
  154. printf("RestoreConfiguration: cannot open config file\n");
  155. return;
  156. }
  157. fread(&IfConfig, sizeof(IF_CONFIG), 1, ConfigFile);
  158. while(!IfConfig.Empty)
  159. {
  160. IfInfop = GlobalAlloc(GPTR, IfConfig.IfSize);
  161. fread(IfInfop, IfConfig.IfSize, 1, ConfigFile);
  162. rc = (*(dimifp->AddInterface))(IfConfig.InterfaceName,
  163. IfInfop,
  164. NULL,
  165. NULL,
  166. IfConfig.IfType,
  167. TRUE,
  168. IfConfig.DimIfHandle,
  169. &IfIndex);
  170. if(rc != NO_ERROR) {
  171. printf("RestoreConfiguration: failed with rc = %d\n", rc);
  172. return;
  173. }
  174. printf("RestoreConfiguration: added if index %d\n", IfIndex);
  175. fread(&IfConfig, sizeof(IF_CONFIG), 1, ConfigFile);
  176. }
  177. fclose(ConfigFile);
  178. }