-move IPv4 header initialization to tun library
[oweals/gnunet.git] / src / tun / tun.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2011, 2012 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file tun/tun.
23  * @brief standard IP calculations for TUN interaction
24  * @author Philipp Toelke
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_tun_lib.h"
29
30 /**
31  * IP TTL we use for packets that we assemble (8 bit unsigned integer)
32  */
33 #define FRESH_TTL 255
34
35 /**
36  * Initialize an IPv4 header.
37  *
38  * @param ip header to initialize
39  * @param protocol protocol to use (i.e. IPPROTO_UDP)
40  * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
41  * @param src source IP address to use
42  * @param dst destination IP address to use
43  */
44 void
45 GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
46                                    uint8_t protocol,
47                                    uint16_t payload_length,
48                                    const struct in_addr *src,
49                                    const struct in_addr *dst)
50 {
51   ip->header_length =  sizeof (struct GNUNET_TUN_IPv4Header) / 4;
52   ip->version = 4;
53   ip->diff_serv = 0;
54   ip->total_length = htons (sizeof (struct GNUNET_TUN_IPv4Header) + payload_length);
55   ip->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
56                                                             65536);
57   ip->flags = 0;
58   ip->fragmentation_offset = 0;
59   ip->ttl = FRESH_TTL;
60   ip->protocol = protocol;
61   ip->checksum = 0;
62   ip->source_address = *src;
63   ip->destination_address = *dst;
64   ip->checksum = GNUNET_CRYPTO_crc16_n (ip, sizeof (struct GNUNET_TUN_IPv4Header));
65 }
66
67
68
69 /* end of tun.c */