[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 | |
---|
| 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 | |
---|
| 32 | static 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 | |
---|
| 44 | int 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 | |
---|
| 73 | void 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 | |
---|
| 95 | static 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 | for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) { |
---|
| 116 | |
---|
| 117 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && |
---|
| 118 | (cur->family != snac.family)) |
---|
| 119 | continue; |
---|
| 120 | |
---|
| 121 | if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) |
---|
| 122 | return 1; |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | static int consumenonsnac(aim_session_t *sess, aim_frame_t *rx, guint16 family, guint16 subtype) |
---|
| 130 | { |
---|
| 131 | aim_module_t *cur; |
---|
| 132 | aim_modsnac_t snac; |
---|
| 133 | |
---|
| 134 | snac.family = family; |
---|
| 135 | snac.subtype = subtype; |
---|
| 136 | snac.flags = snac.id = 0; |
---|
| 137 | |
---|
| 138 | for (cur = (aim_module_t *)sess->modlistv; cur; cur = cur->next) { |
---|
| 139 | |
---|
| 140 | if (!(cur->flags & AIM_MODFLAG_MULTIFAMILY) && |
---|
| 141 | (cur->family != snac.family)) |
---|
| 142 | continue; |
---|
| 143 | |
---|
| 144 | if (cur->snachandler(sess, cur, rx, &snac, &rx->data)) |
---|
| 145 | return 1; |
---|
| 146 | |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | return 0; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static int negchan_middle(aim_session_t *sess, aim_frame_t *fr) |
---|
| 153 | { |
---|
| 154 | aim_tlvlist_t *tlvlist; |
---|
| 155 | char *msg = NULL; |
---|
| 156 | guint16 code = 0; |
---|
| 157 | aim_rxcallback_t userfunc; |
---|
| 158 | int ret = 1; |
---|
| 159 | |
---|
| 160 | if (aim_bstream_empty(&fr->data) == 0) { |
---|
| 161 | /* XXX should do something with this */ |
---|
| 162 | return 1; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /* Used only by the older login protocol */ |
---|
| 166 | /* XXX remove this special case? */ |
---|
| 167 | if (fr->conn->type == AIM_CONN_TYPE_AUTH) |
---|
| 168 | return consumenonsnac(sess, fr, 0x0017, 0x0003); |
---|
| 169 | |
---|
| 170 | tlvlist = aim_readtlvchain(&fr->data); |
---|
| 171 | |
---|
| 172 | if (aim_gettlv(tlvlist, 0x0009, 1)) |
---|
| 173 | code = aim_gettlv16(tlvlist, 0x0009, 1); |
---|
| 174 | |
---|
| 175 | if (aim_gettlv(tlvlist, 0x000b, 1)) |
---|
| 176 | msg = aim_gettlv_str(tlvlist, 0x000b, 1); |
---|
| 177 | |
---|
| 178 | if ((userfunc = aim_callhandler(sess, fr->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNERR))) |
---|
| 179 | ret = userfunc(sess, fr, code, msg); |
---|
| 180 | |
---|
| 181 | aim_freetlvchain(&tlvlist); |
---|
| 182 | |
---|
| 183 | g_free(msg); |
---|
| 184 | |
---|
| 185 | return ret; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /* |
---|
| 189 | * Some SNACs we do not allow to be hooked, for good reason. |
---|
| 190 | */ |
---|
| 191 | static int checkdisallowed(guint16 group, guint16 type) |
---|
| 192 | { |
---|
| 193 | static const struct { |
---|
| 194 | guint16 group; |
---|
| 195 | guint16 type; |
---|
| 196 | } dontuse[] = { |
---|
| 197 | {0x0001, 0x0002}, |
---|
| 198 | {0x0001, 0x0003}, |
---|
| 199 | {0x0001, 0x0006}, |
---|
| 200 | {0x0001, 0x0007}, |
---|
| 201 | {0x0001, 0x0008}, |
---|
| 202 | {0x0001, 0x0017}, |
---|
| 203 | {0x0001, 0x0018}, |
---|
| 204 | {0x0000, 0x0000} |
---|
| 205 | }; |
---|
| 206 | int i; |
---|
| 207 | |
---|
| 208 | for (i = 0; dontuse[i].group != 0x0000; i++) { |
---|
| 209 | if ((dontuse[i].group == group) && (dontuse[i].type == type)) |
---|
| 210 | return 1; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | return 0; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | int aim_conn_addhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type, aim_rxcallback_t newhandler, guint16 flags) |
---|
| 217 | { |
---|
| 218 | struct aim_rxcblist_s *newcb; |
---|
| 219 | |
---|
| 220 | if (!conn) |
---|
| 221 | return -1; |
---|
| 222 | |
---|
| 223 | if (checkdisallowed(family, type)) { |
---|
| 224 | g_assert(0); |
---|
| 225 | return -1; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | if (!(newcb = (struct aim_rxcblist_s *)g_new0(struct aim_rxcblist_s, 1))) |
---|
| 229 | return -1; |
---|
| 230 | |
---|
| 231 | newcb->family = family; |
---|
| 232 | newcb->type = type; |
---|
| 233 | newcb->flags = flags; |
---|
| 234 | newcb->handler = newhandler; |
---|
| 235 | newcb->next = NULL; |
---|
| 236 | |
---|
| 237 | if (!conn->handlerlist) |
---|
| 238 | conn->handlerlist = (void *)newcb; |
---|
| 239 | else { |
---|
| 240 | struct aim_rxcblist_s *cur; |
---|
| 241 | |
---|
| 242 | for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur->next; cur = cur->next) |
---|
| 243 | ; |
---|
| 244 | cur->next = newcb; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | return 0; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | int aim_clearhandlers(aim_conn_t *conn) |
---|
| 251 | { |
---|
| 252 | struct aim_rxcblist_s *cur; |
---|
| 253 | |
---|
| 254 | if (!conn) |
---|
| 255 | return -1; |
---|
| 256 | |
---|
| 257 | for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; ) { |
---|
| 258 | struct aim_rxcblist_s *tmp; |
---|
| 259 | |
---|
| 260 | tmp = cur->next; |
---|
| 261 | g_free(cur); |
---|
| 262 | cur = tmp; |
---|
| 263 | } |
---|
| 264 | conn->handlerlist = NULL; |
---|
| 265 | |
---|
| 266 | return 0; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | aim_rxcallback_t aim_callhandler(aim_session_t *sess, aim_conn_t *conn, guint16 family, guint16 type) |
---|
| 270 | { |
---|
| 271 | struct aim_rxcblist_s *cur; |
---|
| 272 | |
---|
| 273 | if (!conn) |
---|
| 274 | return NULL; |
---|
| 275 | |
---|
| 276 | for (cur = (struct aim_rxcblist_s *)conn->handlerlist; cur; cur = cur->next) { |
---|
| 277 | if ((cur->family == family) && (cur->type == type)) |
---|
| 278 | return cur->handler; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | if (type == AIM_CB_SPECIAL_DEFAULT) { |
---|
| 282 | return NULL; /* prevent infinite recursion */ |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | return aim_callhandler(sess, conn, family, AIM_CB_SPECIAL_DEFAULT); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | static int aim_callhandler_noparam(aim_session_t *sess, aim_conn_t *conn,guint16 family, guint16 type, aim_frame_t *ptr) |
---|
| 289 | { |
---|
| 290 | aim_rxcallback_t userfunc; |
---|
| 291 | |
---|
| 292 | if ((userfunc = aim_callhandler(sess, conn, family, type))) |
---|
| 293 | return userfunc(sess, ptr); |
---|
| 294 | |
---|
| 295 | return 1; /* XXX */ |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | /* |
---|
| 299 | * aim_rxdispatch() |
---|
| 300 | * |
---|
| 301 | * Basically, heres what this should do: |
---|
| 302 | * 1) Determine correct packet handler for this packet |
---|
| 303 | * 2) Mark the packet handled (so it can be dequeued in purge_queue()) |
---|
| 304 | * 3) Send the packet to the packet handler |
---|
| 305 | * 4) Go to next packet in the queue and start over |
---|
| 306 | * 5) When done, run purge_queue() to purge handled commands |
---|
| 307 | * |
---|
| 308 | * TODO: Clean up. |
---|
| 309 | * TODO: More support for mid-level handlers. |
---|
| 310 | * TODO: Allow for NULL handlers. |
---|
| 311 | * |
---|
| 312 | */ |
---|
| 313 | void aim_rxdispatch(aim_session_t *sess) |
---|
| 314 | { |
---|
| 315 | int i; |
---|
| 316 | aim_frame_t *cur; |
---|
| 317 | |
---|
| 318 | for (cur = sess->queue_incoming, i = 0; cur; cur = cur->next, i++) { |
---|
| 319 | |
---|
| 320 | /* |
---|
| 321 | * XXX: This is still fairly ugly. |
---|
| 322 | */ |
---|
| 323 | |
---|
| 324 | if (cur->handled) |
---|
| 325 | continue; |
---|
| 326 | |
---|
| 327 | if (cur->hdr.flap.type == 0x01) { |
---|
| 328 | |
---|
| 329 | cur->handled = aim_callhandler_noparam(sess, cur->conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_FLAPVER, cur); /* XXX use consumenonsnac */ |
---|
| 330 | |
---|
| 331 | continue; |
---|
| 332 | |
---|
| 333 | } else if (cur->hdr.flap.type == 0x02) { |
---|
| 334 | |
---|
| 335 | if ((cur->handled = consumesnac(sess, cur))) |
---|
| 336 | continue; |
---|
| 337 | |
---|
| 338 | } else if (cur->hdr.flap.type == 0x04) { |
---|
| 339 | |
---|
| 340 | cur->handled = negchan_middle(sess, cur); |
---|
| 341 | continue; |
---|
| 342 | |
---|
| 343 | } else if (cur->hdr.flap.type == 0x05) |
---|
| 344 | ; |
---|
| 345 | |
---|
| 346 | if (!cur->handled) { |
---|
| 347 | consumenonsnac(sess, cur, 0xffff, 0xffff); /* last chance! */ |
---|
| 348 | cur->handled = 1; |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /* |
---|
| 353 | * This doesn't have to be called here. It could easily be done |
---|
| 354 | * by a seperate thread or something. It's an administrative operation, |
---|
| 355 | * and can take a while. Though the less you call it the less memory |
---|
| 356 | * you'll have :) |
---|
| 357 | */ |
---|
| 358 | aim_purge_rxqueue(sess); |
---|
| 359 | |
---|
| 360 | return; |
---|
| 361 | } |
---|