- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u (%u ch, %u conn)\n",
- (unsigned int) sizeof (struct GNUNET_CADET_LocalInfoTunnel),
- (unsigned int) sizeof (CADET_ChannelNumber),
- (unsigned int) sizeof (struct GNUNET_HashCode));
- h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
- goto clean_cls;
- }
-
- /* Call Callback with tunnel info. */
- conns = (struct GNUNET_CADET_Hash *) &msg[1];
- chns = (CADET_ChannelNumber *) &conns[c_n];
- h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
- ch_n, c_n, chns, conns,
- ntohs (msg->estate), ntohs (msg->cstate));
-
-clean_cls:
- h->info_cb.tunnel_cb = NULL;
- h->info_cls = NULL;
-}
-
-
-/**
- * Function to process all messages received from the service
- *
- * @param cls closure
- * @param msg message received, NULL on timeout or fatal error
- */
-static void
-msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
-{
- struct GNUNET_CADET_Handle *h = cls;
- uint16_t type;
-
- if (msg == NULL)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Cadet service disconnected, reconnecting\n", h);
- reconnect (h);
- return;
- }
- type = ntohs (msg->type);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
- GC_m2s (type));
- switch (type)
- {
- /* Notify of a new incoming channel */
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
- process_channel_created (h,
- (struct GNUNET_CADET_ChannelCreateMessage *) msg);
- break;
- /* Notify of a channel disconnection */
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
- case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
- process_channel_destroy (h,
- (struct GNUNET_CADET_ChannelDestroyMessage *) msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
- process_incoming_data (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
- process_ack (h, msg);
- break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
-// process_get_channels (h, msg);
-// break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-// process_show_channel (h, msg);
-// break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
- process_get_peers (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
- process_get_peer (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
- process_get_tunnels (h, msg);
- break;
- case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
- process_get_tunnel (h, msg);
- break;
-// case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
-// process_show_channel (h, msg);
-// break;
- default:
- /* We shouldn't get any other packages, log and ignore */
- LOG (GNUNET_ERROR_TYPE_WARNING,
- "unsolicited message form service (type %s)\n",
- GC_m2s (ntohs (msg->type)));
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
- if (GNUNET_YES == h->in_receive)
- {
- GNUNET_CLIENT_receive (h->client, &msg_received, h,
- GNUNET_TIME_UNIT_FOREVER_REL);
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "in receive off, not calling CLIENT_receive\n");
- }
-}
-
-
-/******************************************************************************/
-/************************ SEND FUNCTIONS ****************************/
-/******************************************************************************/
-
-/**
- * Function called to send a message to the service.
- * "buf" will be NULL and "size" zero if the socket was closed for writing in
- * the meantime.
- *
- * @param cls closure, the cadet handle
- * @param size number of bytes available in buf
- * @param buf where the callee should write the connect message
- * @return number of bytes written to buf
- */
-static size_t
-send_callback (void *cls, size_t size, void *buf)
-{
- struct GNUNET_CADET_Handle *h = cls;
- struct GNUNET_CADET_TransmitHandle *th;
- struct GNUNET_CADET_TransmitHandle *next;
- struct GNUNET_CADET_Channel *ch;
- char *cbuf = buf;
- size_t tsize;
- size_t psize;
- size_t nsize;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send callback, buffer %u\n", size);
- if ((0 == size) || (NULL == buf))
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
- reconnect (h);
- h->th = NULL;
- return 0;
- }
- tsize = 0;
- next = h->th_head;
- nsize = message_ready_size (h);
- while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
- {
- ch = th->channel;
- if (GNUNET_YES == th_is_payload (th))
- {
- struct GNUNET_CADET_LocalData *dmsg;
- struct GNUNET_MessageHeader *mh;
-
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload, %u bytes on %X (%p)\n",
- th->size, ch->chid, ch);
- if (GNUNET_NO == ch->allow_send)
- {
- /* This channel is not ready to transmit yet, Try the next message */
- next = th->next;
- continue;
- }
- ch->packet_size = 0;
- GNUNET_assert (size >= th->size);
- dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
- mh = (struct GNUNET_MessageHeader *) &dmsg[1];
- psize = th->notify (th->notify_cls, size - DATA_OVERHEAD, mh);
-
- if (psize > 0)
- {
- GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= psize);
- psize += DATA_OVERHEAD;
- GNUNET_assert (size >= psize);
- dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
- dmsg->header.size = htons (psize);
- dmsg->id = htonl (ch->chid);
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# sending, type %s\n",
- GC_m2s (ntohs (mh->type)));
- ch->allow_send = GNUNET_NO;
- }
- else
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "# callback returned size 0, "
- "application canceled transmission\n");
- }
- }
- else
- {
- const struct GNUNET_MessageHeader *mh;
-
- mh = (const struct GNUNET_MessageHeader *) &th[1];
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
- GC_m2s (ntohs (mh->type)));
- GNUNET_memcpy (cbuf, &th[1], th->size);
- psize = th->size;
- }
- GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= psize);
- if (th->timeout_task != NULL)
- GNUNET_SCHEDULER_cancel (th->timeout_task);
- next = th->next;
- GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
- GNUNET_free (th);
- nsize = message_ready_size (h);
- cbuf += psize;
- size -= psize;
- tsize += psize;
- }
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
- h->th = NULL;
- size = message_ready_size (h);
- if (0 != size)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
- h->th =
- GNUNET_CLIENT_notify_transmit_ready (h->client, size,
- GNUNET_TIME_UNIT_FOREVER_REL,
- GNUNET_YES, &send_callback, h);
- }
- else
- {
- if (NULL != h->th_head)
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing ready to transmit\n");
- else
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
- }
- if (GNUNET_NO == h->in_receive)
- {
- LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
- h->in_receive = GNUNET_YES;
- GNUNET_CLIENT_receive (h->client, &msg_received, h,
- GNUNET_TIME_UNIT_FOREVER_REL);