my_query_helper.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file my/my_query_helper.c
  18. * @brief library to help with access to a MySQL database
  19. * @author Christian Grothoff
  20. * @author Christophe Genevey
  21. */
  22. #include "platform.h"
  23. #include <mysql/mysql.h>
  24. #include "gnunet_my_lib.h"
  25. /**
  26. * Function called to clean up memory allocated
  27. * by a #GNUNET_MY_QueryConverter.
  28. *
  29. * @param cls closure
  30. * @param qbind array of parameter to clean up
  31. */
  32. static void
  33. my_clean_query (void *cls,
  34. MYSQL_BIND *qbind)
  35. {
  36. (void) cls;
  37. GNUNET_free (qbind[0].buffer);
  38. }
  39. /**
  40. * Function called to convert input argument into SQL parameters.
  41. *
  42. * @param cls closure
  43. * @param pq data about the query
  44. * @param qbind array of parameters to initialize
  45. * @return -1 on error
  46. */
  47. static int
  48. my_conv_fixed_size (void *cls,
  49. const struct GNUNET_MY_QueryParam *qp,
  50. MYSQL_BIND *qbind)
  51. {
  52. (void) cls;
  53. GNUNET_assert (1 == qp->num_params);
  54. qbind->buffer = (void *) qp->data;
  55. qbind->buffer_length = qp->data_len;
  56. qbind->buffer_type = MYSQL_TYPE_BLOB;
  57. return 1;
  58. }
  59. /**
  60. * Generate query parameter for a buffer @a ptr of
  61. * @a ptr_size bytes.
  62. *
  63. * @param ptr pointer to the query parameter to pass
  64. * @param ptr_size number of bytes in @a ptr
  65. */
  66. struct GNUNET_MY_QueryParam
  67. GNUNET_MY_query_param_fixed_size (const void *ptr,
  68. size_t ptr_size)
  69. {
  70. struct GNUNET_MY_QueryParam qp = {
  71. .conv = &my_conv_fixed_size,
  72. .cleaner = NULL,
  73. .conv_cls = NULL,
  74. .num_params = 1,
  75. .data = ptr,
  76. .data_len = (unsigned long) ptr_size
  77. };
  78. return qp;
  79. }
  80. /**
  81. * Function called to convert input argument into SQL parameters.
  82. *
  83. * @param cls closure
  84. * @param pq data about the query
  85. * @param qbind array of parameters to initialize
  86. * @return -1 on error
  87. */
  88. static int
  89. my_conv_string (void *cls,
  90. const struct GNUNET_MY_QueryParam *qp,
  91. MYSQL_BIND *qbind)
  92. {
  93. (void) cls;
  94. GNUNET_assert (1 == qp->num_params);
  95. qbind->buffer = (void *) qp->data;
  96. qbind->buffer_length = qp->data_len;
  97. qbind->buffer_type = MYSQL_TYPE_STRING;
  98. return 1;
  99. }
  100. /**
  101. * Generate query parameter for a string
  102. *
  103. * @param ptr pointer to the string query parameter to pass
  104. */
  105. struct GNUNET_MY_QueryParam
  106. GNUNET_MY_query_param_string (const char *ptr)
  107. {
  108. struct GNUNET_MY_QueryParam qp = {
  109. .conv = &my_conv_string,
  110. .cleaner = NULL,
  111. .conv_cls = NULL,
  112. .num_params = 1,
  113. .data = ptr,
  114. .data_len = strlen (ptr)
  115. };
  116. return qp;
  117. }
  118. /**
  119. * Function called to convert input argument into SQL parameters
  120. *
  121. * @param cls closure
  122. * @param pq data about the query
  123. * @param qbind array of parameters to initialize
  124. * @return -1 on error
  125. */
  126. static int
  127. my_conv_uint16 (void *cls,
  128. const struct GNUNET_MY_QueryParam *qp,
  129. MYSQL_BIND *qbind)
  130. {
  131. (void) cls;
  132. GNUNET_assert (1 == qp->num_params);
  133. qbind->buffer = (void *) qp->data;
  134. qbind->buffer_length = sizeof(uint16_t);
  135. qbind->buffer_type = MYSQL_TYPE_SHORT;
  136. qbind->is_unsigned = 1;
  137. return 1;
  138. }
  139. /**
  140. * Generate query parameter for an uint16_t in host byte order.
  141. *
  142. * @param x pointer to the query parameter to pass
  143. */
  144. struct GNUNET_MY_QueryParam
  145. GNUNET_MY_query_param_uint16 (const uint16_t *x)
  146. {
  147. struct GNUNET_MY_QueryParam res = {
  148. .conv = &my_conv_uint16,
  149. .cleaner = NULL,
  150. .conv_cls = NULL,
  151. .num_params = 1,
  152. .data = x,
  153. .data_len = sizeof(*x)
  154. };
  155. return res;
  156. }
  157. /**
  158. * Function called to convert input argument into SQL parameters
  159. *
  160. * @param cls closure
  161. * @param pq data about the query
  162. * @param qbind array of parameters to initialize
  163. * @return -1 on error
  164. */
  165. static int
  166. my_conv_uint32 (void *cls,
  167. const struct GNUNET_MY_QueryParam *qp,
  168. MYSQL_BIND *qbind)
  169. {
  170. (void) cls;
  171. GNUNET_assert (1 == qp->num_params);
  172. qbind->buffer = (void *) qp->data;
  173. qbind->buffer_length = sizeof(uint32_t);
  174. qbind->buffer_type = MYSQL_TYPE_LONG;
  175. qbind->is_unsigned = 1;
  176. return 1;
  177. }
  178. /**
  179. * Generate query parameter for an uint32_t in host byte order
  180. *
  181. * @param x pointer to the query parameter to pass
  182. */
  183. struct GNUNET_MY_QueryParam
  184. GNUNET_MY_query_param_uint32 (const uint32_t *x)
  185. {
  186. struct GNUNET_MY_QueryParam res = {
  187. .conv = &my_conv_uint32,
  188. .cleaner = NULL,
  189. .conv_cls = NULL,
  190. .num_params = 1,
  191. .data = x,
  192. .data_len = sizeof(*x)
  193. };
  194. return res;
  195. }
  196. /**
  197. * Function called to convert input argument into SQL parameters
  198. *
  199. * @param cls closure
  200. * @param pq data about the query
  201. * @param qbind array of parameters to initialize
  202. * @return -1 on error
  203. */
  204. static int
  205. my_conv_uint64 (void *cls,
  206. const struct GNUNET_MY_QueryParam *qp,
  207. MYSQL_BIND *qbind)
  208. {
  209. (void) cls;
  210. GNUNET_assert (1 == qp->num_params);
  211. qbind->buffer = (void *) qp->data;
  212. qbind->buffer_length = sizeof(uint64_t);
  213. qbind->buffer_type = MYSQL_TYPE_LONGLONG;
  214. qbind->is_unsigned = 1;
  215. return 1;
  216. }
  217. /**
  218. * Generate query parameter for an uint64_t in host byte order
  219. *
  220. * @param x pointer to the query parameter to pass
  221. */
  222. struct GNUNET_MY_QueryParam
  223. GNUNET_MY_query_param_uint64 (const uint64_t *x)
  224. {
  225. struct GNUNET_MY_QueryParam res = {
  226. .conv = &my_conv_uint64,
  227. .cleaner = NULL,
  228. .conv_cls = NULL,
  229. .num_params = 1,
  230. .data = x,
  231. .data_len = sizeof(*x)
  232. };
  233. return res;
  234. }
  235. /**
  236. * Function called to convert input argument into SQL parameters
  237. *
  238. * @param cls closure
  239. * @param pq data about the query
  240. * @param qbind array of parameters to initialize
  241. * @return -1 on error
  242. */
  243. static int
  244. my_conv_rsa_public_key (void *cls,
  245. const struct GNUNET_MY_QueryParam *qp,
  246. MYSQL_BIND *qbind)
  247. {
  248. const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
  249. void *buf;
  250. size_t buf_size;
  251. (void) cls;
  252. GNUNET_assert (1 == qp->num_params);
  253. buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
  254. &buf);
  255. qbind->buffer = buf;
  256. qbind->buffer_length = buf_size;
  257. qbind->buffer_type = MYSQL_TYPE_BLOB;
  258. return 1;
  259. }
  260. /**
  261. * Generate query parameter for an RSA public key. The
  262. * database must contain a BLOB type in the respective position.
  263. *
  264. * @param x the query parameter to pass
  265. * @return array entry for the query parameters to use
  266. */
  267. struct GNUNET_MY_QueryParam
  268. GNUNET_MY_query_param_rsa_public_key (const struct
  269. GNUNET_CRYPTO_RsaPublicKey *x)
  270. {
  271. struct GNUNET_MY_QueryParam res = {
  272. .conv = &my_conv_rsa_public_key,
  273. .cleaner = &my_clean_query,
  274. .conv_cls = NULL,
  275. .num_params = 1,
  276. .data = x,
  277. .data_len = 0
  278. };
  279. return res;
  280. }
  281. /**
  282. * Function called to convert input argument into SQL parameters
  283. *
  284. *@param cls closure
  285. *@param pq data about the query
  286. *@param qbind array of parameters to initialize
  287. *@return -1 on error
  288. */
  289. static int
  290. my_conv_rsa_signature (void *cls,
  291. const struct GNUNET_MY_QueryParam *qp,
  292. MYSQL_BIND *qbind)
  293. {
  294. const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
  295. void *buf;
  296. size_t buf_size;
  297. (void) cls;
  298. GNUNET_assert (1 == qp->num_params);
  299. buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
  300. &buf);
  301. qbind->buffer = buf;
  302. qbind->buffer_length = buf_size;
  303. qbind->buffer_type = MYSQL_TYPE_BLOB;
  304. return 1;
  305. }
  306. /**
  307. * Generate query parameter for an RSA signature. The
  308. * database must contain a BLOB type in the respective position
  309. *
  310. * @param x the query parameter to pass
  311. * @return array entry for the query parameters to use
  312. */
  313. struct GNUNET_MY_QueryParam
  314. GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
  315. {
  316. struct GNUNET_MY_QueryParam res = {
  317. .conv = &my_conv_rsa_signature,
  318. .cleaner = &my_clean_query,
  319. .conv_cls = NULL,
  320. .num_params = 1,
  321. .data = (x),
  322. .data_len = 0
  323. };
  324. return res;
  325. }
  326. /**
  327. * Generate query parameter for an absolute time value.
  328. * The database must store a 64-bit integer.
  329. *
  330. * @param x pointer to the query parameter to pass
  331. * @return array entry for the query parameters to use
  332. */
  333. struct GNUNET_MY_QueryParam
  334. GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
  335. {
  336. return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
  337. }
  338. /**
  339. * Generate query parameter for an absolute time value.
  340. * The database must store a 64-bit integer.
  341. *
  342. * @param x pointer to the query parameter to pass
  343. */
  344. struct GNUNET_MY_QueryParam
  345. GNUNET_MY_query_param_absolute_time_nbo (const struct
  346. GNUNET_TIME_AbsoluteNBO *x)
  347. {
  348. return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
  349. }
  350. /* end of my_query_helper.c */