ip.h
Go to the documentation of this file.
1 /**
2  * @file ip.h
3  * @brief IPv4 and IPv6 common routines
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 #ifndef _IP_H
32 #define _IP_H
33 
34 //Dependencies
35 #include "ipv4/ipv4.h"
36 #include "ipv6/ipv6.h"
37 
38 //Default value for DF flag
39 #ifndef IP_DEFAULT_DF
40  #define IP_DEFAULT_DF FALSE
41 #elif (IP_DEFAULT_DF != FALSE && IP_DEFAULT_DF != TRUE)
42  #error IP_DEFAULT_DF parameter is not valid
43 #endif
44 
45 //C++ guard
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 
51 /**
52  * @brief IP supported protocols
53  **/
54 
55 typedef enum
56 {
58  IP_PROTOCOL_UDP = 17
60 
61 
62 /**
63  * @brief Multicast filter mode
64  **/
65 
66 typedef enum
67 {
71 
72 
73 /**
74  * @brief Flags used by I/O functions
75  **/
76 
77 typedef enum
78 {
80  IP_FLAG_TTL = 0x00FF,
81  IP_FLAG_HOP_LIMIT = 0x00FF
83 
84 
85 /**
86  * @brief IP network address
87  **/
88 
89 typedef struct
90 {
91  size_t length;
92  union
93  {
94 #if (IPV4_SUPPORT == ENABLED)
96 #endif
97 #if (IPV6_SUPPORT == ENABLED)
99 #endif
100  uint8_t addr[4];
101  };
102 } IpAddr;
103 
104 
105 /**
106  * @brief IP pseudo header
107  **/
108 
109 typedef struct
110 {
111  size_t length;
112  union
113  {
114 #if (IPV4_SUPPORT == ENABLED)
116 #endif
117 #if (IPV6_SUPPORT == ENABLED)
119 #endif
120  uint8_t data[4];
121  };
123 
124 
125 //IP related constants
126 extern const IpAddr IP_ADDR_ANY;
127 extern const IpAddr IP_ADDR_UNSPECIFIED;
128 
129 //IP related functions
131  const IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset,
132  NetTxAncillary *ancillary);
133 
135  IpAddr *srcAddr);
136 
141 
142 bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2);
143 
144 bool_t ipCompPrefix(const IpAddr *ipAddr1, const IpAddr *ipAddr2,
145  size_t length);
146 
147 void ipUpdateMulticastFilter(NetInterface *interface, const IpAddr *groupAddr);
148 
149 uint16_t ipCalcChecksum(const void *data, size_t length);
150 uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length);
151 
152 uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader,
153  size_t pseudoHeaderLen, const void *data, size_t dataLen);
154 
155 uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader,
156  size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length);
157 
158 NetBuffer *ipAllocBuffer(size_t length, size_t *offset);
159 
161 char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str);
162 
163 //C++ guard
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif
IpFilterMode
Multicast filter mode.
Definition: ip.h:67
IPv6 (Internet Protocol Version 6)
Ipv6PseudoHeader ipv6Data
Definition: ip.h:118
int bool_t
Definition: compiler_port.h:53
Ipv4Addr destAddr
Definition: ipv4.h:329
IP network address.
Definition: ip.h:90
const IpAddr IP_ADDR_ANY
Definition: ip.c:53
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t data[]
Definition: ethernet.h:222
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:250
Ipv6Addr
Definition: ipv6.h:260
@ IP_FILTER_MODE_EXCLUDE
Definition: ip.h:68
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:466
@ IP_PROTOCOL_UDP
Definition: ip.h:58
size_t length
Definition: ip.h:111
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:297
uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP checksum over a multi-part buffer.
Definition: ip.c:586
IP pseudo header.
Definition: ip.h:110
@ IP_FLAG_TTL
Definition: ip.h:80
bool_t ipIsBroadcastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a broadcast address.
Definition: ip.c:288
error_t
Error codes.
Definition: error.h:43
#define Ipv6PseudoHeader
Definition: ipv6.h:42
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:805
uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader, size_t pseudoHeaderLen, const void *data, size_t dataLen)
Calculate IP upper-layer checksum.
Definition: ip.c:659
@ IP_FLAG_DONT_ROUTE
Definition: ip.h:79
#define NetInterface
Definition: net.h:36
#define NetTxAncillary
Definition: net_misc.h:36
#define Ipv4PseudoHeader
Definition: ipv4.h:39
const IpAddr IP_ADDR_UNSPECIFIED
Definition: ip.c:54
uint8_t length
Definition: tcp.h:368
error_t ipStringToAddr(const char_t *str, IpAddr *ipAddr)
Convert a string representation of an IP address to a binary IP address.
Definition: ip.c:761
size_t length
Definition: ip.h:91
uint32_t dataLen
Definition: sftp_common.h:229
bool_t ipIsLinkLocalAddr(const IpAddr *ipAddr)
Determine whether an IP address is a link-local address.
Definition: ip.c:212
char char_t
Definition: compiler_port.h:48
Ipv4Addr ipv4Addr
Definition: ip.h:95
IpFlags
Flags used by I/O functions.
Definition: ip.h:78
error_t ipSelectSourceAddr(NetInterface **interface, const IpAddr *destAddr, IpAddr *srcAddr)
IP source address selection.
Definition: ip.c:119
Ipv4Addr groupAddr
Definition: igmp_common.h:214
error_t ipSendDatagram(NetInterface *interface, const IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an IP datagram.
Definition: ip.c:68
@ IP_FILTER_MODE_INCLUDE
Definition: ip.h:69
@ IP_PROTOCOL_TCP
Definition: ip.h:57
void ipUpdateMulticastFilter(NetInterface *interface, const IpAddr *groupAddr)
Update IP multicast filter table.
Definition: ip.c:410
MacAddr srcAddr
Definition: ethernet.h:220
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:711
Ipv4Addr ipAddr
Definition: ipcp.h:105
@ IP_FLAG_HOP_LIMIT
Definition: ip.h:81
Ipv4Addr addr
Definition: nbns_common.h:123
IPv4 (Internet Protocol Version 4)
Ipv6Addr ipv6Addr
Definition: ip.h:98
bool_t ipCompPrefix(const IpAddr *ipAddr1, const IpAddr *ipAddr2, size_t length)
Compare IP address prefixes.
Definition: ip.c:370
uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader, size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP upper-layer checksum over a multi-part buffer.
Definition: ip.c:686
bool_t ipIsUnspecifiedAddr(const IpAddr *ipAddr)
Compare an IP address against the unspecified address.
Definition: ip.c:167
IpProtocol
IP supported protocols.
Definition: ip.h:56
Ipv4PseudoHeader ipv4Data
Definition: ip.h:115
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:317