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.

431 lines
27 KiB

  1. From 5200105f412ceefa0784bf914aa215146fd067b0 Mon Sep 17 00:00:00 2001
  2. From: Eneas U de Queiroz <cote2004-github@yahoo.com>
  3. Date: Wed, 26 Dec 2018 16:45:47 -0200
  4. Subject: [PATCH] Ipset.cxx: update libipset API to version 7
  5. Old API compatibility was kept with a compatibility shim.
  6. Signed-off-by: Eneas U de Queiroz <cote2004-github@yahoo.com>
  7. diff --git a/src/Ipset.cxx b/src/Ipset.cxx
  8. index 9333fe6..da97f93 100644
  9. --- a/src/Ipset.cxx
  10. +++ b/src/Ipset.cxx
  11. @@ -90,23 +90,19 @@ void Ipset::Open (const std::string inIpsetName, std::string inIpsetType, bool i
  12. isIpsetv4 = inisIpsetv4;
  13. ipset_load_types();
  14. - struct ipset_session *session = ipset_session_init(printf);
  15. + struct ipset_session *session = noddos_ipset_session_init();
  16. if (session == nullptr) {
  17. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  18. ipset_session_fini(session);
  19. throw std::runtime_error ("Cannot initialize ipset session.");
  20. }
  21. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  22. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  23. - ipset_session_fini(session);
  24. - throw std::runtime_error ("Can't set environment option.");
  25. - }
  26. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  27. int r = ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str());
  28. if ( r < 0) {
  29. - syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  30. + syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  31. ipset_session_fini(session);
  32. - throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_error(session));
  33. + throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_report_msg(session));
  34. } else if (r > 0) {
  35. if (Debug == true) {
  36. syslog (LOG_DEBUG, "Ipset: Not creating set %s as it already exists", ipsetName.c_str());
  37. @@ -115,27 +111,27 @@ void Ipset::Open (const std::string inIpsetName, std::string inIpsetType, bool i
  38. return;
  39. }
  40. if (ipset_session_data_set(session, IPSET_OPT_TYPENAME, ipsetType.c_str()) < 0) {
  41. - syslog (LOG_ERR, "Ipset: Can't set setname %s to type %s: %s", ipsetName.c_str(), ipsetType.c_str(), ipset_session_error(session));
  42. + syslog (LOG_ERR, "Ipset: Can't set setname %s to type %s: %s", ipsetName.c_str(), ipsetType.c_str(), ipset_session_report_msg(session));
  43. ipset_session_fini(session);
  44. - throw std::runtime_error("Can't set type " + ipsetType + ": " + ipset_session_error(session));
  45. + throw std::runtime_error("Can't set type " + ipsetType + ": " + ipset_session_report_msg(session));
  46. }
  47. const struct ipset_type *type = ipset_type_get(session, IPSET_CMD_CREATE);
  48. if (type == NULL) {
  49. - syslog (LOG_ERR, "Ipset: Can't set create ip %s: %s", ipsetName.c_str(), ipset_session_error(session));
  50. + syslog (LOG_ERR, "Ipset: Can't set create ip %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  51. ipset_session_fini(session);
  52. - throw std::runtime_error("Can't create ipset " + ipsetName + ": " + ipset_session_error(session));
  53. + throw std::runtime_error("Can't create ipset " + ipsetName + ": " + ipset_session_report_msg(session));
  54. }
  55. uint32_t timeout = 0; /* default to infinity */
  56. if (ipset_session_data_set(session, IPSET_OPT_TIMEOUT, &timeout) < 0) {
  57. - syslog (LOG_ERR, "Ipset: Can't set setname %s to timeout %d: %s", ipsetName.c_str(), timeout, ipset_session_error(session));
  58. + syslog (LOG_ERR, "Ipset: Can't set setname %s to timeout %d: %s", ipsetName.c_str(), timeout, ipset_session_report_msg(session));
  59. ipset_session_fini(session);
  60. - throw std::runtime_error("Can't set time-out " + ipsetName + ": " + ipset_session_error(session));
  61. + throw std::runtime_error("Can't set time-out " + ipsetName + ": " + ipset_session_report_msg(session));
  62. }
  63. if (ipset_session_data_set(session, IPSET_OPT_TYPE, type)) {
  64. - syslog (LOG_ERR, "Ipset: Can't set setname %s option type: %s", ipsetName.c_str(), ipset_session_error(session));
  65. + syslog (LOG_ERR, "Ipset: Can't set setname %s option type: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  66. ipset_session_fini(session);
  67. - throw std::runtime_error("Can't set ipset type: " + ipsetName + ": " + ipset_session_error(session));
  68. + throw std::runtime_error("Can't set ipset type: " + ipsetName + ": " + ipset_session_report_msg(session));
  69. }
  70. uint8_t family = 0;
  71. if (ipsetType == "hash:ip" && isIpsetv4 == true) {
  72. @@ -149,20 +145,20 @@ void Ipset::Open (const std::string inIpsetName, std::string inIpsetType, bool i
  73. throw std::invalid_argument("Unknown ipset data type " + ipsetType);
  74. }
  75. if (ipset_session_data_set(session, IPSET_OPT_FAMILY, &family) < 0) {
  76. - syslog (LOG_ERR, "Ipset: Can't set setname %s address family %d: %s", ipsetName.c_str(), family, ipset_session_error(session));
  77. + syslog (LOG_ERR, "Ipset: Can't set setname %s address family %d: %s", ipsetName.c_str(), family, ipset_session_report_msg(session));
  78. ipset_session_fini(session);
  79. - throw std::runtime_error("Cannot set ipset family: " + ipsetName + ": " + ipset_session_error(session));
  80. + throw std::runtime_error("Cannot set ipset family: " + ipsetName + ": " + ipset_session_report_msg(session));
  81. }
  82. if (ipset_cmd(session, IPSET_CMD_CREATE, /*lineno*/ 0) != 0) {
  83. - syslog (LOG_ERR, "Ipset: Can't create setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  84. + syslog (LOG_ERR, "Ipset: Can't create setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  85. ipset_session_fini(session);
  86. - throw std::runtime_error("Failed to create ipset " + ipsetName + ": " + ipset_session_error(session));
  87. + throw std::runtime_error("Failed to create ipset " + ipsetName + ": " + ipset_session_report_msg(session));
  88. }
  89. if (ipset_commit(session) < 0) {
  90. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  91. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  92. ipset_session_fini(session);
  93. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  94. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  95. }
  96. ipset_session_fini(session);
  97. }
  98. @@ -173,33 +169,29 @@ bool Ipset::ipset_exec(enum ipset_cmd cmd) {
  99. if (Debug == true) {
  100. syslog(LOG_DEBUG, "Ipset: received command %d for ipset %s", cmd, ipsetName.c_str());
  101. }
  102. - struct ipset_session *session = ipset_session_init(printf);
  103. + struct ipset_session *session = noddos_ipset_session_init();
  104. if (session == nullptr) {
  105. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  106. ipset_session_fini(session);
  107. throw std::runtime_error ("Cannot initialize ipset session.");
  108. }
  109. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  110. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  111. - ipset_session_fini(session);
  112. - throw std::runtime_error ("Can't set environment option.");
  113. - }
  114. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  115. if (ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str()) < 0) {
  116. - syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  117. + syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  118. ipset_session_fini(session);
  119. - throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_error(session));
  120. + throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_report_msg(session));
  121. }
  122. if (ipset_cmd(session, cmd, 0) != 0) {
  123. ipset_session_fini(session);
  124. - syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  125. - throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_error(session));
  126. + syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  127. + throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_report_msg(session));
  128. }
  129. if (ipset_commit(session) < 0) {
  130. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  131. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  132. ipset_session_fini(session);
  133. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  134. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  135. }
  136. ipset_session_fini(session);
  137. return true;
  138. @@ -210,61 +202,57 @@ bool Ipset::ipset_exec(enum ipset_cmd cmd, const Tins::IPv4Address &inIpAddress
  139. if (Debug == true) {
  140. syslog(LOG_DEBUG, "Ipset: received command %d for IP address %s for ipset %s", cmd, inIpAddress.to_string().c_str(), ipsetName.c_str());
  141. }
  142. - struct ipset_session *session = ipset_session_init(printf);
  143. + struct ipset_session *session = noddos_ipset_session_init();
  144. if (session == nullptr) {
  145. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  146. ipset_session_fini(session);
  147. throw std::runtime_error ("Cannot initialize ipset session.");
  148. }
  149. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  150. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  151. - ipset_session_fini(session);
  152. - throw std::runtime_error ("Can't set environment option.");
  153. - }
  154. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  155. if (ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str()) < 0) {
  156. - syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  157. + syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  158. ipset_session_fini(session);
  159. - throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_error(session));
  160. + throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_report_msg(session));
  161. }
  162. const struct ipset_type *type = ipset_type_get(session, cmd);
  163. if (type == NULL) {
  164. - syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_error(session));
  165. + syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  166. ipset_session_fini(session);
  167. - throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_error(session));
  168. + throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_report_msg(session));
  169. }
  170. uint8_t family = NFPROTO_IPV4;
  171. if (ipset_session_data_set(session, IPSET_OPT_FAMILY, &family) < 0) {
  172. - syslog (LOG_ERR, "Ipset: Can't set session data to IPv4 family for set %s: %s", ipsetName.c_str(), ipset_session_error(session));
  173. + syslog (LOG_ERR, "Ipset: Can't set session data to IPv4 family for set %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  174. ipset_session_fini(session);
  175. - throw std::runtime_error("Can't set session data for " + ipsetName + " to the IPv4 family, error: " + ipset_session_error(session));
  176. + throw std::runtime_error("Can't set session data for " + ipsetName + " to the IPv4 family, error: " + ipset_session_report_msg(session));
  177. }
  178. struct in_addr sin;
  179. inet_aton (inIpAddress.to_string().c_str(), &sin);
  180. if (ipset_session_data_set(session, IPSET_OPT_IP, &sin) < 0) {
  181. - syslog (LOG_ERR, "Can't set session data to the IPv4 address for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  182. + syslog (LOG_ERR, "Can't set session data to the IPv4 address for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  183. ipset_session_fini(session);
  184. - throw std::runtime_error("Can't set session data to the IPv4 address for setname " + ipsetName + ", error: " + ipset_session_error(session));
  185. + throw std::runtime_error("Can't set session data to the IPv4 address for setname " + ipsetName + ", error: " + ipset_session_report_msg(session));
  186. }
  187. if (timeout) {
  188. if (ipset_session_data_set(session, IPSET_OPT_TIMEOUT, &timeout) != 0) {
  189. - syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  190. + syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  191. ipset_session_fini(session);
  192. - throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_error(session));
  193. + throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_report_msg(session));
  194. return false;
  195. }
  196. }
  197. if (ipset_cmd(session, cmd, 0) != 0) {
  198. ipset_session_fini(session);
  199. - syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  200. - throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_error(session));
  201. + syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  202. + throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_report_msg(session));
  203. }
  204. if (ipset_commit(session) < 0) {
  205. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  206. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  207. ipset_session_fini(session);
  208. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  209. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  210. }
  211. ipset_session_fini(session);
  212. return true;
  213. @@ -274,61 +262,57 @@ bool Ipset::ipset_exec(enum ipset_cmd cmd, const Tins::IPv6Address &inIpAddress
  214. if (Debug == true) {
  215. syslog(LOG_DEBUG, "Ipset: received command %d for IP address %s for ipset %s", cmd, inIpAddress.to_string().c_str(), ipsetName.c_str());
  216. }
  217. - struct ipset_session *session = ipset_session_init(printf);
  218. + struct ipset_session *session = noddos_ipset_session_init();
  219. if (session == nullptr) {
  220. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  221. ipset_session_fini(session);
  222. throw std::runtime_error ("Cannot initialize ipset session.");
  223. }
  224. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  225. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  226. - ipset_session_fini(session);
  227. - throw std::runtime_error ("Can't set environment option.");
  228. - }
  229. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  230. if (ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str()) < 0) {
  231. - syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  232. + syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  233. ipset_session_fini(session);
  234. - throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_error(session));
  235. + throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_report_msg(session));
  236. }
  237. const struct ipset_type *type = ipset_type_get(session, cmd);
  238. if (type == NULL) {
  239. - syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_error(session));
  240. + syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  241. ipset_session_fini(session);
  242. - throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_error(session));
  243. + throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_report_msg(session));
  244. }
  245. uint8_t family = NFPROTO_IPV6;
  246. if (ipset_session_data_set(session, IPSET_OPT_FAMILY, &family) < 0) {
  247. - syslog (LOG_ERR, "Ipset: Can't set session data to IPv6 family for set %s: %s", ipsetName.c_str(), ipset_session_error(session));
  248. + syslog (LOG_ERR, "Ipset: Can't set session data to IPv6 family for set %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  249. ipset_session_fini(session);
  250. - throw std::runtime_error("Can't set session data for " + ipsetName + " to the IPv6 family, error: " + ipset_session_error(session));
  251. + throw std::runtime_error("Can't set session data for " + ipsetName + " to the IPv6 family, error: " + ipset_session_report_msg(session));
  252. }
  253. unsigned char buf[sizeof(struct in6_addr)];
  254. int s = inet_pton(AF_INET6, inIpAddress.to_string().c_str(), buf);
  255. if (ipset_session_data_set(session, IPSET_OPT_IP, &buf) < 0) {
  256. - syslog (LOG_ERR, "Ipset: Can't set session data to the IPv4 address for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  257. + syslog (LOG_ERR, "Ipset: Can't set session data to the IPv4 address for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  258. ipset_session_fini(session);
  259. - throw std::runtime_error("Can't set session data to the IPv4 address for setname " + ipsetName + ", error: " + ipset_session_error(session));
  260. + throw std::runtime_error("Can't set session data to the IPv4 address for setname " + ipsetName + ", error: " + ipset_session_report_msg(session));
  261. }
  262. if (timeout) {
  263. if (ipset_session_data_set(session, IPSET_OPT_TIMEOUT, &timeout) != 0) {
  264. - syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  265. + syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  266. ipset_session_fini(session);
  267. - throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_error(session));
  268. + throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_report_msg(session));
  269. }
  270. }
  271. if (ipset_cmd(session, cmd, 0) != 0) {
  272. - syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  273. + syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  274. ipset_session_fini(session);
  275. - throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_error(session));
  276. + throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_report_msg(session));
  277. }
  278. if (ipset_commit(session) < 0) {
  279. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  280. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  281. ipset_session_fini(session);
  282. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  283. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  284. }
  285. ipset_session_fini(session);
  286. return true;
  287. @@ -338,50 +322,46 @@ bool Ipset::ipset_exec(enum ipset_cmd cmd, const std::string Mac, time_t timeout
  288. if (Debug == true) {
  289. syslog(LOG_DEBUG, "Ipset: received command %d for MAC address %s for ipset %s", cmd, Mac.c_str(), ipsetName.c_str());
  290. }
  291. - struct ipset_session *session = ipset_session_init(printf);
  292. + struct ipset_session *session = noddos_ipset_session_init();
  293. if (session == nullptr) {
  294. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  295. ipset_session_fini(session);
  296. throw std::runtime_error ("Cannot initialize ipset session.");
  297. }
  298. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  299. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  300. - ipset_session_fini(session);
  301. - throw std::runtime_error ("Can't set environment option.");
  302. - }
  303. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  304. if (ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str()) < 0) {
  305. - syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  306. + syslog (LOG_ERR, "Ipset: Can't set setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  307. ipset_session_fini(session);
  308. - throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_error(session));
  309. + throw std::runtime_error("Can't set setname " + ipsetName + ": " + ipset_session_report_msg(session));
  310. }
  311. const struct ipset_type *type = ipset_type_get(session, cmd);
  312. if (type == NULL) {
  313. - syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_error(session));
  314. + syslog (LOG_ERR, "Ipset: Can't get type for set %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  315. ipset_session_fini(session);
  316. - throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_error(session));
  317. + throw std::runtime_error("Can't get type for set " + ipsetName + ": " + ipset_session_report_msg(session));
  318. }
  319. if (ipset_parse_elem(session, (ipset_opt)type->last_elem_optional, Mac.c_str()) < 0) {
  320. - syslog (LOG_ERR, "Ipset: Can't call ipset_parse_elem for %s: %s ", ipsetName.c_str(), ipset_session_error(session));
  321. + syslog (LOG_ERR, "Ipset: Can't call ipset_parse_elem for %s: %s ", ipsetName.c_str(), ipset_session_report_msg(session));
  322. ipset_session_fini(session);
  323. - throw std::runtime_error("Can't call ipset_parse_elem for ipset " + ipsetName + ": " + ipset_session_error(session));
  324. + throw std::runtime_error("Can't call ipset_parse_elem for ipset " + ipsetName + ": " + ipset_session_report_msg(session));
  325. }
  326. if (timeout) {
  327. if (ipset_session_data_set(session, IPSET_OPT_TIMEOUT, &timeout) != 0) {
  328. - syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  329. + syslog (LOG_ERR, "Ipset: Can't set timeout for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  330. ipset_session_fini(session);
  331. - throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_error(session));
  332. + throw std::runtime_error("Can't set timeout for " + ipsetName + ": " + ipset_session_report_msg(session));
  333. }
  334. }
  335. if (ipset_cmd(session, cmd, 0) != 0) {
  336. - syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  337. + syslog (LOG_ERR, "Ipset: Can't exec ipset cmd for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  338. ipset_session_fini(session);
  339. - throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_error(session));
  340. + throw std::runtime_error("Can't exec ipset cmd for " + ipsetName + ": " + ipset_session_report_msg(session));
  341. }
  342. if (ipset_commit(session) < 0) {
  343. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  344. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  345. ipset_session_fini(session);
  346. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  347. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  348. }
  349. ipset_session_fini(session);
  350. return true;
  351. diff --git a/src/Ipset.h b/src/Ipset.h
  352. index 2c5f7b2..eb180da 100644
  353. --- a/src/Ipset.h
  354. +++ b/src/Ipset.h
  355. @@ -41,6 +41,31 @@
  356. #include "MacAddress.h"
  357. +#if IPSET_PROTOCOL < 7
  358. +/* compatibility shims */
  359. +
  360. +inline void ipset_envopt_set(struct ipset_session *session, enum ipset_envopt opt)
  361. +{
  362. + ipset_envopt_parse(session, opt, NULL);
  363. +}
  364. +
  365. +inline const char * ipset_session_report_msg(const struct ipset_session *session)
  366. +{
  367. + return ipset_session_error(session);
  368. +}
  369. +
  370. +static inline struct ipset_session *noddos_ipset_session_init(void)
  371. +{
  372. + return ipset_session_init(printf);
  373. +}
  374. +
  375. +#else
  376. +
  377. +static inline struct ipset_session *noddos_ipset_session_init(void)
  378. +{
  379. + return ipset_session_init(NULL, NULL);
  380. +}
  381. +#endif
  382. std::string getIpsetUuid (std::string inUuid);
  383. std::string getIpsetName (std::string inUuid, bool inSrc, bool inIpv4 = true);
  384. @@ -99,23 +124,19 @@ public:
  385. }
  386. bool Exists() {
  387. try {
  388. - struct ipset_session *session = ipset_session_init(printf);
  389. + struct ipset_session *session = noddos_ipset_session_init();
  390. if (session == nullptr) {
  391. syslog (LOG_ERR, "Ipset: Cannot initialize ipset session.");
  392. ipset_session_fini(session);
  393. throw std::runtime_error ("Cannot initialize ipset session.");
  394. }
  395. - if (ipset_envopt_parse(session, IPSET_ENV_EXIST, NULL) < 0) {
  396. - syslog (LOG_ERR, "Ipset: Can't set environment option.");
  397. - ipset_session_fini(session);
  398. - throw std::runtime_error ("Can't set environment option.");
  399. - }
  400. + ipset_envopt_set(session, IPSET_ENV_EXIST);
  401. int r = ipset_session_data_set(session, IPSET_SETNAME, ipsetName.c_str());
  402. if (ipset_commit(session) < 0) {
  403. - syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_error(session));
  404. + syslog (LOG_ERR, "Ipset: Can't commit for setname %s: %s", ipsetName.c_str(), ipset_session_report_msg(session));
  405. ipset_session_fini(session);
  406. - throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_error(session));
  407. + throw std::runtime_error("Can't call ipset_commit for " + ipsetName + ": " + ipset_session_report_msg(session));
  408. }
  409. ipset_session_fini(session);
  410. return r == 0;