Add check for __Static_assert() and asserts on struct sizes
[tinc] / src / ipv4.h
1 #ifndef TINC_IPV4_H
2 #define TINC_IPV4_H
3
4 /*
5     ipv4.h -- missing IPv4 related definitions
6     Copyright (C) 2005 Ivo Timmermans
7                   2006-2012 Guus Sliepen <guus@tinc-vpn.org>
8
9     This program is free software; you can redistribute it and/or modify
10     it under the terms of the GNU General Public License as published by
11     the Free Software Foundation; either version 2 of the License, or
12     (at your option) any later version.
13
14     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18
19     You should have received a copy of the GNU General Public License along
20     with this program; if not, write to the Free Software Foundation, Inc.,
21     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include "system.h"
25
26 #ifndef AF_INET
27 #define AF_INET 2
28 #endif
29
30 #ifndef IPPROTO_ICMP
31 #define IPPROTO_ICMP 1
32 #endif
33
34 #ifndef ICMP_DEST_UNREACH
35 #define ICMP_DEST_UNREACH 3
36 #endif
37
38 #ifndef ICMP_FRAG_NEEDED
39 #define ICMP_FRAG_NEEDED 4
40 #endif
41
42 #ifndef ICMP_NET_UNKNOWN
43 #define ICMP_NET_UNKNOWN 6
44 #endif
45
46 #ifndef ICMP_TIME_EXCEEDED
47 #define ICMP_TIME_EXCEEDED 11
48 #endif
49
50 #ifndef ICMP_EXC_TTL
51 #define ICMP_EXC_TTL 0
52 #endif
53
54 #ifndef ICMP_NET_UNREACH
55 #define ICMP_NET_UNREACH 0
56 #endif
57
58 #ifndef ICMP_NET_ANO
59 #define ICMP_NET_ANO 9
60 #endif
61
62 #ifndef IP_MSS
63 #define       IP_MSS          576
64 #endif
65
66 #ifndef HAVE_STRUCT_IP
67 struct ip {
68 #if __BYTE_ORDER == __LITTLE_ENDIAN
69         unsigned int ip_hl: 4;
70         unsigned int ip_v: 4;
71 #else
72         unsigned int ip_v: 4;
73         unsigned int ip_hl: 4;
74 #endif
75         uint8_t ip_tos;
76         uint16_t ip_len;
77         uint16_t ip_id;
78         uint16_t ip_off;
79 #define IP_RF 0x8000
80 #define IP_DF 0x4000
81 #define IP_MF 0x2000
82         uint8_t ip_ttl;
83         uint8_t ip_p;
84         uint16_t ip_sum;
85         struct in_addr ip_src, ip_dst;
86 };
87 #endif
88
89 STATIC_ASSERT(sizeof(struct ip) == 20, "ip has incorrect size");
90
91 #ifndef IP_OFFMASK
92 #define IP_OFFMASK 0x1fff
93 #endif
94
95 #ifndef HAVE_STRUCT_ICMP
96 struct icmp {
97         uint8_t icmp_type;
98         uint8_t icmp_code;
99         uint16_t icmp_cksum;
100         union {
101                 uint8_t ih_pptr;
102                 struct in_addr ih_gwaddr;
103                 struct ih_idseq {
104                         uint16_t icd_id;
105                         uint16_t icd_seq;
106                 } ih_idseq;
107                 uint32_t ih_void;
108
109
110                 struct ih_pmtu {
111                         uint16_t ipm_void;
112                         uint16_t ipm_nextmtu;
113                 } ih_pmtu;
114
115                 struct ih_rtradv {
116                         uint8_t irt_num_addrs;
117                         uint8_t irt_wpa;
118                         uint16_t irt_lifetime;
119                 } ih_rtradv;
120         } icmp_hun;
121 #define icmp_pptr icmp_hun.ih_pptr
122 #define icmp_gwaddr icmp_hun.ih_gwaddr
123 #define icmp_id icmp_hun.ih_idseq.icd_id
124 #define icmp_seq icmp_hun.ih_idseq.icd_seq
125 #define icmp_void icmp_hun.ih_void
126 #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
127 #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
128 #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
129 #define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
130 #define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
131         union {
132                 struct {
133                         uint32_t its_otime;
134                         uint32_t its_rtime;
135                         uint32_t its_ttime;
136                 } id_ts;
137                 struct {
138                         struct ip idi_ip;
139                 } id_ip;
140                 uint32_t id_mask;
141                 uint8_t id_data[1];
142         } icmp_dun;
143 #define icmp_otime icmp_dun.id_ts.its_otime
144 #define icmp_rtime icmp_dun.id_ts.its_rtime
145 #define icmp_ttime icmp_dun.id_ts.its_ttime
146 #define icmp_ip icmp_dun.id_ip.idi_ip
147 #define icmp_radv icmp_dun.id_radv
148 #define icmp_mask icmp_dun.id_mask
149 #define icmp_data icmp_dun.id_data
150 };
151 #endif
152
153 STATIC_ASSERT(sizeof(struct icmp) == 28, "icmp has incorrect size");
154
155 #endif