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