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.

220 lines
5.4 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. ipaddr.c
  5. Abstract:
  6. add/del ip addresses to an adapter
  7. Author:
  8. Charlie Wickham (charlwi) 03-Nov-00
  9. Environment:
  10. User mode
  11. Revision History:
  12. --*/
  13. #define UNICODE 1
  14. #define _UNICODE 1
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <winsock2.h>
  19. #include <iphlpapi.h>
  20. typedef enum {
  21. OpAdd = 1,
  22. OpDel,
  23. OpCycle
  24. } OPERATION;
  25. int __cdecl
  26. wmain(
  27. int argc,
  28. WCHAR *argv[]
  29. )
  30. /*++
  31. Routine Description:
  32. main routine for utility
  33. Arguments:
  34. -add <Adapter name> <Ip addr>
  35. -del <NTE context>
  36. -cycle <Adapter name> <Ip addr>
  37. Return Value:
  38. 0 if it worked successfully
  39. --*/
  40. {
  41. DWORD status;
  42. DWORD adapterIndex;
  43. struct in_addr ipAddress;
  44. struct in_addr ipMask;
  45. OPERATION opCode;
  46. DWORD nteContext;
  47. DWORD nteInstance;
  48. HANDLE mprConfig;
  49. WCHAR guidName[64] = L"\\DEVICE\\TCPIP_";
  50. if ( argc < 2 ) {
  51. printf("%ws -add <adapter name> address mask\n", argv[0] );
  52. printf("%ws -cycle <adapter name> address mask <cycle sleep time>\n", argv[0] );
  53. printf("%ws -del <NTE context>\n", argv[0] );
  54. return 0;
  55. }
  56. switch (*(argv[1]+1)) {
  57. case L'a':
  58. case L'A':
  59. opCode = OpAdd;
  60. break;
  61. case L'd':
  62. case L'D':
  63. opCode = OpDel;
  64. break;
  65. case L'c':
  66. case L'C':
  67. opCode = OpCycle;
  68. break;
  69. default:
  70. printf("invalid command: use -add, -del, or -cycle\n");
  71. return 0;
  72. }
  73. if ( opCode == OpAdd && argc < 5 ) {
  74. printf("invalid arg: %ws -add <adapter name> address mask\n", argv[0] );
  75. return 0;
  76. }
  77. else if ( opCode == OpCycle && argc < 6 ) {
  78. printf("invalid arg: %ws -cycle <adapter name> address mask <cycle sleep time>\n", argv[0] );
  79. return 0;
  80. }
  81. else if ( opCode == OpDel && argc < 3 ) {
  82. printf("invalid arg: %ws -del <NTE context>\n", argv[0] );
  83. return 0;
  84. }
  85. if ( opCode == OpAdd || opCode == OpCycle ) {
  86. DWORD byte1;
  87. DWORD byte2;
  88. DWORD byte3;
  89. DWORD byte4;
  90. status = MprConfigServerConnect( NULL, &mprConfig );
  91. if ( status != NO_ERROR ) {
  92. printf("Can't get handle to MprConfig - %u\n", status );
  93. return status;
  94. }
  95. status = MprConfigGetGuidName(mprConfig, argv[2], &guidName[14], sizeof(guidName ));
  96. if (status != NO_ERROR ) {
  97. printf("Can't get GUID name for '%ws' - %u\n", argv[2], status );
  98. }
  99. status = GetAdapterIndex( guidName, &adapterIndex );
  100. if ( status != NO_ERROR ) {
  101. printf("Error getting adapter index for '%ws' - %u\n",
  102. guidName, status);
  103. return status;
  104. }
  105. swscanf(argv[3], L"%u.%u.%u.%u",
  106. &byte1,
  107. &byte2,
  108. &byte3,
  109. &byte4);
  110. ipAddress.S_un.S_un_b.s_b1 = (CHAR)byte1;
  111. ipAddress.S_un.S_un_b.s_b2 = (CHAR)byte2;
  112. ipAddress.S_un.S_un_b.s_b3 = (CHAR)byte3;
  113. ipAddress.S_un.S_un_b.s_b4 = (CHAR)byte4;
  114. swscanf(argv[4], L"%u.%u.%u.%u",
  115. &byte1,
  116. &byte2,
  117. &byte3,
  118. &byte4);
  119. ipMask.S_un.S_un_b.s_b1 = (CHAR)byte1;
  120. ipMask.S_un.S_un_b.s_b2 = (CHAR)byte2;
  121. ipMask.S_un.S_un_b.s_b3 = (CHAR)byte3;
  122. ipMask.S_un.S_un_b.s_b4 = (CHAR)byte4;
  123. }
  124. if ( opCode == OpAdd ) {
  125. status = AddIPAddress((IPAddr)ipAddress.s_addr,
  126. (IPMask)ipMask.s_addr,
  127. adapterIndex,
  128. &nteContext,
  129. &nteInstance);
  130. if ( status == NO_ERROR ) {
  131. printf("Added IP address %ws with mask %ws to adapter '%ws'\n",
  132. argv[3], argv[4], argv[2]);
  133. printf("NTE context to use with delete: %u\n", nteContext);
  134. }
  135. else {
  136. printf("Failed to add IP address %ws with mask %ws to adapter '%ws' - %u\n",
  137. argv[3], argv[4], argv[2], status);
  138. }
  139. }
  140. else if ( opCode == OpDel ) {
  141. nteContext = _wtoi( argv[2] );
  142. status = DeleteIPAddress( nteContext );
  143. printf("Delete status = %u\n", status );
  144. }
  145. else if ( opCode == OpCycle ) {
  146. DWORD sleepTime = _wtoi( argv[5] );
  147. do {
  148. status = AddIPAddress((IPAddr)ipAddress.s_addr,
  149. (IPMask)ipMask.s_addr,
  150. adapterIndex,
  151. &nteContext,
  152. &nteInstance);
  153. if ( status == NO_ERROR ) {
  154. printf("Added IP address %ws with mask %ws to adapter '%ws'\n",
  155. argv[3], argv[4], argv[2]);
  156. printf("NTE context to use with delete: %u\n", nteContext);
  157. }
  158. else {
  159. printf("Failed to add IP address %ws with mask %ws to adapter '%ws' - %u\n",
  160. argv[3], argv[4], argv[2], status);
  161. return status;
  162. }
  163. Sleep( sleepTime * 1000 );
  164. status = DeleteIPAddress( nteContext );
  165. printf("Delete status = %u\n", status );
  166. Sleep( sleepTime * 1000 );
  167. } while ( TRUE );
  168. }
  169. return 0;
  170. } // wmain
  171. /* end ipaddr.c */