Mercurial > pidgin
annotate src/protocols/oscar/util.c @ 12756:6ef1cdc26b40
[gaim-migrate @ 15103]
Cleanup to STUN code. Fixed endianness. Fixed to work where sizeof(short) != 2 or sizeof(int) != 4. Close the socket when we're done with it. Instead of using a bunch of static variables, pass data around the various callbacks. Don't invoke the specified StunCallback before the initial function has returned. Deal with requerying if the STUN server has changed since last query, or the last query was unsuccessful and 5 minutes have elapsed.
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Sat, 07 Jan 2006 22:10:18 +0000 |
parents | 7d31d61e6438 |
children | bdb8d7beb5a7 |
rev | line source |
---|---|
2086 | 1 /* |
4246 | 2 * A little bit of this |
3 * A little bit of that | |
4 * It started with a kiss | |
5 * Now we're up to bat | |
2086 | 6 */ |
7 | |
2672 | 8 #define FAIM_INTERNAL |
2086 | 9 #include <aim.h> |
10 #include <ctype.h> | |
11 | |
4246 | 12 #ifdef _WIN32 |
13 #include "win32dep.h" | |
14 #endif | |
15 | |
5927 | 16 faim_export int aimutil_putstr(char *dest, const char *src, int len) |
2086 | 17 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
18 memcpy(dest, src, len); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
19 return len; |
2086 | 20 } |
21 | |
22 /* | |
23 * Tokenizing functions. Used to portably replace strtok/sep. | |
24 * -- DMP. | |
25 * | |
26 */ | |
6323 | 27 faim_export int aimutil_tokslen(char *toSearch, int theindex, char dl) |
2086 | 28 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
29 int curCount = 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
30 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
31 char *last; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
32 int toReturn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
33 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
34 last = toSearch; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
35 next = strchr(toSearch, dl); |
2086 | 36 |
6323 | 37 while(curCount < theindex && next != NULL) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
38 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
39 last = next + 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
40 next = strchr(last, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
41 } |
2086 | 42 |
6323 | 43 if ((curCount < theindex) || (next == NULL)) |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
44 toReturn = strlen(toSearch) - (curCount - 1); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
45 else |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
46 toReturn = next - toSearch - (curCount - 1); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
47 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
48 return toReturn; |
2086 | 49 } |
50 | |
51 faim_export int aimutil_itemcnt(char *toSearch, char dl) | |
52 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
53 int curCount; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
54 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
55 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
56 curCount = 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
57 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
58 next = strchr(toSearch, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
59 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
60 while(next != NULL) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
61 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
62 next = strchr(next + 1, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
63 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
64 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
65 return curCount; |
2086 | 66 } |
67 | |
6323 | 68 faim_export char *aimutil_itemindex(char *toSearch, int theindex, char dl) |
2086 | 69 { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
70 int curCount; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
71 char *next; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
72 char *last; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
73 char *toReturn; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
74 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
75 curCount = 0; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
76 |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
77 last = toSearch; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
78 next = strchr(toSearch, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
79 |
6323 | 80 while (curCount < theindex && next != NULL) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
81 curCount++; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
82 last = next + 1; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
83 next = strchr(last, dl); |
2086 | 84 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
85 |
6323 | 86 if (curCount < theindex) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
87 toReturn = malloc(sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
88 *toReturn = '\0'; |
2086 | 89 } |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
90 next = strchr(last, dl); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
91 |
6323 | 92 if (curCount < theindex) { |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
93 toReturn = malloc(sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
94 *toReturn = '\0'; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
95 } else { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
96 if (next == NULL) { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
97 toReturn = malloc((strlen(last) + 1) * sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
98 strcpy(toReturn, last); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
99 } else { |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
100 toReturn = malloc((next - last + 1) * sizeof(char)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
101 memcpy(toReturn, last, (next - last)); |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
102 toReturn[next - last] = '\0'; |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
103 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
104 } |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
105 return toReturn; |
2086 | 106 } |
107 | |
4617 | 108 /** |
109 * Calculate the checksum of a given icon. | |
110 * | |
111 */ | |
112 faim_export fu16_t aimutil_iconsum(const fu8_t *buf, int buflen) | |
113 { | |
114 fu32_t sum; | |
115 int i; | |
116 | |
117 for (i=0, sum=0; i+1<buflen; i+=2) | |
118 sum += (buf[i+1] << 8) + buf[i]; | |
119 if (i < buflen) | |
120 sum += buf[i]; | |
121 sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff); | |
122 | |
123 return sum; | |
124 } | |
125 | |
8092 | 126 /** |
8972 | 127 * Check if the given screen name is a valid AIM screen name. |
128 * Example: BobDole | |
9681 | 129 * Example: Henry_Ford@mac.com |
8972 | 130 * |
131 * @return 1 if the screen name is valid, 0 if not. | |
132 */ | |
133 static int aim_snvalid_aim(const char *sn) | |
134 { | |
135 int i; | |
136 | |
137 for (i = 0; sn[i] != '\0'; i++) { | |
9681 | 138 if (!isalnum(sn[i]) && (sn[i] != ' ') && |
139 (sn[i] != '@') && (sn[i] != '.') && | |
140 (sn[i] != '_') && (sn[i] != '-')) | |
8972 | 141 return 0; |
142 } | |
143 | |
144 return 1; | |
145 } | |
146 | |
147 /** | |
148 * Check if the given screen name is a valid ICQ screen name. | |
149 * Example: 1234567 | |
150 * | |
151 * @return 1 if the screen name is valid, 0 if not. | |
152 */ | |
153 static int aim_snvalid_icq(const char *sn) | |
154 { | |
155 int i; | |
156 | |
157 for (i = 0; sn[i] != '\0'; i++) { | |
158 if (!isdigit(sn[i])) | |
159 return 0; | |
160 } | |
161 | |
162 return 1; | |
163 } | |
164 | |
165 /** | |
166 * Check if the given screen name is a valid SMS screen name. | |
167 * Example: +19195551234 | |
168 * | |
169 * @return 1 if the screen name is valid, 0 if not. | |
170 */ | |
171 static int aim_snvalid_sms(const char *sn) | |
172 { | |
173 int i; | |
174 | |
175 if (sn[0] != '+') | |
176 return 0; | |
177 | |
178 for (i = 1; sn[i] != '\0'; i++) { | |
179 if (!isdigit(sn[i])) | |
180 return 0; | |
181 } | |
182 | |
183 return 1; | |
184 } | |
185 | |
186 /** | |
187 * Check if the given screen name is a valid oscar screen name. | |
8092 | 188 * |
189 * @return 1 if the screen name is valid, 0 if not. | |
190 */ | |
191 faim_export int aim_snvalid(const char *sn) | |
192 { | |
8972 | 193 if ((sn == NULL) || (*sn == '\0')) |
8092 | 194 return 0; |
195 | |
8972 | 196 if (isalpha(sn[0])) |
197 return aim_snvalid_aim(sn); | |
198 else if (isdigit(sn[0])) | |
199 return aim_snvalid_icq(sn); | |
200 else if (sn[0] == '+') | |
201 return aim_snvalid_sms(sn); | |
8092 | 202 |
8972 | 203 return 0; |
8092 | 204 } |
205 | |
9975 | 206 /** |
207 * Determine if a given screen name is an ICQ screen name | |
208 * (i.e. it begins with a number). | |
209 * | |
210 * @sn A valid AIM or ICQ screen name. | |
211 * @return 1 if the screen name is an ICQ screen name. Otherwise 0 | |
212 * is returned. | |
213 */ | |
214 faim_export int aim_sn_is_icq(const char *sn) | |
215 { | |
216 if (isalpha(sn[0])) | |
217 return 0; | |
218 return 1; | |
219 } | |
220 | |
2086 | 221 /* |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
222 * int snlen(const char *) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
223 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
224 * This takes a screen name and returns its length without |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
225 * spaces. If there are no spaces in the SN, then the |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
226 * return is equal to that of strlen(). |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
227 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
228 */ |
2086 | 229 faim_export int aim_snlen(const char *sn) |
230 { | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
231 int i = 0; |
2086 | 232 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
233 if (!sn) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
234 return 0; |
2086 | 235 |
5556 | 236 while (*sn != '\0') { |
237 if (*sn != ' ') | |
238 i++; | |
239 sn++; | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
240 } |
2086 | 241 |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
242 return i; |
2086 | 243 } |
244 | |
245 /* | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
246 * int sncmp(const char *, const char *) |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
247 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
248 * This takes two screen names and compares them using the rules |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
249 * on screen names for AIM/AOL. Mainly, this means case and space |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
250 * insensitivity (all case differences and spacing differences are |
5628 | 251 * ignored, with the exception that screen names can not start with |
252 * a space). | |
2246
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
253 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
254 * Return: 0 if equal |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
255 * non-0 if different |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
256 * |
933346315b9b
[gaim-migrate @ 2256]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
2086
diff
changeset
|
257 */ |
2086 | 258 faim_export int aim_sncmp(const char *sn1, const char *sn2) |
259 { | |
260 | |
10116 | 261 if ((sn1 == NULL) || (sn2 == NULL)) |
262 return -1; | |
263 | |
5928 | 264 do { |
5556 | 265 while (*sn2 == ' ') |
5724 | 266 sn2++; |
5556 | 267 while (*sn1 == ' ') |
5724 | 268 sn1++; |
5928 | 269 if (toupper(*sn1) != toupper(*sn2)) |
270 return 1; | |
271 } while ((*sn1 != '\0') && sn1++ && sn2++); | |
2086 | 272 |
5928 | 273 return 0; |
2086 | 274 } |