1 /* 2 * Copyright 2003-2004, Waldemar Kornewald <wkornew@gmx.net> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <cstdio> 7 #include <String.h> 8 #include <driver_settings.h> 9 10 #include <PPPInterface.h> 11 #include <PPPManager.h> 12 13 14 static const char sVersion[] = "0.12 pre-alpha"; 15 static const char sPPPInterfaceModuleName[] = PPP_INTERFACE_MODULE_NAME; 16 17 18 static 19 status_t 20 print_help() 21 { 22 fprintf(stderr, "Haiku Network Team: pppconfig: sVersion %s\n", sVersion); 23 fprintf(stderr, "With pppconfig you can create and manage PPP connections.\n"); 24 fprintf(stderr, "Usage:\n"); 25 fprintf(stderr, "pppconfig show | -a\n"); 26 fprintf(stderr, "pppconfig init <name>\n"); 27 fprintf(stderr, "pppconfig create <name>\n"); 28 fprintf(stderr, "pppconfig connect <name|interface|id>\n"); 29 fprintf(stderr, "pppconfig disconnect <name|interface|id>\n"); 30 fprintf(stderr, "pppconfig delete <name|interface|id>\n"); 31 fprintf(stderr, "pppconfig details <name|interface|id>\n"); 32 fprintf(stderr, "\t<name> must be an interface description file\n"); 33 34 return -1; 35 } 36 37 38 static 39 status_t 40 show(ppp_interface_filter filter = PPP_REGISTERED_INTERFACES) 41 { 42 PPPManager manager; 43 if (manager.InitCheck() != B_OK) { 44 fprintf(stderr, "Error: Could not load interface manager!\n"); 45 return -1; 46 } 47 48 int32 count = 0; 49 ppp_interface_id *interfaces = manager.Interfaces(&count, filter); 50 51 if (!interfaces || count <= 0) { 52 fprintf(stderr, "Error: Could not get interfaces information!\n"); 53 return -1; 54 } 55 56 fprintf(stderr, "Get %ld ppp interfaces, first is %ld!\n", count, interfaces[0]); 57 58 ppp_interface_info_t info; 59 PPPInterface interface; 60 61 printf("Listing PPP interfaces:\n"); 62 63 // print out information for each interface 64 for (int32 index = 0; index < count; index++) { 65 interface.SetTo(interfaces[index]); 66 if (interface.InitCheck() == B_OK) { 67 interface.GetInterfaceInfo(&info); 68 printf("\n"); 69 70 // type and unit (if it has one) 71 if (info.info.if_unit >= 0) { 72 printf("Type: Visible\n"); 73 printf("\tInterface: ppp%ld\n", info.info.if_unit); 74 } else 75 printf("Type: Hidden\n"); 76 77 printf("\tName: %s\n", info.info.name); 78 79 // ID 80 printf("\tID: %ld\n", interface.ID()); 81 82 // mode 83 printf("\tMode: "); 84 if (info.info.mode == PPP_CLIENT_MODE) 85 printf("Client\n"); 86 else if (info.info.mode == PPP_SERVER_MODE) 87 printf("Server\n"); 88 else 89 printf("Unknown\n"); 90 91 // status 92 printf("\tStatus: "); 93 switch(info.info.phase) { 94 case PPP_ESTABLISHED_PHASE: 95 printf("Connected\n"); 96 break; 97 98 case PPP_DOWN_PHASE: 99 printf("Disconnected\n"); 100 break; 101 102 case PPP_TERMINATION_PHASE: 103 printf("Disconnecting\n"); 104 break; 105 106 default: 107 printf("Connecting\n"); 108 } 109 } 110 } 111 112 delete interfaces; 113 114 return 0; 115 } 116 117 118 static 119 status_t 120 create(const char *name, bool bringUp = true) 121 { 122 PPPManager manager; 123 if (manager.InitCheck() != B_OK) { 124 fprintf(stderr, "Error: Could not load interface manager!\n"); 125 return -1; 126 } 127 128 PPPInterface interface(manager.CreateInterfaceWithName(name)); 129 if (interface.InitCheck() != B_OK) { 130 fprintf(stderr, "Error: Could not create interface: %s!\n", name); 131 return -1; 132 } 133 134 printf("Created interface with ID: %ld\n", interface.ID()); 135 136 ppp_interface_info_t info; 137 interface.GetInterfaceInfo(&info); 138 139 if (info.info.if_unit >= 0) 140 printf("Interface: ppp%ld\n", info.info.if_unit); 141 else 142 printf("This interface is hidden! You can delete it by typing:\n" 143 "pppconfig delete %ld\n", interface.ID()); 144 145 if (bringUp) { 146 interface.Up(); 147 printf("Connecting in background...\n"); 148 } 149 150 return 0; 151 } 152 153 154 static 155 status_t 156 connect(const char *name) 157 { 158 if (!name || strlen(name) == 0) 159 return -1; 160 161 PPPManager manager; 162 if (manager.InitCheck() != B_OK) { 163 fprintf(stderr, "Error: Could not load interface manager!\n"); 164 return -1; 165 } 166 167 PPPInterface interface(manager.InterfaceWithName(name)); 168 if (interface.InitCheck() != B_OK) { 169 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 170 return -1; 171 } 172 173 if (!interface.Up()) { 174 fprintf(stderr, "Error: Could not connect!\n"); 175 return -1; 176 } 177 178 printf("Connecting in background...\n"); 179 180 return 0; 181 } 182 183 184 static 185 status_t 186 setuser(const char *name, const char* user) 187 { 188 if (!name || strlen(name) == 0) 189 return -1; 190 191 if (!user || strlen(user) == 0) 192 return -1; 193 194 PPPManager manager; 195 if (manager.InitCheck() != B_OK) { 196 fprintf(stderr, "Error: Could not load interface manager!\n"); 197 return -1; 198 } 199 200 PPPInterface interface(manager.InterfaceWithName(name)); 201 if (interface.InitCheck() != B_OK) { 202 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 203 return -1; 204 } 205 206 if (!interface.SetUsername(user)) { 207 fprintf(stderr, "Error: Could not SetUsername %s!\n", user); 208 return -1; 209 } 210 211 return 0; 212 } 213 214 215 static 216 status_t 217 setpass(const char *name, const char* pass) 218 { 219 if (!name || strlen(name) == 0) 220 return -1; 221 222 if (!pass || strlen(pass) == 0) 223 return -1; 224 225 PPPManager manager; 226 if (manager.InitCheck() != B_OK) { 227 fprintf(stderr, "Error: Could not load interface manager!\n"); 228 return -1; 229 } 230 231 PPPInterface interface(manager.InterfaceWithName(name)); 232 if (interface.InitCheck() != B_OK) { 233 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 234 return -1; 235 } 236 237 if (!interface.SetPassword(pass)) { 238 fprintf(stderr, "Error: Could not SetUsername %s!\n", pass); 239 return -1; 240 } 241 242 return 0; 243 } 244 245 246 static 247 status_t 248 setaskbeforeconnect(const char *name, const char* connect) 249 { 250 if (!name || strlen(name) == 0) 251 return -1; 252 253 bool askBeforeConnecting = false; 254 if (connect || !strcmp(connect, "true") || !strcmp(connect, "ask") || 255 !strcmp(connect, "yes") || !strcmp(connect, "y")) 256 askBeforeConnecting = true; 257 258 259 PPPManager manager; 260 if (manager.InitCheck() != B_OK) { 261 fprintf(stderr, "Error: Could not load interface manager!\n"); 262 return -1; 263 } 264 265 PPPInterface interface(manager.InterfaceWithName(name)); 266 if (interface.InitCheck() != B_OK) { 267 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 268 return -1; 269 } 270 271 if (!interface.SetAskBeforeConnecting(askBeforeConnecting)) { 272 fprintf(stderr, "Error: Could not connect %s!\n", connect); 273 return -1; 274 } 275 276 return 0; 277 } 278 279 280 static 281 status_t 282 getstatistics(const char *name) 283 { 284 if (!name || strlen(name) == 0) 285 return -1; 286 287 PPPManager manager; 288 if (manager.InitCheck() != B_OK) { 289 fprintf(stderr, "Error: Could not load interface manager!\n"); 290 return -1; 291 } 292 293 PPPInterface interface(manager.InterfaceWithName(name)); 294 if (interface.InitCheck() != B_OK) { 295 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 296 return -1; 297 } 298 299 ppp_statistics pppStatistics; 300 if (interface.GetStatistics(&pppStatistics) != B_OK) { 301 fprintf(stderr, "Error: Could not getstatistics: %s!\n", name); 302 return -1; 303 } 304 305 return 0; 306 } 307 308 309 static 310 status_t 311 hassettings(const char *name) 312 { 313 if (!name || strlen(name) == 0) 314 return -1; 315 316 PPPManager manager; 317 if (manager.InitCheck() != B_OK) { 318 fprintf(stderr, "Error: Could not load interface manager!\n"); 319 return -1; 320 } 321 322 PPPInterface interface(manager.InterfaceWithName(name)); 323 if (interface.InitCheck() != B_OK) { 324 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 325 return -1; 326 } 327 328 driver_settings settings; 329 if (interface.HasSettings(&settings) != B_OK) { 330 fprintf(stderr, "Error: Could not getstatistics: %s!\n", name); 331 return -1; 332 } 333 334 return 0; 335 } 336 337 338 static 339 status_t 340 enablereports(const char *name) 341 { 342 if (!name || strlen(name) == 0) 343 return -1; 344 345 PPPManager manager; 346 if (manager.InitCheck() != B_OK) { 347 fprintf(stderr, "Error: Could not load interface manager!\n"); 348 return -1; 349 } 350 351 PPPInterface interface(manager.InterfaceWithName(name)); 352 if (interface.InitCheck() != B_OK) { 353 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 354 return -1; 355 } 356 357 ppp_report_type type = PPP_ALL_REPORTS; 358 thread_id thread = 0; 359 int32 flags = 0; 360 361 if (interface.EnableReports(type, thread, flags) != true) { 362 fprintf(stderr, "Error: Could not EnableReports: %s!\n", name); 363 return -1; 364 } 365 366 return 0; 367 } 368 369 370 static 371 status_t 372 disablereports(const char *name) 373 { 374 if (!name || strlen(name) == 0) 375 return -1; 376 377 PPPManager manager; 378 if (manager.InitCheck() != B_OK) { 379 fprintf(stderr, "Error: Could not load interface manager!\n"); 380 return -1; 381 } 382 383 PPPInterface interface(manager.InterfaceWithName(name)); 384 if (interface.InitCheck() != B_OK) { 385 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 386 return -1; 387 } 388 389 ppp_report_type type = PPP_ALL_REPORTS; 390 thread_id thread = 0; 391 392 if (interface.DisableReports(type, thread) != true) { 393 fprintf(stderr, "Error: Could not EnableReports: %s!\n", name); 394 return -1; 395 } 396 397 return 0; 398 } 399 400 401 static 402 status_t 403 controlchild(const char *name) 404 { 405 if (!name || strlen(name) == 0) 406 return -1; 407 408 PPPManager manager; 409 if (manager.InitCheck() != B_OK) { 410 fprintf(stderr, "Error: Could not load interface manager!\n"); 411 return -1; 412 } 413 414 PPPInterface interface(manager.InterfaceWithName(name)); 415 if (interface.InitCheck() != B_OK) { 416 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 417 return -1; 418 } 419 420 ppp_simple_handler_info_t info; 421 422 if (interface.ControlChild(&info, 0, PPPC_GET_SIMPLE_HANDLER_INFO) != true) { 423 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name); 424 return -1; 425 } 426 427 printf("LCPExtensionHandler: %s\n", info.info.name); 428 printf("isEnabled: %d\n", info.info.isEnabled); 429 430 if (interface.ControlChild(&info, 0, PPPC_ENABLE) != true) { 431 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name); 432 return -1; 433 } 434 return 0; 435 } 436 437 438 static 439 status_t 440 controllcpextension(const char *name) 441 { 442 if (!name || strlen(name) == 0) 443 return -1; 444 445 PPPManager manager; 446 if (manager.InitCheck() != B_OK) { 447 fprintf(stderr, "Error: Could not load interface manager!\n"); 448 return -1; 449 } 450 451 PPPInterface interface(manager.InterfaceWithName(name)); 452 if (interface.InitCheck() != B_OK) { 453 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 454 return -1; 455 } 456 457 ppp_simple_handler_info_t info; 458 459 if (interface.ControlLCPExtension(&info, 1, PPPC_GET_SIMPLE_HANDLER_INFO) != true) { 460 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name); 461 return -1; 462 } 463 464 printf("LCPExtensionHandler: %s\n", info.info.name); 465 printf("isEnabled: %d\n", info.info.isEnabled); 466 467 if (interface.ControlLCPExtension(&info, 1, PPPC_ENABLE) != true) { 468 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name); 469 return -1; 470 } 471 return 0; 472 } 473 474 475 static 476 status_t 477 controloptionhandler(const char *name) 478 { 479 if (!name || strlen(name) == 0) 480 return -1; 481 482 PPPManager manager; 483 if (manager.InitCheck() != B_OK) { 484 fprintf(stderr, "Error: Could not load interface manager!\n"); 485 return -1; 486 } 487 488 PPPInterface interface(manager.InterfaceWithName(name)); 489 if (interface.InitCheck() != B_OK) { 490 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 491 return -1; 492 } 493 494 ppp_simple_handler_info_t info; 495 496 if (interface.ControlOptionHandler(&info, 0, PPPC_GET_SIMPLE_HANDLER_INFO) != true) { 497 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name); 498 return -1; 499 } 500 501 printf("protocol: %s\n", info.info.name); 502 printf("isEnabled: %d\n", info.info.isEnabled); 503 504 if (interface.ControlOptionHandler(&info, 0, PPPC_ENABLE) != true) { 505 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name); 506 return -1; 507 } 508 return 0; 509 } 510 511 512 static 513 status_t 514 controlprotocol(const char *name) 515 { 516 if (!name || strlen(name) == 0) 517 return -1; 518 519 PPPManager manager; 520 if (manager.InitCheck() != B_OK) { 521 fprintf(stderr, "Error: Could not load interface manager!\n"); 522 return -1; 523 } 524 525 PPPInterface interface(manager.InterfaceWithName(name)); 526 if (interface.InitCheck() != B_OK) { 527 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 528 return -1; 529 } 530 531 ppp_protocol_info_t info; 532 533 if (interface.ControlProtocol(&info, 0, PPPC_GET_PROTOCOL_INFO) != true) { 534 fprintf(stderr, "Error: Could not PPPC_GET_PROTOCOL_INFO: %s!\n", name); 535 return -1; 536 } 537 538 printf("protocol: %s\n", info.info.name); 539 printf("type: %s\n", info.info.type); 540 printf("activationPhase: %d\n", info.info.activationPhase); 541 printf("addressFamily: %ld\n", info.info.addressFamily); 542 printf("flags: %ld\n", info.info.flags); 543 printf("side: %d\n", info.info.side); 544 printf("level: %d\n", info.info.level); 545 printf("connectionPhase: %d\n", info.info.connectionPhase); 546 printf("protocolNumber: %d\n", info.info.protocolNumber); 547 printf("isEnabled: %d\n", info.info.isEnabled); 548 printf("isUpRequested: %d\n", info.info.isUpRequested); 549 550 if (interface.ControlProtocol(&info, 0, PPPC_ENABLE) != true) { 551 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name); 552 return -1; 553 } 554 return 0; 555 } 556 557 558 static 559 status_t 560 controldevice(const char *name) 561 { 562 if (!name || strlen(name) == 0) 563 return -1; 564 565 PPPManager manager; 566 if (manager.InitCheck() != B_OK) { 567 fprintf(stderr, "Error: Could not load interface manager!\n"); 568 return -1; 569 } 570 571 PPPInterface interface(manager.InterfaceWithName(name)); 572 if (interface.InitCheck() != B_OK) { 573 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 574 return -1; 575 } 576 577 ppp_device_info_t info; 578 579 if (interface.ControlDevice(&info) != true) { 580 fprintf(stderr, "Error: Could not ControlDevice: %s!\n", name); 581 return -1; 582 } 583 584 printf("name: %s\n", info.info.name); 585 printf("MTU: %ld\n", info.info.MTU); 586 printf("inputTransferRate: %ld\n", info.info.inputTransferRate); 587 printf("outputTransferRate: %ld\n", info.info.outputTransferRate); 588 printf("outputBytesCount: %ld\n", info.info.outputBytesCount); 589 printf("isUp: %d\n", info.info.isUp); 590 591 return 0; 592 } 593 594 595 static 596 status_t 597 disconnect(const char *name) 598 { 599 if (!name || strlen(name) == 0) 600 return -1; 601 602 PPPManager manager; 603 if (manager.InitCheck() != B_OK) { 604 fprintf(stderr, "Error: Could not load interface manager!\n"); 605 return -1; 606 } 607 608 PPPInterface interface(manager.InterfaceWithName(name)); 609 if (interface.InitCheck() != B_OK) { 610 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 611 return -1; 612 } 613 614 if (!interface.Down()) { 615 fprintf(stderr, "Error: Could not disconnect!\n"); 616 return -1; 617 } 618 619 return 0; 620 } 621 622 623 static 624 status_t 625 delete_interface(const char *name) 626 { 627 if (!name || strlen(name) == 0) 628 return -1; 629 630 PPPManager manager; 631 if (manager.InitCheck() != B_OK) { 632 fprintf(stderr, "Error: Could not load interface manager!\n"); 633 return -1; 634 } 635 636 if (!manager.DeleteInterface(name)) { 637 fprintf(stderr, "Error: Could not delete interface!\n"); 638 return -1; 639 } 640 641 return 0; 642 } 643 644 645 static 646 status_t 647 show_details(const char *name) 648 { 649 if (!name || strlen(name) == 0) 650 return -1; 651 652 PPPManager manager; 653 if (manager.InitCheck() != B_OK) { 654 fprintf(stderr, "Error: Could not load interface manager!\n"); 655 return -1; 656 } 657 658 ppp_interface_id ID = manager.InterfaceWithName(name); 659 if (ID <= 0) { 660 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 661 return -1; 662 } 663 664 PPPInterface interface(ID); 665 if (interface.InitCheck() != B_OK) { 666 fprintf(stderr, "Error: Could not find interface ID: %ld!\n", ID); 667 return -1; 668 } 669 670 ppp_interface_info_t info; 671 // printf("ppp_interface_info_t addr:%p\n", &info); 672 interface.GetInterfaceInfo(&info); 673 674 // type and name (if it has one) 675 if (info.info.if_unit >= 0) { 676 printf("Type: Visible\n"); 677 printf("Interface: ppp%ld\n", info.info.if_unit); 678 } else 679 printf("Type: Hidden\n"); 680 681 printf("Name: %s\n", info.info.name); 682 683 // ID 684 printf("ID: %ld\n", interface.ID()); 685 686 // ConnectOnDemand 687 printf("ConnectOnDemand: %s\n", info.info.doesConnectOnDemand ? 688 "Enabled" : "Disabled"); 689 690 // AutoReconnect 691 printf("AutoReconnect: %s\n", info.info.doesAutoReconnect ? 692 "Enabled" : "Disabled"); 693 694 // MRU and interfaceMTU 695 printf("MRU: %ld\n", info.info.MRU); 696 printf("Interface MTU: %ld\n", info.info.interfaceMTU); 697 698 // mode 699 printf("Mode: "); 700 if (info.info.mode == PPP_CLIENT_MODE) 701 printf("Client\n"); 702 else if (info.info.mode == PPP_SERVER_MODE) 703 printf("Server\n"); 704 else 705 printf("Unknown\n"); 706 707 // status 708 printf("\tStatus: "); 709 switch(info.info.phase) { 710 case PPP_ESTABLISHED_PHASE: 711 printf("Connected\n"); 712 break; 713 714 case PPP_DOWN_PHASE: 715 printf("Disconnected\n"); 716 break; 717 718 case PPP_TERMINATION_PHASE: 719 printf("Disconnecting\n"); 720 break; 721 722 default: 723 printf("Connecting\n"); 724 } 725 726 return 0; 727 } 728 729 730 int 731 main(int argc, char *argv[]) 732 { 733 if (argc == 2) { 734 if (!strcmp(argv[1], "show") || !strcmp(argv[1], "-a")) 735 return show(PPP_ALL_INTERFACES); 736 else 737 return print_help(); 738 }if (argc == 3) { 739 if (!strcmp(argv[1], "init")) 740 return create(argv[2], false); 741 else if (!strcmp(argv[1], "create")) 742 return create(argv[2], true); 743 else if (!strcmp(argv[1], "connect")) 744 return connect(argv[2]); 745 else if (!strcmp(argv[1], "disconnect")) 746 return disconnect(argv[2]); 747 else if (!strcmp(argv[1], "delete")) 748 return delete_interface(argv[2]); 749 else if (!strcmp(argv[1], "details")) 750 return show_details(argv[2]); 751 else if (!strcmp(argv[1], "getstatistics")) 752 return getstatistics(argv[2]); 753 else if (!strcmp(argv[1], "hassettings")) 754 return hassettings(argv[2]); 755 else if (!strcmp(argv[1], "enablereports")) 756 return enablereports(argv[2]); 757 else if (!strcmp(argv[1], "disablereports")) 758 return disablereports(argv[2]); 759 else if (!strcmp(argv[1], "controldevice")) 760 return controldevice(argv[2]); 761 else if (!strcmp(argv[1], "controlprotocol")) 762 return controlprotocol(argv[2]); 763 else if (!strcmp(argv[1], "controloptionhandler")) 764 return controloptionhandler(argv[2]); 765 else if (!strcmp(argv[1], "controllcpextension")) 766 return controllcpextension(argv[2]); 767 else if (!strcmp(argv[1], "controlchild")) 768 return controlchild(argv[2]); 769 else 770 return print_help(); 771 } if (argc == 4) { 772 if (!strcmp(argv[1], "setuser")) 773 return setuser(argv[2], argv[3]); 774 else if (!strcmp(argv[1], "setpass")) 775 return setpass(argv[2], argv[3]); 776 else if (!strcmp(argv[1], "setaskbeforeconnect")) 777 return setaskbeforeconnect(argv[2], argv[3]); 778 else 779 return print_help(); 780 } else 781 return print_help(); 782 } 783