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