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.

1498 lines
43 KiB

  1. #include "precomp.h"
  2. extern WCHAR IfNamBuffer[MaxIfDisplayLength];
  3. DWORD IfNamBufferLength=1024;
  4. ENUM_TO_STR InterfaceTypes[] = {
  5. { ROUTER_IF_TYPE_CLIENT, VAL_IFTYPE_CLIENT },
  6. { ROUTER_IF_TYPE_HOME_ROUTER, VAL_IFTYPE_HOME_ROUTER },
  7. { ROUTER_IF_TYPE_FULL_ROUTER, VAL_IFTYPE_FULL_ROUTER },
  8. { ROUTER_IF_TYPE_DEDICATED, VAL_IFTYPE_DEDICATED },
  9. { ROUTER_IF_TYPE_INTERNAL, VAL_IFTYPE_INTERNAL},
  10. { ROUTER_IF_TYPE_LOOPBACK, VAL_IFTYPE_LOOPBACK},
  11. { ROUTER_IF_TYPE_TUNNEL1, VAL_IFTYPE_TUNNEL1},
  12. };
  13. ENUM_TO_STR InterfaceStates[] = {
  14. { ROUTER_IF_STATE_DISCONNECTED, VAL_IFSTATE_DISCONNECTED },
  15. { ROUTER_IF_STATE_CONNECTING, VAL_IFSTATE_CONNECTING },
  16. { ROUTER_IF_STATE_CONNECTED, VAL_IFSTATE_CONNECTED }
  17. };
  18. ENUM_TO_STR InterfacePersistency[] = {
  19. { FALSE, VAL_NO },
  20. { TRUE, VAL_YES }
  21. };
  22. ENUM_TO_STR InterfaceEnableStatus[] = {
  23. { FALSE, VAL_ENABLED },
  24. { TRUE, VAL_DISABLED }
  25. };
  26. ENUM_TO_STR TransportIds[] = {
  27. { PID_IP, TOKEN_IP },
  28. { PID_IPX, TOKEN_IPX }
  29. };
  30. #define LOCAL_ROUTER_PB_PATHW L"%SystemRoot%\\system32\\RAS\\Router.Pbk"
  31. #define REMOTE_ROUTER_PB_PATHW L"\\\\%ls\\Admin$\\system32\\RAS\\Router.Pbk"
  32. int
  33. UpdateInterface (
  34. IN LPTSTR InterfaceName,
  35. IN DWORD pid
  36. );
  37. int
  38. CreateInterface (
  39. IN INT argc,
  40. IN LPTSTR *argv
  41. );
  42. int
  43. DeleteInterface (
  44. IN LPTSTR InterfaceName
  45. );
  46. int
  47. SetInterface (
  48. IN LPTSTR InterfaceName,
  49. IN LPTSTR UserName,
  50. IN LPTSTR Domain,
  51. IN LPTSTR Password
  52. );
  53. int
  54. ConnectInterface (
  55. IN LPTSTR InterfaceName
  56. );
  57. int
  58. DisconnectInterface (
  59. IN LPTSTR InterfaceName
  60. );
  61. int
  62. EnableInterface(
  63. IN LPTSTR lpInterface,
  64. IN BOOL bEnable
  65. );
  66. int
  67. ShowInterfaces (
  68. VOID
  69. );
  70. int
  71. ShowInterface (
  72. IN LPTSTR InterfaceName
  73. );
  74. DWORD
  75. IsPhoneBookEntry (
  76. LPWSTR InterfaceName
  77. );
  78. HINSTANCE HIf;
  79. PROUTEMON_PARAMS pParams;
  80. PROUTEMON_UTILS pUtils;
  81. #if defined( UNICODE ) || defined( _UNICODE )
  82. #define PrintString( s ) wprintf( L"%ls\n", (s) )
  83. #define PrintWord( w ) wprintf( L"%0x\n", (w) )
  84. #else
  85. #define PrintString( s ) _tprintf( TEXT( "%s\n" ), (s) )
  86. #define PrintWord( w ) _tprintf( TEXT( "%0x\n" ), (w) )
  87. #endif
  88. //-----------------------------------------------------------------------------
  89. // InterfaceMonitor
  90. //
  91. // Dispatches the command to the appropriate function.
  92. //-----------------------------------------------------------------------------
  93. int APIENTRY
  94. InterfaceMonitor (
  95. IN int argc,
  96. IN TCHAR *argv[],
  97. IN PROUTEMON_PARAMS params,
  98. IN PROUTEMON_UTILS utils
  99. ) {
  100. DWORD res = 0;
  101. TCHAR buffer[MAX_TOKEN];
  102. HIf = GetModuleHandle (NULL);
  103. pParams = params;
  104. pUtils = utils;
  105. if (argc>0) {
  106. if (_tcsicmp (argv[0], GetString (HIf, TOKEN_CREATE, buffer))==0) {
  107. if (argc>1)
  108. return CreateInterface (argc-1, &argv[1]);
  109. else
  110. res = ERROR_INVALID_PARAMETER;
  111. }
  112. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_DELETE, buffer))==0) {
  113. if (argc>1)
  114. return DeleteInterface (argv[1]);
  115. else
  116. res = ERROR_INVALID_PARAMETER;
  117. }
  118. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_SET, buffer))==0) {
  119. if (argc>4)
  120. return SetInterface (argv[1],argv[2],argv[3],argv[4]);
  121. else
  122. res = ERROR_INVALID_PARAMETER;
  123. }
  124. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_SHOW, buffer))==0) {
  125. if (argc>1)
  126. return ShowInterface (argv[1]);
  127. else
  128. return ShowInterfaces ();
  129. }
  130. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_CONNECT, buffer))==0) {
  131. if (argc>1)
  132. return ConnectInterface (argv[1]);
  133. else
  134. res = ERROR_INVALID_PARAMETER;
  135. }
  136. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_DISCONNECT, buffer))==0) {
  137. if (argc>1)
  138. return DisconnectInterface (argv[1]);
  139. else
  140. res = ERROR_INVALID_PARAMETER;
  141. }
  142. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_ENABLE, buffer))==0) {
  143. if ( argc > 1 )
  144. return EnableInterface (argv[1], TRUE);
  145. else
  146. res = ERROR_INVALID_PARAMETER;
  147. }
  148. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_DISABLE, buffer))==0) {
  149. if ( argc > 1 )
  150. return EnableInterface ( argv[1], FALSE );
  151. else
  152. res = ERROR_INVALID_PARAMETER;
  153. }
  154. else if (_tcsicmp (argv[0], GetString (HIf, TOKEN_UPDATE, buffer))==0) {
  155. DWORD pid;
  156. if ((argc>2) && (GetValueFromString (HIf, pUtils, TransportIds, argv[2], &pid)))
  157. return UpdateInterface (argv[1], pid);
  158. else
  159. res = ERROR_INVALID_PARAMETER;
  160. }
  161. else if ((_tcsicmp (argv[0], GetString (HIf, TOKEN_HELP1, buffer))==0)
  162. || (_tcsicmp (argv[0], GetString (HIf, TOKEN_HELP2, buffer))==0)
  163. || (_tcsicmp (argv[0], GetString (HIf, TOKEN_HELP3, buffer))==0))
  164. NOTHING;
  165. else
  166. res = ERROR_INVALID_PARAMETER;
  167. }
  168. else
  169. res = ERROR_INVALID_PARAMETER;
  170. pUtils->put_msg (HIf, MSG_INTERFACE_HELP, pParams->pszProgramName);
  171. return res;
  172. }
  173. //-----------------------------------------------------------------------------
  174. // CreateInterface
  175. //
  176. // Create a demand-dial interface
  177. //-----------------------------------------------------------------------------
  178. int
  179. CreateInterface (
  180. IN INT argc,
  181. IN LPTSTR *argv
  182. ) {
  183. MPR_INTERFACE_0 Ri0;
  184. HANDLE hIfCfg;
  185. DWORD rc, dwIfType;
  186. unsigned count;
  187. TCHAR buffer[MAX_TOKEN];
  188. ULONG ulIfIndex;
  189. ZeroMemory(&Ri0,
  190. sizeof(Ri0));
  191. //
  192. // if there is only one argument, then it is the interface name
  193. // which may even be called TUNNEL1
  194. // if there is more than one argument, and the first is TUNNEL1, then
  195. // the next one is the interface name
  196. //
  197. ulIfIndex = 0;
  198. dwIfType = ROUTER_IF_TYPE_FULL_ROUTER;
  199. if(argc > 1)
  200. {
  201. if(_tcsicmp(argv[0], GetString (HIf, TOKEN_TUNNEL1, buffer))==0)
  202. {
  203. ulIfIndex = 1;
  204. dwIfType = ROUTER_IF_TYPE_TUNNEL1;
  205. Ri0.fEnabled = TRUE;
  206. }
  207. }
  208. //
  209. // convert interface name to unicode
  210. //
  211. #if defined(UNICODE) || defined (_UNICODE)
  212. wcsncpy (Ri0.wszInterfaceName, argv[ulIfIndex],
  213. sizeof (Ri0.wszInterfaceName)/sizeof (Ri0.wszInterfaceName[0]));
  214. count = wcslen (argv[ulIfIndex]);
  215. #else
  216. count = mbstowcs (Ri0.wszInterfaceName, argv[ulIfIndex],
  217. sizeof (Ri0.wszInterfaceName));
  218. #endif
  219. do
  220. {
  221. if ( count > MAX_INTERFACE_NAME_LEN )
  222. {
  223. rc = ERROR_INVALID_PARAMETER;
  224. break;
  225. }
  226. if(dwIfType == ROUTER_IF_TYPE_FULL_ROUTER)
  227. {
  228. //
  229. // to create an interface we need a phone book entry
  230. // for it.
  231. //
  232. rc = IsPhoneBookEntry (Ri0.wszInterfaceName);
  233. if ( rc != NO_ERROR )
  234. {
  235. break;
  236. }
  237. }
  238. //
  239. // create interface with defaults
  240. //
  241. Ri0.hInterface = INVALID_HANDLE_VALUE;
  242. Ri0.dwIfType = dwIfType;
  243. rc = MprConfigInterfaceCreate (
  244. pParams->hRouterConfig,
  245. 0,
  246. (LPBYTE)&Ri0,
  247. &hIfCfg
  248. );
  249. if ( rc != NO_ERROR )
  250. {
  251. break;
  252. }
  253. pUtils->put_msg (HIf, MSG_INTERFACE_CREATED, Ri0.wszInterfaceName);
  254. //
  255. // if router service is running add the interface
  256. // to it too.
  257. //
  258. if ( pParams->hRouterAdmin ) {
  259. HANDLE hIfAdmin;
  260. rc = MprAdminInterfaceCreate (
  261. pParams->hRouterAdmin,
  262. 0,
  263. (LPBYTE)&Ri0,
  264. &hIfAdmin
  265. );
  266. if ( rc != NO_ERROR )
  267. {
  268. break;
  269. }
  270. pUtils->put_msg (HIf, MSG_INTERFACE_ADDED, Ri0.wszInterfaceName);
  271. }
  272. } while( FALSE );
  273. if ( rc != NO_ERROR ) { pUtils->put_error_msg (rc); }
  274. return rc;
  275. }
  276. //-----------------------------------------------------------------------------
  277. // SetInterface
  278. //
  279. // sets the credentials to be used by an interface when dialing into
  280. // a remote router.
  281. //-----------------------------------------------------------------------------
  282. int
  283. SetInterface (
  284. IN LPTSTR InterfaceName,
  285. IN LPTSTR UserName,
  286. IN LPTSTR Domain,
  287. IN LPTSTR Password
  288. )
  289. {
  290. HANDLE hIfCfg = NULL;
  291. DWORD rc = (DWORD) -1,
  292. rc2 = 0,
  293. dwSize = 0;
  294. unsigned ci = 0,
  295. cu = 0,
  296. cd = 0,
  297. cp = 0;
  298. PMPR_INTERFACE_0 pRi0 = NULL;
  299. //
  300. // convert parameters to WCHAR
  301. //
  302. #if defined(UNICODE) || defined (_UNICODE)
  303. #define pInterfaceName InterfaceName
  304. #define pUserName UserName
  305. #define pDomain Domain
  306. #define pPassword Password
  307. ci = wcslen (InterfaceName);
  308. cu = wcslen (UserName);
  309. cd = wcslen (Domain);
  310. cp = wcslen (Password);
  311. #else
  312. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  313. WCHAR UserNameW[257];
  314. WCHAR DomainW[257];
  315. WCHAR PasswordW[257];
  316. #define pInterfaceName InterfaceNameW
  317. #define pUserName UserNameW
  318. #define pDomain DomainW
  319. #define pPassword PasswordW
  320. ci = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  321. cu = mbstowcs (UserNameW, UserName, sizeof (UserNameW));
  322. cd = mbstowcs (DomainW, Domain, sizeof (DomainW));
  323. cp = mbstowcs (PasswordW, Password, sizeof (PasswordW));
  324. #endif
  325. //======================================
  326. // Translate the Interface Name
  327. //======================================
  328. rc2 = Description2IfNameW(pInterfaceName,
  329. IfNamBuffer,
  330. &IfNamBufferLength);
  331. //======================================
  332. do
  333. {
  334. //
  335. // verify parameters
  336. //
  337. if ( ( ci > MAX_INTERFACE_NAME_LEN ) ||
  338. ( cu > 256 ) ||
  339. ( cd > 256 ) ||
  340. ( cp > 256 ) )
  341. {
  342. rc = ERROR_INVALID_PARAMETER;
  343. break;
  344. }
  345. //
  346. // verify if the interface is a demand-dial interface
  347. // before setting credentials on it.
  348. //
  349. rc = MprConfigInterfaceGetHandle(
  350. pParams->hRouterConfig,
  351. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  352. &hIfCfg
  353. );
  354. if ( rc != NO_ERROR )
  355. {
  356. break;
  357. }
  358. rc = MprConfigInterfaceGetInfo (
  359. pParams->hRouterConfig,
  360. hIfCfg,
  361. 0,
  362. (LPBYTE *) &pRi0,
  363. &dwSize
  364. );
  365. if ( rc != NO_ERROR )
  366. {
  367. break;
  368. }
  369. if ( pRi0-> dwIfType != ROUTER_IF_TYPE_FULL_ROUTER )
  370. {
  371. rc = ERROR_INVALID_PARAMETER;
  372. break;
  373. }
  374. //
  375. // set the credentials in the router
  376. //
  377. rc = MprAdminInterfaceSetCredentials (
  378. pParams-> wszRouterName,
  379. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  380. pUserName,
  381. pDomain,
  382. pPassword
  383. );
  384. if ( rc != NO_ERROR )
  385. {
  386. break;
  387. }
  388. pUtils->put_msg (HIf, MSG_INTERFACE_SET, (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName);
  389. } while( FALSE );
  390. //
  391. // free allocations and report errors
  392. //
  393. if ( pRi0 ) { MprConfigBufferFree( pRi0 ); }
  394. if ( rc != NO_ERROR ) { pUtils-> put_error_msg( rc ); }
  395. #undef pInterfaceName
  396. #undef pUserName
  397. #undef pDomain
  398. #undef pPassword
  399. return rc;
  400. }
  401. //-----------------------------------------------------------------------------
  402. // DeleteInterface
  403. //
  404. // Deletes a demand-dial Interface.
  405. //-----------------------------------------------------------------------------
  406. int
  407. DeleteInterface (
  408. IN LPTSTR InterfaceName
  409. ) {
  410. HANDLE hIfCfg;
  411. DWORD rc, rc2;
  412. unsigned count;
  413. PMPR_INTERFACE_0 pRi0;
  414. DWORD sz;
  415. #if defined(UNICODE) || defined (_UNICODE)
  416. #define pInterfaceName InterfaceName
  417. count = wcslen (InterfaceName);
  418. #else
  419. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  420. #define pInterfaceName InterfaceNameW
  421. count = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  422. #endif
  423. //======================================
  424. // Translate the Interface Name
  425. //======================================
  426. rc2 = Description2IfNameW(pInterfaceName,
  427. IfNamBuffer,
  428. &IfNamBufferLength);
  429. //======================================
  430. if (count<=MAX_INTERFACE_NAME_LEN) {
  431. rc = MprConfigInterfaceGetHandle (
  432. pParams->hRouterConfig,
  433. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  434. &hIfCfg
  435. );
  436. if (rc==NO_ERROR) {
  437. rc = MprConfigInterfaceGetInfo (
  438. pParams->hRouterConfig,
  439. hIfCfg,
  440. 0,
  441. (LPBYTE *)&pRi0,
  442. &sz);
  443. if (rc==NO_ERROR) {
  444. if((pRi0->dwIfType==ROUTER_IF_TYPE_FULL_ROUTER) ||
  445. (pRi0->dwIfType==ROUTER_IF_TYPE_TUNNEL1))
  446. {
  447. rc = MprConfigInterfaceDelete (
  448. pParams->hRouterConfig,
  449. hIfCfg);
  450. if (rc==NO_ERROR) {
  451. pUtils->put_msg (HIf, MSG_INTERFACE_DELETED, (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName);
  452. if (pParams->hRouterAdmin) {
  453. HANDLE hIfAdmin;
  454. rc = MprAdminInterfaceGetHandle (
  455. pParams->hRouterAdmin,
  456. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  457. &hIfAdmin,
  458. FALSE
  459. );
  460. if (rc==NO_ERROR) {
  461. rc = MprAdminInterfaceDelete (
  462. pParams->hRouterAdmin,
  463. hIfAdmin);
  464. if (rc==NO_ERROR)
  465. pUtils->put_msg (HIf, MSG_INTERFACE_REMOVED, (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName);
  466. }
  467. }
  468. }
  469. }
  470. else
  471. rc = ERROR_INVALID_PARAMETER;
  472. MprConfigBufferFree (pRi0);
  473. }
  474. }
  475. }
  476. else
  477. {
  478. rc = ERROR_INVALID_PARAMETER;
  479. }
  480. if ( rc != NO_ERROR )
  481. {
  482. pUtils->put_error_msg (rc);
  483. }
  484. return rc;
  485. #undef pInterfaceName
  486. }
  487. //-----------------------------------------------------------------------------
  488. // ShowInterfaces
  489. //
  490. // Display Interfaces on a router.
  491. //-----------------------------------------------------------------------------
  492. int
  493. ShowInterfaces (
  494. VOID
  495. ) {
  496. DWORD rc = NO_ERROR, rc2;
  497. DWORD read, total, processed=0, i;
  498. DWORD hResume = 0;
  499. PMPR_INTERFACE_0 pRi0;
  500. if (pParams->hRouterAdmin)
  501. pUtils->put_msg (HIf, MSG_INTERFACE_RTR_TABLE_HDR);
  502. else
  503. pUtils->put_msg (HIf, MSG_INTERFACE_CFG_TABLE_HDR);
  504. do {
  505. if (pParams->hRouterAdmin)
  506. rc = MprAdminInterfaceEnum (
  507. pParams->hRouterAdmin,
  508. 0,
  509. (LPBYTE *)&pRi0,
  510. MAXULONG,
  511. &read,
  512. &total,
  513. &hResume);
  514. else
  515. rc = MprConfigInterfaceEnum (
  516. pParams->hRouterConfig,
  517. 0,
  518. (LPBYTE *)&pRi0,
  519. MAXULONG,
  520. &read,
  521. &total,
  522. &hResume);
  523. if (rc==NO_ERROR) {
  524. for (i=0; i<read; i++) {
  525. TCHAR buffer[3][MAX_VALUE];
  526. //======================================
  527. // Translate the Interface Name
  528. //======================================
  529. rc2 = IfName2DescriptionW(pRi0[i].wszInterfaceName,
  530. IfNamBuffer,
  531. &IfNamBufferLength);
  532. //======================================
  533. if (pParams->hRouterAdmin)
  534. pUtils->put_msg (HIf,
  535. MSG_INTERFACE_RTR_TABLE_FMT,
  536. GetValueString (HIf, pUtils, InterfaceEnableStatus,
  537. pRi0[i].fEnabled ? 0 : 1,
  538. buffer[1]),
  539. GetValueString (HIf, pUtils, InterfaceStates,
  540. pRi0[i].dwConnectionState, buffer[2]),
  541. GetValueString (HIf, pUtils, InterfaceTypes,
  542. pRi0[i].dwIfType, buffer[0]),
  543. (rc2 == NO_ERROR) ? IfNamBuffer : pRi0[i].wszInterfaceName
  544. );
  545. else
  546. pUtils->put_msg (HIf,
  547. MSG_INTERFACE_CFG_TABLE_FMT,
  548. GetValueString (HIf, pUtils, InterfaceEnableStatus,
  549. pRi0[i].fEnabled ? 0 : 1,
  550. buffer[1]),
  551. GetValueString (HIf, pUtils, InterfaceTypes,
  552. pRi0[i].dwIfType, buffer[0] ),
  553. (rc2 == NO_ERROR) ? IfNamBuffer : pRi0[i].wszInterfaceName
  554. );
  555. }
  556. processed += read;
  557. if (pParams->hRouterAdmin)
  558. MprAdminBufferFree (pRi0);
  559. else
  560. MprConfigBufferFree (pRi0);
  561. }
  562. else {
  563. pUtils->put_error_msg (rc);
  564. break;
  565. }
  566. }
  567. while (processed<total);
  568. return rc;
  569. }
  570. //-----------------------------------------------------------------------------
  571. // ShowInterface
  572. //
  573. // Display data for a single interface
  574. //-----------------------------------------------------------------------------
  575. int
  576. ShowInterface (
  577. IN LPTSTR InterfaceName
  578. ) {
  579. HANDLE hIfCfg;
  580. HANDLE hIfAdmin;
  581. DWORD rc, rc2;
  582. unsigned count;
  583. PMPR_INTERFACE_0 pRi0;
  584. DWORD sz;
  585. #if defined(UNICODE) || defined (_UNICODE)
  586. #define pInterfaceName InterfaceName
  587. count = wcslen (InterfaceName);
  588. #else
  589. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  590. #define pInterfaceName InterfaceNameW
  591. count = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  592. #endif
  593. //======================================
  594. // Translate the Interface Name
  595. //======================================
  596. rc2 = Description2IfNameW(pInterfaceName,
  597. IfNamBuffer,
  598. &IfNamBufferLength);
  599. //======================================
  600. if (count<=MAX_INTERFACE_NAME_LEN) {
  601. if (pParams->hRouterAdmin)
  602. rc = MprAdminInterfaceGetHandle (
  603. pParams->hRouterAdmin,
  604. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  605. &hIfAdmin,
  606. FALSE
  607. );
  608. else
  609. rc = MprConfigInterfaceGetHandle (
  610. pParams->hRouterConfig,
  611. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  612. &hIfCfg
  613. );
  614. if (rc==NO_ERROR) {
  615. if (pParams->hRouterAdmin)
  616. rc = MprAdminInterfaceGetInfo (
  617. pParams->hRouterAdmin,
  618. hIfAdmin,
  619. 0,
  620. (LPBYTE *)&pRi0);
  621. else
  622. rc = MprConfigInterfaceGetInfo (
  623. pParams->hRouterConfig,
  624. hIfCfg,
  625. 0,
  626. (LPBYTE *)&pRi0,
  627. &sz);
  628. if (rc==NO_ERROR) {
  629. TCHAR buffer[3][MAX_VALUE];
  630. //======================================
  631. // Translate the Interface Name
  632. //======================================
  633. rc2 = IfName2DescriptionW(pRi0->wszInterfaceName,
  634. IfNamBuffer,
  635. &IfNamBufferLength);
  636. //======================================
  637. if (pParams->hRouterAdmin)
  638. pUtils->put_msg (HIf,
  639. MSG_INTERFACE_RTR_SCREEN_FMT,
  640. (rc2 == NO_ERROR) ? IfNamBuffer : pRi0->wszInterfaceName,
  641. GetValueString (HIf, pUtils, InterfaceTypes,
  642. pRi0->dwIfType, buffer[0]),
  643. GetValueString (HIf, pUtils, InterfaceEnableStatus,
  644. pRi0-> fEnabled ? 0 : 1,
  645. buffer[1]),
  646. GetValueString (HIf, pUtils, InterfaceStates,
  647. pRi0->dwConnectionState, buffer[2])
  648. );
  649. else
  650. pUtils->put_msg (HIf,
  651. MSG_INTERFACE_CFG_SCREEN_FMT,
  652. (rc2 == NO_ERROR) ? IfNamBuffer : pRi0->wszInterfaceName,
  653. IfNamBuffer,
  654. GetValueString (HIf, pUtils, InterfaceTypes,
  655. pRi0->dwIfType, buffer[0]),
  656. GetValueString (HIf, pUtils, InterfaceEnableStatus,
  657. pRi0-> fEnabled ? 0 : 1,
  658. buffer[1])
  659. );
  660. }
  661. }
  662. if ( rc != NO_ERROR )
  663. {
  664. pUtils->put_error_msg (rc);
  665. }
  666. return rc;
  667. }
  668. else
  669. {
  670. pUtils->put_msg (HIf, MSG_INVALID_INTERFACE_NAME);
  671. return ERROR_INVALID_PARAMETER;
  672. }
  673. }
  674. //-----------------------------------------------------------------------------
  675. // UpdateInterface
  676. //
  677. // Initiate autostatic updates over an interface
  678. //-----------------------------------------------------------------------------
  679. int
  680. UpdateInterface (
  681. IN LPTSTR InterfaceName,
  682. IN DWORD pid
  683. ) {
  684. if (pParams->hRouterAdmin) {
  685. HANDLE hIfAdmin;
  686. DWORD rc, rc2;
  687. unsigned count;
  688. PMPR_INTERFACE_0 pRi0;
  689. #if defined(UNICODE) || defined (_UNICODE)
  690. #define pInterfaceName InterfaceName
  691. count = wcslen (InterfaceName);
  692. #else
  693. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  694. #define pInterfaceName InterfaceNameW
  695. count = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  696. #endif
  697. //======================================
  698. // Translate the Interface Name
  699. //======================================
  700. rc2 = Description2IfNameW(pInterfaceName,
  701. IfNamBuffer,
  702. &IfNamBufferLength);
  703. //======================================
  704. if (count<=MAX_INTERFACE_NAME_LEN) {
  705. rc = MprAdminInterfaceGetHandle (
  706. pParams->hRouterAdmin,
  707. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  708. &hIfAdmin,
  709. FALSE
  710. );
  711. if (rc==NO_ERROR) {
  712. rc = MprAdminInterfaceGetInfo (
  713. pParams->hRouterAdmin,
  714. hIfAdmin,
  715. 0,
  716. (LPBYTE *)&pRi0);
  717. if (rc==NO_ERROR) {
  718. if (pRi0->dwIfType==ROUTER_IF_TYPE_FULL_ROUTER) {
  719. HANDLE hEvent = NULL;
  720. if (pParams->fLocalRouter) {
  721. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  722. if (hEvent==NULL) {
  723. rc = GetLastError ();
  724. goto Exit;
  725. }
  726. }
  727. pUtils->put_msg (HIf, MSG_WAIT_FOR_UPDATE);
  728. rc = MprAdminInterfaceUpdateRoutes (
  729. pParams->hRouterAdmin,
  730. hIfAdmin,
  731. pid,
  732. hEvent);
  733. if (pParams->fLocalRouter) {
  734. if (rc==PENDING) {
  735. rc = WaitForSingleObject (hEvent, INFINITE);
  736. ASSERT (rc==WAIT_OBJECT_0);
  737. }
  738. CloseHandle (hEvent);
  739. }
  740. if (rc==NO_ERROR) {
  741. DWORD result;
  742. rc = MprAdminInterfaceQueryUpdateResult (
  743. pParams->hRouterAdmin,
  744. hIfAdmin,
  745. pid,
  746. &result);
  747. if (rc==NO_ERROR)
  748. rc = result;
  749. }
  750. }
  751. else
  752. rc = ERROR_INVALID_PARAMETER;
  753. Exit:
  754. MprAdminBufferFree (pRi0);
  755. }
  756. if (rc == NO_ERROR) {
  757. pUtils->put_msg (HIf, MSG_UPDATE_COMPLETED);
  758. return rc;
  759. }
  760. else {
  761. pUtils->put_error_msg (rc);
  762. return rc;
  763. }
  764. }
  765. else {
  766. pUtils->put_error_msg (rc);
  767. return rc;
  768. }
  769. }
  770. else {
  771. pUtils->put_msg (HIf, MSG_INVALID_INTERFACE_NAME);
  772. return ERROR_INVALID_PARAMETER;
  773. }
  774. }
  775. else {
  776. pUtils->put_msg (HIf, MSG_ROUTER_NOT_RUNNING);
  777. return NO_ERROR;
  778. }
  779. }
  780. //-----------------------------------------------------------------------------
  781. // ConnectInterface
  782. //
  783. // Initiate a connect on a demand-dial interface
  784. //-----------------------------------------------------------------------------
  785. int
  786. ConnectInterface (
  787. IN LPTSTR InterfaceName
  788. ) {
  789. HANDLE hIfAdmin = NULL;
  790. DWORD rc = (DWORD) -1, rc2;
  791. unsigned count = 0;
  792. PMPR_INTERFACE_0 pRi0 = NULL;
  793. //
  794. // convert interface name to unicode
  795. //
  796. #if defined( UNICODE ) || defined( _UNICODE )
  797. #define pInterfaceName InterfaceName
  798. count = wcslen (InterfaceName);
  799. #else
  800. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  801. #define pInterfaceName InterfaceNameW
  802. count = mbstowcs (
  803. InterfaceNameW,
  804. InterfaceName,
  805. sizeof (InterfaceNameW)
  806. );
  807. #endif
  808. //======================================
  809. // Translate the Interface Name
  810. //======================================
  811. rc2 = Description2IfNameW(pInterfaceName,
  812. IfNamBuffer,
  813. &IfNamBufferLength);
  814. //======================================
  815. do
  816. {
  817. //
  818. // check if connected to router
  819. //
  820. if ( !pParams-> hRouterAdmin )
  821. {
  822. pUtils-> put_msg( HIf, MSG_ROUTER_NOT_RUNNING );
  823. break;
  824. }
  825. //
  826. // verify valid interface name
  827. //
  828. if ( count > MAX_INTERFACE_NAME_LEN )
  829. {
  830. pUtils-> put_msg( HIf, MSG_INVALID_INTERFACE_NAME );
  831. break;
  832. }
  833. //
  834. // verify that specified interface is a demand dial interface
  835. //
  836. rc = MprAdminInterfaceGetHandle (
  837. pParams->hRouterAdmin,
  838. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  839. &hIfAdmin,
  840. FALSE
  841. );
  842. if ( rc != NO_ERROR )
  843. {
  844. pUtils-> put_error_msg( rc );
  845. break;
  846. }
  847. rc = MprAdminInterfaceGetInfo (
  848. pParams->hRouterAdmin,
  849. hIfAdmin,
  850. 0,
  851. (LPBYTE *) &pRi0
  852. );
  853. if ( rc != NO_ERROR )
  854. {
  855. pUtils-> put_error_msg( rc );
  856. break;
  857. }
  858. if ( pRi0-> dwIfType != ROUTER_IF_TYPE_FULL_ROUTER )
  859. {
  860. pUtils-> put_msg( HIf, ERROR_INVALID_PARAMETER );
  861. break;
  862. }
  863. //
  864. // connect interface.
  865. //
  866. pUtils-> put_msg( HIf, MSG_WAIT_FOR_CONNECT );
  867. rc = MprAdminInterfaceConnect(
  868. pParams-> hRouterAdmin,
  869. hIfAdmin,
  870. NULL,
  871. TRUE
  872. );
  873. if ( rc != NO_ERROR && rc != PENDING )
  874. {
  875. pUtils-> put_error_msg( rc );
  876. break;
  877. }
  878. pUtils-> put_msg( HIf, MSG_CONNECT_COMPLETED );
  879. rc = NO_ERROR;
  880. } while( FALSE );
  881. //
  882. // clean up
  883. //
  884. if ( pRi0 ) { MprAdminBufferFree( pRi0 ); }
  885. return rc;
  886. #if 0
  887. if (pParams->hRouterAdmin) {
  888. HANDLE hIfAdmin;
  889. DWORD rc;
  890. unsigned count;
  891. PMPR_INTERFACE_0 pRi0;
  892. #if defined(UNICODE) || defined (_UNICODE)
  893. #define pInterfaceName InterfaceName
  894. count = wcslen (InterfaceName);
  895. #else
  896. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  897. #define pInterfaceName InterfaceNameW
  898. count = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  899. #endif
  900. if (count<=MAX_INTERFACE_NAME_LEN) {
  901. rc = MprAdminInterfaceGetHandle (
  902. pParams->hRouterAdmin,
  903. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  904. &hIfAdmin,
  905. FALSE
  906. );
  907. if (rc==NO_ERROR) {
  908. rc = MprAdminInterfaceGetInfo (
  909. pParams->hRouterAdmin,
  910. hIfAdmin,
  911. 0,
  912. (LPBYTE *)&pRi0);
  913. if (rc==NO_ERROR) {
  914. if (pRi0->dwIfType==ROUTER_IF_TYPE_FULL_ROUTER) {
  915. HANDLE hEvent = NULL;
  916. if (pParams->fLocalRouter) {
  917. hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
  918. if (hEvent==NULL) {
  919. rc = GetLastError ();
  920. goto Exit;
  921. }
  922. }
  923. pUtils->put_msg (HIf, MSG_WAIT_FOR_CONNECT);
  924. rc = MprAdminInterfaceConnect (
  925. pParams->hRouterAdmin,
  926. hIfAdmin,
  927. hEvent,
  928. TRUE
  929. );
  930. if (pParams->fLocalRouter) {
  931. if (rc==PENDING) {
  932. rc = WaitForSingleObject (hEvent, INFINITE);
  933. ASSERT (rc==WAIT_OBJECT_0);
  934. }
  935. CloseHandle (hEvent);
  936. }
  937. }
  938. else
  939. rc = ERROR_INVALID_PARAMETER;
  940. Exit:
  941. MprAdminBufferFree (pRi0);
  942. }
  943. if (rc==NO_ERROR) {
  944. pUtils->put_msg (HIf, MSG_CONNECT_COMPLETED);
  945. return 0;
  946. }
  947. else {
  948. pUtils->put_error_msg (rc);
  949. return 1;
  950. }
  951. }
  952. else {
  953. pUtils->put_error_msg (rc);
  954. return 1;
  955. }
  956. }
  957. else {
  958. pUtils->put_msg (HIf, MSG_INVALID_INTERFACE_NAME);
  959. return 1;
  960. }
  961. }
  962. else {
  963. pUtils->put_msg (HIf, MSG_ROUTER_NOT_RUNNING);
  964. return 1;
  965. }
  966. #endif
  967. }
  968. //-----------------------------------------------------------------------------
  969. // DisconnectInterface
  970. //
  971. // Disconnect a demand-dial interface
  972. //-----------------------------------------------------------------------------
  973. int
  974. DisconnectInterface (
  975. IN LPTSTR InterfaceName
  976. ) {
  977. if (pParams->hRouterAdmin) {
  978. HANDLE hIfAdmin;
  979. DWORD rc, rc2;
  980. unsigned count;
  981. PMPR_INTERFACE_0 pRi0;
  982. #if defined(UNICODE) || defined (_UNICODE)
  983. #define pInterfaceName InterfaceName
  984. count = wcslen (InterfaceName);
  985. #else
  986. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  987. #define pInterfaceName InterfaceNameW
  988. count = mbstowcs (InterfaceNameW, InterfaceName, sizeof (InterfaceNameW));
  989. #endif
  990. //======================================
  991. // Translate the Interface Name
  992. //======================================
  993. rc2 = Description2IfNameW(pInterfaceName,
  994. IfNamBuffer,
  995. &IfNamBufferLength);
  996. //======================================
  997. if (count<=MAX_INTERFACE_NAME_LEN) {
  998. rc = MprAdminInterfaceGetHandle (
  999. pParams->hRouterAdmin,
  1000. (rc2 == NO_ERROR) ? IfNamBuffer : pInterfaceName,
  1001. &hIfAdmin,
  1002. FALSE
  1003. );
  1004. if (rc==NO_ERROR) {
  1005. rc = MprAdminInterfaceGetInfo (
  1006. pParams->hRouterAdmin,
  1007. hIfAdmin,
  1008. 0,
  1009. (LPBYTE *)&pRi0);
  1010. if (rc==NO_ERROR) {
  1011. if (pRi0->dwIfType==ROUTER_IF_TYPE_FULL_ROUTER) {
  1012. rc = MprAdminInterfaceDisconnect (
  1013. pParams->hRouterAdmin,
  1014. hIfAdmin);
  1015. }
  1016. else
  1017. rc = ERROR_INVALID_PARAMETER;
  1018. //Exit:
  1019. MprAdminBufferFree (pRi0);
  1020. }
  1021. if (rc==NO_ERROR) {
  1022. pUtils->put_msg (HIf, MSG_DISCONNECT_COMPLETED);
  1023. return rc;
  1024. }
  1025. else {
  1026. pUtils->put_error_msg (rc);
  1027. return rc;
  1028. }
  1029. }
  1030. else {
  1031. pUtils->put_error_msg (rc);
  1032. return rc;
  1033. }
  1034. }
  1035. else {
  1036. pUtils->put_msg (HIf, MSG_INVALID_INTERFACE_NAME);
  1037. return ERROR_INVALID_PARAMETER;
  1038. }
  1039. }
  1040. else {
  1041. pUtils->put_msg (HIf, MSG_ROUTER_NOT_RUNNING);
  1042. return NO_ERROR;
  1043. }
  1044. }
  1045. //-----------------------------------------------------------------------------
  1046. // EnableInterface
  1047. //
  1048. // Enable/disable a demand-dial interface.
  1049. //-----------------------------------------------------------------------------
  1050. int
  1051. EnableInterface(
  1052. IN LPTSTR lpInterface,
  1053. IN BOOL bEnable
  1054. )
  1055. {
  1056. DWORD dwCount = 0,
  1057. dwSize = 0,
  1058. rc2 = 0,
  1059. dwErr = (DWORD) -1;
  1060. HANDLE hInterface = NULL;
  1061. PMPR_INTERFACE_0 pMprIf0 = NULL;
  1062. //
  1063. // convert interface name to Unicode
  1064. //
  1065. #if defined( UNICODE ) || defined( _UNICODE )
  1066. #define lpInterfaceName lpInterface
  1067. dwCount = wcslen( lpInterface );
  1068. #else
  1069. WCHAR InterfaceNameW[MAX_INTERFACE_NAME_LEN+1];
  1070. #define lpInterfaceName InterfaceNameW
  1071. dwCount = mbstowcs (
  1072. InterfaceNameW,
  1073. lpInterface,
  1074. sizeof( InterfaceNameW )
  1075. );
  1076. #endif
  1077. //======================================
  1078. // Translate the Interface Name
  1079. //======================================
  1080. rc2 = Description2IfNameW(lpInterfaceName,
  1081. IfNamBuffer,
  1082. &IfNamBufferLength);
  1083. //======================================
  1084. //
  1085. // Error break out loop
  1086. //
  1087. do
  1088. {
  1089. //
  1090. // Update the enable flag in the router config
  1091. //
  1092. dwErr = MprConfigInterfaceGetHandle(
  1093. pParams-> hRouterConfig,
  1094. (rc2 == NO_ERROR) ? IfNamBuffer : lpInterfaceName,
  1095. &hInterface
  1096. );
  1097. if ( dwErr != NO_ERROR )
  1098. {
  1099. break;
  1100. }
  1101. dwErr = MprConfigInterfaceGetInfo(
  1102. pParams-> hRouterConfig,
  1103. hInterface,
  1104. 0,
  1105. (LPBYTE *) &pMprIf0,
  1106. &dwSize
  1107. );
  1108. if ( dwErr != NO_ERROR )
  1109. {
  1110. break;
  1111. }
  1112. pMprIf0-> fEnabled = bEnable;
  1113. dwErr = MprConfigInterfaceSetInfo(
  1114. pParams-> hRouterConfig,
  1115. hInterface,
  1116. 0,
  1117. (LPBYTE) pMprIf0
  1118. );
  1119. if ( dwErr != NO_ERROR )
  1120. {
  1121. break;
  1122. }
  1123. //
  1124. // if you have a handle to the router service, update
  1125. // the interface in the router service as well.
  1126. //
  1127. if ( !pParams-> hRouterAdmin )
  1128. {
  1129. break;
  1130. }
  1131. dwErr = MprAdminInterfaceGetHandle(
  1132. pParams-> hRouterAdmin,
  1133. (rc2 == NO_ERROR) ? IfNamBuffer : lpInterfaceName,
  1134. &hInterface,
  1135. FALSE
  1136. );
  1137. if ( dwErr != NO_ERROR )
  1138. {
  1139. break;
  1140. }
  1141. dwErr = MprAdminInterfaceSetInfo(
  1142. pParams-> hRouterAdmin,
  1143. hInterface,
  1144. 0,
  1145. (LPBYTE) pMprIf0
  1146. );
  1147. if ( dwErr != NO_ERROR )
  1148. {
  1149. break;
  1150. }
  1151. } while ( FALSE );
  1152. if ( dwErr != NO_ERROR ) { pUtils-> put_error_msg( dwErr ); }
  1153. if ( pMprIf0 ) { MprConfigBufferFree( pMprIf0 ); }
  1154. return (int) dwErr;
  1155. }
  1156. typedef DWORD (*PRasValidateEntryName)(
  1157. LPWSTR lpszPhonebook, // pointer to full path and filename of phone-book file
  1158. LPWSTR lpszEntry // pointer to the entry name to validate
  1159. );
  1160. //-----------------------------------------------------------------------------
  1161. // IsPhoneBookEntry
  1162. //
  1163. // Verify that a phone book entry is present for a specified interface
  1164. //-----------------------------------------------------------------------------
  1165. DWORD
  1166. IsPhoneBookEntry (
  1167. LPWSTR InterfaceNameW
  1168. ) {
  1169. HMODULE hRasApi32;
  1170. PRasValidateEntryName RasValidateEntryName;
  1171. DWORD rc;
  1172. WCHAR wszPbPath[MAX_PATH+1];
  1173. //
  1174. // get phone book path + file name
  1175. //
  1176. if ( pParams->fLocalRouter ) {
  1177. rc = ExpandEnvironmentStringsW (
  1178. LOCAL_ROUTER_PB_PATHW,
  1179. wszPbPath,
  1180. sizeof (wszPbPath)/sizeof (wszPbPath[0])
  1181. );
  1182. }
  1183. else {
  1184. rc = wsprintfW (wszPbPath, REMOTE_ROUTER_PB_PATHW, pParams->wszRouterName);
  1185. }
  1186. ASSERT (rc > 0);
  1187. //
  1188. // Load RASAPI32 DLL and call into it to verify specified
  1189. // phone book entry
  1190. //
  1191. hRasApi32 = LoadLibrary ("RASAPI32.DLL");
  1192. if (hRasApi32!=NULL) {
  1193. RasValidateEntryName = (PRasValidateEntryName)
  1194. GetProcAddress (
  1195. hRasApi32,
  1196. "RasValidateEntryNameW"
  1197. );
  1198. if ( RasValidateEntryName != NULL ) {
  1199. rc = RasValidateEntryName (
  1200. wszPbPath,
  1201. InterfaceNameW
  1202. );
  1203. if ( rc == NO_ERROR )
  1204. rc = ERROR_CANNOT_FIND_PHONEBOOK_ENTRY;
  1205. else if (rc == ERROR_ALREADY_EXISTS)
  1206. rc = NO_ERROR;
  1207. }
  1208. else
  1209. rc = GetLastError ();
  1210. FreeLibrary (hRasApi32);
  1211. }
  1212. else
  1213. rc = GetLastError ();
  1214. return rc;
  1215. }