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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _IP_H
30 #define _IP_H
31 
32 //Dependencies
33 #include "ipv4/ipv4.h"
34 #include "ipv6/ipv6.h"
35 
36 //C++ guard
37 #ifdef __cplusplus
38  extern "C" {
39 #endif
40 
41 
42 /**
43  * @brief IP supported protocols
44  **/
45 
46 typedef enum
47 {
50 } IpProtocol;
51 
52 
53 /**
54  * @brief IP network address
55  **/
56 
57 typedef struct
58 {
59  size_t length;
60  union
61  {
62 #if (IPV4_SUPPORT == ENABLED)
64 #endif
65 #if (IPV6_SUPPORT == ENABLED)
67 #endif
68  };
69 } IpAddr;
70 
71 
72 /**
73  * @brief IP pseudo header
74  **/
75 
76 typedef struct
77 {
78  size_t length;
79  union
80  {
81 #if (IPV4_SUPPORT == ENABLED)
83 #endif
84 #if (IPV6_SUPPORT == ENABLED)
86 #endif
87  uint8_t data[4];
88  };
90 
91 
92 //IP related constants
93 extern const IpAddr IP_ADDR_ANY;
94 extern const IpAddr IP_ADDR_UNSPECIFIED;
95 
96 //IP related functions
97 error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader,
98  NetBuffer *buffer, size_t offset, uint8_t ttl);
99 
101  const IpAddr *destAddr, IpAddr *srcAddr);
102 
103 bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2);
105 
108 
109 uint16_t ipCalcChecksum(const void *data, size_t length);
110 uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length);
111 
112 uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader,
113  size_t pseudoHeaderLen, const void *data, size_t dataLen);
114 
115 uint16_t ipCalcUpperLayerChecksumEx(const void *pseudoHeader,
116  size_t pseudoHeaderLen, const NetBuffer *buffer, size_t offset, size_t length);
117 
118 NetBuffer *ipAllocBuffer(size_t length, size_t *offset);
119 
121 char_t *ipAddrToString(const IpAddr *ipAddr, char_t *str);
122 
123 //C++ guard
124 #ifdef __cplusplus
125  }
126 #endif
127 
128 #endif
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
error_t ipSelectSourceAddr(NetInterface **interface, const IpAddr *destAddr, IpAddr *srcAddr)
IP source address selection.
Definition: ip.c:106
char char_t
Definition: compiler_port.h:41
Ipv4Addr groupAddr
Definition: igmp.h:125
Ipv4Addr ipv4Addr
Definition: ip.h:63
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:571
IP network address.
Definition: ip.h:57
__start_packed struct @183 Ipv6Addr
IPv6 network address.
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:596
#define Ipv4PseudoHeader
Definition: ipv4.h:37
const IpAddr IP_ADDR_ANY
Definition: ip.c:42
uint8_t ipAddr[4]
Definition: mib_common.h:185
bool_t ipIsUnspecifiedAddr(const IpAddr *ipAddr)
Compare an IP address against the unspecified address.
Definition: ip.c:206
uint16_t ipCalcChecksumEx(const NetBuffer *buffer, size_t offset, size_t length)
Calculate IP checksum over a multi-part buffer.
Definition: ip.c:471
size_t length
Definition: ip.h:78
char_t * ipAddrToString(const IpAddr *ipAddr, char_t *str)
Convert a binary IP address to a string representation.
Definition: ip.c:685
IPv4 (Internet Protocol Version 4)
uint32_t ttl
Definition: dns_common.h:203
error_t ipLeaveMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Leave the specified host group.
Definition: ip.c:302
#define Ipv6PseudoHeader
Definition: ipv6.h:40
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:351
error_t ipJoinMulticastGroup(NetInterface *interface, const IpAddr *groupAddr)
Join the specified host group.
Definition: ip.c:252
IPv6 (Internet Protocol Version 6)
error_t
Error codes.
Definition: error.h:40
size_t length
Definition: ip.h:59
Ipv6Addr ipv6Addr
Definition: ip.h:66
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
MacAddr srcAddr
Definition: ethernet.h:181
IP pseudo header.
Definition: ip.h:76
Ipv6PseudoHeader ipv6Data
Definition: ip.h:85
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:641
IpProtocol
IP supported protocols.
Definition: ip.h:46
Ipv4PseudoHeader ipv4Data
Definition: ip.h:82
uint16_t ipCalcUpperLayerChecksum(const void *pseudoHeader, size_t pseudoHeaderLen, const void *data, size_t dataLen)
Calculate IP upper-layer checksum.
Definition: ip.c:544
uint8_t length
Definition: dtls_misc.h:140
error_t ipSendDatagram(NetInterface *interface, IpPseudoHeader *pseudoHeader, NetBuffer *buffer, size_t offset, uint8_t ttl)
Send an IP datagram.
Definition: ip.c:56
bool_t ipCompAddr(const IpAddr *ipAddr1, const IpAddr *ipAddr2)
Compare IP addresses.
Definition: ip.c:155
int bool_t
Definition: compiler_port.h:47
const IpAddr IP_ADDR_UNSPECIFIED
Definition: ip.c:43
MacAddr destAddr
Definition: ethernet.h:180