VirtualBox

Ignore:
Timestamp:
Feb 5, 2021 3:48:44 AM (4 years ago)
Author:
vboxsync
Message:

NAT/Net: Get IPv4 settings from the API, not from the command line
(but for now cross check that they are the same). The command line
settings will be gone soon. bugref:9929.

Location:
trunk/src/VBox/NetworkServices/NAT
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r87618 r87620  
    177177    int homeInit();
    178178    int logInit();
     179    int ipv4Init();
    179180    int eventsInit();
    180181
     
    239240    LogFlowFuncEnter();
    240241
     242    RT_ZERO(m_ProxyOptions.ipv4_addr);
     243    RT_ZERO(m_ProxyOptions.ipv4_mask);
    241244    m_ProxyOptions.ipv6_enabled = 0;
    242245    m_ProxyOptions.ipv6_defroute = 0;
     
    362365    AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
    363366
     367    /*
     368     * Get the settings related to IPv4.
     369     */
     370    rc = ipv4Init();
     371    if (RT_FAILURE(rc))
     372        return rc;
     373
    364374
    365375    BOOL fIPv6Enabled = FALSE;
     
    377387    m_ProxyOptions.ipv6_defroute = fIPv6DefaultRoute;
    378388
    379 
    380     /*
    381      * IPv4 source address, if configured.
    382      */
    383     com::Utf8Str strSourceIp4;
    384     rc = getExtraData(strSourceIp4, "SourceIp4");
    385     if (RT_SUCCESS(rc) && strSourceIp4.isNotEmpty())
    386     {
    387         RTNETADDRIPV4 addr;
    388         rc = RTNetStrToIPv4Addr(strSourceIp4.c_str(), &addr);
    389         if (RT_SUCCESS(rc))
    390         {
    391             m_src4.sin_addr.s_addr = addr.u;
    392             m_ProxyOptions.src4 = &m_src4;
    393 
    394             LogRel(("Will use %RTnaipv4 as IPv4 source address\n",
    395                     m_src4.sin_addr.s_addr));
    396         }
    397         else
    398         {
    399             LogRel(("Failed to parse \"%s\" IPv4 source address specification\n",
    400                     strSourceIp4.c_str()));
    401         }
    402     }
    403389
    404390    /*
     
    431417
    432418
    433     createRawSock4();
    434419    if (fIPv6Enabled)
    435420        createRawSock6();
     
    578563
    579564
    580 /**
    581  * Create and register event listeners.
     565/*
     566 * Read IPv4 related settings and do necessary initialization.  These
     567 * settings will be picked up by the proxy on the lwIP thread.  See
     568 * onLwipTcpIpInit().
     569 */
     570int VBoxNetLwipNAT::ipv4Init()
     571{
     572    HRESULT hrc;
     573    int rc;
     574
     575    AssertReturn(m_net.isNotNull(), VERR_GENERAL_FAILURE);
     576
     577    /*
     578     * IPv4 address and mask.
     579     */
     580    com::Bstr bstrIPv4Prefix;
     581    hrc = m_net->COMGETTER(Network)(bstrIPv4Prefix.asOutParam());
     582    if (FAILED(hrc))
     583    {
     584        reportComError(m_net, "Network", hrc);
     585        return VERR_GENERAL_FAILURE;
     586    }
     587
     588    RTNETADDRIPV4 Net4, Mask4;
     589    int iPrefixLength;
     590    rc = RTNetStrToIPv4Cidr(com::Utf8Str(bstrIPv4Prefix).c_str(),
     591                            &Net4, &iPrefixLength);
     592    if (RT_FAILURE(rc))
     593    {
     594        reportError("Failed to parse IPv4 prefix %ls\n", bstrIPv4Prefix.raw());
     595        return rc;
     596    }
     597
     598    if (iPrefixLength > 30)
     599    {
     600        reportError("IPv4 prefix length %d is too narrow\n", iPrefixLength);
     601        return VERR_INVALID_PARAMETER;
     602    }
     603
     604    rc = RTNetPrefixToMaskIPv4(iPrefixLength, &Mask4);
     605    AssertRCReturn(rc, rc);
     606    AssertReturn(iPrefixLength > 0, VERR_INVALID_PARAMETER);
     607
     608    RTNETADDRIPV4 Addr4;
     609    Addr4.u = Net4.u | RT_H2N_U32_C(0x00000001);
     610
     611    /* Transitional: check that old and new ways agree */
     612    const RTNETADDRIPV4 &CmdLineAddr4 = getIpv4Address();
     613    AssertReturn(CmdLineAddr4.u == 0 || CmdLineAddr4.u == Addr4.u,
     614                 VERR_INVALID_PARAMETER);
     615
     616    const RTNETADDRIPV4 &CmdLineMask4 = getIpv4Netmask();
     617    AssertReturn(CmdLineMask4.u == 0 || CmdLineMask4.u == Mask4.u,
     618                 VERR_INVALID_PARAMETER);
     619
     620    memcpy(&m_ProxyOptions.ipv4_addr, &Addr4, sizeof(ip_addr));
     621    memcpy(&m_ProxyOptions.ipv4_mask, &Mask4, sizeof(ip_addr));
     622
     623
     624    /*
     625     * Raw socket for ICMP.
     626     */
     627    createRawSock4();
     628
     629
     630    /*
     631     * IPv4 source address (host), if configured.
     632     */
     633    com::Utf8Str strSourceIp4;
     634    rc = getExtraData(strSourceIp4, "SourceIp4");
     635    if (RT_SUCCESS(rc) && strSourceIp4.isNotEmpty())
     636    {
     637        RTNETADDRIPV4 addr;
     638        rc = RTNetStrToIPv4Addr(strSourceIp4.c_str(), &addr);
     639        if (RT_SUCCESS(rc))
     640        {
     641            m_src4.sin_addr.s_addr = addr.u;
     642            m_ProxyOptions.src4 = &m_src4;
     643
     644            LogRel(("Will use %RTnaipv4 as IPv4 source address\n",
     645                    m_src4.sin_addr.s_addr));
     646        }
     647        else
     648        {
     649            LogRel(("Failed to parse \"%s\" IPv4 source address specification\n",
     650                    strSourceIp4.c_str()));
     651        }
     652    }
     653
     654    return VINF_SUCCESS;
     655}
     656
     657
     658
     659/**
     660 * Create and register API event listeners.
    582661 */
    583662int VBoxNetLwipNAT::eventsInit()
     
    734813    proxy_ip6_divert_hook = pxremap_ip6_divert;
    735814
    736     /* lwip thread */
    737     RTNETADDRIPV4 network;
    738     RTNETADDRIPV4 address = self->getIpv4Address();
    739     RTNETADDRIPV4 netmask = self->getIpv4Netmask();
    740     network.u = address.u & netmask.u;
    741 
    742     ip_addr LwipIpAddr, LwipIpNetMask, LwipIpNetwork;
    743 
    744     memcpy(&LwipIpAddr, &address, sizeof(ip_addr));
    745     memcpy(&LwipIpNetMask, &netmask, sizeof(ip_addr));
    746     memcpy(&LwipIpNetwork, &network, sizeof(ip_addr));
    747 
    748815    netif *pNetif = netif_add(&self->m_LwipNetIf /* Lwip Interface */,
    749                               &LwipIpAddr /* IP address*/,
    750                               &LwipIpNetMask /* Network mask */,
    751                               &LwipIpAddr /* gateway address, @todo: is self IP acceptable? */,
     816                              &self->m_ProxyOptions.ipv4_addr, /* IP address*/
     817                              &self->m_ProxyOptions.ipv4_mask, /* Network mask */
     818                              &self->m_ProxyOptions.ipv4_addr, /* XXX: Gateway address */
    752819                              self /* state */,
    753820                              VBoxNetLwipNAT::netifInit /* netif_init_fn */,
  • trunk/src/VBox/NetworkServices/NAT/proxy.h

    r82968 r87620  
    5151
    5252struct proxy_options {
     53    ip_addr_t ipv4_addr;
     54    ip_addr_t ipv4_mask;
    5355    int ipv6_enabled;
    5456    int ipv6_defroute;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette