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