]> andersk Git - libfaim.git/blame - aim_snac.c
- Fri Sep 1 23:34:28 UTC 2000
[libfaim.git] / aim_snac.c
CommitLineData
9de3ca7e 1
2/*
3 *
4 * Various SNAC-related dodads...
5 *
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
b13c9e13 8 * response for it has been receieved.
9de3ca7e 9 *
b13c9e13 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.
9de3ca7e 13 *
14 */
15
a25832e6 16#include <faim/aim.h>
9de3ca7e 17
b13c9e13 18/*
19 * Called from aim_session_init() to initialize the hash.
20 */
21void aim_initsnachash(struct aim_session_t *sess)
22{
23 int i;
24
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]);
28 }
29
30 return;
31}
32
33/*
34 * Clones the passed snac structure and caches it in the
35 * list/hash.
36 */
a25832e6 37u_long aim_newsnac(struct aim_session_t *sess,
38 struct aim_snac_t *newsnac)
39{
40 struct aim_snac_t *snac = NULL, *cur = NULL;
b13c9e13 41 int index;
42
a25832e6 43 if (!newsnac)
44 return 0;
9de3ca7e 45
17d80b55 46 if (!(snac = calloc(1, sizeof(struct aim_snac_t))))
a25832e6 47 return 0;
48 memcpy(snac, newsnac, sizeof(struct aim_snac_t));
49 snac->issuetime = time(&snac->issuetime);
50 snac->next = NULL;
b13c9e13 51
52 index = snac->id % FAIM_SNAC_HASH_SIZE;
53
54 faim_mutex_lock(&sess->snac_hash_locks[index]);
17d80b55 55 snac->next = sess->snac_hash[index];
56 sess->snac_hash[index] = snac;
b13c9e13 57 faim_mutex_unlock(&sess->snac_hash_locks[index]);
5b401785 58
a25832e6 59 return(snac->id);
60}
9de3ca7e 61
b13c9e13 62/*
63 * Finds a snac structure with the passed SNAC ID,
64 * removes it from the list/hash, and returns a pointer to it.
65 *
66 * The returned structure must be freed by the caller.
67 *
68 */
a25832e6 69struct aim_snac_t *aim_remsnac(struct aim_session_t *sess,
70 u_long id)
71{
72 struct aim_snac_t *cur;
b13c9e13 73 int index;
5b401785 74
b13c9e13 75 index = id % FAIM_SNAC_HASH_SIZE;
5b401785 76
b13c9e13 77 faim_mutex_lock(&sess->snac_hash_locks[index]);
78 if (!sess->snac_hash[index])
79 ;
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;
84 }
85 } else {
86 cur = sess->snac_hash[index];
87 while (cur->next) {
88 if (cur->next->id == id) {
89 struct aim_snac_t *tmp;
90
91 tmp = cur->next;
92 cur->next = cur->next->next;
93 cur = tmp;
94 break;
95 }
96 cur = cur->next;
a25832e6 97 }
a25832e6 98 }
b13c9e13 99 faim_mutex_unlock(&sess->snac_hash_locks[index]);
100
101 return cur;
9de3ca7e 102}
103
104/*
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.
107 *
b13c9e13 108 * maxage is the _minimum_ age in seconds to keep SNACs.
9de3ca7e 109 *
110 */
a25832e6 111int aim_cleansnacs(struct aim_session_t *sess,
112 int maxage)
9de3ca7e 113{
a25832e6 114 struct aim_snac_t *cur;
9de3ca7e 115 struct aim_snac_t *remed = NULL;
116 time_t curtime;
b13c9e13 117 int i;
118
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])
122 ;
123 else if (!sess->snac_hash[i]->next) {
124 if ((sess->snac_hash[i]->issuetime + maxage) >= curtime) {
125 remed = sess->snac_hash[i];
126 if(remed->data)
127 free(remed->data);
128 free(remed);
129 sess->snac_hash[i] = NULL;
130 }
131 } else {
132 cur = sess->snac_hash[i];
133 while(cur && cur->next) {
134 if ((cur->next->issuetime + maxage) >= curtime) {
135 remed = cur->next;
136 cur->next = cur->next->next;
137 if (remed->data)
138 free(remed->data);
139 free(remed);
9de3ca7e 140 }
b13c9e13 141 cur = cur->next;
142 }
9de3ca7e 143 }
b13c9e13 144 faim_mutex_unlock(&sess->snac_hash_locks[i]);
145 }
146
9de3ca7e 147 return 0;
148}
149
150int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
151{
152 int curbyte = 0;
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);
157 return curbyte;
158}
This page took 0.07145 seconds and 5 git commands to generate.