Make UEFI boot-platform build again
[haiku.git] / src / bin / network / pppconfig / pppconfig.cpp
blobdfbc14f0058c52458b8c31db11750465eff579d6
1 /*
2 * Copyright 2003-2004, Waldemar Kornewald <wkornew@gmx.net>
3 * Copyright 2006-2017, Haiku, Inc. All rights Reserved.
4 * Distributed under the terms of the MIT License.
6 * Authors:
7 * Alexander von Gluck IV <kallisti5@unixzen.com>
8 */
10 #include <cstdio>
11 #include <String.h>
12 #include <driver_settings.h>
14 #include <PPPInterface.h>
15 #include <PPPManager.h>
18 static const char sVersion[] = "0.12 pre-alpha";
19 static const char sPPPInterfaceModuleName[] = PPP_INTERFACE_MODULE_NAME;
22 static
23 status_t
24 print_help()
26 fprintf(stderr, "Haiku Network Team: pppconfig: sVersion %s\n", sVersion);
27 fprintf(stderr, "With pppconfig you can create and manage PPP connections.\n");
28 fprintf(stderr, "Usage:\n");
29 fprintf(stderr, "pppconfig show | -a\n");
30 fprintf(stderr, "pppconfig init <name>\n");
31 fprintf(stderr, "pppconfig create <name>\n");
32 fprintf(stderr, "pppconfig connect <name|interface|id>\n");
33 fprintf(stderr, "pppconfig disconnect <name|interface|id>\n");
34 fprintf(stderr, "pppconfig delete <name|interface|id>\n");
35 fprintf(stderr, "pppconfig details <name|interface|id>\n");
36 fprintf(stderr, "\t<name> must be an interface description file\n");
38 return -1;
42 static
43 status_t
44 show(ppp_interface_filter filter = PPP_REGISTERED_INTERFACES)
46 PPPManager manager;
47 if (manager.InitCheck() != B_OK) {
48 fprintf(stderr, "Error: Could not load interface manager!\n");
49 return -1;
52 int32 count = 0;
53 ppp_interface_id *interfaces = manager.Interfaces(&count, filter);
55 if (!interfaces || count <= 0) {
56 fprintf(stderr, "Error: Could not get interfaces information!\n");
57 return -1;
60 fprintf(stderr, "Get %" B_PRId32 " ppp interfaces, first is %" B_PRIu32 "!\n",
61 count, interfaces[0]);
63 ppp_interface_info_t info;
64 PPPInterface interface;
66 printf("Listing PPP interfaces:\n");
68 // print out information for each interface
69 for (int32 index = 0; index < count; index++) {
70 interface.SetTo(interfaces[index]);
71 if (interface.InitCheck() == B_OK) {
72 interface.GetInterfaceInfo(&info);
73 printf("\n");
75 // type and unit (if it has one)
76 if (info.info.if_unit >= 0) {
77 printf("Type: Visible\n");
78 printf("\tInterface: ppp%" B_PRId32 "\n", info.info.if_unit);
79 } else
80 printf("Type: Hidden\n");
82 printf("\tName: %s\n", info.info.name);
84 // ID
85 printf("\tID: %" B_PRIu32 "\n", interface.ID());
87 // mode
88 printf("\tMode: ");
89 if (info.info.mode == PPP_CLIENT_MODE)
90 printf("Client\n");
91 else if (info.info.mode == PPP_SERVER_MODE)
92 printf("Server\n");
93 else
94 printf("Unknown\n");
96 // status
97 printf("\tStatus: ");
98 switch(info.info.phase) {
99 case PPP_ESTABLISHED_PHASE:
100 printf("Connected\n");
101 break;
103 case PPP_DOWN_PHASE:
104 printf("Disconnected\n");
105 break;
107 case PPP_TERMINATION_PHASE:
108 printf("Disconnecting\n");
109 break;
111 default:
112 printf("Connecting\n");
117 delete interfaces;
119 return 0;
123 static
124 status_t
125 create(const char *name, bool bringUp = true)
127 PPPManager manager;
128 if (manager.InitCheck() != B_OK) {
129 fprintf(stderr, "Error: Could not load interface manager!\n");
130 return -1;
133 PPPInterface interface(manager.CreateInterfaceWithName(name));
134 if (interface.InitCheck() != B_OK) {
135 fprintf(stderr, "Error: Could not create interface: %s!\n", name);
136 return -1;
139 printf("Created interface with ID: %" B_PRIu32 "\n", interface.ID());
141 ppp_interface_info_t info;
142 interface.GetInterfaceInfo(&info);
144 if (info.info.if_unit >= 0)
145 printf("Interface: ppp%" B_PRId32 "\n", info.info.if_unit);
146 else
147 printf("This interface is hidden! You can delete it by typing:\n"
148 "pppconfig delete %" B_PRIu32 "\n", interface.ID());
150 if (bringUp) {
151 interface.Up();
152 printf("Connecting in background...\n");
155 return 0;
159 static
160 status_t
161 connect(const char *name)
163 if (!name || strlen(name) == 0)
164 return -1;
166 PPPManager manager;
167 if (manager.InitCheck() != B_OK) {
168 fprintf(stderr, "Error: Could not load interface manager!\n");
169 return -1;
172 PPPInterface interface(manager.InterfaceWithName(name));
173 if (interface.InitCheck() != B_OK) {
174 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
175 return -1;
178 if (!interface.Up()) {
179 fprintf(stderr, "Error: Could not connect!\n");
180 return -1;
183 printf("Connecting in background...\n");
185 return 0;
189 static
190 status_t
191 setuser(const char *name, const char* user)
193 if (!name || strlen(name) == 0)
194 return -1;
196 if (!user || strlen(user) == 0)
197 return -1;
199 PPPManager manager;
200 if (manager.InitCheck() != B_OK) {
201 fprintf(stderr, "Error: Could not load interface manager!\n");
202 return -1;
205 PPPInterface interface(manager.InterfaceWithName(name));
206 if (interface.InitCheck() != B_OK) {
207 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
208 return -1;
211 if (!interface.SetUsername(user)) {
212 fprintf(stderr, "Error: Could not SetUsername %s!\n", user);
213 return -1;
216 return 0;
220 static
221 status_t
222 setpass(const char *name, const char* pass)
224 if (!name || strlen(name) == 0)
225 return -1;
227 if (!pass || strlen(pass) == 0)
228 return -1;
230 PPPManager manager;
231 if (manager.InitCheck() != B_OK) {
232 fprintf(stderr, "Error: Could not load interface manager!\n");
233 return -1;
236 PPPInterface interface(manager.InterfaceWithName(name));
237 if (interface.InitCheck() != B_OK) {
238 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
239 return -1;
242 if (!interface.SetPassword(pass)) {
243 fprintf(stderr, "Error: Could not SetUsername %s!\n", pass);
244 return -1;
247 return 0;
251 static
252 status_t
253 setaskbeforeconnect(const char *name, const char* connect)
255 if (!name || strlen(name) == 0)
256 return -1;
258 bool askBeforeConnecting = false;
259 if (connect || !strcmp(connect, "true") || !strcmp(connect, "ask") ||
260 !strcmp(connect, "yes") || !strcmp(connect, "y"))
261 askBeforeConnecting = true;
264 PPPManager manager;
265 if (manager.InitCheck() != B_OK) {
266 fprintf(stderr, "Error: Could not load interface manager!\n");
267 return -1;
270 PPPInterface interface(manager.InterfaceWithName(name));
271 if (interface.InitCheck() != B_OK) {
272 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
273 return -1;
276 if (!interface.SetAskBeforeConnecting(askBeforeConnecting)) {
277 fprintf(stderr, "Error: Could not connect %s!\n", connect);
278 return -1;
281 return 0;
285 static
286 status_t
287 getstatistics(const char *name)
289 if (!name || strlen(name) == 0)
290 return -1;
292 PPPManager manager;
293 if (manager.InitCheck() != B_OK) {
294 fprintf(stderr, "Error: Could not load interface manager!\n");
295 return -1;
298 PPPInterface interface(manager.InterfaceWithName(name));
299 if (interface.InitCheck() != B_OK) {
300 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
301 return -1;
304 ppp_statistics pppStatistics;
305 if (interface.GetStatistics(&pppStatistics) != B_OK) {
306 fprintf(stderr, "Error: Could not getstatistics: %s!\n", name);
307 return -1;
310 return 0;
314 static
315 status_t
316 hassettings(const char *name)
318 if (!name || strlen(name) == 0)
319 return -1;
321 PPPManager manager;
322 if (manager.InitCheck() != B_OK) {
323 fprintf(stderr, "Error: Could not load interface manager!\n");
324 return -1;
327 PPPInterface interface(manager.InterfaceWithName(name));
328 if (interface.InitCheck() != B_OK) {
329 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
330 return -1;
333 driver_settings settings;
334 if (interface.HasSettings(&settings) != B_OK) {
335 fprintf(stderr, "Error: Could not getstatistics: %s!\n", name);
336 return -1;
339 return 0;
343 static
344 status_t
345 enablereports(const char *name)
347 if (!name || strlen(name) == 0)
348 return -1;
350 PPPManager manager;
351 if (manager.InitCheck() != B_OK) {
352 fprintf(stderr, "Error: Could not load interface manager!\n");
353 return -1;
356 PPPInterface interface(manager.InterfaceWithName(name));
357 if (interface.InitCheck() != B_OK) {
358 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
359 return -1;
362 ppp_report_type type = PPP_ALL_REPORTS;
363 thread_id thread = 0;
364 int32 flags = 0;
366 if (interface.EnableReports(type, thread, flags) != true) {
367 fprintf(stderr, "Error: Could not EnableReports: %s!\n", name);
368 return -1;
371 return 0;
375 static
376 status_t
377 disablereports(const char *name)
379 if (!name || strlen(name) == 0)
380 return -1;
382 PPPManager manager;
383 if (manager.InitCheck() != B_OK) {
384 fprintf(stderr, "Error: Could not load interface manager!\n");
385 return -1;
388 PPPInterface interface(manager.InterfaceWithName(name));
389 if (interface.InitCheck() != B_OK) {
390 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
391 return -1;
394 ppp_report_type type = PPP_ALL_REPORTS;
395 thread_id thread = 0;
397 if (interface.DisableReports(type, thread) != true) {
398 fprintf(stderr, "Error: Could not EnableReports: %s!\n", name);
399 return -1;
402 return 0;
406 static
407 status_t
408 controlchild(const char *name)
410 if (!name || strlen(name) == 0)
411 return -1;
413 PPPManager manager;
414 if (manager.InitCheck() != B_OK) {
415 fprintf(stderr, "Error: Could not load interface manager!\n");
416 return -1;
419 PPPInterface interface(manager.InterfaceWithName(name));
420 if (interface.InitCheck() != B_OK) {
421 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
422 return -1;
425 ppp_simple_handler_info_t info;
427 if (interface.ControlChild(&info, 0, PPPC_GET_SIMPLE_HANDLER_INFO) != true) {
428 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name);
429 return -1;
432 printf("LCPExtensionHandler: %s\n", info.info.name);
433 printf("isEnabled: %d\n", info.info.isEnabled);
435 if (interface.ControlChild(&info, 0, PPPC_ENABLE) != true) {
436 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name);
437 return -1;
439 return 0;
443 static
444 status_t
445 controllcpextension(const char *name)
447 if (!name || strlen(name) == 0)
448 return -1;
450 PPPManager manager;
451 if (manager.InitCheck() != B_OK) {
452 fprintf(stderr, "Error: Could not load interface manager!\n");
453 return -1;
456 PPPInterface interface(manager.InterfaceWithName(name));
457 if (interface.InitCheck() != B_OK) {
458 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
459 return -1;
462 ppp_simple_handler_info_t info;
464 if (interface.ControlLCPExtension(&info, 1, PPPC_GET_SIMPLE_HANDLER_INFO) != true) {
465 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name);
466 return -1;
469 printf("LCPExtensionHandler: %s\n", info.info.name);
470 printf("isEnabled: %d\n", info.info.isEnabled);
472 if (interface.ControlLCPExtension(&info, 1, PPPC_ENABLE) != true) {
473 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name);
474 return -1;
476 return 0;
480 static
481 status_t
482 controloptionhandler(const char *name)
484 if (!name || strlen(name) == 0)
485 return -1;
487 PPPManager manager;
488 if (manager.InitCheck() != B_OK) {
489 fprintf(stderr, "Error: Could not load interface manager!\n");
490 return -1;
493 PPPInterface interface(manager.InterfaceWithName(name));
494 if (interface.InitCheck() != B_OK) {
495 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
496 return -1;
499 ppp_simple_handler_info_t info;
501 if (interface.ControlOptionHandler(&info, 0, PPPC_GET_SIMPLE_HANDLER_INFO) != true) {
502 fprintf(stderr, "Error: Could not PPPC_GET_SIMPLE_HANDLER_INFO: %s!\n", name);
503 return -1;
506 printf("protocol: %s\n", info.info.name);
507 printf("isEnabled: %d\n", info.info.isEnabled);
509 if (interface.ControlOptionHandler(&info, 0, PPPC_ENABLE) != true) {
510 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name);
511 return -1;
513 return 0;
517 static
518 status_t
519 controlprotocol(const char *name)
521 if (!name || strlen(name) == 0)
522 return -1;
524 PPPManager manager;
525 if (manager.InitCheck() != B_OK) {
526 fprintf(stderr, "Error: Could not load interface manager!\n");
527 return -1;
530 PPPInterface interface(manager.InterfaceWithName(name));
531 if (interface.InitCheck() != B_OK) {
532 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
533 return -1;
536 ppp_protocol_info_t info;
538 if (interface.ControlProtocol(&info, 0, PPPC_GET_PROTOCOL_INFO) != true) {
539 fprintf(stderr, "Error: Could not PPPC_GET_PROTOCOL_INFO: %s!\n", name);
540 return -1;
543 printf("protocol: %s\n", info.info.name);
544 printf("type: %s\n", info.info.type);
545 printf("activationPhase: %d\n", info.info.activationPhase);
546 printf("addressFamily: %" B_PRId32 "\n", info.info.addressFamily);
547 printf("flags: %" B_PRId32 "\n", info.info.flags);
548 printf("side: %d\n", info.info.side);
549 printf("level: %d\n", info.info.level);
550 printf("connectionPhase: %d\n", info.info.connectionPhase);
551 printf("protocolNumber: %d\n", info.info.protocolNumber);
552 printf("isEnabled: %d\n", info.info.isEnabled);
553 printf("isUpRequested: %d\n", info.info.isUpRequested);
555 if (interface.ControlProtocol(&info, 0, PPPC_ENABLE) != true) {
556 fprintf(stderr, "Error: Could not PPPC_ENABLE: %s!\n", name);
557 return -1;
559 return 0;
563 static
564 status_t
565 controldevice(const char *name)
567 if (!name || strlen(name) == 0)
568 return -1;
570 PPPManager manager;
571 if (manager.InitCheck() != B_OK) {
572 fprintf(stderr, "Error: Could not load interface manager!\n");
573 return -1;
576 PPPInterface interface(manager.InterfaceWithName(name));
577 if (interface.InitCheck() != B_OK) {
578 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
579 return -1;
582 ppp_device_info_t info;
584 if (interface.ControlDevice(&info) != true) {
585 fprintf(stderr, "Error: Could not ControlDevice: %s!\n", name);
586 return -1;
589 printf("name: %s\n", info.info.name);
590 printf("MTU: %" B_PRIu32 "\n", info.info.MTU);
591 printf("inputTransferRate: %" B_PRIu32 "\n", info.info.inputTransferRate);
592 printf("outputTransferRate: %" B_PRIu32 "\n", info.info.outputTransferRate);
593 printf("outputBytesCount: %" B_PRIu32 "\n", info.info.outputBytesCount);
594 printf("isUp: %d\n", info.info.isUp);
596 return 0;
600 static
601 status_t
602 disconnect(const char *name)
604 if (!name || strlen(name) == 0)
605 return -1;
607 PPPManager manager;
608 if (manager.InitCheck() != B_OK) {
609 fprintf(stderr, "Error: Could not load interface manager!\n");
610 return -1;
613 PPPInterface interface(manager.InterfaceWithName(name));
614 if (interface.InitCheck() != B_OK) {
615 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
616 return -1;
619 if (!interface.Down()) {
620 fprintf(stderr, "Error: Could not disconnect!\n");
621 return -1;
624 return 0;
628 static
629 status_t
630 delete_interface(const char *name)
632 if (!name || strlen(name) == 0)
633 return -1;
635 PPPManager manager;
636 if (manager.InitCheck() != B_OK) {
637 fprintf(stderr, "Error: Could not load interface manager!\n");
638 return -1;
641 if (!manager.DeleteInterface(name)) {
642 fprintf(stderr, "Error: Could not delete interface!\n");
643 return -1;
646 return 0;
650 static
651 status_t
652 show_details(const char *name)
654 if (!name || strlen(name) == 0)
655 return -1;
657 PPPManager manager;
658 if (manager.InitCheck() != B_OK) {
659 fprintf(stderr, "Error: Could not load interface manager!\n");
660 return -1;
663 ppp_interface_id ID = manager.InterfaceWithName(name);
664 if (ID <= 0) {
665 fprintf(stderr, "Error: Could not find interface: %s!\n", name);
666 return -1;
669 PPPInterface interface(ID);
670 if (interface.InitCheck() != B_OK) {
671 fprintf(stderr, "Error: Could not find interface ID: %" B_PRIu32 "!\n", ID);
672 return -1;
675 ppp_interface_info_t info;
676 // printf("ppp_interface_info_t addr:%p\n", &info);
677 interface.GetInterfaceInfo(&info);
679 // type and name (if it has one)
680 if (info.info.if_unit >= 0) {
681 printf("Type: Visible\n");
682 printf("Interface: ppp%" B_PRId32 "\n", info.info.if_unit);
683 } else
684 printf("Type: Hidden\n");
686 printf("Name: %s\n", info.info.name);
688 // ID
689 printf("ID: %" B_PRIu32 "\n", interface.ID());
691 // ConnectOnDemand
692 printf("ConnectOnDemand: %s\n", info.info.doesConnectOnDemand ?
693 "Enabled" : "Disabled");
695 // AutoReconnect
696 printf("AutoReconnect: %s\n", info.info.doesAutoReconnect ?
697 "Enabled" : "Disabled");
699 // MRU and interfaceMTU
700 printf("MRU: %" B_PRIu32 "\n", info.info.MRU);
701 printf("Interface MTU: %" B_PRIu32 "\n", info.info.interfaceMTU);
703 // mode
704 printf("Mode: ");
705 if (info.info.mode == PPP_CLIENT_MODE)
706 printf("Client\n");
707 else if (info.info.mode == PPP_SERVER_MODE)
708 printf("Server\n");
709 else
710 printf("Unknown\n");
712 // status
713 printf("\tStatus: ");
714 switch(info.info.phase) {
715 case PPP_ESTABLISHED_PHASE:
716 printf("Connected\n");
717 break;
719 case PPP_DOWN_PHASE:
720 printf("Disconnected\n");
721 break;
723 case PPP_TERMINATION_PHASE:
724 printf("Disconnecting\n");
725 break;
727 default:
728 printf("Connecting\n");
731 return 0;
736 main(int argc, char *argv[])
738 if (argc == 2) {
739 if (!strcmp(argv[1], "show") || !strcmp(argv[1], "-a"))
740 return show(PPP_ALL_INTERFACES);
741 else
742 return print_help();
743 }if (argc == 3) {
744 if (!strcmp(argv[1], "init"))
745 return create(argv[2], false);
746 else if (!strcmp(argv[1], "create"))
747 return create(argv[2], true);
748 else if (!strcmp(argv[1], "connect"))
749 return connect(argv[2]);
750 else if (!strcmp(argv[1], "disconnect"))
751 return disconnect(argv[2]);
752 else if (!strcmp(argv[1], "delete"))
753 return delete_interface(argv[2]);
754 else if (!strcmp(argv[1], "details"))
755 return show_details(argv[2]);
756 else if (!strcmp(argv[1], "getstatistics"))
757 return getstatistics(argv[2]);
758 else if (!strcmp(argv[1], "hassettings"))
759 return hassettings(argv[2]);
760 else if (!strcmp(argv[1], "enablereports"))
761 return enablereports(argv[2]);
762 else if (!strcmp(argv[1], "disablereports"))
763 return disablereports(argv[2]);
764 else if (!strcmp(argv[1], "controldevice"))
765 return controldevice(argv[2]);
766 else if (!strcmp(argv[1], "controlprotocol"))
767 return controlprotocol(argv[2]);
768 else if (!strcmp(argv[1], "controloptionhandler"))
769 return controloptionhandler(argv[2]);
770 else if (!strcmp(argv[1], "controllcpextension"))
771 return controllcpextension(argv[2]);
772 else if (!strcmp(argv[1], "controlchild"))
773 return controlchild(argv[2]);
774 else
775 return print_help();
776 } if (argc == 4) {
777 if (!strcmp(argv[1], "setuser"))
778 return setuser(argv[2], argv[3]);
779 else if (!strcmp(argv[1], "setpass"))
780 return setpass(argv[2], argv[3]);
781 else if (!strcmp(argv[1], "setaskbeforeconnect"))
782 return setaskbeforeconnect(argv[2], argv[3]);
783 else
784 return print_help();
785 } else
786 return print_help();