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