[b7d3cc34] | 1 | /* |
---|
| 2 | * |
---|
| 3 | * Various SNAC-related dodads... |
---|
| 4 | * |
---|
| 5 | * outstanding_snacs is a list of aim_snac_t structs. A SNAC should be added |
---|
| 6 | * whenever a new SNAC is sent and it should remain in the list until the |
---|
| 7 | * response for it has been receieved. |
---|
| 8 | * |
---|
| 9 | * cleansnacs() should be called periodically by the client in order |
---|
| 10 | * to facilitate the aging out of unreplied-to SNACs. This can and does |
---|
| 11 | * happen, so it should be handled. |
---|
| 12 | * |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #include <aim.h> |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * Called from aim_session_init() to initialize the hash. |
---|
| 19 | */ |
---|
| 20 | void aim_initsnachash(aim_session_t *sess) |
---|
| 21 | { |
---|
| 22 | int i; |
---|
| 23 | |
---|
| 24 | for (i = 0; i < AIM_SNAC_HASH_SIZE; i++) |
---|
| 25 | sess->snac_hash[i] = NULL; |
---|
| 26 | |
---|
| 27 | return; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | aim_snacid_t aim_cachesnac(aim_session_t *sess, const guint16 family, const guint16 type, const guint16 flags, const void *data, const int datalen) |
---|
| 31 | { |
---|
| 32 | aim_snac_t snac; |
---|
| 33 | |
---|
| 34 | snac.id = sess->snacid_next++; |
---|
| 35 | snac.family = family; |
---|
| 36 | snac.type = type; |
---|
| 37 | snac.flags = flags; |
---|
| 38 | |
---|
| 39 | if (datalen) { |
---|
| 40 | if (!(snac.data = g_malloc(datalen))) |
---|
| 41 | return 0; /* er... */ |
---|
| 42 | memcpy(snac.data, data, datalen); |
---|
| 43 | } else |
---|
| 44 | snac.data = NULL; |
---|
| 45 | |
---|
| 46 | return aim_newsnac(sess, &snac); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /* |
---|
| 50 | * Clones the passed snac structure and caches it in the |
---|
| 51 | * list/hash. |
---|
| 52 | */ |
---|
| 53 | aim_snacid_t aim_newsnac(aim_session_t *sess, aim_snac_t *newsnac) |
---|
| 54 | { |
---|
| 55 | aim_snac_t *snac; |
---|
| 56 | int index; |
---|
| 57 | |
---|
| 58 | if (!newsnac) |
---|
| 59 | return 0; |
---|
| 60 | |
---|
| 61 | if (!(snac = g_malloc(sizeof(aim_snac_t)))) |
---|
| 62 | return 0; |
---|
| 63 | memcpy(snac, newsnac, sizeof(aim_snac_t)); |
---|
| 64 | snac->issuetime = time(NULL); |
---|
| 65 | |
---|
| 66 | index = snac->id % AIM_SNAC_HASH_SIZE; |
---|
| 67 | |
---|
| 68 | snac->next = (aim_snac_t *)sess->snac_hash[index]; |
---|
| 69 | sess->snac_hash[index] = (void *)snac; |
---|
| 70 | |
---|
| 71 | return snac->id; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* |
---|
| 75 | * Finds a snac structure with the passed SNAC ID, |
---|
| 76 | * removes it from the list/hash, and returns a pointer to it. |
---|
| 77 | * |
---|
| 78 | * The returned structure must be freed by the caller. |
---|
| 79 | * |
---|
| 80 | */ |
---|
| 81 | aim_snac_t *aim_remsnac(aim_session_t *sess, aim_snacid_t id) |
---|
| 82 | { |
---|
| 83 | aim_snac_t *cur, **prev; |
---|
| 84 | int index; |
---|
| 85 | |
---|
| 86 | index = id % AIM_SNAC_HASH_SIZE; |
---|
| 87 | |
---|
| 88 | for (prev = (aim_snac_t **)&sess->snac_hash[index]; (cur = *prev); ) { |
---|
| 89 | if (cur->id == id) { |
---|
| 90 | *prev = cur->next; |
---|
| 91 | return cur; |
---|
| 92 | } else |
---|
| 93 | prev = &cur->next; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return cur; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | * This is for cleaning up old SNACs that either don't get replies or |
---|
| 101 | * a reply was never received for. Garabage collection. Plain and simple. |
---|
| 102 | * |
---|
| 103 | * maxage is the _minimum_ age in seconds to keep SNACs. |
---|
| 104 | * |
---|
| 105 | */ |
---|
| 106 | void aim_cleansnacs(aim_session_t *sess, int maxage) |
---|
| 107 | { |
---|
| 108 | int i; |
---|
| 109 | |
---|
| 110 | for (i = 0; i < AIM_SNAC_HASH_SIZE; i++) { |
---|
| 111 | aim_snac_t *cur, **prev; |
---|
| 112 | time_t curtime; |
---|
| 113 | |
---|
| 114 | if (!sess->snac_hash[i]) |
---|
| 115 | continue; |
---|
| 116 | |
---|
| 117 | curtime = time(NULL); /* done here in case we waited for the lock */ |
---|
| 118 | |
---|
| 119 | for (prev = (aim_snac_t **)&sess->snac_hash[i]; (cur = *prev); ) { |
---|
| 120 | if ((curtime - cur->issuetime) > maxage) { |
---|
| 121 | |
---|
| 122 | *prev = cur->next; |
---|
| 123 | |
---|
| 124 | /* XXX should we have destructors here? */ |
---|
| 125 | g_free(cur->data); |
---|
| 126 | g_free(cur); |
---|
| 127 | |
---|
| 128 | } else |
---|
| 129 | prev = &cur->next; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | int aim_putsnac(aim_bstream_t *bs, guint16 family, guint16 subtype, guint16 flags, aim_snacid_t snacid) |
---|
| 137 | { |
---|
| 138 | |
---|
| 139 | aimbs_put16(bs, family); |
---|
| 140 | aimbs_put16(bs, subtype); |
---|
| 141 | aimbs_put16(bs, flags); |
---|
| 142 | aimbs_put32(bs, snacid); |
---|
| 143 | |
---|
| 144 | return 10; |
---|
| 145 | } |
---|