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