/**
 * @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

/* Solaris */
#if defined (__SVR4) && defined (__sun)
#include <sys/sockio.h>
#endif

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

struct stun_header {
	short	type;
	short	len;
	int	transid[4];
};

struct stun_attrib {
	short type;
	short len;
};

struct stun_change {
	struct stun_header hdr;
	struct stun_attrib attrib;
	char value[4];
};

static GaimStunNatDiscovery 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 = GAIM_STUN_NAT_TYPE_SYMMETRIC;

		/* remove input */
		gaim_input_remove(incb);

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

		/* 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 = GAIM_STUN_STATUS_DISCOVERED;
		nattype.type = GAIM_STUN_NAT_TYPE_UNKNOWN_NAT;

		/* 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 = GAIM_STUN_NAT_TYPE_PUBLIC_IP;
				}
			}
		}
		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 = GAIM_STUN_NAT_TYPE_FULL_CONE;
	}
}

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 = GAIM_STUN_STATUS_UNKNOWN;
		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 = GAIM_STUN_STATUS_UNKNOWN;
		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 = GAIM_STUN_STATUS_UNKNOWN;
		do_callbacks();
		return;
	}
	test = 1;
	packet = &data;
	packetsize = sizeof(struct stun_header);
	timeout = gaim_timeout_add(500, (GSourceFunc)timeoutfunc, NULL);
}

static void do_test1(GaimSrvResponse *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);
}

GaimStunNatDiscovery *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 == GAIM_STUN_STATUS_DISCOVERING) {
		if(cb)
			callbacks = g_slist_append(callbacks, cb);
		return NULL;
	}

	if(nattype.status != GAIM_STUN_STATUS_UNDISCOVERED) {
		if(cb)
			cb(&nattype);
		return &nattype;
	}

	if(!servername || (strlen(servername) < 2)) {
		nattype.status = GAIM_STUN_STATUS_UNKNOWN;
		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", "");
}
