aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/plugin_transport_tcp.c')
-rw-r--r--src/transport/plugin_transport_tcp.c528
1 files changed, 486 insertions, 42 deletions
diff --git a/src/transport/plugin_transport_tcp.c b/src/transport/plugin_transport_tcp.c
index 34bbd00e0..10ea01cec 100644
--- a/src/transport/plugin_transport_tcp.c
+++ b/src/transport/plugin_transport_tcp.c
@@ -45,10 +45,495 @@
45 */ 45 */
46#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) 46#define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
47 47
48GNUNET_NETWORK_STRUCT_BEGIN 48/**
49 * Opaque handle that can be used to cancel
50 * a transmit-ready notification.
51 */
52struct GNUNET_CONNECTION_TransmitHandle;
53
54/**
55 * @brief handle for a server
56 */
57struct GNUNET_SERVER_Handle;
58
59/**
60 * @brief opaque handle for a client of the server
61 */
62struct GNUNET_SERVER_Client;
63
64/**
65 * @brief opaque handle server returns for aborting transmission to a client.
66 */
67struct GNUNET_SERVER_TransmitHandle;
68
69/**
70 * @brief handle for a network connection
71 */
72struct GNUNET_CONNECTION_Handle;
73
74
75/**
76 * Function called to notify a client about the connection begin ready
77 * to queue more data. @a buf will be NULL and @a size zero if the
78 * connection was closed for writing in the meantime.
79 *
80 * @param cls closure
81 * @param size number of bytes available in @a buf
82 * @param buf where the callee should write the message
83 * @return number of bytes written to @a buf
84 */
85typedef size_t
86(*GNUNET_CONNECTION_TransmitReadyNotify) (void *cls,
87 size_t size,
88 void *buf);
89
90/**
91 * Credentials for UNIX domain sockets.
92 */
93struct GNUNET_CONNECTION_Credentials
94{
95 /**
96 * UID of the other end of the connection.
97 */
98 uid_t uid;
99
100 /**
101 * GID of the other end of the connection.
102 */
103 gid_t gid;
104};
105
106
107/**
108 * Functions with this signature are called whenever a client
109 * is disconnected on the network level.
110 *
111 * @param cls closure
112 * @param client identification of the client; NULL
113 * for the last call when the server is destroyed
114 */
115typedef void
116(*GNUNET_SERVER_DisconnectCallback) (void *cls,
117 struct GNUNET_SERVER_Client *client);
118
119
120/**
121 * Functions with this signature are called whenever a client
122 * is connected on the network level.
123 *
124 * @param cls closure
125 * @param client identification of the client
126 */
127typedef void
128(*GNUNET_SERVER_ConnectCallback) (void *cls,
129 struct GNUNET_SERVER_Client *client);
130
131
49 132
50 133
51/** 134/**
135 * Function to call for access control checks.
136 *
137 * @param cls closure
138 * @param ucred credentials, if available, otherwise NULL
139 * @param addr address
140 * @param addrlen length of address
141 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
142 * for unknown address family (will be denied).
143 */
144typedef int
145(*GNUNET_CONNECTION_AccessCheck) (void *cls,
146 const struct
147 GNUNET_CONNECTION_Credentials *
148 ucred,
149 const struct sockaddr * addr,
150 socklen_t addrlen);
151
152/**
153 * Callback function for data received from the network. Note that
154 * both "available" and "err" would be 0 if the read simply timed out.
155 *
156 * @param cls closure
157 * @param buf pointer to received data
158 * @param available number of bytes availabe in "buf",
159 * possibly 0 (on errors)
160 * @param addr address of the sender
161 * @param addrlen size of addr
162 * @param errCode value of errno (on errors receiving)
163 */
164typedef void
165(*GNUNET_CONNECTION_Receiver) (void *cls, const void *buf,
166 size_t available,
167 const struct sockaddr * addr,
168 socklen_t addrlen, int errCode);
169
170
171
172/**
173 * Close the connection and free associated resources. There must
174 * not be any pending requests for reading or writing to the
175 * connection at this time.
176 *
177 * @param connection connection to destroy
178 */
179void
180GNUNET_CONNECTION_destroy (struct GNUNET_CONNECTION_Handle *connection);
181
182
183/**
184 * Signature of a function to create a custom tokenizer.
185 *
186 * @param cls closure from #GNUNET_SERVER_set_callbacks
187 * @param client handle to client the tokenzier will be used for
188 * @return handle to custom tokenizer ('mst')
189 */
190typedef void*
191(*GNUNET_SERVER_MstCreateCallback) (void *cls,
192 struct GNUNET_SERVER_Client *client);
193
194
195/**
196 * Signature of a function to destroy a custom tokenizer.
197 *
198 * @param cls closure from #GNUNET_SERVER_set_callbacks
199 * @param mst custom tokenizer handle
200 */
201typedef void
202(*GNUNET_SERVER_MstDestroyCallback) (void *cls,
203 void *mst);
204
205/**
206 * Signature of a function to receive data for a custom tokenizer.
207 *
208 * @param cls closure from #GNUNET_SERVER_set_callbacks
209 * @param mst custom tokenizer handle
210 * @param client_identity ID of client for which this is a buffer,
211 * can be NULL (will be passed back to 'cb')
212 * @param buf input data to add
213 * @param size number of bytes in @a buf
214 * @param purge should any excess bytes in the buffer be discarded
215 * (i.e. for packet-based services like UDP)
216 * @param one_shot only call callback once, keep rest of message in buffer
217 * @return #GNUNET_OK if we are done processing (need more data)
218 * #GNUNET_NO if one_shot was set and we have another message ready
219 * #GNUNET_SYSERR if the data stream is corrupt
220 */
221typedef int
222(*GNUNET_SERVER_MstReceiveCallback) (void *cls, void *mst,
223 struct GNUNET_SERVER_Client *client,
224 const char *buf,
225 size_t size,
226 int purge,
227 int one_shot);
228/**
229 * Functions with this signature are called whenever a message is
230 * received.
231 *
232 * @param cls closure
233 * @param client identification of the client
234 * @param message the actual message
235 */
236typedef void
237(*GNUNET_SERVER_MessageCallback) (void *cls,
238 struct GNUNET_SERVER_Client *client,
239 const struct GNUNET_MessageHeader *message);
240
241/**
242 * Message handler. Each struct specifies how to handle on particular
243 * type of message received.
244 */
245struct GNUNET_SERVER_MessageHandler
246{
247 /**
248 * Function to call for messages of "type".
249 */
250 GNUNET_SERVER_MessageCallback callback;
251
252 /**
253 * Closure argument for @e callback.
254 */
255 void *callback_cls;
256
257 /**
258 * Type of the message this handler covers.
259 */
260 uint16_t type;
261
262 /**
263 * Expected size of messages of this type. Use 0 for
264 * variable-size. If non-zero, messages of the given
265 * type will be discarded (and the connection closed)
266 * if they do not have the right size.
267 */
268 uint16_t expected_size;
269
270};
271
272/**
273 * Ask the server to disconnect from the given client. This is the
274 * same as passing #GNUNET_SYSERR to #GNUNET_SERVER_receive_done,
275 * except that it allows dropping of a client even when not handling a
276 * message from that client.
277 *
278 * @param client the client to disconnect from
279 */
280void
281GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client);
282
283/**
284 * Return user context associated with the given client.
285 * Note: you should probably use the macro (call without the underscore).
286 *
287 * @param client client to query
288 * @param size number of bytes in user context struct (for verification only)
289 * @return pointer to user context
290 */
291void *
292GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
293 size_t size);
294
295
296/**
297 * Functions with this signature are called whenever a
298 * complete message is received by the tokenizer.
299 *
300 * Do not call #GNUNET_SERVER_mst_destroy from within
301 * the scope of this callback.
302 *
303 * @param cls closure
304 * @param client identification of the client
305 * @param message the actual message
306 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
307 */
308typedef int
309(*GNUNET_SERVER_MessageTokenizerCallback) (void *cls,
310 void *client,
311 const struct GNUNET_MessageHeader *message);
312
313
314/**
315 * Create a message stream tokenizer.
316 *
317 * @param cb function to call on completed messages
318 * @param cb_cls closure for @a cb
319 * @return handle to tokenizer
320 */
321struct GNUNET_SERVER_MessageStreamTokenizer *
322GNUNET_SERVER_mst_create (GNUNET_SERVER_MessageTokenizerCallback cb,
323 void *cb_cls);
324
325/**
326 * Add incoming data to the receive buffer and call the
327 * callback for all complete messages.
328 *
329 * @param mst tokenizer to use
330 * @param client_identity ID of client for which this is a buffer,
331 * can be NULL (will be passed back to 'cb')
332 * @param buf input data to add
333 * @param size number of bytes in @a buf
334 * @param purge should any excess bytes in the buffer be discarded
335 * (i.e. for packet-based services like UDP)
336 * @param one_shot only call callback once, keep rest of message in buffer
337 * @return #GNUNET_OK if we are done processing (need more data)
338 * #GNUNET_NO if one_shot was set and we have another message ready
339 * #GNUNET_SYSERR if the data stream is corrupt
340 */
341int
342GNUNET_SERVER_mst_receive (struct GNUNET_SERVER_MessageStreamTokenizer *mst,
343 void *client_identity,
344 const char *buf, size_t size,
345 int purge, int one_shot);
346
347
348
349/**
350 * Destroys a tokenizer.
351 *
352 * @param mst tokenizer to destroy
353 */
354void
355GNUNET_SERVER_mst_destroy (struct GNUNET_SERVER_MessageStreamTokenizer *mst);
356
357
358/**
359 * Set user context to be associated with the given client.
360 * Note: you should probably use the macro (call without the underscore).
361 *
362 * @param client client to query
363 * @param ptr pointer to user context
364 * @param size number of bytes in user context struct (for verification only)
365 */
366void
367GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
368 void *ptr,
369 size_t size);
370/**
371 * Return user context associated with the given client.
372 *
373 * @param client client to query
374 * @param type expected return type (i.e. 'struct Foo')
375 * @return pointer to user context of type 'type *'.
376 */
377#define GNUNET_SERVER_client_get_user_context(client,type) \
378 (type *) GNUNET_SERVER_client_get_user_context_ (client, sizeof (type))
379
380/**
381 * Set user context to be associated with the given client.
382 *
383 * @param client client to query
384 * @param value pointer to user context
385 */
386#define GNUNET_SERVER_client_set_user_context(client,value) \
387 GNUNET_SERVER_client_set_user_context_ (client, value, sizeof (*value))
388
389
390
391/**
392 * Notify us when the server has enough space to transmit
393 * a message of the given size to the given client.
394 *
395 * @param client client to transmit message to
396 * @param size requested amount of buffer space
397 * @param timeout after how long should we give up (and call
398 * notify with buf NULL and size 0)?
399 * @param callback function to call when space is available
400 * @param callback_cls closure for @a callback
401 * @return non-NULL if the notify callback was queued; can be used
402 * to cancel the request using
403 * #GNUNET_SERVER_notify_transmit_ready_cancel.
404 * NULL if we are already going to notify someone else (busy)
405 */
406struct GNUNET_SERVER_TransmitHandle *
407GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
408 size_t size,
409 struct GNUNET_TIME_Relative timeout,
410 GNUNET_CONNECTION_TransmitReadyNotify callback,
411 void *callback_cls);
412
413/**
414 * Abort transmission request.
415 *
416 * @param th request to abort
417 */
418void
419GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th);
420
421
422
423
424/**
425 * Notify the server that the given client handle should
426 * be kept (keeps the connection up if possible, increments
427 * the internal reference counter).
428 *
429 * @param client the client to keep
430 */
431void
432GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client);
433
434
435/**
436 * Notify the server that the given client handle is no
437 * longer required. Decrements the reference counter. If
438 * that counter reaches zero an inactive connection maybe
439 * closed.
440 *
441 * @param client the client to drop
442 */
443void
444GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client);
445
446
447/**
448 * Function called by the service's run
449 * method to run service-specific setup code.
450 *
451 * @param cls closure
452 * @param server the initialized server
453 * @param cfg configuration to use
454 */
455typedef void
456(*GNUNET_SERVICE_Main) (void *cls,
457 struct GNUNET_SERVER_Handle *server,
458 const struct GNUNET_CONFIGURATION_Handle *cfg);
459
460
461
462/**
463 * Suspend accepting connections from the listen socket temporarily.
464 * Resume activity using #GNUNET_SERVER_resume.
465 *
466 * @param server server to stop accepting connections.
467 */
468void
469GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server);
470
471/**
472 * Notify us when the server has enough space to transmit
473 * a message of the given size to the given client.
474 *
475 * @param client client to transmit message to
476 * @param size requested amount of buffer space
477 * @param timeout after how long should we give up (and call
478 * notify with buf NULL and size 0)?
479 * @param callback function to call when space is available
480 * @param callback_cls closure for @a callback
481 * @return non-NULL if the notify callback was queued; can be used
482 * to cancel the request using
483 * #GNUNET_SERVER_notify_transmit_ready_cancel.
484 * NULL if we are already going to notify someone else (busy)
485 */
486struct GNUNET_SERVER_TransmitHandle *
487GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
488 size_t size,
489 struct GNUNET_TIME_Relative timeout,
490 GNUNET_CONNECTION_TransmitReadyNotify callback,
491 void *callback_cls);
492
493
494/**
495 * Add a TCP socket-based connection to the set of handles managed by
496 * this server. Use this function for outgoing (P2P) connections that
497 * we initiated (and where this server should process incoming
498 * messages).
499 *
500 * @param server the server to use
501 * @param connection the connection to manage (client must
502 * stop using this connection from now on)
503 * @return the client handle
504 */
505struct GNUNET_SERVER_Client *
506GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
507 struct GNUNET_CONNECTION_Handle *connection);
508
509
510/**
511 * Resume accepting connections from the listen socket.
512 *
513 * @param server server to resume accepting connections.
514 */
515void
516GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server);
517
518/**
519 * Free resources held by this server.
520 *
521 * @param server server to destroy
522 */
523void
524GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server);
525
526
527
528
529#include "tcp_connection_legacy.c"
530#include "tcp_server_mst_legacy.c"
531#include "tcp_server_legacy.c"
532#include "tcp_service_legacy.c"
533
534GNUNET_NETWORK_STRUCT_BEGIN
535
536/**
52 * Initial handshake message for a session. 537 * Initial handshake message for a session.
53 */ 538 */
54struct WelcomeMessage 539struct WelcomeMessage
@@ -521,47 +1006,6 @@ struct Plugin
521}; 1006};
522 1007
523 1008
524/* begin of ancient copy-and-pasted code that should be
525 specialized for TCP ...*/
526/**
527 * Add the given UNIX domain path as an address to the
528 * list (as the first entry).
529 *
530 * @param saddrs array to update
531 * @param saddrlens where to store the address length
532 * @param unixpath path to add
533 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
534 * parameter is ignore on systems other than LINUX
535 */
536static void
537add_unixpath (struct sockaddr **saddrs,
538 socklen_t *saddrlens,
539 const char *unixpath,
540 int abstract)
541{
542#ifdef AF_UNIX
543 struct sockaddr_un *un;
544
545 un = GNUNET_new (struct sockaddr_un);
546 un->sun_family = AF_UNIX;
547 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
548#ifdef LINUX
549 if (GNUNET_YES == abstract)
550 un->sun_path[0] = '\0';
551#endif
552#if HAVE_SOCKADDR_UN_SUN_LEN
553 un->sun_len = (u_char) sizeof (struct sockaddr_un);
554#endif
555 *saddrs = (struct sockaddr *) un;
556 *saddrlens = sizeof (struct sockaddr_un);
557#else
558 /* this function should never be called
559 * unless AF_UNIX is defined! */
560 GNUNET_assert (0);
561#endif
562}
563
564
565/** 1009/**
566 * Get the list of addresses that a server for the given service 1010 * Get the list of addresses that a server for the given service
567 * should bind to. 1011 * should bind to.