- leaks
[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 /**
31  * Check if one pid is bigger than other, accounting for overflow.
32  *
33  * @param bigger Argument that should be bigger.
34  * @param smaller Argument that should be smaller.
35  *
36  * @return True if bigger (arg1) has a higher value than smaller (arg 2).
37  */
38 int
39 GMC_is_pid_bigger (uint32_t bigger, uint32_t smaller)
40 {
41     return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
42             (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
43 }
44
45 /**
46  * Get the higher ACK value out of two values, taking in account overflow.
47  *
48  * @param a First ACK value.
49  * @param b Second ACK value.
50  *
51  * @return Highest ACK value from the two.
52  */
53 uint32_t
54 GMC_max_pid (uint32_t a, uint32_t b)
55 {
56   if (GMC_is_pid_bigger(a, b))
57     return a;
58   return b;
59 }
60
61
62 /**
63  * Get the lower ACK value out of two values, taking in account overflow.
64  *
65  * @param a First ACK value.
66  * @param b Second ACK value.
67  *
68  * @return Lowest ACK value from the two.
69  */
70 uint32_t
71 GMC_min_pid (uint32_t a, uint32_t b)
72 {
73   if (GMC_is_pid_bigger(a, b))
74     return b;
75   return a;
76 }
77
78
79 #if !defined(GNUNET_CULL_LOGGING)
80 const char *
81 GNUNET_MESH_DEBUG_M2S (uint16_t m)
82 {
83   static char buf[32];
84   switch (m)
85     {
86       /**
87        * Request the creation of a path
88        */
89     case 256: return "GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE"; break;
90
91       /**
92        * Request the modification of an existing path
93        */
94     case 257: return "GNUNET_MESSAGE_TYPE_MESH_PATH_CHANGE"; break;
95
96       /**
97        * Notify that a connection of a path is no longer valid
98        */
99     case 258: return "GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN"; break;
100
101       /**
102        * At some point, the route will spontaneously change
103        */
104     case 259: return "GNUNET_MESSAGE_TYPE_MESH_PATH_CHANGED"; break;
105
106       /**
107        * Transport data in the mesh (origin->end) unicast
108        */
109     case 260: return "GNUNET_MESSAGE_TYPE_MESH_UNICAST"; break;
110
111       /**
112        * Transport data to all peers in a tunnel
113        */
114     case 261: return "GNUNET_MESSAGE_TYPE_MESH_MULTICAST"; break;
115
116       /**
117        * Transport data back in the mesh (end->origin)
118        */
119     case 262: return "GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN"; break;
120
121       /**
122        * Send origin an ACK that the path is complete
123        */
124     case 263: return "GNUNET_MESSAGE_TYPE_MESH_PATH_ACK"; break;
125
126       /**
127        * Avoid path timeouts
128        */
129     case 264: return "GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE"; break;
130
131       /**
132        * Request the destuction of a path
133        */
134     case 265: return "GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY"; break;
135
136       /**
137        * Request the destruction of a whole tunnel
138        */
139     case 266: return "GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY"; break;
140
141       /**
142        * ACK for a data packet.
143        */
144     case 267: return "GNUNET_MESSAGE_TYPE_MESH_ACK"; break;
145
146       /**
147        * Connect to the mesh service, specifying subscriptions
148        */
149     case 272: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT"; break;
150
151       /**
152        * Ask the mesh service to create a new tunnel
153        */
154     case 273: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE"; break;
155
156       /**
157        * Ask the mesh service to destroy a tunnel
158        */
159     case 274: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY"; break;
160
161       /**
162        * Ask the mesh service to add a peer to an existing tunnel
163        */
164     case 275: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD"; break;
165
166       /**
167        * Ask the mesh service to remove a peer from a tunnel
168        */
169     case 276: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL"; break;
170
171       /**
172        * Ask the mesh service to add a peer offering a service to an existing tunnel
173        */
174     case 277: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE"; break;
175
176       /**
177        * Ask the mesh service to add a peer described by a service string
178        */
179     case 278: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX"; break;
180
181       /**
182        * Ask the mesh service to add a peer described by a service string
183        */
184     case 279: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING"; break;
185
186       /**
187        * Ask the mesh service to add a peer to the blacklist of an existing tunnel
188        */
189     case 280: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST"; break;
190
191       /**
192        * Ask the mesh service to remove a peer from the blacklist of a tunnel
193        */
194     case 281: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST"; break;
195
196       /**
197        * Set tunnel speed to slowest peer
198        */
199     case 282: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN"; break;
200
201       /**
202        * Set tunnel speed to fastest peer
203        */
204     case 283: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX"; break;
205
206       /**
207        * Set tunnel buffering on.
208        */
209     case 284: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER"; break;
210
211       /**
212        * Set tunnel buffering off.
213        */
214     case 285: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER"; break;
215
216       /**
217        * Local ACK for data.
218        */
219     case 286: return "GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK"; break;
220
221       /**
222        * 640kb should be enough for everybody
223        */
224     case 299: return "GNUNET_MESSAGE_TYPE_MESH_RESERVE_END"; break;
225     }
226   sprintf(buf, "%u (UNKNOWN TYPE)", m);
227   return buf;
228 }
229 #else
230 const char *
231 GNUNET_MESH_DEBUG_M2S (uint16_t m)
232 {
233   return "";
234 }
235 #endif