view src/stun.c @ 12116:e75ef7aa913e

[gaim-migrate @ 14416] " This patch implements a replacement for the queuing system from 1.x. It also obsoletes a previous patch [#1338873] I submitted to prioritize the unseen states in gtk conversations. The attached envelope.png is ripped from the msgunread.png already included in gaim. It should be dropped in the pixmaps directory (Makefile.am is updated accordingly in this patch). The two separate queuing preferences from 1.x, queuing messages while away and queuing all new messages (from docklet), are replaced with a single 3-way preference for conversations. The new preference is "Hide new IM conversations". This preference can be set to never, away and always. When a gtk conversation is created, it may be placed in a hidden conversation window instead of being placed normally. This decision is based upon the preference and possibly the away state of the account the conversation is being created for. This *will* effect conversations the user explicitly requests to be created, so in these cases the caller must be sure to present the conversation to the user, using gaim_gtkconv_present_conversation(). This is done already in gtkdialogs.c which handles creating conversations requested by the user from gaim proper (menus, double-clicking on budy in blist, etc.). The main advantage to not queuing messages is that the conversations exist, the message is written to the conversation (and logged if appropriate) and the unseen state is set on the conversation. This means no additional features are needed to track whether there are queued messages or not, just use the unseen state on conversations. Since conversations may not be visible (messages "queued"), gaim proper needs some notification that there are messages waiting. I opted for a menutray icon that shows up when an im conversation has an unseen message. Clicking this icon will focus (and show if hidden) the first conversation with an unseen message. This is essentially the same behavior of the docklet in cvs right now, except that the icon is only visible when there is a conversation with an unread message. The api that is added is flexible enough to allow either the docklet or the new blist menutray icon to be visible for conversations of any/all types and for unseen messages >= any state. Currently they are set to only IM conversations and only unseen states >= TEXT (system messages and no log messages will not trigger blinking the docklet or showing the blist tray icon), but these could be made preferences relatively easily in the future. Other plugins could probably benefit as well: gaim_gtk_conversations_get_first_unseen(). There is probably some limit to comment size, so I'll stop rambling now. If anyone has more questions/comments, catch me in #gaim, here or on gaim-devel." committer: Tailor Script <tailor@pidgin.im>
author Luke Schierer <lschiere@pidgin.im>
date Wed, 16 Nov 2005 18:17:01 +0000
parents 8d019c4bf454
children 5f65a0cca87c
line wrap: on
line source

/**
 * @file stun.c STUN (RFC3489) Implementation
 * @ingroup core
 *
 * gaim
 *
 * STUN implementation inspired by jstun [http://jstun.javawi.de/]
 *
 * Gaim is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "internal.h"

#ifndef _WIN32
#include <net/if.h>
#include <sys/ioctl.h>
#endif

#include "debug.h"
#include "account.h"
#include "dnssrv.h"
#include "proxy.h"
#include "stun.h"
#include "prefs.h"

static struct stun_nattype nattype = {-1, 0, "\0"};

static GSList *callbacks = 0;
static int fd = -1;
static gint incb = -1;
static gint timeout = -1;
static struct stun_header *packet;
static int packetsize = 0;
static int test = 0;
static int retry = 0;
static struct sockaddr_in addr;

static void do_callbacks() {
	while(callbacks) {
		StunCallback cb = callbacks->data;
		if(cb)
			cb(&nattype);
		callbacks = g_slist_remove(callbacks, cb);
	}
}

static gboolean timeoutfunc(void *blah) {
	if(retry > 2) {
		if(test == 2) nattype.type = 5;
		/* remove input */
		gaim_input_remove(incb);

		/* set unknown */
		nattype.status = 0;

		/* callbacks */
		do_callbacks();

		return FALSE;
	}
	retry++;
	sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
	return TRUE;
}

#ifdef NOTYET
static void do_test2() {
	struct stun_change data;
	data.hdr.type = htons(0x0001);
	data.hdr.len = 0;
	data.hdr.transid[0] = rand();
	data.hdr.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
	data.hdr.transid[2] = rand();
	data.hdr.transid[3] = rand();
	data.attrib.type = htons(0x003);
	data.attrib.len = htons(4);
	data.value[3] = 6;
	packet = (struct stun_header*)&data;
	packetsize = sizeof(struct stun_change);
	retry = 0;
	test = 2;
	sendto(fd, packet, packetsize, 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
	timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
}
#endif

static void reply_cb(gpointer data, gint source, GaimInputCondition cond) {
	char buffer[1024];
	char *tmp;
	int len;
	struct in_addr in;
	struct stun_attrib *attrib;
	struct stun_header *hdr;
	struct ifconf ifc;
	struct ifreq *ifr;
	struct sockaddr_in *sinptr;

	len = recv(source, buffer, 1024, 0);

	hdr = (struct stun_header*)buffer;
	if(hdr->transid[0]!=packet->transid[0] || hdr->transid[1]!=packet->transid[1] || hdr->transid[2]!=packet->transid[2] || hdr->transid[3]!=packet->transid[3]) { /* wrong transaction */
		gaim_debug_info("stun", "got wrong transid\n");
		return;
	}
	if(test==1) {
		tmp = buffer + sizeof(struct stun_header);
		while(buffer+len > tmp) {

			attrib = (struct stun_attrib*) tmp;
			if(attrib->type == htons(0x0001) && attrib->len == htons(8)) {
				memcpy(&in.s_addr, tmp+sizeof(struct stun_attrib)+2+2, 4);
				strcpy(nattype.publicip, inet_ntoa(in));
			}
			tmp += sizeof(struct stun_attrib) + attrib->len;
		}
		gaim_debug_info("stun", "got public ip %s\n", nattype.publicip);
		nattype.status = 2;
		nattype.type = 1;

		/* is it a NAT? */

		ifc.ifc_len = sizeof(buffer);
		ifc.ifc_req = (struct ifreq *) buffer;
		ioctl(source, SIOCGIFCONF, &ifc);

		tmp = buffer;
		while(tmp < buffer + ifc.ifc_len) {
			ifr = (struct ifreq *) tmp;

			tmp += sizeof(struct ifreq);

			if(ifr->ifr_addr.sa_family == AF_INET) {
				/* we only care about ipv4 interfaces */
				sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
				if(sinptr->sin_addr.s_addr == in.s_addr) {
					/* no NAT */
					gaim_debug_info("stun", "no nat");
					nattype.type = 0;
				}
			}
		}
		gaim_timeout_remove(timeout);

#ifdef NOTYET
		do_test2();
#endif
		return;
	} else if(test == 2) {
		do_callbacks();
		gaim_input_remove(incb);
		gaim_timeout_remove(timeout);
		nattype.type = 2;
	}
}

static void hbn_cb(GSList *hosts, gpointer edata, const char *error_message) {
	static struct stun_header data;
	int ret;

	if(!hosts) return;
	if(!hosts->data) return;

	if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		nattype.status = 0;
		do_callbacks();
		return;
	}

	addr.sin_family = AF_INET;
	addr.sin_port = htons(12108);
	addr.sin_addr.s_addr = INADDR_ANY;
	while( ((ret = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in))) < 0 ) && ntohs(addr.sin_port) < 12208) {
		addr.sin_port = htons(ntohs(addr.sin_port)+1);
	}
	if( ret < 0 ) {
		nattype.status = 0;
		do_callbacks();
		return;
	}
	incb = gaim_input_add(fd, GAIM_INPUT_READ, reply_cb, NULL);

	ret = GPOINTER_TO_INT(hosts->data);
	hosts = g_slist_remove(hosts, hosts->data);
	memcpy(&addr, hosts->data, sizeof(struct sockaddr_in));
	g_free(hosts->data);
	hosts = g_slist_remove(hosts, hosts->data);
	while(hosts) {
		hosts = g_slist_remove(hosts, hosts->data);
		g_free(hosts->data);
		hosts = g_slist_remove(hosts, hosts->data);
	}

	data.type = htons(0x0001);
	data.len = 0;
	data.transid[0] = rand();
	data.transid[1] = ntohl(((int)'g' << 24) + ((int)'a' << 16) + ((int)'i' << 8) + (int)'m');
	data.transid[2] = rand();
	data.transid[3] = rand();

	if( sendto(fd, &data, sizeof(struct stun_header), 0, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < sizeof(struct stun_header)) {
		nattype.status = 0;
		do_callbacks();
		return;
	}
	test = 1;
	packet = &data;
	packetsize = sizeof(struct stun_header);
	timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
}

static void do_test1(struct srv_response *resp, int results, gpointer sdata) {
	const char *servername = sdata;
	int port = 3478;

	if(results) {
		servername = resp[0].hostname;
		port = resp[0].port;
	}
	gaim_debug_info("stun", "got %d SRV responses, server: %s, port: %d\n", results, servername, port);

	gaim_gethostbyname_async(servername, port, hbn_cb, NULL);
	g_free(resp);
}

struct stun_nattype *gaim_stun_discover(StunCallback cb) {
	const char *servername = gaim_prefs_get_string("/core/network/stun_server");

	gaim_debug_info("stun", "using server %s\n", servername);
	if(nattype.status == 1) { /* currently discovering */
		if(cb) callbacks = g_slist_append(callbacks, cb);
		return NULL;
	}
	if(nattype.status != -1) { /* already discovered */
		if(cb) cb(&nattype);
		return &nattype;
	}

	if(!servername || (strlen(servername)<2)) {
		nattype.status = 0;
		if(cb) cb(&nattype);
		return &nattype;
	}
	callbacks = g_slist_append(callbacks, cb);
	gaim_srv_resolve("stun","udp",servername, do_test1,
		(gpointer) servername);
	return &nattype;
}

void gaim_stun_init() {
	gaim_prefs_add_string("/core/network/stun_server", "");
}