*/
static uint32_t target_port;
+/**
+ * Data pending in netcat mode.
+ */
+size_t data_size;
/**
*/
static struct GNUNET_MESH_Handle *mh;
+/**
+ * Channel handle.
+ */
static struct GNUNET_MESH_Channel *ch;
/**
*/
GNUNET_SCHEDULER_TaskIdentifier sd;
+
+
+static void
+listen_stdio (void);
+
+
+
/**
* Task run in monitor mode when the user presses CTRL-C to abort.
* Stops monitoring activity.
}
+/**
+ * Function called to notify a client about the connection
+ * begin ready to queue more data. "buf" will be
+ * NULL and "size" zero if the connection was closed for
+ * writing in the meantime.
+ *
+ * FIXME
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+size_t
+data_ready (void *cls, size_t size, void *buf)
+{
+ struct GNUNET_MessageHeader *msg;
+ size_t total_size;
+
+ if (NULL == buf || 0 == size)
+ {
+ GNUNET_SCHEDULER_shutdown();
+ return 0;
+ }
+
+ total_size = data_size + sizeof (struct GNUNET_MessageHeader);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
+ GNUNET_assert (size >= total_size);
+
+ msg = buf;
+ msg->size = ntohs (total_size);
+ msg->type = ntohs (GNUNET_MESSAGE_TYPE_MESH_CLI);
+ memcpy (&msg[1], cls, data_size);
+ listen_stdio ();
+
+ return total_size;
+}
+
+
+/**
+ * Task run in monitor mode when the user presses CTRL-C to abort.
+ * Stops monitoring activity.
+ *
+ * @param cls Closure (unused).
+ * @param tc scheduler context
+ */
+static void
+read_stdio (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ char buf[60000];
+
+ if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+ {
+ return;
+ }
+
+ data_size = read (0, buf, 60000);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
+ if (data_size < 1)
+ {
+ GNUNET_SCHEDULER_shutdown();
+ return;
+ }
+ GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ data_size
+ + sizeof (struct GNUNET_MessageHeader),
+ &data_ready, buf);
+}
+
+
+/**
+ * Start listening to stdin
+ */
+static void
+listen_stdio (void)
+{
+ struct GNUNET_NETWORK_FDSet *rs;
+
+ rs = GNUNET_NETWORK_fdset_create ();
+ GNUNET_NETWORK_fdset_set_native (rs, 0);
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ rs, NULL,
+ &read_stdio, NULL);
+}
+
+
/**
* Function called whenever a channel is destroyed. Should clean up
* any associated state.
const struct GNUNET_MESH_Channel *channel,
void *channel_ctx)
{
- FPRINTF (stdout, "Channel ended!\n");
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
GNUNET_break (channel == ch);
+ ch = NULL;
GNUNET_SCHEDULER_shutdown ();
}
* @return initial channel context for the channel
* (can be NULL -- that's not an error)
*/
-void *
+static void *
channel_incoming (void *cls,
struct GNUNET_MESH_Channel * channel,
const struct GNUNET_PeerIdentity * initiator,
- uint32_t port, enum MeshOption options)
+ uint32_t port, enum GNUNET_MESH_ChannelOption options)
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Incoming channel on port %u\n", port);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Incoming channel %p on port %u\n",
+ channel, port);
if (NULL != ch)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
return NULL;
}
ch = channel;
+ listen_stdio ();
return NULL;
}
{
struct GNUNET_PeerIdentity pid;
+ GNUNET_assert (NULL == ch);
+
if (GNUNET_OK !=
GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
strlen (target_id),
GNUNET_SCHEDULER_shutdown ();
return;
}
-// GNUNET_MESH_channel_create ()
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
+ ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port,
+ GNUNET_MESH_OPTION_DEFAULT);
+ listen_stdio ();
}
* @return #GNUNET_OK to keep the channel open,
* #GNUNET_SYSERR to close it (signal serious error).
*/
-int
+static int
data_callback (void *cls,
struct GNUNET_MESH_Channel *channel,
void **channel_ctx,
GNUNET_break (ch == channel);
len = ntohs (message->size) - sizeof (*message);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
FPRINTF (stdout, "%.*s", len, (char *) &message[1]);
return GNUNET_OK;
}
{&data_callback, GNUNET_MESSAGE_TYPE_MESH_CLI, 0},
{NULL, 0, 0} /* FIXME add option to monitor msg types */
};
+ static uint32_t *ports = NULL;
/* FIXME add option to monitor apps */
target_id = args[0];
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
newch = &channel_incoming;
+ endch = &channel_ended;
+ ports = GNUNET_malloc (sizeof (uint32_t) * 2);
+ ports[0] = listen_port;
}
else if (NULL != tunnel_id)
{
newch, /* new channel */
endch, /* cleaner */
handlers,
- NULL);
- FPRINTF (stdout, "Done\n");
+ ports);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
if (NULL == mh)
GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
else
main (int argc, char *const *argv)
{
int res;
+ const char helpstr[] = "Create channels and retreive info about meshs status.";
static const struct GNUNET_GETOPT_CommandLineOption options[] = {
{'a', "channel", "TUNNEL_ID:CHANNEL_ID",
gettext_noop ("provide information about a particular channel"),
return 2;
res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
- gettext_noop
- ("Create channels and retreive info about meshs status."),
- options, &run, NULL);
+ gettext_noop (helpstr),
+ options, &run, NULL);
GNUNET_free ((void *) argv);