aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2016-10-26 04:20:48 +0000
committerBart Polot <bart@net.in.tum.de>2016-10-26 04:20:48 +0000
commit2f3bf5f8f2f80ead818e514ab2f69052cc201a78 (patch)
tree8f06a3b1f687f747dd3d5742a291afe32bb55864 /src
parent3968a7b989f996116e2ddb679482655e25aa20fd (diff)
downloadgnunet-2f3bf5f8f2f80ead818e514ab2f69052cc201a78.tar.gz
gnunet-2f3bf5f8f2f80ead818e514ab2f69052cc201a78.zip
- don't allocate message copies on the heap
Diffstat (limited to 'src')
-rw-r--r--src/cadet/gnunet-service-cadet_connection.c77
-rw-r--r--src/cadet/gnunet-service-cadet_connection.h3
2 files changed, 58 insertions, 22 deletions
diff --git a/src/cadet/gnunet-service-cadet_connection.c b/src/cadet/gnunet-service-cadet_connection.c
index 1b8bb1c3e..11f7fd9eb 100644
--- a/src/cadet/gnunet-service-cadet_connection.c
+++ b/src/cadet/gnunet-service-cadet_connection.c
@@ -3233,11 +3233,12 @@ GCC_is_direct (struct CadetConnection *c)
3233 3233
3234 3234
3235/** 3235/**
3236 * Internal implementation of the send function.
3237 *
3236 * Sends an already built message on a connection, properly registering 3238 * Sends an already built message on a connection, properly registering
3237 * all used resources. 3239 * all used resources.
3238 * 3240 *
3239 * @param message Message to send. Function makes a copy of it. 3241 * @param message Modificable copy of the message to send.
3240 * If message is not hop-by-hop, decrements TTL of copy.
3241 * @param payload_type Type of payload, in case the message is encrypted. 3242 * @param payload_type Type of payload, in case the message is encrypted.
3242 * @param payload_id ID of the payload (PID, ACK, ...). 3243 * @param payload_id ID of the payload (PID, ACK, ...).
3243 * @param c Connection on which this message is transmitted. 3244 * @param c Connection on which this message is transmitted.
@@ -3250,11 +3251,11 @@ GCC_is_direct (struct CadetConnection *c)
3250 * NULL on error or if @c cont is NULL. 3251 * NULL on error or if @c cont is NULL.
3251 * Invalid on @c cont call. 3252 * Invalid on @c cont call.
3252 */ 3253 */
3253struct CadetConnectionQueue * 3254static struct CadetConnectionQueue *
3254GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message, 3255send_prebuilt_message (struct GNUNET_MessageHeader *message,
3255 uint16_t payload_type, uint32_t payload_id, 3256 uint16_t payload_type, uint32_t payload_id,
3256 struct CadetConnection *c, int fwd, int force, 3257 struct CadetConnection *c, int fwd, int force,
3257 GCC_sent cont, void *cont_cls) 3258 GCC_sent cont, void *cont_cls)
3258{ 3259{
3259 struct GNUNET_CADET_AX *axmsg; 3260 struct GNUNET_CADET_AX *axmsg;
3260 struct GNUNET_CADET_KX *kmsg; 3261 struct GNUNET_CADET_KX *kmsg;
@@ -3264,7 +3265,6 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3264 struct GNUNET_CADET_ConnectionBroken *bmsg; 3265 struct GNUNET_CADET_ConnectionBroken *bmsg;
3265 struct CadetFlowControl *fc; 3266 struct CadetFlowControl *fc;
3266 struct CadetConnectionQueue *q; 3267 struct CadetConnectionQueue *q;
3267 struct GNUNET_MessageHeader *copy;
3268 size_t size; 3268 size_t size;
3269 uint16_t type; 3269 uint16_t type;
3270 int droppable; 3270 int droppable;
@@ -3278,8 +3278,6 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3278 } 3278 }
3279 3279
3280 size = ntohs (message->size); 3280 size = ntohs (message->size);
3281 copy = GNUNET_malloc (size);
3282 GNUNET_memcpy (copy, message, size);
3283 type = ntohs (message->type); 3281 type = ntohs (message->type);
3284 LOG (GNUNET_ERROR_TYPE_INFO, 3282 LOG (GNUNET_ERROR_TYPE_INFO,
3285 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n", 3283 "--> %s (%s %4u) on conn %s (%p) %s [%5u]\n",
@@ -3289,7 +3287,7 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3289 switch (type) 3287 switch (type)
3290 { 3288 {
3291 case GNUNET_MESSAGE_TYPE_CADET_AX: 3289 case GNUNET_MESSAGE_TYPE_CADET_AX:
3292 axmsg = (struct GNUNET_CADET_AX *) copy; 3290 axmsg = (struct GNUNET_CADET_AX *) message;
3293 axmsg->cid = c->id; 3291 axmsg->cid = c->id;
3294 axmsg->pid = htonl (GCC_get_pid (c, fwd)); 3292 axmsg->pid = htonl (GCC_get_pid (c, fwd));
3295 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n); 3293 LOG (GNUNET_ERROR_TYPE_DEBUG, " Q_N+ %p %u\n", fc, fc->queue_n);
@@ -3306,33 +3304,33 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3306 break; 3304 break;
3307 3305
3308 case GNUNET_MESSAGE_TYPE_CADET_KX: 3306 case GNUNET_MESSAGE_TYPE_CADET_KX:
3309 kmsg = (struct GNUNET_CADET_KX *) copy; 3307 kmsg = (struct GNUNET_CADET_KX *) message;
3310 kmsg->reserved = htonl (0); 3308 kmsg->reserved = htonl (0);
3311 kmsg->cid = c->id; 3309 kmsg->cid = c->id;
3312 break; 3310 break;
3313 3311
3314 case GNUNET_MESSAGE_TYPE_CADET_ACK: 3312 case GNUNET_MESSAGE_TYPE_CADET_ACK:
3315 amsg = (struct GNUNET_CADET_ACK *) copy; 3313 amsg = (struct GNUNET_CADET_ACK *) message;
3316 amsg->cid = c->id; 3314 amsg->cid = c->id;
3317 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack)); 3315 LOG (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ntohl (amsg->ack));
3318 droppable = GNUNET_NO; 3316 droppable = GNUNET_NO;
3319 break; 3317 break;
3320 3318
3321 case GNUNET_MESSAGE_TYPE_CADET_POLL: 3319 case GNUNET_MESSAGE_TYPE_CADET_POLL:
3322 pmsg = (struct GNUNET_CADET_Poll *) copy; 3320 pmsg = (struct GNUNET_CADET_Poll *) message;
3323 pmsg->cid = c->id; 3321 pmsg->cid = c->id;
3324 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid)); 3322 LOG (GNUNET_ERROR_TYPE_DEBUG, " POLL %u\n", ntohl (pmsg->pid));
3325 droppable = GNUNET_NO; 3323 droppable = GNUNET_NO;
3326 break; 3324 break;
3327 3325
3328 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY: 3326 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_DESTROY:
3329 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) copy; 3327 dmsg = (struct GNUNET_CADET_ConnectionDestroy *) message;
3330 dmsg->reserved = htonl (0); 3328 dmsg->reserved = htonl (0);
3331 dmsg->cid = c->id; 3329 dmsg->cid = c->id;
3332 break; 3330 break;
3333 3331
3334 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN: 3332 case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_BROKEN:
3335 bmsg = (struct GNUNET_CADET_ConnectionBroken *) copy; 3333 bmsg = (struct GNUNET_CADET_ConnectionBroken *) message;
3336 bmsg->reserved = htonl (0); 3334 bmsg->reserved = htonl (0);
3337 bmsg->cid = c->id; 3335 bmsg->cid = c->id;
3338 break; 3336 break;
@@ -3344,7 +3342,6 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3344 3342
3345 default: 3343 default:
3346 GNUNET_break (0); 3344 GNUNET_break (0);
3347 GNUNET_free (copy);
3348 return NULL; 3345 return NULL;
3349 } 3346 }
3350 3347
@@ -3359,7 +3356,6 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3359 { 3356 {
3360 fc->queue_n--; 3357 fc->queue_n--;
3361 } 3358 }
3362 GNUNET_free (copy);
3363 return NULL; /* Drop this message */ 3359 return NULL; /* Drop this message */
3364 } 3360 }
3365 3361
@@ -3369,14 +3365,13 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3369 3365
3370 q = GNUNET_new (struct CadetConnectionQueue); 3366 q = GNUNET_new (struct CadetConnectionQueue);
3371 q->forced = !droppable; 3367 q->forced = !droppable;
3372 q->peer_q = GCP_send (get_hop (c, fwd), copy, 3368 q->peer_q = GCP_send (get_hop (c, fwd), message,
3373 payload_type, payload_id, 3369 payload_type, payload_id,
3374 c, fwd, 3370 c, fwd,
3375 &conn_message_sent, q); 3371 &conn_message_sent, q);
3376 if (NULL == q->peer_q) 3372 if (NULL == q->peer_q)
3377 { 3373 {
3378 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c)); 3374 LOG (GNUNET_ERROR_TYPE_DEBUG, "dropping msg on %s, NULL q\n", GCC_2s (c));
3379 GNUNET_free (copy);
3380 GNUNET_free (q); 3375 GNUNET_free (q);
3381 GCC_check_connections (); 3376 GCC_check_connections ();
3382 return NULL; 3377 return NULL;
@@ -3390,6 +3385,48 @@ GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3390 3385
3391 3386
3392/** 3387/**
3388 * Sends an already built message on a connection, properly registering
3389 * all used resources.
3390 *
3391 * @param message Message to send.
3392 * @param payload_type Type of payload, in case the message is encrypted.
3393 * @param payload_id ID of the payload (PID, ACK, ...).
3394 * @param c Connection on which this message is transmitted.
3395 * @param fwd Is this a fwd message?
3396 * @param force Force the connection to accept the message (buffer overfill).
3397 * @param cont Continuation called once message is sent. Can be NULL.
3398 * @param cont_cls Closure for @c cont.
3399 *
3400 * @return Handle to cancel the message before it's sent.
3401 * NULL on error or if @c cont is NULL.
3402 * Invalid on @c cont call.
3403 */
3404struct CadetConnectionQueue *
3405GCC_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
3406 uint16_t payload_type, uint32_t payload_id,
3407 struct CadetConnection *c, int fwd, int force,
3408 GCC_sent cont, void *cont_cls)
3409{
3410 uint16_t size;
3411
3412 /* Allocate a copy of the message on the stack, so we can modify it as needed,
3413 * adding the Connection ID.
3414 */
3415 size = ntohs (message->size);
3416 {
3417 struct GNUNET_MessageHeader *copy;
3418 unsigned char cbuf[size];
3419
3420 copy = (struct GNUNET_MessageHeader *)cbuf;
3421 GNUNET_memcpy (copy, message, size);
3422 return send_prebuilt_message (copy, payload_type, payload_id,
3423 c, fwd, force,
3424 cont, cont_cls);
3425 }
3426}
3427
3428
3429/**
3393 * Cancel a previously sent message while it's in the queue. 3430 * Cancel a previously sent message while it's in the queue.
3394 * 3431 *
3395 * ONLY can be called before the continuation given to the send function 3432 * ONLY can be called before the continuation given to the send function
diff --git a/src/cadet/gnunet-service-cadet_connection.h b/src/cadet/gnunet-service-cadet_connection.h
index ac2a1492f..7c41a28bf 100644
--- a/src/cadet/gnunet-service-cadet_connection.h
+++ b/src/cadet/gnunet-service-cadet_connection.h
@@ -491,8 +491,7 @@ GCC_cancel (struct CadetConnectionQueue *q);
491 * Sends an already built message on a connection, properly registering 491 * Sends an already built message on a connection, properly registering
492 * all used resources. 492 * all used resources.
493 * 493 *
494 * @param message Message to send. Function makes a copy of it. 494 * @param message Message to send.
495 * If message is not hop-by-hop, decrements TTL of copy.
496 * @param payload_type Type of payload, in case the message is encrypted. 495 * @param payload_type Type of payload, in case the message is encrypted.
497 * @param payload_id ID of the payload (PID, ACK, ...). 496 * @param payload_id ID of the payload (PID, ACK, ...).
498 * @param c Connection on which this message is transmitted. 497 * @param c Connection on which this message is transmitted.