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-2022 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.1.8
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 //DiffServ support
39 #ifndef IP_DIFF_SERV_SUPPORT
40  #define IP_DIFF_SERV_SUPPORT DISABLED
41 #elif (IP_DIFF_SERV_SUPPORT != ENABLED && IP_DIFF_SERV_SUPPORT != DISABLED)
42  #error IP_DIFF_SERV_SUPPORT 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  };
90 } IpAddr;
91 
92 
93 /**
94  * @brief IP pseudo header
95  **/
96 
97 typedef struct
98 {
99  size_t length;
100  union
101  {
102 #if (IPV4_SUPPORT == ENABLED)
104 #endif
105 #if (IPV6_SUPPORT == ENABLED)
107 #endif
108  uint8_t data[4];
109  };
111 
112 
113 //IP related constants
114 extern const IpAddr IP_ADDR_ANY;
115 extern const IpAddr IP_ADDR_UNSPECIFIED;
116 
117 //IP related functions
118 error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader,
119  NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
120 
122  const IpAddr *destAddr, IpAddr *srcAddr);
123 
127 
128 bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2);
129 
130 bool_t ipCompPrefix(const IpAddr * ipAddr1, const IpAddr * ipAddr2,
131  size_t length);
132 
135 
136 uint16_t ipCalcChecksum(const void *data, size_t length);
137 uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length);
138 
139 uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader,
140  size_t pseudoHeaderLen, const void *data, size_t dataLen);
141 
142 uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader,
143  size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length);
144 
145 NetBuffer *ipAllocBuffer(size_t length, size_t *offset);
146 
148 char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str);
149 
150 //C++ guard
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif
uint8_t length
Definition: coap_common.h:193
IPv6 (Internet Protocol Version 6)
Ipv6PseudoHeader ipv6Data
Definition: ip.h:106
int bool_t
Definition: compiler_port.h:48
MacAddr srcAddr
Definition: ethernet.h:218
uint8_t data[]
Definition: ethernet.h:220
IP network address.
Definition: ip.h:79
const IpAddr IP_ADDR_ANY
Definition: ip.c:45
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:241
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:472
@ IP_PROTOCOL_UDP
Definition: ip.h:58
size_t length
Definition: ip.h:99
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:239
error_t ipJoinMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Join the specified host group.
Definition: ip.c:373
uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP checksum over a multi-part buffer.
Definition: ip.c:592
MacAddr destAddr
Definition: ethernet.h:217
IP pseudo header.
Definition: ip.h:98
@ IP_FLAG_TTL
Definition: ip.h:69
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:806
uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader, size_t pseudoHeaderLen, const void *data, size_t dataLen)
Calculate IP upper-layer checksum.
Definition: ip.c:665
@ IP_FLAG_DONT_ROUTE
Definition: ip.h:68
#define NetInterface
Definition: net.h:36
error_t ipLeaveMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Leave the specified host group.
Definition: ip.c:423
#define NetTxAncillary
Definition: net_misc.h:36
#define Ipv4PseudoHeader
Definition: ipv4.h:39
const IpAddr IP_ADDR_UNSPECIFIED
Definition: ip.c:46
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:762
size_t length
Definition: ip.h:80
uint32_t dataLen
Definition: sftp_common.h:227
bool_t ipIsLinkLocalAddr(const IpAddr *ipAddr)
Determine whether an IP address is a link-local address.
Definition: ip.c:203
char char_t
Definition: compiler_port.h:43
Ipv4Addr ipv4Addr
Definition: ip.h:84
IpFlags
Flags used by I/O functions.
Definition: ip.h:67
error_t ipSelectSourceAddr(NetInterface **interface, const IpAddr *destAddr, IpAddr *srcAddr)
IP source address selection.
Definition: ip.c:110
Ipv4Addr groupAddr
Definition: igmp_common.h:169
@ IP_PROTOCOL_TCP
Definition: ip.h:57
error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send an IP datagram.
Definition: ip.c:60
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:717
@ IP_FLAG_HOP_LIMIT
Definition: ip.h:70
IPv4 (Internet Protocol Version 4)
Ipv6Addr ipv6Addr
Definition: ip.h:87
bool_t ipCompPrefix(const IpAddr *ipAddr1, const IpAddr *ipAddr2, size_t length)
Compare IP address prefixes.
Definition: ip.c:333
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:692
bool_t ipIsUnspecifiedAddr(const IpAddr *ipAddr)
Compare an IP address against the unspecified address.
Definition: ip.c:158
IpProtocol
IP supported protocols.
Definition: ip.h:56
__start_packed struct @0 Ipv6Addr
IPv6 network address.
uint8_t ipAddr[4]
Definition: mib_common.h:187
Ipv4PseudoHeader ipv4Data
Definition: ip.h:103
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:280