paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / rps / rps-test_util.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file rps/rps-test_util.c
21  * @brief Some utils faciliating the view into the internals for the sampler
22  *        needed for evaluation
23  *
24  * @author Julius Bünger
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29
30 #include <inttypes.h>
31
32 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
33
34 #ifndef TO_FILE
35 #define TO_FILE
36 #endif /* TO_FILE */
37
38 #ifdef TO_FILE
39 void
40 to_file_ (const char *file_name, char *line)
41 {
42   struct GNUNET_DISK_FileHandle *f;
43   char output_buffer[512];
44   size_t output_buffer_size = 512;
45   char *output_buffer_p;
46   //size_t size;
47   int size;
48   size_t size2;
49
50
51   if (NULL == (f = GNUNET_DISK_file_open (file_name,
52                                           GNUNET_DISK_OPEN_APPEND |
53                                           GNUNET_DISK_OPEN_WRITE |
54                                           GNUNET_DISK_OPEN_CREATE,
55                                           GNUNET_DISK_PERM_USER_READ |
56                                           GNUNET_DISK_PERM_USER_WRITE |
57                                           GNUNET_DISK_PERM_GROUP_READ |
58                                           GNUNET_DISK_PERM_OTHER_READ)))
59   {
60     LOG (GNUNET_ERROR_TYPE_WARNING,
61          "Not able to open file %s\n",
62          file_name);
63     return;
64   }
65   output_buffer_size = strlen (line) + 18;
66   if (512 < output_buffer_size)
67   {
68     output_buffer_p = GNUNET_malloc ((output_buffer_size) * sizeof (char));
69   } else {
70     output_buffer_p = &output_buffer[0];
71   }
72   size = GNUNET_snprintf (output_buffer_p,
73                           output_buffer_size,
74                           "%llu %s\n",
75                           (GNUNET_TIME_absolute_get ().abs_value_us) / 1000000, // microsec -> sec
76                           line);
77   if (0 > size)
78   {
79     LOG (GNUNET_ERROR_TYPE_WARNING,
80          "Failed to write string to buffer (size: %i)\n",
81          size);
82     return;
83   }
84
85   size2 = GNUNET_DISK_file_write (f, output_buffer_p, size);
86   if (size != size2)
87   {
88     LOG (GNUNET_ERROR_TYPE_WARNING,
89          "Unable to write to file! (Size: %u, size2: %u)\n",
90          size,
91          size2);
92
93     if (GNUNET_YES != GNUNET_DISK_file_close (f))
94       LOG (GNUNET_ERROR_TYPE_WARNING,
95            "Unable to close file\n");
96
97     return;
98   }
99
100   if (512 < output_buffer_size)
101   {
102     GNUNET_free (output_buffer_p);
103   }
104
105   if (GNUNET_YES != GNUNET_DISK_file_close (f))
106     LOG (GNUNET_ERROR_TYPE_WARNING,
107          "Unable to close file\n");
108 }
109
110 char *
111 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
112 {
113   int size;
114   size_t name_buf_size;
115   char *end;
116   char *buf;
117   char *name_buf;
118   size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
119
120   name_buf_size = 512 * sizeof (char);
121   name_buf = GNUNET_malloc (name_buf_size);
122
123   if (keylen % 5 > 0)
124     keylen += 5 - keylen % 5;
125   keylen /= 5;
126   buf = GNUNET_malloc (keylen + 1);
127
128   end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
129       sizeof (struct GNUNET_CRYPTO_AuthKey),
130       buf,
131       keylen);
132
133   if (NULL == end)
134   {
135     GNUNET_free (buf);
136     GNUNET_break (0);
137   }
138   else
139   {
140     *end = '\0';
141   }
142
143   size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
144   if (0 > size)
145     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
146
147   GNUNET_free (buf);
148
149   return name_buf;
150 }
151
152
153 struct GNUNET_CRYPTO_AuthKey
154 string_to_auth_key (const char *str)
155 {
156   struct GNUNET_CRYPTO_AuthKey auth_key;
157
158   if (GNUNET_OK !=
159       GNUNET_STRINGS_string_to_data (str,
160                                      strlen (str),
161                                      &auth_key.key,
162                                      sizeof (struct GNUNET_CRYPTO_AuthKey)))
163   {
164     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
165   }
166
167   return auth_key;
168 }
169
170
171 char *
172 create_file (const char *name)
173 {
174   int size;
175   size_t name_buf_size;
176   char *name_buf;
177   char *prefix;
178   char *file_name;
179
180   prefix = "/tmp/rps/";
181   name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
182   name_buf = GNUNET_malloc (name_buf_size);
183
184   size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
185   if (0 > size)
186     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
187
188   if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
189   {
190     LOG (GNUNET_ERROR_TYPE_WARNING,
191          "Could not create directory %s.\n",
192          prefix);
193   }
194
195   if (NULL == strstr (name, "sampler_el"))
196   {/* only append random string to sampler */
197     if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
198           LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
199
200   GNUNET_free (name_buf);
201   return file_name;
202   }
203
204   return name_buf;
205 }
206
207 #endif /* TO_FILE */
208
209 /**
210  * @brief Try to ensure that `/tmp/rps` exists.
211  *
212  * @return #GNUNET_YES on success
213  *         #GNUNET_SYSERR on failure
214  */
215 static int ensure_folder_exist (void)
216 {
217   if (GNUNET_NO == GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
218   {
219     GNUNET_DISK_directory_create ("/tmp/rps");
220   }
221   if (GNUNET_YES != GNUNET_DISK_directory_test ("/tmp/rps/", GNUNET_NO))
222   {
223     return GNUNET_SYSERR;
224   }
225   return GNUNET_YES;
226 }
227
228 const char *
229 store_prefix_file_name (const struct GNUNET_PeerIdentity *peer,
230     const char *prefix)
231 {
232   unsigned int len_file_name;
233   unsigned int out_size;
234   char *file_name;
235   const char *pid_long;
236
237   if (GNUNET_SYSERR == ensure_folder_exist()) return NULL;
238   pid_long = GNUNET_i2s_full (peer);
239   len_file_name = (strlen (prefix) +
240                    strlen (pid_long) +
241                    11)
242                      * sizeof (char);
243   file_name = GNUNET_malloc (len_file_name);
244   out_size = GNUNET_snprintf (file_name,
245                               len_file_name,
246                               "/tmp/rps/%s-%s",
247                               prefix,
248                               pid_long);
249   if (len_file_name < out_size ||
250       0 > out_size)
251   {
252     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
253                "Failed to write string to buffer (size: %i, out_size: %i)\n",
254                len_file_name,
255                out_size);
256   }
257   return file_name;
258 }
259
260 /* end of gnunet-service-rps.c */