Adapting verify successor code to use trail id
[oweals/gnunet.git] / src / cadet / cadet_common.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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[32];
107   const char *t;
108
109   switch (m)
110   {
111       /**
112        * Request the creation of a path
113        */
114     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE:
115       t = "CONNECTION_CREATE";
116       break;
117
118       /**
119        * Request the modification of an existing path
120        */
121     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
122       t = "CONNECTION_ACK";
123       break;
124
125       /**
126        * Notify that a connection of a path is no longer valid
127        */
128     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
129       t = "CONNECTION_BROKEN";
130       break;
131
132       /**
133        * At some point, the route will spontaneously change
134        */
135     case GNUNET_MESSAGE_TYPE_CADET_PATH_CHANGED:
136       t = "PATH_CHANGED";
137       break;
138
139       /**
140        * Transport payload data.
141        */
142     case GNUNET_MESSAGE_TYPE_CADET_DATA:
143       t = "DATA";
144       break;
145
146     /**
147      * Confirm receipt of payload data.
148      */
149     case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
150       t = "DATA_ACK";
151       break;
152
153       /**
154        * Key exchange encapsulation.
155        */
156     case GNUNET_MESSAGE_TYPE_CADET_KX:
157       t = "KX";
158       break;
159
160       /**
161        * New ephemeral key.
162        */
163     case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
164       t = "KX_EPHEMERAL";
165       break;
166
167       /**
168        * Challenge to test peer's session key.
169        */
170     case GNUNET_MESSAGE_TYPE_CADET_KX_PING:
171       t = "KX_PING";
172       break;
173
174       /**
175        * Answer to session key challenge.
176        */
177     case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
178       t = "KX_PONG";
179       break;
180
181       /**
182        * Request the destuction of a path
183        */
184     case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
185       t = "CONNECTION_DESTROY";
186       break;
187
188       /**
189        * ACK for a data packet.
190        */
191     case GNUNET_MESSAGE_TYPE_CADET_ACK:
192       t = "ACK";
193       break;
194
195       /**
196        * POLL for ACK.
197        */
198     case GNUNET_MESSAGE_TYPE_CADET_POLL:
199       t = "POLL";
200       break;
201
202       /**
203        * Announce origin is still alive.
204        */
205     case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
206       t = "KEEPALIVE";
207       break;
208
209     /**
210        * Connect to the cadet service, specifying subscriptions
211        */
212     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
213       t = "LOCAL_CONNECT";
214       break;
215
216       /**
217        * Ask the cadet service to create a new tunnel
218        */
219     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
220       t = "CHANNEL_CREATE";
221       break;
222
223       /**
224        * Ask the cadet service to destroy a tunnel
225        */
226     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
227       t = "CHANNEL_DESTROY";
228       break;
229
230       /**
231        * Confirm the creation of a channel.
232        */
233     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
234       t = "CHANNEL_ACK";
235       break;
236
237       /**
238        * Confirm the creation of a channel.
239        */
240     case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
241       t = "CHANNEL_NACK";
242       break;
243
244       /**
245        * Encrypted payload.
246        */
247     case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
248       t = "ENCRYPTED";
249       break;
250
251       /**
252        * Local payload traffic
253        */
254     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
255       t = "LOCAL_DATA";
256       break;
257
258       /**
259        * Local ACK for data.
260        */
261     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
262       t = "LOCAL_ACK";
263       break;
264
265       /**
266        * Local monitoring of channels.
267        */
268     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
269       t = "LOCAL_INFO_CHANNELS";
270       break;
271
272       /**
273        * Local monitoring of a channel.
274        */
275     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
276       t = "LOCAL_INFO_CHANNEL";
277       break;
278
279       /**
280        * Local monitoring of service.
281        */
282     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
283       t = "LOCAL_INFO_TUNNELS";
284       break;
285
286       /**
287        * Local monitoring of service.
288        */
289     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
290       t = "LOCAL_INFO_TUNNEL";
291       break;
292
293       /**
294        * Local information about all connections of service.
295        */
296     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTIONS:
297       t = "LOCAL_INFO_CONNECTIONS";
298       break;
299
300       /**
301        * Local information of service about a specific connection.
302        */
303     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CONNECTION:
304       t = "LOCAL_INFO_CONNECTION";
305       break;
306
307       /**
308        * Local information about all peers known to the service.
309        */
310     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
311       t = "LOCAL_INFO_PEERS";
312       break;
313
314       /**
315        * Local information of service about a specific peer.
316        */
317     case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
318       t = "LOCAL_INFO_PEER";
319       break;
320
321       /**
322        * Traffic (net-cat style) used by the Command Line Interface.
323        */
324     case GNUNET_MESSAGE_TYPE_CADET_CLI:
325       t = "CLI";
326       break;
327
328       /**
329        * 640kb should be enough for everybody
330        */
331     case 299:
332       t = "RESERVE_END";
333       break;
334
335     default:
336       sprintf(buf, "%u (UNKNOWN TYPE)", m);
337       return buf;
338   }
339   sprintf(buf, "{%18s}", t);
340   return buf;
341 }
342 #else
343 const char *
344 GC_m2s (uint16_t m)
345 {
346   return "";
347 }
348 #endif