implement MTU calculation and adjustments
[oweals/gnunet.git] / src / include / gnunet_tun_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2013 Christian Grothoff
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @author Philipp Toelke
23  * @author Christian Grothoff
24  *
25  * @file
26  * Standard TCP/IP network structs and IP checksum calculations for TUN interaction
27  *
28  * @defgroup tun  TUN library
29  * Standard TCP/IP network structs and IP checksum calculations for TUN interaction
30  * @{
31  */
32 #ifndef GNUNET_TUN_LIB_H
33 #define GNUNET_TUN_LIB_H
34
35 #include "gnunet_common.h"
36 #include "gnunet_crypto_lib.h"
37
38
39 /* see http://www.iana.org/assignments/ethernet-numbers */
40 #ifndef ETH_P_IPV4
41 /**
42  * Number for IPv4
43  */
44 #define ETH_P_IPV4 0x0800
45 #endif
46
47 #ifndef ETH_P_IPV6
48 /**
49  * Number for IPv6
50  */
51 #define ETH_P_IPV6 0x86DD
52 #endif
53
54
55 /**
56  * Maximum regex string length for use with #GNUNET_TUN_ipv4toregexsearch.
57  *
58  * 8 bytes for IPv4, 4 bytes for port, 1 byte for "4", 2 bytes for "-",
59  * one byte for 0-termination.
60  */
61
62 #define GNUNET_TUN_IPV4_REGEXLEN 16
63
64
65 /**
66  * Maximum regex string length for use with #GNUNET_TUN_ipv6toregexsearch
67  *
68  * 32 bytes for IPv4, 4 bytes for port, 1 byte for "4", 2 bytes for "-",
69  * one byte for 0-termination.
70  */
71 #define GNUNET_TUN_IPV6_REGEXLEN 40
72
73
74 GNUNET_NETWORK_STRUCT_BEGIN
75
76 /**
77  * Header from Linux TUN interface.
78  */
79 struct GNUNET_TUN_Layer2PacketHeader
80 {
81   /**
82    * Some flags (unused).
83    */
84   uint16_t flags GNUNET_PACKED;
85
86   /**
87    * Here we get an ETH_P_-number.
88    */
89   uint16_t proto GNUNET_PACKED;
90 };
91
92
93 /**
94  * Standard IPv4 header.
95  */
96 struct GNUNET_TUN_IPv4Header
97 {
98 #if __BYTE_ORDER == __LITTLE_ENDIAN
99   unsigned int header_length:4 GNUNET_PACKED;
100   unsigned int version:4 GNUNET_PACKED;
101 #elif __BYTE_ORDER == __BIG_ENDIAN
102   unsigned int version:4 GNUNET_PACKED;
103   unsigned int header_length:4 GNUNET_PACKED;
104 #else
105   #error byteorder undefined
106 #endif
107   uint8_t diff_serv;
108
109   /**
110    * Length of the packet, including this header.
111    */
112   uint16_t total_length GNUNET_PACKED;
113
114   /**
115    * Unique random ID for matching up fragments.
116    */
117   uint16_t identification GNUNET_PACKED;
118
119   unsigned int flags:3 GNUNET_PACKED;
120
121   unsigned int fragmentation_offset:13 GNUNET_PACKED;
122
123   /**
124    * How many more hops can this packet be forwarded?
125    */
126   uint8_t ttl;
127
128   /**
129    * L4-protocol, for example, IPPROTO_UDP or IPPROTO_TCP.
130    */
131   uint8_t protocol;
132
133   /**
134    * Checksum.
135    */
136   uint16_t checksum GNUNET_PACKED;
137
138   /**
139    * Origin of the packet.
140    */
141   struct in_addr source_address GNUNET_PACKED;
142
143   /**
144    * Destination of the packet.
145    */
146   struct in_addr destination_address GNUNET_PACKED;
147 } GNUNET_GCC_STRUCT_LAYOUT;
148
149
150 /**
151  * Standard IPv6 header.
152  */
153 struct GNUNET_TUN_IPv6Header
154 {
155 #if __BYTE_ORDER == __LITTLE_ENDIAN
156   unsigned int traffic_class_h:4 GNUNET_PACKED;
157   unsigned int version:4 GNUNET_PACKED;
158   unsigned int traffic_class_l:4 GNUNET_PACKED;
159   unsigned int flow_label:20 GNUNET_PACKED;
160 #elif __BYTE_ORDER == __BIG_ENDIAN
161   unsigned int version:4 GNUNET_PACKED;
162   unsigned int traffic_class:8 GNUNET_PACKED;
163   unsigned int flow_label:20 GNUNET_PACKED;
164 #else
165   #error byteorder undefined
166 #endif
167   /**
168    * Length of the payload, excluding this header.
169    */
170   uint16_t payload_length GNUNET_PACKED;
171
172   /**
173    * For example, IPPROTO_UDP or IPPROTO_TCP.
174    */
175   uint8_t next_header;
176
177   /**
178    * How many more hops can this packet be forwarded?
179    */
180   uint8_t hop_limit;
181
182   /**
183    * Origin of the packet.
184    */
185   struct in6_addr source_address GNUNET_PACKED;
186
187   /**
188    * Destination of the packet.
189    */
190   struct in6_addr destination_address GNUNET_PACKED;
191 } GNUNET_GCC_STRUCT_LAYOUT;
192
193
194 /**
195  * TCP flags.
196  */
197 #define GNUNET_TUN_TCP_FLAGS_FIN 1
198 #define GNUNET_TUN_TCP_FLAGS_SYN 2
199 #define GNUNET_TUN_TCP_FLAGS_RST 4
200 #define GNUNET_TUN_TCP_FLAGS_PSH 8
201 #define GNUNET_TUN_TCP_FLAGS_ACK 16
202 #define GNUNET_TUN_TCP_FLAGS_URG 32
203 #define GNUNET_TUN_TCP_FLAGS_ECE 64
204 #define GNUNET_TUN_TCP_FLAGS_CWR 128
205
206 /**
207  * TCP packet header.
208  */
209 struct GNUNET_TUN_TcpHeader
210 {
211   /**
212    * Source port (in NBO).
213    */
214   uint16_t source_port GNUNET_PACKED;
215
216   /**
217    * Destination port (in NBO).
218    */
219   uint16_t destination_port GNUNET_PACKED;
220
221   /**
222    * Sequence number.
223    */
224   uint32_t seq GNUNET_PACKED;
225
226   /**
227    * Acknowledgement number.
228    */
229   uint32_t ack GNUNET_PACKED;
230 #if __BYTE_ORDER == __LITTLE_ENDIAN
231   /**
232    * Reserved.  Must be zero.
233    */
234   unsigned int reserved : 4 GNUNET_PACKED;
235   /**
236    * Number of 32-bit words in TCP header.
237    */
238   unsigned int off : 4 GNUNET_PACKED;
239 #elif __BYTE_ORDER == __BIG_ENDIAN
240   /**
241    * Number of 32-bit words in TCP header.
242    */
243   unsigned int off : 4 GNUNET_PACKED;
244   /**
245    * Reserved.  Must be zero.
246    */
247   unsigned int reserved : 4 GNUNET_PACKED;
248 #else
249   #error byteorder undefined
250 #endif
251
252   /**
253    * Flags (SYN, FIN, ACK, etc.)
254    */
255   uint8_t flags;
256
257   /**
258    * Window size.
259    */
260   uint16_t window_size GNUNET_PACKED;
261
262   /**
263    * Checksum.
264    */
265   uint16_t crc GNUNET_PACKED;
266
267   /**
268    * Urgent pointer.
269    */
270   uint16_t urgent_pointer GNUNET_PACKED;
271 } GNUNET_GCC_STRUCT_LAYOUT;
272
273
274 /**
275  * UDP packet header.
276  */
277 struct GNUNET_TUN_UdpHeader
278 {
279   /**
280    * Source port (in NBO).
281    */
282   uint16_t source_port GNUNET_PACKED;
283
284   /**
285    * Destination port (in NBO).
286    */
287   uint16_t destination_port GNUNET_PACKED;
288
289   /**
290    * Number of bytes of payload.
291    */
292   uint16_t len GNUNET_PACKED;
293
294   /**
295    * Checksum.
296    */
297   uint16_t crc GNUNET_PACKED;
298 };
299
300
301
302 /**
303  * A few common DNS classes (ok, only one is common, but I list a
304  * couple more to make it clear what we're talking about here).
305  */
306 #define GNUNET_TUN_DNS_CLASS_INTERNET 1
307 #define GNUNET_TUN_DNS_CLASS_CHAOS 3
308 #define GNUNET_TUN_DNS_CLASS_HESIOD 4
309
310 #define GNUNET_TUN_DNS_OPCODE_QUERY 0
311 #define GNUNET_TUN_DNS_OPCODE_INVERSE_QUERY 1
312 #define GNUNET_TUN_DNS_OPCODE_STATUS 2
313
314
315 /**
316  * RFC 1035 codes.
317  */
318 #define GNUNET_TUN_DNS_RETURN_CODE_NO_ERROR 0
319 #define GNUNET_TUN_DNS_RETURN_CODE_FORMAT_ERROR 1
320 #define GNUNET_TUN_DNS_RETURN_CODE_SERVER_FAILURE 2
321 #define GNUNET_TUN_DNS_RETURN_CODE_NAME_ERROR 3
322 #define GNUNET_TUN_DNS_RETURN_CODE_NOT_IMPLEMENTED 4
323 #define GNUNET_TUN_DNS_RETURN_CODE_REFUSED 5
324
325 /**
326  * RFC 2136 codes
327  */
328 #define GNUNET_TUN_DNS_RETURN_CODE_YXDOMAIN 6
329 #define GNUNET_TUN_DNS_RETURN_CODE_YXRRSET 7
330 #define GNUNET_TUN_DNS_RETURN_CODE_NXRRSET 8
331 #define GNUNET_TUN_DNS_RETURN_CODE_NOT_AUTH 9
332 #define GNUNET_TUN_DNS_RETURN_CODE_NOT_ZONE 10
333
334
335 /**
336  * DNS flags (largely RFC 1035 / RFC 2136).
337  */
338 struct GNUNET_TUN_DnsFlags
339 {
340 #if __BYTE_ORDER == __LITTLE_ENDIAN
341   /**
342    * Set to 1 if recursion is desired (client -> server)
343    */
344   unsigned int recursion_desired    : 1 GNUNET_PACKED;
345
346   /**
347    * Set to 1 if message is truncated
348    */
349   unsigned int message_truncated    : 1 GNUNET_PACKED;
350
351   /**
352    * Set to 1 if this is an authoritative answer
353    */
354   unsigned int authoritative_answer : 1 GNUNET_PACKED;
355
356   /**
357    * See GNUNET_TUN_DNS_OPCODE_ defines.
358    */
359   unsigned int opcode               : 4 GNUNET_PACKED;
360
361   /**
362    * query:0, response:1
363    */
364   unsigned int query_or_response    : 1 GNUNET_PACKED;
365
366   /**
367    * See GNUNET_TUN_DNS_RETURN_CODE_ defines.
368    */
369   unsigned int return_code          : 4 GNUNET_PACKED;
370
371   /**
372    * See RFC 4035.
373    */
374   unsigned int checking_disabled    : 1 GNUNET_PACKED;
375
376   /**
377    * Response has been cryptographically verified, RFC 4035.
378    */
379   unsigned int authenticated_data   : 1 GNUNET_PACKED;
380
381   /**
382    * Always zero.
383    */
384   unsigned int zero                 : 1 GNUNET_PACKED;
385
386   /**
387    * Set to 1 if recursion is available (server -> client)
388    */
389   unsigned int recursion_available  : 1 GNUNET_PACKED;
390 #elif __BYTE_ORDER == __BIG_ENDIAN
391
392   /**
393    * query:0, response:1
394    */
395   unsigned int query_or_response    : 1 GNUNET_PACKED;
396
397   /**
398    * See GNUNET_TUN_DNS_OPCODE_ defines.
399    */
400   unsigned int opcode               : 4 GNUNET_PACKED;
401
402   /**
403    * Set to 1 if this is an authoritative answer
404    */
405   unsigned int authoritative_answer : 1 GNUNET_PACKED;
406
407   /**
408    * Set to 1 if message is truncated
409    */
410   unsigned int message_truncated    : 1 GNUNET_PACKED;
411
412   /**
413    * Set to 1 if recursion is desired (client -> server)
414    */
415   unsigned int recursion_desired    : 1 GNUNET_PACKED;
416
417
418   /**
419    * Set to 1 if recursion is available (server -> client)
420    */
421   unsigned int recursion_available  : 1 GNUNET_PACKED;
422
423   /**
424    * Always zero.
425    */
426   unsigned int zero                 : 1 GNUNET_PACKED;
427
428   /**
429    * Response has been cryptographically verified, RFC 4035.
430    */
431   unsigned int authenticated_data   : 1 GNUNET_PACKED;
432
433   /**
434    * See RFC 4035.
435    */
436   unsigned int checking_disabled    : 1 GNUNET_PACKED;
437
438   /**
439    * See GNUNET_TUN_DNS_RETURN_CODE_ defines.
440    */
441   unsigned int return_code          : 4 GNUNET_PACKED;
442 #else
443   #error byteorder undefined
444 #endif
445
446 } GNUNET_GCC_STRUCT_LAYOUT;
447
448
449
450 /**
451  * DNS header.
452  */
453 struct GNUNET_TUN_DnsHeader
454 {
455   /**
456    * Unique identifier for the request/response.
457    */
458   uint16_t id GNUNET_PACKED;
459
460   /**
461    * Flags.
462    */
463   struct GNUNET_TUN_DnsFlags flags;
464
465   /**
466    * Number of queries.
467    */
468   uint16_t query_count GNUNET_PACKED;
469
470   /**
471    * Number of answers.
472    */
473   uint16_t answer_rcount GNUNET_PACKED;
474
475   /**
476    * Number of authoritative answers.
477    */
478   uint16_t authority_rcount GNUNET_PACKED;
479
480   /**
481    * Number of additional records.
482    */
483   uint16_t additional_rcount GNUNET_PACKED;
484 };
485
486
487 /**
488  * Payload of DNS SOA record (header).
489  */
490 struct GNUNET_TUN_DnsSoaRecord
491 {
492   /**
493    * The version number of the original copy of the zone.   (NBO)
494    */
495   uint32_t serial GNUNET_PACKED;
496
497   /**
498    * Time interval before the zone should be refreshed. (NBO)
499    */
500   uint32_t refresh GNUNET_PACKED;
501
502   /**
503    * Time interval that should elapse before a failed refresh should
504    * be retried. (NBO)
505    */
506   uint32_t retry GNUNET_PACKED;
507
508   /**
509    * Time value that specifies the upper limit on the time interval
510    * that can elapse before the zone is no longer authoritative. (NBO)
511    */
512   uint32_t expire GNUNET_PACKED;
513
514   /**
515    * The bit minimum TTL field that should be exported with any RR
516    * from this zone. (NBO)
517    */
518   uint32_t minimum GNUNET_PACKED;
519 };
520
521
522 /**
523  * Payload of DNS SRV record (header).
524  */
525 struct GNUNET_TUN_DnsSrvRecord
526 {
527
528   /**
529    * Preference for this entry (lower value is higher preference).  Clients
530    * will contact hosts from the lowest-priority group first and fall back
531    * to higher priorities if the low-priority entries are unavailable. (NBO)
532    */
533   uint16_t prio GNUNET_PACKED;
534
535   /**
536    * Relative weight for records with the same priority.  Clients will use
537    * the hosts of the same (lowest) priority with a probability proportional
538    * to the weight given. (NBO)
539    */
540   uint16_t weight GNUNET_PACKED;
541
542   /**
543    * TCP or UDP port of the service. (NBO)
544    */
545   uint16_t port GNUNET_PACKED;
546
547   /* followed by 'target' name */
548 };
549
550
551 /**
552  * Payload of DNS CERT record.
553  */
554 struct GNUNET_TUN_DnsCertRecord
555 {
556
557   /**
558    * Certificate type
559    */
560   uint16_t cert_type;
561
562   /**
563    * Certificate KeyTag
564    */
565   uint16_t cert_tag;
566
567   /**
568    * Algorithm
569    */
570   uint8_t algorithm;
571
572   /* Followed by the certificate */
573 };
574
575
576 /**
577  * Payload of DNSSEC TLSA record.
578  * http://datatracker.ietf.org/doc/draft-ietf-dane-protocol/
579  */
580 struct GNUNET_TUN_DnsTlsaRecord
581 {
582
583   /**
584    * Certificate usage
585    * 0: CA cert
586    * 1: Entity cert
587    * 2: Trust anchor
588    * 3: domain-issued cert
589    */
590   uint8_t usage;
591
592   /**
593    * Selector
594    * What part will be matched against the cert
595    * presented by server
596    * 0: Full cert (in binary)
597    * 1: Full cert (in DER)
598    */
599   uint8_t selector;
600
601   /**
602    * Matching type (of selected content)
603    * 0: exact match
604    * 1: SHA-256 hash
605    * 2: SHA-512 hash
606    */
607   uint8_t matching_type;
608
609   /**
610    * followed by certificate association data
611    * The "certificate association data" to be matched.
612    * These bytes are either raw data (that is, the full certificate or
613    * its SubjectPublicKeyInfo, depending on the selector) for matching
614    * type 0, or the hash of the raw data for matching types 1 and 2.
615    * The data refers to the certificate in the association, not to the
616    * TLS ASN.1 Certificate object.
617    *
618    * The data is represented as a string of hex chars
619    */
620 };
621
622
623 /**
624  * Payload of GNS VPN record
625  */
626 struct GNUNET_TUN_GnsVpnRecord
627 {
628   /**
629    * The peer to contact
630    */
631   struct GNUNET_PeerIdentity peer;
632
633   /**
634    * The protocol to use
635    */
636   uint16_t proto;
637
638   /* followed by the servicename */
639 };
640
641
642 /**
643  * DNS query prefix.
644  */
645 struct GNUNET_TUN_DnsQueryLine
646 {
647   /**
648    * Desired type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
649    */
650   uint16_t type GNUNET_PACKED;
651
652   /**
653    * Desired class (usually GNUNET_TUN_DNS_CLASS_INTERNET). (NBO)
654    */
655   uint16_t dns_traffic_class GNUNET_PACKED;
656 };
657
658
659 /**
660  * General DNS record prefix.
661  */
662 struct GNUNET_TUN_DnsRecordLine
663 {
664   /**
665    * Record type (GNUNET_DNSPARSER_TYPE_XXX). (NBO)
666    */
667   uint16_t type GNUNET_PACKED;
668
669   /**
670    * Record class (usually GNUNET_TUN_DNS_CLASS_INTERNET). (NBO)
671    */
672   uint16_t dns_traffic_class GNUNET_PACKED;
673
674   /**
675    * Expiration for the record (in seconds). (NBO)
676    */
677   uint32_t ttl GNUNET_PACKED;
678
679   /**
680    * Number of bytes of data that follow. (NBO)
681    */
682   uint16_t data_len GNUNET_PACKED;
683 };
684
685
686 #define GNUNET_TUN_ICMPTYPE_ECHO_REPLY 0
687 #define GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE 3
688 #define GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH 4
689 #define GNUNET_TUN_ICMPTYPE_REDIRECT_MESSAGE 5
690 #define GNUNET_TUN_ICMPTYPE_ECHO_REQUEST 8
691 #define GNUNET_TUN_ICMPTYPE_ROUTER_ADVERTISEMENT 9
692 #define GNUNET_TUN_ICMPTYPE_ROUTER_SOLICITATION 10
693 #define GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED 11
694
695 #define GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE 1
696 #define GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG 2
697 #define GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED 3
698 #define GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM 4
699 #define GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST 128
700 #define GNUNET_TUN_ICMPTYPE6_ECHO_REPLY 129
701
702
703 /**
704  * ICMP header.
705  */
706 struct GNUNET_TUN_IcmpHeader
707 {
708   uint8_t type;
709   uint8_t code;
710   uint16_t crc GNUNET_PACKED;
711
712   union
713   {
714     /**
715      * ICMP Echo (request/reply)
716      */
717     struct
718     {
719       uint16_t  identifier GNUNET_PACKED;
720       uint16_t  sequence_number GNUNET_PACKED;
721     } echo;
722
723     /**
724      * ICMP Destination Unreachable (RFC 1191)
725      */
726     struct ih_pmtu
727     {
728       uint16_t empty GNUNET_PACKED;
729       uint16_t next_hop_mtu GNUNET_PACKED;
730       /* followed by original IP header + first 8 bytes of original IP datagram */
731     } destination_unreachable;
732
733     /**
734      * ICMP Redirect
735      */
736     struct in_addr redirect_gateway_address GNUNET_PACKED;
737
738     /**
739      * MTU for packets that are too big (IPv6).
740      */
741     uint32_t packet_too_big_mtu GNUNET_PACKED;
742
743   } quench;
744
745 };
746
747
748 GNUNET_NETWORK_STRUCT_END
749
750
751 /**
752  * Initialize an IPv4 header.
753  *
754  * @param ip header to initialize
755  * @param protocol protocol to use (i.e. IPPROTO_UDP)
756  * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
757  * @param src source IP address to use
758  * @param dst destination IP address to use
759  */
760 void
761 GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
762                                    uint8_t protocol,
763                                    uint16_t payload_length,
764                                    const struct in_addr *src,
765                                    const struct in_addr *dst);
766
767
768 /**
769  * Initialize an IPv6 header.
770  *
771  * @param ip header to initialize
772  * @param protocol protocol to use (i.e. IPPROTO_UDP)
773  * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
774  * @param src source IP address to use
775  * @param dst destination IP address to use
776  */
777 void
778 GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
779                                    uint8_t protocol,
780                                    uint16_t payload_length,
781                                    const struct in6_addr *src,
782                                    const struct in6_addr *dst);
783
784 /**
785  * Calculate IPv4 TCP checksum.
786  *
787  * @param ip ipv4 header fully initialized
788  * @param tcp TCP header (initialized except for CRC)
789  * @param payload the TCP payload
790  * @param payload_length number of bytes of TCP @a payload
791  */
792 void
793 GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
794                                     struct GNUNET_TUN_TcpHeader *tcp,
795                                     const void *payload,
796                                     uint16_t payload_length);
797
798 /**
799  * Calculate IPv6 TCP checksum.
800  *
801  * @param ip ipv6 header fully initialized
802  * @param tcp TCP header (initialized except for CRC)
803  * @param payload the TCP payload
804  * @param payload_length number of bytes of TCP payload
805  */
806 void
807 GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
808                                     struct GNUNET_TUN_TcpHeader *tcp,
809                                     const void *payload,
810                                     uint16_t payload_length);
811
812 /**
813  * Calculate IPv4 UDP checksum.
814  *
815  * @param ip ipv4 header fully initialized
816  * @param udp UDP header (initialized except for CRC)
817  * @param payload the UDP payload
818  * @param payload_length number of bytes of UDP @a payload
819  */
820 void
821 GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
822                                     struct GNUNET_TUN_UdpHeader *udp,
823                                     const void *payload,
824                                     uint16_t payload_length);
825
826
827 /**
828  * Calculate IPv6 UDP checksum.
829  *
830  * @param ip ipv6 header fully initialized
831  * @param udp UDP header (initialized except for CRC)
832  * @param payload the UDP payload
833  * @param payload_length number of bytes of @a payload
834  */
835 void
836 GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
837                                     struct GNUNET_TUN_UdpHeader *udp,
838                                     const void *payload,
839                                     uint16_t payload_length);
840
841
842 /**
843  * Calculate ICMP checksum.
844  *
845  * @param icmp IMCP header (initialized except for CRC)
846  * @param payload the ICMP payload
847  * @param payload_length number of bytes of @a payload
848  */
849 void
850 GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp,
851                                     const void *payload,
852                                     uint16_t payload_length);
853
854
855 /**
856  * Create a regex in @a rxstr from the given @a ip and @a port.
857  *
858  * @param ip IPv4 representation.
859  * @param port destination port
860  * @param rxstr generated regex, must be at least #GNUNET_TUN_IPV4_REGEXLEN
861  *              bytes long.
862  */
863 void
864 GNUNET_TUN_ipv4toregexsearch (const struct in_addr *ip,
865                               uint16_t port,
866                               char *rxstr);
867
868
869 /**
870  * Create a regex in @a rxstr from the given @a ipv6 and @a port.
871  *
872  * @param ipv6 IPv6 representation.
873  * @param port destination port
874  * @param rxstr generated regex, must be at least #GNUNET_TUN_IPV6_REGEXLEN
875  *              bytes long.
876  */
877 void
878 GNUNET_TUN_ipv6toregexsearch (const struct in6_addr *ipv6,
879                               uint16_t port,
880                               char *rxstr);
881
882
883 /**
884  * Convert an exit policy to a regular expression.  The exit policy
885  * specifies a set of subnets this peer is willing to serve as an
886  * exit for; the resulting regular expression will match the
887  * IPv6 address strings as returned by #GNUNET_TUN_ipv6toregexsearch.
888  *
889  * @param policy exit policy specification
890  * @return regular expression, NULL on error
891  */
892 char *
893 GNUNET_TUN_ipv6policy2regex (const char *policy);
894
895
896 /**
897  * Convert an exit policy to a regular expression.  The exit policy
898  * specifies a set of subnets this peer is willing to serve as an
899  * exit for; the resulting regular expression will match the
900  * IPv4 address strings as returned by #GNUNET_TUN_ipv4toregexsearch.
901  *
902  * @param policy exit policy specification
903  * @return regular expression, NULL on error
904  */
905 char *
906 GNUNET_TUN_ipv4policy2regex (const char *policy);
907
908
909 /**
910  * Hash the service name of a hosted service to the
911  * hash code that is used to identify the service on
912  * the network.
913  *
914  * @param service_name a string
915  * @param[out] hc corresponding hash
916  */
917 void
918 GNUNET_TUN_service_name_to_hash (const char *service_name,
919                                  struct GNUNET_HashCode *hc);
920
921
922 /**
923  * Check if two sockaddrs are equal.
924  *
925  * @param sa one address
926  * @param sb another address
927  * @param include_port also check ports
928  * @return #GNUNET_YES if they are equal
929  */
930 int
931 GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa,
932                          const struct sockaddr *sb,
933                          int include_port);
934
935
936 /**
937  * Compute the CADET port given a service descriptor
938  * (returned from #GNUNET_TUN_service_name_to_hash) and
939  * a TCP/UDP port @a ip_port.
940  *
941  * @param desc service shared secret
942  * @param ip_port TCP/UDP port, use 0 for ICMP
943  * @param[out] cadet_port CADET port to use
944  */
945 void
946 GNUNET_TUN_compute_service_cadet_port (const struct GNUNET_HashCode *desc,
947                                        uint16_t ip_port,
948                                        struct GNUNET_HashCode *cadet_port);
949
950 #endif
951
952 /** @} */  /* end of group */