auto_ip.h
Go to the documentation of this file.
1 /**
2  * @file auto_ip.h
3  * @brief Auto-IP (Dynamic Configuration of IPv4 Link-Local Addresses)
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 _AUTO_IP_H
30 #define _AUTO_IP_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //Auto-IP support
36 #ifndef AUTO_IP_SUPPORT
37  #define AUTO_IP_SUPPORT DISABLED
38 #elif (AUTO_IP_SUPPORT != ENABLED && AUTO_IP_SUPPORT != DISABLED)
39  #error AUTO_IP_SUPPORT parameter is not valid
40 #endif
41 
42 //Bonjour Conformance Test support
43 #ifndef AUTO_IP_BCT_SUPPORT
44  #define AUTO_IP_BCT_SUPPORT DISABLED
45 #elif (AUTO_IP_BCT_SUPPORT != ENABLED && AUTO_IP_BCT_SUPPORT != DISABLED)
46  #error AUTO_IP_BCT_SUPPORT parameter is not valid
47 #endif
48 
49 //Auto-IP tick interval
50 #ifndef AUTO_IP_TICK_INTERVAL
51  #define AUTO_IP_TICK_INTERVAL 200
52 #elif (AUTO_IP_TICK_INTERVAL < 10)
53  #error AUTO_IP_TICK_INTERVAL parameter is not valid
54 #endif
55 
56 //Initial random delay
57 #ifndef AUTO_IP_PROBE_WAIT
58  #define AUTO_IP_PROBE_WAIT 1000
59 #elif (AUTO_IP_PROBE_WAIT < 0)
60  #error AUTO_IP_PROBE_WAIT parameter is not valid
61 #endif
62 
63 //Number of probe packets
64 #ifndef AUTO_IP_PROBE_NUM
65  #define AUTO_IP_PROBE_NUM 3
66 #elif (AUTO_IP_PROBE_NUM < 1)
67  #error AUTO_IP_PROBE_NUM parameter is not valid
68 #endif
69 
70 //Minimum delay till repeated probe
71 #ifndef AUTO_IP_PROBE_MIN
72  #define AUTO_IP_PROBE_MIN 1000
73 #elif (AUTO_IP_PROBE_MIN < 100)
74  #error AUTO_IP_PROBE_MIN parameter is not valid
75 #endif
76 
77 //Maximum delay till repeated probe
78 #ifndef AUTO_IP_PROBE_MAX
79  #define AUTO_IP_PROBE_MAX 2000
80 #elif (AUTO_IP_PROBE_MAX < AUTO_IP_PROBE_MIN)
81  #error AUTO_IP_PROBE_MAX parameter is not valid
82 #endif
83 
84 //Delay before announcing
85 #ifndef AUTO_IP_ANNOUNCE_WAIT
86  #define AUTO_IP_ANNOUNCE_WAIT 2000
87 #elif (AUTO_IP_ANNOUNCE_WAIT < 100)
88  #error AUTO_IP_ANNOUNCE_WAIT parameter is not valid
89 #endif
90 
91 //Number of announcement packets
92 #ifndef AUTO_IP_ANNOUNCE_NUM
93  #define AUTO_IP_ANNOUNCE_NUM 2
94 #elif (AUTO_IP_ANNOUNCE_NUM < 1)
95  #error AUTO_IP_ANNOUNCE_NUM parameter is not valid
96 #endif
97 
98 //Time between announcement packets
99 #ifndef AUTO_IP_ANNOUNCE_INTERVAL
100  #define AUTO_IP_ANNOUNCE_INTERVAL 2000
101 #elif (AUTO_IP_ANNOUNCE_INTERVAL < 100)
102  #error AUTO_IP_ANNOUNCE_INTERVAL parameter is not valid
103 #endif
104 
105 //Max conflicts before rate limiting
106 #ifndef AUTO_IP_MAX_CONFLICTS
107  #define AUTO_IP_MAX_CONFLICTS 10
108 #elif (AUTO_IP_MAX_CONFLICTS < 1)
109  #error AUTO_IP_MAX_CONFLICTS parameter is not valid
110 #endif
111 
112 //Delay between successive attempts
113 #ifndef AUTO_IP_RATE_LIMIT_INTERVAL
114  #define AUTO_IP_RATE_LIMIT_INTERVAL 60000
115 #elif (AUTO_IP_RATE_LIMIT_INTERVAL < 1000)
116  #error AUTO_IP_RATE_LIMIT_INTERVAL parameter is not valid
117 #endif
118 
119 //Minimum interval between defensive
120 #ifndef AUTO_IP_DEFEND_INTERVAL
121  #define AUTO_IP_DEFEND_INTERVAL 10000
122 #elif (AUTO_IP_DEFEND_INTERVAL < 1000)
123  #error AUTO_IP_DEFEND_INTERVAL parameter is not valid
124 #endif
125 
126 //Auto-IP address prefix
127 #define AUTO_IP_PREFIX IPV4_ADDR(169, 254, 0, 0)
128 //Auto-IP subnet mask
129 #define AUTO_IP_MASK IPV4_ADDR(255, 255, 0, 0)
130 
131 //Auto-IP address range
132 #define AUTO_IP_ADDR_MIN IPV4_ADDR(169, 254, 1, 0)
133 #define AUTO_IP_ADDR_MAX IPV4_ADDR(169, 254, 254, 255)
134 
135 //Forward declaration of AutoIpContext structure
136 struct _AutoIpContext;
137 #define AutoIpContext struct _AutoIpContext
138 
139 //C++ guard
140 #ifdef __cplusplus
141  extern "C" {
142 #endif
143 
144 
145 /**
146  * @brief Auto-IP FSM states
147  **/
148 
149 typedef enum
150 {
156 } AutoIpState;
157 
158 
159 /**
160  * @brief Link state change callback
161  **/
162 
163 typedef void (*AutoIpLinkChangeCallback)(AutoIpContext *context,
164  NetInterface *interface, bool_t linkState);
165 
166 
167 /**
168  * @brief FSM state change callback
169  **/
170 
171 typedef void (*AutoIpStateChangeCallback)(AutoIpContext *context,
172  NetInterface *interface, AutoIpState state);
173 
174 
175 /**
176  * @brief Auto-IP settings
177  **/
178 
179 typedef struct
180 {
181  NetInterface *interface; ///<Network interface to configure
182  Ipv4Addr linkLocalAddr; ///<Initial link-local address to be used
183  AutoIpLinkChangeCallback linkChangeEvent; ///<Link state change event
184  AutoIpStateChangeCallback stateChangeEvent; ///<FSM state change event
186 
187 
188 /**
189  * @brief Auto-IP context
190  **/
191 
193 {
194  AutoIpSettings settings; ///<Auto-IP settings
195  bool_t running; ///<Auto-IP is currently running
196  AutoIpState state; ///<Current state of the FSM
197  Ipv4Addr linkLocalAddr; ///<Link-local address
198  systime_t timestamp; ///<Timestamp to manage retransmissions
199  systime_t timeout; ///<Timeout value
200  uint_t retransmitCount; ///<Retransmission counter
201  uint_t conflictCount; ///<Number of conflicts
202 };
203 
204 
205 //Tick counter to handle periodic operations
207 
208 //Auto-IP related functions
210 error_t autoIpInit(AutoIpContext *context, const AutoIpSettings *settings);
214 
215 void autoIpTick(AutoIpContext *context);
216 void autoIpLinkChangeEvent(AutoIpContext *context);
217 
218 void autoIpChangeState(AutoIpContext *context,
219  AutoIpState newState, systime_t delay);
220 
222 
223 void autoIpDumpConfig(AutoIpContext *context);
224 
225 //C++ guard
226 #ifdef __cplusplus
227  }
228 #endif
229 
230 #endif
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:232
uint32_t systime_t
Definition: compiler_port.h:44
error_t autoIpStop(AutoIpContext *context)
Stop Auto-IP process.
Definition: auto_ip.c:175
error_t autoIpInit(AutoIpContext *context, const AutoIpSettings *settings)
Auto-IP initialization.
Definition: auto_ip.c:83
bool_t running
Auto-IP is currently running.
Definition: auto_ip.h:195
Auto-IP settings.
Definition: auto_ip.h:179
TCP/IP stack core.
void(* AutoIpStateChangeCallback)(AutoIpContext *context, NetInterface *interface, AutoIpState state)
FSM state change callback.
Definition: auto_ip.h:171
systime_t autoIpTickCounter
Definition: auto_ip.c:54
void autoIpGenerateAddr(Ipv4Addr *ipAddr)
Generate a random link-local address.
Definition: auto_ip.c:527
Auto-IP context.
Definition: auto_ip.h:192
AutoIpSettings settings
Auto-IP settings.
Definition: auto_ip.h:194
void autoIpTick(AutoIpContext *context)
Auto-IP timer handler.
Definition: auto_ip.c:231
void autoIpLinkChangeEvent(AutoIpContext *context)
Callback function for link change event.
Definition: auto_ip.c:440
void autoIpChangeState(AutoIpContext *context, AutoIpState newState, systime_t delay)
Update Auto-IP FSM state.
Definition: auto_ip.c:492
systime_t timeout
Timeout value.
Definition: auto_ip.h:199
AutoIpState state
Current state of the FSM.
Definition: auto_ip.h:196
uint_t retransmitCount
Retransmission counter.
Definition: auto_ip.h:200
uint_t conflictCount
Number of conflicts.
Definition: auto_ip.h:201
uint8_t ipAddr[4]
Definition: mib_common.h:185
void autoIpDumpConfig(AutoIpContext *context)
Dump Auto-IP configuration for debugging purpose.
Definition: auto_ip.c:545
AutoIpLinkChangeCallback linkChangeEvent
Link state change event.
Definition: auto_ip.h:183
AutoIpState
Auto-IP FSM states.
Definition: auto_ip.h:149
void(* AutoIpLinkChangeCallback)(AutoIpContext *context, NetInterface *interface, bool_t linkState)
Link state change callback.
Definition: auto_ip.h:163
systime_t timestamp
Timestamp to manage retransmissions.
Definition: auto_ip.h:198
void autoIpGetDefaultSettings(AutoIpSettings *settings)
Initialize settings with default values.
Definition: auto_ip.c:62
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
Ipv4Addr linkLocalAddr
Initial link-local address to be used.
Definition: auto_ip.h:182
#define NetInterface
Definition: net.h:34
NetInterface * interface
Network interface to configure.
Definition: auto_ip.h:181
AutoIpStateChangeCallback stateChangeEvent
FSM state change event.
Definition: auto_ip.h:184
AutoIpState autoIpGetState(AutoIpContext *context)
Retrieve current state.
Definition: auto_ip.c:206
int bool_t
Definition: compiler_port.h:47
Ipv4Addr linkLocalAddr
Link-local address.
Definition: auto_ip.h:197
#define AutoIpContext
Definition: auto_ip.h:137
error_t autoIpStart(AutoIpContext *context)
Start Auto-IP process.
Definition: auto_ip.c:130