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