changeset 8738:dbbf5470ba05

[gaim-migrate @ 9493] Committing some stuff before I break it committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Wed, 21 Apr 2004 03:37:30 +0000
parents 504570a1acd5
children 52969b8e9c58
files src/protocols/rendezvous/Makefile.am src/protocols/rendezvous/Makefile.mingw src/protocols/rendezvous/mdns.c src/protocols/rendezvous/mdns.h src/protocols/rendezvous/mdns_cache.c src/protocols/rendezvous/mdns_cache.h
diffstat 6 files changed, 210 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/src/protocols/rendezvous/Makefile.am	Wed Apr 21 01:45:57 2004 +0000
+++ b/src/protocols/rendezvous/Makefile.am	Wed Apr 21 03:37:30 2004 +0000
@@ -5,6 +5,7 @@
 
 RENDEZVOUSSOURCES = \
 	mdns.c \
+	mdns_cache.c \
 	rendezvous.c
 
 AM_CFLAGS = $(st)
--- a/src/protocols/rendezvous/Makefile.mingw	Wed Apr 21 01:45:57 2004 +0000
+++ b/src/protocols/rendezvous/Makefile.mingw	Wed Apr 21 03:37:30 2004 +0000
@@ -69,6 +69,7 @@
 ##
 
 C_SRC =			mdns.c \
+				mdns_cache.c \
 				rendezvous.c
 
 
--- a/src/protocols/rendezvous/mdns.c	Wed Apr 21 01:45:57 2004 +0000
+++ b/src/protocols/rendezvous/mdns.c	Wed Apr 21 03:37:30 2004 +0000
@@ -31,13 +31,16 @@
 
 /*
  * XXX - This entire file could use another pair of eyes to audit for 
- * any possible buffer overflow exploits.
+ * any possible buffer overflow exploits.  It doesn't even HAVE to be
+ * a pair, neither--one eye would suffice.  Oh, snap, somebody call
+ * One Eyed Willie.
  */
 
 #include "internal.h"
 #include "debug.h"
 
 #include "mdns.h"
+#include "mdns_cache.h"
 #include "util.h"
 
 /******************************************/
@@ -82,7 +85,7 @@
 /**
  * Free a given resource record.
  */
-static void
+void
 mdns_free_rr(ResourceRecord *rr)
 {
 	g_free(rr->name);
@@ -429,55 +432,11 @@
 }
 
 int
-mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength)
+mdns_send_rr(int fd, ResourceRecord *rr)
 {
 	int ret;
 	DNSPacket *dns;
 
-	if ((strlen(name) > 255)) {
-		return -EINVAL;
-	}
-
-	dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
-	dns->header.id = 0x0000;
-	dns->header.flags = 0x8400;
-	dns->header.numquestions = 0x0000;
-	dns->header.numanswers = 0x0001;
-	dns->header.numauthority = 0x0000;
-	dns->header.numadditional = 0x0000;
-	dns->questions = NULL;
-
-	dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
-	dns->answers[0].name = g_strdup(name);
-	dns->answers[0].type = RENDEZVOUS_RRTYPE_NULL;
-	dns->answers[0].class = 0x0001;
-	dns->answers[0].ttl = 0x00001c20;
-	dns->answers[0].rdlength = rdlength;
-	dns->answers[0].rdata = (void *)rdata;
-
-	dns->authority = NULL;
-	dns->additional = NULL;
-
-	mdns_send_dns(fd, dns);
-
-	/* The rdata should be freed by the caller of this function */
-	dns->answers[0].rdata = NULL;
-
-	mdns_free(dns);
-
-	return ret;
-}
-
-int
-mdns_advertise_ptr(int fd, const char *name, const char *domain)
-{
-	int ret;
-	DNSPacket *dns;
-
-	if ((strlen(name) > 255) || (strlen(domain) > 255)) {
-		return -EINVAL;
-	}
-
 	dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
 	dns->header.id = 0x0000;
 	dns->header.flags = 0x8400;
@@ -486,61 +445,98 @@
 	dns->header.numauthority = 0x0000;
 	dns->header.numadditional = 0x0000;
 	dns->questions = NULL;
-
-	dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
-	dns->answers[0].name = g_strdup(name);
-	dns->answers[0].type = RENDEZVOUS_RRTYPE_PTR;
-	dns->answers[0].class = 0x8001;
-	dns->answers[0].ttl = 0x00001c20;
-	dns->answers[0].rdata = (void *)g_strdup(domain);
-	dns->answers[0].rdlength = mdns_getlength_RR_rdata(dns->answers[0].type, dns->answers[0].rdata);
-
+	dns->answers = rr;
 	dns->authority = NULL;
 	dns->additional = NULL;
 
 	mdns_send_dns(fd, dns);
 
+	/* The rr should be freed by the caller of this function */
+	dns->header.numanswers = 0x0000;
+	dns->answers = NULL;
+
 	mdns_free(dns);
 
 	return ret;
 }
 
 int
+mdns_advertise_null(int fd, const char *name, const char *rdata, unsigned short rdlength)
+{
+	int ret;
+	ResourceRecord *rr;
+
+	if ((strlen(name) > 255)) {
+		return -EINVAL;
+	}
+
+	rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
+	rr->name = g_strdup(name);
+	rr->type = RENDEZVOUS_RRTYPE_NULL;
+	rr->class = 0x0001;
+	rr->ttl = 0x00001c20;
+	rr->rdlength = rdlength;
+	rr->rdata = (void *)rdata;
+
+	mdns_send_rr(fd, rr);
+
+	/* The rdata should be freed by the caller of this function */
+	rr->rdata = NULL;
+
+	mdns_free_rr(rr);
+
+	return ret;
+}
+
+int
+mdns_advertise_ptr(int fd, const char *name, const char *domain)
+{
+	int ret;
+	ResourceRecord *rr;
+
+	if ((strlen(name) > 255) || (strlen(domain) > 255)) {
+		return -EINVAL;
+	}
+
+	rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
+	rr->name = g_strdup(name);
+	rr->type = RENDEZVOUS_RRTYPE_PTR;
+	rr->class = 0x8001;
+	rr->ttl = 0x00001c20;
+	rr->rdata = (void *)g_strdup(domain);
+	rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
+
+	mdns_send_rr(fd, rr);
+
+	mdns_free_rr(rr);
+
+	return ret;
+}
+
+int
 mdns_advertise_txt(int fd, const char *name, const GSList *rdata)
 {
 	int ret;
-	DNSPacket *dns;
+	ResourceRecord *rr;
 
 	if ((strlen(name) > 255)) {
 		return -EINVAL;
 	}
 
-	dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
-	dns->header.id = 0x0000;
-	dns->header.flags = 0x8400;
-	dns->header.numquestions = 0x0000;
-	dns->header.numanswers = 0x0001;
-	dns->header.numauthority = 0x0000;
-	dns->header.numadditional = 0x0000;
-	dns->questions = NULL;
+	rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
+	rr->name = g_strdup(name);
+	rr->type = RENDEZVOUS_RRTYPE_TXT;
+	rr->class = 0x8001;
+	rr->ttl = 0x00001c20;
+	rr->rdata = (void *)rdata;
+	rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
 
-	dns->answers = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
-	dns->answers[0].name = g_strdup(name);
-	dns->answers[0].type = RENDEZVOUS_RRTYPE_TXT;
-	dns->answers[0].class = 0x8001;
-	dns->answers[0].ttl = 0x00001c20;
-	dns->answers[0].rdata = (void *)rdata;
-	dns->answers[0].rdlength = mdns_getlength_RR_rdata(dns->answers[0].type, dns->answers[0].rdata);
-
-	dns->authority = NULL;
-	dns->additional = NULL;
-
-	mdns_send_dns(fd, dns);
+	mdns_send_rr(fd, rr);
 
 	/* The rdata should be freed by the caller of this function */
-	dns->answers[0].rdata = NULL;
+	rr->rdata = NULL;
 
-	mdns_free(dns);
+	mdns_free_rr(rr);
 
 	return ret;
 }
@@ -549,7 +545,7 @@
 mdns_advertise_srv(int fd, const char *name, unsigned short port, const char *target)
 {
 	int ret;
-	DNSPacket *dns;
+	ResourceRecord *rr;
 	ResourceRecordRDataSRV *rdata;
 
 	if ((strlen(target) > 255)) {
@@ -560,29 +556,17 @@
 	rdata->port = port;
 	rdata->target = g_strdup(target);
 
-	dns = (DNSPacket *)g_malloc(sizeof(DNSPacket));
-	dns->header.id = 0x0000;
-	dns->header.flags = 0x8400;
-	dns->header.numquestions = 0x0000;
-	dns->header.numanswers = 0x0000;
-	dns->header.numauthority = 0x0001;
-	dns->header.numadditional = 0x0000;
-	dns->questions = NULL;
-	dns->answers = NULL;
+	rr = (ResourceRecord *)g_malloc(sizeof(ResourceRecord));
+	rr->name = g_strdup(name);
+	rr->type = RENDEZVOUS_RRTYPE_SRV;
+	rr->class = 0x8001;
+	rr->ttl = 0x00001c20;
+	rr->rdata = rdata;
+	rr->rdlength = mdns_getlength_RR_rdata(rr->type, rr->rdata);
 
-	dns->authority = (ResourceRecord *)g_malloc(1 * sizeof(ResourceRecord));
-	dns->authority[0].name = g_strdup(name);
-	dns->authority[0].type = RENDEZVOUS_RRTYPE_SRV;
-	dns->authority[0].class = 0x8001;
-	dns->authority[0].ttl = 0x00001c20;
-	dns->authority[0].rdata = rdata;
-	dns->authority[0].rdlength = mdns_getlength_RR_rdata(dns->authority[0].type, dns->authority[0].rdata);
+	mdns_send_rr(fd, rr);
 
-	dns->additional = NULL;
-
-	mdns_send_dns(fd, dns);
-
-	mdns_free(dns);
+	mdns_free_rr(rr);
 
 	return ret;
 }
@@ -928,6 +912,7 @@
 mdns_read(int fd)
 {
 	DNSPacket *ret = NULL;
+	int i;
 	int offset; /* Current position in datagram */
 	/* XXX - Find out what to use as a maximum incoming UDP packet size */
 	/* char data[512]; */
@@ -1009,5 +994,16 @@
 		return NULL;
 	}
 
+#if 0
+	for (i = 0; i < ret->header.numanswers; i++)
+		mdns_cache_add(&ret->answers[i]);
+	for (i = 0; i < ret->header.numauthority; i++)
+		mdns_cache_add(&ret->authority[i]);
+	for (i = 0; i < ret->header.numadditional; i++)
+		mdns_cache_add(&ret->additional[i]);
+	for (i = 0; i < ret->header.numquestions; i++)
+		mdns_cache_respond(fd, &ret->questions[i]);
+#endif
+
 	return ret;
 }
--- a/src/protocols/rendezvous/mdns.h	Wed Apr 21 01:45:57 2004 +0000
+++ b/src/protocols/rendezvous/mdns.h	Wed Apr 21 03:37:30 2004 +0000
@@ -156,6 +156,7 @@
  */
 int mdns_query(int fd, const char *domain, unsigned short type);
 
+int mdns_send_rr(int fd, ResourceRecord *rr);
 int mdns_advertise_null(int fd, const char *name, const char *data, unsigned short rdlength);
 int mdns_advertise_ptr(int fd, const char *name, const char *domain);
 int mdns_advertise_txt(int fd, const char *name, const GSList *txt);
@@ -177,5 +178,6 @@
  * @param dns The DNSPacket that you want to free.
  */
 void mdns_free(DNSPacket *dns);
+void mdns_free_rr(ResourceRecord *rr);
 
 #endif /* _MDNS_H_ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/protocols/rendezvous/mdns_cache.c	Wed Apr 21 03:37:30 2004 +0000
@@ -0,0 +1,67 @@
+/**
+ * @file mdns_cache.c Multicast DNS resource record caching code.
+ *
+ * gaim
+ *
+ * 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"
+#include "debug.h"
+
+#include "mdns.h"
+
+GSList *resourcerecords = NULL;
+
+void mdns_cache_add(ResourceRecord *rr)
+{
+	g_return_if_fail(rr != NULL);
+
+	resourcerecords = g_slist_prepend(resourcerecords, rr);
+}
+
+void mdns_cache_remove(ResourceRecord *rr)
+{
+	g_return_if_fail(rr != NULL);
+
+	resourcerecords = g_slist_remove_all(resourcerecords, rr);
+
+	mdns_free_rr(rr);
+}
+
+void mdns_cache_remove_all()
+{
+	while (resourcerecords != NULL)
+		mdns_cache_remove(resourcerecords->data);
+}
+
+void mdns_cache_respond(int fd, Question *q)
+{
+	GSList *slist;
+	ResourceRecord *cur;
+
+	g_return_if_fail(q != NULL);
+
+	for (slist = resourcerecords; slist != NULL; slist = g_slist_next(slist)) {
+		cur = slist->data;
+		if ((q->type == cur->type) && (!strcmp(q->name, cur->name)))
+			mdns_send_rr(fd, cur);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/protocols/rendezvous/mdns_cache.h	Wed Apr 21 03:37:30 2004 +0000
@@ -0,0 +1,42 @@
+/**
+ * @file mdns_cache.h Multicast DNS resource record caching code.
+ *
+ * gaim
+ *
+ * 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
+ *
+ */
+
+#ifndef _MDNS_CACHE_H_
+#define _MDNS_CACHE_H_
+
+#include "internal.h"
+#include "debug.h"
+
+#include "mdns.h"
+
+void mdns_cache_add(ResourceRecord *rr);
+
+void mdns_cache_remove(ResourceRecord *rr);
+
+void mdns_cache_remove_all();
+
+void mdns_cache_respond(int fd, Question *q);
+
+#endif /* _MDNS_CACHE_H_ */