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