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; 49 ppp_interface_id *interfaces = manager.Interfaces(&count, filter); 50 51 if(!interfaces) { 52 fprintf(stderr, "Error: Could not get interfaces information!\n"); 53 return -1; 54 } 55 56 ppp_interface_info_t info; 57 PPPInterface interface; 58 59 printf("Listing PPP interfaces:\n"); 60 61 // print out information for each interface 62 for(int32 index = 0; index < count; index++) { 63 interface.SetTo(interfaces[index]); 64 if(interface.InitCheck() == B_OK) { 65 interface.GetInterfaceInfo(&info); 66 printf("\n"); 67 68 // type and unit (if it has one) 69 if(info.info.if_unit >= 0) { 70 printf("Type: Visible\n"); 71 printf("\tInterface: ppp%ld\n", info.info.if_unit); 72 } else 73 printf("Type: Hidden\n"); 74 75 printf("\tName: %s\n", info.info.name); 76 77 // ID 78 printf("\tID: %ld\n", interface.ID()); 79 80 // mode 81 printf("\tMode: "); 82 if(info.info.mode == PPP_CLIENT_MODE) 83 printf("Client\n"); 84 else if(info.info.mode == PPP_SERVER_MODE) 85 printf("Server\n"); 86 else 87 printf("Unknown\n"); 88 89 // status 90 printf("\tStatus: "); 91 switch(info.info.phase) { 92 case PPP_ESTABLISHED_PHASE: 93 printf("Connected\n"); 94 break; 95 96 case PPP_DOWN_PHASE: 97 printf("Disconnected\n"); 98 break; 99 100 case PPP_TERMINATION_PHASE: 101 printf("Disconnecting\n"); 102 break; 103 104 default: 105 printf("Connecting\n"); 106 } 107 } 108 } 109 110 delete interfaces; 111 112 return 0; 113 } 114 115 116 static 117 status_t 118 create(const char *name, bool bringUp = true) 119 { 120 PPPManager manager; 121 if(manager.InitCheck() != B_OK) { 122 fprintf(stderr, "Error: Could not load interface manager!\n"); 123 return -1; 124 } 125 126 PPPInterface interface(manager.CreateInterfaceWithName(name)); 127 if(interface.InitCheck() != B_OK) { 128 fprintf(stderr, "Error: Could not create interface: %s!\n", name); 129 return -1; 130 } 131 132 printf("Created interface with ID: %ld\n", interface.ID()); 133 134 ppp_interface_info_t info; 135 interface.GetInterfaceInfo(&info); 136 137 if(info.info.if_unit >= 0) 138 printf("Interface: ppp%ld\n", info.info.if_unit); 139 else 140 printf("This interface is hidden! You can delete it by typing:\n" 141 "pppconfig delete %ld\n", interface.ID()); 142 143 if(bringUp) { 144 interface.Up(); 145 printf("Connecting in background...\n"); 146 } 147 148 return 0; 149 } 150 151 152 static 153 status_t 154 connect(const char *name) 155 { 156 if(!name || strlen(name) == 0) 157 return -1; 158 159 PPPManager manager; 160 if(manager.InitCheck() != B_OK) { 161 fprintf(stderr, "Error: Could not load interface manager!\n"); 162 return -1; 163 } 164 165 PPPInterface interface(manager.InterfaceWithName(name)); 166 if(interface.InitCheck() != B_OK) { 167 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 168 return -1; 169 } 170 171 if(!interface.Up()) { 172 fprintf(stderr, "Error: Could not connect!\n"); 173 return -1; 174 } 175 176 printf("Connecting in background...\n"); 177 178 return 0; 179 } 180 181 182 static 183 status_t 184 disconnect(const char *name) 185 { 186 if(!name || strlen(name) == 0) 187 return -1; 188 189 PPPManager manager; 190 if(manager.InitCheck() != B_OK) { 191 fprintf(stderr, "Error: Could not load interface manager!\n"); 192 return -1; 193 } 194 195 PPPInterface interface(manager.InterfaceWithName(name)); 196 if(interface.InitCheck() != B_OK) { 197 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 198 return -1; 199 } 200 201 if(!interface.Down()) { 202 fprintf(stderr, "Error: Could not disconnect!\n"); 203 return -1; 204 } 205 206 return 0; 207 } 208 209 210 static 211 status_t 212 delete_interface(const char *name) 213 { 214 if(!name || strlen(name) == 0) 215 return -1; 216 217 PPPManager manager; 218 if(manager.InitCheck() != B_OK) { 219 fprintf(stderr, "Error: Could not load interface manager!\n"); 220 return -1; 221 } 222 223 if(!manager.DeleteInterface(manager.InterfaceWithName(name))) { 224 fprintf(stderr, "Error: Could not delete interface!\n"); 225 return -1; 226 } 227 228 return 0; 229 } 230 231 232 static 233 status_t 234 show_details(const char *name) 235 { 236 if(!name || strlen(name) == 0) 237 return -1; 238 239 PPPManager manager; 240 if(manager.InitCheck() != B_OK) { 241 fprintf(stderr, "Error: Could not load interface manager!\n"); 242 return -1; 243 } 244 245 PPPInterface interface(manager.InterfaceWithName(name)); 246 if(interface.InitCheck() != B_OK) { 247 fprintf(stderr, "Error: Could not find interface: %s!\n", name); 248 return -1; 249 } 250 251 ppp_interface_info_t info; 252 interface.GetInterfaceInfo(&info); 253 254 // type and name (if it has one) 255 if(info.info.if_unit >= 0) { 256 printf("Type: Visible\n"); 257 printf("Interface: ppp%ld\n", info.info.if_unit); 258 } else 259 printf("Type: Hidden\n"); 260 261 printf("Name: %s\n", info.info.name); 262 263 // ID 264 printf("ID: %ld\n", interface.ID()); 265 266 // ConnectOnDemand 267 printf("ConnectOnDemand: %s\n", info.info.doesConnectOnDemand ? 268 "Enabled" : "Disabled"); 269 270 // AutoReconnect 271 printf("AutoReconnect: %s\n", info.info.doesAutoReconnect ? 272 "Enabled" : "Disabled"); 273 274 // MRU and interfaceMTU 275 printf("MRU: %ld\n", info.info.MRU); 276 printf("Interface MTU: %ld\n", info.info.interfaceMTU); 277 278 // mode 279 printf("Mode: "); 280 if(info.info.mode == PPP_CLIENT_MODE) 281 printf("Client\n"); 282 else if(info.info.mode == PPP_SERVER_MODE) 283 printf("Server\n"); 284 else 285 printf("Unknown\n"); 286 287 // status 288 printf("\tStatus: "); 289 switch(info.info.phase) { 290 case PPP_ESTABLISHED_PHASE: 291 printf("Connected\n"); 292 break; 293 294 case PPP_DOWN_PHASE: 295 printf("Disconnected\n"); 296 break; 297 298 case PPP_TERMINATION_PHASE: 299 printf("Disconnecting\n"); 300 break; 301 302 default: 303 printf("Connecting\n"); 304 } 305 306 return 0; 307 } 308 309 310 int 311 main(int argc, char *argv[]) 312 { 313 if(argc == 2) { 314 if(!strcmp(argv[1], "show") || !strcmp(argv[1], "-a")) 315 return show(PPP_ALL_INTERFACES); 316 else 317 return print_help(); 318 }if(argc == 3) { 319 if(!strcmp(argv[1], "init")) 320 return create(argv[2], false); 321 else if(!strcmp(argv[1], "create")) 322 return create(argv[2], true); 323 else if(!strcmp(argv[1], "connect")) 324 return connect(argv[2]); 325 else if(!strcmp(argv[1], "disconnect")) 326 return disconnect(argv[2]); 327 else if(!strcmp(argv[1], "delete")) 328 return delete_interface(argv[2]); 329 else if(!strcmp(argv[1], "details")) 330 return show_details(argv[2]); 331 else 332 return print_help(); 333 } else 334 return print_help(); 335 } 336