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