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