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