]> andersk Git - libfaim.git/blame - src/ft.c
- Sat Sep 8 21:26:27 PDT 2001
[libfaim.git] / src / ft.c
CommitLineData
37ee990e 1/*
646c6b52 2 * File transfer (OFT) and DirectIM (ODC).
3 * (OSCAR File Transfer, Oscar Direct Connect(ion?)
37ee990e 4 */
5
37ee990e 6#define FAIM_INTERNAL
dd60ff8b 7#include <aim.h>
7392c79f 8
646c6b52 9
5ac21963 10#ifndef _WIN32
78b3fb13 11#include <netdb.h>
12#include <sys/socket.h>
13#include <netinet/in.h>
7392c79f 14#include <sys/utsname.h> /* for aim_directim_initiate */
37ee990e 15
7392c79f 16#include <arpa/inet.h> /* for inet_ntoa */
37ee990e 17
5ac21963 18#endif
7392c79f 19
37ee990e 20/* TODO:
37ee990e 21 o look for memory leaks.. there's going to be shitloads, i'm sure.
7392c79f 22*/
23
59e1da90 24struct aim_directim_intdata {
25 fu8_t cookie[8];
26 char sn[MAXSNLEN+1];
27 char ip[22];
28};
29
c5f5b7f1 30static int listenestablish(fu16_t portnum);
37ee990e 31static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr);
32
33/**
59e1da90 34 * aim_handlerendconnect - call this to accept OFT connections and set up the required structures
37ee990e 35 * @sess: the session
36 * @cur: the conn the incoming connection is on
37 *
38 * call this when you get an outstanding read on a conn with subtype
646c6b52 39 * AIM_CONN_SUBTYPE_RENDEZVOUS_OUT, it will clone the current
40 * &aim_conn_t and tweak things as appropriate. the new conn and the
41 * listener conn are both returned to the client in the
42 * %AIM_CB_FAM_OFT, %AIM_CB_OFT_<CLASS>INITIATE callback.
37ee990e 43 */
c5f5b7f1 44faim_export int aim_handlerendconnect(aim_session_t *sess, aim_conn_t *cur)
37ee990e 45{
c5f5b7f1 46 int acceptfd = 0;
47 struct sockaddr cliaddr;
48 int clilen = sizeof(cliaddr);
49 int ret = 0;
50 aim_conn_t *newconn;
7392c79f 51
c5f5b7f1 52 if ((acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1)
53 return 0; /* not an error */
646c6b52 54
c5f5b7f1 55 if (cliaddr.sa_family != AF_INET) { /* just in case IPv6 really is happening */
56 close(acceptfd);
57 aim_conn_close(cur);
58 return -1;
59 }
646c6b52 60
c5f5b7f1 61 if (!(newconn = aim_cloneconn(sess, cur))) {
62 close(acceptfd);
63 aim_conn_close(cur);
64 return -1;
65 }
37ee990e 66
c5f5b7f1 67 newconn->type = AIM_CONN_TYPE_RENDEZVOUS;
68 newconn->fd = acceptfd;
7392c79f 69
c5f5b7f1 70 if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) {
71 struct aim_directim_intdata *priv;
72 aim_rxcallback_t userfunc;
7392c79f 73
c5f5b7f1 74 priv = (struct aim_directim_intdata *)(newconn->internal = cur->internal);
75 cur->internal = NULL;
646c6b52 76
c5f5b7f1 77 snprintf(priv->ip, sizeof(priv->ip), "%s:%u",
78 inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr),
79 ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
646c6b52 80
c5f5b7f1 81 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE)))
82 ret = userfunc(sess, NULL, newconn, cur);
7392c79f 83
c5f5b7f1 84 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) {
85#if 0
86 struct aim_filetransfer_priv *priv;
87 aim_rxcallback_t userfunc;
646c6b52 88
7392c79f 89
c5f5b7f1 90 newconn->priv = cur->priv;
91 cur->priv = NULL;
92 priv = (struct aim_filetransfer_priv *)newconn->priv;
7392c79f 93
c5f5b7f1 94 snprintf(priv->ip, sizeof(priv->ip), "%s:%u", inet_ntoa(((struct sockaddr_in *)&cliaddr)->sin_addr), ntohs(((struct sockaddr_in *)&cliaddr)->sin_port));
7392c79f 95
c5f5b7f1 96 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE)))
97 ret = userfunc(sess, NULL, newconn, cur);
98#endif
99 } else {
100 faimdprintf(sess, 1,"Got a Connection on a listener that's not Rendezvous(??!) Closing conn.\n");
101 aim_conn_close(newconn);
102 ret = -1;
103 }
37ee990e 104
c5f5b7f1 105 return ret;
7392c79f 106}
d410cf58 107
646c6b52 108/**
37ee990e 109 * aim_send_im_direct - send IM client-to-client over established connection
110 * @sess: session to conn
111 * @conn: directim connection
112 * @msg: null-terminated string to send; if this is NULL, it will send a "typing" notice.
113 *
646c6b52 114 * Call this just like you would aim_send_im, to send a directim. You
115 * _must_ have previously established the directim connection.
7392c79f 116 */
d410cf58 117faim_export int aim_send_im_direct(aim_session_t *sess, aim_conn_t *conn, const char *msg)
646c6b52 118{
c5f5b7f1 119 struct aim_directim_intdata *intdata = (struct aim_directim_intdata *)conn->internal;
120 aim_frame_t *fr;
121 aim_bstream_t hdrbs; /* XXX this should be within aim_frame_t */
122
123 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS))
124 return -EINVAL;
125
126 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, strlen(msg))))
127 return -ENOMEM;
128
129 memcpy(fr->hdr.oft.magic, "ODC2", 4);
130
131 fr->hdr.oft.hdr2len = 0x44;
132
133 if (!(fr->hdr.oft.hdr2 = calloc(1, fr->hdr.oft.hdr2len))) {
134 aim_frame_destroy(fr);
135 return -ENOMEM;
136 }
137
138 aim_bstream_init(&hdrbs, fr->hdr.oft.hdr2, fr->hdr.oft.hdr2len);
139
140 aimbs_put16(&hdrbs, 0x0006);
141 aimbs_put16(&hdrbs, 0x0000);
142 aimbs_putraw(&hdrbs, intdata->cookie, 8);
143 aimbs_put16(&hdrbs, 0x0000);
144 aimbs_put16(&hdrbs, 0x0000);
145 aimbs_put16(&hdrbs, 0x0000);
146 aimbs_put16(&hdrbs, 0x0000);
147 aimbs_put32(&hdrbs, strlen(msg));
148 aimbs_put16(&hdrbs, 0x0000);
149 aimbs_put16(&hdrbs, 0x0000);
150 aimbs_put16(&hdrbs, 0x0000);
151
152 /* flags -- 0x000e for "typing", 0x0000 for message */
153 aimbs_put16(&hdrbs, msg ? 0x0000 : 0x000e);
154
155 aimbs_put16(&hdrbs, 0x0000);
156 aimbs_put16(&hdrbs, 0x0000);
157 aimbs_putraw(&hdrbs, sess->sn, strlen(sess->sn));
158
159 aim_bstream_setpos(&hdrbs, 52); /* bleeehh */
160
161 aimbs_put8(&hdrbs, 0x00);
162 aimbs_put16(&hdrbs, 0x0000);
163 aimbs_put16(&hdrbs, 0x0000);
164 aimbs_put16(&hdrbs, 0x0000);
165 aimbs_put16(&hdrbs, 0x0000);
166 aimbs_put16(&hdrbs, 0x0000);
167 aimbs_put16(&hdrbs, 0x0000);
168 aimbs_put16(&hdrbs, 0x0000);
169
170 /* end of hdr2 */
171
172 if (msg) {
173#if 0 /* XXX this is how you send buddy icon info... */
174 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0008);
175 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000c);
176 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000);
177 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x1466);
178 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0001);
179 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x2e0f);
180 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x393e);
181 i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0xcac8);
182#endif
183 aimbs_putraw(&fr->data, msg, strlen(msg));
184 }
7392c79f 185
c5f5b7f1 186 aim_tx_enqueue(sess, fr);
d410cf58 187
c5f5b7f1 188 return 0;
37ee990e 189}
7392c79f 190
646c6b52 191/* XXX: give the client author the responsibility of setting up a
192 * listener, then we no longer have a libfaim problem with broken
193 * solaris *innocent smile* -jbm */
194
c5f5b7f1 195static int getlocalip(fu8_t *ip)
196{
197 struct hostent *hptr;
198 char localhost[129];
199
200 /* XXX if available, use getaddrinfo() */
201 /* XXX allow client to specify which IP to use for multihomed boxes */
202
203 if (gethostname(localhost, 128) < 0)
204 return -1;
205
206 if (!(hptr = gethostbyname(localhost)))
207 return -1;
208
209 memcpy(ip, hptr->h_addr_list[0], 4);
210
211 return 0;
212}
213
214/* XXX this should probably go in im.c */
215static int aim_request_directim(aim_session_t *sess, aim_conn_t *conn, const char *destsn, fu8_t *ip, fu16_t port, fu8_t *ckret)
216{
217 fu8_t ck[8];
218 aim_frame_t *fr;
219 aim_snacid_t snacid;
220 aim_tlvlist_t *tl = NULL, *itl = NULL;
221 int hdrlen, i;
222 fu8_t *hdr;
223 aim_bstream_t hdrbs;
224
225 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x02, 256+strlen(destsn))))
226 return -ENOMEM;
227
228 snacid = aim_cachesnac(sess, 0x0004, 0x0006, 0x0000, NULL, 0);
229 aim_putsnac(&fr->data, 0x0004, 0x0006, 0x0000, snacid);
230
231 /*
232 * Generate a random message cookie
233 *
234 * This cookie needs to be alphanumeric and NULL-terminated to be
235 * TOC-compatible.
236 *
237 * XXX have I mentioned these should be generated in msgcookie.c?
238 *
239 */
240 for (i = 0; i < 7; i++)
241 ck[i] = 0x30 + ((fu8_t) rand() % 10);
242 ck[7] = '\0';
243
244 if (ckret)
245 memcpy(ckret, ck, 8);
246
247 /* Cookie */
248 aimbs_putraw(&fr->data, ck, 8);
249
250 /* Channel */
251 aimbs_put16(&fr->data, 0x0002);
252
253 /* Destination SN */
254 aimbs_put8(&fr->data, strlen(destsn));
255 aimbs_putraw(&fr->data, destsn, strlen(destsn));
256
257 aim_addtlvtochain_noval(&tl, 0x0003);
258
259 hdrlen = 2+8+16+6+8+6+4;
260 hdr = malloc(hdrlen);
261 aim_bstream_init(&hdrbs, hdr, hdrlen);
262
263 aimbs_put16(&hdrbs, 0x0000);
264 aimbs_putraw(&hdrbs, ck, 8);
265 aim_putcap(&hdrbs, AIM_CAPS_IMIMAGE);
266
267 aim_addtlvtochain16(&itl, 0x000a, 0x0001);
268 aim_addtlvtochain_raw(&itl, 0x0003, 4, ip);
269 aim_addtlvtochain16(&itl, 0x0005, port);
270 aim_addtlvtochain_noval(&itl, 0x000f);
271
272 aim_writetlvchain(&hdrbs, &itl);
273
274 aim_addtlvtochain_raw(&tl, 0x0005, aim_bstream_curpos(&hdrbs), hdr);
275
276 aim_writetlvchain(&fr->data, &tl);
277
278 free(hdr);
279 aim_freetlvchain(&itl);
280 aim_freetlvchain(&tl);
281
282 aim_tx_enqueue(sess, fr);
283
284 return 0;
285}
286
646c6b52 287/**
37ee990e 288 * aim_directim_intitiate - For those times when we want to open up the directim channel ourselves.
289 * @sess: your session,
290 * @conn: the BOS conn,
646c6b52 291 * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a %NULL, we alloc one)
37ee990e 292 * @destsn: the SN to connect to.
293 *
7392c79f 294 */
c5f5b7f1 295faim_export aim_conn_t *aim_directim_initiate(aim_session_t *sess, aim_conn_t *conn, const char *destsn)
37ee990e 296{
c5f5b7f1 297 aim_conn_t *newconn;
298 aim_msgcookie_t *cookie;
299 struct aim_directim_intdata *priv;
300 int listenfd;
301 fu16_t port = 4443;
302 fu8_t localip[4];
303 fu8_t ck[8];
37ee990e 304
c5f5b7f1 305 if (getlocalip(localip) == -1)
306 return NULL;
7392c79f 307
c5f5b7f1 308 if ((listenfd = listenestablish(port)) == -1)
309 return NULL;
37ee990e 310
c5f5b7f1 311 aim_request_directim(sess, conn, destsn, localip, port, ck);
7392c79f 312
c5f5b7f1 313 cookie = (aim_msgcookie_t *)calloc(1, sizeof(aim_msgcookie_t));
314 memcpy(cookie->cookie, ck, 8);
315 cookie->type = AIM_COOKIETYPE_OFTIM;
7392c79f 316
c5f5b7f1 317 priv = (struct aim_directim_intdata *)calloc(1, sizeof(struct aim_directim_intdata));
7392c79f 318
c5f5b7f1 319 memcpy(priv->cookie, ck, 8);
320 memcpy(priv->sn, destsn, sizeof(priv->sn));
321 cookie->data = priv;
322 aim_cachecookie(sess, cookie);
7392c79f 323
c5f5b7f1 324 /* XXX switch to aim_cloneconn()? */
325 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL))) {
326 close(listenfd);
327 return NULL;
328 }
7392c79f 329
c5f5b7f1 330 /* this one is for the conn */
331 priv = (struct aim_directim_intdata *)calloc(1, sizeof(struct aim_directim_intdata));
7392c79f 332
c5f5b7f1 333 memcpy(priv->cookie, ck, 8);
334 memcpy(priv->sn, destsn, sizeof(priv->sn));
7392c79f 335
c5f5b7f1 336 newconn->fd = listenfd;
337 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
338 newconn->internal = priv;
339 newconn->lastactivity = time(NULL);
7392c79f 340
c5f5b7f1 341 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
7392c79f 342
c5f5b7f1 343 return newconn;
7392c79f 344}
345
d410cf58 346#if 0
646c6b52 347/**
348 * unsigned int aim_oft_listener_clean - close up old listeners
37ee990e 349 * @sess: session to clean up in
350 * @age: maximum age in seconds
351 *
352 * returns number closed, -1 on error.
871e2fd0 353 */
37ee990e 354faim_export unsigned int aim_oft_listener_clean(struct aim_session_t *sess, time_t age)
355{
356 struct aim_conn_t *cur;
357 time_t now;
358 unsigned int hit = 0;
359
646c6b52 360 if (!sess)
37ee990e 361 return -1;
362 now = time(NULL);
363 faim_mutex_lock(&sess->connlistlock);
364 for(cur = sess->connlist;cur; cur = cur->next)
646c6b52 365 if (cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
37ee990e 366 faim_mutex_lock(&cur->active);
367 if (cur->lastactivity < (now - age) ) {
368 faim_mutex_unlock(&cur->active);
369 aim_conn_close(cur);
370 hit++;
371 } else
372 faim_mutex_unlock(&cur->active);
373 }
374 faim_mutex_unlock(&sess->connlistlock);
375 return hit;
376}
d410cf58 377#endif
871e2fd0 378
c5f5b7f1 379faim_export const char *aim_directim_getsn(aim_conn_t *conn)
380{
381 struct aim_directim_intdata *intdata;
382
383 if (!conn)
384 return NULL;
385
386 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) ||
387 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM))
388 return NULL;
389
390 if (!conn->internal)
391 return NULL;
392
393 intdata = (struct aim_directim_intdata *)conn->internal;
394
395 return intdata->sn;
396}
397
646c6b52 398/**
37ee990e 399 * aim_directim_connect - connect to buddy for directim
400 * @sess: the session to append the conn to,
c5f5b7f1 401 * @sn: the SN we're connecting to
402 * @addr: address to connect to
403 *
404 * This is a wrapper for aim_newconn.
405 *
406 * If addr is NULL, the socket is not created, but the connection is
407 * allocated and setup to connect.
37ee990e 408 *
37ee990e 409 */
c5f5b7f1 410faim_export aim_conn_t *aim_directim_connect(aim_session_t *sess, const char *sn, const char *addr, const fu8_t *cookie)
37ee990e 411{
c5f5b7f1 412 aim_conn_t *newconn;
413 struct aim_directim_intdata *intdata;
7392c79f 414
c5f5b7f1 415 if (!sess || !sn)
416 return NULL;
37ee990e 417
c5f5b7f1 418 if (!(intdata = malloc(sizeof(struct aim_directim_intdata))))
419 return NULL;
420 memset(intdata, 0, sizeof(struct aim_directim_intdata));
37ee990e 421
c5f5b7f1 422 memcpy(intdata->cookie, cookie, 8);
423 strncpy(intdata->sn, sn, sizeof(intdata->sn));
e8634a4b 424 if (addr)
425 strncpy(intdata->ip, addr, sizeof(intdata->ip));
c5f5b7f1 426
427 /* XXX verify that non-blocking connects actually work */
428 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, addr))) {
429 free(intdata);
430 return NULL;
431 }
432
433 if (!newconn || (newconn->fd == -1)) {
434 free(intdata);
435 return newconn;
436 }
437
438 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM;
439 newconn->internal = intdata;
440
441 return newconn;
37ee990e 442}
7392c79f 443
646c6b52 444/**
37ee990e 445 * aim_directim_getconn - find a directim conn for buddy name
446 * @sess: your session,
447 * @name: the name to get,
448 *
449 * returns conn for directim with name, %NULL if none found.
450 *
871e2fd0 451 */
c5f5b7f1 452faim_export aim_conn_t *aim_directim_getconn(aim_session_t *sess, const char *name)
871e2fd0 453{
c5f5b7f1 454 aim_conn_t *cur;
37ee990e 455
c5f5b7f1 456 if (!sess || !name || !strlen(name))
457 return NULL;
37ee990e 458
c5f5b7f1 459 for (cur = sess->connlist; cur; cur = cur->next) {
460 struct aim_directim_intdata *intdata;
461
462 if ((cur->type != AIM_CONN_TYPE_RENDEZVOUS) || (cur->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM))
463 continue;
37ee990e 464
c5f5b7f1 465 intdata = cur->internal;
466
467 if (aim_sncmp(intdata->sn, name) == 0)
468 break;
469 }
470
471 return cur;
37ee990e 472}
871e2fd0 473
646c6b52 474/**
37ee990e 475 * aim_accepttransfer - accept a file transfer request
476 * @sess: the session,
477 * @conn: the BOS conn for the CAP reply
478 * @sn: the screenname to send it to,
479 * @cookie: the cookie used
480 * @ip: the ip to connect to
481 * @listingfiles: number of files to share
482 * @listingtotsize: total size of shared files
483 * @listingsize: length of the listing file(buffer)
484 * @listingchecksum: checksum of the listing
485 * @rendid: capability type (%AIM_CAPS_GETFILE or %AIM_CAPS_SENDFILE)
871e2fd0 486 *
646c6b52 487 * Returns new connection or %NULL on error.
d410cf58 488 *
489 * XXX this should take a struct.
871e2fd0 490 */
d410cf58 491faim_export aim_conn_t *aim_accepttransfer(aim_session_t *sess,
492 aim_conn_t *conn,
493 const char *sn, const fu8_t *cookie,
494 const fu8_t *ip,
495 fu16_t listingfiles,
496 fu16_t listingtotsize,
497 fu16_t listingsize,
498 fu32_t listingchecksum,
499 fu16_t rendid)
500{
501 return NULL;
502#if 0
7392c79f 503 struct command_tx_struct *newpacket, *newoft;
871e2fd0 504 struct aim_conn_t *newconn;
37ee990e 505 struct aim_fileheader_t *fh;
871e2fd0 506 struct aim_filetransfer_priv *priv;
507 struct aim_msgcookie_t *cachedcook;
7392c79f 508 int curbyte, i;
7392c79f 509
646c6b52 510 if (!sess || !conn || !sn || !cookie || !ip) {
37ee990e 511 return NULL;
646c6b52 512 }
871e2fd0 513
646c6b52 514 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip);
37ee990e 515
646c6b52 516 if (!newconn || (newconn->fd == -1)) {
517 perror("aim_newconn");
518 faimdprintf(sess, 2, "could not connect to %s (fd: %i)\n", ip, newconn?newconn->fd:0);
519 return newconn;
520 } else {
521 priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv));
522
523 memcpy(priv->cookie, cookie, 8);
524 priv->state = 0;
525 strncpy(priv->sn, sn, MAXSNLEN);
526 strncpy(priv->ip, ip, sizeof(priv->ip));
527 newconn->priv = (void *)priv;
528
529 faimdprintf(sess, 2, "faim: connected to peer (fd = %d)\n", newconn->fd);
530 }
531
532 if (rendid == AIM_CAPS_GETFILE) {
533 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
7392c79f 534
646c6b52 535 faimdprintf(sess, 2, "faim: getfile request accept\n");
37ee990e 536
646c6b52 537 if (!(newoft = aim_tx_new(sess, newconn, AIM_FRAMETYPE_OFT, 0x1108, 0))) {
538 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
539 /* XXX: conn leak here */
871e2fd0 540 return NULL;
37ee990e 541 }
542
871e2fd0 543 newoft->lock = 1;
871e2fd0 544 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
37ee990e 545 newoft->hdr.oft.hdr2len = 0x100 - 8;
7392c79f 546
646c6b52 547 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) {
548 /* XXX: conn leak here */
549 perror("calloc");
871e2fd0 550 return NULL;
646c6b52 551 }
7392c79f 552
37ee990e 553 fh->encrypt = 0x0000;
554 fh->compress = 0x0000;
555 fh->totfiles = listingfiles;
556 fh->filesleft = listingfiles; /* is this right -- total parts and parts left?*/
557 fh->totparts = 0x0001;
558 fh->partsleft = 0x0001;
559 fh->totsize = listingtotsize;
560 fh->size = listingsize; /* ls -l listing.txt */
561 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
562 fh->checksum = listingchecksum;
563 fh->rfcsum = 0x00000000;
564 fh->rfsize = 0x00000000;
565 fh->cretime = 0x00000000;
566 fh->rfcsum = 0x00000000;
567 fh->nrecvd = 0x00000000;
568 fh->recvcsum = 0x00000000;
569 memset(fh->idstring, 0, sizeof(fh->idstring));
570 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
571 fh->flags = 0x02;
572 fh->lnameoffset = 0x1a;
573 fh->lsizeoffset = 0x10;
574 memset(fh->dummy, 0, sizeof(fh->dummy));
575 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
576
577 /* we need to figure out these encodings for filenames */
578 fh->nencode = 0x0000;
579 fh->nlanguage = 0x0000;
580 memset(fh->name, 0, sizeof(fh->name));
581 memcpy(fh->name, "listing.txt", sizeof(fh->name));
582
583 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
584 newoft->lock = 0;
d410cf58 585 aim_frame_destroy(newoft);
646c6b52 586 /* XXX: conn leak */
587 perror("calloc (1)");
37ee990e 588 return NULL;
589 }
7392c79f 590
37ee990e 591 memcpy(fh->bcookie, cookie, 8);
7392c79f 592
646c6b52 593 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh)))
594 faimdprintf(sess, 1, "eek, bh fail!\n");
871e2fd0 595
596 newoft->lock = 0;
597 aim_tx_enqueue(sess, newoft);
37ee990e 598
646c6b52 599 if (!(cachedcook = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) {
600 faimdprintf(sess, 1, "faim: accepttransfer: couldn't calloc cachedcook. yeep!\n");
601 /* XXX: more cleanup, conn leak */
602 perror("calloc (2)");
37ee990e 603 return NULL;
604 }
7392c79f 605
37ee990e 606 memcpy(&(priv->fh), fh, sizeof(struct aim_fileheader_t));
607 memcpy(cachedcook->cookie, cookie, 8);
608
609 cachedcook->type = AIM_COOKIETYPE_OFTGET;
610 cachedcook->data = (void *)priv;
7392c79f 611
646c6b52 612 if (aim_cachecookie(sess, cachedcook) == -1)
613 faimdprintf(sess, 1, "faim: ERROR caching message cookie\n");
37ee990e 614
646c6b52 615 free(fh);
37ee990e 616
646c6b52 617 /* OSCAR CAP accept packet */
618
619 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(sn)+4+2+8+16))) {
620 return NULL;
621 }
622 } else {
623 return NULL;
624 }
625
626 newpacket->lock = 1;
627 curbyte = aim_putsnac(newpacket->data, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
37ee990e 628
646c6b52 629 for (i = 0; i < 8; i++)
630 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
37ee990e 631
646c6b52 632 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
633 curbyte += aimutil_put8(newpacket->data+curbyte, strlen(sn));
634 curbyte += aimutil_putstr(newpacket->data+curbyte, sn, strlen(sn));
635 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
636 curbyte += aimutil_put16(newpacket->data+curbyte, 0x001a);
637 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002 /* accept*/);
37ee990e 638
646c6b52 639 for (i = 0;i < 8; i++)
640 curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]);
871e2fd0 641
646c6b52 642 curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid);
643 newpacket->lock = 0;
644 aim_tx_enqueue(sess, newpacket);
7392c79f 645
646c6b52 646 return newconn;
d410cf58 647#endif
7392c79f 648}
649
646c6b52 650/**
37ee990e 651 * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE*
652 * @file is an opened listing file
7392c79f 653 *
871e2fd0 654 * returns a pointer to the filled-in fileheader_t
655 *
646c6b52 656 * Currently omits checksum. we'll fix this when AOL breaks us, i
871e2fd0 657 * guess.
658 *
7392c79f 659 */
d410cf58 660faim_export struct aim_fileheader_t *aim_getlisting(aim_session_t *sess, FILE *file)
7392c79f 661{
d410cf58 662 return NULL;
663#if 0
7392c79f 664 struct aim_fileheader_t *fh;
871e2fd0 665 u_long totsize = 0, size = 0, checksum = 0xffff0000;
666 short totfiles = 0;
667 char *linebuf, sizebuf[9];
668
669 int linelength = 1024;
670
671 /* XXX: if we have a line longer than 1024chars, God help us. */
646c6b52 672 if ( (linebuf = (char *)calloc(1, linelength)) == NULL ) {
673 faimdprintf(sess, 2, "linebuf calloc failed\n");
871e2fd0 674 return NULL;
675 }
676
646c6b52 677 if (fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */
871e2fd0 678 perror("getlisting END1 fseek:");
646c6b52 679 faimdprintf(sess, 2, "getlising fseek END1 error\n");
871e2fd0 680 }
681
646c6b52 682 if ((size = ftell(file)) == -1) {
871e2fd0 683 perror("getlisting END1 getpos:");
646c6b52 684 faimdprintf(sess, 2, "getlising getpos END1 error\n");
871e2fd0 685 }
686
646c6b52 687 if (fseek(file, 0, SEEK_SET) != 0) {
871e2fd0 688 perror("getlesting fseek(SET):");
646c6b52 689 faimdprintf(sess, 2, "faim: getlisting: couldn't seek to beginning of listing file\n");
871e2fd0 690 }
691
37ee990e 692 memset(linebuf, 0, linelength);
871e2fd0 693
694 size = 0;
695
696 while(fgets(linebuf, linelength, file)) {
697 totfiles++;
37ee990e 698 memset(sizebuf, 0, 9);
871e2fd0 699
700 size += strlen(linebuf);
701
646c6b52 702 if (strlen(linebuf) < 23) {
703 faimdprintf(sess, 2, "line \"%s\" too short. skipping\n", linebuf);
871e2fd0 704 continue;
705 }
646c6b52 706 if (linebuf[strlen(linebuf)-1] != '\n') {
707 faimdprintf(sess, 2, "faim: OFT: getlisting -- hit EOF or line too long!\n");
871e2fd0 708 }
709
710 memcpy(sizebuf, linebuf+17, 8);
711
712 totsize += strtol(sizebuf, NULL, 10);
37ee990e 713 memset(linebuf, 0, linelength);
871e2fd0 714 }
715
646c6b52 716 if (fseek(file, 0, SEEK_SET) == -1) {
871e2fd0 717 perror("getlisting END2 fseek:");
646c6b52 718 faimdprintf(sess, 2, "getlising fseek END2 error\n");
871e2fd0 719 }
720
721 free(linebuf);
722
723 /* we're going to ignore checksumming the data for now -- that
724 * requires walking the whole listing.txt. it should probably be
725 * done at register time and cached, but, eh. */
7392c79f 726
646c6b52 727 if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t))))
7392c79f 728 return NULL;
729
730 fh->encrypt = 0x0000;
871e2fd0 731 fh->compress = 0x0000;
732 fh->totfiles = totfiles;
733 fh->filesleft = totfiles; /* is this right ?*/
7392c79f 734 fh->totparts = 0x0001;
735 fh->partsleft = 0x0001;
871e2fd0 736 fh->totsize = totsize;
737 fh->size = size; /* ls -l listing.txt */
738 fh->modtime = (int)time(NULL); /* we'll go with current time for now */
739 fh->checksum = checksum; /* XXX: checksum ! */
7392c79f 740 fh->rfcsum = 0x00000000;
741 fh->rfsize = 0x00000000;
742 fh->cretime = 0x00000000;
743 fh->rfcsum = 0x00000000;
744 fh->nrecvd = 0x00000000;
745 fh->recvcsum = 0x00000000;
746
871e2fd0 747 /* memset(fh->idstring, 0, sizeof(fh->idstring)); */
748 memcpy(fh->idstring, "OFT_Windows ICBMFT V1.1 32", sizeof(fh->idstring));
749 memset(fh->idstring+strlen(fh->idstring), 0, sizeof(fh->idstring)-strlen(fh->idstring));
7392c79f 750
751 fh->flags = 0x02;
752 fh->lnameoffset = 0x1a;
753 fh->lsizeoffset = 0x10;
754
871e2fd0 755 /* memset(fh->dummy, 0, sizeof(fh->dummy)); */
756 memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo));
7392c79f 757
871e2fd0 758 fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */
7392c79f 759 fh->nlanguage = 0x0000;
760
871e2fd0 761 /* memset(fh->name, 0, sizeof(fh->name)); */
762 memcpy(fh->name, "listing.txt", sizeof(fh->name));
763 memset(fh->name+strlen(fh->name), 0, 64-strlen(fh->name));
7392c79f 764
646c6b52 765 faimdprintf(sess, 2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name))));
7392c79f 766 return fh;
d410cf58 767#endif
7392c79f 768}
769
646c6b52 770/**
37ee990e 771 * aim_listenestablish - create a listening socket on a port.
772 * @portnum: the port number to bind to.
773 *
774 * you need to call accept() when it's connected. returns your fd
775 *
7392c79f 776 */
c5f5b7f1 777static int listenestablish(fu16_t portnum)
7392c79f 778{
37ee990e 779#if defined(__linux__)
c5f5b7f1 780 /* XXX what other OS's support getaddrinfo? */
781 int listenfd;
782 const int on = 1;
783 struct addrinfo hints, *res, *ressave;
784 char serv[5];
785
786 snprintf(serv, sizeof(serv), "%d", portnum);
787 memset(&hints, 0, sizeof(struct addrinfo));
788 hints.ai_flags = AI_PASSIVE;
789 hints.ai_family = AF_UNSPEC;
790 hints.ai_socktype = SOCK_STREAM;
791 if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) {
792 perror("getaddrinfo");
793 return -1;
794 }
795 ressave = res;
796 do {
797 listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
798 if (listenfd < 0)
799 continue;
800 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
801 if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
802 break;
803 /* success */
804 close(listenfd);
805 } while ( (res = res->ai_next) );
806
807 if (!res)
808 return -1;
809
810 if (listen(listenfd, 1024)!=0) {
811 perror("listen");
812 return -1;
813 }
814
815 freeaddrinfo(ressave);
816 return listenfd;
817#else
818 int listenfd;
819 const int on = 1;
820 struct sockaddr_in sockin;
821
822 if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
823 perror("socket(listenfd)");
824 return -1;
825 }
826
827 if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) {
828 perror("setsockopt(listenfd)");
829 close(listenfd);
830 return -1;
831 }
832
833 memset(&sockin, 0, sizeof(struct sockaddr_in));
834 sockin.sin_family = AF_INET;
835 sockin.sin_port = htons(portnum);
836
837 if (bind(listenfd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) {
838 perror("bind(listenfd)");
839 close(listenfd);
840 return -1;
841 }
842 if (listen(listenfd, 4) != 0) {
843 perror("listen(listenfd)");
844 close(listenfd);
845 return -1;
846 }
847 return listenfd;
848#endif
849}
37ee990e 850
c5f5b7f1 851static int getcommand_getfile(aim_session_t *sess, aim_conn_t *conn)
852{
853#if 0
854 struct aim_filetransfer_priv *ft;
855 aim_rxcallback_t userfunc;
856
857 ft = conn->priv;
858 if (ft->state == 2) {
859 /* waiting on listing data */
860 int ret = 0;
861 char *listing;
862 struct command_tx_struct *newoft;
863
864 if (!(listing = malloc(ft->fh.size)))
865 return -1;
866
867 ft->state = 0;
868 if (aim_recv(conn->fd, listing, ft->fh.size) != ft->fh.size)
869 faimdprintf(sess, 2, "OFT get: file %s was short. (0x%lx)\n", ft->fh.name, ft->fh.size);
870
871 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120b, 0))) {
872 faimdprintf(sess, 2, "faim: aim_get_command_rendezvous: getfile listing: tx_new OFT failed\n");
873 faim_mutex_unlock(&conn->active);
874 free(listing);
875 aim_conn_close(conn);
876 return -1;
877 }
878
879 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
880 newoft->hdr.oft.hdr2len = 0x100 - 8;
881
882 /* Protocol BS - set nrecvd to size of listing, recvcsum to listing checksum, flags to 0 */
883
884 ft->fh.nrecvd = ft->fh.size;
885 ft->fh.recvcsum = ft->fh.checksum;
886 ft->fh.flags = 0;
887
888 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
889 aim_frame_destroy(newoft);
890 free(listing);
891 return -1;
892 }
893
894 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh))))
895 faimdprintf(sess, 2, "eek! bh fail listing\n");
896
897 /* send the 120b */
898 aim_tx_enqueue(sess, newoft);
899 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) )
900 ret = userfunc(sess, NULL, conn, ft, listing);
901
902 free(listing);
903 return ret;
904 }
905
906 if (ft->state == 3) {
907 /* waiting on file data */
908 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECEIVE)) )
909 return userfunc(sess, NULL, conn, ft);
910 return 0;
911 }
912
913 if (ft->state == 4) {
914 if( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILESTATE4)) )
915 return userfunc(sess, NULL, conn);
916 aim_conn_close(conn);
917 return 0;
918 }
919
920 return 0;
37ee990e 921#else
c5f5b7f1 922 return -1;
923#endif
924}
646c6b52 925
59e1da90 926static void connclose_sendfile(aim_session_t *sess, aim_conn_t *conn)
c5f5b7f1 927{
c5f5b7f1 928 aim_msgcookie_t *cook;
929 struct aim_filetransfer_priv *priv = (struct aim_filetransfer_priv *)conn->priv;
37ee990e 930
c5f5b7f1 931 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTSEND);
932 aim_cookie_free(sess, cook);
37ee990e 933
59e1da90 934 return;
935}
646c6b52 936
59e1da90 937static void connkill_sendfile(aim_session_t *sess, aim_conn_t *conn)
938{
939
940 free(conn->internal);
c5f5b7f1 941
942 return;
943}
944
59e1da90 945static void connclose_getfile(aim_session_t *sess, aim_conn_t *conn)
c5f5b7f1 946{
c5f5b7f1 947 aim_msgcookie_t *cook;
948 struct aim_filetransfer_priv *priv = (struct aim_filetransfer_priv *)conn->priv;
949
950 cook = aim_uncachecookie(sess, priv->cookie, AIM_COOKIETYPE_OFTGET);
951 aim_cookie_free(sess, cook);
952
59e1da90 953 return;
954}
c5f5b7f1 955
59e1da90 956static void connkill_getfile(aim_session_t *sess, aim_conn_t *conn)
957{
958
959 free(conn->internal);
c5f5b7f1 960
961 return;
962}
963
59e1da90 964static void connclose_directim(aim_session_t *sess, aim_conn_t *conn)
c5f5b7f1 965{
c5f5b7f1 966 struct aim_directim_intdata *intdata = (struct aim_directim_intdata *)conn->internal;
c5f5b7f1 967 aim_msgcookie_t *cook;
968
969 cook = aim_uncachecookie(sess, intdata->cookie, AIM_COOKIETYPE_OFTIM);
970 aim_cookie_free(sess, cook);
971
59e1da90 972 return;
973}
974
975static void connkill_directim(aim_session_t *sess, aim_conn_t *conn)
976{
977
978 free(conn->internal);
979
980 return;
981}
982
983faim_internal void aim_conn_close_rend(aim_session_t *sess, aim_conn_t *conn)
984{
c5f5b7f1 985
59e1da90 986 if (conn->type != AIM_CONN_TYPE_RENDEZVOUS)
987 return;
988
989 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE)
990 connclose_sendfile(sess, conn);
991 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE)
992 connclose_getfile(sess, conn);
993 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)
994 connclose_directim(sess, conn);
995
996 return;
997}
998
999faim_internal void aim_conn_kill_rend(aim_session_t *sess, aim_conn_t *conn)
1000{
1001
1002 if (conn->type != AIM_CONN_TYPE_RENDEZVOUS)
1003 return;
1004
1005 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE)
1006 connkill_sendfile(sess, conn);
1007 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE)
1008 connkill_getfile(sess, conn);
1009 else if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)
1010 connkill_directim(sess, conn);
c5f5b7f1 1011
1012 return;
1013}
1014
1015static int handlehdr_directim(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1016{
1017 aim_frame_t fr;
1018 aim_rxcallback_t userfunc;
1019 fu32_t payloadlength;
1020 fu16_t flags;
1021 char *snptr = NULL;
1022
1023 fr.conn = conn;
1024
1025 payloadlength = aimutil_get32(hdr+22);
1026 flags = aimutil_get16(hdr+32);
1027 snptr = (char *)hdr+38;
1028
1029 faimdprintf(sess, 2, "faim: OFT frame: handlehdr_directim: %04x / %04x / %s\n", payloadlength, flags, snptr);
1030
1031 if (flags == 0x000e) {
1032 int ret = 0;
1033
1034 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)))
1035 ret = userfunc(sess, &fr, snptr);
1036
1037 return ret;
1038
1039 } else if ((flags == 0x0000) && payloadlength) {
1040 char *msg;
1041 int ret = 0;
1042
1043 if (!(msg = calloc(1, payloadlength+1)))
1044 return -1;
1045
1046 if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) {
1047 free(msg);
1048 return -1;
1049 }
1050
1051 msg[payloadlength] = '\0';
1052
1053 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) )
1054 ret = userfunc(sess, &fr, snptr, msg);
1055
1056 free(msg);
1057
1058 return ret;
1059 }
1060
1061 return 0;
1062}
1063
1064static int handlehdr_getfile_listing(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
7392c79f 1065{
d410cf58 1066#if 0
c5f5b7f1 1067 struct aim_filetransfer_priv *ft;
1068 struct aim_fileheader_t *fh;
1069 struct aim_msgcookie_t *cook;
1070 struct command_tx_struct *newoft;
1071 aim_rxcallback_t userfunc;
1072
1073 faimdprintf(sess, 2,"faim: rend: fileget 0x1108\n");
1074 fh = aim_oft_getfh(hdr);
646c6b52 1075
37ee990e 1076 faim_mutex_unlock(&conn->active);
4dd56961 1077
c5f5b7f1 1078 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1079 free(fh);
1080 return -1;
1081 }
646c6b52 1082
c5f5b7f1 1083 ft = cook->data;
646c6b52 1084
c5f5b7f1 1085 /* we're waaaaiiiting.. for listing.txt */
1086 ft->state = 2;
37ee990e 1087
c5f5b7f1 1088 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1089 free(fh);
37ee990e 1090
c5f5b7f1 1091 if(aim_cachecookie(sess, cook) == -1) {
1092 faimdprintf(sess, 1, "error caching cookie\n");
1093 return -1;
1094 }
37ee990e 1095
c5f5b7f1 1096 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x1209, 0))) {
1097 aim_conn_close(conn);
1098 return -1;
1099 }
37ee990e 1100
c5f5b7f1 1101 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1102 newoft->hdr.oft.hdr2len = 0x100 - 8;
37ee990e 1103
c5f5b7f1 1104 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1105 newoft->lock = 0;
1106 aim_frame_destroy(newoft);
1107 return -1;
1108 }
37ee990e 1109
c5f5b7f1 1110 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
1111 newoft->lock = 0;
1112 aim_frame_destroy(newoft);
1113 return -1;
1114 }
37ee990e 1115
c5f5b7f1 1116 newoft->lock = 0;
1117 aim_tx_enqueue(sess, newoft);
1118#endif
1119 return -1;
1120}
37ee990e 1121
c5f5b7f1 1122static int handlehdr_getfile_listing2(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1123{
1124#if 0
1125 struct aim_filetransfer_priv *ft;
1126 struct aim_fileheader_t *fh;
1127 struct aim_msgcookie_t *cook;
1128 int ret = 0;
1129 aim_rxcallback_t userfunc;
1130
1131 fh = aim_oft_getfh(hdr);
37ee990e 1132
c5f5b7f1 1133 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET)))
1134 faimdprintf(sess, 2, "shit, no cookie in 0x1209. (%i/%s)going to crash..\n", AIM_COOKIETYPE_OFTGET, fh->bcookie);
646c6b52 1135
c5f5b7f1 1136 ft = cook->data;
646c6b52 1137
c5f5b7f1 1138 if (ft->fh.size != fh->size)
1139 faimdprintf(sess, 2, "hrm. ft->fh.size (%ld) != fh->size (%ld). um. using ft->fh.size\n", ft->fh.size, fh->size);
37ee990e 1140
c5f5b7f1 1141 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ)))
1142 ret = userfunc(sess, NULL, conn, fh);
37ee990e 1143
c5f5b7f1 1144 faimdprintf(sess, 2, "faim: get_command_rendezvous: hit end of 1209\n");
37ee990e 1145
c5f5b7f1 1146 free(fh);
646c6b52 1147
c5f5b7f1 1148 return ret;
1149#else
1150 return -1;
1151#endif
1152}
646c6b52 1153
c5f5b7f1 1154static int handlehdr_getfile_listing3(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1155{
1156#if 0
1157 struct aim_filetransfer_priv *ft;
1158 struct aim_msgcookie_t *cook;
1159 struct aim_fileheader_t *fh;
1160 aim_rxcallback_t userfunc;
646c6b52 1161
c5f5b7f1 1162 fh = aim_oft_getfh(hdr);
37ee990e 1163
c5f5b7f1 1164 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1165 free(fh);
1166 return -1;
1167 }
646c6b52 1168
c5f5b7f1 1169 free(fh);
646c6b52 1170
c5f5b7f1 1171 ft = cook->data;
871e2fd0 1172
c5f5b7f1 1173 if (aim_cachecookie(sess, cook) == -1)
1174 return -1;
3b101546 1175
c5f5b7f1 1176 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGRXCONFIRM)))
1177 return userfunc(sess, NULL, conn);
1178#endif
1179 return -1;
1180}
871e2fd0 1181
c5f5b7f1 1182static int handlehdr_getfile_request(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1183{
1184#if 0
1185 struct aim_filetransfer_priv *ft;
1186 struct aim_msgcookie_t *cook;
1187 struct aim_fileheader_t *fh;
1188 struct command_tx_struct *newoft;
1189 int i = 0;
1190 aim_rxcallback_t userfunc;
3b101546 1191
c5f5b7f1 1192 fh = aim_oft_getfh(hdr);
3b101546 1193
c5f5b7f1 1194 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1195 free(fh);
1196 return -1;
1197 }
3b101546 1198
c5f5b7f1 1199 ft = cook->data;
1200 memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t));
1201 free(fh);
871e2fd0 1202
c5f5b7f1 1203 aim_cachecookie(sess, cook);
871e2fd0 1204
c5f5b7f1 1205 faimdprintf(sess, 2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name);
871e2fd0 1206
c5f5b7f1 1207 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) )
1208 i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie);
871e2fd0 1209
c5f5b7f1 1210 if (i < 0)
1211 return i;
871e2fd0 1212
c5f5b7f1 1213 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0101, 0))) {
1214 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1215 return -1;
1216 }
871e2fd0 1217
c5f5b7f1 1218 newoft->lock = 1;
1219 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1220 newoft->hdr.oft.hdr2len = 0x100 - 8;
871e2fd0 1221
c5f5b7f1 1222 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1223 aim_frame_destroy(newoft);
1224 return -1;
1225 }
7392c79f 1226
c5f5b7f1 1227 /* protocol BS: nrecvd, recvcsum to 0, flags to 0x20. */
1228 ft->fh.nrecvd = 0;
1229 ft->fh.recvcsum = 0;
1230 ft->fh.flags = 0x20;
7392c79f 1231
c5f5b7f1 1232 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
7392c79f 1233
c5f5b7f1 1234 newoft->lock = 0;
1235 aim_tx_enqueue(sess, newoft);
646c6b52 1236
c5f5b7f1 1237 faimdprintf(sess, 2, "faim: OFT: OFT file header enqueued.\n");
646c6b52 1238
c5f5b7f1 1239 return i;
1240#else
1241 return -1;
1242#endif
1243}
871e2fd0 1244
c5f5b7f1 1245static int handlehdr_getfile_sending(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1246{
1247#if 0
1248 struct aim_fileheader_t *fh;
1249 struct aim_filetransfer_priv *ft;
1250 struct aim_msgcookie_t *cook;
1251 struct command_tx_struct *newoft;
1252 aim_rxcallback_t userfunc;
871e2fd0 1253
c5f5b7f1 1254 fh = aim_oft_getfh(hdr);
7392c79f 1255
c5f5b7f1 1256 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1257 free(fh);
1258 return -1;
1259 }
7392c79f 1260
c5f5b7f1 1261 free(fh);
646c6b52 1262
c5f5b7f1 1263 ft = cook->data;
7392c79f 1264
c5f5b7f1 1265 ft->state = 3;
7392c79f 1266
c5f5b7f1 1267 if (aim_cachecookie(sess, cook) == -1)
1268 return -1;
7392c79f 1269
c5f5b7f1 1270 faimdprintf(sess, 2, "faim: fileget: %s seems to want to send %s\n", ft->sn, ft->fh.name);
7392c79f 1271
c5f5b7f1 1272 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1273 faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n");
1274 return -1;
1275 }
4dd56961 1276
c5f5b7f1 1277 newoft->lock = 1;
1278 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
871e2fd0 1279
c5f5b7f1 1280 newoft->hdr.oft.hdr2len = 0x100 - 8;
7392c79f 1281
c5f5b7f1 1282 if (!(newoft->hdr.oft.hdr2 = calloc(1,newoft->hdr.oft.hdr2len))) {
1283 aim_frame_destroy(newoft);
1284 return -1;
1285 }
7392c79f 1286
c5f5b7f1 1287 aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh));
7392c79f 1288
c5f5b7f1 1289 newoft->lock = 0;
1290 aim_tx_enqueue(sess, newoft);
7392c79f 1291
c5f5b7f1 1292 faimdprintf(sess, 2, "faim: OFT: OFT 0x0202 enqueued.\n");
646c6b52 1293
c5f5b7f1 1294 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL)
1295 return 1;
1296#else
1297 return -1;
1298#endif
1299}
646c6b52 1300
c5f5b7f1 1301static int handlehdr_getfile_recv(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1302{
1303#if 0
1304 struct aim_fileheader_t *fh;
1305 struct aim_filetransfer_priv *ft;
1306 struct aim_msgcookie_t *cook;
1307 int ret = 1;
1308 aim_rxcallback_t userfunc;
7392c79f 1309
c5f5b7f1 1310 fh = aim_oft_getfh(hdr);
7392c79f 1311
c5f5b7f1 1312 if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) {
1313 free(fh);
1314 return -1;
1315 }
7392c79f 1316
c5f5b7f1 1317 ft = cook->data;
7392c79f 1318
c5f5b7f1 1319 faimdprintf(sess, 2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n");
4dd56961 1320
c5f5b7f1 1321 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) )
1322 ret = userfunc(sess, NULL, conn, fh);
871e2fd0 1323
c5f5b7f1 1324 free(fh);
646c6b52 1325
c5f5b7f1 1326 return ret;
1327#else
1328 return -1;
1329#endif
1330}
646c6b52 1331
c5f5b7f1 1332static int handlehdr_getfile_finish(aim_session_t *sess, aim_conn_t *conn, fu8_t *hdr)
1333{
1334#if 0
1335 struct aim_fileheader_t *fh;
1336 aim_rxcallback_t userfunc;
871e2fd0 1337
c5f5b7f1 1338 fh = aim_oft_getfh(hdr);
871e2fd0 1339
c5f5b7f1 1340 faimdprintf(sess, 2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n");
871e2fd0 1341
c5f5b7f1 1342 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) )
1343 userfunc(sess, NULL, conn, fh);
871e2fd0 1344
c5f5b7f1 1345 free(fh);
1346#endif
7392c79f 1347
c5f5b7f1 1348 return -1;
1349}
9e8c4225 1350
c5f5b7f1 1351/**
1352 * aim_get_command_rendezvous - OFT equivalent of aim_get_command
1353 * @sess: session to work on
1354 * @conn: conn to pull data from
1355 *
1356 * this reads and handles data from conn->fd. currently a little rough
1357 * around the edges
1358 */
1359faim_internal int aim_get_command_rendezvous(aim_session_t *sess, aim_conn_t *conn)
1360{
1361 fu8_t hdrbuf1[6];
1362 fu8_t *hdr = NULL;
1363 int hdrlen, hdrtype;
1364 int ret = -1;
1365
1366 if (!sess || !conn)
1367 return -1;
1368
1369 memset(hdrbuf1, 0, sizeof(hdrbuf1));
1370
1371 /* I guess? I didn't understand any of that mess... */
1372 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE)
1373 return getcommand_getfile(sess, conn);
1374
1375 /* XXX fix all the error cases here */
1376 if (aim_recv(conn->fd, hdrbuf1, 6) < 6) {
1377
1378 faimdprintf(sess, 2, "faim: rend: read error (fd: %i)\n", conn->fd);
1379
c5f5b7f1 1380 aim_conn_close(conn);
1381
1382 return -1;
1383 }
1384
1385 hdrlen = aimutil_get16(hdrbuf1+4);
1386 hdrlen -= 6;
1387
1388 hdr = malloc(hdrlen);
1389
1390 if (aim_recv(conn->fd, hdr, hdrlen) < hdrlen) {
1391 faimdprintf(sess, 2, "faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen);
1392 free(hdr);
1393 aim_conn_close(conn);
1394 return -1;
1395 }
1396
1397 hdrtype = aimutil_get16(hdr);
1398
1399 if (hdrtype == 0x0001)
1400 ret = handlehdr_directim(sess, conn, hdr);
1401 else if (hdrtype == 0x1108) /* getfile listing.txt incoming tx->rx */
1402 ret = handlehdr_getfile_listing(sess, conn, hdr);
1403 else if (hdrtype == 0x1209) /* get file listing ack rx->tx */
1404 ret = handlehdr_getfile_listing2(sess, conn, hdr);
1405 else if (hdrtype == 0x120b) /* get file listing rx confirm */
1406 ret = handlehdr_getfile_listing3(sess, conn, hdr);
1407 else if (hdrtype == 0x120c) /* getfile request */
1408 ret = handlehdr_getfile_request(sess, conn, hdr);
1409 else if (hdrtype == 0x0101) /* getfile sending data */
1410 ret = handlehdr_getfile_sending(sess, conn, hdr);
1411 else if (hdrtype == 0x0202) /* getfile recv data */
1412 ret = handlehdr_getfile_recv(sess, conn, hdr);
1413 else if (hdrtype == 0x0204) /* getfile finished */
1414 ret = handlehdr_getfile_finish(sess, conn, hdr);
1415 else {
1416 faimdprintf(sess, 2,"faim: OFT frame: uknown type %04x\n", hdrtype);
1417 ret = -1;
1418 }
1419
1420 free(hdr);
1421
1422 if (ret == -1)
1423 aim_conn_close(conn);
1424
1425 return ret;
7392c79f 1426}
d410cf58 1427
1428#if 0
646c6b52 1429/**
1430 * aim_oft_getfh - extracts an &aim_fileheader_t from buffer hdr.
37ee990e 1431 * @hdr: buffer to extract header from
1432 *
646c6b52 1433 * returns pointer to new struct on success; %NULL on error.
871e2fd0 1434 *
7392c79f 1435 */
37ee990e 1436static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr)
7392c79f 1437{
1438 struct aim_fileheader_t *fh;
1439 int i, j;
646c6b52 1440 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t))))
7392c79f 1441 return NULL;
37ee990e 1442
7392c79f 1443 /* [0] and [1] are the type. we can ignore those here. */
7392c79f 1444 i = 2;
7392c79f 1445 for(j = 0; j < 8; j++, i++)
1446 fh->bcookie[j] = hdr[i];
1447 fh->encrypt = aimutil_get16(hdr+i);
1448 i += 2;
1449 fh->compress = aimutil_get16(hdr+i);
1450 i += 2;
1451 fh->totfiles = aimutil_get16(hdr+i);
1452 i += 2;
1453 fh->filesleft = aimutil_get16(hdr+i);
1454 i += 2;
1455 fh->totparts = aimutil_get16(hdr+i);
1456 i += 2;
1457 fh->partsleft = aimutil_get16(hdr+i);
1458 i += 2;
1459 fh->totsize = aimutil_get32(hdr+i);
1460 i += 4;
1461 fh->size = aimutil_get32(hdr+i);
1462 i += 4;
1463 fh->modtime = aimutil_get32(hdr+i);
1464 i += 4;
1465 fh->checksum = aimutil_get32(hdr+i);
1466 i += 4;
1467 fh->rfrcsum = aimutil_get32(hdr+i);
1468 i += 4;
1469 fh->rfsize = aimutil_get32(hdr+i);
1470 i += 4;
1471 fh->cretime = aimutil_get32(hdr+i);
1472 i += 4;
1473 fh->rfcsum = aimutil_get32(hdr+i);
1474 i += 4;
1475 fh->nrecvd = aimutil_get32(hdr+i);
1476 i += 4;
1477 fh->recvcsum = aimutil_get32(hdr+i);
1478 i += 4;
7392c79f 1479 memcpy(fh->idstring, hdr+i, 32);
1480 i += 32;
7392c79f 1481 fh->flags = aimutil_get8(hdr+i);
1482 i += 1;
1483 fh->lnameoffset = aimutil_get8(hdr+i);
1484 i += 1;
1485 fh->lsizeoffset = aimutil_get8(hdr+i);
1486 i += 1;
7392c79f 1487 memcpy(fh->dummy, hdr+i, 69);
1488 i += 69;
7392c79f 1489 memcpy(fh->macfileinfo, hdr+i, 16);
1490 i += 16;
7392c79f 1491 fh->nencode = aimutil_get16(hdr+i);
1492 i += 2;
1493 fh->nlanguage = aimutil_get16(hdr+i);
1494 i += 2;
7392c79f 1495 memcpy(fh->name, hdr+i, 64);
1496 i += 64;
7392c79f 1497 return fh;
37ee990e 1498}
d410cf58 1499#endif
871e2fd0 1500
646c6b52 1501/**
37ee990e 1502 * aim_oft_checksum - calculate oft checksum of buffer
1503 * @buffer: buffer of data to checksum
1504 * @bufsize: size of buffer
1505 * @checksum: pointer to integer to place result in (pointer!)
37ee990e 1506 *
646c6b52 1507 *
1508 * Note that checksum is a pointer. Checksum should be filled with
37ee990e 1509 * 0xFFFF0000 for each new file; you can have this checksum chunks of
1510 * files in series if you just call it repeatedly in a for(; ; ) loop
646c6b52 1511 * and don't reset the checksum between each call. And you thought we
37ee990e 1512 * didn't care about you and your pathetic client's meomry footprint
1513 * ;^)
1514 *
646c6b52 1515 *
1516 * Also, it's been said that this is incorrect as currently
1517 * written. You were warned.
37ee990e 1518 */
d410cf58 1519faim_export fu32_t aim_oft_checksum(aim_session_t *sess, const char *buffer, int bufsize, fu32_t *checksum)
646c6b52 1520{
d410cf58 1521 return 0xdeadbeef;
1522#if 0
1523 fu16_t check0, check1;
646c6b52 1524 int i;
d410cf58 1525
646c6b52 1526 check0 = ((*checksum & 0xFF000000) >> 16);
1527 check1 = ((*checksum & 0x00ff0000) >> 16);
1528 for(i = 0; i < bufsize; i++) {
1529 if (i % 2) { /* use check1 -- second byte */
1530 if ( (short)buffer[i] > check1 ) { /* wrapping */
1531 check1 += 0x100; /* this is a cheap way to wrap */
1532
1533 /* if we're wrapping, decrement the other one */
1534 /* XXX: check this corner case */
1535 if (check0 == 0)
1536 check0 = 0x00ff;
1537 else
1538 check0--;
1539 }
1540 check1 -= buffer[i];
1541 } else { /* use check0 -- first byte */
1542 if ( (short)buffer[i] > check0 ) { /* wrapping */
1543 check0 += 0x100; /* this is a cheap way to wrap */
871e2fd0 1544
646c6b52 1545 /* if we're wrapping, decrement the other one */
1546 /* XXX: check this corner case */
1547 if (check1 == 0)
1548 check1 = 0x00ff;
1549 else
1550 check1--;
1551 }
1552 check0 -= buffer[i];
1553 }
1554 }
37ee990e 1555
646c6b52 1556 if (check0 > 0xff || check1 > 0xff) {
1557 /* they shouldn't be able to do this. error! */
1558 faimdprintf(sess, 2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1);
1559 return -1;
1560 }
37ee990e 1561
646c6b52 1562 /* grab just the lowest byte; this should be clean, but just in
1563 case */
1564 check0 &= 0xff;
1565 check1 &= 0xff;
37ee990e 1566
646c6b52 1567 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000));
1568 return *checksum;
d410cf58 1569#endif
37ee990e 1570}
871e2fd0 1571
d410cf58 1572#if 0
646c6b52 1573/**
37ee990e 1574 * aim_oft_buildheader - fills a buffer with network-order fh data
1575 * @dest: buffer to fill -- pre-alloced
1576 * @fh: fh to get data from
871e2fd0 1577 *
37ee990e 1578 * returns length written; -1 on error.
871e2fd0 1579 * DOES NOT DO BOUNDS CHECKING!
37ee990e 1580 *
871e2fd0 1581 */
d410cf58 1582static int oft_buildheader(unsigned char *dest, struct aim_fileheader_t *fh)
37ee990e 1583{
871e2fd0 1584 int i, curbyte;
646c6b52 1585 if (!dest || !fh)
871e2fd0 1586 return -1;
37ee990e 1587 curbyte = 0;
1588 for(i = 0; i < 8; i++)
1589 curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]);
1590 curbyte += aimutil_put16(dest+curbyte, fh->encrypt);
1591 curbyte += aimutil_put16(dest+curbyte, fh->compress);
1592 curbyte += aimutil_put16(dest+curbyte, fh->totfiles);
1593 curbyte += aimutil_put16(dest+curbyte, fh->filesleft);
1594 curbyte += aimutil_put16(dest+curbyte, fh->totparts);
1595 curbyte += aimutil_put16(dest+curbyte, fh->partsleft);
1596 curbyte += aimutil_put32(dest+curbyte, fh->totsize);
1597 curbyte += aimutil_put32(dest+curbyte, fh->size);
1598 curbyte += aimutil_put32(dest+curbyte, fh->modtime);
1599 curbyte += aimutil_put32(dest+curbyte, fh->checksum);
1600 curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum);
1601 curbyte += aimutil_put32(dest+curbyte, fh->rfsize);
1602 curbyte += aimutil_put32(dest+curbyte, fh->cretime);
1603 curbyte += aimutil_put32(dest+curbyte, fh->rfcsum);
1604 curbyte += aimutil_put32(dest+curbyte, fh->nrecvd);
1605 curbyte += aimutil_put32(dest+curbyte, fh->recvcsum);
1606 memcpy(dest+curbyte, fh->idstring, 32);
871e2fd0 1607 curbyte += 32;
37ee990e 1608 curbyte += aimutil_put8(dest+curbyte, fh->flags);
1609 curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset);
1610 curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset);
1611 memcpy(dest+curbyte, fh->dummy, 69);
871e2fd0 1612 curbyte += 69;
37ee990e 1613 memcpy(dest+curbyte, fh->macfileinfo, 16);
871e2fd0 1614 curbyte += 16;
37ee990e 1615 curbyte += aimutil_put16(dest+curbyte, fh->nencode);
1616 curbyte += aimutil_put16(dest+curbyte, fh->nlanguage);
1617 memset(dest+curbyte, 0x00, 64);
1618 memcpy(dest+curbyte, fh->name, 64);
871e2fd0 1619
37ee990e 1620 /* XXX: Filenames longer than 64B */
871e2fd0 1621 curbyte += 64;
871e2fd0 1622 return curbyte;
1623}
d410cf58 1624#endif
871e2fd0 1625
646c6b52 1626/**
1627 * aim_getfile_intitiate - Request an OFT getfile session
37ee990e 1628 * @sess: your session,
1629 * @conn: the BOS conn,
1630 * @destsn is the SN to connect to.
646c6b52 1631 *
1632 * returns a new &aim_conn_t on success, %NULL on error
871e2fd0 1633 */
d410cf58 1634faim_export aim_conn_t *aim_getfile_initiate(aim_session_t *sess, aim_conn_t *conn, const char *destsn)
37ee990e 1635{
d410cf58 1636 return NULL;
1637#if 0
871e2fd0 1638 struct command_tx_struct *newpacket;
646c6b52 1639 struct aim_conn_t *newconn;
1640 struct aim_filetransfer_priv *priv;
1641 struct aim_msgcookie_t *cookie;
1642 int curbyte, i, listenfd;
1643 short port = 4443;
1644 struct hostent *hptr;
1645 struct utsname myname;
1646 char cap[16];
1647 char d[4];
37ee990e 1648
646c6b52 1649 /* Open our socket */
871e2fd0 1650
646c6b52 1651 if ( (listenfd = aim_listenestablish(port)) == -1)
1652 return NULL;
871e2fd0 1653
646c6b52 1654 /* get our local IP */
871e2fd0 1655
646c6b52 1656 if (uname(&myname) < 0)
1657 return NULL;
1658 if ( (hptr = gethostbyname(myname.nodename)) == NULL)
1659 return NULL;
1660 memcpy(&d, hptr->h_addr_list[0], 4);
871e2fd0 1661
646c6b52 1662 aim_putcap(cap, 16, AIM_CAPS_GETFILE);
871e2fd0 1663
646c6b52 1664 /* create the OSCAR packet */
871e2fd0 1665
646c6b52 1666 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x42)))
1667 return NULL;
1668 newpacket->lock = 1;
871e2fd0 1669
646c6b52 1670 /* lock struct */
1671 curbyte = 0;
1672 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid);
871e2fd0 1673
646c6b52 1674 /* XXX: check the cookie before commiting to using it */
871e2fd0 1675
646c6b52 1676 /* Generate a random message cookie
1677 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */
1678 for (i=0; i<7; i++)
1679 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10));
871e2fd0 1680
646c6b52 1681 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
871e2fd0 1682
646c6b52 1683 /* grab all the data for cookie caching. */
1684
1685 if (!(cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t))))
1686 return NULL;
1687 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8);
1688 cookie->type = AIM_COOKIETYPE_OFTGET;
871e2fd0 1689
646c6b52 1690 if (!(priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv))))
1691 return NULL;
1692 memcpy(priv->cookie, cookie, 8);
1693 memcpy(priv->sn, destsn, sizeof(priv->sn));
1694 memcpy(priv->fh.name, "listing.txt", strlen("listing.txt"));
1695 priv->state = 1;
871e2fd0 1696
646c6b52 1697 cookie->data = priv;
871e2fd0 1698
646c6b52 1699 aim_cachecookie(sess, cookie);
1700
1701 /* Channel ID */
1702 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002);
871e2fd0 1703
646c6b52 1704 /* Destination SN (prepended with byte length) */
1705 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn));
1706 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn));
1707 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1708 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
871e2fd0 1709
646c6b52 1710 /* enTLV start */
1711 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1712 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042);
871e2fd0 1713
646c6b52 1714 /* Flag data / ICBM Parameters? */
1715 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
1716 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
871e2fd0 1717
646c6b52 1718 /* Cookie */
1719 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8);
871e2fd0 1720
646c6b52 1721 /* Capability String */
1722 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10);
871e2fd0 1723
646c6b52 1724 /* 000a/0002 : 0001 */
1725 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a);
1726 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1727 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001);
871e2fd0 1728
646c6b52 1729 /* 0003/0004: IP address */
1730 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003);
1731 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004);
1732 for(i = 0; i < 4; i++)
1733 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]);
871e2fd0 1734
646c6b52 1735 /* already in network byte order */
37ee990e 1736
646c6b52 1737 /* 0005/0002: Port */
1738 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005);
1739 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002);
1740 curbyte += aimutil_put16(newpacket->data+curbyte, port);
871e2fd0 1741
646c6b52 1742 /* 000f/0000: ?? */
1743 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f);
1744 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000);
871e2fd0 1745
646c6b52 1746 /* 2711/000c: ?? */
1747 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711);
1748 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c);
1749 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001);
871e2fd0 1750
646c6b52 1751 for(i = 0; i < 0x000c - 4; i++)
1752 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00);
871e2fd0 1753
646c6b52 1754 newpacket->commandlen = curbyte;
1755 newpacket->lock = 0;
1756 aim_tx_enqueue(sess, newpacket);
871e2fd0 1757
646c6b52 1758 /* allocate and set up our connection */
871e2fd0 1759
646c6b52 1760 i = fcntl(listenfd, F_GETFL, 0);
1761 fcntl(listenfd, F_SETFL, i | O_NONBLOCK);
1762 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL);
871e2fd0 1763
646c6b52 1764 if (!newconn){
1765 perror("aim_newconn");
1766 return NULL;
1767 }
871e2fd0 1768
646c6b52 1769 newconn->fd = listenfd;
1770 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE;
1771 newconn->priv = priv;
1772 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd);
871e2fd0 1773
646c6b52 1774 return newconn;
d410cf58 1775#endif
37ee990e 1776}
1777
646c6b52 1778/**
37ee990e 1779 * aim_oft_getfile_request - request a particular file over an established getfile connection
1780 * @sess: your session
1781 * @conn: the established OFT getfile connection
1782 * @name: filename to request
1783 * @size: size of the file
1784 *
646c6b52 1785 *
1786 * returns -1 on error, 0 on successful enqueuing
37ee990e 1787 */
d410cf58 1788faim_export int aim_oft_getfile_request(aim_session_t *sess, aim_conn_t *conn, const char *name, int size)
37ee990e 1789{
d410cf58 1790 return -EINVAL;
1791#if 0
37ee990e 1792 struct command_tx_struct *newoft;
1793 struct aim_filetransfer_priv *ft;
646c6b52 1794 if (!sess || !conn || !conn->priv || !name)
1795 return -1;
37ee990e 1796
646c6b52 1797 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120c, 0))) {
1798 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
1799 return -1;
1800 }
37ee990e 1801
646c6b52 1802 newoft->lock = 1;
37ee990e 1803
646c6b52 1804 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1805 newoft->hdr.oft.hdr2len = 0x100 - 8;
37ee990e 1806
646c6b52 1807 ft = (struct aim_filetransfer_priv *)conn->priv;
1808 ft->fh.filesleft = 1;
1809 ft->fh.totfiles = 1;
1810 ft->fh.totparts = 1;
1811 ft->fh.partsleft = 1;
1812 ft->fh.totsize = size;
1813 ft->fh.size = size;
1814 ft->fh.checksum = 0;
1815 memcpy(ft->fh.name, name, strlen(name));
1816 memset(ft->fh.name+strlen(name), 0, 1);
1817
1818 if (!(newoft->hdr.oft.hdr2 = (unsigned char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1819 newoft->lock = 0;
d410cf58 1820 aim_frame_destroy(newoft);
646c6b52 1821 return -1;
1822 }
37ee990e 1823
646c6b52 1824 if (!(aim_oft_buildheader(newoft->hdr.oft.hdr2, &(ft->fh)))) {
1825 newoft->lock = 0;
d410cf58 1826 aim_frame_destroy(newoft);
646c6b52 1827 return -1;
1828 }
37ee990e 1829
646c6b52 1830 newoft->lock = 0;
37ee990e 1831
646c6b52 1832 aim_tx_enqueue(sess, newoft);
1833 return 0;
d410cf58 1834#endif
37ee990e 1835}
1836
646c6b52 1837/**
37ee990e 1838 * aim_oft_getfile_ack - acknowledge a getfile download as complete
1839 * @sess: your session
1840 * @conn: the getfile conn to send the ack over
1841 *
646c6b52 1842 * Call this function after you have read all the data in a particular
1843 * filetransfer. Returns -1 on error, 0 on apparent success
37ee990e 1844 *
1845 */
d410cf58 1846faim_export int aim_oft_getfile_ack(aim_session_t *sess, aim_conn_t *conn)
646c6b52 1847{
d410cf58 1848 return -EINVAL;
1849#if 0
37ee990e 1850 struct command_tx_struct *newoft;
1851 struct aim_filetransfer_priv *ft;
871e2fd0 1852
646c6b52 1853 if (!sess || !conn || !conn->priv)
37ee990e 1854 return -1;
871e2fd0 1855
646c6b52 1856 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) {
1857 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
37ee990e 1858 return -1;
1859 }
871e2fd0 1860
37ee990e 1861 newoft->lock = 1;
871e2fd0 1862
37ee990e 1863 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1864 newoft->hdr.oft.hdr2len = 0x100-8;
871e2fd0 1865
37ee990e 1866 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1867 newoft->lock = 0;
d410cf58 1868 aim_frame_destroy(newoft);
37ee990e 1869 return -1;
1870 }
871e2fd0 1871
37ee990e 1872 ft = (struct aim_filetransfer_priv *)conn->priv;
871e2fd0 1873
646c6b52 1874 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
37ee990e 1875 newoft->lock = 0;
d410cf58 1876 aim_frame_destroy(newoft);
37ee990e 1877 return -1;
1878 }
871e2fd0 1879
37ee990e 1880 newoft->lock = 0;
1881 aim_tx_enqueue(sess, newoft);
1882 return 0;
d410cf58 1883#endif
871e2fd0 1884}
37ee990e 1885
646c6b52 1886/**
37ee990e 1887 * aim_oft_getfile_end - end a getfile.
1888 * @sess: your session
1889 * @conn: the getfile connection
1890 *
1891 * call this before you close the getfile connection if you're on the
1892 * receiving/requesting end.
1893 */
d410cf58 1894faim_export int aim_oft_getfile_end(aim_session_t *sess, aim_conn_t *conn)
37ee990e 1895{
d410cf58 1896 return -EINVAL;
1897#if 0
37ee990e 1898 struct command_tx_struct *newoft;
1899 struct aim_filetransfer_priv *ft;
1900
646c6b52 1901 if (!sess || !conn || !conn->priv)
37ee990e 1902 return -1;
871e2fd0 1903
646c6b52 1904 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0204, 0))) {
1905 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n");
37ee990e 1906 return -1;
1907 }
1908
1909 newoft->lock = 1;
1910
1911 memcpy(newoft->hdr.oft.magic, "OFT2", 4);
1912 newoft->hdr.oft.hdr2len = 0x100 - 8;
1913
1914 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) {
1915 newoft->lock = 0;
d410cf58 1916 aim_frame_destroy(newoft);
37ee990e 1917 return -1;
1918 }
1919
1920 ft = (struct aim_filetransfer_priv *)conn->priv;
1921 ft->state = 4; /* no longer wanting data */
1922 ft->fh.nrecvd = ft->fh.size;
1923 ft->fh.recvcsum = ft->fh.checksum;
1924 ft->fh.flags = 0x21;
1925
646c6b52 1926 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) {
37ee990e 1927 newoft->lock = 0;
d410cf58 1928 aim_frame_destroy(newoft);
37ee990e 1929 return -1;
1930 }
1931
1932 newoft->lock = 0;
1933 aim_tx_enqueue(sess, newoft);
1934
1935 return 0;
d410cf58 1936#endif /* 0 */
37ee990e 1937}
d410cf58 1938
This page took 0.492412 seconds and 5 git commands to generate.