- Timestamp:
- Apr 16, 2013 6:11:59 AM (12 years ago)
- Location:
- trunk/src/VBox
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/Makefile.kmk
r45156 r45559 56 56 VBoxManageStorageController.cpp \ 57 57 VBoxManageUSB.cpp \ 58 $(if $(VBOX_WITH_NAT_SERVICE),VBoxManageNATNetwork.cpp,) 58 $(if $(VBOX_WITH_NAT_SERVICE),VBoxManageNATNetwork.cpp,) \ 59 $(if $(VBOX_WITH_NAT_SERVICE),../../NetworkServices/NetLib/VBoxNetPortForwardString.cpp,) 59 60 VBoxManage_LIBS += $(LIB_DDU) 60 61 … … 82 83 $(if $(VBOX_WITH_NAT_SERVICE),VBOX_WITH_NAT_SERVICE) 83 84 85 # VBoxNetPortForwardString.h 86 VBoxManageNATNetwork.cpp_INCS += ../../NetworkServices/NetLib/ 87 84 88 ifneq ($(KBUILD_TARGET),win) 85 89 # Workaround for buggy gcc-4.3 compilers, see -
trunk/src/VBox/Frontends/VBoxManage/VBoxManageNATNetwork.cpp
r45356 r45559 20 20 *******************************************************************************/ 21 21 #ifndef VBOX_ONLY_DOCS 22 23 22 24 #include <VBox/com/com.h> 23 25 #include <VBox/com/array.h> … … 29 31 #endif /* !VBOX_ONLY_DOCS */ 30 32 33 #include <netinet/in.h> 34 35 #define IPv6 36 37 #include <iprt/cdefs.h> 31 38 #include <iprt/cidr.h> 32 39 #include <iprt/param.h> … … 40 47 #include <VBox/log.h> 41 48 49 #include <vector> 50 42 51 #include "VBoxManage.h" 52 #include "VBoxPortForwardString.h" 43 53 44 54 #ifndef VBOX_ONLY_DOCS 55 45 56 using namespace com; 46 57 … … 62 73 { "--enable", 'e', RTGETOPT_REQ_NOTHING }, 63 74 { "--disable", 'd', RTGETOPT_REQ_NOTHING }, 64 75 { "--port-forward-4", 'p', RTGETOPT_REQ_STRING }, 76 { "--port-forward-6", 'P', RTGETOPT_REQ_STRING }, 65 77 }; 78 79 typedef struct PFNAME2DELETE 80 { 81 char aszName[PF_NAMELEN]; 82 bool fIPv6; 83 } PFNAME2DELETE, *PPFNAME2DELETE; 84 85 typedef std::vector<PFNAME2DELETE> VPF2DELETE; 86 typedef VPF2DELETE::const_iterator VPF2DELETEITERATOR; 87 88 typedef std::vector<PORTFORWARDRULE> VPF2ADD; 89 typedef VPF2ADD::const_iterator VPF2ADDITERATOR; 90 66 91 67 92 static int handleOp(HandlerArg *a, OPCODE enmCode, int iStart, int *pcProcessed) … … 79 104 int ipv6 = -1; 80 105 106 VPF2DELETE vPfName2Delete; 107 VPF2ADD vPf2Add; 108 81 109 int c; 82 110 RTGETOPTUNION ValueUnion; 83 111 RTGETOPTSTATE GetState; 112 84 113 RTGetOptInit(&GetState, 85 114 a->argc, … … 101 130 } 102 131 break; 132 103 133 case 'n': // --network 104 134 if(pNetworkCidr) … … 109 139 } 110 140 break; 141 111 142 case 'e': // --enable 112 143 if(enable >= 0) … … 117 148 } 118 149 break; 150 119 151 case 'd': // --disable 120 152 if(enable >= 0) … … 125 157 } 126 158 break; 127 case VINF_GETOPT_NOT_OPTION: 128 return errorSyntax(USAGE_NATNETWORK, "unhandled parameter: %s", ValueUnion.psz); 129 break; 159 130 160 case 'h': 131 161 if (dhcp != -1) … … 133 163 dhcp = ValueUnion.f; 134 164 break; 165 135 166 case '6': 136 167 if (ipv6 != -1) … … 138 169 ipv6 = ValueUnion.f; 139 170 break; 171 172 case 'P': /* ipv6 portforwarding*/ 173 case 'p': /* ipv4 portforwarding */ 174 { 175 if (RTStrCmp(ValueUnion.psz, "delete") != 0) 176 { 177 PORTFORWARDRULE Pfr; 178 179 /* netPfStrToPf will clean up the Pfr */ 180 int irc = netPfStrToPf(ValueUnion.psz, (c == 'P'), &Pfr); 181 if (RT_FAILURE(irc)) 182 return errorSyntax(USAGE_NATNETWORK, 183 "Invalid port-forward rule %s\n", 184 ValueUnion.psz); 185 186 vPf2Add.push_back(Pfr); 187 } 188 else 189 { 190 int vrc; 191 RTGETOPTUNION NamePf2DeleteUnion; 192 PFNAME2DELETE Name2Delete; 193 194 if (enmCode != OP_MODIFY) 195 return errorSyntax(USAGE_NATNETWORK, 196 "Port-forward could be deleted on modify \n"); 197 198 vrc = RTGetOptFetchValue(&GetState, 199 &NamePf2DeleteUnion, 200 RTGETOPT_REQ_STRING); 201 if (RT_FAILURE(vrc)) 202 return errorSyntax(USAGE_NATNETWORK, 203 "Not enough parmaters\n"); 204 205 if (strlen(NamePf2DeleteUnion.psz) > PF_NAMELEN) 206 return errorSyntax(USAGE_NATNETWORK, 207 "Port-forward rule name is too long\n"); 208 209 RT_ZERO(Name2Delete); 210 RTStrCopy(Name2Delete.aszName, 211 PF_NAMELEN, 212 NamePf2DeleteUnion.psz); 213 Name2Delete.fIPv6 = (c == 'P'); 214 215 vPfName2Delete.push_back(Name2Delete); 216 } 217 break; 218 } 219 220 case VINF_GETOPT_NOT_OPTION: 221 return errorSyntax(USAGE_NATNETWORK, 222 "unhandled parameter: %s", 223 ValueUnion.psz); 224 break; 225 140 226 default: 141 227 if (c > 0) 142 228 { 143 229 if (RT_C_IS_GRAPH(c)) 144 return errorSyntax(USAGE_NATNETWORK, "unhandled option: -%c", c); 230 return errorSyntax(USAGE_NATNETWORK, 231 "unhandled option: -%c", c); 145 232 else 146 return errorSyntax(USAGE_NATNETWORK, "unhandled option: %i", c); 233 return errorSyntax(USAGE_NATNETWORK, 234 "unhandled option: %i", c); 147 235 } 148 236 else if (c == VERR_GETOPT_UNKNOWN_OPTION) 149 return errorSyntax(USAGE_NATNETWORK, "unknown option: %s", ValueUnion.psz); 237 return errorSyntax(USAGE_NATNETWORK, 238 "unknown option: %s", ValueUnion.psz); 150 239 else if (ValueUnion.pDef) 151 return errorSyntax(USAGE_NATNETWORK, "%s: %Rrs", ValueUnion.pDef->pszLong, c); 240 return errorSyntax(USAGE_NATNETWORK, 241 "%s: %Rrs", ValueUnion.pDef->pszLong, c); 152 242 else 153 243 return errorSyntax(USAGE_NATNETWORK, "%Rrs", c); … … 218 308 if(FAILED(rc)) 219 309 return errorArgument("Failed to set configuration"); 310 } 311 312 if (!vPfName2Delete.empty()) 313 { 314 VPF2DELETEITERATOR it; 315 for (it = vPfName2Delete.begin(); it != vPfName2Delete.end(); ++it) 316 { 317 CHECK_ERROR(net, RemovePortForwardRule((BOOL)(*it).fIPv6, 318 Bstr((*it).aszName).raw())); 319 if(FAILED(rc)) 320 return errorArgument("Failed to delete pf"); 321 322 } 323 } 324 325 if (!vPf2Add.empty()) 326 { 327 VPF2ADDITERATOR it; 328 for(it = vPf2Add.begin(); it != vPf2Add.end(); ++it) 329 { 330 NATProtocol_T proto = NATProtocol_TCP; 331 if ((*it).iPfrProto == IPPROTO_TCP) 332 proto = NATProtocol_TCP; 333 else if ((*it).iPfrProto == IPPROTO_UDP) 334 proto = NATProtocol_UDP; 335 else 336 continue; /* XXX: warning here. */ 337 338 CHECK_ERROR(net, AddPortForwardRule( 339 (BOOL)(*it).fPfrIPv6, 340 Bstr((*it).aszPfrName).raw(), 341 proto, 342 Bstr((*it).aszPfrHostAddr).raw(), 343 (*it).u16PfrHostPort, 344 Bstr((*it).aszPfrGuestAddr).raw(), 345 (*it).u16PfrGuestPort)); 346 if(FAILED(rc)) 347 return errorArgument("Failed to add pf"); 348 349 } 220 350 } 221 351 -
trunk/src/VBox/Main/include/NATNetworkImpl.h
r45138 r45559 112 112 private: 113 113 int RecalculateIpv4AddressAssignments(); 114 114 115 typedef std::map<Utf8Str, settings::NATRule> NATRuleMap; 116 typedef NATRuleMap::const_iterator constNATRuleMapIterator; 117 115 118 void GetPortForwardRulesFromMap(ComSafeArrayOut(BSTR, aPortForwardRules), NATRuleMap& aRules); 116 119 /** weak VirtualBox parent */ … … 120 123 struct Data; 121 124 struct Data *m; 122 123 125 124 126 }; -
trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp
r45141 r45559 33 33 34 34 #include "EventImpl.h" 35 #include "VBoxEvents.h" 36 35 37 #include "NATNetworkServiceRunner.h" 36 38 #include "VirtualBoxImpl.h" … … 119 121 /* share VirtualBox weakly (parent remains NULL so far) */ 120 122 unconst(mVirtualBox) = aVirtualBox; 121 122 123 unconst(mName) = aName; 123 124 m = new Data(); … … 126 127 m->IPv6Prefix = "fe80::/64"; 127 128 m->fEnabled = FALSE; 129 130 131 128 132 RecalculateIpv4AddressAssignments(); 133 134 HRESULT hrc = unconst(m->pEventSource).createObject(); 135 if (FAILED(hrc)) throw hrc; 136 137 hrc = m->pEventSource->init(static_cast<INATNetwork *>(this)); 138 if (FAILED(hrc)) throw hrc; 139 129 140 /* Confirm a successful initialization */ 130 141 autoInitSpan.setSucceeded(); … … 150 161 m->fAdvertiseDefaultIPv6Route = data.fAdvertiseDefaultIPv6Route; 151 162 m->fNeedDhcpServer = data.fNeedDhcpServer; 163 152 164 RecalculateIpv4AddressAssignments(); 165 153 166 /* IPv4 port-forward rules */ 154 167 m->mapName2PortForwardRule4.clear(); … … 156 169 it != data.llPortForwardRules4.end(); ++it) 157 170 { 158 m->mapName2PortForwardRule4.insert(std::make_pair(it->strName , *it));171 m->mapName2PortForwardRule4.insert(std::make_pair(it->strName.c_str(), *it)); 159 172 } 160 173 … … 166 179 m->mapName2PortForwardRule6.insert(std::make_pair(it->strName, *it)); 167 180 } 181 182 HRESULT hrc = unconst(m->pEventSource).createObject(); 183 if (FAILED(hrc)) throw hrc; 184 185 hrc = m->pEventSource->init(static_cast<INATNetwork *>(this)); 186 if (FAILED(hrc)) throw hrc; 168 187 169 188 autoInitSpan.setSucceeded(); … … 199 218 it != m->mapName2PortForwardRule6.end(); ++it) 200 219 data.llPortForwardRules4.push_back(it->second); 220 201 221 /* XXX: should we do here a copy of params */ 202 222 /* XXX: should we unlock here? */ … … 247 267 248 268 alock.release(); 269 249 270 #ifdef NAT_XML_SERIALIZATION 250 271 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 322 343 RecalculateIpv4AddressAssignments(); 323 344 alock.release(); 345 324 346 #ifdef NAT_XML_SERIALIZATION 325 347 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 352 374 // save the global settings; for that we should hold only the VirtualBox lock 353 375 alock.release(); 376 354 377 #ifdef NAT_XML_SERIALIZATION 355 378 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 381 404 382 405 unconst(m->IPv6Prefix) = Bstr(aIPv6Prefix); 383 /* @todo: do we need recalcu altion ? */406 /* @todo: do we need recalculation ? */ 384 407 alock.release(); 408 385 409 #ifdef NAT_XML_SERIALIZATION 386 410 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 413 437 // save the global settings; for that we should hold only the VirtualBox lock 414 438 alock.release(); 439 415 440 #ifdef NAT_XML_SERIALIZATION 416 441 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 439 464 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); 440 465 m->fNeedDhcpServer = aNeedDhcpServer; 466 441 467 RecalculateIpv4AddressAssignments(); 468 442 469 // save the global settings; for that we should hold only the VirtualBox lock 443 470 alock.release(); 471 444 472 #ifdef NAT_XML_SERIALIZATION 445 473 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 457 485 458 486 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS); 459 GetPortForwardRulesFromMap(ComSafeArrayInArg(aPortForwardRules4), m->mapName2PortForwardRule4); 487 GetPortForwardRulesFromMap(ComSafeArrayInArg(aPortForwardRules4), 488 m->mapName2PortForwardRule4); 460 489 alock.release(); 461 490 return S_OK; 462 491 } 463 492 464 STDMETHODIMP NATNetwork::COMGETTER(PortForwardRules6)(ComSafeArrayOut(BSTR, aPortForwardRules6)) 493 STDMETHODIMP NATNetwork::COMGETTER(PortForwardRules6)(ComSafeArrayOut(BSTR, 494 aPortForwardRules6)) 465 495 { 466 496 CheckComArgOutSafeArrayPointerValid(aPortForwardRules6); … … 503 533 } 504 534 if (name.isEmpty()) 505 name = Utf8StrFmt("%s_ %s:%d_%s:%d", proto.c_str(),506 Utf8Str(aHostIp).c_str(), aHostPort, 535 name = Utf8StrFmt("%s_[%s]%%%d_[%s]%%%d", proto.c_str(), 536 Utf8Str(aHostIp).c_str(), aHostPort, 507 537 Utf8Str(aGuestIp).c_str(), aGuestPort); 508 538 509 539 NATRuleMap::iterator it; 540 510 541 for (it = mapRules.begin(); it != mapRules.end(); ++it) 511 542 { … … 534 565 aHostIp, aHostPort, 535 566 aGuestIp, aGuestPort); 567 568 /* Notify listerners listening on this network only */ 569 fireNATNetworkPortForwardEvent(m->pEventSource, mName.raw(), TRUE, 570 aIsIpv6, aPortForwardRuleName, aProto, 571 aHostIp, aHostPort, 572 aGuestIp, aGuestPort); 573 536 574 #ifdef NAT_XML_SERIALIZATION 537 575 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); 538 576 rc = mVirtualBox->saveSettings(); 539 577 #endif 540 /* @todo: fire the event */541 578 return rc; 542 579 } … … 545 582 { 546 583 int rc = S_OK; 584 Utf8Str strHostIP, strGuestIP; 585 uint16_t u16HostPort, u16GuestPort; 586 NATProtocol_T proto = NATProtocol_TCP; 587 547 588 AutoCaller autoCaller(this); 548 589 if (FAILED(autoCaller.rc())) return autoCaller.rc(); … … 551 592 NATRuleMap& mapRules = aIsIpv6 ? m->mapName2PortForwardRule6 : m->mapName2PortForwardRule4; 552 593 NATRuleMap::iterator it = mapRules.find(aPortForwardRuleName); 594 553 595 if (it == mapRules.end()) 554 596 return E_INVALIDARG; 555 597 598 strHostIP = it->second.strHostIP; 599 strGuestIP = it->second.strGuestIP; 600 u16HostPort = it->second.u16HostPort; 601 u16GuestPort = it->second.u16GuestPort; 602 proto = it->second.proto; 603 556 604 mapRules.erase(it); 557 605 558 606 alock.release(); 559 /* we need only name here, it supposed to be uniq within IP version protocols */ 607 560 608 mVirtualBox->onNATNetworkPortForward(mName.raw(), FALSE, aIsIpv6, 561 aPortForwardRuleName, NATProtocol_TCP, 562 Bstr().raw(), 0, 563 Bstr().raw(), 0); 609 aPortForwardRuleName, proto, 610 Bstr(strHostIP).raw(), u16HostPort, 611 Bstr(strGuestIP).raw(), u16GuestPort); 612 613 /* Notify listerners listening on this network only */ 614 fireNATNetworkPortForwardEvent(m->pEventSource, mName.raw(), FALSE, 615 aIsIpv6, aPortForwardRuleName, proto, 616 Bstr(strHostIP).raw(), u16HostPort, 617 Bstr(strGuestIP).raw(), u16GuestPort); 564 618 #ifdef NAT_XML_SERIALIZATION 565 619 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); 566 620 rc = mVirtualBox->saveSettings(); 567 621 #endif 568 /* @todo: fire the event */ 622 569 623 return rc; 570 624 } … … 582 636 m->NATRunner.setOption(NATSCCFG_IPADDRESS, m->IPv4Gateway, true); 583 637 m->NATRunner.setOption(NATSCCFG_NETMASK, m->IPv4NetworkMask, true); 638 639 /* port-forwarding */ 640 641 for (constNATRuleMapIterator it = m->mapName2PortForwardRule4.begin(); 642 it != m->mapName2PortForwardRule4.end(); ++it) 643 { 644 settings::NATRule r = it->second; 645 m->NATRunner.setOption(NATSCCFG_PORTFORWARD4, 646 Bstr(Utf8StrFmt("%s:%d:[%s]:%d:[%s]:%d", 647 r.strName.c_str(), 648 r.proto, 649 r.strHostIP.isEmpty() ? 650 "0.0.0.0" : 651 r.strHostIP.c_str(), 652 r.u16HostPort, 653 r.strGuestIP.c_str(), 654 r.u16GuestPort)), true); 655 } 584 656 585 657 if (m->fNeedDhcpServer) … … 684 756 { 685 757 settings::NATRule r = it->second; 686 BstrFmt bstr("%s ,%d,%s,%d,%s,%d",758 BstrFmt bstr("%s:%s:[%s]:%d:[%s]:%d", 687 759 r.strName.c_str(), 688 r.proto,760 (r.proto == NATProtocol_TCP? "tcp" : "udp"), 689 761 r.strHostIP.c_str(), 690 762 r.u16HostPort, … … 698 770 int NATNetwork::RecalculateIpv4AddressAssignments() 699 771 { 700 /**701 * We assume that port-forwarding rules set is empty!702 * possible scenarious on change of CIDR we clean up (1) pfs703 * or (2) rewrite all rules to new network.704 */705 AssertReturn(m->mapName2PortForwardRule4.empty(), VERR_INTERNAL_ERROR);706 772 RTNETADDRIPV4 network, netmask, gateway; 707 773 char aszGatewayIp[16], aszNetmask[16]; -
trunk/src/VBox/NetworkServices/NAT/Makefile.kmk
r44824 r45559 69 69 PROGRAMS += VBoxNetLwipNAT 70 70 VBoxNetLwipNAT_TEMPLATE = 71 VBoxNetLwipNAT_TEMPLATE := VBOXR3$(if-expr defined(VBOX_WITH_HARDENING),,EXE) 72 #VBoxNetLwipNAT_INCS += ${LWIP_INCS} 71 VBoxNetLwipNAT_TEMPLATE := VBOXMAINCLIENTEXE 73 72 VBoxNetLwipNAT_INCS += ../../Devices/Network \ 74 73 ../../Devices/Network/lwip-new/vbox # testproxy.h … … 76 75 VBoxNetLwipNAT_SOURCES += VBoxNetLwipNAT.cpp \ 77 76 ../NetLib/VBoxNetBaseService.cpp \ 77 ../NetLib/VBoxNetPortForwardString.cpp \ 78 78 ../../Devices/Network/VBoxLwipCore.cpp 79 79 VBoxNetLwipNAT_LIBS = \ … … 90 90 endif 91 91 92 PROGRAMS += tstNetPfAddressPortPairParse 93 94 tstNetPfAddressPortPairParse_TEMPLATE = VBOXR3TSTEXE 95 #tstNetPfAddressPortPairParse_INSTTYPE = none 96 tstNetPfAddressPortPairParse_SOURCES = ../NetLib/testcase/tstNetPfAddressPortPairParse.cpp \ 97 ../NetLib/VBoxNetPortForwardString.cpp 98 99 92 100 include $(FILE_KBUILD_SUB_FOOTER)
Note:
See TracChangeset
for help on using the changeset viewer.