refactor DHT for new service API
[oweals/gnunet.git] / src / cadet / cadet_common.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 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 /**
22  * @file cadet/cadet_common.c
23  * @brief CADET helper functions
24  * @author Bartlomiej Polot
25  */
26
27 #include "cadet.h"
28
29 /**
30  * @brief Translate a fwd variable into a string representation, for logging.
31  *
32  * @param fwd Is FWD? (#GNUNET_YES or #GNUNET_NO)
33  *
34  * @return String representing FWD or BCK.
35  */
36 char *
37 GC_f2s (int fwd)
38 {
39   if (GNUNET_YES == fwd)
40   {
41     return "FWD";
42   }
43   else if (GNUNET_NO == fwd)
44   {
45     return "BCK";
46   }
47   else
48   {
49     /* Not an error, can happen with CONNECTION_BROKEN messages. */
50     return "\???";
51   }
52 }
53
54 int
55 GC_is_pid_bigger (uint32_t bigger, uint32_t smaller)
56 {
57     return (GNUNET_YES == PID_OVERFLOW (smaller, bigger) ||
58             (bigger > smaller && GNUNET_NO == PID_OVERFLOW (bigger, smaller)));
59 }
60
61
62 uint32_t
63 GC_max_pid (uint32_t a, uint32_t b)
64 {
65   if (GC_is_pid_bigger(a, b))
66     return a;
67   return b;
68 }
69
70
71 uint32_t
72 GC_min_pid (uint32_t a, uint32_t b)
73 {
74   if (GC_is_pid_bigger(a, b))
75     return b;
76   return a;
77 }
78
79
80 const struct GNUNET_HashCode *
81 GC_h2hc (const struct GNUNET_CADET_Hash *id)
82 {
83   static struct GNUNET_HashCode hc;
84   GNUNET_memcpy (&hc, id, sizeof (*id));
85
86   return &hc;
87 }
88
89
90 const char *
91 GC_h2s (const struct GNUNET_CADET_Hash *id)
92 {
93   static char s[53];
94
95   GNUNET_memcpy (s, GNUNET_h2s_full (GC_h2hc (id)), 52);
96   s[52] = '\0';
97
98   return s;
99 }
100
101
102 /**
103  * Allocate a string with a hexdump of any binary data.
104  *
105  * @param bin Arbitrary binary data.
106  * @param len Length of @a bin in bytes.
107  * @param output Where to write the output (if *output be NULL it's allocated).
108  *
109  * @return The size of the output.
110  */
111 size_t
112 GC_bin2s (void *bin, unsigned int len, char **output)
113 {
114   char *data = bin;
115   char *buf;
116   unsigned int s_len;
117   unsigned int i;
118
119   s_len = 2 * len + 1;
120   if (NULL == *output)
121     *output = GNUNET_malloc (s_len);
122   buf = *output;
123
124   for (i = 0; i < len; i++)
125   {
126     SPRINTF (&buf[2 * i], "%2X", data[i]);
127   }
128   buf[s_len - 1] = '\0';
129
130   return s_len;
131 }
132
133
134 #if !defined(GNUNET_CULL_LOGGING)
135 const char *
136 GC_m2s (uint16_t m)
137 {
138   static char buf[2][16];
139   static int idx;
140   const char *s;
141
142   idx = (idx + 1) % 2;
143   switch (m)
144   {
145     /**
146      * Used to mark the "payload" of a non-payload message.
147      */
148     case 0:
149       s = "retransmit";
150       break;
151
152       /**
153        * Request the creation of a path
154        */
155     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
156       s = "CONN_CREAT";
157       break;
158
159       /**
160        * Request the modification of an existing path
161        */
162     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
163       s = "CONN_ACK";
164       break;
165
166       /**
167        * Notify that a connection of a path is no longer valid
168        */
169     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
170       s = "CONN_BRKN";
171       break;
172
173       /**
174        * At some point, the route will spontaneously change
175        */
176     case GNUNET_MESSAGE_TYPE_CADET_PATH_CHANGED:
177       s = "PATH_CHNGD";
178       break;
179
180       /**
181        * Transport payload data.
182        */
183     case GNUNET_MESSAGE_TYPE_CADET_DATA:
184       s = "DATA";
185       break;
186
187     /**
188      * Confirm receipt of payload data.
189      */
190     case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
191       s = "DATA_ACK";
192       break;
193
194       /**
195        * Key exchange encapsulation.
196        */
197     case GNUNET_MESSAGE_TYPE_CADET_KX:
198       s = "KX";
199       break;
200
201       /**
202        * Axolotl key exchange message.
203        */
204     case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
205       s = "AX_KX";
206       break;
207
208       /**
209        * Request the destuction of a path
210        */
211     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
212       s = "CONN_DSTRY";
213       break;
214
215       /**
216        * ACK for a data packet.
217        */
218     case GNUNET_MESSAGE_TYPE_CADET_ACK:
219       s = "ACK";
220       break;
221
222       /**
223        * POLL for ACK.
224        */
225     case GNUNET_MESSAGE_TYPE_CADET_POLL:
226       s = "POLL";
227       break;
228
229       /**
230        * Announce origin is still alive.
231        */
232     case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
233       s = "KEEPALIVE";
234       break;
235
236       /**
237        * Open port
238        */
239     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
240       s = "OPEN_PORT";
241       break;
242
243       /**
244        * Close port
245        */
246     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
247       s = "CLOSE_PORT";
248       break;
249
250       /**
251        * Ask the cadet service to create a new tunnel
252        */
253     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
254       s = "CHAN_CREAT";
255       break;
256
257       /**
258        * Ask the cadet service to destroy a tunnel
259        */
260     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
261       s = "CHAN_DSTRY";
262       break;
263
264       /**
265        * Confirm the creation of a channel.
266        */
267     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
268       s = "CHAN_ACK";
269       break;
270
271       /**
272        * Confirm the creation of a channel.
273        */
274     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
275       s = "CHAN_NACK";
276       break;
277
278       /**
279        * Axolotl encrypted payload.
280        */
281     case GNUNET_MESSAGE_TYPE_CADET_AX:
282       s = "AX";
283       break;
284
285       /**
286        * Local payload traffic
287        */
288     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
289       s = "LOC_DATA";
290       break;
291
292       /**
293        * Local ACK for data.
294        */
295     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
296       s = "LOC_ACK";
297       break;
298
299       /**
300        * Local monitoring of channels.
301        */
302     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
303       s = "INFO_CHANS";
304       break;
305
306       /**
307        * Local monitoring of a channel.
308        */
309     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
310       s = "INFO_CHAN";
311       break;
312
313       /**
314        * Local monitoring of service.
315        */
316     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
317       s = "INFO_TUNS";
318       break;
319
320       /**
321        * Local monitoring of service.
322        */
323     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
324       s = "INFO_TUN";
325       break;
326
327       /**
328        * Local information about all connections of service.
329        */
330     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTIONS:
331       s = "INFO_CONNS";
332       break;
333
334       /**
335        * Local information of service about a specific connection.
336        */
337     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTION:
338       s = "INFO_CONN";
339       break;
340
341       /**
342        * Local information about all peers known to the service.
343        */
344     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
345       s = "INFO_PEERS";
346       break;
347
348       /**
349        * Local information of service about a specific peer.
350        */
351     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
352       s = "INFO_PEER";
353       break;
354
355       /**
356        * Traffic (net-cat style) used by the Command Line Interface.
357        */
358     case GNUNET_MESSAGE_TYPE_CADET_CLI:
359       s = "CLI";
360       break;
361
362       /**
363        * Debug request.
364        */
365     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP:
366       s = "INFO_DUMP";
367       break;
368
369       /**
370        * Used to mark the "payload" of a non-payload message.
371        */
372     case UINT16_MAX:
373       s = "      N/A";
374       break;
375
376
377     default:
378       SPRINTF (buf[idx], "{UNK: %5u}", m);
379       return buf[idx];
380   }
381   SPRINTF (buf[idx], "{%10s}", s);
382   return buf[idx];
383 }
384 #else
385 const char *
386 GC_m2s (uint16_t m)
387 {
388   return "";
389 }
390 #endif