Mon, 24 Jul 2006 04:27:42 +0000
[gaim-migrate @ 16556]
Fix a minor case of accessing something that had been free'd in oscar,
and some other minor cleanup. FYI "0" is a valid file descriptor,
but it will pretty much always be STDOUT, STDERR or STDIN. But I
like to check for -1 anyway, because I'm weird.
| 13593 | 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 | #include "oscar.h" | |
| 22 | ||
| 23 | #include "eventloop.h" | |
| 24 | ||
| 25 | #ifndef _WIN32 | |
| 26 | #include <netdb.h> | |
| 27 | #include <sys/socket.h> | |
| 28 | #include <netinet/in.h> | |
| 29 | #endif | |
| 30 | ||
| 31 | #ifdef _WIN32 | |
| 32 | #include "win32dep.h" | |
| 33 | #endif | |
| 34 | ||
| 35 | /** | |
| 36 | * This sends a channel 1 SNAC containing the FLAP version. | |
| 37 | * The FLAP version is sent by itself at the beginning of every | |
| 38 | * connection to a FLAP server. It is always the very first | |
| 39 | * packet sent by both the server and the client after the SYN, | |
| 40 | * SYN/ACK, ACK handshake. | |
| 41 | */ | |
| 42 | void | |
| 43 | flap_connection_send_version(OscarData *od, FlapConnection *conn) | |
| 44 | { | |
| 45 | FlapFrame *frame; | |
| 46 | ||
| 47 | frame = flap_frame_new(od, 0x01, 4); | |
| 48 | byte_stream_put32(&frame->data, 0x00000001); | |
| 49 | flap_connection_send(conn, frame); | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * This sends a channel 1 SNAC containing the FLAP version and | |
| 54 | * the authentication cookie. This is sent when connecting to | |
| 55 | * any FLAP server after the initial connection to the auth | |
| 56 | * server. It is always the very first packet sent by both the | |
| 57 | * server and the client after the SYN, SYN/ACK, ACK handshake. | |
| 58 | */ | |
| 59 | void | |
| 60 | flap_connection_send_version_with_cookie(OscarData *od, FlapConnection *conn, guint16 length, const guint8 *chipsahoy) | |
| 61 | { | |
| 62 | FlapFrame *frame; | |
| 63 | aim_tlvlist_t *tl = NULL; | |
| 64 | ||
| 65 | frame = flap_frame_new(od, 0x01, 4 + 2 + 2 + length); | |
| 66 | byte_stream_put32(&frame->data, 0x00000001); | |
| 67 | aim_tlvlist_add_raw(&tl, 0x0006, length, chipsahoy); | |
| 68 | aim_tlvlist_write(&frame->data, &tl); | |
| 69 | aim_tlvlist_free(&tl); | |
| 70 | ||
| 71 | flap_connection_send(conn, frame); | |
| 72 | } | |
| 73 | ||
| 74 | /** | |
| 75 | * This sends an empty channel 4 SNAC. This is sent to signify | |
| 76 | * that we're logging off. This shouldn't really be necessary-- | |
| 77 | * usually the AIM server will detect that the TCP connection has | |
| 78 | * been destroyed--but it's good practice. | |
| 79 | */ | |
| 80 | static void | |
| 81 | flap_connection_send_close(OscarData *od, FlapConnection *conn) | |
| 82 | { | |
| 83 | FlapFrame *frame; | |
| 84 | ||
| 85 | frame = flap_frame_new(od, 0x04, 0); | |
| 86 | flap_connection_send(conn, frame); | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * This sends an empty channel 5 SNAC. This is used as a keepalive | |
| 91 | * packet in FLAP connections. WinAIM 4.x and higher send these | |
| 92 | * _every minute_ to keep the connection alive. | |
| 93 | */ | |
| 94 | void | |
| 95 | flap_connection_send_keepalive(OscarData *od, FlapConnection *conn) | |
| 96 | { | |
| 97 | FlapFrame *frame; | |
| 98 | ||
| 99 | frame = flap_frame_new(od, 0x05, 0); | |
| 100 | flap_connection_send(conn, frame); | |
| 101 | ||
| 102 | /* clean out SNACs over 60sec old */ | |
| 103 | aim_cleansnacs(od, 60); | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Allocate a new empty connection structure. | |
| 108 | * | |
| 109 | * @param od The oscar session associated with this connection. | |
| 110 | * @param type Type of connection to create | |
| 111 | * | |
| 112 | * @return Returns the new connection structure. | |
| 113 | */ | |
| 114 | FlapConnection * | |
| 115 | flap_connection_new(OscarData *od, int type) | |
| 116 | { | |
| 117 | FlapConnection *conn; | |
| 118 | ||
| 119 | conn = g_new0(FlapConnection, 1); | |
| 120 | conn->od = od; | |
| 121 | conn->buffer_outgoing = gaim_circ_buffer_new(0); | |
| 122 | conn->fd = -1; | |
| 123 | conn->subtype = -1; | |
| 124 | conn->type = type; | |
| 125 | ||
| 126 | od->oscar_connections = g_list_prepend(od->oscar_connections, conn); | |
| 127 | ||
| 128 | return conn; | |
| 129 | } | |
| 130 | ||
| 131 | /** | |
| 132 | * Close (but not free) a connection. | |
| 133 | * | |
| 134 | * This leaves everything untouched except for setting the fd | |
| 135 | * to -1 (used to recognize dead connections). | |
| 136 | * | |
| 137 | * @param conn The connection to close. | |
| 138 | */ | |
| 139 | void | |
| 140 | flap_connection_close(OscarData *od, FlapConnection *conn) | |
| 141 | { | |
| 142 | if (conn->fd == -1) | |
| 143 | return; | |
| 144 | ||
| 145 | if (conn->type == SNAC_FAMILY_LOCATE) | |
| 146 | flap_connection_send_close(od, conn); | |
| 147 | ||
| 148 | close(conn->fd); | |
| 149 | } | |
| 150 | ||
| 151 | static void | |
| 152 | flap_connection_destroy_rates(struct rateclass *head) | |
| 153 | { | |
| 154 | struct rateclass *rc; | |
| 155 | ||
| 156 | for (rc = head; rc; ) | |
| 157 | { | |
| 158 | struct rateclass *tmp; | |
| 159 | struct snacpair *sp; | |
| 160 | ||
| 161 | tmp = rc->next; | |
| 162 | ||
| 163 | for (sp = rc->members; sp; ) { | |
| 164 | struct snacpair *tmpsp; | |
| 165 | ||
| 166 | tmpsp = sp->next; | |
| 167 | free(sp); | |
| 168 | sp = tmpsp; | |
| 169 | } | |
| 170 | free(rc); | |
| 171 | ||
| 172 | rc = tmp; | |
| 173 | } | |
| 174 | } | |
| 175 | ||
| 176 | static gboolean | |
| 177 | flap_connection_destroy_cb(gpointer data) | |
| 178 | { | |
| 179 | FlapConnection *conn; | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
180 | OscarData *od; |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
181 | GaimAccount *account; |
| 13593 | 182 | |
| 183 | conn = data; | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
184 | od = conn->od; |
| 13593 | 185 | |
| 186 | gaim_debug_info("oscar", "Destroying oscar connection of " | |
| 187 | "type 0x%04hx\n", conn->type); | |
| 188 | ||
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
189 | flap_connection_close(od, conn); |
| 13593 | 190 | |
| 191 | if (conn->watcher_incoming != 0) | |
| 192 | gaim_input_remove(conn->watcher_incoming); | |
| 193 | if (conn->watcher_outgoing != 0) | |
| 194 | gaim_input_remove(conn->watcher_outgoing); | |
| 195 | g_free(conn->buffer_incoming.data.data); | |
| 196 | gaim_circ_buffer_destroy(conn->buffer_outgoing); | |
| 197 | ||
| 198 | /* | |
| 199 | * Free conn->internal, if necessary | |
| 200 | */ | |
| 201 | if (conn->type == SNAC_FAMILY_CHAT) | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
202 | flap_connection_destroy_chat(od, conn); |
| 13593 | 203 | |
|
13612
c51da9e6fec1
[gaim-migrate @ 15997]
Mark Doliner <markdoliner@pidgin.im>
parents:
13611
diff
changeset
|
204 | g_list_free(conn->groups); |
|
13610
b2c15a312144
[gaim-migrate @ 15995]
Mark Doliner <markdoliner@pidgin.im>
parents:
13609
diff
changeset
|
205 | flap_connection_destroy_rates(conn->rates); |
| 13593 | 206 | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
207 | od->oscar_connections = g_list_remove(od->oscar_connections, conn); |
| 13593 | 208 | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
209 | account = gaim_connection_get_account(od->gc); |
|
13726
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
210 | |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
211 | /* |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
212 | * TODO: If we don't have a SNAC_FAMILY_LOCATE connection then |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
213 | * we should try to request one instead of disconnecting. |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
214 | */ |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
215 | if (!account->disconnecting && ((od->oscar_connections == NULL) |
|
14bf8b5f8e45
[gaim-migrate @ 16133]
Mark Doliner <markdoliner@pidgin.im>
parents:
13612
diff
changeset
|
216 | || (!flap_connection_getbytype(od, SNAC_FAMILY_LOCATE)))) |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
217 | { |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
218 | /* No more FLAP connections! Sign off this GaimConnection! */ |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
219 | const gchar *tmp; |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
220 | if (conn->disconnect_reason == OSCAR_DISCONNECT_REMOTE_CLOSED) |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
221 | tmp = _("Server closed the connection."); |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
222 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_LOST_CONNECTION) |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
223 | tmp = _("Lost connection with server for an unknown reason."); |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
224 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_INVALID_DATA) |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
225 | tmp = _("Received invalid data on connection with server."); |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
226 | else if (conn->disconnect_reason == OSCAR_DISCONNECT_COULD_NOT_CONNECT) |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
227 | tmp = _("Could not establish a connection with the server."); |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
228 | else |
|
14048
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
229 | /* |
|
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
230 | * We shouldn't print a message for some disconnect_reasons. |
|
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
231 | * Like OSCAR_DISCONNECT_LOCAL_CLOSED. |
|
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
232 | */ |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
233 | tmp = NULL; |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
234 | |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
235 | if (tmp != NULL) |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
236 | gaim_connection_error(od->gc, tmp); |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
237 | } |
| 13593 | 238 | |
|
14048
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
239 | g_free(conn); |
|
b577c53406c0
[gaim-migrate @ 16556]
Mark Doliner <markdoliner@pidgin.im>
parents:
13726
diff
changeset
|
240 | |
| 13593 | 241 | return FALSE; |
| 242 | } | |
| 243 | ||
| 244 | void | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
245 | flap_connection_destroy(FlapConnection *conn, OscarDisconnectReason reason) |
| 13593 | 246 | { |
| 247 | if (conn->destroy_timeout != 0) | |
| 248 | gaim_timeout_remove(conn->destroy_timeout); | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
249 | conn->disconnect_reason = reason; |
| 13593 | 250 | flap_connection_destroy_cb(conn); |
| 251 | } | |
| 252 | ||
| 253 | /** | |
| 254 | * Schedule Gaim to destroy the given FlapConnection as soon as we | |
| 255 | * return control back to the program's main loop. We must do this | |
| 256 | * if we want to destroy the connection but we are still using it | |
| 257 | * for some reason. | |
| 258 | */ | |
| 259 | void | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
260 | flap_connection_schedule_destroy(FlapConnection *conn, OscarDisconnectReason reason) |
| 13593 | 261 | { |
| 262 | if (conn->destroy_timeout != 0) | |
| 263 | /* Already taken care of */ | |
| 264 | return; | |
| 265 | ||
| 266 | gaim_debug_info("oscar", "Scheduling destruction of FLAP " | |
| 267 | "connection of type 0x%04hx\n", conn->type); | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
268 | conn->disconnect_reason = reason; |
| 13593 | 269 | conn->destroy_timeout = gaim_timeout_add(0, flap_connection_destroy_cb, conn); |
| 270 | } | |
| 271 | ||
| 272 | /** | |
| 273 | * In OSCAR, every connection has a set of SNAC groups associated | |
| 274 | * with it. These are the groups that you can send over this connection | |
| 275 | * without being guaranteed a "Not supported" SNAC error. | |
| 276 | * | |
| 277 | * The grand theory of things says that these associations transcend | |
| 278 | * what libfaim calls "connection types" (conn->type). You can probably | |
| 279 | * see the elegance here, but since I want to revel in it for a bit, you | |
| 280 | * get to hear it all spelled out. | |
| 281 | * | |
| 282 | * So let us say that you have your core BOS connection running. One | |
| 283 | * of your modules has just given you a SNAC of the group 0x0004 to send | |
| 284 | * you. Maybe an IM destined for some twit in Greenland. So you start | |
| 285 | * at the top of your connection list, looking for a connection that | |
| 286 | * claims to support group 0x0004. You find one. Why, that neat BOS | |
| 287 | * connection of yours can do that. So you send it on its way. | |
| 288 | * | |
| 289 | * Now, say, that fellow from Greenland has friends and they all want to | |
| 290 | * meet up with you in a lame chat room. This has landed you a SNAC | |
| 291 | * in the family 0x000e and you have to admit you're a bit lost. You've | |
| 292 | * searched your connection list for someone who wants to make your life | |
| 293 | * easy and deliver this SNAC for you, but there isn't one there. | |
| 294 | * | |
| 295 | * Here comes the good bit. Without even letting anyone know, particularly | |
| 296 | * the module that decided to send this SNAC, and definitely not that twit | |
| 297 | * in Greenland, you send out a service request. In this request, you have | |
| 298 | * marked the need for a connection supporting group 0x000e. A few seconds | |
| 299 | * later, you receive a service redirect with an IP address and a cookie in | |
| 300 | * it. Great, you say. Now I have something to do. Off you go, making | |
| 301 | * that connection. One of the first things you get from this new server | |
| 302 | * is a message saying that indeed it does support the group you were looking | |
| 303 | * for. So you continue and send rate confirmation and all that. | |
| 304 | * | |
| 305 | * Then you remember you had that SNAC to send, and now you have a means to | |
| 306 | * do it, and you do, and everyone is happy. Except the Greenlander, who is | |
| 307 | * still stuck in the bitter cold. | |
| 308 | * | |
| 309 | * Oh, and this is useful for building the Migration SNACs, too. In the | |
| 310 | * future, this may help convince me to implement rate limit mitigation | |
| 311 | * for real. We'll see. | |
| 312 | * | |
| 313 | * Just to make me look better, I'll say that I've known about this great | |
| 314 | * scheme for quite some time now. But I still haven't convinced myself | |
| 315 | * to make libfaim work that way. It would take a fair amount of effort, | |
| 316 | * and probably some client API changes as well. (Whenever I don't want | |
| 317 | * to do something, I just say it would change the client API. Then I | |
| 318 | * instantly have a couple of supporters of not doing it.) | |
| 319 | * | |
| 320 | * Generally, addgroup is only called by the internal handling of the | |
| 321 | * server ready SNAC. So if you want to do something before that, you'll | |
| 322 | * have to be more creative. That is done rather early, though, so I don't | |
| 323 | * think you have to worry about it. Unless you're me. I care deeply | |
| 324 | * about such inane things. | |
| 325 | * | |
| 326 | */ | |
| 327 | ||
| 328 | /** | |
| 329 | * Find a FlapConnection that supports the given oscar | |
| 330 | * family. | |
| 331 | */ | |
| 332 | FlapConnection * | |
| 333 | flap_connection_findbygroup(OscarData *od, guint16 group) | |
| 334 | { | |
| 335 | GList *cur; | |
| 336 | ||
| 337 | for (cur = od->oscar_connections; cur != NULL; cur = cur->next) | |
| 338 | { | |
| 339 | FlapConnection *conn; | |
|
13612
c51da9e6fec1
[gaim-migrate @ 15997]
Mark Doliner <markdoliner@pidgin.im>
parents:
13611
diff
changeset
|
340 | GList *l; |
| 13593 | 341 | |
| 342 | conn = cur->data; | |
| 343 | ||
|
13612
c51da9e6fec1
[gaim-migrate @ 15997]
Mark Doliner <markdoliner@pidgin.im>
parents:
13611
diff
changeset
|
344 | for (l = conn->groups; l != NULL; l = l->next) |
| 13593 | 345 | { |
|
13612
c51da9e6fec1
[gaim-migrate @ 15997]
Mark Doliner <markdoliner@pidgin.im>
parents:
13611
diff
changeset
|
346 | if (GPOINTER_TO_UINT(l->data) == group) |
| 13593 | 347 | return conn; |
| 348 | } | |
| 349 | } | |
| 350 | ||
| 351 | return NULL; | |
| 352 | } | |
| 353 | ||
| 354 | /** | |
| 355 | * Locates a connection of the specified type in the | |
| 356 | * specified session. | |
| 357 | * | |
| 358 | * TODO: Use flap_connection_findbygroup everywhere and get rid of this. | |
| 359 | * | |
| 360 | * @param od The session to search. | |
| 361 | * @param type The type of connection to look for. | |
| 362 | * | |
| 363 | * @return Returns the first connection found of the given target type, | |
| 364 | * or NULL if none could be found. | |
| 365 | */ | |
| 366 | FlapConnection * | |
| 367 | flap_connection_getbytype(OscarData *od, int type) | |
| 368 | { | |
| 369 | GList *cur; | |
| 370 | ||
| 371 | for (cur = od->oscar_connections; cur != NULL; cur = cur->next) | |
| 372 | { | |
| 373 | FlapConnection *conn; | |
| 374 | conn = cur->data; | |
| 375 | if ((conn->type == type) && (conn->connected)) | |
| 376 | return conn; | |
| 377 | } | |
| 378 | ||
| 379 | return NULL; | |
| 380 | } | |
| 381 | ||
| 382 | FlapConnection * | |
| 383 | flap_connection_getbytype_all(OscarData *od, int type) | |
| 384 | { | |
| 385 | GList *cur; | |
| 386 | ||
| 387 | for (cur = od->oscar_connections; cur; cur = cur->next) | |
| 388 | { | |
| 389 | FlapConnection *conn; | |
| 390 | conn = cur->data; | |
| 391 | if (conn->type == type) | |
| 392 | return conn; | |
| 393 | } | |
| 394 | ||
| 395 | return NULL; | |
| 396 | } | |
| 397 | ||
| 398 | /** | |
| 399 | * Allocate a new FLAP frame. | |
| 400 | * | |
| 401 | * @param channel The FLAP channel. This is almost always 2. | |
| 402 | */ | |
| 403 | FlapFrame * | |
| 404 | flap_frame_new(OscarData *od, guint16 channel, int datalen) | |
| 405 | { | |
| 406 | FlapFrame *frame; | |
| 407 | ||
| 408 | frame = g_new0(FlapFrame, 1); | |
| 409 | frame->channel = channel; | |
| 410 | ||
| 411 | if (datalen > 0) | |
| 412 | { | |
| 413 | guint8 *data; | |
| 414 | data = g_malloc(datalen); | |
| 415 | byte_stream_init(&frame->data, data, datalen); | |
| 416 | } | |
| 417 | ||
| 418 | return frame; | |
| 419 | } | |
| 420 | ||
| 421 | /** | |
| 422 | * Free a FlapFrame | |
| 423 | * | |
| 424 | * @param frame The frame to free. | |
| 425 | * @return -1 on error; 0 on success. | |
| 426 | */ | |
| 427 | static void | |
| 428 | flap_frame_destroy(FlapFrame *frame) | |
| 429 | { | |
| 430 | free(frame->data.data); | |
| 431 | free(frame); | |
| 432 | ||
| 433 | return; | |
| 434 | } | |
| 435 | ||
| 436 | static void | |
| 437 | parse_snac(OscarData *od, FlapConnection *conn, FlapFrame *frame) | |
| 438 | { | |
| 439 | aim_module_t *cur; | |
| 440 | aim_modsnac_t snac; | |
| 441 | ||
| 442 | if (byte_stream_empty(&frame->data) < 10) | |
| 443 | return; | |
| 444 | ||
| 445 | snac.family = byte_stream_get16(&frame->data); | |
| 446 | snac.subtype = byte_stream_get16(&frame->data); | |
| 447 | snac.flags = byte_stream_get16(&frame->data); | |
| 448 | snac.id = byte_stream_get32(&frame->data); | |
| 449 | ||
| 450 | /* SNAC flags are apparently uniform across all SNACs, so we handle them here */ | |
| 451 | if (snac.flags & 0x0001) { | |
| 452 | /* | |
| 453 | * This means the SNAC will be followed by another SNAC with | |
| 454 | * related information. We don't need to do anything about | |
| 455 | * this here. | |
| 456 | */ | |
| 457 | } | |
| 458 | if (snac.flags & 0x8000) { | |
| 459 | /* | |
| 460 | * This packet contains the version of the family that this SNAC is | |
| 461 | * in. You get this when your SSI module is version 2 or higher. | |
| 462 | * For now we have no need for this, but you could always save | |
| 463 | * it as a part of aim_modnsac_t, or something. The format is... | |
| 464 | * 2 byte length of total mini-header (which is 6 bytes), then TLV | |
| 465 | * of type 0x0001, length 0x0002, value is the 2 byte version | |
| 466 | * number | |
| 467 | */ | |
| 468 | byte_stream_advance(&frame->data, byte_stream_get16(&frame->data)); | |
| 469 | } | |
| 470 | ||
| 471 | for (cur = (aim_module_t *)od->modlistv; cur; cur = cur->next) { | |
| 472 | ||
| 473 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && | |
| 474 | (cur->family != snac.family)) | |
| 475 | continue; | |
| 476 | ||
| 477 | if (cur->snachandler(od, conn, cur, frame, &snac, &frame->data)) | |
| 478 | return; | |
| 479 | } | |
| 480 | } | |
| 481 | ||
| 482 | static void | |
| 483 | parse_fakesnac(OscarData *od, FlapConnection *conn, FlapFrame *frame, guint16 family, guint16 subtype) | |
| 484 | { | |
| 485 | aim_module_t *cur; | |
| 486 | aim_modsnac_t snac; | |
| 487 | ||
| 488 | snac.family = family; | |
| 489 | snac.subtype = subtype; | |
| 490 | snac.flags = snac.id = 0; | |
| 491 | ||
| 492 | for (cur = (aim_module_t *)od->modlistv; cur; cur = cur->next) { | |
| 493 | ||
| 494 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && | |
| 495 | (cur->family != snac.family)) | |
| 496 | continue; | |
| 497 | ||
| 498 | if (cur->snachandler(od, conn, cur, frame, &snac, &frame->data)) | |
| 499 | return; | |
| 500 | } | |
| 501 | } | |
| 502 | ||
| 503 | static void | |
| 504 | parse_flap_ch4(OscarData *od, FlapConnection *conn, FlapFrame *frame) | |
| 505 | { | |
| 506 | aim_tlvlist_t *tlvlist; | |
| 507 | char *msg = NULL; | |
| 508 | guint16 code = 0; | |
| 509 | aim_rxcallback_t userfunc; | |
| 510 | ||
| 511 | if (byte_stream_empty(&frame->data) == 0) { | |
| 512 | /* XXX should do something with this */ | |
| 513 | return; | |
| 514 | } | |
| 515 | ||
|
13603
12489c04a4ea
[gaim-migrate @ 15988]
Mark Doliner <markdoliner@pidgin.im>
parents:
13593
diff
changeset
|
516 | /* An ICQ account is logging in */ |
| 13593 | 517 | if (conn->type == SNAC_FAMILY_AUTH) |
| 518 | { | |
| 519 | parse_fakesnac(od, conn, frame, 0x0017, 0x0003); | |
| 520 | return; | |
| 521 | } | |
| 522 | ||
| 523 | tlvlist = aim_tlvlist_read(&frame->data); | |
| 524 | ||
| 525 | if (aim_tlv_gettlv(tlvlist, 0x0009, 1)) | |
| 526 | code = aim_tlv_get16(tlvlist, 0x0009, 1); | |
| 527 | ||
| 528 | if (aim_tlv_gettlv(tlvlist, 0x000b, 1)) | |
| 529 | msg = aim_tlv_getstr(tlvlist, 0x000b, 1); | |
| 530 | ||
| 531 | if ((userfunc = aim_callhandler(od, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) | |
| 532 | userfunc(od, conn, frame, code, msg); | |
| 533 | ||
| 534 | aim_tlvlist_free(&tlvlist); | |
| 535 | ||
| 536 | free(msg); | |
| 537 | } | |
| 538 | ||
| 539 | /** | |
| 540 | * Takes a new incoming FLAP frame and sends it to the appropriate | |
| 541 | * handler function to be parsed. | |
| 542 | */ | |
| 543 | static void | |
| 544 | parse_flap(OscarData *od, FlapConnection *conn, FlapFrame *frame) | |
| 545 | { | |
| 546 | if (frame->channel == 0x01) { | |
| 547 | guint32 flap_version = byte_stream_get32(&frame->data); | |
| 548 | if (flap_version != 0x00000001) | |
| 549 | { | |
| 550 | /* Error! */ | |
| 551 | gaim_debug_warning("oscar", "Expecting FLAP version " | |
| 552 | "0x00000001 but received FLAP version %08lx. Closing connection.\n", | |
| 553 | flap_version); | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
554 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
555 | OSCAR_DISCONNECT_INVALID_DATA); |
| 13593 | 556 | } |
| 557 | else | |
| 558 | conn->connected = TRUE; | |
| 559 | ||
| 560 | } else if (frame->channel == 0x02) { | |
| 561 | parse_snac(od, conn, frame); | |
| 562 | ||
| 563 | } else if (frame->channel == 0x04) { | |
| 564 | parse_flap_ch4(od, conn, frame); | |
| 565 | ||
| 566 | } else if (frame->channel == 0x05) { | |
| 567 | /* TODO: Reset our keepalive watchdog? */ | |
| 568 | ||
| 569 | } | |
| 570 | } | |
| 571 | ||
| 572 | /** | |
| 573 | * Read in all available data on the socket for a given connection. | |
| 574 | * All complete FLAPs handled immedate after they're received. | |
| 575 | * Incomplete FLAP data is stored locally and appended to the next | |
| 576 | * time this callback is triggered. | |
| 577 | */ | |
| 578 | void | |
| 579 | flap_connection_recv_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 580 | { | |
| 581 | FlapConnection *conn; | |
| 582 | ssize_t read; | |
| 583 | guint8 header[6]; | |
| 584 | ||
| 585 | conn = data; | |
| 586 | ||
| 587 | /* Read data until we run out of data and break out of the loop */ | |
| 588 | while (TRUE) | |
| 589 | { | |
| 590 | /* Start reading a new FLAP */ | |
| 591 | if (conn->buffer_incoming.data.data == NULL) | |
| 592 | { | |
| 593 | /* Peek at the first 6 bytes to get the length */ | |
| 594 | read = recv(conn->fd, &header, 6, MSG_PEEK); | |
| 595 | ||
| 596 | /* Check if the FLAP server closed the connection */ | |
| 597 | if (read == 0) | |
| 598 | { | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
599 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
600 | OSCAR_DISCONNECT_REMOTE_CLOSED); |
| 13593 | 601 | break; |
| 602 | } | |
| 603 | ||
| 604 | /* If there was an error then close the connection */ | |
| 605 | if (read == -1) | |
| 606 | { | |
| 607 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | |
| 608 | /* No worries */ | |
| 609 | break; | |
| 610 | ||
| 611 | /* Error! */ | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
612 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
613 | OSCAR_DISCONNECT_LOST_CONNECTION); |
| 13593 | 614 | break; |
| 615 | } | |
| 616 | ||
| 617 | /* If we don't even have a complete FLAP header then do nothing */ | |
| 618 | if (read < 6) | |
| 619 | break; | |
| 620 | ||
| 621 | /* Read the first 6 bytes (the FLAP header) */ | |
| 622 | read = recv(conn->fd, &header, 6, 0); | |
| 623 | ||
| 624 | /* All FLAP frames must start with the byte 0x2a */ | |
| 625 | if (aimutil_get8(&header[0]) != 0x2a) | |
| 626 | { | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
627 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
628 | OSCAR_DISCONNECT_INVALID_DATA); |
| 13593 | 629 | break; |
| 630 | } | |
| 631 | ||
| 632 | /* Initialize a new temporary FlapFrame for incoming data */ | |
| 633 | conn->buffer_incoming.channel = aimutil_get8(&header[1]); | |
| 634 | conn->buffer_incoming.seqnum = aimutil_get16(&header[2]); | |
| 635 | conn->buffer_incoming.data.len = aimutil_get16(&header[4]); | |
| 636 | conn->buffer_incoming.data.data = g_new(guint8, conn->buffer_incoming.data.len); | |
| 637 | conn->buffer_incoming.data.offset = 0; | |
| 638 | } | |
| 639 | ||
| 640 | if (conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset) | |
| 641 | { | |
| 642 | /* Read data into the temporary FlapFrame until it is complete */ | |
| 643 | read = recv(conn->fd, | |
| 644 | &conn->buffer_incoming.data.data[conn->buffer_incoming.data.offset], | |
| 645 | conn->buffer_incoming.data.len - conn->buffer_incoming.data.offset, | |
| 646 | 0); | |
| 647 | ||
| 648 | /* Check if the FLAP server closed the connection */ | |
| 649 | if (read == 0) | |
| 650 | { | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
651 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
652 | OSCAR_DISCONNECT_REMOTE_CLOSED); |
| 13593 | 653 | break; |
| 654 | } | |
| 655 | ||
| 656 | if (read == -1) | |
| 657 | { | |
| 658 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | |
| 659 | /* No worries */ | |
| 660 | break; | |
| 661 | ||
| 662 | /* Error! */ | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
663 | flap_connection_schedule_destroy(conn, |
|
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
664 | OSCAR_DISCONNECT_LOST_CONNECTION); |
| 13593 | 665 | break; |
| 666 | } | |
| 667 | ||
| 668 | conn->buffer_incoming.data.offset += read; | |
| 669 | if (conn->buffer_incoming.data.offset < conn->buffer_incoming.data.len) | |
| 670 | /* Waiting for more data to arrive */ | |
| 671 | break; | |
| 672 | } | |
| 673 | ||
| 674 | /* We have a complete FLAP! Handle it and continue reading */ | |
| 675 | byte_stream_rewind(&conn->buffer_incoming.data); | |
| 676 | parse_flap(conn->od, conn, &conn->buffer_incoming); | |
| 677 | conn->lastactivity = time(NULL); | |
| 678 | ||
| 679 | g_free(conn->buffer_incoming.data.data); | |
| 680 | conn->buffer_incoming.data.data = NULL; | |
| 681 | } | |
| 682 | } | |
| 683 | ||
| 684 | static void | |
| 685 | send_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 686 | { | |
| 687 | FlapConnection *conn; | |
| 688 | int writelen, ret; | |
| 689 | ||
| 690 | conn = data; | |
| 691 | writelen = gaim_circ_buffer_get_max_read(conn->buffer_outgoing); | |
| 692 | ||
| 693 | if (writelen == 0) | |
| 694 | { | |
| 695 | gaim_input_remove(conn->watcher_outgoing); | |
| 696 | conn->watcher_outgoing = 0; | |
| 697 | return; | |
| 698 | } | |
| 699 | ||
| 700 | ret = send(conn->fd, conn->buffer_outgoing->outptr, writelen, 0); | |
| 701 | if (ret <= 0) | |
| 702 | { | |
| 703 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) | |
| 704 | /* No worries */ | |
| 705 | return; | |
| 706 | ||
| 707 | /* Error! */ | |
|
13609
a6fbfad454b6
[gaim-migrate @ 15994]
Mark Doliner <markdoliner@pidgin.im>
parents:
13603
diff
changeset
|
708 | flap_connection_schedule_destroy(conn, OSCAR_DISCONNECT_LOST_CONNECTION); |
| 13593 | 709 | return; |
| 710 | } | |
| 711 | ||
| 712 | gaim_circ_buffer_mark_read(conn->buffer_outgoing, ret); | |
| 713 | } | |
| 714 | ||
| 715 | static void | |
| 716 | flap_connection_send_byte_stream(ByteStream *bs, FlapConnection *conn, size_t count) | |
| 717 | { | |
| 718 | if (conn == NULL) | |
| 719 | return; | |
| 720 | ||
| 721 | /* Make sure we don't send past the end of the bs */ | |
| 722 | if (count > byte_stream_empty(bs)) | |
| 723 | count = byte_stream_empty(bs); /* truncate to remaining space */ | |
| 724 | ||
| 725 | if (count == 0) | |
| 726 | return; | |
| 727 | ||
| 728 | /* Add everything to our outgoing buffer */ | |
| 729 | gaim_circ_buffer_append(conn->buffer_outgoing, bs->data, count); | |
| 730 | ||
| 731 | /* If we haven't already started writing stuff, then start the cycle */ | |
| 732 | if (conn->watcher_outgoing == 0) | |
| 733 | { | |
| 734 | conn->watcher_outgoing = gaim_input_add(conn->fd, | |
| 735 | GAIM_INPUT_WRITE, send_cb, conn); | |
| 736 | send_cb(conn, conn->fd, 0); | |
| 737 | } | |
| 738 | } | |
| 739 | ||
| 740 | static void | |
| 741 | sendframe_flap(FlapConnection *conn, FlapFrame *frame) | |
| 742 | { | |
| 743 | ByteStream bs; | |
| 744 | int payloadlen, bslen; | |
| 745 | ||
| 746 | payloadlen = byte_stream_curpos(&frame->data); | |
| 747 | ||
| 748 | byte_stream_init(&bs, malloc(6 + payloadlen), 6 + payloadlen); | |
| 749 | ||
| 750 | /* FLAP header */ | |
| 751 | byte_stream_put8(&bs, 0x2a); | |
| 752 | byte_stream_put8(&bs, frame->channel); | |
| 753 | byte_stream_put16(&bs, frame->seqnum); | |
| 754 | byte_stream_put16(&bs, payloadlen); | |
| 755 | ||
| 756 | /* Payload */ | |
| 757 | byte_stream_rewind(&frame->data); | |
| 758 | byte_stream_putbs(&bs, &frame->data, payloadlen); | |
| 759 | ||
| 760 | bslen = byte_stream_curpos(&bs); | |
| 761 | byte_stream_rewind(&bs); | |
| 762 | flap_connection_send_byte_stream(&bs, conn, bslen); | |
| 763 | ||
| 764 | free(bs.data); /* XXX byte_stream_free */ | |
| 765 | } | |
| 766 | ||
| 767 | void | |
| 768 | flap_connection_send(FlapConnection *conn, FlapFrame *frame) | |
| 769 | { | |
| 770 | frame->seqnum = ++(conn->seqnum); | |
| 771 | sendframe_flap(conn, frame); | |
| 772 | flap_frame_destroy(frame); | |
| 773 | } | |
| 774 |