13234
|
1 /*
|
|
2 * Gaim's oscar protocol plugin
|
|
3 * This file is the legal property of its developers.
|
|
4 * Please see the AUTHORS file distributed alongside this file.
|
|
5 *
|
|
6 * This library is free software; you can redistribute it and/or
|
|
7 * modify it under the terms of the GNU Lesser General Public
|
|
8 * License as published by the Free Software Foundation; either
|
|
9 * version 2 of the License, or (at your option) any later version.
|
|
10 *
|
|
11 * This library is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 * Lesser General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU Lesser General Public
|
|
17 * License along with this library; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21 /*
|
|
22 * Oscar File transfer (OFT) and Oscar Direct Connect (ODC).
|
|
23 * (ODC is also referred to as DirectIM and IM Image.)
|
|
24 *
|
13239
|
25 * There are a few static helper functions at the top, then
|
13234
|
26 * ODC stuff, then ft stuff.
|
|
27 *
|
13239
|
28 * I feel like this is a good place to explain OFT, so I'm going to
|
|
29 * do just that. Each OFT packet has a header type. I guess this
|
|
30 * is pretty similar to the subtype of a SNAC packet. The type
|
|
31 * basically tells the other client the meaning of the OFT packet.
|
|
32 * There are two distinct types of file transfer, which I usually
|
|
33 * call "sendfile" and "getfile." Sendfile is when you send a file
|
|
34 * to another AIM user. Getfile is when you share a group of files,
|
13234
|
35 * and other users request that you send them the files.
|
|
36 *
|
|
37 * A typical sendfile file transfer goes like this:
|
13239
|
38 * 1) Sender sends a channel 2 ICBM telling the other user that
|
|
39 * we want to send them a file. At the same time, we open a
|
|
40 * listener socket (this should be done before sending the
|
|
41 * ICBM) on some port, and wait for them to connect to us.
|
|
42 * The ICBM we sent should contain our IP address and the port
|
13234
|
43 * number that we're listening on.
|
13239
|
44 * 2) The receiver connects to the sender on the given IP address
|
|
45 * and port. After the connection is established, the receiver
|
13234
|
46 * sends an ICBM signifying that we are ready and waiting.
|
13239
|
47 * 3) The sender sends an OFT PROMPT message over the OFT
|
13234
|
48 * connection.
|
13239
|
49 * 4) The receiver of the file sends back an exact copy of this
|
|
50 * OFT packet, except the cookie is filled in with the cookie
|
|
51 * from the ICBM. I think this might be an attempt to verify
|
|
52 * that the user that is connected is actually the guy that
|
13234
|
53 * we sent the ICBM to. Oh, I've been calling this the ACK.
|
13239
|
54 * 5) The sender starts sending raw data across the connection
|
13234
|
55 * until the entire file has been sent.
|
13239
|
56 * 6) The receiver knows the file is finished because the sender
|
|
57 * sent the file size in an earlier OFT packet. So then the
|
|
58 * receiver sends the DONE thingy (after filling in the
|
13234
|
59 * "received" checksum and size) and closes the connection.
|
|
60 */
|
|
61
|
|
62 #ifdef HAVE_CONFIG_H
|
|
63 #include <config.h>
|
|
64 #endif
|
|
65
|
|
66 #include "oscar.h"
|
|
67 #include "peer.h"
|
|
68
|
|
69 #ifndef _WIN32
|
|
70 #include <stdio.h>
|
|
71 #include <netdb.h>
|
|
72 #include <sys/socket.h>
|
|
73 #include <netinet/in.h>
|
|
74 #include <sys/utsname.h> /* for aim_odc_initiate */
|
|
75 #include <arpa/inet.h> /* for inet_ntoa */
|
|
76 #include <limits.h> /* for UINT_MAX */
|
|
77 #endif
|
|
78
|
|
79 #ifdef _WIN32
|
|
80 #include "win32dep.h"
|
|
81 #endif
|
|
82
|
|
83 /*
|
|
84 * I really want to switch all our networking code to using IPv6 only,
|
|
85 * but that really isn't a good idea at all. Evan S. of Adium says
|
|
86 * OS X sets all connections as "AF_INET6/PF_INET6," even if there is
|
|
87 * nothing inherently IPv6 about them. And I feel like Linux kernel
|
|
88 * 2.6.5 is doing the same thing. So we REALLY should accept
|
|
89 * connections if they're showing up as IPv6. Old OSes (Solaris?)
|
|
90 * that might not have full IPv6 support yet will fail if we try
|
|
91 * to use PF_INET6 but it isn't defined. --Mark Doliner
|
|
92 */
|
|
93 #ifndef PF_INET6
|
|
94 #define PF_INET6 PF_INET
|
|
95 #endif
|
|
96
|
|
97 struct aim_odc_intdata {
|
|
98 guint8 cookie[8];
|
|
99 char sn[MAXSNLEN+1];
|
|
100 char ip[22];
|
|
101 };
|
|
102
|
|
103 /**
|
|
104 * Convert the directory separator from / (0x2f) to ^A (0x01)
|
|
105 *
|
|
106 * @param name The filename to convert.
|
|
107 */
|
13239
|
108 static void
|
|
109 aim_oft_dirconvert_tostupid(char *name)
|
13234
|
110 {
|
|
111 while (name[0]) {
|
|
112 if (name[0] == 0x01)
|
|
113 name[0] = G_DIR_SEPARATOR;
|
|
114 name++;
|
|
115 }
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * Convert the directory separator from ^A (0x01) to / (0x2f)
|
|
120 *
|
|
121 * @param name The filename to convert.
|
|
122 */
|
13239
|
123 static void
|
|
124 aim_oft_dirconvert_fromstupid(char *name)
|
13234
|
125 {
|
|
126 while (name[0]) {
|
|
127 if (name[0] == G_DIR_SEPARATOR)
|
|
128 name[0] = 0x01;
|
|
129 name++;
|
|
130 }
|
|
131 }
|
|
132
|
|
133 /**
|
|
134 * Calculate oft checksum of buffer
|
|
135 *
|
13239
|
136 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The
|
|
137 * checksum is kind of a rolling checksum thing, so each time you get bytes
|
|
138 * of a file you just call this puppy and it updates the checksum. You can
|
|
139 * calculate the checksum of an entire file by calling this in a while or a
|
13234
|
140 * for loop, or something.
|
|
141 *
|
13239
|
142 * Thanks to Graham Booker for providing this improved checksum routine,
|
|
143 * which is simpler and should be more accurate than Josh Myer's original
|
13234
|
144 * code. -- wtm
|
|
145 *
|
13239
|
146 * This algorithm works every time I have tried it. The other fails
|
|
147 * sometimes. So, AOL who thought this up? It has got to be the weirdest
|
13234
|
148 * checksum I have ever seen.
|
|
149 *
|
|
150 * @param buffer Buffer of data to checksum. Man I'd like to buff her...
|
|
151 * @param bufsize Size of buffer.
|
|
152 * @param prevcheck Previous checksum.
|
|
153 */
|
13239
|
154 guint32
|
|
155 aim_oft_checksum_chunk(const guint8 *buffer, int bufferlen, guint32 prevcheck)
|
13234
|
156 {
|
|
157 guint32 check = (prevcheck >> 16) & 0xffff, oldcheck;
|
|
158 int i;
|
|
159 unsigned short val;
|
|
160
|
|
161 for (i=0; i<bufferlen; i++) {
|
|
162 oldcheck = check;
|
|
163 if (i&1)
|
|
164 val = buffer[i];
|
|
165 else
|
|
166 val = buffer[i] << 8;
|
|
167 check -= val;
|
|
168 /*
|
13239
|
169 * The following appears to be necessary.... It happens
|
13234
|
170 * every once in a while and the checksum doesn't fail.
|
|
171 */
|
|
172 if (check > oldcheck)
|
|
173 check--;
|
|
174 }
|
|
175 check = ((check & 0x0000ffff) + (check >> 16));
|
|
176 check = ((check & 0x0000ffff) + (check >> 16));
|
|
177 return check << 16;
|
|
178 }
|
|
179
|
13239
|
180 guint32
|
|
181 aim_oft_checksum_file(char *filename)
|
|
182 {
|
13234
|
183 FILE *fd;
|
|
184 guint32 checksum = 0xffff0000;
|
|
185
|
|
186 if ((fd = fopen(filename, "rb"))) {
|
|
187 int bytes;
|
|
188 guint8 buffer[1024];
|
|
189
|
|
190 while ((bytes = fread(buffer, 1, 1024, fd)))
|
|
191 checksum = aim_oft_checksum_chunk(buffer, bytes, checksum);
|
|
192 fclose(fd);
|
|
193 }
|
|
194
|
|
195 return checksum;
|
|
196 }
|
|
197
|
|
198 /**
|
|
199 * After establishing a listening socket, this is called to accept a connection. It
|
|
200 * clones the conn used by the listener, and passes both of these to a signal handler.
|
|
201 * The signal handler should close the listener conn and keep track of the new conn,
|
|
202 * since this is what is used for file transfers and what not.
|
|
203 *
|
|
204 * @param sess The session.
|
|
205 * @param cur The conn the incoming connection is on.
|
|
206 * @return Return 0 if no errors, otherwise return the error number.
|
|
207 */
|
13239
|
208 int
|
|
209 aim_handlerendconnect(OscarSession *sess, OscarConnection *cur)
|
13234
|
210 {
|
|
211 int acceptfd = 0;
|
|
212 struct sockaddr addr;
|
|
213 socklen_t addrlen = sizeof(addr);
|
|
214 int ret = 0;
|
13239
|
215 OscarConnection *newconn;
|
13234
|
216 char ip[20];
|
|
217 unsigned short port;
|
|
218
|
|
219 if ((acceptfd = accept(cur->fd, &addr, &addrlen)) == -1)
|
|
220 return 0; /* not an error */
|
|
221
|
|
222 if ((addr.sa_family != PF_INET) && (addr.sa_family != PF_INET6)) {
|
|
223 close(acceptfd);
|
|
224 aim_conn_close(cur);
|
|
225 return -1;
|
|
226 }
|
|
227
|
|
228 strncpy(ip, inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr), sizeof(ip));
|
|
229 port = ntohs(((struct sockaddr_in *)&addr)->sin_port);
|
|
230
|
|
231 if (!(newconn = aim_cloneconn(sess, cur))) {
|
|
232 close(acceptfd);
|
|
233 aim_conn_close(cur);
|
|
234 return -ENOMEM;
|
|
235 }
|
|
236
|
|
237 newconn->type = AIM_CONN_TYPE_RENDEZVOUS;
|
|
238 newconn->fd = acceptfd;
|
|
239
|
|
240 if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) {
|
|
241 aim_rxcallback_t userfunc;
|
|
242 struct aim_odc_intdata *priv;
|
|
243
|
|
244 priv = (struct aim_odc_intdata *)(newconn->internal = cur->internal);
|
|
245 cur->internal = NULL;
|
|
246 snprintf(priv->ip, sizeof(priv->ip), "%s:%hu", ip, port);
|
|
247
|
13239
|
248 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIM_ESTABLISHED)))
|
13234
|
249 ret = userfunc(sess, NULL, newconn, cur);
|
|
250
|
|
251 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) {
|
|
252 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE) {
|
|
253 aim_rxcallback_t userfunc;
|
|
254
|
13239
|
255 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, PEER_TYPE_ESTABLISHED)))
|
13234
|
256 ret = userfunc(sess, NULL, newconn, cur);
|
|
257
|
|
258 } else {
|
|
259 gaim_debug_warning("oscar", "Got a connection on a listener that's not rendezvous. Closing connection.\n");
|
|
260 aim_conn_close(newconn);
|
|
261 ret = -1;
|
|
262 }
|
|
263
|
|
264 return ret;
|
|
265 }
|
|
266
|
|
267 /**
|
|
268 * Send client-to-client typing notification over an established direct connection.
|
|
269 *
|
|
270 * @param sess The session.
|
|
271 * @param conn The already-connected ODC connection.
|
13239
|
272 * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and
|
13234
|
273 * 0x0000 sends "stopped."
|
|
274 * @return Return 0 if no errors, otherwise return the error number.
|
|
275 */
|
13239
|
276 int
|
|
277 aim_odc_send_typing(OscarSession *sess, OscarConnection *conn, int typing)
|
13234
|
278 {
|
|
279 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal;
|
13239
|
280 FlapFrame *fr;
|
|
281 ByteStream *hdrbs;
|
13234
|
282 guint8 *hdr;
|
|
283 int hdrlen = 0x44;
|
|
284
|
|
285 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS))
|
|
286 return -EINVAL;
|
|
287
|
13253
|
288 if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, 0)))
|
13234
|
289 return -ENOMEM;
|
|
290 memcpy(fr->hdr.rend.magic, "ODC2", 4);
|
|
291 fr->hdr.rend.hdrlen = hdrlen + 8;
|
|
292
|
|
293 if (!(hdr = calloc(1, hdrlen))) {
|
|
294 aim_frame_destroy(fr);
|
|
295 return -ENOMEM;
|
|
296 }
|
|
297
|
|
298 hdrbs = &(fr->data);
|
|
299 aim_bstream_init(hdrbs, hdr, hdrlen);
|
|
300
|
|
301 aimbs_put16(hdrbs, 0x0006);
|
|
302 aimbs_put16(hdrbs, 0x0000);
|
|
303 aimbs_putraw(hdrbs, intdata->cookie, 8);
|
|
304 aimbs_put16(hdrbs, 0x0000);
|
|
305 aimbs_put16(hdrbs, 0x0000);
|
|
306 aimbs_put16(hdrbs, 0x0000);
|
|
307 aimbs_put16(hdrbs, 0x0000);
|
|
308 aimbs_put32(hdrbs, 0x00000000);
|
|
309 aimbs_put16(hdrbs, 0x0000);
|
|
310 aimbs_put16(hdrbs, 0x0000);
|
|
311 aimbs_put16(hdrbs, 0x0000);
|
|
312
|
|
313 if (typing == 0x0002)
|
|
314 aimbs_put16(hdrbs, 0x0002 | 0x0008);
|
|
315 else if (typing == 0x0001)
|
|
316 aimbs_put16(hdrbs, 0x0002 | 0x0004);
|
|
317 else
|
|
318 aimbs_put16(hdrbs, 0x0002);
|
|
319
|
|
320 aimbs_put16(hdrbs, 0x0000);
|
|
321 aimbs_put16(hdrbs, 0x0000);
|
|
322 aimbs_putstr(hdrbs, sess->sn);
|
|
323
|
|
324 aim_bstream_setpos(hdrbs, 52); /* bleeehh */
|
|
325
|
|
326 aimbs_put8(hdrbs, 0x00);
|
|
327 aimbs_put16(hdrbs, 0x0000);
|
|
328 aimbs_put16(hdrbs, 0x0000);
|
|
329 aimbs_put16(hdrbs, 0x0000);
|
|
330 aimbs_put16(hdrbs, 0x0000);
|
|
331 aimbs_put16(hdrbs, 0x0000);
|
|
332 aimbs_put16(hdrbs, 0x0000);
|
|
333 aimbs_put16(hdrbs, 0x0000);
|
|
334 aimbs_put8(hdrbs, 0x00);
|
|
335
|
|
336 /* end of hdr */
|
|
337
|
|
338 aim_tx_enqueue(sess, fr);
|
|
339
|
|
340 return 0;
|
|
341 }
|
|
342
|
|
343 /**
|
|
344 * Send client-to-client IM over an established direct connection.
|
|
345 * Call this just like you would aim_send_im, to send a directim.
|
13239
|
346 *
|
13234
|
347 * @param sess The session.
|
|
348 * @param conn The already-connected ODC connection.
|
|
349 * @param msg Null-terminated string to send.
|
|
350 * @param len The length of the message to send, including binary data.
|
|
351 * @param encoding See the AIM_CHARSET_* defines in oscar.h
|
|
352 * @param isawaymsg 0 if this is not an auto-response, 1 if it is.
|
|
353 * @return Return 0 if no errors, otherwise return the error number.
|
|
354 */
|
13239
|
355 int
|
|
356 aim_odc_send_im(OscarSession *sess, OscarConnection *conn, const char *msg, int len, int encoding, int isawaymsg)
|
13234
|
357 {
|
13239
|
358 FlapFrame *fr;
|
|
359 ByteStream *hdrbs;
|
13234
|
360 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal;
|
|
361 int hdrlen = 0x44;
|
|
362 guint8 *hdr;
|
|
363
|
|
364 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !msg)
|
|
365 return -EINVAL;
|
|
366
|
13253
|
367 if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, 0)))
|
13234
|
368 return -ENOMEM;
|
|
369
|
|
370 memcpy(fr->hdr.rend.magic, "ODC2", 4);
|
|
371 fr->hdr.rend.hdrlen = hdrlen + 8;
|
|
372
|
|
373 if (!(hdr = calloc(1, hdrlen + len))) {
|
|
374 aim_frame_destroy(fr);
|
|
375 return -ENOMEM;
|
|
376 }
|
|
377
|
|
378 hdrbs = &(fr->data);
|
|
379 aim_bstream_init(hdrbs, hdr, hdrlen + len);
|
|
380
|
|
381 aimbs_put16(hdrbs, 0x0006);
|
|
382 aimbs_put16(hdrbs, 0x0000);
|
|
383 aimbs_putraw(hdrbs, intdata->cookie, 8);
|
|
384 aimbs_put16(hdrbs, 0x0000);
|
|
385 aimbs_put16(hdrbs, 0x0000);
|
|
386 aimbs_put16(hdrbs, 0x0000);
|
|
387 aimbs_put16(hdrbs, 0x0000);
|
|
388 aimbs_put32(hdrbs, len);
|
|
389 aimbs_put16(hdrbs, encoding);
|
|
390 aimbs_put16(hdrbs, 0x0000);
|
|
391 aimbs_put16(hdrbs, 0x0000);
|
|
392
|
|
393 /* flags - used for typing notification and to mark if this is an away message */
|
|
394 aimbs_put16(hdrbs, 0x0000 | isawaymsg);
|
|
395
|
|
396 aimbs_put16(hdrbs, 0x0000);
|
|
397 aimbs_put16(hdrbs, 0x0000);
|
|
398 aimbs_putstr(hdrbs, sess->sn);
|
|
399
|
|
400 aim_bstream_setpos(hdrbs, 52); /* bleeehh */
|
|
401
|
|
402 aimbs_put8(hdrbs, 0x00);
|
|
403 aimbs_put16(hdrbs, 0x0000);
|
|
404 aimbs_put16(hdrbs, 0x0000);
|
|
405 aimbs_put16(hdrbs, 0x0000);
|
|
406 aimbs_put16(hdrbs, 0x0000);
|
|
407 aimbs_put16(hdrbs, 0x0000);
|
|
408 aimbs_put16(hdrbs, 0x0000);
|
|
409 aimbs_put16(hdrbs, 0x0000);
|
|
410 aimbs_put8(hdrbs, 0x00);
|
|
411
|
|
412 /* end of hdr2 */
|
|
413
|
13239
|
414 #if 0 /* XXX - this is how you send buddy icon info... */
|
13234
|
415 aimbs_put16(hdrbs, 0x0008);
|
|
416 aimbs_put16(hdrbs, 0x000c);
|
|
417 aimbs_put16(hdrbs, 0x0000);
|
|
418 aimbs_put16(hdrbs, 0x1466);
|
|
419 aimbs_put16(hdrbs, 0x0001);
|
|
420 aimbs_put16(hdrbs, 0x2e0f);
|
|
421 aimbs_put16(hdrbs, 0x393e);
|
|
422 aimbs_put16(hdrbs, 0xcac8);
|
|
423 #endif
|
|
424 aimbs_putraw(hdrbs, (guchar *)msg, len);
|
|
425
|
|
426 aim_tx_enqueue(sess, fr);
|
|
427
|
|
428 return 0;
|
|
429 }
|
|
430
|
|
431 /**
|
|
432 * Get the screen name of the peer of a direct connection.
|
|
433 *
|
|
434 * @param conn The ODC connection.
|
|
435 * @return The screen name of the dude, or NULL if there was an anomaly.
|
|
436 */
|
13239
|
437 const char *
|
|
438 aim_odc_getsn(OscarConnection *conn)
|
13234
|
439 {
|
|
440 struct aim_odc_intdata *intdata;
|
|
441
|
|
442 if (!conn || !conn->internal)
|
|
443 return NULL;
|
|
444
|
|
445 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) ||
|
|
446 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM))
|
|
447 return NULL;
|
|
448
|
|
449 intdata = (struct aim_odc_intdata *)conn->internal;
|
|
450
|
|
451 return intdata->sn;
|
|
452 }
|
|
453
|
|
454 /**
|
|
455 * Get the cookie of a direct connection.
|
|
456 *
|
|
457 * @param conn The ODC connection.
|
|
458 * @return The cookie, an 8 byte unterminated string, or NULL if there was an anomaly.
|
|
459 */
|
13239
|
460 const guchar *
|
|
461 aim_odc_getcookie(OscarConnection *conn)
|
13234
|
462 {
|
|
463 struct aim_odc_intdata *intdata;
|
|
464
|
|
465 if (!conn || !conn->internal)
|
|
466 return NULL;
|
|
467
|
|
468 intdata = (struct aim_odc_intdata *)conn->internal;
|
|
469
|
|
470 return intdata->cookie;
|
|
471 }
|
|
472
|
|
473 /**
|
|
474 * Find the conn of a direct connection with the given buddy.
|
|
475 *
|
|
476 * @param sess The session.
|
|
477 * @param sn The screen name of the buddy whose direct connection you want to find.
|
13239
|
478 * @return The conn for the direct connection with the given buddy, or NULL if no
|
13234
|
479 * connection was found.
|
|
480 */
|
13239
|
481 OscarConnection *
|
|
482 aim_odc_getconn(OscarSession *sess, const char *sn)
|
13234
|
483 {
|
13239
|
484 OscarConnection *cur;
|
13234
|
485 struct aim_odc_intdata *intdata;
|
|
486
|
|
487 if (!sess || !sn || !strlen(sn))
|
|
488 return NULL;
|
|
489
|
|
490 for (cur = sess->connlist; cur; cur = cur->next) {
|
|
491 if ((cur->type == AIM_CONN_TYPE_RENDEZVOUS) && (cur->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)) {
|
|
492 intdata = cur->internal;
|
|
493 if (!aim_sncmp(intdata->sn, sn))
|
|
494 return cur;
|
|
495 }
|
|
496 }
|
|
497
|
|
498 return NULL;
|
|
499 }
|
|
500
|
|
501 /**
|
|
502 * For those times when we want to open up the direct connection channel ourselves.
|
|
503 *
|
|
504 * You'll want to set up some kind of watcher on this socket.
|
|
505 * When the state changes, call aim_handlerendconnection with
|
|
506 * the connection returned by this. aim_handlerendconnection
|
|
507 * will accept the pending connection and stop listening.
|
|
508 *
|
|
509 * @param sess The session
|
|
510 * @param sn The screen name to connect to.
|
|
511 * @return The new connection.
|
|
512 */
|
13239
|
513 OscarConnection *
|
|
514 aim_odc_initiate(OscarSession *sess, const char *sn, int listenfd,
|
|
515 const guint8 *localip, guint16 port, const guint8 *mycookie)
|
13234
|
516 {
|
13239
|
517 OscarConnection *newconn;
|
|
518 IcbmCookie *cookie;
|
13234
|
519 struct aim_odc_intdata *priv;
|
|
520 guint8 ck[8];
|
|
521
|
|
522 if (!localip)
|
|
523 return NULL;
|
|
524
|
|
525 if (mycookie) {
|
|
526 memcpy(ck, mycookie, 8);
|
|
527 aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port);
|
|
528 } else
|
|
529 aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port);
|
|
530
|
13239
|
531 cookie = (IcbmCookie *)calloc(1, sizeof(IcbmCookie));
|
13234
|
532 memcpy(cookie->cookie, ck, 8);
|
|
533 cookie->type = AIM_COOKIETYPE_OFTIM;
|
|
534
|
|
535 /* this one is for the cookie */
|
|
536 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata));
|
|
537
|
|
538 memcpy(priv->cookie, ck, 8);
|
|
539 strncpy(priv->sn, sn, sizeof(priv->sn));
|
|
540 cookie->data = priv;
|
|
541 aim_cachecookie(sess, cookie);
|
|
542
|
|
543 /* XXX - switch to aim_cloneconn()? */
|
|
544 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) {
|
|
545 close(listenfd);
|
|
546 return NULL;
|
|
547 }
|
|
548
|
|
549 /* this one is for the conn */
|
|
550 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata));
|
|
551
|
|
552 memcpy(priv->cookie, ck, 8);
|
|
553 strncpy(priv->sn, sn, sizeof(priv->sn));
|
|
554
|
|
555 newconn->fd = listenfd;
|
|
556 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
|
|
557 newconn->internal = priv;
|
|
558 newconn->lastactivity = time(NULL);
|
|
559
|
|
560 return newconn;
|
|
561 }
|
|
562
|
|
563 /**
|
|
564 * Connect directly to the given buddy for directim.
|
|
565 *
|
|
566 * This is a wrapper for aim_newconn.
|
|
567 *
|
13239
|
568 * If addr is NULL, the socket is not created, but the connection is
|
13234
|
569 * allocated and setup to connect.
|
|
570 *
|
|
571 * @param sess The Godly session.
|
|
572 * @param sn The screen name we're connecting to. I hope it's a girl...
|
|
573 * @param addr Address to connect to.
|
|
574 * @return The new connection.
|
|
575 */
|
13239
|
576 OscarConnection *
|
|
577 aim_odc_connect(OscarSession *sess, const char *sn, const char *addr, const guint8 *cookie)
|
13234
|
578 {
|
13239
|
579 OscarConnection *newconn;
|
13234
|
580 struct aim_odc_intdata *intdata;
|
|
581
|
|
582 if (!sess || !sn)
|
|
583 return NULL;
|
|
584
|
|
585 if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata))))
|
|
586 return NULL;
|
|
587 memcpy(intdata->cookie, cookie, 8);
|
|
588 strncpy(intdata->sn, sn, sizeof(intdata->sn));
|
|
589 if (addr)
|
|
590 strncpy(intdata->ip, addr, sizeof(intdata->ip));
|
|
591
|
|
592 /* XXX - verify that non-blocking connects actually work */
|
|
593 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS))) {
|
|
594 free(intdata);
|
|
595 return NULL;
|
|
596 }
|
|
597
|
|
598 newconn->internal = intdata;
|
|
599 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
|
|
600
|
|
601 return newconn;
|
|
602 }
|
|
603
|
|
604 /**
|
|
605 * Sometimes you just don't know with these kinds of people.
|
|
606 *
|
|
607 * @param sess The session.
|
|
608 * @param conn The ODC connection of the incoming data.
|
|
609 * @param frr The frame allocated for the incoming data.
|
|
610 * @param bs It stands for "bologna sandwich."
|
|
611 * @return Return 0 if no errors, otherwise return the error number.
|
|
612 */
|
13239
|
613 static int
|
|
614 handlehdr_odc(OscarSession *sess, OscarConnection *conn, FlapFrame *frr, ByteStream *bs)
|
13234
|
615 {
|
13239
|
616 FlapFrame fr;
|
13234
|
617 int ret = 0;
|
|
618 aim_rxcallback_t userfunc;
|
|
619 guint32 payloadlength;
|
|
620 guint16 flags, encoding;
|
|
621 char *snptr = NULL;
|
|
622
|
|
623 fr.conn = conn;
|
|
624
|
|
625 /* AAA - ugly */
|
|
626 aim_bstream_setpos(bs, 20);
|
|
627 payloadlength = aimbs_get32(bs);
|
|
628
|
|
629 aim_bstream_setpos(bs, 24);
|
|
630 encoding = aimbs_get16(bs);
|
|
631
|
|
632 aim_bstream_setpos(bs, 30);
|
|
633 flags = aimbs_get16(bs);
|
|
634
|
|
635 aim_bstream_setpos(bs, 36);
|
|
636 /* XXX - create an aimbs_getnullstr function? */
|
|
637 snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */
|
|
638
|
|
639 gaim_debug_misc("oscar", "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr);
|
|
640
|
|
641 if (flags & 0x0008) {
|
13239
|
642 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
643 ret = userfunc(sess, &fr, snptr, 2);
|
|
644 } else if (flags & 0x0004) {
|
13239
|
645 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
646 ret = userfunc(sess, &fr, snptr, 1);
|
|
647 } else {
|
13239
|
648 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
649 ret = userfunc(sess, &fr, snptr, 0);
|
|
650 }
|
|
651
|
|
652 if ((payloadlength != 0) && (payloadlength != UINT_MAX)) {
|
|
653 char *msg;
|
|
654 int recvd = 0;
|
|
655 int i, isawaymsg;
|
|
656
|
|
657 isawaymsg = flags & 0x0001;
|
|
658
|
|
659 if (!(msg = calloc(1, payloadlength+1))) {
|
|
660 free(snptr);
|
|
661 return -ENOMEM;
|
|
662 }
|
|
663
|
|
664 while (payloadlength - recvd) {
|
|
665 if (payloadlength - recvd >= 1024)
|
|
666 i = aim_recv(conn->fd, &msg[recvd], 1024);
|
13239
|
667 else
|
13234
|
668 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd);
|
|
669 if (i <= 0) {
|
|
670 free(msg);
|
|
671 free(snptr);
|
|
672 return -1;
|
|
673 }
|
|
674 recvd = recvd + i;
|
|
675 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER)))
|
|
676 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength);
|
|
677 }
|
13239
|
678
|
|
679 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMINCOMING)))
|
13234
|
680 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg);
|
|
681
|
|
682 free(msg);
|
|
683 }
|
|
684
|
|
685 free(snptr);
|
|
686
|
|
687 return ret;
|
|
688 }
|
|
689
|
13252
|
690 PeerConnection *
|
13239
|
691 aim_oft_createinfo(OscarSession *sess, const guint8 *cookie, const char *sn, const char *ip, guint16 port, guint32 size, guint32 modtime, char *filename, int send_or_recv, int method, int stage)
|
13234
|
692 {
|
13252
|
693 PeerConnection *new;
|
13234
|
694
|
|
695 if (!sess)
|
|
696 return NULL;
|
|
697
|
13252
|
698 if (!(new = (PeerConnection *)calloc(1, sizeof(PeerConnection))))
|
13234
|
699 return NULL;
|
|
700
|
|
701 new->sess = sess;
|
|
702 if (cookie)
|
|
703 memcpy(new->cookie, cookie, 8);
|
|
704 else
|
|
705 aim_icbm_makecookie(new->cookie);
|
|
706 if (ip)
|
|
707 new->clientip = strdup(ip);
|
|
708 else
|
|
709 new->clientip = NULL;
|
|
710 if (sn)
|
|
711 new->sn = strdup(sn);
|
|
712 else
|
|
713 new->sn = NULL;
|
|
714 new->method = method;
|
|
715 new->send_or_recv = send_or_recv;
|
|
716 new->stage = stage;
|
|
717 new->port = port;
|
|
718 new->xfer_reffed = FALSE;
|
|
719 new->success = FALSE;
|
|
720 new->fh.totfiles = 1;
|
|
721 new->fh.filesleft = 1;
|
|
722 new->fh.totparts = 1;
|
|
723 new->fh.partsleft = 1;
|
|
724 new->fh.totsize = size;
|
|
725 new->fh.size = size;
|
|
726 new->fh.modtime = modtime;
|
|
727 new->fh.checksum = 0xffff0000;
|
|
728 new->fh.rfrcsum = 0xffff0000;
|
|
729 new->fh.rfcsum = 0xffff0000;
|
|
730 new->fh.recvcsum = 0xffff0000;
|
|
731 strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31);
|
|
732 if (filename) {
|
|
733 strncpy(new->fh.name, filename, 63);
|
|
734 new->fh.name[63] = '\0';
|
|
735 }
|
|
736
|
13252
|
737 sess->peer_connections = g_list_prepend(sess->peer_connections, new);
|
13234
|
738
|
|
739 return new;
|
|
740 }
|
|
741
|
13239
|
742 PeerProxyInfo *aim_rv_proxy_createinfo(OscarSession *sess, const guint8 *cookie,
|
13234
|
743 guint16 port)
|
|
744 {
|
13239
|
745 PeerProxyInfo *proxy_info;
|
|
746
|
|
747 if (!(proxy_info = (PeerProxyInfo*)calloc(1, sizeof(PeerProxyInfo))))
|
13234
|
748 return NULL;
|
13239
|
749
|
13234
|
750 proxy_info->sess = sess;
|
|
751 proxy_info->port = port;
|
|
752 proxy_info->packet_ver = AIM_RV_PROXY_PACKETVER_DFLT;
|
|
753 proxy_info->unknownA = AIM_RV_PROXY_UNKNOWNA_DFLT;
|
13239
|
754
|
13234
|
755 if (cookie)
|
|
756 memcpy(proxy_info->cookie, cookie, 8);
|
13239
|
757
|
13234
|
758 return proxy_info;
|
|
759 }
|
|
760
|
|
761 /**
|
13252
|
762 * Remove the given PeerConnection from the PeerConnection linked list, and
|
13234
|
763 * then free its memory.
|
|
764 *
|
|
765 * @param sess The session.
|
13252
|
766 * @param peer_connection The PeerConnection that we're destroying.
|
13234
|
767 * @return Return 0 if no errors, otherwise return the error number.
|
|
768 */
|
13239
|
769 int
|
13252
|
770 aim_oft_destroyinfo(PeerConnection *peer_connection)
|
13234
|
771 {
|
13239
|
772 OscarSession *sess;
|
13234
|
773
|
13252
|
774 if (!peer_connection || !(sess = peer_connection->sess))
|
13234
|
775 return -EINVAL;
|
|
776
|
13252
|
777 sess->peer_connections = g_list_remove(sess->peer_connections, peer_connection);
|
13234
|
778
|
13252
|
779 free(peer_connection->sn);
|
|
780 free(peer_connection->proxyip);
|
|
781 free(peer_connection->clientip);
|
|
782 free(peer_connection->verifiedip);
|
|
783 free(peer_connection);
|
13234
|
784
|
|
785 return 0;
|
|
786 }
|
|
787
|
|
788 /**
|
|
789 * Creates a listener socket so the other dude can connect to us.
|
|
790 *
|
13239
|
791 * You'll want to set up some kind of watcher on this socket.
|
|
792 * When the state changes, call aim_handlerendconnection with
|
|
793 * the connection returned by this. aim_handlerendconnection
|
13234
|
794 * will accept the pending connection and stop listening.
|
|
795 *
|
|
796 * @param sess The session.
|
13252
|
797 * @param peer_connection File transfer information associated with this
|
13234
|
798 * connection.
|
|
799 * @return Return 0 if no errors, otherwise return the error number.
|
|
800 */
|
13239
|
801 int
|
13252
|
802 aim_sendfile_listen(OscarSession *sess, PeerConnection *peer_connection, int listenfd)
|
13234
|
803 {
|
13252
|
804 if (!peer_connection)
|
13234
|
805 return -EINVAL;
|
|
806
|
13252
|
807 if (!(peer_connection->conn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) {
|
13234
|
808 close(listenfd);
|
|
809 return -ENOMEM;
|
|
810 }
|
|
811
|
13252
|
812 peer_connection->conn->fd = listenfd;
|
|
813 peer_connection->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE;
|
|
814 peer_connection->conn->lastactivity = time(NULL);
|
13234
|
815
|
|
816 return 0;
|
|
817 }
|
|
818
|
|
819 /**
|
|
820 * Extract an &aim_fileheader_t from the given buffer.
|
|
821 *
|
|
822 * @param bs The should be from an incoming rendezvous packet.
|
|
823 * @return A pointer to new struct on success, or NULL on error.
|
|
824 */
|
13239
|
825 static PeerFrame *
|
|
826 aim_oft_getheader(ByteStream *bs)
|
13234
|
827 {
|
13239
|
828 PeerFrame *fh;
|
13234
|
829
|
13239
|
830 if (!(fh = calloc(1, sizeof(PeerFrame))))
|
13234
|
831 return NULL;
|
|
832
|
|
833 /* The bstream should be positioned after the hdrtype. */
|
|
834 aimbs_getrawbuf(bs, fh->bcookie, 8);
|
|
835 fh->encrypt = aimbs_get16(bs);
|
|
836 fh->compress = aimbs_get16(bs);
|
|
837 fh->totfiles = aimbs_get16(bs);
|
|
838 fh->filesleft = aimbs_get16(bs);
|
|
839 fh->totparts = aimbs_get16(bs);
|
|
840 fh->partsleft = aimbs_get16(bs);
|
|
841 fh->totsize = aimbs_get32(bs);
|
|
842 fh->size = aimbs_get32(bs);
|
|
843 fh->modtime = aimbs_get32(bs);
|
|
844 fh->checksum = aimbs_get32(bs);
|
|
845 fh->rfrcsum = aimbs_get32(bs);
|
|
846 fh->rfsize = aimbs_get32(bs);
|
|
847 fh->cretime = aimbs_get32(bs);
|
|
848 fh->rfcsum = aimbs_get32(bs);
|
|
849 fh->nrecvd = aimbs_get32(bs);
|
|
850 fh->recvcsum = aimbs_get32(bs);
|
|
851 aimbs_getrawbuf(bs, (guchar *)fh->idstring, 32);
|
|
852 fh->flags = aimbs_get8(bs);
|
|
853 fh->lnameoffset = aimbs_get8(bs);
|
|
854 fh->lsizeoffset = aimbs_get8(bs);
|
|
855 aimbs_getrawbuf(bs, (guchar *)fh->dummy, 69);
|
|
856 aimbs_getrawbuf(bs, (guchar *)fh->macfileinfo, 16);
|
|
857 fh->nencode = aimbs_get16(bs);
|
|
858 fh->nlanguage = aimbs_get16(bs);
|
|
859 aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */
|
|
860 fh->name[63] = '\0';
|
|
861
|
|
862 return fh;
|
13239
|
863 }
|
13234
|
864
|
|
865 /**
|
|
866 * Fills a buffer with network-order fh data
|
|
867 *
|
|
868 * @param bs A bstream to fill -- automatically initialized
|
13239
|
869 * @param fh A PeerFrame to get data from.
|
13234
|
870 * @return Return non-zero on error.
|
|
871 */
|
13239
|
872 static int
|
|
873 aim_oft_buildheader(ByteStream *bs, PeerFrame *fh)
|
|
874 {
|
13234
|
875 guint8 *hdr;
|
|
876
|
|
877 if (!bs || !fh)
|
|
878 return -EINVAL;
|
|
879
|
|
880 if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8)))
|
|
881 return -ENOMEM;
|
|
882
|
|
883 aim_bstream_init(bs, hdr, 0x100 - 8);
|
|
884 aimbs_putraw(bs, fh->bcookie, 8);
|
|
885 aimbs_put16(bs, fh->encrypt);
|
|
886 aimbs_put16(bs, fh->compress);
|
|
887 aimbs_put16(bs, fh->totfiles);
|
|
888 aimbs_put16(bs, fh->filesleft);
|
|
889 aimbs_put16(bs, fh->totparts);
|
|
890 aimbs_put16(bs, fh->partsleft);
|
|
891 aimbs_put32(bs, fh->totsize);
|
|
892 aimbs_put32(bs, fh->size);
|
|
893 aimbs_put32(bs, fh->modtime);
|
|
894 aimbs_put32(bs, fh->checksum);
|
|
895 aimbs_put32(bs, fh->rfrcsum);
|
|
896 aimbs_put32(bs, fh->rfsize);
|
|
897 aimbs_put32(bs, fh->cretime);
|
|
898 aimbs_put32(bs, fh->rfcsum);
|
|
899 aimbs_put32(bs, fh->nrecvd);
|
|
900 aimbs_put32(bs, fh->recvcsum);
|
|
901 aimbs_putraw(bs, (guchar *)fh->idstring, 32);
|
|
902 aimbs_put8(bs, fh->flags);
|
|
903 aimbs_put8(bs, fh->lnameoffset);
|
|
904 aimbs_put8(bs, fh->lsizeoffset);
|
|
905 aimbs_putraw(bs, (guchar *)fh->dummy, 69);
|
|
906 aimbs_putraw(bs, (guchar *)fh->macfileinfo, 16);
|
|
907 aimbs_put16(bs, fh->nencode);
|
|
908 aimbs_put16(bs, fh->nlanguage);
|
|
909 aimbs_putraw(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */
|
|
910
|
|
911 return 0;
|
|
912 }
|
|
913
|
|
914 /**
|
|
915 * Create an OFT packet based on the given information, and send it on its merry way.
|
|
916 *
|
|
917 * @param sess The session.
|
|
918 * @param type The subtype of the OFT packet we're sending.
|
13252
|
919 * @param peer_connection The PeerConnection with the connection and OFT
|
13234
|
920 * info we're sending.
|
|
921 * @return Return 0 if no errors, otherwise return the error number.
|
|
922 */
|
13252
|
923 int aim_oft_sendheader(OscarSession *sess, guint16 type, PeerConnection *peer_connection)
|
13234
|
924 {
|
13239
|
925 FlapFrame *fr;
|
13234
|
926
|
13252
|
927 if (!sess || !peer_connection || !peer_connection->conn || (peer_connection->conn->type != AIM_CONN_TYPE_RENDEZVOUS))
|
13234
|
928 return -EINVAL;
|
|
929
|
|
930 #if 0
|
|
931 /*
|
13239
|
932 * If you are receiving a file, the cookie should be null, if you are sending a
|
|
933 * file, the cookie should be the same as the one used in the ICBM negotiation
|
13234
|
934 * SNACs.
|
|
935 */
|
|
936 fh->lnameoffset = 0x1a;
|
|
937 fh->lsizeoffset = 0x10;
|
|
938
|
|
939 /* These should be the same as charset and charsubset in ICBMs */
|
|
940 fh->nencode = 0x0000;
|
|
941 fh->nlanguage = 0x0000;
|
|
942 #endif
|
|
943
|
13252
|
944 aim_oft_dirconvert_tostupid(peer_connection->fh.name);
|
13234
|
945
|
13253
|
946 if (!(fr = flap_frame_new(sess, peer_connection->conn, AIM_FRAMETYPE_OFT, type, 0)))
|
13234
|
947 return -ENOMEM;
|
|
948
|
13252
|
949 if (aim_oft_buildheader(&fr->data, &peer_connection->fh) == -1) {
|
13234
|
950 aim_frame_destroy(fr);
|
|
951 return -ENOMEM;
|
|
952 }
|
|
953
|
|
954 memcpy(fr->hdr.rend.magic, "OFT2", 4);
|
|
955 fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8;
|
|
956
|
|
957 aim_tx_enqueue(sess, fr);
|
|
958
|
|
959 return 0;
|
|
960 }
|
|
961
|
|
962 /**
|
|
963 * Create a rendezvous "init recv" packet and send it on its merry way.
|
|
964 * This is the first packet sent to the proxy server by the second client
|
|
965 * involved in this rendezvous proxy session.
|
|
966 *
|
|
967 * @param sess The session.
|
|
968 * @param proxy_info Changable pieces of data for this packet
|
|
969 * @return Return 0 if no errors, otherwise return the error number.
|
|
970 */
|
13239
|
971 int
|
|
972 aim_rv_proxy_init_recv(PeerProxyInfo *proxy_info)
|
13234
|
973 {
|
|
974 #if 0
|
|
975 aim_tlvlist_t *tlvlist_sendfile;
|
|
976 #endif
|
13239
|
977 ByteStream bs;
|
13234
|
978 guint8 *bs_raw;
|
|
979 guint16 packet_len;
|
|
980 guint8 sn_len;
|
|
981 int err;
|
|
982
|
|
983 err = 0;
|
|
984
|
|
985 if (!proxy_info)
|
|
986 return -EINVAL;
|
|
987
|
|
988 sn_len = strlen(proxy_info->sess->sn);
|
|
989 packet_len = 2 + 2 /* packet_len, packet_ver */
|
|
990 + 2 + 4 /* cmd_type, unknownA */
|
|
991 + 2 /* flags */
|
|
992 + 1 + sn_len /* Length/value pair for screenname */
|
|
993 + 8 /* ICBM Cookie */
|
|
994 + 2 /* port */
|
|
995 + 2 + 2 + 16; /* TLV for Filesend capability block */
|
|
996
|
|
997 if (!(bs_raw = malloc(packet_len)))
|
|
998 return -ENOMEM;
|
|
999
|
|
1000 aim_bstream_init(&bs, bs_raw, packet_len);
|
|
1001 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */
|
|
1002 aimbs_put16(&bs, proxy_info->packet_ver);
|
|
1003 aimbs_put16(&bs, AIM_RV_PROXY_INIT_RECV);
|
|
1004 aimbs_put32(&bs, proxy_info->unknownA);
|
|
1005 aimbs_put16(&bs, proxy_info->flags);
|
|
1006 aimbs_put8(&bs, sn_len);
|
|
1007 aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len);
|
|
1008 aimbs_put16(&bs, proxy_info->port);
|
|
1009 aimbs_putraw(&bs, proxy_info->cookie, 8);
|
|
1010
|
|
1011 aimbs_put16(&bs, 0x0001); /* Type */
|
|
1012 aimbs_put16(&bs, 16); /* Length */
|
|
1013 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */
|
|
1014
|
|
1015
|
|
1016 #if 0
|
|
1017 /* TODO: Use built-in TLV */
|
|
1018 aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE);
|
|
1019 aim_tlvlist_write(&bs, &tlvlist_sendfile);
|
|
1020 #endif
|
|
1021
|
|
1022 aim_bstream_rewind(&bs);
|
|
1023 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len)
|
|
1024 err = errno;
|
|
1025 proxy_info->conn->lastactivity = time(NULL);
|
|
1026
|
|
1027 #if 0
|
|
1028 aim_tlvlist_free(tlvlist_sendfile);
|
|
1029 #endif
|
|
1030 free(bs_raw);
|
|
1031
|
|
1032 return err;
|
|
1033 }
|
|
1034
|
|
1035
|
|
1036 /**
|
|
1037 * Create a rendezvous "init send" packet and send it on its merry way.
|
|
1038 * This is the first packet sent to the proxy server by the client
|
|
1039 * first indicating that this will be a proxied connection
|
|
1040 *
|
|
1041 * @param sess The session.
|
|
1042 * @param proxy_info Changable pieces of data for this packet
|
|
1043 * @return Return 0 if no errors, otherwise return the error number.
|
|
1044 */
|
13239
|
1045 int
|
|
1046 aim_rv_proxy_init_send(PeerProxyInfo *proxy_info)
|
13234
|
1047 {
|
|
1048 #if 0
|
|
1049 aim_tlvlist_t *tlvlist_sendfile;
|
|
1050 #endif
|
13239
|
1051 ByteStream bs;
|
13234
|
1052 guint8 *bs_raw;
|
|
1053 guint16 packet_len;
|
|
1054 guint8 sn_len;
|
|
1055 int err;
|
|
1056
|
|
1057 err = 0;
|
|
1058
|
|
1059 if (!proxy_info)
|
|
1060 return -EINVAL;
|
|
1061
|
|
1062 sn_len = strlen(proxy_info->sess->sn);
|
|
1063 packet_len = 2 + 2 /* packet_len, packet_ver */
|
|
1064 + 2 + 4 /* cmd_type, unknownA */
|
|
1065 + 2 /* flags */
|
|
1066 + 1 + sn_len /* Length/value pair for screenname */
|
|
1067 + 8 /* ICBM Cookie */
|
|
1068 + 2 + 2 + 16; /* TLV for Filesend capability block */
|
|
1069
|
|
1070 if (!(bs_raw = malloc(packet_len)))
|
|
1071 return -ENOMEM;
|
|
1072
|
|
1073 aim_bstream_init(&bs, bs_raw, packet_len);
|
|
1074 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */
|
|
1075 aimbs_put16(&bs, proxy_info->packet_ver);
|
|
1076 aimbs_put16(&bs, AIM_RV_PROXY_INIT_SEND);
|
|
1077 aimbs_put32(&bs, proxy_info->unknownA);
|
|
1078 aimbs_put16(&bs, proxy_info->flags);
|
|
1079 aimbs_put8(&bs, sn_len);
|
|
1080 aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len);
|
|
1081 aimbs_putraw(&bs, proxy_info->cookie, 8);
|
|
1082
|
|
1083 aimbs_put16(&bs, 0x0001); /* Type */
|
|
1084 aimbs_put16(&bs, 16); /* Length */
|
|
1085 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */
|
|
1086
|
|
1087 /* TODO: Use built-in TLV */
|
|
1088 #if 0
|
|
1089 aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE);
|
|
1090 aim_tlvlist_write(&bs, &tlvlist_sendfile);
|
|
1091 #endif
|
|
1092
|
|
1093 aim_bstream_rewind(&bs);
|
|
1094 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len)
|
|
1095 err = errno;
|
|
1096 proxy_info->conn->lastactivity = time(NULL);
|
|
1097
|
|
1098 #if 0
|
|
1099 aim_tlvlist_free(tlvlist_sendfile);
|
|
1100 #endif
|
|
1101 free(bs_raw);
|
|
1102
|
|
1103 return err;
|
|
1104 }
|
|
1105
|
|
1106 /**
|
|
1107 * Handle incoming data on a rendezvous connection. This is analogous to the
|
|
1108 * consumesnac function in rxhandlers.c, and I really think this should probably
|
|
1109 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet.
|
|
1110 *
|
|
1111 * @param sess The session.
|
|
1112 * @param fr The frame allocated for the incoming data.
|
|
1113 * @return Return 0 if the packet was handled correctly, otherwise return the
|
|
1114 * error number.
|
|
1115 */
|
13239
|
1116 int
|
|
1117 aim_rxdispatch_rendezvous(OscarSession *sess, FlapFrame *fr)
|
13234
|
1118 {
|
13239
|
1119 OscarConnection *conn = fr->conn;
|
13234
|
1120 int ret = 1;
|
|
1121
|
|
1122 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) {
|
|
1123 if (fr->hdr.rend.type == 0x0001)
|
|
1124 ret = handlehdr_odc(sess, conn, fr, &fr->data);
|
|
1125 else
|
|
1126 gaim_debug_info("oscar", "ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type);
|
|
1127
|
|
1128 } else {
|
|
1129 aim_rxcallback_t userfunc;
|
13239
|
1130 PeerFrame *header = aim_oft_getheader(&fr->data);
|
13234
|
1131 aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */
|
|
1132
|
|
1133 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type)))
|
|
1134 ret = userfunc(sess, fr, conn, header->bcookie, header);
|
|
1135
|
|
1136 free(header);
|
|
1137 }
|
|
1138
|
|
1139 if (ret == -1)
|
|
1140 aim_conn_close(conn);
|
|
1141
|
|
1142 return ret;
|
|
1143 }
|
|
1144
|
|
1145 /**
|
|
1146 * Handle incoming data on a rendezvous proxy connection. This is similar to
|
|
1147 * aim_rxdispatch_rendezvous above and should probably be kept with that function.
|
|
1148 *
|
|
1149 * @param sess The session.
|
|
1150 * @param fr The frame allocated for the incoming data.
|
13239
|
1151 * @return Return 0 if the packet was handled correctly, otherwise return the
|
13234
|
1152 * error number.
|
|
1153 */
|
13239
|
1154 PeerProxyInfo *
|
|
1155 aim_rv_proxy_read(OscarSession *sess, OscarConnection *conn)
|
13234
|
1156 {
|
13239
|
1157 ByteStream bs_hdr;
|
13234
|
1158 guint8 hdr_buf[AIM_RV_PROXY_HDR_LEN];
|
13239
|
1159 ByteStream bs_body; /* The body (everything but the header) of the packet */
|
13234
|
1160 guint8 *body_buf = NULL;
|
|
1161 guint8 body_len;
|
13239
|
1162
|
13234
|
1163 char str_ip[30] = {""};
|
|
1164 guint8 ip_temp[4];
|
13239
|
1165
|
13234
|
1166 guint16 len;
|
13239
|
1167 PeerProxyInfo *proxy_info;
|
|
1168
|
|
1169 if(!(proxy_info = malloc(sizeof(PeerProxyInfo))))
|
13234
|
1170 return NULL;
|
|
1171
|
|
1172 aim_bstream_init(&bs_hdr, hdr_buf, AIM_RV_PROXY_HDR_LEN);
|
|
1173 if (aim_bstream_recv(&bs_hdr, conn->fd, AIM_RV_PROXY_HDR_LEN) == AIM_RV_PROXY_HDR_LEN) {
|
|
1174 aim_bstream_rewind(&bs_hdr);
|
|
1175 len = aimbs_get16(&bs_hdr);
|
|
1176 proxy_info->packet_ver = aimbs_get16(&bs_hdr);
|
|
1177 proxy_info->cmd_type = aimbs_get16(&bs_hdr);
|
|
1178 proxy_info->unknownA = aimbs_get32(&bs_hdr);
|
|
1179 proxy_info->flags = aimbs_get16(&bs_hdr);
|
|
1180 if(proxy_info->cmd_type == AIM_RV_PROXY_READY) {
|
|
1181 /* Do a little victory dance
|
|
1182 * A ready packet contains no additional information */
|
|
1183 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ERROR) {
|
|
1184 if(len == AIM_RV_PROXY_ERROR_LEN - 2) {
|
|
1185 body_len = AIM_RV_PROXY_ERROR_LEN - AIM_RV_PROXY_HDR_LEN;
|
|
1186 body_buf = malloc(body_len);
|
|
1187 aim_bstream_init(&bs_body, body_buf, body_len);
|
|
1188 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) {
|
|
1189 aim_bstream_rewind(&bs_body);
|
|
1190 proxy_info->err_code = aimbs_get16(&bs_body);
|
|
1191 } else {
|
|
1192 gaim_debug_warning("oscar","error reading rv proxy error packet\n");
|
|
1193 aim_conn_close(conn);
|
|
1194 free(proxy_info);
|
|
1195 proxy_info = NULL;
|
|
1196 }
|
|
1197 } else {
|
|
1198 gaim_debug_warning("oscar","invalid length for proxy error packet\n");
|
|
1199 free(proxy_info);
|
|
1200 proxy_info = NULL;
|
|
1201 }
|
|
1202 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ACK) {
|
|
1203 if(len == AIM_RV_PROXY_ACK_LEN - 2) {
|
|
1204 body_len = AIM_RV_PROXY_ACK_LEN - AIM_RV_PROXY_HDR_LEN;
|
|
1205 body_buf = malloc(body_len);
|
|
1206 aim_bstream_init(&bs_body, body_buf, body_len);
|
|
1207 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) {
|
|
1208 int i;
|
|
1209 aim_bstream_rewind(&bs_body);
|
|
1210 proxy_info->port = aimbs_get16(&bs_body);
|
|
1211 for(i=0; i<4; i++)
|
|
1212 ip_temp[i] = aimbs_get8(&bs_body);
|
|
1213 snprintf(str_ip, sizeof(str_ip), "%hhu.%hhu.%hhu.%hhu",
|
|
1214 ip_temp[0], ip_temp[1],
|
|
1215 ip_temp[2], ip_temp[3]);
|
|
1216 proxy_info->ip = strdup(str_ip);
|
|
1217 } else {
|
|
1218 gaim_debug_warning("oscar","error reading rv proxy error packet\n");
|
|
1219 aim_conn_close(conn);
|
|
1220 free(proxy_info);
|
|
1221 proxy_info = NULL;
|
|
1222 }
|
|
1223 } else {
|
|
1224 gaim_debug_warning("oscar","invalid length for proxy error packet\n");
|
|
1225 free(proxy_info);
|
|
1226 proxy_info = NULL;
|
|
1227 }
|
|
1228 } else {
|
|
1229 gaim_debug_warning("oscar","unknown type for aim rendezvous proxy packet\n");
|
13239
|
1230 }
|
13234
|
1231 } else {
|
|
1232 gaim_debug_warning("oscar","error reading header of rv proxy packet\n");
|
|
1233 aim_conn_close(conn);
|
|
1234 free(proxy_info);
|
|
1235 proxy_info = NULL;
|
|
1236 }
|
|
1237 if(body_buf) {
|
|
1238 free(body_buf);
|
|
1239 body_buf = NULL;
|
|
1240 }
|
|
1241 return proxy_info;
|
|
1242 }
|