2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 Christian Grothoff
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.
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.
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.
23 * @brief standard IP calculations for TUN interaction
24 * @author Philipp Toelke
25 * @author Christian Grothoff
28 #include "gnunet_tun_lib.h"
31 * IP TTL we use for packets that we assemble (8 bit unsigned integer)
36 * Initialize an IPv4 header.
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
45 GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
47 uint16_t payload_length,
48 const struct in_addr *src,
49 const struct in_addr *dst)
51 ip->header_length = sizeof (struct GNUNET_TUN_IPv4Header) / 4;
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,
58 ip->fragmentation_offset = 0;
60 ip->protocol = protocol;
62 ip->source_address = *src;
63 ip->destination_address = *dst;
64 ip->checksum = GNUNET_CRYPTO_crc16_n (ip, sizeof (struct GNUNET_TUN_IPv4Header));