int VBoxNetDhcp::hostDnsServers(const ComHostPtr& host, const RTNETADDRIPV4& networkid, const AddressToOffsetMapping& mapping, AddressList& servers) { ComBstrArray strs; HRESULT hrc = host->COMGETTER(NameServers)(ComSafeArrayAsOutParam(strs)); if (FAILED(hrc)) return VERR_NOT_FOUND; /* * Recent fashion is to run dnsmasq on 127.0.1.1 which we * currently can't map. If that's the only nameserver we've got, * we need to use DNS proxy for VMs to reach it. */ bool fUnmappedLoopback = false; for (size_t i = 0; i < strs.size(); ++i) { RTNETADDRIPV4 addr; int rc; rc = RTNetStrToIPv4Addr(com::Utf8Str(strs[i]).c_str(), &addr); if (RT_FAILURE(rc)) continue; if (addr.au8[0] == 127) { AddressToOffsetMapping::const_iterator remap(mapping.find(addr)); if (remap != mapping.end()) { int offset = remap->second; addr.u = RT_H2N_U32(RT_N2H_U32(networkid.u) + offset); } else { fUnmappedLoopback = true; continue; } } servers.push_back(addr); } if (servers.empty() && fUnmappedLoopback) { RTNETADDRIPV4 proxy; proxy.u = networkid.u | RT_H2N_U32_C(1U); servers.push_back(proxy); } return VINF_SUCCESS; }
/** * Main thread. Starts also the LWIP thread. */ int VBoxNetLwipNAT::init() { LogFlowFuncEnter(); /* virtualbox initialized in super class */ int rc = ::VBoxNetBaseService::init(); AssertRCReturn(rc, rc); std::string networkName = getNetworkName(); rc = findNatNetwork(virtualbox, networkName, m_net); AssertRCReturn(rc, rc); ComEventTypeArray aNetEvents; aNetEvents.push_back(VBoxEventType_OnNATNetworkPortForward); aNetEvents.push_back(VBoxEventType_OnNATNetworkSetting); rc = createNatListener(m_listener, virtualbox, this, aNetEvents); AssertRCReturn(rc, rc); // resolver changes are reported on vbox but are retrieved from // host so stash a pointer for future lookups HRESULT hrc = virtualbox->COMGETTER(Host)(m_host.asOutParam()); AssertComRCReturn(hrc, VERR_INTERNAL_ERROR); ComEventTypeArray aVBoxEvents; aVBoxEvents.push_back(VBoxEventType_OnHostNameResolutionConfigurationChange); aVBoxEvents.push_back(VBoxEventType_OnNATNetworkStartStop); rc = createNatListener(m_vboxListener, virtualbox, this, aVBoxEvents); AssertRCReturn(rc, rc); BOOL fIPv6Enabled = FALSE; hrc = m_net->COMGETTER(IPv6Enabled)(&fIPv6Enabled); AssertComRCReturn(hrc, VERR_NOT_FOUND); BOOL fIPv6DefaultRoute = FALSE; if (fIPv6Enabled) { hrc = m_net->COMGETTER(AdvertiseDefaultIPv6RouteEnabled)(&fIPv6DefaultRoute); AssertComRCReturn(hrc, VERR_NOT_FOUND); } m_ProxyOptions.ipv6_enabled = fIPv6Enabled; m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute; com::Bstr bstrSourceIp4Key = com::BstrFmt("NAT/%s/SourceIp4", networkName.c_str()); com::Bstr bstrSourceIpX; hrc = virtualbox->GetExtraData(bstrSourceIp4Key.raw(), bstrSourceIpX.asOutParam()); if (SUCCEEDED(hrc)) { RTNETADDRIPV4 addr; rc = RTNetStrToIPv4Addr(com::Utf8Str(bstrSourceIpX).c_str(), &addr); if (RT_SUCCESS(rc)) { RT_ZERO(m_src4); m_src4.sin_addr.s_addr = addr.u; m_ProxyOptions.src4 = &m_src4; bstrSourceIpX.setNull(); } } if (!fDontLoadRulesOnStartup) { fetchNatPortForwardRules(m_net, false, m_vecPortForwardRule4); fetchNatPortForwardRules(m_net, true, m_vecPortForwardRule6); } /* if (!fDontLoadRulesOnStartup) */ AddressToOffsetMapping tmp; rc = localMappings(m_net, tmp); if (RT_SUCCESS(rc) && tmp.size() != 0) { unsigned long i = 0; for (AddressToOffsetMapping::iterator it = tmp.begin(); it != tmp.end() && i < RT_ELEMENTS(m_lo2off); ++it, ++i) { ip4_addr_set_u32(&m_lo2off[i].loaddr, it->first.u); m_lo2off[i].off = it->second; } m_loOptDescriptor.lomap = m_lo2off; m_loOptDescriptor.num_lomap = i; m_ProxyOptions.lomap_desc = &m_loOptDescriptor; } com::Bstr bstr; hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam()); AssertComRCReturn(hrc, VERR_NOT_FOUND); if (!bstr.isEmpty()) { com::Utf8Str strTftpRoot(com::Utf8StrFmt("%ls%c%s", bstr.raw(), RTPATH_DELIMITER, "TFTP")); char *pszStrTemp; // avoid const char ** vs char ** rc = RTStrUtf8ToCurrentCP(&pszStrTemp, strTftpRoot.c_str()); AssertRC(rc); m_ProxyOptions.tftp_root = pszStrTemp; } m_ProxyOptions.nameservers = getHostNameservers(); /* end of COM initialization */ rc = g_pLwipNat->tryGoOnline(); if (RT_FAILURE(rc)) return rc; /* this starts LWIP thread */ vboxLwipCoreInitialize(VBoxNetLwipNAT::onLwipTcpIpInit, this); LogFlowFuncLeaveRC(rc); return rc; }