Changeset 76153 in vbox for trunk/src/VBox/Main
- Timestamp:
- Dec 11, 2018 8:52:44 AM (6 years ago)
- svn:sync-xref-src-repo-rev:
- 127398
- Location:
- trunk/src/VBox/Main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/include/NATNetworkImpl.h
r76132 r76153 118 118 119 119 void i_updateDnsOptions(); 120 void i_updateDomainNameOption(ComPtr<IHost> &host); 121 void i_updateDomainNameServerOption(ComPtr<IHost> &host); 120 122 121 123 struct Data; -
trunk/src/VBox/Main/src-server/NATNetworkImpl.cpp
r76132 r76153 643 643 644 644 645 void NATNetwork::i_updateDnsOptions() 645 void NATNetwork::i_updateDomainNameOption(ComPtr<IHost> &host) 646 { 647 com::Bstr domain; 648 if (FAILED(host->COMGETTER(DomainName)(domain.asOutParam()))) 649 LogRel(("NATNetwork: Failed to get host's domain name\n")); 650 if (domain.isNotEmpty()) 651 { 652 HRESULT hrc = m->dhcpServer->AddGlobalOption(DhcpOpt_DomainName, domain.raw()); 653 if (FAILED(hrc)) 654 LogRel(("NATNetwork: Failed to add domain name option with %Rhrc\n", hrc)); 655 } 656 else 657 m->dhcpServer->RemoveGlobalOption(DhcpOpt_DomainName); 658 } 659 660 void NATNetwork::i_updateDomainNameServerOption(ComPtr<IHost> &host) 646 661 { 647 662 RTNETADDRIPV4 networkid, netmask; … … 649 664 int rc = RTCidrStrToIPv4(m->s.strIPv4NetworkCidr.c_str(), &networkid, &netmask); 650 665 if (RT_FAILURE(rc)) 666 { 667 LogRel(("NATNetwork: Failed to parse cidr %s with %Rrc\n", m->s.strIPv4NetworkCidr.c_str(), rc)); 651 668 return; 652 669 } 670 671 com::SafeArray<BSTR> nameServers; 672 HRESULT hrc = host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(nameServers)); 673 if (FAILED(hrc)) 674 { 675 LogRel(("NATNetwork: Failed to get name servers from host with %Rhrc\n", hrc)); 676 return; 677 } 678 679 size_t cAddresses = nameServers.size(); 680 if (cAddresses) 681 { 682 RTCList<RTCString> lstServers; 683 /* The following code was copied (and adapted a bit) from VBoxNetDhcp::hostDnsServers */ 684 /* 685 * Recent fashion is to run dnsmasq on 127.0.1.1 which we 686 * currently can't map. If that's the only nameserver we've got, 687 * we need to use DNS proxy for VMs to reach it. 688 */ 689 bool fUnmappedLoopback = false; 690 691 for (size_t i = 0; i < nameServers.size(); ++i) 692 { 693 RTNETADDRIPV4 addr; 694 695 com::Utf8Str strNameServerAddress(nameServers[i]); 696 rc = RTNetStrToIPv4Addr(strNameServerAddress.c_str(), &addr); 697 if (RT_FAILURE(rc)) 698 { 699 LogRel(("NATNetwork: Failed to parse IP address %s with %Rrc\n", strNameServerAddress.c_str(), rc)); 700 continue; 701 } 702 703 if (addr.u == INADDR_ANY) 704 { 705 /* 706 * This doesn't seem to be very well documented except for 707 * RTFS of res_init.c, but INADDR_ANY is a valid value for 708 * for "nameserver". 709 */ 710 addr.u = RT_H2N_U32_C(INADDR_LOOPBACK); 711 } 712 713 if (addr.au8[0] == 127) 714 { 715 settings::NATLoopbackOffsetList::const_iterator it; 716 717 it = std::find(m->s.llHostLoopbackOffsetList.begin(), 718 m->s.llHostLoopbackOffsetList.end(), 719 strNameServerAddress); 720 if (it == m->s.llHostLoopbackOffsetList.end()) 721 { 722 fUnmappedLoopback = true; 723 continue; 724 } 725 addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u) + it->u32Offset); 726 } 727 lstServers.append(RTCStringFmt("%RTnaipv4", addr)); 728 } 729 730 if (lstServers.isEmpty() && fUnmappedLoopback) 731 lstServers.append(RTCStringFmt("%RTnaipv4", networkid.u | RT_H2N_U32_C(1U))); /* proxy */ 732 733 hrc = m->dhcpServer->AddGlobalOption(DhcpOpt_DomainNameServer, Bstr(RTCString::join(lstServers, " ")).raw()); 734 if (FAILED(hrc)) 735 LogRel(("NATNetwork: Failed to add domain name server option '%s' with %Rhrc\n", RTCString::join(lstServers, " ").c_str(), hrc)); 736 } 737 else 738 m->dhcpServer->RemoveGlobalOption(DhcpOpt_DomainNameServer); 739 } 740 741 void NATNetwork::i_updateDnsOptions() 742 { 653 743 ComPtr<IHost> host; 654 744 if (SUCCEEDED(m->pVirtualBox->COMGETTER(Host)(host.asOutParam()))) 655 745 { 656 com::Bstr domain; 657 if (SUCCEEDED(host->COMGETTER(DomainName)(domain.asOutParam()))) 658 { 659 if (domain.isNotEmpty()) 660 m->dhcpServer->AddGlobalOption(DhcpOpt_DomainName, domain.raw()); 661 } 662 RTCList<RTCString> lstServers; 663 com::SafeArray<BSTR> nameServers; 664 if (SUCCEEDED(host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(nameServers)))) 665 { 666 size_t cAddresses = nameServers.size(); 667 if (cAddresses) 668 { 669 /* The following code was copied (and adapted a bit) from VBoxNetDhcp::hostDnsServers */ 670 /* 671 * Recent fashion is to run dnsmasq on 127.0.1.1 which we 672 * currently can't map. If that's the only nameserver we've got, 673 * we need to use DNS proxy for VMs to reach it. 674 */ 675 bool fUnmappedLoopback = false; 676 677 for (size_t i = 0; i < nameServers.size(); ++i) 678 { 679 RTNETADDRIPV4 addr; 680 681 com::Utf8Str strNameServerAddress(nameServers[i]); 682 rc = RTNetStrToIPv4Addr(strNameServerAddress.c_str(), &addr); 683 if (RT_FAILURE(rc)) 684 continue; 685 686 if (addr.u == INADDR_ANY) 687 { 688 /* 689 * This doesn't seem to be very well documented except for 690 * RTFS of res_init.c, but INADDR_ANY is a valid value for 691 * for "nameserver". 692 */ 693 addr.u = RT_H2N_U32_C(INADDR_LOOPBACK); 694 } 695 696 if (addr.au8[0] == 127) 697 { 698 settings::NATLoopbackOffsetList::const_iterator it; 699 for (it = m->s.llHostLoopbackOffsetList.begin(); 700 it != m->s.llHostLoopbackOffsetList.end(); ++it) 701 { 702 if (it->strLoopbackHostAddress == strNameServerAddress) 703 { 704 addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u) + it->u32Offset); 705 break; 706 } 707 } 708 if (it == m->s.llHostLoopbackOffsetList.end()) 709 { 710 fUnmappedLoopback = true; 711 continue; 712 } 713 } 714 lstServers.append(RTCStringFmt("%RTnaipv4", addr)); 715 } 716 717 if (lstServers.isEmpty() && fUnmappedLoopback) 718 lstServers.append(RTCStringFmt("%RTnaipv4", networkid.u | RT_H2N_U32_C(1U))); /* proxy */ 719 720 m->dhcpServer->AddGlobalOption(DhcpOpt_DomainNameServer, Bstr(RTCString::join(lstServers, " ")).raw()); 721 } 722 } 746 i_updateDomainNameOption(host); 747 i_updateDomainNameServerOption(host); 723 748 } 724 749 }
Note:
See TracChangeset
for help on using the changeset viewer.