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