-fix NPE
[oweals/gnunet.git] / src / transport / transport.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file transport/transport.h
23  * @brief common internal definitions for transport service
24  * @author Christian Grothoff
25  */
26 #ifndef TRANSPORT_H
27 #define TRANSPORT_H
28
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_time_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "gnunet_constants.h"
33
34 #define DEBUG_TRANSPORT GNUNET_EXTRA_LOGGING
35
36
37 /**
38  * For how long do we allow unused bandwidth
39  * from the past to carry over into the future? (in seconds)
40  */
41 #define MAX_BANDWIDTH_CARRY_S GNUNET_CONSTANTS_MAX_BANDWIDTH_CARRY_S
42
43 /**
44  * How often do we (at most) do a full quota
45  * recalculation? (in ms)
46  */
47 #define MIN_QUOTA_REFRESH_TIME 2000
48
49 /**
50  * What's the maximum number of sockets transport uses for validation and
51  * neighbors
52  */
53 #define DEFAULT_MAX_FDS 256
54
55 /**
56  * Maximum frequency for re-evaluating latencies for all transport addresses.
57  */
58 #define LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
59
60 /**
61  * Maximum frequency for re-evaluating latencies for connected addresses.
62  */
63 #define CONNECTED_LATENCY_EVALUATION_MAX_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
64
65 /**
66  * Similiar to GNUNET_TRANSPORT_NotifyDisconnect but in and out quotas are
67  * included here. These values are not required outside transport_api
68  *
69  * @param cls closure
70  * @param peer the peer that connected
71  * @param bandwidth_in inbound bandwidth in NBO
72  * @param bandwidth_out outbound bandwidth in NBO
73  *
74  */
75 typedef void
76 (*NotifyConnect) (void *cls,
77                   const struct GNUNET_PeerIdentity *peer,
78                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
79                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out);
80
81
82 GNUNET_NETWORK_STRUCT_BEGIN
83
84 /**
85  * Message from the transport service to the library
86  * asking to check if both processes agree about this
87  * peers identity.
88  */
89 struct StartMessage
90 {
91
92   /**
93    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_START
94    */
95   struct GNUNET_MessageHeader header;
96
97   /**
98    * 0: no options
99    * 1: The 'self' field should be checked
100    * 2: this client is interested in payload traffic
101    */
102   uint32_t options;
103
104   /**
105    * Identity we think we have.  If it does not match, the
106    * receiver should print out an error message and disconnect.
107    */
108   struct GNUNET_PeerIdentity self;
109
110 };
111
112
113 /**
114  * Message from the transport service to the library
115  * informing about neighbors.
116  */
117 struct ConnectInfoMessage
118 {
119
120   /**
121    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT
122    */
123   struct GNUNET_MessageHeader header;
124
125   /**
126    * Identity of the new neighbour.
127    */
128   struct GNUNET_PeerIdentity id;
129
130   /**
131    * Current inbound quota for this peer
132    */
133   struct GNUNET_BANDWIDTH_Value32NBO quota_in;
134
135   /**
136    * Current outbound quota for this peer
137    */
138   struct GNUNET_BANDWIDTH_Value32NBO quota_out;
139 };
140
141
142 /**
143  * Message from the transport service to the library
144  * informing about disconnects.
145  */
146 struct DisconnectInfoMessage
147 {
148
149   /**
150    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT
151    */
152   struct GNUNET_MessageHeader header;
153
154   /**
155    * Reserved, always zero.
156    */
157   uint32_t reserved GNUNET_PACKED;
158
159   /**
160    * Who got disconnected?
161    */
162   struct GNUNET_PeerIdentity peer;
163
164 };
165
166
167 /**
168  * Message type for sending a request connect message
169  * to the transport service.  Must be done before transport
170  * api will allow messages to be queued/sent to transport
171  * service for transmission to a peer.
172  */
173 struct TransportRequestConnectMessage
174 {
175   /**
176    *  Message header with type #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_CONNECT
177    */
178   struct GNUNET_MessageHeader header;
179
180   /**
181    * Reserved (0).
182    */
183   uint32_t reserved GNUNET_PACKED;
184
185   /**
186    * Identity of the peer we would like to connect to.
187    */
188   struct GNUNET_PeerIdentity peer;
189 };
190
191
192 /**
193  * Message type for sending a request connection to
194  * a peer to be torn down.
195  */
196 struct TransportRequestDisconnectMessage
197 {
198   /**
199    *  Message header with type #GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_DISCONNECT
200    */
201   struct GNUNET_MessageHeader header;
202
203   /**
204    * Reserved (0).
205    */
206   uint32_t reserved GNUNET_PACKED;
207
208   /**
209    * Identity of the peer we would like to connect to.
210    */
211   struct GNUNET_PeerIdentity peer;
212 };
213
214
215 /**
216  * Message used to set a particular bandwidth quota.  Sent TO the
217  * service to set an incoming quota, sent FROM the service to update
218  * an outgoing quota.
219  */
220 struct QuotaSetMessage
221 {
222
223   /**
224    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA
225    */
226   struct GNUNET_MessageHeader header;
227
228   /**
229    * Quota.
230    */
231   struct GNUNET_BANDWIDTH_Value32NBO quota;
232
233   /**
234    * About which peer are we talking here?
235    */
236   struct GNUNET_PeerIdentity peer;
237
238 };
239
240
241 /**
242  * Message used to notify the transport API about a message
243  * received from the network.  The actual message follows.
244  */
245 struct InboundMessage
246 {
247
248   /**
249    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_RECV
250    */
251   struct GNUNET_MessageHeader header;
252
253   /**
254    * Which peer sent the message?
255    */
256   struct GNUNET_PeerIdentity peer;
257
258 };
259
260
261 /**
262  * Message used to notify the transport API that it can
263  * send another message to the transport service.
264  */
265 struct SendOkMessage
266 {
267
268   /**
269    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK
270    */
271   struct GNUNET_MessageHeader header;
272
273   /**
274    * #GNUNET_OK if the transmission succeeded,
275    * #GNUNET_SYSERR if it failed (i.e. network disconnect);
276    * in either case, it is now OK for this client to
277    * send us another message for the given peer.
278    */
279   uint32_t success GNUNET_PACKED;
280
281   /**
282    * Size of message sent
283    */
284   uint32_t bytes_msg GNUNET_PACKED;
285
286   /**
287    * Size of message sent over wire
288    * Includes plugin and protocol specific overhead
289    */
290   uint32_t bytes_physical GNUNET_PACKED;
291
292   /**
293    * Which peer can send more now?
294    */
295   struct GNUNET_PeerIdentity peer;
296
297 };
298
299 /**
300  * Message used to notify the transport API about an address to string
301  * conversion. Message is followed by the string with the humand-readable
302  * address.  For each lookup, multiple results may be returned.  The
303  * last message must have a @e res of #GNUNET_OK and an @e addr_len
304  * of zero.
305  */
306 struct AddressToStringResultMessage
307 {
308
309   /**
310    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY
311    */
312   struct GNUNET_MessageHeader header;
313
314   /**
315    * #GNUNET_OK if the conversion succeeded,
316    * #GNUNET_SYSERR if it failed
317    */
318   uint32_t res GNUNET_PACKED;
319
320   /**
321    * Length of the following string, zero if @e is #GNUNET_SYSERR
322    */
323   uint32_t addr_len GNUNET_PACKED;
324 };
325
326
327 /**
328  * Message used to notify the transport service about a message
329  * to be transmitted to another peer.  The actual message follows.
330  */
331 struct OutboundMessage
332 {
333
334   /**
335    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND
336    */
337   struct GNUNET_MessageHeader header;
338
339   /**
340    * Always zero.
341    */
342   uint32_t reserved GNUNET_PACKED;
343
344   /**
345    * Allowed delay.
346    */
347   struct GNUNET_TIME_RelativeNBO timeout;
348
349   /**
350    * Which peer should receive the message?
351    */
352   struct GNUNET_PeerIdentity peer;
353
354 };
355
356
357 /**
358  * Message from the library to the transport service
359  * asking for converting a transport address to a
360  * human-readable UTF-8 string.
361  */
362 struct AddressLookupMessage
363 {
364
365   /**
366    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING
367    */
368   struct GNUNET_MessageHeader header;
369
370   /**
371    * Should the conversion use numeric IP addresses (otherwise
372    * a reverse DNS lookup is OK -- if applicable).
373    */
374   int16_t numeric_only GNUNET_PACKED;
375
376   /**
377    * Length of the (binary) address in bytes, in big-endian.
378    */
379   uint16_t addrlen GNUNET_PACKED;
380
381   /**
382    * timeout to give up (for DNS resolution timeout mostly)
383    */
384   struct GNUNET_TIME_RelativeNBO timeout;
385
386   /* followed by @e addrlen bytes of the actual address, then
387    * followed by the 0-terminated name of the transport */
388 };
389
390
391 /**
392  * Message from the transport service to the library containing information
393  * about a peer. Information contained are:
394  * - current address used to communicate with this peer
395  * - state
396  * - state timeout
397  *
398  * Memory layout:
399  * [AddressIterateResponseMessage][address[addrlen]][transportname[pluginlen]]
400  */
401 struct ValidationIterateResponseMessage
402 {
403   /**
404    * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE
405    */
406   struct GNUNET_MessageHeader header;
407
408   /**
409    * For alignment.
410    */
411   uint32_t reserved;
412
413   /**
414    * Peer identity
415    */
416   struct GNUNET_PeerIdentity peer;
417
418   /**
419    * Local info about the address
420    */
421   uint32_t local_address_info GNUNET_PACKED;
422
423   /**
424    * Address length
425    */
426   uint32_t addrlen GNUNET_PACKED;
427
428   /**
429    * Length of the plugin name
430    */
431   uint32_t pluginlen GNUNET_PACKED;
432
433   /**
434    * State
435    */
436   uint32_t state GNUNET_PACKED;
437
438   /**
439    * At what time did we successfully validate the address last.
440    * Will be NEVER if the address failed validation.
441    */
442   struct GNUNET_TIME_AbsoluteNBO last_validation;
443
444   /**
445    * Until when is the address believed to be valid.
446    * Will be ZERO if the address is not belived to be valid.
447    */
448   struct GNUNET_TIME_AbsoluteNBO valid_until;
449
450   /**
451    * When will we next try to validate the address (typically
452    * done before @e valid_until happens).
453    */
454   struct GNUNET_TIME_AbsoluteNBO next_validation;
455 };
456
457 /**
458  * Message from the library to the transport service
459  * asking for binary addresses known for a peer.
460  */
461 struct ValidationMonitorMessage
462 {
463   /**
464    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST
465    */
466   struct GNUNET_MessageHeader header;
467
468   /**
469    * One shot call or continous replies?
470    */
471   uint32_t one_shot GNUNET_PACKED;
472
473   /**
474    * The identity of the peer to look up.
475    */
476   struct GNUNET_PeerIdentity peer;
477
478 };
479
480
481 /**
482  * Message from the library to the transport service
483  * asking for binary addresses known for a peer.
484  */
485 struct PeerMonitorMessage
486 {
487   /**
488    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST
489    */
490   struct GNUNET_MessageHeader header;
491
492   /**
493    * One shot call or continous replies?
494    */
495   uint32_t one_shot GNUNET_PACKED;
496
497   /**
498    * The identity of the peer to look up.
499    */
500   struct GNUNET_PeerIdentity peer;
501
502 };
503
504
505 /**
506  * Message from the library to the transport service
507  * asking for binary addresses known for a peer.
508  */
509 struct TrafficMetricMessage
510 {
511   /**
512    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC
513    */
514   struct GNUNET_MessageHeader header;
515
516   /**
517    * Always zero.
518    */
519   uint32_t reserved GNUNET_PACKED;
520
521   /**
522    * The identity of the peer to look up.
523    */
524   struct GNUNET_PeerIdentity peer;
525
526   /**
527    * Fake properties to generate.
528    */
529   struct GNUNET_ATS_PropertiesNBO properties;
530
531   /**
532    * Fake delay to add on inbound traffic.
533    */
534   struct GNUNET_TIME_RelativeNBO delay_in;
535
536   /**
537    * Fake delay to add on outbound traffic.
538    */
539   struct GNUNET_TIME_RelativeNBO delay_out;
540 };
541
542
543 /**
544  * Message from the transport service to the library containing information
545  * about a peer. Information contained are:
546  * - current address used to communicate with this peer
547  * - state
548  * - state timeout
549  *
550  * Memory layout:
551  * [AddressIterateResponseMessage][address[addrlen]][transportname[pluginlen]]
552  */
553 struct PeerIterateResponseMessage
554 {
555   /**
556    * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE
557    */
558   struct GNUNET_MessageHeader header;
559
560   /**
561    * For alignment.
562    */
563   uint32_t reserved;
564
565   /**
566    * Peer identity
567    */
568   struct GNUNET_PeerIdentity peer;
569
570   /**
571    * Timeout for the state this peer is in
572    */
573   struct GNUNET_TIME_AbsoluteNBO state_timeout;
574
575   /**
576    * Local info about the address
577    */
578   uint32_t local_address_info GNUNET_PACKED;
579
580   /**
581    * State this peer is in as an `enum GNUNET_TRANSPORT_PeerState`
582    */
583   uint32_t state GNUNET_PACKED;
584
585   /**
586    * Address length
587    */
588   uint32_t addrlen GNUNET_PACKED;
589
590   /**
591    * Length of the plugin name
592    */
593   uint32_t pluginlen GNUNET_PACKED;
594
595 };
596
597
598 /**
599  * Change in blacklisting (either request or notification,
600  * depending on which direction it is going).
601  */
602 struct BlacklistMessage
603 {
604
605   /**
606    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY or
607    * #GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY.
608    */
609   struct GNUNET_MessageHeader header;
610
611   /**
612    * 0 for the query, #GNUNET_OK (allowed) or #GNUNET_SYSERR (disallowed)
613    * for the response.
614    */
615   uint32_t is_allowed GNUNET_PACKED;
616
617   /**
618    * Which peer is being blacklisted or queried?
619    */
620   struct GNUNET_PeerIdentity peer;
621
622 };
623
624
625 /**
626  * Transport-level connection status update.
627  */
628 struct TransportPluginMonitorMessage
629 {
630
631   /**
632    * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT.
633    */
634   struct GNUNET_MessageHeader header;
635
636   /**
637    * An `enum GNUNET_TRANSPORT_SessionState` in NBO.
638    */
639   uint16_t session_state GNUNET_PACKED;
640
641   /**
642    * #GNUNET_YES if this is an inbound connection,
643    * #GNUNET_NO if this is an outbound connection,
644    * #GNUNET_SYSERR if connections of this plugin
645    *             are so fundamentally bidirectional
646    *             that they have no 'initiator'
647    * Value given in NBO.
648    */
649   int16_t is_inbound GNUNET_PACKED;
650
651   /**
652    * Number of messages waiting transmission.
653    */
654   uint32_t msgs_pending GNUNET_PACKED;
655
656   /**
657    * Number of bytes waiting for transmission.
658    */
659   uint32_t bytes_pending GNUNET_PACKED;
660
661   /**
662    * When will this transport plugin session time out?
663    */
664   struct GNUNET_TIME_AbsoluteNBO timeout;
665
666   /**
667    * Until how long is this plugin currently blocked from reading?
668    */
669   struct GNUNET_TIME_AbsoluteNBO delay;
670
671   /**
672    * Which peer is this connection for?
673    */
674   struct GNUNET_PeerIdentity peer;
675
676   /**
677    * Unique identifier for the session.
678    */
679   uint64_t session_id;
680
681   /**
682    * Length of the plugin name in bytes, including 0-termination.
683    */
684   uint16_t plugin_name_len GNUNET_PACKED;
685
686   /**
687    * Length of the plugin address in bytes.
688    */
689   uint16_t plugin_address_len GNUNET_PACKED;
690
691   /* followed by 0-terminated plugin name and
692      @e plugin_address_len bytes of plugin address */
693
694 };
695
696
697 GNUNET_NETWORK_STRUCT_END
698
699 /* end of transport.h */
700 #endif