]>
Commit | Line | Data |
---|---|---|
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 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 | |
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. | |
36 | */ | |
37 | faim_export int aim_handlerendconnect(struct aim_session_t *sess, struct aim_conn_t *cur) | |
38 | { | |
39 | int acceptfd = 0; | |
40 | rxcallback_t userfunc; | |
41 | struct sockaddr cliaddr; | |
42 | socklen_t clilen = sizeof(cliaddr); | |
43 | int ret = 0; | |
44 | struct aim_conn_t *newconn; | |
45 | ||
46 | if ( (acceptfd = accept(cur->fd, &cliaddr, &clilen)) == -1) | |
47 | return -1; | |
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 | } | |
53 | ||
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 | ||
60 | if (!(newconn = aim_cloneconn(sess, cur))) { | |
61 | close(acceptfd); | |
62 | aim_conn_close(cur); | |
63 | return -1; | |
64 | } | |
65 | ||
66 | newconn->type = AIM_CONN_TYPE_RENDEZVOUS; | |
67 | newconn->fd = acceptfd; | |
68 | ||
69 | switch(newconn->subtype) { | |
70 | case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { | |
71 | struct aim_directim_priv *priv; | |
72 | ||
73 | priv = cur->priv; | |
74 | ||
75 | newconn->priv = cur->priv; | |
76 | ||
77 | cur->priv = NULL; | |
78 | ||
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)); | |
82 | ||
83 | if ( (userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINITIATE))) | |
84 | ret = userfunc(sess, NULL, newconn, cur); | |
85 | ||
86 | break; | |
87 | } | |
88 | case AIM_CONN_SUBTYPE_OFT_GETFILE: { | |
89 | struct aim_filetransfer_priv *priv; | |
90 | ||
91 | ||
92 | newconn->priv = cur->priv; | |
93 | cur->priv = NULL; | |
94 | priv = (struct aim_filetransfer_priv *)newconn->priv; | |
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 | ||
98 | if ( (userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEINITIATE))) | |
99 | ret = userfunc(sess, NULL, newconn, cur); | |
100 | ||
101 | break; | |
102 | } | |
103 | default: { | |
104 | faimdprintf(sess, 1,"Got a Connection on a listener that's not Rendezvous(??!) Closing conn.\n"); | |
105 | aim_conn_close(newconn); | |
106 | break; | |
107 | } | |
108 | } | |
109 | ||
110 | return ret; | |
111 | } | |
112 | ||
113 | /** | |
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 | * | |
119 | * Call this just like you would aim_send_im, to send a directim. You | |
120 | * _must_ have previously established the directim connection. | |
121 | */ | |
122 | faim_export int aim_send_im_direct(struct aim_session_t *sess, struct aim_conn_t *conn, char *msg) | |
123 | { | |
124 | struct command_tx_struct *newpacket; | |
125 | struct aim_directim_priv *priv = NULL; | |
126 | int i; | |
127 | ||
128 | if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !conn->priv) { | |
129 | faimdprintf(sess, 2,"faim: directim: invalid arguments\n"); | |
130 | return -1; | |
131 | } | |
132 | ||
133 | priv = (struct aim_directim_priv *)conn->priv; | |
134 | ||
135 | if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, strlen(msg)))) { | |
136 | faimdprintf(sess, 2,"faim: directim: tx_new failed\n"); | |
137 | return -1; | |
138 | } | |
139 | ||
140 | newpacket->lock = 1; | |
141 | ||
142 | /* if msg is non-null, we'resending an IM, else a "typing" notice */ | |
143 | if (msg) { | |
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 | } | |
160 | ||
161 | memcpy(newpacket->hdr.oft.magic, "ODC2", 4); | |
162 | newpacket->data = NULL; | |
163 | ||
164 | i = 0; | |
165 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0006); | |
166 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
167 | i += aimutil_putstr(newpacket->hdr.oft.hdr2+i, (char *)priv->cookie, 8); | |
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); | |
172 | i += aimutil_put32(newpacket->hdr.oft.hdr2+i, strlen(msg)); | |
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); | |
176 | ||
177 | /* flags -- 0x000e for "typing", 0x0000 for message */ | |
178 | if (msg) | |
179 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
180 | else | |
181 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x000e); | |
182 | ||
183 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
184 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
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); | |
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); | |
193 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
194 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
195 | i += aimutil_put16(newpacket->hdr.oft.hdr2+i, 0x0000); | |
196 | ||
197 | /* end of hdr2 */ | |
198 | ||
199 | if (msg) { | |
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); | |
209 | if(!(newpacket->data = strdup(msg))) | |
210 | return -1; | |
211 | } | |
212 | newpacket->lock = 0; | |
213 | aim_tx_enqueue(sess, newpacket); | |
214 | return 0; | |
215 | } | |
216 | ||
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 | /** | |
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, | |
225 | * @priv: a dummy priv value (we'll let it get filled in later) (if you pass a %NULL, we alloc one) | |
226 | * @destsn: the SN to connect to. | |
227 | * | |
228 | */ | |
229 | faim_export struct aim_conn_t *aim_directim_initiate(struct aim_session_t *sess, | |
230 | struct aim_conn_t *conn, | |
231 | struct aim_directim_priv *priv, | |
232 | char *destsn) | |
233 | { | |
234 | ||
235 | struct command_tx_struct *newpacket; | |
236 | struct aim_conn_t *newconn; | |
237 | struct aim_msgcookie_t *cookie; | |
238 | int curbyte, i, listenfd; | |
239 | short port = 4443; | |
240 | struct hostent *hptr; | |
241 | char localhost[129]; | |
242 | unsigned char cap[16]; | |
243 | char d[4]; /* IPv6 is a bit bigger... */ | |
244 | ||
245 | /* XXX: TLVlist-ize this */ | |
246 | ||
247 | /* Open our socket */ | |
248 | ||
249 | if ( (listenfd = aim_listenestablish(port)) == -1) | |
250 | return NULL; | |
251 | ||
252 | /* get our local IP */ | |
253 | /* XXX if available, use getaddrinfo() */ | |
254 | /* XXX allow client to specify which IP to use for multihomed boxes */ | |
255 | if (gethostname(localhost, 128) < 0) | |
256 | return NULL; | |
257 | if ( (hptr = gethostbyname(localhost)) == NULL) | |
258 | return NULL; | |
259 | memcpy(&d, hptr->h_addr_list[0], 4); | |
260 | ||
261 | aim_putcap(cap, 16, AIM_CAPS_IMIMAGE); | |
262 | ||
263 | /* create the OSCAR packet */ | |
264 | ||
265 | if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x32))) | |
266 | return NULL; | |
267 | newpacket->lock = 1; | |
268 | ||
269 | curbyte = 0; | |
270 | curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid); | |
271 | ||
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++) | |
275 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) rand() % 20)); | |
276 | ||
277 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
278 | ||
279 | /* grab all the data for cookie caching */ | |
280 | cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)); | |
281 | memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); | |
282 | cookie->type = AIM_COOKIETYPE_OFTIM; | |
283 | priv = cookie->data; | |
284 | ||
285 | if (!priv) | |
286 | priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)); | |
287 | ||
288 | memcpy(priv->cookie, cookie, 8); | |
289 | memcpy(priv->sn, destsn, sizeof(priv->sn)); | |
290 | cookie->data = priv; | |
291 | aim_cachecookie(sess, cookie); | |
292 | ||
293 | /* Channel ID */ | |
294 | curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); | |
295 | ||
296 | /* Destination SN (prepended with byte length)*/ | |
297 | curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); | |
298 | curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); | |
299 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
300 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
301 | ||
302 | /* enTLV start */ | |
303 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); | |
304 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0032); | |
305 | ||
306 | /* Flag data / ICBM Parameters */ | |
307 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
308 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
309 | ||
310 | /* Cookie */ | |
311 | curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); | |
312 | ||
313 | /*Capability String */ | |
314 | curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); | |
315 | ||
316 | /* 000a/0002 : 0001 */ | |
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 | ||
321 | /* 0003/0004: IP address */ | |
322 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
323 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); | |
324 | for(i = 0;i < 4; i++) | |
325 | curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); | |
326 | ||
327 | /* 0005/0002: Port */ | |
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 | ||
332 | /* 000f/0000: ?? */ | |
333 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); | |
334 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
335 | newpacket->commandlen = curbyte; | |
336 | newpacket->lock = 0; | |
337 | aim_tx_enqueue(sess, newpacket); | |
338 | ||
339 | ||
340 | /* XXX switch to aim_cloneconn()? */ | |
341 | if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL))) | |
342 | return NULL; | |
343 | ||
344 | newconn->fd = listenfd; | |
345 | newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
346 | newconn->priv = priv; | |
347 | newconn->lastactivity = time(NULL); | |
348 | ||
349 | faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd); | |
350 | ||
351 | return newconn; | |
352 | } | |
353 | ||
354 | /** | |
355 | * unsigned int aim_oft_listener_clean - close up old listeners | |
356 | * @sess: session to clean up in | |
357 | * @age: maximum age in seconds | |
358 | * | |
359 | * returns number closed, -1 on error. | |
360 | */ | |
361 | faim_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 | ||
367 | if (!sess) | |
368 | return -1; | |
369 | now = time(NULL); | |
370 | faim_mutex_lock(&sess->connlistlock); | |
371 | for(cur = sess->connlist;cur; cur = cur->next) | |
372 | if (cur->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) { | |
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 | } | |
384 | ||
385 | /** | |
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 | */ | |
393 | faim_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; | |
396 | ||
397 | if (!sess || !conn || !priv) | |
398 | return NULL; | |
399 | ||
400 | /* XXX verify that non-blocking connects actually work */ | |
401 | newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, priv->ip); | |
402 | if (!newconn || (newconn->fd == -1)) { | |
403 | faimdprintf(sess, 2, "could not connect to %s\n", priv->ip); | |
404 | perror("aim_newconn"); | |
405 | return newconn; | |
406 | } | |
407 | ||
408 | newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
409 | newconn->priv = priv; | |
410 | faimdprintf(sess, 2, "faim: connected to peer (fd = %d)\n", newconn->fd); | |
411 | ||
412 | return newconn; | |
413 | } | |
414 | ||
415 | /** | |
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 | * | |
422 | */ | |
423 | faim_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; | |
427 | ||
428 | if (!sess || !name) | |
429 | return NULL; | |
430 | ||
431 | faim_mutex_lock(&sess->connlistlock); | |
432 | ||
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; | |
436 | priv = cur->priv; | |
437 | if (aim_sncmp(priv->sn, name) == 0) | |
438 | break; | |
439 | } faim_mutex_unlock(&sess->connlistlock); | |
440 | return cur; | |
441 | } | |
442 | ||
443 | /** | |
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) | |
455 | * | |
456 | * Returns new connection or %NULL on error. | |
457 | */ | |
458 | faim_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 | { | |
468 | struct command_tx_struct *newpacket, *newoft; | |
469 | struct aim_conn_t *newconn; | |
470 | struct aim_fileheader_t *fh; | |
471 | struct aim_filetransfer_priv *priv; | |
472 | struct aim_msgcookie_t *cachedcook; | |
473 | int curbyte, i; | |
474 | ||
475 | if (!sess || !conn || !sn || !cookie || !ip) { | |
476 | return NULL; | |
477 | } | |
478 | ||
479 | newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, ip); | |
480 | ||
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; | |
499 | ||
500 | faimdprintf(sess, 2, "faim: getfile request accept\n"); | |
501 | ||
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 */ | |
505 | return NULL; | |
506 | } | |
507 | ||
508 | newoft->lock = 1; | |
509 | memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
510 | newoft->hdr.oft.hdr2len = 0x100 - 8; | |
511 | ||
512 | if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) { | |
513 | /* XXX: conn leak here */ | |
514 | perror("calloc"); | |
515 | return NULL; | |
516 | } | |
517 | ||
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); | |
551 | /* XXX: conn leak */ | |
552 | perror("calloc (1)"); | |
553 | return NULL; | |
554 | } | |
555 | ||
556 | memcpy(fh->bcookie, cookie, 8); | |
557 | ||
558 | if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, fh))) | |
559 | faimdprintf(sess, 1, "eek, bh fail!\n"); | |
560 | ||
561 | newoft->lock = 0; | |
562 | aim_tx_enqueue(sess, newoft); | |
563 | ||
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)"); | |
568 | return NULL; | |
569 | } | |
570 | ||
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; | |
576 | ||
577 | if (aim_cachecookie(sess, cachedcook) == -1) | |
578 | faimdprintf(sess, 1, "faim: ERROR caching message cookie\n"); | |
579 | ||
580 | free(fh); | |
581 | ||
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); | |
593 | ||
594 | for (i = 0; i < 8; i++) | |
595 | curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]); | |
596 | ||
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*/); | |
603 | ||
604 | for (i = 0;i < 8; i++) | |
605 | curbyte += aimutil_put8(newpacket->data+curbyte, cookie[i]); | |
606 | ||
607 | curbyte += aim_putcap(newpacket->data+curbyte, 0x10, rendid); | |
608 | newpacket->lock = 0; | |
609 | aim_tx_enqueue(sess, newpacket); | |
610 | ||
611 | return newconn; | |
612 | } | |
613 | ||
614 | /** | |
615 | * aim_getlisting(FILE *file) -- get an aim_fileheader_t for a given FILE* | |
616 | * @file is an opened listing file | |
617 | * | |
618 | * returns a pointer to the filled-in fileheader_t | |
619 | * | |
620 | * Currently omits checksum. we'll fix this when AOL breaks us, i | |
621 | * guess. | |
622 | * | |
623 | */ | |
624 | ||
625 | faim_export struct aim_fileheader_t *aim_getlisting(struct aim_session_t *sess, FILE *file) | |
626 | { | |
627 | struct aim_fileheader_t *fh; | |
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. */ | |
635 | if ( (linebuf = (char *)calloc(1, linelength)) == NULL ) { | |
636 | faimdprintf(sess, 2, "linebuf calloc failed\n"); | |
637 | return NULL; | |
638 | } | |
639 | ||
640 | if (fseek(file, 0, SEEK_END) == -1) { /* use this for sanity check */ | |
641 | perror("getlisting END1 fseek:"); | |
642 | faimdprintf(sess, 2, "getlising fseek END1 error\n"); | |
643 | } | |
644 | ||
645 | if ((size = ftell(file)) == -1) { | |
646 | perror("getlisting END1 getpos:"); | |
647 | faimdprintf(sess, 2, "getlising getpos END1 error\n"); | |
648 | } | |
649 | ||
650 | if (fseek(file, 0, SEEK_SET) != 0) { | |
651 | perror("getlesting fseek(SET):"); | |
652 | faimdprintf(sess, 2, "faim: getlisting: couldn't seek to beginning of listing file\n"); | |
653 | } | |
654 | ||
655 | memset(linebuf, 0, linelength); | |
656 | ||
657 | size = 0; | |
658 | ||
659 | while(fgets(linebuf, linelength, file)) { | |
660 | totfiles++; | |
661 | memset(sizebuf, 0, 9); | |
662 | ||
663 | size += strlen(linebuf); | |
664 | ||
665 | if (strlen(linebuf) < 23) { | |
666 | faimdprintf(sess, 2, "line \"%s\" too short. skipping\n", linebuf); | |
667 | continue; | |
668 | } | |
669 | if (linebuf[strlen(linebuf)-1] != '\n') { | |
670 | faimdprintf(sess, 2, "faim: OFT: getlisting -- hit EOF or line too long!\n"); | |
671 | } | |
672 | ||
673 | memcpy(sizebuf, linebuf+17, 8); | |
674 | ||
675 | totsize += strtol(sizebuf, NULL, 10); | |
676 | memset(linebuf, 0, linelength); | |
677 | } | |
678 | ||
679 | if (fseek(file, 0, SEEK_SET) == -1) { | |
680 | perror("getlisting END2 fseek:"); | |
681 | faimdprintf(sess, 2, "getlising fseek END2 error\n"); | |
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. */ | |
689 | ||
690 | if (!(fh = (struct aim_fileheader_t*)calloc(1, sizeof(struct aim_fileheader_t)))) | |
691 | return NULL; | |
692 | ||
693 | fh->encrypt = 0x0000; | |
694 | fh->compress = 0x0000; | |
695 | fh->totfiles = totfiles; | |
696 | fh->filesleft = totfiles; /* is this right ?*/ | |
697 | fh->totparts = 0x0001; | |
698 | fh->partsleft = 0x0001; | |
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 ! */ | |
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 | ||
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)); | |
713 | ||
714 | fh->flags = 0x02; | |
715 | fh->lnameoffset = 0x1a; | |
716 | fh->lsizeoffset = 0x10; | |
717 | ||
718 | /* memset(fh->dummy, 0, sizeof(fh->dummy)); */ | |
719 | memset(fh->macfileinfo, 0, sizeof(fh->macfileinfo)); | |
720 | ||
721 | fh->nencode = 0x0000; /* we need to figure out these encodings for filenames */ | |
722 | fh->nlanguage = 0x0000; | |
723 | ||
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)); | |
727 | ||
728 | faimdprintf(sess, 2, "faim: OFT: listing fh name %s / %s\n", fh->name, (fh->name+(strlen(fh->name)))); | |
729 | return fh; | |
730 | } | |
731 | ||
732 | /** | |
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 | * | |
738 | */ | |
739 | faim_export int aim_listenestablish(u_short portnum) | |
740 | { | |
741 | #if defined(__linux__) | |
742 | /* XXX what other OS's support getaddrinfo? */ | |
743 | int listenfd; | |
744 | const int on = 1; | |
745 | struct addrinfo hints, *res, *ressave; | |
746 | char serv[5]; | |
747 | ||
748 | snprintf(serv, sizeof(serv), "%d", portnum); | |
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; | |
753 | if (getaddrinfo(NULL /*any IP*/, serv, &hints, &res) != 0) { | |
754 | perror("getaddrinfo"); | |
755 | return -1; | |
756 | } | |
757 | ressave = res; | |
758 | do { | |
759 | listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
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) | |
764 | break; | |
765 | /* success */ | |
766 | close(listenfd); | |
767 | } while ( (res = res->ai_next) ); | |
768 | ||
769 | if (!res) | |
770 | return -1; | |
771 | ||
772 | if (listen(listenfd, 1024)!=0) { | |
773 | perror("listen"); | |
774 | return -1; | |
775 | } | |
776 | ||
777 | freeaddrinfo(ressave); | |
778 | return listenfd; | |
779 | #else | |
780 | int listenfd; | |
781 | const int on = 1; | |
782 | struct sockaddr_in sockin; | |
783 | ||
784 | if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | |
785 | perror("socket(listenfd)"); | |
786 | return -1; | |
787 | } | |
788 | ||
789 | if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on) != 0)) { | |
790 | perror("setsockopt(listenfd)"); | |
791 | close(listenfd); | |
792 | return -1; | |
793 | } | |
794 | ||
795 | memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
796 | sockin.sin_family = AF_INET; | |
797 | sockin.sin_port = htons(portnum); | |
798 | ||
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 | } | |
809 | return listenfd; | |
810 | #endif | |
811 | } | |
812 | ||
813 | /** | |
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 | */ | |
821 | faim_internal int aim_get_command_rendezvous(struct aim_session_t *sess, struct aim_conn_t *conn) | |
822 | { | |
823 | unsigned char hdrbuf1[6]; | |
824 | unsigned char *hdr = NULL; | |
825 | int hdrlen, hdrtype; | |
826 | int flags = 0; | |
827 | rxcallback_t userfunc = NULL; | |
828 | ||
829 | if (!sess || !conn || !conn->priv) | |
830 | return -1; | |
831 | ||
832 | memset(hdrbuf1, 0, sizeof(hdrbuf1)); | |
833 | faim_mutex_lock(&conn->active); | |
834 | ||
835 | /* gets locked down for the entirety */ | |
836 | ||
837 | if (conn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE ) { | |
838 | struct aim_filetransfer_priv *ft; | |
839 | ft = conn->priv; | |
840 | if (ft->state == 2) { | |
841 | /* waiting on listing data */ | |
842 | int ret = 0; | |
843 | char *listing; | |
844 | struct command_tx_struct *newoft; | |
845 | if (!(listing = malloc(ft->fh.size))) { | |
846 | faim_mutex_unlock(&conn->active); | |
847 | return -1; | |
848 | } | |
849 | ||
850 | ft->state = 0; | |
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); | |
853 | ||
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"); | |
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 | ||
867 | /* Protocol BS - set nrecvd to size of listing, recvcsum to | |
868 | listing checksum, flags to 0 */ | |
869 | ||
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 | ||
882 | if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) | |
883 | faimdprintf(sess, 2, "eek! bh fail listing\n"); | |
884 | ||
885 | /* send the 120b */ | |
886 | newoft->lock = 0; | |
887 | aim_tx_enqueue(sess, newoft); | |
888 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTING)) ) | |
889 | ret = userfunc(sess, NULL, conn, ft, listing); | |
890 | ||
891 | faim_mutex_unlock(&conn->active); | |
892 | free(listing); | |
893 | return ret; | |
894 | } | |
895 | if (ft->state == 3) { | |
896 | /* waiting on file data */ | |
897 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILERECEIVE)) ) { | |
898 | faim_mutex_unlock(&conn->active); | |
899 | return userfunc(sess, NULL, conn, ft); | |
900 | } | |
901 | faim_mutex_unlock(&conn->active); | |
902 | return 0; | |
903 | } | |
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 | } | |
913 | } | |
914 | ||
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", | |
917 | conn->fd, hdrbuf1[0],hdrbuf1[1],hdrbuf1[2],hdrbuf1[3],hdrbuf1[4],hdrbuf1[5],hdrlen); | |
918 | faim_mutex_unlock(&conn->active); | |
919 | if (hdrlen < 0) | |
920 | perror("read"); | |
921 | else { /* disconnected */ | |
922 | char *screenname = NULL; | |
923 | int ret; | |
924 | struct aim_msgcookie_t *cook; | |
925 | ||
926 | switch(conn->subtype) { | |
927 | case AIM_CONN_SUBTYPE_OFT_DIRECTIM: { | |
928 | struct aim_directim_priv *priv = NULL; | |
929 | if (!(priv = (struct aim_directim_priv *)conn->priv) ) | |
930 | return -1; | |
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)) ) { | |
937 | aim_conn_close(conn); | |
938 | ret = userfunc(sess, NULL, conn, screenname); | |
939 | free(screenname); | |
940 | return ret; | |
941 | } | |
942 | break; | |
943 | } | |
944 | case AIM_CONN_SUBTYPE_OFT_GETFILE: { | |
945 | struct aim_filetransfer_priv *priv; | |
946 | if (!(priv = (struct aim_filetransfer_priv *)conn->priv)) | |
947 | return -1; | |
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)) ) { | |
955 | aim_conn_close(conn); | |
956 | ret = userfunc(sess, NULL, conn, screenname); | |
957 | free(screenname); | |
958 | return ret; | |
959 | } | |
960 | break; | |
961 | } | |
962 | case AIM_CONN_SUBTYPE_OFT_SENDFILE: { | |
963 | struct aim_filetransfer_priv *priv; | |
964 | if (!(priv = (struct aim_filetransfer_priv *)conn->priv)) | |
965 | return -1; | |
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)) ) { | |
972 | aim_conn_close(conn); | |
973 | ret = userfunc(sess, NULL, conn, screenname); | |
974 | free(screenname); | |
975 | return ret; | |
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"); | |
996 | faimdprintf(sess, 2,"faim: rend: read2 error on %d (%d)\n", conn->fd, hdrlen); | |
997 | free(hdr); | |
998 | faim_mutex_unlock(&conn->active); | |
999 | aim_conn_close(conn); | |
1000 | return -1; | |
1001 | } | |
1002 | hdrtype = aimutil_get16(hdr); | |
1003 | ||
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 | ||
1011 | if (!(priv = (struct aim_directim_priv *)calloc(1, sizeof(struct aim_directim_priv)))) { | |
1012 | faim_mutex_unlock(&conn->active); | |
1013 | free(hdr); | |
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 | ||
1022 | faimdprintf(sess, 2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, snptr); | |
1023 | ||
1024 | free(hdr); | |
1025 | hdr = NULL; | |
1026 | ||
1027 | if (flags == 0x000e) { | |
1028 | faim_mutex_unlock(&conn->active); | |
1029 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING)) ) | |
1030 | return userfunc(sess, NULL, snptr); | |
1031 | } else { | |
1032 | ||
1033 | if ((flags == 0x0000) && payloadlength) { | |
1034 | unsigned char *msg; | |
1035 | ||
1036 | if (!(msg = calloc(1, payloadlength+1))) { | |
1037 | faim_mutex_unlock(&conn->active); | |
1038 | return -1; | |
1039 | } | |
1040 | ||
1041 | if (aim_recv(conn->fd, msg, payloadlength) < payloadlength) { | |
1042 | perror("read"); | |
1043 | faimdprintf(sess, 2,"faim: rend: read3 error\n"); | |
1044 | free(msg); | |
1045 | faim_mutex_unlock(&conn->active); | |
1046 | aim_conn_close(conn); | |
1047 | return -1; | |
1048 | } | |
1049 | ||
1050 | faim_mutex_unlock(&conn->active); | |
1051 | msg[payloadlength] = 0x00; | |
1052 | faimdprintf(sess, 2, "faim: directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg); | |
1053 | ||
1054 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) ) | |
1055 | i = userfunc(sess, NULL, conn, snptr, msg); | |
1056 | else { | |
1057 | faimdprintf(sess, 0, "directim: %s/%04x/%04x/%s\n", snptr, payloadlength, flags, msg); | |
1058 | i = 1; | |
1059 | } | |
1060 | ||
1061 | free(msg); | |
1062 | ||
1063 | return i; | |
1064 | } | |
1065 | } | |
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; | |
1073 | ||
1074 | faimdprintf(sess, 2,"faim: rend: fileget 0x1108\n"); | |
1075 | fh = aim_oft_getfh(hdr); | |
1076 | ||
1077 | free(hdr); | |
1078 | hdr = NULL; | |
1079 | ||
1080 | faim_mutex_unlock(&conn->active); | |
1081 | ||
1082 | if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { | |
1083 | faim_mutex_unlock(&conn->active); | |
1084 | free(fh); | |
1085 | return -1; | |
1086 | } | |
1087 | ||
1088 | ft = cook->data; | |
1089 | ||
1090 | /* we're waaaaiiiting.. for listing.txt */ | |
1091 | ft->state = 2; | |
1092 | ||
1093 | memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); | |
1094 | free(fh); | |
1095 | ||
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))) { | |
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 | ||
1115 | if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
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; | |
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; | |
1140 | ||
1141 | faim_mutex_unlock(&conn->active); | |
1142 | ||
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", | |
1145 | AIM_COOKIETYPE_OFTGET, fh->bcookie); | |
1146 | ||
1147 | ft = cook->data; | |
1148 | ||
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", | |
1151 | ft->fh.size, fh->size); | |
1152 | ||
1153 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILELISTINGREQ))) | |
1154 | ret = userfunc(sess, NULL, conn, fh); | |
1155 | ||
1156 | faimdprintf(sess, 2, "faim: get_command_rendezvous: hit end of 1209\n"); | |
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); | |
1170 | ||
1171 | free(hdr); | |
1172 | hdr = NULL; | |
1173 | ||
1174 | faim_mutex_unlock(&conn->active); | |
1175 | ||
1176 | if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { | |
1177 | free(fh); | |
1178 | return -1; | |
1179 | } | |
1180 | ||
1181 | free(fh); | |
1182 | ||
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 | ||
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; | |
1199 | int i = 0; | |
1200 | ||
1201 | fh = aim_oft_getfh(hdr); | |
1202 | ||
1203 | free(hdr); | |
1204 | hdr = NULL; | |
1205 | ||
1206 | faim_mutex_unlock(&conn->active); | |
1207 | ||
1208 | if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { | |
1209 | faimdprintf(sess, 2, "no cookie in 120c\n"); | |
1210 | return -1; | |
1211 | } | |
1212 | ||
1213 | ft = cook->data; | |
1214 | memcpy(&(ft->fh), fh, sizeof(struct aim_fileheader_t)); | |
1215 | free(fh); | |
1216 | ||
1217 | aim_cachecookie(sess, cook); | |
1218 | ||
1219 | faimdprintf(sess, 2, "faim: fileget: %s seems to want %s\n", ft->sn, ft->fh.name); | |
1220 | ||
1221 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) ) | |
1222 | i = userfunc(sess, NULL, conn, &(ft->fh), cook->cookie); | |
1223 | ||
1224 | if (i < 0) | |
1225 | return i; | |
1226 | ||
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"); | |
1229 | return -1; | |
1230 | } | |
1231 | ||
1232 | newoft->lock = 1; | |
1233 | memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1234 | newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1235 | ||
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 | } | |
1241 | ||
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; | |
1246 | ||
1247 | aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)); | |
1248 | ||
1249 | newoft->lock = 0; | |
1250 | aim_tx_enqueue(sess, newoft); | |
1251 | ||
1252 | faimdprintf(sess, 2, "faim: OFT: OFT file header enqueued.\n"); | |
1253 | ||
1254 | return i; | |
1255 | ||
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; | |
1263 | ||
1264 | fh = aim_oft_getfh(hdr); | |
1265 | ||
1266 | free(hdr); | |
1267 | hdr = NULL; | |
1268 | ||
1269 | faim_mutex_unlock(&conn->active); | |
1270 | ||
1271 | if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { | |
1272 | free(fh); | |
1273 | return -1; | |
1274 | } | |
1275 | free(fh); | |
1276 | ||
1277 | ft = cook->data; | |
1278 | ||
1279 | ft->state = 3; | |
1280 | ||
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); | |
1287 | ||
1288 | if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) { | |
1289 | aim_conn_close(conn); | |
1290 | faimdprintf(sess, 2, "faim: send_final_transfer: tx_new OFT failed\n"); | |
1291 | return -1; | |
1292 | } | |
1293 | ||
1294 | newoft->lock = 1; | |
1295 | memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1296 | ||
1297 | newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1298 | ||
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 | } | |
1304 | ||
1305 | aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)); | |
1306 | ||
1307 | newoft->lock = 0; | |
1308 | aim_tx_enqueue(sess, newoft); | |
1309 | ||
1310 | faimdprintf(sess, 2, "faim: OFT: OFT 0x0202 enqueued.\n"); | |
1311 | ||
1312 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILEREQ)) == NULL) | |
1313 | return 1; | |
1314 | ||
1315 | break; | |
1316 | } | |
1317 | case 0x0202: { /* get file: ready to receive data */ | |
1318 | struct aim_fileheader_t *fh; | |
1319 | struct aim_filetransfer_priv *ft; | |
1320 | struct aim_msgcookie_t *cook; | |
1321 | int ret = 1; | |
1322 | ||
1323 | fh = aim_oft_getfh(hdr); | |
1324 | ||
1325 | free(hdr); | |
1326 | hdr = NULL; | |
1327 | ||
1328 | faim_mutex_unlock(&conn->active); | |
1329 | ||
1330 | if (!(cook = aim_checkcookie(sess, fh->bcookie, AIM_COOKIETYPE_OFTGET))) { | |
1331 | free(fh); | |
1332 | return -1; | |
1333 | } | |
1334 | ||
1335 | ft = cook->data; | |
1336 | ||
1337 | faimdprintf(sess, 2, "faim: get_rend: looks like we're ready to send data.(oft 0x0202)\n"); | |
1338 | ||
1339 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILEFILESEND)) ) | |
1340 | ret = userfunc(sess, NULL, conn, fh); | |
1341 | ||
1342 | free(fh); | |
1343 | ||
1344 | return ret; | |
1345 | break; | |
1346 | } | |
1347 | case 0x0204: { /* get file: finished. close it up */ | |
1348 | int i; | |
1349 | struct aim_fileheader_t *fh; | |
1350 | ||
1351 | fh = aim_oft_getfh(hdr); | |
1352 | ||
1353 | free(hdr); | |
1354 | hdr = NULL; | |
1355 | ||
1356 | faim_mutex_unlock(&conn->active); | |
1357 | ||
1358 | faimdprintf(sess, 2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n"); | |
1359 | ||
1360 | if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) ) | |
1361 | i = userfunc(sess, NULL, conn, fh); | |
1362 | else | |
1363 | i = 1; | |
1364 | ||
1365 | if (conn) | |
1366 | aim_conn_close(conn); | |
1367 | ||
1368 | free(fh); | |
1369 | ||
1370 | return i; | |
1371 | break; | |
1372 | } | |
1373 | default: { | |
1374 | free(hdr); | |
1375 | hdr = NULL; | |
1376 | faimdprintf(sess, 2,"faim: OFT frame: uknown type %04x\n", hdrtype); | |
1377 | faim_mutex_unlock(&conn->active); | |
1378 | break; | |
1379 | } | |
1380 | } /* switch */ | |
1381 | if (hdr) { | |
1382 | faimdprintf(sess, 0, "hdr wasn't freed by a rendezvous switch case (hdrtype: %0x04x)!\n", hdrtype); | |
1383 | free(hdr); | |
1384 | hdr = NULL; | |
1385 | } | |
1386 | return 0; | |
1387 | } | |
1388 | ||
1389 | /** | |
1390 | * aim_oft_getfh - extracts an &aim_fileheader_t from buffer hdr. | |
1391 | * @hdr: buffer to extract header from | |
1392 | * | |
1393 | * returns pointer to new struct on success; %NULL on error. | |
1394 | * | |
1395 | */ | |
1396 | static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr) | |
1397 | { | |
1398 | struct aim_fileheader_t *fh; | |
1399 | int i, j; | |
1400 | if (!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) | |
1401 | return NULL; | |
1402 | ||
1403 | /* [0] and [1] are the type. we can ignore those here. */ | |
1404 | i = 2; | |
1405 | for(j = 0; j < 8; j++, i++) | |
1406 | fh->bcookie[j] = hdr[i]; | |
1407 | fh->encrypt = aimutil_get16(hdr+i); | |
1408 | i += 2; | |
1409 | fh->compress = aimutil_get16(hdr+i); | |
1410 | i += 2; | |
1411 | fh->totfiles = aimutil_get16(hdr+i); | |
1412 | i += 2; | |
1413 | fh->filesleft = aimutil_get16(hdr+i); | |
1414 | i += 2; | |
1415 | fh->totparts = aimutil_get16(hdr+i); | |
1416 | i += 2; | |
1417 | fh->partsleft = aimutil_get16(hdr+i); | |
1418 | i += 2; | |
1419 | fh->totsize = aimutil_get32(hdr+i); | |
1420 | i += 4; | |
1421 | fh->size = aimutil_get32(hdr+i); | |
1422 | i += 4; | |
1423 | fh->modtime = aimutil_get32(hdr+i); | |
1424 | i += 4; | |
1425 | fh->checksum = aimutil_get32(hdr+i); | |
1426 | i += 4; | |
1427 | fh->rfrcsum = aimutil_get32(hdr+i); | |
1428 | i += 4; | |
1429 | fh->rfsize = aimutil_get32(hdr+i); | |
1430 | i += 4; | |
1431 | fh->cretime = aimutil_get32(hdr+i); | |
1432 | i += 4; | |
1433 | fh->rfcsum = aimutil_get32(hdr+i); | |
1434 | i += 4; | |
1435 | fh->nrecvd = aimutil_get32(hdr+i); | |
1436 | i += 4; | |
1437 | fh->recvcsum = aimutil_get32(hdr+i); | |
1438 | i += 4; | |
1439 | memcpy(fh->idstring, hdr+i, 32); | |
1440 | i += 32; | |
1441 | fh->flags = aimutil_get8(hdr+i); | |
1442 | i += 1; | |
1443 | fh->lnameoffset = aimutil_get8(hdr+i); | |
1444 | i += 1; | |
1445 | fh->lsizeoffset = aimutil_get8(hdr+i); | |
1446 | i += 1; | |
1447 | memcpy(fh->dummy, hdr+i, 69); | |
1448 | i += 69; | |
1449 | memcpy(fh->macfileinfo, hdr+i, 16); | |
1450 | i += 16; | |
1451 | fh->nencode = aimutil_get16(hdr+i); | |
1452 | i += 2; | |
1453 | fh->nlanguage = aimutil_get16(hdr+i); | |
1454 | i += 2; | |
1455 | memcpy(fh->name, hdr+i, 64); | |
1456 | i += 64; | |
1457 | return fh; | |
1458 | } | |
1459 | ||
1460 | /** | |
1461 | * aim_oft_checksum - calculate oft checksum of buffer | |
1462 | * @buffer: buffer of data to checksum | |
1463 | * @bufsize: size of buffer | |
1464 | * @checksum: pointer to integer to place result in (pointer!) | |
1465 | * | |
1466 | * | |
1467 | * Note that checksum is a pointer. Checksum should be filled with | |
1468 | * 0xFFFF0000 for each new file; you can have this checksum chunks of | |
1469 | * files in series if you just call it repeatedly in a for(; ; ) loop | |
1470 | * and don't reset the checksum between each call. And you thought we | |
1471 | * didn't care about you and your pathetic client's meomry footprint | |
1472 | * ;^) | |
1473 | * | |
1474 | * | |
1475 | * Also, it's been said that this is incorrect as currently | |
1476 | * written. You were warned. | |
1477 | */ | |
1478 | faim_export int aim_oft_checksum(struct aim_session_t *sess, char *buffer, int bufsize, int *checksum) | |
1479 | { | |
1480 | short check0, check1; | |
1481 | int i; | |
1482 | check0 = ((*checksum & 0xFF000000) >> 16); | |
1483 | check1 = ((*checksum & 0x00ff0000) >> 16); | |
1484 | for(i = 0; i < bufsize; i++) { | |
1485 | if (i % 2) { /* use check1 -- second byte */ | |
1486 | if ( (short)buffer[i] > check1 ) { /* wrapping */ | |
1487 | check1 += 0x100; /* this is a cheap way to wrap */ | |
1488 | ||
1489 | /* if we're wrapping, decrement the other one */ | |
1490 | /* XXX: check this corner case */ | |
1491 | if (check0 == 0) | |
1492 | check0 = 0x00ff; | |
1493 | else | |
1494 | check0--; | |
1495 | } | |
1496 | check1 -= buffer[i]; | |
1497 | } else { /* use check0 -- first byte */ | |
1498 | if ( (short)buffer[i] > check0 ) { /* wrapping */ | |
1499 | check0 += 0x100; /* this is a cheap way to wrap */ | |
1500 | ||
1501 | /* if we're wrapping, decrement the other one */ | |
1502 | /* XXX: check this corner case */ | |
1503 | if (check1 == 0) | |
1504 | check1 = 0x00ff; | |
1505 | else | |
1506 | check1--; | |
1507 | } | |
1508 | check0 -= buffer[i]; | |
1509 | } | |
1510 | } | |
1511 | ||
1512 | if (check0 > 0xff || check1 > 0xff) { | |
1513 | /* they shouldn't be able to do this. error! */ | |
1514 | faimdprintf(sess, 2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1); | |
1515 | return -1; | |
1516 | } | |
1517 | ||
1518 | /* grab just the lowest byte; this should be clean, but just in | |
1519 | case */ | |
1520 | check0 &= 0xff; | |
1521 | check1 &= 0xff; | |
1522 | ||
1523 | *checksum = ((check0 * 0x1000000) + (check1 * 0x10000)); | |
1524 | return *checksum; | |
1525 | } | |
1526 | ||
1527 | /** | |
1528 | * aim_oft_buildheader - fills a buffer with network-order fh data | |
1529 | * @dest: buffer to fill -- pre-alloced | |
1530 | * @fh: fh to get data from | |
1531 | * | |
1532 | * returns length written; -1 on error. | |
1533 | * DOES NOT DO BOUNDS CHECKING! | |
1534 | * | |
1535 | */ | |
1536 | faim_internal int aim_oft_buildheader(unsigned char *dest,struct aim_fileheader_t *fh) | |
1537 | { | |
1538 | int i, curbyte; | |
1539 | if (!dest || !fh) | |
1540 | return -1; | |
1541 | curbyte = 0; | |
1542 | for(i = 0; i < 8; i++) | |
1543 | curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]); | |
1544 | curbyte += aimutil_put16(dest+curbyte, fh->encrypt); | |
1545 | curbyte += aimutil_put16(dest+curbyte, fh->compress); | |
1546 | curbyte += aimutil_put16(dest+curbyte, fh->totfiles); | |
1547 | curbyte += aimutil_put16(dest+curbyte, fh->filesleft); | |
1548 | curbyte += aimutil_put16(dest+curbyte, fh->totparts); | |
1549 | curbyte += aimutil_put16(dest+curbyte, fh->partsleft); | |
1550 | curbyte += aimutil_put32(dest+curbyte, fh->totsize); | |
1551 | curbyte += aimutil_put32(dest+curbyte, fh->size); | |
1552 | curbyte += aimutil_put32(dest+curbyte, fh->modtime); | |
1553 | curbyte += aimutil_put32(dest+curbyte, fh->checksum); | |
1554 | curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum); | |
1555 | curbyte += aimutil_put32(dest+curbyte, fh->rfsize); | |
1556 | curbyte += aimutil_put32(dest+curbyte, fh->cretime); | |
1557 | curbyte += aimutil_put32(dest+curbyte, fh->rfcsum); | |
1558 | curbyte += aimutil_put32(dest+curbyte, fh->nrecvd); | |
1559 | curbyte += aimutil_put32(dest+curbyte, fh->recvcsum); | |
1560 | memcpy(dest+curbyte, fh->idstring, 32); | |
1561 | curbyte += 32; | |
1562 | curbyte += aimutil_put8(dest+curbyte, fh->flags); | |
1563 | curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset); | |
1564 | curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset); | |
1565 | memcpy(dest+curbyte, fh->dummy, 69); | |
1566 | curbyte += 69; | |
1567 | memcpy(dest+curbyte, fh->macfileinfo, 16); | |
1568 | curbyte += 16; | |
1569 | curbyte += aimutil_put16(dest+curbyte, fh->nencode); | |
1570 | curbyte += aimutil_put16(dest+curbyte, fh->nlanguage); | |
1571 | memset(dest+curbyte, 0x00, 64); | |
1572 | memcpy(dest+curbyte, fh->name, 64); | |
1573 | ||
1574 | /* XXX: Filenames longer than 64B */ | |
1575 | curbyte += 64; | |
1576 | return curbyte; | |
1577 | } | |
1578 | ||
1579 | ||
1580 | /** | |
1581 | * aim_tx_destroy - free's tx_command_t's | |
1582 | * @command: the command to free | |
1583 | * | |
1584 | * if command is locked, doesn't free. | |
1585 | * returns -1 on error (locked struct); 0 on success. | |
1586 | * | |
1587 | */ | |
1588 | faim_internal int aim_tx_destroy(struct command_tx_struct *command){ | |
1589 | if (command->lock) | |
1590 | return -1; | |
1591 | if (command->data) | |
1592 | free(command->data); | |
1593 | if (command->hdrtype == AIM_FRAMETYPE_OFT && command->hdr.oft.hdr2) | |
1594 | free(command->hdr.oft.hdr2); | |
1595 | free(command); | |
1596 | return 0; | |
1597 | } | |
1598 | ||
1599 | /** | |
1600 | * aim_getfile_intitiate - Request an OFT getfile session | |
1601 | * @sess: your session, | |
1602 | * @conn: the BOS conn, | |
1603 | * @destsn is the SN to connect to. | |
1604 | * | |
1605 | * returns a new &aim_conn_t on success, %NULL on error | |
1606 | */ | |
1607 | faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn) | |
1608 | { | |
1609 | struct command_tx_struct *newpacket; | |
1610 | struct aim_conn_t *newconn; | |
1611 | struct aim_filetransfer_priv *priv; | |
1612 | struct aim_msgcookie_t *cookie; | |
1613 | int curbyte, i, listenfd; | |
1614 | short port = 4443; | |
1615 | struct hostent *hptr; | |
1616 | struct utsname myname; | |
1617 | char cap[16]; | |
1618 | char d[4]; | |
1619 | ||
1620 | /* Open our socket */ | |
1621 | ||
1622 | if ( (listenfd = aim_listenestablish(port)) == -1) | |
1623 | return NULL; | |
1624 | ||
1625 | /* get our local IP */ | |
1626 | ||
1627 | if (uname(&myname) < 0) | |
1628 | return NULL; | |
1629 | if ( (hptr = gethostbyname(myname.nodename)) == NULL) | |
1630 | return NULL; | |
1631 | memcpy(&d, hptr->h_addr_list[0], 4); | |
1632 | ||
1633 | aim_putcap(cap, 16, AIM_CAPS_GETFILE); | |
1634 | ||
1635 | /* create the OSCAR packet */ | |
1636 | ||
1637 | if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x42))) | |
1638 | return NULL; | |
1639 | newpacket->lock = 1; | |
1640 | ||
1641 | /* lock struct */ | |
1642 | curbyte = 0; | |
1643 | curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid); | |
1644 | ||
1645 | /* XXX: check the cookie before commiting to using it */ | |
1646 | ||
1647 | /* Generate a random message cookie | |
1648 | * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */ | |
1649 | for (i=0; i<7; i++) | |
1650 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10)); | |
1651 | ||
1652 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1653 | ||
1654 | /* grab all the data for cookie caching. */ | |
1655 | ||
1656 | if (!(cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) | |
1657 | return NULL; | |
1658 | memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); | |
1659 | cookie->type = AIM_COOKIETYPE_OFTGET; | |
1660 | ||
1661 | if (!(priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) | |
1662 | return NULL; | |
1663 | memcpy(priv->cookie, cookie, 8); | |
1664 | memcpy(priv->sn, destsn, sizeof(priv->sn)); | |
1665 | memcpy(priv->fh.name, "listing.txt", strlen("listing.txt")); | |
1666 | priv->state = 1; | |
1667 | ||
1668 | cookie->data = priv; | |
1669 | ||
1670 | aim_cachecookie(sess, cookie); | |
1671 | ||
1672 | /* Channel ID */ | |
1673 | curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); | |
1674 | ||
1675 | /* Destination SN (prepended with byte length) */ | |
1676 | curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); | |
1677 | curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); | |
1678 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
1679 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
1680 | ||
1681 | /* enTLV start */ | |
1682 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); | |
1683 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042); | |
1684 | ||
1685 | /* Flag data / ICBM Parameters? */ | |
1686 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1687 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1688 | ||
1689 | /* Cookie */ | |
1690 | curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); | |
1691 | ||
1692 | /* Capability String */ | |
1693 | curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); | |
1694 | ||
1695 | /* 000a/0002 : 0001 */ | |
1696 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a); | |
1697 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); | |
1698 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001); | |
1699 | ||
1700 | /* 0003/0004: IP address */ | |
1701 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
1702 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); | |
1703 | for(i = 0; i < 4; i++) | |
1704 | curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); | |
1705 | ||
1706 | /* already in network byte order */ | |
1707 | ||
1708 | /* 0005/0002: Port */ | |
1709 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); | |
1710 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); | |
1711 | curbyte += aimutil_put16(newpacket->data+curbyte, port); | |
1712 | ||
1713 | /* 000f/0000: ?? */ | |
1714 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); | |
1715 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
1716 | ||
1717 | /* 2711/000c: ?? */ | |
1718 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711); | |
1719 | curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c); | |
1720 | curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001); | |
1721 | ||
1722 | for(i = 0; i < 0x000c - 4; i++) | |
1723 | curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1724 | ||
1725 | newpacket->commandlen = curbyte; | |
1726 | newpacket->lock = 0; | |
1727 | aim_tx_enqueue(sess, newpacket); | |
1728 | ||
1729 | /* allocate and set up our connection */ | |
1730 | ||
1731 | i = fcntl(listenfd, F_GETFL, 0); | |
1732 | fcntl(listenfd, F_SETFL, i | O_NONBLOCK); | |
1733 | newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL); | |
1734 | ||
1735 | if (!newconn){ | |
1736 | perror("aim_newconn"); | |
1737 | return NULL; | |
1738 | } | |
1739 | ||
1740 | newconn->fd = listenfd; | |
1741 | newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE; | |
1742 | newconn->priv = priv; | |
1743 | faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd); | |
1744 | ||
1745 | return newconn; | |
1746 | } | |
1747 | ||
1748 | /** | |
1749 | * aim_oft_getfile_request - request a particular file over an established getfile connection | |
1750 | * @sess: your session | |
1751 | * @conn: the established OFT getfile connection | |
1752 | * @name: filename to request | |
1753 | * @size: size of the file | |
1754 | * | |
1755 | * | |
1756 | * returns -1 on error, 0 on successful enqueuing | |
1757 | */ | |
1758 | faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size) | |
1759 | { | |
1760 | struct command_tx_struct *newoft; | |
1761 | struct aim_filetransfer_priv *ft; | |
1762 | if (!sess || !conn || !conn->priv || !name) | |
1763 | return -1; | |
1764 | ||
1765 | if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120c, 0))) { | |
1766 | faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n"); | |
1767 | return -1; | |
1768 | } | |
1769 | ||
1770 | newoft->lock = 1; | |
1771 | ||
1772 | memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1773 | newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1774 | ||
1775 | ft = (struct aim_filetransfer_priv *)conn->priv; | |
1776 | ft->fh.filesleft = 1; | |
1777 | ft->fh.totfiles = 1; | |
1778 | ft->fh.totparts = 1; | |
1779 | ft->fh.partsleft = 1; | |
1780 | ft->fh.totsize = size; | |
1781 | ft->fh.size = size; | |
1782 | ft->fh.checksum = 0; | |
1783 | memcpy(ft->fh.name, name, strlen(name)); | |
1784 | memset(ft->fh.name+strlen(name), 0, 1); | |
1785 | ||
1786 | if (!(newoft->hdr.oft.hdr2 = (unsigned char *)calloc(1,newoft->hdr.oft.hdr2len))) { | |
1787 | newoft->lock = 0; | |
1788 | aim_tx_destroy(newoft); | |
1789 | return -1; | |
1790 | } | |
1791 | ||
1792 | if (!(aim_oft_buildheader(newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
1793 | newoft->lock = 0; | |
1794 | aim_tx_destroy(newoft); | |
1795 | return -1; | |
1796 | } | |
1797 | ||
1798 | newoft->lock = 0; | |
1799 | ||
1800 | aim_tx_enqueue(sess, newoft); | |
1801 | return 0; | |
1802 | } | |
1803 | ||
1804 | /** | |
1805 | * aim_oft_getfile_ack - acknowledge a getfile download as complete | |
1806 | * @sess: your session | |
1807 | * @conn: the getfile conn to send the ack over | |
1808 | * | |
1809 | * Call this function after you have read all the data in a particular | |
1810 | * filetransfer. Returns -1 on error, 0 on apparent success | |
1811 | * | |
1812 | */ | |
1813 | faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn) | |
1814 | { | |
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_tx_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_tx_destroy(newoft); | |
1842 | return -1; | |
1843 | } | |
1844 | ||
1845 | newoft->lock = 0; | |
1846 | aim_tx_enqueue(sess, newoft); | |
1847 | return 0; | |
1848 | } | |
1849 | ||
1850 | /** | |
1851 | * aim_oft_getfile_end - end a getfile. | |
1852 | * @sess: your session | |
1853 | * @conn: the getfile connection | |
1854 | * | |
1855 | * call this before you close the getfile connection if you're on the | |
1856 | * receiving/requesting end. | |
1857 | */ | |
1858 | faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn) | |
1859 | { | |
1860 | struct command_tx_struct *newoft; | |
1861 | struct aim_filetransfer_priv *ft; | |
1862 | ||
1863 | if (!sess || !conn || !conn->priv) | |
1864 | return -1; | |
1865 | ||
1866 | if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0204, 0))) { | |
1867 | faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n"); | |
1868 | return -1; | |
1869 | } | |
1870 | ||
1871 | newoft->lock = 1; | |
1872 | ||
1873 | memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1874 | newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1875 | ||
1876 | if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { | |
1877 | newoft->lock = 0; | |
1878 | aim_tx_destroy(newoft); | |
1879 | return -1; | |
1880 | } | |
1881 | ||
1882 | ft = (struct aim_filetransfer_priv *)conn->priv; | |
1883 | ft->state = 4; /* no longer wanting data */ | |
1884 | ft->fh.nrecvd = ft->fh.size; | |
1885 | ft->fh.recvcsum = ft->fh.checksum; | |
1886 | ft->fh.flags = 0x21; | |
1887 | ||
1888 | if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
1889 | newoft->lock = 0; | |
1890 | aim_tx_destroy(newoft); | |
1891 | return -1; | |
1892 | } | |
1893 | ||
1894 | newoft->lock = 0; | |
1895 | aim_tx_enqueue(sess, newoft); | |
1896 | ||
1897 | return 0; | |
1898 | } |