1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* Purple's oscar protocol plugin
* This file is the legal property of its developers.
* Please see the AUTHORS file distributed alongside this file.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "oscar.h"
#include "peer.h"
static void
peer_proxy_send(PeerConnection *conn, ProxyFrame *frame)
{
size_t length;
ByteStream bs;
purple_debug_info("oscar", "Outgoing peer proxy frame with "
"type=0x%04hx, unknown=0x%08x, "
"flags=0x%04hx, and payload length=%hd\n",
frame->type, frame->unknown,
frame->flags, frame->payload.len);
length = 12 + frame->payload.len;
byte_stream_new(&bs, length);
byte_stream_put16(&bs, length - 2);
byte_stream_put16(&bs, PEER_PROXY_PACKET_VERSION);
byte_stream_put16(&bs, frame->type);
byte_stream_put32(&bs, frame->unknown);
byte_stream_put16(&bs, frame->flags);
byte_stream_putraw(&bs, frame->payload.data, frame->payload.len);
peer_connection_send(conn, &bs);
byte_stream_destroy(&bs);
}
/**
* Create a rendezvous "init send" packet and send it on its merry way.
* This is the first packet sent to the proxy server by the first client
* to indicate that this will be a proxied connection
*
* @param conn The peer connection.
*/
static void
peer_proxy_send_create_new_conn(PeerConnection *conn)
{
ProxyFrame frame;
PurpleAccount *account;
const gchar *bn;
guint8 bn_length;
memset(&frame, 0, sizeof(ProxyFrame));
frame.type = PEER_PROXY_TYPE_CREATE;
frame.flags = 0x0000;
account = purple_connection_get_account(conn->od->gc);
bn = purple_account_get_username(account);
bn_length = strlen(bn);
byte_stream_new(&frame.payload, 1 + bn_length + 8 + 20);
byte_stream_put8(&frame.payload, bn_length);
byte_stream_putraw(&frame.payload, (const guint8 *)bn, bn_length);
byte_stream_putraw(&frame.payload, conn->cookie, 8);
byte_stream_put16(&frame.payload, 0x0001); /* Type */
byte_stream_put16(&frame.payload, 16); /* Length */
byte_stream_putcaps(&frame.payload, conn->type); /* Value */
peer_proxy_send(conn, &frame);
}
/**
* Create a rendezvous "init recv" packet and send it on its merry way.
* This is the first packet sent to the proxy server by the second client
* involved in this rendezvous proxy session.
*
* @param conn The peer connection.
* @param pin The 2 byte PIN sent to us by the other user. This acts
* as our passcode when establishing the proxy session.
*/
static void
peer_proxy_send_join_existing_conn(PeerConnection *conn, guint16 pin)
{
ProxyFrame frame;
PurpleAccount *account;
const gchar *bn;
guint8 bn_length;
memset(&frame, 0, sizeof(ProxyFrame));
frame.type = PEER_PROXY_TYPE_JOIN;
frame.flags = 0x0000;
account = purple_connection_get_account(conn->od->gc);
bn = purple_account_get_username(account);
bn_length = strlen(bn);
byte_stream_new(&frame.payload, 1 + bn_length + 2 + 8 + 20);
byte_stream_put8(&frame.payload, bn_length);
byte_stream_putraw(&frame.payload, (const guint8 *)bn, bn_length);
byte_stream_put16(&frame.payload, pin);
byte_stream_putraw(&frame.payload, conn->cookie, 8);
byte_stream_put16(&frame.payload, 0x0001); /* Type */
byte_stream_put16(&frame.payload, 16); /* Length */
byte_stream_putcaps(&frame.payload, conn->type); /* Value */
peer_proxy_send(conn, &frame);
}
/**
* Handle an incoming peer proxy negotiation frame.
*/
static void
peer_proxy_recv_frame(PeerConnection *conn, ProxyFrame *frame)
{
purple_debug_info("oscar", "Incoming peer proxy frame with "
"type=0x%04hx, unknown=0x%08x, "
"flags=0x%04hx, and payload length=%hd\n", frame->type,
frame->unknown, frame->flags, frame->payload.len);
if (frame->type == PEER_PROXY_TYPE_CREATED)
{
/*
* Read in 2 byte port then 4 byte IP and tell the
* remote user to connect to it by sending an ICBM.
*/
guint16 pin;
int i;
guint8 ip[4];
pin = byte_stream_get16(&frame->payload);
for (i = 0; i < 4; i++)
ip[i] = byte_stream_get8(&frame->payload);
if (conn->type == OSCAR_CAPABILITY_DIRECTIM)
aim_im_sendch2_odc_requestproxy(conn->od,
conn->cookie,
conn->bn, ip, pin, ++conn->lastrequestnumber);
else if (conn->type == OSCAR_CAPABILITY_SENDFILE)
{
aim_im_sendch2_sendfile_requestproxy(conn->od,
conn->cookie, conn->bn,
ip, pin, ++conn->lastrequestnumber,
(const gchar *)conn->xferdata.name,
conn->xferdata.size, conn->xferdata.totfiles);
}
}
else if (frame->type == PEER_PROXY_TYPE_READY)
{
purple_input_remove(conn->watcher_incoming);
conn->watcher_incoming = 0;
peer_connection_finalize_connection(conn);
}
else if (frame->type == PEER_PROXY_TYPE_ERROR)
{
if (byte_stream_empty(&frame->payload) >= 2)
{
guint16 error;
const char *msg;
error = byte_stream_get16(&frame->payload);
if (error == 0x000d)
msg = "bad request";
else if (error == 0x0010)
msg = "initial request timed out";
else if (error == 0x001a)
msg ="accept period timed out";
else
msg = "unknown reason";
purple_debug_info("oscar", "Proxy negotiation failed with "
"error 0x%04hx: %s\n", error, msg);
}
else
{
purple_debug_warning("oscar", "Proxy negotiation failed with "
"an unknown error\n");
}
peer_connection_trynext(conn);
}
else
{
purple_debug_warning("oscar", "Unknown peer proxy frame type 0x%04hx.\n",
frame->type);
}
}
static void
peer_proxy_connection_recv_cb(gpointer data, gint source, PurpleInputCondition cond)
{
PeerConnection *conn;
gssize read;
ProxyFrame *frame;
conn = data;
frame = conn->frame;
/* Start reading a new proxy frame */
if (frame == NULL)
{
/* Read the first 12 bytes (frame length and header) */
read = recv(conn->fd, conn->proxy_header + conn->proxy_header_received,
12 - conn->proxy_header_received, 0);
/* Check if the proxy server closed the connection */
if (read == 0)
{
purple_debug_info("oscar", "Peer proxy server closed connection\n");
peer_connection_trynext(conn);
return;
}
/* If there was an error then close the connection */
if (read < 0)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
/* No worries */
return;
purple_debug_info("oscar", "Lost connection with peer proxy server\n");
peer_connection_trynext(conn);
return;
}
conn->lastactivity = time(NULL);
/* If we don't even have the first 12 bytes then do nothing */
conn->proxy_header_received += read;
if (conn->proxy_header_received < 12)
return;
/* We only support a specific version of the proxy protocol */
if (aimutil_get16(&conn->proxy_header[2]) != PEER_PROXY_PACKET_VERSION)
{
purple_debug_warning("oscar", "Expected peer proxy protocol "
"version %u but received version %u. Closing "
"connection.\n", PEER_PROXY_PACKET_VERSION,
aimutil_get16(&conn->proxy_header[2]));
peer_connection_trynext(conn);
return;
}
/* Initialize a new temporary ProxyFrame for incoming data */
frame = g_new0(ProxyFrame, 1);
frame->payload.len = aimutil_get16(&conn->proxy_header[0]) - 10;
frame->version = aimutil_get16(&conn->proxy_header[2]);
frame->type = aimutil_get16(&conn->proxy_header[4]);
frame->unknown = aimutil_get16(&conn->proxy_header[6]);
frame->flags = aimutil_get16(&conn->proxy_header[10]);
if (frame->payload.len > 0)
frame->payload.data = g_new(guint8, frame->payload.len);
conn->frame = frame;
}
/* If this frame has a payload then attempt to read it */
if (frame->payload.len - frame->payload.offset > 0)
{
/* Read data into the temporary buffer until it is complete */
read = recv(conn->fd,
&frame->payload.data[frame->payload.offset],
frame->payload.len - frame->payload.offset,
0);
/* Check if the proxy server closed the connection */
if (read == 0)
{
purple_debug_info("oscar", "Peer proxy server closed connection\n");
g_free(frame->payload.data);
g_free(frame);
conn->frame = NULL;
peer_connection_trynext(conn);
return;
}
/* If there was an error then close the connection */
if (read < 0)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
/* No worries */
return;
purple_debug_info("oscar", "Lost connection with peer proxy server\n");
g_free(frame->payload.data);
g_free(frame);
conn->frame = NULL;
peer_connection_trynext(conn);
return;
}
frame->payload.offset += read;
}
conn->lastactivity = time(NULL);
if (frame->payload.offset < frame->payload.len)
/* Waiting for more data to arrive */
return;
/* We have a complete proxy frame! Handle it and continue reading */
conn->frame = NULL;
byte_stream_rewind(&frame->payload);
peer_proxy_recv_frame(conn, frame);
g_free(frame->payload.data);
g_free(frame);
conn->proxy_header_received = 0;
}
/**
* We tried to make an outgoing connection to a proxy server. It
* either connected or failed to connect.
*/
void
peer_proxy_connection_established_cb(gpointer data, gint source, const gchar *error_message)
{
PeerConnection *conn;
conn = data;
conn->verified_connect_data = NULL;
if (source < 0)
{
peer_connection_trynext(conn);
return;
}
conn->fd = source;
conn->watcher_incoming = purple_input_add(conn->fd,
PURPLE_INPUT_READ, peer_proxy_connection_recv_cb, conn);
if (conn->proxyip != NULL)
/* Connect to the session created by the remote user */
peer_proxy_send_join_existing_conn(conn, conn->port);
else
/* Create a new session */
peer_proxy_send_create_new_conn(conn);
}
|