Пример #1
0
/**
 * Call MESH's monitor API, get info of one connection.
 *
 * @param cls Closure (unused).
 * @param tc TaskContext
 */
static void
create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_PeerIdentity pid;
  enum GNUNET_MESH_ChannelOption opt;

  GNUNET_assert (NULL == ch);

  if (GNUNET_OK !=
      GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
                                                  strlen (target_id),
                                                  &pid.public_key))
  {
    FPRINTF (stderr,
             _("Invalid target `%s'\n"),
             target_id);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
  opt = GNUNET_MESH_OPTION_DEFAULT | GNUNET_MESH_OPTION_RELIABLE;
  ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, opt);
  if (GNUNET_NO == echo)
    listen_stdio ();
  else
    GNUNET_SCHEDULER_add_now (send_echo, NULL);
}
Пример #2
0
/**
 * Search callback function called when a subscribed peer is found.
 *
 * @param cls closure provided in GNUNET_REGEX_search()
 * @param id peer providing a regex that matches the string
 * @param get_path path of the get request
 * @param get_path_length length of @a get_path
 * @param put_path Path of the put request
 * @param put_path_length length of the @a put_path
 */
static void
subscribed_peer_found (void *cls, const struct GNUNET_PeerIdentity *id,
                       const struct GNUNET_PeerIdentity *get_path,
                       unsigned int get_path_length,
                       const struct GNUNET_PeerIdentity *put_path,
                       unsigned int put_path_length)
{
  struct PendingMessage *pm;
  struct RemoteSubscriberInfo *subscriber;
  struct GNUNET_MessageHeader *msg;
  struct RegexSearchContext *context = cls;
  size_t msg_len = ntohs (context->publish_msg->header.size);

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "--------> Found an active subscription from %s\n",
	     GNUNET_i2s (id));

  /*
   * We may have delivered the message to the peer already if it has
   * other matching subscriptions; ignore this search result if that is
   * the case.
   */
  if (GNUNET_CONTAINER_multipeermap_contains (context->subscribers,
                                              id))
    return;

  GNUNET_CONTAINER_multipeermap_put (context->subscribers, id,
    NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  subscriber = GNUNET_CONTAINER_multipeermap_get (remote_subscribers,
                                                  id);

  if (0 == memcmp (id, &my_id, sizeof (struct GNUNET_PeerIdentity)))
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
         "fast tracking PUBLISH message to local subscribers\n");

    deliver_incoming_publish (context->publish_msg, context);
    return;
  }

  msg = GNUNET_malloc (msg_len);
  memcpy (msg, context->publish_msg, msg_len);

  pm = GNUNET_new (struct PendingMessage);
  pm->msg = msg;
  pm->context = context;

  if (NULL == subscriber)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
                "creating a new channel to %s\n", GNUNET_i2s(id));

    subscriber = GNUNET_new (struct RemoteSubscriberInfo);

    subscriber->channel = GNUNET_MESH_channel_create (mesh_handle,
                                                      NULL,
                                                      id,
                                                      GNUNET_APPLICATION_TYPE_MQTT,
                                                      GNUNET_MESH_OPTION_RELIABLE);
    subscriber->peer_added = GNUNET_NO;
    subscriber->peer_connecting = GNUNET_NO;

    subscriber->id = *id;

    GNUNET_CONTAINER_multipeermap_put (remote_subscribers, id,
      subscriber, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  }