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