pidgin/pidgin

Cleanup local IP getters

2020-12-20, Elliott Sales de Andrade
2453bef0abe7
Parents 80764d18e588
Children 52feb9438df4
Cleanup local IP getters

* Rename `purple_network_get_my_ip` to `purple_network_discover_my_ip`.
Nothing actually cares about what it returns, so stop returning anything.
* Use libnice to replace `purple_network_get_local_system_ip`.

Testing Done:
Compile only.

Reviewed at https://reviews.imfreedom.org/r/296/
--- a/ChangeLog.API Sun Dec 20 00:47:19 2020 -0600
+++ b/ChangeLog.API Sun Dec 20 00:51:00 2020 -0600
@@ -188,10 +188,6 @@
* PurpleLog, purple_log_new, purple_log_write and
PurpleLogLogger->write take a GDateTime instead of a time_t
and struct tm
- * purple_network_get_local_system_ip no longer takes a file
- descriptor as first parameter
- * purple_network_get_my_ip no longer takes a file descriptor
- as first parameter
* PurpleNotifyMsgType renamed to PurpleNotifyMessageType
* purple_notify_user_info_add_pair renamed to
purple_notify_user_info_add_pair_html
@@ -395,6 +391,8 @@
* purple_network_convert_idn_to_ascii. Use g_hostname_to_ascii,
instead.
* purple_network_get_all_local_system_ips. Use libnice instead.
+ * purple_network_get_my_ip
+ * purple_network_get_local_system_ip. Use libnice instead.
* purple_network_get_port_from_fd
* purple_network_ip_atoi
* PurpleNetworkListenData
--- a/libpurple/core.c Sun Dec 20 00:47:19 2020 -0600
+++ b/libpurple/core.c Sun Dec 20 00:51:00 2020 -0600
@@ -188,7 +188,7 @@
* Call this early on to try to auto-detect our IP address and
* hopefully save some time later.
*/
- purple_network_get_my_ip();
+ purple_network_discover_my_ip();
if (ops != NULL && ops->ui_init != NULL)
ops->ui_init();
--- a/libpurple/network.c Sun Dec 20 00:47:19 2020 -0600
+++ b/libpurple/network.c Sun Dec 20 00:51:00 2020 -0600
@@ -86,60 +86,6 @@
return purple_prefs_get_string("/purple/network/public_ip");
}
-const gchar *
-purple_network_get_local_system_ip(void)
-{
- struct ifreq buffer[100];
- guchar *it, *it_end;
- static char ip[16];
- struct ifconf ifc;
- struct ifreq *ifr;
- struct sockaddr_in *sinptr;
- guint32 lhost = g_htonl((127 << 24) + 1); /* 127.0.0.1 */
- long unsigned int add;
- int source;
-
- source = socket(PF_INET, SOCK_STREAM, 0);
-
- ifc.ifc_len = sizeof(buffer);
- ifc.ifc_req = buffer;
- ioctl(source, SIOCGIFCONF, &ifc);
-
- if (source >= 0) {
- close(source);
- }
-
- it = (guchar*)buffer;
- it_end = it + ifc.ifc_len;
- while (it < it_end) {
- /* in this case "it" is:
- * a) (struct ifreq)-aligned
- * b) not aligned, because of OS quirks (see
- * _SIZEOF_ADDR_IFREQ), so the OS should deal with it.
- */
- ifr = (struct ifreq *)(gpointer)it;
- it += HX_SIZE_OF_IFREQ(*ifr);
-
- if (ifr->ifr_addr.sa_family == AF_INET)
- {
- sinptr = (struct sockaddr_in *)(gpointer)&ifr->ifr_addr;
- if (sinptr->sin_addr.s_addr != lhost)
- {
- add = g_ntohl(sinptr->sin_addr.s_addr);
- g_snprintf(ip, 16, "%lu.%lu.%lu.%lu",
- ((add >> 24) & 255),
- ((add >> 16) & 255),
- ((add >> 8) & 255),
- add & 255);
-
- return ip;
- }
- }
- }
-
- return "0.0.0.0";
-}
-
static gchar *
purple_network_get_local_system_ip_from_gio(GSocketConnection *sockconn)
{
@@ -183,8 +129,8 @@
return g_hostname_is_ip_address(hostname);
}
-const gchar *
-purple_network_get_my_ip(void)
+void
+purple_network_discover_my_ip(void)
{
const char *ip = NULL;
PurpleStunNatDiscovery *stun;
@@ -193,27 +139,28 @@
if (!purple_prefs_get_bool("/purple/network/auto_ip")) {
ip = purple_network_get_public_ip();
/* Make sure the IP address entered by the user is valid */
- if ((ip != NULL) && (purple_network_is_ipv4(ip)))
- return ip;
- } else {
- /* Check if STUN discovery was already done */
- stun = purple_stun_discover(NULL);
- if ((stun != NULL) && (stun->status == PURPLE_STUN_STATUS_DISCOVERED))
- return stun->publicip;
+ if (ip != NULL && purple_network_is_ipv4(ip)) {
+ return;
+ }
+ }
- /* Attempt to get the IP from a NAT device using UPnP */
- ip = purple_upnp_get_public_ip();
- if (ip != NULL)
- return ip;
-
- /* Attempt to get the IP from a NAT device using NAT-PMP */
- ip = purple_pmp_get_public_ip();
- if (ip != NULL)
- return ip;
+ /* Check if STUN discovery was already done */
+ stun = purple_stun_discover(NULL);
+ if (stun != NULL && stun->status == PURPLE_STUN_STATUS_DISCOVERED) {
+ return;
}
- /* Just fetch the IP of the local system */
- return purple_network_get_local_system_ip();
+ /* Attempt to get the IP from a NAT device using UPnP */
+ ip = purple_upnp_get_public_ip();
+ if (ip != NULL) {
+ return;
+ }
+
+ /* Attempt to get the IP from a NAT device using NAT-PMP */
+ ip = purple_pmp_get_public_ip();
+ if (ip != NULL) {
+ return;
+ }
}
gchar *
--- a/libpurple/network.h Sun Dec 20 00:47:19 2020 -0600
+++ b/libpurple/network.h Sun Dec 20 00:51:00 2020 -0600
@@ -63,46 +63,24 @@
* Returns the IP address of the local system set in preferences.
*
* This returns the value set via purple_network_set_public_ip().
- * You probably want to use purple_network_get_my_ip() instead.
*
* Returns: The local IP address set in preferences.
*/
const char *purple_network_get_public_ip(void);
/**
- * purple_network_get_local_system_ip:
- *
- * Returns the IP address of the local system.
- *
- * You probably want to use purple_network_get_my_ip() instead.
+ * purple_network_discover_my_ip:
*
- * Note: The returned string is a pointer to a static buffer. If this
- * function is called twice, it may be important to make a copy
- * of the returned string.
- *
- * Returns: The local IP address.
- */
-const gchar *purple_network_get_local_system_ip(void);
-
-/**
- * purple_network_get_my_ip:
+ * Discovers the IP address that should be used anywhere a public IP addresses
+ * is needed (listening for an incoming file transfer, etc).
*
- * Returns the IP address that should be used anywhere a
- * public IP addresses is needed (listening for an incoming
- * file transfer, etc).
+ * If the user has manually specified an IP address via preferences, then this
+ * is used. Otherwise STUN, UPnP, and NAT-PMP will be attempted to discover
+ * the local IP address depending on what's available.
*
- * If the user has manually specified an IP address via
- * preferences, then this IP is returned. Otherwise the
- * IP address returned by purple_network_get_local_system_ip()
- * is returned.
- *
- * Note: The returned string is a pointer to a static buffer. If this
- * function is called twice, it may be important to make a copy
- * of the returned string.
- *
- * Returns: The local IP address to be used.
+ * Since: 3.0.0
*/
-const gchar *purple_network_get_my_ip(void);
+void purple_network_discover_my_ip(void);
/**
* purple_network_get_my_ip_from_gio:
@@ -112,7 +90,7 @@
* needed (listening for an incoming file transfer, etc).
*
* If the user has manually specified an IP address via preferences, then this
- * IP is returned. Otherwise STUN, UPNP, NAT-PMP, and finally GIO will be
+ * IP is returned. Otherwise STUN, UPnP, NAT-PMP, and finally GIO will be
* attempted to discover the local IP address depending on what's available.
*
* Returns: The local IP address to be used.
--- a/pidgin/gtkprefs.c Sun Dec 20 00:47:19 2020 -0600
+++ b/pidgin/gtkprefs.c Sun Dec 20 00:51:00 2020 -0600
@@ -29,7 +29,7 @@
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
-
+#include <nice.h>
#include <talkatu.h>
#include <purple.h>
@@ -1760,11 +1760,9 @@
const char *ip;
PurpleStunNatDiscovery *stun;
char *auto_ip_text;
-
- /* purple_network_get_my_ip will return the IP that was set by the user with
- purple_network_set_public_ip, so make a lookup for the auto-detected IP
- ourselves. */
-
+ GList *list = NULL;
+
+ /* Make a lookup for the auto-detected IP ourselves. */
if (purple_prefs_get_bool("/purple/network/auto_ip")) {
/* Check if STUN discovery was already done */
stun = purple_stun_discover(NULL);
@@ -1777,18 +1775,24 @@
/* Attempt to get the IP from a NAT device using NAT-PMP */
ip = purple_pmp_get_public_ip();
if (ip == NULL) {
- /* Just fetch the IP of the local system */
- ip = purple_network_get_local_system_ip();
+ /* Just fetch the first IP of the local system */
+ list = nice_interfaces_get_local_ips(FALSE);
+ if (list) {
+ ip = list->data;
+ } else {
+ ip = "0.0.0.0";
+ }
}
}
}
+ } else{
+ ip = _("Disabled");
}
- else
- ip = _("Disabled");
auto_ip_text = g_strdup_printf(_("Use _automatically detected IP address: %s"), ip);
gtk_button_set_label(GTK_BUTTON(button), auto_ip_text);
g_free(auto_ip_text);
+ g_list_free_full(list, g_free);
}
static void
--- a/pidgin/meson.build Sun Dec 20 00:47:19 2020 -0600
+++ b/pidgin/meson.build Sun Dec 20 00:51:00 2020 -0600
@@ -208,6 +208,7 @@
IOKIT,
json,
math,
+ nice,
libsoup,
talkatu_dep,
libpurple_dep,