Changeset 48104 in vbox for trunk/src/VBox
- Timestamp:
- Aug 27, 2013 7:36:47 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/VBoxManageNATNetwork.cpp
r48096 r48104 20 20 *******************************************************************************/ 21 21 #ifndef VBOX_ONLY_DOCS 22 23 22 24 23 #include <VBox/com/com.h> … … 135 134 case 't': // --netname 136 135 if (pNetName) 137 return errorSyntax(USAGE_NATNETWORK, "You can only specify --netname once."); 138 else 139 { 136 return errorSyntax(USAGE_NATNETWORK, "You can only specify --netname only once."); 137 else 140 138 pNetName = ValueUnion.psz; 141 } 142 break; 139 break; 143 140 144 141 case 'n': // --network 145 142 if (pNetworkCidr) 146 return errorSyntax(USAGE_NATNETWORK, "You can only specify --network once."); 147 else 148 { 143 return errorSyntax(USAGE_NATNETWORK, "You can only specify --network only once."); 144 else 149 145 pNetworkCidr = ValueUnion.psz; 150 } 151 break; 146 break; 152 147 153 148 case 'e': // --enable 154 if (enable >= 0)149 if (enable >= 0) 155 150 return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once."); 156 151 else 157 {158 152 enable = 1; 159 } 160 break; 153 break; 161 154 162 155 case 'd': // --disable … … 164 157 return errorSyntax(USAGE_NATNETWORK, "You can specify either --enable or --disable once."); 165 158 else 166 {167 159 enable = 0; 168 } 169 break; 170 160 break; 161 171 162 case 'h': 172 163 if (dhcp != -1) 173 return errorSyntax(USAGE_NATNETWORK, "You can specify --dhcp on ce.");164 return errorSyntax(USAGE_NATNETWORK, "You can specify --dhcp only once."); 174 165 dhcp = ValueUnion.f; 175 166 break; … … 177 168 case '6': 178 169 if (ipv6 != -1) 179 return errorSyntax(USAGE_NATNETWORK, "You can specify --ipv6 on ce.");170 return errorSyntax(USAGE_NATNETWORK, "You can specify --ipv6 only once."); 180 171 ipv6 = ValueUnion.f; 181 172 break; … … 190 181 "loopback couldn't be deleted on modified\n"); 191 182 if (c == 'L') 192 183 loopback6Offset = -1; 193 184 else 194 185 { … … 227 218 "Invalid port-forward rule %s\n", 228 219 ValueUnion.psz); 229 220 230 221 vPf2Add.push_back(Pfr); 231 222 } … … 240 231 "Port-forward could be deleted on modify \n"); 241 232 242 vrc = RTGetOptFetchValue(&GetState, 243 &NamePf2DeleteUnion, 233 vrc = RTGetOptFetchValue(&GetState, 234 &NamePf2DeleteUnion, 244 235 RTGETOPT_REQ_STRING); 245 236 if (RT_FAILURE(vrc)) 246 237 return errorSyntax(USAGE_NATNETWORK, 247 238 "Not enough parmaters\n"); 248 239 249 240 if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN) 250 241 return errorSyntax(USAGE_NATNETWORK, … … 252 243 253 244 RT_ZERO(Name2Delete); 254 RTStrCopy(Name2Delete.aszName, 255 PF_NAMELEN, 256 NamePf2DeleteUnion.psz); 245 RTStrCopy(Name2Delete.aszName, PF_NAMELEN, NamePf2DeleteUnion.psz); 257 246 Name2Delete.fIPv6 = (c == 'P'); 258 247 … … 263 252 264 253 case VINF_GETOPT_NOT_OPTION: 265 return errorSyntax(USAGE_NATNETWORK, 266 "unhandled parameter: %s", 254 return errorSyntax(USAGE_NATNETWORK, 255 "unhandled parameter: %s", 267 256 ValueUnion.psz); 268 257 break; … … 272 261 { 273 262 if (RT_C_IS_GRAPH(c)) 274 return errorSyntax(USAGE_NATNETWORK, 263 return errorSyntax(USAGE_NATNETWORK, 275 264 "unhandled option: -%c", c); 276 265 else 277 return errorSyntax(USAGE_NATNETWORK, 266 return errorSyntax(USAGE_NATNETWORK, 278 267 "unhandled option: %i", c); 279 268 } 280 269 else if (c == VERR_GETOPT_UNKNOWN_OPTION) 281 return errorSyntax(USAGE_NATNETWORK, 270 return errorSyntax(USAGE_NATNETWORK, 282 271 "unknown option: %s", ValueUnion.psz); 283 272 else if (ValueUnion.pDef) 284 return errorSyntax(USAGE_NATNETWORK, 273 return errorSyntax(USAGE_NATNETWORK, 285 274 "%s: %Rrs", ValueUnion.pDef->pszLong, c); 286 275 else … … 290 279 291 280 if (!pNetName) 292 return errorSyntax(USAGE_NATNETWORK, 293 "You need to specify --netname option");281 return errorSyntax(USAGE_NATNETWORK, 282 "You need to specify the --netname option"); 294 283 /* verification */ 295 284 switch (enmCode) … … 297 286 case OP_ADD: 298 287 if (!pNetworkCidr) 299 return errorSyntax(USAGE_NATNETWORK, 300 "You need to specify --network option");288 return errorSyntax(USAGE_NATNETWORK, 289 "You need to specify the --network option"); 301 290 break; 302 291 case OP_MODIFY: … … 311 300 Bstr NetName; 312 301 NetName = Bstr(pNetName); 313 314 302 315 303 ComPtr<INATNetwork> net; 316 304 rc = a->virtualBox->FindNATNetworkByName(NetName.mutableRaw(), net.asOutParam()); 317 if (enmCode == OP_ADD)305 if (enmCode == OP_ADD) 318 306 { 319 307 if (SUCCEEDED(rc)) … … 325 313 } 326 314 else if (FAILED(rc)) 327 {328 315 return errorArgument("NATNetwork server does not exist"); 329 }330 316 331 317 switch (enmCode) 332 318 { 333 case OP_ADD: 334 case OP_MODIFY: 335 { 336 if (pNetworkCidr) 319 case OP_ADD: 320 case OP_MODIFY: 337 321 { 338 CHECK_ERROR(net, COMSETTER(Network)(Bstr(pNetworkCidr).raw())); 339 if(FAILED(rc)) 340 return errorArgument("Failed to set configuration"); 322 if (pNetworkCidr) 323 { 324 CHECK_ERROR(net, COMSETTER(Network)(Bstr(pNetworkCidr).raw())); 325 if (FAILED(rc)) 326 return errorArgument("Failed to set configuration"); 327 } 328 if (dhcp >= 0) 329 { 330 CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp)); 331 if (FAILED(rc)) 332 return errorArgument("Failed to set configuration"); 333 } 334 335 if (ipv6 >= 0) 336 { 337 CHECK_ERROR(net, COMSETTER(IPv6Enabled) ((BOOL)ipv6)); 338 if (FAILED(rc)) 339 return errorArgument("Failed to set configuration"); 340 } 341 342 if (!vPfName2Delete.empty()) 343 { 344 VPF2DELETEITERATOR it; 345 for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it) 346 { 347 CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6, 348 Bstr((*it).aszName).raw())); 349 if (FAILED(rc)) 350 return errorArgument("Failed to delete pf"); 351 } 352 } 353 354 if (!vPf2Add.empty()) 355 { 356 VPF2ADDITERATOR it; 357 for (it = vPf2Add.begin(); it != vPf2Add.end(); ++it) 358 { 359 NATProtocol_T proto = NATProtocol_TCP; 360 if ((*it).iPfrProto == IPPROTO_TCP) 361 proto = NATProtocol_TCP; 362 else if ((*it).iPfrProto == IPPROTO_UDP) 363 proto = NATProtocol_UDP; 364 else 365 continue; /* XXX: warning here. */ 366 367 CHECK_ERROR(net, AddPortForwardRule((BOOL)(*it).fPfrIPv6, 368 Bstr((*it).aszPfrName).raw(), 369 proto, 370 Bstr((*it).aszPfrHostAddr).raw(), 371 (*it).u16PfrHostPort, 372 Bstr((*it).aszPfrGuestAddr).raw(), 373 (*it).u16PfrGuestPort)); 374 if (FAILED(rc)) 375 return errorArgument("Failed to add pf"); 376 } 377 } 378 379 if (loopback6Offset) 380 { 381 if (loopback6Offset == -1) 382 loopback6Offset = 0; /* deletion */ 383 384 CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), rc); 385 } 386 387 /* addLocalMapping (hostid, offset) */ 388 if (!vLoopback2Add.empty()) 389 { 390 /* we're expecting stings 127.0.0.1;5 */ 391 LOOPBACK2DELETEADDITERATOR it; 392 for (it = vLoopback2Add.begin(); 393 it != vLoopback2Add.end(); 394 ++it) 395 { 396 std::string address, strOffset; 397 int pos = it->find(';'); 398 LONG lOffset = 0; 399 Bstr bstrAddress; 400 401 AssertReturn(pos != -1, errorArgument("invalid loopback string")); 402 403 address = it->substr(0, pos); 404 strOffset = it->substr(pos + 1); 405 406 lOffset = RTStrToUInt32(strOffset.c_str()); 407 AssertReturn(lOffset > 0, errorArgument("invalid loopback string")); 408 409 bstrAddress = Bstr(address.c_str()); 410 411 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), rc); 412 } 413 } 414 415 if (!vLoopback2Delete.empty()) 416 { 417 /* we're expecting stings 127.0.0.1 */ 418 LOOPBACK2DELETEADDITERATOR it; 419 for (it = vLoopback2Add.begin(); 420 it != vLoopback2Add.end(); 421 ++it) 422 { 423 Bstr bstrAddress; 424 bstrAddress = Bstr(it->c_str()); 425 426 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), rc); 427 } 428 } 429 430 if (enable >= 0) 431 { 432 CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable)); 433 if (FAILED(rc)) 434 return errorArgument("Failed to set configuration"); 435 } 436 break; 341 437 } 342 if (dhcp >= 0)438 case OP_REMOVE: 343 439 { 344 CHECK_ERROR(net, COMSETTER(NeedDhcpServer) ((BOOL)dhcp)); 345 if(FAILED(rc)) 346 return errorArgument("Failed to set configuration"); 440 CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net)); 441 if (FAILED(rc)) 442 return errorArgument("Failed to remove nat network"); 443 break; 347 444 } 348 349 if (ipv6 >= 0) 445 case OP_START: 350 446 { 351 CHECK_ERROR(net, COMSETTER(IPv6Enabled) ((BOOL)ipv6)); 352 if(FAILED(rc)) 353 return errorArgument("Failed to set configuration"); 447 CHECK_ERROR(net, Start(Bstr("whatever").raw())); 448 if (FAILED(rc)) 449 return errorArgument("Failed to start network"); 450 break; 354 451 } 355 356 if (!vPfName2Delete.empty()) 452 case OP_STOP: 357 453 { 358 VPF2DELETEITERATOR it; 359 for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it) 360 { 361 CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6, 362 Bstr((*it).aszName).raw())); 363 if(FAILED(rc)) 364 return errorArgument("Failed to delete pf"); 365 366 } 454 CHECK_ERROR(net, Stop()); 455 if (FAILED(rc)) 456 return errorArgument("Failed to start network"); 457 break; 367 458 } 368 369 if (!vPf2Add.empty()) 370 { 371 VPF2ADDITERATOR it; 372 for(it = vPf2Add.begin(); it != vPf2Add.end(); ++it) 373 { 374 NATProtocol_T proto = NATProtocol_TCP; 375 if ((*it).iPfrProto == IPPROTO_TCP) 376 proto = NATProtocol_TCP; 377 else if ((*it).iPfrProto == IPPROTO_UDP) 378 proto = NATProtocol_UDP; 379 else 380 continue; /* XXX: warning here. */ 381 382 CHECK_ERROR(net, AddPortForwardRule( 383 (BOOL)(*it).fPfrIPv6, 384 Bstr((*it).aszPfrName).raw(), 385 proto, 386 Bstr((*it).aszPfrHostAddr).raw(), 387 (*it).u16PfrHostPort, 388 Bstr((*it).aszPfrGuestAddr).raw(), 389 (*it).u16PfrGuestPort)); 390 if(FAILED(rc)) 391 return errorArgument("Failed to add pf"); 392 393 } 394 } 395 396 if (loopback6Offset) 397 { 398 if (loopback6Offset == -1) 399 loopback6Offset = 0; /* deletion */ 400 401 CHECK_ERROR_RET(net, COMSETTER(LoopbackIp6)(loopback6Offset), rc); 402 } 403 404 /* addLocalMapping (hostid, offset) */ 405 if (!vLoopback2Add.empty()) 406 { 407 /* we're expecting stings 127.0.0.1;5 */ 408 LOOPBACK2DELETEADDITERATOR it; 409 for (it = vLoopback2Add.begin(); 410 it != vLoopback2Add.end(); 411 ++it) 412 { 413 std::string address, strOffset; 414 int pos = it->find(';'); 415 LONG lOffset = 0; 416 Bstr bstrAddress; 417 418 AssertReturn(pos != -1, errorArgument("invalid loopback string")); 419 420 address = it->substr(0, pos); 421 strOffset = it->substr(pos + 1); 422 423 lOffset = RTStrToUInt32(strOffset.c_str()); 424 AssertReturn(lOffset > 0, errorArgument("invalid loopback string")); 425 426 bstrAddress = Bstr(address.c_str()); 427 428 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), lOffset), rc); 429 } 430 } 431 432 if (!vLoopback2Delete.empty()) 433 { 434 /* we're expecting stings 127.0.0.1 */ 435 LOOPBACK2DELETEADDITERATOR it; 436 for (it = vLoopback2Add.begin(); 437 it != vLoopback2Add.end(); 438 ++it) 439 { 440 Bstr bstrAddress; 441 bstrAddress = Bstr(it->c_str()); 442 443 CHECK_ERROR_RET(net, AddLocalMapping(bstrAddress.raw(), 0), rc); 444 } 445 } 446 447 448 if(enable >= 0) 449 { 450 CHECK_ERROR(net, COMSETTER(Enabled) ((BOOL)enable)); 451 if(FAILED(rc)) 452 return errorArgument("Failed to set configuration"); 453 454 } 455 break; 456 } 457 case OP_REMOVE: 458 { 459 CHECK_ERROR(a->virtualBox, RemoveNATNetwork(net)); 460 if(FAILED(rc)) 461 return errorArgument("Failed to remove nat network"); 462 break; 463 } 464 case OP_START: 465 { 466 CHECK_ERROR(net, Start(Bstr("whatever").raw())); 467 if(FAILED(rc)) 468 return errorArgument("Failed to start network"); 469 break; 470 } 471 case OP_STOP: 472 { 473 CHECK_ERROR(net, Stop()); 474 if(FAILED(rc)) 475 return errorArgument("Failed to start network"); 476 break; 477 } 478 default:; 459 default:; 479 460 } 480 461 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.