diff options
Diffstat (limited to 'src/nat/nat_api.c')
-rw-r--r-- | src/nat/nat_api.c | 524 |
1 files changed, 263 insertions, 261 deletions
diff --git a/src/nat/nat_api.c b/src/nat/nat_api.c index 877589d85..c1125dc75 100644 --- a/src/nat/nat_api.c +++ b/src/nat/nat_api.c | |||
@@ -35,7 +35,8 @@ | |||
35 | /** | 35 | /** |
36 | * Entry in DLL of addresses of this peer. | 36 | * Entry in DLL of addresses of this peer. |
37 | */ | 37 | */ |
38 | struct AddrEntry { | 38 | struct AddrEntry |
39 | { | ||
39 | /** | 40 | /** |
40 | * DLL. | 41 | * DLL. |
41 | */ | 42 | */ |
@@ -67,7 +68,8 @@ struct AddrEntry { | |||
67 | /** | 68 | /** |
68 | * Handle for active NAT registrations. | 69 | * Handle for active NAT registrations. |
69 | */ | 70 | */ |
70 | struct GNUNET_NAT_Handle { | 71 | struct GNUNET_NAT_Handle |
72 | { | ||
71 | /** | 73 | /** |
72 | * Configuration we use. | 74 | * Configuration we use. |
73 | */ | 75 | */ |
@@ -126,7 +128,7 @@ struct GNUNET_NAT_Handle { | |||
126 | * @param cls our `struct GNUNET_NAT_Handle *` | 128 | * @param cls our `struct GNUNET_NAT_Handle *` |
127 | */ | 129 | */ |
128 | static void | 130 | static void |
129 | do_connect(void *cls); | 131 | do_connect (void *cls); |
130 | 132 | ||
131 | 133 | ||
132 | /** | 134 | /** |
@@ -135,29 +137,29 @@ do_connect(void *cls); | |||
135 | * @param nh handle to reconnect | 137 | * @param nh handle to reconnect |
136 | */ | 138 | */ |
137 | static void | 139 | static void |
138 | reconnect(struct GNUNET_NAT_Handle *nh) | 140 | reconnect (struct GNUNET_NAT_Handle *nh) |
139 | { | 141 | { |
140 | struct AddrEntry *ae; | 142 | struct AddrEntry *ae; |
141 | 143 | ||
142 | if (NULL != nh->mq) | 144 | if (NULL != nh->mq) |
143 | { | 145 | { |
144 | GNUNET_MQ_destroy(nh->mq); | 146 | GNUNET_MQ_destroy (nh->mq); |
145 | nh->mq = NULL; | 147 | nh->mq = NULL; |
146 | } | 148 | } |
147 | while (NULL != (ae = nh->ae_head)) | 149 | while (NULL != (ae = nh->ae_head)) |
148 | { | 150 | { |
149 | GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae); | 151 | GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); |
150 | nh->address_callback(nh->callback_cls, | 152 | nh->address_callback (nh->callback_cls, |
151 | &ae->app_ctx, | 153 | &ae->app_ctx, |
152 | GNUNET_NO, | 154 | GNUNET_NO, |
153 | ae->ac, | 155 | ae->ac, |
154 | (const struct sockaddr *)&ae[1], | 156 | (const struct sockaddr *) &ae[1], |
155 | ae->addrlen); | 157 | ae->addrlen); |
156 | GNUNET_free(ae); | 158 | GNUNET_free (ae); |
157 | } | 159 | } |
158 | nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF(nh->reconnect_delay); | 160 | nh->reconnect_delay = GNUNET_TIME_STD_BACKOFF (nh->reconnect_delay); |
159 | nh->reconnect_task = | 161 | nh->reconnect_task = |
160 | GNUNET_SCHEDULER_add_delayed(nh->reconnect_delay, &do_connect, nh); | 162 | GNUNET_SCHEDULER_add_delayed (nh->reconnect_delay, &do_connect, nh); |
161 | } | 163 | } |
162 | 164 | ||
163 | 165 | ||
@@ -169,15 +171,15 @@ reconnect(struct GNUNET_NAT_Handle *nh) | |||
169 | * @return #GNUNET_OK if @a crm is well-formed | 171 | * @return #GNUNET_OK if @a crm is well-formed |
170 | */ | 172 | */ |
171 | static int | 173 | static int |
172 | check_connection_reversal_request( | 174 | check_connection_reversal_request ( |
173 | void *cls, | 175 | void *cls, |
174 | const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) | 176 | const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) |
175 | { | 177 | { |
176 | if (ntohs(crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in)) | 178 | if (ntohs (crm->header.size) != sizeof(*crm) + sizeof(struct sockaddr_in)) |
177 | { | 179 | { |
178 | GNUNET_break(0); | 180 | GNUNET_break (0); |
179 | return GNUNET_SYSERR; | 181 | return GNUNET_SYSERR; |
180 | } | 182 | } |
181 | return GNUNET_OK; | 183 | return GNUNET_OK; |
182 | } | 184 | } |
183 | 185 | ||
@@ -189,15 +191,15 @@ check_connection_reversal_request( | |||
189 | * @param crm the message | 191 | * @param crm the message |
190 | */ | 192 | */ |
191 | static void | 193 | static void |
192 | handle_connection_reversal_request( | 194 | handle_connection_reversal_request ( |
193 | void *cls, | 195 | void *cls, |
194 | const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) | 196 | const struct GNUNET_NAT_ConnectionReversalRequestedMessage *crm) |
195 | { | 197 | { |
196 | struct GNUNET_NAT_Handle *nh = cls; | 198 | struct GNUNET_NAT_Handle *nh = cls; |
197 | 199 | ||
198 | nh->reversal_callback(nh->callback_cls, | 200 | nh->reversal_callback (nh->callback_cls, |
199 | (const struct sockaddr *)&crm[1], | 201 | (const struct sockaddr *) &crm[1], |
200 | sizeof(struct sockaddr_in)); | 202 | sizeof(struct sockaddr_in)); |
201 | } | 203 | } |
202 | 204 | ||
203 | 205 | ||
@@ -209,38 +211,38 @@ handle_connection_reversal_request( | |||
209 | * @return #GNUNET_OK if @a crm is well-formed | 211 | * @return #GNUNET_OK if @a crm is well-formed |
210 | */ | 212 | */ |
211 | static int | 213 | static int |
212 | check_address_change_notification( | 214 | check_address_change_notification ( |
213 | void *cls, | 215 | void *cls, |
214 | const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) | 216 | const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) |
215 | { | 217 | { |
216 | size_t alen = ntohs(acn->header.size) - sizeof(*acn); | 218 | size_t alen = ntohs (acn->header.size) - sizeof(*acn); |
217 | 219 | ||
218 | switch (alen) | 220 | switch (alen) |
219 | { | 221 | { |
220 | case sizeof(struct sockaddr_in): { | 222 | case sizeof(struct sockaddr_in): { |
221 | const struct sockaddr_in *s4 = (const struct sockaddr_in *)&acn[1]; | 223 | const struct sockaddr_in *s4 = (const struct sockaddr_in *) &acn[1]; |
222 | if (AF_INET != s4->sin_family) | 224 | if (AF_INET != s4->sin_family) |
223 | { | 225 | { |
224 | GNUNET_break(0); | 226 | GNUNET_break (0); |
225 | return GNUNET_SYSERR; | 227 | return GNUNET_SYSERR; |
226 | } | 228 | } |
227 | } | 229 | } |
228 | break; | 230 | break; |
229 | 231 | ||
230 | case sizeof(struct sockaddr_in6): { | 232 | case sizeof(struct sockaddr_in6): { |
231 | const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *)&acn[1]; | 233 | const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) &acn[1]; |
232 | if (AF_INET6 != s6->sin6_family) | 234 | if (AF_INET6 != s6->sin6_family) |
233 | { | 235 | { |
234 | GNUNET_break(0); | 236 | GNUNET_break (0); |
235 | return GNUNET_SYSERR; | 237 | return GNUNET_SYSERR; |
236 | } | 238 | } |
237 | } | 239 | } |
238 | break; | 240 | break; |
239 | 241 | ||
240 | default: | 242 | default: |
241 | GNUNET_break(0); | 243 | GNUNET_break (0); |
242 | return GNUNET_SYSERR; | 244 | return GNUNET_SYSERR; |
243 | } | 245 | } |
244 | return GNUNET_OK; | 246 | return GNUNET_OK; |
245 | } | 247 | } |
246 | 248 | ||
@@ -252,53 +254,53 @@ check_address_change_notification( | |||
252 | * @param acn the message | 254 | * @param acn the message |
253 | */ | 255 | */ |
254 | static void | 256 | static void |
255 | handle_address_change_notification( | 257 | handle_address_change_notification ( |
256 | void *cls, | 258 | void *cls, |
257 | const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) | 259 | const struct GNUNET_NAT_AddressChangeNotificationMessage *acn) |
258 | { | 260 | { |
259 | struct GNUNET_NAT_Handle *nh = cls; | 261 | struct GNUNET_NAT_Handle *nh = cls; |
260 | size_t alen = ntohs(acn->header.size) - sizeof(*acn); | 262 | size_t alen = ntohs (acn->header.size) - sizeof(*acn); |
261 | const struct sockaddr *sa = (const struct sockaddr *)&acn[1]; | 263 | const struct sockaddr *sa = (const struct sockaddr *) &acn[1]; |
262 | enum GNUNET_NAT_AddressClass ac; | 264 | enum GNUNET_NAT_AddressClass ac; |
263 | struct AddrEntry *ae; | 265 | struct AddrEntry *ae; |
264 | 266 | ||
265 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 267 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
266 | "Received address change notification\n"); | 268 | "Received address change notification\n"); |
267 | ac = (enum GNUNET_NAT_AddressClass)ntohl(acn->addr_class); | 269 | ac = (enum GNUNET_NAT_AddressClass) ntohl (acn->addr_class); |
268 | if (GNUNET_YES == ntohl(acn->add_remove)) | 270 | if (GNUNET_YES == ntohl (acn->add_remove)) |
269 | { | 271 | { |
270 | ae = GNUNET_malloc(sizeof(*ae) + alen); | 272 | ae = GNUNET_malloc (sizeof(*ae) + alen); |
271 | ae->ac = ac; | 273 | ae->ac = ac; |
272 | ae->addrlen = alen; | 274 | ae->addrlen = alen; |
273 | GNUNET_memcpy(&ae[1], sa, alen); | 275 | GNUNET_memcpy (&ae[1], sa, alen); |
274 | GNUNET_CONTAINER_DLL_insert(nh->ae_head, nh->ae_tail, ae); | 276 | GNUNET_CONTAINER_DLL_insert (nh->ae_head, nh->ae_tail, ae); |
275 | nh->address_callback(nh->callback_cls, | 277 | nh->address_callback (nh->callback_cls, |
276 | &ae->app_ctx, | 278 | &ae->app_ctx, |
277 | ntohl(acn->add_remove), | 279 | ntohl (acn->add_remove), |
278 | ac, | 280 | ac, |
279 | sa, | 281 | sa, |
280 | alen); | 282 | alen); |
281 | } | 283 | } |
282 | else | 284 | else |
285 | { | ||
286 | for (ae = nh->ae_head; NULL != ae; ae = ae->next) | ||
287 | if ((ae->addrlen == alen) && (0 == memcmp (&ae[1], sa, alen))) | ||
288 | break; | ||
289 | if (NULL == ae) | ||
283 | { | 290 | { |
284 | for (ae = nh->ae_head; NULL != ae; ae = ae->next) | 291 | GNUNET_break (0); |
285 | if ((ae->addrlen == alen) && (0 == memcmp(&ae[1], sa, alen))) | 292 | reconnect (nh); |
286 | break; | 293 | return; |
287 | if (NULL == ae) | ||
288 | { | ||
289 | GNUNET_break(0); | ||
290 | reconnect(nh); | ||
291 | return; | ||
292 | } | ||
293 | GNUNET_CONTAINER_DLL_remove(nh->ae_head, nh->ae_tail, ae); | ||
294 | nh->address_callback(nh->callback_cls, | ||
295 | &ae->app_ctx, | ||
296 | ntohl(acn->add_remove), | ||
297 | ac, | ||
298 | sa, | ||
299 | alen); | ||
300 | GNUNET_free(ae); | ||
301 | } | 294 | } |
295 | GNUNET_CONTAINER_DLL_remove (nh->ae_head, nh->ae_tail, ae); | ||
296 | nh->address_callback (nh->callback_cls, | ||
297 | &ae->app_ctx, | ||
298 | ntohl (acn->add_remove), | ||
299 | ac, | ||
300 | sa, | ||
301 | alen); | ||
302 | GNUNET_free (ae); | ||
303 | } | ||
302 | } | 304 | } |
303 | 305 | ||
304 | 306 | ||
@@ -309,11 +311,11 @@ handle_address_change_notification( | |||
309 | * @param error details about the error | 311 | * @param error details about the error |
310 | */ | 312 | */ |
311 | static void | 313 | static void |
312 | mq_error_handler(void *cls, enum GNUNET_MQ_Error error) | 314 | mq_error_handler (void *cls, enum GNUNET_MQ_Error error) |
313 | { | 315 | { |
314 | struct GNUNET_NAT_Handle *nh = cls; | 316 | struct GNUNET_NAT_Handle *nh = cls; |
315 | 317 | ||
316 | reconnect(nh); | 318 | reconnect (nh); |
317 | } | 319 | } |
318 | 320 | ||
319 | 321 | ||
@@ -323,32 +325,32 @@ mq_error_handler(void *cls, enum GNUNET_MQ_Error error) | |||
323 | * @param cls our `struct GNUNET_NAT_Handle *` | 325 | * @param cls our `struct GNUNET_NAT_Handle *` |
324 | */ | 326 | */ |
325 | static void | 327 | static void |
326 | do_connect(void *cls) | 328 | do_connect (void *cls) |
327 | { | 329 | { |
328 | struct GNUNET_NAT_Handle *nh = cls; | 330 | struct GNUNET_NAT_Handle *nh = cls; |
329 | struct GNUNET_MQ_MessageHandler handlers[] = | 331 | struct GNUNET_MQ_MessageHandler handlers[] = |
330 | { GNUNET_MQ_hd_var_size(connection_reversal_request, | 332 | { GNUNET_MQ_hd_var_size (connection_reversal_request, |
331 | GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, | 333 | GNUNET_MESSAGE_TYPE_NAT_CONNECTION_REVERSAL_REQUESTED, |
332 | struct | 334 | struct |
333 | GNUNET_NAT_ConnectionReversalRequestedMessage, | 335 | GNUNET_NAT_ConnectionReversalRequestedMessage, |
334 | nh), | 336 | nh), |
335 | GNUNET_MQ_hd_var_size(address_change_notification, | 337 | GNUNET_MQ_hd_var_size (address_change_notification, |
336 | GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, | 338 | GNUNET_MESSAGE_TYPE_NAT_ADDRESS_CHANGE, |
337 | struct GNUNET_NAT_AddressChangeNotificationMessage, | 339 | struct GNUNET_NAT_AddressChangeNotificationMessage, |
338 | nh), | 340 | nh), |
339 | GNUNET_MQ_handler_end() }; | 341 | GNUNET_MQ_handler_end () }; |
340 | struct GNUNET_MQ_Envelope *env; | 342 | struct GNUNET_MQ_Envelope *env; |
341 | 343 | ||
342 | nh->reconnect_task = NULL; | 344 | nh->reconnect_task = NULL; |
343 | nh->mq = | 345 | nh->mq = |
344 | GNUNET_CLIENT_connect(nh->cfg, "nat", handlers, &mq_error_handler, nh); | 346 | GNUNET_CLIENT_connect (nh->cfg, "nat", handlers, &mq_error_handler, nh); |
345 | if (NULL == nh->mq) | 347 | if (NULL == nh->mq) |
346 | { | 348 | { |
347 | reconnect(nh); | 349 | reconnect (nh); |
348 | return; | 350 | return; |
349 | } | 351 | } |
350 | env = GNUNET_MQ_msg_copy(nh->reg); | 352 | env = GNUNET_MQ_msg_copy (nh->reg); |
351 | GNUNET_MQ_send(nh->mq, env); | 353 | GNUNET_MQ_send (nh->mq, env); |
352 | } | 354 | } |
353 | 355 | ||
354 | 356 | ||
@@ -373,15 +375,15 @@ do_connect(void *cls) | |||
373 | * @return NULL on error, otherwise handle that can be used to unregister | 375 | * @return NULL on error, otherwise handle that can be used to unregister |
374 | */ | 376 | */ |
375 | struct GNUNET_NAT_Handle * | 377 | struct GNUNET_NAT_Handle * |
376 | GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, | 378 | GNUNET_NAT_register (const struct GNUNET_CONFIGURATION_Handle *cfg, |
377 | const char *config_section, | 379 | const char *config_section, |
378 | uint8_t proto, | 380 | uint8_t proto, |
379 | unsigned int num_addrs, | 381 | unsigned int num_addrs, |
380 | const struct sockaddr **addrs, | 382 | const struct sockaddr **addrs, |
381 | const socklen_t *addrlens, | 383 | const socklen_t *addrlens, |
382 | GNUNET_NAT_AddressCallback address_callback, | 384 | GNUNET_NAT_AddressCallback address_callback, |
383 | GNUNET_NAT_ReversalCallback reversal_callback, | 385 | GNUNET_NAT_ReversalCallback reversal_callback, |
384 | void *callback_cls) | 386 | void *callback_cls) |
385 | { | 387 | { |
386 | struct GNUNET_NAT_Handle *nh; | 388 | struct GNUNET_NAT_Handle *nh; |
387 | struct GNUNET_NAT_RegisterMessage *rm; | 389 | struct GNUNET_NAT_RegisterMessage *rm; |
@@ -392,75 +394,75 @@ GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
392 | len = 0; | 394 | len = 0; |
393 | for (unsigned int i = 0; i < num_addrs; i++) | 395 | for (unsigned int i = 0; i < num_addrs; i++) |
394 | len += addrlens[i]; | 396 | len += addrlens[i]; |
395 | str_len = strlen(config_section) + 1; | 397 | str_len = strlen (config_section) + 1; |
396 | len += str_len; | 398 | len += str_len; |
397 | if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) || | 399 | if ((len > GNUNET_MAX_MESSAGE_SIZE - sizeof(*rm)) || |
398 | (num_addrs > UINT16_MAX)) | 400 | (num_addrs > UINT16_MAX)) |
399 | { | 401 | { |
400 | GNUNET_break(0); | 402 | GNUNET_break (0); |
401 | return NULL; | 403 | return NULL; |
402 | } | 404 | } |
403 | rm = GNUNET_malloc(sizeof(*rm) + len); | 405 | rm = GNUNET_malloc (sizeof(*rm) + len); |
404 | rm->header.size = htons(sizeof(*rm) + len); | 406 | rm->header.size = htons (sizeof(*rm) + len); |
405 | rm->header.type = htons(GNUNET_MESSAGE_TYPE_NAT_REGISTER); | 407 | rm->header.type = htons (GNUNET_MESSAGE_TYPE_NAT_REGISTER); |
406 | rm->flags = GNUNET_NAT_RF_NONE; | 408 | rm->flags = GNUNET_NAT_RF_NONE; |
407 | if (NULL != address_callback) | 409 | if (NULL != address_callback) |
408 | rm->flags |= GNUNET_NAT_RF_ADDRESSES; | 410 | rm->flags |= GNUNET_NAT_RF_ADDRESSES; |
409 | if (NULL != reversal_callback) | 411 | if (NULL != reversal_callback) |
410 | rm->flags |= GNUNET_NAT_RF_REVERSAL; | 412 | rm->flags |= GNUNET_NAT_RF_REVERSAL; |
411 | rm->proto = proto; | 413 | rm->proto = proto; |
412 | rm->str_len = htons(str_len); | 414 | rm->str_len = htons (str_len); |
413 | rm->num_addrs = htons((uint16_t)num_addrs); | 415 | rm->num_addrs = htons ((uint16_t) num_addrs); |
414 | off = (char *)&rm[1]; | 416 | off = (char *) &rm[1]; |
415 | for (unsigned int i = 0; i < num_addrs; i++) | 417 | for (unsigned int i = 0; i < num_addrs; i++) |
418 | { | ||
419 | switch (addrs[i]->sa_family) | ||
416 | { | 420 | { |
417 | switch (addrs[i]->sa_family) | 421 | case AF_INET: |
418 | { | 422 | if (sizeof(struct sockaddr_in) != addrlens[i]) |
419 | case AF_INET: | 423 | { |
420 | if (sizeof(struct sockaddr_in) != addrlens[i]) | 424 | GNUNET_break (0); |
421 | { | 425 | GNUNET_free (rm); |
422 | GNUNET_break(0); | 426 | return NULL; |
423 | GNUNET_free(rm); | 427 | } |
424 | return NULL; | 428 | break; |
425 | } | 429 | |
426 | break; | 430 | case AF_INET6: |
427 | 431 | if (sizeof(struct sockaddr_in6) != addrlens[i]) | |
428 | case AF_INET6: | 432 | { |
429 | if (sizeof(struct sockaddr_in6) != addrlens[i]) | 433 | GNUNET_break (0); |
430 | { | 434 | GNUNET_free (rm); |
431 | GNUNET_break(0); | 435 | return NULL; |
432 | GNUNET_free(rm); | 436 | } |
433 | return NULL; | 437 | break; |
434 | } | ||
435 | break; | ||
436 | 438 | ||
437 | #if AF_UNIX | 439 | #if AF_UNIX |
438 | case AF_UNIX: | 440 | case AF_UNIX: |
439 | if (sizeof(struct sockaddr_un) != addrlens[i]) | 441 | if (sizeof(struct sockaddr_un) != addrlens[i]) |
440 | { | 442 | { |
441 | GNUNET_break(0); | 443 | GNUNET_break (0); |
442 | GNUNET_free(rm); | 444 | GNUNET_free (rm); |
443 | return NULL; | 445 | return NULL; |
444 | } | 446 | } |
445 | break; | 447 | break; |
446 | #endif | 448 | #endif |
447 | default: | 449 | default: |
448 | GNUNET_break(0); | 450 | GNUNET_break (0); |
449 | GNUNET_free(rm); | 451 | GNUNET_free (rm); |
450 | return NULL; | 452 | return NULL; |
451 | } | ||
452 | GNUNET_memcpy(off, addrs[i], addrlens[i]); | ||
453 | off += addrlens[i]; | ||
454 | } | 453 | } |
455 | GNUNET_memcpy(off, config_section, str_len); | 454 | GNUNET_memcpy (off, addrs[i], addrlens[i]); |
455 | off += addrlens[i]; | ||
456 | } | ||
457 | GNUNET_memcpy (off, config_section, str_len); | ||
456 | 458 | ||
457 | nh = GNUNET_new(struct GNUNET_NAT_Handle); | 459 | nh = GNUNET_new (struct GNUNET_NAT_Handle); |
458 | nh->reg = &rm->header; | 460 | nh->reg = &rm->header; |
459 | nh->cfg = cfg; | 461 | nh->cfg = cfg; |
460 | nh->address_callback = address_callback; | 462 | nh->address_callback = address_callback; |
461 | nh->reversal_callback = reversal_callback; | 463 | nh->reversal_callback = reversal_callback; |
462 | nh->callback_cls = callback_cls; | 464 | nh->callback_cls = callback_cls; |
463 | do_connect(nh); | 465 | do_connect (nh); |
464 | return nh; | 466 | return nh; |
465 | } | 467 | } |
466 | 468 | ||
@@ -474,7 +476,7 @@ GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
474 | * #GNUNET_NO if the packet is invalid (not a stun packet) | 476 | * #GNUNET_NO if the packet is invalid (not a stun packet) |
475 | */ | 477 | */ |
476 | static int | 478 | static int |
477 | test_stun_packet(const void *data, size_t len) | 479 | test_stun_packet (const void *data, size_t len) |
478 | { | 480 | { |
479 | const struct stun_header *hdr; | 481 | const struct stun_header *hdr; |
480 | const struct stun_attr *attr; | 482 | const struct stun_attr *attr; |
@@ -486,70 +488,70 @@ test_stun_packet(const void *data, size_t len) | |||
486 | * while 'data' is advanced accordingly. | 488 | * while 'data' is advanced accordingly. |
487 | */ | 489 | */ |
488 | if (len < sizeof(struct stun_header)) | 490 | if (len < sizeof(struct stun_header)) |
489 | { | 491 | { |
490 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 492 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
491 | "STUN packet too short (only %d, wanting at least %d)\n", | 493 | "STUN packet too short (only %d, wanting at least %d)\n", |
492 | (int)len, | 494 | (int) len, |
493 | (int)sizeof(struct stun_header)); | 495 | (int) sizeof(struct stun_header)); |
494 | return GNUNET_NO; | 496 | return GNUNET_NO; |
495 | } | 497 | } |
496 | hdr = (const struct stun_header *)data; | 498 | hdr = (const struct stun_header *) data; |
497 | /* Skip header as it is already in hdr */ | 499 | /* Skip header as it is already in hdr */ |
498 | len -= sizeof(struct stun_header); | 500 | len -= sizeof(struct stun_header); |
499 | data += sizeof(struct stun_header); | 501 | data += sizeof(struct stun_header); |
500 | 502 | ||
501 | /* len as advertised in the message */ | 503 | /* len as advertised in the message */ |
502 | advertised_message_size = ntohs(hdr->msglen); | 504 | advertised_message_size = ntohs (hdr->msglen); |
503 | 505 | ||
504 | message_magic_cookie = ntohl(hdr->magic); | 506 | message_magic_cookie = ntohl (hdr->magic); |
505 | /* Compare if the cookie match */ | 507 | /* Compare if the cookie match */ |
506 | if (STUN_MAGIC_COOKIE != message_magic_cookie) | 508 | if (STUN_MAGIC_COOKIE != message_magic_cookie) |
507 | { | 509 | { |
508 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n"); | 510 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Invalid magic cookie for STUN\n"); |
509 | return GNUNET_NO; | 511 | return GNUNET_NO; |
510 | } | 512 | } |
511 | 513 | ||
512 | if (advertised_message_size > len) | 514 | if (advertised_message_size > len) |
515 | { | ||
516 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
517 | "Scrambled STUN packet length (got %d, expecting %d)\n", | ||
518 | advertised_message_size, | ||
519 | (int) len); | ||
520 | return GNUNET_NO; | ||
521 | } | ||
522 | len = advertised_message_size; | ||
523 | while (len > 0) | ||
524 | { | ||
525 | if (len < sizeof(struct stun_attr)) | ||
513 | { | 526 | { |
514 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 527 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
515 | "Scrambled STUN packet length (got %d, expecting %d)\n", | 528 | "Attribute too short in STUN packet (got %d, expecting %d)\n", |
516 | advertised_message_size, | 529 | (int) len, |
517 | (int)len); | 530 | (int) sizeof(struct stun_attr)); |
518 | return GNUNET_NO; | 531 | return GNUNET_NO; |
519 | } | 532 | } |
520 | len = advertised_message_size; | 533 | attr = (const struct stun_attr *) data; |
521 | while (len > 0) | 534 | |
535 | /* compute total attribute length */ | ||
536 | advertised_message_size = ntohs (attr->len) + sizeof(struct stun_attr); | ||
537 | |||
538 | /* Check if we still have space in our buffer */ | ||
539 | if (advertised_message_size > len) | ||
522 | { | 540 | { |
523 | if (len < sizeof(struct stun_attr)) | 541 | GNUNET_log ( |
524 | { | 542 | GNUNET_ERROR_TYPE_DEBUG, |
525 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 543 | "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", |
526 | "Attribute too short in STUN packet (got %d, expecting %d)\n", | 544 | advertised_message_size, |
527 | (int)len, | 545 | (int) len); |
528 | (int)sizeof(struct stun_attr)); | 546 | return GNUNET_NO; |
529 | return GNUNET_NO; | ||
530 | } | ||
531 | attr = (const struct stun_attr *)data; | ||
532 | |||
533 | /* compute total attribute length */ | ||
534 | advertised_message_size = ntohs(attr->len) + sizeof(struct stun_attr); | ||
535 | |||
536 | /* Check if we still have space in our buffer */ | ||
537 | if (advertised_message_size > len) | ||
538 | { | ||
539 | GNUNET_log( | ||
540 | GNUNET_ERROR_TYPE_DEBUG, | ||
541 | "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", | ||
542 | advertised_message_size, | ||
543 | (int)len); | ||
544 | return GNUNET_NO; | ||
545 | } | ||
546 | data += advertised_message_size; | ||
547 | len -= advertised_message_size; | ||
548 | } | 547 | } |
549 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 548 | data += advertised_message_size; |
550 | "STUN Packet, msg %04x, length: %d\n", | 549 | len -= advertised_message_size; |
551 | ntohs(hdr->msgtype), | 550 | } |
552 | advertised_message_size); | 551 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
552 | "STUN Packet, msg %04x, length: %d\n", | ||
553 | ntohs (hdr->msgtype), | ||
554 | advertised_message_size); | ||
553 | return GNUNET_OK; | 555 | return GNUNET_OK; |
554 | } | 556 | } |
555 | 557 | ||
@@ -578,30 +580,30 @@ test_stun_packet(const void *data, size_t len) | |||
578 | * #GNUNET_SYSERR on internal error handling the packet | 580 | * #GNUNET_SYSERR on internal error handling the packet |
579 | */ | 581 | */ |
580 | int | 582 | int |
581 | GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh, | 583 | GNUNET_NAT_stun_handle_packet (struct GNUNET_NAT_Handle *nh, |
582 | const struct sockaddr *sender_addr, | 584 | const struct sockaddr *sender_addr, |
583 | size_t sender_addr_len, | 585 | size_t sender_addr_len, |
584 | const void *data, | 586 | const void *data, |
585 | size_t data_size) | 587 | size_t data_size) |
586 | { | 588 | { |
587 | struct GNUNET_MQ_Envelope *env; | 589 | struct GNUNET_MQ_Envelope *env; |
588 | struct GNUNET_NAT_HandleStunMessage *hsn; | 590 | struct GNUNET_NAT_HandleStunMessage *hsn; |
589 | char *buf; | 591 | char *buf; |
590 | 592 | ||
591 | if (GNUNET_YES != test_stun_packet(data, data_size)) | 593 | if (GNUNET_YES != test_stun_packet (data, data_size)) |
592 | return GNUNET_NO; | 594 | return GNUNET_NO; |
593 | if (NULL == nh->mq) | 595 | if (NULL == nh->mq) |
594 | return GNUNET_SYSERR; | 596 | return GNUNET_SYSERR; |
595 | env = GNUNET_MQ_msg_extra(hsn, | 597 | env = GNUNET_MQ_msg_extra (hsn, |
596 | data_size + sender_addr_len, | 598 | data_size + sender_addr_len, |
597 | GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); | 599 | GNUNET_MESSAGE_TYPE_NAT_HANDLE_STUN); |
598 | hsn->sender_addr_size = htons((uint16_t)sender_addr_len); | 600 | hsn->sender_addr_size = htons ((uint16_t) sender_addr_len); |
599 | hsn->payload_size = htons((uint16_t)data_size); | 601 | hsn->payload_size = htons ((uint16_t) data_size); |
600 | buf = (char *)&hsn[1]; | 602 | buf = (char *) &hsn[1]; |
601 | GNUNET_memcpy(buf, sender_addr, sender_addr_len); | 603 | GNUNET_memcpy (buf, sender_addr, sender_addr_len); |
602 | buf += sender_addr_len; | 604 | buf += sender_addr_len; |
603 | GNUNET_memcpy(buf, data, data_size); | 605 | GNUNET_memcpy (buf, data, data_size); |
604 | GNUNET_MQ_send(nh->mq, env); | 606 | GNUNET_MQ_send (nh->mq, env); |
605 | return GNUNET_OK; | 607 | return GNUNET_OK; |
606 | } | 608 | } |
607 | 609 | ||
@@ -620,20 +622,20 @@ GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh, | |||
620 | * #GNUNET_SYSERR if the address is malformed | 622 | * #GNUNET_SYSERR if the address is malformed |
621 | */ | 623 | */ |
622 | int | 624 | int |
623 | GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh, | 625 | GNUNET_NAT_test_address (struct GNUNET_NAT_Handle *nh, |
624 | const void *addr, | 626 | const void *addr, |
625 | socklen_t addrlen) | 627 | socklen_t addrlen) |
626 | { | 628 | { |
627 | struct AddrEntry *ae; | 629 | struct AddrEntry *ae; |
628 | 630 | ||
629 | if ((addrlen != sizeof(struct sockaddr_in)) && | 631 | if ((addrlen != sizeof(struct sockaddr_in)) && |
630 | (addrlen != sizeof(struct sockaddr_in6))) | 632 | (addrlen != sizeof(struct sockaddr_in6))) |
631 | { | 633 | { |
632 | GNUNET_break(0); | 634 | GNUNET_break (0); |
633 | return GNUNET_SYSERR; | 635 | return GNUNET_SYSERR; |
634 | } | 636 | } |
635 | for (ae = nh->ae_head; NULL != ae; ae = ae->next) | 637 | for (ae = nh->ae_head; NULL != ae; ae = ae->next) |
636 | if ((addrlen == ae->addrlen) && (0 == memcmp(addr, &ae[1], addrlen))) | 638 | if ((addrlen == ae->addrlen) && (0 == memcmp (addr, &ae[1], addrlen))) |
637 | return GNUNET_YES; | 639 | return GNUNET_YES; |
638 | return GNUNET_NO; | 640 | return GNUNET_NO; |
639 | } | 641 | } |
@@ -652,9 +654,9 @@ GNUNET_NAT_test_address(struct GNUNET_NAT_Handle *nh, | |||
652 | * #GNUNET_OK otherwise (presumably in progress) | 654 | * #GNUNET_OK otherwise (presumably in progress) |
653 | */ | 655 | */ |
654 | int | 656 | int |
655 | GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, | 657 | GNUNET_NAT_request_reversal (struct GNUNET_NAT_Handle *nh, |
656 | const struct sockaddr_in *local_sa, | 658 | const struct sockaddr_in *local_sa, |
657 | const struct sockaddr_in *remote_sa) | 659 | const struct sockaddr_in *remote_sa) |
658 | { | 660 | { |
659 | struct GNUNET_MQ_Envelope *env; | 661 | struct GNUNET_MQ_Envelope *env; |
660 | struct GNUNET_NAT_RequestConnectionReversalMessage *req; | 662 | struct GNUNET_NAT_RequestConnectionReversalMessage *req; |
@@ -662,19 +664,19 @@ GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, | |||
662 | 664 | ||
663 | if (NULL == nh->mq) | 665 | if (NULL == nh->mq) |
664 | return GNUNET_SYSERR; | 666 | return GNUNET_SYSERR; |
665 | GNUNET_break(AF_INET == local_sa->sin_family); | 667 | GNUNET_break (AF_INET == local_sa->sin_family); |
666 | GNUNET_break(AF_INET == remote_sa->sin_family); | 668 | GNUNET_break (AF_INET == remote_sa->sin_family); |
667 | env = | 669 | env = |
668 | GNUNET_MQ_msg_extra(req, | 670 | GNUNET_MQ_msg_extra (req, |
669 | 2 * sizeof(struct sockaddr_in), | 671 | 2 * sizeof(struct sockaddr_in), |
670 | GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); | 672 | GNUNET_MESSAGE_TYPE_NAT_REQUEST_CONNECTION_REVERSAL); |
671 | req->local_addr_size = htons(sizeof(struct sockaddr_in)); | 673 | req->local_addr_size = htons (sizeof(struct sockaddr_in)); |
672 | req->remote_addr_size = htons(sizeof(struct sockaddr_in)); | 674 | req->remote_addr_size = htons (sizeof(struct sockaddr_in)); |
673 | buf = (char *)&req[1]; | 675 | buf = (char *) &req[1]; |
674 | GNUNET_memcpy(buf, local_sa, sizeof(struct sockaddr_in)); | 676 | GNUNET_memcpy (buf, local_sa, sizeof(struct sockaddr_in)); |
675 | buf += sizeof(struct sockaddr_in); | 677 | buf += sizeof(struct sockaddr_in); |
676 | GNUNET_memcpy(buf, remote_sa, sizeof(struct sockaddr_in)); | 678 | GNUNET_memcpy (buf, remote_sa, sizeof(struct sockaddr_in)); |
677 | GNUNET_MQ_send(nh->mq, env); | 679 | GNUNET_MQ_send (nh->mq, env); |
678 | return GNUNET_OK; | 680 | return GNUNET_OK; |
679 | } | 681 | } |
680 | 682 | ||
@@ -687,20 +689,20 @@ GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, | |||
687 | * @param nh the handle to stop | 689 | * @param nh the handle to stop |
688 | */ | 690 | */ |
689 | void | 691 | void |
690 | GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh) | 692 | GNUNET_NAT_unregister (struct GNUNET_NAT_Handle *nh) |
691 | { | 693 | { |
692 | if (NULL != nh->mq) | 694 | if (NULL != nh->mq) |
693 | { | 695 | { |
694 | GNUNET_MQ_destroy(nh->mq); | 696 | GNUNET_MQ_destroy (nh->mq); |
695 | nh->mq = NULL; | 697 | nh->mq = NULL; |
696 | } | 698 | } |
697 | if (NULL != nh->reconnect_task) | 699 | if (NULL != nh->reconnect_task) |
698 | { | 700 | { |
699 | GNUNET_SCHEDULER_cancel(nh->reconnect_task); | 701 | GNUNET_SCHEDULER_cancel (nh->reconnect_task); |
700 | nh->reconnect_task = NULL; | 702 | nh->reconnect_task = NULL; |
701 | } | 703 | } |
702 | GNUNET_free(nh->reg); | 704 | GNUNET_free (nh->reg); |
703 | GNUNET_free(nh); | 705 | GNUNET_free (nh); |
704 | } | 706 | } |
705 | 707 | ||
706 | 708 | ||