Changeset 49328 in vbox for trunk/src/VBox/NetworkServices
- Timestamp:
- Oct 30, 2013 5:14:53 AM (11 years ago)
- svn:sync-xref-src-repo-rev:
- 90314
- Location:
- trunk/src/VBox/NetworkServices/DHCP
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/NetworkServices/DHCP/Config.cpp
r49327 r49328 166 166 std::string m_domainName; 167 167 VecClient m_clients; 168 168 std::string m_leaseStorageFilename; 169 169 bool fFileExists; 170 170 }; … … 173 173 { 174 174 if (!g_ConfigurationManager) 175 176 175 177 { 176 178 g_ConfigurationManager = new ConfigurationManager(); … … 179 181 180 182 return g_ConfigurationManager; 183 } 184 185 186 const std::string tagXMLLeases = "Leases"; 187 const std::string tagXMLLeasesAttributeVersion = "version"; 188 const std::string tagXMLLeasesVersion_1_0 = "1.0"; 189 const std::string tagXMLLease = "Lease"; 190 const std::string tagXMLLeaseAttributeMac = "mac"; 191 const std::string tagXMLLeaseAttributeNetwork = "network"; 192 const std::string tagXMLLeaseAddress = "Address"; 193 const std::string tagXMLAddressAttributeValue = "value"; 194 const std::string tagXMLLeaseTime = "Time"; 195 const std::string tagXMLTimeAttributeIssued = "issued"; 196 const std::string tagXMLTimeAttributeExpiration = "expiration"; 197 const 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 */ 211 int 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 272 int 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; 181 301 } 182 302 … … 384 504 l.phaseStart(RTTimeMilliTS()); 385 505 506 saveToFile(); 507 386 508 return VINF_SUCCESS; 387 509 } 510 388 511 389 512 int ConfigurationManager::expireLease4Client(Client& client) … … 1069 1192 1070 1193 1194 bool 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 1223 bool 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 1071 1263 const Lease Lease::NullLease; 1072 1264 -
trunk/src/VBox/NetworkServices/DHCP/Config.h
r49327 r49328 126 126 MapOptionId2RawOption& options(); 127 127 128 bool toXML(xml::ElementNode *) const; 129 bool fromXML(const xml::ElementNode *); 128 130 129 131 public: … … 421 423 static int extractRequestList(PCRTNETBOOTP pDhcpMsg, size_t cbDhcpMsg, RawOption& rawOpt); 422 424 425 int loadFromFile(const std::string&); 426 int saveToFile(); 423 427 /** 424 428 * -
trunk/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp
r49327 r49328 524 524 UpperAddress); 525 525 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 526 532 } /* if(m_fIgnoreCmdLineParameters) */ 527 533 else
Note:
See TracChangeset
for help on using the changeset viewer.