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