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