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-2019 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 1.9.6
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 //C++ guard
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /**
45  * @brief IP supported protocols
46  **/
47 
48 typedef enum
49 {
52 } IpProtocol;
53 
54 
55 /**
56  * @brief Flags used by I/O functions
57  **/
58 
59 typedef enum
60 {
62  IP_FLAG_TTL = 0x00FF,
64 } IpFlags;
65 
66 
67 /**
68  * @brief IP network address
69  **/
70 
71 typedef struct
72 {
73  size_t length;
74  union
75  {
76 #if (IPV4_SUPPORT == ENABLED)
78 #endif
79 #if (IPV6_SUPPORT == ENABLED)
81 #endif
82  };
83 } IpAddr;
84 
85 
86 /**
87  * @brief IP pseudo header
88  **/
89 
90 typedef struct
91 {
92  size_t length;
93  union
94  {
95 #if (IPV4_SUPPORT == ENABLED)
97 #endif
98 #if (IPV6_SUPPORT == ENABLED)
100 #endif
101  uint8_t data[4];
102  };
104 
105 
106 //IP related constants
107 extern const IpAddr IP_ADDR_ANY;
108 extern const IpAddr IP_ADDR_UNSPECIFIED;
109 
110 //IP related functions
111 error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader,
112  NetBuffer *buffer, size_t offset, uint_t flags);
113 
115  const IpAddr *destAddr, IpAddr *srcAddr);
116 
117 bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2);
120 
123 
124 uint16_t ipCalcChecksum(const void *data, size_t length);
125 uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length);
126 
127 uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader,
128  size_t pseudoHeaderLen, const void *data, size_t dataLen);
129 
130 uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader,
131  size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length);
132 
133 NetBuffer *ipAllocBuffer(size_t length, size_t *offset);
134 
136 char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str);
137 
138 //C++ guard
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif
IPv6 (Internet Protocol Version 6)
uint8_t length
Definition: dtls_misc.h:149
Ipv6PseudoHeader ipv6Data
Definition: ip.h:99
int bool_t
Definition: compiler_port.h:49
MacAddr srcAddr
Definition: ethernet.h:193
IP network address.
Definition: ip.h:71
const IpAddr IP_ADDR_ANY
Definition: ip.c:45
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
bool_t ipIsMulticastAddr(const IpAddr *ipAddr)
Determine whether an IP address is a multicast address.
Definition: ip.c:254
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:392
@ IP_PROTOCOL_UDP
Definition: ip.h:51
size_t length
Definition: ip.h:92
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:293
uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP checksum over a multi-part buffer.
Definition: ip.c:512
MacAddr destAddr
Definition: ethernet.h:192
IP pseudo header.
Definition: ip.h:90
@ IP_FLAG_TTL
Definition: ip.h:62
error_t
Error codes.
Definition: error.h:42
#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:726
uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader, size_t pseudoHeaderLen, const void *data, size_t dataLen)
Calculate IP upper-layer checksum.
Definition: ip.c:585
@ IP_FLAG_DONT_ROUTE
Definition: ip.h:61
#define NetInterface
Definition: net.h:36
error_t ipLeaveMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Leave the specified host group.
Definition: ip.c:343
__start_packed struct @179 Ipv6Addr
IPv6 network address.
#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:682
size_t length
Definition: ip.h:73
uint8_t flags
Definition: tcp.h:314
char char_t
Definition: compiler_port.h:43
Ipv4Addr ipv4Addr
Definition: ip.h:77
IpFlags
Flags used by I/O functions.
Definition: ip.h:59
error_t ipSelectSourceAddr(NetInterface **interface, const IpAddr *destAddr, IpAddr *srcAddr)
IP source address selection.
Definition: ip.c:109
@ IP_PROTOCOL_TCP
Definition: ip.h:50
Ipv4Addr groupAddr
Definition: igmp.h:127
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:637
error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, uint_t flags)
Send an IP datagram.
Definition: ip.c:59
@ IP_FLAG_HOP_LIMIT
Definition: ip.h:63
IPv4 (Internet Protocol Version 4)
Ipv6Addr ipv6Addr
Definition: ip.h:80
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
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:612
bool_t ipIsUnspecifiedAddr(const IpAddr *ipAddr)
Compare an IP address against the unspecified address.
Definition: ip.c:209
IpProtocol
IP supported protocols.
Definition: ip.h:48
uint8_t ipAddr[4]
Definition: mib_common.h:187
Ipv4PseudoHeader ipv4Data
Definition: ip.h:96
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:158