]> andersk Git - libfaim.git/blame - src/snac.c
- Sun Oct 14 19:45:54 PDT 2001
[libfaim.git] / src / snac.c
CommitLineData
9de3ca7e 1/*
2 *
3 * Various SNAC-related dodads...
4 *
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
b13c9e13 7 * response for it has been receieved.
9de3ca7e 8 *
b13c9e13 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.
9de3ca7e 12 *
13 */
14
37ee990e 15#define FAIM_INTERNAL
dd60ff8b 16#include <aim.h>
9de3ca7e 17
b13c9e13 18/*
19 * Called from aim_session_init() to initialize the hash.
20 */
d410cf58 21faim_internal void aim_initsnachash(aim_session_t *sess)
b13c9e13 22{
d410cf58 23 int i;
b13c9e13 24
031c2fb3 25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++)
d410cf58 26 sess->snac_hash[i] = NULL;
b13c9e13 27
d410cf58 28 return;
b13c9e13 29}
30
d410cf58 31faim_internal aim_snacid_t aim_cachesnac(aim_session_t *sess, const fu16_t family, const fu16_t type, const fu16_t flags, const void *data, const int datalen)
1ea867e3 32{
d410cf58 33 aim_snac_t snac;
1ea867e3 34
d410cf58 35 snac.id = sess->snacid_next++;
36 snac.family = family;
37 snac.type = type;
38 snac.flags = flags;
1ea867e3 39
d410cf58 40 if (datalen) {
41 if (!(snac.data = malloc(datalen)))
42 return 0; /* er... */
43 memcpy(snac.data, data, datalen);
44 } else
45 snac.data = NULL;
1ea867e3 46
d410cf58 47 return aim_newsnac(sess, &snac);
1ea867e3 48}
49
b13c9e13 50/*
51 * Clones the passed snac structure and caches it in the
52 * list/hash.
53 */
d410cf58 54faim_internal aim_snacid_t aim_newsnac(aim_session_t *sess, aim_snac_t *newsnac)
a25832e6 55{
d410cf58 56 aim_snac_t *snac;
57 int index;
b13c9e13 58
d410cf58 59 if (!newsnac)
60 return 0;
9de3ca7e 61
d410cf58 62 if (!(snac = malloc(sizeof(aim_snac_t))))
63 return 0;
64 memcpy(snac, newsnac, sizeof(aim_snac_t));
65 snac->issuetime = time(NULL);
b13c9e13 66
d410cf58 67 index = snac->id % FAIM_SNAC_HASH_SIZE;
b13c9e13 68
d410cf58 69 snac->next = (aim_snac_t *)sess->snac_hash[index];
70 sess->snac_hash[index] = (void *)snac;
5b401785 71
d410cf58 72 return snac->id;
a25832e6 73}
9de3ca7e 74
b13c9e13 75/*
76 * Finds a snac structure with the passed SNAC ID,
77 * removes it from the list/hash, and returns a pointer to it.
78 *
79 * The returned structure must be freed by the caller.
80 *
81 */
d410cf58 82faim_internal aim_snac_t *aim_remsnac(aim_session_t *sess, aim_snacid_t id)
a25832e6 83{
d410cf58 84 aim_snac_t *cur, **prev;
85 int index;
86
87 index = id % FAIM_SNAC_HASH_SIZE;
88
d410cf58 89 for (prev = (aim_snac_t **)&sess->snac_hash[index]; (cur = *prev); ) {
90 if (cur->id == id) {
91 *prev = cur->next;
92 return cur;
93 } else
94 prev = &cur->next;
95 }
d410cf58 96
97 return cur;
9de3ca7e 98}
99
100/*
101 * This is for cleaning up old SNACs that either don't get replies or
102 * a reply was never received for. Garabage collection. Plain and simple.
103 *
b13c9e13 104 * maxage is the _minimum_ age in seconds to keep SNACs.
9de3ca7e 105 *
106 */
d410cf58 107faim_internal void aim_cleansnacs(aim_session_t *sess, int maxage)
9de3ca7e 108{
d410cf58 109 int i;
b13c9e13 110
d410cf58 111 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
112 aim_snac_t *cur, **prev;
113 time_t curtime;
53c935f8 114
031c2fb3 115 if (!sess->snac_hash[i])
d410cf58 116 continue;
96f8b1ed 117
d410cf58 118 curtime = time(NULL); /* done here in case we waited for the lock */
96f8b1ed 119
d410cf58 120 for (prev = (aim_snac_t **)&sess->snac_hash[i]; (cur = *prev); ) {
121 if ((curtime - cur->issuetime) > maxage) {
37ee990e 122
d410cf58 123 *prev = cur->next;
96f8b1ed 124
d410cf58 125 /* XXX should we have destructors here? */
126 free(cur->data);
127 free(cur);
96f8b1ed 128
d410cf58 129 } else
130 prev = &cur->next;
131 }
d410cf58 132 }
96f8b1ed 133
d410cf58 134 return;
9de3ca7e 135}
136
d410cf58 137faim_internal int aim_putsnac(aim_bstream_t *bs, fu16_t family, fu16_t subtype, fu16_t flags, aim_snacid_t snacid)
9de3ca7e 138{
d410cf58 139
140 aimbs_put16(bs, family);
141 aimbs_put16(bs, subtype);
142 aimbs_put16(bs, flags);
143 aimbs_put32(bs, snacid);
144
145 return 10;
9de3ca7e 146}
This page took 0.107813 seconds and 5 git commands to generate.