You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionAddHost.cpp 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // Created by robin on 8/9/15.
  3. //
  4. #include <string>
  5. #include <sstream>
  6. #include <vector>
  7. #include "ActionAddHost.h"
  8. std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
  9. std::stringstream ss(s);
  10. std::string item;
  11. while (std::getline(ss, item, delim)) {
  12. elems.push_back(item);
  13. }
  14. return elems;
  15. }
  16. std::vector<std::string> split(const std::string &s, char delim) {
  17. std::vector<std::string> elems;
  18. split(s, delim, elems);
  19. return elems;
  20. }
  21. ActionAddHost::ActionAddHost()
  22. {
  23. }
  24. const std::string ActionAddHost::getSql() const
  25. {
  26. auto host = _host.empty() ? _domain : _host + "." + _domain;
  27. auto query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
  28. " VALUES(@domain_id, \"" + host + "\", \"" + _recordType + "\","
  29. " \"" + _recordValue + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
  30. if (_reverseEnabled && _recordType == "A")
  31. {
  32. auto reversedValue = getReversedValue();
  33. auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
  34. query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
  35. " VALUES((SELECT id FROM domains WHERE name=\"" + reverseDomain + "\"), \""
  36. + reversedValue + "." + reverseDomain + "\", \"PTR\","
  37. " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
  38. }
  39. return query;
  40. }
  41. const std::string &ActionAddHost::getHost() const
  42. {
  43. return _host;
  44. }
  45. void ActionAddHost::setHost(const std::string &host)
  46. {
  47. _host = host;
  48. }
  49. const std::string &ActionAddHost::getRecordValue() const
  50. {
  51. return _recordValue;
  52. }
  53. void ActionAddHost::setRecordValue(const std::string &recordValue)
  54. {
  55. _recordValue = recordValue;
  56. }
  57. const std::string &ActionAddHost::getRecordType() const
  58. {
  59. return _recordType;
  60. }
  61. void ActionAddHost::setRecordType(const std::string &recordType)
  62. {
  63. _recordType = recordType;
  64. }
  65. const std::string &ActionAddHost::getDhcpMac() const
  66. {
  67. return _dhcpMac;
  68. }
  69. void ActionAddHost::setDhcpMac(const std::string &dhcpMac)
  70. {
  71. _dhcpMac = dhcpMac;
  72. }
  73. const std::string &ActionAddHost::getReverseDomain() const
  74. {
  75. return _reverseDomain;
  76. }
  77. void ActionAddHost::setReverseDomain(const std::string &reverseDomain)
  78. {
  79. _reverseDomain = reverseDomain;
  80. }
  81. bool ActionAddHost::isReverseEnabled() const
  82. {
  83. return _reverseEnabled;
  84. }
  85. void ActionAddHost::setReverseEnabled(bool reverseEnabled)
  86. {
  87. _reverseEnabled = reverseEnabled;
  88. }
  89. const std::string ActionAddHost::getReversedValue() const
  90. {
  91. auto splitValue = split(_recordValue, '.');
  92. int index = 1;
  93. for (; index < splitValue.size(); ++index)
  94. {
  95. std::string reversedValue;
  96. for (int i = (int) splitValue.size() - 1 - index; i >= 0; --i)
  97. reversedValue += splitValue[i] + (i == 0 ? "" : ".");
  98. if (_reverseDomain == reversedValue)
  99. break;
  100. }
  101. std::string reversedValue;
  102. for (int i = (int) splitValue.size() - 1; i >= (int) splitValue.size() - index; --i)
  103. reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
  104. return reversedValue;
  105. }
  106. const std::string ActionAddHost::getDhcp() const
  107. {
  108. if (!_dhcpMac.empty() && _recordType == "A")
  109. {
  110. return "\thost " + _host + " {\n"
  111. "\t\thardware ethernet " + _dhcpMac + ";\n"
  112. "\t\tfixed-address " + _host + "." + _domain + ";\n"
  113. "\t}\n";
  114. }
  115. else
  116. return "";
  117. }