Mercurial > pidgin.yaz
annotate libfaim/aim_snac.c @ 1152:201ec77f3a60
[gaim-migrate @ 1162]
icq. whoop de doo
committer: Tailor Script <tailor@pidgin.im>
author | Eric Warmenhoven <eric@warmenhoven.org> |
---|---|
date | Tue, 28 Nov 2000 02:22:42 +0000 |
parents | 595ac7759563 |
children | 1e7625916fa7 |
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 */ |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
21 faim_internal void aim_initsnachash(struct aim_session_t *sess) |
715
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 */ |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
37 faim_internal unsigned long aim_newsnac(struct aim_session_t *sess, |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
38 struct aim_snac_t *newsnac) |
237 | 39 { |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
40 struct aim_snac_t *snac = 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 | |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
46 if (!(snac = calloc(1, sizeof(struct aim_snac_t)))) |
237 | 47 return 0; |
48 memcpy(snac, newsnac, sizeof(struct aim_snac_t)); | |
49 snac->issuetime = time(&snac->issuetime); | |
50 snac->next = NULL; | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
51 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
52 index = snac->id % FAIM_SNAC_HASH_SIZE; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
53 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
54 faim_mutex_lock(&sess->snac_hash_locks[index]); |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
55 snac->next = sess->snac_hash[index]; |
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
56 sess->snac_hash[index] = snac; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
57 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
237 | 58 |
59 return(snac->id); | |
2 | 60 } |
61 | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
62 /* |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
63 * Finds a snac structure with the passed SNAC ID, |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
64 * 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
|
65 * |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
66 * The returned structure must be freed by the caller. |
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 */ |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
69 faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
70 u_long id) |
237 | 71 { |
771
72e556f6b99d
[gaim-migrate @ 781]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
715
diff
changeset
|
72 struct aim_snac_t *cur = NULL; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
73 int index; |
237 | 74 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
75 index = id % FAIM_SNAC_HASH_SIZE; |
237 | 76 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
77 faim_mutex_lock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
78 if (!sess->snac_hash[index]) |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
79 ; |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
80 else if (sess->snac_hash[index]->id == id) { |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
81 cur = sess->snac_hash[index]; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
82 sess->snac_hash[index] = cur->next; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
83 } else { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
84 cur = sess->snac_hash[index]; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
85 while (cur->next) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
86 if (cur->next->id == id) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
87 struct aim_snac_t *tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
88 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
89 tmp = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
90 cur->next = cur->next->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
91 cur = tmp; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
92 break; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
93 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
94 cur = cur->next; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
95 } |
237 | 96 } |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
97 faim_mutex_unlock(&sess->snac_hash_locks[index]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
98 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
99 return cur; |
2 | 100 } |
101 | |
102 /* | |
103 * This is for cleaning up old SNACs that either don't get replies or | |
104 * a reply was never received for. Garabage collection. Plain and simple. | |
105 * | |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
106 * maxage is the _minimum_ age in seconds to keep SNACs. |
2 | 107 * |
108 */ | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
109 faim_internal int aim_cleansnacs(struct aim_session_t *sess, |
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
110 int maxage) |
2 | 111 { |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
112 struct aim_snac_t *cur, *next, *prev = NULL; |
2 | 113 time_t curtime; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
114 int i; |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
115 |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
116 for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
117 faim_mutex_lock(&sess->snac_hash_locks[i]); |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
118 if (!sess->snac_hash[i]) { |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
119 faim_mutex_unlock(&sess->snac_hash_locks[i]); |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
120 continue; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
121 } |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
122 |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
123 curtime = time(NULL); /* done here in case we waited for the lock */ |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
124 |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
125 cur = sess->snac_hash[i]; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
126 while (cur) { |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
127 next = cur->next; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
128 if ((curtime - cur->issuetime) > maxage) { |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
129 if (sess->snac_hash[i] == cur) |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
130 prev = sess->snac_hash[i] = next; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
131 else |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
132 prev->next = next; |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
133 |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
134 /* XXX should we have destructors here? */ |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
135 if (cur->data) |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
136 free(cur->data); |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
137 free(cur); |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
138 |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
139 } else { |
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
140 prev = cur; |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
141 } |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
142 cur = next; |
2 | 143 } |
835
88f8f98de02d
[gaim-migrate @ 845]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
771
diff
changeset
|
144 |
715
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
145 faim_mutex_unlock(&sess->snac_hash_locks[i]); |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
146 } |
58106806ac2b
[gaim-migrate @ 725]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
283
diff
changeset
|
147 |
2 | 148 return 0; |
149 } | |
150 | |
840
595ac7759563
[gaim-migrate @ 850]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
835
diff
changeset
|
151 faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) |
2 | 152 { |
153 int curbyte = 0; | |
237 | 154 curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); |
155 curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); | |
156 curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); | |
157 curbyte += aimutil_put32(buf+curbyte, snacid); | |
2 | 158 return curbyte; |
159 } |