diff src/protocols/rendezvous/direct.c @ 10596:913ec44675c3

[gaim-migrate @ 12011] Get some rendezvous stuff out of my tree. No, you still can't get or send IMs. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 14 Feb 2005 03:08:43 +0000
parents 8bc7ba019e96
children 430b2a8cccb5
line wrap: on
line diff
--- a/src/protocols/rendezvous/direct.c	Mon Feb 14 03:07:06 2005 +0000
+++ b/src/protocols/rendezvous/direct.c	Mon Feb 14 03:08:43 2005 +0000
@@ -27,42 +27,33 @@
 #include "direct.h"
 #include "rendezvous.h"
 
-#if 0
-gchar *
-gaim_network_convert_ipv4_to_string(void *ip)
-{
-	gchar *ret;
-	unsigned char *ipv4 = (unsigned char *)ip;
-
-	ret = g_strdup_printf("::ffff:%02hhx%02hhx:%02hhx%02hhx", ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
-
-	return ret;
-}
-
-gchar *
-gaim_network_convert_ipv6_to_string(void *ip)
-{
-	gchar *ret;
-
-	//ret = g_strdup_printf("%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx", ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6], ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13], ip[14], ip[15]);
-	ret = g_malloc0(INET6_ADDRSTRLEN + 1);
-	inet_ntop(AF_INET6, ip, ret, sizeof(ret));
-
-	return ret;
-}
-
 static gboolean
-rendezvous_find_buddy_by_ip(gpointer key, gpointer value, gpointer user_data)
+rendezvous_find_buddy_by_ipv4(gpointer key, gpointer value, gpointer user_data)
 {
 	RendezvousBuddy *rb = value;
 
 	if (rb->ipv4 == NULL)
 		return FALSE;
 
-printf("looking at ip=%hu.%hu.%hu.%hu\n", rb->ipv4[0], rb->ipv4[1], rb->ipv4[2], rb->ipv4[3]);
+int *ipv4 = user_data;
+printf("looking for ip=%hu.%hu.%hu.%hu\n", ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
+printf("looking at  ip=%hu.%hu.%hu.%hu, %s\n", rb->ipv4[0], rb->ipv4[1], rb->ipv4[2], rb->ipv4[3], rb->firstandlast);
 	return !memcmp(rb->ipv4, user_data, 4);
 }
-#endif
+
+static gboolean
+rendezvous_find_buddy_by_ipv6(gpointer key, gpointer value, gpointer user_data)
+{
+	RendezvousBuddy *rb = value;
+
+	if (rb->ipv6 == NULL)
+		return FALSE;
+
+int *ipv6 = user_data;
+printf("looking for ip=%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx\n", ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7], ipv6[8], ipv6[9], ipv6[10], ipv6[11], ipv6[12], ipv6[13], ipv6[14], ipv6[15]);
+printf("looking at  ip=%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx:%hx%hx, %s\n", rb->ipv6[0], rb->ipv6[1], rb->ipv6[2], rb->ipv6[3], rb->ipv6[4], rb->ipv6[5], rb->ipv6[6], rb->ipv6[7], rb->ipv6[8], rb->ipv6[9], rb->ipv6[10], rb->ipv6[11], rb->ipv6[12], rb->ipv6[13], rb->ipv6[14], rb->ipv6[15], rb->firstandlast);
+	return !memcmp(rb->ipv6, user_data, 16);
+}
 
 void
 rendezvous_direct_acceptconnection(gpointer data, gint source, GaimInputCondition condition)
@@ -72,10 +63,7 @@
 	int fd;
 	struct sockaddr_in6 addr;
 	socklen_t addrlen = sizeof(addr);
-#if 0
-	gchar *ip;
-#endif
-	RendezvousBuddy *rb;
+	RendezvousBuddy *rb = NULL;
 
 	fd = accept(rd->listener, (struct sockaddr *)&addr, &addrlen);
 	if (fd == -1) {
@@ -83,17 +71,10 @@
 		return;
 	}
 
-#if 0
-	printf("\nsa_family=%d\n\n", ((struct sockaddr *)&addr)->sa_family);
 	if (((struct sockaddr *)&addr)->sa_family == AF_INET)
-		ip = gaim_network_convert_ipv4_to_string((unsigned char *)&ip);
+		rb = g_hash_table_find(rd->buddies, rendezvous_find_buddy_by_ipv4, &(((struct sockaddr_in *)&addr)->sin_addr));
 	else if (((struct sockaddr *)&addr)->sa_family == AF_INET6)
-		ip = gaim_network_convert_ipv6_to_string((unsigned char *)&(addr.sin6_addr));
-	printf("\nip=%s\n", ip);
-
-	rb = g_hash_table_find(rd->buddies, rendezvous_find_buddy_by_ip, ip);
-	g_free(ip);
-#endif
+		rb = g_hash_table_find(rd->buddies, rendezvous_find_buddy_by_ipv6, &(addr.sin6_addr.s6_addr));
 
 	if (rb == NULL) {
 		/* We don't want to talk to people that don't advertise themselves */
@@ -104,4 +85,80 @@
 printf("\nip belongs to=%s\n\n", rb->aim);
 
 	rb->fd = fd;
+
+	/* TODO: Add a watcher on the connection. */
 }
+
+static void
+rendezvous_direct_connect(RendezvousBuddy *rb)
+{
+	struct sockaddr_in addr;
+
+	/* If we already have a connection then do nothing */
+	if (rb->fd != -1)
+		return;
+
+	if ((rb->ipv4 == NULL) && (rb->ipv6 == NULL))
+	{
+		gaim_debug_warning("rendezvous", "Could not connect: Unknown IP address.\n");
+		/* TODO: Show an error message to the user. */
+		return;
+	}
+
+	if ((rb->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+	{
+		gaim_debug_warning("rendezvous", "Could not connect: %s.\n", strerror(errno));
+		/* TODO: Show an error message to the user. */
+		return;
+	}
+
+	addr.sin_family = AF_INET;
+	addr.sin_port = rb->p2pjport;
+	memcpy(&addr.sin_addr, rb->ipv4, 4);
+	memset(&addr.sin_zero, 0, 8);
+
+	if (connect(rb->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)
+	{
+		gaim_debug_warning("rendezvous", "Could not connect: %s.\n", strerror(errno));
+		/* TODO: Show an error message to the user. */
+		return;
+	}
+
+	/* TODO: Connect a watcher */
+}
+
+static void
+rendezvous_direct_write_message_to_socket(int fd, const char *message)
+{
+
+}
+
+/*
+ * TODO: Establish a direct connection, then send IM.  Will need to
+ * queue the message somewhere, while the connection is established.
+ */
+void
+rendezvous_direct_send_message(GaimConnection *gc, const char *who, const char *message)
+{
+	RendezvousData *rd = gc->proto_data;
+	RendezvousBuddy *rb;
+
+	rb = g_hash_table_lookup(rd->buddies, who);
+	if (rb == NULL)
+	{
+		/* TODO: Should print an error to the user, here */
+		gaim_debug_error("rendezvous", "Could not send message to %s: Could not find user information.\n", who);
+		return;
+	}
+
+	if (rb->fd == -1)
+	{
+		rendezvous_direct_connect(rb);
+		/* TODO: Queue message */
+		//gaim_debug_warning("rendezvous", "Could not send message to %s: Unable to establish connection.\n", who);
+	}
+	else
+	{
+		rendezvous_direct_write_message_to_socket(rb->fd, message);
+	}
+}