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