Mercurial > pidgin
comparison libpurple/network.c @ 15646:552be3958d6a
Added nat-pmp implementation and #ifdef'd out changes to network.c which would utilize it.
author | Evan Schoenberg <evan.s@dreskin.net> |
---|---|
date | Sun, 18 Feb 2007 18:26:55 +0000 |
parents | 56a2a0bb290a |
children | 32c366eeeb99 |
comparison
equal
deleted
inserted
replaced
15645:61b42cf81aa4 | 15646:552be3958d6a |
---|---|
45 #include "network.h" | 45 #include "network.h" |
46 #include "prefs.h" | 46 #include "prefs.h" |
47 #include "stun.h" | 47 #include "stun.h" |
48 #include "upnp.h" | 48 #include "upnp.h" |
49 | 49 |
50 #ifdef ENABLE_NAT_PMP | |
51 #include "nat-pmp.h" | |
52 #endif | |
53 | |
50 /* | 54 /* |
51 * Calling sizeof(struct ifreq) isn't always correct on | 55 * Calling sizeof(struct ifreq) isn't always correct on |
52 * Mac OS X (and maybe others). | 56 * Mac OS X (and maybe others). |
53 */ | 57 */ |
54 #ifdef _SIZEOF_ADDR_IFREQ | 58 #ifdef _SIZEOF_ADDR_IFREQ |
73 gboolean retry; | 77 gboolean retry; |
74 gboolean adding; | 78 gboolean adding; |
75 GaimNetworkListenCallback cb; | 79 GaimNetworkListenCallback cb; |
76 gpointer cb_data; | 80 gpointer cb_data; |
77 UPnPMappingAddRemove *mapping_data; | 81 UPnPMappingAddRemove *mapping_data; |
82 #ifdef ENABLE_NAT_PMP | |
83 gboolean has_pmp_mapping; | |
84 #endif | |
78 }; | 85 }; |
79 | 86 |
80 #ifdef HAVE_LIBNM | 87 #ifdef HAVE_LIBNM |
81 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data); | 88 void nm_callback_func(libnm_glib_ctx* ctx, gpointer user_data); |
82 #endif | 89 #endif |
189 | 196 |
190 /* Attempt to get the IP from a NAT device using UPnP */ | 197 /* Attempt to get the IP from a NAT device using UPnP */ |
191 ip = gaim_upnp_get_public_ip(); | 198 ip = gaim_upnp_get_public_ip(); |
192 if (ip != NULL) | 199 if (ip != NULL) |
193 return ip; | 200 return ip; |
201 | |
202 #ifdef ENABLE_NAT_PMP | |
203 /* Attempt to ge tthe IP from a NAT device using NAT-PMP */ | |
204 ip = gaim_pmp_get_public_ip(); | |
205 if (ip != NULL) | |
206 return ip; | |
207 #endif | |
194 | 208 |
195 /* Just fetch the IP of the local system */ | 209 /* Just fetch the IP of the local system */ |
196 return gaim_network_get_local_system_ip(fd); | 210 return gaim_network_get_local_system_ip(fd); |
197 } | 211 } |
198 | 212 |
242 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) | 256 gaim_network_do_listen(unsigned short port, int socket_type, GaimNetworkListenCallback cb, gpointer cb_data) |
243 { | 257 { |
244 int listenfd = -1; | 258 int listenfd = -1; |
245 const int on = 1; | 259 const int on = 1; |
246 GaimNetworkListenData *listen_data; | 260 GaimNetworkListenData *listen_data; |
261 unsigned short actual_port; | |
247 #ifdef HAVE_GETADDRINFO | 262 #ifdef HAVE_GETADDRINFO |
248 int errnum; | 263 int errnum; |
249 struct addrinfo hints, *res, *next; | 264 struct addrinfo hints, *res, *next; |
250 char serv[6]; | 265 char serv[6]; |
251 | 266 |
318 close(listenfd); | 333 close(listenfd); |
319 return NULL; | 334 return NULL; |
320 } | 335 } |
321 fcntl(listenfd, F_SETFL, O_NONBLOCK); | 336 fcntl(listenfd, F_SETFL, O_NONBLOCK); |
322 | 337 |
323 gaim_debug_info("network", "Listening on port: %hu\n", gaim_network_get_port_from_fd(listenfd)); | 338 actual_port = gaim_network_get_port_from_fd(listenfd); |
324 | 339 |
340 gaim_debug_info("network", "Listening on port: %hu\n", actual_port); | |
341 | |
325 listen_data = g_new0(GaimNetworkListenData, 1); | 342 listen_data = g_new0(GaimNetworkListenData, 1); |
326 listen_data->listenfd = listenfd; | 343 listen_data->listenfd = listenfd; |
327 listen_data->adding = TRUE; | 344 listen_data->adding = TRUE; |
328 listen_data->retry = TRUE; | 345 listen_data->retry = TRUE; |
329 listen_data->cb = cb; | 346 listen_data->cb = cb; |
330 listen_data->cb_data = cb_data; | 347 listen_data->cb_data = cb_data; |
331 | 348 |
349 #ifdef ENABLE_NAT_PMP | |
350 /* Attempt a NAT-PMP Mapping, which will return immediately */ | |
351 listen_data->has_pmp_mapping = (gaim_pmp_create_map(((socket_type == SOCK_STREAM) ? GAIM_PMP_TYPE_TCP : GAIM_PMP_TYPE_UDP), | |
352 actual_port, actual_port, GAIM_PMP_LIFETIME) != NULL); | |
353 #endif | |
354 | |
355 /* Attempt a UPnP Mapping */ | |
332 listen_data->mapping_data = gaim_upnp_set_port_mapping( | 356 listen_data->mapping_data = gaim_upnp_set_port_mapping( |
333 gaim_network_get_port_from_fd(listenfd), | 357 actual_port, |
334 (socket_type == SOCK_STREAM) ? "TCP" : "UDP", | 358 (socket_type == SOCK_STREAM) ? "TCP" : "UDP", |
335 gaim_network_set_upnp_port_mapping_cb, listen_data); | 359 gaim_network_set_upnp_port_mapping_cb, listen_data); |
336 | 360 |
337 return listen_data; | 361 return listen_data; |
338 } | 362 } |
371 | 395 |
372 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data) | 396 void gaim_network_listen_cancel(GaimNetworkListenData *listen_data) |
373 { | 397 { |
374 if (listen_data->mapping_data != NULL) | 398 if (listen_data->mapping_data != NULL) |
375 gaim_upnp_cancel_port_mapping(listen_data->mapping_data); | 399 gaim_upnp_cancel_port_mapping(listen_data->mapping_data); |
400 | |
401 #ifdef ENABLE_NAT_PMP | |
402 if (listen_data->has_pmp_mapping) | |
403 gaim_pmp_destroy_map(((listen_data->socket_type == SOCK_STREAM) ? GAIM_PMP_TYPE_TCP : GAIM_PMP_TYPE_UDP), | |
404 gaim_network_get_port_from_fd(listen_data->listenfd)); | |
405 #endif | |
376 | 406 |
377 g_free(listen_data); | 407 g_free(listen_data); |
378 } | 408 } |
379 | 409 |
380 unsigned short | 410 unsigned short |