3 * Various SNAC-related dodads...
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.
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.
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]);
33 faim_internal unsigned long aim_cachesnac(struct aim_session_t *sess,
34 const unsigned short family,
35 const unsigned short type,
36 const unsigned short flags,
37 const void *data, const int datalen)
39 struct aim_snac_t snac;
41 snac.id = sess->snac_nextid++;
47 if (!(snac.data = malloc(datalen)))
49 memcpy(snac.data, data, datalen);
53 return aim_newsnac(sess, &snac);
57 * Clones the passed snac structure and caches it in the
60 faim_internal unsigned long aim_newsnac(struct aim_session_t *sess,
61 struct aim_snac_t *newsnac)
63 struct aim_snac_t *snac = NULL;
69 if (!(snac = calloc(1, sizeof(struct aim_snac_t))))
71 memcpy(snac, newsnac, sizeof(struct aim_snac_t));
72 snac->issuetime = time(&snac->issuetime);
75 index = snac->id % FAIM_SNAC_HASH_SIZE;
77 faim_mutex_lock(&sess->snac_hash_locks[index]);
78 snac->next = sess->snac_hash[index];
79 sess->snac_hash[index] = snac;
80 faim_mutex_unlock(&sess->snac_hash_locks[index]);
86 * Finds a snac structure with the passed SNAC ID,
87 * removes it from the list/hash, and returns a pointer to it.
89 * The returned structure must be freed by the caller.
92 faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess,
95 struct aim_snac_t *cur = NULL;
98 index = id % FAIM_SNAC_HASH_SIZE;
100 faim_mutex_lock(&sess->snac_hash_locks[index]);
101 if (!sess->snac_hash[index])
103 else if (sess->snac_hash[index]->id == id) {
104 cur = sess->snac_hash[index];
105 sess->snac_hash[index] = cur->next;
107 cur = sess->snac_hash[index];
109 if (cur->next->id == id) {
110 struct aim_snac_t *tmp;
113 cur->next = cur->next->next;
120 faim_mutex_unlock(&sess->snac_hash_locks[index]);
126 * This is for cleaning up old SNACs that either don't get replies or
127 * a reply was never received for. Garabage collection. Plain and simple.
129 * maxage is the _minimum_ age in seconds to keep SNACs.
132 faim_internal int aim_cleansnacs(struct aim_session_t *sess,
137 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
138 struct aim_snac_t *cur, **prev;
141 faim_mutex_lock(&sess->snac_hash_locks[i]);
142 if (!sess->snac_hash[i]) {
143 faim_mutex_unlock(&sess->snac_hash_locks[i]);
147 curtime = time(NULL); /* done here in case we waited for the lock */
149 for (prev = &sess->snac_hash[i]; (cur = *prev); ) {
150 if ((curtime - cur->issuetime) > maxage) {
154 /* XXX should we have destructors here? */
163 faim_mutex_unlock(&sess->snac_hash_locks[i]);
169 faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
172 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));
173 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff));
174 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff));
175 curbyte += aimutil_put32(buf+curbyte, snacid);