aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-service-nat_externalip.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat/gnunet-service-nat_externalip.c')
-rw-r--r--src/nat/gnunet-service-nat_externalip.c181
1 files changed, 90 insertions, 91 deletions
diff --git a/src/nat/gnunet-service-nat_externalip.c b/src/nat/gnunet-service-nat_externalip.c
index 282b070d3..c97f7f58b 100644
--- a/src/nat/gnunet-service-nat_externalip.c
+++ b/src/nat/gnunet-service-nat_externalip.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * Code to figure out what our external IPv4 address(es) might 21 * Code to figure out what our external IPv4 address(es) might
22 * be (external IPv4s are what is seen on the rest of the Internet). 22 * be (external IPv4s are what is seen on the rest of the Internet).
@@ -54,26 +54,25 @@
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(GNUNET_TIME_UNIT_MINUTES, 15)
58 58
59/** 59/**
60 * How long do we wait until we re-try running `external-ip` if the 60 * How long do we wait until we re-try running `external-ip` if the
61 * command failed (but terminated)? 61 * command failed (but terminated)?
62 */ 62 */
63#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30) 63#define EXTERN_IP_RETRY_FAILURE GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
64 64
65/** 65/**
66 * How long do we wait until we re-try running `external-ip` if the 66 * How long do we wait until we re-try running `external-ip` if the
67 * command succeeded? 67 * command succeeded?
68 */ 68 */
69#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 69#define EXTERN_IP_RETRY_SUCCESS GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
70 70
71 71
72/** 72/**
73 * Handle to monitor for external IP changes. 73 * Handle to monitor for external IP changes.
74 */ 74 */
75struct GN_ExternalIPMonitor 75struct GN_ExternalIPMonitor {
76{
77 /** 76 /**
78 * Kept in DLL. 77 * Kept in DLL.
79 */ 78 */
@@ -93,7 +92,6 @@ struct GN_ExternalIPMonitor
93 * Closure for @e cb. 92 * Closure for @e cb.
94 */ 93 */
95 void *cb_cls; 94 void *cb_cls;
96
97}; 95};
98 96
99 97
@@ -133,15 +131,15 @@ static struct in_addr mini_external_ipv4;
133 * @param v4 the external address that changed 131 * @param v4 the external address that changed
134 */ 132 */
135static void 133static void
136notify_monitors_external_ipv4_change (int add, 134notify_monitors_external_ipv4_change(int add,
137 const struct in_addr *v4) 135 const struct in_addr *v4)
138{ 136{
139 for (struct GN_ExternalIPMonitor *mon = mon_head; 137 for (struct GN_ExternalIPMonitor *mon = mon_head;
140 NULL != mon; 138 NULL != mon;
141 mon = mon->next) 139 mon = mon->next)
142 mon->cb (mon->cb_cls, 140 mon->cb(mon->cb_cls,
143 v4, 141 v4,
144 add); 142 add);
145} 143}
146 144
147 145
@@ -152,7 +150,7 @@ notify_monitors_external_ipv4_change (int add,
152 * @param cls NULL 150 * @param cls NULL
153 */ 151 */
154static void 152static void
155run_external_ip (void *cls); 153run_external_ip(void *cls);
156 154
157 155
158/** 156/**
@@ -165,46 +163,47 @@ run_external_ip (void *cls);
165 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code 163 * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
166 */ 164 */
167static void 165static void
168handle_external_ip (void *cls, 166handle_external_ip(void *cls,
169 const struct in_addr *addr, 167 const struct in_addr *addr,
170 enum GNUNET_NAT_StatusCode result) 168 enum GNUNET_NAT_StatusCode result)
171{ 169{
172 char buf[INET_ADDRSTRLEN]; 170 char buf[INET_ADDRSTRLEN];
173 171
174 probe_external_ip_op = NULL; 172 probe_external_ip_op = NULL;
175 GNUNET_SCHEDULER_cancel (probe_external_ip_task); 173 GNUNET_SCHEDULER_cancel(probe_external_ip_task);
176 probe_external_ip_task 174 probe_external_ip_task
177 = GNUNET_SCHEDULER_add_delayed ((NULL == addr) 175 = GNUNET_SCHEDULER_add_delayed((NULL == addr)
178 ? EXTERN_IP_RETRY_FAILURE 176 ? EXTERN_IP_RETRY_FAILURE
179 : EXTERN_IP_RETRY_SUCCESS, 177 : EXTERN_IP_RETRY_SUCCESS,
180 &run_external_ip, 178 &run_external_ip,
181 NULL); 179 NULL);
182 switch (result) 180 switch (result)
183 { 181 {
184 case GNUNET_NAT_ERROR_SUCCESS: 182 case GNUNET_NAT_ERROR_SUCCESS:
185 GNUNET_assert (NULL != addr); 183 GNUNET_assert(NULL != addr);
186 if (addr->s_addr == mini_external_ipv4.s_addr) 184 if (addr->s_addr == mini_external_ipv4.s_addr)
187 return; /* not change */ 185 return; /* not change */
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
189 "Our external IP is now %s\n", 187 "Our external IP is now %s\n",
190 inet_ntop (AF_INET, 188 inet_ntop(AF_INET,
191 addr, 189 addr,
192 buf, 190 buf,
193 sizeof (buf))); 191 sizeof(buf)));
194 if (0 != mini_external_ipv4.s_addr) 192 if (0 != mini_external_ipv4.s_addr)
195 notify_monitors_external_ipv4_change (GNUNET_NO, 193 notify_monitors_external_ipv4_change(GNUNET_NO,
196 &mini_external_ipv4); 194 &mini_external_ipv4);
197 mini_external_ipv4 = *addr; 195 mini_external_ipv4 = *addr;
198 notify_monitors_external_ipv4_change (GNUNET_YES, 196 notify_monitors_external_ipv4_change(GNUNET_YES,
199 &mini_external_ipv4); 197 &mini_external_ipv4);
200 break; 198 break;
201 default: 199
202 if (0 != mini_external_ipv4.s_addr) 200 default:
203 notify_monitors_external_ipv4_change (GNUNET_NO, 201 if (0 != mini_external_ipv4.s_addr)
204 &mini_external_ipv4); 202 notify_monitors_external_ipv4_change(GNUNET_NO,
205 mini_external_ipv4.s_addr = 0; 203 &mini_external_ipv4);
206 break; 204 mini_external_ipv4.s_addr = 0;
207 } 205 break;
206 }
208} 207}
209 208
210 209
@@ -215,20 +214,20 @@ handle_external_ip (void *cls,
215 * @param cls NULL 214 * @param cls NULL
216 */ 215 */
217static void 216static void
218run_external_ip (void *cls) 217run_external_ip(void *cls)
219{ 218{
220 probe_external_ip_task 219 probe_external_ip_task
221 = GNUNET_SCHEDULER_add_delayed (EXTERN_IP_RETRY_TIMEOUT, 220 = GNUNET_SCHEDULER_add_delayed(EXTERN_IP_RETRY_TIMEOUT,
222 &run_external_ip, 221 &run_external_ip,
223 NULL); 222 NULL);
224 if (NULL != probe_external_ip_op) 223 if (NULL != probe_external_ip_op)
225 { 224 {
226 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); 225 GNUNET_NAT_mini_get_external_ipv4_cancel_(probe_external_ip_op);
227 probe_external_ip_op = NULL; 226 probe_external_ip_op = NULL;
228 } 227 }
229 probe_external_ip_op 228 probe_external_ip_op
230 = GNUNET_NAT_mini_get_external_ipv4_ (&handle_external_ip, 229 = GNUNET_NAT_mini_get_external_ipv4_(&handle_external_ip,
231 NULL); 230 NULL);
232} 231}
233 232
234 233
@@ -239,32 +238,32 @@ run_external_ip (void *cls)
239 * @param have_nat #GNUNET_YES if we believe we are behind NAT 238 * @param have_nat #GNUNET_YES if we believe we are behind NAT
240 */ 239 */
241void 240void
242GN_nat_status_changed (int have_nat) 241GN_nat_status_changed(int have_nat)
243{ 242{
244 if (GNUNET_YES != enable_upnp) 243 if (GNUNET_YES != enable_upnp)
245 return; 244 return;
246 if ( (GNUNET_YES == have_nat) && 245 if ((GNUNET_YES == have_nat) &&
247 (NULL == probe_external_ip_task) && 246 (NULL == probe_external_ip_task) &&
248 (NULL == probe_external_ip_op) ) 247 (NULL == probe_external_ip_op))
249 {
250 probe_external_ip_task
251 = GNUNET_SCHEDULER_add_now (&run_external_ip,
252 NULL);
253 return;
254 }
255 if (GNUNET_NO == have_nat)
256 {
257 if (NULL != probe_external_ip_task)
258 { 248 {
259 GNUNET_SCHEDULER_cancel (probe_external_ip_task); 249 probe_external_ip_task
260 probe_external_ip_task = NULL; 250 = GNUNET_SCHEDULER_add_now(&run_external_ip,
251 NULL);
252 return;
261 } 253 }
262 if (NULL != probe_external_ip_op) 254 if (GNUNET_NO == have_nat)
263 { 255 {
264 GNUNET_NAT_mini_get_external_ipv4_cancel_ (probe_external_ip_op); 256 if (NULL != probe_external_ip_task)
265 probe_external_ip_op = NULL; 257 {
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 } 266 }
267 }
268} 267}
269 268
270 269
@@ -276,21 +275,21 @@ GN_nat_status_changed (int have_nat)
276 * @return handle to cancel 275 * @return handle to cancel
277 */ 276 */
278struct GN_ExternalIPMonitor * 277struct GN_ExternalIPMonitor *
279GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb, 278GN_external_ipv4_monitor_start(GN_NotifyExternalIPv4Change cb,
280 void *cb_cls) 279 void *cb_cls)
281{ 280{
282 struct GN_ExternalIPMonitor *mon; 281 struct GN_ExternalIPMonitor *mon;
283 282
284 mon = GNUNET_new (struct GN_ExternalIPMonitor); 283 mon = GNUNET_new(struct GN_ExternalIPMonitor);
285 mon->cb = cb; 284 mon->cb = cb;
286 mon->cb_cls = cb_cls; 285 mon->cb_cls = cb_cls;
287 GNUNET_CONTAINER_DLL_insert (mon_head, 286 GNUNET_CONTAINER_DLL_insert(mon_head,
288 mon_tail, 287 mon_tail,
289 mon); 288 mon);
290 if (0 != mini_external_ipv4.s_addr) 289 if (0 != mini_external_ipv4.s_addr)
291 cb (cb_cls, 290 cb(cb_cls,
292 &mini_external_ipv4, 291 &mini_external_ipv4,
293 GNUNET_YES); 292 GNUNET_YES);
294 return mon; 293 return mon;
295} 294}
296 295
@@ -301,12 +300,12 @@ GN_external_ipv4_monitor_start (GN_NotifyExternalIPv4Change cb,
301 * @param mon monitor to call 300 * @param mon monitor to call
302 */ 301 */
303void 302void
304GN_external_ipv4_monitor_stop (struct GN_ExternalIPMonitor *mon) 303GN_external_ipv4_monitor_stop(struct GN_ExternalIPMonitor *mon)
305{ 304{
306 GNUNET_CONTAINER_DLL_remove (mon_head, 305 GNUNET_CONTAINER_DLL_remove(mon_head,
307 mon_tail, 306 mon_tail,
308 mon); 307 mon);
309 GNUNET_free (mon); 308 GNUNET_free(mon);
310} 309}
311 310
312/* end of gnunet-service-nat_externalip.c */ 311/* end of gnunet-service-nat_externalip.c */