[b7d3cc34] | 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 | |
---|
[5ebff60] | 24 | for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) { |
---|
| 25 | if (cur->family == group) { |
---|
[b7d3cc34] | 26 | return cur; |
---|
[5ebff60] | 27 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 37 | for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) { |
---|
| 38 | if (strcmp(name, cur->name) == 0) { |
---|
[b7d3cc34] | 39 | return cur; |
---|
[5ebff60] | 40 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 50 | if (!sess || !modfirst) { |
---|
[b7d3cc34] | 51 | return -1; |
---|
[5ebff60] | 52 | } |
---|
[b7d3cc34] | 53 | |
---|
[5ebff60] | 54 | if (!(mod = g_new0(aim_module_t, 1))) { |
---|
[b7d3cc34] | 55 | return -1; |
---|
[5ebff60] | 56 | } |
---|
[b7d3cc34] | 57 | |
---|
| 58 | if (modfirst(sess, mod) == -1) { |
---|
| 59 | g_free(mod); |
---|
| 60 | return -1; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if (aim__findmodule(sess, mod->name)) { |
---|
[5ebff60] | 64 | if (mod->shutdown) { |
---|
[b7d3cc34] | 65 | mod->shutdown(sess, mod); |
---|
[5ebff60] | 66 | } |
---|
[b7d3cc34] | 67 | g_free(mod); |
---|
| 68 | return -1; |
---|
| 69 | } |
---|
| 70 | |
---|
[5ebff60] | 71 | mod->next = (aim_module_t *) sess->modlistv; |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 82 | for (cur = (aim_module_t *) sess->modlistv; cur; ) { |
---|
[b7d3cc34] | 83 | aim_module_t *tmp; |
---|
| 84 | |
---|
| 85 | tmp = cur->next; |
---|
| 86 | |
---|
[5ebff60] | 87 | if (cur->shutdown) { |
---|
[b7d3cc34] | 88 | cur->shutdown(sess, cur); |
---|
[5ebff60] | 89 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 106 | if (aim_bstream_empty(&rx->data) < 10) { |
---|
[b7d3cc34] | 107 | return 0; |
---|
[5ebff60] | 108 | } |
---|
[b7d3cc34] | 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 */ |
---|
[5ebff60] | 116 | if (snac.flags & 0x8000) { |
---|
[b7d3cc34] | 117 | aim_bstream_advance(&rx->data, aimbs_get16(&rx->data)); |
---|
[5ebff60] | 118 | } else if (snac.flags & 0x0001) { |
---|
[b7d3cc34] | 119 | /* Following SNAC will be related */ |
---|
| 120 | } |
---|
| 121 | |
---|
[5ebff60] | 122 | for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) { |
---|
[b7d3cc34] | 123 | |
---|
[5ebff60] | 124 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && |
---|
| 125 | (cur->family != snac.family)) { |
---|
[b7d3cc34] | 126 | continue; |
---|
[5ebff60] | 127 | } |
---|
[b7d3cc34] | 128 | |
---|
[5ebff60] | 129 | if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) { |
---|
[b7d3cc34] | 130 | return 1; |
---|
[5ebff60] | 131 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 147 | for (cur = (aim_module_t *) sess->modlistv; cur; cur = cur->next) { |
---|
[b7d3cc34] | 148 | |
---|
[5ebff60] | 149 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && |
---|
| 150 | (cur->family != snac.family)) { |
---|
[b7d3cc34] | 151 | continue; |
---|
[5ebff60] | 152 | } |
---|
[b7d3cc34] | 153 | |
---|
[5ebff60] | 154 | if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) { |
---|
[b7d3cc34] | 155 | return 1; |
---|
[5ebff60] | 156 | } |
---|
[b7d3cc34] | 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? */ |
---|
[5ebff60] | 178 | if (fr->conn->type == AIM_CONN_TYPE_AUTH) { |
---|
[b7d3cc34] | 179 | return consumenonsnac(sess, fr, 0x0017, 0x0003); |
---|
[5ebff60] | 180 | } |
---|
[b7d3cc34] | 181 | |
---|
| 182 | tlvlist = aim_readtlvchain(&fr->data); |
---|
| 183 | |
---|
[5ebff60] | 184 | if (aim_gettlv(tlvlist, 0x0009, 1)) { |
---|
[b7d3cc34] | 185 | code = aim_gettlv16(tlvlist, 0x0009, 1); |
---|
[5ebff60] | 186 | } |
---|
[b7d3cc34] | 187 | |
---|
[5ebff60] | 188 | if (aim_gettlv(tlvlist, 0x000b, 1)) { |
---|
[b7d3cc34] | 189 | msg = aim_gettlv_str(tlvlist, 0x000b, 1); |
---|
[5ebff60] | 190 | } |
---|
[b7d3cc34] | 191 | |
---|
[5ebff60] | 192 | if ((userfunc = aim_callhandler(sess, fr->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) { |
---|
[b7d3cc34] | 193 | ret = userfunc(sess, fr, code, msg); |
---|
[5ebff60] | 194 | } |
---|
[b7d3cc34] | 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[] = { |
---|
[5ebff60] | 212 | { 0x0001, 0x0002 }, |
---|
| 213 | { 0x0001, 0x0003 }, |
---|
| 214 | { 0x0001, 0x0006 }, |
---|
| 215 | { 0x0001, 0x0007 }, |
---|
| 216 | { 0x0001, 0x0008 }, |
---|
| 217 | { 0x0001, 0x0017 }, |
---|
| 218 | { 0x0001, 0x0018 }, |
---|
| 219 | { 0x0000, 0x0000 } |
---|
[b7d3cc34] | 220 | }; |
---|
| 221 | int i; |
---|
| 222 | |
---|
| 223 | for (i = 0; dontuse[i].group != 0x0000; i++) { |
---|
[5ebff60] | 224 | if ((dontuse[i].group == group) && (dontuse[i].type == type)) { |
---|
[b7d3cc34] | 225 | return 1; |
---|
[5ebff60] | 226 | } |
---|
[b7d3cc34] | 227 | } |
---|
| 228 | |
---|
| 229 | return 0; |
---|
| 230 | } |
---|
| 231 | |
---|
[5ebff60] | 232 | int aim_conn_addhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, |
---|
| 233 | aim_rxcallback_t newhandler, guint16 flags) |
---|
[b7d3cc34] | 234 | { |
---|
| 235 | struct aim_rxcblist_s *newcb; |
---|
| 236 | |
---|
[5ebff60] | 237 | if (!conn) { |
---|
[b7d3cc34] | 238 | return -1; |
---|
[5ebff60] | 239 | } |
---|
[b7d3cc34] | 240 | |
---|
| 241 | if (checkdisallowed(family, type)) { |
---|
| 242 | g_assert(0); |
---|
| 243 | return -1; |
---|
| 244 | } |
---|
| 245 | |
---|
[5ebff60] | 246 | if (!(newcb = (struct aim_rxcblist_s *) g_new0(struct aim_rxcblist_s, 1))) { |
---|
[b7d3cc34] | 247 | return -1; |
---|
[5ebff60] | 248 | } |
---|
[b7d3cc34] | 249 | |
---|
| 250 | newcb->family = family; |
---|
| 251 | newcb->type = type; |
---|
| 252 | newcb->flags = flags; |
---|
| 253 | newcb->handler = newhandler; |
---|
| 254 | newcb->next = NULL; |
---|
| 255 | |
---|
[5ebff60] | 256 | if (!conn->handlerlist) { |
---|
| 257 | conn->handlerlist = (void *) newcb; |
---|
| 258 | } else { |
---|
[b7d3cc34] | 259 | struct aim_rxcblist_s *cur; |
---|
| 260 | |
---|
[5ebff60] | 261 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur->next; cur = cur->next) { |
---|
[b7d3cc34] | 262 | ; |
---|
[5ebff60] | 263 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 274 | if (!conn) { |
---|
[b7d3cc34] | 275 | return -1; |
---|
[5ebff60] | 276 | } |
---|
[b7d3cc34] | 277 | |
---|
[5ebff60] | 278 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; ) { |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 294 | if (!conn) { |
---|
[b7d3cc34] | 295 | return NULL; |
---|
[5ebff60] | 296 | } |
---|
[b7d3cc34] | 297 | |
---|
[5ebff60] | 298 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; cur = cur->next) { |
---|
| 299 | if ((cur->family == family) && (cur->type == type)) { |
---|
[b7d3cc34] | 300 | return cur->handler; |
---|
[5ebff60] | 301 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 311 | static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, |
---|
| 312 | aim_frame_t *ptr) |
---|
[b7d3cc34] | 313 | { |
---|
| 314 | aim_rxcallback_t userfunc; |
---|
| 315 | |
---|
[5ebff60] | 316 | if ((userfunc = aim_callhandler(sess, conn, family, type))) { |
---|
[b7d3cc34] | 317 | return userfunc(sess, ptr); |
---|
[5ebff60] | 318 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 349 | if (cur->handled) { |
---|
[b7d3cc34] | 350 | continue; |
---|
[5ebff60] | 351 | } |
---|
[b7d3cc34] | 352 | |
---|
| 353 | if (cur->hdr.flap.type == 0x01) { |
---|
[5ebff60] | 354 | |
---|
| 355 | cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, |
---|
| 356 | AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */ |
---|
| 357 | |
---|
[b7d3cc34] | 358 | continue; |
---|
[5ebff60] | 359 | |
---|
[b7d3cc34] | 360 | } else if (cur->hdr.flap.type == 0x02) { |
---|
| 361 | |
---|
[5ebff60] | 362 | if ((cur->handled = consumesnac(sess, cur))) { |
---|
[b7d3cc34] | 363 | continue; |
---|
[5ebff60] | 364 | } |
---|
[b7d3cc34] | 365 | |
---|
| 366 | } else if (cur->hdr.flap.type == 0x04) { |
---|
| 367 | |
---|
| 368 | cur->handled = negchan_middle(sess, cur); |
---|
| 369 | continue; |
---|
| 370 | |
---|
[5ebff60] | 371 | } else if (cur->hdr.flap.type == 0x05) { |
---|
[b7d3cc34] | 372 | ; |
---|
[5ebff60] | 373 | } |
---|
| 374 | |
---|
[b7d3cc34] | 375 | if (!cur->handled) { |
---|
| 376 | consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */ |
---|
| 377 | cur->handled = 1; |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | |
---|
[5ebff60] | 381 | /* |
---|
[b7d3cc34] | 382 | * This doesn't have to be called here. It could easily be done |
---|
| 383 | * by a seperate 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 | } |
---|