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.

657 lines
17 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. nbnames.c
  5. Abstract:
  6. IPX Router Console Monitoring and Configuration tool.
  7. NetBIOS name configuration and monitoring.
  8. Author:
  9. Vadim Eydelman 06/07/1996
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. #define OPERATION_DEL_NBNAME (-1)
  14. #define OPERATION_ADD_NBNAME 1
  15. DWORD
  16. AdmSetNbName (
  17. int operation,
  18. LPWSTR InterfaceNameW,
  19. PUCHAR NbName,
  20. PWCHAR IfName
  21. );
  22. DWORD
  23. CfgSetNbName (
  24. int operation,
  25. LPWSTR InterfaceNameW,
  26. PUCHAR NbName,
  27. PWCHAR IfName
  28. );
  29. DWORD
  30. APIENTRY
  31. HelpNbName (
  32. IN int argc,
  33. IN WCHAR *argv[]
  34. )
  35. {
  36. DisplayMessage (g_hModule, HLP_IPX_NBNAME);
  37. return 0;
  38. }
  39. DWORD
  40. APIENTRY
  41. ShowNbName (
  42. IN int argc,
  43. IN WCHAR *argv[],
  44. IN HANDLE hFile
  45. )
  46. {
  47. WCHAR IfName[ MAX_INTERFACE_NAME_LEN + 1 ];
  48. DWORD rc, dwSize = sizeof(IfName);
  49. if (argc > 0)
  50. {
  51. unsigned count;
  52. #define InterfaceNameW argv[0]
  53. count = wcslen (InterfaceNameW);
  54. rc = IpmontrGetIfNameFromFriendlyName(
  55. InterfaceNameW, IfName, &dwSize
  56. );
  57. if ( rc != NO_ERROR )
  58. {
  59. if ( !hFile ) { DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME); }
  60. return ERROR_INVALID_PARAMETER;
  61. }
  62. if ((count > 0) && (count <= MAX_INTERFACE_NAME_LEN))
  63. {
  64. LPBYTE pIfBlock;
  65. BOOLEAN fRouter = FALSE;
  66. if ( !hFile ) { DisplayIPXMessage (g_hModule, MSG_NBNAME_TABLE_HDR); }
  67. if (g_hMprAdmin)
  68. {
  69. HANDLE hIfAdm;
  70. rc = MprAdminInterfaceGetHandle (
  71. g_hMprAdmin, IfName, &hIfAdm, FALSE
  72. );
  73. if (rc == NO_ERROR)
  74. {
  75. DWORD sz;
  76. rc = MprAdminInterfaceTransportGetInfo (
  77. g_hMprAdmin, hIfAdm, PID_IPX, &pIfBlock, &sz
  78. );
  79. }
  80. if (rc == NO_ERROR)
  81. {
  82. fRouter = TRUE;
  83. }
  84. else
  85. {
  86. if ( !hFile ) { DisplayError( g_hModule, rc); }
  87. goto GetFromCfg;
  88. }
  89. }
  90. else
  91. {
  92. HANDLE hIfCfg;
  93. GetFromCfg:
  94. rc = MprConfigInterfaceGetHandle (
  95. g_hMprConfig, IfName, &hIfCfg
  96. );
  97. if (rc == NO_ERROR)
  98. {
  99. HANDLE hIfTrCfg;
  100. rc = MprConfigInterfaceTransportGetHandle (
  101. g_hMprConfig, hIfCfg, PID_IPX, &hIfTrCfg
  102. );
  103. if (rc == NO_ERROR)
  104. {
  105. DWORD sz;
  106. rc = MprConfigInterfaceTransportGetInfo (
  107. g_hMprConfig, hIfCfg, hIfTrCfg, &pIfBlock, &sz
  108. );
  109. }
  110. }
  111. }
  112. if (rc == NO_ERROR)
  113. {
  114. PIPX_TOC_ENTRY pNnToc;
  115. pNnToc = GetIPXTocEntry (
  116. (PIPX_INFO_BLOCK_HEADER)pIfBlock,
  117. IPX_STATIC_NETBIOS_NAME_INFO_TYPE
  118. );
  119. if (pNnToc != NULL)
  120. {
  121. PIPX_STATIC_NETBIOS_NAME_INFO pNnInfo;
  122. UINT i;
  123. pNnInfo = (PIPX_STATIC_NETBIOS_NAME_INFO)
  124. (pIfBlock + pNnToc->Offset);
  125. for (i = 0; i < pNnToc->Count; i++, pNnInfo++)
  126. {
  127. if ( hFile )
  128. {
  129. UCHAR ucType = pNnInfo->Name[15];
  130. pNnInfo->Name[15] = '\0';
  131. DisplayMessageT(
  132. DMP_IPX_NB_ADD_NAME, InterfaceNameW,
  133. pNnInfo->Name, pNnInfo->Name[15]
  134. );
  135. }
  136. else
  137. {
  138. DisplayIPXMessage (g_hModule,
  139. MSG_NBNAME_TABLE_FMT,
  140. pNnInfo->Name, pNnInfo->Name[15]
  141. );
  142. }
  143. }
  144. }
  145. else
  146. {
  147. rc = ERROR_FILE_NOT_FOUND;
  148. if ( !hFile ) { DisplayError( g_hModule, rc); }
  149. }
  150. if (fRouter)
  151. {
  152. MprAdminBufferFree (pIfBlock);
  153. }
  154. else
  155. {
  156. MprConfigBufferFree (pIfBlock);
  157. }
  158. }
  159. else
  160. {
  161. if ( !hFile ) { DisplayError( g_hModule, rc); }
  162. }
  163. }
  164. else
  165. {
  166. if ( !hFile ) { DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME); }
  167. rc = ERROR_INVALID_PARAMETER;
  168. }
  169. }
  170. else
  171. {
  172. if ( !hFile ) { DisplayMessage (g_hModule, HLP_IPX_NBNAME); }
  173. rc = ERROR_INVALID_PARAMETER;
  174. }
  175. //Exit:
  176. return rc;
  177. #undef InterfaceNameW
  178. }
  179. DWORD
  180. APIENTRY
  181. CreateNbName (
  182. IN int argc,
  183. IN WCHAR *argv[]
  184. )
  185. {
  186. WCHAR IfName[ MAX_INTERFACE_NAME_LEN + 1 ];
  187. DWORD rc, dwSize = sizeof(IfName);
  188. if ((argc == 2) || (argc == 3))
  189. {
  190. unsigned count;
  191. #define InterfaceNameW argv[0]
  192. count = wcslen (InterfaceNameW);
  193. rc = IpmontrGetIfNameFromFriendlyName(
  194. InterfaceNameW, IfName, &dwSize
  195. );
  196. if ( rc != NO_ERROR )
  197. {
  198. DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME);
  199. return ERROR_INVALID_PARAMETER;
  200. }
  201. if ((count > 0) && (count <= MAX_INTERFACE_NAME_LEN))
  202. {
  203. UINT val, n;
  204. UCHAR NbName[16];
  205. memset (NbName, ' ', sizeof (NbName));
  206. if ( (swscanf (argv[1], L"%16hc%n", NbName, &n) == 1) &&
  207. (n == wcslen (argv[1])) &&
  208. ( (argc < 3) ||
  209. ( (swscanf (argv[2], L"%2x%n", &val, &n) == 1) &&
  210. ( n == wcslen (argv[2]) ) ) )
  211. )
  212. {
  213. if (argc < 3)
  214. {
  215. val = NbName[15];
  216. }
  217. NbName[15] = 0;
  218. _strupr (NbName);
  219. NbName[15] = (UCHAR) val;
  220. if (g_hMprAdmin)
  221. {
  222. rc = AdmSetNbName(
  223. OPERATION_ADD_NBNAME, IfName, NbName,
  224. InterfaceNameW
  225. );
  226. }
  227. else
  228. {
  229. rc = NO_ERROR;
  230. }
  231. if (rc == NO_ERROR)
  232. {
  233. rc = CfgSetNbName (
  234. OPERATION_ADD_NBNAME, IfName, NbName,
  235. InterfaceNameW
  236. );
  237. }
  238. }
  239. else
  240. {
  241. rc = ERROR_INVALID_PARAMETER;
  242. DisplayMessage (g_hModule, HLP_IPX_NBNAME);
  243. }
  244. }
  245. else
  246. {
  247. DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME);
  248. rc = ERROR_INVALID_PARAMETER;
  249. }
  250. }
  251. else
  252. {
  253. DisplayMessage (g_hModule, HLP_IPX_NBNAME);
  254. rc = ERROR_INVALID_PARAMETER;
  255. }
  256. return rc;
  257. #undef InterfaceNameW
  258. }
  259. DWORD
  260. APIENTRY
  261. DeleteNbName(
  262. IN int argc,
  263. IN WCHAR *argv[]
  264. )
  265. {
  266. WCHAR IfName[ MAX_INTERFACE_NAME_LEN + 1 ];
  267. DWORD rc, dwSize = sizeof(IfName);
  268. if ((argc == 2) || (argc == 3))
  269. {
  270. unsigned count;
  271. #define InterfaceNameW argv[0]
  272. count = wcslen (InterfaceNameW);
  273. rc = IpmontrGetIfNameFromFriendlyName(
  274. InterfaceNameW, IfName, &dwSize
  275. );
  276. if ( rc != NO_ERROR )
  277. {
  278. DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME);
  279. return ERROR_INVALID_PARAMETER;
  280. }
  281. if ((count > 0) && (count <= MAX_INTERFACE_NAME_LEN))
  282. {
  283. UINT val, n;
  284. UCHAR NbName[16];
  285. memset (NbName, ' ', sizeof (NbName));
  286. if ( (swscanf (argv[1], L"%16hc%n", NbName, &n) == 1) &&
  287. (n == wcslen (argv[1])) &&
  288. ( (argc < 3) ||
  289. ( (swscanf (argv[2], L"%2x%n", &val, &n) == 1) &&
  290. ( n == wcslen (argv[2]))) ) )
  291. {
  292. if (argc < 3)
  293. {
  294. val = NbName[15];
  295. }
  296. NbName[15] = 0;
  297. _strupr (NbName);
  298. NbName[15] = (UCHAR) val;
  299. if (g_hMprAdmin)
  300. {
  301. rc = AdmSetNbName(
  302. OPERATION_DEL_NBNAME, IfName, NbName,
  303. InterfaceNameW
  304. );
  305. }
  306. else
  307. {
  308. rc = NO_ERROR;
  309. }
  310. if (rc == NO_ERROR)
  311. {
  312. rc = CfgSetNbName(
  313. OPERATION_DEL_NBNAME, IfName, NbName,
  314. InterfaceNameW
  315. );
  316. }
  317. }
  318. else
  319. {
  320. rc = ERROR_INVALID_PARAMETER;
  321. DisplayMessage (g_hModule, HLP_IPX_NBNAME);
  322. }
  323. }
  324. else
  325. {
  326. DisplayIPXMessage (g_hModule, MSG_INVALID_INTERFACE_NAME);
  327. rc = ERROR_INVALID_PARAMETER;
  328. }
  329. }
  330. else
  331. {
  332. DisplayMessage (g_hModule, HLP_IPX_NBNAME);
  333. rc = ERROR_INVALID_PARAMETER;
  334. }
  335. return rc;
  336. #undef InterfaceNameW
  337. }
  338. BOOL
  339. NbNameEqual (
  340. PVOID info1,
  341. PVOID info2
  342. )
  343. {
  344. #define NbName1 ((PIPX_STATIC_NETBIOS_NAME_INFO)info1)
  345. #define NbName2 ((PIPX_STATIC_NETBIOS_NAME_INFO)info2)
  346. return ( _strnicmp (NbName1->Name, NbName2->Name, 15) == 0) &&
  347. (NbName1->Name[15] == NbName2->Name[15] );
  348. #undef NbName1
  349. #undef NbName2
  350. }
  351. DWORD
  352. AdmSetNbName (
  353. int operation,
  354. LPWSTR InterfaceNameW,
  355. PUCHAR NbName,
  356. PWCHAR IfName
  357. )
  358. {
  359. DWORD rc;
  360. HANDLE hIfAdm;
  361. rc = MprAdminInterfaceGetHandle (
  362. g_hMprAdmin, InterfaceNameW, &hIfAdm, FALSE
  363. );
  364. if (rc == NO_ERROR)
  365. {
  366. LPBYTE pIfBlock;
  367. DWORD sz;
  368. rc = MprAdminInterfaceTransportGetInfo (
  369. g_hMprAdmin, hIfAdm, PID_IPX, &pIfBlock, &sz
  370. );
  371. if (rc == NO_ERROR)
  372. {
  373. UINT msg;
  374. LPBYTE pNewBlock;
  375. switch (operation)
  376. {
  377. case OPERATION_ADD_NBNAME:
  378. rc = AddIPXInfoEntry (
  379. (PIPX_INFO_BLOCK_HEADER)pIfBlock,
  380. IPX_STATIC_NETBIOS_NAME_INFO_TYPE,
  381. sizeof (IPX_STATIC_NETBIOS_NAME_INFO),
  382. NbName, NbNameEqual,
  383. (PIPX_INFO_BLOCK_HEADER * ) & pNewBlock
  384. );
  385. msg = MSG_NBNAME_CREATED_ADM;
  386. break;
  387. case OPERATION_DEL_NBNAME:
  388. rc = DeleteIPXInfoEntry (
  389. (PIPX_INFO_BLOCK_HEADER)pIfBlock,
  390. IPX_STATIC_NETBIOS_NAME_INFO_TYPE,
  391. sizeof (IPX_STATIC_NETBIOS_NAME_INFO),
  392. NbName, NbNameEqual,
  393. (PIPX_INFO_BLOCK_HEADER * ) & pNewBlock
  394. );
  395. msg = MSG_NBNAME_DELETED_ADM;
  396. break;
  397. }
  398. if (rc == NO_ERROR)
  399. {
  400. if (pIfBlock != pNewBlock)
  401. {
  402. MprAdminBufferFree (pIfBlock);
  403. pIfBlock = pNewBlock;
  404. }
  405. rc = MprAdminInterfaceTransportSetInfo (
  406. g_hMprAdmin, hIfAdm, PID_IPX, pIfBlock,
  407. ((PIPX_INFO_BLOCK_HEADER)pIfBlock)->Size
  408. );
  409. if (rc == NO_ERROR)
  410. {
  411. DisplayIPXMessage (g_hModule, msg, IfName);
  412. }
  413. else
  414. {
  415. DisplayError( g_hModule, rc);
  416. }
  417. }
  418. else
  419. {
  420. DisplayError( g_hModule, rc);
  421. }
  422. MprAdminBufferFree (pIfBlock);
  423. }
  424. }
  425. else
  426. {
  427. DisplayError( g_hModule, rc);
  428. }
  429. return rc;
  430. }
  431. DWORD
  432. CfgSetNbName (
  433. int operation,
  434. LPWSTR InterfaceNameW,
  435. PUCHAR NbName,
  436. PWCHAR IfName
  437. )
  438. {
  439. DWORD rc;
  440. HANDLE hIfCfg;
  441. rc = MprConfigInterfaceGetHandle (
  442. g_hMprConfig, InterfaceNameW, &hIfCfg
  443. );
  444. if (rc == NO_ERROR)
  445. {
  446. HANDLE hIfTrCfg;
  447. rc = MprConfigInterfaceTransportGetHandle (
  448. g_hMprConfig, hIfCfg, PID_IPX, &hIfTrCfg
  449. );
  450. if (rc == NO_ERROR)
  451. {
  452. LPBYTE pIfBlock;
  453. DWORD sz;
  454. rc = MprConfigInterfaceTransportGetInfo (
  455. g_hMprConfig, hIfCfg, hIfTrCfg, &pIfBlock, &sz
  456. );
  457. if (rc == NO_ERROR)
  458. {
  459. UINT msg;
  460. LPBYTE pNewBlock;
  461. switch (operation)
  462. {
  463. case OPERATION_ADD_NBNAME:
  464. rc = AddIPXInfoEntry (
  465. (PIPX_INFO_BLOCK_HEADER)pIfBlock,
  466. IPX_STATIC_NETBIOS_NAME_INFO_TYPE,
  467. sizeof (IPX_STATIC_NETBIOS_NAME_INFO),
  468. NbName, NbNameEqual,
  469. (PIPX_INFO_BLOCK_HEADER * ) & pNewBlock
  470. );
  471. msg = MSG_NBNAME_CREATED_CFG;
  472. break;
  473. case OPERATION_DEL_NBNAME:
  474. rc = DeleteIPXInfoEntry (
  475. (PIPX_INFO_BLOCK_HEADER)pIfBlock,
  476. IPX_STATIC_NETBIOS_NAME_INFO_TYPE,
  477. sizeof (IPX_STATIC_NETBIOS_NAME_INFO),
  478. NbName, NbNameEqual,
  479. (PIPX_INFO_BLOCK_HEADER * ) & pNewBlock
  480. );
  481. msg = MSG_NBNAME_DELETED_CFG;
  482. break;
  483. }
  484. if (rc == NO_ERROR)
  485. {
  486. rc = MprConfigInterfaceTransportSetInfo (
  487. g_hMprConfig, hIfCfg, hIfTrCfg, pNewBlock,
  488. ((PIPX_INFO_BLOCK_HEADER)pNewBlock)->Size
  489. );
  490. if (pNewBlock != pIfBlock)
  491. {
  492. GlobalFree (pNewBlock);
  493. }
  494. if (rc == NO_ERROR)
  495. {
  496. DisplayIPXMessage (g_hModule, msg, IfName );
  497. }
  498. else
  499. {
  500. DisplayError( g_hModule, rc);
  501. }
  502. }
  503. else
  504. {
  505. DisplayError( g_hModule, rc);
  506. }
  507. MprConfigBufferFree (pIfBlock);
  508. }
  509. else
  510. {
  511. DisplayError( g_hModule, rc);
  512. }
  513. }
  514. else
  515. {
  516. DisplayError( g_hModule, rc);
  517. }
  518. }
  519. else
  520. {
  521. DisplayError( g_hModule, rc);
  522. }
  523. return rc;
  524. }