mysql_host_data_source.cc 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746
  1. // Copyright (C) 2015-2016 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 <dhcp/libdhcp++.h>
  8. #include <dhcp/option.h>
  9. #include <dhcp/option_definition.h>
  10. #include <dhcp/option_space.h>
  11. #include <dhcpsrv/cfg_option.h>
  12. #include <dhcpsrv/db_exceptions.h>
  13. #include <dhcpsrv/dhcpsrv_log.h>
  14. #include <dhcpsrv/mysql_host_data_source.h>
  15. #include <dhcpsrv/db_exceptions.h>
  16. #include <util/buffer.h>
  17. #include <util/optional_value.h>
  18. #include <boost/algorithm/string/split.hpp>
  19. #include <boost/algorithm/string/classification.hpp>
  20. #include <boost/array.hpp>
  21. #include <boost/pointer_cast.hpp>
  22. #include <boost/static_assert.hpp>
  23. #include <mysql.h>
  24. #include <mysqld_error.h>
  25. #include <stdint.h>
  26. #include <string>
  27. using namespace isc;
  28. using namespace isc::asiolink;
  29. using namespace isc::dhcp;
  30. using namespace isc::util;
  31. using namespace std;
  32. namespace {
  33. /// @brief Maximum size of an IPv6 address represented as a text string.
  34. ///
  35. /// This is 32 hexadecimal characters written in 8 groups of four, plus seven
  36. /// colon separators.
  37. const size_t ADDRESS6_TEXT_MAX_LEN = 39;
  38. /// @brief Maximum length of classes stored in a dhcp4/6_client_classes
  39. /// columns.
  40. const size_t CLIENT_CLASSES_MAX_LEN = 255;
  41. /// @brief Maximum length of the hostname stored in DNS.
  42. ///
  43. /// This length is restricted by the length of the domain-name carried
  44. /// in the Client FQDN %Option (see RFC4702 and RFC4704).
  45. const size_t HOSTNAME_MAX_LEN = 255;
  46. /// @brief Maximum length of option value.
  47. const size_t OPTION_VALUE_MAX_LEN = 4096;
  48. /// @brief Maximum length of option value specified in textual format.
  49. const size_t OPTION_FORMATTED_VALUE_MAX_LEN = 8192;
  50. /// @brief Maximum length of option space name.
  51. const size_t OPTION_SPACE_MAX_LEN = 128;
  52. /// @brief Maximum length of the server hostname.
  53. const size_t SERVER_HOSTNAME_MAX_LEN = 64;
  54. /// @brief Maximum length of the boot file name.
  55. const size_t BOOT_FILE_NAME_MAX_LEN = 128;
  56. /// @brief Numeric value representing last supported identifier.
  57. ///
  58. /// This value is used to validate whether the identifier type stored in
  59. /// a database is within bounds. of supported identifiers.
  60. const uint8_t MAX_IDENTIFIER_TYPE = static_cast<uint8_t>(Host::IDENT_CIRCUIT_ID);
  61. /// @brief This class provides mechanisms for sending and retrieving
  62. /// information from the 'hosts' table.
  63. ///
  64. /// This class is used to insert and retrieve entries from the 'hosts' table.
  65. /// The queries used with this class do not retrieve IPv6 reservations or
  66. /// options associated with a host to minimize impact on performance. Other
  67. /// classes derived from @ref MySqlHostExchange should be used to retrieve
  68. /// information about IPv6 reservations and options.
  69. class MySqlHostExchange {
  70. private:
  71. /// @brief Number of columns returned for SELECT queries send by this class.
  72. static const size_t HOST_COLUMNS = 12;
  73. public:
  74. /// @brief Constructor
  75. ///
  76. /// @param additional_columns_num This value is set by the derived classes
  77. /// to indicate how many additional columns will be returned by SELECT
  78. /// queries performed by the derived class. This constructor will allocate
  79. /// resources for these columns, e.g. binding table, error indicators.
  80. MySqlHostExchange(const size_t additional_columns_num = 0)
  81. : columns_num_(HOST_COLUMNS + additional_columns_num),
  82. bind_(columns_num_), columns_(columns_num_),
  83. error_(columns_num_, MLM_FALSE), host_id_(0),
  84. dhcp_identifier_length_(0), dhcp_identifier_type_(0),
  85. dhcp4_subnet_id_(0), dhcp6_subnet_id_(0), ipv4_address_(0),
  86. hostname_length_(0), dhcp4_client_classes_length_(0),
  87. dhcp6_client_classes_length_(0),
  88. dhcp4_next_server_(0),
  89. dhcp4_server_hostname_length_(0),
  90. dhcp4_boot_file_name_length_(0),
  91. dhcp4_subnet_id_null_(MLM_FALSE),
  92. dhcp6_subnet_id_null_(MLM_FALSE),
  93. ipv4_address_null_(MLM_FALSE), hostname_null_(MLM_FALSE),
  94. dhcp4_client_classes_null_(MLM_FALSE),
  95. dhcp6_client_classes_null_(MLM_FALSE),
  96. dhcp4_next_server_null_(MLM_FALSE),
  97. dhcp4_server_hostname_null_(MLM_FALSE),
  98. dhcp4_boot_file_name_null_(MLM_FALSE) {
  99. // Fill arrays with 0 so as they don't include any garbage.
  100. memset(dhcp_identifier_buffer_, 0, sizeof(dhcp_identifier_buffer_));
  101. memset(hostname_, 0, sizeof(hostname_));
  102. memset(dhcp4_client_classes_, 0, sizeof(dhcp4_client_classes_));
  103. memset(dhcp6_client_classes_, 0, sizeof(dhcp6_client_classes_));
  104. memset(dhcp4_server_hostname_, 0, sizeof(dhcp4_server_hostname_));
  105. memset(dhcp4_boot_file_name_, 0, sizeof(dhcp4_boot_file_name_));
  106. // Set the column names for use by this class. This only comprises
  107. // names used by the MySqlHostExchange class. Derived classes will
  108. // need to set names for the columns they use.
  109. columns_[0] = "host_id";
  110. columns_[1] = "dhcp_identifier";
  111. columns_[2] = "dhcp_identifier_type";
  112. columns_[3] = "dhcp4_subnet_id";
  113. columns_[4] = "dhcp6_subnet_id";
  114. columns_[5] = "ipv4_address";
  115. columns_[6] = "hostname";
  116. columns_[7] = "dhcp4_client_classes";
  117. columns_[8] = "dhcp6_client_classes";
  118. columns_[9] = "dhcp4_next_server";
  119. columns_[10] = "dhcp4_server_hostname";
  120. columns_[11] = "dhcp4_boot_file_name";
  121. BOOST_STATIC_ASSERT(11 < HOST_COLUMNS);
  122. };
  123. /// @brief Virtual destructor.
  124. virtual ~MySqlHostExchange() {
  125. }
  126. /// @brief Returns index of the first uninitialized column name.
  127. ///
  128. /// This method is called by the derived classes to determine which
  129. /// column indexes are available for the derived classes within a
  130. /// binding array, error array and column names. This method
  131. /// determines the first available index by searching the first
  132. /// empty value within the columns_ vector. Previously we relied on
  133. /// the fixed values set for each class, but this was hard to maintain
  134. /// when new columns were added to the SELECT queries. It required
  135. /// modifying indexes in all derived classes.
  136. ///
  137. /// Derived classes must call this method in their constructors and
  138. /// use returned value as an index for the first column used by the
  139. /// derived class and increment this value for each subsequent column.
  140. size_t findAvailColumn() const {
  141. std::vector<std::string>::const_iterator empty_column =
  142. std::find(columns_.begin(), columns_.end(), std::string());
  143. return (std::distance(columns_.begin(), empty_column));
  144. }
  145. /// @brief Returns value of host id.
  146. ///
  147. /// This method is used by derived classes.
  148. uint64_t getHostId() const {
  149. return (host_id_);
  150. };
  151. /// @brief Set error indicators
  152. ///
  153. /// Sets the error indicator for each of the MYSQL_BIND elements. It points
  154. /// the "error" field within an element of the input array to the
  155. /// corresponding element of the passed error array.
  156. ///
  157. /// @param bind Array of BIND elements
  158. /// @param error Array of error elements. If there is an error in getting
  159. /// data associated with one of the "bind" elements, the
  160. /// corresponding element in the error array is set to MLM_TRUE.
  161. static void setErrorIndicators(std::vector<MYSQL_BIND>& bind,
  162. std::vector<my_bool>& error) {
  163. for (size_t i = 0; i < error.size(); ++i) {
  164. error[i] = MLM_FALSE;
  165. bind[i].error = reinterpret_cast<char*>(&error[i]);
  166. }
  167. };
  168. /// @brief Return columns in error
  169. ///
  170. /// If an error is returned from a fetch (in particular, a truncated
  171. /// status), this method can be called to get the names of the fields in
  172. /// error. It returns a string comprising the names of the fields
  173. /// separated by commas. In the case of there being no error indicators
  174. /// set, it returns the string "(None)".
  175. ///
  176. /// @param error Array of error elements. An element is set to MLM_TRUE
  177. /// if the corresponding column in the database is the source of
  178. /// the error.
  179. /// @param names Array of column names, the same size as the error array.
  180. /// @param count Size of each of the arrays.
  181. static std::string getColumnsInError(std::vector<my_bool>& error,
  182. const std::vector<std::string>& names) {
  183. std::string result = "";
  184. // Accumulate list of column names
  185. for (size_t i = 0; i < names.size(); ++i) {
  186. if (error[i] == MLM_TRUE) {
  187. if (!result.empty()) {
  188. result += ", ";
  189. }
  190. result += names[i];
  191. }
  192. }
  193. if (result.empty()) {
  194. result = "(None)";
  195. }
  196. return (result);
  197. };
  198. /// @brief Create MYSQL_BIND objects for Host Pointer
  199. ///
  200. /// Fills in the MYSQL_BIND array for sending data stored in the Host object
  201. /// to the database.
  202. ///
  203. /// @param host Host object to be added to the database.
  204. /// None of the fields in the host reservation are modified -
  205. /// the host data is only read.
  206. ///
  207. /// @return Vector of MySQL BIND objects representing the data to be added.
  208. std::vector<MYSQL_BIND> createBindForSend(const HostPtr& host) {
  209. // Store host object to ensure it remains valid.
  210. host_ = host;
  211. // Initialize prior to constructing the array of MYSQL_BIND structures.
  212. // It sets all fields, including is_null, to zero, so we need to set
  213. // is_null only if it should be true. This gives up minor performance
  214. // benefit while being safe approach.
  215. memset(&bind_[0], 0, sizeof(MYSQL_BIND) * bind_.size());
  216. // Set up the structures for the various components of the host structure.
  217. try {
  218. // host_id : INT UNSIGNED NOT NULL
  219. // The host_id is auto_incremented by MySQL database,
  220. // so we need to pass the NULL value
  221. host_id_ = static_cast<uint32_t>(NULL);
  222. bind_[0].buffer_type = MYSQL_TYPE_LONG;
  223. bind_[0].buffer = reinterpret_cast<char*>(&host_id_);
  224. bind_[0].is_unsigned = MLM_TRUE;
  225. // dhcp_identifier : VARBINARY(128) NOT NULL
  226. dhcp_identifier_length_ = host->getIdentifier().size();
  227. memcpy(static_cast<void*>(dhcp_identifier_buffer_),
  228. &(host->getIdentifier())[0],
  229. host->getIdentifier().size());
  230. bind_[1].buffer_type = MYSQL_TYPE_BLOB;
  231. bind_[1].buffer = dhcp_identifier_buffer_;
  232. bind_[1].buffer_length = dhcp_identifier_length_;
  233. bind_[1].length = &dhcp_identifier_length_;
  234. // dhcp_identifier_type : TINYINT NOT NULL
  235. dhcp_identifier_type_ = static_cast<uint8_t>(host->getIdentifierType());
  236. bind_[2].buffer_type = MYSQL_TYPE_TINY;
  237. bind_[2].buffer = reinterpret_cast<char*>(&dhcp_identifier_type_);
  238. bind_[2].is_unsigned = MLM_TRUE;
  239. // dhcp4_subnet_id : INT UNSIGNED NULL
  240. // Can't take an address of intermediate object, so let's store it
  241. // in dhcp4_subnet_id_
  242. dhcp4_subnet_id_ = host->getIPv4SubnetID();
  243. bind_[3].buffer_type = MYSQL_TYPE_LONG;
  244. bind_[3].buffer = reinterpret_cast<char*>(&dhcp4_subnet_id_);
  245. bind_[3].is_unsigned = MLM_TRUE;
  246. // dhcp6_subnet_id : INT UNSIGNED NULL
  247. // Can't take an address of intermediate object, so let's store it
  248. // in dhcp6_subnet_id_
  249. dhcp6_subnet_id_ = host->getIPv6SubnetID();
  250. bind_[4].buffer_type = MYSQL_TYPE_LONG;
  251. bind_[4].buffer = reinterpret_cast<char*>(&dhcp6_subnet_id_);
  252. bind_[4].is_unsigned = MLM_TRUE;
  253. // ipv4_address : INT UNSIGNED NULL
  254. // The address in the Host structure is an IOAddress object. Convert
  255. // this to an integer for storage.
  256. ipv4_address_ = host->getIPv4Reservation().toUint32();
  257. bind_[5].buffer_type = MYSQL_TYPE_LONG;
  258. bind_[5].buffer = reinterpret_cast<char*>(&ipv4_address_);
  259. bind_[5].is_unsigned = MLM_TRUE;
  260. // bind_[5].is_null = &MLM_FALSE; // commented out for performance
  261. // reasons, see memset() above
  262. // hostname : VARCHAR(255) NULL
  263. strncpy(hostname_, host->getHostname().c_str(), HOSTNAME_MAX_LEN - 1);
  264. hostname_length_ = host->getHostname().length();
  265. bind_[6].buffer_type = MYSQL_TYPE_STRING;
  266. bind_[6].buffer = reinterpret_cast<char*>(hostname_);
  267. bind_[6].buffer_length = hostname_length_;
  268. // dhcp4_client_classes : VARCHAR(255) NULL
  269. bind_[7].buffer_type = MYSQL_TYPE_STRING;
  270. // Override default separator to not include space after comma.
  271. string classes4_txt = host->getClientClasses4().toText(",");
  272. strncpy(dhcp4_client_classes_, classes4_txt.c_str(), CLIENT_CLASSES_MAX_LEN - 1);
  273. bind_[7].buffer = dhcp4_client_classes_;
  274. bind_[7].buffer_length = classes4_txt.length();
  275. // dhcp6_client_classes : VARCHAR(255) NULL
  276. bind_[8].buffer_type = MYSQL_TYPE_STRING;
  277. // Override default separator to not include space after comma.
  278. string classes6_txt = host->getClientClasses6().toText(",");
  279. strncpy(dhcp6_client_classes_, classes6_txt.c_str(), CLIENT_CLASSES_MAX_LEN - 1);
  280. bind_[8].buffer = dhcp6_client_classes_;
  281. bind_[8].buffer_length = classes6_txt.length();
  282. // ipv4_address : INT UNSIGNED NULL
  283. // The address in the Host structure is an IOAddress object. Convert
  284. // this to an integer for storage.
  285. dhcp4_next_server_ = host->getNextServer().toUint32();
  286. bind_[9].buffer_type = MYSQL_TYPE_LONG;
  287. bind_[9].buffer = reinterpret_cast<char*>(&dhcp4_next_server_);
  288. bind_[9].is_unsigned = MLM_TRUE;
  289. // bind_[9].is_null = &MLM_FALSE; // commented out for performance
  290. // reasons, see memset() above
  291. // dhcp4_server_hostname
  292. bind_[10].buffer_type = MYSQL_TYPE_STRING;
  293. std::string server_hostname = host->getServerHostname();
  294. strncpy(dhcp4_server_hostname_, server_hostname.c_str(),
  295. SERVER_HOSTNAME_MAX_LEN - 1);
  296. bind_[10].buffer = dhcp4_server_hostname_;
  297. bind_[10].buffer_length = server_hostname.length();
  298. // dhcp4_boot_file_name
  299. bind_[11].buffer_type = MYSQL_TYPE_STRING;
  300. std::string boot_file_name = host->getBootFileName();
  301. strncpy(dhcp4_boot_file_name_, boot_file_name.c_str(),
  302. BOOT_FILE_NAME_MAX_LEN - 1);
  303. bind_[11].buffer = dhcp4_boot_file_name_;
  304. bind_[11].buffer_length = boot_file_name.length();
  305. } catch (const std::exception& ex) {
  306. isc_throw(DbOperationError,
  307. "Could not create bind array from Host: "
  308. << host->getHostname() << ", reason: " << ex.what());
  309. }
  310. // Add the data to the vector. Note the end element is one after the
  311. // end of the array.
  312. return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[columns_num_]));
  313. };
  314. /// @brief Create BIND array to receive Host data.
  315. ///
  316. /// Creates a MYSQL_BIND array to receive Host data from the database.
  317. /// After data is successfully received, @ref retrieveHost can be called
  318. /// to retrieve the Host object.
  319. ///
  320. /// @return Vector of MYSQL_BIND objects representing data to be retrieved.
  321. virtual std::vector<MYSQL_BIND> createBindForReceive() {
  322. // Initialize MYSQL_BIND array.
  323. // It sets all fields, including is_null, to zero, so we need to set
  324. // is_null only if it should be true. This gives up minor performance
  325. // benefit while being safe approach. For improved readability, the
  326. // code that explicitly sets is_null is there, but is commented out.
  327. // This also takes care of seeeting bind_[X].is_null to MLM_FALSE.
  328. memset(&bind_[0], 0, sizeof(MYSQL_BIND) * bind_.size());
  329. // host_id : INT UNSIGNED NOT NULL
  330. bind_[0].buffer_type = MYSQL_TYPE_LONG;
  331. bind_[0].buffer = reinterpret_cast<char*>(&host_id_);
  332. bind_[0].is_unsigned = MLM_TRUE;
  333. // dhcp_identifier : VARBINARY(128) NOT NULL
  334. dhcp_identifier_length_ = sizeof(dhcp_identifier_buffer_);
  335. bind_[1].buffer_type = MYSQL_TYPE_BLOB;
  336. bind_[1].buffer = reinterpret_cast<char*>(dhcp_identifier_buffer_);
  337. bind_[1].buffer_length = dhcp_identifier_length_;
  338. bind_[1].length = &dhcp_identifier_length_;
  339. // dhcp_identifier_type : TINYINT NOT NULL
  340. bind_[2].buffer_type = MYSQL_TYPE_TINY;
  341. bind_[2].buffer = reinterpret_cast<char*>(&dhcp_identifier_type_);
  342. bind_[2].is_unsigned = MLM_TRUE;
  343. // dhcp4_subnet_id : INT UNSIGNED NULL
  344. dhcp4_subnet_id_null_ = MLM_FALSE;
  345. bind_[3].buffer_type = MYSQL_TYPE_LONG;
  346. bind_[3].buffer = reinterpret_cast<char*>(&dhcp4_subnet_id_);
  347. bind_[3].is_unsigned = MLM_TRUE;
  348. bind_[3].is_null = &dhcp4_subnet_id_null_;
  349. // dhcp6_subnet_id : INT UNSIGNED NULL
  350. dhcp6_subnet_id_null_ = MLM_FALSE;
  351. bind_[4].buffer_type = MYSQL_TYPE_LONG;
  352. bind_[4].buffer = reinterpret_cast<char*>(&dhcp6_subnet_id_);
  353. bind_[4].is_unsigned = MLM_TRUE;
  354. bind_[4].is_null = &dhcp6_subnet_id_null_;
  355. // ipv4_address : INT UNSIGNED NULL
  356. ipv4_address_null_ = MLM_FALSE;
  357. bind_[5].buffer_type = MYSQL_TYPE_LONG;
  358. bind_[5].buffer = reinterpret_cast<char*>(&ipv4_address_);
  359. bind_[5].is_unsigned = MLM_TRUE;
  360. bind_[5].is_null = &ipv4_address_null_;
  361. // hostname : VARCHAR(255) NULL
  362. hostname_null_ = MLM_FALSE;
  363. hostname_length_ = sizeof(hostname_);
  364. bind_[6].buffer_type = MYSQL_TYPE_STRING;
  365. bind_[6].buffer = reinterpret_cast<char*>(hostname_);
  366. bind_[6].buffer_length = hostname_length_;
  367. bind_[6].length = &hostname_length_;
  368. bind_[6].is_null = &hostname_null_;
  369. // dhcp4_client_classes : VARCHAR(255) NULL
  370. dhcp4_client_classes_null_ = MLM_FALSE;
  371. dhcp4_client_classes_length_ = sizeof(dhcp4_client_classes_);
  372. bind_[7].buffer_type = MYSQL_TYPE_STRING;
  373. bind_[7].buffer = reinterpret_cast<char*>(dhcp4_client_classes_);
  374. bind_[7].buffer_length = dhcp4_client_classes_length_;
  375. bind_[7].length = &dhcp4_client_classes_length_;
  376. bind_[7].is_null = &dhcp4_client_classes_null_;
  377. // dhcp6_client_classes : VARCHAR(255) NULL
  378. dhcp6_client_classes_null_ = MLM_FALSE;
  379. dhcp6_client_classes_length_ = sizeof(dhcp6_client_classes_);
  380. bind_[8].buffer_type = MYSQL_TYPE_STRING;
  381. bind_[8].buffer = reinterpret_cast<char*>(dhcp6_client_classes_);
  382. bind_[8].buffer_length = dhcp6_client_classes_length_;
  383. bind_[8].length = &dhcp6_client_classes_length_;
  384. bind_[8].is_null = &dhcp6_client_classes_null_;
  385. // dhcp4_next_server
  386. dhcp4_next_server_null_ = MLM_FALSE;
  387. bind_[9].buffer_type = MYSQL_TYPE_LONG;
  388. bind_[9].buffer = reinterpret_cast<char*>(&dhcp4_next_server_);
  389. bind_[9].is_unsigned = MLM_TRUE;
  390. bind_[9].is_null = &dhcp4_next_server_null_;
  391. // dhcp4_server_hostname
  392. dhcp4_server_hostname_null_ = MLM_FALSE;
  393. dhcp4_server_hostname_length_ = sizeof(dhcp4_server_hostname_);
  394. bind_[10].buffer_type = MYSQL_TYPE_STRING;
  395. bind_[10].buffer = reinterpret_cast<char*>(dhcp4_server_hostname_);
  396. bind_[10].buffer_length = dhcp4_server_hostname_length_;
  397. bind_[10].length = &dhcp4_server_hostname_length_;
  398. bind_[10].is_null = &dhcp4_server_hostname_null_;
  399. // dhcp4_boot_file_name
  400. dhcp4_boot_file_name_null_ = MLM_FALSE;
  401. dhcp4_boot_file_name_length_ = sizeof(dhcp4_boot_file_name_);
  402. bind_[11].buffer_type = MYSQL_TYPE_STRING;
  403. bind_[11].buffer = reinterpret_cast<char*>(dhcp4_boot_file_name_);
  404. bind_[11].buffer_length = dhcp4_boot_file_name_length_;
  405. bind_[11].length = &dhcp4_boot_file_name_length_;
  406. bind_[11].is_null = &dhcp4_boot_file_name_null_;
  407. // Add the error flags
  408. setErrorIndicators(bind_, error_);
  409. // Add the data to the vector. Note the end element is one after the
  410. // end of the array.
  411. return (bind_);
  412. };
  413. /// @brief Copy received data into Host object
  414. ///
  415. /// This function copies information about the host into a newly created
  416. /// @ref Host object. This method is called after @ref createBindForReceive.
  417. /// has been used.
  418. ///
  419. /// @return Host Pointer to a @ref HostPtr object holding a pointer to the
  420. /// @ref Host object returned.
  421. HostPtr retrieveHost() {
  422. // Check if the identifier stored in the database is correct.
  423. if (dhcp_identifier_type_ > MAX_IDENTIFIER_TYPE) {
  424. isc_throw(BadValue, "invalid dhcp identifier type returned: "
  425. << static_cast<int>(dhcp_identifier_type_));
  426. }
  427. // Set the dhcp identifier type in a variable of the appropriate
  428. // data type.
  429. Host::IdentifierType type =
  430. static_cast<Host::IdentifierType>(dhcp_identifier_type_);
  431. // Set DHCPv4 subnet ID to the value returned. If NULL returned,
  432. // set to 0.
  433. SubnetID ipv4_subnet_id(0);
  434. if (dhcp4_subnet_id_null_ == MLM_FALSE) {
  435. ipv4_subnet_id = static_cast<SubnetID>(dhcp4_subnet_id_);
  436. }
  437. // Set DHCPv6 subnet ID to the value returned. If NULL returned,
  438. // set to 0.
  439. SubnetID ipv6_subnet_id(0);
  440. if (dhcp6_subnet_id_null_ == MLM_FALSE) {
  441. ipv6_subnet_id = static_cast<SubnetID>(dhcp6_subnet_id_);
  442. }
  443. // Set IPv4 address reservation if it was given, if not, set IPv4 zero
  444. // address
  445. asiolink::IOAddress ipv4_reservation = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
  446. if (ipv4_address_null_ == MLM_FALSE) {
  447. ipv4_reservation = asiolink::IOAddress(ipv4_address_);
  448. }
  449. // Set hostname if non NULL value returned. Otherwise, leave an
  450. // empty string.
  451. std::string hostname;
  452. if (hostname_null_ == MLM_FALSE) {
  453. hostname = std::string(hostname_, hostname_length_);
  454. }
  455. // Set DHCPv4 client classes if non NULL value returned.
  456. std::string dhcp4_client_classes;
  457. if (dhcp4_client_classes_null_ == MLM_FALSE) {
  458. dhcp4_client_classes = std::string(dhcp4_client_classes_,
  459. dhcp4_client_classes_length_);
  460. }
  461. // Set DHCPv6 client classes if non NULL value returned.
  462. std::string dhcp6_client_classes;
  463. if (dhcp6_client_classes_null_ == MLM_FALSE) {
  464. dhcp6_client_classes = std::string(dhcp6_client_classes_,
  465. dhcp6_client_classes_length_);
  466. }
  467. // Set next server value (siaddr) if non NULL value returned.
  468. asiolink::IOAddress next_server = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
  469. if (dhcp4_next_server_null_ == MLM_FALSE) {
  470. next_server = asiolink::IOAddress(dhcp4_next_server_);
  471. }
  472. // Set server hostname (sname) if non NULL value returned.
  473. std::string dhcp4_server_hostname;
  474. if (dhcp4_server_hostname_null_ == MLM_FALSE) {
  475. dhcp4_server_hostname = std::string(dhcp4_server_hostname_,
  476. dhcp4_server_hostname_length_);
  477. }
  478. // Set boot file name (file) if non NULL value returned.
  479. std::string dhcp4_boot_file_name;
  480. if (dhcp4_boot_file_name_null_ == MLM_FALSE) {
  481. dhcp4_boot_file_name = std::string(dhcp4_boot_file_name_,
  482. dhcp4_boot_file_name_length_);
  483. }
  484. // Create and return Host object from the data gathered.
  485. HostPtr h(new Host(dhcp_identifier_buffer_, dhcp_identifier_length_,
  486. type, ipv4_subnet_id, ipv6_subnet_id, ipv4_reservation,
  487. hostname, dhcp4_client_classes, dhcp6_client_classes,
  488. next_server, dhcp4_server_hostname,
  489. dhcp4_boot_file_name));
  490. h->setHostId(host_id_);
  491. return (h);
  492. };
  493. /// @brief Processes one row of data fetched from a database.
  494. ///
  495. /// The processed data must contain host id, which uniquely identifies a
  496. /// host. This method creates a host and inserts it to the hosts collection
  497. /// only if the last inserted host has a different host id. This prevents
  498. /// adding duplicated hosts to the collection, assuming that processed
  499. /// rows are primarily ordered by host id column.
  500. ///
  501. /// This method must be overriden in the derived classes to also
  502. /// retrieve IPv6 reservations and DHCP options associated with a host.
  503. ///
  504. /// @param [out] hosts Collection of hosts to which a new host created
  505. /// from the processed data should be inserted.
  506. virtual void processFetchedData(ConstHostCollection& hosts) {
  507. HostPtr host;
  508. // Add new host only if there are no hosts yet or the host id of the
  509. // most recently added host is different than the host id of the
  510. // currently processed host.
  511. if (hosts.empty() || (hosts.back()->getHostId() != getHostId())) {
  512. // Create Host object from the fetched data and append it to the
  513. // collection.
  514. host = retrieveHost();
  515. hosts.push_back(host);
  516. }
  517. }
  518. /// @brief Return columns in error
  519. ///
  520. /// If an error is returned from a fetch (in particular, a truncated
  521. /// status), this method can be called to get the names of the fields in
  522. /// error. It returns a string comprising the names of the fields
  523. /// separated by commas. In the case of there being no error indicators
  524. /// set, it returns the string "(None)".
  525. ///
  526. /// @return Comma-separated list of columns in error, or the string
  527. /// "(None)".
  528. std::string getErrorColumns() {
  529. return (getColumnsInError(error_, columns_));
  530. };
  531. protected:
  532. /// Number of columns returned in queries.
  533. size_t columns_num_;
  534. /// Vector of MySQL bindings.
  535. std::vector<MYSQL_BIND> bind_;
  536. /// Column names.
  537. std::vector<std::string> columns_;
  538. /// Error array.
  539. std::vector<my_bool> error_;
  540. /// Pointer to Host object holding information to be inserted into
  541. /// Hosts table.
  542. HostPtr host_;
  543. private:
  544. /// Host identifier (primary key in Hosts table).
  545. uint64_t host_id_;
  546. /// Buffer holding client's identifier (e.g. DUID, HW address)
  547. /// in the binary format.
  548. uint8_t dhcp_identifier_buffer_[DUID::MAX_DUID_LEN];
  549. /// Length of a data in the dhcp_identifier_buffer_.
  550. unsigned long dhcp_identifier_length_;
  551. /// Type of the identifier in the dhcp_identifier_buffer_. This
  552. /// value corresponds to the @ref Host::IdentifierType value.
  553. uint8_t dhcp_identifier_type_;
  554. /// DHCPv4 subnet identifier.
  555. uint32_t dhcp4_subnet_id_;
  556. /// DHCPv6 subnet identifier.
  557. uint32_t dhcp6_subnet_id_;
  558. /// Reserved IPv4 address.
  559. uint32_t ipv4_address_;
  560. /// Name reserved for the host.
  561. char hostname_[HOSTNAME_MAX_LEN];
  562. /// Hostname length.
  563. unsigned long hostname_length_;
  564. /// A string holding comma separated list of DHCPv4 client classes.
  565. char dhcp4_client_classes_[CLIENT_CLASSES_MAX_LEN];
  566. /// A length of the string holding comma separated list of DHCPv4
  567. /// client classes.
  568. unsigned long dhcp4_client_classes_length_;
  569. /// A string holding comma separated list of DHCPv6 client classes.
  570. char dhcp6_client_classes_[CLIENT_CLASSES_MAX_LEN];
  571. /// A length of the string holding comma separated list of DHCPv6
  572. /// client classes.
  573. unsigned long dhcp6_client_classes_length_;
  574. /// Next server address (siaddr).
  575. uint32_t dhcp4_next_server_;
  576. /// Server hostname (sname).
  577. char dhcp4_server_hostname_[SERVER_HOSTNAME_MAX_LEN];
  578. /// A length of the string holding server hostname.
  579. unsigned long dhcp4_server_hostname_length_;
  580. /// Boot file name (file).
  581. char dhcp4_boot_file_name_[BOOT_FILE_NAME_MAX_LEN];
  582. /// A length of the string holding boot file name.
  583. unsigned long dhcp4_boot_file_name_length_;
  584. /// @name Boolean values indicating if values of specific columns in
  585. /// the database are NULL.
  586. //@{
  587. /// Boolean flag indicating if the value of the DHCPv4 subnet is NULL.
  588. my_bool dhcp4_subnet_id_null_;
  589. /// Boolean flag indicating if the value of the DHCPv6 subnet is NULL.
  590. my_bool dhcp6_subnet_id_null_;
  591. /// Boolean flag indicating if the value of IPv4 reservation is NULL.
  592. my_bool ipv4_address_null_;
  593. /// Boolean flag indicating if the value if hostname is NULL.
  594. my_bool hostname_null_;
  595. /// Boolean flag indicating if the value of DHCPv4 client classes is
  596. /// NULL.
  597. my_bool dhcp4_client_classes_null_;
  598. /// Boolean flag indicating if the value of DHCPv6 client classes is
  599. /// NULL.
  600. my_bool dhcp6_client_classes_null_;
  601. /// Boolean flag indicating if the value of next server is NULL.
  602. my_bool dhcp4_next_server_null_;
  603. /// Boolean flag indicating if the value of server hostname is NULL.
  604. my_bool dhcp4_server_hostname_null_;
  605. /// Boolean flag indicating if the value of boot file name is NULL.
  606. my_bool dhcp4_boot_file_name_null_;
  607. //@}
  608. };
  609. /// @brief Extends base exchange class with ability to retrieve DHCP options
  610. /// from the 'dhcp4_options' and 'dhcp6_options' tables.
  611. ///
  612. /// This class provides means to retrieve both DHCPv4 and DHCPv6 options
  613. /// along with the host information. It is not used to retrieve IPv6
  614. /// reservations. The following types of queries are supported:
  615. /// - SELECT ? FROM hosts LEFT JOIN dhcp4_options LEFT JOIN dhcp6_options ...
  616. /// - SELECT ? FROM hosts LEFT JOIN dhcp4_options ...
  617. /// - SELECT ? FROM hosts LEFT JOIN dhcp6_options ...
  618. class MySqlHostWithOptionsExchange : public MySqlHostExchange {
  619. private:
  620. /// @brief Number of columns holding DHCPv4 or DHCPv6 option information.
  621. static const size_t OPTION_COLUMNS = 6;
  622. /// @brief Receives DHCPv4 or DHCPv6 options information from the
  623. /// dhcp4_options or dhcp6_options tables respectively.
  624. ///
  625. /// The MySqlHostWithOptionsExchange class holds two respective instances
  626. /// of this class, one for receiving DHCPv4 options, one for receiving
  627. /// DHCPv6 options.
  628. ///
  629. /// The following are the basic functions of this class:
  630. /// - bind class members to specific columns in MySQL binding tables,
  631. /// - set DHCP options specific column names,
  632. /// - create instances of options retrieved from the database.
  633. ///
  634. /// The reason for isolating those functions in a separate C++ class is
  635. /// to prevent code duplication for handling DHCPv4 and DHCPv6 options.
  636. class OptionProcessor {
  637. public:
  638. /// @brief Constructor.
  639. ///
  640. /// @param universe V4 or V6. The type of the options' instances
  641. /// created by this class depends on this parameter.
  642. /// @param start_column Index of the first column to be used by this
  643. /// class.
  644. OptionProcessor(const Option::Universe& universe,
  645. const size_t start_column)
  646. : universe_(universe), start_column_(start_column), option_id_(0),
  647. code_(0), value_length_(0), formatted_value_length_(0),
  648. space_length_(0), persistent_(false), option_id_null_(MLM_FALSE),
  649. code_null_(MLM_FALSE), value_null_(MLM_FALSE),
  650. formatted_value_null_(MLM_FALSE), space_null_(MLM_FALSE),
  651. option_id_index_(start_column), code_index_(start_column_ + 1),
  652. value_index_(start_column_ + 2),
  653. formatted_value_index_(start_column_ + 3),
  654. space_index_(start_column_ + 4),
  655. persistent_index_(start_column_ + 5),
  656. most_recent_option_id_(0) {
  657. memset(value_, 0, sizeof(value_));
  658. memset(formatted_value_, 0, sizeof(formatted_value_));
  659. memset(space_, 0, sizeof(space_));
  660. }
  661. /// @brief Returns identifier of the currently processed option.
  662. uint64_t getOptionId() const {
  663. if (option_id_null_ == MLM_FALSE) {
  664. return (option_id_);
  665. }
  666. return (0);
  667. }
  668. /// @brief Creates instance of the currently processed option.
  669. ///
  670. /// This method detects if the currently processed option is a new
  671. /// instance. It makes it determination by comparing the identifier
  672. /// of the currently processed option, with the most recently processed
  673. /// option. If the current value is greater than the id of the recently
  674. /// processed option it is assumed that the processed row holds new
  675. /// option information. In such case the option instance is created and
  676. /// inserted into the configuration passed as argument.
  677. ///
  678. /// @param cfg Pointer to the configuration object into which new
  679. /// option instances should be inserted.
  680. void retrieveOption(const CfgOptionPtr& cfg) {
  681. // option_id may be NULL if dhcp4_options or dhcp6_options table
  682. // doesn't contain any options for the particular host. Also, the
  683. // current option id must be greater than id if the most recent
  684. // option because options are ordered by option id. Otherwise
  685. // we assume that this is already processed option.
  686. if ((option_id_null_ == MLM_TRUE) ||
  687. (most_recent_option_id_ >= option_id_)) {
  688. return;
  689. }
  690. // Remember current option id as the most recent processed one. We
  691. // will be comparing it with option ids in subsequent rows.
  692. most_recent_option_id_ = option_id_;
  693. // Convert it to string object for easier comparison.
  694. std::string space;
  695. if (space_null_ == MLM_FALSE) {
  696. // Typically, the string values returned by the database are not
  697. // NULL terminated.
  698. space_[space_length_] = '\0';
  699. space.assign(space_);
  700. }
  701. // If empty or null space provided, use a default top level space.
  702. if (space.empty()) {
  703. space = (universe_ == Option::V4 ? "dhcp4" : "dhcp6");
  704. }
  705. // Convert formatted_value to string as well.
  706. std::string formatted_value;
  707. if (formatted_value_null_ == MLM_FALSE) {
  708. formatted_value_[formatted_value_length_] = '\0';
  709. formatted_value.assign(formatted_value_);
  710. }
  711. // Options are held in a binary or textual format in the database.
  712. // This is similar to having an option specified in a server
  713. // configuration file. Such option is converted to appropriate C++
  714. // class, using option definition. Thus, we need to find the
  715. // option definition for this option code and option space.
  716. // Check if this is a standard option.
  717. OptionDefinitionPtr def = LibDHCP::getOptionDef(space, code_);
  718. // Otherwise, we may check if this an option encapsulated within the
  719. // vendor space.
  720. if (!def && (space != DHCP4_OPTION_SPACE) &&
  721. (space != DHCP6_OPTION_SPACE)) {
  722. uint32_t vendor_id = LibDHCP::optionSpaceToVendorId(space);
  723. if (vendor_id > 0) {
  724. def = LibDHCP::getVendorOptionDef(universe_, vendor_id, code_);
  725. }
  726. }
  727. // In all other cases, we use runtime option definitions, which
  728. // should be also registered within the libdhcp++.
  729. if (!def) {
  730. def = LibDHCP::getRuntimeOptionDef(space, code_);
  731. }
  732. OptionPtr option;
  733. if (!def) {
  734. // If no definition found, we use generic option type.
  735. OptionBuffer buf(value_, value_ + value_length_);
  736. option.reset(new Option(universe_, code_, buf.begin(),
  737. buf.end()));
  738. } else {
  739. // The option value may be specified in textual or binary format
  740. // in the database. If formatted_value is empty, the binary
  741. // format is used. Depending on the format we use a different
  742. // variant of the optionFactory function.
  743. if (formatted_value.empty()) {
  744. OptionBuffer buf(value_, value_ + value_length_);
  745. option = def->optionFactory(universe_, code_, buf.begin(),
  746. buf.end());
  747. } else {
  748. // Spit the value specified in comma separated values
  749. // format.
  750. std::vector<std::string> split_vec;
  751. boost::split(split_vec, formatted_value, boost::is_any_of(","));
  752. option = def->optionFactory(universe_, code_, split_vec);
  753. }
  754. }
  755. OptionDescriptor desc(option, persistent_, formatted_value);
  756. cfg->add(desc, space);
  757. }
  758. /// @brief Specify column names.
  759. ///
  760. /// @param [out] columns Reference to a vector holding names of option
  761. /// specific columns.
  762. void setColumnNames(std::vector<std::string>& columns) {
  763. columns[option_id_index_] = "option_id";
  764. columns[code_index_] = "code";
  765. columns[value_index_] = "value";
  766. columns[formatted_value_index_] = "formatted_value";
  767. columns[space_index_] = "space";
  768. columns[persistent_index_] = "persistent";
  769. }
  770. /// @brief Initialize binding table fields for options.
  771. ///
  772. /// Resets most_recent_option_id_ value to 0.
  773. ///
  774. /// @param [out] bind Binding table.
  775. void setBindFields(std::vector<MYSQL_BIND>& bind) {
  776. // This method is called just before making a new query, so we
  777. // reset the most_recent_option_id_ to start over with options
  778. // processing.
  779. most_recent_option_id_ = 0;
  780. // option_id : INT UNSIGNED NOT NULL AUTO_INCREMENT,
  781. bind[option_id_index_].buffer_type = MYSQL_TYPE_LONG;
  782. bind[option_id_index_].buffer = reinterpret_cast<char*>(&option_id_);
  783. bind[option_id_index_].is_unsigned = MLM_TRUE;
  784. // code : TINYINT OR SHORT UNSIGNED NOT NULL
  785. bind[code_index_].buffer_type = MYSQL_TYPE_SHORT;
  786. bind[code_index_].buffer = reinterpret_cast<char*>(&code_);
  787. bind[code_index_].is_unsigned = MLM_TRUE;
  788. bind[code_index_].is_null = &code_null_;
  789. // value : BLOB NULL
  790. value_length_ = sizeof(value_);
  791. bind[value_index_].buffer_type = MYSQL_TYPE_BLOB;
  792. bind[value_index_].buffer = reinterpret_cast<char*>(value_);
  793. bind[value_index_].buffer_length = value_length_;
  794. bind[value_index_].length = &value_length_;
  795. bind[value_index_].is_null = &value_null_;
  796. // formatted_value : TEXT NULL
  797. formatted_value_length_ = sizeof(formatted_value_);
  798. bind[formatted_value_index_].buffer_type = MYSQL_TYPE_STRING;
  799. bind[formatted_value_index_].buffer = reinterpret_cast<char*>(formatted_value_);
  800. bind[formatted_value_index_].buffer_length = formatted_value_length_;
  801. bind[formatted_value_index_].length = &formatted_value_length_;
  802. bind[formatted_value_index_].is_null = &formatted_value_null_;
  803. // space : VARCHAR(128) NULL
  804. space_length_ = sizeof(space_);
  805. bind[space_index_].buffer_type = MYSQL_TYPE_STRING;
  806. bind[space_index_].buffer = reinterpret_cast<char*>(space_);
  807. bind[space_index_].buffer_length = space_length_;
  808. bind[space_index_].length = &space_length_;
  809. bind[space_index_].is_null = &space_null_;
  810. // persistent : TINYINT(1) NOT NULL DEFAULT 0
  811. bind[persistent_index_].buffer_type = MYSQL_TYPE_TINY;
  812. bind[persistent_index_].buffer = reinterpret_cast<char*>(&persistent_);
  813. bind[persistent_index_].is_unsigned = MLM_TRUE;
  814. }
  815. private:
  816. /// @brief Universe: V4 or V6.
  817. Option::Universe universe_;
  818. /// @brief Index of first column used by this class.
  819. size_t start_column_;
  820. /// @brief Option id.
  821. uint32_t option_id_;
  822. /// @brief Option code.
  823. uint16_t code_;
  824. /// @brief Buffer holding binary value of an option.
  825. uint8_t value_[OPTION_VALUE_MAX_LEN];
  826. /// @brief Option value length.
  827. unsigned long value_length_;
  828. /// @brief Buffer holding textual value of an option.
  829. char formatted_value_[OPTION_FORMATTED_VALUE_MAX_LEN];
  830. /// @brief Formatted option value length.
  831. unsigned long formatted_value_length_;
  832. /// @brief Buffer holding option space name.
  833. char space_[OPTION_SPACE_MAX_LEN];
  834. /// @brief Option space length.
  835. unsigned long space_length_;
  836. /// @brief Flag indicating if option is always sent or only if
  837. /// requested.
  838. bool persistent_;
  839. /// @name Boolean values indicating if values of specific columns in
  840. /// the database are NULL.
  841. //@{
  842. /// @brief Boolean flag indicating if the DHCPv4 option id is NULL.
  843. my_bool option_id_null_;
  844. /// @brief Boolean flag indicating if the DHCPv4 option code is NULL.
  845. my_bool code_null_;
  846. /// @brief Boolean flag indicating if the DHCPv4 option value is NULL.
  847. my_bool value_null_;
  848. /// @brief Boolean flag indicating if the DHCPv4 formatted option value
  849. /// is NULL.
  850. my_bool formatted_value_null_;
  851. /// @brief Boolean flag indicating if the DHCPv4 option space is NULL.
  852. my_bool space_null_;
  853. //@}
  854. /// @name Indexes of the specific columns
  855. //@{
  856. /// @brief Option id
  857. size_t option_id_index_;
  858. /// @brief Code
  859. size_t code_index_;
  860. /// @brief Value
  861. size_t value_index_;
  862. /// @brief Formatted value
  863. size_t formatted_value_index_;
  864. /// @brief Space
  865. size_t space_index_;
  866. /// @brief Persistent
  867. size_t persistent_index_;
  868. //@}
  869. /// @brief Option id for last processed row.
  870. uint32_t most_recent_option_id_;
  871. };
  872. /// @brief Pointer to the @ref OptionProcessor class.
  873. typedef boost::shared_ptr<OptionProcessor> OptionProcessorPtr;
  874. public:
  875. /// @brief DHCP option types to be fetched from the database.
  876. ///
  877. /// Supported types are:
  878. /// - Only DHCPv4 options,
  879. /// - Only DHCPv6 options,
  880. /// - Both DHCPv4 and DHCPv6 options.
  881. enum FetchedOptions {
  882. DHCP4_ONLY,
  883. DHCP6_ONLY,
  884. DHCP4_AND_DHCP6
  885. };
  886. /// @brief Constructor.
  887. ///
  888. /// @param fetched_options Specifies if DHCPv4, DHCPv6 or both should
  889. /// be fetched from the database for a host.
  890. /// @param additional_columns_num Number of additional columns for which
  891. /// resources should be allocated, e.g. binding table, column names etc.
  892. /// This parameter should be set to a non zero value by derived classes to
  893. /// allocate resources for the columns supported by derived classes.
  894. MySqlHostWithOptionsExchange(const FetchedOptions& fetched_options,
  895. const size_t additional_columns_num = 0)
  896. : MySqlHostExchange(getRequiredColumnsNum(fetched_options)
  897. + additional_columns_num),
  898. opt_proc4_(), opt_proc6_() {
  899. // Create option processor for DHCPv4 options, if required.
  900. if ((fetched_options == DHCP4_ONLY) ||
  901. (fetched_options == DHCP4_AND_DHCP6)) {
  902. opt_proc4_.reset(new OptionProcessor(Option::V4,
  903. findAvailColumn()));
  904. opt_proc4_->setColumnNames(columns_);
  905. }
  906. // Create option processor for DHCPv6 options, if required.
  907. if ((fetched_options == DHCP6_ONLY) ||
  908. (fetched_options == DHCP4_AND_DHCP6)) {
  909. opt_proc6_.reset(new OptionProcessor(Option::V6,
  910. findAvailColumn()));
  911. opt_proc6_->setColumnNames(columns_);
  912. }
  913. }
  914. /// @brief Processes the current row.
  915. ///
  916. /// The processed row includes both host information and DHCP option
  917. /// information. Because used SELECT query use LEFT JOIN clause, the
  918. /// some rows contain duplicated host or options entries. This method
  919. /// detects duplicated information and discards such entries.
  920. ///
  921. /// @param [out] hosts Container holding parsed hosts and options.
  922. virtual void processFetchedData(ConstHostCollection& hosts) {
  923. // Holds pointer to the previously parsed host.
  924. HostPtr most_recent_host;
  925. if (!hosts.empty()) {
  926. // Const cast is not very elegant way to deal with it, but
  927. // there is a good reason to use it here. This method is called
  928. // to build a collection of const hosts to be returned to the
  929. // caller. If we wanted to use non-const collection we'd need
  930. // to copy the whole collection before returning it, which has
  931. // performance implications. Alternatively, we could store the
  932. // most recently added host in a class member but this would
  933. // make the code less readable.
  934. most_recent_host = boost::const_pointer_cast<Host>(hosts.back());
  935. }
  936. // If no host has been parsed yet or we're at the row holding next
  937. // host, we create a new host object and put it at the end of the
  938. // list.
  939. if (!most_recent_host || (most_recent_host->getHostId() < getHostId())) {
  940. HostPtr host = retrieveHost();
  941. hosts.push_back(host);
  942. most_recent_host = host;
  943. }
  944. // Parse DHCPv4 options if required to do so.
  945. if (opt_proc4_) {
  946. CfgOptionPtr cfg = most_recent_host->getCfgOption4();
  947. opt_proc4_->retrieveOption(cfg);
  948. }
  949. // Parse DHCPv6 options if required to do so.
  950. if (opt_proc6_) {
  951. CfgOptionPtr cfg = most_recent_host->getCfgOption6();
  952. opt_proc6_->retrieveOption(cfg);
  953. }
  954. }
  955. /// @brief Bind variables for receiving option data.
  956. ///
  957. /// @return Vector of MYSQL_BIND object representing data to be retrieved.
  958. virtual std::vector<MYSQL_BIND> createBindForReceive() {
  959. // The following call sets bind_ values between 0 and 8.
  960. static_cast<void>(MySqlHostExchange::createBindForReceive());
  961. // Bind variables for DHCPv4 options.
  962. if (opt_proc4_) {
  963. opt_proc4_->setBindFields(bind_);
  964. }
  965. // Bind variables for DHCPv6 options.
  966. if (opt_proc6_) {
  967. opt_proc6_->setBindFields(bind_);
  968. }
  969. // Add the error flags
  970. setErrorIndicators(bind_, error_);
  971. return (bind_);
  972. };
  973. private:
  974. /// @brief Returns a number of columns required to retrieve option data.
  975. ///
  976. /// Depending if we need DHCPv4/DHCPv6 options only, or both DHCPv4 and
  977. /// DHCPv6 a different number of columns is required in the binding array.
  978. /// This method returns the number of required columns, according to the
  979. /// value of @c fetched_columns passed in the constructor.
  980. ///
  981. /// @param fetched_columns A value which specifies whether DHCPv4, DHCPv6 or
  982. /// both types of options should be retrieved.
  983. ///
  984. /// @return Number of required columns.
  985. static size_t getRequiredColumnsNum(const FetchedOptions& fetched_options) {
  986. return (fetched_options == DHCP4_AND_DHCP6 ? 2 * OPTION_COLUMNS :
  987. OPTION_COLUMNS);
  988. }
  989. /// @brief Pointer to DHCPv4 options processor.
  990. ///
  991. /// If this object is NULL, the DHCPv4 options are not fetched.
  992. OptionProcessorPtr opt_proc4_;
  993. /// @brief Pointer to DHCPv6 options processor.
  994. ///
  995. /// If this object is NULL, the DHCPv6 options are not fetched.
  996. OptionProcessorPtr opt_proc6_;
  997. };
  998. /// @brief This class provides mechanisms for sending and retrieving
  999. /// host information, DHCPv4 options, DHCPv6 options and IPv6 reservations.
  1000. ///
  1001. /// This class extends the @ref MySqlHostWithOptionsExchange class with the
  1002. /// mechanisms to retrieve IPv6 reservations. This class is used in sitations
  1003. /// when it is desired to retrieve DHCPv6 specific information about the host
  1004. /// (DHCPv6 options and reservations), or entire information about the host
  1005. /// (DHCPv4 options, DHCPv6 options and reservations). The following are the
  1006. /// queries used with this class:
  1007. /// - SELECT ? FROM hosts LEFT JOIN dhcp4_options LEFT JOIN dhcp6_options
  1008. /// LEFT JOIN ipv6_reservations ...
  1009. /// - SELECT ? FROM hosts LEFT JOIN dhcp6_options LEFT JOIN ipv6_reservations ..
  1010. class MySqlHostIPv6Exchange : public MySqlHostWithOptionsExchange {
  1011. private:
  1012. /// @brief Number of columns holding IPv6 reservation information.
  1013. static const size_t RESERVATION_COLUMNS = 5;
  1014. public:
  1015. /// @brief Constructor.
  1016. ///
  1017. /// Apart from initializing the base class data structures it also
  1018. /// initializes values representing IPv6 reservation information.
  1019. MySqlHostIPv6Exchange(const FetchedOptions& fetched_options)
  1020. : MySqlHostWithOptionsExchange(fetched_options, RESERVATION_COLUMNS),
  1021. reservation_id_(0),
  1022. reserv_type_(0), reserv_type_null_(MLM_FALSE),
  1023. ipv6_address_buffer_len_(0), prefix_len_(0), iaid_(0),
  1024. reservation_id_index_(findAvailColumn()),
  1025. address_index_(reservation_id_index_ + 1),
  1026. prefix_len_index_(reservation_id_index_ + 2),
  1027. type_index_(reservation_id_index_ + 3),
  1028. iaid_index_(reservation_id_index_ + 4),
  1029. most_recent_reservation_id_(0) {
  1030. memset(ipv6_address_buffer_, 0, sizeof(ipv6_address_buffer_));
  1031. // Provide names of additional columns returned by the queries.
  1032. columns_[reservation_id_index_] = "reservation_id";
  1033. columns_[address_index_] = "address";
  1034. columns_[prefix_len_index_] = "prefix_len";
  1035. columns_[type_index_] = "type";
  1036. columns_[iaid_index_] = "dhcp6_iaid";
  1037. }
  1038. /// @brief Returns last fetched reservation id.
  1039. ///
  1040. /// @return Reservation id or 0 if no reservation data is fetched.
  1041. uint32_t getReservationId() const {
  1042. if (reserv_type_null_ == MLM_FALSE) {
  1043. return (reservation_id_);
  1044. }
  1045. return (0);
  1046. };
  1047. /// @brief Creates IPv6 reservation from the data contained in the
  1048. /// currently processed row.
  1049. ///
  1050. /// Called after the MYSQL_BIND array created by createBindForReceive().
  1051. ///
  1052. /// @return IPv6Resrv object (containing IPv6 address or prefix reservation)
  1053. IPv6Resrv retrieveReservation() {
  1054. // Set the IPv6 Reservation type (0 = IA_NA, 2 = IA_PD)
  1055. IPv6Resrv::Type type = IPv6Resrv::TYPE_NA;
  1056. switch (reserv_type_) {
  1057. case 0:
  1058. type = IPv6Resrv::TYPE_NA;
  1059. break;
  1060. case 2:
  1061. type = IPv6Resrv::TYPE_PD;
  1062. break;
  1063. default:
  1064. isc_throw(BadValue,
  1065. "invalid IPv6 reservation type returned: "
  1066. << static_cast<int>(reserv_type_)
  1067. << ". Only 0 or 2 are allowed.");
  1068. }
  1069. ipv6_address_buffer_[ipv6_address_buffer_len_] = '\0';
  1070. std::string address = ipv6_address_buffer_;
  1071. IPv6Resrv r(type, IOAddress(address), prefix_len_);
  1072. return (r);
  1073. };
  1074. /// @brief Processes one row of data fetched from a database.
  1075. ///
  1076. /// The processed data must contain host id, which uniquely identifies a
  1077. /// host. This method creates a host and inserts it to the hosts collection
  1078. /// only if the last inserted host has a different host id. This prevents
  1079. /// adding duplicated hosts to the collection, assuming that processed
  1080. /// rows are primarily ordered by host id column.
  1081. ///
  1082. /// Depending on the value of the @c fetched_options specified in the
  1083. /// constructor, this method also parses options returned as a result
  1084. /// of SELECT queries.
  1085. ///
  1086. /// For any returned row which contains IPv6 reservation information it
  1087. /// checks if the reservation is not a duplicate of previously parsed
  1088. /// reservation and appends the IPv6Resrv object into the host object
  1089. /// if the parsed row contains new reservation information.
  1090. ///
  1091. /// @param [out] hosts Collection of hosts to which a new host created
  1092. /// from the processed data should be inserted.
  1093. virtual void processFetchedData(ConstHostCollection& hosts) {
  1094. // Call parent class to fetch host information and options.
  1095. MySqlHostWithOptionsExchange::processFetchedData(hosts);
  1096. if (getReservationId() == 0) {
  1097. return;
  1098. }
  1099. if (hosts.empty()) {
  1100. isc_throw(Unexpected, "no host information while retrieving"
  1101. " IPv6 reservation");
  1102. }
  1103. HostPtr host = boost::const_pointer_cast<Host>(hosts.back());
  1104. // If we're dealing with a new reservation, let's add it to the
  1105. // host.
  1106. if (getReservationId() > most_recent_reservation_id_) {
  1107. most_recent_reservation_id_ = getReservationId();
  1108. if (most_recent_reservation_id_ > 0) {
  1109. host->addReservation(retrieveReservation());
  1110. }
  1111. }
  1112. }
  1113. /// @brief Create BIND array to receive Host data with IPv6 reservations.
  1114. ///
  1115. /// Creates a MYSQL_BIND array to receive Host data from the database.
  1116. /// After data is successfully received, @ref processedFetchedData is
  1117. /// called for each returned row to build collection of @ref Host
  1118. /// objects with associated IPv6 reservations.
  1119. ///
  1120. /// @return Vector of MYSQL_BIND objects representing data to be retrieved.
  1121. virtual std::vector<MYSQL_BIND> createBindForReceive() {
  1122. // Reset most recent reservation id value because we're now making
  1123. // a new SELECT query.
  1124. most_recent_reservation_id_ = 0;
  1125. // Bind values supported by parent classes.
  1126. static_cast<void>(MySqlHostWithOptionsExchange::createBindForReceive());
  1127. // reservation_id : INT UNSIGNED NOT NULL AUTO_INCREMENT
  1128. bind_[reservation_id_index_].buffer_type = MYSQL_TYPE_LONG;
  1129. bind_[reservation_id_index_].buffer = reinterpret_cast<char*>(&reservation_id_);
  1130. bind_[reservation_id_index_].is_unsigned = MLM_TRUE;
  1131. // IPv6 address/prefix VARCHAR(39)
  1132. ipv6_address_buffer_len_ = sizeof(ipv6_address_buffer_) - 1;
  1133. bind_[address_index_].buffer_type = MYSQL_TYPE_STRING;
  1134. bind_[address_index_].buffer = ipv6_address_buffer_;
  1135. bind_[address_index_].buffer_length = ipv6_address_buffer_len_;
  1136. bind_[address_index_].length = &ipv6_address_buffer_len_;
  1137. // prefix_len : TINYINT
  1138. bind_[prefix_len_index_].buffer_type = MYSQL_TYPE_TINY;
  1139. bind_[prefix_len_index_].buffer = reinterpret_cast<char*>(&prefix_len_);
  1140. bind_[prefix_len_index_].is_unsigned = MLM_TRUE;
  1141. // (reservation) type : TINYINT
  1142. reserv_type_null_ = MLM_FALSE;
  1143. bind_[type_index_].buffer_type = MYSQL_TYPE_TINY;
  1144. bind_[type_index_].buffer = reinterpret_cast<char*>(&reserv_type_);
  1145. bind_[type_index_].is_unsigned = MLM_TRUE;
  1146. bind_[type_index_].is_null = &reserv_type_null_;
  1147. // dhcp6_iaid INT UNSIGNED
  1148. bind_[iaid_index_].buffer_type = MYSQL_TYPE_LONG;
  1149. bind_[iaid_index_].buffer = reinterpret_cast<char*>(&iaid_);
  1150. bind_[iaid_index_].is_unsigned = MLM_TRUE;
  1151. // Add the error flags
  1152. setErrorIndicators(bind_, error_);
  1153. return (bind_);
  1154. };
  1155. private:
  1156. /// @brief IPv6 reservation id.
  1157. uint32_t reservation_id_;
  1158. /// @brief IPv6 reservation type.
  1159. uint8_t reserv_type_;
  1160. /// @brief Boolean flag indicating if reservation type field is null.
  1161. ///
  1162. /// This flag is used by the class to determine if the returned row
  1163. /// contains IPv6 reservation information.
  1164. my_bool reserv_type_null_;
  1165. /// @brief Buffer holding IPv6 address/prefix in textual format.
  1166. char ipv6_address_buffer_[ADDRESS6_TEXT_MAX_LEN + 1];
  1167. /// @brief Length of the textual address representation.
  1168. unsigned long ipv6_address_buffer_len_;
  1169. /// @brief Length of the prefix (128 for addresses)
  1170. uint8_t prefix_len_;
  1171. /// @brief IAID.
  1172. uint32_t iaid_;
  1173. /// @name Indexes of columns holding information about IPv6 reservations.
  1174. //@{
  1175. /// @brief Index of reservation_id column.
  1176. size_t reservation_id_index_;
  1177. /// @brief Index of address column.
  1178. size_t address_index_;
  1179. /// @brief Index of prefix_len column.
  1180. size_t prefix_len_index_;
  1181. /// @brief Index of type column.
  1182. size_t type_index_;
  1183. /// @brief Index of IAID column.
  1184. size_t iaid_index_;
  1185. //@}
  1186. /// @brief Reservation id for last processed row.
  1187. uint32_t most_recent_reservation_id_;
  1188. };
  1189. /// @brief This class is used for storing IPv6 reservations in a MySQL database.
  1190. ///
  1191. /// This class is only used to insert IPv6 reservations into the
  1192. /// ipv6_reservations table. It is not used to retrieve IPv6 reservations. To
  1193. /// retrieve IPv6 reservation the @ref MySqlIPv6HostExchange class should be
  1194. /// used instead.
  1195. ///
  1196. /// When a new IPv6 reservation is inserted into the database, an appropriate
  1197. /// host must be defined in the hosts table. An attempt to insert IPv6
  1198. /// reservation for non-existing host will result in failure.
  1199. class MySqlIPv6ReservationExchange {
  1200. private:
  1201. /// @brief Set number of columns for ipv6_reservation table.
  1202. static const size_t RESRV_COLUMNS = 6;
  1203. public:
  1204. /// @brief Constructor
  1205. ///
  1206. /// Initialize class members representing a single IPv6 reservation.
  1207. MySqlIPv6ReservationExchange()
  1208. : host_id_(0), address_("::"), address_len_(0), prefix_len_(0), type_(0),
  1209. iaid_(0), resv_(IPv6Resrv::TYPE_NA, asiolink::IOAddress("::"), 128) {
  1210. // Reset error table.
  1211. std::fill(&error_[0], &error_[RESRV_COLUMNS], MLM_FALSE);
  1212. // Set the column names (for error messages)
  1213. columns_[0] = "host_id";
  1214. columns_[1] = "address";
  1215. columns_[2] = "prefix_len";
  1216. columns_[3] = "type";
  1217. columns_[4] = "dhcp6_iaid";
  1218. BOOST_STATIC_ASSERT(4 < RESRV_COLUMNS);
  1219. }
  1220. /// @brief Create MYSQL_BIND objects for IPv6 Reservation.
  1221. ///
  1222. /// Fills in the MYSQL_BIND array for sending data in the IPv6 Reservation
  1223. /// object to the database.
  1224. ///
  1225. /// @param resv An object representing IPv6 reservation which will be
  1226. /// sent to the database.
  1227. /// None of the fields in the reservation are modified -
  1228. /// the reservation data is only read.
  1229. /// @param id ID of a host owning this reservation
  1230. ///
  1231. /// @return Vector of MySQL BIND objects representing the data to be added.
  1232. std::vector<MYSQL_BIND> createBindForSend(const IPv6Resrv& resv,
  1233. const HostID& id) {
  1234. // Store the values to ensure they remain valid.
  1235. resv_ = resv;
  1236. host_id_ = id;
  1237. // Initialize prior to constructing the array of MYSQL_BIND structures.
  1238. // It sets all fields, including is_null, to zero, so we need to set
  1239. // is_null only if it should be true. This gives up minor performance
  1240. // benefit while being safe approach. For improved readability, the
  1241. // code that explicitly sets is_null is there, but is commented out.
  1242. memset(bind_, 0, sizeof(bind_));
  1243. // Set up the structures for the various components of the host structure.
  1244. try {
  1245. // address VARCHAR(39)
  1246. address_ = resv.getPrefix().toText();
  1247. address_len_ = address_.length();
  1248. bind_[0].buffer_type = MYSQL_TYPE_BLOB;
  1249. bind_[0].buffer = reinterpret_cast<char*>
  1250. (const_cast<char*>(address_.c_str()));
  1251. bind_[0].buffer_length = address_len_;
  1252. bind_[0].length = &address_len_;
  1253. // prefix_len tinyint
  1254. prefix_len_ = resv.getPrefixLen();
  1255. bind_[1].buffer_type = MYSQL_TYPE_TINY;
  1256. bind_[1].buffer = reinterpret_cast<char*>(&prefix_len_);
  1257. bind_[1].is_unsigned = MLM_TRUE;
  1258. // type tinyint
  1259. // See lease6_types for values (0 = IA_NA, 1 = IA_TA, 2 = IA_PD)
  1260. type_ = resv.getType() == IPv6Resrv::TYPE_NA ? 0 : 2;
  1261. bind_[2].buffer_type = MYSQL_TYPE_TINY;
  1262. bind_[2].buffer = reinterpret_cast<char*>(&type_);
  1263. bind_[2].is_unsigned = MLM_TRUE;
  1264. // dhcp6_iaid INT UNSIGNED
  1265. /// @todo: We don't support iaid in the IPv6Resrv yet.
  1266. iaid_ = 0;
  1267. bind_[3].buffer_type = MYSQL_TYPE_LONG;
  1268. bind_[3].buffer = reinterpret_cast<char*>(&iaid_);
  1269. bind_[3].is_unsigned = MLM_TRUE;
  1270. // host_id INT UNSIGNED NOT NULL
  1271. bind_[4].buffer_type = MYSQL_TYPE_LONG;
  1272. bind_[4].buffer = reinterpret_cast<char*>(&host_id_);
  1273. bind_[4].is_unsigned = MLM_TRUE;
  1274. } catch (const std::exception& ex) {
  1275. isc_throw(DbOperationError,
  1276. "Could not create bind array from IPv6 Reservation: "
  1277. << resv_.toText() << ", reason: " << ex.what());
  1278. }
  1279. // Add the data to the vector. Note the end element is one after the
  1280. // end of the array.
  1281. // RESRV_COLUMNS -1 as we do not set reservation_id.
  1282. return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[RESRV_COLUMNS-1]));
  1283. }
  1284. private:
  1285. /// @brief Host unique identifier.
  1286. uint64_t host_id_;
  1287. /// @brief Address (or prefix).
  1288. std::string address_;
  1289. /// @brief Length of the textual address representation.
  1290. unsigned long address_len_;
  1291. /// @brief Length of the prefix (128 for addresses).
  1292. uint8_t prefix_len_;
  1293. /// @brief Reservation type.
  1294. uint8_t type_;
  1295. /// @brief IAID.
  1296. uint8_t iaid_;
  1297. /// @brief Object holding reservation being sent to the database.
  1298. IPv6Resrv resv_;
  1299. /// @brief Array of MySQL bindings.
  1300. MYSQL_BIND bind_[RESRV_COLUMNS];
  1301. /// @brief Array of strings holding columns names.
  1302. std::string columns_[RESRV_COLUMNS];
  1303. /// @brief Array of boolean values indicating if error occurred
  1304. /// for respective columns.
  1305. my_bool error_[RESRV_COLUMNS];
  1306. };
  1307. /// @brief This class is used for inserting options into a database.
  1308. ///
  1309. /// This class supports inserting both DHCPv4 and DHCPv6 options.
  1310. class MySqlOptionExchange {
  1311. private:
  1312. /// @brief Number of columns in the tables holding options.
  1313. static const size_t OPTION_COLUMNS = 9;
  1314. public:
  1315. /// @brief Constructor.
  1316. MySqlOptionExchange()
  1317. : type_(0), value_len_(0), formatted_value_len_(0), space_(), space_len_(0),
  1318. persistent_(false), client_class_(), client_class_len_(0),
  1319. subnet_id_(0), host_id_(0), option_() {
  1320. BOOST_STATIC_ASSERT(8 < OPTION_COLUMNS);
  1321. }
  1322. /// @brief Creates binding array to insert option data into database.
  1323. ///
  1324. /// @return Vector of MYSQL_BIND object representing an option.
  1325. std::vector<MYSQL_BIND>
  1326. createBindForSend(const OptionDescriptor& opt_desc,
  1327. const std::string& opt_space,
  1328. const OptionalValue<SubnetID>& subnet_id,
  1329. const HostID& host_id) {
  1330. // Hold pointer to the option to make sure it remains valid until
  1331. // we complete a query.
  1332. option_ = opt_desc.option_;
  1333. memset(bind_, 0, sizeof(bind_));
  1334. try {
  1335. // option_id: INT UNSIGNED NOT NULL
  1336. // The option_id is auto_incremented, so we need to pass the NULL
  1337. // value.
  1338. bind_[0].buffer_type = MYSQL_TYPE_NULL;
  1339. // code: SMALLINT UNSIGNED NOT NULL
  1340. type_ = option_->getType();
  1341. bind_[1].buffer_type = MYSQL_TYPE_SHORT;
  1342. bind_[1].buffer = reinterpret_cast<char*>(&type_);
  1343. bind_[1].is_unsigned = MLM_TRUE;
  1344. // value: BLOB NULL
  1345. if (opt_desc.formatted_value_.empty() &&
  1346. (opt_desc.option_->len() > opt_desc.option_->getHeaderLen())) {
  1347. // The formatted_value is empty and the option value is
  1348. // non-empty so we need to prepare on-wire format for the
  1349. // option and store it in the database as a blob.
  1350. OutputBuffer buf(opt_desc.option_->len());
  1351. opt_desc.option_->pack(buf);
  1352. const char* buf_ptr = static_cast<const char*>(buf.getData());
  1353. value_.assign(buf_ptr + opt_desc.option_->getHeaderLen(),
  1354. buf_ptr + buf.getLength());
  1355. value_len_ = value_.size();
  1356. bind_[2].buffer_type = MYSQL_TYPE_BLOB;
  1357. bind_[2].buffer = &value_[0];
  1358. bind_[2].buffer_length = value_len_;
  1359. bind_[2].length = &value_len_;
  1360. } else {
  1361. // No value or formatted_value specified. In this case, the
  1362. // value blob is NULL.
  1363. value_.clear();
  1364. bind_[2].buffer_type = MYSQL_TYPE_NULL;
  1365. }
  1366. // formatted_value: TEXT NULL,
  1367. if (!opt_desc.formatted_value_.empty()) {
  1368. formatted_value_len_ = opt_desc.formatted_value_.size();
  1369. bind_[3].buffer_type = MYSQL_TYPE_STRING;
  1370. bind_[3].buffer = const_cast<char*>(opt_desc.formatted_value_.c_str());
  1371. bind_[3].buffer_length = formatted_value_len_;
  1372. bind_[3].length = &formatted_value_len_;
  1373. } else {
  1374. bind_[3].buffer_type = MYSQL_TYPE_NULL;
  1375. }
  1376. // space: VARCHAR(128) NULL
  1377. space_ = opt_space;
  1378. space_len_ = space_.size();
  1379. bind_[4].buffer_type = MYSQL_TYPE_STRING;
  1380. bind_[4].buffer = const_cast<char*>(space_.c_str());
  1381. bind_[4].buffer_length = space_len_;
  1382. bind_[4].length = &space_len_;
  1383. // persistent: TINYINT(1) NOT NULL DEFAULT 0
  1384. persistent_ = opt_desc.persistent_;
  1385. bind_[5].buffer_type = MYSQL_TYPE_TINY;
  1386. bind_[5].buffer = reinterpret_cast<char*>(&persistent_);
  1387. bind_[5].is_unsigned = MLM_TRUE;
  1388. // dhcp_client_class: VARCHAR(128) NULL
  1389. /// @todo Assign actual value to client class string.
  1390. client_class_len_ = client_class_.size();
  1391. bind_[6].buffer_type = MYSQL_TYPE_STRING;
  1392. bind_[6].buffer = const_cast<char*>(client_class_.c_str());
  1393. bind_[6].buffer_length = client_class_len_;
  1394. bind_[6].length = &client_class_len_;
  1395. // dhcp4_subnet_id: INT UNSIGNED NULL
  1396. if (subnet_id.isSpecified()) {
  1397. subnet_id_ = subnet_id;
  1398. bind_[7].buffer_type = MYSQL_TYPE_LONG;
  1399. bind_[7].buffer = reinterpret_cast<char*>(subnet_id_);
  1400. bind_[7].is_unsigned = MLM_TRUE;
  1401. } else {
  1402. bind_[7].buffer_type = MYSQL_TYPE_NULL;
  1403. }
  1404. // host_id: INT UNSIGNED NOT NULL
  1405. host_id_ = host_id;
  1406. bind_[8].buffer_type = MYSQL_TYPE_LONG;
  1407. bind_[8].buffer = reinterpret_cast<char*>(&host_id_);
  1408. bind_[8].is_unsigned = MLM_TRUE;
  1409. } catch (const std::exception& ex) {
  1410. isc_throw(DbOperationError,
  1411. "Could not create bind array for inserting DHCP "
  1412. "option: " << option_->toText() << ", reason: "
  1413. << ex.what());
  1414. }
  1415. return (std::vector<MYSQL_BIND>(&bind_[0], &bind_[OPTION_COLUMNS]));
  1416. }
  1417. private:
  1418. /// @brief Option type.
  1419. uint16_t type_;
  1420. /// @brief Option value as binary.
  1421. std::vector<uint8_t> value_;
  1422. /// @brief Option value length.
  1423. unsigned long value_len_;
  1424. /// @brief Formatted option value length.
  1425. unsigned long formatted_value_len_;
  1426. /// @brief Option space name.
  1427. std::string space_;
  1428. /// @brief Option space name length.
  1429. unsigned long space_len_;
  1430. /// @brief Boolean flag indicating if the option is always returned to
  1431. /// a client or only when requested.
  1432. bool persistent_;
  1433. /// @brief Client classes for the option.
  1434. std::string client_class_;
  1435. /// @brief Length of the string holding client classes for the option.
  1436. unsigned long client_class_len_;
  1437. /// @brief Subnet identifier.
  1438. uint32_t subnet_id_;
  1439. /// @brief Host identifier.
  1440. uint32_t host_id_;
  1441. /// @brief Pointer to currently parsed option.
  1442. OptionPtr option_;
  1443. /// @brief Array of MYSQL_BIND elements representing inserted data.
  1444. MYSQL_BIND bind_[OPTION_COLUMNS];
  1445. };
  1446. } // end of anonymous namespace
  1447. namespace isc {
  1448. namespace dhcp {
  1449. /// @brief Implementation of the @ref MySqlHostDataSource.
  1450. class MySqlHostDataSourceImpl {
  1451. public:
  1452. /// @brief Statement Tags
  1453. ///
  1454. /// The contents of the enum are indexes into the list of SQL statements.
  1455. /// It is assumed that the order is such that the indicies of statements
  1456. /// reading the database are less than those of statements modifying the
  1457. /// database.
  1458. enum StatementIndex {
  1459. GET_HOST_DHCPID, // Gets hosts by host identifier
  1460. GET_HOST_ADDR, // Gets hosts by IPv4 address
  1461. GET_HOST_SUBID4_DHCPID, // Gets host by IPv4 SubnetID, HW address/DUID
  1462. GET_HOST_SUBID6_DHCPID, // Gets host by IPv6 SubnetID, HW address/DUID
  1463. GET_HOST_SUBID_ADDR, // Gets host by IPv4 SubnetID and IPv4 address
  1464. GET_HOST_PREFIX, // Gets host by IPv6 prefix
  1465. GET_HOST_SUBID6_ADDR, // Gets host by IPv6 SubnetID and IPv6 prefix
  1466. GET_VERSION, // Obtain version number
  1467. INSERT_HOST, // Insert new host to collection
  1468. INSERT_V6_RESRV, // Insert v6 reservation
  1469. INSERT_V4_OPTION, // Insert DHCPv4 option
  1470. INSERT_V6_OPTION, // Insert DHCPv6 option
  1471. NUM_STATEMENTS // Number of statements
  1472. };
  1473. /// @brief Index of first statement performing write to the database.
  1474. ///
  1475. /// This value is used to mark border line between queries and other
  1476. /// statements and statements performing write operation on the database,
  1477. /// such as INSERT, DELETE, UPDATE.
  1478. static const StatementIndex WRITE_STMTS_BEGIN = INSERT_HOST;
  1479. /// @brief Constructor.
  1480. ///
  1481. /// This constructor opens database connection and initializes prepared
  1482. /// statements used in the queries.
  1483. MySqlHostDataSourceImpl(const MySqlConnection::ParameterMap& parameters);
  1484. /// @brief Destructor.
  1485. ~MySqlHostDataSourceImpl();
  1486. /// @brief Executes statements which inserts a row into one of the tables.
  1487. ///
  1488. /// @param stindex Index of a statement being executed.
  1489. /// @param bind Vector of MYSQL_BIND objects to be used when making the
  1490. /// query.
  1491. ///
  1492. /// @throw isc::dhcp::DuplicateEntry Database throws duplicate entry error
  1493. void addStatement(MySqlHostDataSourceImpl::StatementIndex stindex,
  1494. std::vector<MYSQL_BIND>& bind);
  1495. /// @brief Inserts IPv6 Reservation into ipv6_reservation table.
  1496. ///
  1497. /// @param resv IPv6 Reservation to be added
  1498. /// @param id ID of a host owning this reservation
  1499. void addResv(const IPv6Resrv& resv, const HostID& id);
  1500. /// @brief Inserts a single DHCP option into the database.
  1501. ///
  1502. /// @param stindex Index of a statement being executed.
  1503. /// @param opt_desc Option descriptor holding information about an option
  1504. /// to be inserted into the database.
  1505. /// @param opt_space Option space name.
  1506. /// @param subnet_id Subnet identifier.
  1507. /// @param host_id Host identifier.
  1508. void addOption(const MySqlHostDataSourceImpl::StatementIndex& stindex,
  1509. const OptionDescriptor& opt_desc,
  1510. const std::string& opt_space,
  1511. const OptionalValue<SubnetID>& subnet_id,
  1512. const HostID& host_id);
  1513. /// @brief Inserts multiple options into the database.
  1514. ///
  1515. /// @param stindex Index of a statement being executed.
  1516. /// @param options_cfg An object holding a collection of options to be
  1517. /// inserted into the database.
  1518. /// @param host_id Host identifier retrieved using @c mysql_insert_id.
  1519. void addOptions(const StatementIndex& stindex, const ConstCfgOptionPtr& options_cfg,
  1520. const uint64_t host_id);
  1521. /// @brief Check Error and Throw Exception
  1522. ///
  1523. /// This method invokes @ref MySqlConnection::checkError.
  1524. ///
  1525. /// @param status Status code: non-zero implies an error
  1526. /// @param index Index of statement that caused the error
  1527. /// @param what High-level description of the error
  1528. ///
  1529. /// @throw isc::dhcp::DbOperationError An operation on the open database has
  1530. /// failed.
  1531. void checkError(const int status, const StatementIndex index,
  1532. const char* what) const;
  1533. /// @brief Creates collection of @ref Host objects with associated
  1534. /// information such as IPv6 reservations and/or DHCP options.
  1535. ///
  1536. /// This method performs a query which returns host information from
  1537. /// the 'hosts' table. The query may also use LEFT JOIN clause to
  1538. /// retrieve information from other tables, e.g. ipv6_reservations,
  1539. /// dhcp4_options and dhcp6_options.
  1540. /// Whether IPv6 reservations and/or options are assigned to the
  1541. /// @ref Host objects depends on the type of the exchange object.
  1542. ///
  1543. /// @param stindex Statement index.
  1544. /// @param bind Pointer to an array of MySQL bindings.
  1545. /// @param exchange Pointer to the exchange object used for the
  1546. /// particular query.
  1547. /// @param [out] result Reference to the collection of hosts returned.
  1548. /// @param single A boolean value indicating if a single host is
  1549. /// expected to be returned, or multiple hosts.
  1550. void getHostCollection(StatementIndex stindex, MYSQL_BIND* bind,
  1551. boost::shared_ptr<MySqlHostExchange> exchange,
  1552. ConstHostCollection& result, bool single) const;
  1553. /// @brief Retrieves a host by subnet and client's unique identifier.
  1554. ///
  1555. /// This method is used by both MySqlHostDataSource::get4 and
  1556. /// MySqlHOstDataSource::get6 methods.
  1557. ///
  1558. /// @param subnet_id Subnet identifier.
  1559. /// @param identifier_type Identifier type.
  1560. /// @param identifier_begin Pointer to a begining of a buffer containing
  1561. /// an identifier.
  1562. /// @param identifier_len Identifier length.
  1563. /// @param stindex Statement index.
  1564. /// @param exchange Pointer to the exchange object used for the
  1565. /// particular query.
  1566. ///
  1567. /// @return Pointer to const instance of Host or null pointer if
  1568. /// no host found.
  1569. ConstHostPtr getHost(const SubnetID& subnet_id,
  1570. const Host::IdentifierType& identifier_type,
  1571. const uint8_t* identifier_begin,
  1572. const size_t identifier_len,
  1573. StatementIndex stindex,
  1574. boost::shared_ptr<MySqlHostExchange> exchange) const;
  1575. /// @brief Throws exception if database is read only.
  1576. ///
  1577. /// This method should be called by the methods which write to the
  1578. /// database. If the backend is operating in read-only mode this
  1579. /// method will throw exception.
  1580. ///
  1581. /// @throw DbReadOnly if backend is operating in read only mode.
  1582. void checkReadOnly() const;
  1583. /// @brief Pointer to the object representing an exchange which
  1584. /// can be used to retrieve hosts and DHCPv4 options.
  1585. boost::shared_ptr<MySqlHostWithOptionsExchange> host_exchange_;
  1586. /// @brief Pointer to an object representing an exchange which can
  1587. /// be used to retrieve hosts, DHCPv6 options and IPv6 reservations.
  1588. boost::shared_ptr<MySqlHostIPv6Exchange> host_ipv6_exchange_;
  1589. /// @brief Pointer to an object representing an exchange which can
  1590. /// be used to retrieve hosts, DHCPv4 and DHCPv6 options, and
  1591. /// IPv6 reservations using a single query.
  1592. boost::shared_ptr<MySqlHostIPv6Exchange> host_ipv46_exchange_;
  1593. /// @brief Pointer to an object representing an exchange which can
  1594. /// be used to insert new IPv6 reservation.
  1595. boost::shared_ptr<MySqlIPv6ReservationExchange> host_ipv6_reservation_exchange_;
  1596. /// @brief Pointer to an object representing an exchange which can
  1597. /// be used to insert DHCPv4 or DHCPv6 option into dhcp4_options
  1598. /// or dhcp6_options table.
  1599. boost::shared_ptr<MySqlOptionExchange> host_option_exchange_;
  1600. /// @brief MySQL connection
  1601. MySqlConnection conn_;
  1602. /// @brief Indicates if the database is opened in read only mode.
  1603. bool is_readonly_;
  1604. };
  1605. /// @brief Array of tagged statements.
  1606. typedef boost::array<TaggedStatement, MySqlHostDataSourceImpl::NUM_STATEMENTS>
  1607. TaggedStatementArray;
  1608. /// @brief Prepared MySQL statements used by the backend to insert and
  1609. /// retrieve hosts from the database.
  1610. TaggedStatementArray tagged_statements = { {
  1611. // Retrieves host information, IPv6 reservations and both DHCPv4 and
  1612. // DHCPv6 options associated with the host. The LEFT JOIN clause is used
  1613. // to retrieve information from 4 different tables using a single query.
  1614. // Hence, this query returns multiple rows for a single host.
  1615. {MySqlHostDataSourceImpl::GET_HOST_DHCPID,
  1616. "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
  1617. "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, "
  1618. "h.hostname, h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1619. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1620. "o4.option_id, o4.code, o4.value, o4.formatted_value, o4.space, "
  1621. "o4.persistent, "
  1622. "o6.option_id, o6.code, o6.value, o6.formatted_value, o6.space, "
  1623. "o6.persistent, "
  1624. "r.reservation_id, r.address, r.prefix_len, r.type, "
  1625. "r.dhcp6_iaid "
  1626. "FROM hosts AS h "
  1627. "LEFT JOIN dhcp4_options AS o4 "
  1628. "ON h.host_id = o4.host_id "
  1629. "LEFT JOIN dhcp6_options AS o6 "
  1630. "ON h.host_id = o6.host_id "
  1631. "LEFT JOIN ipv6_reservations AS r "
  1632. "ON h.host_id = r.host_id "
  1633. "WHERE dhcp_identifier = ? AND dhcp_identifier_type = ? "
  1634. "ORDER BY h.host_id, o4.option_id, o6.option_id, r.reservation_id"},
  1635. // Retrieves host information along with the DHCPv4 options associated with
  1636. // it. Left joining the dhcp4_options table results in multiple rows being
  1637. // returned for the same host. The host is retrieved by IPv4 address.
  1638. {MySqlHostDataSourceImpl::GET_HOST_ADDR,
  1639. "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
  1640. "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1641. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1642. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1643. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1644. "o.persistent "
  1645. "FROM hosts AS h "
  1646. "LEFT JOIN dhcp4_options AS o "
  1647. "ON h.host_id = o.host_id "
  1648. "WHERE ipv4_address = ? "
  1649. "ORDER BY h.host_id, o.option_id"},
  1650. // Retrieves host information and DHCPv4 options using subnet identifier
  1651. // and client's identifier. Left joining the dhcp4_options table results in
  1652. // multiple rows being returned for the same host.
  1653. {MySqlHostDataSourceImpl::GET_HOST_SUBID4_DHCPID,
  1654. "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
  1655. "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1656. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1657. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1658. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1659. "o.persistent "
  1660. "FROM hosts AS h "
  1661. "LEFT JOIN dhcp4_options AS o "
  1662. "ON h.host_id = o.host_id "
  1663. "WHERE h.dhcp4_subnet_id = ? AND h.dhcp_identifier_type = ? "
  1664. " AND h.dhcp_identifier = ? "
  1665. "ORDER BY h.host_id, o.option_id"},
  1666. // Retrieves host information, IPv6 reservations and DHCPv6 options
  1667. // associated with a host. The number of rows returned is a multiplication
  1668. // of number of IPv6 reservations and DHCPv6 options.
  1669. {MySqlHostDataSourceImpl::GET_HOST_SUBID6_DHCPID,
  1670. "SELECT h.host_id, h.dhcp_identifier, "
  1671. "h.dhcp_identifier_type, h.dhcp4_subnet_id, "
  1672. "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1673. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1674. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1675. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1676. "o.persistent, "
  1677. "r.reservation_id, r.address, r.prefix_len, r.type, "
  1678. "r.dhcp6_iaid "
  1679. "FROM hosts AS h "
  1680. "LEFT JOIN dhcp6_options AS o "
  1681. "ON h.host_id = o.host_id "
  1682. "LEFT JOIN ipv6_reservations AS r "
  1683. "ON h.host_id = r.host_id "
  1684. "WHERE h.dhcp6_subnet_id = ? AND h.dhcp_identifier_type = ? "
  1685. "AND h.dhcp_identifier = ? "
  1686. "ORDER BY h.host_id, o.option_id, r.reservation_id"},
  1687. // Retrieves host information and DHCPv4 options for the host using subnet
  1688. // identifier and IPv4 reservation. Left joining the dhcp4_options table
  1689. // results in multiple rows being returned for the host. The number of
  1690. // rows depends on the number of options defined for the host.
  1691. {MySqlHostDataSourceImpl::GET_HOST_SUBID_ADDR,
  1692. "SELECT h.host_id, h.dhcp_identifier, h.dhcp_identifier_type, "
  1693. "h.dhcp4_subnet_id, h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1694. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1695. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1696. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1697. "o.persistent "
  1698. "FROM hosts AS h "
  1699. "LEFT JOIN dhcp4_options AS o "
  1700. "ON h.host_id = o.host_id "
  1701. "WHERE h.dhcp4_subnet_id = ? AND h.ipv4_address = ? "
  1702. "ORDER BY h.host_id, o.option_id"},
  1703. // Retrieves host information, IPv6 reservations and DHCPv6 options
  1704. // associated with a host using prefix and prefix length. This query
  1705. // returns host information for a single host. However, multiple rows
  1706. // are returned due to left joining IPv6 reservations and DHCPv6 options.
  1707. // The number of rows returned is multiplication of number of existing
  1708. // IPv6 reservations and DHCPv6 options.
  1709. {MySqlHostDataSourceImpl::GET_HOST_PREFIX,
  1710. "SELECT h.host_id, h.dhcp_identifier, "
  1711. "h.dhcp_identifier_type, h.dhcp4_subnet_id, "
  1712. "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1713. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1714. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1715. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1716. "o.persistent, "
  1717. "r.reservation_id, r.address, r.prefix_len, r.type, "
  1718. "r.dhcp6_iaid "
  1719. "FROM hosts AS h "
  1720. "LEFT JOIN dhcp6_options AS o "
  1721. "ON h.host_id = o.host_id "
  1722. "LEFT JOIN ipv6_reservations AS r "
  1723. "ON h.host_id = r.host_id "
  1724. "WHERE h.host_id = "
  1725. "(SELECT host_id FROM ipv6_reservations "
  1726. "WHERE address = ? AND prefix_len = ?) "
  1727. "ORDER BY h.host_id, o.option_id, r.reservation_id"},
  1728. // Retrieves host information, IPv6 reservations and DHCPv6 options
  1729. // associated with a host using subnet id and prefix. This query
  1730. // returns host information for a single host. However, multiple rows
  1731. // are returned due to left joining IPv6 reservations and DHCPv6 options.
  1732. // The number of rows returned is multiplication of number of existing
  1733. // IPv6 reservations and DHCPv6 options.
  1734. {MySqlHostDataSourceImpl::GET_HOST_SUBID6_ADDR,
  1735. "SELECT h.host_id, h.dhcp_identifier, "
  1736. "h.dhcp_identifier_type, h.dhcp4_subnet_id, "
  1737. "h.dhcp6_subnet_id, h.ipv4_address, h.hostname, "
  1738. "h.dhcp4_client_classes, h.dhcp6_client_classes, "
  1739. "h.dhcp4_next_server, h.dhcp4_server_hostname, h.dhcp4_boot_file_name, "
  1740. "o.option_id, o.code, o.value, o.formatted_value, o.space, "
  1741. "o.persistent, "
  1742. "r.reservation_id, r.address, r.prefix_len, r.type, "
  1743. "r.dhcp6_iaid "
  1744. "FROM hosts AS h "
  1745. "LEFT JOIN dhcp6_options AS o "
  1746. "ON h.host_id = o.host_id "
  1747. "LEFT JOIN ipv6_reservations AS r "
  1748. "ON h.host_id = r.host_id "
  1749. "WHERE h.dhcp6_subnet_id = ? AND r.address = ? "
  1750. "ORDER BY h.host_id, o.option_id, r.reservation_id"},
  1751. // Retrieves MySQL schema version.
  1752. {MySqlHostDataSourceImpl::GET_VERSION,
  1753. "SELECT version, minor FROM schema_version"},
  1754. // Inserts a host into the 'hosts' table.
  1755. {MySqlHostDataSourceImpl::INSERT_HOST,
  1756. "INSERT INTO hosts(host_id, dhcp_identifier, dhcp_identifier_type, "
  1757. "dhcp4_subnet_id, dhcp6_subnet_id, ipv4_address, hostname, "
  1758. "dhcp4_client_classes, dhcp6_client_classes, dhcp4_next_server, "
  1759. "dhcp4_server_hostname, dhcp4_boot_file_name) "
  1760. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"},
  1761. // Inserts a single IPv6 reservation into 'reservations' table.
  1762. {MySqlHostDataSourceImpl::INSERT_V6_RESRV,
  1763. "INSERT INTO ipv6_reservations(address, prefix_len, type, "
  1764. "dhcp6_iaid, host_id) "
  1765. "VALUES (?,?,?,?,?)"},
  1766. // Inserts a single DHCPv4 option into 'dhcp4_options' table.
  1767. // Using fixed scope_id = 3, which associates an option with host.
  1768. {MySqlHostDataSourceImpl::INSERT_V4_OPTION,
  1769. "INSERT INTO dhcp4_options(option_id, code, value, formatted_value, space, "
  1770. "persistent, dhcp_client_class, dhcp4_subnet_id, host_id, scope_id) "
  1771. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 3)"},
  1772. // Inserts a single DHCPv6 option into 'dhcp6_options' table.
  1773. // Using fixed scope_id = 3, which associates an option with host.
  1774. {MySqlHostDataSourceImpl::INSERT_V6_OPTION,
  1775. "INSERT INTO dhcp6_options(option_id, code, value, formatted_value, space, "
  1776. "persistent, dhcp_client_class, dhcp6_subnet_id, host_id, scope_id) "
  1777. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 3)"}}
  1778. };
  1779. MySqlHostDataSourceImpl::
  1780. MySqlHostDataSourceImpl(const MySqlConnection::ParameterMap& parameters)
  1781. : host_exchange_(new MySqlHostWithOptionsExchange(MySqlHostWithOptionsExchange::DHCP4_ONLY)),
  1782. host_ipv6_exchange_(new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::DHCP6_ONLY)),
  1783. host_ipv46_exchange_(new MySqlHostIPv6Exchange(MySqlHostWithOptionsExchange::
  1784. DHCP4_AND_DHCP6)),
  1785. host_ipv6_reservation_exchange_(new MySqlIPv6ReservationExchange()),
  1786. host_option_exchange_(new MySqlOptionExchange()),
  1787. conn_(parameters),
  1788. is_readonly_(false) {
  1789. // Open the database.
  1790. conn_.openDatabase();
  1791. // Enable autocommit. In case transaction is explicitly used, this
  1792. // setting will be overwritten for the transaction. However, there are
  1793. // cases when lack of autocommit could cause transactions to hang
  1794. // until commit or rollback is explicitly called. This already
  1795. // caused issues for some unit tests which were unable to cleanup
  1796. // the database after the test because of pending transactions.
  1797. // Use of autocommit will eliminate this problem.
  1798. my_bool result = mysql_autocommit(conn_.mysql_, 1);
  1799. if (result != 0) {
  1800. isc_throw(DbOperationError, mysql_error(conn_.mysql_));
  1801. }
  1802. // Prepare query statements. Those are will be only used to retrieve
  1803. // information from the database, so they can be used even if the
  1804. // database is read only for the current user.
  1805. conn_.prepareStatements(tagged_statements.begin(),
  1806. tagged_statements.begin() + WRITE_STMTS_BEGIN);
  1807. // Check if the backend is explicitly configured to operate with
  1808. // read only access to the database.
  1809. is_readonly_ = conn_.configuredReadOnly();
  1810. // If we are using read-write mode for the database we also prepare
  1811. // statements for INSERTS etc.
  1812. if (!is_readonly_) {
  1813. // Prepare statements for writing to the database, e.g. INSERT.
  1814. conn_.prepareStatements(tagged_statements.begin() + WRITE_STMTS_BEGIN,
  1815. tagged_statements.end());
  1816. } else {
  1817. LOG_INFO(dhcpsrv_logger, DHCPSRV_MYSQL_HOST_DB_READONLY);
  1818. }
  1819. }
  1820. MySqlHostDataSourceImpl::~MySqlHostDataSourceImpl() {
  1821. // Free up the prepared statements, ignoring errors. (What would we do
  1822. // about them? We're destroying this object and are not really concerned
  1823. // with errors on a database connection that is about to go away.)
  1824. for (int i = 0; i < conn_.statements_.size(); ++i) {
  1825. if (conn_.statements_[i] != NULL) {
  1826. (void) mysql_stmt_close(conn_.statements_[i]);
  1827. conn_.statements_[i] = NULL;
  1828. }
  1829. }
  1830. // There is no need to close the database in this destructor: it is
  1831. // closed in the destructor of the mysql_ member variable.
  1832. }
  1833. void
  1834. MySqlHostDataSourceImpl::addStatement(StatementIndex stindex,
  1835. std::vector<MYSQL_BIND>& bind) {
  1836. // Bind the parameters to the statement
  1837. int status = mysql_stmt_bind_param(conn_.statements_[stindex], &bind[0]);
  1838. checkError(status, stindex, "unable to bind parameters");
  1839. // Execute the statement
  1840. status = mysql_stmt_execute(conn_.statements_[stindex]);
  1841. if (status != 0) {
  1842. // Failure: check for the special case of duplicate entry.
  1843. if (mysql_errno(conn_.mysql_) == ER_DUP_ENTRY) {
  1844. isc_throw(DuplicateEntry, "Database duplicate entry error");
  1845. }
  1846. checkError(status, stindex, "unable to execute");
  1847. }
  1848. }
  1849. void
  1850. MySqlHostDataSourceImpl::addResv(const IPv6Resrv& resv,
  1851. const HostID& id) {
  1852. std::vector<MYSQL_BIND> bind =
  1853. host_ipv6_reservation_exchange_->createBindForSend(resv, id);
  1854. addStatement(INSERT_V6_RESRV, bind);
  1855. }
  1856. void
  1857. MySqlHostDataSourceImpl::addOption(const StatementIndex& stindex,
  1858. const OptionDescriptor& opt_desc,
  1859. const std::string& opt_space,
  1860. const OptionalValue<SubnetID>& subnet_id,
  1861. const HostID& id) {
  1862. std::vector<MYSQL_BIND> bind =
  1863. host_option_exchange_->createBindForSend(opt_desc, opt_space,
  1864. subnet_id, id);
  1865. addStatement(stindex, bind);
  1866. }
  1867. void
  1868. MySqlHostDataSourceImpl::addOptions(const StatementIndex& stindex,
  1869. const ConstCfgOptionPtr& options_cfg,
  1870. const uint64_t host_id) {
  1871. // Get option space names and vendor space names and combine them within a
  1872. // single list.
  1873. std::list<std::string> option_spaces = options_cfg->getOptionSpaceNames();
  1874. std::list<std::string> vendor_spaces = options_cfg->getVendorIdsSpaceNames();
  1875. option_spaces.insert(option_spaces.end(), vendor_spaces.begin(),
  1876. vendor_spaces.end());
  1877. // For each option space retrieve all options and insert them into the
  1878. // database.
  1879. for (std::list<std::string>::const_iterator space = option_spaces.begin();
  1880. space != option_spaces.end(); ++space) {
  1881. OptionContainerPtr options = options_cfg->getAll(*space);
  1882. if (options && !options->empty()) {
  1883. for (OptionContainer::const_iterator opt = options->begin();
  1884. opt != options->end(); ++opt) {
  1885. addOption(stindex, *opt, *space, OptionalValue<SubnetID>(),
  1886. host_id);
  1887. }
  1888. }
  1889. }
  1890. }
  1891. void
  1892. MySqlHostDataSourceImpl::
  1893. checkError(const int status, const StatementIndex index,
  1894. const char* what) const {
  1895. conn_.checkError(status, index, what);
  1896. }
  1897. void
  1898. MySqlHostDataSourceImpl::
  1899. getHostCollection(StatementIndex stindex, MYSQL_BIND* bind,
  1900. boost::shared_ptr<MySqlHostExchange> exchange,
  1901. ConstHostCollection& result, bool single) const {
  1902. // Bind the selection parameters to the statement
  1903. int status = mysql_stmt_bind_param(conn_.statements_[stindex], bind);
  1904. checkError(status, stindex, "unable to bind WHERE clause parameter");
  1905. // Set up the MYSQL_BIND array for the data being returned and bind it to
  1906. // the statement.
  1907. std::vector<MYSQL_BIND> outbind = exchange->createBindForReceive();
  1908. status = mysql_stmt_bind_result(conn_.statements_[stindex], &outbind[0]);
  1909. checkError(status, stindex, "unable to bind SELECT clause parameters");
  1910. // Execute the statement
  1911. status = mysql_stmt_execute(conn_.statements_[stindex]);
  1912. checkError(status, stindex, "unable to execute");
  1913. // Ensure that all the lease information is retrieved in one go to avoid
  1914. // overhead of going back and forth between client and server.
  1915. status = mysql_stmt_store_result(conn_.statements_[stindex]);
  1916. checkError(status, stindex, "unable to set up for storing all results");
  1917. // Set up the fetch "release" object to release resources associated
  1918. // with the call to mysql_stmt_fetch when this method exits, then
  1919. // retrieve the data. mysql_stmt_fetch return value equal to 0 represents
  1920. // successful data fetch.
  1921. MySqlFreeResult fetch_release(conn_.statements_[stindex]);
  1922. while ((status = mysql_stmt_fetch(conn_.statements_[stindex])) ==
  1923. MLM_MYSQL_FETCH_SUCCESS) {
  1924. try {
  1925. exchange->processFetchedData(result);
  1926. } catch (const isc::BadValue& ex) {
  1927. // Rethrow the exception with a bit more data.
  1928. isc_throw(BadValue, ex.what() << ". Statement is <" <<
  1929. conn_.text_statements_[stindex] << ">");
  1930. }
  1931. if (single && (result.size() > 1)) {
  1932. isc_throw(MultipleRecords, "multiple records were found in the "
  1933. "database where only one was expected for query "
  1934. << conn_.text_statements_[stindex]);
  1935. }
  1936. }
  1937. // How did the fetch end?
  1938. // If mysql_stmt_fetch return value is equal to 1 an error occurred.
  1939. if (status == MLM_MYSQL_FETCH_FAILURE) {
  1940. // Error - unable to fetch results
  1941. checkError(status, stindex, "unable to fetch results");
  1942. } else if (status == MYSQL_DATA_TRUNCATED) {
  1943. // Data truncated - throw an exception indicating what was at fault
  1944. isc_throw(DataTruncated, conn_.text_statements_[stindex]
  1945. << " returned truncated data: columns affected are "
  1946. << exchange->getErrorColumns());
  1947. }
  1948. }
  1949. ConstHostPtr
  1950. MySqlHostDataSourceImpl::
  1951. getHost(const SubnetID& subnet_id,
  1952. const Host::IdentifierType& identifier_type,
  1953. const uint8_t* identifier_begin,
  1954. const size_t identifier_len,
  1955. StatementIndex stindex,
  1956. boost::shared_ptr<MySqlHostExchange> exchange) const {
  1957. // Set up the WHERE clause value
  1958. MYSQL_BIND inbind[3];
  1959. memset(inbind, 0, sizeof(inbind));
  1960. uint32_t subnet_buffer = static_cast<uint32_t>(subnet_id);
  1961. inbind[0].buffer_type = MYSQL_TYPE_LONG;
  1962. inbind[0].buffer = reinterpret_cast<char*>(&subnet_buffer);
  1963. inbind[0].is_unsigned = MLM_TRUE;
  1964. // Identifier value.
  1965. std::vector<char> identifier_vec(identifier_begin,
  1966. identifier_begin + identifier_len);
  1967. unsigned long length = identifier_vec.size();
  1968. inbind[2].buffer_type = MYSQL_TYPE_BLOB;
  1969. inbind[2].buffer = &identifier_vec[0];
  1970. inbind[2].buffer_length = length;
  1971. inbind[2].length = &length;
  1972. // Identifier type.
  1973. char identifier_type_copy = static_cast<char>(identifier_type);
  1974. inbind[1].buffer_type = MYSQL_TYPE_TINY;
  1975. inbind[1].buffer = reinterpret_cast<char*>(&identifier_type_copy);
  1976. inbind[1].is_unsigned = MLM_TRUE;
  1977. ConstHostCollection collection;
  1978. getHostCollection(stindex, inbind, exchange, collection, true);
  1979. // Return single record if present, else clear the host.
  1980. ConstHostPtr result;
  1981. if (!collection.empty())
  1982. result = *collection.begin();
  1983. return (result);
  1984. }
  1985. void
  1986. MySqlHostDataSourceImpl::checkReadOnly() const {
  1987. if (is_readonly_) {
  1988. isc_throw(ReadOnlyDb, "MySQL host database backend is configured to"
  1989. " operate in read only mode");
  1990. }
  1991. }
  1992. MySqlHostDataSource::
  1993. MySqlHostDataSource(const MySqlConnection::ParameterMap& parameters)
  1994. : impl_(new MySqlHostDataSourceImpl(parameters)) {
  1995. }
  1996. MySqlHostDataSource::~MySqlHostDataSource() {
  1997. delete impl_;
  1998. }
  1999. void
  2000. MySqlHostDataSource::add(const HostPtr& host) {
  2001. // If operating in read-only mode, throw exception.
  2002. impl_->checkReadOnly();
  2003. // Initiate MySQL transaction as we will have to make multiple queries
  2004. // to insert host information into multiple tables. If that fails on
  2005. // any stage, the transaction will be rolled back by the destructor of
  2006. // the MySqlTransaction class.
  2007. MySqlTransaction transaction(impl_->conn_);
  2008. // Create the MYSQL_BIND array for the host
  2009. std::vector<MYSQL_BIND> bind = impl_->host_exchange_->createBindForSend(host);
  2010. // ... and insert the host.
  2011. impl_->addStatement(MySqlHostDataSourceImpl::INSERT_HOST, bind);
  2012. // Gets the last inserted hosts id
  2013. uint64_t host_id = mysql_insert_id(impl_->conn_.mysql_);
  2014. // Insert DHCPv4 options.
  2015. ConstCfgOptionPtr cfg_option4 = host->getCfgOption4();
  2016. if (cfg_option4) {
  2017. impl_->addOptions(MySqlHostDataSourceImpl::INSERT_V4_OPTION,
  2018. cfg_option4, host_id);
  2019. }
  2020. // Insert DHCPv6 options.
  2021. ConstCfgOptionPtr cfg_option6 = host->getCfgOption6();
  2022. if (cfg_option6) {
  2023. impl_->addOptions(MySqlHostDataSourceImpl::INSERT_V6_OPTION,
  2024. cfg_option6, host_id);
  2025. }
  2026. // Insert IPv6 reservations.
  2027. IPv6ResrvRange v6resv = host->getIPv6Reservations();
  2028. if (std::distance(v6resv.first, v6resv.second) > 0) {
  2029. for (IPv6ResrvIterator resv = v6resv.first; resv != v6resv.second;
  2030. ++resv) {
  2031. impl_->addResv(resv->second, host_id);
  2032. }
  2033. }
  2034. // Everything went fine, so explicitly commit the transaction.
  2035. transaction.commit();
  2036. }
  2037. ConstHostCollection
  2038. MySqlHostDataSource::getAll(const HWAddrPtr& hwaddr,
  2039. const DuidPtr& duid) const {
  2040. if (duid){
  2041. return (getAll(Host::IDENT_DUID, &duid->getDuid()[0],
  2042. duid->getDuid().size()));
  2043. } else if (hwaddr) {
  2044. return (getAll(Host::IDENT_HWADDR,
  2045. &hwaddr->hwaddr_[0],
  2046. hwaddr->hwaddr_.size()));
  2047. }
  2048. return (ConstHostCollection());
  2049. }
  2050. ConstHostCollection
  2051. MySqlHostDataSource::getAll(const Host::IdentifierType& identifier_type,
  2052. const uint8_t* identifier_begin,
  2053. const size_t identifier_len) const {
  2054. // Set up the WHERE clause value
  2055. MYSQL_BIND inbind[2];
  2056. memset(inbind, 0, sizeof(inbind));
  2057. // Identifier type.
  2058. char identifier_type_copy = static_cast<char>(identifier_type);
  2059. inbind[1].buffer = &identifier_type_copy;
  2060. inbind[1].buffer_type = MYSQL_TYPE_TINY;
  2061. inbind[1].is_unsigned = MLM_TRUE;
  2062. // Identifier value.
  2063. std::vector<char> identifier_vec(identifier_begin,
  2064. identifier_begin + identifier_len);
  2065. unsigned long int length = identifier_vec.size();
  2066. inbind[0].buffer_type = MYSQL_TYPE_BLOB;
  2067. inbind[0].buffer = &identifier_vec[0];
  2068. inbind[0].buffer_length = length;
  2069. inbind[0].length = &length;
  2070. ConstHostCollection result;
  2071. impl_->getHostCollection(MySqlHostDataSourceImpl::GET_HOST_DHCPID, inbind,
  2072. impl_->host_ipv46_exchange_,
  2073. result, false);
  2074. return (result);
  2075. }
  2076. ConstHostCollection
  2077. MySqlHostDataSource::getAll4(const asiolink::IOAddress& address) const {
  2078. // Set up the WHERE clause value
  2079. MYSQL_BIND inbind[1];
  2080. memset(inbind, 0, sizeof(inbind));
  2081. uint32_t addr4 = address.toUint32();
  2082. inbind[0].buffer_type = MYSQL_TYPE_LONG;
  2083. inbind[0].buffer = reinterpret_cast<char*>(&addr4);
  2084. inbind[0].is_unsigned = MLM_TRUE;
  2085. ConstHostCollection result;
  2086. impl_->getHostCollection(MySqlHostDataSourceImpl::GET_HOST_ADDR, inbind,
  2087. impl_->host_exchange_, result, false);
  2088. return (result);
  2089. }
  2090. ConstHostPtr
  2091. MySqlHostDataSource::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  2092. const DuidPtr& duid) const {
  2093. /// @todo: Rethink the logic in BaseHostDataSource::get4(subnet, hwaddr, duid)
  2094. if (hwaddr && duid) {
  2095. isc_throw(BadValue, "MySQL host data source get4() called with both"
  2096. " hwaddr and duid, only one of them is allowed");
  2097. }
  2098. if (!hwaddr && !duid) {
  2099. isc_throw(BadValue, "MySQL host data source get4() called with "
  2100. "neither hwaddr or duid specified, one of them is required");
  2101. }
  2102. // Choosing one of the identifiers
  2103. if (hwaddr) {
  2104. return (get4(subnet_id, Host::IDENT_HWADDR, &hwaddr->hwaddr_[0],
  2105. hwaddr->hwaddr_.size()));
  2106. } else if (duid) {
  2107. return (get4(subnet_id, Host::IDENT_DUID, &duid->getDuid()[0],
  2108. duid->getDuid().size()));
  2109. }
  2110. return (ConstHostPtr());
  2111. }
  2112. ConstHostPtr
  2113. MySqlHostDataSource::get4(const SubnetID& subnet_id,
  2114. const Host::IdentifierType& identifier_type,
  2115. const uint8_t* identifier_begin,
  2116. const size_t identifier_len) const {
  2117. return (impl_->getHost(subnet_id, identifier_type, identifier_begin,
  2118. identifier_len, MySqlHostDataSourceImpl::GET_HOST_SUBID4_DHCPID,
  2119. impl_->host_exchange_));
  2120. }
  2121. ConstHostPtr
  2122. MySqlHostDataSource::get4(const SubnetID& subnet_id,
  2123. const asiolink::IOAddress& address) const {
  2124. /// @todo: check that address is really v4, not v6.
  2125. // Set up the WHERE clause value
  2126. MYSQL_BIND inbind[2];
  2127. uint32_t subnet = subnet_id;
  2128. memset(inbind, 0, sizeof(inbind));
  2129. inbind[0].buffer_type = MYSQL_TYPE_LONG;
  2130. inbind[0].buffer = reinterpret_cast<char*>(&subnet);
  2131. inbind[0].is_unsigned = MLM_TRUE;
  2132. uint32_t addr4 = address.toUint32();
  2133. inbind[1].buffer_type = MYSQL_TYPE_LONG;
  2134. inbind[1].buffer = reinterpret_cast<char*>(&addr4);
  2135. inbind[1].is_unsigned = MLM_TRUE;
  2136. ConstHostCollection collection;
  2137. impl_->getHostCollection(MySqlHostDataSourceImpl::GET_HOST_SUBID_ADDR,
  2138. inbind, impl_->host_exchange_, collection, true);
  2139. // Return single record if present, else clear the host.
  2140. ConstHostPtr result;
  2141. if (!collection.empty())
  2142. result = *collection.begin();
  2143. return (result);
  2144. }
  2145. ConstHostPtr
  2146. MySqlHostDataSource::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  2147. const HWAddrPtr& hwaddr) const {
  2148. /// @todo: Rethink the logic in BaseHostDataSource::get6(subnet, hwaddr, duid)
  2149. if (hwaddr && duid) {
  2150. isc_throw(BadValue, "MySQL host data source get6() called with both"
  2151. " hwaddr and duid, only one of them is allowed");
  2152. }
  2153. if (!hwaddr && !duid) {
  2154. isc_throw(BadValue, "MySQL host data source get6() called with "
  2155. "neither hwaddr or duid specified, one of them is required");
  2156. }
  2157. // Choosing one of the identifiers
  2158. if (hwaddr) {
  2159. return (get6(subnet_id, Host::IDENT_HWADDR, &hwaddr->hwaddr_[0],
  2160. hwaddr->hwaddr_.size()));
  2161. } else if (duid) {
  2162. return (get6(subnet_id, Host::IDENT_DUID, &duid->getDuid()[0],
  2163. duid->getDuid().size()));
  2164. }
  2165. return (ConstHostPtr());
  2166. }
  2167. ConstHostPtr
  2168. MySqlHostDataSource::get6(const SubnetID& subnet_id,
  2169. const Host::IdentifierType& identifier_type,
  2170. const uint8_t* identifier_begin,
  2171. const size_t identifier_len) const {
  2172. return (impl_->getHost(subnet_id, identifier_type, identifier_begin,
  2173. identifier_len, MySqlHostDataSourceImpl::GET_HOST_SUBID6_DHCPID,
  2174. impl_->host_ipv6_exchange_));
  2175. }
  2176. ConstHostPtr
  2177. MySqlHostDataSource::get6(const asiolink::IOAddress& prefix,
  2178. const uint8_t prefix_len) const {
  2179. /// @todo: Check that prefix is v6 address, not v4.
  2180. // Set up the WHERE clause value
  2181. MYSQL_BIND inbind[2];
  2182. memset(inbind, 0, sizeof(inbind));
  2183. std::string addr6 = prefix.toText();
  2184. unsigned long addr6_length = addr6.size();
  2185. inbind[0].buffer_type = MYSQL_TYPE_BLOB;
  2186. inbind[0].buffer = reinterpret_cast<char*>
  2187. (const_cast<char*>(addr6.c_str()));
  2188. inbind[0].length = &addr6_length;
  2189. inbind[0].buffer_length = addr6_length;
  2190. uint8_t tmp = prefix_len;
  2191. inbind[1].buffer_type = MYSQL_TYPE_TINY;
  2192. inbind[1].buffer = reinterpret_cast<char*>(&tmp);
  2193. inbind[1].is_unsigned = MLM_TRUE;
  2194. ConstHostCollection collection;
  2195. impl_->getHostCollection(MySqlHostDataSourceImpl::GET_HOST_PREFIX,
  2196. inbind, impl_->host_ipv6_exchange_,
  2197. collection, true);
  2198. // Return single record if present, else clear the host.
  2199. ConstHostPtr result;
  2200. if (!collection.empty()) {
  2201. result = *collection.begin();
  2202. }
  2203. return (result);
  2204. }
  2205. ConstHostPtr
  2206. MySqlHostDataSource::get6(const SubnetID& subnet_id,
  2207. const asiolink::IOAddress& address) const {
  2208. // Set up the WHERE clause value
  2209. MYSQL_BIND inbind[2];
  2210. memset(inbind, 0, sizeof(inbind));
  2211. uint32_t subnet_buffer = static_cast<uint32_t>(subnet_id);
  2212. inbind[0].buffer_type = MYSQL_TYPE_LONG;
  2213. inbind[0].buffer = reinterpret_cast<char*>(&subnet_buffer);
  2214. inbind[0].is_unsigned = MLM_TRUE;
  2215. std::string addr6 = address.toText();
  2216. unsigned long addr6_length = addr6.size();
  2217. inbind[1].buffer_type = MYSQL_TYPE_BLOB;
  2218. inbind[1].buffer = reinterpret_cast<char*>
  2219. (const_cast<char*>(addr6.c_str()));
  2220. inbind[1].length = &addr6_length;
  2221. inbind[1].buffer_length = addr6_length;
  2222. ConstHostCollection collection;
  2223. impl_->getHostCollection(MySqlHostDataSourceImpl::GET_HOST_SUBID6_ADDR,
  2224. inbind, impl_->host_ipv6_exchange_,
  2225. collection, true);
  2226. // Return single record if present, else clear the host.
  2227. ConstHostPtr result;
  2228. if (!collection.empty()) {
  2229. result = *collection.begin();
  2230. }
  2231. return (result);
  2232. }
  2233. // Miscellaneous database methods.
  2234. std::string MySqlHostDataSource::getName() const {
  2235. std::string name = "";
  2236. try {
  2237. name = impl_->conn_.getParameter("name");
  2238. } catch (...) {
  2239. // Return an empty name
  2240. }
  2241. return (name);
  2242. }
  2243. std::string MySqlHostDataSource::getDescription() const {
  2244. return (std::string("Host data source that stores host information"
  2245. "in MySQL database"));
  2246. }
  2247. std::pair<uint32_t, uint32_t> MySqlHostDataSource::getVersion() const {
  2248. const MySqlHostDataSourceImpl::StatementIndex stindex =
  2249. MySqlHostDataSourceImpl::GET_VERSION;
  2250. LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE_DETAIL,
  2251. DHCPSRV_MYSQL_HOST_DB_GET_VERSION);
  2252. uint32_t major; // Major version number
  2253. uint32_t minor; // Minor version number
  2254. // Execute the prepared statement
  2255. int status = mysql_stmt_execute(impl_->conn_.statements_[stindex]);
  2256. if (status != 0) {
  2257. isc_throw(DbOperationError, "unable to execute <"
  2258. << impl_->conn_.text_statements_[stindex]
  2259. << "> - reason: " << mysql_error(impl_->conn_.mysql_));
  2260. }
  2261. // Bind the output of the statement to the appropriate variables.
  2262. MYSQL_BIND bind[2];
  2263. memset(bind, 0, sizeof(bind));
  2264. bind[0].buffer_type = MYSQL_TYPE_LONG;
  2265. bind[0].is_unsigned = 1;
  2266. bind[0].buffer = &major;
  2267. bind[0].buffer_length = sizeof(major);
  2268. bind[1].buffer_type = MYSQL_TYPE_LONG;
  2269. bind[1].is_unsigned = 1;
  2270. bind[1].buffer = &minor;
  2271. bind[1].buffer_length = sizeof(minor);
  2272. status = mysql_stmt_bind_result(impl_->conn_.statements_[stindex], bind);
  2273. if (status != 0) {
  2274. isc_throw(DbOperationError, "unable to bind result set: "
  2275. << mysql_error(impl_->conn_.mysql_));
  2276. }
  2277. // Fetch the data and set up the "release" object to release associated
  2278. // resources when this method exits then retrieve the data.
  2279. // mysql_stmt_fetch return value other than 0 means error occurrence.
  2280. MySqlFreeResult fetch_release(impl_->conn_.statements_[stindex]);
  2281. status = mysql_stmt_fetch(impl_->conn_.statements_[stindex]);
  2282. if (status != 0) {
  2283. isc_throw(DbOperationError, "unable to obtain result set: "
  2284. << mysql_error(impl_->conn_.mysql_));
  2285. }
  2286. return (std::make_pair(major, minor));
  2287. }
  2288. void
  2289. MySqlHostDataSource::commit() {
  2290. // If operating in read-only mode, throw exception.
  2291. impl_->checkReadOnly();
  2292. impl_->conn_.commit();
  2293. }
  2294. void
  2295. MySqlHostDataSource::rollback() {
  2296. // If operating in read-only mode, throw exception.
  2297. impl_->checkReadOnly();
  2298. impl_->conn_.rollback();
  2299. }
  2300. }; // end of isc::dhcp namespace
  2301. }; // end of isc namespace