Mercurial > pidgin
annotate libfaim/aim_snac.c @ 727:8b9f0537f473
[gaim-migrate @ 737]
Added a few more little pixmaps to the file menu.
committer: Tailor Script <tailor@pidgin.im>
author | Rob Flynn <gaim@robflynn.com> |
---|---|
date | Sat, 19 Aug 2000 21:21:59 +0000 |
parents | 58106806ac2b |
children | 72e556f6b99d |
rev | line source |
---|---|
2 | 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 | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
8 * response for it has been receieved. |
2 | 9 * |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
10 * cleansnacs() should be called periodically by the client in order |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
11 * to facilitate the aging out of unreplied-to SNACs. This can and does |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
12 * happen, so it should be handled. |
2 | 13 * |
14 */ | |
15 | |
283
0f14e6d8a51b
[gaim-migrate @ 293]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
237
diff
changeset
|
16 #include <faim/aim.h> |
2 | 17 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
18 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
19 * Called from aim_session_init() to initialize the hash. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
20 */ |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
21 void aim_initsnachash(struct aim_session_t *sess) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
22 { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
23 int i; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
24 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
25 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
26 sess->snac_hash[i] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
27 faim_mutex_init(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
28 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
29 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
30 return; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
31 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
32 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
33 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
34 * Clones the passed snac structure and caches it in the |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
35 * list/hash. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
36 */ |
237 | 37 u_long aim_newsnac(struct aim_session_t *sess, |
38 struct aim_snac_t *newsnac) | |
39 { | |
40 struct aim_snac_t *snac = NULL, *cur = NULL; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
41 int index; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
42 |
237 | 43 if (!newsnac) |
44 return 0; | |
45 | |
46 snac = calloc(1, sizeof(struct aim_snac_t)); | |
47 if (!snac) | |
48 return 0; | |
49 memcpy(snac, newsnac, sizeof(struct aim_snac_t)); | |
50 snac->issuetime = time(&snac->issuetime); | |
51 snac->next = NULL; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
52 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
53 index = snac->id % FAIM_SNAC_HASH_SIZE; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
54 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
55 faim_mutex_lock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
56 if (!sess->snac_hash[index]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
57 sess->snac_hash[index] = snac; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
58 else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
59 snac->next = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
60 sess->snac_hash[index] = snac; |
237 | 61 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
62 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
237 | 63 |
64 return(snac->id); | |
2 | 65 } |
66 | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
67 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
68 * Finds a snac structure with the passed SNAC ID, |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
69 * removes it from the list/hash, and returns a pointer to it. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
70 * |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
71 * The returned structure must be freed by the caller. |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
72 * |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
73 */ |
237 | 74 struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, |
75 u_long id) | |
76 { | |
77 struct aim_snac_t *cur; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
78 int index; |
237 | 79 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
80 index = id % FAIM_SNAC_HASH_SIZE; |
237 | 81 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
82 faim_mutex_lock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
83 if (!sess->snac_hash[index]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
84 ; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
85 else if (!sess->snac_hash[index]->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
86 if (sess->snac_hash[index]->id == id) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
87 cur = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
88 sess->snac_hash[index] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
89 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
90 } else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
91 cur = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
92 while (cur->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
93 if (cur->next->id == id) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
94 struct aim_snac_t *tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
95 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
96 tmp = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
97 cur->next = cur->next->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
98 cur = tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
99 break; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
100 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
101 cur = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
102 } |
237 | 103 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
104 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
105 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
106 return cur; |
2 | 107 } |
108 | |
109 /* | |
110 * This is for cleaning up old SNACs that either don't get replies or | |
111 * a reply was never received for. Garabage collection. Plain and simple. | |
112 * | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
113 * maxage is the _minimum_ age in seconds to keep SNACs. |
2 | 114 * |
115 */ | |
237 | 116 int aim_cleansnacs(struct aim_session_t *sess, |
117 int maxage) | |
2 | 118 { |
237 | 119 struct aim_snac_t *cur; |
2 | 120 struct aim_snac_t *remed = NULL; |
121 time_t curtime; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
122 int i; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
123 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
124 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
125 faim_mutex_lock(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
126 if (!sess->snac_hash[i]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
127 ; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
128 else if (!sess->snac_hash[i]->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
129 if ((sess->snac_hash[i]->issuetime + maxage) >= curtime) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
130 remed = sess->snac_hash[i]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
131 if(remed->data) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
132 free(remed->data); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
133 free(remed); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
134 sess->snac_hash[i] = NULL; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
135 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
136 } else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
137 cur = sess->snac_hash[i]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
138 while(cur && cur->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
139 if ((cur->next->issuetime + maxage) >= curtime) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
140 remed = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
141 cur->next = cur->next->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
142 if (remed->data) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
143 free(remed->data); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
144 free(remed); |
2 | 145 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
146 cur = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
147 } |
2 | 148 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
149 faim_mutex_unlock(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
150 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
151 |
2 | 152 return 0; |
153 } | |
154 | |
155 int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) | |
156 { | |
157 int curbyte = 0; | |
237 | 158 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); |
159 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); | |
160 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); | |
161 curbyte += aimutil_put32(buf+curbyte, snacid); | |
2 | 162 return curbyte; |
163 } |