VirtualBox

Changeset 49328 in vbox for trunk/src/VBox/NetworkServices


Ignore:
Timestamp:
Oct 30, 2013 5:14:53 AM (11 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
90314
Message:

NetServices/DHCP: XML lease serialization/deserialization.

Location:
trunk/src/VBox/NetworkServices/DHCP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/DHCP/Config.cpp

    r49327 r49328  
    166166    std::string          m_domainName;
    167167    VecClient            m_clients;
    168    
     168    std::string          m_leaseStorageFilename;
    169169    bool                 fFileExists;
    170170};
     
    173173{
    174174    if (!g_ConfigurationManager)
     175
     176
    175177    {
    176178        g_ConfigurationManager = new ConfigurationManager();
     
    179181
    180182    return g_ConfigurationManager;
     183}
     184
     185
     186const std::string tagXMLLeases = "Leases";
     187const std::string tagXMLLeasesAttributeVersion = "version";
     188const std::string tagXMLLeasesVersion_1_0 = "1.0";
     189const std::string tagXMLLease = "Lease";
     190const std::string tagXMLLeaseAttributeMac = "mac";
     191const std::string tagXMLLeaseAttributeNetwork = "network";
     192const std::string tagXMLLeaseAddress = "Address";
     193const std::string tagXMLAddressAttributeValue = "value";
     194const std::string tagXMLLeaseTime = "Time";
     195const std::string tagXMLTimeAttributeIssued = "issued";
     196const std::string tagXMLTimeAttributeExpiration = "expiration";
     197const std::string tagXMLLeaseOptions = "Options";
     198
     199/**
     200 * <Leases version="1.0">
     201 *   <Lease mac="" network=""/>
     202 *    <Address value=""/>
     203 *    <Time issued="" expiration=""/>
     204 *    <options>
     205 *      <option name="" type=""/>
     206 *      </option>
     207 *    </options>
     208 *   </Lease>
     209 * </Leases>
     210 */
     211int ConfigurationManager::loadFromFile(const std::string& leaseStorageFileName)
     212{
     213    m->m_leaseStorageFilename = leaseStorageFileName;
     214   
     215    xml::XmlFileParser parser;
     216    xml::Document doc;
     217
     218    try {
     219        parser.read(m->m_leaseStorageFilename.c_str(), doc);
     220    }
     221    catch (...)
     222    {
     223        return VINF_SUCCESS;
     224    }
     225
     226    /* XML parsing */
     227    xml::ElementNode *root = doc.getRootElement();
     228
     229    if (!root || !root->nameEquals(tagXMLLeases.c_str()))
     230    {
     231        m->fFileExists = false;
     232        return VERR_NOT_FOUND;
     233    }
     234
     235    com::Utf8Str version;
     236    if (root)
     237        root->getAttributeValue(tagXMLLeasesAttributeVersion.c_str(), version);
     238
     239    /* XXX: version check */
     240    xml::NodesLoop leases(*root);
     241
     242    bool valueExists;
     243    const xml::ElementNode *lease;
     244    while ((lease = leases.forAllNodes()))
     245    {
     246        if (!lease->nameEquals(tagXMLLease.c_str()))
     247            continue;
     248       
     249        ClientData *data = new ClientData();
     250        Lease l(data);
     251        if (l.fromXML(lease))
     252        {
     253
     254            m->m_allocations.insert(MapLease2Ip4AddressPair(l, l.getAddress()));
     255
     256
     257            NetworkConfigEntity *pNetCfg = NULL;
     258            Client c(data);
     259            int rc = g_RootConfig->match(c, (BaseConfigEntity **)&pNetCfg);
     260            Assert(rc >= 0 && pNetCfg);
     261
     262            l.setConfig(pNetCfg);
     263
     264            m->m_clients.push_back(c);
     265        }
     266    }
     267
     268    return VINF_SUCCESS;
     269}
     270
     271
     272int ConfigurationManager::saveToFile()
     273{
     274    if (m->m_leaseStorageFilename.empty())
     275        return VINF_SUCCESS;
     276
     277    xml::Document doc;
     278
     279    xml::ElementNode *root = doc.createRootElement(tagXMLLeases.c_str());
     280    if (!root)
     281        return VERR_INTERNAL_ERROR;
     282   
     283    root->setAttribute(tagXMLLeasesAttributeVersion.c_str(), tagXMLLeasesVersion_1_0.c_str());
     284
     285    for(MapLease2Ip4AddressConstIterator it = m->m_allocations.begin();
     286        it != m->m_allocations.end(); ++it)
     287    {
     288        xml::ElementNode *lease = root->createChild(tagXMLLease.c_str());
     289        if (!it->first.toXML(lease))
     290        {
     291            /* XXX: todo logging + error handling */
     292        }
     293    }
     294
     295    try {
     296        xml::XmlFileWriter writer(doc);
     297        writer.write(m->m_leaseStorageFilename.c_str(), true);
     298    } catch(...){}
     299
     300    return VINF_SUCCESS;
    181301}
    182302
     
    384504    l.phaseStart(RTTimeMilliTS());
    385505
     506    saveToFile();
     507
    386508    return VINF_SUCCESS;
    387509}
     510
    388511
    389512int ConfigurationManager::expireLease4Client(Client& client)
     
    10691192
    10701193
     1194bool Lease::toXML(xml::ElementNode *node) const
     1195{
     1196    bool valueAddition = node->setAttribute(tagXMLLeaseAttributeMac.c_str(), com::Utf8StrFmt("%RTmac", &m->m_mac));
     1197    if (!valueAddition) return false;
     1198
     1199    valueAddition = node->setAttribute(tagXMLLeaseAttributeNetwork.c_str(), com::Utf8StrFmt("%RTnaipv4", m->m_network));
     1200    if (!valueAddition) return false;
     1201
     1202    xml::ElementNode *address = node->createChild(tagXMLLeaseAddress.c_str());
     1203    if (!address) return false;
     1204
     1205    valueAddition = address->setAttribute(tagXMLAddressAttributeValue.c_str(), com::Utf8StrFmt("%RTnaipv4", m->m_address));
     1206    if (!valueAddition) return false;
     1207
     1208    xml::ElementNode *time = node->createChild(tagXMLLeaseTime.c_str());
     1209    if (!time) return false;
     1210
     1211    valueAddition = time->setAttribute(tagXMLTimeAttributeIssued.c_str(),
     1212                                       m->u64TimestampLeasingStarted);
     1213    if (!valueAddition) return false;
     1214
     1215    valueAddition = time->setAttribute(tagXMLTimeAttributeExpiration.c_str(),
     1216                                       m->u32LeaseExpirationPeriod);
     1217    if (!valueAddition) return false;
     1218
     1219    return true;
     1220}
     1221
     1222
     1223bool Lease::fromXML(const xml::ElementNode *node)
     1224{
     1225    com::Utf8Str mac;
     1226    bool valueExists = node->getAttributeValue(tagXMLLeaseAttributeMac.c_str(), mac);
     1227    if (!valueExists) return false;
     1228    int rc = RTNetStrToMacAddr(mac.c_str(), &m->m_mac);
     1229    if (RT_FAILURE(rc)) return false;
     1230
     1231    com::Utf8Str network;
     1232    valueExists = node->getAttributeValue(tagXMLLeaseAttributeNetwork.c_str(), network);
     1233    if (!valueExists) return false;
     1234    rc = RTNetStrToIPv4Addr(network.c_str(), &m->m_network);
     1235    if (RT_FAILURE(rc)) return false;
     1236
     1237    /* Address */
     1238    const xml::ElementNode *address = node->findChildElement(tagXMLLeaseAddress.c_str());
     1239    if (!address) return false;
     1240    com::Utf8Str addressValue;
     1241    valueExists = address->getAttributeValue(tagXMLAddressAttributeValue.c_str(), addressValue);
     1242    if (!valueExists) return false;
     1243    rc = RTNetStrToIPv4Addr(addressValue.c_str(), &m->m_address);
     1244   
     1245    /* Time */
     1246    const xml::ElementNode *time = node->findChildElement(tagXMLLeaseTime.c_str());
     1247    if (!time) return false;
     1248
     1249    valueExists = time->getAttributeValue(tagXMLTimeAttributeIssued.c_str(),
     1250                                          &m->u64TimestampLeasingStarted);
     1251    if (!valueExists) return false;
     1252    m->fBinding = false;
     1253   
     1254    valueExists = time->getAttributeValue(tagXMLTimeAttributeExpiration.c_str(),
     1255                                          &m->u32LeaseExpirationPeriod);
     1256    if (!valueExists) return false;
     1257
     1258    m->fHasLease = true;
     1259    return true;
     1260}
     1261
     1262
    10711263const Lease Lease::NullLease;
    10721264
  • trunk/src/VBox/NetworkServices/DHCP/Config.h

    r49327 r49328  
    126126    MapOptionId2RawOption& options();
    127127
     128    bool toXML(xml::ElementNode *) const;
     129    bool fromXML(const xml::ElementNode *);
    128130
    129131    public:
     
    421423    static int extractRequestList(PCRTNETBOOTP pDhcpMsg, size_t cbDhcpMsg, RawOption& rawOpt);
    422424
     425    int loadFromFile(const std::string&);
     426    int saveToFile();
    423427    /**
    424428     *
  • trunk/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp

    r49327 r49328  
    524524                                                            UpperAddress);
    525525
     526        com::Bstr bstr;
     527        hrc = virtualbox->COMGETTER(HomeFolder)(bstr.asOutParam());
     528        std::string strXmlLeaseFile(com::Utf8StrFmt("%ls%c%s.leases",
     529                                                    bstr.raw(), RTPATH_DELIMITER, m_Network.c_str()).c_str());
     530        confManager->loadFromFile(strXmlLeaseFile);
     531
    526532    } /* if(m_fIgnoreCmdLineParameters) */
    527533    else
Note: See TracChangeset for help on using the changeset viewer.

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