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