- add underlay api implementation
[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   switch (m)
86     {
87       /**
88        * Request the creation of a path
89        */
90     case 256: return "GNUNET_MESSAGE_TYPE_MESH_CONNECTION_CREATE";
91
92       /**
93        * Request the modification of an existing path
94        */
95     case 257: return "GNUNET_MESSAGE_TYPE_MESH_CONNECTION_ACK";
96
97       /**
98        * Notify that a connection of a path is no longer valid
99        */
100     case 258: return "GNUNET_MESSAGE_TYPE_MESH_CONNECTION_BROKEN";
101
102       /**
103        * At some point, the route will spontaneously change
104        */
105     case 259: return "GNUNET_MESSAGE_TYPE_MESH_PATH_CHANGED";
106
107       /**
108        * Transport payload data.
109        */
110     case 260: return "GNUNET_MESSAGE_TYPE_MESH_DATA";
111
112     /**
113      * Confirm receipt of payload data.
114      */
115     case 261: return "GNUNET_MESSAGE_TYPE_MESH_DATA_ACK";
116
117       /**
118        * Key exchange encapsulation.
119        */
120     case 262: return "GNUNET_MESSAGE_TYPE_MESH_KX";
121
122       /**
123        * New ephemeral key.
124        */
125     case 263: return "GNUNET_MESSAGE_TYPE_MESH_KX_EPHEMERAL";
126
127       /**
128        * Challenge to test peer's session key.
129        */
130     case 264: return "GNUNET_MESSAGE_TYPE_MESH_KX_PING";
131
132       /**
133        * Answer to session key challenge.
134        */
135     case 265: return "GNUNET_MESSAGE_TYPE_MESH_KX_PONG";
136
137       /**
138        * Request the destuction of a path
139        */
140     case 266: return "GNUNET_MESSAGE_TYPE_MESH_CONNECTION_DESTROY";
141
142       /**
143        * ACK for a data packet.
144        */
145     case 268: return "GNUNET_MESSAGE_TYPE_MESH_ACK";
146
147       /**
148        * POLL for ACK.
149        */
150     case 269: return "GNUNET_MESSAGE_TYPE_MESH_POLL";
151
152       /**
153        * Announce origin is still alive.
154        */
155     case 270: return "GNUNET_MESSAGE_TYPE_MESH_KEEPALIVE";
156
157     /**
158        * Connect to the mesh service, specifying subscriptions
159        */
160     case 272: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT";
161
162       /**
163        * Ask the mesh service to create a new tunnel
164        */
165     case 273: return "GNUNET_MESSAGE_TYPE_MESH_CHANNEL_CREATE";
166
167       /**
168        * Ask the mesh service to destroy a tunnel
169        */
170     case 274: return "GNUNET_MESSAGE_TYPE_MESH_CHANNEL_DESTROY";
171
172       /**
173        * Confirm the creation of a channel.
174        */
175     case 275: return "GNUNET_MESSAGE_TYPE_MESH_CHANNEL_ACK";
176
177       /**
178        * Confirm the creation of a channel.
179        */
180     case 276: return "GNUNET_MESSAGE_TYPE_MESH_CHANNEL_NACK";
181
182       /**
183        * Encrypted payload.
184        */
185     case 280: return "GNUNET_MESSAGE_TYPE_MESH_ENCRYPTED";
186
187       /**
188        * Local payload traffic
189        */
190     case 285: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA";
191
192       /**
193        * Local ACK for data.
194        */
195     case 286: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK";
196
197       /**
198        * Local monitoring of service.
199        */
200     case 287: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_NACK";
201
202       /**
203        * 640kb should be enough for everybody
204        */
205     case 299: return "GNUNET_MESSAGE_TYPE_MESH_RESERVE_END";
206     }
207   sprintf(buf, "%u (UNKNOWN TYPE)", m);
208   return buf;
209 }
210 #else
211 const char *
212 GM_m2s (uint16_t m)
213 {
214   return "";
215 }
216 #endif