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);
|
13254
|
224 aim_conn_close(sess, cur);
|
13234
|
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);
|
13254
|
233 aim_conn_close(sess, cur);
|
13234
|
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");
|
13254
|
260 aim_conn_close(sess, newconn);
|
13234
|
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 {
|
13254
|
484 GList *cur;
|
13234
|
485 struct aim_odc_intdata *intdata;
|
|
486
|
|
487 if (!sess || !sn || !strlen(sn))
|
|
488 return NULL;
|
|
489
|
13254
|
490 for (cur = sess->oscar_connections; cur; cur = cur->next)
|
|
491 {
|
|
492 OscarConnection *conn;
|
|
493 conn = cur->data;
|
|
494 if ((conn->type == AIM_CONN_TYPE_RENDEZVOUS) && (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)) {
|
|
495 intdata = conn->internal;
|
13234
|
496 if (!aim_sncmp(intdata->sn, sn))
|
13254
|
497 return conn;
|
13234
|
498 }
|
|
499 }
|
|
500
|
|
501 return NULL;
|
|
502 }
|
|
503
|
|
504 /**
|
|
505 * For those times when we want to open up the direct connection channel ourselves.
|
|
506 *
|
|
507 * You'll want to set up some kind of watcher on this socket.
|
|
508 * When the state changes, call aim_handlerendconnection with
|
|
509 * the connection returned by this. aim_handlerendconnection
|
|
510 * will accept the pending connection and stop listening.
|
|
511 *
|
|
512 * @param sess The session
|
|
513 * @param sn The screen name to connect to.
|
|
514 * @return The new connection.
|
|
515 */
|
13239
|
516 OscarConnection *
|
|
517 aim_odc_initiate(OscarSession *sess, const char *sn, int listenfd,
|
|
518 const guint8 *localip, guint16 port, const guint8 *mycookie)
|
13234
|
519 {
|
13239
|
520 OscarConnection *newconn;
|
|
521 IcbmCookie *cookie;
|
13234
|
522 struct aim_odc_intdata *priv;
|
|
523 guint8 ck[8];
|
|
524
|
|
525 if (!localip)
|
|
526 return NULL;
|
|
527
|
|
528 if (mycookie) {
|
|
529 memcpy(ck, mycookie, 8);
|
|
530 aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port);
|
|
531 } else
|
|
532 aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port);
|
|
533
|
13239
|
534 cookie = (IcbmCookie *)calloc(1, sizeof(IcbmCookie));
|
13234
|
535 memcpy(cookie->cookie, ck, 8);
|
|
536 cookie->type = AIM_COOKIETYPE_OFTIM;
|
|
537
|
|
538 /* this one is for the cookie */
|
|
539 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata));
|
|
540
|
|
541 memcpy(priv->cookie, ck, 8);
|
|
542 strncpy(priv->sn, sn, sizeof(priv->sn));
|
|
543 cookie->data = priv;
|
|
544 aim_cachecookie(sess, cookie);
|
|
545
|
|
546 /* XXX - switch to aim_cloneconn()? */
|
13254
|
547 if (!(newconn = oscar_connection_new(sess, AIM_CONN_TYPE_LISTENER))) {
|
13234
|
548 close(listenfd);
|
|
549 return NULL;
|
|
550 }
|
|
551
|
|
552 /* this one is for the conn */
|
|
553 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata));
|
|
554
|
|
555 memcpy(priv->cookie, ck, 8);
|
|
556 strncpy(priv->sn, sn, sizeof(priv->sn));
|
|
557
|
|
558 newconn->fd = listenfd;
|
|
559 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
|
|
560 newconn->internal = priv;
|
|
561 newconn->lastactivity = time(NULL);
|
|
562
|
|
563 return newconn;
|
|
564 }
|
|
565
|
|
566 /**
|
|
567 * Connect directly to the given buddy for directim.
|
|
568 *
|
13254
|
569 * This is a wrapper for oscar_connection_new.
|
13234
|
570 *
|
13239
|
571 * If addr is NULL, the socket is not created, but the connection is
|
13234
|
572 * allocated and setup to connect.
|
|
573 *
|
|
574 * @param sess The Godly session.
|
|
575 * @param sn The screen name we're connecting to. I hope it's a girl...
|
|
576 * @param addr Address to connect to.
|
|
577 * @return The new connection.
|
|
578 */
|
13239
|
579 OscarConnection *
|
|
580 aim_odc_connect(OscarSession *sess, const char *sn, const char *addr, const guint8 *cookie)
|
13234
|
581 {
|
13239
|
582 OscarConnection *newconn;
|
13234
|
583 struct aim_odc_intdata *intdata;
|
|
584
|
|
585 if (!sess || !sn)
|
|
586 return NULL;
|
|
587
|
|
588 if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata))))
|
|
589 return NULL;
|
|
590 memcpy(intdata->cookie, cookie, 8);
|
|
591 strncpy(intdata->sn, sn, sizeof(intdata->sn));
|
|
592 if (addr)
|
|
593 strncpy(intdata->ip, addr, sizeof(intdata->ip));
|
|
594
|
|
595 /* XXX - verify that non-blocking connects actually work */
|
13254
|
596 if (!(newconn = oscar_connection_new(sess, AIM_CONN_TYPE_RENDEZVOUS))) {
|
13234
|
597 free(intdata);
|
|
598 return NULL;
|
|
599 }
|
|
600
|
|
601 newconn->internal = intdata;
|
|
602 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
|
|
603
|
|
604 return newconn;
|
|
605 }
|
|
606
|
|
607 /**
|
|
608 * Sometimes you just don't know with these kinds of people.
|
|
609 *
|
|
610 * @param sess The session.
|
|
611 * @param conn The ODC connection of the incoming data.
|
|
612 * @param frr The frame allocated for the incoming data.
|
|
613 * @param bs It stands for "bologna sandwich."
|
|
614 * @return Return 0 if no errors, otherwise return the error number.
|
|
615 */
|
13239
|
616 static int
|
|
617 handlehdr_odc(OscarSession *sess, OscarConnection *conn, FlapFrame *frr, ByteStream *bs)
|
13234
|
618 {
|
13239
|
619 FlapFrame fr;
|
13234
|
620 int ret = 0;
|
|
621 aim_rxcallback_t userfunc;
|
|
622 guint32 payloadlength;
|
|
623 guint16 flags, encoding;
|
|
624 char *snptr = NULL;
|
|
625
|
|
626 fr.conn = conn;
|
|
627
|
|
628 /* AAA - ugly */
|
|
629 aim_bstream_setpos(bs, 20);
|
|
630 payloadlength = aimbs_get32(bs);
|
|
631
|
|
632 aim_bstream_setpos(bs, 24);
|
|
633 encoding = aimbs_get16(bs);
|
|
634
|
|
635 aim_bstream_setpos(bs, 30);
|
|
636 flags = aimbs_get16(bs);
|
|
637
|
|
638 aim_bstream_setpos(bs, 36);
|
|
639 /* XXX - create an aimbs_getnullstr function? */
|
|
640 snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */
|
|
641
|
|
642 gaim_debug_misc("oscar", "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr);
|
|
643
|
|
644 if (flags & 0x0008) {
|
13239
|
645 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
646 ret = userfunc(sess, &fr, snptr, 2);
|
|
647 } else if (flags & 0x0004) {
|
13239
|
648 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
649 ret = userfunc(sess, &fr, snptr, 1);
|
|
650 } else {
|
13239
|
651 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING)))
|
13234
|
652 ret = userfunc(sess, &fr, snptr, 0);
|
|
653 }
|
|
654
|
|
655 if ((payloadlength != 0) && (payloadlength != UINT_MAX)) {
|
|
656 char *msg;
|
|
657 int recvd = 0;
|
|
658 int i, isawaymsg;
|
|
659
|
|
660 isawaymsg = flags & 0x0001;
|
|
661
|
|
662 if (!(msg = calloc(1, payloadlength+1))) {
|
|
663 free(snptr);
|
|
664 return -ENOMEM;
|
|
665 }
|
|
666
|
|
667 while (payloadlength - recvd) {
|
|
668 if (payloadlength - recvd >= 1024)
|
|
669 i = aim_recv(conn->fd, &msg[recvd], 1024);
|
13239
|
670 else
|
13234
|
671 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd);
|
|
672 if (i <= 0) {
|
|
673 free(msg);
|
|
674 free(snptr);
|
|
675 return -1;
|
|
676 }
|
|
677 recvd = recvd + i;
|
|
678 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER)))
|
|
679 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength);
|
|
680 }
|
13239
|
681
|
|
682 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMINCOMING)))
|
13234
|
683 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg);
|
|
684
|
|
685 free(msg);
|
|
686 }
|
|
687
|
|
688 free(snptr);
|
|
689
|
|
690 return ret;
|
|
691 }
|
|
692
|
13252
|
693 PeerConnection *
|
13239
|
694 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
|
695 {
|
13252
|
696 PeerConnection *new;
|
13234
|
697
|
|
698 if (!sess)
|
|
699 return NULL;
|
|
700
|
13252
|
701 if (!(new = (PeerConnection *)calloc(1, sizeof(PeerConnection))))
|
13234
|
702 return NULL;
|
|
703
|
|
704 new->sess = sess;
|
|
705 if (cookie)
|
|
706 memcpy(new->cookie, cookie, 8);
|
|
707 else
|
|
708 aim_icbm_makecookie(new->cookie);
|
|
709 if (ip)
|
|
710 new->clientip = strdup(ip);
|
|
711 else
|
|
712 new->clientip = NULL;
|
|
713 if (sn)
|
|
714 new->sn = strdup(sn);
|
|
715 else
|
|
716 new->sn = NULL;
|
|
717 new->method = method;
|
|
718 new->send_or_recv = send_or_recv;
|
|
719 new->stage = stage;
|
|
720 new->port = port;
|
|
721 new->xfer_reffed = FALSE;
|
|
722 new->success = FALSE;
|
|
723 new->fh.totfiles = 1;
|
|
724 new->fh.filesleft = 1;
|
|
725 new->fh.totparts = 1;
|
|
726 new->fh.partsleft = 1;
|
|
727 new->fh.totsize = size;
|
|
728 new->fh.size = size;
|
|
729 new->fh.modtime = modtime;
|
|
730 new->fh.checksum = 0xffff0000;
|
|
731 new->fh.rfrcsum = 0xffff0000;
|
|
732 new->fh.rfcsum = 0xffff0000;
|
|
733 new->fh.recvcsum = 0xffff0000;
|
|
734 strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31);
|
|
735 if (filename) {
|
|
736 strncpy(new->fh.name, filename, 63);
|
|
737 new->fh.name[63] = '\0';
|
|
738 }
|
|
739
|
13252
|
740 sess->peer_connections = g_list_prepend(sess->peer_connections, new);
|
13234
|
741
|
|
742 return new;
|
|
743 }
|
|
744
|
13239
|
745 PeerProxyInfo *aim_rv_proxy_createinfo(OscarSession *sess, const guint8 *cookie,
|
13234
|
746 guint16 port)
|
|
747 {
|
13239
|
748 PeerProxyInfo *proxy_info;
|
|
749
|
|
750 if (!(proxy_info = (PeerProxyInfo*)calloc(1, sizeof(PeerProxyInfo))))
|
13234
|
751 return NULL;
|
13239
|
752
|
13234
|
753 proxy_info->sess = sess;
|
|
754 proxy_info->port = port;
|
|
755 proxy_info->packet_ver = AIM_RV_PROXY_PACKETVER_DFLT;
|
|
756 proxy_info->unknownA = AIM_RV_PROXY_UNKNOWNA_DFLT;
|
13239
|
757
|
13234
|
758 if (cookie)
|
|
759 memcpy(proxy_info->cookie, cookie, 8);
|
13239
|
760
|
13234
|
761 return proxy_info;
|
|
762 }
|
|
763
|
|
764 /**
|
13252
|
765 * Remove the given PeerConnection from the PeerConnection linked list, and
|
13234
|
766 * then free its memory.
|
|
767 *
|
|
768 * @param sess The session.
|
13252
|
769 * @param peer_connection The PeerConnection that we're destroying.
|
13234
|
770 * @return Return 0 if no errors, otherwise return the error number.
|
|
771 */
|
13239
|
772 int
|
13252
|
773 aim_oft_destroyinfo(PeerConnection *peer_connection)
|
13234
|
774 {
|
13239
|
775 OscarSession *sess;
|
13234
|
776
|
13252
|
777 if (!peer_connection || !(sess = peer_connection->sess))
|
13234
|
778 return -EINVAL;
|
|
779
|
13252
|
780 sess->peer_connections = g_list_remove(sess->peer_connections, peer_connection);
|
13234
|
781
|
13252
|
782 free(peer_connection->sn);
|
|
783 free(peer_connection->proxyip);
|
|
784 free(peer_connection->clientip);
|
|
785 free(peer_connection->verifiedip);
|
|
786 free(peer_connection);
|
13234
|
787
|
|
788 return 0;
|
|
789 }
|
|
790
|
|
791 /**
|
|
792 * Creates a listener socket so the other dude can connect to us.
|
|
793 *
|
13239
|
794 * You'll want to set up some kind of watcher on this socket.
|
|
795 * When the state changes, call aim_handlerendconnection with
|
|
796 * the connection returned by this. aim_handlerendconnection
|
13234
|
797 * will accept the pending connection and stop listening.
|
|
798 *
|
|
799 * @param sess The session.
|
13252
|
800 * @param peer_connection File transfer information associated with this
|
13234
|
801 * connection.
|
|
802 * @return Return 0 if no errors, otherwise return the error number.
|
|
803 */
|
13239
|
804 int
|
13252
|
805 aim_sendfile_listen(OscarSession *sess, PeerConnection *peer_connection, int listenfd)
|
13234
|
806 {
|
13252
|
807 if (!peer_connection)
|
13234
|
808 return -EINVAL;
|
|
809
|
13254
|
810 if (!(peer_connection->conn = oscar_connection_new(sess, AIM_CONN_TYPE_LISTENER))) {
|
13234
|
811 close(listenfd);
|
|
812 return -ENOMEM;
|
|
813 }
|
|
814
|
13252
|
815 peer_connection->conn->fd = listenfd;
|
|
816 peer_connection->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE;
|
|
817 peer_connection->conn->lastactivity = time(NULL);
|
13234
|
818
|
|
819 return 0;
|
|
820 }
|
|
821
|
|
822 /**
|
|
823 * Extract an &aim_fileheader_t from the given buffer.
|
|
824 *
|
|
825 * @param bs The should be from an incoming rendezvous packet.
|
|
826 * @return A pointer to new struct on success, or NULL on error.
|
|
827 */
|
13239
|
828 static PeerFrame *
|
|
829 aim_oft_getheader(ByteStream *bs)
|
13234
|
830 {
|
13239
|
831 PeerFrame *fh;
|
13234
|
832
|
13239
|
833 if (!(fh = calloc(1, sizeof(PeerFrame))))
|
13234
|
834 return NULL;
|
|
835
|
|
836 /* The bstream should be positioned after the hdrtype. */
|
|
837 aimbs_getrawbuf(bs, fh->bcookie, 8);
|
|
838 fh->encrypt = aimbs_get16(bs);
|
|
839 fh->compress = aimbs_get16(bs);
|
|
840 fh->totfiles = aimbs_get16(bs);
|
|
841 fh->filesleft = aimbs_get16(bs);
|
|
842 fh->totparts = aimbs_get16(bs);
|
|
843 fh->partsleft = aimbs_get16(bs);
|
|
844 fh->totsize = aimbs_get32(bs);
|
|
845 fh->size = aimbs_get32(bs);
|
|
846 fh->modtime = aimbs_get32(bs);
|
|
847 fh->checksum = aimbs_get32(bs);
|
|
848 fh->rfrcsum = aimbs_get32(bs);
|
|
849 fh->rfsize = aimbs_get32(bs);
|
|
850 fh->cretime = aimbs_get32(bs);
|
|
851 fh->rfcsum = aimbs_get32(bs);
|
|
852 fh->nrecvd = aimbs_get32(bs);
|
|
853 fh->recvcsum = aimbs_get32(bs);
|
|
854 aimbs_getrawbuf(bs, (guchar *)fh->idstring, 32);
|
|
855 fh->flags = aimbs_get8(bs);
|
|
856 fh->lnameoffset = aimbs_get8(bs);
|
|
857 fh->lsizeoffset = aimbs_get8(bs);
|
|
858 aimbs_getrawbuf(bs, (guchar *)fh->dummy, 69);
|
|
859 aimbs_getrawbuf(bs, (guchar *)fh->macfileinfo, 16);
|
|
860 fh->nencode = aimbs_get16(bs);
|
|
861 fh->nlanguage = aimbs_get16(bs);
|
|
862 aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */
|
|
863 fh->name[63] = '\0';
|
|
864
|
|
865 return fh;
|
13239
|
866 }
|
13234
|
867
|
|
868 /**
|
|
869 * Fills a buffer with network-order fh data
|
|
870 *
|
|
871 * @param bs A bstream to fill -- automatically initialized
|
13239
|
872 * @param fh A PeerFrame to get data from.
|
13234
|
873 * @return Return non-zero on error.
|
|
874 */
|
13239
|
875 static int
|
|
876 aim_oft_buildheader(ByteStream *bs, PeerFrame *fh)
|
|
877 {
|
13234
|
878 guint8 *hdr;
|
|
879
|
|
880 if (!bs || !fh)
|
|
881 return -EINVAL;
|
|
882
|
|
883 if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8)))
|
|
884 return -ENOMEM;
|
|
885
|
|
886 aim_bstream_init(bs, hdr, 0x100 - 8);
|
|
887 aimbs_putraw(bs, fh->bcookie, 8);
|
|
888 aimbs_put16(bs, fh->encrypt);
|
|
889 aimbs_put16(bs, fh->compress);
|
|
890 aimbs_put16(bs, fh->totfiles);
|
|
891 aimbs_put16(bs, fh->filesleft);
|
|
892 aimbs_put16(bs, fh->totparts);
|
|
893 aimbs_put16(bs, fh->partsleft);
|
|
894 aimbs_put32(bs, fh->totsize);
|
|
895 aimbs_put32(bs, fh->size);
|
|
896 aimbs_put32(bs, fh->modtime);
|
|
897 aimbs_put32(bs, fh->checksum);
|
|
898 aimbs_put32(bs, fh->rfrcsum);
|
|
899 aimbs_put32(bs, fh->rfsize);
|
|
900 aimbs_put32(bs, fh->cretime);
|
|
901 aimbs_put32(bs, fh->rfcsum);
|
|
902 aimbs_put32(bs, fh->nrecvd);
|
|
903 aimbs_put32(bs, fh->recvcsum);
|
|
904 aimbs_putraw(bs, (guchar *)fh->idstring, 32);
|
|
905 aimbs_put8(bs, fh->flags);
|
|
906 aimbs_put8(bs, fh->lnameoffset);
|
|
907 aimbs_put8(bs, fh->lsizeoffset);
|
|
908 aimbs_putraw(bs, (guchar *)fh->dummy, 69);
|
|
909 aimbs_putraw(bs, (guchar *)fh->macfileinfo, 16);
|
|
910 aimbs_put16(bs, fh->nencode);
|
|
911 aimbs_put16(bs, fh->nlanguage);
|
|
912 aimbs_putraw(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */
|
|
913
|
|
914 return 0;
|
|
915 }
|
|
916
|
|
917 /**
|
|
918 * Create an OFT packet based on the given information, and send it on its merry way.
|
|
919 *
|
|
920 * @param sess The session.
|
|
921 * @param type The subtype of the OFT packet we're sending.
|
13252
|
922 * @param peer_connection The PeerConnection with the connection and OFT
|
13234
|
923 * info we're sending.
|
|
924 * @return Return 0 if no errors, otherwise return the error number.
|
|
925 */
|
13252
|
926 int aim_oft_sendheader(OscarSession *sess, guint16 type, PeerConnection *peer_connection)
|
13234
|
927 {
|
13239
|
928 FlapFrame *fr;
|
13234
|
929
|
13252
|
930 if (!sess || !peer_connection || !peer_connection->conn || (peer_connection->conn->type != AIM_CONN_TYPE_RENDEZVOUS))
|
13234
|
931 return -EINVAL;
|
|
932
|
|
933 #if 0
|
|
934 /*
|
13239
|
935 * If you are receiving a file, the cookie should be null, if you are sending a
|
|
936 * file, the cookie should be the same as the one used in the ICBM negotiation
|
13234
|
937 * SNACs.
|
|
938 */
|
|
939 fh->lnameoffset = 0x1a;
|
|
940 fh->lsizeoffset = 0x10;
|
|
941
|
|
942 /* These should be the same as charset and charsubset in ICBMs */
|
|
943 fh->nencode = 0x0000;
|
|
944 fh->nlanguage = 0x0000;
|
|
945 #endif
|
|
946
|
13252
|
947 aim_oft_dirconvert_tostupid(peer_connection->fh.name);
|
13234
|
948
|
13253
|
949 if (!(fr = flap_frame_new(sess, peer_connection->conn, AIM_FRAMETYPE_OFT, type, 0)))
|
13234
|
950 return -ENOMEM;
|
|
951
|
13252
|
952 if (aim_oft_buildheader(&fr->data, &peer_connection->fh) == -1) {
|
13234
|
953 aim_frame_destroy(fr);
|
|
954 return -ENOMEM;
|
|
955 }
|
|
956
|
|
957 memcpy(fr->hdr.rend.magic, "OFT2", 4);
|
|
958 fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8;
|
|
959
|
|
960 aim_tx_enqueue(sess, fr);
|
|
961
|
|
962 return 0;
|
|
963 }
|
|
964
|
|
965 /**
|
|
966 * Create a rendezvous "init recv" packet and send it on its merry way.
|
|
967 * This is the first packet sent to the proxy server by the second client
|
|
968 * involved in this rendezvous proxy session.
|
|
969 *
|
|
970 * @param sess The session.
|
|
971 * @param proxy_info Changable pieces of data for this packet
|
|
972 * @return Return 0 if no errors, otherwise return the error number.
|
|
973 */
|
13239
|
974 int
|
|
975 aim_rv_proxy_init_recv(PeerProxyInfo *proxy_info)
|
13234
|
976 {
|
|
977 #if 0
|
|
978 aim_tlvlist_t *tlvlist_sendfile;
|
|
979 #endif
|
13239
|
980 ByteStream bs;
|
13234
|
981 guint8 *bs_raw;
|
|
982 guint16 packet_len;
|
|
983 guint8 sn_len;
|
|
984 int err;
|
|
985
|
|
986 err = 0;
|
|
987
|
|
988 if (!proxy_info)
|
|
989 return -EINVAL;
|
|
990
|
|
991 sn_len = strlen(proxy_info->sess->sn);
|
|
992 packet_len = 2 + 2 /* packet_len, packet_ver */
|
|
993 + 2 + 4 /* cmd_type, unknownA */
|
|
994 + 2 /* flags */
|
|
995 + 1 + sn_len /* Length/value pair for screenname */
|
|
996 + 8 /* ICBM Cookie */
|
|
997 + 2 /* port */
|
|
998 + 2 + 2 + 16; /* TLV for Filesend capability block */
|
|
999
|
|
1000 if (!(bs_raw = malloc(packet_len)))
|
|
1001 return -ENOMEM;
|
|
1002
|
|
1003 aim_bstream_init(&bs, bs_raw, packet_len);
|
|
1004 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */
|
|
1005 aimbs_put16(&bs, proxy_info->packet_ver);
|
|
1006 aimbs_put16(&bs, AIM_RV_PROXY_INIT_RECV);
|
|
1007 aimbs_put32(&bs, proxy_info->unknownA);
|
|
1008 aimbs_put16(&bs, proxy_info->flags);
|
|
1009 aimbs_put8(&bs, sn_len);
|
|
1010 aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len);
|
|
1011 aimbs_put16(&bs, proxy_info->port);
|
|
1012 aimbs_putraw(&bs, proxy_info->cookie, 8);
|
|
1013
|
|
1014 aimbs_put16(&bs, 0x0001); /* Type */
|
|
1015 aimbs_put16(&bs, 16); /* Length */
|
|
1016 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */
|
|
1017
|
|
1018
|
|
1019 #if 0
|
|
1020 /* TODO: Use built-in TLV */
|
|
1021 aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE);
|
|
1022 aim_tlvlist_write(&bs, &tlvlist_sendfile);
|
|
1023 #endif
|
|
1024
|
|
1025 aim_bstream_rewind(&bs);
|
|
1026 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len)
|
|
1027 err = errno;
|
|
1028 proxy_info->conn->lastactivity = time(NULL);
|
|
1029
|
|
1030 #if 0
|
|
1031 aim_tlvlist_free(tlvlist_sendfile);
|
|
1032 #endif
|
|
1033 free(bs_raw);
|
|
1034
|
|
1035 return err;
|
|
1036 }
|
|
1037
|
|
1038
|
|
1039 /**
|
|
1040 * Create a rendezvous "init send" packet and send it on its merry way.
|
|
1041 * This is the first packet sent to the proxy server by the client
|
|
1042 * first indicating that this will be a proxied connection
|
|
1043 *
|
|
1044 * @param sess The session.
|
|
1045 * @param proxy_info Changable pieces of data for this packet
|
|
1046 * @return Return 0 if no errors, otherwise return the error number.
|
|
1047 */
|
13239
|
1048 int
|
|
1049 aim_rv_proxy_init_send(PeerProxyInfo *proxy_info)
|
13234
|
1050 {
|
|
1051 #if 0
|
|
1052 aim_tlvlist_t *tlvlist_sendfile;
|
|
1053 #endif
|
13239
|
1054 ByteStream bs;
|
13234
|
1055 guint8 *bs_raw;
|
|
1056 guint16 packet_len;
|
|
1057 guint8 sn_len;
|
|
1058 int err;
|
|
1059
|
|
1060 err = 0;
|
|
1061
|
|
1062 if (!proxy_info)
|
|
1063 return -EINVAL;
|
|
1064
|
|
1065 sn_len = strlen(proxy_info->sess->sn);
|
|
1066 packet_len = 2 + 2 /* packet_len, packet_ver */
|
|
1067 + 2 + 4 /* cmd_type, unknownA */
|
|
1068 + 2 /* flags */
|
|
1069 + 1 + sn_len /* Length/value pair for screenname */
|
|
1070 + 8 /* ICBM Cookie */
|
|
1071 + 2 + 2 + 16; /* TLV for Filesend capability block */
|
|
1072
|
|
1073 if (!(bs_raw = malloc(packet_len)))
|
|
1074 return -ENOMEM;
|
|
1075
|
|
1076 aim_bstream_init(&bs, bs_raw, packet_len);
|
|
1077 aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */
|
|
1078 aimbs_put16(&bs, proxy_info->packet_ver);
|
|
1079 aimbs_put16(&bs, AIM_RV_PROXY_INIT_SEND);
|
|
1080 aimbs_put32(&bs, proxy_info->unknownA);
|
|
1081 aimbs_put16(&bs, proxy_info->flags);
|
|
1082 aimbs_put8(&bs, sn_len);
|
|
1083 aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len);
|
|
1084 aimbs_putraw(&bs, proxy_info->cookie, 8);
|
|
1085
|
|
1086 aimbs_put16(&bs, 0x0001); /* Type */
|
|
1087 aimbs_put16(&bs, 16); /* Length */
|
|
1088 aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */
|
|
1089
|
|
1090 /* TODO: Use built-in TLV */
|
|
1091 #if 0
|
|
1092 aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE);
|
|
1093 aim_tlvlist_write(&bs, &tlvlist_sendfile);
|
|
1094 #endif
|
|
1095
|
|
1096 aim_bstream_rewind(&bs);
|
|
1097 if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len)
|
|
1098 err = errno;
|
|
1099 proxy_info->conn->lastactivity = time(NULL);
|
|
1100
|
|
1101 #if 0
|
|
1102 aim_tlvlist_free(tlvlist_sendfile);
|
|
1103 #endif
|
|
1104 free(bs_raw);
|
|
1105
|
|
1106 return err;
|
|
1107 }
|
|
1108
|
|
1109 /**
|
|
1110 * Handle incoming data on a rendezvous connection. This is analogous to the
|
|
1111 * consumesnac function in rxhandlers.c, and I really think this should probably
|
|
1112 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet.
|
|
1113 *
|
|
1114 * @param sess The session.
|
|
1115 * @param fr The frame allocated for the incoming data.
|
|
1116 * @return Return 0 if the packet was handled correctly, otherwise return the
|
|
1117 * error number.
|
|
1118 */
|
13239
|
1119 int
|
|
1120 aim_rxdispatch_rendezvous(OscarSession *sess, FlapFrame *fr)
|
13234
|
1121 {
|
13239
|
1122 OscarConnection *conn = fr->conn;
|
13234
|
1123 int ret = 1;
|
|
1124
|
|
1125 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) {
|
|
1126 if (fr->hdr.rend.type == 0x0001)
|
|
1127 ret = handlehdr_odc(sess, conn, fr, &fr->data);
|
|
1128 else
|
|
1129 gaim_debug_info("oscar", "ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type);
|
|
1130
|
|
1131 } else {
|
|
1132 aim_rxcallback_t userfunc;
|
13239
|
1133 PeerFrame *header = aim_oft_getheader(&fr->data);
|
13234
|
1134 aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */
|
|
1135
|
|
1136 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type)))
|
|
1137 ret = userfunc(sess, fr, conn, header->bcookie, header);
|
|
1138
|
|
1139 free(header);
|
|
1140 }
|
|
1141
|
|
1142 if (ret == -1)
|
13254
|
1143 aim_conn_close(sess, conn);
|
13234
|
1144
|
|
1145 return ret;
|
|
1146 }
|
|
1147
|
|
1148 /**
|
|
1149 * Handle incoming data on a rendezvous proxy connection. This is similar to
|
|
1150 * aim_rxdispatch_rendezvous above and should probably be kept with that function.
|
|
1151 *
|
|
1152 * @param sess The session.
|
|
1153 * @param fr The frame allocated for the incoming data.
|
13239
|
1154 * @return Return 0 if the packet was handled correctly, otherwise return the
|
13234
|
1155 * error number.
|
|
1156 */
|
13239
|
1157 PeerProxyInfo *
|
|
1158 aim_rv_proxy_read(OscarSession *sess, OscarConnection *conn)
|
13234
|
1159 {
|
13239
|
1160 ByteStream bs_hdr;
|
13234
|
1161 guint8 hdr_buf[AIM_RV_PROXY_HDR_LEN];
|
13239
|
1162 ByteStream bs_body; /* The body (everything but the header) of the packet */
|
13234
|
1163 guint8 *body_buf = NULL;
|
|
1164 guint8 body_len;
|
13239
|
1165
|
13234
|
1166 char str_ip[30] = {""};
|
|
1167 guint8 ip_temp[4];
|
13239
|
1168
|
13234
|
1169 guint16 len;
|
13239
|
1170 PeerProxyInfo *proxy_info;
|
|
1171
|
|
1172 if(!(proxy_info = malloc(sizeof(PeerProxyInfo))))
|
13234
|
1173 return NULL;
|
|
1174
|
|
1175 aim_bstream_init(&bs_hdr, hdr_buf, AIM_RV_PROXY_HDR_LEN);
|
|
1176 if (aim_bstream_recv(&bs_hdr, conn->fd, AIM_RV_PROXY_HDR_LEN) == AIM_RV_PROXY_HDR_LEN) {
|
|
1177 aim_bstream_rewind(&bs_hdr);
|
|
1178 len = aimbs_get16(&bs_hdr);
|
|
1179 proxy_info->packet_ver = aimbs_get16(&bs_hdr);
|
|
1180 proxy_info->cmd_type = aimbs_get16(&bs_hdr);
|
|
1181 proxy_info->unknownA = aimbs_get32(&bs_hdr);
|
|
1182 proxy_info->flags = aimbs_get16(&bs_hdr);
|
|
1183 if(proxy_info->cmd_type == AIM_RV_PROXY_READY) {
|
|
1184 /* Do a little victory dance
|
|
1185 * A ready packet contains no additional information */
|
|
1186 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ERROR) {
|
|
1187 if(len == AIM_RV_PROXY_ERROR_LEN - 2) {
|
|
1188 body_len = AIM_RV_PROXY_ERROR_LEN - AIM_RV_PROXY_HDR_LEN;
|
|
1189 body_buf = malloc(body_len);
|
|
1190 aim_bstream_init(&bs_body, body_buf, body_len);
|
|
1191 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) {
|
|
1192 aim_bstream_rewind(&bs_body);
|
|
1193 proxy_info->err_code = aimbs_get16(&bs_body);
|
|
1194 } else {
|
|
1195 gaim_debug_warning("oscar","error reading rv proxy error packet\n");
|
13254
|
1196 aim_conn_close(sess, conn);
|
13234
|
1197 free(proxy_info);
|
|
1198 proxy_info = NULL;
|
|
1199 }
|
|
1200 } else {
|
|
1201 gaim_debug_warning("oscar","invalid length for proxy error packet\n");
|
|
1202 free(proxy_info);
|
|
1203 proxy_info = NULL;
|
|
1204 }
|
|
1205 } else if(proxy_info->cmd_type == AIM_RV_PROXY_ACK) {
|
|
1206 if(len == AIM_RV_PROXY_ACK_LEN - 2) {
|
|
1207 body_len = AIM_RV_PROXY_ACK_LEN - AIM_RV_PROXY_HDR_LEN;
|
|
1208 body_buf = malloc(body_len);
|
|
1209 aim_bstream_init(&bs_body, body_buf, body_len);
|
|
1210 if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) {
|
|
1211 int i;
|
|
1212 aim_bstream_rewind(&bs_body);
|
|
1213 proxy_info->port = aimbs_get16(&bs_body);
|
|
1214 for(i=0; i<4; i++)
|
|
1215 ip_temp[i] = aimbs_get8(&bs_body);
|
|
1216 snprintf(str_ip, sizeof(str_ip), "%hhu.%hhu.%hhu.%hhu",
|
|
1217 ip_temp[0], ip_temp[1],
|
|
1218 ip_temp[2], ip_temp[3]);
|
|
1219 proxy_info->ip = strdup(str_ip);
|
|
1220 } else {
|
|
1221 gaim_debug_warning("oscar","error reading rv proxy error packet\n");
|
13254
|
1222 aim_conn_close(sess, conn);
|
13234
|
1223 free(proxy_info);
|
|
1224 proxy_info = NULL;
|
|
1225 }
|
|
1226 } else {
|
|
1227 gaim_debug_warning("oscar","invalid length for proxy error packet\n");
|
|
1228 free(proxy_info);
|
|
1229 proxy_info = NULL;
|
|
1230 }
|
|
1231 } else {
|
|
1232 gaim_debug_warning("oscar","unknown type for aim rendezvous proxy packet\n");
|
13239
|
1233 }
|
13234
|
1234 } else {
|
|
1235 gaim_debug_warning("oscar","error reading header of rv proxy packet\n");
|
13254
|
1236 aim_conn_close(sess, conn);
|
13234
|
1237 free(proxy_info);
|
|
1238 proxy_info = NULL;
|
|
1239 }
|
|
1240 if(body_buf) {
|
|
1241 free(body_buf);
|
|
1242 body_buf = NULL;
|
|
1243 }
|
|
1244 return proxy_info;
|
|
1245 }
|