]>
Commit | Line | Data |
---|---|---|
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 | |
8 | * response for it has been receieved. | |
9 | * | |
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. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <faim/aim.h> | |
17 | ||
18 | /* | |
19 | * Called from aim_session_init() to initialize the hash. | |
20 | */ | |
21 | faim_internal void 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 | 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) | |
38 | { | |
39 | struct aim_snac_t snac; | |
40 | ||
41 | snac.id = sess->snac_nextid++; | |
42 | snac.family = family; | |
43 | snac.type = type; | |
44 | snac.flags = flags; | |
45 | ||
46 | snac.data = malloc(datalen); | |
47 | memcpy(snac.data, data, datalen); | |
48 | ||
49 | return aim_newsnac(sess, &snac); | |
50 | } | |
51 | ||
52 | /* | |
53 | * Clones the passed snac structure and caches it in the | |
54 | * list/hash. | |
55 | */ | |
56 | faim_internal unsigned long aim_newsnac(struct aim_session_t *sess, | |
57 | struct aim_snac_t *newsnac) | |
58 | { | |
59 | struct aim_snac_t *snac = NULL; | |
60 | int index; | |
61 | ||
62 | if (!newsnac) | |
63 | return 0; | |
64 | ||
65 | if (!(snac = calloc(1, sizeof(struct aim_snac_t)))) | |
66 | return 0; | |
67 | memcpy(snac, newsnac, sizeof(struct aim_snac_t)); | |
68 | snac->issuetime = time(&snac->issuetime); | |
69 | snac->next = NULL; | |
70 | ||
71 | index = snac->id % FAIM_SNAC_HASH_SIZE; | |
72 | ||
73 | faim_mutex_lock(&sess->snac_hash_locks[index]); | |
74 | snac->next = sess->snac_hash[index]; | |
75 | sess->snac_hash[index] = snac; | |
76 | faim_mutex_unlock(&sess->snac_hash_locks[index]); | |
77 | ||
78 | return(snac->id); | |
79 | } | |
80 | ||
81 | /* | |
82 | * Finds a snac structure with the passed SNAC ID, | |
83 | * removes it from the list/hash, and returns a pointer to it. | |
84 | * | |
85 | * The returned structure must be freed by the caller. | |
86 | * | |
87 | */ | |
88 | faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, | |
89 | u_long id) | |
90 | { | |
91 | struct aim_snac_t *cur = NULL; | |
92 | int index; | |
93 | ||
94 | index = id % FAIM_SNAC_HASH_SIZE; | |
95 | ||
96 | faim_mutex_lock(&sess->snac_hash_locks[index]); | |
97 | if (!sess->snac_hash[index]) | |
98 | ; | |
99 | else if (sess->snac_hash[index]->id == id) { | |
100 | cur = sess->snac_hash[index]; | |
101 | sess->snac_hash[index] = cur->next; | |
102 | } else { | |
103 | cur = sess->snac_hash[index]; | |
104 | while (cur->next) { | |
105 | if (cur->next->id == id) { | |
106 | struct aim_snac_t *tmp; | |
107 | ||
108 | tmp = cur->next; | |
109 | cur->next = cur->next->next; | |
110 | cur = tmp; | |
111 | break; | |
112 | } | |
113 | cur = cur->next; | |
114 | } | |
115 | } | |
116 | faim_mutex_unlock(&sess->snac_hash_locks[index]); | |
117 | ||
118 | return cur; | |
119 | } | |
120 | ||
121 | /* | |
122 | * This is for cleaning up old SNACs that either don't get replies or | |
123 | * a reply was never received for. Garabage collection. Plain and simple. | |
124 | * | |
125 | * maxage is the _minimum_ age in seconds to keep SNACs. | |
126 | * | |
127 | */ | |
128 | faim_internal int aim_cleansnacs(struct aim_session_t *sess, | |
129 | int maxage) | |
130 | { | |
131 | int i; | |
132 | ||
133 | for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { | |
134 | struct aim_snac_t *cur = NULL, *next = NULL, *prev = NULL; | |
135 | time_t curtime; | |
136 | ||
137 | faim_mutex_lock(&sess->snac_hash_locks[i]); | |
138 | if (!sess->snac_hash[i]) { | |
139 | faim_mutex_unlock(&sess->snac_hash_locks[i]); | |
140 | continue; | |
141 | } | |
142 | ||
143 | curtime = time(NULL); /* done here in case we waited for the lock */ | |
144 | ||
145 | cur = sess->snac_hash[i]; | |
146 | while (cur) { | |
147 | next = cur->next; | |
148 | if ((curtime - cur->issuetime) > maxage) { | |
149 | if (sess->snac_hash[i] == cur) | |
150 | prev = sess->snac_hash[i] = next; | |
151 | else | |
152 | prev->next = next; | |
153 | ||
154 | /* XXX should we have destructors here? */ | |
155 | if (cur->data) | |
156 | free(cur->data); | |
157 | free(cur); | |
158 | ||
159 | } else { | |
160 | prev = cur; | |
161 | } | |
162 | cur = next; | |
163 | } | |
164 | ||
165 | faim_mutex_unlock(&sess->snac_hash_locks[i]); | |
166 | } | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
171 | faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) | |
172 | { | |
173 | int curbyte = 0; | |
174 | curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); | |
175 | curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); | |
176 | curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); | |
177 | curbyte += aimutil_put32(buf+curbyte, snacid); | |
178 | return curbyte; | |
179 | } |