4 * Various SNAC-related dodads...
6 * outstanding_snacs is a list of aim_snac_t structs. A SNAC should be added
7 * whenever a new SNAC is sent and it should remain in the list until the
8 * response for it has been receieved.
10 * cleansnacs() should be called periodically by the client in order
11 * to facilitate the aging out of unreplied-to SNACs. This can and does
12 * happen, so it should be handled.
19 * Called from aim_session_init() to initialize the hash.
21 faim_internal void aim_initsnachash(struct aim_session_t *sess)
25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
26 sess->snac_hash[i] = NULL;
27 faim_mutex_init(&sess->snac_hash_locks[i]);
34 * Clones the passed snac structure and caches it in the
37 faim_internal unsigned long aim_newsnac(struct aim_session_t *sess,
38 struct aim_snac_t *newsnac)
40 struct aim_snac_t *snac = NULL;
46 if (!(snac = calloc(1, sizeof(struct aim_snac_t))))
48 memcpy(snac, newsnac, sizeof(struct aim_snac_t));
49 snac->issuetime = time(&snac->issuetime);
52 index = snac->id % FAIM_SNAC_HASH_SIZE;
54 faim_mutex_lock(&sess->snac_hash_locks[index]);
55 snac->next = sess->snac_hash[index];
56 sess->snac_hash[index] = snac;
57 faim_mutex_unlock(&sess->snac_hash_locks[index]);
63 * Finds a snac structure with the passed SNAC ID,
64 * removes it from the list/hash, and returns a pointer to it.
66 * The returned structure must be freed by the caller.
69 faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess,
72 struct aim_snac_t *cur = NULL;
75 index = id % FAIM_SNAC_HASH_SIZE;
77 faim_mutex_lock(&sess->snac_hash_locks[index]);
78 if (!sess->snac_hash[index])
80 else if (sess->snac_hash[index]->id == id) {
81 cur = sess->snac_hash[index];
82 sess->snac_hash[index] = cur->next;
84 cur = sess->snac_hash[index];
86 if (cur->next->id == id) {
87 struct aim_snac_t *tmp;
90 cur->next = cur->next->next;
97 faim_mutex_unlock(&sess->snac_hash_locks[index]);
103 * This is for cleaning up old SNACs that either don't get replies or
104 * a reply was never received for. Garabage collection. Plain and simple.
106 * maxage is the _minimum_ age in seconds to keep SNACs.
109 faim_internal int aim_cleansnacs(struct aim_session_t *sess,
114 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
115 struct aim_snac_t *cur = NULL, *next = NULL, *prev = NULL;
118 faim_mutex_lock(&sess->snac_hash_locks[i]);
119 if (!sess->snac_hash[i]) {
120 faim_mutex_unlock(&sess->snac_hash_locks[i]);
124 curtime = time(NULL); /* done here in case we waited for the lock */
126 cur = sess->snac_hash[i];
129 if ((curtime - cur->issuetime) > maxage) {
130 if (sess->snac_hash[i] == cur)
131 prev = sess->snac_hash[i] = next;
135 /* XXX should we have destructors here? */
146 faim_mutex_unlock(&sess->snac_hash_locks[i]);
152 faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
155 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));
156 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff));
157 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff));
158 curbyte += aimutil_put32(buf+curbyte, snacid);