Changeset 48107 in vbox for trunk/src/VBox/Main/src-server
- Timestamp:
- Aug 27, 2013 10:15:13 PM (12 years ago)
- svn:sync-xref-src-repo-rev:
- 88483
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp
r48103 r48107 1 1 /* $Id$ */ 2 3 2 /** @file 4 * 5 * VirtualBox COM class implementation 3 * INATNetwork implementation. 6 4 */ 7 5 … … 40 38 41 39 #ifndef RT_OS_WINDOWS 42 # include <netinet/in.h>40 # include <netinet/in.h> 43 41 #else 44 # include <ws2def.h>45 42 # define IN_LOOPBACKNET 127 46 43 #endif … … 52 49 struct NATNetwork::Data 53 50 { 54 Data() : 55 56 fEnabled(FALSE), 57 fIPv6Enabled(FALSE), 58 fAdvertiseDefaultIPv6Route(FALSE), 59 fNeedDhcpServer(FALSE) 51 Data() 52 : fEnabled(FALSE) 53 , fIPv6Enabled(FALSE) 54 , fAdvertiseDefaultIPv6Route(FALSE) 55 , fNeedDhcpServer(FALSE) 60 56 { 61 57 IPv4Gateway.setNull(); … … 88 84 settings::NATLoopbackOffsetList llNATLoopbackOffsetList; 89 85 uint32_t u32LoopbackIp6; 90 uint32_t u32GatewayOffset;91 uint32_t u32DhcpOffset;86 uint32_t offGateway; 87 uint32_t offDhcp; 92 88 }; 93 89 … … 135 131 unconst(mName) = aName; 136 132 m = new Data(); 137 m-> u32GatewayOffset= 1;133 m->offGateway = 1; 138 134 m->IPv4NetworkCidr = "10.0.2.0/24"; 139 135 m->IPv6Prefix = "fe80::/64"; … … 145 141 m->llNATLoopbackOffsetList.push_back(off); 146 142 147 RecalculateIpv4AddressAssignments();143 recalculateIpv4AddressAssignments(); 148 144 149 145 HRESULT hrc = unconst(m->pEventSource).createObject(); … … 183 179 data.llHostLoopbackOffsetList.end()); 184 180 185 RecalculateIpv4AddressAssignments();181 recalculateIpv4AddressAssignments(); 186 182 187 183 /* IPv4 port-forward rules */ … … 232 228 for (NATRuleMap::iterator it = m->mapName2PortForwardRule4.begin(); 233 229 it != m->mapName2PortForwardRule4.end(); ++it) 234 data.llPortForwardRules4.push_back(it->second);230 data.llPortForwardRules4.push_back(it->second); 235 231 236 232 /* saving ipv6 port-forward Rules*/ … … 238 234 for (NATRuleMap::iterator it = m->mapName2PortForwardRule6.begin(); 239 235 it != m->mapName2PortForwardRule6.end(); ++it) 240 data.llPortForwardRules4.push_back(it->second);236 data.llPortForwardRules4.push_back(it->second); 241 237 242 238 data.u32HostLoopback6Offset = m->u32LoopbackIp6; … … 255 251 return S_OK; 256 252 } 257 #endif 253 #endif /* NAT_XML_SERIALIZATION */ 258 254 259 255 STDMETHODIMP NATNetwork::COMGETTER(EventSource)(IEventSource ** aEventSource) … … 270 266 } 271 267 272 STDMETHODIMP NATNetwork::COMGETTER(NetworkName) 268 STDMETHODIMP NATNetwork::COMGETTER(NetworkName)(BSTR *aName) 273 269 { 274 270 CheckComArgOutPointerValid(aName); … … 282 278 } 283 279 284 STDMETHODIMP NATNetwork::COMSETTER(NetworkName) 280 STDMETHODIMP NATNetwork::COMSETTER(NetworkName)(IN_BSTR aName) 285 281 { 286 282 CheckComArgOutPointerValid(aName); … … 302 298 303 299 304 STDMETHODIMP NATNetwork::COMGETTER(Enabled) 300 STDMETHODIMP NATNetwork::COMGETTER(Enabled)(BOOL *aEnabled) 305 301 { 306 302 CheckComArgOutPointerValid(aEnabled); … … 310 306 311 307 *aEnabled = m->fEnabled; 312 RecalculateIpv4AddressAssignments();313 314 return S_OK; 315 } 316 317 STDMETHODIMP NATNetwork::COMSETTER(Enabled) 308 recalculateIpv4AddressAssignments(); 309 310 return S_OK; 311 } 312 313 STDMETHODIMP NATNetwork::COMSETTER(Enabled)(BOOL aEnabled) 318 314 { 319 315 AutoCaller autoCaller(this); … … 332 328 } 333 329 334 STDMETHODIMP NATNetwork::COMGETTER(Gateway) 330 STDMETHODIMP NATNetwork::COMGETTER(Gateway)(BSTR *aIPv4Gateway) 335 331 { 336 332 CheckComArgOutPointerValid(aIPv4Gateway); … … 344 340 } 345 341 346 STDMETHODIMP NATNetwork::COMGETTER(Network) 342 STDMETHODIMP NATNetwork::COMGETTER(Network)(BSTR *aIPv4NetworkCidr) 347 343 { 348 344 CheckComArgOutPointerValid(aIPv4NetworkCidr); … … 354 350 } 355 351 356 STDMETHODIMP NATNetwork::COMSETTER(Network) 352 STDMETHODIMP NATNetwork::COMSETTER(Network)(IN_BSTR aIPv4NetworkCidr) 357 353 { 358 354 CheckComArgOutPointerValid(aIPv4NetworkCidr); … … 367 363 368 364 unconst(m->IPv4NetworkCidr) = Bstr(aIPv4NetworkCidr); 369 RecalculateIpv4AddressAssignments();365 recalculateIpv4AddressAssignments(); 370 366 alock.release(); 371 367 … … 394 390 AutoCaller autoCaller(this); 395 391 if (FAILED(autoCaller.rc())) return autoCaller.rc(); 396 HRESULT rc = S_OK;397 392 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); 398 393 m->fAdvertiseDefaultIPv6Route = aAdvertiseDefaultIPv6Route; … … 401 396 alock.release(); 402 397 398 HRESULT rc = S_OK; 403 399 #ifdef NAT_XML_SERIALIZATION 404 400 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 462 458 AutoCaller autoCaller(this); 463 459 if (FAILED(autoCaller.rc())) return autoCaller.rc(); 464 HRESULT rc = S_OK;465 460 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); 466 461 m->fAdvertiseDefaultIPv6Route = aAdvertiseDefaultIPv6Route; … … 469 464 alock.release(); 470 465 466 HRESULT rc = S_OK; 471 467 #ifdef NAT_XML_SERIALIZATION 472 468 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 492 488 AutoCaller autoCaller(this); 493 489 if (FAILED(autoCaller.rc())) return autoCaller.rc(); 494 HRESULT rc = S_OK;495 490 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); 496 491 m->fNeedDhcpServer = aNeedDhcpServer; 497 492 498 RecalculateIpv4AddressAssignments();493 recalculateIpv4AddressAssignments(); 499 494 500 495 // save the global settings; for that we should hold only the VirtualBox lock 501 496 alock.release(); 502 497 498 HRESULT rc = S_OK; 503 499 #ifdef NAT_XML_SERIALIZATION 504 500 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 684 680 } 685 681 if (name.isEmpty()) 686 name = Utf8StrFmt("%s_[%s]%%%d_[%s]%%%d", proto.c_str(),687 Utf8Str(aHostIp).c_str(), aHostPort,688 Utf8Str(aGuestIp).c_str(), aGuestPort);682 name = Utf8StrFmt("%s_[%s]%%%d_[%s]%%%d", proto.c_str(), 683 Utf8Str(aHostIp).c_str(), aHostPort, 684 Utf8Str(aGuestIp).c_str(), aGuestPort); 689 685 690 686 NATRuleMap::iterator it; … … 732 728 STDMETHODIMP NATNetwork::RemovePortForwardRule(BOOL aIsIpv6, IN_BSTR aPortForwardRuleName) 733 729 { 734 int rc = S_OK;735 Utf8Str strHostIP, strGuestIP;736 uint16_t u16HostPort, u16GuestPort;737 NATProtocol_T proto = NATProtocol_TCP;738 739 730 AutoCaller autoCaller(this); 740 731 if (FAILED(autoCaller.rc())) return autoCaller.rc(); … … 747 738 return E_INVALIDARG; 748 739 749 strHostIP = it->second.strHostIP;750 strGuestIP = it->second.strGuestIP;751 u 16HostPort = it->second.u16HostPort;752 u 16GuestPort = it->second.u16GuestPort;753 proto = it->second.proto;740 Utf8Str strHostIP = it->second.strHostIP; 741 Utf8Str strGuestIP = it->second.strGuestIP; 742 uint16_t u16HostPort = it->second.u16HostPort; 743 uint16_t u16GuestPort = it->second.u16GuestPort; 744 NATProtocol_T proto = it->second.proto; 754 745 755 746 mapRules.erase(it); … … 767 758 Bstr(strHostIP).raw(), u16HostPort, 768 759 Bstr(strGuestIP).raw(), u16GuestPort); 760 HRESULT rc = S_OK; 769 761 #ifdef NAT_XML_SERIALIZATION 770 762 AutoWriteLock vboxLock(mVirtualBox COMMA_LOCKVAL_SRC_POS); … … 793 785 if (m->fNeedDhcpServer) 794 786 { 795 /* *787 /* 796 788 * Just to as idea... via API (on creation user pass the cidr of network and) 797 789 * and we calculate it's addreses (mutable?). … … 864 856 return S_OK; 865 857 } 866 else return E_FAIL; 858 /** @todo missing setError()! */ 859 return E_FAIL; 867 860 #else 868 861 NOREF(aTrunkType); … … 882 875 return S_OK; 883 876 } 884 else return E_FAIL; 877 /** @todo missing setError()! */ 878 return E_FAIL; 885 879 #else 886 880 ReturnComNotImplemented(); … … 910 904 911 905 912 int NATNetwork::findFirstAvailableOffset(uint32_t *pu32Offset) 913 { 914 uint32_t offset; 906 int NATNetwork::findFirstAvailableOffset(uint32_t *poff) 907 { 915 908 RTNETADDRIPV4 network, netmask; 916 909 … … 920 913 AssertRCReturn(rc, rc); 921 914 915 uint32_t off; 922 916 settings::NATLoopbackOffsetList::iterator it; 923 for (offset = 1; offset < (network.u & (~netmask.u)); ++offset) 924 { 925 bool skip = false; 926 927 if (offset == m->u32GatewayOffset) 917 for (off = 1; off < (network.u & ~netmask.u); ++off) 918 { 919 920 if (off == m->offGateway) 928 921 continue; 929 922 930 if (off set == m->u32DhcpOffset)923 if (off == m->offDhcp) 931 924 continue; 932 925 926 bool skip = false; 933 927 for (it = m->llNATLoopbackOffsetList.begin(); 934 928 it != m->llNATLoopbackOffsetList.end(); 935 929 ++it) 936 930 { 937 if ((*it).u32Offset == off set)931 if ((*it).u32Offset == off) 938 932 { 939 933 skip = true; … … 943 937 } 944 938 945 if (!skip)939 if (!skip) 946 940 break; 947 941 } 948 942 949 if (p u32Offset)950 *p u32Offset = offset;943 if (poff) 944 *poff = off; 951 945 952 946 return VINF_SUCCESS; 953 947 } 954 948 955 int NATNetwork::RecalculateIpv4AddressAssignments() 956 { 957 RTNETADDRIPV4 network, netmask, gateway; 958 char aszGatewayIp[16], aszNetmask[16]; 959 RT_ZERO(aszNetmask); 949 int NATNetwork::recalculateIpv4AddressAssignments() 950 { 951 RTNETADDRIPV4 network, netmask; 960 952 int rc = RTCidrStrToIPv4(Utf8Str(m->IPv4NetworkCidr.raw()).c_str(), 961 953 &network, … … 963 955 AssertRCReturn(rc, rc); 964 956 965 findFirstAvailableOffset(&m-> u32GatewayOffset);957 findFirstAvailableOffset(&m->offGateway); 966 958 if (m->fNeedDhcpServer) 967 findFirstAvailableOffset(&m->u32DhcpOffset); 968 969 /* I don't remember the reason CIDR calcualted in host */ 970 gateway.u = network.u; 971 972 gateway.u += m->u32GatewayOffset; 959 findFirstAvailableOffset(&m->offDhcp); 960 961 /* I don't remember the reason CIDR calculated on the host. */ 962 RTNETADDRIPV4 gateway = network; 963 gateway.u += m->offGateway; 973 964 gateway.u = RT_H2N_U32(gateway.u); 974 RTStrPrintf(aszGatewayIp, 16, "%RTnaipv4", gateway);975 976 m->IPv4Gateway = RTStrDup(aszGatewayIp);965 char szTmpIp[16]; 966 RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", gateway); 967 m->IPv4Gateway = szTmpIp; 977 968 978 969 if (m->fNeedDhcpServer) 979 970 { 980 RTNETADDRIPV4 dhcpserver, 981 dhcplowerip, 982 dhcpupperip; 983 char aszNetwork[16], 984 aszDhcpIp[16], 985 aszDhcpLowerIp[16], 986 aszDhcpUpperIp[16]; 987 RT_ZERO(aszNetwork); 988 989 RT_ZERO(aszDhcpIp); 990 RT_ZERO(aszDhcpLowerIp); 991 RT_ZERO(aszDhcpUpperIp); 992 993 dhcpserver.u = network.u; 994 dhcpserver.u += m->u32DhcpOffset; 995 971 RTNETADDRIPV4 dhcpserver = network; 972 dhcpserver.u += m->offDhcp; 996 973 997 974 /* XXX: adding more services should change the math here */ 975 RTNETADDRIPV4 dhcplowerip; 998 976 dhcplowerip.u = RT_H2N_U32(dhcpserver.u + 1); 999 dhcpupperip.u = RT_H2N_U32((network.u | (~netmask.u)) -1); 977 RTNETADDRIPV4 dhcpupperip; 978 dhcpupperip.u = RT_H2N_U32((network.u | ~netmask.u) - 1); 979 1000 980 dhcpserver.u = RT_H2N_U32(dhcpserver.u); 1001 981 network.u = RT_H2N_U32(network.u); 1002 982 1003 RTStrPrintf(aszNetwork, 16, "%RTnaipv4", network); 1004 RTStrPrintf(aszDhcpLowerIp, 16, "%RTnaipv4", dhcplowerip); 1005 RTStrPrintf(aszDhcpUpperIp, 16, "%RTnaipv4", dhcpupperip); 1006 RTStrPrintf(aszDhcpIp, 16, "%RTnaipv4", dhcpserver); 1007 1008 m->IPv4DhcpServer = aszDhcpIp; 1009 m->IPv4DhcpServerLowerIp = aszDhcpLowerIp; 1010 m->IPv4DhcpServerUpperIp = aszDhcpUpperIp; 1011 1012 LogFunc(("network: %RTnaipv4, dhcpserver:%RTnaipv4, dhcplowerip:%RTnaipv4, dhcpupperip:%RTnaipv4\n", 1013 network, 1014 dhcpserver, 1015 dhcplowerip, 1016 dhcpupperip)); 1017 } 983 RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpserver); 984 m->IPv4DhcpServer = szTmpIp; 985 RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcplowerip); 986 m->IPv4DhcpServerLowerIp = szTmpIp; 987 RTStrPrintf(szTmpIp, sizeof(szTmpIp), "%RTnaipv4", dhcpupperip); 988 m->IPv4DhcpServerUpperIp = szTmpIp; 989 990 LogFunc(("network:%RTnaipv4, dhcpserver:%RTnaipv4, dhcplowerip:%RTnaipv4, dhcpupperip:%RTnaipv4\n", 991 network, dhcpserver, dhcplowerip, dhcpupperip)); 992 } 993 1018 994 /* we need IPv4NetworkMask for NAT's gw service start */ 1019 995 netmask.u = RT_H2N_U32(netmask.u); 1020 RTStrPrintf(aszNetmask, 16, "%RTnaipv4", netmask); 1021 m->IPv4NetworkMask = aszNetmask; 996 RTStrPrintf(szTmpIp, 16, "%RTnaipv4", netmask); 997 m->IPv4NetworkMask = szTmpIp; 998 1022 999 LogFlowFunc(("getaway:%RTnaipv4, netmask:%RTnaipv4\n", gateway, netmask)); 1023 1000 return VINF_SUCCESS; 1024 1001 } 1002
Note:
See TracChangeset
for help on using the changeset viewer.