Webserver+3d print
cyclone_tcp/ipv4/ipv4.h@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file ipv4.h |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief IPv4 (Internet Protocol Version 4) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneTCP Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 26 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 27 | **/ |
Sergunb | 0:8918a71cdbe9 | 28 | |
Sergunb | 0:8918a71cdbe9 | 29 | #ifndef _IPV4_H |
Sergunb | 0:8918a71cdbe9 | 30 | #define _IPV4_H |
Sergunb | 0:8918a71cdbe9 | 31 | |
Sergunb | 0:8918a71cdbe9 | 32 | //Forward declaration of structures |
Sergunb | 0:8918a71cdbe9 | 33 | struct _Ipv4Header; |
Sergunb | 0:8918a71cdbe9 | 34 | #define Ipv4Header struct _Ipv4Header |
Sergunb | 0:8918a71cdbe9 | 35 | |
Sergunb | 0:8918a71cdbe9 | 36 | struct _Ipv4PseudoHeader; |
Sergunb | 0:8918a71cdbe9 | 37 | #define Ipv4PseudoHeader struct _Ipv4PseudoHeader |
Sergunb | 0:8918a71cdbe9 | 38 | |
Sergunb | 0:8918a71cdbe9 | 39 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 40 | #include <string.h> |
Sergunb | 0:8918a71cdbe9 | 41 | #include "core/net.h" |
Sergunb | 0:8918a71cdbe9 | 42 | #include "core/ethernet.h" |
Sergunb | 0:8918a71cdbe9 | 43 | #include "ipv4/ipv4_frag.h" |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | //IPv4 support |
Sergunb | 0:8918a71cdbe9 | 46 | #ifndef IPV4_SUPPORT |
Sergunb | 0:8918a71cdbe9 | 47 | #define IPV4_SUPPORT ENABLED |
Sergunb | 0:8918a71cdbe9 | 48 | #elif (IPV4_SUPPORT != ENABLED && IPV4_SUPPORT != DISABLED) |
Sergunb | 0:8918a71cdbe9 | 49 | #error IPV4_SUPPORT parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 50 | #endif |
Sergunb | 0:8918a71cdbe9 | 51 | |
Sergunb | 0:8918a71cdbe9 | 52 | //Default IPv4 time-to-live value |
Sergunb | 0:8918a71cdbe9 | 53 | #ifndef IPV4_DEFAULT_TTL |
Sergunb | 0:8918a71cdbe9 | 54 | #define IPV4_DEFAULT_TTL 64 |
Sergunb | 0:8918a71cdbe9 | 55 | #elif (IPV4_DEFAULT_TTL < 1) |
Sergunb | 0:8918a71cdbe9 | 56 | #error IPV4_DEFAULT_TTL parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 57 | #endif |
Sergunb | 0:8918a71cdbe9 | 58 | |
Sergunb | 0:8918a71cdbe9 | 59 | //Maximum number of DNS servers |
Sergunb | 0:8918a71cdbe9 | 60 | #ifndef IPV4_DNS_SERVER_LIST_SIZE |
Sergunb | 0:8918a71cdbe9 | 61 | #define IPV4_DNS_SERVER_LIST_SIZE 2 |
Sergunb | 0:8918a71cdbe9 | 62 | #elif (IPV4_DNS_SERVER_LIST_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 63 | #error IPV4_DNS_SERVER_LIST_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 64 | #endif |
Sergunb | 0:8918a71cdbe9 | 65 | |
Sergunb | 0:8918a71cdbe9 | 66 | //Size of the IPv4 multicast filter |
Sergunb | 0:8918a71cdbe9 | 67 | #ifndef IPV4_MULTICAST_FILTER_SIZE |
Sergunb | 0:8918a71cdbe9 | 68 | #define IPV4_MULTICAST_FILTER_SIZE 4 |
Sergunb | 0:8918a71cdbe9 | 69 | #elif (IPV4_MULTICAST_FILTER_SIZE < 1) |
Sergunb | 0:8918a71cdbe9 | 70 | #error IPV4_MULTICAST_FILTER_SIZE parameter is not valid |
Sergunb | 0:8918a71cdbe9 | 71 | #endif |
Sergunb | 0:8918a71cdbe9 | 72 | |
Sergunb | 0:8918a71cdbe9 | 73 | //Version number for IPv4 |
Sergunb | 0:8918a71cdbe9 | 74 | #define IPV4_VERSION 4 |
Sergunb | 0:8918a71cdbe9 | 75 | //Minimum MTU |
Sergunb | 0:8918a71cdbe9 | 76 | #define IPV4_MINIMUM_MTU 68 |
Sergunb | 0:8918a71cdbe9 | 77 | //Default MTU |
Sergunb | 0:8918a71cdbe9 | 78 | #define IPV4_DEFAULT_MTU 576 |
Sergunb | 0:8918a71cdbe9 | 79 | //Minimum header length |
Sergunb | 0:8918a71cdbe9 | 80 | #define IPV4_MIN_HEADER_LENGTH 20 |
Sergunb | 0:8918a71cdbe9 | 81 | //Maximum header length |
Sergunb | 0:8918a71cdbe9 | 82 | #define IPV4_MAX_HEADER_LENGTH 60 |
Sergunb | 0:8918a71cdbe9 | 83 | |
Sergunb | 0:8918a71cdbe9 | 84 | //Shortcut to data field |
Sergunb | 0:8918a71cdbe9 | 85 | #define IPV4_DATA(packet) PTR_OFFSET(packet, packet->headerLength * 4) |
Sergunb | 0:8918a71cdbe9 | 86 | |
Sergunb | 0:8918a71cdbe9 | 87 | //Macro used for defining an IPv4 address |
Sergunb | 0:8918a71cdbe9 | 88 | #ifdef _CPU_BIG_ENDIAN |
Sergunb | 0:8918a71cdbe9 | 89 | #define IPV4_ADDR(a, b, c, d) (((uint32_t) (a) << 24) | ((b) << 16) | ((c) << 8) | (d)) |
Sergunb | 0:8918a71cdbe9 | 90 | #else |
Sergunb | 0:8918a71cdbe9 | 91 | #define IPV4_ADDR(a, b, c, d) ((a) | ((b) << 8) | ((c) << 16) | ((uint32_t) (d) << 24)) |
Sergunb | 0:8918a71cdbe9 | 92 | #endif |
Sergunb | 0:8918a71cdbe9 | 93 | |
Sergunb | 0:8918a71cdbe9 | 94 | //Unspecified IPv4 address |
Sergunb | 0:8918a71cdbe9 | 95 | #define IPV4_UNSPECIFIED_ADDR IPV4_ADDR(0, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 96 | //Broadcast IPV4 address |
Sergunb | 0:8918a71cdbe9 | 97 | #define IPV4_BROADCAST_ADDR IPV4_ADDR(255, 255, 255, 255) |
Sergunb | 0:8918a71cdbe9 | 98 | |
Sergunb | 0:8918a71cdbe9 | 99 | //Loopback IPv4 address |
Sergunb | 0:8918a71cdbe9 | 100 | #define IPV4_LOOPBACK_ADDR IPV4_ADDR(127, 0, 0, 1) |
Sergunb | 0:8918a71cdbe9 | 101 | #define IPV4_LOOPBACK_ADDR_PREFIX IPV4_ADDR(127, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 102 | #define IPV4_LOOPBACK_ADDR_MASK IPV4_ADDR(255, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 103 | |
Sergunb | 0:8918a71cdbe9 | 104 | //Link-local addresses |
Sergunb | 0:8918a71cdbe9 | 105 | #define IPV4_LINK_LOCAL_PREFIX IPV4_ADDR(169, 254, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 106 | #define IPV4_LINK_LOCAL_MASK IPV4_ADDR(255, 255, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 107 | |
Sergunb | 0:8918a71cdbe9 | 108 | //Multicast addresses |
Sergunb | 0:8918a71cdbe9 | 109 | #define IPV4_MULTICAST_PREFIX IPV4_ADDR(224, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 110 | #define IPV4_MULTICAST_MASK IPV4_ADDR(240, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 111 | |
Sergunb | 0:8918a71cdbe9 | 112 | //Local Network Control Block (RFC 5771) |
Sergunb | 0:8918a71cdbe9 | 113 | #define IPV4_MULTICAST_LNCB_PREFIX IPV4_ADDR(224, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 114 | #define IPV4_MULTICAST_LNCB_MASK IPV4_ADDR(255, 255, 255, 0) |
Sergunb | 0:8918a71cdbe9 | 115 | |
Sergunb | 0:8918a71cdbe9 | 116 | //Internetwork Control Block (RFC 5771) |
Sergunb | 0:8918a71cdbe9 | 117 | #define IPV4_MULTICAST_INCB_PREFIX IPV4_ADDR(224, 0, 1, 0) |
Sergunb | 0:8918a71cdbe9 | 118 | #define IPV4_MULTICAST_INCB_MASK IPV4_ADDR(255, 255, 255, 0) |
Sergunb | 0:8918a71cdbe9 | 119 | |
Sergunb | 0:8918a71cdbe9 | 120 | //IPv4 address classes |
Sergunb | 0:8918a71cdbe9 | 121 | #define IPV4_CLASS_A_ADDR IPV4_ADDR(0, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 122 | #define IPV4_CLASS_A_MASK IPV4_ADDR(128, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 123 | #define IPV4_CLASS_B_ADDR IPV4_ADDR(128, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 124 | #define IPV4_CLASS_B_MASK IPV4_ADDR(192, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 125 | #define IPV4_CLASS_C_ADDR IPV4_ADDR(192, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 126 | #define IPV4_CLASS_C_MASK IPV4_ADDR(224, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 127 | #define IPV4_CLASS_D_ADDR IPV4_ADDR(224, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 128 | #define IPV4_CLASS_D_MASK IPV4_ADDR(240, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 129 | #define IPV4_CLASS_E_ADDR IPV4_ADDR(240, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 130 | #define IPV4_CLASS_E_MASK IPV4_ADDR(240, 0, 0, 0) |
Sergunb | 0:8918a71cdbe9 | 131 | |
Sergunb | 0:8918a71cdbe9 | 132 | //Copy IPv4 address |
Sergunb | 0:8918a71cdbe9 | 133 | #define ipv4CopyAddr(destIpAddr, srcIpAddr) \ |
Sergunb | 0:8918a71cdbe9 | 134 | memcpy(destIpAddr, srcIpAddr, sizeof(Ipv4Addr)) |
Sergunb | 0:8918a71cdbe9 | 135 | |
Sergunb | 0:8918a71cdbe9 | 136 | //Compare IPv4 addresses |
Sergunb | 0:8918a71cdbe9 | 137 | #define ipv4CompAddr(ipAddr1, ipAddr2) \ |
Sergunb | 0:8918a71cdbe9 | 138 | (!memcmp(ipAddr1, ipAddr2, sizeof(Ipv4Addr))) |
Sergunb | 0:8918a71cdbe9 | 139 | |
Sergunb | 0:8918a71cdbe9 | 140 | //Determine whether an IPv4 address belongs to the local network |
Sergunb | 0:8918a71cdbe9 | 141 | #define ipv4IsOnLocalSubnet(interface, ipAddr) \ |
Sergunb | 0:8918a71cdbe9 | 142 | ((ipAddr & interface->ipv4Context.subnetMask) == \ |
Sergunb | 0:8918a71cdbe9 | 143 | (interface->ipv4Context.addr & interface->ipv4Context.subnetMask)) |
Sergunb | 0:8918a71cdbe9 | 144 | |
Sergunb | 0:8918a71cdbe9 | 145 | //Determine whether an IPv4 address is a link-local address |
Sergunb | 0:8918a71cdbe9 | 146 | #define ipv4IsLinkLocalAddr(ipAddr) \ |
Sergunb | 0:8918a71cdbe9 | 147 | ((ipAddr & IPV4_LINK_LOCAL_MASK) == IPV4_LINK_LOCAL_PREFIX) |
Sergunb | 0:8918a71cdbe9 | 148 | |
Sergunb | 0:8918a71cdbe9 | 149 | //Determine whether an IPv4 address is a multicast address |
Sergunb | 0:8918a71cdbe9 | 150 | #define ipv4IsMulticastAddr(ipAddr) \ |
Sergunb | 0:8918a71cdbe9 | 151 | ((ipAddr & IPV4_MULTICAST_MASK) == IPV4_MULTICAST_PREFIX) |
Sergunb | 0:8918a71cdbe9 | 152 | |
Sergunb | 0:8918a71cdbe9 | 153 | //Check whether an IPv4 address is a tentative address |
Sergunb | 0:8918a71cdbe9 | 154 | #define ipv4IsTentativeAddr(interface, ipAddr) \ |
Sergunb | 0:8918a71cdbe9 | 155 | (interface->ipv4Context.addrState == IPV4_ADDR_STATE_TENTATIVE && \ |
Sergunb | 0:8918a71cdbe9 | 156 | interface->ipv4Context.addr == ipAddr) |
Sergunb | 0:8918a71cdbe9 | 157 | |
Sergunb | 0:8918a71cdbe9 | 158 | |
Sergunb | 0:8918a71cdbe9 | 159 | /** |
Sergunb | 0:8918a71cdbe9 | 160 | * @brief IPv4 address scopes |
Sergunb | 0:8918a71cdbe9 | 161 | **/ |
Sergunb | 0:8918a71cdbe9 | 162 | |
Sergunb | 0:8918a71cdbe9 | 163 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 164 | { |
Sergunb | 0:8918a71cdbe9 | 165 | IPV4_ADDR_SCOPE_INTERFACE_LOCAL = 1, |
Sergunb | 0:8918a71cdbe9 | 166 | IPV4_ADDR_SCOPE_LINK_LOCAL = 2, |
Sergunb | 0:8918a71cdbe9 | 167 | IPV4_ADDR_SCOPE_GLOBAL = 3 |
Sergunb | 0:8918a71cdbe9 | 168 | } Ipv4AddrScope; |
Sergunb | 0:8918a71cdbe9 | 169 | |
Sergunb | 0:8918a71cdbe9 | 170 | |
Sergunb | 0:8918a71cdbe9 | 171 | /** |
Sergunb | 0:8918a71cdbe9 | 172 | * @brief IPv4 address state |
Sergunb | 0:8918a71cdbe9 | 173 | **/ |
Sergunb | 0:8918a71cdbe9 | 174 | |
Sergunb | 0:8918a71cdbe9 | 175 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 176 | { |
Sergunb | 0:8918a71cdbe9 | 177 | IPV4_ADDR_STATE_INVALID = 0, ///<An address that is not assigned to any interface |
Sergunb | 0:8918a71cdbe9 | 178 | IPV4_ADDR_STATE_TENTATIVE = 1, ///<An address whose uniqueness on a link is being verified |
Sergunb | 0:8918a71cdbe9 | 179 | IPV4_ADDR_STATE_VALID = 2 ///<An address assigned to an interface whose use is unrestricted |
Sergunb | 0:8918a71cdbe9 | 180 | } Ipv4AddrState; |
Sergunb | 0:8918a71cdbe9 | 181 | |
Sergunb | 0:8918a71cdbe9 | 182 | |
Sergunb | 0:8918a71cdbe9 | 183 | /** |
Sergunb | 0:8918a71cdbe9 | 184 | * @brief IPv4 fragment offset field |
Sergunb | 0:8918a71cdbe9 | 185 | **/ |
Sergunb | 0:8918a71cdbe9 | 186 | |
Sergunb | 0:8918a71cdbe9 | 187 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 188 | { |
Sergunb | 0:8918a71cdbe9 | 189 | IPV4_FLAG_RES = 0x8000, |
Sergunb | 0:8918a71cdbe9 | 190 | IPV4_FLAG_DF = 0x4000, |
Sergunb | 0:8918a71cdbe9 | 191 | IPV4_FLAG_MF = 0x2000, |
Sergunb | 0:8918a71cdbe9 | 192 | IPV4_OFFSET_MASK = 0x1FFF |
Sergunb | 0:8918a71cdbe9 | 193 | } Ipv4FragmentOffset; |
Sergunb | 0:8918a71cdbe9 | 194 | |
Sergunb | 0:8918a71cdbe9 | 195 | |
Sergunb | 0:8918a71cdbe9 | 196 | /** |
Sergunb | 0:8918a71cdbe9 | 197 | * @brief IPv4 protocol field |
Sergunb | 0:8918a71cdbe9 | 198 | **/ |
Sergunb | 0:8918a71cdbe9 | 199 | |
Sergunb | 0:8918a71cdbe9 | 200 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 201 | { |
Sergunb | 0:8918a71cdbe9 | 202 | IPV4_PROTOCOL_ICMP = 1, |
Sergunb | 0:8918a71cdbe9 | 203 | IPV4_PROTOCOL_IGMP = 2, |
Sergunb | 0:8918a71cdbe9 | 204 | IPV4_PROTOCOL_TCP = 6, |
Sergunb | 0:8918a71cdbe9 | 205 | IPV4_PROTOCOL_UDP = 17 |
Sergunb | 0:8918a71cdbe9 | 206 | } Ipv4Protocol; |
Sergunb | 0:8918a71cdbe9 | 207 | |
Sergunb | 0:8918a71cdbe9 | 208 | |
Sergunb | 0:8918a71cdbe9 | 209 | /** |
Sergunb | 0:8918a71cdbe9 | 210 | * @brief IPv4 option types |
Sergunb | 0:8918a71cdbe9 | 211 | **/ |
Sergunb | 0:8918a71cdbe9 | 212 | |
Sergunb | 0:8918a71cdbe9 | 213 | typedef enum |
Sergunb | 0:8918a71cdbe9 | 214 | { |
Sergunb | 0:8918a71cdbe9 | 215 | IPV4_OPTION_EEOL = 0, |
Sergunb | 0:8918a71cdbe9 | 216 | IPV4_OPTION_NOP = 1, |
Sergunb | 0:8918a71cdbe9 | 217 | IPV4_OPTION_RTRALT = 148 |
Sergunb | 0:8918a71cdbe9 | 218 | } Ipv4OptionType; |
Sergunb | 0:8918a71cdbe9 | 219 | |
Sergunb | 0:8918a71cdbe9 | 220 | |
Sergunb | 0:8918a71cdbe9 | 221 | /** |
Sergunb | 0:8918a71cdbe9 | 222 | * @brief IPv4 network address |
Sergunb | 0:8918a71cdbe9 | 223 | **/ |
Sergunb | 0:8918a71cdbe9 | 224 | |
Sergunb | 0:8918a71cdbe9 | 225 | typedef uint32_t Ipv4Addr; |
Sergunb | 0:8918a71cdbe9 | 226 | |
Sergunb | 0:8918a71cdbe9 | 227 | |
Sergunb | 0:8918a71cdbe9 | 228 | //CodeWarrior or Win32 compiler? |
Sergunb | 0:8918a71cdbe9 | 229 | #if defined(__CWCC__) || defined(_WIN32) |
Sergunb | 0:8918a71cdbe9 | 230 | #pragma pack(push, 1) |
Sergunb | 0:8918a71cdbe9 | 231 | #endif |
Sergunb | 0:8918a71cdbe9 | 232 | |
Sergunb | 0:8918a71cdbe9 | 233 | |
Sergunb | 0:8918a71cdbe9 | 234 | /** |
Sergunb | 0:8918a71cdbe9 | 235 | * @brief IPv4 header |
Sergunb | 0:8918a71cdbe9 | 236 | **/ |
Sergunb | 0:8918a71cdbe9 | 237 | |
Sergunb | 0:8918a71cdbe9 | 238 | __start_packed struct _Ipv4Header |
Sergunb | 0:8918a71cdbe9 | 239 | { |
Sergunb | 0:8918a71cdbe9 | 240 | #ifdef _CPU_BIG_ENDIAN |
Sergunb | 0:8918a71cdbe9 | 241 | uint8_t version : 4; //0 |
Sergunb | 0:8918a71cdbe9 | 242 | uint8_t headerLength : 4; |
Sergunb | 0:8918a71cdbe9 | 243 | #else |
Sergunb | 0:8918a71cdbe9 | 244 | uint8_t headerLength : 4; //0 |
Sergunb | 0:8918a71cdbe9 | 245 | uint8_t version : 4; |
Sergunb | 0:8918a71cdbe9 | 246 | #endif |
Sergunb | 0:8918a71cdbe9 | 247 | uint8_t typeOfService; //1 |
Sergunb | 0:8918a71cdbe9 | 248 | uint16_t totalLength; //2-3 |
Sergunb | 0:8918a71cdbe9 | 249 | uint16_t identification; //4-5 |
Sergunb | 0:8918a71cdbe9 | 250 | uint16_t fragmentOffset; //6-7 |
Sergunb | 0:8918a71cdbe9 | 251 | uint8_t timeToLive; //8 |
Sergunb | 0:8918a71cdbe9 | 252 | uint8_t protocol; //9 |
Sergunb | 0:8918a71cdbe9 | 253 | uint16_t headerChecksum; //10-11 |
Sergunb | 0:8918a71cdbe9 | 254 | Ipv4Addr srcAddr; //12-15 |
Sergunb | 0:8918a71cdbe9 | 255 | Ipv4Addr destAddr; //16-19 |
Sergunb | 0:8918a71cdbe9 | 256 | uint8_t options[]; //20 |
Sergunb | 0:8918a71cdbe9 | 257 | } __end_packed; |
Sergunb | 0:8918a71cdbe9 | 258 | |
Sergunb | 0:8918a71cdbe9 | 259 | |
Sergunb | 0:8918a71cdbe9 | 260 | /** |
Sergunb | 0:8918a71cdbe9 | 261 | * @brief IPv4 pseudo header |
Sergunb | 0:8918a71cdbe9 | 262 | **/ |
Sergunb | 0:8918a71cdbe9 | 263 | |
Sergunb | 0:8918a71cdbe9 | 264 | __start_packed struct _Ipv4PseudoHeader |
Sergunb | 0:8918a71cdbe9 | 265 | { |
Sergunb | 0:8918a71cdbe9 | 266 | Ipv4Addr srcAddr; //0-3 |
Sergunb | 0:8918a71cdbe9 | 267 | Ipv4Addr destAddr; //4-7 |
Sergunb | 0:8918a71cdbe9 | 268 | uint8_t reserved; //8 |
Sergunb | 0:8918a71cdbe9 | 269 | uint8_t protocol; //9 |
Sergunb | 0:8918a71cdbe9 | 270 | uint16_t length; //10-11 |
Sergunb | 0:8918a71cdbe9 | 271 | } __end_packed; |
Sergunb | 0:8918a71cdbe9 | 272 | |
Sergunb | 0:8918a71cdbe9 | 273 | |
Sergunb | 0:8918a71cdbe9 | 274 | /** |
Sergunb | 0:8918a71cdbe9 | 275 | * @brief IPv4 option |
Sergunb | 0:8918a71cdbe9 | 276 | **/ |
Sergunb | 0:8918a71cdbe9 | 277 | |
Sergunb | 0:8918a71cdbe9 | 278 | typedef __start_packed struct |
Sergunb | 0:8918a71cdbe9 | 279 | { |
Sergunb | 0:8918a71cdbe9 | 280 | uint8_t type; //0 |
Sergunb | 0:8918a71cdbe9 | 281 | uint8_t length; //1 |
Sergunb | 0:8918a71cdbe9 | 282 | uint8_t value[]; //2 |
Sergunb | 0:8918a71cdbe9 | 283 | } __end_packed Ipv4Option; |
Sergunb | 0:8918a71cdbe9 | 284 | |
Sergunb | 0:8918a71cdbe9 | 285 | |
Sergunb | 0:8918a71cdbe9 | 286 | //CodeWarrior or Win32 compiler? |
Sergunb | 0:8918a71cdbe9 | 287 | #if defined(__CWCC__) || defined(_WIN32) |
Sergunb | 0:8918a71cdbe9 | 288 | #pragma pack(pop) |
Sergunb | 0:8918a71cdbe9 | 289 | #endif |
Sergunb | 0:8918a71cdbe9 | 290 | |
Sergunb | 0:8918a71cdbe9 | 291 | |
Sergunb | 0:8918a71cdbe9 | 292 | /** |
Sergunb | 0:8918a71cdbe9 | 293 | * @brief IPv4 multicast filter entry |
Sergunb | 0:8918a71cdbe9 | 294 | **/ |
Sergunb | 0:8918a71cdbe9 | 295 | |
Sergunb | 0:8918a71cdbe9 | 296 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 297 | { |
Sergunb | 0:8918a71cdbe9 | 298 | Ipv4Addr addr; ///<Multicast address |
Sergunb | 0:8918a71cdbe9 | 299 | uint_t refCount; ///<Reference count for the current entry |
Sergunb | 0:8918a71cdbe9 | 300 | uint_t state; ///<IGMP host state |
Sergunb | 0:8918a71cdbe9 | 301 | bool_t flag; ///<IGMP flag |
Sergunb | 0:8918a71cdbe9 | 302 | systime_t timer; ///<Delay timer |
Sergunb | 0:8918a71cdbe9 | 303 | } Ipv4FilterEntry; |
Sergunb | 0:8918a71cdbe9 | 304 | |
Sergunb | 0:8918a71cdbe9 | 305 | |
Sergunb | 0:8918a71cdbe9 | 306 | /** |
Sergunb | 0:8918a71cdbe9 | 307 | * @brief IPv4 context |
Sergunb | 0:8918a71cdbe9 | 308 | **/ |
Sergunb | 0:8918a71cdbe9 | 309 | |
Sergunb | 0:8918a71cdbe9 | 310 | typedef struct |
Sergunb | 0:8918a71cdbe9 | 311 | { |
Sergunb | 0:8918a71cdbe9 | 312 | size_t linkMtu; ///<Maximum transmission unit |
Sergunb | 0:8918a71cdbe9 | 313 | bool_t isRouter; ///<A flag indicating whether routing is enabled on this interface |
Sergunb | 0:8918a71cdbe9 | 314 | uint16_t identification; ///<IPv4 fragment identification field |
Sergunb | 0:8918a71cdbe9 | 315 | Ipv4Addr addr; ///<Host address |
Sergunb | 0:8918a71cdbe9 | 316 | Ipv4AddrState addrState; ///<State of the host address |
Sergunb | 0:8918a71cdbe9 | 317 | bool_t addrConflict; ///<Address conflict detected |
Sergunb | 0:8918a71cdbe9 | 318 | Ipv4Addr subnetMask; ///<Subnet mask |
Sergunb | 0:8918a71cdbe9 | 319 | Ipv4Addr defaultGateway; ///<Default gateway |
Sergunb | 0:8918a71cdbe9 | 320 | Ipv4Addr dnsServerList[IPV4_DNS_SERVER_LIST_SIZE]; ///<DNS servers |
Sergunb | 0:8918a71cdbe9 | 321 | Ipv4FilterEntry multicastFilter[IPV4_MULTICAST_FILTER_SIZE]; ///<Multicast filter table |
Sergunb | 0:8918a71cdbe9 | 322 | #if (IPV4_FRAG_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 323 | Ipv4FragDesc fragQueue[IPV4_MAX_FRAG_DATAGRAMS]; ///<IPv4 fragment reassembly queue |
Sergunb | 0:8918a71cdbe9 | 324 | #endif |
Sergunb | 0:8918a71cdbe9 | 325 | } Ipv4Context; |
Sergunb | 0:8918a71cdbe9 | 326 | |
Sergunb | 0:8918a71cdbe9 | 327 | |
Sergunb | 0:8918a71cdbe9 | 328 | //IPv4 related functions |
Sergunb | 0:8918a71cdbe9 | 329 | error_t ipv4Init(NetInterface *interface); |
Sergunb | 0:8918a71cdbe9 | 330 | |
Sergunb | 0:8918a71cdbe9 | 331 | error_t ipv4SetHostAddr(NetInterface *interface, Ipv4Addr addr); |
Sergunb | 0:8918a71cdbe9 | 332 | error_t ipv4GetHostAddr(NetInterface *interface, Ipv4Addr *addr); |
Sergunb | 0:8918a71cdbe9 | 333 | |
Sergunb | 0:8918a71cdbe9 | 334 | error_t ipv4SetSubnetMask(NetInterface *interface, Ipv4Addr mask); |
Sergunb | 0:8918a71cdbe9 | 335 | error_t ipv4GetSubnetMask(NetInterface *interface, Ipv4Addr *mask); |
Sergunb | 0:8918a71cdbe9 | 336 | |
Sergunb | 0:8918a71cdbe9 | 337 | error_t ipv4SetDefaultGateway(NetInterface *interface, Ipv4Addr addr); |
Sergunb | 0:8918a71cdbe9 | 338 | error_t ipv4GetDefaultGateway(NetInterface *interface, Ipv4Addr *addr); |
Sergunb | 0:8918a71cdbe9 | 339 | |
Sergunb | 0:8918a71cdbe9 | 340 | error_t ipv4SetDnsServer(NetInterface *interface, uint_t index, Ipv4Addr addr); |
Sergunb | 0:8918a71cdbe9 | 341 | error_t ipv4GetDnsServer(NetInterface *interface, uint_t index, Ipv4Addr *addr); |
Sergunb | 0:8918a71cdbe9 | 342 | |
Sergunb | 0:8918a71cdbe9 | 343 | error_t ipv4GetBroadcastAddr(NetInterface *interface, Ipv4Addr *addr); |
Sergunb | 0:8918a71cdbe9 | 344 | |
Sergunb | 0:8918a71cdbe9 | 345 | void ipv4LinkChangeEvent(NetInterface *interface); |
Sergunb | 0:8918a71cdbe9 | 346 | |
Sergunb | 0:8918a71cdbe9 | 347 | void ipv4ProcessPacket(NetInterface *interface, Ipv4Header *packet, size_t length); |
Sergunb | 0:8918a71cdbe9 | 348 | void ipv4ProcessDatagram(NetInterface *interface, const NetBuffer *buffer); |
Sergunb | 0:8918a71cdbe9 | 349 | |
Sergunb | 0:8918a71cdbe9 | 350 | error_t ipv4SendDatagram(NetInterface *interface, Ipv4PseudoHeader *pseudoHeader, |
Sergunb | 0:8918a71cdbe9 | 351 | NetBuffer *buffer, size_t offset, uint8_t ttl); |
Sergunb | 0:8918a71cdbe9 | 352 | |
Sergunb | 0:8918a71cdbe9 | 353 | error_t ipv4SendPacket(NetInterface *interface, Ipv4PseudoHeader *pseudoHeader, |
Sergunb | 0:8918a71cdbe9 | 354 | uint16_t fragId, size_t fragOffset, NetBuffer *buffer, size_t offset, uint8_t ttl); |
Sergunb | 0:8918a71cdbe9 | 355 | |
Sergunb | 0:8918a71cdbe9 | 356 | error_t ipv4CheckSourceAddr(NetInterface *interface, Ipv4Addr ipAddr); |
Sergunb | 0:8918a71cdbe9 | 357 | error_t ipv4CheckDestAddr(NetInterface *interface, Ipv4Addr ipAddr); |
Sergunb | 0:8918a71cdbe9 | 358 | |
Sergunb | 0:8918a71cdbe9 | 359 | error_t ipv4SelectSourceAddr(NetInterface **interface, |
Sergunb | 0:8918a71cdbe9 | 360 | Ipv4Addr destAddr, Ipv4Addr *srcAddr); |
Sergunb | 0:8918a71cdbe9 | 361 | |
Sergunb | 0:8918a71cdbe9 | 362 | bool_t ipv4IsBroadcastAddr(NetInterface *interface, Ipv4Addr ipAddr); |
Sergunb | 0:8918a71cdbe9 | 363 | |
Sergunb | 0:8918a71cdbe9 | 364 | uint_t ipv4GetAddrScope(Ipv4Addr ipAddr); |
Sergunb | 0:8918a71cdbe9 | 365 | |
Sergunb | 0:8918a71cdbe9 | 366 | error_t ipv4JoinMulticastGroup(NetInterface *interface, Ipv4Addr groupAddr); |
Sergunb | 0:8918a71cdbe9 | 367 | error_t ipv4LeaveMulticastGroup(NetInterface *interface, Ipv4Addr groupAddr); |
Sergunb | 0:8918a71cdbe9 | 368 | |
Sergunb | 0:8918a71cdbe9 | 369 | error_t ipv4MapMulticastAddrToMac(Ipv4Addr ipAddr, MacAddr *macAddr); |
Sergunb | 0:8918a71cdbe9 | 370 | |
Sergunb | 0:8918a71cdbe9 | 371 | error_t ipv4StringToAddr(const char_t *str, Ipv4Addr *ipAddr); |
Sergunb | 0:8918a71cdbe9 | 372 | char_t *ipv4AddrToString(Ipv4Addr ipAddr, char_t *str); |
Sergunb | 0:8918a71cdbe9 | 373 | |
Sergunb | 0:8918a71cdbe9 | 374 | void ipv4DumpHeader(const Ipv4Header *ipHeader); |
Sergunb | 0:8918a71cdbe9 | 375 | |
Sergunb | 0:8918a71cdbe9 | 376 | #endif |
Sergunb | 0:8918a71cdbe9 | 377 |