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