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 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 u_long aim_newsnac(struct aim_session_t *sess,
38 struct aim_snac_t *newsnac)
40 struct aim_snac_t *snac = NULL, *cur = 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 struct aim_snac_t *aim_remsnac(struct aim_session_t *sess,
72 struct aim_snac_t *cur;
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]->next) {
81 if (sess->snac_hash[index]->id == id) {
82 cur = sess->snac_hash[index];
83 sess->snac_hash[index] = NULL;
86 cur = sess->snac_hash[index];
88 if (cur->next->id == id) {
89 struct aim_snac_t *tmp;
92 cur->next = cur->next->next;
99 faim_mutex_unlock(&sess->snac_hash_locks[index]);
105 * This is for cleaning up old SNACs that either don't get replies or
106 * a reply was never received for. Garabage collection. Plain and simple.
108 * maxage is the _minimum_ age in seconds to keep SNACs.
111 int aim_cleansnacs(struct aim_session_t *sess,
114 struct aim_snac_t *cur;
115 struct aim_snac_t *remed = NULL;
119 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
120 faim_mutex_lock(&sess->snac_hash_locks[i]);
121 if (!sess->snac_hash[i])
123 else if (!sess->snac_hash[i]->next) {
124 if ((sess->snac_hash[i]->issuetime + maxage) >= curtime) {
125 remed = sess->snac_hash[i];
129 sess->snac_hash[i] = NULL;
132 cur = sess->snac_hash[i];
133 while(cur && cur->next) {
134 if ((cur->next->issuetime + maxage) >= curtime) {
136 cur->next = cur->next->next;
144 faim_mutex_unlock(&sess->snac_hash_locks[i]);
150 int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
153 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));
154 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff));
155 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff));
156 curbyte += aimutil_put32(buf+curbyte, snacid);