arp.h
Go to the documentation of this file.
1 /**
2  * @file arp.h
3  * @brief ARP (Address Resolution Protocol)
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 _ARP_H
30 #define _ARP_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //ARP tick interval
36 #ifndef ARP_TICK_INTERVAL
37  #define ARP_TICK_INTERVAL 200
38 #elif (ARP_TICK_INTERVAL < 10)
39  #error ARP_TICK_INTERVAL parameter is not valid
40 #endif
41 
42 //Size of ARP cache
43 #ifndef ARP_CACHE_SIZE
44  #define ARP_CACHE_SIZE 8
45 #elif (ARP_CACHE_SIZE < 4)
46  #error ARP_CACHE_SIZE parameter is not valid
47 #endif
48 
49 //Maximum number of packets waiting for address resolution to complete
50 #ifndef ARP_MAX_PENDING_PACKETS
51  #define ARP_MAX_PENDING_PACKETS 2
52 #elif (ARP_MAX_PENDING_PACKETS < 1)
53  #error ARP_MAX_PENDING_PACKETS parameter is not valid
54 #endif
55 
56 //Maximum number of times that an ARP request will be retransmitted
57 #ifndef ARP_MAX_REQUESTS
58  #define ARP_MAX_REQUESTS 3
59 #elif (ARP_MAX_REQUESTS < 1)
60  #error ARP_MAX_REQUESTS parameter is not valid
61 #endif
62 
63 //Time interval between subsequent retransmissions of ARP requests
64 #ifndef ARP_REQUEST_TIMEOUT
65  #define ARP_REQUEST_TIMEOUT 1000
66 #elif (ARP_REQUEST_TIMEOUT < 100)
67  #error ARP_REQUEST_TIMEOUT parameter is not valid
68 #endif
69 
70 //Maximum number of times that a probe will be retransmitted
71 #ifndef ARP_MAX_PROBES
72  #define ARP_MAX_PROBES 2
73 #elif (ARP_MAX_PROBES < 1)
74  #error ARP_MAX_PROBES parameter is not valid
75 #endif
76 
77 //time interval between subsequent retransmissions of probes
78 #ifndef ARP_PROBE_TIMEOUT
79  #define ARP_PROBE_TIMEOUT 60000
80 #elif (ARP_PROBE_TIMEOUT < 1000)
81  #error ARP_PROBE_TIMEOUT parameter is not valid
82 #endif
83 
84 //The time a host is considered reachable after receiving a reachability confirmation
85 #ifndef ARP_REACHABLE_TIME
86  #define ARP_REACHABLE_TIME 60000
87 #elif (ARP_REACHABLE_TIME < 1000)
88  #error ARP_REACHABLE_TIME parameter is not valid
89 #endif
90 
91 //Delay before sending the first probe
92 #ifndef ARP_DELAY_FIRST_PROBE_TIME
93  #define ARP_DELAY_FIRST_PROBE_TIME 5000
94 #elif (ARP_DELAY_FIRST_PROBE_TIME < 1000)
95  #error ARP_DELAY_FIRST_PROBE_TIME parameter is not valid
96 #endif
97 
98 //Hardware type
99 #define ARP_HARDWARE_TYPE_ETH 0x0001
100 //Protocol type
101 #define ARP_PROTOCOL_TYPE_IPV4 0x0800
102 
103 //C++ guard
104 #ifdef __cplusplus
105  extern "C" {
106 #endif
107 
108 
109 /**
110  * @brief ARP opcodes
111  **/
112 
113 typedef enum
114 {
117 } ArpOpcode;
118 
119 
120 /**
121  * @brief ARP cache entry states
122  **/
123 
124 typedef enum
125 {
133 } ArpState;
134 
135 
136 //CodeWarrior or Win32 compiler?
137 #if defined(__CWCC__) || defined(_WIN32)
138  #pragma pack(push, 1)
139 #endif
140 
141 
142 /**
143  * @brief ARP packet
144  **/
145 
146 typedef __start_packed struct
147 {
148  uint16_t hrd; //0-1
149  uint16_t pro; //2-3
150  uint8_t hln; //4
151  uint8_t pln; //5
152  uint16_t op; //6-7
153  MacAddr sha; //8-13
154  Ipv4Addr spa; //14-17
155  MacAddr tha; //18-23
156  Ipv4Addr tpa; //24-27
158 
159 
160 //CodeWarrior or Win32 compiler?
161 #if defined(__CWCC__) || defined(_WIN32)
162  #pragma pack(pop)
163 #endif
164 
165 
166 /**
167  * @brief ARP queue item
168  **/
169 
170 typedef struct
171 {
172  NetBuffer *buffer; //Packet waiting for address resolution
173  size_t offset; //Offset to the first byte of the packet
174 } ArpQueueItem;
175 
176 
177 /**
178  * @brief ARP cache entry
179  **/
180 
181 typedef struct
182 {
183  ArpState state; //Reachability state
184  Ipv4Addr ipAddr; //Unicast IPv4 address
185  MacAddr macAddr; //Link layer address associated with the IPv4 address
186  systime_t timestamp; //Time stamp to manage entry lifetime
187  systime_t timeout; //Timeout value
188  uint_t retransmitCount; //Retransmission counter
189  ArpQueueItem queue[ARP_MAX_PENDING_PACKETS]; //Packets waiting for address resolution to complete
190  uint_t queueSize; //Number of queued packets
191 } ArpCacheEntry;
192 
193 
194 //Tick counter to handle periodic operations
196 
197 //ARP related functions
198 error_t arpInit(NetInterface *interface);
199 void arpFlushCache(NetInterface *interface);
200 
203 
204 void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
205 void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry);
206 
207 error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr);
208 
210  Ipv4Addr ipAddr, NetBuffer *buffer, size_t offset);
211 
212 void arpTick(NetInterface *interface);
213 
214 void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket, size_t length);
215 void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest);
216 void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse);
217 
218 error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr);
219 
221  Ipv4Addr targetIpAddr, const MacAddr *destMacAddr);
222 
223 error_t arpSendReply(NetInterface *interface, Ipv4Addr targetIpAddr,
224  const MacAddr *targetMacAddr, const MacAddr *destMacAddr);
225 
226 void arpDumpPacket(const ArpPacket *arpPacket);
227 
228 //C++ guard
229 #ifdef __cplusplus
230  }
231 #endif
232 
233 #endif
Ipv4Addr ipAddr
Definition: arp.h:184
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
Ipv4Addr tpa
Definition: arp.h:156
#define ARP_MAX_PENDING_PACKETS
Definition: arp.h:51
uint32_t systime_t
Definition: compiler_port.h:44
void arpProcessReply(NetInterface *interface, ArpPacket *arpResponse)
Incoming ARP reply processing.
Definition: arp.c:675
void arpSendQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Send packets that are waiting for address resolution.
Definition: arp.c:180
TCP/IP stack core.
void arpProcessRequest(NetInterface *interface, ArpPacket *arpRequest)
Incoming ARP request processing.
Definition: arp.c:628
void arpFlushQueuedPackets(NetInterface *interface, ArpCacheEntry *entry)
Flush packet queue.
Definition: arp.c:220
error_t arpResolve(NetInterface *interface, Ipv4Addr ipAddr, MacAddr *macAddr)
Address resolution using ARP protocol.
Definition: arp.c:248
ArpState
ARP cache entry states.
Definition: arp.h:124
error_t arpEnqueuePacket(NetInterface *interface, Ipv4Addr ipAddr, NetBuffer *buffer, size_t offset)
Enqueue an IPv4 packet waiting for address resolution.
Definition: arp.c:339
__start_packed struct @166 ArpPacket
ARP packet.
ArpCacheEntry * arpCreateEntry(NetInterface *interface)
Create a new entry in the ARP cache.
Definition: arp.c:97
uint16_t hrd
Definition: arp.h:148
ArpCacheEntry * arpFindEntry(NetInterface *interface, Ipv4Addr ipAddr)
Search the ARP cache for a given IPv4 address.
Definition: arp.c:149
uint8_t ipAddr[4]
Definition: mib_common.h:185
void arpDumpPacket(const ArpPacket *arpPacket)
Dump ARP packet for debugging purpose.
Definition: arp.c:927
uint_t queueSize
Definition: arp.h:190
MacAddr macAddr
Definition: arp.h:185
uint_t retransmitCount
Definition: arp.h:188
MacAddr tha
Definition: arp.h:155
error_t arpSendProbe(NetInterface *interface, Ipv4Addr targetIpAddr)
Send ARP probe.
Definition: arp.c:757
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
Ipv4Addr spa
Definition: arp.h:154
size_t offset
Definition: arp.h:173
ArpState state
Definition: arp.h:183
__start_packed struct _Ipv4Header __end_packed
ARP queue item.
Definition: arp.h:170
void arpFlushCache(NetInterface *interface)
Flush ARP cache.
Definition: arp.c:72
uint8_t pln
Definition: arp.h:151
error_t
Error codes.
Definition: error.h:40
uint8_t hln
Definition: arp.h:150
systime_t timestamp
Definition: arp.h:186
unsigned int uint_t
Definition: compiler_port.h:43
NetBuffer * buffer
Definition: arp.h:172
ArpOpcode
ARP opcodes.
Definition: arp.h:113
__start_packed struct @112 MacAddr
MAC address.
error_t arpInit(NetInterface *interface)
ARP cache initialization.
Definition: arp.c:57
#define NetInterface
Definition: net.h:34
ARP cache entry.
Definition: arp.h:181
MacAddr sha
Definition: arp.h:153
uint16_t pro
Definition: arp.h:149
error_t arpSendRequest(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *destMacAddr)
Send ARP request.
Definition: arp.c:812
systime_t timeout
Definition: arp.h:187
void arpTick(NetInterface *interface)
ARP timer handler.
Definition: arp.c:423
uint16_t op
Definition: arp.h:152
uint8_t length
Definition: dtls_misc.h:140
void arpProcessPacket(NetInterface *interface, ArpPacket *arpPacket, size_t length)
Incoming ARP packet processing.
Definition: arp.c:534
systime_t arpTickCounter
Definition: arp.c:48
error_t arpSendReply(NetInterface *interface, Ipv4Addr targetIpAddr, const MacAddr *targetMacAddr, const MacAddr *destMacAddr)
Send ARP reply.
Definition: arp.c:875