pidgin/pidgin

Cleanup some stun bits

2020-12-11, Elliott Sales de Andrade
18050336a45f
Parents 172be5d704ed
Children 1c9bdf8d3e85
Cleanup some stun bits

* Remove PurpleStunNatType, which nothing ever reads.
* Use GInetAddress to convert STUN public IP to a string.
* Use GLib byte order macros in stun.

Testing Done:
compile only.

Reviewed at https://reviews.imfreedom.org/r/269/
--- a/ChangeLog.API Thu Dec 10 02:58:55 2020 -0600
+++ b/ChangeLog.API Fri Dec 11 01:48:49 2020 -0600
@@ -486,6 +486,8 @@
* purple_srv_cancel
* purple_srv_resolve_account
* purple_srv_txt_query_destroy
+ * PurpleStunNatType
+ * PurpleStunNatDiscovery.type
* PURPLE_SSL_DEFAULT_PORT
* PurpleSslConnection
* PurpleSslErrorFunction
--- a/libpurple/stun.c Thu Dec 10 02:58:55 2020 -0600
+++ b/libpurple/stun.c Fri Dec 11 01:48:49 2020 -0600
@@ -75,7 +75,6 @@
static PurpleStunNatDiscovery nattype = {
PURPLE_STUN_STATUS_UNDISCOVERED,
- PURPLE_STUN_NAT_TYPE_PUBLIC_IP,
"\0", NULL, 0};
static GSList *callbacks = NULL;
@@ -149,19 +148,12 @@
{
struct stun_conn *sc = data;
gchar buffer[65536];
- struct ifreq buffer_ifr[1000];
- gchar *it, *it_end;
+ gchar *it;
gssize len;
- struct in_addr in;
struct stun_attrib attrib;
struct stun_header hdr;
- struct ifconf ifc;
- struct ifreq *ifr;
- struct sockaddr_in *sinptr;
GError *error = NULL;
- memset(&in, 0, sizeof(in));
-
len = g_socket_receive(socket, buffer, sizeof(buffer) - 1, NULL, &error);
if (len <= 0) {
purple_debug_warning("stun", "unable to read stun response: %s",
@@ -179,7 +171,7 @@
}
memcpy(&hdr, buffer, sizeof(hdr));
- if ((gsize)len != (ntohs(hdr.len) + sizeof(struct stun_header))) {
+ if ((gsize)len != (g_ntohs(hdr.len) + sizeof(struct stun_header))) {
purple_debug_warning("stun", "got incomplete response\n");
sc->incb = 0;
return FALSE;
@@ -207,53 +199,32 @@
memcpy(&attrib, it, sizeof(attrib));
it += sizeof(struct stun_attrib);
- if (!((buffer + len) > (it + ntohs(attrib.len)))) {
+ if (!((buffer + len) > (it + g_ntohs(attrib.len)))) {
break;
}
- if (attrib.type == htons(ATTRIB_MAPPEDADDRESS) &&
- ntohs(attrib.len) == 8) {
- char *ip;
+ if (attrib.type == g_htons(ATTRIB_MAPPEDADDRESS) &&
+ g_ntohs(attrib.len) == 8) {
+ GInetAddress *inet_addr = NULL;
/* Skip the first unused byte,
* the family(1 byte), and the port(2 bytes);
* then read the 4 byte IPv4 address */
- memcpy(&in.s_addr, it + 4, 4);
- ip = inet_ntoa(in);
- if (ip) {
+ inet_addr = g_inet_address_new_from_bytes((const guint8 *)it + 4,
+ G_SOCKET_FAMILY_IPV4);
+ if (inet_addr) {
+ gchar *ip = g_inet_address_to_string(inet_addr);
g_strlcpy(nattype.publicip, ip, sizeof(nattype.publicip));
+ g_free(ip);
+ g_object_unref(inet_addr);
}
}
- it += ntohs(attrib.len);
+ it += g_ntohs(attrib.len);
}
purple_debug_info("stun", "got public ip %s\n", nattype.publicip);
nattype.status = PURPLE_STUN_STATUS_DISCOVERED;
- nattype.type = PURPLE_STUN_NAT_TYPE_UNKNOWN_NAT;
nattype.lookup_time = g_get_monotonic_time();
- /* is it a NAT? */
-
- ifc.ifc_len = sizeof(buffer_ifr);
- ifc.ifc_req = buffer_ifr;
- ioctl(g_socket_get_fd(socket), SIOCGIFCONF, &ifc);
-
- it = (gchar *)buffer_ifr;
- it_end = it + ifc.ifc_len;
- while (it < it_end) {
- ifr = (struct ifreq *)(gpointer)it;
- it += _SIZEOF_ADDR_IFREQ(*ifr);
-
- if (ifr->ifr_addr.sa_family == AF_INET) {
- /* we only care about ipv4 interfaces */
- sinptr = (struct sockaddr_in *)(gpointer)&ifr->ifr_addr;
- if (sinptr->sin_addr.s_addr == in.s_addr) {
- /* no NAT */
- purple_debug_info("stun", "no nat\n");
- nattype.type = PURPLE_STUN_NAT_TYPE_PUBLIC_IP;
- }
- }
- }
-
do_callbacks();
/* sc will be freed by the GSource destruction. */
@@ -332,11 +303,11 @@
g_source_unref(read_source);
hdr_data = g_new0(struct stun_header, 1);
- hdr_data->type = htons(MSGTYPE_BINDINGREQUEST);
+ hdr_data->type = g_htons(MSGTYPE_BINDINGREQUEST);
hdr_data->len = 0;
hdr_data->transid[0] = g_random_int();
- hdr_data->transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) +
- ((int)'i' << 8) + (int)'m');
+ hdr_data->transid[1] = g_ntohl(((int)'g' << 24) + ((int)'a' << 16) +
+ ((int)'i' << 8) + (int)'m');
hdr_data->transid[2] = g_random_int();
hdr_data->transid[3] = g_random_int();
sc->packet = hdr_data;
--- a/libpurple/stun.h Thu Dec 10 02:58:55 2020 -0600
+++ b/libpurple/stun.h Fri Dec 11 01:48:49 2020 -0600
@@ -56,29 +56,8 @@
} PurpleStunStatus;
/**
- * PurpleStunNatType:
- * @PURPLE_STUN_NAT_TYPE_PUBLIC_IP: No NAT
- * @PURPLE_STUN_NAT_TYPE_UNKNOWN_NAT: NAT is unknown
- * @PURPLE_STUN_NAT_TYPE_FULL_CONE: NAT is a full cone
- * @PURPLE_STUN_NAT_TYPE_RESTRICTED_CONE: NAT is a restricted cone
- * @PURPLE_STUN_NAT_TYPE_PORT_RESTRICTED_CONE: NAT is a port restricted cone
- * @PURPLE_STUN_NAT_TYPE_SYMMETRIC: NAT is symmetric
- *
- * The type of NAT that was discovered.
- */
-typedef enum {
- PURPLE_STUN_NAT_TYPE_PUBLIC_IP,
- PURPLE_STUN_NAT_TYPE_UNKNOWN_NAT,
- PURPLE_STUN_NAT_TYPE_FULL_CONE,
- PURPLE_STUN_NAT_TYPE_RESTRICTED_CONE,
- PURPLE_STUN_NAT_TYPE_PORT_RESTRICTED_CONE,
- PURPLE_STUN_NAT_TYPE_SYMMETRIC
-} PurpleStunNatType;
-
-/**
* PurpleStunNatDiscovery:
* @status: The #PurpleStunStatus
- * @type: The #PurpleStunNatType
* @publicip: The public ip
* @servername: The name of the stun server
* @lookup_time: The time when the lookup occurred
@@ -87,7 +66,6 @@
*/
struct _PurpleStunNatDiscovery {
PurpleStunStatus status;
- PurpleStunNatType type;
char publicip[16];
char *servername;
gint64 lookup_time;