diff options
author | Bart Polot <bart@net.in.tum.de> | 2013-12-06 17:13:08 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2013-12-06 17:13:08 +0000 |
commit | 1666ec7f20e2abe20fb460a6732843116a6b4baa (patch) | |
tree | f784cd0365a0df9b72bc7599f7e041d2391831fd /src/mesh/gnunet-mesh.c | |
parent | d02dbf1d82253695d7a50c404a415c26ba2ffba6 (diff) | |
download | gnunet-1666ec7f20e2abe20fb460a6732843116a6b4baa.tar.gz gnunet-1666ec7f20e2abe20fb460a6732843116a6b4baa.zip |
gnunet-mesh has netcat capabilities
Diffstat (limited to 'src/mesh/gnunet-mesh.c')
-rw-r--r-- | src/mesh/gnunet-mesh.c | 101 |
1 files changed, 100 insertions, 1 deletions
diff --git a/src/mesh/gnunet-mesh.c b/src/mesh/gnunet-mesh.c index a771f6948..af34d4ed4 100644 --- a/src/mesh/gnunet-mesh.c +++ b/src/mesh/gnunet-mesh.c | |||
@@ -68,6 +68,10 @@ static char *target_id; | |||
68 | */ | 68 | */ |
69 | static uint32_t target_port; | 69 | static uint32_t target_port; |
70 | 70 | ||
71 | /** | ||
72 | * Data pending in netcat mode. | ||
73 | */ | ||
74 | size_t data_size; | ||
71 | 75 | ||
72 | 76 | ||
73 | /** | 77 | /** |
@@ -75,6 +79,9 @@ static uint32_t target_port; | |||
75 | */ | 79 | */ |
76 | static struct GNUNET_MESH_Handle *mh; | 80 | static struct GNUNET_MESH_Handle *mh; |
77 | 81 | ||
82 | /** | ||
83 | * Channel handle. | ||
84 | */ | ||
78 | static struct GNUNET_MESH_Channel *ch; | 85 | static struct GNUNET_MESH_Channel *ch; |
79 | 86 | ||
80 | /** | 87 | /** |
@@ -82,6 +89,13 @@ static struct GNUNET_MESH_Channel *ch; | |||
82 | */ | 89 | */ |
83 | GNUNET_SCHEDULER_TaskIdentifier sd; | 90 | GNUNET_SCHEDULER_TaskIdentifier sd; |
84 | 91 | ||
92 | |||
93 | |||
94 | void | ||
95 | listen_stdio (void); | ||
96 | |||
97 | |||
98 | |||
85 | /** | 99 | /** |
86 | * Task run in monitor mode when the user presses CTRL-C to abort. | 100 | * Task run in monitor mode when the user presses CTRL-C to abort. |
87 | * Stops monitoring activity. | 101 | * Stops monitoring activity. |
@@ -107,6 +121,86 @@ shutdown_task (void *cls, | |||
107 | 121 | ||
108 | 122 | ||
109 | /** | 123 | /** |
124 | * Function called to notify a client about the connection | ||
125 | * begin ready to queue more data. "buf" will be | ||
126 | * NULL and "size" zero if the connection was closed for | ||
127 | * writing in the meantime. | ||
128 | * | ||
129 | * FIXME | ||
130 | * | ||
131 | * @param cls closure | ||
132 | * @param size number of bytes available in buf | ||
133 | * @param buf where the callee should write the message | ||
134 | * @return number of bytes written to buf | ||
135 | */ | ||
136 | size_t | ||
137 | data_ready (void *cls, size_t size, void *buf) | ||
138 | { | ||
139 | struct GNUNET_MessageHeader *msg; | ||
140 | size_t total_size; | ||
141 | |||
142 | if (NULL == buf || 0 == size) | ||
143 | { | ||
144 | GNUNET_SCHEDULER_shutdown(); | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | total_size = data_size + sizeof (struct GNUNET_MessageHeader); | ||
149 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size); | ||
150 | GNUNET_assert (size >= total_size); | ||
151 | |||
152 | msg = buf; | ||
153 | msg->size = ntohs (total_size); | ||
154 | msg->type = ntohs (GNUNET_MESSAGE_TYPE_MESH_CLI); | ||
155 | memcpy (&msg[1], cls, data_size); | ||
156 | listen_stdio (); | ||
157 | |||
158 | return total_size; | ||
159 | } | ||
160 | |||
161 | |||
162 | /** | ||
163 | * Task run in monitor mode when the user presses CTRL-C to abort. | ||
164 | * Stops monitoring activity. | ||
165 | * | ||
166 | * @param cls Closure (unused). | ||
167 | * @param tc scheduler context | ||
168 | */ | ||
169 | static void | ||
170 | read_stdio (void *cls, | ||
171 | const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
172 | { | ||
173 | char buf[60000]; | ||
174 | |||
175 | if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)) | ||
176 | { | ||
177 | return; | ||
178 | } | ||
179 | |||
180 | data_size = read (2, buf, 60000); | ||
181 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size); | ||
182 | GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO, | ||
183 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
184 | data_size | ||
185 | + sizeof (struct GNUNET_MessageHeader), | ||
186 | &data_ready, buf); | ||
187 | } | ||
188 | |||
189 | void | ||
190 | listen_stdio (void) | ||
191 | { | ||
192 | struct GNUNET_NETWORK_FDSet *rs; | ||
193 | |||
194 | rs = GNUNET_NETWORK_fdset_create (); | ||
195 | GNUNET_NETWORK_fdset_set_native (rs, 2); | ||
196 | GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
197 | GNUNET_TIME_UNIT_FOREVER_REL, | ||
198 | rs, NULL, | ||
199 | &read_stdio, NULL); | ||
200 | } | ||
201 | |||
202 | |||
203 | /** | ||
110 | * Function called whenever a channel is destroyed. Should clean up | 204 | * Function called whenever a channel is destroyed. Should clean up |
111 | * any associated state. | 205 | * any associated state. |
112 | * | 206 | * |
@@ -153,7 +247,9 @@ channel_incoming (void *cls, | |||
153 | const struct GNUNET_PeerIdentity * initiator, | 247 | const struct GNUNET_PeerIdentity * initiator, |
154 | uint32_t port, enum MeshOption options) | 248 | uint32_t port, enum MeshOption options) |
155 | { | 249 | { |
156 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Incoming channel %p on port %u\n", channel, port); | 250 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
251 | "Incoming channel %p on port %u\n", | ||
252 | channel, port); | ||
157 | if (NULL != ch) | 253 | if (NULL != ch) |
158 | { | 254 | { |
159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n"); | 255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n"); |
@@ -165,6 +261,7 @@ channel_incoming (void *cls, | |||
165 | return NULL; | 261 | return NULL; |
166 | } | 262 | } |
167 | ch = channel; | 263 | ch = channel; |
264 | listen_stdio (); | ||
168 | return NULL; | 265 | return NULL; |
169 | } | 266 | } |
170 | 267 | ||
@@ -197,6 +294,7 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
197 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id); | 294 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id); |
198 | ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, | 295 | ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, |
199 | GNUNET_MESH_OPTION_DEFAULT); | 296 | GNUNET_MESH_OPTION_DEFAULT); |
297 | listen_stdio (); | ||
200 | } | 298 | } |
201 | 299 | ||
202 | 300 | ||
@@ -224,6 +322,7 @@ data_callback (void *cls, | |||
224 | GNUNET_break (ch == channel); | 322 | GNUNET_break (ch == channel); |
225 | 323 | ||
226 | len = ntohs (message->size) - sizeof (*message); | 324 | len = ntohs (message->size) - sizeof (*message); |
325 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len); | ||
227 | FPRINTF (stdout, "%.*s", len, (char *) &message[1]); | 326 | FPRINTF (stdout, "%.*s", len, (char *) &message[1]); |
228 | return GNUNET_OK; | 327 | return GNUNET_OK; |
229 | } | 328 | } |