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 | g_return_val_if_fail(conn, -1); |
---|
238 | g_return_val_if_fail(!checkdisallowed(family, type), -1); |
---|
239 | |
---|
240 | if (!(newcb = (struct aim_rxcblist_s *) g_new0(struct aim_rxcblist_s, 1))) { |
---|
241 | return -1; |
---|
242 | } |
---|
243 | |
---|
244 | newcb->family = family; |
---|
245 | newcb->type = type; |
---|
246 | newcb->flags = flags; |
---|
247 | newcb->handler = newhandler; |
---|
248 | newcb->next = NULL; |
---|
249 | |
---|
250 | if (!conn->handlerlist) { |
---|
251 | conn->handlerlist = (void *) newcb; |
---|
252 | } else { |
---|
253 | struct aim_rxcblist_s *cur; |
---|
254 | |
---|
255 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur->next; cur = cur->next) { |
---|
256 | ; |
---|
257 | } |
---|
258 | cur->next = newcb; |
---|
259 | } |
---|
260 | |
---|
261 | return 0; |
---|
262 | } |
---|
263 | |
---|
264 | int aim_clearhandlers(aim_conn_t *conn) |
---|
265 | { |
---|
266 | struct aim_rxcblist_s *cur; |
---|
267 | |
---|
268 | if (!conn) { |
---|
269 | return -1; |
---|
270 | } |
---|
271 | |
---|
272 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; ) { |
---|
273 | struct aim_rxcblist_s *tmp; |
---|
274 | |
---|
275 | tmp = cur->next; |
---|
276 | g_free(cur); |
---|
277 | cur = tmp; |
---|
278 | } |
---|
279 | conn->handlerlist = NULL; |
---|
280 | |
---|
281 | return 0; |
---|
282 | } |
---|
283 | |
---|
284 | aim_rxcallback_t aim_callhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type) |
---|
285 | { |
---|
286 | struct aim_rxcblist_s *cur; |
---|
287 | |
---|
288 | if (!conn) { |
---|
289 | return NULL; |
---|
290 | } |
---|
291 | |
---|
292 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; cur = cur->next) { |
---|
293 | if ((cur->family == family) && (cur->type == type)) { |
---|
294 | return cur->handler; |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | if (type == AIM_CB_SPECIAL_DEFAULT) { |
---|
299 | return NULL; /* prevent infinite recursion */ |
---|
300 | } |
---|
301 | |
---|
302 | return aim_callhandler(sess, conn, family, AIM_CB_SPECIAL_DEFAULT); |
---|
303 | } |
---|
304 | |
---|
305 | static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, |
---|
306 | aim_frame_t *ptr) |
---|
307 | { |
---|
308 | aim_rxcallback_t userfunc; |
---|
309 | |
---|
310 | if ((userfunc = aim_callhandler(sess, conn, family, type))) { |
---|
311 | return userfunc(sess, ptr); |
---|
312 | } |
---|
313 | |
---|
314 | return 1; /* XXX */ |
---|
315 | } |
---|
316 | |
---|
317 | /* |
---|
318 | * aim_rxdispatch() |
---|
319 | * |
---|
320 | * Basically, heres what this should do: |
---|
321 | * 1) Determine correct packet handler for this packet |
---|
322 | * 2) Mark the packet handled (so it can be dequeued in purge_queue()) |
---|
323 | * 3) Send the packet to the packet handler |
---|
324 | * 4) Go to next packet in the queue and start over |
---|
325 | * 5) When done, run purge_queue() to purge handled commands |
---|
326 | * |
---|
327 | * TODO: Clean up. |
---|
328 | * TODO: More support for mid-level handlers. |
---|
329 | * TODO: Allow for NULL handlers. |
---|
330 | * |
---|
331 | */ |
---|
332 | void aim_rxdispatch(aim_session_t *sess) |
---|
333 | { |
---|
334 | int i; |
---|
335 | aim_frame_t *cur; |
---|
336 | |
---|
337 | for (cur = sess->queue_incoming, i = 0; cur; cur = cur->next, i++) { |
---|
338 | |
---|
339 | /* |
---|
340 | * XXX: This is still fairly ugly. |
---|
341 | */ |
---|
342 | |
---|
343 | if (cur->handled) { |
---|
344 | continue; |
---|
345 | } |
---|
346 | |
---|
347 | if (cur->hdr.flap.type == 0x01) { |
---|
348 | |
---|
349 | cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, |
---|
350 | AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */ |
---|
351 | |
---|
352 | continue; |
---|
353 | |
---|
354 | } else if (cur->hdr.flap.type == 0x02) { |
---|
355 | |
---|
356 | if ((cur->handled = consumesnac(sess, cur))) { |
---|
357 | continue; |
---|
358 | } |
---|
359 | |
---|
360 | } else if (cur->hdr.flap.type == 0x04) { |
---|
361 | |
---|
362 | cur->handled = negchan_middle(sess, cur); |
---|
363 | continue; |
---|
364 | |
---|
365 | } else if (cur->hdr.flap.type == 0x05) { |
---|
366 | ; |
---|
367 | } |
---|
368 | |
---|
369 | if (!cur->handled) { |
---|
370 | consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */ |
---|
371 | cur->handled = 1; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | /* |
---|
376 | * This doesn't have to be called here. It could easily be done |
---|
377 | * by a separate thread or something. It's an administrative operation, |
---|
378 | * and can take a while. Though the less you call it the less memory |
---|
379 | * you'll have :) |
---|
380 | */ |
---|
381 | aim_purge_rxqueue(sess); |
---|
382 | |
---|
383 | return; |
---|
384 | } |
---|