source: protocols/oscar/rxhandlers.c @ b7d3cc34

0.99
Last change on this file since b7d3cc34 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*
2 * aim_rxhandlers.c
3 *
4 * This file contains most all of the incoming packet handlers, along
5 * with aim_rxdispatch(), the Rx dispatcher.  Queue/list management is
6 * actually done in aim_rxqueue.c.
7 *
8 */
9
10#include <aim.h>
11
12struct aim_rxcblist_s {
13        guint16 family;
14        guint16 type;
15        aim_rxcallback_t handler;
16        u_short flags;
17        struct aim_rxcblist_s *next;
18};
19
20aim_module_t *aim__findmodulebygroup(aim_session_t *sess, guint16 group)
21{
22        aim_module_t *cur;
23
24        for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) {
25                if (cur->family == group)
26                        return cur;
27        }
28
29        return NULL;
30}
31
32static aim_module_t *aim__findmodule(aim_session_t *sess, const char *name)
33{
34        aim_module_t *cur;
35
36        for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) {
37                if (strcmp(name, cur->name) == 0)
38                        return cur;
39        }
40
41        return NULL;
42}
43
44int aim__registermodule(aim_session_t *sess, int (*modfirst)(aim_session_t *, aim_module_t *))
45{
46        aim_module_t *mod;
47
48        if (!sess || !modfirst)
49                return -1;
50
51        if (!(mod = g_new0(aim_module_t,1)))
52                return -1;
53
54        if (modfirst(sess, mod) == -1) {
55                g_free(mod);
56                return -1;
57        }
58
59        if (aim__findmodule(sess, mod->name)) {
60                if (mod->shutdown)
61                        mod->shutdown(sess, mod);
62                g_free(mod);
63                return -1;
64        }
65
66        mod->next = (aim_module_t *)sess->modlistv;
67        sess->modlistv = mod;
68
69
70        return 0;
71}
72
73void aim__shutdownmodules(aim_session_t *sess)
74{
75        aim_module_t *cur;
76
77        for (cur = (aim_module_t *)sess->modlistv; cur; ) {
78                aim_module_t *tmp;
79
80                tmp = cur->next;
81
82                if (cur->shutdown)
83                        cur->shutdown(sess, cur);
84
85                g_free(cur);
86
87                cur = tmp;
88        }
89
90        sess->modlistv = NULL;
91
92        return;
93}
94
95static int consumesnac(aim_session_t *sess, aim_frame_t *rx)
96{
97        aim_module_t *cur;
98        aim_modsnac_t snac;
99
100        if (aim_bstream_empty(&rx->data) < 10)
101                return 0;
102
103        snac.family = aimbs_get16(&rx->data);
104        snac.subtype = aimbs_get16(&rx->data);
105        snac.flags = aimbs_get16(&rx->data);
106        snac.id = aimbs_get32(&rx->data);
107
108        /* Contains TLV(s) in the FNAC header */
109        if(snac.flags & 0x8000) {
110                aim_bstream_advance(&rx->data, aimbs_get16(&rx->data));
111        } else if(snac.flags & 0x0001) {
112                /* Following SNAC will be related */
113        }
114
115    if (set_getint(sess->aux_data, "debug")) {
116        serv_got_crap(sess->aux_data, "snac %x/%x received", snac.family, snac.subtype);
117    }
118
119        for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) {
120
121                if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && 
122                                (cur->family != snac.family))
123                        continue;
124
125                if (cur->snachandler(sess, cur, rx, &snac, &rx->data))
126                        return 1;
127
128        }
129
130        return 0;
131}
132
133static int consumenonsnac(aim_session_t *sess, aim_frame_t *rx, guint16 family, guint16 subtype)
134{
135        aim_module_t *cur;
136        aim_modsnac_t snac;
137
138        snac.family = family;
139        snac.subtype = subtype;
140        snac.flags = snac.id = 0;
141
142        for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) {
143
144                if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && 
145                                (cur->family != snac.family))
146                        continue;
147
148                if (cur->snachandler(sess, cur, rx, &snac, &rx->data))
149                        return 1;
150
151        }
152
153        return 0;
154}
155
156static int negchan_middle(aim_session_t *sess, aim_frame_t *fr)
157{
158        aim_tlvlist_t *tlvlist;
159        char *msg = NULL;
160        guint16 code = 0;
161        aim_rxcallback_t userfunc;
162        int ret = 1;
163
164        if (aim_bstream_empty(&fr->data) == 0) {
165                /* XXX should do something with this */
166                return 1;
167        }
168
169        /* Used only by the older login protocol */
170        /* XXX remove this special case? */
171        if (fr->conn->type == AIM_CONN_TYPE_AUTH)
172                return consumenonsnac(sess, fr, 0x0017, 0x0003);
173
174        tlvlist = aim_readtlvchain(&fr->data);
175
176        if (aim_gettlv(tlvlist, 0x0009, 1))
177                code = aim_gettlv16(tlvlist, 0x0009, 1);
178
179        if (aim_gettlv(tlvlist, 0x000b, 1))
180                msg = aim_gettlv_str(tlvlist, 0x000b, 1);
181
182        if ((userfunc = aim_callhandler(sess, fr->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) 
183                ret = userfunc(sess, fr, code, msg);
184
185        aim_freetlvchain(&tlvlist);
186
187        g_free(msg);
188
189        return ret;
190}
191
192/*
193 * Some SNACs we do not allow to be hooked, for good reason.
194 */
195static int checkdisallowed(guint16 group, guint16 type)
196{
197        static const struct {
198                guint16 group;
199                guint16 type;
200        } dontuse[] = {
201                {0x0001, 0x0002},
202                {0x0001, 0x0003},
203                {0x0001, 0x0006},
204                {0x0001, 0x0007},
205                {0x0001, 0x0008},
206                {0x0001, 0x0017},
207                {0x0001, 0x0018},
208                {0x0000, 0x0000}
209        };
210        int i;
211
212        for (i = 0; dontuse[i].group != 0x0000; i++) {
213                if ((dontuse[i].group == group) && (dontuse[i].type == type))
214                        return 1;
215        }
216
217        return 0;
218}
219
220int aim_conn_addhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, aim_rxcallback_t newhandler, guint16 flags)
221{
222        struct aim_rxcblist_s *newcb;
223
224        if (!conn)
225                return -1;
226
227        if (checkdisallowed(family, type)) {
228                g_assert(0);
229                return -1;
230        }
231
232        if (!(newcb = (struct aim_rxcblist_s *)g_new0(struct aim_rxcblist_s, 1)))
233                return -1;
234
235        newcb->family = family;
236        newcb->type = type;
237        newcb->flags = flags;
238        newcb->handler = newhandler;
239        newcb->next = NULL;
240
241        if (!conn->handlerlist)
242                conn->handlerlist = (void *)newcb;
243        else {
244                struct aim_rxcblist_s *cur;
245
246                for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur->next; cur = cur->next)
247                        ;
248                cur->next = newcb;
249        }
250
251        return 0;
252}
253
254int aim_clearhandlers(aim_conn_t *conn)
255{
256        struct aim_rxcblist_s *cur;
257
258        if (!conn)
259                return -1;
260
261        for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; ) {
262                struct aim_rxcblist_s *tmp;
263
264                tmp = cur->next;
265                g_free(cur);
266                cur = tmp;
267        }
268        conn->handlerlist = NULL;
269
270        return 0;
271}
272
273aim_rxcallback_t aim_callhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type)
274{
275        struct aim_rxcblist_s *cur;
276
277        if (!conn)
278                return NULL;
279
280        for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; cur = cur->next) {
281                if ((cur->family == family) && (cur->type == type))
282                        return cur->handler;
283        }
284
285        if (type == AIM_CB_SPECIAL_DEFAULT) {
286                return NULL; /* prevent infinite recursion */
287        }
288
289        return aim_callhandler(sess, conn, family, AIM_CB_SPECIAL_DEFAULT);
290}
291
292void aim_clonehandlers(aim_session_t *sess, aim_conn_t *dest, aim_conn_t *src)
293{
294        struct aim_rxcblist_s *cur;
295
296        for (cur = (struct aim_rxcblist_s *)src->handlerlist; cur; cur = cur->next) {
297                aim_conn_addhandler(sess, dest, cur->family, cur->type, 
298                                                cur->handler, cur->flags);
299        }
300
301        return;
302}
303
304static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn,guint16 family, guint16 type, aim_frame_t *ptr)
305{
306        aim_rxcallback_t userfunc;
307
308        if ((userfunc = aim_callhandler(sess, conn, family, type)))
309                return userfunc(sess, ptr);
310
311        return 1; /* XXX */
312}
313
314/*
315 * aim_rxdispatch()
316 *
317 * Basically, heres what this should do:
318 *   1) Determine correct packet handler for this packet
319 *   2) Mark the packet handled (so it can be dequeued in purge_queue())
320 *   3) Send the packet to the packet handler
321 *   4) Go to next packet in the queue and start over
322 *   5) When done, run purge_queue() to purge handled commands
323 *
324 * TODO: Clean up.
325 * TODO: More support for mid-level handlers.
326 * TODO: Allow for NULL handlers.
327 *
328 */
329void aim_rxdispatch(aim_session_t *sess)
330{
331        int i;
332        aim_frame_t *cur;
333
334        for (cur = sess->queue_incoming, i = 0; cur; cur = cur->next, i++) {
335
336                /*
337                 * XXX: This is still fairly ugly.
338                 */
339
340                if (cur->handled)
341                        continue;
342
343                /*
344                 * This is a debugging/sanity check only and probably
345                 * could/should be removed for stable code.
346                 */
347                if (((cur->hdrtype == AIM_FRAMETYPE_OFT) && 
348                   (cur->conn->type != AIM_CONN_TYPE_RENDEZVOUS)) || 
349                  ((cur->hdrtype == AIM_FRAMETYPE_FLAP) && 
350                   (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS))) {
351                        do_error_dialog(sess->aux_data, "incompatible frame type/connection type combination", "Gaim");
352                        cur->handled = 1;
353                        continue;
354                }
355
356                if (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS) {
357                        if (cur->hdrtype != AIM_FRAMETYPE_OFT) {
358                                do_error_dialog(sess->aux_data, "non-OFT frames on OFT connection", "Gaim");
359                                cur->handled = 1; /* get rid of it */
360                        } else {
361                                /* FIXME: implement this (OFT frame) */
362                                cur->handled = 1; /* get rid of it */
363                        }
364                        continue;
365                }
366
367                if (cur->conn->type == AIM_CONN_TYPE_RENDEZVOUS_OUT) {
368                        /* not possible */
369                        do_error_dialog(sess->aux_data, "RENDEZVOUS packet in rxqueue", "Gaim");
370                        cur->handled = 1;
371                        continue;
372                }
373
374                if (cur->hdr.flap.type == 0x01) {
375                       
376                        cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */
377                       
378                        continue;
379                       
380                } else if (cur->hdr.flap.type == 0x02) {
381
382                        if ((cur->handled = consumesnac(sess, cur)))
383                                continue;
384
385                } else if (cur->hdr.flap.type == 0x04) {
386
387                        cur->handled = negchan_middle(sess, cur);
388                        continue;
389
390                } else if (cur->hdr.flap.type == 0x05)
391                        ;
392               
393                if (!cur->handled) {
394                        consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */
395                        cur->handled = 1;
396                }
397        }
398
399        /*
400         * This doesn't have to be called here.  It could easily be done
401         * by a seperate thread or something. It's an administrative operation,
402         * and can take a while. Though the less you call it the less memory
403         * you'll have :)
404         */
405        aim_purge_rxqueue(sess);
406
407        return;
408}
Note: See TracBrowser for help on using the repository browser.