- add missing message type names
[oweals/gnunet.git] / src / mesh / mesh_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 mesh/mesh_common.c
23  * @brief MESH helper functions
24  * @author Bartlomiej Polot
25  */
26
27 #include "mesh.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 GM_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     GNUNET_break (0);
50     return "";
51   }
52 }
53
54 int
55 GM_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 GM_max_pid (uint32_t a, uint32_t b)
64 {
65   if (GM_is_pid_bigger(a, b))
66     return a;
67   return b;
68 }
69
70
71 uint32_t
72 GM_min_pid (uint32_t a, uint32_t b)
73 {
74   if (GM_is_pid_bigger(a, b))
75     return b;
76   return a;
77 }
78
79
80 #if !defined(GNUNET_CULL_LOGGING)
81 const char *
82 GM_m2s (uint16_t m)
83 {
84   static char buf[32];
85   const char *t;
86
87   switch (m)
88   {
89       /**
90        * Request the creation of a path
91        */
92     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE:
93       t = "CONNECTION_CREATE";
94       break;
95
96       /**
97        * Request the modification of an existing path
98        */
99     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK:
100       t = "CONNECTION_ACK";
101       break;
102
103       /**
104        * Notify that a connection of a path is no longer valid
105        */
106     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN:
107       t = "CONNECTION_BROKEN";
108       break;
109
110       /**
111        * At some point, the route will spontaneously change
112        */
113     case GNUNET_MESSAGE_TYPE_MESH_PATH_CHANGED:
114       t = "PATH_CHANGED";
115       break;
116
117       /**
118        * Transport payload data.
119        */
120     case GNUNET_MESSAGE_TYPE_MESH_DATA:
121       t = "DATA";
122       break;
123
124     /**
125      * Confirm receipt of payload data.
126      */
127     case GNUNET_MESSAGE_TYPE_MESH_DATA_ACK:
128       t = "DATA_ACK";
129       break;
130
131       /**
132        * Key exchange encapsulation.
133        */
134     case GNUNET_MESSAGE_TYPE_MESH_KX:
135       t = "KX";
136       break;
137
138       /**
139        * New ephemeral key.
140        */
141     case GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL:
142       t = "KX_EPHEMERAL";
143       break;
144
145       /**
146        * Challenge to test peer's session key.
147        */
148     case GNUNET_MESSAGE_TYPE_MESH_KX_PING:
149       t = "KX_PING";
150       break;
151
152       /**
153        * Answer to session key challenge.
154        */
155     case GNUNET_MESSAGE_TYPE_MESH_KX_PONG:
156       t = "KX_PONG";
157       break;
158
159       /**
160        * Request the destuction of a path
161        */
162     case GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY:
163       t = "CONNECTION_DESTROY";
164       break;
165
166       /**
167        * ACK for a data packet.
168        */
169     case GNUNET_MESSAGE_TYPE_MESH_ACK:
170       t = "ACK";
171       break;
172
173       /**
174        * POLL for ACK.
175        */
176     case GNUNET_MESSAGE_TYPE_MESH_POLL:
177       t = "POLL";
178       break;
179
180       /**
181        * Announce origin is still alive.
182        */
183     case GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE:
184       t = "KEEPALIVE";
185       break;
186
187     /**
188        * Connect to the mesh service, specifying subscriptions
189        */
190     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT:
191       t = "LOCAL_CONNECT";
192       break;
193
194       /**
195        * Ask the mesh service to create a new tunnel
196        */
197     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE:
198       t = "CHANNEL_CREATE";
199       break;
200
201       /**
202        * Ask the mesh service to destroy a tunnel
203        */
204     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY:
205       t = "CHANNEL_DESTROY";
206       break;
207
208       /**
209        * Confirm the creation of a channel.
210        */
211     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK:
212       t = "CHANNEL_ACK";
213       break;
214
215       /**
216        * Confirm the creation of a channel.
217        */
218     case GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK:
219       t = "CHANNEL_NACK";
220       break;
221
222       /**
223        * Encrypted payload.
224        */
225     case GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED:
226       t = "ENCRYPTED";
227       break;
228
229       /**
230        * Local payload traffic
231        */
232     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
233       t = "LOCAL_DATA";
234       break;
235
236       /**
237        * Local ACK for data.
238        */
239     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
240       t = "LOCAL_ACK";
241       break;
242
243       /**
244        * Local monitoring of channels.
245        */
246     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNELS:
247       t = "LOCAL_INFO_CHANNELS";
248       break;
249
250       /**
251        * Local monitoring of a channel.
252        */
253     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CHANNEL:
254       t = "LOCAL_INFO_CHANNEL";
255       break;
256
257       /**
258        * Local monitoring of service.
259        */
260     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS:
261       t = "LOCAL_INFO_TUNNELS";
262       break;
263
264       /**
265        * Local monitoring of service.
266        */
267     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL:
268       t = "LOCAL_INFO_TUNNEL";
269       break;
270
271       /**
272        * Local information about all connections of service.
273        */
274     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CONNECTIONS:
275       t = "LOCAL_INFO_CONNECTIONS";
276       break;
277
278       /**
279        * Local information of service about a specific connection.
280        */
281     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_CONNECTION:
282       t = "LOCAL_INFO_CONNECTION";
283       break;
284
285       /**
286        * Local information about all peers known to the service.
287        */
288     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEERS:
289       t = "LOCAL_INFO_PEERS";
290       break;
291
292       /**
293        * Local information of service about a specific peer.
294        */
295     case GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_PEER:
296       t = "LOCAL_INFO_PEER";
297       break;
298
299       /**
300        * Traffic (net-cat style) used by the Command Line Interface.
301        */
302     case GNUNET_MESSAGE_TYPE_MESH_CLI:
303       t = "CLI";
304       break;
305
306       /**
307        * 640kb should be enough for everybody
308        */
309     case 299:
310       t = "RESERVE_END";
311       break;
312
313     default:
314       sprintf(buf, "%u (UNKNOWN TYPE)", m);
315       return buf;
316   }
317   sprintf(buf, "%31s", t);
318   return buf;
319 }
320 #else
321 const char *
322 GM_m2s (uint16_t m)
323 {
324   return "";
325 }
326 #endif