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