lease_cmds.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Copyright (C) 2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <lease_cmds.h>
  8. #include <config/command_mgr.h>
  9. #include <lease_parser.h>
  10. #include <cc/command_interpreter.h>
  11. #include <cc/data.h>
  12. #include <asiolink/io_address.h>
  13. #include <lease_cmds_log.h>
  14. #include <dhcpsrv/cfgmgr.h>
  15. #include <dhcpsrv/lease_mgr.h>
  16. #include <dhcpsrv/lease_mgr_factory.h>
  17. #include <dhcpsrv/subnet_id.h>
  18. #include <dhcp/duid.h>
  19. #include <util/encode/hex.h>
  20. #include <util/strutil.h>
  21. #include <exceptions/exceptions.h>
  22. #include <boost/bind.hpp>
  23. #include <string>
  24. using namespace isc::dhcp;
  25. using namespace isc::data;
  26. using namespace isc::config;
  27. using namespace isc::asiolink;
  28. using namespace std;
  29. namespace isc {
  30. namespace lease_cmds {
  31. /// @brief Wrapper class around reservation command handlers.
  32. class LeaseCmdsImpl {
  33. public:
  34. LeaseCmdsImpl();
  35. ~LeaseCmdsImpl();
  36. /// @brief Parameters specified for reservation-get and reservation-del
  37. ///
  38. /// As both call types (get and delete) need specify which reservation to
  39. /// act on, they have the same set of parameters. In particular, those
  40. /// two call types support the following sets of parameters:
  41. /// - address
  42. /// - subnet-id, identifier-type, identifier-value (v4)
  43. /// - subnet-id, lease-type, iaid, identifier-type, identifier-value (v6)
  44. ///
  45. /// This class stores those parameters and is used to pass them around.
  46. class Parameters {
  47. public:
  48. /// @brief specifies type of query (by IP addr, by hwaddr, by DUID)
  49. typedef enum {
  50. TYPE_ADDR, ///< query by IP address (either v4 or v6)
  51. TYPE_HWADDR, ///< query by hardware address (v4 only)
  52. TYPE_DUID ///< query by DUID (v6 only)
  53. } Type;
  54. /// @brief Specifies subnet-id (always used)
  55. SubnetID subnet_id;
  56. /// @brief Specifies IPv4/v6 address (used when query_type is TYPE_ADDR)
  57. IOAddress addr;
  58. /// @brief Specifies hardware address (used when query_type is TYPE_HWADDR)
  59. HWAddrPtr hwaddr;
  60. /// @brief Specifies identifier value (used when query_type is TYPE_DUID)
  61. isc::dhcp::DuidPtr duid;
  62. /// @brief Attempts to covert text to one of specified types
  63. ///
  64. /// Supported values are: "address", hw-address and duid.
  65. ///
  66. /// @param txt text to be converted
  67. /// @return value converted to Parameters::Type
  68. /// @throw BadValue if unsupported type is specified
  69. static Type txtToType(const std::string& txt) {
  70. if (txt == "address") {
  71. return (Parameters::TYPE_ADDR);
  72. } else if (txt == "hw-address") {
  73. return (Parameters::TYPE_HWADDR);
  74. } else if (txt == "duid") {
  75. return (Parameters::TYPE_DUID);
  76. } else {
  77. isc_throw(BadValue, "Incorrect identifier type: "
  78. << txt << ", the only supported values are: "
  79. "address, hw-address, duid");
  80. }
  81. }
  82. /// @brief specifies parameter types (true = query by address, false =
  83. /// query by identifier-type,identifier)
  84. Type query_type;
  85. /// @brief Lease type (NA,TA or PD) used for v6 leases
  86. Lease::Type lease_type;
  87. /// @brief IAID identifier used for v6 leases
  88. uint32_t iaid;
  89. /// @brief Default constructor.
  90. Parameters()
  91. :addr("::"), query_type(TYPE_ADDR), lease_type(Lease::TYPE_NA),
  92. iaid(0) {
  93. }
  94. };
  95. private:
  96. /// @brief Registers commands:
  97. ///
  98. /// Registers:
  99. /// - lease4-add
  100. /// - lease6-add
  101. /// - lease4-get
  102. /// - lease6-get
  103. /// - lease4-del
  104. /// - lease6-del
  105. /// - lease4-update
  106. /// - lease6-update
  107. /// - lease4-del-all
  108. /// - lease6-del-all
  109. /// @throw Unexpected if CommandMgr is not available (should not happen)
  110. void registerCommands();
  111. /// @brief Deregisters commands:
  112. ///
  113. /// Deregisters:
  114. /// - lease4-add
  115. /// - lease6-add
  116. /// - lease4-get
  117. /// - lease6-get
  118. /// - lease4-del
  119. /// - lease6-del
  120. /// - lease4-update
  121. /// - lease6-update
  122. /// - lease4-del-all
  123. /// - lease6-del-all
  124. ///
  125. /// @throw Unexpected if CommandMgr is not available (should not happen)
  126. void deregisterCommands();
  127. /// @brief lease4-add, lease6-add command handler
  128. ///
  129. /// This command attempts to add a lease.
  130. ///
  131. /// An example full command looks as follows. Note that the args
  132. /// parameter is expected to contain the "arguments" portion of it.
  133. /// This function covers both v4 and v6 leases.
  134. ///
  135. /// Example command for v4:
  136. /// {
  137. /// "command": "lease4-add",
  138. /// "parameters": {
  139. /// "address": "192.0.2.1",
  140. /// "hwaddr": "00:01:02:03:04:05",
  141. /// "client-id": "this-is-a-client",
  142. /// "valid-lft": 3600,
  143. /// "expire": 12345678,
  144. /// "subnet-id": 1,
  145. /// "fqdn-fwd": true,
  146. /// "fqdn-rev": true,
  147. /// "hostname": "myhost.example.org",
  148. /// "state": 0
  149. /// }
  150. /// }
  151. /// Example command for v6:
  152. /// {
  153. /// "command": "lease6-add",
  154. /// "arguments": {
  155. /// "subnet-id": 66,
  156. /// "ip-address": "2001:db8:abcd::",
  157. /// "type": "IA_PD",
  158. /// "prefix-len": 48,
  159. /// "duid": "01:02:03:04:05:06:07:08",
  160. /// "iaid": 1234,
  161. /// "preferred-lft": 500,
  162. /// "valid-lft": 1000,
  163. /// "expire": 12345678,
  164. /// "fqdn-fwd": true,
  165. /// "fqdn-rev": true,
  166. /// "hostname": "urania.example.org""
  167. /// }
  168. /// }
  169. ///
  170. /// @param command should be 'lease4-add' or 'lease6-add'
  171. /// @param args must contain host reservation definition.
  172. /// @return result of the operation
  173. static ConstElementPtr
  174. leaseAddHandler(const string& command, ConstElementPtr args);
  175. /// @brief lease4-get, lease6-get command handler
  176. ///
  177. /// This command attempts to retrieve a lease that match selected criteria.
  178. /// The following types of parameters are supported:
  179. /// - (subnet-id, address) for both v4 and v6
  180. /// - (subnet-id, identifier-type, identifier) for v4
  181. /// - (subnet-id, type, iana, identifier-type, identifier) for v6
  182. ///
  183. /// Example command for query by (subnet-id, address):
  184. /// {
  185. /// "command": "lease4-get",
  186. /// "arguments": {
  187. /// "subnet-id": 1,
  188. /// "ip-address": "192.0.2.202"
  189. /// }
  190. /// }
  191. ///
  192. /// Example command for query by (subnet-id, identifier-type, identifier)
  193. /// {
  194. /// "command": "lease4-get",
  195. /// "arguments": {
  196. /// "subnet-id": 1,
  197. /// "identifier-type": "hw-address",
  198. /// "identifier": "00:01:02:03:04:05"
  199. /// }
  200. /// }
  201. ///
  202. /// Example command for query by (subnet-id, type, iana, identifier-type,
  203. /// identifier):
  204. /// {
  205. /// "command": "lease6-get",
  206. /// "arguments": {
  207. /// "subnet-id": 66,
  208. /// "iaid": 42,
  209. /// "type": "IA_NA",
  210. /// "identifier-type": "duid",
  211. /// "identifier": "77:77:77:77:77:77:77:77"
  212. /// }
  213. /// }
  214. /// @param command "lease4-get" or "lease6-get"
  215. /// @param args must contain host reservation definition.
  216. /// @return result of the operation (includes lease details, if found)
  217. static ConstElementPtr
  218. leaseGetHandler(const string& command, ConstElementPtr args);
  219. /// @brief lease4-del command handler
  220. ///
  221. /// This command attempts to delete an IPv4 lease that match selected
  222. /// criteria. Two types of parameters are supported: (subnet-id, address) or
  223. /// (subnet-id, identifier-type, identifier).
  224. ///
  225. /// Example command for deletion by (subnet-id, address):
  226. /// {
  227. /// "command": "lease4-del",
  228. /// "arguments": {
  229. /// "subnet-id": 1,
  230. /// "ip-address": "192.0.2.202"
  231. /// }
  232. /// }
  233. ///
  234. /// Example command for deletion by (subnet-id, identifier-type, identifier)
  235. /// {
  236. /// "command": "lease4-del",
  237. /// "arguments": {
  238. /// "subnet-id": 1,
  239. /// "identifier-type": "hw-address",
  240. /// "identifier": "00:01:02:03:04:05"
  241. /// }
  242. /// }";
  243. /// @param command should be 'lease4-del' (but it's ignored)
  244. /// @param args must contain host reservation definition.
  245. /// @return result of the operation (host will be included as parameters, if found)
  246. static ConstElementPtr
  247. lease4DelHandler(const string& command, ConstElementPtr args);
  248. /// @brief lease6-del command handler
  249. ///
  250. /// This command attempts to delete a lease that match selected criteria.
  251. /// Two types of parameters are supported: (subnet-id, address) or
  252. /// (subnet-id, type, iaid, identifier-type, identifier).
  253. ///
  254. /// Example command for deletion by (subnet-id, address):
  255. /// {
  256. /// "command": "lease6-del",
  257. /// "arguments": {
  258. /// "subnet-id": 1,
  259. /// "ip-address": "192.0.2.202"
  260. /// }
  261. /// }
  262. ///
  263. /// Example command for deletion by (subnet-id, type, iaid, identifier-type,
  264. /// identifier):
  265. /// {
  266. /// "command": "lease6-del",
  267. /// "arguments": {
  268. /// "subnet-id": 1,
  269. /// "type": "IA_NA",
  270. /// "iaid": 123456,
  271. /// "identifier-type": "hw-address",
  272. /// "identifier": "00:01:02:03:04:05"
  273. /// }
  274. /// }";
  275. /// @param command should be 'lease6-del' (but it's ignored)
  276. /// @param args must contain host reservation definition.
  277. /// @return result of the operation (host will be included as parameters, if found)
  278. static ConstElementPtr
  279. lease6DelHandler(const string& command, ConstElementPtr args);
  280. static ConstElementPtr
  281. lease4UpdateHandler(const string& command, ConstElementPtr args);
  282. /// @brief Not implemented yet.
  283. static ConstElementPtr
  284. lease6UpdateHandler(const string& command, ConstElementPtr args);
  285. static ConstElementPtr
  286. lease4WipeHandler(const string& command, ConstElementPtr args);
  287. static ConstElementPtr
  288. lease6WipeHandler(const string& command, ConstElementPtr args);
  289. /// @brief Extracts parameters required for reservation-get and reservation-del
  290. ///
  291. /// See @ref Parameters class for detailed description of what is expected
  292. /// in the args structure.
  293. ///
  294. /// @param args - arguments passed to command
  295. /// @return parsed parameters
  296. /// @throw BadValue if input arguments don't make sense.
  297. static Parameters getParameters(const ConstElementPtr& args);
  298. };
  299. LeaseCmdsImpl::LeaseCmdsImpl() {
  300. registerCommands();
  301. }
  302. LeaseCmdsImpl::~LeaseCmdsImpl() {
  303. deregisterCommands();
  304. }
  305. void LeaseCmdsImpl::registerCommands() {
  306. /// @todo: Use registration mechanism once #5314 is merged.
  307. /// See #5321 discussion.
  308. CommandMgr::instance().registerCommand("lease4-add",
  309. boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));
  310. CommandMgr::instance().registerCommand("lease6-add",
  311. boost::bind(&LeaseCmdsImpl::leaseAddHandler, _1, _2));
  312. CommandMgr::instance().registerCommand("lease4-get",
  313. boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));
  314. CommandMgr::instance().registerCommand("lease6-get",
  315. boost::bind(&LeaseCmdsImpl::leaseGetHandler, _1, _2));
  316. CommandMgr::instance().registerCommand("lease4-del",
  317. boost::bind(&LeaseCmdsImpl::lease4DelHandler, _1, _2));
  318. CommandMgr::instance().registerCommand("lease6-del",
  319. boost::bind(&LeaseCmdsImpl::lease6DelHandler, _1, _2));
  320. CommandMgr::instance().registerCommand("lease4-update",
  321. boost::bind(&LeaseCmdsImpl::lease4UpdateHandler, _1, _2));
  322. CommandMgr::instance().registerCommand("lease6-update",
  323. boost::bind(&LeaseCmdsImpl::lease6UpdateHandler, _1, _2));
  324. CommandMgr::instance().registerCommand("lease4-del-all",
  325. boost::bind(&LeaseCmdsImpl::lease4WipeHandler, _1, _2));
  326. CommandMgr::instance().registerCommand("lease6-del-all",
  327. boost::bind(&LeaseCmdsImpl::lease6WipeHandler, _1, _2));
  328. }
  329. void LeaseCmdsImpl::deregisterCommands() {
  330. /// @todo: Use deregistration mechanism once #5321 discussion is done
  331. CommandMgr::instance().deregisterCommand("lease4-add");
  332. CommandMgr::instance().deregisterCommand("lease6-add");
  333. CommandMgr::instance().deregisterCommand("lease4-get");
  334. CommandMgr::instance().deregisterCommand("lease6-get");
  335. CommandMgr::instance().deregisterCommand("lease4-del");
  336. CommandMgr::instance().deregisterCommand("lease6-del");
  337. CommandMgr::instance().deregisterCommand("lease4-update");
  338. CommandMgr::instance().deregisterCommand("lease6-update");
  339. CommandMgr::instance().deregisterCommand("lease4-del-all");
  340. CommandMgr::instance().deregisterCommand("lease6-del-all");
  341. }
  342. ConstElementPtr
  343. LeaseCmdsImpl::leaseAddHandler(const std::string& name,
  344. ConstElementPtr params) {
  345. bool v4 = (name == "lease4-add");
  346. string txt = "(missing parameters)";
  347. if (params) {
  348. txt = params->str();
  349. }
  350. try {
  351. if (!params) {
  352. isc_throw(isc::BadValue, "no parameters specified for the command");
  353. }
  354. ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
  355. Lease4Ptr lease4;
  356. Lease6Ptr lease6;
  357. if (v4) {
  358. Lease4Parser parser;
  359. lease4 = parser.parse(config, params);
  360. // checkLeaseIntegrity(config, lease4);
  361. if (lease4) {
  362. LeaseMgrFactory::instance().addLease(lease4);
  363. }
  364. } else {
  365. Lease6Parser parser;
  366. lease6 = parser.parse(config, params);
  367. // checkLeaseIntegrity(config, lease6);
  368. if (lease6) {
  369. LeaseMgrFactory::instance().addLease(lease6);
  370. }
  371. }
  372. } catch (const std::exception& ex) {
  373. LOG_ERROR(lease_cmds_logger, v4 ? LEASE_CMDS_ADD4_FAILED : LEASE_CMDS_ADD6_FAILED)
  374. .arg(txt)
  375. .arg(ex.what());
  376. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  377. }
  378. LOG_INFO(lease_cmds_logger,
  379. v4 ? LEASE_CMDS_ADD4 : LEASE_CMDS_ADD6).arg(txt);
  380. return (createAnswer(CONTROL_RESULT_SUCCESS, "Lease added."));
  381. }
  382. LeaseCmdsImpl::Parameters
  383. LeaseCmdsImpl::getParameters(const ConstElementPtr& params) {
  384. Parameters x;
  385. if (!params || params->getType() != Element::map) {
  386. isc_throw(BadValue, "Parameters missing or are not a map.");
  387. }
  388. // We support several sets of parameters for leaseX-get/lease-del:
  389. // lease-get(type, address)
  390. // lease-get(type, subnet-id, identifier-type, identifier)
  391. if (params->contains("type")) {
  392. string t = params->get("type")->stringValue();
  393. if (t == "IA_NA" || t == "0") {
  394. x.lease_type = Lease::TYPE_NA;
  395. } else if (t == "IA_TA" || t == "1") {
  396. x.lease_type = Lease::TYPE_TA;
  397. } else if (t == "IA_PD" || t == "2") {
  398. x.lease_type = Lease::TYPE_PD;
  399. } else if (t == "V4" || t == "3") {
  400. x.lease_type = Lease::TYPE_V4;
  401. } else {
  402. isc_throw(BadValue, "Invalid lease type specified: "
  403. << t << ", only supported values are: IA_NA, IA_TA,"
  404. << " IA_PD and V4");
  405. }
  406. }
  407. ConstElementPtr tmp = params->get("ip-address");
  408. if (tmp) {
  409. if (tmp->getType() != Element::string) {
  410. isc_throw(BadValue, "'ip-address' is not a string.");
  411. }
  412. x.addr = IOAddress(tmp->stringValue());
  413. x.query_type = Parameters::TYPE_ADDR;
  414. return (x);
  415. }
  416. tmp = params->get("subnet-id");
  417. if (!tmp) {
  418. isc_throw(BadValue, "Mandatory 'subnet-id' parameter missing.");
  419. }
  420. if (tmp->getType() != Element::integer) {
  421. isc_throw(BadValue, "'subnet-id' parameter is not integer.");
  422. }
  423. x.subnet_id = tmp->intValue();
  424. if (params->contains("iaid")) {
  425. x.iaid = params->get("iaid")->intValue();
  426. }
  427. // No address specified. Ok, so it must be identifier based query.
  428. // "identifier-type": "duid",
  429. // "identifier": "aa:bb:cc:dd:ee:..."
  430. ConstElementPtr type = params->get("identifier-type");
  431. ConstElementPtr ident = params->get("identifier");
  432. if (!type || type->getType() != Element::string) {
  433. isc_throw(BadValue, "No 'ip-address' provided"
  434. " and 'identifier-type' is either missing or not a string.");
  435. }
  436. if (!ident || ident->getType() != Element::string) {
  437. isc_throw(BadValue, "No 'ip-address' provided"
  438. " and 'identifier' is either missing or not a string.");
  439. }
  440. // Got the parameters. Let's see if their values make sense.
  441. // Try to convert identifier-type
  442. x.query_type = Parameters::txtToType(type->stringValue());
  443. switch (x.query_type) {
  444. case Parameters::TYPE_HWADDR: {
  445. HWAddr hw = HWAddr::fromText(ident->stringValue());
  446. x.hwaddr = HWAddrPtr(new HWAddr(hw));
  447. break;
  448. }
  449. case Parameters::TYPE_DUID: {
  450. DUID duid = DUID::fromText(ident->stringValue());
  451. x.duid = DuidPtr(new DUID(duid));
  452. break;
  453. }
  454. case Parameters::TYPE_ADDR: {
  455. // We should never get here. The address clause should have been caught
  456. // earlier.
  457. return (x);
  458. }
  459. default: {
  460. isc_throw(BadValue, "Identifier type " << type->stringValue() <<
  461. " is not supported.");
  462. }
  463. }
  464. return (x);
  465. }
  466. ConstElementPtr
  467. LeaseCmdsImpl::leaseGetHandler(const std::string& name, ConstElementPtr params) {
  468. Parameters p;
  469. Lease4Ptr lease4;
  470. Lease6Ptr lease6;
  471. bool v4 = (name == "lease4-get");
  472. try {
  473. p = getParameters(params);
  474. switch (p.query_type) {
  475. case Parameters::TYPE_ADDR: {
  476. // Query by address
  477. if (v4) {
  478. lease4 = LeaseMgrFactory::instance().getLease4(p.addr);
  479. } else {
  480. lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, p.addr);
  481. }
  482. break;
  483. }
  484. case Parameters::TYPE_HWADDR:
  485. if (v4) {
  486. if (!p.hwaddr) {
  487. return (createAnswer(CONTROL_RESULT_ERROR,
  488. "Program error: Query by hw-address "
  489. "requires hwaddr to be specified"));
  490. }
  491. lease4 = LeaseMgrFactory::instance().getLease4(*p.hwaddr, p.subnet_id);
  492. } else {
  493. return (createAnswer(CONTROL_RESULT_ERROR,
  494. "Query by hw-address is not allowed in v6."));
  495. }
  496. break;
  497. case Parameters::TYPE_DUID:
  498. if (!v4) {
  499. if (!p.duid) {
  500. return (createAnswer(CONTROL_RESULT_ERROR,
  501. "Program error: Query by duid "
  502. "requires duid to be specified"));
  503. }
  504. lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, *p.duid,
  505. p.iaid, p.subnet_id);
  506. } else {
  507. return (createAnswer(CONTROL_RESULT_ERROR,
  508. "Query by duid is not allowed in v4."));
  509. }
  510. break;
  511. default: {
  512. stringstream tmp;
  513. tmp << "Unknown query type: " << static_cast<int>(p.query_type);
  514. return (createAnswer(CONTROL_RESULT_ERROR, tmp.str()));
  515. }
  516. }
  517. } catch (const std::exception& ex) {
  518. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  519. }
  520. ElementPtr lease_json;
  521. if (v4 && lease4) {
  522. lease_json = lease4->toElement();
  523. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv4 lease found.", lease_json));
  524. }
  525. if (!v4 && lease6) {
  526. lease_json = lease6->toElement();
  527. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv6 lease found.", lease_json));
  528. }
  529. // If we got here, the lease has not been found.
  530. return (createAnswer(CONTROL_RESULT_EMPTY, "Lease not found."));
  531. }
  532. ConstElementPtr
  533. LeaseCmdsImpl::lease4DelHandler(const std::string& , ConstElementPtr params) {
  534. Parameters p;
  535. Lease4Ptr lease4;
  536. IOAddress addr(IOAddress::IPV4_ZERO_ADDRESS());
  537. try {
  538. p = getParameters(params);
  539. switch (p.query_type) {
  540. case Parameters::TYPE_ADDR: {
  541. // If address was specified explicitly, let's use it as is.
  542. addr = p.addr;
  543. break;
  544. }
  545. case Parameters::TYPE_HWADDR:
  546. if (!p.hwaddr) {
  547. return (createAnswer(CONTROL_RESULT_ERROR,
  548. "Program error: Query by hw-address "
  549. "requires hwaddr to be specified"));
  550. }
  551. // Let's see if there's such a lease at all.
  552. lease4 = LeaseMgrFactory::instance().getLease4(*p.hwaddr, p.subnet_id);
  553. if (!lease4) {
  554. return (createAnswer(CONTROL_RESULT_EMPTY, "IPv4 lease not found."));
  555. }
  556. // Found it, can use it as is.
  557. addr = lease4->addr_;
  558. break;
  559. case Parameters::TYPE_DUID:
  560. return (createAnswer(CONTROL_RESULT_ERROR,
  561. "Delete by duid is not allowed in v4."));
  562. break;
  563. default: {
  564. stringstream tmp;
  565. tmp << "Unknown query type: " << static_cast<int>(p.query_type);
  566. return (createAnswer(CONTROL_RESULT_ERROR, tmp.str()));
  567. }
  568. }
  569. if (LeaseMgrFactory::instance().deleteLease(addr)) {
  570. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv4 lease deleted."));
  571. } else {
  572. return (createAnswer(CONTROL_RESULT_EMPTY, "IPv4 lease not found."));
  573. }
  574. } catch (const std::exception& ex) {
  575. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  576. }
  577. }
  578. ConstElementPtr
  579. LeaseCmdsImpl::lease6DelHandler(const std::string& , ConstElementPtr params) {
  580. Parameters p;
  581. Lease6Ptr lease6;
  582. IOAddress addr(IOAddress::IPV6_ZERO_ADDRESS());
  583. try {
  584. p = getParameters(params);
  585. switch (p.query_type) {
  586. case Parameters::TYPE_ADDR: {
  587. // If address was specified explicitly, let's use it as is.
  588. addr = p.addr;
  589. break;
  590. }
  591. case Parameters::TYPE_HWADDR:
  592. return (createAnswer(CONTROL_RESULT_ERROR,
  593. "Delete by hw-address is not allowed in v6."));
  594. case Parameters::TYPE_DUID:
  595. if (!p.duid) {
  596. return (createAnswer(CONTROL_RESULT_ERROR,
  597. "Program error: Query by duid "
  598. "requires duid to be specified"));
  599. }
  600. // Let's see if there's such a lease at all.
  601. lease6 = LeaseMgrFactory::instance().getLease6(p.lease_type, *p.duid,
  602. p.iaid, p.subnet_id);
  603. if (!lease6) {
  604. return (createAnswer(CONTROL_RESULT_EMPTY, "IPv6 lease not found."));
  605. }
  606. // Found it, can use it as is.
  607. addr = lease6->addr_;
  608. break;
  609. default: {
  610. stringstream tmp;
  611. tmp << "Unknown query type: " << static_cast<int>(p.query_type);
  612. return (createAnswer(CONTROL_RESULT_ERROR, tmp.str()));
  613. }
  614. }
  615. if (LeaseMgrFactory::instance().deleteLease(addr)) {
  616. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv6 lease deleted."));
  617. } else {
  618. return (createAnswer(CONTROL_RESULT_EMPTY, "IPv6 lease not found."));
  619. }
  620. } catch (const std::exception& ex) {
  621. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  622. }
  623. }
  624. ConstElementPtr
  625. LeaseCmdsImpl::lease4UpdateHandler(const string& , ConstElementPtr params) {
  626. try {
  627. // We need the lease to be specified.
  628. if (!params) {
  629. isc_throw(isc::BadValue, "no parameters specified for lease4-update command");
  630. }
  631. // Get the parameters specified by the user first.
  632. ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
  633. Lease4Ptr lease4;
  634. Lease4Parser parser;
  635. // The parser does sanity checks (if the address is in scope, if
  636. // subnet-id is valid, etc)
  637. lease4 = parser.parse(config, params);
  638. // Ok, now check if there is a lease to be updated.
  639. Lease4Ptr existing = LeaseMgrFactory::instance().getLease4(lease4->addr_);
  640. if (!existing) {
  641. stringstream tmp;
  642. tmp << "There is no lease for address " << lease4->addr_ << ", can't update.";
  643. return (createAnswer(CONTROL_RESULT_EMPTY, tmp.str()));
  644. }
  645. LeaseMgrFactory::instance().updateLease4(lease4);
  646. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv4 lease updated."));
  647. } catch (const std::exception& ex) {
  648. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  649. }
  650. }
  651. ConstElementPtr
  652. LeaseCmdsImpl::lease6UpdateHandler(const string& , ConstElementPtr params) {
  653. try {
  654. // We need the lease to be specified.
  655. if (!params) {
  656. isc_throw(isc::BadValue, "no parameters specified for lease6-update command");
  657. }
  658. // Get the parameters specified by the user first.
  659. ConstSrvConfigPtr config = CfgMgr::instance().getCurrentCfg();
  660. Lease6Ptr lease6;
  661. Lease6Parser parser;
  662. // The parser does sanity checks (if the address is in scope, if
  663. // subnet-id is valid, etc)
  664. lease6 = parser.parse(config, params);
  665. // Ok, now check if there is a lease to be updated.
  666. Lease6Ptr existing = LeaseMgrFactory::instance().getLease6(lease6->type_,
  667. lease6->addr_);
  668. if (!existing) {
  669. stringstream tmp;
  670. tmp << "There is no lease for address " << lease6->addr_ << ", can't update.";
  671. return (createAnswer(CONTROL_RESULT_EMPTY, tmp.str()));
  672. }
  673. LeaseMgrFactory::instance().updateLease6(lease6);
  674. return (createAnswer(CONTROL_RESULT_SUCCESS, "IPv6 lease updated."));
  675. } catch (const std::exception& ex) {
  676. return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
  677. }
  678. }
  679. ConstElementPtr
  680. LeaseCmdsImpl::lease4WipeHandler(const string& cmd, ConstElementPtr args) {
  681. return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
  682. }
  683. ConstElementPtr
  684. LeaseCmdsImpl::lease6WipeHandler(const string& cmd, ConstElementPtr args) {
  685. return (createAnswer(CONTROL_RESULT_ERROR, "not implemented yet."));
  686. }
  687. LeaseCmds::LeaseCmds()
  688. :impl_(new LeaseCmdsImpl()) {
  689. }
  690. LeaseCmds::~LeaseCmds() {
  691. impl_.reset();
  692. }
  693. };
  694. };