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