+ * @param op the channel context
+ */
+static void
+handle_incoming_disconnect (struct Operation *op)
+{
+ GNUNET_assert (GNUNET_YES == op->is_incoming);
+ /* channel is already dead, incoming_destroy must not
+ * destroy it ... */
+ op->channel = NULL;
+ incoming_destroy (op);
+ op->vt = NULL;
+}
+
+
+/**
+ * Method called whenever another peer has added us to a channel the
+ * other peer initiated. Only called (once) upon reception of data
+ * from a channel we listen on.
+ *
+ * The channel context represents the operation itself and gets added
+ * to a DLL, from where it gets looked up when our local listener
+ * client responds to a proposed/suggested operation or connects and
+ * associates with this operation.
+ *
+ * @param cls closure
+ * @param channel new handle to the channel
+ * @param initiator peer that started the channel
+ * @param port Port this channel is for.
+ * @param options Unused.
+ * @return initial channel context for the channel
+ * returns NULL on error
+ */
+static void *
+channel_new_cb (void *cls,
+ struct GNUNET_CADET_Channel *channel,
+ const struct GNUNET_PeerIdentity *initiator,
+ const struct GNUNET_HashCode *port,
+ enum GNUNET_CADET_ChannelOption options)
+{
+ static const struct SetVT incoming_vt = {
+ .msg_handler = &handle_incoming_msg,
+ .peer_disconnect = &handle_incoming_disconnect
+ };
+ struct Listener *listener = cls;
+ struct Operation *incoming;
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "New incoming channel\n");
+ incoming = GNUNET_new (struct Operation);
+ incoming->listener = listener;
+ incoming->is_incoming = GNUNET_YES;
+ incoming->peer = *initiator;
+ incoming->channel = channel;
+ incoming->mq = GNUNET_CADET_mq_create (incoming->channel);
+ incoming->vt = &incoming_vt;
+ incoming->timeout_task
+ = GNUNET_SCHEDULER_add_delayed (INCOMING_CHANNEL_TIMEOUT,
+ &incoming_timeout_cb,
+ incoming);
+ GNUNET_CONTAINER_DLL_insert_tail (incoming_head,
+ incoming_tail,
+ incoming);
+ // incoming_suggest (incoming,
+ // listener);
+ return incoming;
+}
+
+
+/**
+ * Called when a client wants to create a new listener.
+ *
+ * @param cls unused
+ * @param client client that sent the message
+ * @param m message sent by the client
+ */
+static void
+handle_client_listen (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader *m)
+{
+ const struct GNUNET_SET_ListenMessage *msg;
+ struct Listener *listener;
+ struct Operation *op;
+
+ msg = (const struct GNUNET_SET_ListenMessage *) m;
+ if (NULL != listener_get (client))
+ {
+ /* max. one active listener per client! */
+ GNUNET_break (0);
+ GNUNET_SERVER_client_disconnect (client);
+ return;
+ }
+ listener = GNUNET_new (struct Listener);
+ listener->client = client;
+ listener->client_mq = GNUNET_MQ_queue_for_server_client (client);
+ listener->app_id = msg->app_id;
+ listener->operation = ntohl (msg->operation);
+ GNUNET_CONTAINER_DLL_insert_tail (listeners_head,
+ listeners_tail,
+ listener);
+ listener->open_port = GNUNET_CADET_open_port (cadet,
+ &msg->app_id,
+ &channel_new_cb,
+ listener);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "New listener created (op %u, port %s)\n",
+ listener->operation,
+ GNUNET_h2s (&listener->app_id));
+
+ /* check for existing incoming requests the listener might be interested in */
+ for (op = incoming_head; NULL != op; op = op->next)
+ {
+ if (NULL == op->spec)
+ continue; /* no details available yet */
+ if (0 != op->suggest_id)
+ continue; /* this one has been already suggested to a listener */
+ if (listener->operation != op->spec->operation)
+ continue; /* incompatible operation */
+ if (0 != GNUNET_CRYPTO_hash_cmp (&listener->app_id,
+ &op->spec->app_id))
+ continue; /* incompatible appliation */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Found matching existing request\n");
+ incoming_suggest (op,
+ listener);
+ }
+ GNUNET_SERVER_receive_done (client,
+ GNUNET_OK);
+}
+
+
+/**
+ * Called when the listening client rejects an operation
+ * request by another peer.
+ *
+ * @param cls unused
+ * @param client client that sent the message
+ * @param m message sent by the client