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.0
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 Flags used by I/O functions
64  **/
65 
66 typedef enum
67 {
69  IP_FLAG_TTL = 0x00FF,
70  IP_FLAG_HOP_LIMIT = 0x00FF
72 
73 
74 /**
75  * @brief IP network address
76  **/
77 
78 typedef struct
79 {
80  size_t length;
81  union
82  {
83 #if (IPV4_SUPPORT == ENABLED)
85 #endif
86 #if (IPV6_SUPPORT == ENABLED)
88 #endif
89  uint8_t addr[4];
90  };
91 } IpAddr;
92 
93 
94 /**
95  * @brief IP pseudo header
96  **/
97 
98 typedef struct
99 {
100  size_t length;
101  union
102  {
103 #if (IPV4_SUPPORT == ENABLED)
105 #endif
106 #if (IPV6_SUPPORT == ENABLED)
108 #endif
109  uint8_t data[4];
110  };
112 
113 
114 //IP related constants
115 extern const IpAddr IP_ADDR_ANY;
116 extern const IpAddr IP_ADDR_UNSPECIFIED;
117 
118 //IP related functions
120  const IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset,
121  NetTxAncillary *ancillary);
122 
124  IpAddr *srcAddr);
125 
130 
131 bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2);
132 
133 bool_t ipCompPrefix(const IpAddr *ipAddr1, const IpAddr *ipAddr2,
134  size_t length);
135 
138 
139 uint16_t ipCalcChecksum(const void *data, size_t length);
140 uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length);
141 
142 uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader,
143  size_t pseudoHeaderLen, const void *data, size_t dataLen);
144 
145 uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader,
146  size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length);
147 
148 NetBuffer *ipAllocBuffer(size_t length, size_t *offset);
149 
151 char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str);
152 
153 //C++ guard
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
MacAddr srcAddr
Definition: ethernet.h:220
uint8_t data[]
Definition: ethernet.h:222
Ipv4Addr groupAddr
Definition: igmp_common.h:171
const IpAddr IP_ADDR_ANY
Definition: ip.c:51
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:744
bool_t ipIsBroadcastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a broadcast address.
Definition: ip.c:286
bool_t ipIsLinkLocalAddr(const IpAddr *ipAddr)
Determine whether an IP address is a link-local address.
Definition: ip.c:210
error_t ipSelectSourceAddr(NetInterface **interface, const IpAddr *destAddr, IpAddr *srcAddr)
IP source address selection.
Definition: ip.c:117
error_t ipLeaveMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Leave the specified host group.
Definition: ip.c:454
uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP checksum over a multi-part buffer.
Definition: ip.c:619
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:248
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:719
IpProtocol
IP supported protocols.
Definition: ip.h:56
@ IP_PROTOCOL_UDP
Definition: ip.h:58
@ IP_PROTOCOL_TCP
Definition: ip.h:57
error_t ipSendDatagram(NetInterface *interface, const IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an IP datagram.
Definition: ip.c:66
bool_t ipIsUnspecifiedAddr(const IpAddr *ipAddr)
Compare an IP address against the unspecified address.
Definition: ip.c:165
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:794
bool_t ipCompPrefix(const IpAddr *ipAddr1, const IpAddr *ipAddr2, size_t length)
Compare IP address prefixes.
Definition: ip.c:368
const IpAddr IP_ADDR_UNSPECIFIED
Definition: ip.c:52
error_t ipJoinMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Join the specified host group.
Definition: ip.c:408
IpFlags
Flags used by I/O functions.
Definition: ip.h:67
@ IP_FLAG_HOP_LIMIT
Definition: ip.h:70
@ IP_FLAG_DONT_ROUTE
Definition: ip.h:68
@ IP_FLAG_TTL
Definition: ip.h:69
uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader, size_t pseudoHeaderLen, const void *data, size_t dataLen)
Calculate IP upper-layer checksum.
Definition: ip.c:692
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:315
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:838
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:499
Ipv4Addr ipAddr
Definition: ipcp.h:105
IPv4 (Internet Protocol Version 4)
#define Ipv4PseudoHeader
Definition: ipv4.h:39
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:267
Ipv4Addr destAddr
Definition: ipv4.h:299
IPv6 (Internet Protocol Version 6)
Ipv6Addr
Definition: ipv6.h:251
#define Ipv6PseudoHeader
Definition: ipv6.h:42
Ipv4Addr addr
Definition: nbns_common.h:123
#define NetInterface
Definition: net.h:36
#define NetTxAncillary
Definition: net_misc.h:36
uint32_t dataLen
Definition: sftp_common.h:229
IP network address.
Definition: ip.h:79
Ipv6Addr ipv6Addr
Definition: ip.h:87
Ipv4Addr ipv4Addr
Definition: ip.h:84
size_t length
Definition: ip.h:80
IP pseudo header.
Definition: ip.h:99
Ipv4PseudoHeader ipv4Data
Definition: ip.h:104
Ipv6PseudoHeader ipv6Data
Definition: ip.h:107
size_t length
Definition: ip.h:100
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t length
Definition: tcp.h:368