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