Mercurial > pidgin
annotate src/protocols/oscar/ft.c @ 8983:460d02fe03df
[gaim-migrate @ 9758]
This makes us tell the other person to connect to us if we can't connect
to them, for direct im. Also I made us used the clientip instead of the
verified ip for direct im, because I get the impression that's what the
official aim clients use, and I think it sort of makes sense.
Maybe I should compare the two and not bother trying if they're different.
committer: Tailor Script <tailor@pidgin.im>
author | Tim Ringenbach <marv@pidgin.im> |
---|---|
date | Thu, 20 May 2004 02:03:34 +0000 |
parents | a4fd6666bb83 |
children | f13172eed3ad |
rev | line source |
---|---|
2086 | 1 /* |
4617 | 2 * Oscar File transfer (OFT) and Oscar Direct Connect (ODC). |
3 * (ODC is also referred to as DirectIM and IM Image.) | |
4 * | |
5 * There are a few static helper functions at the top, then | |
6 * ODC stuff, then ft stuff. | |
7 * | |
8 * I feel like this is a good place to explain OFT, so I'm going to | |
9 * do just that. Each OFT packet has a header type. I guess this | |
10 * is pretty similar to the subtype of a SNAC packet. The type | |
11 * basically tells the other client the meaning of the OFT packet. | |
12 * There are two distinct types of file transfer, which I usually | |
13 * call "sendfile" and "getfile." Sendfile is when you send a file | |
14 * to another AIM user. Getfile is when you share a group of files, | |
15 * and other users request that you send them the files. | |
16 * | |
17 * A typical sendfile file transfer goes like this: | |
18 * 1) Sender sends a channel 2 ICBM telling the other user that | |
19 * we want to send them a file. At the same time, we open a | |
20 * listener socket (this should be done before sending the | |
21 * ICBM) on some port, and wait for them to connect to us. | |
22 * The ICBM we sent should contain our IP address and the port | |
23 * number that we're listening on. | |
24 * 2) The receiver connects to the sender on the given IP address | |
25 * and port. After the connection is established, the receiver | |
5146 | 26 * sends an ICBM signifying that we are ready and waiting. |
4617 | 27 * 3) The sender sends an OFT PROMPT message over the OFT |
28 * connection. | |
29 * 4) The receiver of the file sends back an exact copy of this | |
30 * OFT packet, except the cookie is filled in with the cookie | |
31 * from the ICBM. I think this might be an attempt to verify | |
32 * that the user that is connected is actually the guy that | |
33 * we sent the ICBM to. Oh, I've been calling this the ACK. | |
34 * 5) The sender starts sending raw data across the connection | |
35 * until the entire file has been sent. | |
36 * 6) The receiver knows the file is finished because the sender | |
37 * sent the file size in an earlier OFT packet. So then the | |
5146 | 38 * receiver sends the DONE thingy (after filling in the |
39 * "received" checksum and size) and closes the connection. | |
2086 | 40 */ |
41 | |
42 #define FAIM_INTERNAL | |
5415 | 43 #ifdef HAVE_CONFIG_H |
44 #include <config.h> | |
45 #endif | |
46 | |
2086 | 47 #include <aim.h> |
48 | |
49 #ifndef _WIN32 | |
5927 | 50 #include <stdio.h> |
2086 | 51 #include <netdb.h> |
52 #include <sys/socket.h> | |
53 #include <netinet/in.h> | |
4617 | 54 #include <sys/utsname.h> /* for aim_odc_initiate */ |
3630 | 55 #include <arpa/inet.h> /* for inet_ntoa */ |
8080 | 56 #include <limits.h> /* for UINT_MAX */ |
3960 | 57 #define G_DIR_SEPARATOR '/' |
3646
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
58 #endif |
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
59 |
bfd8df165f32
[gaim-migrate @ 3770]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3630
diff
changeset
|
60 #ifdef _WIN32 |
3630 | 61 #include "win32dep.h" |
62 #endif | |
2086 | 63 |
4617 | 64 struct aim_odc_intdata { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
65 fu8_t cookie[8]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
66 char sn[MAXSNLEN+1]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
67 char ip[22]; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
68 }; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
69 |
4617 | 70 /** |
71 * Convert the directory separator from / (0x2f) to ^A (0x01) | |
72 * | |
73 * @param name The filename to convert. | |
74 */ | |
75 static void aim_oft_dirconvert_tostupid(char *name) | |
76 { | |
77 while (name[0]) { | |
78 if (name[0] == 0x01) | |
79 name[0] = G_DIR_SEPARATOR; | |
80 name++; | |
81 } | |
82 } | |
83 | |
84 /** | |
85 * Convert the directory separator from ^A (0x01) to / (0x2f) | |
86 * | |
87 * @param name The filename to convert. | |
88 */ | |
89 static void aim_oft_dirconvert_fromstupid(char *name) | |
90 { | |
91 while (name[0]) { | |
92 if (name[0] == G_DIR_SEPARATOR) | |
93 name[0] = 0x01; | |
94 name++; | |
95 } | |
96 } | |
97 | |
98 /** | |
99 * Calculate oft checksum of buffer | |
100 * | |
101 * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The | |
102 * checksum is kind of a rolling checksum thing, so each time you get bytes | |
103 * of a file you just call this puppy and it updates the checksum. You can | |
104 * calculate the checksum of an entire file by calling this in a while or a | |
105 * for loop, or something. | |
106 * | |
107 * Thanks to Graham Booker for providing this improved checksum routine, | |
108 * which is simpler and should be more accurate than Josh Myer's original | |
109 * code. -- wtm | |
110 * | |
8735
92cbf9713795
[gaim-migrate @ 9490]
Christian Hammond <chipx86@chipx86.com>
parents:
8610
diff
changeset
|
111 * This algorithm works every time I have tried it. The other fails |
4617 | 112 * sometimes. So, AOL who thought this up? It has got to be the weirdest |
113 * checksum I have ever seen. | |
114 * | |
115 * @param buffer Buffer of data to checksum. Man I'd like to buff her... | |
116 * @param bufsize Size of buffer. | |
117 * @param prevcheck Previous checksum. | |
118 */ | |
4763 | 119 faim_export fu32_t aim_oft_checksum_chunk(const fu8_t *buffer, int bufferlen, fu32_t prevcheck) |
4617 | 120 { |
121 fu32_t check = (prevcheck >> 16) & 0xffff, oldcheck; | |
122 int i; | |
123 unsigned short val; | |
124 | |
125 for (i=0; i<bufferlen; i++) { | |
126 oldcheck = check; | |
127 if (i&1) | |
128 val = buffer[i]; | |
129 else | |
130 val = buffer[i] << 8; | |
131 check -= val; | |
132 /* | |
133 * The following appears to be necessary.... It happens | |
134 * every once in a while and the checksum doesn't fail. | |
135 */ | |
136 if (check > oldcheck) | |
137 check--; | |
138 } | |
139 check = ((check & 0x0000ffff) + (check >> 16)); | |
140 check = ((check & 0x0000ffff) + (check >> 16)); | |
141 return check << 16; | |
142 } | |
143 | |
4650 | 144 faim_export fu32_t aim_oft_checksum_file(char *filename) { |
145 FILE *fd; | |
146 fu32_t checksum = 0xffff0000; | |
147 | |
148 if ((fd = fopen(filename, "rb"))) { | |
149 int bytes; | |
4763 | 150 fu8_t buffer[1024]; |
4650 | 151 |
152 while ((bytes = fread(buffer, 1, 1024, fd))) | |
153 checksum = aim_oft_checksum_chunk(buffer, bytes, checksum); | |
154 fclose(fd); | |
155 } | |
156 | |
157 return checksum; | |
158 } | |
159 | |
2086 | 160 /** |
4617 | 161 * After establishing a listening socket, this is called to accept a connection. It |
162 * clones the conn used by the listener, and passes both of these to a signal handler. | |
163 * The signal handler should close the listener conn and keep track of the new conn, | |
164 * since this is what is used for file transfers and what not. | |
165 * | |
166 * @param sess The session. | |
167 * @param cur The conn the incoming connection is on. | |
168 * @return Return 0 if no errors, otherwise return the error number. | |
2086 | 169 */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
170 faim_export int aim_handlerendconnect(aim_session_t *sess, aim_conn_t *cur) |
4617 | 171 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
172 int acceptfd = 0; |
4617 | 173 struct sockaddr addr; |
174 socklen_t addrlen = sizeof(addr); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
175 int ret = 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
176 aim_conn_t *newconn; |
4617 | 177 char ip[20]; |
8880 | 178 unsigned short port; |
2086 | 179 |
4617 | 180 if ((acceptfd = accept(cur->fd, &addr, &addrlen)) == -1) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
181 return 0; /* not an error */ |
2086 | 182 |
8880 | 183 /* Also accept inet6? */ |
8610 | 184 if (addr.sa_family != AF_INET) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
185 close(acceptfd); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
186 aim_conn_close(cur); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
187 return -1; |
4617 | 188 } |
189 | |
190 strncpy(ip, inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr), sizeof(ip)); | |
191 port = ntohs(((struct sockaddr_in *)&addr)->sin_port); | |
2086 | 192 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
193 if (!(newconn = aim_cloneconn(sess, cur))) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
194 close(acceptfd); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
195 aim_conn_close(cur); |
4617 | 196 return -ENOMEM; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
197 } |
2086 | 198 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
199 newconn->type = AIM_CONN_TYPE_RENDEZVOUS; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
200 newconn->fd = acceptfd; |
2086 | 201 |
4617 | 202 if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
203 aim_rxcallback_t userfunc; |
4617 | 204 struct aim_odc_intdata *priv; |
2086 | 205 |
4617 | 206 priv = (struct aim_odc_intdata *)(newconn->internal = cur->internal); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
207 cur->internal = NULL; |
8446 | 208 snprintf(priv->ip, sizeof(priv->ip), "%s:%hu", ip, port); |
2086 | 209 |
4617 | 210 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIM_ESTABLISHED))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
211 ret = userfunc(sess, NULL, newconn, cur); |
2086 | 212 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
213 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) { |
4617 | 214 } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
215 aim_rxcallback_t userfunc; |
2086 | 216 |
4617 | 217 if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, AIM_CB_OFT_ESTABLISHED))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
218 ret = userfunc(sess, NULL, newconn, cur); |
3630 | 219 |
4617 | 220 } else { |
221 faimdprintf(sess, 1,"Got a connection on a listener that's not rendezvous. Closing connection.\n"); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
222 aim_conn_close(newconn); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
223 ret = -1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
224 } |
2086 | 225 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
226 return ret; |
2086 | 227 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
228 |
2086 | 229 /** |
4617 | 230 * Send client-to-client typing notification over an established direct connection. |
2086 | 231 * |
4617 | 232 * @param sess The session. |
233 * @param conn The already-connected ODC connection. | |
4870 | 234 * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and |
235 * 0x0000 sends "stopped." | |
4617 | 236 * @return Return 0 if no errors, otherwise return the error number. |
2086 | 237 */ |
4617 | 238 faim_export int aim_odc_send_typing(aim_session_t *sess, aim_conn_t *conn, int typing) |
2086 | 239 { |
4617 | 240 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
241 aim_frame_t *fr; |
3952 | 242 aim_bstream_t *hdrbs; |
243 fu8_t *hdr; | |
244 int hdrlen = 0x44; | |
2086 | 245 |
3952 | 246 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
247 return -EINVAL; | |
2086 | 248 |
4870 | 249 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, 0))) |
3952 | 250 return -ENOMEM; |
251 memcpy(fr->hdr.rend.magic, "ODC2", 4); | |
8934 | 252 fr->hdr.rend.hdrlen = hdrlen + 8; |
2086 | 253 |
3952 | 254 if (!(hdr = calloc(1, hdrlen))) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
255 aim_frame_destroy(fr); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
256 return -ENOMEM; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
257 } |
3952 | 258 |
259 hdrbs = &(fr->data); | |
260 aim_bstream_init(hdrbs, hdr, hdrlen); | |
2086 | 261 |
3952 | 262 aimbs_put16(hdrbs, 0x0006); |
263 aimbs_put16(hdrbs, 0x0000); | |
264 aimbs_putraw(hdrbs, intdata->cookie, 8); | |
265 aimbs_put16(hdrbs, 0x0000); | |
266 aimbs_put16(hdrbs, 0x0000); | |
267 aimbs_put16(hdrbs, 0x0000); | |
268 aimbs_put16(hdrbs, 0x0000); | |
269 aimbs_put32(hdrbs, 0x00000000); | |
270 aimbs_put16(hdrbs, 0x0000); | |
271 aimbs_put16(hdrbs, 0x0000); | |
272 aimbs_put16(hdrbs, 0x0000); | |
2086 | 273 |
4870 | 274 if (typing == 0x0002) |
275 aimbs_put16(hdrbs, 0x0002 | 0x0008); | |
276 else if (typing == 0x0001) | |
277 aimbs_put16(hdrbs, 0x0002 | 0x0004); | |
278 else | |
279 aimbs_put16(hdrbs, 0x0002); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
280 |
3952 | 281 aimbs_put16(hdrbs, 0x0000); |
282 aimbs_put16(hdrbs, 0x0000); | |
283 aimbs_putraw(hdrbs, sess->sn, strlen(sess->sn)); | |
4617 | 284 |
3952 | 285 aim_bstream_setpos(hdrbs, 52); /* bleeehh */ |
2086 | 286 |
3952 | 287 aimbs_put8(hdrbs, 0x00); |
288 aimbs_put16(hdrbs, 0x0000); | |
289 aimbs_put16(hdrbs, 0x0000); | |
290 aimbs_put16(hdrbs, 0x0000); | |
291 aimbs_put16(hdrbs, 0x0000); | |
292 aimbs_put16(hdrbs, 0x0000); | |
293 aimbs_put16(hdrbs, 0x0000); | |
294 aimbs_put16(hdrbs, 0x0000); | |
295 aimbs_put8(hdrbs, 0x00); | |
2086 | 296 |
3952 | 297 /* end of hdr */ |
2086 | 298 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
299 aim_tx_enqueue(sess, fr); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
300 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
301 return 0; |
4617 | 302 } |
2086 | 303 |
2993 | 304 /** |
4617 | 305 * Send client-to-client IM over an established direct connection. |
306 * Call this just like you would aim_send_im, to send a directim. | |
2993 | 307 * |
4617 | 308 * @param sess The session. |
309 * @param conn The already-connected ODC connection. | |
310 * @param msg Null-terminated string to send. | |
311 * @param len The length of the message to send, including binary data. | |
312 * @param encoding 0 for ascii, 2 for Unicode, 3 for ISO 8859-1. | |
4870 | 313 * @param isawaymsg 0 if this is not an auto-response, 1 if it is. |
4617 | 314 * @return Return 0 if no errors, otherwise return the error number. |
2993 | 315 */ |
4870 | 316 faim_export int aim_odc_send_im(aim_session_t *sess, aim_conn_t *conn, const char *msg, int len, int encoding, int isawaymsg) |
2993 | 317 { |
318 aim_frame_t *fr; | |
3952 | 319 aim_bstream_t *hdrbs; |
4617 | 320 struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; |
3952 | 321 int hdrlen = 0x44; |
322 fu8_t *hdr; | |
2993 | 323 |
4617 | 324 if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !msg) |
325 return -EINVAL; | |
2993 | 326 |
4875 | 327 if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, 0))) |
4617 | 328 return -ENOMEM; |
2993 | 329 |
3952 | 330 memcpy(fr->hdr.rend.magic, "ODC2", 4); |
8934 | 331 fr->hdr.rend.hdrlen = hdrlen + 8; |
4617 | 332 |
3952 | 333 if (!(hdr = calloc(1, hdrlen + len))) { |
2993 | 334 aim_frame_destroy(fr); |
335 return -ENOMEM; | |
336 } | |
3952 | 337 |
338 hdrbs = &(fr->data); | |
339 aim_bstream_init(hdrbs, hdr, hdrlen + len); | |
340 | |
341 aimbs_put16(hdrbs, 0x0006); | |
342 aimbs_put16(hdrbs, 0x0000); | |
343 aimbs_putraw(hdrbs, intdata->cookie, 8); | |
344 aimbs_put16(hdrbs, 0x0000); | |
345 aimbs_put16(hdrbs, 0x0000); | |
346 aimbs_put16(hdrbs, 0x0000); | |
347 aimbs_put16(hdrbs, 0x0000); | |
348 aimbs_put32(hdrbs, len); | |
349 aimbs_put16(hdrbs, encoding); | |
350 aimbs_put16(hdrbs, 0x0000); | |
351 aimbs_put16(hdrbs, 0x0000); | |
4617 | 352 |
4870 | 353 /* flags - used for typing notification and to mark if this is an away message */ |
354 aimbs_put16(hdrbs, 0x0000 | isawaymsg); | |
3952 | 355 |
356 aimbs_put16(hdrbs, 0x0000); | |
357 aimbs_put16(hdrbs, 0x0000); | |
358 aimbs_putraw(hdrbs, sess->sn, strlen(sess->sn)); | |
359 | |
360 aim_bstream_setpos(hdrbs, 52); /* bleeehh */ | |
361 | |
362 aimbs_put8(hdrbs, 0x00); | |
363 aimbs_put16(hdrbs, 0x0000); | |
364 aimbs_put16(hdrbs, 0x0000); | |
365 aimbs_put16(hdrbs, 0x0000); | |
366 aimbs_put16(hdrbs, 0x0000); | |
367 aimbs_put16(hdrbs, 0x0000); | |
368 aimbs_put16(hdrbs, 0x0000); | |
369 aimbs_put16(hdrbs, 0x0000); | |
370 aimbs_put8(hdrbs, 0x00); | |
4617 | 371 |
2993 | 372 /* end of hdr2 */ |
4617 | 373 |
374 #if 0 /* XXX - this is how you send buddy icon info... */ | |
375 aimbs_put16(hdrbs, 0x0008); | |
376 aimbs_put16(hdrbs, 0x000c); | |
377 aimbs_put16(hdrbs, 0x0000); | |
378 aimbs_put16(hdrbs, 0x1466); | |
379 aimbs_put16(hdrbs, 0x0001); | |
380 aimbs_put16(hdrbs, 0x2e0f); | |
381 aimbs_put16(hdrbs, 0x393e); | |
382 aimbs_put16(hdrbs, 0xcac8); | |
2993 | 383 #endif |
3952 | 384 aimbs_putraw(hdrbs, msg, len); |
4617 | 385 |
2993 | 386 aim_tx_enqueue(sess, fr); |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
387 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
388 return 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
389 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
390 |
2086 | 391 /** |
4617 | 392 * Get the screen name of the peer of a direct connection. |
8983 | 393 * |
4617 | 394 * @param conn The ODC connection. |
395 * @return The screen name of the dude, or NULL if there was an anomaly. | |
2086 | 396 */ |
4617 | 397 faim_export const char *aim_odc_getsn(aim_conn_t *conn) |
398 { | |
399 struct aim_odc_intdata *intdata; | |
3630 | 400 |
4617 | 401 if (!conn || !conn->internal) |
3952 | 402 return NULL; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
403 |
8983 | 404 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) || |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
405 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)) |
3952 | 406 return NULL; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
407 |
4617 | 408 intdata = (struct aim_odc_intdata *)conn->internal; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
409 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
410 return intdata->sn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
411 } |
2086 | 412 |
413 /** | |
8983 | 414 * Get the cookie of a direct connection. |
415 * | |
416 * @param conn The ODC connection. | |
417 * @return The cookie, an 8 byte unterminated string, or NULL if there was an anomaly. | |
418 */ | |
419 faim_export const char *aim_odc_getcookie(aim_conn_t *conn) | |
420 { | |
421 struct aim_odc_intdata *intdata; | |
422 | |
423 if (!conn || !conn->internal) | |
424 return NULL; | |
425 | |
426 if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) || | |
427 (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)) | |
428 return NULL; | |
429 | |
430 intdata = (struct aim_odc_intdata *)conn->internal; | |
431 | |
432 return intdata->cookie; | |
433 } | |
434 | |
435 /** | |
4617 | 436 * Find the conn of a direct connection with the given buddy. |
437 * | |
438 * @param sess The session. | |
439 * @param sn The screen name of the buddy whose direct connection you want to find. | |
440 * @return The conn for the direct connection with the given buddy, or NULL if no | |
441 * connection was found. | |
442 */ | |
443 faim_export aim_conn_t *aim_odc_getconn(aim_session_t *sess, const char *sn) | |
444 { | |
445 aim_conn_t *cur; | |
446 struct aim_odc_intdata *intdata; | |
447 | |
448 if (!sess || !sn || !strlen(sn)) | |
449 return NULL; | |
450 | |
451 for (cur = sess->connlist; cur; cur = cur->next) { | |
452 if ((cur->type == AIM_CONN_TYPE_RENDEZVOUS) && (cur->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)) { | |
453 intdata = cur->internal; | |
454 if (!aim_sncmp(intdata->sn, sn)) | |
455 return cur; | |
456 } | |
457 } | |
458 | |
459 return NULL; | |
460 } | |
461 | |
462 /** | |
463 * For those times when we want to open up the direct connection channel ourselves. | |
464 * | |
8982 | 465 * You'll want to set up some kind of watcher on this socket. |
466 * When the state changes, call aim_handlerendconnection with | |
467 * the connection returned by this. aim_handlerendconnection | |
4617 | 468 * will accept the pending connection and stop listening. |
469 * | |
470 * @param sess The session | |
471 * @param sn The screen name to connect to. | |
472 * @return The new connection. | |
473 */ | |
8982 | 474 faim_export aim_conn_t *aim_odc_initiate(aim_session_t *sess, const char *sn, int listenfd, |
475 const fu8_t *localip, fu16_t port, const fu8_t *mycookie) | |
4617 | 476 { |
477 aim_conn_t *newconn; | |
478 aim_msgcookie_t *cookie; | |
479 struct aim_odc_intdata *priv; | |
480 fu8_t ck[8]; | |
481 | |
8982 | 482 if (!localip) |
4617 | 483 return NULL; |
484 | |
8982 | 485 if (mycookie) { |
486 memcpy(ck, mycookie, 8); | |
487 aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port); | |
488 } else | |
489 aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port); | |
4617 | 490 |
491 cookie = (aim_msgcookie_t *)calloc(1, sizeof(aim_msgcookie_t)); | |
492 memcpy(cookie->cookie, ck, 8); | |
493 cookie->type = AIM_COOKIETYPE_OFTIM; | |
494 | |
495 /* this one is for the cookie */ | |
496 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
497 | |
498 memcpy(priv->cookie, ck, 8); | |
499 strncpy(priv->sn, sn, sizeof(priv->sn)); | |
500 cookie->data = priv; | |
501 aim_cachecookie(sess, cookie); | |
502 | |
503 /* XXX - switch to aim_cloneconn()? */ | |
504 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER, NULL))) { | |
505 close(listenfd); | |
506 return NULL; | |
507 } | |
508 | |
509 /* this one is for the conn */ | |
510 priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
511 | |
512 memcpy(priv->cookie, ck, 8); | |
513 strncpy(priv->sn, sn, sizeof(priv->sn)); | |
514 | |
515 newconn->fd = listenfd; | |
516 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
517 newconn->internal = priv; | |
518 newconn->lastactivity = time(NULL); | |
519 | |
520 return newconn; | |
521 } | |
522 | |
523 /** | |
524 * Connect directly to the given buddy for directim. | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
525 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
526 * This is a wrapper for aim_newconn. |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
527 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
528 * If addr is NULL, the socket is not created, but the connection is |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
529 * allocated and setup to connect. |
2086 | 530 * |
4617 | 531 * @param sess The Godly session. |
532 * @param sn The screen name we're connecting to. I hope it's a girl... | |
533 * @param addr Address to connect to. | |
534 * @return The new connection. | |
2086 | 535 */ |
4617 | 536 faim_export aim_conn_t *aim_odc_connect(aim_session_t *sess, const char *sn, const char *addr, const fu8_t *cookie) |
537 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
538 aim_conn_t *newconn; |
4617 | 539 struct aim_odc_intdata *intdata; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
540 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
541 if (!sess || !sn) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
542 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
543 |
5146 | 544 if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata)))) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
545 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
546 memcpy(intdata->cookie, cookie, 8); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
547 strncpy(intdata->sn, sn, sizeof(intdata->sn)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
548 if (addr) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
549 strncpy(intdata->ip, addr, sizeof(intdata->ip)); |
2086 | 550 |
4617 | 551 /* XXX - verify that non-blocking connects actually work */ |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
552 if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS, addr))) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
553 free(intdata); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
554 return NULL; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
555 } |
2086 | 556 |
4617 | 557 newconn->internal = intdata; |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
558 newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
559 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
560 return newconn; |
4617 | 561 } |
2086 | 562 |
563 /** | |
4826 | 564 * Sometimes you just don't know with these kinds of people. |
565 * | |
566 * @param sess The session. | |
567 * @param conn The ODC connection of the incoming data. | |
568 * @param frr The frame allocated for the incoming data. | |
569 * @param bs It stands for "bologna sandwich." | |
570 * @return Return 0 if no errors, otherwise return the error number. | |
571 */ | |
572 static int handlehdr_odc(aim_session_t *sess, aim_conn_t *conn, aim_frame_t *frr, aim_bstream_t *bs) | |
573 { | |
574 aim_frame_t fr; | |
4870 | 575 int ret = 0; |
4826 | 576 aim_rxcallback_t userfunc; |
577 fu32_t payloadlength; | |
578 fu16_t flags, encoding; | |
579 char *snptr = NULL; | |
580 | |
581 fr.conn = conn; | |
582 | |
583 /* AAA - ugly */ | |
584 aim_bstream_setpos(bs, 20); | |
585 payloadlength = aimbs_get32(bs); | |
586 | |
587 aim_bstream_setpos(bs, 24); | |
588 encoding = aimbs_get16(bs); | |
589 | |
590 aim_bstream_setpos(bs, 30); | |
591 flags = aimbs_get16(bs); | |
592 | |
593 aim_bstream_setpos(bs, 36); | |
594 /* XXX - create an aimbs_getnullstr function? */ | |
4913 | 595 snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */ |
4826 | 596 |
597 faimdprintf(sess, 2, "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr); | |
598 | |
4870 | 599 if (flags & 0x0008) { |
600 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) | |
601 ret = userfunc(sess, &fr, snptr, 2); | |
602 } else if (flags & 0x0004) { | |
603 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) | |
604 ret = userfunc(sess, &fr, snptr, 1); | |
605 } else { | |
4826 | 606 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMTYPING))) |
607 ret = userfunc(sess, &fr, snptr, 0); | |
4870 | 608 } |
4826 | 609 |
8000 | 610 if ((payloadlength != 0) && (payloadlength != UINT_MAX)) { |
4870 | 611 char *msg; |
4826 | 612 int recvd = 0; |
4870 | 613 int i, isawaymsg; |
614 | |
615 isawaymsg = flags & 0x0001; | |
4826 | 616 |
4895 | 617 if (!(msg = calloc(1, payloadlength+1))) { |
618 free(snptr); | |
4870 | 619 return -ENOMEM; |
4895 | 620 } |
4870 | 621 |
4826 | 622 while (payloadlength - recvd) { |
623 if (payloadlength - recvd >= 1024) | |
4870 | 624 i = aim_recv(conn->fd, &msg[recvd], 1024); |
4826 | 625 else |
4870 | 626 i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); |
4826 | 627 if (i <= 0) { |
628 free(msg); | |
4895 | 629 free(snptr); |
4826 | 630 return -1; |
631 } | |
632 recvd = recvd + i; | |
633 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) | |
4870 | 634 ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); |
4826 | 635 } |
636 | |
4870 | 637 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, AIM_CB_OFT_DIRECTIMINCOMING))) |
638 ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); | |
4826 | 639 |
640 free(msg); | |
641 } | |
642 | |
4895 | 643 free(snptr); |
644 | |
4870 | 645 return ret; |
4826 | 646 } |
647 | |
5146 | 648 faim_export struct aim_oft_info *aim_oft_createinfo(aim_session_t *sess, const fu8_t *cookie, const char *sn, const char *ip, fu16_t port, fu32_t size, fu32_t modtime, char *filename) |
649 { | |
650 struct aim_oft_info *new; | |
651 | |
652 if (!sess) | |
653 return NULL; | |
654 | |
655 if (!(new = (struct aim_oft_info *)calloc(1, sizeof(struct aim_oft_info)))) | |
656 return NULL; | |
657 | |
658 new->sess = sess; | |
659 if (cookie) | |
660 memcpy(new->cookie, cookie, 8); | |
661 if (ip) | |
662 new->clientip = strdup(ip); | |
663 if (sn) | |
664 new->sn = strdup(sn); | |
665 new->port = port; | |
666 new->fh.totfiles = 1; | |
667 new->fh.filesleft = 1; | |
668 new->fh.totparts = 1; | |
669 new->fh.partsleft = 1; | |
670 new->fh.totsize = size; | |
671 new->fh.size = size; | |
672 new->fh.modtime = modtime; | |
673 new->fh.checksum = 0xffff0000; | |
674 new->fh.rfrcsum = 0xffff0000; | |
675 new->fh.rfcsum = 0xffff0000; | |
676 new->fh.recvcsum = 0xffff0000; | |
677 strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31); | |
8446 | 678 if (filename) { |
5146 | 679 strncpy(new->fh.name, filename, 63); |
8446 | 680 new->fh.name[63] = '\0'; |
681 } | |
5146 | 682 |
683 new->next = sess->oft_info; | |
684 sess->oft_info = new; | |
685 | |
686 return new; | |
687 } | |
688 | |
689 /** | |
690 * Remove the given oft_info struct from the oft_info linked list, and | |
691 * then free its memory. | |
692 * | |
693 * @param sess The session. | |
694 * @param oft_info The aim_oft_info struct that we're destroying. | |
695 * @return Return 0 if no errors, otherwise return the error number. | |
696 */ | |
697 faim_export int aim_oft_destroyinfo(struct aim_oft_info *oft_info) | |
698 { | |
699 aim_session_t *sess; | |
700 | |
701 if (!oft_info || !(sess = oft_info->sess)) | |
702 return -EINVAL; | |
703 | |
704 if (sess->oft_info && (sess->oft_info == oft_info)) { | |
705 sess->oft_info = sess->oft_info->next; | |
706 } else { | |
707 struct aim_oft_info *cur; | |
708 for (cur=sess->oft_info; (cur->next && (cur->next!=oft_info)); cur=cur->next); | |
709 if (cur->next) | |
710 cur->next = cur->next->next; | |
711 } | |
712 | |
713 free(oft_info->sn); | |
714 free(oft_info->proxyip); | |
715 free(oft_info->clientip); | |
716 free(oft_info->verifiedip); | |
717 free(oft_info); | |
718 | |
719 return 0; | |
720 } | |
721 | |
4826 | 722 /** |
4617 | 723 * Creates a listener socket so the other dude can connect to us. |
2086 | 724 * |
4617 | 725 * You'll want to set up some kind of watcher on this socket. |
726 * When the state changes, call aim_handlerendconnection with | |
727 * the connection returned by this. aim_handlerendconnection | |
728 * will accept the pending connection and stop listening. | |
2086 | 729 * |
4617 | 730 * @param sess The session. |
5146 | 731 * @param oft_info File transfer information associated with this |
732 * connection. | |
733 * @return Return 0 if no errors, otherwise return the error number. | |
2086 | 734 */ |
8240 | 735 faim_export int aim_sendfile_listen(aim_session_t *sess, struct aim_oft_info *oft_info, int listenfd) |
2086 | 736 { |
5146 | 737 if (!oft_info) |
738 return -EINVAL; | |
2086 | 739 |
5146 | 740 if (!(oft_info->conn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER, NULL))) { |
4617 | 741 close(listenfd); |
5146 | 742 return -ENOMEM; |
3952 | 743 } |
3630 | 744 |
5146 | 745 oft_info->conn->fd = listenfd; |
746 oft_info->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE; | |
747 oft_info->conn->lastactivity = time(NULL); | |
3952 | 748 |
5146 | 749 return 0; |
4617 | 750 } |
3630 | 751 |
4617 | 752 /** |
753 * Extract an &aim_fileheader_t from the given buffer. | |
754 * | |
755 * @param bs The should be from an incoming rendezvous packet. | |
756 * @return A pointer to new struct on success, or NULL on error. | |
757 */ | |
758 static struct aim_fileheader_t *aim_oft_getheader(aim_bstream_t *bs) | |
759 { | |
760 struct aim_fileheader_t *fh; | |
3630 | 761 |
4617 | 762 if (!(fh = calloc(1, sizeof(struct aim_fileheader_t)))) |
763 return NULL; | |
3630 | 764 |
4617 | 765 /* The bstream should be positioned after the hdrtype. */ |
766 aimbs_getrawbuf(bs, fh->bcookie, 8); | |
767 fh->encrypt = aimbs_get16(bs); | |
768 fh->compress = aimbs_get16(bs); | |
769 fh->totfiles = aimbs_get16(bs); | |
770 fh->filesleft = aimbs_get16(bs); | |
771 fh->totparts = aimbs_get16(bs); | |
772 fh->partsleft = aimbs_get16(bs); | |
773 fh->totsize = aimbs_get32(bs); | |
774 fh->size = aimbs_get32(bs); | |
775 fh->modtime = aimbs_get32(bs); | |
776 fh->checksum = aimbs_get32(bs); | |
777 fh->rfrcsum = aimbs_get32(bs); | |
778 fh->rfsize = aimbs_get32(bs); | |
779 fh->cretime = aimbs_get32(bs); | |
780 fh->rfcsum = aimbs_get32(bs); | |
781 fh->nrecvd = aimbs_get32(bs); | |
782 fh->recvcsum = aimbs_get32(bs); | |
783 aimbs_getrawbuf(bs, fh->idstring, 32); | |
784 fh->flags = aimbs_get8(bs); | |
785 fh->lnameoffset = aimbs_get8(bs); | |
786 fh->lsizeoffset = aimbs_get8(bs); | |
787 aimbs_getrawbuf(bs, fh->dummy, 69); | |
788 aimbs_getrawbuf(bs, fh->macfileinfo, 16); | |
789 fh->nencode = aimbs_get16(bs); | |
790 fh->nlanguage = aimbs_get16(bs); | |
791 aimbs_getrawbuf(bs, fh->name, 64); /* XXX - filenames longer than 64B */ | |
8446 | 792 fh->name[63] = '\0'; |
2086 | 793 |
4617 | 794 return fh; |
795 } | |
3630 | 796 |
4617 | 797 /** |
798 * Fills a buffer with network-order fh data | |
799 * | |
800 * @param bs A bstream to fill -- automatically initialized | |
801 * @param fh A struct aim_fileheader_t to get data from. | |
802 * @return Return non-zero on error. | |
803 */ | |
804 static int aim_oft_buildheader(aim_bstream_t *bs, struct aim_fileheader_t *fh) | |
805 { | |
806 fu8_t *hdr; | |
3630 | 807 |
4617 | 808 if (!bs || !fh) |
809 return -EINVAL; | |
3952 | 810 |
4617 | 811 if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8))) |
812 return -ENOMEM; | |
3630 | 813 |
4617 | 814 aim_bstream_init(bs, hdr, 0x100 - 8); |
815 aimbs_putraw(bs, fh->bcookie, 8); | |
816 aimbs_put16(bs, fh->encrypt); | |
817 aimbs_put16(bs, fh->compress); | |
818 aimbs_put16(bs, fh->totfiles); | |
819 aimbs_put16(bs, fh->filesleft); | |
820 aimbs_put16(bs, fh->totparts); | |
821 aimbs_put16(bs, fh->partsleft); | |
822 aimbs_put32(bs, fh->totsize); | |
823 aimbs_put32(bs, fh->size); | |
824 aimbs_put32(bs, fh->modtime); | |
825 aimbs_put32(bs, fh->checksum); | |
826 aimbs_put32(bs, fh->rfrcsum); | |
827 aimbs_put32(bs, fh->rfsize); | |
828 aimbs_put32(bs, fh->cretime); | |
829 aimbs_put32(bs, fh->rfcsum); | |
830 aimbs_put32(bs, fh->nrecvd); | |
831 aimbs_put32(bs, fh->recvcsum); | |
832 aimbs_putraw(bs, fh->idstring, 32); | |
833 aimbs_put8(bs, fh->flags); | |
834 aimbs_put8(bs, fh->lnameoffset); | |
835 aimbs_put8(bs, fh->lsizeoffset); | |
836 aimbs_putraw(bs, fh->dummy, 69); | |
837 aimbs_putraw(bs, fh->macfileinfo, 16); | |
838 aimbs_put16(bs, fh->nencode); | |
839 aimbs_put16(bs, fh->nlanguage); | |
840 aimbs_putraw(bs, fh->name, 64); /* XXX - filenames longer than 64B */ | |
3952 | 841 |
4617 | 842 return 0; |
843 } | |
2086 | 844 |
4617 | 845 /** |
846 * Create an OFT packet based on the given information, and send it on its merry way. | |
847 * | |
848 * @param sess The session. | |
5146 | 849 * @param type The subtype of the OFT packet we're sending. |
850 * @param oft_info The aim_oft_info struct with the connection and OFT | |
851 * info we're sending. | |
4617 | 852 * @return Return 0 if no errors, otherwise return the error number. |
853 */ | |
5146 | 854 faim_export int aim_oft_sendheader(aim_session_t *sess, fu16_t type, struct aim_oft_info *oft_info) |
4617 | 855 { |
5146 | 856 aim_frame_t *fr; |
2086 | 857 |
5146 | 858 if (!sess || !oft_info || !oft_info->conn || (oft_info->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
4617 | 859 return -EINVAL; |
860 | |
5146 | 861 #if 0 |
4617 | 862 /* |
863 * If you are receiving a file, the cookie should be null, if you are sending a | |
864 * file, the cookie should be the same as the one used in the ICBM negotiation | |
865 * SNACs. | |
866 */ | |
3952 | 867 fh->lnameoffset = 0x1a; |
868 fh->lsizeoffset = 0x10; | |
2086 | 869 |
4617 | 870 /* apparently 0 is ASCII, 2 is UCS-2 */ |
871 /* it is likely that 3 is ISO 8859-1 */ | |
4826 | 872 /* I think "nlanguage" might be the same thing as "subenc" in im.c */ |
3952 | 873 fh->nencode = 0x0000; |
874 fh->nlanguage = 0x0000; | |
5146 | 875 #endif |
4617 | 876 |
5146 | 877 aim_oft_dirconvert_tostupid(oft_info->fh.name); |
2086 | 878 |
5146 | 879 if (!(fr = aim_tx_new(sess, oft_info->conn, AIM_FRAMETYPE_OFT, type, 0))) |
880 return -ENOMEM; | |
881 | |
882 if (aim_oft_buildheader(&fr->data, &oft_info->fh) == -1) { | |
883 aim_frame_destroy(fr); | |
4617 | 884 return -ENOMEM; |
3952 | 885 } |
2086 | 886 |
5146 | 887 memcpy(fr->hdr.rend.magic, "OFT2", 4); |
8426 | 888 fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8; |
2086 | 889 |
5146 | 890 aim_tx_enqueue(sess, fr); |
3952 | 891 |
892 return 0; | |
2086 | 893 } |
894 | |
895 /** | |
4617 | 896 * Handle incoming data on a rendezvous connection. This is analogous to the |
897 * consumesnac function in rxhandlers.c, and I really think this should probably | |
898 * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. | |
899 * | |
900 * @param sess The session. | |
901 * @param fr The frame allocated for the incoming data. | |
902 * @return Return 0 if the packet was handled correctly, otherwise return the | |
903 * error number. | |
3771 | 904 */ |
3952 | 905 faim_internal int aim_rxdispatch_rendezvous(aim_session_t *sess, aim_frame_t *fr) |
2086 | 906 { |
3952 | 907 aim_conn_t *conn = fr->conn; |
4617 | 908 int ret = 1; |
2086 | 909 |
4617 | 910 if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { |
911 if (fr->hdr.rend.type == 0x0001) | |
912 ret = handlehdr_odc(sess, conn, fr, &fr->data); | |
913 else | |
914 faimdprintf(sess, 0, "faim: ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type); | |
2086 | 915 |
4826 | 916 } else { |
4617 | 917 aim_rxcallback_t userfunc; |
918 struct aim_fileheader_t *header = aim_oft_getheader(&fr->data); | |
919 aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */ | |
3771 | 920 |
4617 | 921 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type))) |
922 ret = userfunc(sess, fr, conn, header->bcookie, header); | |
923 | |
924 free(header); | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
925 } |
4617 | 926 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
927 if (ret == -1) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
928 aim_conn_close(conn); |
2086 | 929 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
930 return ret; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
931 } |