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.
7 * Alexander von Gluck IV <kallisti5@unixzen.com>
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
;
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");
44 show(ppp_interface_filter filter
= PPP_REGISTERED_INTERFACES
)
47 if (manager
.InitCheck() != B_OK
) {
48 fprintf(stderr
, "Error: Could not load interface manager!\n");
53 ppp_interface_id
*interfaces
= manager
.Interfaces(&count
, filter
);
55 if (!interfaces
|| count
<= 0) {
56 fprintf(stderr
, "Error: Could not get interfaces information!\n");
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
);
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
);
80 printf("Type: Hidden\n");
82 printf("\tName: %s\n", info
.info
.name
);
85 printf("\tID: %" B_PRIu32
"\n", interface
.ID());
89 if (info
.info
.mode
== PPP_CLIENT_MODE
)
91 else if (info
.info
.mode
== PPP_SERVER_MODE
)
98 switch(info
.info
.phase
) {
99 case PPP_ESTABLISHED_PHASE
:
100 printf("Connected\n");
104 printf("Disconnected\n");
107 case PPP_TERMINATION_PHASE
:
108 printf("Disconnecting\n");
112 printf("Connecting\n");
125 create(const char *name
, bool bringUp
= true)
128 if (manager
.InitCheck() != B_OK
) {
129 fprintf(stderr
, "Error: Could not load interface manager!\n");
133 PPPInterface
interface(manager
.CreateInterfaceWithName(name
));
134 if (interface
.InitCheck() != B_OK
) {
135 fprintf(stderr
, "Error: Could not create interface: %s!\n", name
);
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
);
147 printf("This interface is hidden! You can delete it by typing:\n"
148 "pppconfig delete %" B_PRIu32
"\n", interface
.ID());
152 printf("Connecting in background...\n");
161 connect(const char *name
)
163 if (!name
|| strlen(name
) == 0)
167 if (manager
.InitCheck() != B_OK
) {
168 fprintf(stderr
, "Error: Could not load interface manager!\n");
172 PPPInterface
interface(manager
.InterfaceWithName(name
));
173 if (interface
.InitCheck() != B_OK
) {
174 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
178 if (!interface
.Up()) {
179 fprintf(stderr
, "Error: Could not connect!\n");
183 printf("Connecting in background...\n");
191 setuser(const char *name
, const char* user
)
193 if (!name
|| strlen(name
) == 0)
196 if (!user
|| strlen(user
) == 0)
200 if (manager
.InitCheck() != B_OK
) {
201 fprintf(stderr
, "Error: Could not load interface manager!\n");
205 PPPInterface
interface(manager
.InterfaceWithName(name
));
206 if (interface
.InitCheck() != B_OK
) {
207 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
211 if (!interface
.SetUsername(user
)) {
212 fprintf(stderr
, "Error: Could not SetUsername %s!\n", user
);
222 setpass(const char *name
, const char* pass
)
224 if (!name
|| strlen(name
) == 0)
227 if (!pass
|| strlen(pass
) == 0)
231 if (manager
.InitCheck() != B_OK
) {
232 fprintf(stderr
, "Error: Could not load interface manager!\n");
236 PPPInterface
interface(manager
.InterfaceWithName(name
));
237 if (interface
.InitCheck() != B_OK
) {
238 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
242 if (!interface
.SetPassword(pass
)) {
243 fprintf(stderr
, "Error: Could not SetUsername %s!\n", pass
);
253 setaskbeforeconnect(const char *name
, const char* connect
)
255 if (!name
|| strlen(name
) == 0)
258 bool askBeforeConnecting
= false;
259 if (connect
|| !strcmp(connect
, "true") || !strcmp(connect
, "ask") ||
260 !strcmp(connect
, "yes") || !strcmp(connect
, "y"))
261 askBeforeConnecting
= true;
265 if (manager
.InitCheck() != B_OK
) {
266 fprintf(stderr
, "Error: Could not load interface manager!\n");
270 PPPInterface
interface(manager
.InterfaceWithName(name
));
271 if (interface
.InitCheck() != B_OK
) {
272 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
276 if (!interface
.SetAskBeforeConnecting(askBeforeConnecting
)) {
277 fprintf(stderr
, "Error: Could not connect %s!\n", connect
);
287 getstatistics(const char *name
)
289 if (!name
|| strlen(name
) == 0)
293 if (manager
.InitCheck() != B_OK
) {
294 fprintf(stderr
, "Error: Could not load interface manager!\n");
298 PPPInterface
interface(manager
.InterfaceWithName(name
));
299 if (interface
.InitCheck() != B_OK
) {
300 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
304 ppp_statistics pppStatistics
;
305 if (interface
.GetStatistics(&pppStatistics
) != B_OK
) {
306 fprintf(stderr
, "Error: Could not getstatistics: %s!\n", name
);
316 hassettings(const char *name
)
318 if (!name
|| strlen(name
) == 0)
322 if (manager
.InitCheck() != B_OK
) {
323 fprintf(stderr
, "Error: Could not load interface manager!\n");
327 PPPInterface
interface(manager
.InterfaceWithName(name
));
328 if (interface
.InitCheck() != B_OK
) {
329 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
333 driver_settings settings
;
334 if (interface
.HasSettings(&settings
) != B_OK
) {
335 fprintf(stderr
, "Error: Could not getstatistics: %s!\n", name
);
345 enablereports(const char *name
)
347 if (!name
|| strlen(name
) == 0)
351 if (manager
.InitCheck() != B_OK
) {
352 fprintf(stderr
, "Error: Could not load interface manager!\n");
356 PPPInterface
interface(manager
.InterfaceWithName(name
));
357 if (interface
.InitCheck() != B_OK
) {
358 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
362 ppp_report_type type
= PPP_ALL_REPORTS
;
363 thread_id thread
= 0;
366 if (interface
.EnableReports(type
, thread
, flags
) != true) {
367 fprintf(stderr
, "Error: Could not EnableReports: %s!\n", name
);
377 disablereports(const char *name
)
379 if (!name
|| strlen(name
) == 0)
383 if (manager
.InitCheck() != B_OK
) {
384 fprintf(stderr
, "Error: Could not load interface manager!\n");
388 PPPInterface
interface(manager
.InterfaceWithName(name
));
389 if (interface
.InitCheck() != B_OK
) {
390 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
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
);
408 controlchild(const char *name
)
410 if (!name
|| strlen(name
) == 0)
414 if (manager
.InitCheck() != B_OK
) {
415 fprintf(stderr
, "Error: Could not load interface manager!\n");
419 PPPInterface
interface(manager
.InterfaceWithName(name
));
420 if (interface
.InitCheck() != B_OK
) {
421 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
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
);
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
);
445 controllcpextension(const char *name
)
447 if (!name
|| strlen(name
) == 0)
451 if (manager
.InitCheck() != B_OK
) {
452 fprintf(stderr
, "Error: Could not load interface manager!\n");
456 PPPInterface
interface(manager
.InterfaceWithName(name
));
457 if (interface
.InitCheck() != B_OK
) {
458 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
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
);
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
);
482 controloptionhandler(const char *name
)
484 if (!name
|| strlen(name
) == 0)
488 if (manager
.InitCheck() != B_OK
) {
489 fprintf(stderr
, "Error: Could not load interface manager!\n");
493 PPPInterface
interface(manager
.InterfaceWithName(name
));
494 if (interface
.InitCheck() != B_OK
) {
495 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
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
);
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
);
519 controlprotocol(const char *name
)
521 if (!name
|| strlen(name
) == 0)
525 if (manager
.InitCheck() != B_OK
) {
526 fprintf(stderr
, "Error: Could not load interface manager!\n");
530 PPPInterface
interface(manager
.InterfaceWithName(name
));
531 if (interface
.InitCheck() != B_OK
) {
532 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
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
);
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
);
565 controldevice(const char *name
)
567 if (!name
|| strlen(name
) == 0)
571 if (manager
.InitCheck() != B_OK
) {
572 fprintf(stderr
, "Error: Could not load interface manager!\n");
576 PPPInterface
interface(manager
.InterfaceWithName(name
));
577 if (interface
.InitCheck() != B_OK
) {
578 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
582 ppp_device_info_t info
;
584 if (interface
.ControlDevice(&info
) != true) {
585 fprintf(stderr
, "Error: Could not ControlDevice: %s!\n", name
);
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
);
602 disconnect(const char *name
)
604 if (!name
|| strlen(name
) == 0)
608 if (manager
.InitCheck() != B_OK
) {
609 fprintf(stderr
, "Error: Could not load interface manager!\n");
613 PPPInterface
interface(manager
.InterfaceWithName(name
));
614 if (interface
.InitCheck() != B_OK
) {
615 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
619 if (!interface
.Down()) {
620 fprintf(stderr
, "Error: Could not disconnect!\n");
630 delete_interface(const char *name
)
632 if (!name
|| strlen(name
) == 0)
636 if (manager
.InitCheck() != B_OK
) {
637 fprintf(stderr
, "Error: Could not load interface manager!\n");
641 if (!manager
.DeleteInterface(name
)) {
642 fprintf(stderr
, "Error: Could not delete interface!\n");
652 show_details(const char *name
)
654 if (!name
|| strlen(name
) == 0)
658 if (manager
.InitCheck() != B_OK
) {
659 fprintf(stderr
, "Error: Could not load interface manager!\n");
663 ppp_interface_id ID
= manager
.InterfaceWithName(name
);
665 fprintf(stderr
, "Error: Could not find interface: %s!\n", name
);
669 PPPInterface
interface(ID
);
670 if (interface
.InitCheck() != B_OK
) {
671 fprintf(stderr
, "Error: Could not find interface ID: %" B_PRIu32
"!\n", ID
);
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
);
684 printf("Type: Hidden\n");
686 printf("Name: %s\n", info
.info
.name
);
689 printf("ID: %" B_PRIu32
"\n", interface
.ID());
692 printf("ConnectOnDemand: %s\n", info
.info
.doesConnectOnDemand
?
693 "Enabled" : "Disabled");
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
);
705 if (info
.info
.mode
== PPP_CLIENT_MODE
)
707 else if (info
.info
.mode
== PPP_SERVER_MODE
)
713 printf("\tStatus: ");
714 switch(info
.info
.phase
) {
715 case PPP_ESTABLISHED_PHASE
:
716 printf("Connected\n");
720 printf("Disconnected\n");
723 case PPP_TERMINATION_PHASE
:
724 printf("Disconnecting\n");
728 printf("Connecting\n");
736 main(int argc
, char *argv
[])
739 if (!strcmp(argv
[1], "show") || !strcmp(argv
[1], "-a"))
740 return show(PPP_ALL_INTERFACES
);
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]);
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]);