- use correct hash type
[oweals/gnunet.git] / src / cadet / cadet_common.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 Christian Grothoff (and other contributing authors)
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   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   memcpy (s, GNUNET_h2s_full (GC_h2hc (id)), 52);
96   s[52] = '\0';
97
98   return s;
99 }
100
101
102 #if !defined(GNUNET_CULL_LOGGING)
103 const char *
104 GC_m2s (uint16_t m)
105 {
106   static char buf[2][16];
107   static int idx;
108   const char *s;
109
110   idx = (idx + 1) % 2;
111   switch (m)
112   {
113     /**
114      * Used to mark the "payload" of a non-payload message.
115      */
116     case 0:
117       s = "retrnsmit";
118       break;
119
120       /**
121        * Request the creation of a path
122        */
123     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
124       s = "CONN_CREAT";
125       break;
126
127       /**
128        * Request the modification of an existing path
129        */
130     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
131       s = "CONN_ACK";
132       break;
133
134       /**
135        * Notify that a connection of a path is no longer valid
136        */
137     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
138       s = "CONN_BRKN";
139       break;
140
141       /**
142        * At some point, the route will spontaneously change
143        */
144     case GNUNET_MESSAGE_TYPE_CADET_PATH_CHANGED:
145       s = "PATH_CHNGD";
146       break;
147
148       /**
149        * Transport payload data.
150        */
151     case GNUNET_MESSAGE_TYPE_CADET_DATA:
152       s = "DATA";
153       break;
154
155     /**
156      * Confirm receipt of payload data.
157      */
158     case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
159       s = "DATA_ACK";
160       break;
161
162       /**
163        * Key exchange encapsulation.
164        */
165     case GNUNET_MESSAGE_TYPE_CADET_KX:
166       s = "KX";
167       break;
168
169       /**
170        * Axolotl key exchange message.
171        */
172     case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
173       s = "AX_KX";
174       break;
175
176       /**
177        * New ephemeral key.
178        */
179     case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
180       s = "KX_EPHM";
181       break;
182
183       /**
184        * Answer to session key challenge.
185        */
186     case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
187       s = "KX_PONG";
188       break;
189
190       /**
191        * Request the destuction of a path
192        */
193     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
194       s = "CONN_DSTRY";
195       break;
196
197       /**
198        * ACK for a data packet.
199        */
200     case GNUNET_MESSAGE_TYPE_CADET_ACK:
201       s = "ACK";
202       break;
203
204       /**
205        * POLL for ACK.
206        */
207     case GNUNET_MESSAGE_TYPE_CADET_POLL:
208       s = "POLL";
209       break;
210
211       /**
212        * Announce origin is still alive.
213        */
214     case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
215       s = "KEEPALIVE";
216       break;
217
218     /**
219        * Connect to the cadet service, specifying subscriptions
220        */
221     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
222       s = "LOC_CONNCT";
223       break;
224
225       /**
226        * Ask the cadet service to create a new tunnel
227        */
228     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
229       s = "CHAN_CREAT";
230       break;
231
232       /**
233        * Ask the cadet service to destroy a tunnel
234        */
235     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
236       s = "CHAN_DSTRY";
237       break;
238
239       /**
240        * Confirm the creation of a channel.
241        */
242     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
243       s = "CHAN_ACK";
244       break;
245
246       /**
247        * Confirm the creation of a channel.
248        */
249     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
250       s = "CHAN_NACK";
251       break;
252
253       /**
254        * OTR encrypted payload.
255        */
256     case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
257       s = "OTR";
258       break;
259
260       /**
261        * Axolotl encrypted payload.
262        */
263     case GNUNET_MESSAGE_TYPE_CADET_AX:
264       s = "AX";
265       break;
266
267       /**
268        * Local payload traffic
269        */
270     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
271       s = "LOC_DATA";
272       break;
273
274       /**
275        * Local ACK for data.
276        */
277     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
278       s = "LOC_ACK";
279       break;
280
281       /**
282        * Local monitoring of channels.
283        */
284     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
285       s = "INFO_CHANS";
286       break;
287
288       /**
289        * Local monitoring of a channel.
290        */
291     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
292       s = "INFO_CHAN";
293       break;
294
295       /**
296        * Local monitoring of service.
297        */
298     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
299       s = "INFO_TUNS";
300       break;
301
302       /**
303        * Local monitoring of service.
304        */
305     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
306       s = "INFO_TUN";
307       break;
308
309       /**
310        * Local information about all connections of service.
311        */
312     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTIONS:
313       s = "INFO_CONNS";
314       break;
315
316       /**
317        * Local information of service about a specific connection.
318        */
319     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTION:
320       s = "INFO_CONN";
321       break;
322
323       /**
324        * Local information about all peers known to the service.
325        */
326     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
327       s = "INFO_PEERS";
328       break;
329
330       /**
331        * Local information of service about a specific peer.
332        */
333     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
334       s = "INFO_PEER";
335       break;
336
337       /**
338        * Traffic (net-cat style) used by the Command Line Interface.
339        */
340     case GNUNET_MESSAGE_TYPE_CADET_CLI:
341       s = "CLI";
342       break;
343
344       /**
345        * Debug request.
346        */
347     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP:
348       s = "INFO_DUMP";
349       break;
350
351     default:
352       SPRINTF (buf[idx], "%u (UNKNOWN)", m);
353       return buf[idx];
354   }
355   SPRINTF (buf[idx], "{%10s}", s);
356   return buf[idx];
357 }
358 #else
359 const char *
360 GC_m2s (uint16_t m)
361 {
362   return "";
363 }
364 #endif