The Pedigree Project  0.1
prot/ip4.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
25 /*
26  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without modification,
30  * are permitted provided that the following conditions are met:
31  *
32  * 1. Redistributions of source code must retain the above copyright notice,
33  * this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright notice,
35  * this list of conditions and the following disclaimer in the documentation
36  * and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  * derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
43  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
45  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
48  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49  * OF SUCH DAMAGE.
50  *
51  * This file is part of the lwIP TCP/IP stack.
52  *
53  * Author: Adam Dunkels <adam@sics.se>
54  *
55  */
56 #ifndef LWIP_HDR_PROT_IP4_H
57 #define LWIP_HDR_PROT_IP4_H
58 
59 #include "lwip/arch.h"
60 #include "lwip/ip4_addr.h"
61 
62 #ifdef __cplusplus
63 extern "C" {
64 #endif
65 
68 #ifdef PACK_STRUCT_USE_INCLUDES
69 # include "arch/bpstruct.h"
70 #endif
73  PACK_STRUCT_FIELD(u32_t addr);
74 } PACK_STRUCT_STRUCT;
76 #ifdef PACK_STRUCT_USE_INCLUDES
77 # include "arch/epstruct.h"
78 #endif
79 
80 typedef struct ip4_addr_packed ip4_addr_p_t;
81 
82 /* Size of the IPv4 header. Same as 'sizeof(struct ip_hdr)'. */
83 #define IP_HLEN 20
84 
85 #ifdef PACK_STRUCT_USE_INCLUDES
86 # include "arch/bpstruct.h"
87 #endif
89 /* The IPv4 header */
90 struct ip_hdr {
91  /* version / header length */
92  PACK_STRUCT_FLD_8(u8_t _v_hl);
93  /* type of service */
94  PACK_STRUCT_FLD_8(u8_t _tos);
95  /* total length */
96  PACK_STRUCT_FIELD(u16_t _len);
97  /* identification */
98  PACK_STRUCT_FIELD(u16_t _id);
99  /* fragment offset field */
100  PACK_STRUCT_FIELD(u16_t _offset);
101 #define IP_RF 0x8000U /* reserved fragment flag */
102 #define IP_DF 0x4000U /* don't fragment flag */
103 #define IP_MF 0x2000U /* more fragments flag */
104 #define IP_OFFMASK 0x1fffU /* mask for fragmenting bits */
105  /* time to live */
106  PACK_STRUCT_FLD_8(u8_t _ttl);
107  /* protocol*/
108  PACK_STRUCT_FLD_8(u8_t _proto);
109  /* checksum */
110  PACK_STRUCT_FIELD(u16_t _chksum);
111  /* source and destination IP addresses */
112  PACK_STRUCT_FLD_S(ip4_addr_p_t src);
113  PACK_STRUCT_FLD_S(ip4_addr_p_t dest);
114 } PACK_STRUCT_STRUCT;
116 #ifdef PACK_STRUCT_USE_INCLUDES
117 # include "arch/epstruct.h"
118 #endif
119 
120 /* Macros to get struct ip_hdr fields: */
121 #define IPH_V(hdr) ((hdr)->_v_hl >> 4)
122 #define IPH_HL(hdr) ((hdr)->_v_hl & 0x0f)
123 #define IPH_TOS(hdr) ((hdr)->_tos)
124 #define IPH_LEN(hdr) ((hdr)->_len)
125 #define IPH_ID(hdr) ((hdr)->_id)
126 #define IPH_OFFSET(hdr) ((hdr)->_offset)
127 #define IPH_TTL(hdr) ((hdr)->_ttl)
128 #define IPH_PROTO(hdr) ((hdr)->_proto)
129 #define IPH_CHKSUM(hdr) ((hdr)->_chksum)
130 
131 /* Macros to set struct ip_hdr fields: */
132 #define IPH_VHL_SET(hdr, v, hl) (hdr)->_v_hl = (u8_t)((((v) << 4) | (hl)))
133 #define IPH_TOS_SET(hdr, tos) (hdr)->_tos = (tos)
134 #define IPH_LEN_SET(hdr, len) (hdr)->_len = (len)
135 #define IPH_ID_SET(hdr, id) (hdr)->_id = (id)
136 #define IPH_OFFSET_SET(hdr, off) (hdr)->_offset = (off)
137 #define IPH_TTL_SET(hdr, ttl) (hdr)->_ttl = (u8_t)(ttl)
138 #define IPH_PROTO_SET(hdr, proto) (hdr)->_proto = (u8_t)(proto)
139 #define IPH_CHKSUM_SET(hdr, chksum) (hdr)->_chksum = (chksum)
140 
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* LWIP_HDR_PROT_IP4_H */
#define PACK_STRUCT_BEGIN
Definition: arch.h:261
#define PACK_STRUCT_FLD_S(x)
Definition: arch.h:310
#define PACK_STRUCT_END
Definition: arch.h:270
#define PACK_STRUCT_FLD_8(x)
Definition: arch.h:301
#define PACK_STRUCT_FIELD(x)
Definition: arch.h:292