comparison src/protocols/oscar/snac.c @ 2246:933346315b9b

[gaim-migrate @ 2256] heh. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sun, 09 Sep 2001 10:07:14 +0000
parents 424a40f12a6c
children d82efea341ef
comparison
equal deleted inserted replaced
2245:31157c54fe6e 2246:933346315b9b
16 #include <aim.h> 16 #include <aim.h>
17 17
18 /* 18 /*
19 * Called from aim_session_init() to initialize the hash. 19 * Called from aim_session_init() to initialize the hash.
20 */ 20 */
21 faim_internal void aim_initsnachash(struct aim_session_t *sess) 21 faim_internal void aim_initsnachash(aim_session_t *sess)
22 { 22 {
23 int i; 23 int i;
24 24
25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { 25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
26 sess->snac_hash[i] = NULL; 26 sess->snac_hash[i] = NULL;
27 faim_mutex_init(&sess->snac_hash_locks[i]); 27 faim_mutex_init(&sess->snac_hash_locks[i]);
28 } 28 }
29 29
30 return; 30 return;
31 } 31 }
32 32
33 faim_internal unsigned long aim_cachesnac(struct aim_session_t *sess, 33 faim_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)
34 const unsigned short family,
35 const unsigned short type,
36 const unsigned short flags,
37 const void *data, const int datalen)
38 { 34 {
39 struct aim_snac_t snac; 35 aim_snac_t snac;
40 36
41 snac.id = sess->snac_nextid++; 37 snac.id = sess->snacid_next++;
42 snac.family = family; 38 snac.family = family;
43 snac.type = type; 39 snac.type = type;
44 snac.flags = flags; 40 snac.flags = flags;
45 41
46 if (datalen) { 42 if (datalen) {
47 if (!(snac.data = malloc(datalen))) 43 if (!(snac.data = malloc(datalen)))
48 return 0; /* er... */ 44 return 0; /* er... */
49 memcpy(snac.data, data, datalen); 45 memcpy(snac.data, data, datalen);
50 } else 46 } else
51 snac.data = NULL; 47 snac.data = NULL;
52 48
53 return aim_newsnac(sess, &snac); 49 return aim_newsnac(sess, &snac);
54 } 50 }
55 51
56 /* 52 /*
57 * Clones the passed snac structure and caches it in the 53 * Clones the passed snac structure and caches it in the
58 * list/hash. 54 * list/hash.
59 */ 55 */
60 faim_internal unsigned long aim_newsnac(struct aim_session_t *sess, 56 faim_internal aim_snacid_t aim_newsnac(aim_session_t *sess, aim_snac_t *newsnac)
61 struct aim_snac_t *newsnac)
62 { 57 {
63 struct aim_snac_t *snac = NULL; 58 aim_snac_t *snac;
64 int index; 59 int index;
65 60
66 if (!newsnac) 61 if (!newsnac)
67 return 0; 62 return 0;
68 63
69 if (!(snac = calloc(1, sizeof(struct aim_snac_t)))) 64 if (!(snac = malloc(sizeof(aim_snac_t))))
70 return 0; 65 return 0;
71 memcpy(snac, newsnac, sizeof(struct aim_snac_t)); 66 memcpy(snac, newsnac, sizeof(aim_snac_t));
72 snac->issuetime = time(&snac->issuetime); 67 snac->issuetime = time(NULL);
73 snac->next = NULL;
74 68
75 index = snac->id % FAIM_SNAC_HASH_SIZE; 69 index = snac->id % FAIM_SNAC_HASH_SIZE;
76 70
77 faim_mutex_lock(&sess->snac_hash_locks[index]); 71 faim_mutex_lock(&sess->snac_hash_locks[index]);
78 snac->next = sess->snac_hash[index]; 72 snac->next = (aim_snac_t *)sess->snac_hash[index];
79 sess->snac_hash[index] = snac; 73 sess->snac_hash[index] = (void *)snac;
80 faim_mutex_unlock(&sess->snac_hash_locks[index]); 74 faim_mutex_unlock(&sess->snac_hash_locks[index]);
81 75
82 return(snac->id); 76 return snac->id;
83 } 77 }
84 78
85 /* 79 /*
86 * Finds a snac structure with the passed SNAC ID, 80 * Finds a snac structure with the passed SNAC ID,
87 * removes it from the list/hash, and returns a pointer to it. 81 * removes it from the list/hash, and returns a pointer to it.
88 * 82 *
89 * The returned structure must be freed by the caller. 83 * The returned structure must be freed by the caller.
90 * 84 *
91 */ 85 */
92 faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, 86 faim_internal aim_snac_t *aim_remsnac(aim_session_t *sess, aim_snacid_t id)
93 u_long id)
94 { 87 {
95 struct aim_snac_t *cur = NULL; 88 aim_snac_t *cur, **prev;
96 int index; 89 int index;
97 90
98 index = id % FAIM_SNAC_HASH_SIZE; 91 index = id % FAIM_SNAC_HASH_SIZE;
99 92
100 faim_mutex_lock(&sess->snac_hash_locks[index]); 93 faim_mutex_lock(&sess->snac_hash_locks[index]);
101 if (!sess->snac_hash[index]) 94 for (prev = (aim_snac_t **)&sess->snac_hash[index]; (cur = *prev); ) {
102 ; 95 if (cur->id == id) {
103 else if (sess->snac_hash[index]->id == id) { 96 *prev = cur->next;
104 cur = sess->snac_hash[index]; 97 return cur;
105 sess->snac_hash[index] = cur->next; 98 } else
106 } else { 99 prev = &cur->next;
107 cur = sess->snac_hash[index]; 100 }
108 while (cur->next) { 101 faim_mutex_unlock(&sess->snac_hash_locks[index]);
109 if (cur->next->id == id) {
110 struct aim_snac_t *tmp;
111
112 tmp = cur->next;
113 cur->next = cur->next->next;
114 cur = tmp;
115 break;
116 }
117 cur = cur->next;
118 }
119 }
120 faim_mutex_unlock(&sess->snac_hash_locks[index]);
121 102
122 return cur; 103 return cur;
123 } 104 }
124 105
125 /* 106 /*
126 * This is for cleaning up old SNACs that either don't get replies or 107 * 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. 108 * a reply was never received for. Garabage collection. Plain and simple.
128 * 109 *
129 * maxage is the _minimum_ age in seconds to keep SNACs. 110 * maxage is the _minimum_ age in seconds to keep SNACs.
130 * 111 *
131 */ 112 */
132 faim_internal int aim_cleansnacs(struct aim_session_t *sess, 113 faim_internal void aim_cleansnacs(aim_session_t *sess, int maxage)
133 int maxage)
134 { 114 {
135 int i; 115 int i;
136 116
137 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { 117 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
138 struct aim_snac_t *cur, **prev; 118 aim_snac_t *cur, **prev;
139 time_t curtime; 119 time_t curtime;
140 120
141 faim_mutex_lock(&sess->snac_hash_locks[i]); 121 faim_mutex_lock(&sess->snac_hash_locks[i]);
142 if (!sess->snac_hash[i]) { 122 if (!sess->snac_hash[i]) {
143 faim_mutex_unlock(&sess->snac_hash_locks[i]); 123 faim_mutex_unlock(&sess->snac_hash_locks[i]);
144 continue; 124 continue;
145 } 125 }
146 126
147 curtime = time(NULL); /* done here in case we waited for the lock */ 127 curtime = time(NULL); /* done here in case we waited for the lock */
148 128
149 for (prev = &sess->snac_hash[i]; (cur = *prev); ) { 129 for (prev = (aim_snac_t **)&sess->snac_hash[i]; (cur = *prev); ) {
150 if ((curtime - cur->issuetime) > maxage) { 130 if ((curtime - cur->issuetime) > maxage) {
151 131
152 *prev = cur->next; 132 *prev = cur->next;
153 133
154 /* XXX should we have destructors here? */ 134 /* XXX should we have destructors here? */
155 if (cur->data) 135 free(cur->data);
156 free(cur->data); 136 free(cur);
157 free(cur);
158 137
159 } else 138 } else
160 prev = &cur->next; 139 prev = &cur->next;
161 } 140 }
141 faim_mutex_unlock(&sess->snac_hash_locks[i]);
142 }
162 143
163 faim_mutex_unlock(&sess->snac_hash_locks[i]); 144 return;
164 }
165
166 return 0;
167 } 145 }
168 146
169 faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) 147 faim_internal int aim_putsnac(aim_bstream_t *bs, fu16_t family, fu16_t subtype, fu16_t flags, aim_snacid_t snacid)
170 { 148 {
171 int curbyte = 0; 149
172 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); 150 aimbs_put16(bs, family);
173 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); 151 aimbs_put16(bs, subtype);
174 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); 152 aimbs_put16(bs, flags);
175 curbyte += aimutil_put32(buf+curbyte, snacid); 153 aimbs_put32(bs, snacid);
176 return curbyte; 154
155 return 10;
177 } 156 }