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