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