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