10321
|
1 /*
|
|
2 * gaim - Rendezvous Protocol Plugin
|
|
3 *
|
|
4 * Gaim is the legal property of its developers, whose names are too numerous
|
|
5 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
6 * source distribution.
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2 of the License, or
|
|
11 * (at your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; if not, write to the Free Software
|
|
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
21 */
|
|
22 #include "internal.h"
|
|
23
|
|
24 #include "connection.h"
|
|
25 #include "network.h"
|
|
26
|
|
27 #include "direct.h"
|
|
28 #include "rendezvous.h"
|
|
29
|
10549
|
30 static gboolean
|
10596
|
31 rendezvous_find_buddy_by_ipv4(gpointer key, gpointer value, gpointer user_data)
|
10321
|
32 {
|
|
33 RendezvousBuddy *rb = value;
|
|
34
|
10549
|
35 if (rb->ipv4 == NULL)
|
|
36 return FALSE;
|
10321
|
37
|
10596
|
38 int *ipv4 = user_data;
|
|
39 printf("looking for ip=%hu.%hu.%hu.%hu\n", ipv4[0], ipv4[1], ipv4[2], ipv4[3]);
|
|
40 printf("looking at ip=%hu.%hu.%hu.%hu, %s\n", rb->ipv4[0], rb->ipv4[1], rb->ipv4[2], rb->ipv4[3], rb->firstandlast);
|
10549
|
41 return !memcmp(rb->ipv4, user_data, 4);
|
10321
|
42 }
|
10596
|
43
|
|
44 static gboolean
|
|
45 rendezvous_find_buddy_by_ipv6(gpointer key, gpointer value, gpointer user_data)
|
|
46 {
|
|
47 RendezvousBuddy *rb = value;
|
|
48
|
|
49 if (rb->ipv6 == NULL)
|
|
50 return FALSE;
|
|
51
|
|
52 int *ipv6 = user_data;
|
|
53 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]);
|
|
54 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);
|
|
55 return !memcmp(rb->ipv6, user_data, 16);
|
|
56 }
|
10321
|
57
|
10549
|
58 void
|
|
59 rendezvous_direct_acceptconnection(gpointer data, gint source, GaimInputCondition condition)
|
10321
|
60 {
|
|
61 GaimConnection *gc = (GaimConnection *)data;
|
|
62 RendezvousData *rd = gc->proto_data;
|
|
63 int fd;
|
|
64 struct sockaddr_in6 addr;
|
|
65 socklen_t addrlen = sizeof(addr);
|
10596
|
66 RendezvousBuddy *rb = NULL;
|
10321
|
67
|
|
68 fd = accept(rd->listener, (struct sockaddr *)&addr, &addrlen);
|
|
69 if (fd == -1) {
|
|
70 gaim_debug_warning("rendezvous", "accept: %s\n", strerror(errno));
|
|
71 return;
|
|
72 }
|
10549
|
73
|
10321
|
74 if (((struct sockaddr *)&addr)->sa_family == AF_INET)
|
10596
|
75 rb = g_hash_table_find(rd->buddies, rendezvous_find_buddy_by_ipv4, &(((struct sockaddr_in *)&addr)->sin_addr));
|
10321
|
76 else if (((struct sockaddr *)&addr)->sa_family == AF_INET6)
|
10596
|
77 rb = g_hash_table_find(rd->buddies, rendezvous_find_buddy_by_ipv6, &(addr.sin6_addr.s6_addr));
|
10549
|
78
|
10321
|
79 if (rb == NULL) {
|
|
80 /* We don't want to talk to people that don't advertise themselves */
|
|
81 printf("\ndid not find rb\n\n");
|
|
82 close(fd);
|
|
83 return;
|
|
84 }
|
|
85 printf("\nip belongs to=%s\n\n", rb->aim);
|
|
86
|
|
87 rb->fd = fd;
|
10596
|
88
|
|
89 /* TODO: Add a watcher on the connection. */
|
10321
|
90 }
|
10596
|
91
|
|
92 static void
|
|
93 rendezvous_direct_connect(RendezvousBuddy *rb)
|
|
94 {
|
|
95 struct sockaddr_in addr;
|
|
96
|
|
97 /* If we already have a connection then do nothing */
|
|
98 if (rb->fd != -1)
|
|
99 return;
|
|
100
|
|
101 if ((rb->ipv4 == NULL) && (rb->ipv6 == NULL))
|
|
102 {
|
|
103 gaim_debug_warning("rendezvous", "Could not connect: Unknown IP address.\n");
|
|
104 /* TODO: Show an error message to the user. */
|
|
105 return;
|
|
106 }
|
|
107
|
|
108 if ((rb->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
|
|
109 {
|
|
110 gaim_debug_warning("rendezvous", "Could not connect: %s.\n", strerror(errno));
|
|
111 /* TODO: Show an error message to the user. */
|
|
112 return;
|
|
113 }
|
|
114
|
|
115 addr.sin_family = AF_INET;
|
|
116 addr.sin_port = rb->p2pjport;
|
|
117 memcpy(&addr.sin_addr, rb->ipv4, 4);
|
|
118 memset(&addr.sin_zero, 0, 8);
|
|
119
|
|
120 if (connect(rb->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1)
|
|
121 {
|
|
122 gaim_debug_warning("rendezvous", "Could not connect: %s.\n", strerror(errno));
|
|
123 /* TODO: Show an error message to the user. */
|
|
124 return;
|
|
125 }
|
|
126
|
|
127 /* TODO: Connect a watcher */
|
|
128 }
|
|
129
|
|
130 static void
|
|
131 rendezvous_direct_write_message_to_socket(int fd, const char *message)
|
|
132 {
|
|
133
|
|
134 }
|
|
135
|
|
136 /*
|
|
137 * TODO: Establish a direct connection, then send IM. Will need to
|
|
138 * queue the message somewhere, while the connection is established.
|
|
139 */
|
|
140 void
|
|
141 rendezvous_direct_send_message(GaimConnection *gc, const char *who, const char *message)
|
|
142 {
|
|
143 RendezvousData *rd = gc->proto_data;
|
|
144 RendezvousBuddy *rb;
|
|
145
|
|
146 rb = g_hash_table_lookup(rd->buddies, who);
|
|
147 if (rb == NULL)
|
|
148 {
|
|
149 /* TODO: Should print an error to the user, here */
|
|
150 gaim_debug_error("rendezvous", "Could not send message to %s: Could not find user information.\n", who);
|
|
151 return;
|
|
152 }
|
|
153
|
|
154 if (rb->fd == -1)
|
|
155 {
|
|
156 rendezvous_direct_connect(rb);
|
|
157 /* TODO: Queue message */
|
|
158 //gaim_debug_warning("rendezvous", "Could not send message to %s: Unable to establish connection.\n", who);
|
|
159 }
|
|
160 else
|
|
161 {
|
|
162 rendezvous_direct_write_message_to_socket(rb->fd, message);
|
|
163 }
|
|
164 }
|