[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 | |
---|
[d57484d] | 237 | g_return_val_if_fail(conn, -1); |
---|
[188b75e] | 238 | g_return_val_if_fail(!checkdisallowed(family, type), -1); |
---|
[b7d3cc34] | 239 | |
---|
[5ebff60] | 240 | if (!(newcb = (struct aim_rxcblist_s *) g_new0(struct aim_rxcblist_s, 1))) { |
---|
[b7d3cc34] | 241 | return -1; |
---|
[5ebff60] | 242 | } |
---|
[b7d3cc34] | 243 | |
---|
| 244 | newcb->family = family; |
---|
| 245 | newcb->type = type; |
---|
| 246 | newcb->flags = flags; |
---|
| 247 | newcb->handler = newhandler; |
---|
| 248 | newcb->next = NULL; |
---|
| 249 | |
---|
[5ebff60] | 250 | if (!conn->handlerlist) { |
---|
| 251 | conn->handlerlist = (void *) newcb; |
---|
| 252 | } else { |
---|
[b7d3cc34] | 253 | struct aim_rxcblist_s *cur; |
---|
| 254 | |
---|
[5ebff60] | 255 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur->next; cur = cur->next) { |
---|
[b7d3cc34] | 256 | ; |
---|
[5ebff60] | 257 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 268 | if (!conn) { |
---|
[b7d3cc34] | 269 | return -1; |
---|
[5ebff60] | 270 | } |
---|
[b7d3cc34] | 271 | |
---|
[5ebff60] | 272 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; ) { |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 288 | if (!conn) { |
---|
[b7d3cc34] | 289 | return NULL; |
---|
[5ebff60] | 290 | } |
---|
[b7d3cc34] | 291 | |
---|
[5ebff60] | 292 | for (cur = (struct aim_rxcblist_s *) conn->handlerlist; cur; cur = cur->next) { |
---|
| 293 | if ((cur->family == family) && (cur->type == type)) { |
---|
[b7d3cc34] | 294 | return cur->handler; |
---|
[5ebff60] | 295 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 305 | static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, |
---|
| 306 | aim_frame_t *ptr) |
---|
[b7d3cc34] | 307 | { |
---|
| 308 | aim_rxcallback_t userfunc; |
---|
| 309 | |
---|
[5ebff60] | 310 | if ((userfunc = aim_callhandler(sess, conn, family, type))) { |
---|
[b7d3cc34] | 311 | return userfunc(sess, ptr); |
---|
[5ebff60] | 312 | } |
---|
[b7d3cc34] | 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 | |
---|
[5ebff60] | 343 | if (cur->handled) { |
---|
[b7d3cc34] | 344 | continue; |
---|
[5ebff60] | 345 | } |
---|
[b7d3cc34] | 346 | |
---|
| 347 | if (cur->hdr.flap.type == 0x01) { |
---|
[5ebff60] | 348 | |
---|
| 349 | cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, |
---|
| 350 | AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */ |
---|
| 351 | |
---|
[b7d3cc34] | 352 | continue; |
---|
[5ebff60] | 353 | |
---|
[b7d3cc34] | 354 | } else if (cur->hdr.flap.type == 0x02) { |
---|
| 355 | |
---|
[5ebff60] | 356 | if ((cur->handled = consumesnac(sess, cur))) { |
---|
[b7d3cc34] | 357 | continue; |
---|
[5ebff60] | 358 | } |
---|
[b7d3cc34] | 359 | |
---|
| 360 | } else if (cur->hdr.flap.type == 0x04) { |
---|
| 361 | |
---|
| 362 | cur->handled = negchan_middle(sess, cur); |
---|
| 363 | continue; |
---|
| 364 | |
---|
[5ebff60] | 365 | } else if (cur->hdr.flap.type == 0x05) { |
---|
[b7d3cc34] | 366 | ; |
---|
[5ebff60] | 367 | } |
---|
| 368 | |
---|
[b7d3cc34] | 369 | if (!cur->handled) { |
---|
| 370 | consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */ |
---|
| 371 | cur->handled = 1; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
[5ebff60] | 375 | /* |
---|
[b7d3cc34] | 376 | * This doesn't have to be called here. It could easily be done |
---|
[e88fe7da] | 377 | * by a separate thread or something. It's an administrative operation, |
---|
[b7d3cc34] | 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 | } |
---|