Mercurial > pidgin
annotate libfaim/ft.c @ 1909:8b915b0f681b
[gaim-migrate @ 1919]
one last segfault
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 29 May 2001 01:39:05 +0000 |
parents | 109cacf1ff97 |
children |
rev | line source |
---|---|
1535 | 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; | |
1839
109cacf1ff97
[gaim-migrate @ 1849]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1593
diff
changeset
|
40 aim_rxcallback_t userfunc; |
1535 | 41 struct sockaddr cliaddr; |
1550
001a79aec218
[gaim-migrate @ 1560]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1535
diff
changeset
|
42 int clilen = sizeof(cliaddr); |
1535 | 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; | |
1839
109cacf1ff97
[gaim-migrate @ 1849]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1593
diff
changeset
|
827 aim_rxcallback_t userfunc = NULL; |
1535 | 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; | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1001 } |
1535 | 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 | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1022 faimdprintf(sess, 2, "faim: OFT frame: %04x / %04x / %04x / %s\n", hdrtype, payloadlength, flags, priv->sn); |
1535 | 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)) ) | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1030 return userfunc(sess, NULL, conn); |
1535 | 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; | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1052 faimdprintf(sess, 2, "faim: directim: %s/%04x/%04x/%s\n", priv->sn, payloadlength, flags, msg); |
1535 | 1053 |
1054 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING)) ) | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1055 i = userfunc(sess, NULL, conn, msg); |
1535 | 1056 else { |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1057 faimdprintf(sess, 0, "directim: %s/%04x/%04x/%s\n", priv->sn, payloadlength, flags, msg); |
1535 | 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 | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1351 if(!(fh = aim_oft_getfh(hdr))) |
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1352 return -1; |
1535 | 1353 |
1354 free(hdr); | |
1355 hdr = NULL; | |
1356 | |
1357 faim_mutex_unlock(&conn->active); | |
1358 | |
1359 faimdprintf(sess, 2, "faim: get_rend: looks like we're done with a transfer (oft 0x0204)\n"); | |
1360 | |
1361 if ( (userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_GETFILECOMPLETE)) ) | |
1362 i = userfunc(sess, NULL, conn, fh); | |
1363 else | |
1364 i = 1; | |
1365 | |
1366 if (conn) | |
1367 aim_conn_close(conn); | |
1368 | |
1369 free(fh); | |
1370 | |
1371 return i; | |
1372 break; | |
1373 } | |
1374 default: { | |
1375 free(hdr); | |
1376 hdr = NULL; | |
1377 faimdprintf(sess, 2,"faim: OFT frame: uknown type %04x\n", hdrtype); | |
1378 faim_mutex_unlock(&conn->active); | |
1379 break; | |
1380 } | |
1381 } /* switch */ | |
1593
ec31e23aadc7
[gaim-migrate @ 1603]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
1550
diff
changeset
|
1382 |
1535 | 1383 if (hdr) { |
1384 faimdprintf(sess, 0, "hdr wasn't freed by a rendezvous switch case (hdrtype: %0x04x)!\n", hdrtype); | |
1385 free(hdr); | |
1386 hdr = NULL; | |
1387 } | |
1388 return 0; | |
1389 } | |
1390 | |
1391 /** | |
1392 * aim_oft_getfh - extracts an &aim_fileheader_t from buffer hdr. | |
1393 * @hdr: buffer to extract header from | |
1394 * | |
1395 * returns pointer to new struct on success; %NULL on error. | |
1396 * | |
1397 */ | |
1398 static struct aim_fileheader_t *aim_oft_getfh(unsigned char *hdr) | |
1399 { | |
1400 struct aim_fileheader_t *fh; | |
1401 int i, j; | |
1402 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) | |
1403 return NULL; | |
1404 | |
1405 /* [0] and [1] are the type. we can ignore those here. */ | |
1406 i = 2; | |
1407 for(j = 0; j < 8; j++, i++) | |
1408 fh->bcookie[j] = hdr[i]; | |
1409 fh->encrypt = aimutil_get16(hdr+i); | |
1410 i += 2; | |
1411 fh->compress = aimutil_get16(hdr+i); | |
1412 i += 2; | |
1413 fh->totfiles = aimutil_get16(hdr+i); | |
1414 i += 2; | |
1415 fh->filesleft = aimutil_get16(hdr+i); | |
1416 i += 2; | |
1417 fh->totparts = aimutil_get16(hdr+i); | |
1418 i += 2; | |
1419 fh->partsleft = aimutil_get16(hdr+i); | |
1420 i += 2; | |
1421 fh->totsize = aimutil_get32(hdr+i); | |
1422 i += 4; | |
1423 fh->size = aimutil_get32(hdr+i); | |
1424 i += 4; | |
1425 fh->modtime = aimutil_get32(hdr+i); | |
1426 i += 4; | |
1427 fh->checksum = aimutil_get32(hdr+i); | |
1428 i += 4; | |
1429 fh->rfrcsum = aimutil_get32(hdr+i); | |
1430 i += 4; | |
1431 fh->rfsize = aimutil_get32(hdr+i); | |
1432 i += 4; | |
1433 fh->cretime = aimutil_get32(hdr+i); | |
1434 i += 4; | |
1435 fh->rfcsum = aimutil_get32(hdr+i); | |
1436 i += 4; | |
1437 fh->nrecvd = aimutil_get32(hdr+i); | |
1438 i += 4; | |
1439 fh->recvcsum = aimutil_get32(hdr+i); | |
1440 i += 4; | |
1441 memcpy(fh->idstring, hdr+i, 32); | |
1442 i += 32; | |
1443 fh->flags = aimutil_get8(hdr+i); | |
1444 i += 1; | |
1445 fh->lnameoffset = aimutil_get8(hdr+i); | |
1446 i += 1; | |
1447 fh->lsizeoffset = aimutil_get8(hdr+i); | |
1448 i += 1; | |
1449 memcpy(fh->dummy, hdr+i, 69); | |
1450 i += 69; | |
1451 memcpy(fh->macfileinfo, hdr+i, 16); | |
1452 i += 16; | |
1453 fh->nencode = aimutil_get16(hdr+i); | |
1454 i += 2; | |
1455 fh->nlanguage = aimutil_get16(hdr+i); | |
1456 i += 2; | |
1457 memcpy(fh->name, hdr+i, 64); | |
1458 i += 64; | |
1459 return fh; | |
1460 } | |
1461 | |
1462 /** | |
1463 * aim_oft_checksum - calculate oft checksum of buffer | |
1464 * @buffer: buffer of data to checksum | |
1465 * @bufsize: size of buffer | |
1466 * @checksum: pointer to integer to place result in (pointer!) | |
1467 * | |
1468 * | |
1469 * Note that checksum is a pointer. Checksum should be filled with | |
1470 * 0xFFFF0000 for each new file; you can have this checksum chunks of | |
1471 * files in series if you just call it repeatedly in a for(; ; ) loop | |
1472 * and don't reset the checksum between each call. And you thought we | |
1473 * didn't care about you and your pathetic client's meomry footprint | |
1474 * ;^) | |
1475 * | |
1476 * | |
1477 * Also, it's been said that this is incorrect as currently | |
1478 * written. You were warned. | |
1479 */ | |
1480 faim_export int aim_oft_checksum(struct aim_session_t *sess, char *buffer, int bufsize, int *checksum) | |
1481 { | |
1482 short check0, check1; | |
1483 int i; | |
1484 check0 = ((*checksum & 0xFF000000) >> 16); | |
1485 check1 = ((*checksum & 0x00ff0000) >> 16); | |
1486 for(i = 0; i < bufsize; i++) { | |
1487 if (i % 2) { /* use check1 -- second byte */ | |
1488 if ( (short)buffer[i] > check1 ) { /* wrapping */ | |
1489 check1 += 0x100; /* this is a cheap way to wrap */ | |
1490 | |
1491 /* if we're wrapping, decrement the other one */ | |
1492 /* XXX: check this corner case */ | |
1493 if (check0 == 0) | |
1494 check0 = 0x00ff; | |
1495 else | |
1496 check0--; | |
1497 } | |
1498 check1 -= buffer[i]; | |
1499 } else { /* use check0 -- first byte */ | |
1500 if ( (short)buffer[i] > check0 ) { /* wrapping */ | |
1501 check0 += 0x100; /* this is a cheap way to wrap */ | |
1502 | |
1503 /* if we're wrapping, decrement the other one */ | |
1504 /* XXX: check this corner case */ | |
1505 if (check1 == 0) | |
1506 check1 = 0x00ff; | |
1507 else | |
1508 check1--; | |
1509 } | |
1510 check0 -= buffer[i]; | |
1511 } | |
1512 } | |
1513 | |
1514 if (check0 > 0xff || check1 > 0xff) { | |
1515 /* they shouldn't be able to do this. error! */ | |
1516 faimdprintf(sess, 2, "check0 or check1 is too high: 0x%04x, 0x%04x\n", check0, check1); | |
1517 return -1; | |
1518 } | |
1519 | |
1520 /* grab just the lowest byte; this should be clean, but just in | |
1521 case */ | |
1522 check0 &= 0xff; | |
1523 check1 &= 0xff; | |
1524 | |
1525 *checksum = ((check0 * 0x1000000) + (check1 * 0x10000)); | |
1526 return *checksum; | |
1527 } | |
1528 | |
1529 /** | |
1530 * aim_oft_buildheader - fills a buffer with network-order fh data | |
1531 * @dest: buffer to fill -- pre-alloced | |
1532 * @fh: fh to get data from | |
1533 * | |
1534 * returns length written; -1 on error. | |
1535 * DOES NOT DO BOUNDS CHECKING! | |
1536 * | |
1537 */ | |
1538 faim_internal int aim_oft_buildheader(unsigned char *dest,struct aim_fileheader_t *fh) | |
1539 { | |
1540 int i, curbyte; | |
1541 if (!dest || !fh) | |
1542 return -1; | |
1543 curbyte = 0; | |
1544 for(i = 0; i < 8; i++) | |
1545 curbyte += aimutil_put8(dest+curbyte, fh->bcookie[i]); | |
1546 curbyte += aimutil_put16(dest+curbyte, fh->encrypt); | |
1547 curbyte += aimutil_put16(dest+curbyte, fh->compress); | |
1548 curbyte += aimutil_put16(dest+curbyte, fh->totfiles); | |
1549 curbyte += aimutil_put16(dest+curbyte, fh->filesleft); | |
1550 curbyte += aimutil_put16(dest+curbyte, fh->totparts); | |
1551 curbyte += aimutil_put16(dest+curbyte, fh->partsleft); | |
1552 curbyte += aimutil_put32(dest+curbyte, fh->totsize); | |
1553 curbyte += aimutil_put32(dest+curbyte, fh->size); | |
1554 curbyte += aimutil_put32(dest+curbyte, fh->modtime); | |
1555 curbyte += aimutil_put32(dest+curbyte, fh->checksum); | |
1556 curbyte += aimutil_put32(dest+curbyte, fh->rfrcsum); | |
1557 curbyte += aimutil_put32(dest+curbyte, fh->rfsize); | |
1558 curbyte += aimutil_put32(dest+curbyte, fh->cretime); | |
1559 curbyte += aimutil_put32(dest+curbyte, fh->rfcsum); | |
1560 curbyte += aimutil_put32(dest+curbyte, fh->nrecvd); | |
1561 curbyte += aimutil_put32(dest+curbyte, fh->recvcsum); | |
1562 memcpy(dest+curbyte, fh->idstring, 32); | |
1563 curbyte += 32; | |
1564 curbyte += aimutil_put8(dest+curbyte, fh->flags); | |
1565 curbyte += aimutil_put8(dest+curbyte, fh->lnameoffset); | |
1566 curbyte += aimutil_put8(dest+curbyte, fh->lsizeoffset); | |
1567 memcpy(dest+curbyte, fh->dummy, 69); | |
1568 curbyte += 69; | |
1569 memcpy(dest+curbyte, fh->macfileinfo, 16); | |
1570 curbyte += 16; | |
1571 curbyte += aimutil_put16(dest+curbyte, fh->nencode); | |
1572 curbyte += aimutil_put16(dest+curbyte, fh->nlanguage); | |
1573 memset(dest+curbyte, 0x00, 64); | |
1574 memcpy(dest+curbyte, fh->name, 64); | |
1575 | |
1576 /* XXX: Filenames longer than 64B */ | |
1577 curbyte += 64; | |
1578 return curbyte; | |
1579 } | |
1580 | |
1581 | |
1582 /** | |
1583 * aim_tx_destroy - free's tx_command_t's | |
1584 * @command: the command to free | |
1585 * | |
1586 * if command is locked, doesn't free. | |
1587 * returns -1 on error (locked struct); 0 on success. | |
1588 * | |
1589 */ | |
1590 faim_internal int aim_tx_destroy(struct command_tx_struct *command){ | |
1591 if (command->lock) | |
1592 return -1; | |
1593 if (command->data) | |
1594 free(command->data); | |
1595 if (command->hdrtype == AIM_FRAMETYPE_OFT && command->hdr.oft.hdr2) | |
1596 free(command->hdr.oft.hdr2); | |
1597 free(command); | |
1598 return 0; | |
1599 } | |
1600 | |
1601 /** | |
1602 * aim_getfile_intitiate - Request an OFT getfile session | |
1603 * @sess: your session, | |
1604 * @conn: the BOS conn, | |
1605 * @destsn is the SN to connect to. | |
1606 * | |
1607 * returns a new &aim_conn_t on success, %NULL on error | |
1608 */ | |
1609 faim_export struct aim_conn_t *aim_getfile_initiate(struct aim_session_t *sess, struct aim_conn_t *conn, char *destsn) | |
1610 { | |
1611 struct command_tx_struct *newpacket; | |
1612 struct aim_conn_t *newconn; | |
1613 struct aim_filetransfer_priv *priv; | |
1614 struct aim_msgcookie_t *cookie; | |
1615 int curbyte, i, listenfd; | |
1616 short port = 4443; | |
1617 struct hostent *hptr; | |
1618 struct utsname myname; | |
1619 char cap[16]; | |
1620 char d[4]; | |
1621 | |
1622 /* Open our socket */ | |
1623 | |
1624 if ( (listenfd = aim_listenestablish(port)) == -1) | |
1625 return NULL; | |
1626 | |
1627 /* get our local IP */ | |
1628 | |
1629 if (uname(&myname) < 0) | |
1630 return NULL; | |
1631 if ( (hptr = gethostbyname(myname.nodename)) == NULL) | |
1632 return NULL; | |
1633 memcpy(&d, hptr->h_addr_list[0], 4); | |
1634 | |
1635 aim_putcap(cap, 16, AIM_CAPS_GETFILE); | |
1636 | |
1637 /* create the OSCAR packet */ | |
1638 | |
1639 if (!(newpacket = aim_tx_new(sess, conn, AIM_FRAMETYPE_OSCAR, 0x0002, 10+8+2+1+strlen(destsn)+4+4+0x42))) | |
1640 return NULL; | |
1641 newpacket->lock = 1; | |
1642 | |
1643 /* lock struct */ | |
1644 curbyte = 0; | |
1645 curbyte += aim_putsnac(newpacket->data+curbyte, 0x0004, 0x0006, 0x0000, sess->snac_nextid); | |
1646 | |
1647 /* XXX: check the cookie before commiting to using it */ | |
1648 | |
1649 /* Generate a random message cookie | |
1650 * This cookie needs to be alphanumeric and NULL-terminated to be TOC-compatible. */ | |
1651 for (i=0; i<7; i++) | |
1652 curbyte += aimutil_put8(newpacket->data+curbyte, 0x30 + ((u_char) random() % 10)); | |
1653 | |
1654 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1655 | |
1656 /* grab all the data for cookie caching. */ | |
1657 | |
1658 if (!(cookie = (struct aim_msgcookie_t *)calloc(1, sizeof(struct aim_msgcookie_t)))) | |
1659 return NULL; | |
1660 memcpy(cookie->cookie, newpacket->data+curbyte-8, 8); | |
1661 cookie->type = AIM_COOKIETYPE_OFTGET; | |
1662 | |
1663 if (!(priv = (struct aim_filetransfer_priv *)calloc(1, sizeof(struct aim_filetransfer_priv)))) | |
1664 return NULL; | |
1665 memcpy(priv->cookie, cookie, 8); | |
1666 memcpy(priv->sn, destsn, sizeof(priv->sn)); | |
1667 memcpy(priv->fh.name, "listing.txt", strlen("listing.txt")); | |
1668 priv->state = 1; | |
1669 | |
1670 cookie->data = priv; | |
1671 | |
1672 aim_cachecookie(sess, cookie); | |
1673 | |
1674 /* Channel ID */ | |
1675 curbyte += aimutil_put16(newpacket->data+curbyte,0x0002); | |
1676 | |
1677 /* Destination SN (prepended with byte length) */ | |
1678 curbyte += aimutil_put8(newpacket->data+curbyte,strlen(destsn)); | |
1679 curbyte += aimutil_putstr(newpacket->data+curbyte, destsn, strlen(destsn)); | |
1680 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
1681 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
1682 | |
1683 /* enTLV start */ | |
1684 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); | |
1685 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0042); | |
1686 | |
1687 /* Flag data / ICBM Parameters? */ | |
1688 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1689 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1690 | |
1691 /* Cookie */ | |
1692 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cookie, 8); | |
1693 | |
1694 /* Capability String */ | |
1695 curbyte += aimutil_putstr(newpacket->data+curbyte, (char *)cap, 0x10); | |
1696 | |
1697 /* 000a/0002 : 0001 */ | |
1698 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000a); | |
1699 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); | |
1700 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0001); | |
1701 | |
1702 /* 0003/0004: IP address */ | |
1703 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0003); | |
1704 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0004); | |
1705 for(i = 0; i < 4; i++) | |
1706 curbyte += aimutil_put8(newpacket->data+curbyte, d[i]); | |
1707 | |
1708 /* already in network byte order */ | |
1709 | |
1710 /* 0005/0002: Port */ | |
1711 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0005); | |
1712 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0002); | |
1713 curbyte += aimutil_put16(newpacket->data+curbyte, port); | |
1714 | |
1715 /* 000f/0000: ?? */ | |
1716 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000f); | |
1717 curbyte += aimutil_put16(newpacket->data+curbyte, 0x0000); | |
1718 | |
1719 /* 2711/000c: ?? */ | |
1720 curbyte += aimutil_put16(newpacket->data+curbyte, 0x2711); | |
1721 curbyte += aimutil_put16(newpacket->data+curbyte, 0x000c); | |
1722 curbyte += aimutil_put32(newpacket->data+curbyte, 0x00120001); | |
1723 | |
1724 for(i = 0; i < 0x000c - 4; i++) | |
1725 curbyte += aimutil_put8(newpacket->data+curbyte, 0x00); | |
1726 | |
1727 newpacket->commandlen = curbyte; | |
1728 newpacket->lock = 0; | |
1729 aim_tx_enqueue(sess, newpacket); | |
1730 | |
1731 /* allocate and set up our connection */ | |
1732 | |
1733 i = fcntl(listenfd, F_GETFL, 0); | |
1734 fcntl(listenfd, F_SETFL, i | O_NONBLOCK); | |
1735 newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS_OUT, NULL); | |
1736 | |
1737 if (!newconn){ | |
1738 perror("aim_newconn"); | |
1739 return NULL; | |
1740 } | |
1741 | |
1742 newconn->fd = listenfd; | |
1743 newconn->subtype = AIM_CONN_SUBTYPE_OFT_GETFILE; | |
1744 newconn->priv = priv; | |
1745 faimdprintf(sess, 2,"faim: listening (fd = %d, unconnected)\n", newconn->fd); | |
1746 | |
1747 return newconn; | |
1748 } | |
1749 | |
1750 /** | |
1751 * aim_oft_getfile_request - request a particular file over an established getfile connection | |
1752 * @sess: your session | |
1753 * @conn: the established OFT getfile connection | |
1754 * @name: filename to request | |
1755 * @size: size of the file | |
1756 * | |
1757 * | |
1758 * returns -1 on error, 0 on successful enqueuing | |
1759 */ | |
1760 faim_export int aim_oft_getfile_request(struct aim_session_t *sess, struct aim_conn_t *conn, const unsigned char *name, const int size) | |
1761 { | |
1762 struct command_tx_struct *newoft; | |
1763 struct aim_filetransfer_priv *ft; | |
1764 if (!sess || !conn || !conn->priv || !name) | |
1765 return -1; | |
1766 | |
1767 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x120c, 0))) { | |
1768 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n"); | |
1769 return -1; | |
1770 } | |
1771 | |
1772 newoft->lock = 1; | |
1773 | |
1774 memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1775 newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1776 | |
1777 ft = (struct aim_filetransfer_priv *)conn->priv; | |
1778 ft->fh.filesleft = 1; | |
1779 ft->fh.totfiles = 1; | |
1780 ft->fh.totparts = 1; | |
1781 ft->fh.partsleft = 1; | |
1782 ft->fh.totsize = size; | |
1783 ft->fh.size = size; | |
1784 ft->fh.checksum = 0; | |
1785 memcpy(ft->fh.name, name, strlen(name)); | |
1786 memset(ft->fh.name+strlen(name), 0, 1); | |
1787 | |
1788 if (!(newoft->hdr.oft.hdr2 = (unsigned char *)calloc(1,newoft->hdr.oft.hdr2len))) { | |
1789 newoft->lock = 0; | |
1790 aim_tx_destroy(newoft); | |
1791 return -1; | |
1792 } | |
1793 | |
1794 if (!(aim_oft_buildheader(newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
1795 newoft->lock = 0; | |
1796 aim_tx_destroy(newoft); | |
1797 return -1; | |
1798 } | |
1799 | |
1800 newoft->lock = 0; | |
1801 | |
1802 aim_tx_enqueue(sess, newoft); | |
1803 return 0; | |
1804 } | |
1805 | |
1806 /** | |
1807 * aim_oft_getfile_ack - acknowledge a getfile download as complete | |
1808 * @sess: your session | |
1809 * @conn: the getfile conn to send the ack over | |
1810 * | |
1811 * Call this function after you have read all the data in a particular | |
1812 * filetransfer. Returns -1 on error, 0 on apparent success | |
1813 * | |
1814 */ | |
1815 faim_export int aim_oft_getfile_ack(struct aim_session_t *sess, struct aim_conn_t *conn) | |
1816 { | |
1817 struct command_tx_struct *newoft; | |
1818 struct aim_filetransfer_priv *ft; | |
1819 | |
1820 if (!sess || !conn || !conn->priv) | |
1821 return -1; | |
1822 | |
1823 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0202, 0))) { | |
1824 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n"); | |
1825 return -1; | |
1826 } | |
1827 | |
1828 newoft->lock = 1; | |
1829 | |
1830 memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1831 newoft->hdr.oft.hdr2len = 0x100-8; | |
1832 | |
1833 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { | |
1834 newoft->lock = 0; | |
1835 aim_tx_destroy(newoft); | |
1836 return -1; | |
1837 } | |
1838 | |
1839 ft = (struct aim_filetransfer_priv *)conn->priv; | |
1840 | |
1841 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
1842 newoft->lock = 0; | |
1843 aim_tx_destroy(newoft); | |
1844 return -1; | |
1845 } | |
1846 | |
1847 newoft->lock = 0; | |
1848 aim_tx_enqueue(sess, newoft); | |
1849 return 0; | |
1850 } | |
1851 | |
1852 /** | |
1853 * aim_oft_getfile_end - end a getfile. | |
1854 * @sess: your session | |
1855 * @conn: the getfile connection | |
1856 * | |
1857 * call this before you close the getfile connection if you're on the | |
1858 * receiving/requesting end. | |
1859 */ | |
1860 faim_export int aim_oft_getfile_end(struct aim_session_t *sess, struct aim_conn_t *conn) | |
1861 { | |
1862 struct command_tx_struct *newoft; | |
1863 struct aim_filetransfer_priv *ft; | |
1864 | |
1865 if (!sess || !conn || !conn->priv) | |
1866 return -1; | |
1867 | |
1868 if (!(newoft = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0204, 0))) { | |
1869 faimdprintf(sess, 2, "faim: aim_accepttransfer: tx_new OFT failed\n"); | |
1870 return -1; | |
1871 } | |
1872 | |
1873 newoft->lock = 1; | |
1874 | |
1875 memcpy(newoft->hdr.oft.magic, "OFT2", 4); | |
1876 newoft->hdr.oft.hdr2len = 0x100 - 8; | |
1877 | |
1878 if (!(newoft->hdr.oft.hdr2 = (char *)calloc(1,newoft->hdr.oft.hdr2len))) { | |
1879 newoft->lock = 0; | |
1880 aim_tx_destroy(newoft); | |
1881 return -1; | |
1882 } | |
1883 | |
1884 ft = (struct aim_filetransfer_priv *)conn->priv; | |
1885 ft->state = 4; /* no longer wanting data */ | |
1886 ft->fh.nrecvd = ft->fh.size; | |
1887 ft->fh.recvcsum = ft->fh.checksum; | |
1888 ft->fh.flags = 0x21; | |
1889 | |
1890 if (!(aim_oft_buildheader((unsigned char *)newoft->hdr.oft.hdr2, &(ft->fh)))) { | |
1891 newoft->lock = 0; | |
1892 aim_tx_destroy(newoft); | |
1893 return -1; | |
1894 } | |
1895 | |
1896 newoft->lock = 0; | |
1897 aim_tx_enqueue(sess, newoft); | |
1898 | |
1899 return 0; | |
1900 } |