diff options
Diffstat (limited to 'src/nat/gnunet-service-nat_externalip.c')
-rw-r--r-- | src/nat/gnunet-service-nat_externalip.c | 168 |
1 files changed, 86 insertions, 82 deletions
diff --git a/src/nat/gnunet-service-nat_externalip.c b/src/nat/gnunet-service-nat_externalip.c index c97f7f58b..0a251bdb2 100644 --- a/src/nat/gnunet-service-nat_externalip.c +++ b/src/nat/gnunet-service-nat_externalip.c | |||
@@ -54,25 +54,29 @@ | |||
54 | * How long do we wait until we re-try running `external-ip` if the | 54 | * How long do we wait until we re-try running `external-ip` if the |
55 | * command failed to terminate nicely? | 55 | * command failed to terminate nicely? |
56 | */ | 56 | */ |
57 | #define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 15) | 57 | #define EXTERN_IP_RETRY_TIMEOUT GNUNET_TIME_relative_multiply ( \ |
58 | GNUNET_TIME_UNIT_MINUTES, 15) | ||
58 | 59 | ||
59 | /** | 60 | /** |
60 | * How long do we wait until we re-try running `external-ip` if the | 61 | * How long do we wait until we re-try running `external-ip` if the |
61 | * command failed (but terminated)? | 62 | * command failed (but terminated)? |
62 | */ | 63 | */ |
63 | #define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30) | 64 | #define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply ( \ |
65 | GNUNET_TIME_UNIT_MINUTES, 30) | ||
64 | 66 | ||
65 | /** | 67 | /** |
66 | * How long do we wait until we re-try running `external-ip` if the | 68 | * How long do we wait until we re-try running `external-ip` if the |
67 | * command succeeded? | 69 | * command succeeded? |
68 | */ | 70 | */ |
69 | #define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5) | 71 | #define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply ( \ |
72 | GNUNET_TIME_UNIT_MINUTES, 5) | ||
70 | 73 | ||
71 | 74 | ||
72 | /** | 75 | /** |
73 | * Handle to monitor for external IP changes. | 76 | * Handle to monitor for external IP changes. |
74 | */ | 77 | */ |
75 | struct GN_ExternalIPMonitor { | 78 | struct GN_ExternalIPMonitor |
79 | { | ||
76 | /** | 80 | /** |
77 | * Kept in DLL. | 81 | * Kept in DLL. |
78 | */ | 82 | */ |
@@ -131,15 +135,15 @@ static struct in_addr mini_external_ipv4; | |||
131 | * @param v4 the external address that changed | 135 | * @param v4 the external address that changed |
132 | */ | 136 | */ |
133 | static void | 137 | static void |
134 | notify_monitors_external_ipv4_change(int add, | 138 | notify_monitors_external_ipv4_change (int add, |
135 | const struct in_addr *v4) | 139 | const struct in_addr *v4) |
136 | { | 140 | { |
137 | for (struct GN_ExternalIPMonitor *mon = mon_head; | 141 | for (struct GN_ExternalIPMonitor *mon = mon_head; |
138 | NULL != mon; | 142 | NULL != mon; |
139 | mon = mon->next) | 143 | mon = mon->next) |
140 | mon->cb(mon->cb_cls, | 144 | mon->cb (mon->cb_cls, |
141 | v4, | 145 | v4, |
142 | add); | 146 | add); |
143 | } | 147 | } |
144 | 148 | ||
145 | 149 | ||
@@ -150,7 +154,7 @@ notify_monitors_external_ipv4_change(int add, | |||
150 | * @param cls NULL | 154 | * @param cls NULL |
151 | */ | 155 | */ |
152 | static void | 156 | static void |
153 | run_external_ip(void *cls); | 157 | run_external_ip (void *cls); |
154 | 158 | ||
155 | 159 | ||
156 | /** | 160 | /** |
@@ -163,47 +167,47 @@ run_external_ip(void *cls); | |||
163 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code | 167 | * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code |
164 | */ | 168 | */ |
165 | static void | 169 | static void |
166 | handle_external_ip(void *cls, | 170 | handle_external_ip (void *cls, |
167 | const struct in_addr *addr, | 171 | const struct in_addr *addr, |
168 | enum GNUNET_NAT_StatusCode result) | 172 | enum GNUNET_NAT_StatusCode result) |
169 | { | 173 | { |
170 | char buf[INET_ADDRSTRLEN]; | 174 | char buf[INET_ADDRSTRLEN]; |
171 | 175 | ||
172 | probe_external_ip_op = NULL; | 176 | probe_external_ip_op = NULL; |
173 | GNUNET_SCHEDULER_cancel(probe_external_ip_task); | 177 | GNUNET_SCHEDULER_cancel (probe_external_ip_task); |
174 | probe_external_ip_task | 178 | probe_external_ip_task |
175 | = GNUNET_SCHEDULER_add_delayed((NULL == addr) | 179 | = GNUNET_SCHEDULER_add_delayed ((NULL == addr) |
176 | ? EXTERN_IP_RETRY_FAILURE | 180 | ? EXTERN_IP_RETRY_FAILURE |
177 | : EXTERN_IP_RETRY_SUCCESS, | 181 | : EXTERN_IP_RETRY_SUCCESS, |
178 | &run_external_ip, | 182 | &run_external_ip, |
179 | NULL); | 183 | NULL); |
180 | switch (result) | 184 | switch (result) |
181 | { | 185 | { |
182 | case GNUNET_NAT_ERROR_SUCCESS: | 186 | case GNUNET_NAT_ERROR_SUCCESS: |
183 | GNUNET_assert(NULL != addr); | 187 | GNUNET_assert (NULL != addr); |
184 | if (addr->s_addr == mini_external_ipv4.s_addr) | 188 | if (addr->s_addr == mini_external_ipv4.s_addr) |
185 | return; /* not change */ | 189 | return; /* not change */ |
186 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, | 190 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
187 | "Our external IP is now %s\n", | 191 | "Our external IP is now %s\n", |
188 | inet_ntop(AF_INET, | 192 | inet_ntop (AF_INET, |
189 | addr, | 193 | addr, |
190 | buf, | 194 | buf, |
191 | sizeof(buf))); | 195 | sizeof(buf))); |
192 | if (0 != mini_external_ipv4.s_addr) | 196 | if (0 != mini_external_ipv4.s_addr) |
193 | notify_monitors_external_ipv4_change(GNUNET_NO, | 197 | notify_monitors_external_ipv4_change (GNUNET_NO, |
194 | &mini_external_ipv4); | 198 | &mini_external_ipv4); |
195 | mini_external_ipv4 = *addr; | 199 | mini_external_ipv4 = *addr; |
196 | notify_monitors_external_ipv4_change(GNUNET_YES, | 200 | notify_monitors_external_ipv4_change (GNUNET_YES, |
197 | &mini_external_ipv4); | 201 | &mini_external_ipv4); |
198 | break; | 202 | break; |
199 | 203 | ||
200 | default: | 204 | default: |
201 | if (0 != mini_external_ipv4.s_addr) | 205 | if (0 != mini_external_ipv4.s_addr) |
202 | notify_monitors_external_ipv4_change(GNUNET_NO, | 206 | notify_monitors_external_ipv4_change (GNUNET_NO, |
203 | &mini_external_ipv4); | 207 | &mini_external_ipv4); |
204 | mini_external_ipv4.s_addr = 0; | 208 | mini_external_ipv4.s_addr = 0; |
205 | break; | 209 | break; |
206 | } | 210 | } |
207 | } | 211 | } |
208 | 212 | ||
209 | 213 | ||
@@ -214,20 +218,20 @@ handle_external_ip(void *cls, | |||
214 | * @param cls NULL | 218 | * @param cls NULL |
215 | */ | 219 | */ |
216 | static void | 220 | static void |
217 | run_external_ip(void *cls) | 221 | run_external_ip (void *cls) |
218 | { | 222 | { |
219 | probe_external_ip_task | 223 | probe_external_ip_task |
220 | = GNUNET_SCHEDULER_add_delayed(EXTERN_IP_RETRY_TIMEOUT, | 224 | = GNUNET_SCHEDULER_add_delayed (EXTERN_IP_RETRY_TIMEOUT, |
221 | &run_external_ip, | 225 | &run_external_ip, |
222 | NULL); | 226 | NULL); |
223 | if (NULL != probe_external_ip_op) | 227 | if (NULL != probe_external_ip_op) |
224 | { | 228 | { |
225 | GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op); | 229 | GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); |
226 | probe_external_ip_op = NULL; | 230 | probe_external_ip_op = NULL; |
227 | } | 231 | } |
228 | probe_external_ip_op | 232 | probe_external_ip_op |
229 | = GNUNET_NAT_mini_get_external_ipv4_(&handle_external_ip, | 233 | = GNUNET_NAT_mini_get_external_ipv4_ (&handle_external_ip, |
230 | NULL); | 234 | NULL); |
231 | } | 235 | } |
232 | 236 | ||
233 | 237 | ||
@@ -238,32 +242,32 @@ run_external_ip(void *cls) | |||
238 | * @param have_nat #GNUNET_YES if we believe we are behind NAT | 242 | * @param have_nat #GNUNET_YES if we believe we are behind NAT |
239 | */ | 243 | */ |
240 | void | 244 | void |
241 | GN_nat_status_changed(int have_nat) | 245 | GN_nat_status_changed (int have_nat) |
242 | { | 246 | { |
243 | if (GNUNET_YES != enable_upnp) | 247 | if (GNUNET_YES != enable_upnp) |
244 | return; | 248 | return; |
245 | if ((GNUNET_YES == have_nat) && | 249 | if ((GNUNET_YES == have_nat) && |
246 | (NULL == probe_external_ip_task) && | 250 | (NULL == probe_external_ip_task) && |
247 | (NULL == probe_external_ip_op)) | 251 | (NULL == probe_external_ip_op)) |
252 | { | ||
253 | probe_external_ip_task | ||
254 | = GNUNET_SCHEDULER_add_now (&run_external_ip, | ||
255 | NULL); | ||
256 | return; | ||
257 | } | ||
258 | if (GNUNET_NO == have_nat) | ||
259 | { | ||
260 | if (NULL != probe_external_ip_task) | ||
248 | { | 261 | { |
249 | probe_external_ip_task | 262 | GNUNET_SCHEDULER_cancel (probe_external_ip_task); |
250 | = GNUNET_SCHEDULER_add_now(&run_external_ip, | 263 | probe_external_ip_task = NULL; |
251 | NULL); | ||
252 | return; | ||
253 | } | 264 | } |
254 | if (GNUNET_NO == have_nat) | 265 | if (NULL != probe_external_ip_op) |
255 | { | 266 | { |
256 | if (NULL != probe_external_ip_task) | 267 | GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); |
257 | { | 268 | probe_external_ip_op = NULL; |
258 | GNUNET_SCHEDULER_cancel(probe_external_ip_task); | ||
259 | probe_external_ip_task = NULL; | ||
260 | } | ||
261 | if (NULL != probe_external_ip_op) | ||
262 | { | ||
263 | GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op); | ||
264 | probe_external_ip_op = NULL; | ||
265 | } | ||
266 | } | 269 | } |
270 | } | ||
267 | } | 271 | } |
268 | 272 | ||
269 | 273 | ||
@@ -275,21 +279,21 @@ GN_nat_status_changed(int have_nat) | |||
275 | * @return handle to cancel | 279 | * @return handle to cancel |
276 | */ | 280 | */ |
277 | struct GN_ExternalIPMonitor * | 281 | struct GN_ExternalIPMonitor * |
278 | GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, | 282 | GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, |
279 | void *cb_cls) | 283 | void *cb_cls) |
280 | { | 284 | { |
281 | struct GN_ExternalIPMonitor *mon; | 285 | struct GN_ExternalIPMonitor *mon; |
282 | 286 | ||
283 | mon = GNUNET_new(struct GN_ExternalIPMonitor); | 287 | mon = GNUNET_new (struct GN_ExternalIPMonitor); |
284 | mon->cb = cb; | 288 | mon->cb = cb; |
285 | mon->cb_cls = cb_cls; | 289 | mon->cb_cls = cb_cls; |
286 | GNUNET_CONTAINER_DLL_insert(mon_head, | 290 | GNUNET_CONTAINER_DLL_insert (mon_head, |
287 | mon_tail, | 291 | mon_tail, |
288 | mon); | 292 | mon); |
289 | if (0 != mini_external_ipv4.s_addr) | 293 | if (0 != mini_external_ipv4.s_addr) |
290 | cb(cb_cls, | 294 | cb (cb_cls, |
291 | &mini_external_ipv4, | 295 | &mini_external_ipv4, |
292 | GNUNET_YES); | 296 | GNUNET_YES); |
293 | return mon; | 297 | return mon; |
294 | } | 298 | } |
295 | 299 | ||
@@ -300,12 +304,12 @@ GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb, | |||
300 | * @param mon monitor to call | 304 | * @param mon monitor to call |
301 | */ | 305 | */ |
302 | void | 306 | void |
303 | GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon) | 307 | GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon) |
304 | { | 308 | { |
305 | GNUNET_CONTAINER_DLL_remove(mon_head, | 309 | GNUNET_CONTAINER_DLL_remove (mon_head, |
306 | mon_tail, | 310 | mon_tail, |
307 | mon); | 311 | mon); |
308 | GNUNET_free(mon); | 312 | GNUNET_free (mon); |
309 | } | 313 | } |
310 | 314 | ||
311 | /* end of gnunet-service-nat_externalip.c */ | 315 | /* end of gnunet-service-nat_externalip.c */ |