*/
enum SessionState
{
+ CLIENT_REQUEST_RECEIVED,
WAITING_FOR_BOBS_CONNECT,
- MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED,
- WAITING_FOR_RESPONSE_FROM_SERVICE,
- REQUEST_FROM_SERVICE_RECEIVED,
+ CLIENT_RESPONSE_RECEIVED,
+ WAITING_FOR_SERVICE_REQUEST,
+ WAITING_FOR_SERVICE_RESPONSE,
+ SERVICE_REQUEST_RECEIVED,
+ SERVICE_RESPONSE_RECEIVED,
FINALIZED
};
*/
struct GNUNET_SERVER_Client * client;
+ /**
+ * The message to send
+ */
+ struct GNUNET_MessageHeader * msg;
+
/**
* how many elements we were supplied with from the client
*/
- uint16_t element_count;
+ uint32_t element_count;
/**
* how many elements actually are used after applying the mask
*/
- uint16_t used_element_count;
+ uint32_t used_element_count;
/**
* how many bytes the mask is long.
* just for convenience so we don't have to re-re-re calculate it each time
*/
- uint16_t mask_length;
+ uint32_t mask_length;
/**
* all the vector elements we received
* tunnel-handle associated with our mesh handle
*/
struct GNUNET_MESH_Tunnel * tunnel;
+
+ GNUNET_SCHEDULER_TaskIdentifier client_notification_task;
+
+ GNUNET_SCHEDULER_TaskIdentifier service_request_task;
};
-/**
- * We need to do a minimum of bookkeeping to maintain track of our transmit handles.
- * each msg is associated with a session and handle. using this information we can determine which msg was sent.
- */
-struct MessageObject
-{
- /**
- * The handle used to transmit with this request
- */
- void ** transmit_handle;
-
- /**
- * The message to send
- */
- struct GNUNET_MessageHeader * msg;
-};
-
-/**
- * Linked list for all queued tasks.
- */
-struct TaskClosure
-{
-
- /**
- * point to the next element of the list
- */
- struct TaskClosure *next;
-
- /**
- * point to the previous element of the list
- */
- struct TaskClosure *prev;
-
- /**
- * the handle associated with this task, canceled upon shutdown
- */
- GNUNET_SCHEDULER_TaskIdentifier my_handle;
-
- /**
- * the closure, as it's always a session-struct, we don't cast this to void
- */
- struct ServiceSession * my_session;
-};
///////////////////////////////////////////////////////////////////////////////
// Global Variables
///////////////////////////////////////////////////////////////////////////////
/**
* Service's own public key represented as string
*/
-static uint16_t my_pubkey_external_length = 0;
+static uint32_t my_pubkey_external_length = 0;
/**
* Service's own n
*/
static int do_shutdown;
-/**
- * DLL for all out active, queued tasks. Purged upon disconnect
- */
-static struct TaskClosure * tasklist_head;
-/**
- * DLL for all out active, queued tasks. Purged upon disconnect
- */
-static struct TaskClosure * tasklist_tail;
-
///////////////////////////////////////////////////////////////////////////////
// Helper Functions
///////////////////////////////////////////////////////////////////////////////
* @return an MPI value containing the calculated sum, never NULL
*/
static gcry_mpi_t
-compute_square_sum (gcry_mpi_t * vector, uint16_t length)
+compute_square_sum (gcry_mpi_t * vector, uint32_t length)
{
gcry_mpi_t elem;
gcry_mpi_t sum;
static size_t
do_send_message (void *cls, size_t size, void *buf)
{
- struct MessageObject * info = cls;
- struct GNUNET_MessageHeader * msg;
+ struct ServiceSession * session = cls;
size_t written = 0;
- GNUNET_assert (info);
- msg = info->msg;
- GNUNET_assert (msg);
GNUNET_assert (buf);
- if (ntohs (msg->size) == size)
+ if (ntohs (session->msg->size) == size)
{
- memcpy (buf, msg, size);
+ memcpy (buf, session->msg, size);
written = size;
}
- // reset the transmit handle, if necessary
- if (info->transmit_handle)
- *info->transmit_handle = NULL;
-
+ switch (ntohs(session->msg->type)){
+ case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT:
+ session->state = FINALIZED;
+ session->client_transmit_handle = NULL;
+ break;
+ default:
+ session->service_transmit_handle = NULL;
+ }
+
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Sent a message of type %hu.\n",
- ntohs (msg->type));
- GNUNET_free(msg);
- GNUNET_free(info);
+ ntohs (session->msg->type));
+ GNUNET_free(session->msg);
+ session->msg = NULL;
+
return written;
}
* @return the initialized vector, never NULL
*/
static gcry_mpi_t *
-initialize_mpi_vector (uint16_t length)
+initialize_mpi_vector (uint32_t length)
{
uint32_t i;
gcry_mpi_t * output = GNUNET_malloc (sizeof (gcry_mpi_t) * length);
* @return an array of MPI values with random values
*/
static gcry_mpi_t *
-generate_random_vector (uint16_t length)
+generate_random_vector (uint32_t length)
{
gcry_mpi_t * random_vector;
int32_t value;
static struct ServiceSession *
find_matching_session (struct ServiceSession * tail,
const struct GNUNET_HashCode * key,
- uint16_t element_count,
+ uint32_t element_count,
enum SessionState * state,
const struct GNUNET_PeerIdentity * peerid)
{
{
unsigned int i;
- if (FINALIZED != session->state)
- {
- if (session->a)
- {
- for (i = 0; i < session->used_element_count; i++)
- gcry_mpi_release (session->a[i]);
+ if (session->a)
+ {
+ for (i = 0; i < session->used_element_count; i++)
+ gcry_mpi_release (session->a[i]);
- GNUNET_free (session->a);
- }
- if (session->product)
- gcry_mpi_release (session->product);
+ GNUNET_free (session->a);
+ }
+ if (session->product)
+ gcry_mpi_release (session->product);
- if (session->remote_pubkey)
- gcry_sexp_release (session->remote_pubkey);
+ if (session->remote_pubkey)
+ gcry_sexp_release (session->remote_pubkey);
- GNUNET_free_non_null (session->vector);
- }
-#if FIXME_FUCHS
- if (GNUNET_SCHEDULER_NO_TASK != session->prepare_response_task)
- {
- GNUNET_SCHEDULER_cancel (session->prepare_response_task);
- session->prepare_response_task = GNUNET_SCHEDULER_NO_TASK;
- }
- if (NULL != session->client_transmit_handle)
- {
- GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle);
- session->client_transmit_handle = NULL;
- }
-#endif
+ GNUNET_free_non_null (session->vector);
GNUNET_free (session);
}
///////////////////////////////////////////////////////////////////////////////
handle_client_disconnect (void *cls,
struct GNUNET_SERVER_Client *client)
{
- struct ServiceSession *elem;
-
- elem = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
- if (NULL == elem)
+ struct ServiceSession *session;
+
+ session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
+ if (NULL == session)
return;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_ ("Client (%p) disconnected from us.\n"), client);
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, elem);
+ GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
- if (!(elem->role == BOB && elem->state == FINALIZED))
+ if (!(session->role == BOB && session->state == FINALIZED))
{
//we MUST terminate any client message underway
- if (elem->service_transmit_handle && elem->tunnel)
- GNUNET_MESH_notify_transmit_ready_cancel (elem->service_transmit_handle);
- if (elem->tunnel && elem->state == WAITING_FOR_RESPONSE_FROM_SERVICE)
- GNUNET_MESH_tunnel_destroy (elem->tunnel);
+ if (session->service_transmit_handle && session->tunnel)
+ GNUNET_MESH_notify_transmit_ready_cancel (session->service_transmit_handle);
+ if (session->tunnel && session->state == WAITING_FOR_SERVICE_RESPONSE)
+ GNUNET_MESH_tunnel_destroy (session->tunnel);
}
- free_session (elem);
+ if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
+ {
+ GNUNET_SCHEDULER_cancel (session->client_notification_task);
+ session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
+ {
+ GNUNET_SCHEDULER_cancel (session->service_request_task);
+ session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL != session->client_transmit_handle)
+ {
+ GNUNET_SERVER_notify_transmit_ready_cancel (session->client_transmit_handle);
+ session->client_transmit_handle = NULL;
+ }
+ free_session (session);
}
prepare_client_end_notification (void * cls,
const struct GNUNET_SCHEDULER_TaskContext * tc)
{
- struct TaskClosure * task = cls;
- struct ServiceSession * session = task->my_session;
+ struct ServiceSession * session = cls;
struct GNUNET_SCALARPRODUCT_client_response * msg;
- struct MessageObject * msg_obj;
- GNUNET_CONTAINER_DLL_remove (tasklist_head, tasklist_tail, task);
- GNUNET_free(task);
+ session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
msg = GNUNET_new (struct GNUNET_SCALARPRODUCT_client_response);
msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
// 0 size and the first char in the product is 0, which should never be zero if encoding is used.
msg->product_length = htonl (0);
msg->range = 1;
-
- msg_obj = GNUNET_new (struct MessageObject);
- msg_obj->msg = &msg->header;
- msg_obj->transmit_handle = NULL; // do not reset the transmit handle, please
+
+ session->msg = &msg->header;
//transmit this message to our client
session->client_transmit_handle =
sizeof (struct GNUNET_SCALARPRODUCT_client_response),
GNUNET_TIME_UNIT_FOREVER_REL,
&do_send_message,
- msg_obj);
-
+ session);
// if we could not even queue our request, something is wrong
- if ( ! session->client_transmit_handle)
+ if ( NULL == session->client_transmit_handle)
{
-
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)! This is OK if it was disconnected beforehand already.\n"), session->client);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Could not send message to client (%p)!\n"), session->client);
// usually gets freed by do_send_message
- GNUNET_free (msg_obj);
+ session->msg = NULL;
GNUNET_free (msg);
}
else
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Sending session-end notification to client (%p) for session %s\n"), &session->client, GNUNET_h2s (&session->key));
- free_session(session);
}
struct ServiceSession * response)
{
struct GNUNET_SCALARPRODUCT_service_response * msg;
- uint16_t msg_length = 0;
+ uint32_t msg_length = 0;
unsigned char * current = NULL;
unsigned char * element_exported = NULL;
size_t element_length = 0;
msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_BOB_TO_ALICE);
msg->header.size = htons (msg_length);
- msg->element_count = htons (request->element_count);
- msg->used_element_count = htons (request->used_element_count);
+ msg->element_count = htonl (request->element_count);
+ msg->used_element_count = htonl (request->used_element_count);
memcpy (&msg->key, &request->key, sizeof (struct GNUNET_HashCode));
current = (unsigned char *) &msg[1];
if (GNUNET_SERVER_MAX_MESSAGE_SIZE >= msg_length)
{
- struct MessageObject * msg_obj;
-
- msg_obj = GNUNET_new (struct MessageObject);
- msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
- msg_obj->transmit_handle = (void *) &request->service_transmit_handle; //and reset the transmit handle
+ request->msg = (struct GNUNET_MessageHeader *) msg;
request->service_transmit_handle =
GNUNET_MESH_notify_transmit_ready (request->tunnel,
GNUNET_YES,
GNUNET_TIME_UNIT_FOREVER_REL,
msg_length,
&do_send_message,
- msg_obj);
+ request);
// we don't care if it could be send or not. either way, the session is over for us.
request->state = FINALIZED;
- response->state = FINALIZED;
}
else
- {
- // TODO FEATURE: fallback to fragmentation, in case the message is too long
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!)\n"));
- }
//disconnect our client
- if ( ! request->service_transmit_handle)
+ if ( NULL == request->service_transmit_handle)
{
- struct TaskClosure * task = GNUNET_new(struct TaskClosure);
-
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Could not send service-response message via mesh!)\n"));
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, response);
- task->my_session = response;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+
+ response->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ response);
return GNUNET_NO;
}
return GNUNET_OK;
int ret = GNUNET_SYSERR;
unsigned int * p;
unsigned int * q;
- uint16_t count;
+ uint32_t count;
gcry_mpi_t * rand = NULL;
gcry_mpi_t * r = NULL;
gcry_mpi_t * r_prime = NULL;
prepare_service_request (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct TaskClosure * task = cls;
- struct ServiceSession * session = task->my_session;
+ struct ServiceSession * session = cls;
unsigned char * current;
struct GNUNET_SCALARPRODUCT_service_request * msg;
- struct MessageObject * msg_obj;
unsigned int i;
unsigned int j;
- uint16_t msg_length;
- size_t element_length = 0; //gets initialized by gcry_mpi_print, but the compiler doesn't know that
+ uint32_t msg_length;
+ size_t element_length = 0; // initialized by gcry_mpi_print, but the compiler doesn't know that
gcry_mpi_t a;
uint32_t value;
- GNUNET_CONTAINER_DLL_remove (tasklist_head, tasklist_tail, task);
- GNUNET_free(task);
+ session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _ ("Successfully created new tunnel to peer (%s)!\n"), GNUNET_i2s (&session->peer));
+ session->mask_length
+ my_pubkey_external_length)
{
- struct TaskClosure * task = GNUNET_new(struct TaskClosure);
- // TODO FEATURE: fallback to fragmentation, in case the message is too long
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Message too large, fragmentation is currently not supported!\n"));
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
-
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+ session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ session);
return;
}
+
msg = GNUNET_malloc (msg_length);
-
msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_ALICE_TO_BOB);
memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
- msg->mask_length = htons (session->mask_length);
- msg->pk_length = htons (my_pubkey_external_length);
- msg->used_element_count = htons (session->used_element_count);
- msg->element_count = htons (session->element_count);
+ msg->mask_length = htonl (session->mask_length);
+ msg->pk_length = htonl (my_pubkey_external_length);
+ msg->used_element_count = htonl (session->used_element_count);
+ msg->element_count = htonl (session->element_count);
msg->header.size = htons (msg_length);
// fill in the payload
}
gcry_mpi_release (a);
- msg_obj = GNUNET_new (struct MessageObject);
- msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
- msg_obj->transmit_handle = (void *) &session->service_transmit_handle; //and reset the transmit handle
+ session->msg = (struct GNUNET_MessageHeader *) msg;
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Transmitting service request.\n"));
//transmit via mesh messaging
- session->state = WAITING_FOR_RESPONSE_FROM_SERVICE;
session->service_transmit_handle = GNUNET_MESH_notify_transmit_ready (session->tunnel, GNUNET_YES,
GNUNET_TIME_UNIT_FOREVER_REL,
msg_length,
&do_send_message,
- msg_obj);
+ session);
if ( ! session->service_transmit_handle)
{
- struct TaskClosure * task = GNUNET_new(struct TaskClosure);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not send mutlicast message to tunnel!\n"));
- GNUNET_free (msg_obj);
GNUNET_free (msg);
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
-
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+ session->msg = NULL;
+ session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ session);
+ return;
}
+ session->state = WAITING_FOR_SERVICE_RESPONSE;
}
/**
const struct GNUNET_MessageHeader *message)
{
const struct GNUNET_SCALARPRODUCT_client_request * msg = (const struct GNUNET_SCALARPRODUCT_client_request *) message;
- struct TaskClosure * task;
struct ServiceSession * session;
- uint16_t element_count;
- uint16_t mask_length;
- uint16_t msg_type;
+ uint32_t element_count;
+ uint32_t mask_length;
+ uint32_t msg_type;
int32_t * vector;
uint32_t i;
- GNUNET_assert (message);
+ // only one concurrent session per client connection allowed, simplifies logics a lot...
+ session = GNUNET_SERVER_client_get_user_context (client, struct ServiceSession);
+ if ((NULL != session) && (session->state != FINALIZED)){
+ GNUNET_SERVER_receive_done (client, GNUNET_OK);
+ return;
+ }
+ else if(NULL != session){
+ // old session is already completed, clean it up
+ GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
+ free_session(session);
+ }
//we need at least a peer and one message id to compare
if (sizeof (struct GNUNET_SCALARPRODUCT_client_request) > ntohs (msg->header.size))
}
msg_type = ntohs (msg->header.type);
- element_count = ntohs (msg->element_count);
- mask_length = ntohs (msg->mask_length);
+ element_count = ntohl (msg->element_count);
+ mask_length = ntohl (msg->mask_length);
//sanity check: is the message as long as the message_count fields suggests?
if (( ntohs (msg->header.size) != (sizeof (struct GNUNET_SCALARPRODUCT_client_request) + element_count * sizeof (int32_t) + mask_length))
}
session = GNUNET_new (struct ServiceSession);
- GNUNET_SERVER_client_set_user_context (client, session);
+ session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
+ session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
session->client = client;
session->element_count = element_count;
session->mask_length = mask_length;
session->used_element_count++;
}
- if ( ! session->used_element_count)
+ if ( 0 == session->used_element_count)
{
GNUNET_break_op (0);
GNUNET_free (session->vector);
- GNUNET_free (session->a);
GNUNET_free (session);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
{
GNUNET_break (0);
GNUNET_free (session->vector);
- GNUNET_free (session->a);
GNUNET_free (session);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
_ ("Creating new tunnel to for session with key %s.\n"),
GNUNET_h2s (&session->key));
- GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
session->tunnel = GNUNET_MESH_tunnel_create (my_mesh, session,
&session->peer,
GNUNET_APPLICATION_TYPE_SCALARPRODUCT,
{
GNUNET_break (0);
GNUNET_free (session->vector);
- GNUNET_free (session->a);
GNUNET_free (session);
GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
return;
}
- session->state = WAITING_FOR_BOBS_CONNECT;
+ GNUNET_SERVER_client_set_user_context (client, session);
+ GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
- task = GNUNET_new(struct TaskClosure);
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_service_request, task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+ session->state = CLIENT_REQUEST_RECEIVED;
+ session->service_request_task =
+ GNUNET_SCHEDULER_add_now (&prepare_service_request,
+ session);
- GNUNET_SERVER_receive_done (client, GNUNET_YES);
}
else
{
struct ServiceSession * requesting_session;
- enum SessionState needed_state = REQUEST_FROM_SERVICE_RECEIVED;
-
+ enum SessionState needed_state = SERVICE_REQUEST_RECEIVED;
+
session->role = BOB;
session->mask = NULL;
// copy over the elements
session->used_element_count = element_count;
for (i = 0; i < element_count; i++)
session->vector[i] = ntohl (vector[i]);
- session->state = MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED;
+ session->state = CLIENT_RESPONSE_RECEIVED;
+ GNUNET_SERVER_client_set_user_context (client, session);
GNUNET_CONTAINER_DLL_insert (from_client_head, from_client_tail, session);
- GNUNET_SERVER_receive_done (client, GNUNET_YES);
+
//check if service queue contains a matching request
requesting_session = find_matching_session (from_service_tail,
&session->key,
{
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s and a matching service-request-session set, processing.\n"), GNUNET_h2s (&session->key));
if (GNUNET_OK != compute_service_response (requesting_session, session))
- {
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
+ session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ session);
- task = GNUNET_new(struct TaskClosure);
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification, task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
- }
}
- else
+ else{
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got client-responder-session with key %s but NO matching service-request-session set, queuing element for later use.\n"), GNUNET_h2s (&session->key));
// no matching session exists yet, store the response
// for later processing by handle_service_request()
+ }
}
+ GNUNET_SERVER_receive_done (client, GNUNET_YES);
}
c->peer = *initiator;
c->tunnel = tunnel;
c->role = BOB;
+ c->state = WAITING_FOR_SERVICE_REQUEST;
return c;
}
struct ServiceSession * session = tunnel_ctx;
struct ServiceSession * client_session;
struct ServiceSession * curr;
- struct TaskClosure * task;
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Peer disconnected, terminating session %s with peer (%s)\n"),
if (ALICE == session->role) {
// as we have only one peer connected in each session, just remove the session
- if ((FINALIZED != session->state) && (!do_shutdown))
+ if ((SERVICE_RESPONSE_RECEIVED > session->state) && (!do_shutdown))
{
- for (curr = from_client_head; NULL != curr; curr = curr->next)
- if (curr == session)
- {
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
- break;
- }
session->tunnel = NULL;
// if this happened before we received the answer, we must terminate the session
- task = GNUNET_new(struct TaskClosure);
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+ session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ session);
}
}
- else { //(BOB == session->role)
-
+ else { //(BOB == session->role) service session
+
// remove the session, unless it has already been dequeued, but somehow still active
// this could bug without the IF in case the queue is empty and the service session was the only one know to the service
+ // scenario: disconnect before alice can send her message to bob.
for (curr = from_service_head; NULL != curr; curr = curr->next)
if (curr == session)
{
// or if it was a responder, no point in adding more statefulness
if (client_session && (!do_shutdown))
{
- // remove the session, we just found it in the queue, so it must be there
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, client_session);
-
- task = GNUNET_new(struct TaskClosure);
- task->my_session = client_session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
+ client_session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ client_session);
}
}
}
compute_scalar_product (struct ServiceSession * session,
gcry_mpi_t * r, gcry_mpi_t * r_prime, gcry_mpi_t s, gcry_mpi_t s_prime)
{
- uint16_t count;
+ uint32_t count;
gcry_mpi_t t;
gcry_mpi_t u;
gcry_mpi_t utick;
prepare_client_response (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- struct TaskClosure * task = cls;
- struct ServiceSession * session = task->my_session;
+ struct ServiceSession * session = cls;
struct GNUNET_SCALARPRODUCT_client_response * msg;
unsigned char * product_exported = NULL;
size_t product_length = 0;
- uint16_t msg_length = 0;
- struct MessageObject * msg_obj;
+ uint32_t msg_length = 0;
int8_t range = -1;
gcry_error_t rc;
int sign;
- GNUNET_CONTAINER_DLL_remove (tasklist_head, tasklist_tail, task);
- GNUNET_free(task);
+ session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
if (session->product)
{
sign = gcry_mpi_cmp_ui(session->product, 0);
// libgcrypt can not handle a print of a negative number
+ // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
if (0 > sign){
gcry_mpi_sub(value, value, session->product);
}
else
range = 0;
+ gcry_mpi_release (session->product);
+ session->product = NULL;
+
// get representation as string
- // unfortunately libgcrypt is too stupid to implement print-support in
- // signed GCRYMPI_FMT_STD format, and simply asserts in that case.
- // here is the associated sourcecode:
- // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it yet. */
if (range
&& (0 != (rc = gcry_mpi_aprint (GCRYMPI_FMT_USG,
&product_exported,
&product_length,
- session->product)))){
+ value)))){
LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
product_length = 0;
range = -1; // signal error with product-length = 0 and range = -1
}
-
- gcry_mpi_release (session->product);
- session->product = NULL;
+ gcry_mpi_release (value);
}
msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) + product_length;
msg = GNUNET_malloc (msg_length);
- memcpy (&msg[1], product_exported, product_length);
- GNUNET_free_non_null (product_exported);
+ memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
+ memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
+ if (product_exported != NULL){
+ memcpy (&msg[1], product_exported, product_length);
+ GNUNET_free(product_exported);
+ }
msg->header.type = htons (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT);
msg->header.size = htons (msg_length);
msg->range = range;
- memcpy (&msg->key, &session->key, sizeof (struct GNUNET_HashCode));
- memcpy (&msg->peer, &session->peer, sizeof ( struct GNUNET_PeerIdentity));
msg->product_length = htonl (product_length);
- msg_obj = GNUNET_new (struct MessageObject);
- msg_obj->msg = (struct GNUNET_MessageHeader *) msg;
- msg_obj->transmit_handle = NULL; // don't reset the transmit handle
-
+ session->msg = (struct GNUNET_MessageHeader *) msg;
//transmit this message to our client
- session->client_transmit_handle = // FIXME: use after free possibility during shutdown
+ session->client_transmit_handle =
GNUNET_SERVER_notify_transmit_ready (session->client,
msg_length,
GNUNET_TIME_UNIT_FOREVER_REL,
&do_send_message,
- msg_obj);
- if ( ! session->client_transmit_handle)
+ session);
+ if ( NULL == session->client_transmit_handle)
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- _ ("Could not send message to client (%p)! This probably is OK if the client disconnected before us.\n"),
+ _ ("Could not send message to client (%p)!\n"),
session->client);
session->client = NULL;
// callback was not called!
- GNUNET_free (msg_obj);
GNUNET_free (msg);
+ session->msg = NULL;
}
else
// gracefully sent message, just terminate session structure
_ ("Sent result to client (%p), this session (%s) has ended!\n"),
session->client,
GNUNET_h2s (&session->key));
- free_session (session);
}
{
struct ServiceSession * session;
const struct GNUNET_SCALARPRODUCT_service_request * msg = (const struct GNUNET_SCALARPRODUCT_service_request *) message;
- uint16_t mask_length;
- uint16_t pk_length;
- uint16_t used_elements;
- uint16_t element_count;
- uint16_t msg_length;
+ uint32_t mask_length;
+ uint32_t pk_length;
+ uint32_t used_elements;
+ uint32_t element_count;
+ uint32_t msg_length;
unsigned char * current;
struct ServiceSession * responder_session;
int32_t i = -1;
// Check if message was sent by me, which would be bad!
if ( ! memcmp (&session->peer, &me, sizeof (struct GNUNET_PeerIdentity)))
{
- GNUNET_break (0);
GNUNET_free (session);
+ GNUNET_break (0);
return GNUNET_SYSERR;
}
//we need at least a peer and one message id to compare
if (ntohs (msg->header.size) < sizeof (struct GNUNET_SCALARPRODUCT_service_request))
{
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Too short message received from peer!\n"));
GNUNET_free (session);
+ GNUNET_break_op(0);
return GNUNET_SYSERR;
}
- mask_length = ntohs (msg->mask_length);
- pk_length = ntohs (msg->pk_length);
- used_elements = ntohs (msg->used_element_count);
- element_count = ntohs (msg->element_count);
+ mask_length = ntohl (msg->mask_length);
+ pk_length = ntohl (msg->pk_length);
+ used_elements = ntohl (msg->used_element_count);
+ element_count = ntohl (msg->element_count);
msg_length = sizeof (struct GNUNET_SCALARPRODUCT_service_request)
+ mask_length + pk_length + used_elements * PAILLIER_ELEMENT_LENGTH;
|| (used_elements == 0) || (mask_length != (element_count / 8 + (element_count % 8 ? 1 : 0)))
)
{
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Invalid message received from peer, message count does not match message length!\n"));
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Used elements: %hu\nElement Count: %hu\nExpected Mask Length: %hu\nCalculated Masklength: %d\n"), used_elements, element_count, mask_length, (element_count / 8 + (element_count % 8 ? 1 : 0)));
GNUNET_free (session);
+ GNUNET_break_op(0);
return GNUNET_SYSERR;
}
if (find_matching_session (from_service_tail,
}
memcpy (&session->peer, &session->peer, sizeof (struct GNUNET_PeerIdentity));
- session->state = REQUEST_FROM_SERVICE_RECEIVED;
- session->element_count = ntohs (msg->element_count);
+ session->state = SERVICE_REQUEST_RECEIVED;
+ session->element_count = ntohl (msg->element_count);
session->used_element_count = used_elements;
session->tunnel = tunnel;
current += pk_length;
//check if service queue contains a matching request
- needed_state = MESSAGE_FROM_RESPONDING_CLIENT_RECEIVED;
+ needed_state = CLIENT_RESPONSE_RECEIVED;
responder_session = find_matching_session (from_client_tail,
&session->key,
session->element_count,
}
else
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Got session with key %s without a matching element set, queueing.\n"), GNUNET_h2s (&session->key));
+
return GNUNET_OK;
}
else
free_session (session);
// and notify our client-session that we could not complete the session
if (responder_session)
- {
- struct TaskClosure * task = GNUNET_new(struct TaskClosure);
// we just found the responder session in this queue
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, responder_session);
- task->my_session = responder_session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
- task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
- }
+ responder_session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_end_notification,
+ responder_session);
return GNUNET_SYSERR;
}
{
struct ServiceSession * session;
const struct GNUNET_SCALARPRODUCT_service_response * msg = (const struct GNUNET_SCALARPRODUCT_service_response *) message;
- struct TaskClosure * task;
unsigned char * current;
- uint16_t count;
+ uint32_t count;
gcry_mpi_t s = NULL;
gcry_mpi_t s_prime = NULL;
size_t read;
size_t i;
- uint16_t used_element_count;
+ uint32_t used_element_count;
size_t msg_size;
gcry_mpi_t * r = NULL;
gcry_mpi_t * r_prime = NULL;
count = session->used_element_count;
session->product = NULL;
+ session->state = SERVICE_RESPONSE_RECEIVED;
//we need at least a peer and one message id to compare
if (sizeof (struct GNUNET_SCALARPRODUCT_service_response) > ntohs (msg->header.size))
GNUNET_break_op (0);
goto invalid_msg;
}
- used_element_count = ntohs (msg->used_element_count);
+ used_element_count = ntohl (msg->used_element_count);
msg_size = sizeof (struct GNUNET_SCALARPRODUCT_service_response)
+ 2 * used_element_count * PAILLIER_ELEMENT_LENGTH
+ 2 * PAILLIER_ELEMENT_LENGTH;
PAILLIER_ELEMENT_LENGTH, &read)))
{
LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
- GNUNET_break_op (0);
+ GNUNET_break_op (0);
goto invalid_msg;
}
current += PAILLIER_ELEMENT_LENGTH;
PAILLIER_ELEMENT_LENGTH, &read)))
{
LOG_GCRY (GNUNET_ERROR_TYPE_DEBUG, "gcry_mpi_scan", rc);
- GNUNET_break_op (0);
+ GNUNET_break_op (0);
goto invalid_msg;
}
current += PAILLIER_ELEMENT_LENGTH;
}
-
session->product = compute_scalar_product (session, r, r_prime, s, s_prime);
invalid_msg:
GNUNET_free_non_null (r);
GNUNET_free_non_null (r_prime);
- session->state = FINALIZED;
+ session->tunnel = NULL;
+ // send message with product to client
+ session->client_notification_task =
+ GNUNET_SCHEDULER_add_now (&prepare_client_response,
+ session);
// the tunnel has done its job, terminate our connection and the tunnel
// the peer will be notified that the tunnel was destroyed via tunnel_destruction_handler
- GNUNET_CONTAINER_DLL_remove (from_client_head, from_client_tail, session);
- // send message with product to client
-
- task = GNUNET_new (struct TaskClosure);
- task->my_session = session;
- task->my_handle = GNUNET_SCHEDULER_add_now (&prepare_client_response, task);
- GNUNET_CONTAINER_DLL_insert (tasklist_head, tasklist_tail, task);
- // just close the connection.
+ // just close the connection, as recommended by Christian
return GNUNET_SYSERR;
}
-
/**
* Task run during shutdown.
*
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct ServiceSession * session;
- struct TaskClosure * task;
GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Shutting down, initiating cleanup.\n"));
do_shutdown = GNUNET_YES;
- for (task = tasklist_head; NULL != task; task = task->next)
- GNUNET_SCHEDULER_cancel(task->my_handle);
-
+
// terminate all owned open tunnels.
for (session = from_client_head; NULL != session; session = session->next)
- if (FINALIZED != session->state)
- GNUNET_MESH_tunnel_destroy (session->tunnel);
- if (my_mesh)
+ {
+ if (FINALIZED != session->state)
+ GNUNET_MESH_tunnel_destroy (session->tunnel);
+ if (GNUNET_SCHEDULER_NO_TASK != session->client_notification_task)
+ {
+ GNUNET_SCHEDULER_cancel (session->client_notification_task);
+ session->client_notification_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != session->service_request_task)
+ {
+ GNUNET_SCHEDULER_cancel (session->service_request_task);
+ session->service_request_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ }
+ for (session = from_service_head; NULL != session; session = session->next)
+
+ if (my_mesh)
{
GNUNET_MESH_disconnect (my_mesh);
my_mesh = NULL;