Sun, 12 Feb 2006 21:27:04 +0000
[gaim-migrate @ 15619]
More cleaning up of oscar. Renamed some functions to be more clear.
Got rid of some stuff that wasn't used. Inlined some small things
in conn.c that were only used once.
The goals of all this are
1. Non-blocking I/O for all connections
2. p2p stuff won't use the same struct as oscar connections, because
that's stupid
3. The oscar PRPL should be less scary
| 13235 | 1 | /* |
| 2 | * Gaim's oscar protocol plugin | |
| 3 | * This file is the legal property of its developers. | |
| 4 | * Please see the AUTHORS file distributed alongside this file. | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation; either | |
| 9 | * version 2 of the License, or (at your option) any later version. | |
| 10 | * | |
| 11 | * This library is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | * Lesser General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU Lesser General Public | |
| 17 | * License along with this library; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | */ | |
| 20 | ||
| 21 | /* | |
| 22 | * Oscar File transfer (OFT) and Oscar Direct Connect (ODC). | |
| 23 | * (ODC is also referred to as DirectIM and IM Image.) | |
| 24 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
25 | * There are a few static helper functions at the top, then |
| 13235 | 26 | * ODC stuff, then ft stuff. |
| 27 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
28 | * I feel like this is a good place to explain OFT, so I'm going to |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
29 | * do just that. Each OFT packet has a header type. I guess this |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
30 | * is pretty similar to the subtype of a SNAC packet. The type |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
31 | * basically tells the other client the meaning of the OFT packet. |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
32 | * There are two distinct types of file transfer, which I usually |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
33 | * call "sendfile" and "getfile." Sendfile is when you send a file |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
34 | * to another AIM user. Getfile is when you share a group of files, |
| 13235 | 35 | * and other users request that you send them the files. |
| 36 | * | |
| 37 | * A typical sendfile file transfer goes like this: | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
38 | * 1) Sender sends a channel 2 ICBM telling the other user that |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
39 | * we want to send them a file. At the same time, we open a |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
40 | * listener socket (this should be done before sending the |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
41 | * ICBM) on some port, and wait for them to connect to us. |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
42 | * The ICBM we sent should contain our IP address and the port |
| 13235 | 43 | * number that we're listening on. |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
44 | * 2) The receiver connects to the sender on the given IP address |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
45 | * and port. After the connection is established, the receiver |
| 13235 | 46 | * sends an ICBM signifying that we are ready and waiting. |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
47 | * 3) The sender sends an OFT PROMPT message over the OFT |
| 13235 | 48 | * connection. |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
49 | * 4) The receiver of the file sends back an exact copy of this |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
50 | * OFT packet, except the cookie is filled in with the cookie |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
51 | * from the ICBM. I think this might be an attempt to verify |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
52 | * that the user that is connected is actually the guy that |
| 13235 | 53 | * we sent the ICBM to. Oh, I've been calling this the ACK. |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
54 | * 5) The sender starts sending raw data across the connection |
| 13235 | 55 | * until the entire file has been sent. |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
56 | * 6) The receiver knows the file is finished because the sender |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
57 | * sent the file size in an earlier OFT packet. So then the |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
58 | * receiver sends the DONE thingy (after filling in the |
| 13235 | 59 | * "received" checksum and size) and closes the connection. |
| 60 | */ | |
| 61 | ||
| 62 | #ifdef HAVE_CONFIG_H | |
| 63 | #include <config.h> | |
| 64 | #endif | |
| 65 | ||
| 66 | #include "oscar.h" | |
| 67 | #include "peer.h" | |
| 68 | ||
| 69 | #ifndef _WIN32 | |
| 70 | #include <stdio.h> | |
| 71 | #include <netdb.h> | |
| 72 | #include <sys/socket.h> | |
| 73 | #include <netinet/in.h> | |
| 74 | #include <sys/utsname.h> /* for aim_odc_initiate */ | |
| 75 | #include <arpa/inet.h> /* for inet_ntoa */ | |
| 76 | #include <limits.h> /* for UINT_MAX */ | |
| 77 | #endif | |
| 78 | ||
| 79 | #ifdef _WIN32 | |
| 80 | #include "win32dep.h" | |
| 81 | #endif | |
| 82 | ||
| 83 | /* | |
| 84 | * I really want to switch all our networking code to using IPv6 only, | |
| 85 | * but that really isn't a good idea at all. Evan S. of Adium says | |
| 86 | * OS X sets all connections as "AF_INET6/PF_INET6," even if there is | |
| 87 | * nothing inherently IPv6 about them. And I feel like Linux kernel | |
| 88 | * 2.6.5 is doing the same thing. So we REALLY should accept | |
| 89 | * connections if they're showing up as IPv6. Old OSes (Solaris?) | |
| 90 | * that might not have full IPv6 support yet will fail if we try | |
| 91 | * to use PF_INET6 but it isn't defined. --Mark Doliner | |
| 92 | */ | |
| 93 | #ifndef PF_INET6 | |
| 94 | #define PF_INET6 PF_INET | |
| 95 | #endif | |
| 96 | ||
| 97 | struct aim_odc_intdata { | |
| 98 | guint8 cookie[8]; | |
| 99 | char sn[MAXSNLEN+1]; | |
| 100 | char ip[22]; | |
| 101 | }; | |
| 102 | ||
| 103 | /** | |
| 104 | * Convert the directory separator from / (0x2f) to ^A (0x01) | |
| 105 | * | |
| 106 | * @param name The filename to convert. | |
| 107 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
108 | static void |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
109 | aim_oft_dirconvert_tostupid(char *name) |
| 13235 | 110 | { |
| 111 | while (name[0]) { | |
| 112 | if (name[0] == 0x01) | |
| 113 | name[0] = G_DIR_SEPARATOR; | |
| 114 | name++; | |
| 115 | } | |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 119 | * Convert the directory separator from ^A (0x01) to / (0x2f) | |
| 120 | * | |
| 121 | * @param name The filename to convert. | |
| 122 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
123 | static void |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
124 | aim_oft_dirconvert_fromstupid(char *name) |
| 13235 | 125 | { |
| 126 | while (name[0]) { | |
| 127 | if (name[0] == G_DIR_SEPARATOR) | |
| 128 | name[0] = 0x01; | |
| 129 | name++; | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Calculate oft checksum of buffer | |
| 135 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
136 | * Prevcheck should be 0xFFFF0000 when starting a checksum of a file. The |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
137 | * checksum is kind of a rolling checksum thing, so each time you get bytes |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
138 | * of a file you just call this puppy and it updates the checksum. You can |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
139 | * calculate the checksum of an entire file by calling this in a while or a |
| 13235 | 140 | * for loop, or something. |
| 141 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
142 | * Thanks to Graham Booker for providing this improved checksum routine, |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
143 | * which is simpler and should be more accurate than Josh Myer's original |
| 13235 | 144 | * code. -- wtm |
| 145 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
146 | * This algorithm works every time I have tried it. The other fails |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
147 | * sometimes. So, AOL who thought this up? It has got to be the weirdest |
| 13235 | 148 | * checksum I have ever seen. |
| 149 | * | |
| 150 | * @param buffer Buffer of data to checksum. Man I'd like to buff her... | |
| 151 | * @param bufsize Size of buffer. | |
| 152 | * @param prevcheck Previous checksum. | |
| 153 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
154 | guint32 |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
155 | aim_oft_checksum_chunk(const guint8 *buffer, int bufferlen, guint32 prevcheck) |
| 13235 | 156 | { |
| 157 | guint32 check = (prevcheck >> 16) & 0xffff, oldcheck; | |
| 158 | int i; | |
| 159 | unsigned short val; | |
| 160 | ||
| 161 | for (i=0; i<bufferlen; i++) { | |
| 162 | oldcheck = check; | |
| 163 | if (i&1) | |
| 164 | val = buffer[i]; | |
| 165 | else | |
| 166 | val = buffer[i] << 8; | |
| 167 | check -= val; | |
| 168 | /* | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
169 | * The following appears to be necessary.... It happens |
| 13235 | 170 | * every once in a while and the checksum doesn't fail. |
| 171 | */ | |
| 172 | if (check > oldcheck) | |
| 173 | check--; | |
| 174 | } | |
| 175 | check = ((check & 0x0000ffff) + (check >> 16)); | |
| 176 | check = ((check & 0x0000ffff) + (check >> 16)); | |
| 177 | return check << 16; | |
| 178 | } | |
| 179 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
180 | guint32 |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
181 | aim_oft_checksum_file(char *filename) |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
182 | { |
| 13235 | 183 | FILE *fd; |
| 184 | guint32 checksum = 0xffff0000; | |
| 185 | ||
| 186 | if ((fd = fopen(filename, "rb"))) { | |
| 187 | int bytes; | |
| 188 | guint8 buffer[1024]; | |
| 189 | ||
| 190 | while ((bytes = fread(buffer, 1, 1024, fd))) | |
| 191 | checksum = aim_oft_checksum_chunk(buffer, bytes, checksum); | |
| 192 | fclose(fd); | |
| 193 | } | |
| 194 | ||
| 195 | return checksum; | |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * After establishing a listening socket, this is called to accept a connection. It | |
| 200 | * clones the conn used by the listener, and passes both of these to a signal handler. | |
| 201 | * The signal handler should close the listener conn and keep track of the new conn, | |
| 202 | * since this is what is used for file transfers and what not. | |
| 203 | * | |
| 204 | * @param sess The session. | |
| 205 | * @param cur The conn the incoming connection is on. | |
| 206 | * @return Return 0 if no errors, otherwise return the error number. | |
| 207 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
208 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
209 | aim_handlerendconnect(OscarSession *sess, OscarConnection *cur) |
| 13235 | 210 | { |
| 211 | int acceptfd = 0; | |
| 212 | struct sockaddr addr; | |
| 213 | socklen_t addrlen = sizeof(addr); | |
| 214 | int ret = 0; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
215 | OscarConnection *newconn; |
| 13235 | 216 | char ip[20]; |
| 217 | unsigned short port; | |
| 218 | ||
| 219 | if ((acceptfd = accept(cur->fd, &addr, &addrlen)) == -1) | |
| 220 | return 0; /* not an error */ | |
| 221 | ||
| 222 | if ((addr.sa_family != PF_INET) && (addr.sa_family != PF_INET6)) { | |
| 223 | close(acceptfd); | |
| 224 | aim_conn_close(cur); | |
| 225 | return -1; | |
| 226 | } | |
| 227 | ||
| 228 | strncpy(ip, inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr), sizeof(ip)); | |
| 229 | port = ntohs(((struct sockaddr_in *)&addr)->sin_port); | |
| 230 | ||
| 231 | if (!(newconn = aim_cloneconn(sess, cur))) { | |
| 232 | close(acceptfd); | |
| 233 | aim_conn_close(cur); | |
| 234 | return -ENOMEM; | |
| 235 | } | |
| 236 | ||
| 237 | newconn->type = AIM_CONN_TYPE_RENDEZVOUS; | |
| 238 | newconn->fd = acceptfd; | |
| 239 | ||
| 240 | if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { | |
| 241 | aim_rxcallback_t userfunc; | |
| 242 | struct aim_odc_intdata *priv; | |
| 243 | ||
| 244 | priv = (struct aim_odc_intdata *)(newconn->internal = cur->internal); | |
| 245 | cur->internal = NULL; | |
| 246 | snprintf(priv->ip, sizeof(priv->ip), "%s:%hu", ip, port); | |
| 247 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
248 | if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIM_ESTABLISHED))) |
| 13235 | 249 | ret = userfunc(sess, NULL, newconn, cur); |
| 250 | ||
| 251 | } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_GETFILE) { | |
| 252 | } else if (newconn->subtype == AIM_CONN_SUBTYPE_OFT_SENDFILE) { | |
| 253 | aim_rxcallback_t userfunc; | |
| 254 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
255 | if ((userfunc = aim_callhandler(sess, newconn, AIM_CB_FAM_OFT, PEER_TYPE_ESTABLISHED))) |
| 13235 | 256 | ret = userfunc(sess, NULL, newconn, cur); |
| 257 | ||
| 258 | } else { | |
| 259 | gaim_debug_warning("oscar", "Got a connection on a listener that's not rendezvous. Closing connection.\n"); | |
| 260 | aim_conn_close(newconn); | |
| 261 | ret = -1; | |
| 262 | } | |
| 263 | ||
| 264 | return ret; | |
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * Send client-to-client typing notification over an established direct connection. | |
| 269 | * | |
| 270 | * @param sess The session. | |
| 271 | * @param conn The already-connected ODC connection. | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
272 | * @param typing If 0x0002, sends a "typing" message, 0x0001 sends "typed," and |
| 13235 | 273 | * 0x0000 sends "stopped." |
| 274 | * @return Return 0 if no errors, otherwise return the error number. | |
| 275 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
276 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
277 | aim_odc_send_typing(OscarSession *sess, OscarConnection *conn, int typing) |
| 13235 | 278 | { |
| 279 | struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
280 | FlapFrame *fr; |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
281 | ByteStream *hdrbs; |
| 13235 | 282 | guint8 *hdr; |
| 283 | int hdrlen = 0x44; | |
| 284 | ||
| 285 | if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS)) | |
| 286 | return -EINVAL; | |
| 287 | ||
|
13254
3fbb3f3efba7
[gaim-migrate @ 15619]
Mark Doliner <markdoliner@pidgin.im>
parents:
13253
diff
changeset
|
288 | if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_OFT, 0x0001, 0))) |
| 13235 | 289 | return -ENOMEM; |
| 290 | memcpy(fr->hdr.rend.magic, "ODC2", 4); | |
| 291 | fr->hdr.rend.hdrlen = hdrlen + 8; | |
| 292 | ||
| 293 | if (!(hdr = calloc(1, hdrlen))) { | |
| 294 | aim_frame_destroy(fr); | |
| 295 | return -ENOMEM; | |
| 296 | } | |
| 297 | ||
| 298 | hdrbs = &(fr->data); | |
| 299 | aim_bstream_init(hdrbs, hdr, hdrlen); | |
| 300 | ||
| 301 | aimbs_put16(hdrbs, 0x0006); | |
| 302 | aimbs_put16(hdrbs, 0x0000); | |
| 303 | aimbs_putraw(hdrbs, intdata->cookie, 8); | |
| 304 | aimbs_put16(hdrbs, 0x0000); | |
| 305 | aimbs_put16(hdrbs, 0x0000); | |
| 306 | aimbs_put16(hdrbs, 0x0000); | |
| 307 | aimbs_put16(hdrbs, 0x0000); | |
| 308 | aimbs_put32(hdrbs, 0x00000000); | |
| 309 | aimbs_put16(hdrbs, 0x0000); | |
| 310 | aimbs_put16(hdrbs, 0x0000); | |
| 311 | aimbs_put16(hdrbs, 0x0000); | |
| 312 | ||
| 313 | if (typing == 0x0002) | |
| 314 | aimbs_put16(hdrbs, 0x0002 | 0x0008); | |
| 315 | else if (typing == 0x0001) | |
| 316 | aimbs_put16(hdrbs, 0x0002 | 0x0004); | |
| 317 | else | |
| 318 | aimbs_put16(hdrbs, 0x0002); | |
| 319 | ||
| 320 | aimbs_put16(hdrbs, 0x0000); | |
| 321 | aimbs_put16(hdrbs, 0x0000); | |
| 322 | aimbs_putstr(hdrbs, sess->sn); | |
| 323 | ||
| 324 | aim_bstream_setpos(hdrbs, 52); /* bleeehh */ | |
| 325 | ||
| 326 | aimbs_put8(hdrbs, 0x00); | |
| 327 | aimbs_put16(hdrbs, 0x0000); | |
| 328 | aimbs_put16(hdrbs, 0x0000); | |
| 329 | aimbs_put16(hdrbs, 0x0000); | |
| 330 | aimbs_put16(hdrbs, 0x0000); | |
| 331 | aimbs_put16(hdrbs, 0x0000); | |
| 332 | aimbs_put16(hdrbs, 0x0000); | |
| 333 | aimbs_put16(hdrbs, 0x0000); | |
| 334 | aimbs_put8(hdrbs, 0x00); | |
| 335 | ||
| 336 | /* end of hdr */ | |
| 337 | ||
| 338 | aim_tx_enqueue(sess, fr); | |
| 339 | ||
| 340 | return 0; | |
| 341 | } | |
| 342 | ||
| 343 | /** | |
| 344 | * Send client-to-client IM over an established direct connection. | |
| 345 | * Call this just like you would aim_send_im, to send a directim. | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
346 | * |
| 13235 | 347 | * @param sess The session. |
| 348 | * @param conn The already-connected ODC connection. | |
| 349 | * @param msg Null-terminated string to send. | |
| 350 | * @param len The length of the message to send, including binary data. | |
| 351 | * @param encoding See the AIM_CHARSET_* defines in oscar.h | |
| 352 | * @param isawaymsg 0 if this is not an auto-response, 1 if it is. | |
| 353 | * @return Return 0 if no errors, otherwise return the error number. | |
| 354 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
355 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
356 | aim_odc_send_im(OscarSession *sess, OscarConnection *conn, const char *msg, int len, int encoding, int isawaymsg) |
| 13235 | 357 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
358 | FlapFrame *fr; |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
359 | ByteStream *hdrbs; |
| 13235 | 360 | struct aim_odc_intdata *intdata = (struct aim_odc_intdata *)conn->internal; |
| 361 | int hdrlen = 0x44; | |
| 362 | guint8 *hdr; | |
| 363 | ||
| 364 | if (!sess || !conn || (conn->type != AIM_CONN_TYPE_RENDEZVOUS) || !msg) | |
| 365 | return -EINVAL; | |
| 366 | ||
|
13254
3fbb3f3efba7
[gaim-migrate @ 15619]
Mark Doliner <markdoliner@pidgin.im>
parents:
13253
diff
changeset
|
367 | if (!(fr = flap_frame_new(sess, conn, AIM_FRAMETYPE_OFT, 0x01, 0))) |
| 13235 | 368 | return -ENOMEM; |
| 369 | ||
| 370 | memcpy(fr->hdr.rend.magic, "ODC2", 4); | |
| 371 | fr->hdr.rend.hdrlen = hdrlen + 8; | |
| 372 | ||
| 373 | if (!(hdr = calloc(1, hdrlen + len))) { | |
| 374 | aim_frame_destroy(fr); | |
| 375 | return -ENOMEM; | |
| 376 | } | |
| 377 | ||
| 378 | hdrbs = &(fr->data); | |
| 379 | aim_bstream_init(hdrbs, hdr, hdrlen + len); | |
| 380 | ||
| 381 | aimbs_put16(hdrbs, 0x0006); | |
| 382 | aimbs_put16(hdrbs, 0x0000); | |
| 383 | aimbs_putraw(hdrbs, intdata->cookie, 8); | |
| 384 | aimbs_put16(hdrbs, 0x0000); | |
| 385 | aimbs_put16(hdrbs, 0x0000); | |
| 386 | aimbs_put16(hdrbs, 0x0000); | |
| 387 | aimbs_put16(hdrbs, 0x0000); | |
| 388 | aimbs_put32(hdrbs, len); | |
| 389 | aimbs_put16(hdrbs, encoding); | |
| 390 | aimbs_put16(hdrbs, 0x0000); | |
| 391 | aimbs_put16(hdrbs, 0x0000); | |
| 392 | ||
| 393 | /* flags - used for typing notification and to mark if this is an away message */ | |
| 394 | aimbs_put16(hdrbs, 0x0000 | isawaymsg); | |
| 395 | ||
| 396 | aimbs_put16(hdrbs, 0x0000); | |
| 397 | aimbs_put16(hdrbs, 0x0000); | |
| 398 | aimbs_putstr(hdrbs, sess->sn); | |
| 399 | ||
| 400 | aim_bstream_setpos(hdrbs, 52); /* bleeehh */ | |
| 401 | ||
| 402 | aimbs_put8(hdrbs, 0x00); | |
| 403 | aimbs_put16(hdrbs, 0x0000); | |
| 404 | aimbs_put16(hdrbs, 0x0000); | |
| 405 | aimbs_put16(hdrbs, 0x0000); | |
| 406 | aimbs_put16(hdrbs, 0x0000); | |
| 407 | aimbs_put16(hdrbs, 0x0000); | |
| 408 | aimbs_put16(hdrbs, 0x0000); | |
| 409 | aimbs_put16(hdrbs, 0x0000); | |
| 410 | aimbs_put8(hdrbs, 0x00); | |
| 411 | ||
| 412 | /* end of hdr2 */ | |
| 413 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
414 | #if 0 /* XXX - this is how you send buddy icon info... */ |
| 13235 | 415 | aimbs_put16(hdrbs, 0x0008); |
| 416 | aimbs_put16(hdrbs, 0x000c); | |
| 417 | aimbs_put16(hdrbs, 0x0000); | |
| 418 | aimbs_put16(hdrbs, 0x1466); | |
| 419 | aimbs_put16(hdrbs, 0x0001); | |
| 420 | aimbs_put16(hdrbs, 0x2e0f); | |
| 421 | aimbs_put16(hdrbs, 0x393e); | |
| 422 | aimbs_put16(hdrbs, 0xcac8); | |
| 423 | #endif | |
| 424 | aimbs_putraw(hdrbs, (guchar *)msg, len); | |
| 425 | ||
| 426 | aim_tx_enqueue(sess, fr); | |
| 427 | ||
| 428 | return 0; | |
| 429 | } | |
| 430 | ||
| 431 | /** | |
| 432 | * Get the screen name of the peer of a direct connection. | |
| 433 | * | |
| 434 | * @param conn The ODC connection. | |
| 435 | * @return The screen name of the dude, or NULL if there was an anomaly. | |
| 436 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
437 | const char * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
438 | aim_odc_getsn(OscarConnection *conn) |
| 13235 | 439 | { |
| 440 | struct aim_odc_intdata *intdata; | |
| 441 | ||
| 442 | if (!conn || !conn->internal) | |
| 443 | return NULL; | |
| 444 | ||
| 445 | if ((conn->type != AIM_CONN_TYPE_RENDEZVOUS) || | |
| 446 | (conn->subtype != AIM_CONN_SUBTYPE_OFT_DIRECTIM)) | |
| 447 | return NULL; | |
| 448 | ||
| 449 | intdata = (struct aim_odc_intdata *)conn->internal; | |
| 450 | ||
| 451 | return intdata->sn; | |
| 452 | } | |
| 453 | ||
| 454 | /** | |
| 455 | * Get the cookie of a direct connection. | |
| 456 | * | |
| 457 | * @param conn The ODC connection. | |
| 458 | * @return The cookie, an 8 byte unterminated string, or NULL if there was an anomaly. | |
| 459 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
460 | const guchar * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
461 | aim_odc_getcookie(OscarConnection *conn) |
| 13235 | 462 | { |
| 463 | struct aim_odc_intdata *intdata; | |
| 464 | ||
| 465 | if (!conn || !conn->internal) | |
| 466 | return NULL; | |
| 467 | ||
| 468 | intdata = (struct aim_odc_intdata *)conn->internal; | |
| 469 | ||
| 470 | return intdata->cookie; | |
| 471 | } | |
| 472 | ||
| 473 | /** | |
| 474 | * Find the conn of a direct connection with the given buddy. | |
| 475 | * | |
| 476 | * @param sess The session. | |
| 477 | * @param sn The screen name of the buddy whose direct connection you want to find. | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
478 | * @return The conn for the direct connection with the given buddy, or NULL if no |
| 13235 | 479 | * connection was found. |
| 480 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
481 | OscarConnection * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
482 | aim_odc_getconn(OscarSession *sess, const char *sn) |
| 13235 | 483 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
484 | OscarConnection *cur; |
| 13235 | 485 | struct aim_odc_intdata *intdata; |
| 486 | ||
| 487 | if (!sess || !sn || !strlen(sn)) | |
| 488 | return NULL; | |
| 489 | ||
| 490 | for (cur = sess->connlist; cur; cur = cur->next) { | |
| 491 | if ((cur->type == AIM_CONN_TYPE_RENDEZVOUS) && (cur->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM)) { | |
| 492 | intdata = cur->internal; | |
| 493 | if (!aim_sncmp(intdata->sn, sn)) | |
| 494 | return cur; | |
| 495 | } | |
| 496 | } | |
| 497 | ||
| 498 | return NULL; | |
| 499 | } | |
| 500 | ||
| 501 | /** | |
| 502 | * For those times when we want to open up the direct connection channel ourselves. | |
| 503 | * | |
| 504 | * You'll want to set up some kind of watcher on this socket. | |
| 505 | * When the state changes, call aim_handlerendconnection with | |
| 506 | * the connection returned by this. aim_handlerendconnection | |
| 507 | * will accept the pending connection and stop listening. | |
| 508 | * | |
| 509 | * @param sess The session | |
| 510 | * @param sn The screen name to connect to. | |
| 511 | * @return The new connection. | |
| 512 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
513 | OscarConnection * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
514 | aim_odc_initiate(OscarSession *sess, const char *sn, int listenfd, |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
515 | const guint8 *localip, guint16 port, const guint8 *mycookie) |
| 13235 | 516 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
517 | OscarConnection *newconn; |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
518 | IcbmCookie *cookie; |
| 13235 | 519 | struct aim_odc_intdata *priv; |
| 520 | guint8 ck[8]; | |
| 521 | ||
| 522 | if (!localip) | |
| 523 | return NULL; | |
| 524 | ||
| 525 | if (mycookie) { | |
| 526 | memcpy(ck, mycookie, 8); | |
| 527 | aim_im_sendch2_odcrequest(sess, ck, TRUE, sn, localip, port); | |
| 528 | } else | |
| 529 | aim_im_sendch2_odcrequest(sess, ck, FALSE, sn, localip, port); | |
| 530 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
531 | cookie = (IcbmCookie *)calloc(1, sizeof(IcbmCookie)); |
| 13235 | 532 | memcpy(cookie->cookie, ck, 8); |
| 533 | cookie->type = AIM_COOKIETYPE_OFTIM; | |
| 534 | ||
| 535 | /* this one is for the cookie */ | |
| 536 | priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
| 537 | ||
| 538 | memcpy(priv->cookie, ck, 8); | |
| 539 | strncpy(priv->sn, sn, sizeof(priv->sn)); | |
| 540 | cookie->data = priv; | |
| 541 | aim_cachecookie(sess, cookie); | |
| 542 | ||
| 543 | /* XXX - switch to aim_cloneconn()? */ | |
| 544 | if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) { | |
| 545 | close(listenfd); | |
| 546 | return NULL; | |
| 547 | } | |
| 548 | ||
| 549 | /* this one is for the conn */ | |
| 550 | priv = (struct aim_odc_intdata *)calloc(1, sizeof(struct aim_odc_intdata)); | |
| 551 | ||
| 552 | memcpy(priv->cookie, ck, 8); | |
| 553 | strncpy(priv->sn, sn, sizeof(priv->sn)); | |
| 554 | ||
| 555 | newconn->fd = listenfd; | |
| 556 | newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
| 557 | newconn->internal = priv; | |
| 558 | newconn->lastactivity = time(NULL); | |
| 559 | ||
| 560 | return newconn; | |
| 561 | } | |
| 562 | ||
| 563 | /** | |
| 564 | * Connect directly to the given buddy for directim. | |
| 565 | * | |
| 566 | * This is a wrapper for aim_newconn. | |
| 567 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
568 | * If addr is NULL, the socket is not created, but the connection is |
| 13235 | 569 | * allocated and setup to connect. |
| 570 | * | |
| 571 | * @param sess The Godly session. | |
| 572 | * @param sn The screen name we're connecting to. I hope it's a girl... | |
| 573 | * @param addr Address to connect to. | |
| 574 | * @return The new connection. | |
| 575 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
576 | OscarConnection * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
577 | aim_odc_connect(OscarSession *sess, const char *sn, const char *addr, const guint8 *cookie) |
| 13235 | 578 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
579 | OscarConnection *newconn; |
| 13235 | 580 | struct aim_odc_intdata *intdata; |
| 581 | ||
| 582 | if (!sess || !sn) | |
| 583 | return NULL; | |
| 584 | ||
| 585 | if (!(intdata = calloc(1, sizeof(struct aim_odc_intdata)))) | |
| 586 | return NULL; | |
| 587 | memcpy(intdata->cookie, cookie, 8); | |
| 588 | strncpy(intdata->sn, sn, sizeof(intdata->sn)); | |
| 589 | if (addr) | |
| 590 | strncpy(intdata->ip, addr, sizeof(intdata->ip)); | |
| 591 | ||
| 592 | /* XXX - verify that non-blocking connects actually work */ | |
| 593 | if (!(newconn = aim_newconn(sess, AIM_CONN_TYPE_RENDEZVOUS))) { | |
| 594 | free(intdata); | |
| 595 | return NULL; | |
| 596 | } | |
| 597 | ||
| 598 | newconn->internal = intdata; | |
| 599 | newconn->subtype = AIM_CONN_SUBTYPE_OFT_DIRECTIM; | |
| 600 | ||
| 601 | return newconn; | |
| 602 | } | |
| 603 | ||
| 604 | /** | |
| 605 | * Sometimes you just don't know with these kinds of people. | |
| 606 | * | |
| 607 | * @param sess The session. | |
| 608 | * @param conn The ODC connection of the incoming data. | |
| 609 | * @param frr The frame allocated for the incoming data. | |
| 610 | * @param bs It stands for "bologna sandwich." | |
| 611 | * @return Return 0 if no errors, otherwise return the error number. | |
| 612 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
613 | static int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
614 | handlehdr_odc(OscarSession *sess, OscarConnection *conn, FlapFrame *frr, ByteStream *bs) |
| 13235 | 615 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
616 | FlapFrame fr; |
| 13235 | 617 | int ret = 0; |
| 618 | aim_rxcallback_t userfunc; | |
| 619 | guint32 payloadlength; | |
| 620 | guint16 flags, encoding; | |
| 621 | char *snptr = NULL; | |
| 622 | ||
| 623 | fr.conn = conn; | |
| 624 | ||
| 625 | /* AAA - ugly */ | |
| 626 | aim_bstream_setpos(bs, 20); | |
| 627 | payloadlength = aimbs_get32(bs); | |
| 628 | ||
| 629 | aim_bstream_setpos(bs, 24); | |
| 630 | encoding = aimbs_get16(bs); | |
| 631 | ||
| 632 | aim_bstream_setpos(bs, 30); | |
| 633 | flags = aimbs_get16(bs); | |
| 634 | ||
| 635 | aim_bstream_setpos(bs, 36); | |
| 636 | /* XXX - create an aimbs_getnullstr function? */ | |
| 637 | snptr = aimbs_getstr(bs, 32); /* Next 32 bytes contain the sn, padded with null chars */ | |
| 638 | ||
| 639 | gaim_debug_misc("oscar", "faim: OFT frame: handlehdr_odc: %04x / %04x / %s\n", payloadlength, flags, snptr); | |
| 640 | ||
| 641 | if (flags & 0x0008) { | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
642 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING))) |
| 13235 | 643 | ret = userfunc(sess, &fr, snptr, 2); |
| 644 | } else if (flags & 0x0004) { | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
645 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING))) |
| 13235 | 646 | ret = userfunc(sess, &fr, snptr, 1); |
| 647 | } else { | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
648 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMTYPING))) |
| 13235 | 649 | ret = userfunc(sess, &fr, snptr, 0); |
| 650 | } | |
| 651 | ||
| 652 | if ((payloadlength != 0) && (payloadlength != UINT_MAX)) { | |
| 653 | char *msg; | |
| 654 | int recvd = 0; | |
| 655 | int i, isawaymsg; | |
| 656 | ||
| 657 | isawaymsg = flags & 0x0001; | |
| 658 | ||
| 659 | if (!(msg = calloc(1, payloadlength+1))) { | |
| 660 | free(snptr); | |
| 661 | return -ENOMEM; | |
| 662 | } | |
| 663 | ||
| 664 | while (payloadlength - recvd) { | |
| 665 | if (payloadlength - recvd >= 1024) | |
| 666 | i = aim_recv(conn->fd, &msg[recvd], 1024); | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
667 | else |
| 13235 | 668 | i = aim_recv(conn->fd, &msg[recvd], payloadlength - recvd); |
| 669 | if (i <= 0) { | |
| 670 | free(msg); | |
| 671 | free(snptr); | |
| 672 | return -1; | |
| 673 | } | |
| 674 | recvd = recvd + i; | |
| 675 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_IMAGETRANSFER))) | |
| 676 | ret = userfunc(sess, &fr, snptr, (double)recvd / payloadlength); | |
| 677 | } | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
678 | |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
679 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, PEER_TYPE_DIRECTIMINCOMING))) |
| 13235 | 680 | ret = userfunc(sess, &fr, snptr, msg, payloadlength, encoding, isawaymsg); |
| 681 | ||
| 682 | free(msg); | |
| 683 | } | |
| 684 | ||
| 685 | free(snptr); | |
| 686 | ||
| 687 | return ret; | |
| 688 | } | |
| 689 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
690 | PeerConnection * |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
691 | aim_oft_createinfo(OscarSession *sess, const guint8 *cookie, const char *sn, const char *ip, guint16 port, guint32 size, guint32 modtime, char *filename, int send_or_recv, int method, int stage) |
| 13235 | 692 | { |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
693 | PeerConnection *new; |
| 13235 | 694 | |
| 695 | if (!sess) | |
| 696 | return NULL; | |
| 697 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
698 | if (!(new = (PeerConnection *)calloc(1, sizeof(PeerConnection)))) |
| 13235 | 699 | return NULL; |
| 700 | ||
| 701 | new->sess = sess; | |
| 702 | if (cookie) | |
| 703 | memcpy(new->cookie, cookie, 8); | |
| 704 | else | |
| 705 | aim_icbm_makecookie(new->cookie); | |
| 706 | if (ip) | |
| 707 | new->clientip = strdup(ip); | |
| 708 | else | |
| 709 | new->clientip = NULL; | |
| 710 | if (sn) | |
| 711 | new->sn = strdup(sn); | |
| 712 | else | |
| 713 | new->sn = NULL; | |
| 714 | new->method = method; | |
| 715 | new->send_or_recv = send_or_recv; | |
| 716 | new->stage = stage; | |
| 717 | new->port = port; | |
| 718 | new->xfer_reffed = FALSE; | |
| 719 | new->success = FALSE; | |
| 720 | new->fh.totfiles = 1; | |
| 721 | new->fh.filesleft = 1; | |
| 722 | new->fh.totparts = 1; | |
| 723 | new->fh.partsleft = 1; | |
| 724 | new->fh.totsize = size; | |
| 725 | new->fh.size = size; | |
| 726 | new->fh.modtime = modtime; | |
| 727 | new->fh.checksum = 0xffff0000; | |
| 728 | new->fh.rfrcsum = 0xffff0000; | |
| 729 | new->fh.rfcsum = 0xffff0000; | |
| 730 | new->fh.recvcsum = 0xffff0000; | |
| 731 | strncpy(new->fh.idstring, "OFT_Windows ICBMFT V1.1 32", 31); | |
| 732 | if (filename) { | |
| 733 | strncpy(new->fh.name, filename, 63); | |
| 734 | new->fh.name[63] = '\0'; | |
| 735 | } | |
| 736 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
737 | sess->peer_connections = g_list_prepend(sess->peer_connections, new); |
| 13235 | 738 | |
| 739 | return new; | |
| 740 | } | |
| 741 | ||
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
742 | PeerProxyInfo *aim_rv_proxy_createinfo(OscarSession *sess, const guint8 *cookie, |
| 13235 | 743 | guint16 port) |
| 744 | { | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
745 | PeerProxyInfo *proxy_info; |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
746 | |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
747 | if (!(proxy_info = (PeerProxyInfo*)calloc(1, sizeof(PeerProxyInfo)))) |
| 13235 | 748 | return NULL; |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
749 | |
| 13235 | 750 | proxy_info->sess = sess; |
| 751 | proxy_info->port = port; | |
| 752 | proxy_info->packet_ver = AIM_RV_PROXY_PACKETVER_DFLT; | |
| 753 | proxy_info->unknownA = AIM_RV_PROXY_UNKNOWNA_DFLT; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
754 | |
| 13235 | 755 | if (cookie) |
| 756 | memcpy(proxy_info->cookie, cookie, 8); | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
757 | |
| 13235 | 758 | return proxy_info; |
| 759 | } | |
| 760 | ||
| 761 | /** | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
762 | * Remove the given PeerConnection from the PeerConnection linked list, and |
| 13235 | 763 | * then free its memory. |
| 764 | * | |
| 765 | * @param sess The session. | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
766 | * @param peer_connection The PeerConnection that we're destroying. |
| 13235 | 767 | * @return Return 0 if no errors, otherwise return the error number. |
| 768 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
769 | int |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
770 | aim_oft_destroyinfo(PeerConnection *peer_connection) |
| 13235 | 771 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
772 | OscarSession *sess; |
| 13235 | 773 | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
774 | if (!peer_connection || !(sess = peer_connection->sess)) |
| 13235 | 775 | return -EINVAL; |
| 776 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
777 | sess->peer_connections = g_list_remove(sess->peer_connections, peer_connection); |
| 13235 | 778 | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
779 | free(peer_connection->sn); |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
780 | free(peer_connection->proxyip); |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
781 | free(peer_connection->clientip); |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
782 | free(peer_connection->verifiedip); |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
783 | free(peer_connection); |
| 13235 | 784 | |
| 785 | return 0; | |
| 786 | } | |
| 787 | ||
| 788 | /** | |
| 789 | * Creates a listener socket so the other dude can connect to us. | |
| 790 | * | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
791 | * You'll want to set up some kind of watcher on this socket. |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
792 | * When the state changes, call aim_handlerendconnection with |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
793 | * the connection returned by this. aim_handlerendconnection |
| 13235 | 794 | * will accept the pending connection and stop listening. |
| 795 | * | |
| 796 | * @param sess The session. | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
797 | * @param peer_connection File transfer information associated with this |
| 13235 | 798 | * connection. |
| 799 | * @return Return 0 if no errors, otherwise return the error number. | |
| 800 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
801 | int |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
802 | aim_sendfile_listen(OscarSession *sess, PeerConnection *peer_connection, int listenfd) |
| 13235 | 803 | { |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
804 | if (!peer_connection) |
| 13235 | 805 | return -EINVAL; |
| 806 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
807 | if (!(peer_connection->conn = aim_newconn(sess, AIM_CONN_TYPE_LISTENER))) { |
| 13235 | 808 | close(listenfd); |
| 809 | return -ENOMEM; | |
| 810 | } | |
| 811 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
812 | peer_connection->conn->fd = listenfd; |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
813 | peer_connection->conn->subtype = AIM_CONN_SUBTYPE_OFT_SENDFILE; |
|
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
814 | peer_connection->conn->lastactivity = time(NULL); |
| 13235 | 815 | |
| 816 | return 0; | |
| 817 | } | |
| 818 | ||
| 819 | /** | |
| 820 | * Extract an &aim_fileheader_t from the given buffer. | |
| 821 | * | |
| 822 | * @param bs The should be from an incoming rendezvous packet. | |
| 823 | * @return A pointer to new struct on success, or NULL on error. | |
| 824 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
825 | static PeerFrame * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
826 | aim_oft_getheader(ByteStream *bs) |
| 13235 | 827 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
828 | PeerFrame *fh; |
| 13235 | 829 | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
830 | if (!(fh = calloc(1, sizeof(PeerFrame)))) |
| 13235 | 831 | return NULL; |
| 832 | ||
| 833 | /* The bstream should be positioned after the hdrtype. */ | |
| 834 | aimbs_getrawbuf(bs, fh->bcookie, 8); | |
| 835 | fh->encrypt = aimbs_get16(bs); | |
| 836 | fh->compress = aimbs_get16(bs); | |
| 837 | fh->totfiles = aimbs_get16(bs); | |
| 838 | fh->filesleft = aimbs_get16(bs); | |
| 839 | fh->totparts = aimbs_get16(bs); | |
| 840 | fh->partsleft = aimbs_get16(bs); | |
| 841 | fh->totsize = aimbs_get32(bs); | |
| 842 | fh->size = aimbs_get32(bs); | |
| 843 | fh->modtime = aimbs_get32(bs); | |
| 844 | fh->checksum = aimbs_get32(bs); | |
| 845 | fh->rfrcsum = aimbs_get32(bs); | |
| 846 | fh->rfsize = aimbs_get32(bs); | |
| 847 | fh->cretime = aimbs_get32(bs); | |
| 848 | fh->rfcsum = aimbs_get32(bs); | |
| 849 | fh->nrecvd = aimbs_get32(bs); | |
| 850 | fh->recvcsum = aimbs_get32(bs); | |
| 851 | aimbs_getrawbuf(bs, (guchar *)fh->idstring, 32); | |
| 852 | fh->flags = aimbs_get8(bs); | |
| 853 | fh->lnameoffset = aimbs_get8(bs); | |
| 854 | fh->lsizeoffset = aimbs_get8(bs); | |
| 855 | aimbs_getrawbuf(bs, (guchar *)fh->dummy, 69); | |
| 856 | aimbs_getrawbuf(bs, (guchar *)fh->macfileinfo, 16); | |
| 857 | fh->nencode = aimbs_get16(bs); | |
| 858 | fh->nlanguage = aimbs_get16(bs); | |
| 859 | aimbs_getrawbuf(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ | |
| 860 | fh->name[63] = '\0'; | |
| 861 | ||
| 862 | return fh; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
863 | } |
| 13235 | 864 | |
| 865 | /** | |
| 866 | * Fills a buffer with network-order fh data | |
| 867 | * | |
| 868 | * @param bs A bstream to fill -- automatically initialized | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
869 | * @param fh A PeerFrame to get data from. |
| 13235 | 870 | * @return Return non-zero on error. |
| 871 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
872 | static int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
873 | aim_oft_buildheader(ByteStream *bs, PeerFrame *fh) |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
874 | { |
| 13235 | 875 | guint8 *hdr; |
| 876 | ||
| 877 | if (!bs || !fh) | |
| 878 | return -EINVAL; | |
| 879 | ||
| 880 | if (!(hdr = (unsigned char *)calloc(1, 0x100 - 8))) | |
| 881 | return -ENOMEM; | |
| 882 | ||
| 883 | aim_bstream_init(bs, hdr, 0x100 - 8); | |
| 884 | aimbs_putraw(bs, fh->bcookie, 8); | |
| 885 | aimbs_put16(bs, fh->encrypt); | |
| 886 | aimbs_put16(bs, fh->compress); | |
| 887 | aimbs_put16(bs, fh->totfiles); | |
| 888 | aimbs_put16(bs, fh->filesleft); | |
| 889 | aimbs_put16(bs, fh->totparts); | |
| 890 | aimbs_put16(bs, fh->partsleft); | |
| 891 | aimbs_put32(bs, fh->totsize); | |
| 892 | aimbs_put32(bs, fh->size); | |
| 893 | aimbs_put32(bs, fh->modtime); | |
| 894 | aimbs_put32(bs, fh->checksum); | |
| 895 | aimbs_put32(bs, fh->rfrcsum); | |
| 896 | aimbs_put32(bs, fh->rfsize); | |
| 897 | aimbs_put32(bs, fh->cretime); | |
| 898 | aimbs_put32(bs, fh->rfcsum); | |
| 899 | aimbs_put32(bs, fh->nrecvd); | |
| 900 | aimbs_put32(bs, fh->recvcsum); | |
| 901 | aimbs_putraw(bs, (guchar *)fh->idstring, 32); | |
| 902 | aimbs_put8(bs, fh->flags); | |
| 903 | aimbs_put8(bs, fh->lnameoffset); | |
| 904 | aimbs_put8(bs, fh->lsizeoffset); | |
| 905 | aimbs_putraw(bs, (guchar *)fh->dummy, 69); | |
| 906 | aimbs_putraw(bs, (guchar *)fh->macfileinfo, 16); | |
| 907 | aimbs_put16(bs, fh->nencode); | |
| 908 | aimbs_put16(bs, fh->nlanguage); | |
| 909 | aimbs_putraw(bs, (guchar *)fh->name, 64); /* XXX - filenames longer than 64B */ | |
| 910 | ||
| 911 | return 0; | |
| 912 | } | |
| 913 | ||
| 914 | /** | |
| 915 | * Create an OFT packet based on the given information, and send it on its merry way. | |
| 916 | * | |
| 917 | * @param sess The session. | |
| 918 | * @param type The subtype of the OFT packet we're sending. | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
919 | * @param peer_connection The PeerConnection with the connection and OFT |
| 13235 | 920 | * info we're sending. |
| 921 | * @return Return 0 if no errors, otherwise return the error number. | |
| 922 | */ | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
923 | int aim_oft_sendheader(OscarSession *sess, guint16 type, PeerConnection *peer_connection) |
| 13235 | 924 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
925 | FlapFrame *fr; |
| 13235 | 926 | |
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
927 | if (!sess || !peer_connection || !peer_connection->conn || (peer_connection->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) |
| 13235 | 928 | return -EINVAL; |
| 929 | ||
| 930 | #if 0 | |
| 931 | /* | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
932 | * If you are receiving a file, the cookie should be null, if you are sending a |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
933 | * file, the cookie should be the same as the one used in the ICBM negotiation |
| 13235 | 934 | * SNACs. |
| 935 | */ | |
| 936 | fh->lnameoffset = 0x1a; | |
| 937 | fh->lsizeoffset = 0x10; | |
| 938 | ||
| 939 | /* These should be the same as charset and charsubset in ICBMs */ | |
| 940 | fh->nencode = 0x0000; | |
| 941 | fh->nlanguage = 0x0000; | |
| 942 | #endif | |
| 943 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
944 | aim_oft_dirconvert_tostupid(peer_connection->fh.name); |
| 13235 | 945 | |
|
13254
3fbb3f3efba7
[gaim-migrate @ 15619]
Mark Doliner <markdoliner@pidgin.im>
parents:
13253
diff
changeset
|
946 | if (!(fr = flap_frame_new(sess, peer_connection->conn, AIM_FRAMETYPE_OFT, type, 0))) |
| 13235 | 947 | return -ENOMEM; |
| 948 | ||
|
13253
5ad8373f2d1e
[gaim-migrate @ 15618]
Mark Doliner <markdoliner@pidgin.im>
parents:
13240
diff
changeset
|
949 | if (aim_oft_buildheader(&fr->data, &peer_connection->fh) == -1) { |
| 13235 | 950 | aim_frame_destroy(fr); |
| 951 | return -ENOMEM; | |
| 952 | } | |
| 953 | ||
| 954 | memcpy(fr->hdr.rend.magic, "OFT2", 4); | |
| 955 | fr->hdr.rend.hdrlen = aim_bstream_curpos(&fr->data) + 8; | |
| 956 | ||
| 957 | aim_tx_enqueue(sess, fr); | |
| 958 | ||
| 959 | return 0; | |
| 960 | } | |
| 961 | ||
| 962 | /** | |
| 963 | * Create a rendezvous "init recv" packet and send it on its merry way. | |
| 964 | * This is the first packet sent to the proxy server by the second client | |
| 965 | * involved in this rendezvous proxy session. | |
| 966 | * | |
| 967 | * @param sess The session. | |
| 968 | * @param proxy_info Changable pieces of data for this packet | |
| 969 | * @return Return 0 if no errors, otherwise return the error number. | |
| 970 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
971 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
972 | aim_rv_proxy_init_recv(PeerProxyInfo *proxy_info) |
| 13235 | 973 | { |
| 974 | #if 0 | |
| 975 | aim_tlvlist_t *tlvlist_sendfile; | |
| 976 | #endif | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
977 | ByteStream bs; |
| 13235 | 978 | guint8 *bs_raw; |
| 979 | guint16 packet_len; | |
| 980 | guint8 sn_len; | |
| 981 | int err; | |
| 982 | ||
| 983 | err = 0; | |
| 984 | ||
| 985 | if (!proxy_info) | |
| 986 | return -EINVAL; | |
| 987 | ||
| 988 | sn_len = strlen(proxy_info->sess->sn); | |
| 989 | packet_len = 2 + 2 /* packet_len, packet_ver */ | |
| 990 | + 2 + 4 /* cmd_type, unknownA */ | |
| 991 | + 2 /* flags */ | |
| 992 | + 1 + sn_len /* Length/value pair for screenname */ | |
| 993 | + 8 /* ICBM Cookie */ | |
| 994 | + 2 /* port */ | |
| 995 | + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
| 996 | ||
| 997 | if (!(bs_raw = malloc(packet_len))) | |
| 998 | return -ENOMEM; | |
| 999 | ||
| 1000 | aim_bstream_init(&bs, bs_raw, packet_len); | |
| 1001 | aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
| 1002 | aimbs_put16(&bs, proxy_info->packet_ver); | |
| 1003 | aimbs_put16(&bs, AIM_RV_PROXY_INIT_RECV); | |
| 1004 | aimbs_put32(&bs, proxy_info->unknownA); | |
| 1005 | aimbs_put16(&bs, proxy_info->flags); | |
| 1006 | aimbs_put8(&bs, sn_len); | |
| 1007 | aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len); | |
| 1008 | aimbs_put16(&bs, proxy_info->port); | |
| 1009 | aimbs_putraw(&bs, proxy_info->cookie, 8); | |
| 1010 | ||
| 1011 | aimbs_put16(&bs, 0x0001); /* Type */ | |
| 1012 | aimbs_put16(&bs, 16); /* Length */ | |
| 1013 | aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
| 1014 | ||
| 1015 | ||
| 1016 | #if 0 | |
| 1017 | /* TODO: Use built-in TLV */ | |
| 1018 | aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
| 1019 | aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
| 1020 | #endif | |
| 1021 | ||
| 1022 | aim_bstream_rewind(&bs); | |
| 1023 | if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
| 1024 | err = errno; | |
| 1025 | proxy_info->conn->lastactivity = time(NULL); | |
| 1026 | ||
| 1027 | #if 0 | |
| 1028 | aim_tlvlist_free(tlvlist_sendfile); | |
| 1029 | #endif | |
| 1030 | free(bs_raw); | |
| 1031 | ||
| 1032 | return err; | |
| 1033 | } | |
| 1034 | ||
| 1035 | ||
| 1036 | /** | |
| 1037 | * Create a rendezvous "init send" packet and send it on its merry way. | |
| 1038 | * This is the first packet sent to the proxy server by the client | |
| 1039 | * first indicating that this will be a proxied connection | |
| 1040 | * | |
| 1041 | * @param sess The session. | |
| 1042 | * @param proxy_info Changable pieces of data for this packet | |
| 1043 | * @return Return 0 if no errors, otherwise return the error number. | |
| 1044 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1045 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1046 | aim_rv_proxy_init_send(PeerProxyInfo *proxy_info) |
| 13235 | 1047 | { |
| 1048 | #if 0 | |
| 1049 | aim_tlvlist_t *tlvlist_sendfile; | |
| 1050 | #endif | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1051 | ByteStream bs; |
| 13235 | 1052 | guint8 *bs_raw; |
| 1053 | guint16 packet_len; | |
| 1054 | guint8 sn_len; | |
| 1055 | int err; | |
| 1056 | ||
| 1057 | err = 0; | |
| 1058 | ||
| 1059 | if (!proxy_info) | |
| 1060 | return -EINVAL; | |
| 1061 | ||
| 1062 | sn_len = strlen(proxy_info->sess->sn); | |
| 1063 | packet_len = 2 + 2 /* packet_len, packet_ver */ | |
| 1064 | + 2 + 4 /* cmd_type, unknownA */ | |
| 1065 | + 2 /* flags */ | |
| 1066 | + 1 + sn_len /* Length/value pair for screenname */ | |
| 1067 | + 8 /* ICBM Cookie */ | |
| 1068 | + 2 + 2 + 16; /* TLV for Filesend capability block */ | |
| 1069 | ||
| 1070 | if (!(bs_raw = malloc(packet_len))) | |
| 1071 | return -ENOMEM; | |
| 1072 | ||
| 1073 | aim_bstream_init(&bs, bs_raw, packet_len); | |
| 1074 | aimbs_put16(&bs, packet_len - 2); /* Length includes only packets after length marker */ | |
| 1075 | aimbs_put16(&bs, proxy_info->packet_ver); | |
| 1076 | aimbs_put16(&bs, AIM_RV_PROXY_INIT_SEND); | |
| 1077 | aimbs_put32(&bs, proxy_info->unknownA); | |
| 1078 | aimbs_put16(&bs, proxy_info->flags); | |
| 1079 | aimbs_put8(&bs, sn_len); | |
| 1080 | aimbs_putraw(&bs, (const guchar *)proxy_info->sess->sn, sn_len); | |
| 1081 | aimbs_putraw(&bs, proxy_info->cookie, 8); | |
| 1082 | ||
| 1083 | aimbs_put16(&bs, 0x0001); /* Type */ | |
| 1084 | aimbs_put16(&bs, 16); /* Length */ | |
| 1085 | aimbs_putcaps(&bs, AIM_CAPS_SENDFILE); /* Value */ | |
| 1086 | ||
| 1087 | /* TODO: Use built-in TLV */ | |
| 1088 | #if 0 | |
| 1089 | aim_tlvlist_add_caps(&tlvlist_sendfile, 0x0001, AIM_CAPS_SENDFILE); | |
| 1090 | aim_tlvlist_write(&bs, &tlvlist_sendfile); | |
| 1091 | #endif | |
| 1092 | ||
| 1093 | aim_bstream_rewind(&bs); | |
| 1094 | if (aim_bstream_send(&bs, proxy_info->conn, packet_len) != packet_len) | |
| 1095 | err = errno; | |
| 1096 | proxy_info->conn->lastactivity = time(NULL); | |
| 1097 | ||
| 1098 | #if 0 | |
| 1099 | aim_tlvlist_free(tlvlist_sendfile); | |
| 1100 | #endif | |
| 1101 | free(bs_raw); | |
| 1102 | ||
| 1103 | return err; | |
| 1104 | } | |
| 1105 | ||
| 1106 | /** | |
| 1107 | * Handle incoming data on a rendezvous connection. This is analogous to the | |
| 1108 | * consumesnac function in rxhandlers.c, and I really think this should probably | |
| 1109 | * be in rxhandlers.c as well, but I haven't finished cleaning everything up yet. | |
| 1110 | * | |
| 1111 | * @param sess The session. | |
| 1112 | * @param fr The frame allocated for the incoming data. | |
| 1113 | * @return Return 0 if the packet was handled correctly, otherwise return the | |
| 1114 | * error number. | |
| 1115 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1116 | int |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1117 | aim_rxdispatch_rendezvous(OscarSession *sess, FlapFrame *fr) |
| 13235 | 1118 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1119 | OscarConnection *conn = fr->conn; |
| 13235 | 1120 | int ret = 1; |
| 1121 | ||
| 1122 | if (conn->subtype == AIM_CONN_SUBTYPE_OFT_DIRECTIM) { | |
| 1123 | if (fr->hdr.rend.type == 0x0001) | |
| 1124 | ret = handlehdr_odc(sess, conn, fr, &fr->data); | |
| 1125 | else | |
| 1126 | gaim_debug_info("oscar", "ODC directim frame unknown, type is %04x\n", fr->hdr.rend.type); | |
| 1127 | ||
| 1128 | } else { | |
| 1129 | aim_rxcallback_t userfunc; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1130 | PeerFrame *header = aim_oft_getheader(&fr->data); |
| 13235 | 1131 | aim_oft_dirconvert_fromstupid(header->name); /* XXX - This should be client-side */ |
| 1132 | ||
| 1133 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_OFT, fr->hdr.rend.type))) | |
| 1134 | ret = userfunc(sess, fr, conn, header->bcookie, header); | |
| 1135 | ||
| 1136 | free(header); | |
| 1137 | } | |
| 1138 | ||
| 1139 | if (ret == -1) | |
| 1140 | aim_conn_close(conn); | |
| 1141 | ||
| 1142 | return ret; | |
| 1143 | } | |
| 1144 | ||
| 1145 | /** | |
| 1146 | * Handle incoming data on a rendezvous proxy connection. This is similar to | |
| 1147 | * aim_rxdispatch_rendezvous above and should probably be kept with that function. | |
| 1148 | * | |
| 1149 | * @param sess The session. | |
| 1150 | * @param fr The frame allocated for the incoming data. | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1151 | * @return Return 0 if the packet was handled correctly, otherwise return the |
| 13235 | 1152 | * error number. |
| 1153 | */ | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1154 | PeerProxyInfo * |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1155 | aim_rv_proxy_read(OscarSession *sess, OscarConnection *conn) |
| 13235 | 1156 | { |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1157 | ByteStream bs_hdr; |
| 13235 | 1158 | guint8 hdr_buf[AIM_RV_PROXY_HDR_LEN]; |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1159 | ByteStream bs_body; /* The body (everything but the header) of the packet */ |
| 13235 | 1160 | guint8 *body_buf = NULL; |
| 1161 | guint8 body_len; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1162 | |
| 13235 | 1163 | char str_ip[30] = {""}; |
| 1164 | guint8 ip_temp[4]; | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1165 | |
| 13235 | 1166 | guint16 len; |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1167 | PeerProxyInfo *proxy_info; |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1168 | |
|
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1169 | if(!(proxy_info = malloc(sizeof(PeerProxyInfo)))) |
| 13235 | 1170 | return NULL; |
| 1171 | ||
| 1172 | aim_bstream_init(&bs_hdr, hdr_buf, AIM_RV_PROXY_HDR_LEN); | |
| 1173 | if (aim_bstream_recv(&bs_hdr, conn->fd, AIM_RV_PROXY_HDR_LEN) == AIM_RV_PROXY_HDR_LEN) { | |
| 1174 | aim_bstream_rewind(&bs_hdr); | |
| 1175 | len = aimbs_get16(&bs_hdr); | |
| 1176 | proxy_info->packet_ver = aimbs_get16(&bs_hdr); | |
| 1177 | proxy_info->cmd_type = aimbs_get16(&bs_hdr); | |
| 1178 | proxy_info->unknownA = aimbs_get32(&bs_hdr); | |
| 1179 | proxy_info->flags = aimbs_get16(&bs_hdr); | |
| 1180 | if(proxy_info->cmd_type == AIM_RV_PROXY_READY) { | |
| 1181 | /* Do a little victory dance | |
| 1182 | * A ready packet contains no additional information */ | |
| 1183 | } else if(proxy_info->cmd_type == AIM_RV_PROXY_ERROR) { | |
| 1184 | if(len == AIM_RV_PROXY_ERROR_LEN - 2) { | |
| 1185 | body_len = AIM_RV_PROXY_ERROR_LEN - AIM_RV_PROXY_HDR_LEN; | |
| 1186 | body_buf = malloc(body_len); | |
| 1187 | aim_bstream_init(&bs_body, body_buf, body_len); | |
| 1188 | if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
| 1189 | aim_bstream_rewind(&bs_body); | |
| 1190 | proxy_info->err_code = aimbs_get16(&bs_body); | |
| 1191 | } else { | |
| 1192 | gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
| 1193 | aim_conn_close(conn); | |
| 1194 | free(proxy_info); | |
| 1195 | proxy_info = NULL; | |
| 1196 | } | |
| 1197 | } else { | |
| 1198 | gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
| 1199 | free(proxy_info); | |
| 1200 | proxy_info = NULL; | |
| 1201 | } | |
| 1202 | } else if(proxy_info->cmd_type == AIM_RV_PROXY_ACK) { | |
| 1203 | if(len == AIM_RV_PROXY_ACK_LEN - 2) { | |
| 1204 | body_len = AIM_RV_PROXY_ACK_LEN - AIM_RV_PROXY_HDR_LEN; | |
| 1205 | body_buf = malloc(body_len); | |
| 1206 | aim_bstream_init(&bs_body, body_buf, body_len); | |
| 1207 | if (aim_bstream_recv(&bs_body, conn->fd, body_len) == body_len) { | |
| 1208 | int i; | |
| 1209 | aim_bstream_rewind(&bs_body); | |
| 1210 | proxy_info->port = aimbs_get16(&bs_body); | |
| 1211 | for(i=0; i<4; i++) | |
| 1212 | ip_temp[i] = aimbs_get8(&bs_body); | |
| 1213 | snprintf(str_ip, sizeof(str_ip), "%hhu.%hhu.%hhu.%hhu", | |
| 1214 | ip_temp[0], ip_temp[1], | |
| 1215 | ip_temp[2], ip_temp[3]); | |
| 1216 | proxy_info->ip = strdup(str_ip); | |
| 1217 | } else { | |
| 1218 | gaim_debug_warning("oscar","error reading rv proxy error packet\n"); | |
| 1219 | aim_conn_close(conn); | |
| 1220 | free(proxy_info); | |
| 1221 | proxy_info = NULL; | |
| 1222 | } | |
| 1223 | } else { | |
| 1224 | gaim_debug_warning("oscar","invalid length for proxy error packet\n"); | |
| 1225 | free(proxy_info); | |
| 1226 | proxy_info = NULL; | |
| 1227 | } | |
| 1228 | } else { | |
| 1229 | gaim_debug_warning("oscar","unknown type for aim rendezvous proxy packet\n"); | |
|
13240
db5187cc40d1
[gaim-migrate @ 15605]
Mark Doliner <markdoliner@pidgin.im>
parents:
13235
diff
changeset
|
1230 | } |
| 13235 | 1231 | } else { |
| 1232 | gaim_debug_warning("oscar","error reading header of rv proxy packet\n"); | |
| 1233 | aim_conn_close(conn); | |
| 1234 | free(proxy_info); | |
| 1235 | proxy_info = NULL; | |
| 1236 | } | |
| 1237 | if(body_buf) { | |
| 1238 | free(body_buf); | |
| 1239 | body_buf = NULL; | |
| 1240 | } | |
| 1241 | return proxy_info; | |
| 1242 | } |