Merge lp:~davewalker/ubuntu/lucid/asterisk/lp_705014 into lp:ubuntu/lucid/asterisk
- Lucid (10.04)
- lp_705014
- Merge into lucid
Status: | Superseded |
---|---|
Proposed branch: | lp:~davewalker/ubuntu/lucid/asterisk/lp_705014 |
Merge into: | lp:ubuntu/lucid/asterisk |
Diff against target: |
4314 lines (+4076/-31) 18 files modified
.pc/.quilt_patches (+1/-0) .pc/.quilt_series (+1/-0) .pc/AST-2011-001-1.6.2/main/utils.c (+1828/-0) .pc/applied-patches (+3/-0) .pc/dnsmgr-A-SRV-handling/include/asterisk/dnsmgr.h (+105/-0) .pc/dnsmgr-A-SRV-handling/main/acl.c (+541/-0) .pc/dnsmgr-A-SRV-handling/main/dnsmgr.c (+439/-0) .pc/unattended_fix/channels/chan_local.c (+885/-0) channels/chan_local.c (+4/-3) debian/changelog (+27/-0) debian/patches/AST-2011-001-1.6.2 (+52/-0) debian/patches/dnsmgr-A-SRV-handling (+132/-0) debian/patches/series (+3/-0) debian/patches/unattended_fix (+18/-0) include/asterisk/dnsmgr.h (+4/-3) main/acl.c (+1/-0) main/dnsmgr.c (+18/-10) main/utils.c (+14/-15) |
To merge this branch: | bzr merge lp:~davewalker/ubuntu/lucid/asterisk/lp_705014 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jamie Strandboge | Disapprove | ||
Review via email: mp+46997@code.launchpad.net |
This proposal has been superseded by a proposal from 2011-01-20.
Commit message
Description of the change
Waiting on 1:1.6.2.
Jamie Strandboge (jdstrand) : | # |
Unmerged revisions
- 59. By Dave Walker
-
* SECURITY UPDATE: Stack buffer overflow in SIP channel driver. (LP: #705014)
- debian/patches/ AST-2011- 001-1.6. 2: The size of the output buffer passed
to the ast_uri_encode function is now properly respected in main/utils.c.
Patch courtesy of upstream.
- CVE-2011-0495 - 58. By Lionel Porcheron
-
debian/
patches/ unattended_ fix: Fix attended transfer call in 1.2.6.5
Patch based on Asterisk project's upstream patch (between 1.2.6.5 and
1.2.6.6 where issue is declared to be fixed see issue 16816 on Asterisk
bug tracker). (LP: #686625) - 57. By Dave Walker
-
debian/
patches/ dnsmgr- A-SRV-handling: Resolve handling of A and SRV
record changes and problem with multiple A/SRV records returned.
Patch based on Asterisk project's upstream patch. (LP: #605358)
Preview Diff
1 | === added file '.pc/.quilt_patches' | |||
2 | --- .pc/.quilt_patches 1970-01-01 00:00:00 +0000 | |||
3 | +++ .pc/.quilt_patches 2011-01-20 23:43:55 +0000 | |||
4 | @@ -0,0 +1,1 @@ | |||
5 | 1 | debian/patches | ||
6 | 0 | 2 | ||
7 | === added file '.pc/.quilt_series' | |||
8 | --- .pc/.quilt_series 1970-01-01 00:00:00 +0000 | |||
9 | +++ .pc/.quilt_series 2011-01-20 23:43:55 +0000 | |||
10 | @@ -0,0 +1,1 @@ | |||
11 | 1 | series | ||
12 | 0 | 2 | ||
13 | === added directory '.pc/AST-2011-001-1.6.2' | |||
14 | === added file '.pc/AST-2011-001-1.6.2/.timestamp' | |||
15 | === added directory '.pc/AST-2011-001-1.6.2/main' | |||
16 | === added file '.pc/AST-2011-001-1.6.2/main/utils.c' | |||
17 | --- .pc/AST-2011-001-1.6.2/main/utils.c 1970-01-01 00:00:00 +0000 | |||
18 | +++ .pc/AST-2011-001-1.6.2/main/utils.c 2011-01-20 23:43:55 +0000 | |||
19 | @@ -0,0 +1,1828 @@ | |||
20 | 1 | /* | ||
21 | 2 | * Asterisk -- An open source telephony toolkit. | ||
22 | 3 | * | ||
23 | 4 | * Copyright (C) 1999 - 2006, Digium, Inc. | ||
24 | 5 | * | ||
25 | 6 | * See http://www.asterisk.org for more information about | ||
26 | 7 | * the Asterisk project. Please do not directly contact | ||
27 | 8 | * any of the maintainers of this project for assistance; | ||
28 | 9 | * the project provides a web site, mailing lists and IRC | ||
29 | 10 | * channels for your use. | ||
30 | 11 | * | ||
31 | 12 | * This program is free software, distributed under the terms of | ||
32 | 13 | * the GNU General Public License Version 2. See the LICENSE file | ||
33 | 14 | * at the top of the source tree. | ||
34 | 15 | */ | ||
35 | 16 | |||
36 | 17 | /*! \file | ||
37 | 18 | * | ||
38 | 19 | * \brief Utility functions | ||
39 | 20 | * | ||
40 | 21 | * \note These are important for portability and security, | ||
41 | 22 | * so please use them in favour of other routines. | ||
42 | 23 | * Please consult the CODING GUIDELINES for more information. | ||
43 | 24 | */ | ||
44 | 25 | |||
45 | 26 | #include "asterisk.h" | ||
46 | 27 | |||
47 | 28 | ASTERISK_FILE_VERSION(__FILE__, "$Revision: 237743 $") | ||
48 | 29 | |||
49 | 30 | #include <ctype.h> | ||
50 | 31 | #include <sys/stat.h> | ||
51 | 32 | |||
52 | 33 | #ifdef HAVE_DEV_URANDOM | ||
53 | 34 | #include <fcntl.h> | ||
54 | 35 | #endif | ||
55 | 36 | |||
56 | 37 | #include "asterisk/network.h" | ||
57 | 38 | |||
58 | 39 | #define AST_API_MODULE /* ensure that inlinable API functions will be built in lock.h if required */ | ||
59 | 40 | #include "asterisk/lock.h" | ||
60 | 41 | #include "asterisk/io.h" | ||
61 | 42 | #include "asterisk/md5.h" | ||
62 | 43 | #include "asterisk/sha1.h" | ||
63 | 44 | #include "asterisk/cli.h" | ||
64 | 45 | #include "asterisk/linkedlists.h" | ||
65 | 46 | |||
66 | 47 | #define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ | ||
67 | 48 | #include "asterisk/strings.h" | ||
68 | 49 | |||
69 | 50 | #define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ | ||
70 | 51 | #include "asterisk/time.h" | ||
71 | 52 | |||
72 | 53 | #define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ | ||
73 | 54 | #include "asterisk/stringfields.h" | ||
74 | 55 | |||
75 | 56 | #define AST_API_MODULE /* ensure that inlinable API functions will be built in this module if required */ | ||
76 | 57 | #include "asterisk/utils.h" | ||
77 | 58 | |||
78 | 59 | #define AST_API_MODULE | ||
79 | 60 | #include "asterisk/threadstorage.h" | ||
80 | 61 | |||
81 | 62 | #define AST_API_MODULE | ||
82 | 63 | #include "asterisk/config.h" | ||
83 | 64 | |||
84 | 65 | static char base64[64]; | ||
85 | 66 | static char b2a[256]; | ||
86 | 67 | |||
87 | 68 | AST_THREADSTORAGE(inet_ntoa_buf); | ||
88 | 69 | |||
89 | 70 | #if !defined(HAVE_GETHOSTBYNAME_R_5) && !defined(HAVE_GETHOSTBYNAME_R_6) | ||
90 | 71 | |||
91 | 72 | #define ERANGE 34 /*!< duh? ERANGE value copied from web... */ | ||
92 | 73 | #undef gethostbyname | ||
93 | 74 | |||
94 | 75 | AST_MUTEX_DEFINE_STATIC(__mutex); | ||
95 | 76 | |||
96 | 77 | /*! \brief Reentrant replacement for gethostbyname for BSD-based systems. | ||
97 | 78 | \note This | ||
98 | 79 | routine is derived from code originally written and placed in the public | ||
99 | 80 | domain by Enzo Michelangeli <em@em.no-ip.com> */ | ||
100 | 81 | |||
101 | 82 | static int gethostbyname_r (const char *name, struct hostent *ret, char *buf, | ||
102 | 83 | size_t buflen, struct hostent **result, | ||
103 | 84 | int *h_errnop) | ||
104 | 85 | { | ||
105 | 86 | int hsave; | ||
106 | 87 | struct hostent *ph; | ||
107 | 88 | ast_mutex_lock(&__mutex); /* begin critical area */ | ||
108 | 89 | hsave = h_errno; | ||
109 | 90 | |||
110 | 91 | ph = gethostbyname(name); | ||
111 | 92 | *h_errnop = h_errno; /* copy h_errno to *h_herrnop */ | ||
112 | 93 | if (ph == NULL) { | ||
113 | 94 | *result = NULL; | ||
114 | 95 | } else { | ||
115 | 96 | char **p, **q; | ||
116 | 97 | char *pbuf; | ||
117 | 98 | int nbytes = 0; | ||
118 | 99 | int naddr = 0, naliases = 0; | ||
119 | 100 | /* determine if we have enough space in buf */ | ||
120 | 101 | |||
121 | 102 | /* count how many addresses */ | ||
122 | 103 | for (p = ph->h_addr_list; *p != 0; p++) { | ||
123 | 104 | nbytes += ph->h_length; /* addresses */ | ||
124 | 105 | nbytes += sizeof(*p); /* pointers */ | ||
125 | 106 | naddr++; | ||
126 | 107 | } | ||
127 | 108 | nbytes += sizeof(*p); /* one more for the terminating NULL */ | ||
128 | 109 | |||
129 | 110 | /* count how many aliases, and total length of strings */ | ||
130 | 111 | for (p = ph->h_aliases; *p != 0; p++) { | ||
131 | 112 | nbytes += (strlen(*p)+1); /* aliases */ | ||
132 | 113 | nbytes += sizeof(*p); /* pointers */ | ||
133 | 114 | naliases++; | ||
134 | 115 | } | ||
135 | 116 | nbytes += sizeof(*p); /* one more for the terminating NULL */ | ||
136 | 117 | |||
137 | 118 | /* here nbytes is the number of bytes required in buffer */ | ||
138 | 119 | /* as a terminator must be there, the minimum value is ph->h_length */ | ||
139 | 120 | if (nbytes > buflen) { | ||
140 | 121 | *result = NULL; | ||
141 | 122 | ast_mutex_unlock(&__mutex); /* end critical area */ | ||
142 | 123 | return ERANGE; /* not enough space in buf!! */ | ||
143 | 124 | } | ||
144 | 125 | |||
145 | 126 | /* There is enough space. Now we need to do a deep copy! */ | ||
146 | 127 | /* Allocation in buffer: | ||
147 | 128 | from [0] to [(naddr-1) * sizeof(*p)]: | ||
148 | 129 | pointers to addresses | ||
149 | 130 | at [naddr * sizeof(*p)]: | ||
150 | 131 | NULL | ||
151 | 132 | from [(naddr+1) * sizeof(*p)] to [(naddr+naliases) * sizeof(*p)] : | ||
152 | 133 | pointers to aliases | ||
153 | 134 | at [(naddr+naliases+1) * sizeof(*p)]: | ||
154 | 135 | NULL | ||
155 | 136 | then naddr addresses (fixed length), and naliases aliases (asciiz). | ||
156 | 137 | */ | ||
157 | 138 | |||
158 | 139 | *ret = *ph; /* copy whole structure (not its address!) */ | ||
159 | 140 | |||
160 | 141 | /* copy addresses */ | ||
161 | 142 | q = (char **)buf; /* pointer to pointers area (type: char **) */ | ||
162 | 143 | ret->h_addr_list = q; /* update pointer to address list */ | ||
163 | 144 | pbuf = buf + ((naddr + naliases + 2) * sizeof(*p)); /* skip that area */ | ||
164 | 145 | for (p = ph->h_addr_list; *p != 0; p++) { | ||
165 | 146 | memcpy(pbuf, *p, ph->h_length); /* copy address bytes */ | ||
166 | 147 | *q++ = pbuf; /* the pointer is the one inside buf... */ | ||
167 | 148 | pbuf += ph->h_length; /* advance pbuf */ | ||
168 | 149 | } | ||
169 | 150 | *q++ = NULL; /* address list terminator */ | ||
170 | 151 | |||
171 | 152 | /* copy aliases */ | ||
172 | 153 | ret->h_aliases = q; /* update pointer to aliases list */ | ||
173 | 154 | for (p = ph->h_aliases; *p != 0; p++) { | ||
174 | 155 | strcpy(pbuf, *p); /* copy alias strings */ | ||
175 | 156 | *q++ = pbuf; /* the pointer is the one inside buf... */ | ||
176 | 157 | pbuf += strlen(*p); /* advance pbuf */ | ||
177 | 158 | *pbuf++ = 0; /* string terminator */ | ||
178 | 159 | } | ||
179 | 160 | *q++ = NULL; /* terminator */ | ||
180 | 161 | |||
181 | 162 | strcpy(pbuf, ph->h_name); /* copy alias strings */ | ||
182 | 163 | ret->h_name = pbuf; | ||
183 | 164 | pbuf += strlen(ph->h_name); /* advance pbuf */ | ||
184 | 165 | *pbuf++ = 0; /* string terminator */ | ||
185 | 166 | |||
186 | 167 | *result = ret; /* and let *result point to structure */ | ||
187 | 168 | |||
188 | 169 | } | ||
189 | 170 | h_errno = hsave; /* restore h_errno */ | ||
190 | 171 | ast_mutex_unlock(&__mutex); /* end critical area */ | ||
191 | 172 | |||
192 | 173 | return (*result == NULL); /* return 0 on success, non-zero on error */ | ||
193 | 174 | } | ||
194 | 175 | |||
195 | 176 | |||
196 | 177 | #endif | ||
197 | 178 | |||
198 | 179 | /*! \brief Re-entrant (thread safe) version of gethostbyname that replaces the | ||
199 | 180 | standard gethostbyname (which is not thread safe) | ||
200 | 181 | */ | ||
201 | 182 | struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp) | ||
202 | 183 | { | ||
203 | 184 | int res; | ||
204 | 185 | int herrno; | ||
205 | 186 | int dots = 0; | ||
206 | 187 | const char *s; | ||
207 | 188 | struct hostent *result = NULL; | ||
208 | 189 | /* Although it is perfectly legitimate to lookup a pure integer, for | ||
209 | 190 | the sake of the sanity of people who like to name their peers as | ||
210 | 191 | integers, we break with tradition and refuse to look up a | ||
211 | 192 | pure integer */ | ||
212 | 193 | s = host; | ||
213 | 194 | res = 0; | ||
214 | 195 | while (s && *s) { | ||
215 | 196 | if (*s == '.') | ||
216 | 197 | dots++; | ||
217 | 198 | else if (!isdigit(*s)) | ||
218 | 199 | break; | ||
219 | 200 | s++; | ||
220 | 201 | } | ||
221 | 202 | if (!s || !*s) { | ||
222 | 203 | /* Forge a reply for IP's to avoid octal IP's being interpreted as octal */ | ||
223 | 204 | if (dots != 3) | ||
224 | 205 | return NULL; | ||
225 | 206 | memset(hp, 0, sizeof(struct ast_hostent)); | ||
226 | 207 | hp->hp.h_addrtype = AF_INET; | ||
227 | 208 | hp->hp.h_addr_list = (void *) hp->buf; | ||
228 | 209 | hp->hp.h_addr = hp->buf + sizeof(void *); | ||
229 | 210 | if (inet_pton(AF_INET, host, hp->hp.h_addr) > 0) | ||
230 | 211 | return &hp->hp; | ||
231 | 212 | return NULL; | ||
232 | 213 | |||
233 | 214 | } | ||
234 | 215 | #ifdef HAVE_GETHOSTBYNAME_R_5 | ||
235 | 216 | result = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &herrno); | ||
236 | 217 | |||
237 | 218 | if (!result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0]) | ||
238 | 219 | return NULL; | ||
239 | 220 | #else | ||
240 | 221 | res = gethostbyname_r(host, &hp->hp, hp->buf, sizeof(hp->buf), &result, &herrno); | ||
241 | 222 | |||
242 | 223 | if (res || !result || !hp->hp.h_addr_list || !hp->hp.h_addr_list[0]) | ||
243 | 224 | return NULL; | ||
244 | 225 | #endif | ||
245 | 226 | return &hp->hp; | ||
246 | 227 | } | ||
247 | 228 | |||
248 | 229 | /*! \brief Produce 32 char MD5 hash of value. */ | ||
249 | 230 | void ast_md5_hash(char *output, char *input) | ||
250 | 231 | { | ||
251 | 232 | struct MD5Context md5; | ||
252 | 233 | unsigned char digest[16]; | ||
253 | 234 | char *ptr; | ||
254 | 235 | int x; | ||
255 | 236 | |||
256 | 237 | MD5Init(&md5); | ||
257 | 238 | MD5Update(&md5, (unsigned char *)input, strlen(input)); | ||
258 | 239 | MD5Final(digest, &md5); | ||
259 | 240 | ptr = output; | ||
260 | 241 | for (x = 0; x < 16; x++) | ||
261 | 242 | ptr += sprintf(ptr, "%2.2x", digest[x]); | ||
262 | 243 | } | ||
263 | 244 | |||
264 | 245 | /*! \brief Produce 40 char SHA1 hash of value. */ | ||
265 | 246 | void ast_sha1_hash(char *output, char *input) | ||
266 | 247 | { | ||
267 | 248 | struct SHA1Context sha; | ||
268 | 249 | char *ptr; | ||
269 | 250 | int x; | ||
270 | 251 | uint8_t Message_Digest[20]; | ||
271 | 252 | |||
272 | 253 | SHA1Reset(&sha); | ||
273 | 254 | |||
274 | 255 | SHA1Input(&sha, (const unsigned char *) input, strlen(input)); | ||
275 | 256 | |||
276 | 257 | SHA1Result(&sha, Message_Digest); | ||
277 | 258 | ptr = output; | ||
278 | 259 | for (x = 0; x < 20; x++) | ||
279 | 260 | ptr += sprintf(ptr, "%2.2x", Message_Digest[x]); | ||
280 | 261 | } | ||
281 | 262 | |||
282 | 263 | /*! \brief decode BASE64 encoded text */ | ||
283 | 264 | int ast_base64decode(unsigned char *dst, const char *src, int max) | ||
284 | 265 | { | ||
285 | 266 | int cnt = 0; | ||
286 | 267 | unsigned int byte = 0; | ||
287 | 268 | unsigned int bits = 0; | ||
288 | 269 | int incnt = 0; | ||
289 | 270 | while(*src && *src != '=' && (cnt < max)) { | ||
290 | 271 | /* Shift in 6 bits of input */ | ||
291 | 272 | byte <<= 6; | ||
292 | 273 | byte |= (b2a[(int)(*src)]) & 0x3f; | ||
293 | 274 | bits += 6; | ||
294 | 275 | src++; | ||
295 | 276 | incnt++; | ||
296 | 277 | /* If we have at least 8 bits left over, take that character | ||
297 | 278 | off the top */ | ||
298 | 279 | if (bits >= 8) { | ||
299 | 280 | bits -= 8; | ||
300 | 281 | *dst = (byte >> bits) & 0xff; | ||
301 | 282 | dst++; | ||
302 | 283 | cnt++; | ||
303 | 284 | } | ||
304 | 285 | } | ||
305 | 286 | /* Dont worry about left over bits, they're extra anyway */ | ||
306 | 287 | return cnt; | ||
307 | 288 | } | ||
308 | 289 | |||
309 | 290 | /*! \brief encode text to BASE64 coding */ | ||
310 | 291 | int ast_base64encode_full(char *dst, const unsigned char *src, int srclen, int max, int linebreaks) | ||
311 | 292 | { | ||
312 | 293 | int cnt = 0; | ||
313 | 294 | int col = 0; | ||
314 | 295 | unsigned int byte = 0; | ||
315 | 296 | int bits = 0; | ||
316 | 297 | int cntin = 0; | ||
317 | 298 | /* Reserve space for null byte at end of string */ | ||
318 | 299 | max--; | ||
319 | 300 | while ((cntin < srclen) && (cnt < max)) { | ||
320 | 301 | byte <<= 8; | ||
321 | 302 | byte |= *(src++); | ||
322 | 303 | bits += 8; | ||
323 | 304 | cntin++; | ||
324 | 305 | if ((bits == 24) && (cnt + 4 <= max)) { | ||
325 | 306 | *dst++ = base64[(byte >> 18) & 0x3f]; | ||
326 | 307 | *dst++ = base64[(byte >> 12) & 0x3f]; | ||
327 | 308 | *dst++ = base64[(byte >> 6) & 0x3f]; | ||
328 | 309 | *dst++ = base64[byte & 0x3f]; | ||
329 | 310 | cnt += 4; | ||
330 | 311 | col += 4; | ||
331 | 312 | bits = 0; | ||
332 | 313 | byte = 0; | ||
333 | 314 | } | ||
334 | 315 | if (linebreaks && (cnt < max) && (col == 64)) { | ||
335 | 316 | *dst++ = '\n'; | ||
336 | 317 | cnt++; | ||
337 | 318 | col = 0; | ||
338 | 319 | } | ||
339 | 320 | } | ||
340 | 321 | if (bits && (cnt + 4 <= max)) { | ||
341 | 322 | /* Add one last character for the remaining bits, | ||
342 | 323 | padding the rest with 0 */ | ||
343 | 324 | byte <<= 24 - bits; | ||
344 | 325 | *dst++ = base64[(byte >> 18) & 0x3f]; | ||
345 | 326 | *dst++ = base64[(byte >> 12) & 0x3f]; | ||
346 | 327 | if (bits == 16) | ||
347 | 328 | *dst++ = base64[(byte >> 6) & 0x3f]; | ||
348 | 329 | else | ||
349 | 330 | *dst++ = '='; | ||
350 | 331 | *dst++ = '='; | ||
351 | 332 | cnt += 4; | ||
352 | 333 | } | ||
353 | 334 | if (linebreaks && (cnt < max)) { | ||
354 | 335 | *dst++ = '\n'; | ||
355 | 336 | cnt++; | ||
356 | 337 | } | ||
357 | 338 | *dst = '\0'; | ||
358 | 339 | return cnt; | ||
359 | 340 | } | ||
360 | 341 | |||
361 | 342 | int ast_base64encode(char *dst, const unsigned char *src, int srclen, int max) | ||
362 | 343 | { | ||
363 | 344 | return ast_base64encode_full(dst, src, srclen, max, 0); | ||
364 | 345 | } | ||
365 | 346 | |||
366 | 347 | static void base64_init(void) | ||
367 | 348 | { | ||
368 | 349 | int x; | ||
369 | 350 | memset(b2a, -1, sizeof(b2a)); | ||
370 | 351 | /* Initialize base-64 Conversion table */ | ||
371 | 352 | for (x = 0; x < 26; x++) { | ||
372 | 353 | /* A-Z */ | ||
373 | 354 | base64[x] = 'A' + x; | ||
374 | 355 | b2a['A' + x] = x; | ||
375 | 356 | /* a-z */ | ||
376 | 357 | base64[x + 26] = 'a' + x; | ||
377 | 358 | b2a['a' + x] = x + 26; | ||
378 | 359 | /* 0-9 */ | ||
379 | 360 | if (x < 10) { | ||
380 | 361 | base64[x + 52] = '0' + x; | ||
381 | 362 | b2a['0' + x] = x + 52; | ||
382 | 363 | } | ||
383 | 364 | } | ||
384 | 365 | base64[62] = '+'; | ||
385 | 366 | base64[63] = '/'; | ||
386 | 367 | b2a[(int)'+'] = 62; | ||
387 | 368 | b2a[(int)'/'] = 63; | ||
388 | 369 | } | ||
389 | 370 | |||
390 | 371 | /*! \brief ast_uri_encode: Turn text string to URI-encoded %XX version | ||
391 | 372 | \note At this point, we're converting from ISO-8859-x (8-bit), not UTF8 | ||
392 | 373 | as in the SIP protocol spec | ||
393 | 374 | If doreserved == 1 we will convert reserved characters also. | ||
394 | 375 | RFC 2396, section 2.4 | ||
395 | 376 | outbuf needs to have more memory allocated than the instring | ||
396 | 377 | to have room for the expansion. Every char that is converted | ||
397 | 378 | is replaced by three ASCII characters. | ||
398 | 379 | |||
399 | 380 | Note: The doreserved option is needed for replaces header in | ||
400 | 381 | SIP transfers. | ||
401 | 382 | */ | ||
402 | 383 | char *ast_uri_encode(const char *string, char *outbuf, int buflen, int doreserved) | ||
403 | 384 | { | ||
404 | 385 | char *reserved = ";/?:@&=+$,# "; /* Reserved chars */ | ||
405 | 386 | |||
406 | 387 | const char *ptr = string; /* Start with the string */ | ||
407 | 388 | char *out = NULL; | ||
408 | 389 | char *buf = NULL; | ||
409 | 390 | |||
410 | 391 | ast_copy_string(outbuf, string, buflen); | ||
411 | 392 | |||
412 | 393 | /* If there's no characters to convert, just go through and don't do anything */ | ||
413 | 394 | while (*ptr) { | ||
414 | 395 | if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) { | ||
415 | 396 | /* Oops, we need to start working here */ | ||
416 | 397 | if (!buf) { | ||
417 | 398 | buf = outbuf; | ||
418 | 399 | out = buf + (ptr - string) ; /* Set output ptr */ | ||
419 | 400 | } | ||
420 | 401 | out += sprintf(out, "%%%02x", (unsigned char) *ptr); | ||
421 | 402 | } else if (buf) { | ||
422 | 403 | *out = *ptr; /* Continue copying the string */ | ||
423 | 404 | out++; | ||
424 | 405 | } | ||
425 | 406 | ptr++; | ||
426 | 407 | } | ||
427 | 408 | if (buf) | ||
428 | 409 | *out = '\0'; | ||
429 | 410 | return outbuf; | ||
430 | 411 | } | ||
431 | 412 | |||
432 | 413 | /*! \brief ast_uri_decode: Decode SIP URI, URN, URL (overwrite the string) */ | ||
433 | 414 | void ast_uri_decode(char *s) | ||
434 | 415 | { | ||
435 | 416 | char *o; | ||
436 | 417 | unsigned int tmp; | ||
437 | 418 | |||
438 | 419 | for (o = s; *s; s++, o++) { | ||
439 | 420 | if (*s == '%' && strlen(s) > 2 && sscanf(s + 1, "%2x", &tmp) == 1) { | ||
440 | 421 | /* have '%', two chars and correct parsing */ | ||
441 | 422 | *o = tmp; | ||
442 | 423 | s += 2; /* Will be incremented once more when we break out */ | ||
443 | 424 | } else /* all other cases, just copy */ | ||
444 | 425 | *o = *s; | ||
445 | 426 | } | ||
446 | 427 | *o = '\0'; | ||
447 | 428 | } | ||
448 | 429 | |||
449 | 430 | /*! \brief ast_inet_ntoa: Recursive thread safe replacement of inet_ntoa */ | ||
450 | 431 | const char *ast_inet_ntoa(struct in_addr ia) | ||
451 | 432 | { | ||
452 | 433 | char *buf; | ||
453 | 434 | |||
454 | 435 | if (!(buf = ast_threadstorage_get(&inet_ntoa_buf, INET_ADDRSTRLEN))) | ||
455 | 436 | return ""; | ||
456 | 437 | |||
457 | 438 | return inet_ntop(AF_INET, &ia, buf, INET_ADDRSTRLEN); | ||
458 | 439 | } | ||
459 | 440 | |||
460 | 441 | #ifdef HAVE_DEV_URANDOM | ||
461 | 442 | static int dev_urandom_fd; | ||
462 | 443 | #endif | ||
463 | 444 | |||
464 | 445 | #ifndef __linux__ | ||
465 | 446 | #undef pthread_create /* For ast_pthread_create function only */ | ||
466 | 447 | #endif /* !__linux__ */ | ||
467 | 448 | |||
468 | 449 | #if !defined(LOW_MEMORY) | ||
469 | 450 | |||
470 | 451 | #ifdef DEBUG_THREADS | ||
471 | 452 | |||
472 | 453 | /*! \brief A reasonable maximum number of locks a thread would be holding ... */ | ||
473 | 454 | #define AST_MAX_LOCKS 64 | ||
474 | 455 | |||
475 | 456 | /* Allow direct use of pthread_mutex_t and friends */ | ||
476 | 457 | #undef pthread_mutex_t | ||
477 | 458 | #undef pthread_mutex_lock | ||
478 | 459 | #undef pthread_mutex_unlock | ||
479 | 460 | #undef pthread_mutex_init | ||
480 | 461 | #undef pthread_mutex_destroy | ||
481 | 462 | |||
482 | 463 | /*! | ||
483 | 464 | * \brief Keep track of which locks a thread holds | ||
484 | 465 | * | ||
485 | 466 | * There is an instance of this struct for every active thread | ||
486 | 467 | */ | ||
487 | 468 | struct thr_lock_info { | ||
488 | 469 | /*! The thread's ID */ | ||
489 | 470 | pthread_t thread_id; | ||
490 | 471 | /*! The thread name which includes where the thread was started */ | ||
491 | 472 | const char *thread_name; | ||
492 | 473 | /*! This is the actual container of info for what locks this thread holds */ | ||
493 | 474 | struct { | ||
494 | 475 | const char *file; | ||
495 | 476 | int line_num; | ||
496 | 477 | const char *func; | ||
497 | 478 | const char *lock_name; | ||
498 | 479 | void *lock_addr; | ||
499 | 480 | int times_locked; | ||
500 | 481 | enum ast_lock_type type; | ||
501 | 482 | /*! This thread is waiting on this lock */ | ||
502 | 483 | int pending:2; | ||
503 | 484 | #ifdef HAVE_BKTR | ||
504 | 485 | struct ast_bt *backtrace; | ||
505 | 486 | #endif | ||
506 | 487 | } locks[AST_MAX_LOCKS]; | ||
507 | 488 | /*! This is the number of locks currently held by this thread. | ||
508 | 489 | * The index (num_locks - 1) has the info on the last one in the | ||
509 | 490 | * locks member */ | ||
510 | 491 | unsigned int num_locks; | ||
511 | 492 | /*! Protects the contents of the locks member | ||
512 | 493 | * Intentionally not ast_mutex_t */ | ||
513 | 494 | pthread_mutex_t lock; | ||
514 | 495 | AST_LIST_ENTRY(thr_lock_info) entry; | ||
515 | 496 | }; | ||
516 | 497 | |||
517 | 498 | /*! | ||
518 | 499 | * \brief Locked when accessing the lock_infos list | ||
519 | 500 | */ | ||
520 | 501 | AST_MUTEX_DEFINE_STATIC(lock_infos_lock); | ||
521 | 502 | /*! | ||
522 | 503 | * \brief A list of each thread's lock info | ||
523 | 504 | */ | ||
524 | 505 | static AST_LIST_HEAD_NOLOCK_STATIC(lock_infos, thr_lock_info); | ||
525 | 506 | |||
526 | 507 | /*! | ||
527 | 508 | * \brief Destroy a thread's lock info | ||
528 | 509 | * | ||
529 | 510 | * This gets called automatically when the thread stops | ||
530 | 511 | */ | ||
531 | 512 | static void lock_info_destroy(void *data) | ||
532 | 513 | { | ||
533 | 514 | struct thr_lock_info *lock_info = data; | ||
534 | 515 | int i; | ||
535 | 516 | |||
536 | 517 | pthread_mutex_lock(&lock_infos_lock.mutex); | ||
537 | 518 | AST_LIST_REMOVE(&lock_infos, lock_info, entry); | ||
538 | 519 | pthread_mutex_unlock(&lock_infos_lock.mutex); | ||
539 | 520 | |||
540 | 521 | |||
541 | 522 | for (i = 0; i < lock_info->num_locks; i++) { | ||
542 | 523 | if (lock_info->locks[i].pending == -1) { | ||
543 | 524 | /* This just means that the last lock this thread went for was by | ||
544 | 525 | * using trylock, and it failed. This is fine. */ | ||
545 | 526 | break; | ||
546 | 527 | } | ||
547 | 528 | |||
548 | 529 | ast_log(LOG_ERROR, | ||
549 | 530 | "Thread '%s' still has a lock! - '%s' (%p) from '%s' in %s:%d!\n", | ||
550 | 531 | lock_info->thread_name, | ||
551 | 532 | lock_info->locks[i].lock_name, | ||
552 | 533 | lock_info->locks[i].lock_addr, | ||
553 | 534 | lock_info->locks[i].func, | ||
554 | 535 | lock_info->locks[i].file, | ||
555 | 536 | lock_info->locks[i].line_num | ||
556 | 537 | ); | ||
557 | 538 | } | ||
558 | 539 | |||
559 | 540 | pthread_mutex_destroy(&lock_info->lock); | ||
560 | 541 | if (lock_info->thread_name) | ||
561 | 542 | free((void *) lock_info->thread_name); | ||
562 | 543 | free(lock_info); | ||
563 | 544 | } | ||
564 | 545 | |||
565 | 546 | /*! | ||
566 | 547 | * \brief The thread storage key for per-thread lock info | ||
567 | 548 | */ | ||
568 | 549 | AST_THREADSTORAGE_CUSTOM(thread_lock_info, NULL, lock_info_destroy); | ||
569 | 550 | #ifdef HAVE_BKTR | ||
570 | 551 | void ast_store_lock_info(enum ast_lock_type type, const char *filename, | ||
571 | 552 | int line_num, const char *func, const char *lock_name, void *lock_addr, struct ast_bt *bt) | ||
572 | 553 | #else | ||
573 | 554 | void ast_store_lock_info(enum ast_lock_type type, const char *filename, | ||
574 | 555 | int line_num, const char *func, const char *lock_name, void *lock_addr) | ||
575 | 556 | #endif | ||
576 | 557 | { | ||
577 | 558 | struct thr_lock_info *lock_info; | ||
578 | 559 | int i; | ||
579 | 560 | |||
580 | 561 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
581 | 562 | return; | ||
582 | 563 | |||
583 | 564 | pthread_mutex_lock(&lock_info->lock); | ||
584 | 565 | |||
585 | 566 | for (i = 0; i < lock_info->num_locks; i++) { | ||
586 | 567 | if (lock_info->locks[i].lock_addr == lock_addr) { | ||
587 | 568 | lock_info->locks[i].times_locked++; | ||
588 | 569 | #ifdef HAVE_BKTR | ||
589 | 570 | lock_info->locks[i].backtrace = bt; | ||
590 | 571 | #endif | ||
591 | 572 | pthread_mutex_unlock(&lock_info->lock); | ||
592 | 573 | return; | ||
593 | 574 | } | ||
594 | 575 | } | ||
595 | 576 | |||
596 | 577 | if (lock_info->num_locks == AST_MAX_LOCKS) { | ||
597 | 578 | /* Can't use ast_log here, because it will cause infinite recursion */ | ||
598 | 579 | fprintf(stderr, "XXX ERROR XXX A thread holds more locks than '%d'." | ||
599 | 580 | " Increase AST_MAX_LOCKS!\n", AST_MAX_LOCKS); | ||
600 | 581 | pthread_mutex_unlock(&lock_info->lock); | ||
601 | 582 | return; | ||
602 | 583 | } | ||
603 | 584 | |||
604 | 585 | if (i && lock_info->locks[i - 1].pending == -1) { | ||
605 | 586 | /* The last lock on the list was one that this thread tried to lock but | ||
606 | 587 | * failed at doing so. It has now moved on to something else, so remove | ||
607 | 588 | * the old lock from the list. */ | ||
608 | 589 | i--; | ||
609 | 590 | lock_info->num_locks--; | ||
610 | 591 | memset(&lock_info->locks[i], 0, sizeof(lock_info->locks[0])); | ||
611 | 592 | } | ||
612 | 593 | |||
613 | 594 | lock_info->locks[i].file = filename; | ||
614 | 595 | lock_info->locks[i].line_num = line_num; | ||
615 | 596 | lock_info->locks[i].func = func; | ||
616 | 597 | lock_info->locks[i].lock_name = lock_name; | ||
617 | 598 | lock_info->locks[i].lock_addr = lock_addr; | ||
618 | 599 | lock_info->locks[i].times_locked = 1; | ||
619 | 600 | lock_info->locks[i].type = type; | ||
620 | 601 | lock_info->locks[i].pending = 1; | ||
621 | 602 | #ifdef HAVE_BKTR | ||
622 | 603 | lock_info->locks[i].backtrace = bt; | ||
623 | 604 | #endif | ||
624 | 605 | lock_info->num_locks++; | ||
625 | 606 | |||
626 | 607 | pthread_mutex_unlock(&lock_info->lock); | ||
627 | 608 | } | ||
628 | 609 | |||
629 | 610 | void ast_mark_lock_acquired(void *lock_addr) | ||
630 | 611 | { | ||
631 | 612 | struct thr_lock_info *lock_info; | ||
632 | 613 | |||
633 | 614 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
634 | 615 | return; | ||
635 | 616 | |||
636 | 617 | pthread_mutex_lock(&lock_info->lock); | ||
637 | 618 | if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) { | ||
638 | 619 | lock_info->locks[lock_info->num_locks - 1].pending = 0; | ||
639 | 620 | } | ||
640 | 621 | pthread_mutex_unlock(&lock_info->lock); | ||
641 | 622 | } | ||
642 | 623 | |||
643 | 624 | void ast_mark_lock_failed(void *lock_addr) | ||
644 | 625 | { | ||
645 | 626 | struct thr_lock_info *lock_info; | ||
646 | 627 | |||
647 | 628 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
648 | 629 | return; | ||
649 | 630 | |||
650 | 631 | pthread_mutex_lock(&lock_info->lock); | ||
651 | 632 | if (lock_info->locks[lock_info->num_locks - 1].lock_addr == lock_addr) { | ||
652 | 633 | lock_info->locks[lock_info->num_locks - 1].pending = -1; | ||
653 | 634 | lock_info->locks[lock_info->num_locks - 1].times_locked--; | ||
654 | 635 | } | ||
655 | 636 | pthread_mutex_unlock(&lock_info->lock); | ||
656 | 637 | } | ||
657 | 638 | |||
658 | 639 | int ast_find_lock_info(void *lock_addr, char *filename, size_t filename_size, int *lineno, char *func, size_t func_size, char *mutex_name, size_t mutex_name_size) | ||
659 | 640 | { | ||
660 | 641 | struct thr_lock_info *lock_info; | ||
661 | 642 | int i = 0; | ||
662 | 643 | |||
663 | 644 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
664 | 645 | return -1; | ||
665 | 646 | |||
666 | 647 | pthread_mutex_lock(&lock_info->lock); | ||
667 | 648 | |||
668 | 649 | for (i = lock_info->num_locks - 1; i >= 0; i--) { | ||
669 | 650 | if (lock_info->locks[i].lock_addr == lock_addr) | ||
670 | 651 | break; | ||
671 | 652 | } | ||
672 | 653 | |||
673 | 654 | if (i == -1) { | ||
674 | 655 | /* Lock not found :( */ | ||
675 | 656 | pthread_mutex_unlock(&lock_info->lock); | ||
676 | 657 | return -1; | ||
677 | 658 | } | ||
678 | 659 | |||
679 | 660 | ast_copy_string(filename, lock_info->locks[i].file, filename_size); | ||
680 | 661 | *lineno = lock_info->locks[i].line_num; | ||
681 | 662 | ast_copy_string(func, lock_info->locks[i].func, func_size); | ||
682 | 663 | ast_copy_string(mutex_name, lock_info->locks[i].lock_name, mutex_name_size); | ||
683 | 664 | |||
684 | 665 | pthread_mutex_unlock(&lock_info->lock); | ||
685 | 666 | |||
686 | 667 | return 0; | ||
687 | 668 | } | ||
688 | 669 | |||
689 | 670 | #ifdef HAVE_BKTR | ||
690 | 671 | void ast_remove_lock_info(void *lock_addr, struct ast_bt *bt) | ||
691 | 672 | #else | ||
692 | 673 | void ast_remove_lock_info(void *lock_addr) | ||
693 | 674 | #endif | ||
694 | 675 | { | ||
695 | 676 | struct thr_lock_info *lock_info; | ||
696 | 677 | int i = 0; | ||
697 | 678 | |||
698 | 679 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
699 | 680 | return; | ||
700 | 681 | |||
701 | 682 | pthread_mutex_lock(&lock_info->lock); | ||
702 | 683 | |||
703 | 684 | for (i = lock_info->num_locks - 1; i >= 0; i--) { | ||
704 | 685 | if (lock_info->locks[i].lock_addr == lock_addr) | ||
705 | 686 | break; | ||
706 | 687 | } | ||
707 | 688 | |||
708 | 689 | if (i == -1) { | ||
709 | 690 | /* Lock not found :( */ | ||
710 | 691 | pthread_mutex_unlock(&lock_info->lock); | ||
711 | 692 | return; | ||
712 | 693 | } | ||
713 | 694 | |||
714 | 695 | if (lock_info->locks[i].times_locked > 1) { | ||
715 | 696 | lock_info->locks[i].times_locked--; | ||
716 | 697 | #ifdef HAVE_BKTR | ||
717 | 698 | lock_info->locks[i].backtrace = bt; | ||
718 | 699 | #endif | ||
719 | 700 | pthread_mutex_unlock(&lock_info->lock); | ||
720 | 701 | return; | ||
721 | 702 | } | ||
722 | 703 | |||
723 | 704 | if (i < lock_info->num_locks - 1) { | ||
724 | 705 | /* Not the last one ... *should* be rare! */ | ||
725 | 706 | memmove(&lock_info->locks[i], &lock_info->locks[i + 1], | ||
726 | 707 | (lock_info->num_locks - (i + 1)) * sizeof(lock_info->locks[0])); | ||
727 | 708 | } | ||
728 | 709 | |||
729 | 710 | lock_info->num_locks--; | ||
730 | 711 | |||
731 | 712 | pthread_mutex_unlock(&lock_info->lock); | ||
732 | 713 | } | ||
733 | 714 | |||
734 | 715 | static const char *locktype2str(enum ast_lock_type type) | ||
735 | 716 | { | ||
736 | 717 | switch (type) { | ||
737 | 718 | case AST_MUTEX: | ||
738 | 719 | return "MUTEX"; | ||
739 | 720 | case AST_RDLOCK: | ||
740 | 721 | return "RDLOCK"; | ||
741 | 722 | case AST_WRLOCK: | ||
742 | 723 | return "WRLOCK"; | ||
743 | 724 | } | ||
744 | 725 | |||
745 | 726 | return "UNKNOWN"; | ||
746 | 727 | } | ||
747 | 728 | |||
748 | 729 | #ifdef HAVE_BKTR | ||
749 | 730 | static void append_backtrace_information(struct ast_str **str, struct ast_bt *bt) | ||
750 | 731 | { | ||
751 | 732 | char **symbols; | ||
752 | 733 | |||
753 | 734 | if (!bt) { | ||
754 | 735 | ast_str_append(str, 0, "\tNo backtrace to print\n"); | ||
755 | 736 | return; | ||
756 | 737 | } | ||
757 | 738 | |||
758 | 739 | if ((symbols = backtrace_symbols(bt->addresses, bt->num_frames))) { | ||
759 | 740 | int frame_iterator; | ||
760 | 741 | |||
761 | 742 | for (frame_iterator = 0; frame_iterator < bt->num_frames; ++frame_iterator) { | ||
762 | 743 | ast_str_append(str, 0, "\t%s\n", symbols[frame_iterator]); | ||
763 | 744 | } | ||
764 | 745 | |||
765 | 746 | free(symbols); | ||
766 | 747 | } else { | ||
767 | 748 | ast_str_append(str, 0, "\tCouldn't retrieve backtrace symbols\n"); | ||
768 | 749 | } | ||
769 | 750 | } | ||
770 | 751 | #endif | ||
771 | 752 | |||
772 | 753 | static void append_lock_information(struct ast_str **str, struct thr_lock_info *lock_info, int i) | ||
773 | 754 | { | ||
774 | 755 | int j; | ||
775 | 756 | ast_mutex_t *lock; | ||
776 | 757 | struct ast_lock_track *lt; | ||
777 | 758 | |||
778 | 759 | ast_str_append(str, 0, "=== ---> %sLock #%d (%s): %s %d %s %s %p (%d)\n", | ||
779 | 760 | lock_info->locks[i].pending > 0 ? "Waiting for " : | ||
780 | 761 | lock_info->locks[i].pending < 0 ? "Tried and failed to get " : "", i, | ||
781 | 762 | lock_info->locks[i].file, | ||
782 | 763 | locktype2str(lock_info->locks[i].type), | ||
783 | 764 | lock_info->locks[i].line_num, | ||
784 | 765 | lock_info->locks[i].func, lock_info->locks[i].lock_name, | ||
785 | 766 | lock_info->locks[i].lock_addr, | ||
786 | 767 | lock_info->locks[i].times_locked); | ||
787 | 768 | #ifdef HAVE_BKTR | ||
788 | 769 | append_backtrace_information(str, lock_info->locks[i].backtrace); | ||
789 | 770 | #endif | ||
790 | 771 | |||
791 | 772 | if (!lock_info->locks[i].pending || lock_info->locks[i].pending == -1) | ||
792 | 773 | return; | ||
793 | 774 | |||
794 | 775 | /* We only have further details for mutexes right now */ | ||
795 | 776 | if (lock_info->locks[i].type != AST_MUTEX) | ||
796 | 777 | return; | ||
797 | 778 | |||
798 | 779 | lock = lock_info->locks[i].lock_addr; | ||
799 | 780 | lt = &lock->track; | ||
800 | 781 | ast_reentrancy_lock(lt); | ||
801 | 782 | for (j = 0; *str && j < lt->reentrancy; j++) { | ||
802 | 783 | ast_str_append(str, 0, "=== --- ---> Locked Here: %s line %d (%s)\n", | ||
803 | 784 | lt->file[j], lt->lineno[j], lt->func[j]); | ||
804 | 785 | } | ||
805 | 786 | ast_reentrancy_unlock(lt); | ||
806 | 787 | } | ||
807 | 788 | |||
808 | 789 | |||
809 | 790 | /*! This function can help you find highly temporal locks; locks that happen for a | ||
810 | 791 | short time, but at unexpected times, usually at times that create a deadlock, | ||
811 | 792 | Why is this thing locked right then? Who is locking it? Who am I fighting | ||
812 | 793 | with for this lock? | ||
813 | 794 | |||
814 | 795 | To answer such questions, just call this routine before you would normally try | ||
815 | 796 | to aquire a lock. It doesn't do anything if the lock is not acquired. If the | ||
816 | 797 | lock is taken, it will publish a line or two to the console via ast_log(). | ||
817 | 798 | |||
818 | 799 | Sometimes, the lock message is pretty uninformative. For instance, you might | ||
819 | 800 | find that the lock is being aquired deep within the astobj2 code; this tells | ||
820 | 801 | you little about higher level routines that call the astobj2 routines. | ||
821 | 802 | But, using gdb, you can set a break at the ast_log below, and for that | ||
822 | 803 | breakpoint, you can set the commands: | ||
823 | 804 | where | ||
824 | 805 | cont | ||
825 | 806 | which will give a stack trace and continue. -- that aught to do the job! | ||
826 | 807 | |||
827 | 808 | */ | ||
828 | 809 | void log_show_lock(void *this_lock_addr) | ||
829 | 810 | { | ||
830 | 811 | struct thr_lock_info *lock_info; | ||
831 | 812 | struct ast_str *str; | ||
832 | 813 | |||
833 | 814 | if (!(str = ast_str_create(4096))) { | ||
834 | 815 | ast_log(LOG_NOTICE,"Could not create str\n"); | ||
835 | 816 | return; | ||
836 | 817 | } | ||
837 | 818 | |||
838 | 819 | |||
839 | 820 | pthread_mutex_lock(&lock_infos_lock.mutex); | ||
840 | 821 | AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) { | ||
841 | 822 | int i; | ||
842 | 823 | pthread_mutex_lock(&lock_info->lock); | ||
843 | 824 | for (i = 0; str && i < lock_info->num_locks; i++) { | ||
844 | 825 | /* ONLY show info about this particular lock, if | ||
845 | 826 | it's acquired... */ | ||
846 | 827 | if (lock_info->locks[i].lock_addr == this_lock_addr) { | ||
847 | 828 | append_lock_information(&str, lock_info, i); | ||
848 | 829 | ast_log(LOG_NOTICE, "%s", ast_str_buffer(str)); | ||
849 | 830 | break; | ||
850 | 831 | } | ||
851 | 832 | } | ||
852 | 833 | pthread_mutex_unlock(&lock_info->lock); | ||
853 | 834 | } | ||
854 | 835 | pthread_mutex_unlock(&lock_infos_lock.mutex); | ||
855 | 836 | ast_free(str); | ||
856 | 837 | } | ||
857 | 838 | |||
858 | 839 | |||
859 | 840 | static char *handle_show_locks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||
860 | 841 | { | ||
861 | 842 | struct thr_lock_info *lock_info; | ||
862 | 843 | struct ast_str *str; | ||
863 | 844 | |||
864 | 845 | if (!(str = ast_str_create(4096))) | ||
865 | 846 | return CLI_FAILURE; | ||
866 | 847 | |||
867 | 848 | switch (cmd) { | ||
868 | 849 | case CLI_INIT: | ||
869 | 850 | e->command = "core show locks"; | ||
870 | 851 | e->usage = | ||
871 | 852 | "Usage: core show locks\n" | ||
872 | 853 | " This command is for lock debugging. It prints out which locks\n" | ||
873 | 854 | "are owned by each active thread.\n"; | ||
874 | 855 | return NULL; | ||
875 | 856 | |||
876 | 857 | case CLI_GENERATE: | ||
877 | 858 | return NULL; | ||
878 | 859 | } | ||
879 | 860 | |||
880 | 861 | ast_str_append(&str, 0, "\n" | ||
881 | 862 | "=======================================================================\n" | ||
882 | 863 | "=== Currently Held Locks ==============================================\n" | ||
883 | 864 | "=======================================================================\n" | ||
884 | 865 | "===\n" | ||
885 | 866 | "=== <pending> <lock#> (<file>): <lock type> <line num> <function> <lock name> <lock addr> (times locked)\n" | ||
886 | 867 | "===\n"); | ||
887 | 868 | |||
888 | 869 | if (!str) | ||
889 | 870 | return CLI_FAILURE; | ||
890 | 871 | |||
891 | 872 | pthread_mutex_lock(&lock_infos_lock.mutex); | ||
892 | 873 | AST_LIST_TRAVERSE(&lock_infos, lock_info, entry) { | ||
893 | 874 | int i; | ||
894 | 875 | if (lock_info->num_locks) { | ||
895 | 876 | ast_str_append(&str, 0, "=== Thread ID: %ld (%s)\n", (long) lock_info->thread_id, | ||
896 | 877 | lock_info->thread_name); | ||
897 | 878 | pthread_mutex_lock(&lock_info->lock); | ||
898 | 879 | for (i = 0; str && i < lock_info->num_locks; i++) { | ||
899 | 880 | append_lock_information(&str, lock_info, i); | ||
900 | 881 | } | ||
901 | 882 | pthread_mutex_unlock(&lock_info->lock); | ||
902 | 883 | if (!str) | ||
903 | 884 | break; | ||
904 | 885 | ast_str_append(&str, 0, "=== -------------------------------------------------------------------\n" | ||
905 | 886 | "===\n"); | ||
906 | 887 | if (!str) | ||
907 | 888 | break; | ||
908 | 889 | } | ||
909 | 890 | } | ||
910 | 891 | pthread_mutex_unlock(&lock_infos_lock.mutex); | ||
911 | 892 | |||
912 | 893 | if (!str) | ||
913 | 894 | return CLI_FAILURE; | ||
914 | 895 | |||
915 | 896 | ast_str_append(&str, 0, "=======================================================================\n" | ||
916 | 897 | "\n"); | ||
917 | 898 | |||
918 | 899 | if (!str) | ||
919 | 900 | return CLI_FAILURE; | ||
920 | 901 | |||
921 | 902 | ast_cli(a->fd, "%s", ast_str_buffer(str)); | ||
922 | 903 | |||
923 | 904 | ast_free(str); | ||
924 | 905 | |||
925 | 906 | return CLI_SUCCESS; | ||
926 | 907 | } | ||
927 | 908 | |||
928 | 909 | static struct ast_cli_entry utils_cli[] = { | ||
929 | 910 | AST_CLI_DEFINE(handle_show_locks, "Show which locks are held by which thread"), | ||
930 | 911 | }; | ||
931 | 912 | |||
932 | 913 | #endif /* DEBUG_THREADS */ | ||
933 | 914 | |||
934 | 915 | /* | ||
935 | 916 | * support for 'show threads'. The start routine is wrapped by | ||
936 | 917 | * dummy_start(), so that ast_register_thread() and | ||
937 | 918 | * ast_unregister_thread() know the thread identifier. | ||
938 | 919 | */ | ||
939 | 920 | struct thr_arg { | ||
940 | 921 | void *(*start_routine)(void *); | ||
941 | 922 | void *data; | ||
942 | 923 | char *name; | ||
943 | 924 | }; | ||
944 | 925 | |||
945 | 926 | /* | ||
946 | 927 | * on OS/X, pthread_cleanup_push() and pthread_cleanup_pop() | ||
947 | 928 | * are odd macros which start and end a block, so they _must_ be | ||
948 | 929 | * used in pairs (the latter with a '1' argument to call the | ||
949 | 930 | * handler on exit. | ||
950 | 931 | * On BSD we don't need this, but we keep it for compatibility. | ||
951 | 932 | */ | ||
952 | 933 | static void *dummy_start(void *data) | ||
953 | 934 | { | ||
954 | 935 | void *ret; | ||
955 | 936 | struct thr_arg a = *((struct thr_arg *) data); /* make a local copy */ | ||
956 | 937 | #ifdef DEBUG_THREADS | ||
957 | 938 | struct thr_lock_info *lock_info; | ||
958 | 939 | pthread_mutexattr_t mutex_attr; | ||
959 | 940 | #endif | ||
960 | 941 | |||
961 | 942 | /* note that even though data->name is a pointer to allocated memory, | ||
962 | 943 | we are not freeing it here because ast_register_thread is going to | ||
963 | 944 | keep a copy of the pointer and then ast_unregister_thread will | ||
964 | 945 | free the memory | ||
965 | 946 | */ | ||
966 | 947 | ast_free(data); | ||
967 | 948 | ast_register_thread(a.name); | ||
968 | 949 | pthread_cleanup_push(ast_unregister_thread, (void *) pthread_self()); | ||
969 | 950 | |||
970 | 951 | #ifdef DEBUG_THREADS | ||
971 | 952 | if (!(lock_info = ast_threadstorage_get(&thread_lock_info, sizeof(*lock_info)))) | ||
972 | 953 | return NULL; | ||
973 | 954 | |||
974 | 955 | lock_info->thread_id = pthread_self(); | ||
975 | 956 | lock_info->thread_name = strdup(a.name); | ||
976 | 957 | |||
977 | 958 | pthread_mutexattr_init(&mutex_attr); | ||
978 | 959 | pthread_mutexattr_settype(&mutex_attr, AST_MUTEX_KIND); | ||
979 | 960 | pthread_mutex_init(&lock_info->lock, &mutex_attr); | ||
980 | 961 | pthread_mutexattr_destroy(&mutex_attr); | ||
981 | 962 | |||
982 | 963 | pthread_mutex_lock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */ | ||
983 | 964 | AST_LIST_INSERT_TAIL(&lock_infos, lock_info, entry); | ||
984 | 965 | pthread_mutex_unlock(&lock_infos_lock.mutex); /* Intentionally not the wrapper */ | ||
985 | 966 | #endif /* DEBUG_THREADS */ | ||
986 | 967 | |||
987 | 968 | ret = a.start_routine(a.data); | ||
988 | 969 | |||
989 | 970 | pthread_cleanup_pop(1); | ||
990 | 971 | |||
991 | 972 | return ret; | ||
992 | 973 | } | ||
993 | 974 | |||
994 | 975 | #endif /* !LOW_MEMORY */ | ||
995 | 976 | |||
996 | 977 | int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), | ||
997 | 978 | void *data, size_t stacksize, const char *file, const char *caller, | ||
998 | 979 | int line, const char *start_fn) | ||
999 | 980 | { | ||
1000 | 981 | #if !defined(LOW_MEMORY) | ||
1001 | 982 | struct thr_arg *a; | ||
1002 | 983 | #endif | ||
1003 | 984 | |||
1004 | 985 | if (!attr) { | ||
1005 | 986 | attr = alloca(sizeof(*attr)); | ||
1006 | 987 | pthread_attr_init(attr); | ||
1007 | 988 | } | ||
1008 | 989 | |||
1009 | 990 | #ifdef __linux__ | ||
1010 | 991 | /* On Linux, pthread_attr_init() defaults to PTHREAD_EXPLICIT_SCHED, | ||
1011 | 992 | which is kind of useless. Change this here to | ||
1012 | 993 | PTHREAD_INHERIT_SCHED; that way the -p option to set realtime | ||
1013 | 994 | priority will propagate down to new threads by default. | ||
1014 | 995 | This does mean that callers cannot set a different priority using | ||
1015 | 996 | PTHREAD_EXPLICIT_SCHED in the attr argument; instead they must set | ||
1016 | 997 | the priority afterwards with pthread_setschedparam(). */ | ||
1017 | 998 | if ((errno = pthread_attr_setinheritsched(attr, PTHREAD_INHERIT_SCHED))) | ||
1018 | 999 | ast_log(LOG_WARNING, "pthread_attr_setinheritsched: %s\n", strerror(errno)); | ||
1019 | 1000 | #endif | ||
1020 | 1001 | |||
1021 | 1002 | if (!stacksize) | ||
1022 | 1003 | stacksize = AST_STACKSIZE; | ||
1023 | 1004 | |||
1024 | 1005 | if ((errno = pthread_attr_setstacksize(attr, stacksize ? stacksize : AST_STACKSIZE))) | ||
1025 | 1006 | ast_log(LOG_WARNING, "pthread_attr_setstacksize: %s\n", strerror(errno)); | ||
1026 | 1007 | |||
1027 | 1008 | #if !defined(LOW_MEMORY) | ||
1028 | 1009 | if ((a = ast_malloc(sizeof(*a)))) { | ||
1029 | 1010 | a->start_routine = start_routine; | ||
1030 | 1011 | a->data = data; | ||
1031 | 1012 | start_routine = dummy_start; | ||
1032 | 1013 | if (asprintf(&a->name, "%-20s started at [%5d] %s %s()", | ||
1033 | 1014 | start_fn, line, file, caller) < 0) { | ||
1034 | 1015 | ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno)); | ||
1035 | 1016 | a->name = NULL; | ||
1036 | 1017 | } | ||
1037 | 1018 | data = a; | ||
1038 | 1019 | } | ||
1039 | 1020 | #endif /* !LOW_MEMORY */ | ||
1040 | 1021 | |||
1041 | 1022 | return pthread_create(thread, attr, start_routine, data); /* We're in ast_pthread_create, so it's okay */ | ||
1042 | 1023 | } | ||
1043 | 1024 | |||
1044 | 1025 | |||
1045 | 1026 | int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), | ||
1046 | 1027 | void *data, size_t stacksize, const char *file, const char *caller, | ||
1047 | 1028 | int line, const char *start_fn) | ||
1048 | 1029 | { | ||
1049 | 1030 | unsigned char attr_destroy = 0; | ||
1050 | 1031 | int res; | ||
1051 | 1032 | |||
1052 | 1033 | if (!attr) { | ||
1053 | 1034 | attr = alloca(sizeof(*attr)); | ||
1054 | 1035 | pthread_attr_init(attr); | ||
1055 | 1036 | attr_destroy = 1; | ||
1056 | 1037 | } | ||
1057 | 1038 | |||
1058 | 1039 | if ((errno = pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED))) | ||
1059 | 1040 | ast_log(LOG_WARNING, "pthread_attr_setdetachstate: %s\n", strerror(errno)); | ||
1060 | 1041 | |||
1061 | 1042 | res = ast_pthread_create_stack(thread, attr, start_routine, data, | ||
1062 | 1043 | stacksize, file, caller, line, start_fn); | ||
1063 | 1044 | |||
1064 | 1045 | if (attr_destroy) | ||
1065 | 1046 | pthread_attr_destroy(attr); | ||
1066 | 1047 | |||
1067 | 1048 | return res; | ||
1068 | 1049 | } | ||
1069 | 1050 | |||
1070 | 1051 | int ast_wait_for_input(int fd, int ms) | ||
1071 | 1052 | { | ||
1072 | 1053 | struct pollfd pfd[1]; | ||
1073 | 1054 | memset(pfd, 0, sizeof(pfd)); | ||
1074 | 1055 | pfd[0].fd = fd; | ||
1075 | 1056 | pfd[0].events = POLLIN|POLLPRI; | ||
1076 | 1057 | return ast_poll(pfd, 1, ms); | ||
1077 | 1058 | } | ||
1078 | 1059 | |||
1079 | 1060 | static int ast_wait_for_output(int fd, int timeoutms) | ||
1080 | 1061 | { | ||
1081 | 1062 | struct pollfd pfd = { | ||
1082 | 1063 | .fd = fd, | ||
1083 | 1064 | .events = POLLOUT, | ||
1084 | 1065 | }; | ||
1085 | 1066 | int res; | ||
1086 | 1067 | struct timeval start = ast_tvnow(); | ||
1087 | 1068 | int elapsed = 0; | ||
1088 | 1069 | |||
1089 | 1070 | /* poll() until the fd is writable without blocking */ | ||
1090 | 1071 | while ((res = ast_poll(&pfd, 1, timeoutms - elapsed)) <= 0) { | ||
1091 | 1072 | if (res == 0) { | ||
1092 | 1073 | /* timed out. */ | ||
1093 | 1074 | #ifndef STANDALONE | ||
1094 | 1075 | ast_debug(1, "Timed out trying to write\n"); | ||
1095 | 1076 | #endif | ||
1096 | 1077 | return -1; | ||
1097 | 1078 | } else if (res == -1) { | ||
1098 | 1079 | /* poll() returned an error, check to see if it was fatal */ | ||
1099 | 1080 | |||
1100 | 1081 | if (errno == EINTR || errno == EAGAIN) { | ||
1101 | 1082 | elapsed = ast_tvdiff_ms(ast_tvnow(), start); | ||
1102 | 1083 | if (elapsed >= timeoutms) { | ||
1103 | 1084 | return -1; | ||
1104 | 1085 | } | ||
1105 | 1086 | /* This was an acceptable error, go back into poll() */ | ||
1106 | 1087 | continue; | ||
1107 | 1088 | } | ||
1108 | 1089 | |||
1109 | 1090 | /* Fatal error, bail. */ | ||
1110 | 1091 | ast_log(LOG_ERROR, "poll returned error: %s\n", strerror(errno)); | ||
1111 | 1092 | |||
1112 | 1093 | return -1; | ||
1113 | 1094 | } | ||
1114 | 1095 | elapsed = ast_tvdiff_ms(ast_tvnow(), start); | ||
1115 | 1096 | if (elapsed >= timeoutms) { | ||
1116 | 1097 | return -1; | ||
1117 | 1098 | } | ||
1118 | 1099 | } | ||
1119 | 1100 | |||
1120 | 1101 | return 0; | ||
1121 | 1102 | } | ||
1122 | 1103 | |||
1123 | 1104 | /*! | ||
1124 | 1105 | * Try to write string, but wait no more than ms milliseconds before timing out. | ||
1125 | 1106 | * | ||
1126 | 1107 | * \note The code assumes that the file descriptor has NONBLOCK set, | ||
1127 | 1108 | * so there is only one system call made to do a write, unless we actually | ||
1128 | 1109 | * have a need to wait. This way, we get better performance. | ||
1129 | 1110 | * If the descriptor is blocking, all assumptions on the guaranteed | ||
1130 | 1111 | * detail do not apply anymore. | ||
1131 | 1112 | */ | ||
1132 | 1113 | int ast_carefulwrite(int fd, char *s, int len, int timeoutms) | ||
1133 | 1114 | { | ||
1134 | 1115 | struct timeval start = ast_tvnow(); | ||
1135 | 1116 | int res = 0; | ||
1136 | 1117 | int elapsed = 0; | ||
1137 | 1118 | |||
1138 | 1119 | while (len) { | ||
1139 | 1120 | if (ast_wait_for_output(fd, timeoutms - elapsed)) { | ||
1140 | 1121 | return -1; | ||
1141 | 1122 | } | ||
1142 | 1123 | |||
1143 | 1124 | res = write(fd, s, len); | ||
1144 | 1125 | |||
1145 | 1126 | if (res < 0 && errno != EAGAIN && errno != EINTR) { | ||
1146 | 1127 | /* fatal error from write() */ | ||
1147 | 1128 | ast_log(LOG_ERROR, "write() returned error: %s\n", strerror(errno)); | ||
1148 | 1129 | return -1; | ||
1149 | 1130 | } | ||
1150 | 1131 | |||
1151 | 1132 | if (res < 0) { | ||
1152 | 1133 | /* It was an acceptable error */ | ||
1153 | 1134 | res = 0; | ||
1154 | 1135 | } | ||
1155 | 1136 | |||
1156 | 1137 | /* Update how much data we have left to write */ | ||
1157 | 1138 | len -= res; | ||
1158 | 1139 | s += res; | ||
1159 | 1140 | res = 0; | ||
1160 | 1141 | |||
1161 | 1142 | elapsed = ast_tvdiff_ms(ast_tvnow(), start); | ||
1162 | 1143 | if (elapsed >= timeoutms) { | ||
1163 | 1144 | /* We've taken too long to write | ||
1164 | 1145 | * This is only an error condition if we haven't finished writing. */ | ||
1165 | 1146 | res = len ? -1 : 0; | ||
1166 | 1147 | break; | ||
1167 | 1148 | } | ||
1168 | 1149 | } | ||
1169 | 1150 | |||
1170 | 1151 | return res; | ||
1171 | 1152 | } | ||
1172 | 1153 | |||
1173 | 1154 | int ast_careful_fwrite(FILE *f, int fd, const char *src, size_t len, int timeoutms) | ||
1174 | 1155 | { | ||
1175 | 1156 | struct timeval start = ast_tvnow(); | ||
1176 | 1157 | int n = 0; | ||
1177 | 1158 | int elapsed = 0; | ||
1178 | 1159 | |||
1179 | 1160 | while (len) { | ||
1180 | 1161 | if (ast_wait_for_output(fd, timeoutms - elapsed)) { | ||
1181 | 1162 | /* poll returned a fatal error, so bail out immediately. */ | ||
1182 | 1163 | return -1; | ||
1183 | 1164 | } | ||
1184 | 1165 | |||
1185 | 1166 | /* Clear any errors from a previous write */ | ||
1186 | 1167 | clearerr(f); | ||
1187 | 1168 | |||
1188 | 1169 | n = fwrite(src, 1, len, f); | ||
1189 | 1170 | |||
1190 | 1171 | if (ferror(f) && errno != EINTR && errno != EAGAIN) { | ||
1191 | 1172 | /* fatal error from fwrite() */ | ||
1192 | 1173 | if (!feof(f)) { | ||
1193 | 1174 | /* Don't spam the logs if it was just that the connection is closed. */ | ||
1194 | 1175 | ast_log(LOG_ERROR, "fwrite() returned error: %s\n", strerror(errno)); | ||
1195 | 1176 | } | ||
1196 | 1177 | n = -1; | ||
1197 | 1178 | break; | ||
1198 | 1179 | } | ||
1199 | 1180 | |||
1200 | 1181 | /* Update for data already written to the socket */ | ||
1201 | 1182 | len -= n; | ||
1202 | 1183 | src += n; | ||
1203 | 1184 | |||
1204 | 1185 | elapsed = ast_tvdiff_ms(ast_tvnow(), start); | ||
1205 | 1186 | if (elapsed >= timeoutms) { | ||
1206 | 1187 | /* We've taken too long to write | ||
1207 | 1188 | * This is only an error condition if we haven't finished writing. */ | ||
1208 | 1189 | n = len ? -1 : 0; | ||
1209 | 1190 | break; | ||
1210 | 1191 | } | ||
1211 | 1192 | } | ||
1212 | 1193 | |||
1213 | 1194 | while (fflush(f)) { | ||
1214 | 1195 | if (errno == EAGAIN || errno == EINTR) { | ||
1215 | 1196 | continue; | ||
1216 | 1197 | } | ||
1217 | 1198 | if (!feof(f)) { | ||
1218 | 1199 | /* Don't spam the logs if it was just that the connection is closed. */ | ||
1219 | 1200 | ast_log(LOG_ERROR, "fflush() returned error: %s\n", strerror(errno)); | ||
1220 | 1201 | } | ||
1221 | 1202 | n = -1; | ||
1222 | 1203 | break; | ||
1223 | 1204 | } | ||
1224 | 1205 | |||
1225 | 1206 | return n < 0 ? -1 : 0; | ||
1226 | 1207 | } | ||
1227 | 1208 | |||
1228 | 1209 | char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes) | ||
1229 | 1210 | { | ||
1230 | 1211 | char *e; | ||
1231 | 1212 | char *q; | ||
1232 | 1213 | |||
1233 | 1214 | s = ast_strip(s); | ||
1234 | 1215 | if ((q = strchr(beg_quotes, *s)) && *q != '\0') { | ||
1235 | 1216 | e = s + strlen(s) - 1; | ||
1236 | 1217 | if (*e == *(end_quotes + (q - beg_quotes))) { | ||
1237 | 1218 | s++; | ||
1238 | 1219 | *e = '\0'; | ||
1239 | 1220 | } | ||
1240 | 1221 | } | ||
1241 | 1222 | |||
1242 | 1223 | return s; | ||
1243 | 1224 | } | ||
1244 | 1225 | |||
1245 | 1226 | char *ast_unescape_semicolon(char *s) | ||
1246 | 1227 | { | ||
1247 | 1228 | char *e; | ||
1248 | 1229 | char *work = s; | ||
1249 | 1230 | |||
1250 | 1231 | while ((e = strchr(work, ';'))) { | ||
1251 | 1232 | if ((e > work) && (*(e-1) == '\\')) { | ||
1252 | 1233 | memmove(e - 1, e, strlen(e) + 1); | ||
1253 | 1234 | work = e; | ||
1254 | 1235 | } else { | ||
1255 | 1236 | work = e + 1; | ||
1256 | 1237 | } | ||
1257 | 1238 | } | ||
1258 | 1239 | |||
1259 | 1240 | return s; | ||
1260 | 1241 | } | ||
1261 | 1242 | |||
1262 | 1243 | /* !\brief unescape some C sequences in place, return pointer to the original string. | ||
1263 | 1244 | */ | ||
1264 | 1245 | char *ast_unescape_c(char *src) | ||
1265 | 1246 | { | ||
1266 | 1247 | char c, *ret, *dst; | ||
1267 | 1248 | |||
1268 | 1249 | if (src == NULL) | ||
1269 | 1250 | return NULL; | ||
1270 | 1251 | for (ret = dst = src; (c = *src++); *dst++ = c ) { | ||
1271 | 1252 | if (c != '\\') | ||
1272 | 1253 | continue; /* copy char at the end of the loop */ | ||
1273 | 1254 | switch ((c = *src++)) { | ||
1274 | 1255 | case '\0': /* special, trailing '\' */ | ||
1275 | 1256 | c = '\\'; | ||
1276 | 1257 | break; | ||
1277 | 1258 | case 'b': /* backspace */ | ||
1278 | 1259 | c = '\b'; | ||
1279 | 1260 | break; | ||
1280 | 1261 | case 'f': /* form feed */ | ||
1281 | 1262 | c = '\f'; | ||
1282 | 1263 | break; | ||
1283 | 1264 | case 'n': | ||
1284 | 1265 | c = '\n'; | ||
1285 | 1266 | break; | ||
1286 | 1267 | case 'r': | ||
1287 | 1268 | c = '\r'; | ||
1288 | 1269 | break; | ||
1289 | 1270 | case 't': | ||
1290 | 1271 | c = '\t'; | ||
1291 | 1272 | break; | ||
1292 | 1273 | } | ||
1293 | 1274 | /* default, use the char literally */ | ||
1294 | 1275 | } | ||
1295 | 1276 | *dst = '\0'; | ||
1296 | 1277 | return ret; | ||
1297 | 1278 | } | ||
1298 | 1279 | |||
1299 | 1280 | int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) | ||
1300 | 1281 | { | ||
1301 | 1282 | int result; | ||
1302 | 1283 | |||
1303 | 1284 | if (!buffer || !*buffer || !space || !*space) | ||
1304 | 1285 | return -1; | ||
1305 | 1286 | |||
1306 | 1287 | result = vsnprintf(*buffer, *space, fmt, ap); | ||
1307 | 1288 | |||
1308 | 1289 | if (result < 0) | ||
1309 | 1290 | return -1; | ||
1310 | 1291 | else if (result > *space) | ||
1311 | 1292 | result = *space; | ||
1312 | 1293 | |||
1313 | 1294 | *buffer += result; | ||
1314 | 1295 | *space -= result; | ||
1315 | 1296 | return 0; | ||
1316 | 1297 | } | ||
1317 | 1298 | |||
1318 | 1299 | int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) | ||
1319 | 1300 | { | ||
1320 | 1301 | va_list ap; | ||
1321 | 1302 | int result; | ||
1322 | 1303 | |||
1323 | 1304 | va_start(ap, fmt); | ||
1324 | 1305 | result = ast_build_string_va(buffer, space, fmt, ap); | ||
1325 | 1306 | va_end(ap); | ||
1326 | 1307 | |||
1327 | 1308 | return result; | ||
1328 | 1309 | } | ||
1329 | 1310 | |||
1330 | 1311 | int ast_true(const char *s) | ||
1331 | 1312 | { | ||
1332 | 1313 | if (ast_strlen_zero(s)) | ||
1333 | 1314 | return 0; | ||
1334 | 1315 | |||
1335 | 1316 | /* Determine if this is a true value */ | ||
1336 | 1317 | if (!strcasecmp(s, "yes") || | ||
1337 | 1318 | !strcasecmp(s, "true") || | ||
1338 | 1319 | !strcasecmp(s, "y") || | ||
1339 | 1320 | !strcasecmp(s, "t") || | ||
1340 | 1321 | !strcasecmp(s, "1") || | ||
1341 | 1322 | !strcasecmp(s, "on")) | ||
1342 | 1323 | return -1; | ||
1343 | 1324 | |||
1344 | 1325 | return 0; | ||
1345 | 1326 | } | ||
1346 | 1327 | |||
1347 | 1328 | int ast_false(const char *s) | ||
1348 | 1329 | { | ||
1349 | 1330 | if (ast_strlen_zero(s)) | ||
1350 | 1331 | return 0; | ||
1351 | 1332 | |||
1352 | 1333 | /* Determine if this is a false value */ | ||
1353 | 1334 | if (!strcasecmp(s, "no") || | ||
1354 | 1335 | !strcasecmp(s, "false") || | ||
1355 | 1336 | !strcasecmp(s, "n") || | ||
1356 | 1337 | !strcasecmp(s, "f") || | ||
1357 | 1338 | !strcasecmp(s, "0") || | ||
1358 | 1339 | !strcasecmp(s, "off")) | ||
1359 | 1340 | return -1; | ||
1360 | 1341 | |||
1361 | 1342 | return 0; | ||
1362 | 1343 | } | ||
1363 | 1344 | |||
1364 | 1345 | #define ONE_MILLION 1000000 | ||
1365 | 1346 | /* | ||
1366 | 1347 | * put timeval in a valid range. usec is 0..999999 | ||
1367 | 1348 | * negative values are not allowed and truncated. | ||
1368 | 1349 | */ | ||
1369 | 1350 | static struct timeval tvfix(struct timeval a) | ||
1370 | 1351 | { | ||
1371 | 1352 | if (a.tv_usec >= ONE_MILLION) { | ||
1372 | 1353 | ast_log(LOG_WARNING, "warning too large timestamp %ld.%ld\n", | ||
1373 | 1354 | (long)a.tv_sec, (long int) a.tv_usec); | ||
1374 | 1355 | a.tv_sec += a.tv_usec / ONE_MILLION; | ||
1375 | 1356 | a.tv_usec %= ONE_MILLION; | ||
1376 | 1357 | } else if (a.tv_usec < 0) { | ||
1377 | 1358 | ast_log(LOG_WARNING, "warning negative timestamp %ld.%ld\n", | ||
1378 | 1359 | (long)a.tv_sec, (long int) a.tv_usec); | ||
1379 | 1360 | a.tv_usec = 0; | ||
1380 | 1361 | } | ||
1381 | 1362 | return a; | ||
1382 | 1363 | } | ||
1383 | 1364 | |||
1384 | 1365 | struct timeval ast_tvadd(struct timeval a, struct timeval b) | ||
1385 | 1366 | { | ||
1386 | 1367 | /* consistency checks to guarantee usec in 0..999999 */ | ||
1387 | 1368 | a = tvfix(a); | ||
1388 | 1369 | b = tvfix(b); | ||
1389 | 1370 | a.tv_sec += b.tv_sec; | ||
1390 | 1371 | a.tv_usec += b.tv_usec; | ||
1391 | 1372 | if (a.tv_usec >= ONE_MILLION) { | ||
1392 | 1373 | a.tv_sec++; | ||
1393 | 1374 | a.tv_usec -= ONE_MILLION; | ||
1394 | 1375 | } | ||
1395 | 1376 | return a; | ||
1396 | 1377 | } | ||
1397 | 1378 | |||
1398 | 1379 | struct timeval ast_tvsub(struct timeval a, struct timeval b) | ||
1399 | 1380 | { | ||
1400 | 1381 | /* consistency checks to guarantee usec in 0..999999 */ | ||
1401 | 1382 | a = tvfix(a); | ||
1402 | 1383 | b = tvfix(b); | ||
1403 | 1384 | a.tv_sec -= b.tv_sec; | ||
1404 | 1385 | a.tv_usec -= b.tv_usec; | ||
1405 | 1386 | if (a.tv_usec < 0) { | ||
1406 | 1387 | a.tv_sec-- ; | ||
1407 | 1388 | a.tv_usec += ONE_MILLION; | ||
1408 | 1389 | } | ||
1409 | 1390 | return a; | ||
1410 | 1391 | } | ||
1411 | 1392 | #undef ONE_MILLION | ||
1412 | 1393 | |||
1413 | 1394 | /*! \brief glibc puts a lock inside random(3), so that the results are thread-safe. | ||
1414 | 1395 | * BSD libc (and others) do not. */ | ||
1415 | 1396 | |||
1416 | 1397 | #ifndef linux | ||
1417 | 1398 | AST_MUTEX_DEFINE_STATIC(randomlock); | ||
1418 | 1399 | #endif | ||
1419 | 1400 | |||
1420 | 1401 | long int ast_random(void) | ||
1421 | 1402 | { | ||
1422 | 1403 | long int res; | ||
1423 | 1404 | #ifdef HAVE_DEV_URANDOM | ||
1424 | 1405 | if (dev_urandom_fd >= 0) { | ||
1425 | 1406 | int read_res = read(dev_urandom_fd, &res, sizeof(res)); | ||
1426 | 1407 | if (read_res > 0) { | ||
1427 | 1408 | long int rm = RAND_MAX; | ||
1428 | 1409 | res = res < 0 ? ~res : res; | ||
1429 | 1410 | rm++; | ||
1430 | 1411 | return res % rm; | ||
1431 | 1412 | } | ||
1432 | 1413 | } | ||
1433 | 1414 | #endif | ||
1434 | 1415 | #ifdef linux | ||
1435 | 1416 | res = random(); | ||
1436 | 1417 | #else | ||
1437 | 1418 | ast_mutex_lock(&randomlock); | ||
1438 | 1419 | res = random(); | ||
1439 | 1420 | ast_mutex_unlock(&randomlock); | ||
1440 | 1421 | #endif | ||
1441 | 1422 | return res; | ||
1442 | 1423 | } | ||
1443 | 1424 | |||
1444 | 1425 | char *ast_process_quotes_and_slashes(char *start, char find, char replace_with) | ||
1445 | 1426 | { | ||
1446 | 1427 | char *dataPut = start; | ||
1447 | 1428 | int inEscape = 0; | ||
1448 | 1429 | int inQuotes = 0; | ||
1449 | 1430 | |||
1450 | 1431 | for (; *start; start++) { | ||
1451 | 1432 | if (inEscape) { | ||
1452 | 1433 | *dataPut++ = *start; /* Always goes verbatim */ | ||
1453 | 1434 | inEscape = 0; | ||
1454 | 1435 | } else { | ||
1455 | 1436 | if (*start == '\\') { | ||
1456 | 1437 | inEscape = 1; /* Do not copy \ into the data */ | ||
1457 | 1438 | } else if (*start == '\'') { | ||
1458 | 1439 | inQuotes = 1 - inQuotes; /* Do not copy ' into the data */ | ||
1459 | 1440 | } else { | ||
1460 | 1441 | /* Replace , with |, unless in quotes */ | ||
1461 | 1442 | *dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start); | ||
1462 | 1443 | } | ||
1463 | 1444 | } | ||
1464 | 1445 | } | ||
1465 | 1446 | if (start != dataPut) | ||
1466 | 1447 | *dataPut = 0; | ||
1467 | 1448 | return dataPut; | ||
1468 | 1449 | } | ||
1469 | 1450 | |||
1470 | 1451 | void ast_join(char *s, size_t len, char * const w[]) | ||
1471 | 1452 | { | ||
1472 | 1453 | int x, ofs = 0; | ||
1473 | 1454 | const char *src; | ||
1474 | 1455 | |||
1475 | 1456 | /* Join words into a string */ | ||
1476 | 1457 | if (!s) | ||
1477 | 1458 | return; | ||
1478 | 1459 | for (x = 0; ofs < len && w[x]; x++) { | ||
1479 | 1460 | if (x > 0) | ||
1480 | 1461 | s[ofs++] = ' '; | ||
1481 | 1462 | for (src = w[x]; *src && ofs < len; src++) | ||
1482 | 1463 | s[ofs++] = *src; | ||
1483 | 1464 | } | ||
1484 | 1465 | if (ofs == len) | ||
1485 | 1466 | ofs--; | ||
1486 | 1467 | s[ofs] = '\0'; | ||
1487 | 1468 | } | ||
1488 | 1469 | |||
1489 | 1470 | /* | ||
1490 | 1471 | * stringfields support routines. | ||
1491 | 1472 | */ | ||
1492 | 1473 | |||
1493 | 1474 | const char __ast_string_field_empty[] = ""; /*!< the empty string */ | ||
1494 | 1475 | |||
1495 | 1476 | /*! \brief add a new block to the pool. | ||
1496 | 1477 | * We can only allocate from the topmost pool, so the | ||
1497 | 1478 | * fields in *mgr reflect the size of that only. | ||
1498 | 1479 | */ | ||
1499 | 1480 | static int add_string_pool(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, | ||
1500 | 1481 | size_t size, const char *file, int lineno, const char *func) | ||
1501 | 1482 | { | ||
1502 | 1483 | struct ast_string_field_pool *pool; | ||
1503 | 1484 | |||
1504 | 1485 | #if defined(__AST_DEBUG_MALLOC) | ||
1505 | 1486 | if (!(pool = __ast_calloc(1, sizeof(*pool) + size, file, lineno, func))) { | ||
1506 | 1487 | return -1; | ||
1507 | 1488 | } | ||
1508 | 1489 | #else | ||
1509 | 1490 | if (!(pool = ast_calloc(1, sizeof(*pool) + size))) { | ||
1510 | 1491 | return -1; | ||
1511 | 1492 | } | ||
1512 | 1493 | #endif | ||
1513 | 1494 | |||
1514 | 1495 | pool->prev = *pool_head; | ||
1515 | 1496 | *pool_head = pool; | ||
1516 | 1497 | mgr->size = size; | ||
1517 | 1498 | mgr->used = 0; | ||
1518 | 1499 | mgr->last_alloc = NULL; | ||
1519 | 1500 | |||
1520 | 1501 | return 0; | ||
1521 | 1502 | } | ||
1522 | 1503 | |||
1523 | 1504 | /* | ||
1524 | 1505 | * This is an internal API, code should not use it directly. | ||
1525 | 1506 | * It initializes all fields as empty, then uses 'size' for 3 functions: | ||
1526 | 1507 | * size > 0 means initialize the pool list with a pool of given size. | ||
1527 | 1508 | * This must be called right after allocating the object. | ||
1528 | 1509 | * size = 0 means release all pools except the most recent one. | ||
1529 | 1510 | * This is useful to e.g. reset an object to the initial value. | ||
1530 | 1511 | * size < 0 means release all pools. | ||
1531 | 1512 | * This must be done before destroying the object. | ||
1532 | 1513 | */ | ||
1533 | 1514 | int __ast_string_field_init(struct ast_string_field_mgr *mgr, struct ast_string_field_pool **pool_head, | ||
1534 | 1515 | int needed, const char *file, int lineno, const char *func) | ||
1535 | 1516 | { | ||
1536 | 1517 | const char **p = (const char **) pool_head + 1; | ||
1537 | 1518 | struct ast_string_field_pool *cur = NULL; | ||
1538 | 1519 | struct ast_string_field_pool *preserve = NULL; | ||
1539 | 1520 | |||
1540 | 1521 | /* clear fields - this is always necessary */ | ||
1541 | 1522 | while ((struct ast_string_field_mgr *) p != mgr) | ||
1542 | 1523 | *p++ = __ast_string_field_empty; | ||
1543 | 1524 | mgr->last_alloc = NULL; | ||
1544 | 1525 | #if defined(__AST_DEBUG_MALLOC) | ||
1545 | 1526 | mgr->owner_file = file; | ||
1546 | 1527 | mgr->owner_func = func; | ||
1547 | 1528 | mgr->owner_line = lineno; | ||
1548 | 1529 | #endif | ||
1549 | 1530 | if (needed > 0) { /* allocate the initial pool */ | ||
1550 | 1531 | *pool_head = NULL; | ||
1551 | 1532 | return add_string_pool(mgr, pool_head, needed, file, lineno, func); | ||
1552 | 1533 | } | ||
1553 | 1534 | if (needed < 0) { /* reset all pools */ | ||
1554 | 1535 | if (*pool_head == NULL) { | ||
1555 | 1536 | ast_log(LOG_WARNING, "trying to reset empty pool\n"); | ||
1556 | 1537 | return -1; | ||
1557 | 1538 | } | ||
1558 | 1539 | cur = *pool_head; | ||
1559 | 1540 | } else { /* preserve the last pool */ | ||
1560 | 1541 | if (*pool_head == NULL) { | ||
1561 | 1542 | ast_log(LOG_WARNING, "trying to reset empty pool\n"); | ||
1562 | 1543 | return -1; | ||
1563 | 1544 | } | ||
1564 | 1545 | mgr->used = 0; | ||
1565 | 1546 | preserve = *pool_head; | ||
1566 | 1547 | cur = preserve->prev; | ||
1567 | 1548 | } | ||
1568 | 1549 | |||
1569 | 1550 | if (preserve) { | ||
1570 | 1551 | preserve->prev = NULL; | ||
1571 | 1552 | } | ||
1572 | 1553 | |||
1573 | 1554 | while (cur) { | ||
1574 | 1555 | struct ast_string_field_pool *prev = cur->prev; | ||
1575 | 1556 | |||
1576 | 1557 | if (cur != preserve) { | ||
1577 | 1558 | ast_free(cur); | ||
1578 | 1559 | } | ||
1579 | 1560 | cur = prev; | ||
1580 | 1561 | } | ||
1581 | 1562 | |||
1582 | 1563 | *pool_head = preserve; | ||
1583 | 1564 | |||
1584 | 1565 | return 0; | ||
1585 | 1566 | } | ||
1586 | 1567 | |||
1587 | 1568 | ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr, | ||
1588 | 1569 | struct ast_string_field_pool **pool_head, size_t needed) | ||
1589 | 1570 | { | ||
1590 | 1571 | char *result = NULL; | ||
1591 | 1572 | size_t space = mgr->size - mgr->used; | ||
1592 | 1573 | |||
1593 | 1574 | if (__builtin_expect(needed > space, 0)) { | ||
1594 | 1575 | size_t new_size = mgr->size * 2; | ||
1595 | 1576 | |||
1596 | 1577 | while (new_size < needed) | ||
1597 | 1578 | new_size *= 2; | ||
1598 | 1579 | |||
1599 | 1580 | #if defined(__AST_DEBUG_MALLOC) | ||
1600 | 1581 | if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func)) | ||
1601 | 1582 | return NULL; | ||
1602 | 1583 | #else | ||
1603 | 1584 | if (add_string_pool(mgr, pool_head, new_size, __FILE__, __LINE__, __FUNCTION__)) | ||
1604 | 1585 | return NULL; | ||
1605 | 1586 | #endif | ||
1606 | 1587 | } | ||
1607 | 1588 | |||
1608 | 1589 | result = (*pool_head)->base + mgr->used; | ||
1609 | 1590 | mgr->used += needed; | ||
1610 | 1591 | mgr->last_alloc = result; | ||
1611 | 1592 | return result; | ||
1612 | 1593 | } | ||
1613 | 1594 | |||
1614 | 1595 | int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr, size_t needed, | ||
1615 | 1596 | const ast_string_field *ptr) | ||
1616 | 1597 | { | ||
1617 | 1598 | int grow = needed - (strlen(*ptr) + 1); | ||
1618 | 1599 | size_t space = mgr->size - mgr->used; | ||
1619 | 1600 | |||
1620 | 1601 | if (grow <= 0) { | ||
1621 | 1602 | return 0; | ||
1622 | 1603 | } | ||
1623 | 1604 | |||
1624 | 1605 | if (*ptr != mgr->last_alloc) { | ||
1625 | 1606 | return 1; | ||
1626 | 1607 | } | ||
1627 | 1608 | |||
1628 | 1609 | if (space < grow) { | ||
1629 | 1610 | return 1; | ||
1630 | 1611 | } | ||
1631 | 1612 | |||
1632 | 1613 | mgr->used += grow; | ||
1633 | 1614 | |||
1634 | 1615 | return 0; | ||
1635 | 1616 | } | ||
1636 | 1617 | |||
1637 | 1618 | void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr, | ||
1638 | 1619 | struct ast_string_field_pool **pool_head, | ||
1639 | 1620 | ast_string_field *ptr, const char *format, va_list ap1, va_list ap2) | ||
1640 | 1621 | { | ||
1641 | 1622 | size_t needed; | ||
1642 | 1623 | size_t available; | ||
1643 | 1624 | size_t space = mgr->size - mgr->used; | ||
1644 | 1625 | char *target; | ||
1645 | 1626 | |||
1646 | 1627 | /* if the field already has space allocated, try to reuse it; | ||
1647 | 1628 | otherwise, use the empty space at the end of the current | ||
1648 | 1629 | pool | ||
1649 | 1630 | */ | ||
1650 | 1631 | if ((*ptr)[0] != '\0') { | ||
1651 | 1632 | target = (char *) *ptr; | ||
1652 | 1633 | available = strlen(target) + 1; | ||
1653 | 1634 | } else { | ||
1654 | 1635 | target = (*pool_head)->base + mgr->used; | ||
1655 | 1636 | available = space; | ||
1656 | 1637 | } | ||
1657 | 1638 | |||
1658 | 1639 | needed = vsnprintf(target, available, format, ap1) + 1; | ||
1659 | 1640 | |||
1660 | 1641 | va_end(ap1); | ||
1661 | 1642 | |||
1662 | 1643 | if (needed > available) { | ||
1663 | 1644 | /* if the space needed can be satisfied by using the current | ||
1664 | 1645 | pool (which could only occur if we tried to use the field's | ||
1665 | 1646 | allocated space and failed), then use that space; otherwise | ||
1666 | 1647 | allocate a new pool | ||
1667 | 1648 | */ | ||
1668 | 1649 | if (needed > space) { | ||
1669 | 1650 | size_t new_size = mgr->size * 2; | ||
1670 | 1651 | |||
1671 | 1652 | while (new_size < needed) | ||
1672 | 1653 | new_size *= 2; | ||
1673 | 1654 | |||
1674 | 1655 | #if defined(__AST_DEBUG_MALLOC) | ||
1675 | 1656 | if (add_string_pool(mgr, pool_head, new_size, mgr->owner_file, mgr->owner_line, mgr->owner_func)) | ||
1676 | 1657 | return; | ||
1677 | 1658 | #else | ||
1678 | 1659 | if (add_string_pool(mgr, pool_head, new_size, NULL, 0, NULL)) | ||
1679 | 1660 | return; | ||
1680 | 1661 | #endif | ||
1681 | 1662 | } | ||
1682 | 1663 | |||
1683 | 1664 | target = (*pool_head)->base + mgr->used; | ||
1684 | 1665 | vsprintf(target, format, ap2); | ||
1685 | 1666 | } | ||
1686 | 1667 | |||
1687 | 1668 | if (*ptr != target) { | ||
1688 | 1669 | mgr->last_alloc = *ptr = target; | ||
1689 | 1670 | mgr->used += needed; | ||
1690 | 1671 | } | ||
1691 | 1672 | } | ||
1692 | 1673 | |||
1693 | 1674 | void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr, | ||
1694 | 1675 | struct ast_string_field_pool **pool_head, | ||
1695 | 1676 | ast_string_field *ptr, const char *format, ...) | ||
1696 | 1677 | { | ||
1697 | 1678 | va_list ap1, ap2; | ||
1698 | 1679 | |||
1699 | 1680 | va_start(ap1, format); | ||
1700 | 1681 | va_start(ap2, format); /* va_copy does not exist on FreeBSD */ | ||
1701 | 1682 | |||
1702 | 1683 | __ast_string_field_ptr_build_va(mgr, pool_head, ptr, format, ap1, ap2); | ||
1703 | 1684 | |||
1704 | 1685 | va_end(ap1); | ||
1705 | 1686 | va_end(ap2); | ||
1706 | 1687 | } | ||
1707 | 1688 | /* end of stringfields support */ | ||
1708 | 1689 | |||
1709 | 1690 | AST_MUTEX_DEFINE_STATIC(fetchadd_m); /* used for all fetc&add ops */ | ||
1710 | 1691 | |||
1711 | 1692 | int ast_atomic_fetchadd_int_slow(volatile int *p, int v) | ||
1712 | 1693 | { | ||
1713 | 1694 | int ret; | ||
1714 | 1695 | ast_mutex_lock(&fetchadd_m); | ||
1715 | 1696 | ret = *p; | ||
1716 | 1697 | *p += v; | ||
1717 | 1698 | ast_mutex_unlock(&fetchadd_m); | ||
1718 | 1699 | return ret; | ||
1719 | 1700 | } | ||
1720 | 1701 | |||
1721 | 1702 | /*! \brief | ||
1722 | 1703 | * get values from config variables. | ||
1723 | 1704 | */ | ||
1724 | 1705 | int ast_get_timeval(const char *src, struct timeval *dst, struct timeval _default, int *consumed) | ||
1725 | 1706 | { | ||
1726 | 1707 | long double dtv = 0.0; | ||
1727 | 1708 | int scanned; | ||
1728 | 1709 | |||
1729 | 1710 | if (dst == NULL) | ||
1730 | 1711 | return -1; | ||
1731 | 1712 | |||
1732 | 1713 | *dst = _default; | ||
1733 | 1714 | |||
1734 | 1715 | if (ast_strlen_zero(src)) | ||
1735 | 1716 | return -1; | ||
1736 | 1717 | |||
1737 | 1718 | /* only integer at the moment, but one day we could accept more formats */ | ||
1738 | 1719 | if (sscanf(src, "%30Lf%n", &dtv, &scanned) > 0) { | ||
1739 | 1720 | dst->tv_sec = dtv; | ||
1740 | 1721 | dst->tv_usec = (dtv - dst->tv_sec) * 1000000.0; | ||
1741 | 1722 | if (consumed) | ||
1742 | 1723 | *consumed = scanned; | ||
1743 | 1724 | return 0; | ||
1744 | 1725 | } else | ||
1745 | 1726 | return -1; | ||
1746 | 1727 | } | ||
1747 | 1728 | |||
1748 | 1729 | /*! \brief | ||
1749 | 1730 | * get values from config variables. | ||
1750 | 1731 | */ | ||
1751 | 1732 | int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed) | ||
1752 | 1733 | { | ||
1753 | 1734 | long t; | ||
1754 | 1735 | int scanned; | ||
1755 | 1736 | |||
1756 | 1737 | if (dst == NULL) | ||
1757 | 1738 | return -1; | ||
1758 | 1739 | |||
1759 | 1740 | *dst = _default; | ||
1760 | 1741 | |||
1761 | 1742 | if (ast_strlen_zero(src)) | ||
1762 | 1743 | return -1; | ||
1763 | 1744 | |||
1764 | 1745 | /* only integer at the moment, but one day we could accept more formats */ | ||
1765 | 1746 | if (sscanf(src, "%30ld%n", &t, &scanned) == 1) { | ||
1766 | 1747 | *dst = t; | ||
1767 | 1748 | if (consumed) | ||
1768 | 1749 | *consumed = scanned; | ||
1769 | 1750 | return 0; | ||
1770 | 1751 | } else | ||
1771 | 1752 | return -1; | ||
1772 | 1753 | } | ||
1773 | 1754 | |||
1774 | 1755 | void ast_enable_packet_fragmentation(int sock) | ||
1775 | 1756 | { | ||
1776 | 1757 | #if defined(HAVE_IP_MTU_DISCOVER) | ||
1777 | 1758 | int val = IP_PMTUDISC_DONT; | ||
1778 | 1759 | |||
1779 | 1760 | if (setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &val, sizeof(val))) | ||
1780 | 1761 | ast_log(LOG_WARNING, "Unable to disable PMTU discovery. Large UDP packets may fail to be delivered when sent from this socket.\n"); | ||
1781 | 1762 | #endif /* HAVE_IP_MTU_DISCOVER */ | ||
1782 | 1763 | } | ||
1783 | 1764 | |||
1784 | 1765 | int ast_mkdir(const char *path, int mode) | ||
1785 | 1766 | { | ||
1786 | 1767 | char *ptr; | ||
1787 | 1768 | int len = strlen(path), count = 0, x, piececount = 0; | ||
1788 | 1769 | char *tmp = ast_strdupa(path); | ||
1789 | 1770 | char **pieces; | ||
1790 | 1771 | char *fullpath = alloca(len + 1); | ||
1791 | 1772 | int res = 0; | ||
1792 | 1773 | |||
1793 | 1774 | for (ptr = tmp; *ptr; ptr++) { | ||
1794 | 1775 | if (*ptr == '/') | ||
1795 | 1776 | count++; | ||
1796 | 1777 | } | ||
1797 | 1778 | |||
1798 | 1779 | /* Count the components to the directory path */ | ||
1799 | 1780 | pieces = alloca(count * sizeof(*pieces)); | ||
1800 | 1781 | for (ptr = tmp; *ptr; ptr++) { | ||
1801 | 1782 | if (*ptr == '/') { | ||
1802 | 1783 | *ptr = '\0'; | ||
1803 | 1784 | pieces[piececount++] = ptr + 1; | ||
1804 | 1785 | } | ||
1805 | 1786 | } | ||
1806 | 1787 | |||
1807 | 1788 | *fullpath = '\0'; | ||
1808 | 1789 | for (x = 0; x < piececount; x++) { | ||
1809 | 1790 | /* This looks funky, but the buffer is always ideally-sized, so it's fine. */ | ||
1810 | 1791 | strcat(fullpath, "/"); | ||
1811 | 1792 | strcat(fullpath, pieces[x]); | ||
1812 | 1793 | res = mkdir(fullpath, mode); | ||
1813 | 1794 | if (res && errno != EEXIST) | ||
1814 | 1795 | return errno; | ||
1815 | 1796 | } | ||
1816 | 1797 | return 0; | ||
1817 | 1798 | } | ||
1818 | 1799 | |||
1819 | 1800 | int ast_utils_init(void) | ||
1820 | 1801 | { | ||
1821 | 1802 | #ifdef HAVE_DEV_URANDOM | ||
1822 | 1803 | dev_urandom_fd = open("/dev/urandom", O_RDONLY); | ||
1823 | 1804 | #endif | ||
1824 | 1805 | base64_init(); | ||
1825 | 1806 | #ifdef DEBUG_THREADS | ||
1826 | 1807 | #if !defined(LOW_MEMORY) | ||
1827 | 1808 | ast_cli_register_multiple(utils_cli, ARRAY_LEN(utils_cli)); | ||
1828 | 1809 | #endif | ||
1829 | 1810 | #endif | ||
1830 | 1811 | return 0; | ||
1831 | 1812 | } | ||
1832 | 1813 | |||
1833 | 1814 | #ifndef __AST_DEBUG_MALLOC | ||
1834 | 1815 | int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...) | ||
1835 | 1816 | { | ||
1836 | 1817 | int res; | ||
1837 | 1818 | va_list ap; | ||
1838 | 1819 | |||
1839 | 1820 | va_start(ap, fmt); | ||
1840 | 1821 | if ((res = vasprintf(ret, fmt, ap)) == -1) { | ||
1841 | 1822 | MALLOC_FAILURE_MSG; | ||
1842 | 1823 | } | ||
1843 | 1824 | va_end(ap); | ||
1844 | 1825 | |||
1845 | 1826 | return res; | ||
1846 | 1827 | } | ||
1847 | 1828 | #endif | ||
1848 | 0 | 1829 | ||
1849 | === modified file '.pc/applied-patches' | |||
1850 | --- .pc/applied-patches 2010-03-02 10:00:03 +0000 | |||
1851 | +++ .pc/applied-patches 2011-01-20 23:43:55 +0000 | |||
1852 | @@ -11,3 +11,6 @@ | |||
1853 | 11 | dahdi_ptmp_nt | 11 | dahdi_ptmp_nt |
1854 | 12 | dahdi_pri_debug_spannums | 12 | dahdi_pri_debug_spannums |
1855 | 13 | sound_files | 13 | sound_files |
1856 | 14 | dnsmgr-A-SRV-handling | ||
1857 | 15 | unattended_fix | ||
1858 | 16 | AST-2011-001-1.6.2 | ||
1859 | 14 | 17 | ||
1860 | === added directory '.pc/dnsmgr-A-SRV-handling' | |||
1861 | === added directory '.pc/dnsmgr-A-SRV-handling/include' | |||
1862 | === added directory '.pc/dnsmgr-A-SRV-handling/include/asterisk' | |||
1863 | === added file '.pc/dnsmgr-A-SRV-handling/include/asterisk/dnsmgr.h' | |||
1864 | --- .pc/dnsmgr-A-SRV-handling/include/asterisk/dnsmgr.h 1970-01-01 00:00:00 +0000 | |||
1865 | +++ .pc/dnsmgr-A-SRV-handling/include/asterisk/dnsmgr.h 2011-01-20 23:43:55 +0000 | |||
1866 | @@ -0,0 +1,105 @@ | |||
1867 | 1 | /* | ||
1868 | 2 | * Asterisk -- An open source telephony toolkit. | ||
1869 | 3 | * | ||
1870 | 4 | * Copyright (C) 1999 - 2005, Digium, Inc. | ||
1871 | 5 | * | ||
1872 | 6 | * Kevin P. Fleming <kpfleming@digium.com> | ||
1873 | 7 | * | ||
1874 | 8 | * See http://www.asterisk.org for more information about | ||
1875 | 9 | * the Asterisk project. Please do not directly contact | ||
1876 | 10 | * any of the maintainers of this project for assistance; | ||
1877 | 11 | * the project provides a web site, mailing lists and IRC | ||
1878 | 12 | * channels for your use. | ||
1879 | 13 | * | ||
1880 | 14 | * This program is free software, distributed under the terms of | ||
1881 | 15 | * the GNU General Public License Version 2. See the LICENSE file | ||
1882 | 16 | * at the top of the source tree. | ||
1883 | 17 | */ | ||
1884 | 18 | |||
1885 | 19 | /*! \file | ||
1886 | 20 | * \brief Background DNS update manager | ||
1887 | 21 | */ | ||
1888 | 22 | |||
1889 | 23 | #ifndef _ASTERISK_DNSMGR_H | ||
1890 | 24 | #define _ASTERISK_DNSMGR_H | ||
1891 | 25 | |||
1892 | 26 | #if defined(__cplusplus) || defined(c_plusplus) | ||
1893 | 27 | extern "C" { | ||
1894 | 28 | #endif | ||
1895 | 29 | |||
1896 | 30 | #include "asterisk/network.h" | ||
1897 | 31 | #include "asterisk/srv.h" | ||
1898 | 32 | |||
1899 | 33 | /*! | ||
1900 | 34 | * \brief A DNS manager entry | ||
1901 | 35 | * | ||
1902 | 36 | * This is an opaque type. | ||
1903 | 37 | */ | ||
1904 | 38 | struct ast_dnsmgr_entry; | ||
1905 | 39 | |||
1906 | 40 | /*! | ||
1907 | 41 | * \brief Allocate a new DNS manager entry | ||
1908 | 42 | * | ||
1909 | 43 | * \param name the hostname | ||
1910 | 44 | * \param result where the DNS manager should store the IP address as it refreshes it. | ||
1911 | 45 | * it. | ||
1912 | 46 | * | ||
1913 | 47 | * This function allocates a new DNS manager entry object, and fills it with the | ||
1914 | 48 | * provided hostname and IP address. This function does not force an initial lookup | ||
1915 | 49 | * of the IP address. So, generally, this should be used when the initial address | ||
1916 | 50 | * is already known. | ||
1917 | 51 | * | ||
1918 | 52 | * \return a DNS manager entry | ||
1919 | 53 | * \version 1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number | ||
1920 | 54 | */ | ||
1921 | 55 | struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service); | ||
1922 | 56 | |||
1923 | 57 | /*! | ||
1924 | 58 | * \brief Free a DNS manager entry | ||
1925 | 59 | * | ||
1926 | 60 | * \param entry the DNS manager entry to free | ||
1927 | 61 | * | ||
1928 | 62 | * \return nothing | ||
1929 | 63 | */ | ||
1930 | 64 | void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry); | ||
1931 | 65 | |||
1932 | 66 | /*! | ||
1933 | 67 | * \brief Allocate and initialize a DNS manager entry | ||
1934 | 68 | * | ||
1935 | 69 | * \param name the hostname | ||
1936 | 70 | * \param result where to store the IP address as the DNS manager refreshes it | ||
1937 | 71 | * \param dnsmgr Where to store the allocate DNS manager entry | ||
1938 | 72 | * | ||
1939 | 73 | * This function allocates a new DNS manager entry object, and fills it with | ||
1940 | 74 | * the provided hostname and IP address. This function _does_ force an initial | ||
1941 | 75 | * lookup, so it may block for some period of time. | ||
1942 | 76 | * | ||
1943 | 77 | * \retval 0 success | ||
1944 | 78 | * \retval non-zero failure | ||
1945 | 79 | * \version 1.6.1 result changed from struct in_addr to struct aockaddr_in to store port number | ||
1946 | 80 | */ | ||
1947 | 81 | int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service); | ||
1948 | 82 | |||
1949 | 83 | /*! | ||
1950 | 84 | * \brief Force a refresh of a dnsmgr entry | ||
1951 | 85 | * | ||
1952 | 86 | * \retval non-zero if the result is different than the previous result | ||
1953 | 87 | * \retval zero if the result is the same as the previous result | ||
1954 | 88 | */ | ||
1955 | 89 | int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry); | ||
1956 | 90 | |||
1957 | 91 | /*! | ||
1958 | 92 | * \brief Check is see if a dnsmgr entry has changed | ||
1959 | 93 | * | ||
1960 | 94 | * \retval non-zero if the dnsmgr entry has changed since the last call to | ||
1961 | 95 | * this function | ||
1962 | 96 | * \retval zero if the dnsmgr entry has not changed since the last call to | ||
1963 | 97 | * this function | ||
1964 | 98 | */ | ||
1965 | 99 | int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry); | ||
1966 | 100 | |||
1967 | 101 | #if defined(__cplusplus) || defined(c_plusplus) | ||
1968 | 102 | } | ||
1969 | 103 | #endif /* c_plusplus */ | ||
1970 | 104 | |||
1971 | 105 | #endif /* ASTERISK_DNSMGR_H */ | ||
1972 | 0 | 106 | ||
1973 | === added directory '.pc/dnsmgr-A-SRV-handling/main' | |||
1974 | === added file '.pc/dnsmgr-A-SRV-handling/main/acl.c' | |||
1975 | --- .pc/dnsmgr-A-SRV-handling/main/acl.c 1970-01-01 00:00:00 +0000 | |||
1976 | +++ .pc/dnsmgr-A-SRV-handling/main/acl.c 2011-01-20 23:43:55 +0000 | |||
1977 | @@ -0,0 +1,541 @@ | |||
1978 | 1 | /* | ||
1979 | 2 | * Asterisk -- An open source telephony toolkit. | ||
1980 | 3 | * | ||
1981 | 4 | * Copyright (C) 1999 - 2006, Digium, Inc. | ||
1982 | 5 | * | ||
1983 | 6 | * Mark Spencer <markster@digium.com> | ||
1984 | 7 | * | ||
1985 | 8 | * See http://www.asterisk.org for more information about | ||
1986 | 9 | * the Asterisk project. Please do not directly contact | ||
1987 | 10 | * any of the maintainers of this project for assistance; | ||
1988 | 11 | * the project provides a web site, mailing lists and IRC | ||
1989 | 12 | * channels for your use. | ||
1990 | 13 | * | ||
1991 | 14 | * This program is free software, distributed under the terms of | ||
1992 | 15 | * the GNU General Public License Version 2. See the LICENSE file | ||
1993 | 16 | * at the top of the source tree. | ||
1994 | 17 | */ | ||
1995 | 18 | |||
1996 | 19 | /*! \file | ||
1997 | 20 | * | ||
1998 | 21 | * \brief Various sorts of access control | ||
1999 | 22 | * | ||
2000 | 23 | * \author Mark Spencer <markster@digium.com> | ||
2001 | 24 | */ | ||
2002 | 25 | |||
2003 | 26 | #include "asterisk.h" | ||
2004 | 27 | |||
2005 | 28 | ASTERISK_FILE_VERSION(__FILE__, "$Revision: 248849 $") | ||
2006 | 29 | |||
2007 | 30 | #include "asterisk/network.h" | ||
2008 | 31 | |||
2009 | 32 | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__Darwin__) | ||
2010 | 33 | #include <fcntl.h> | ||
2011 | 34 | #include <net/route.h> | ||
2012 | 35 | #endif | ||
2013 | 36 | |||
2014 | 37 | #if defined(SOLARIS) | ||
2015 | 38 | #include <sys/sockio.h> | ||
2016 | 39 | #include <net/if.h> | ||
2017 | 40 | #elif defined(HAVE_GETIFADDRS) | ||
2018 | 41 | #include <ifaddrs.h> | ||
2019 | 42 | #endif | ||
2020 | 43 | |||
2021 | 44 | #include "asterisk/acl.h" | ||
2022 | 45 | #include "asterisk/channel.h" | ||
2023 | 46 | #include "asterisk/utils.h" | ||
2024 | 47 | #include "asterisk/lock.h" | ||
2025 | 48 | #include "asterisk/srv.h" | ||
2026 | 49 | |||
2027 | 50 | #if (!defined(SOLARIS) && !defined(HAVE_GETIFADDRS)) | ||
2028 | 51 | static int get_local_address(struct in_addr *ourip) | ||
2029 | 52 | { | ||
2030 | 53 | return -1; | ||
2031 | 54 | } | ||
2032 | 55 | #else | ||
2033 | 56 | static void score_address(const struct sockaddr_in *sin, struct in_addr *best_addr, int *best_score) | ||
2034 | 57 | { | ||
2035 | 58 | const char *address; | ||
2036 | 59 | int score; | ||
2037 | 60 | |||
2038 | 61 | address = ast_inet_ntoa(sin->sin_addr); | ||
2039 | 62 | |||
2040 | 63 | /* RFC 1700 alias for the local network */ | ||
2041 | 64 | if (address[0] == '0') { | ||
2042 | 65 | score = -25; | ||
2043 | 66 | /* RFC 1700 localnet */ | ||
2044 | 67 | } else if (strncmp(address, "127", 3) == 0) { | ||
2045 | 68 | score = -20; | ||
2046 | 69 | /* RFC 1918 non-public address space */ | ||
2047 | 70 | } else if (strncmp(address, "10.", 3) == 0) { | ||
2048 | 71 | score = -5; | ||
2049 | 72 | /* RFC 1918 non-public address space */ | ||
2050 | 73 | } else if (strncmp(address, "172", 3) == 0) { | ||
2051 | 74 | /* 172.16.0.0 - 172.19.255.255, but not 172.160.0.0 - 172.169.255.255 */ | ||
2052 | 75 | if (address[4] == '1' && address[5] >= '6' && address[6] == '.') { | ||
2053 | 76 | score = -5; | ||
2054 | 77 | /* 172.20.0.0 - 172.29.255.255, but not 172.200.0.0 - 172.255.255.255 nor 172.2.0.0 - 172.2.255.255 */ | ||
2055 | 78 | } else if (address[4] == '2' && address[6] == '.') { | ||
2056 | 79 | score = -5; | ||
2057 | 80 | /* 172.30.0.0 - 172.31.255.255 */ | ||
2058 | 81 | } else if (address[4] == '3' && address[5] <= '1') { | ||
2059 | 82 | score = -5; | ||
2060 | 83 | /* All other 172 addresses are public */ | ||
2061 | 84 | } else { | ||
2062 | 85 | score = 0; | ||
2063 | 86 | } | ||
2064 | 87 | /* RFC 2544 Benchmark test range (198.18.0.0 - 198.19.255.255, but not 198.180.0.0 - 198.199.255.255) */ | ||
2065 | 88 | } else if (strncmp(address, "198.1", 5) == 0 && address[5] >= '8' && address[6] == '.') { | ||
2066 | 89 | score = -10; | ||
2067 | 90 | /* RFC 1918 non-public address space */ | ||
2068 | 91 | } else if (strncmp(address, "192.168", 7) == 0) { | ||
2069 | 92 | score = -5; | ||
2070 | 93 | /* RFC 3330 Zeroconf network */ | ||
2071 | 94 | } else if (strncmp(address, "169.254", 7) == 0) { | ||
2072 | 95 | /*!\note Better score than a test network, but not quite as good as RFC 1918 | ||
2073 | 96 | * address space. The reason is that some Linux distributions automatically | ||
2074 | 97 | * configure a Zeroconf address before trying DHCP, so we want to prefer a | ||
2075 | 98 | * DHCP lease to a Zeroconf address. | ||
2076 | 99 | */ | ||
2077 | 100 | score = -10; | ||
2078 | 101 | /* RFC 3330 Test network */ | ||
2079 | 102 | } else if (strncmp(address, "192.0.2.", 8) == 0) { | ||
2080 | 103 | score = -15; | ||
2081 | 104 | /* Every other address should be publically routable */ | ||
2082 | 105 | } else { | ||
2083 | 106 | score = 0; | ||
2084 | 107 | } | ||
2085 | 108 | |||
2086 | 109 | if (score > *best_score) { | ||
2087 | 110 | *best_score = score; | ||
2088 | 111 | memcpy(best_addr, &sin->sin_addr, sizeof(*best_addr)); | ||
2089 | 112 | } | ||
2090 | 113 | } | ||
2091 | 114 | |||
2092 | 115 | static int get_local_address(struct in_addr *ourip) | ||
2093 | 116 | { | ||
2094 | 117 | int s, res = -1; | ||
2095 | 118 | #ifdef SOLARIS | ||
2096 | 119 | struct lifreq *ifr = NULL; | ||
2097 | 120 | struct lifnum ifn; | ||
2098 | 121 | struct lifconf ifc; | ||
2099 | 122 | struct sockaddr_in *sa; | ||
2100 | 123 | char *buf = NULL; | ||
2101 | 124 | int bufsz, x; | ||
2102 | 125 | #endif /* SOLARIS */ | ||
2103 | 126 | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) | ||
2104 | 127 | struct ifaddrs *ifap, *ifaphead; | ||
2105 | 128 | int rtnerr; | ||
2106 | 129 | const struct sockaddr_in *sin; | ||
2107 | 130 | #endif /* BSD_OR_LINUX */ | ||
2108 | 131 | struct in_addr best_addr; | ||
2109 | 132 | int best_score = -100; | ||
2110 | 133 | memset(&best_addr, 0, sizeof(best_addr)); | ||
2111 | 134 | |||
2112 | 135 | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) | ||
2113 | 136 | rtnerr = getifaddrs(&ifaphead); | ||
2114 | 137 | if (rtnerr) { | ||
2115 | 138 | perror(NULL); | ||
2116 | 139 | return -1; | ||
2117 | 140 | } | ||
2118 | 141 | #endif /* BSD_OR_LINUX */ | ||
2119 | 142 | |||
2120 | 143 | s = socket(AF_INET, SOCK_STREAM, 0); | ||
2121 | 144 | |||
2122 | 145 | if (s > 0) { | ||
2123 | 146 | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) | ||
2124 | 147 | for (ifap = ifaphead; ifap; ifap = ifap->ifa_next) { | ||
2125 | 148 | |||
2126 | 149 | if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_INET) { | ||
2127 | 150 | sin = (const struct sockaddr_in *) ifap->ifa_addr; | ||
2128 | 151 | score_address(sin, &best_addr, &best_score); | ||
2129 | 152 | res = 0; | ||
2130 | 153 | |||
2131 | 154 | if (best_score == 0) { | ||
2132 | 155 | break; | ||
2133 | 156 | } | ||
2134 | 157 | } | ||
2135 | 158 | } | ||
2136 | 159 | #endif /* BSD_OR_LINUX */ | ||
2137 | 160 | |||
2138 | 161 | /* There is no reason whatsoever that this shouldn't work on Linux or BSD also. */ | ||
2139 | 162 | #ifdef SOLARIS | ||
2140 | 163 | /* Get a count of interfaces on the machine */ | ||
2141 | 164 | ifn.lifn_family = AF_INET; | ||
2142 | 165 | ifn.lifn_flags = 0; | ||
2143 | 166 | ifn.lifn_count = 0; | ||
2144 | 167 | if (ioctl(s, SIOCGLIFNUM, &ifn) < 0) { | ||
2145 | 168 | close(s); | ||
2146 | 169 | return -1; | ||
2147 | 170 | } | ||
2148 | 171 | |||
2149 | 172 | bufsz = ifn.lifn_count * sizeof(struct lifreq); | ||
2150 | 173 | if (!(buf = malloc(bufsz))) { | ||
2151 | 174 | close(s); | ||
2152 | 175 | return -1; | ||
2153 | 176 | } | ||
2154 | 177 | memset(buf, 0, bufsz); | ||
2155 | 178 | |||
2156 | 179 | /* Get a list of interfaces on the machine */ | ||
2157 | 180 | ifc.lifc_len = bufsz; | ||
2158 | 181 | ifc.lifc_buf = buf; | ||
2159 | 182 | ifc.lifc_family = AF_INET; | ||
2160 | 183 | ifc.lifc_flags = 0; | ||
2161 | 184 | if (ioctl(s, SIOCGLIFCONF, &ifc) < 0) { | ||
2162 | 185 | close(s); | ||
2163 | 186 | free(buf); | ||
2164 | 187 | return -1; | ||
2165 | 188 | } | ||
2166 | 189 | |||
2167 | 190 | for (ifr = ifc.lifc_req, x = 0; x < ifn.lifn_count; ifr++, x++) { | ||
2168 | 191 | sa = (struct sockaddr_in *)&(ifr->lifr_addr); | ||
2169 | 192 | score_address(sa, &best_addr, &best_score); | ||
2170 | 193 | res = 0; | ||
2171 | 194 | |||
2172 | 195 | if (best_score == 0) { | ||
2173 | 196 | break; | ||
2174 | 197 | } | ||
2175 | 198 | } | ||
2176 | 199 | |||
2177 | 200 | free(buf); | ||
2178 | 201 | #endif /* SOLARIS */ | ||
2179 | 202 | |||
2180 | 203 | close(s); | ||
2181 | 204 | } | ||
2182 | 205 | #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__linux__) || defined(__Darwin__) | ||
2183 | 206 | freeifaddrs(ifaphead); | ||
2184 | 207 | #endif /* BSD_OR_LINUX */ | ||
2185 | 208 | |||
2186 | 209 | if (res == 0 && ourip) { | ||
2187 | 210 | memcpy(ourip, &best_addr, sizeof(*ourip)); | ||
2188 | 211 | } | ||
2189 | 212 | return res; | ||
2190 | 213 | } | ||
2191 | 214 | #endif /* HAVE_GETIFADDRS */ | ||
2192 | 215 | |||
2193 | 216 | /* Free HA structure */ | ||
2194 | 217 | void ast_free_ha(struct ast_ha *ha) | ||
2195 | 218 | { | ||
2196 | 219 | struct ast_ha *hal; | ||
2197 | 220 | while (ha) { | ||
2198 | 221 | hal = ha; | ||
2199 | 222 | ha = ha->next; | ||
2200 | 223 | ast_free(hal); | ||
2201 | 224 | } | ||
2202 | 225 | } | ||
2203 | 226 | |||
2204 | 227 | /* Copy HA structure */ | ||
2205 | 228 | void ast_copy_ha(const struct ast_ha *from, struct ast_ha *to) | ||
2206 | 229 | { | ||
2207 | 230 | memcpy(&to->netaddr, &from->netaddr, sizeof(from->netaddr)); | ||
2208 | 231 | memcpy(&to->netmask, &from->netmask, sizeof(from->netmask)); | ||
2209 | 232 | to->sense = from->sense; | ||
2210 | 233 | } | ||
2211 | 234 | |||
2212 | 235 | /* Create duplicate of ha structure */ | ||
2213 | 236 | static struct ast_ha *ast_duplicate_ha(struct ast_ha *original) | ||
2214 | 237 | { | ||
2215 | 238 | struct ast_ha *new_ha; | ||
2216 | 239 | |||
2217 | 240 | if ((new_ha = ast_malloc(sizeof(*new_ha)))) { | ||
2218 | 241 | /* Copy from original to new object */ | ||
2219 | 242 | ast_copy_ha(original, new_ha); | ||
2220 | 243 | } | ||
2221 | 244 | |||
2222 | 245 | return new_ha; | ||
2223 | 246 | } | ||
2224 | 247 | |||
2225 | 248 | /* Create duplicate HA link list */ | ||
2226 | 249 | /* Used in chan_sip2 templates */ | ||
2227 | 250 | struct ast_ha *ast_duplicate_ha_list(struct ast_ha *original) | ||
2228 | 251 | { | ||
2229 | 252 | struct ast_ha *start = original; | ||
2230 | 253 | struct ast_ha *ret = NULL; | ||
2231 | 254 | struct ast_ha *current, *prev = NULL; | ||
2232 | 255 | |||
2233 | 256 | while (start) { | ||
2234 | 257 | current = ast_duplicate_ha(start); /* Create copy of this object */ | ||
2235 | 258 | if (prev) { | ||
2236 | 259 | prev->next = current; /* Link previous to this object */ | ||
2237 | 260 | } | ||
2238 | 261 | |||
2239 | 262 | if (!ret) { | ||
2240 | 263 | ret = current; /* Save starting point */ | ||
2241 | 264 | } | ||
2242 | 265 | |||
2243 | 266 | start = start->next; /* Go to next object */ | ||
2244 | 267 | prev = current; /* Save pointer to this object */ | ||
2245 | 268 | } | ||
2246 | 269 | return ret; /* Return start of list */ | ||
2247 | 270 | } | ||
2248 | 271 | |||
2249 | 272 | struct ast_ha *ast_append_ha(const char *sense, const char *stuff, struct ast_ha *path, int *error) | ||
2250 | 273 | { | ||
2251 | 274 | struct ast_ha *ha; | ||
2252 | 275 | char *nm; | ||
2253 | 276 | struct ast_ha *prev = NULL; | ||
2254 | 277 | struct ast_ha *ret; | ||
2255 | 278 | int x; | ||
2256 | 279 | char *tmp = ast_strdupa(stuff); | ||
2257 | 280 | |||
2258 | 281 | ret = path; | ||
2259 | 282 | while (path) { | ||
2260 | 283 | prev = path; | ||
2261 | 284 | path = path->next; | ||
2262 | 285 | } | ||
2263 | 286 | |||
2264 | 287 | if (!(ha = ast_malloc(sizeof(*ha)))) { | ||
2265 | 288 | return ret; | ||
2266 | 289 | } | ||
2267 | 290 | |||
2268 | 291 | if (!(nm = strchr(tmp, '/'))) { | ||
2269 | 292 | /* assume /32. Yes, htonl does not do anything for this particular mask | ||
2270 | 293 | but we better use it to show we remember about byte order */ | ||
2271 | 294 | ha->netmask.s_addr = htonl(0xFFFFFFFF); | ||
2272 | 295 | } else { | ||
2273 | 296 | *nm = '\0'; | ||
2274 | 297 | nm++; | ||
2275 | 298 | |||
2276 | 299 | if (!strchr(nm, '.')) { | ||
2277 | 300 | if ((sscanf(nm, "%30d", &x) == 1) && (x >= 0) && (x <= 32)) { | ||
2278 | 301 | if (x == 0) { | ||
2279 | 302 | /* This is special-cased to prevent unpredictable | ||
2280 | 303 | * behavior of shifting left 32 bits | ||
2281 | 304 | */ | ||
2282 | 305 | ha->netmask.s_addr = 0; | ||
2283 | 306 | } else { | ||
2284 | 307 | ha->netmask.s_addr = htonl(0xFFFFFFFF << (32 - x)); | ||
2285 | 308 | } | ||
2286 | 309 | } else { | ||
2287 | 310 | ast_log(LOG_WARNING, "Invalid CIDR in %s\n", stuff); | ||
2288 | 311 | ast_free(ha); | ||
2289 | 312 | if (error) { | ||
2290 | 313 | *error = 1; | ||
2291 | 314 | } | ||
2292 | 315 | return ret; | ||
2293 | 316 | } | ||
2294 | 317 | } else if (!inet_aton(nm, &ha->netmask)) { | ||
2295 | 318 | ast_log(LOG_WARNING, "Invalid mask in %s\n", stuff); | ||
2296 | 319 | ast_free(ha); | ||
2297 | 320 | if (error) { | ||
2298 | 321 | *error = 1; | ||
2299 | 322 | } | ||
2300 | 323 | return ret; | ||
2301 | 324 | } | ||
2302 | 325 | } | ||
2303 | 326 | |||
2304 | 327 | if (!inet_aton(tmp, &ha->netaddr)) { | ||
2305 | 328 | ast_log(LOG_WARNING, "Invalid IP address in %s\n", stuff); | ||
2306 | 329 | ast_free(ha); | ||
2307 | 330 | if (error) { | ||
2308 | 331 | *error = 1; | ||
2309 | 332 | } | ||
2310 | 333 | return ret; | ||
2311 | 334 | } | ||
2312 | 335 | |||
2313 | 336 | ha->netaddr.s_addr &= ha->netmask.s_addr; | ||
2314 | 337 | |||
2315 | 338 | ha->sense = strncasecmp(sense, "p", 1) ? AST_SENSE_DENY : AST_SENSE_ALLOW; | ||
2316 | 339 | |||
2317 | 340 | ha->next = NULL; | ||
2318 | 341 | if (prev) { | ||
2319 | 342 | prev->next = ha; | ||
2320 | 343 | } else { | ||
2321 | 344 | ret = ha; | ||
2322 | 345 | } | ||
2323 | 346 | |||
2324 | 347 | ast_debug(1, "%s/%s sense %d appended to acl for peer\n", ast_strdupa(ast_inet_ntoa(ha->netaddr)), ast_strdupa(ast_inet_ntoa(ha->netmask)), ha->sense); | ||
2325 | 348 | |||
2326 | 349 | return ret; | ||
2327 | 350 | } | ||
2328 | 351 | |||
2329 | 352 | int ast_apply_ha(struct ast_ha *ha, struct sockaddr_in *sin) | ||
2330 | 353 | { | ||
2331 | 354 | /* Start optimistic */ | ||
2332 | 355 | int res = AST_SENSE_ALLOW; | ||
2333 | 356 | while (ha) { | ||
2334 | 357 | #if 0 /* debugging code */ | ||
2335 | 358 | char iabuf[INET_ADDRSTRLEN]; | ||
2336 | 359 | char iabuf2[INET_ADDRSTRLEN]; | ||
2337 | 360 | /* DEBUG */ | ||
2338 | 361 | ast_copy_string(iabuf, ast_inet_ntoa(sin->sin_addr), sizeof(iabuf)); | ||
2339 | 362 | ast_copy_string(iabuf2, ast_inet_ntoa(ha->netaddr), sizeof(iabuf2)); | ||
2340 | 363 | ast_debug(1, "##### Testing %s with %s\n", iabuf, iabuf2); | ||
2341 | 364 | #endif | ||
2342 | 365 | /* For each rule, if this address and the netmask = the net address | ||
2343 | 366 | apply the current rule */ | ||
2344 | 367 | if ((sin->sin_addr.s_addr & ha->netmask.s_addr) == ha->netaddr.s_addr) { | ||
2345 | 368 | res = ha->sense; | ||
2346 | 369 | } | ||
2347 | 370 | ha = ha->next; | ||
2348 | 371 | } | ||
2349 | 372 | return res; | ||
2350 | 373 | } | ||
2351 | 374 | |||
2352 | 375 | int ast_get_ip_or_srv(struct sockaddr_in *sin, const char *value, const char *service) | ||
2353 | 376 | { | ||
2354 | 377 | struct hostent *hp; | ||
2355 | 378 | struct ast_hostent ahp; | ||
2356 | 379 | char srv[256]; | ||
2357 | 380 | char host[256]; | ||
2358 | 381 | int tportno = ntohs(sin->sin_port); | ||
2359 | 382 | if (service) { | ||
2360 | 383 | snprintf(srv, sizeof(srv), "%s.%s", service, value); | ||
2361 | 384 | if (ast_get_srv(NULL, host, sizeof(host), &tportno, srv) > 0) { | ||
2362 | 385 | sin->sin_port = htons(tportno); | ||
2363 | 386 | value = host; | ||
2364 | 387 | } | ||
2365 | 388 | } | ||
2366 | 389 | if ((hp = ast_gethostbyname(value, &ahp))) { | ||
2367 | 390 | memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr)); | ||
2368 | 391 | } else { | ||
2369 | 392 | ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value); | ||
2370 | 393 | return -1; | ||
2371 | 394 | } | ||
2372 | 395 | return 0; | ||
2373 | 396 | } | ||
2374 | 397 | |||
2375 | 398 | struct dscp_codepoint { | ||
2376 | 399 | char *name; | ||
2377 | 400 | unsigned int space; | ||
2378 | 401 | }; | ||
2379 | 402 | |||
2380 | 403 | /* IANA registered DSCP codepoints */ | ||
2381 | 404 | |||
2382 | 405 | static const struct dscp_codepoint dscp_pool1[] = { | ||
2383 | 406 | { "CS0", 0x00 }, | ||
2384 | 407 | { "CS1", 0x08 }, | ||
2385 | 408 | { "CS2", 0x10 }, | ||
2386 | 409 | { "CS3", 0x18 }, | ||
2387 | 410 | { "CS4", 0x20 }, | ||
2388 | 411 | { "CS5", 0x28 }, | ||
2389 | 412 | { "CS6", 0x30 }, | ||
2390 | 413 | { "CS7", 0x38 }, | ||
2391 | 414 | { "AF11", 0x0A }, | ||
2392 | 415 | { "AF12", 0x0C }, | ||
2393 | 416 | { "AF13", 0x0E }, | ||
2394 | 417 | { "AF21", 0x12 }, | ||
2395 | 418 | { "AF22", 0x14 }, | ||
2396 | 419 | { "AF23", 0x16 }, | ||
2397 | 420 | { "AF31", 0x1A }, | ||
2398 | 421 | { "AF32", 0x1C }, | ||
2399 | 422 | { "AF33", 0x1E }, | ||
2400 | 423 | { "AF41", 0x22 }, | ||
2401 | 424 | { "AF42", 0x24 }, | ||
2402 | 425 | { "AF43", 0x26 }, | ||
2403 | 426 | { "EF", 0x2E }, | ||
2404 | 427 | }; | ||
2405 | 428 | |||
2406 | 429 | int ast_str2cos(const char *value, unsigned int *cos) | ||
2407 | 430 | { | ||
2408 | 431 | int fval; | ||
2409 | 432 | |||
2410 | 433 | if (sscanf(value, "%30d", &fval) == 1) { | ||
2411 | 434 | if (fval < 8) { | ||
2412 | 435 | *cos = fval; | ||
2413 | 436 | return 0; | ||
2414 | 437 | } | ||
2415 | 438 | } | ||
2416 | 439 | |||
2417 | 440 | return -1; | ||
2418 | 441 | } | ||
2419 | 442 | |||
2420 | 443 | int ast_str2tos(const char *value, unsigned int *tos) | ||
2421 | 444 | { | ||
2422 | 445 | int fval; | ||
2423 | 446 | unsigned int x; | ||
2424 | 447 | |||
2425 | 448 | if (sscanf(value, "%30i", &fval) == 1) { | ||
2426 | 449 | *tos = fval & 0xFF; | ||
2427 | 450 | return 0; | ||
2428 | 451 | } | ||
2429 | 452 | |||
2430 | 453 | for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) { | ||
2431 | 454 | if (!strcasecmp(value, dscp_pool1[x].name)) { | ||
2432 | 455 | *tos = dscp_pool1[x].space << 2; | ||
2433 | 456 | return 0; | ||
2434 | 457 | } | ||
2435 | 458 | } | ||
2436 | 459 | |||
2437 | 460 | return -1; | ||
2438 | 461 | } | ||
2439 | 462 | |||
2440 | 463 | const char *ast_tos2str(unsigned int tos) | ||
2441 | 464 | { | ||
2442 | 465 | unsigned int x; | ||
2443 | 466 | |||
2444 | 467 | for (x = 0; x < ARRAY_LEN(dscp_pool1); x++) { | ||
2445 | 468 | if (dscp_pool1[x].space == (tos >> 2)) { | ||
2446 | 469 | return dscp_pool1[x].name; | ||
2447 | 470 | } | ||
2448 | 471 | } | ||
2449 | 472 | |||
2450 | 473 | return "unknown"; | ||
2451 | 474 | } | ||
2452 | 475 | |||
2453 | 476 | int ast_get_ip(struct sockaddr_in *sin, const char *value) | ||
2454 | 477 | { | ||
2455 | 478 | return ast_get_ip_or_srv(sin, value, NULL); | ||
2456 | 479 | } | ||
2457 | 480 | |||
2458 | 481 | int ast_ouraddrfor(struct in_addr *them, struct in_addr *us) | ||
2459 | 482 | { | ||
2460 | 483 | int s; | ||
2461 | 484 | struct sockaddr_in sin; | ||
2462 | 485 | socklen_t slen; | ||
2463 | 486 | |||
2464 | 487 | if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { | ||
2465 | 488 | ast_log(LOG_ERROR, "Cannot create socket\n"); | ||
2466 | 489 | return -1; | ||
2467 | 490 | } | ||
2468 | 491 | sin.sin_family = AF_INET; | ||
2469 | 492 | sin.sin_port = htons(5060); | ||
2470 | 493 | sin.sin_addr = *them; | ||
2471 | 494 | if (connect(s, (struct sockaddr *)&sin, sizeof(sin))) { | ||
2472 | 495 | ast_log(LOG_WARNING, "Cannot connect\n"); | ||
2473 | 496 | close(s); | ||
2474 | 497 | return -1; | ||
2475 | 498 | } | ||
2476 | 499 | slen = sizeof(sin); | ||
2477 | 500 | if (getsockname(s, (struct sockaddr *)&sin, &slen)) { | ||
2478 | 501 | ast_log(LOG_WARNING, "Cannot get socket name\n"); | ||
2479 | 502 | close(s); | ||
2480 | 503 | return -1; | ||
2481 | 504 | } | ||
2482 | 505 | close(s); | ||
2483 | 506 | ast_debug(3, "Found IP address for this socket\n"); | ||
2484 | 507 | *us = sin.sin_addr; | ||
2485 | 508 | return 0; | ||
2486 | 509 | } | ||
2487 | 510 | |||
2488 | 511 | int ast_find_ourip(struct in_addr *ourip, struct sockaddr_in bindaddr) | ||
2489 | 512 | { | ||
2490 | 513 | char ourhost[MAXHOSTNAMELEN] = ""; | ||
2491 | 514 | struct ast_hostent ahp; | ||
2492 | 515 | struct hostent *hp; | ||
2493 | 516 | struct in_addr saddr; | ||
2494 | 517 | |||
2495 | 518 | /* just use the bind address if it is nonzero */ | ||
2496 | 519 | if (ntohl(bindaddr.sin_addr.s_addr)) { | ||
2497 | 520 | memcpy(ourip, &bindaddr.sin_addr, sizeof(*ourip)); | ||
2498 | 521 | ast_debug(3, "Attached to given IP address\n"); | ||
2499 | 522 | return 0; | ||
2500 | 523 | } | ||
2501 | 524 | /* try to use our hostname */ | ||
2502 | 525 | if (gethostname(ourhost, sizeof(ourhost) - 1)) { | ||
2503 | 526 | ast_log(LOG_WARNING, "Unable to get hostname\n"); | ||
2504 | 527 | } else { | ||
2505 | 528 | if ((hp = ast_gethostbyname(ourhost, &ahp))) { | ||
2506 | 529 | memcpy(ourip, hp->h_addr, sizeof(*ourip)); | ||
2507 | 530 | ast_debug(3, "Found one IP address based on local hostname %s.\n", ourhost); | ||
2508 | 531 | return 0; | ||
2509 | 532 | } | ||
2510 | 533 | } | ||
2511 | 534 | ast_debug(3, "Trying to check A.ROOT-SERVERS.NET and get our IP address for that connection\n"); | ||
2512 | 535 | /* A.ROOT-SERVERS.NET. */ | ||
2513 | 536 | if (inet_aton("198.41.0.4", &saddr) && !ast_ouraddrfor(&saddr, ourip)) { | ||
2514 | 537 | return 0; | ||
2515 | 538 | } | ||
2516 | 539 | return get_local_address(ourip); | ||
2517 | 540 | } | ||
2518 | 541 | |||
2519 | 0 | 542 | ||
2520 | === added file '.pc/dnsmgr-A-SRV-handling/main/dnsmgr.c' | |||
2521 | --- .pc/dnsmgr-A-SRV-handling/main/dnsmgr.c 1970-01-01 00:00:00 +0000 | |||
2522 | +++ .pc/dnsmgr-A-SRV-handling/main/dnsmgr.c 2011-01-20 23:43:55 +0000 | |||
2523 | @@ -0,0 +1,439 @@ | |||
2524 | 1 | /* | ||
2525 | 2 | * Asterisk -- An open source telephony toolkit. | ||
2526 | 3 | * | ||
2527 | 4 | * Copyright (C) 2005-2006, Kevin P. Fleming | ||
2528 | 5 | * | ||
2529 | 6 | * Kevin P. Fleming <kpfleming@digium.com> | ||
2530 | 7 | * | ||
2531 | 8 | * See http://www.asterisk.org for more information about | ||
2532 | 9 | * the Asterisk project. Please do not directly contact | ||
2533 | 10 | * any of the maintainers of this project for assistance; | ||
2534 | 11 | * the project provides a web site, mailing lists and IRC | ||
2535 | 12 | * channels for your use. | ||
2536 | 13 | * | ||
2537 | 14 | * This program is free software, distributed under the terms of | ||
2538 | 15 | * the GNU General Public License Version 2. See the LICENSE file | ||
2539 | 16 | * at the top of the source tree. | ||
2540 | 17 | */ | ||
2541 | 18 | |||
2542 | 19 | /*! \file | ||
2543 | 20 | * | ||
2544 | 21 | * \brief Background DNS update manager | ||
2545 | 22 | * | ||
2546 | 23 | * \author Kevin P. Fleming <kpfleming@digium.com> | ||
2547 | 24 | * | ||
2548 | 25 | * \bug There is a minor race condition. In the event that an IP address | ||
2549 | 26 | * of a dnsmgr managed host changes, there is the potential for the consumer | ||
2550 | 27 | * of that address to access the in_addr data at the same time that the dnsmgr | ||
2551 | 28 | * thread is in the middle of updating it to the new address. | ||
2552 | 29 | */ | ||
2553 | 30 | |||
2554 | 31 | #include "asterisk.h" | ||
2555 | 32 | |||
2556 | 33 | ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211580 $") | ||
2557 | 34 | |||
2558 | 35 | #include "asterisk/_private.h" | ||
2559 | 36 | #include <regex.h> | ||
2560 | 37 | #include <signal.h> | ||
2561 | 38 | |||
2562 | 39 | #include "asterisk/dnsmgr.h" | ||
2563 | 40 | #include "asterisk/linkedlists.h" | ||
2564 | 41 | #include "asterisk/utils.h" | ||
2565 | 42 | #include "asterisk/config.h" | ||
2566 | 43 | #include "asterisk/sched.h" | ||
2567 | 44 | #include "asterisk/cli.h" | ||
2568 | 45 | #include "asterisk/manager.h" | ||
2569 | 46 | #include "asterisk/acl.h" | ||
2570 | 47 | |||
2571 | 48 | static struct sched_context *sched; | ||
2572 | 49 | static int refresh_sched = -1; | ||
2573 | 50 | static pthread_t refresh_thread = AST_PTHREADT_NULL; | ||
2574 | 51 | |||
2575 | 52 | struct ast_dnsmgr_entry { | ||
2576 | 53 | /*! where we will store the resulting IP address and port number */ | ||
2577 | 54 | struct sockaddr_in *result; | ||
2578 | 55 | /*! the last result, used to check if address/port has changed */ | ||
2579 | 56 | struct sockaddr_in last; | ||
2580 | 57 | /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */ | ||
2581 | 58 | char *service; | ||
2582 | 59 | /*! Set to 1 if the entry changes */ | ||
2583 | 60 | unsigned int changed:1; | ||
2584 | 61 | ast_mutex_t lock; | ||
2585 | 62 | AST_RWLIST_ENTRY(ast_dnsmgr_entry) list; | ||
2586 | 63 | /*! just 1 here, but we use calloc to allocate the correct size */ | ||
2587 | 64 | char name[1]; | ||
2588 | 65 | }; | ||
2589 | 66 | |||
2590 | 67 | static AST_RWLIST_HEAD_STATIC(entry_list, ast_dnsmgr_entry); | ||
2591 | 68 | |||
2592 | 69 | AST_MUTEX_DEFINE_STATIC(refresh_lock); | ||
2593 | 70 | |||
2594 | 71 | #define REFRESH_DEFAULT 300 | ||
2595 | 72 | |||
2596 | 73 | static int enabled; | ||
2597 | 74 | static int refresh_interval; | ||
2598 | 75 | |||
2599 | 76 | struct refresh_info { | ||
2600 | 77 | struct entry_list *entries; | ||
2601 | 78 | int verbose; | ||
2602 | 79 | unsigned int regex_present:1; | ||
2603 | 80 | regex_t filter; | ||
2604 | 81 | }; | ||
2605 | 82 | |||
2606 | 83 | static struct refresh_info master_refresh_info = { | ||
2607 | 84 | .entries = &entry_list, | ||
2608 | 85 | .verbose = 0, | ||
2609 | 86 | }; | ||
2610 | 87 | |||
2611 | 88 | struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service) | ||
2612 | 89 | { | ||
2613 | 90 | struct ast_dnsmgr_entry *entry; | ||
2614 | 91 | int total_size = sizeof(*entry) + strlen(name) + (service ? strlen(service) + 1 : 0); | ||
2615 | 92 | |||
2616 | 93 | if (!result || ast_strlen_zero(name) || !(entry = ast_calloc(1, total_size))) | ||
2617 | 94 | return NULL; | ||
2618 | 95 | |||
2619 | 96 | entry->result = result; | ||
2620 | 97 | ast_mutex_init(&entry->lock); | ||
2621 | 98 | strcpy(entry->name, name); | ||
2622 | 99 | memcpy(&entry->last, result, sizeof(entry->last)); | ||
2623 | 100 | if (service) { | ||
2624 | 101 | entry->service = ((char *) entry) + sizeof(*entry) + strlen(name); | ||
2625 | 102 | strcpy(entry->service, service); | ||
2626 | 103 | } | ||
2627 | 104 | |||
2628 | 105 | AST_RWLIST_WRLOCK(&entry_list); | ||
2629 | 106 | AST_RWLIST_INSERT_HEAD(&entry_list, entry, list); | ||
2630 | 107 | AST_RWLIST_UNLOCK(&entry_list); | ||
2631 | 108 | |||
2632 | 109 | return entry; | ||
2633 | 110 | } | ||
2634 | 111 | |||
2635 | 112 | void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry) | ||
2636 | 113 | { | ||
2637 | 114 | if (!entry) | ||
2638 | 115 | return; | ||
2639 | 116 | |||
2640 | 117 | AST_RWLIST_WRLOCK(&entry_list); | ||
2641 | 118 | AST_RWLIST_REMOVE(&entry_list, entry, list); | ||
2642 | 119 | AST_RWLIST_UNLOCK(&entry_list); | ||
2643 | 120 | ast_verb(4, "removing dns manager for '%s'\n", entry->name); | ||
2644 | 121 | |||
2645 | 122 | ast_mutex_destroy(&entry->lock); | ||
2646 | 123 | ast_free(entry); | ||
2647 | 124 | } | ||
2648 | 125 | |||
2649 | 126 | int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service) | ||
2650 | 127 | { | ||
2651 | 128 | if (ast_strlen_zero(name) || !result || !dnsmgr) | ||
2652 | 129 | return -1; | ||
2653 | 130 | |||
2654 | 131 | if (*dnsmgr && !strcasecmp((*dnsmgr)->name, name)) | ||
2655 | 132 | return 0; | ||
2656 | 133 | |||
2657 | 134 | /* if it's actually an IP address and not a name, | ||
2658 | 135 | there's no need for a managed lookup */ | ||
2659 | 136 | if (inet_aton(name, &result->sin_addr)) | ||
2660 | 137 | return 0; | ||
2661 | 138 | |||
2662 | 139 | ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name); | ||
2663 | 140 | |||
2664 | 141 | /* do a lookup now but add a manager so it will automagically get updated in the background */ | ||
2665 | 142 | ast_get_ip_or_srv(result, name, service); | ||
2666 | 143 | |||
2667 | 144 | /* if dnsmgr is not enable don't bother adding an entry */ | ||
2668 | 145 | if (!enabled) | ||
2669 | 146 | return 0; | ||
2670 | 147 | |||
2671 | 148 | ast_verb(3, "adding dns manager for '%s'\n", name); | ||
2672 | 149 | *dnsmgr = ast_dnsmgr_get(name, result, service); | ||
2673 | 150 | return !*dnsmgr; | ||
2674 | 151 | } | ||
2675 | 152 | |||
2676 | 153 | /* | ||
2677 | 154 | * Refresh a dnsmgr entry | ||
2678 | 155 | */ | ||
2679 | 156 | static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose) | ||
2680 | 157 | { | ||
2681 | 158 | char iabuf[INET_ADDRSTRLEN]; | ||
2682 | 159 | char iabuf2[INET_ADDRSTRLEN]; | ||
2683 | 160 | struct sockaddr_in tmp; | ||
2684 | 161 | int changed = 0; | ||
2685 | 162 | |||
2686 | 163 | ast_mutex_lock(&entry->lock); | ||
2687 | 164 | if (verbose) | ||
2688 | 165 | ast_verb(3, "refreshing '%s'\n", entry->name); | ||
2689 | 166 | |||
2690 | 167 | tmp.sin_port = entry->last.sin_port; | ||
2691 | 168 | |||
2692 | 169 | if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, &entry->last)) { | ||
2693 | 170 | ast_copy_string(iabuf, ast_inet_ntoa(entry->last.sin_addr), sizeof(iabuf)); | ||
2694 | 171 | ast_copy_string(iabuf2, ast_inet_ntoa(tmp.sin_addr), sizeof(iabuf2)); | ||
2695 | 172 | ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s:%d to %s:%d\n", | ||
2696 | 173 | entry->name, iabuf, ntohs(entry->last.sin_port), iabuf2, ntohs(tmp.sin_port)); | ||
2697 | 174 | *entry->result = tmp; | ||
2698 | 175 | entry->last = tmp; | ||
2699 | 176 | changed = entry->changed = 1; | ||
2700 | 177 | } | ||
2701 | 178 | |||
2702 | 179 | ast_mutex_unlock(&entry->lock); | ||
2703 | 180 | return changed; | ||
2704 | 181 | } | ||
2705 | 182 | |||
2706 | 183 | int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry) | ||
2707 | 184 | { | ||
2708 | 185 | return dnsmgr_refresh(entry, 0); | ||
2709 | 186 | } | ||
2710 | 187 | |||
2711 | 188 | /* | ||
2712 | 189 | * Check if dnsmgr entry has changed from since last call to this function | ||
2713 | 190 | */ | ||
2714 | 191 | int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry) | ||
2715 | 192 | { | ||
2716 | 193 | int changed; | ||
2717 | 194 | |||
2718 | 195 | ast_mutex_lock(&entry->lock); | ||
2719 | 196 | |||
2720 | 197 | changed = entry->changed; | ||
2721 | 198 | entry->changed = 0; | ||
2722 | 199 | |||
2723 | 200 | ast_mutex_unlock(&entry->lock); | ||
2724 | 201 | |||
2725 | 202 | return changed; | ||
2726 | 203 | } | ||
2727 | 204 | |||
2728 | 205 | static void *do_refresh(void *data) | ||
2729 | 206 | { | ||
2730 | 207 | for (;;) { | ||
2731 | 208 | pthread_testcancel(); | ||
2732 | 209 | usleep((ast_sched_wait(sched)*1000)); | ||
2733 | 210 | pthread_testcancel(); | ||
2734 | 211 | ast_sched_runq(sched); | ||
2735 | 212 | } | ||
2736 | 213 | return NULL; | ||
2737 | 214 | } | ||
2738 | 215 | |||
2739 | 216 | static int refresh_list(const void *data) | ||
2740 | 217 | { | ||
2741 | 218 | struct refresh_info *info = (struct refresh_info *)data; | ||
2742 | 219 | struct ast_dnsmgr_entry *entry; | ||
2743 | 220 | |||
2744 | 221 | /* if a refresh or reload is already in progress, exit now */ | ||
2745 | 222 | if (ast_mutex_trylock(&refresh_lock)) { | ||
2746 | 223 | if (info->verbose) | ||
2747 | 224 | ast_log(LOG_WARNING, "DNS Manager refresh already in progress.\n"); | ||
2748 | 225 | return -1; | ||
2749 | 226 | } | ||
2750 | 227 | |||
2751 | 228 | ast_verb(3, "Refreshing DNS lookups.\n"); | ||
2752 | 229 | AST_RWLIST_RDLOCK(info->entries); | ||
2753 | 230 | AST_RWLIST_TRAVERSE(info->entries, entry, list) { | ||
2754 | 231 | if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0)) | ||
2755 | 232 | continue; | ||
2756 | 233 | |||
2757 | 234 | dnsmgr_refresh(entry, info->verbose); | ||
2758 | 235 | } | ||
2759 | 236 | AST_RWLIST_UNLOCK(info->entries); | ||
2760 | 237 | |||
2761 | 238 | ast_mutex_unlock(&refresh_lock); | ||
2762 | 239 | |||
2763 | 240 | /* automatically reschedule based on the interval */ | ||
2764 | 241 | return refresh_interval * 1000; | ||
2765 | 242 | } | ||
2766 | 243 | |||
2767 | 244 | void dnsmgr_start_refresh(void) | ||
2768 | 245 | { | ||
2769 | 246 | if (refresh_sched > -1) { | ||
2770 | 247 | AST_SCHED_DEL(sched, refresh_sched); | ||
2771 | 248 | refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1); | ||
2772 | 249 | } | ||
2773 | 250 | } | ||
2774 | 251 | |||
2775 | 252 | static int do_reload(int loading); | ||
2776 | 253 | |||
2777 | 254 | static char *handle_cli_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||
2778 | 255 | { | ||
2779 | 256 | switch (cmd) { | ||
2780 | 257 | case CLI_INIT: | ||
2781 | 258 | e->command = "dnsmgr reload"; | ||
2782 | 259 | e->usage = | ||
2783 | 260 | "Usage: dnsmgr reload\n" | ||
2784 | 261 | " Reloads the DNS manager configuration.\n"; | ||
2785 | 262 | return NULL; | ||
2786 | 263 | case CLI_GENERATE: | ||
2787 | 264 | return NULL; | ||
2788 | 265 | } | ||
2789 | 266 | if (a->argc > 2) | ||
2790 | 267 | return CLI_SHOWUSAGE; | ||
2791 | 268 | |||
2792 | 269 | do_reload(0); | ||
2793 | 270 | return CLI_SUCCESS; | ||
2794 | 271 | } | ||
2795 | 272 | |||
2796 | 273 | static char *handle_cli_refresh(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||
2797 | 274 | { | ||
2798 | 275 | struct refresh_info info = { | ||
2799 | 276 | .entries = &entry_list, | ||
2800 | 277 | .verbose = 1, | ||
2801 | 278 | }; | ||
2802 | 279 | switch (cmd) { | ||
2803 | 280 | case CLI_INIT: | ||
2804 | 281 | e->command = "dnsmgr refresh"; | ||
2805 | 282 | e->usage = | ||
2806 | 283 | "Usage: dnsmgr refresh [pattern]\n" | ||
2807 | 284 | " Peforms an immediate refresh of the managed DNS entries.\n" | ||
2808 | 285 | " Optional regular expression pattern is used to filter the entries to refresh.\n"; | ||
2809 | 286 | return NULL; | ||
2810 | 287 | case CLI_GENERATE: | ||
2811 | 288 | return NULL; | ||
2812 | 289 | } | ||
2813 | 290 | |||
2814 | 291 | if (!enabled) { | ||
2815 | 292 | ast_cli(a->fd, "DNS Manager is disabled.\n"); | ||
2816 | 293 | return 0; | ||
2817 | 294 | } | ||
2818 | 295 | |||
2819 | 296 | if (a->argc > 3) { | ||
2820 | 297 | return CLI_SHOWUSAGE; | ||
2821 | 298 | } | ||
2822 | 299 | |||
2823 | 300 | if (a->argc == 3) { | ||
2824 | 301 | if (regcomp(&info.filter, a->argv[2], REG_EXTENDED | REG_NOSUB)) { | ||
2825 | 302 | return CLI_SHOWUSAGE; | ||
2826 | 303 | } else { | ||
2827 | 304 | info.regex_present = 1; | ||
2828 | 305 | } | ||
2829 | 306 | } | ||
2830 | 307 | |||
2831 | 308 | refresh_list(&info); | ||
2832 | 309 | |||
2833 | 310 | if (info.regex_present) { | ||
2834 | 311 | regfree(&info.filter); | ||
2835 | 312 | } | ||
2836 | 313 | |||
2837 | 314 | return CLI_SUCCESS; | ||
2838 | 315 | } | ||
2839 | 316 | |||
2840 | 317 | static char *handle_cli_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||
2841 | 318 | { | ||
2842 | 319 | int count = 0; | ||
2843 | 320 | struct ast_dnsmgr_entry *entry; | ||
2844 | 321 | switch (cmd) { | ||
2845 | 322 | case CLI_INIT: | ||
2846 | 323 | e->command = "dnsmgr status"; | ||
2847 | 324 | e->usage = | ||
2848 | 325 | "Usage: dnsmgr status\n" | ||
2849 | 326 | " Displays the DNS manager status.\n"; | ||
2850 | 327 | return NULL; | ||
2851 | 328 | case CLI_GENERATE: | ||
2852 | 329 | return NULL; | ||
2853 | 330 | } | ||
2854 | 331 | |||
2855 | 332 | if (a->argc > 2) | ||
2856 | 333 | return CLI_SHOWUSAGE; | ||
2857 | 334 | |||
2858 | 335 | ast_cli(a->fd, "DNS Manager: %s\n", enabled ? "enabled" : "disabled"); | ||
2859 | 336 | ast_cli(a->fd, "Refresh Interval: %d seconds\n", refresh_interval); | ||
2860 | 337 | AST_RWLIST_RDLOCK(&entry_list); | ||
2861 | 338 | AST_RWLIST_TRAVERSE(&entry_list, entry, list) | ||
2862 | 339 | count++; | ||
2863 | 340 | AST_RWLIST_UNLOCK(&entry_list); | ||
2864 | 341 | ast_cli(a->fd, "Number of entries: %d\n", count); | ||
2865 | 342 | |||
2866 | 343 | return CLI_SUCCESS; | ||
2867 | 344 | } | ||
2868 | 345 | |||
2869 | 346 | static struct ast_cli_entry cli_reload = AST_CLI_DEFINE(handle_cli_reload, "Reloads the DNS manager configuration"); | ||
2870 | 347 | static struct ast_cli_entry cli_refresh = AST_CLI_DEFINE(handle_cli_refresh, "Performs an immediate refresh"); | ||
2871 | 348 | static struct ast_cli_entry cli_status = AST_CLI_DEFINE(handle_cli_status, "Display the DNS manager status"); | ||
2872 | 349 | |||
2873 | 350 | int dnsmgr_init(void) | ||
2874 | 351 | { | ||
2875 | 352 | if (!(sched = sched_context_create())) { | ||
2876 | 353 | ast_log(LOG_ERROR, "Unable to create schedule context.\n"); | ||
2877 | 354 | return -1; | ||
2878 | 355 | } | ||
2879 | 356 | ast_cli_register(&cli_reload); | ||
2880 | 357 | ast_cli_register(&cli_status); | ||
2881 | 358 | ast_cli_register(&cli_refresh); | ||
2882 | 359 | return do_reload(1); | ||
2883 | 360 | } | ||
2884 | 361 | |||
2885 | 362 | int dnsmgr_reload(void) | ||
2886 | 363 | { | ||
2887 | 364 | return do_reload(0); | ||
2888 | 365 | } | ||
2889 | 366 | |||
2890 | 367 | static int do_reload(int loading) | ||
2891 | 368 | { | ||
2892 | 369 | struct ast_config *config; | ||
2893 | 370 | struct ast_flags config_flags = { loading ? 0 : CONFIG_FLAG_FILEUNCHANGED }; | ||
2894 | 371 | const char *interval_value; | ||
2895 | 372 | const char *enabled_value; | ||
2896 | 373 | int interval; | ||
2897 | 374 | int was_enabled; | ||
2898 | 375 | int res = -1; | ||
2899 | 376 | |||
2900 | 377 | config = ast_config_load2("dnsmgr.conf", "dnsmgr", config_flags); | ||
2901 | 378 | if (config == CONFIG_STATUS_FILEMISSING || config == CONFIG_STATUS_FILEUNCHANGED || config == CONFIG_STATUS_FILEINVALID) { | ||
2902 | 379 | return 0; | ||
2903 | 380 | } | ||
2904 | 381 | |||
2905 | 382 | /* ensure that no refresh cycles run while the reload is in progress */ | ||
2906 | 383 | ast_mutex_lock(&refresh_lock); | ||
2907 | 384 | |||
2908 | 385 | /* reset defaults in preparation for reading config file */ | ||
2909 | 386 | refresh_interval = REFRESH_DEFAULT; | ||
2910 | 387 | was_enabled = enabled; | ||
2911 | 388 | enabled = 0; | ||
2912 | 389 | |||
2913 | 390 | AST_SCHED_DEL(sched, refresh_sched); | ||
2914 | 391 | |||
2915 | 392 | if (config) { | ||
2916 | 393 | if ((enabled_value = ast_variable_retrieve(config, "general", "enable"))) { | ||
2917 | 394 | enabled = ast_true(enabled_value); | ||
2918 | 395 | } | ||
2919 | 396 | if ((interval_value = ast_variable_retrieve(config, "general", "refreshinterval"))) { | ||
2920 | 397 | if (sscanf(interval_value, "%30d", &interval) < 1) | ||
2921 | 398 | ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", interval_value); | ||
2922 | 399 | else if (interval < 0) | ||
2923 | 400 | ast_log(LOG_WARNING, "Invalid refresh interval '%d' specified, using default\n", interval); | ||
2924 | 401 | else | ||
2925 | 402 | refresh_interval = interval; | ||
2926 | 403 | } | ||
2927 | 404 | ast_config_destroy(config); | ||
2928 | 405 | } | ||
2929 | 406 | |||
2930 | 407 | if (enabled && refresh_interval) | ||
2931 | 408 | ast_log(LOG_NOTICE, "Managed DNS entries will be refreshed every %d seconds.\n", refresh_interval); | ||
2932 | 409 | |||
2933 | 410 | /* if this reload enabled the manager, create the background thread | ||
2934 | 411 | if it does not exist */ | ||
2935 | 412 | if (enabled) { | ||
2936 | 413 | if (!was_enabled && (refresh_thread == AST_PTHREADT_NULL)) { | ||
2937 | 414 | if (ast_pthread_create_background(&refresh_thread, NULL, do_refresh, NULL) < 0) { | ||
2938 | 415 | ast_log(LOG_ERROR, "Unable to start refresh thread.\n"); | ||
2939 | 416 | } | ||
2940 | 417 | } | ||
2941 | 418 | /* make a background refresh happen right away */ | ||
2942 | 419 | refresh_sched = ast_sched_add_variable(sched, 100, refresh_list, &master_refresh_info, 1); | ||
2943 | 420 | res = 0; | ||
2944 | 421 | } | ||
2945 | 422 | /* if this reload disabled the manager and there is a background thread, | ||
2946 | 423 | kill it */ | ||
2947 | 424 | else if (!enabled && was_enabled && (refresh_thread != AST_PTHREADT_NULL)) { | ||
2948 | 425 | /* wake up the thread so it will exit */ | ||
2949 | 426 | pthread_cancel(refresh_thread); | ||
2950 | 427 | pthread_kill(refresh_thread, SIGURG); | ||
2951 | 428 | pthread_join(refresh_thread, NULL); | ||
2952 | 429 | refresh_thread = AST_PTHREADT_NULL; | ||
2953 | 430 | res = 0; | ||
2954 | 431 | } | ||
2955 | 432 | else | ||
2956 | 433 | res = 0; | ||
2957 | 434 | |||
2958 | 435 | ast_mutex_unlock(&refresh_lock); | ||
2959 | 436 | manager_event(EVENT_FLAG_SYSTEM, "Reload", "Module: DNSmgr\r\nStatus: %s\r/nMessage: DNSmgr reload Requested\r\n", enabled ? "Enabled" : "Disabled"); | ||
2960 | 437 | |||
2961 | 438 | return res; | ||
2962 | 439 | } | ||
2963 | 0 | 440 | ||
2964 | === added directory '.pc/unattended_fix' | |||
2965 | === added directory '.pc/unattended_fix/channels' | |||
2966 | === added file '.pc/unattended_fix/channels/chan_local.c' | |||
2967 | --- .pc/unattended_fix/channels/chan_local.c 1970-01-01 00:00:00 +0000 | |||
2968 | +++ .pc/unattended_fix/channels/chan_local.c 2011-01-20 23:43:55 +0000 | |||
2969 | @@ -0,0 +1,885 @@ | |||
2970 | 1 | /* | ||
2971 | 2 | * Asterisk -- An open source telephony toolkit. | ||
2972 | 3 | * | ||
2973 | 4 | * Copyright (C) 1999 - 2005, Digium, Inc. | ||
2974 | 5 | * | ||
2975 | 6 | * Mark Spencer <markster@digium.com> | ||
2976 | 7 | * | ||
2977 | 8 | * See http://www.asterisk.org for more information about | ||
2978 | 9 | * the Asterisk project. Please do not directly contact | ||
2979 | 10 | * any of the maintainers of this project for assistance; | ||
2980 | 11 | * the project provides a web site, mailing lists and IRC | ||
2981 | 12 | * channels for your use. | ||
2982 | 13 | * | ||
2983 | 14 | * This program is free software, distributed under the terms of | ||
2984 | 15 | * the GNU General Public License Version 2. See the LICENSE file | ||
2985 | 16 | * at the top of the source tree. | ||
2986 | 17 | */ | ||
2987 | 18 | |||
2988 | 19 | /*! \file | ||
2989 | 20 | * | ||
2990 | 21 | * \author Mark Spencer <markster@digium.com> | ||
2991 | 22 | * | ||
2992 | 23 | * \brief Local Proxy Channel | ||
2993 | 24 | * | ||
2994 | 25 | * \ingroup channel_drivers | ||
2995 | 26 | */ | ||
2996 | 27 | |||
2997 | 28 | #include "asterisk.h" | ||
2998 | 29 | |||
2999 | 30 | ASTERISK_FILE_VERSION(__FILE__, "$Revision: 237322 $") | ||
3000 | 31 | |||
3001 | 32 | #include <fcntl.h> | ||
3002 | 33 | #include <sys/signal.h> | ||
3003 | 34 | |||
3004 | 35 | #include "asterisk/lock.h" | ||
3005 | 36 | #include "asterisk/channel.h" | ||
3006 | 37 | #include "asterisk/config.h" | ||
3007 | 38 | #include "asterisk/module.h" | ||
3008 | 39 | #include "asterisk/pbx.h" | ||
3009 | 40 | #include "asterisk/sched.h" | ||
3010 | 41 | #include "asterisk/io.h" | ||
3011 | 42 | #include "asterisk/rtp.h" | ||
3012 | 43 | #include "asterisk/acl.h" | ||
3013 | 44 | #include "asterisk/callerid.h" | ||
3014 | 45 | #include "asterisk/file.h" | ||
3015 | 46 | #include "asterisk/cli.h" | ||
3016 | 47 | #include "asterisk/app.h" | ||
3017 | 48 | #include "asterisk/musiconhold.h" | ||
3018 | 49 | #include "asterisk/manager.h" | ||
3019 | 50 | #include "asterisk/stringfields.h" | ||
3020 | 51 | #include "asterisk/devicestate.h" | ||
3021 | 52 | |||
3022 | 53 | static const char tdesc[] = "Local Proxy Channel Driver"; | ||
3023 | 54 | |||
3024 | 55 | #define IS_OUTBOUND(a,b) (a == b->chan ? 1 : 0) | ||
3025 | 56 | |||
3026 | 57 | static struct ast_jb_conf g_jb_conf = { | ||
3027 | 58 | .flags = 0, | ||
3028 | 59 | .max_size = -1, | ||
3029 | 60 | .resync_threshold = -1, | ||
3030 | 61 | .impl = "", | ||
3031 | 62 | }; | ||
3032 | 63 | |||
3033 | 64 | static struct ast_channel *local_request(const char *type, int format, void *data, int *cause); | ||
3034 | 65 | static int local_digit_begin(struct ast_channel *ast, char digit); | ||
3035 | 66 | static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration); | ||
3036 | 67 | static int local_call(struct ast_channel *ast, char *dest, int timeout); | ||
3037 | 68 | static int local_hangup(struct ast_channel *ast); | ||
3038 | 69 | static int local_answer(struct ast_channel *ast); | ||
3039 | 70 | static struct ast_frame *local_read(struct ast_channel *ast); | ||
3040 | 71 | static int local_write(struct ast_channel *ast, struct ast_frame *f); | ||
3041 | 72 | static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen); | ||
3042 | 73 | static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan); | ||
3043 | 74 | static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen); | ||
3044 | 75 | static int local_sendtext(struct ast_channel *ast, const char *text); | ||
3045 | 76 | static int local_devicestate(void *data); | ||
3046 | 77 | static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge); | ||
3047 | 78 | |||
3048 | 79 | /* PBX interface structure for channel registration */ | ||
3049 | 80 | static const struct ast_channel_tech local_tech = { | ||
3050 | 81 | .type = "Local", | ||
3051 | 82 | .description = tdesc, | ||
3052 | 83 | .capabilities = -1, | ||
3053 | 84 | .requester = local_request, | ||
3054 | 85 | .send_digit_begin = local_digit_begin, | ||
3055 | 86 | .send_digit_end = local_digit_end, | ||
3056 | 87 | .call = local_call, | ||
3057 | 88 | .hangup = local_hangup, | ||
3058 | 89 | .answer = local_answer, | ||
3059 | 90 | .read = local_read, | ||
3060 | 91 | .write = local_write, | ||
3061 | 92 | .write_video = local_write, | ||
3062 | 93 | .exception = local_read, | ||
3063 | 94 | .indicate = local_indicate, | ||
3064 | 95 | .fixup = local_fixup, | ||
3065 | 96 | .send_html = local_sendhtml, | ||
3066 | 97 | .send_text = local_sendtext, | ||
3067 | 98 | .devicestate = local_devicestate, | ||
3068 | 99 | .bridged_channel = local_bridgedchannel, | ||
3069 | 100 | }; | ||
3070 | 101 | |||
3071 | 102 | struct local_pvt { | ||
3072 | 103 | ast_mutex_t lock; /* Channel private lock */ | ||
3073 | 104 | unsigned int flags; /* Private flags */ | ||
3074 | 105 | char context[AST_MAX_CONTEXT]; /* Context to call */ | ||
3075 | 106 | char exten[AST_MAX_EXTENSION]; /* Extension to call */ | ||
3076 | 107 | int reqformat; /* Requested format */ | ||
3077 | 108 | struct ast_jb_conf jb_conf; /*!< jitterbuffer configuration for this local channel */ | ||
3078 | 109 | struct ast_channel *owner; /* Master Channel - Bridging happens here */ | ||
3079 | 110 | struct ast_channel *chan; /* Outbound channel - PBX is run here */ | ||
3080 | 111 | struct ast_module_user *u_owner; /*! reference to keep the module loaded while in use */ | ||
3081 | 112 | struct ast_module_user *u_chan; /*! reference to keep the module loaded while in use */ | ||
3082 | 113 | AST_LIST_ENTRY(local_pvt) list; /* Next entity */ | ||
3083 | 114 | }; | ||
3084 | 115 | |||
3085 | 116 | #define LOCAL_GLARE_DETECT (1 << 0) /*!< Detect glare on hangup */ | ||
3086 | 117 | #define LOCAL_CANCEL_QUEUE (1 << 1) /*!< Cancel queue */ | ||
3087 | 118 | #define LOCAL_ALREADY_MASQED (1 << 2) /*!< Already masqueraded */ | ||
3088 | 119 | #define LOCAL_LAUNCHED_PBX (1 << 3) /*!< PBX was launched */ | ||
3089 | 120 | #define LOCAL_NO_OPTIMIZATION (1 << 4) /*!< Do not optimize using masquerading */ | ||
3090 | 121 | #define LOCAL_BRIDGE (1 << 5) /*!< Report back the "true" channel as being bridged to */ | ||
3091 | 122 | #define LOCAL_MOH_PASSTHRU (1 << 6) /*!< Pass through music on hold start/stop frames */ | ||
3092 | 123 | |||
3093 | 124 | static AST_LIST_HEAD_STATIC(locals, local_pvt); | ||
3094 | 125 | |||
3095 | 126 | /*! \brief Adds devicestate to local channels */ | ||
3096 | 127 | static int local_devicestate(void *data) | ||
3097 | 128 | { | ||
3098 | 129 | char *exten = ast_strdupa(data); | ||
3099 | 130 | char *context = NULL, *opts = NULL; | ||
3100 | 131 | int res; | ||
3101 | 132 | struct local_pvt *lp; | ||
3102 | 133 | |||
3103 | 134 | if (!(context = strchr(exten, '@'))) { | ||
3104 | 135 | ast_log(LOG_WARNING, "Someone used Local/%s somewhere without a @context. This is bad.\n", exten); | ||
3105 | 136 | return AST_DEVICE_INVALID; | ||
3106 | 137 | } | ||
3107 | 138 | |||
3108 | 139 | *context++ = '\0'; | ||
3109 | 140 | |||
3110 | 141 | /* Strip options if they exist */ | ||
3111 | 142 | if ((opts = strchr(context, '/'))) | ||
3112 | 143 | *opts = '\0'; | ||
3113 | 144 | |||
3114 | 145 | ast_debug(3, "Checking if extension %s@%s exists (devicestate)\n", exten, context); | ||
3115 | 146 | |||
3116 | 147 | res = ast_exists_extension(NULL, context, exten, 1, NULL); | ||
3117 | 148 | if (!res) | ||
3118 | 149 | return AST_DEVICE_INVALID; | ||
3119 | 150 | |||
3120 | 151 | res = AST_DEVICE_NOT_INUSE; | ||
3121 | 152 | AST_LIST_LOCK(&locals); | ||
3122 | 153 | AST_LIST_TRAVERSE(&locals, lp, list) { | ||
3123 | 154 | if (!strcmp(exten, lp->exten) && !strcmp(context, lp->context) && lp->owner) { | ||
3124 | 155 | res = AST_DEVICE_INUSE; | ||
3125 | 156 | break; | ||
3126 | 157 | } | ||
3127 | 158 | } | ||
3128 | 159 | AST_LIST_UNLOCK(&locals); | ||
3129 | 160 | |||
3130 | 161 | return res; | ||
3131 | 162 | } | ||
3132 | 163 | |||
3133 | 164 | /*! | ||
3134 | 165 | * \note Assumes the pvt is no longer in the pvts list | ||
3135 | 166 | */ | ||
3136 | 167 | static struct local_pvt *local_pvt_destroy(struct local_pvt *pvt) | ||
3137 | 168 | { | ||
3138 | 169 | ast_mutex_destroy(&pvt->lock); | ||
3139 | 170 | ast_free(pvt); | ||
3140 | 171 | return NULL; | ||
3141 | 172 | } | ||
3142 | 173 | |||
3143 | 174 | /*! \brief Return the bridged channel of a Local channel */ | ||
3144 | 175 | static struct ast_channel *local_bridgedchannel(struct ast_channel *chan, struct ast_channel *bridge) | ||
3145 | 176 | { | ||
3146 | 177 | struct local_pvt *p = bridge->tech_pvt; | ||
3147 | 178 | struct ast_channel *bridged = bridge; | ||
3148 | 179 | |||
3149 | 180 | if (!p) { | ||
3150 | 181 | ast_debug(1, "Asked for bridged channel on '%s'/'%s', returning <none>\n", | ||
3151 | 182 | chan->name, bridge->name); | ||
3152 | 183 | return NULL; | ||
3153 | 184 | } | ||
3154 | 185 | |||
3155 | 186 | ast_mutex_lock(&p->lock); | ||
3156 | 187 | |||
3157 | 188 | if (ast_test_flag(p, LOCAL_BRIDGE)) { | ||
3158 | 189 | /* Find the opposite channel */ | ||
3159 | 190 | bridged = (bridge == p->owner ? p->chan : p->owner); | ||
3160 | 191 | |||
3161 | 192 | /* Now see if the opposite channel is bridged to anything */ | ||
3162 | 193 | if (!bridged) { | ||
3163 | 194 | bridged = bridge; | ||
3164 | 195 | } else if (bridged->_bridge) { | ||
3165 | 196 | bridged = bridged->_bridge; | ||
3166 | 197 | } | ||
3167 | 198 | } | ||
3168 | 199 | |||
3169 | 200 | ast_mutex_unlock(&p->lock); | ||
3170 | 201 | |||
3171 | 202 | return bridged; | ||
3172 | 203 | } | ||
3173 | 204 | |||
3174 | 205 | static int local_queue_frame(struct local_pvt *p, int isoutbound, struct ast_frame *f, | ||
3175 | 206 | struct ast_channel *us, int us_locked) | ||
3176 | 207 | { | ||
3177 | 208 | struct ast_channel *other = NULL; | ||
3178 | 209 | |||
3179 | 210 | /* Recalculate outbound channel */ | ||
3180 | 211 | other = isoutbound ? p->owner : p->chan; | ||
3181 | 212 | |||
3182 | 213 | if (!other) { | ||
3183 | 214 | return 0; | ||
3184 | 215 | } | ||
3185 | 216 | |||
3186 | 217 | /* do not queue frame if generator is on both local channels */ | ||
3187 | 218 | if (us && us->generator && other->generator) { | ||
3188 | 219 | return 0; | ||
3189 | 220 | } | ||
3190 | 221 | |||
3191 | 222 | /* Set glare detection */ | ||
3192 | 223 | ast_set_flag(p, LOCAL_GLARE_DETECT); | ||
3193 | 224 | |||
3194 | 225 | /* Ensure that we have both channels locked */ | ||
3195 | 226 | while (other && ast_channel_trylock(other)) { | ||
3196 | 227 | ast_mutex_unlock(&p->lock); | ||
3197 | 228 | if (us && us_locked) { | ||
3198 | 229 | do { | ||
3199 | 230 | CHANNEL_DEADLOCK_AVOIDANCE(us); | ||
3200 | 231 | } while (ast_mutex_trylock(&p->lock)); | ||
3201 | 232 | } else { | ||
3202 | 233 | usleep(1); | ||
3203 | 234 | ast_mutex_lock(&p->lock); | ||
3204 | 235 | } | ||
3205 | 236 | other = isoutbound ? p->owner : p->chan; | ||
3206 | 237 | } | ||
3207 | 238 | |||
3208 | 239 | /* Since glare detection only occurs within this function, and because | ||
3209 | 240 | * a pvt flag cannot be set without having the pvt lock, this is the only | ||
3210 | 241 | * location where we could detect a cancelling of the queue. */ | ||
3211 | 242 | if (ast_test_flag(p, LOCAL_CANCEL_QUEUE)) { | ||
3212 | 243 | /* We had a glare on the hangup. Forget all this business, | ||
3213 | 244 | return and destroy p. */ | ||
3214 | 245 | ast_mutex_unlock(&p->lock); | ||
3215 | 246 | p = local_pvt_destroy(p); | ||
3216 | 247 | if (other) { | ||
3217 | 248 | ast_channel_unlock(other); | ||
3218 | 249 | } | ||
3219 | 250 | return -1; | ||
3220 | 251 | } | ||
3221 | 252 | |||
3222 | 253 | if (other) { | ||
3223 | 254 | if (other->pbx || other->_bridge || !ast_strlen_zero(other->appl)) { | ||
3224 | 255 | ast_queue_frame(other, f); | ||
3225 | 256 | } /* else the frame won't go anywhere */ | ||
3226 | 257 | ast_channel_unlock(other); | ||
3227 | 258 | } | ||
3228 | 259 | |||
3229 | 260 | ast_clear_flag(p, LOCAL_GLARE_DETECT); | ||
3230 | 261 | |||
3231 | 262 | return 0; | ||
3232 | 263 | } | ||
3233 | 264 | |||
3234 | 265 | static int local_answer(struct ast_channel *ast) | ||
3235 | 266 | { | ||
3236 | 267 | struct local_pvt *p = ast->tech_pvt; | ||
3237 | 268 | int isoutbound; | ||
3238 | 269 | int res = -1; | ||
3239 | 270 | |||
3240 | 271 | if (!p) | ||
3241 | 272 | return -1; | ||
3242 | 273 | |||
3243 | 274 | ast_mutex_lock(&p->lock); | ||
3244 | 275 | isoutbound = IS_OUTBOUND(ast, p); | ||
3245 | 276 | if (isoutbound) { | ||
3246 | 277 | /* Pass along answer since somebody answered us */ | ||
3247 | 278 | struct ast_frame answer = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER }; | ||
3248 | 279 | res = local_queue_frame(p, isoutbound, &answer, ast, 1); | ||
3249 | 280 | } else | ||
3250 | 281 | ast_log(LOG_WARNING, "Huh? Local is being asked to answer?\n"); | ||
3251 | 282 | if (!res) | ||
3252 | 283 | ast_mutex_unlock(&p->lock); | ||
3253 | 284 | return res; | ||
3254 | 285 | } | ||
3255 | 286 | |||
3256 | 287 | static void check_bridge(struct local_pvt *p, int isoutbound) | ||
3257 | 288 | { | ||
3258 | 289 | struct ast_channel_monitor *tmp; | ||
3259 | 290 | if (ast_test_flag(p, LOCAL_ALREADY_MASQED) || ast_test_flag(p, LOCAL_NO_OPTIMIZATION) || !p->chan || !p->owner || (p->chan->_bridge != ast_bridged_channel(p->chan))) | ||
3260 | 291 | return; | ||
3261 | 292 | |||
3262 | 293 | /* only do the masquerade if we are being called on the outbound channel, | ||
3263 | 294 | if it has been bridged to another channel and if there are no pending | ||
3264 | 295 | frames on the owner channel (because they would be transferred to the | ||
3265 | 296 | outbound channel during the masquerade) | ||
3266 | 297 | */ | ||
3267 | 298 | if (isoutbound && p->chan->_bridge /* Not ast_bridged_channel! Only go one step! */ && AST_LIST_EMPTY(&p->owner->readq)) { | ||
3268 | 299 | /* Masquerade bridged channel into owner */ | ||
3269 | 300 | /* Lock everything we need, one by one, and give up if | ||
3270 | 301 | we can't get everything. Remember, we'll get another | ||
3271 | 302 | chance in just a little bit */ | ||
3272 | 303 | if (!ast_channel_trylock(p->chan->_bridge)) { | ||
3273 | 304 | if (!ast_check_hangup(p->chan->_bridge)) { | ||
3274 | 305 | if (!ast_channel_trylock(p->owner)) { | ||
3275 | 306 | if (!ast_check_hangup(p->owner)) { | ||
3276 | 307 | if (p->owner->monitor && !p->chan->_bridge->monitor) { | ||
3277 | 308 | /* If a local channel is being monitored, we don't want a masquerade | ||
3278 | 309 | * to cause the monitor to go away. Since the masquerade swaps the monitors, | ||
3279 | 310 | * pre-swapping the monitors before the masquerade will ensure that the monitor | ||
3280 | 311 | * ends up where it is expected. | ||
3281 | 312 | */ | ||
3282 | 313 | tmp = p->owner->monitor; | ||
3283 | 314 | p->owner->monitor = p->chan->_bridge->monitor; | ||
3284 | 315 | p->chan->_bridge->monitor = tmp; | ||
3285 | 316 | } | ||
3286 | 317 | if (p->chan->audiohooks) { | ||
3287 | 318 | struct ast_audiohook_list *audiohooks_swapper; | ||
3288 | 319 | audiohooks_swapper = p->chan->audiohooks; | ||
3289 | 320 | p->chan->audiohooks = p->owner->audiohooks; | ||
3290 | 321 | p->owner->audiohooks = audiohooks_swapper; | ||
3291 | 322 | } | ||
3292 | 323 | ast_app_group_update(p->chan, p->owner); | ||
3293 | 324 | ast_channel_masquerade(p->owner, p->chan->_bridge); | ||
3294 | 325 | ast_set_flag(p, LOCAL_ALREADY_MASQED); | ||
3295 | 326 | } | ||
3296 | 327 | ast_channel_unlock(p->owner); | ||
3297 | 328 | } | ||
3298 | 329 | ast_channel_unlock(p->chan->_bridge); | ||
3299 | 330 | } | ||
3300 | 331 | } | ||
3301 | 332 | /* We only allow masquerading in one 'direction'... it's important to preserve the state | ||
3302 | 333 | (group variables, etc.) that live on p->chan->_bridge (and were put there by the dialplan) | ||
3303 | 334 | when the local channels go away. | ||
3304 | 335 | */ | ||
3305 | 336 | #if 0 | ||
3306 | 337 | } else if (!isoutbound && p->owner && p->owner->_bridge && p->chan && AST_LIST_EMPTY(&p->chan->readq)) { | ||
3307 | 338 | /* Masquerade bridged channel into chan */ | ||
3308 | 339 | if (!ast_mutex_trylock(&(p->owner->_bridge)->lock)) { | ||
3309 | 340 | if (!ast_check_hangup(p->owner->_bridge)) { | ||
3310 | 341 | if (!ast_mutex_trylock(&p->chan->lock)) { | ||
3311 | 342 | if (!ast_check_hangup(p->chan)) { | ||
3312 | 343 | ast_channel_masquerade(p->chan, p->owner->_bridge); | ||
3313 | 344 | ast_set_flag(p, LOCAL_ALREADY_MASQED); | ||
3314 | 345 | } | ||
3315 | 346 | ast_mutex_unlock(&p->chan->lock); | ||
3316 | 347 | } | ||
3317 | 348 | } | ||
3318 | 349 | ast_mutex_unlock(&(p->owner->_bridge)->lock); | ||
3319 | 350 | } | ||
3320 | 351 | #endif | ||
3321 | 352 | } | ||
3322 | 353 | } | ||
3323 | 354 | |||
3324 | 355 | static struct ast_frame *local_read(struct ast_channel *ast) | ||
3325 | 356 | { | ||
3326 | 357 | return &ast_null_frame; | ||
3327 | 358 | } | ||
3328 | 359 | |||
3329 | 360 | static int local_write(struct ast_channel *ast, struct ast_frame *f) | ||
3330 | 361 | { | ||
3331 | 362 | struct local_pvt *p = ast->tech_pvt; | ||
3332 | 363 | int res = -1; | ||
3333 | 364 | int isoutbound; | ||
3334 | 365 | |||
3335 | 366 | if (!p) | ||
3336 | 367 | return -1; | ||
3337 | 368 | |||
3338 | 369 | /* Just queue for delivery to the other side */ | ||
3339 | 370 | ast_mutex_lock(&p->lock); | ||
3340 | 371 | isoutbound = IS_OUTBOUND(ast, p); | ||
3341 | 372 | if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) | ||
3342 | 373 | check_bridge(p, isoutbound); | ||
3343 | 374 | if (!ast_test_flag(p, LOCAL_ALREADY_MASQED)) | ||
3344 | 375 | res = local_queue_frame(p, isoutbound, f, ast, 1); | ||
3345 | 376 | else { | ||
3346 | 377 | ast_debug(1, "Not posting to queue since already masked on '%s'\n", ast->name); | ||
3347 | 378 | res = 0; | ||
3348 | 379 | } | ||
3349 | 380 | if (!res) | ||
3350 | 381 | ast_mutex_unlock(&p->lock); | ||
3351 | 382 | return res; | ||
3352 | 383 | } | ||
3353 | 384 | |||
3354 | 385 | static int local_fixup(struct ast_channel *oldchan, struct ast_channel *newchan) | ||
3355 | 386 | { | ||
3356 | 387 | struct local_pvt *p = newchan->tech_pvt; | ||
3357 | 388 | |||
3358 | 389 | if (!p) | ||
3359 | 390 | return -1; | ||
3360 | 391 | |||
3361 | 392 | ast_mutex_lock(&p->lock); | ||
3362 | 393 | |||
3363 | 394 | if ((p->owner != oldchan) && (p->chan != oldchan)) { | ||
3364 | 395 | ast_log(LOG_WARNING, "Old channel wasn't %p but was %p/%p\n", oldchan, p->owner, p->chan); | ||
3365 | 396 | ast_mutex_unlock(&p->lock); | ||
3366 | 397 | return -1; | ||
3367 | 398 | } | ||
3368 | 399 | if (p->owner == oldchan) | ||
3369 | 400 | p->owner = newchan; | ||
3370 | 401 | else | ||
3371 | 402 | p->chan = newchan; | ||
3372 | 403 | ast_mutex_unlock(&p->lock); | ||
3373 | 404 | return 0; | ||
3374 | 405 | } | ||
3375 | 406 | |||
3376 | 407 | static int local_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen) | ||
3377 | 408 | { | ||
3378 | 409 | struct local_pvt *p = ast->tech_pvt; | ||
3379 | 410 | int res = 0; | ||
3380 | 411 | struct ast_frame f = { AST_FRAME_CONTROL, }; | ||
3381 | 412 | int isoutbound; | ||
3382 | 413 | |||
3383 | 414 | if (!p) | ||
3384 | 415 | return -1; | ||
3385 | 416 | |||
3386 | 417 | /* If this is an MOH hold or unhold, do it on the Local channel versus real channel */ | ||
3387 | 418 | if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_HOLD) { | ||
3388 | 419 | ast_moh_start(ast, data, NULL); | ||
3389 | 420 | } else if (!ast_test_flag(p, LOCAL_MOH_PASSTHRU) && condition == AST_CONTROL_UNHOLD) { | ||
3390 | 421 | ast_moh_stop(ast); | ||
3391 | 422 | } else { | ||
3392 | 423 | /* Queue up a frame representing the indication as a control frame */ | ||
3393 | 424 | ast_mutex_lock(&p->lock); | ||
3394 | 425 | isoutbound = IS_OUTBOUND(ast, p); | ||
3395 | 426 | f.subclass = condition; | ||
3396 | 427 | f.data.ptr = (void*)data; | ||
3397 | 428 | f.datalen = datalen; | ||
3398 | 429 | if (!(res = local_queue_frame(p, isoutbound, &f, ast, 1))) | ||
3399 | 430 | ast_mutex_unlock(&p->lock); | ||
3400 | 431 | } | ||
3401 | 432 | |||
3402 | 433 | return res; | ||
3403 | 434 | } | ||
3404 | 435 | |||
3405 | 436 | static int local_digit_begin(struct ast_channel *ast, char digit) | ||
3406 | 437 | { | ||
3407 | 438 | struct local_pvt *p = ast->tech_pvt; | ||
3408 | 439 | int res = -1; | ||
3409 | 440 | struct ast_frame f = { AST_FRAME_DTMF_BEGIN, }; | ||
3410 | 441 | int isoutbound; | ||
3411 | 442 | |||
3412 | 443 | if (!p) | ||
3413 | 444 | return -1; | ||
3414 | 445 | |||
3415 | 446 | ast_mutex_lock(&p->lock); | ||
3416 | 447 | isoutbound = IS_OUTBOUND(ast, p); | ||
3417 | 448 | f.subclass = digit; | ||
3418 | 449 | if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0))) | ||
3419 | 450 | ast_mutex_unlock(&p->lock); | ||
3420 | 451 | |||
3421 | 452 | return res; | ||
3422 | 453 | } | ||
3423 | 454 | |||
3424 | 455 | static int local_digit_end(struct ast_channel *ast, char digit, unsigned int duration) | ||
3425 | 456 | { | ||
3426 | 457 | struct local_pvt *p = ast->tech_pvt; | ||
3427 | 458 | int res = -1; | ||
3428 | 459 | struct ast_frame f = { AST_FRAME_DTMF_END, }; | ||
3429 | 460 | int isoutbound; | ||
3430 | 461 | |||
3431 | 462 | if (!p) | ||
3432 | 463 | return -1; | ||
3433 | 464 | |||
3434 | 465 | ast_mutex_lock(&p->lock); | ||
3435 | 466 | isoutbound = IS_OUTBOUND(ast, p); | ||
3436 | 467 | f.subclass = digit; | ||
3437 | 468 | f.len = duration; | ||
3438 | 469 | if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0))) | ||
3439 | 470 | ast_mutex_unlock(&p->lock); | ||
3440 | 471 | |||
3441 | 472 | return res; | ||
3442 | 473 | } | ||
3443 | 474 | |||
3444 | 475 | static int local_sendtext(struct ast_channel *ast, const char *text) | ||
3445 | 476 | { | ||
3446 | 477 | struct local_pvt *p = ast->tech_pvt; | ||
3447 | 478 | int res = -1; | ||
3448 | 479 | struct ast_frame f = { AST_FRAME_TEXT, }; | ||
3449 | 480 | int isoutbound; | ||
3450 | 481 | |||
3451 | 482 | if (!p) | ||
3452 | 483 | return -1; | ||
3453 | 484 | |||
3454 | 485 | ast_mutex_lock(&p->lock); | ||
3455 | 486 | isoutbound = IS_OUTBOUND(ast, p); | ||
3456 | 487 | f.data.ptr = (char *) text; | ||
3457 | 488 | f.datalen = strlen(text) + 1; | ||
3458 | 489 | if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0))) | ||
3459 | 490 | ast_mutex_unlock(&p->lock); | ||
3460 | 491 | return res; | ||
3461 | 492 | } | ||
3462 | 493 | |||
3463 | 494 | static int local_sendhtml(struct ast_channel *ast, int subclass, const char *data, int datalen) | ||
3464 | 495 | { | ||
3465 | 496 | struct local_pvt *p = ast->tech_pvt; | ||
3466 | 497 | int res = -1; | ||
3467 | 498 | struct ast_frame f = { AST_FRAME_HTML, }; | ||
3468 | 499 | int isoutbound; | ||
3469 | 500 | |||
3470 | 501 | if (!p) | ||
3471 | 502 | return -1; | ||
3472 | 503 | |||
3473 | 504 | ast_mutex_lock(&p->lock); | ||
3474 | 505 | isoutbound = IS_OUTBOUND(ast, p); | ||
3475 | 506 | f.subclass = subclass; | ||
3476 | 507 | f.data.ptr = (char *)data; | ||
3477 | 508 | f.datalen = datalen; | ||
3478 | 509 | if (!(res = local_queue_frame(p, isoutbound, &f, ast, 0))) | ||
3479 | 510 | ast_mutex_unlock(&p->lock); | ||
3480 | 511 | return res; | ||
3481 | 512 | } | ||
3482 | 513 | |||
3483 | 514 | /*! \brief Initiate new call, part of PBX interface | ||
3484 | 515 | * dest is the dial string */ | ||
3485 | 516 | static int local_call(struct ast_channel *ast, char *dest, int timeout) | ||
3486 | 517 | { | ||
3487 | 518 | struct local_pvt *p = ast->tech_pvt; | ||
3488 | 519 | int res; | ||
3489 | 520 | struct ast_var_t *varptr = NULL, *new; | ||
3490 | 521 | size_t len, namelen; | ||
3491 | 522 | |||
3492 | 523 | if (!p) | ||
3493 | 524 | return -1; | ||
3494 | 525 | |||
3495 | 526 | ast_mutex_lock(&p->lock); | ||
3496 | 527 | |||
3497 | 528 | /* | ||
3498 | 529 | * Note that cid_num and cid_name aren't passed in the ast_channel_alloc | ||
3499 | 530 | * call, so it's done here instead. | ||
3500 | 531 | */ | ||
3501 | 532 | p->chan->cid.cid_dnid = ast_strdup(p->owner->cid.cid_dnid); | ||
3502 | 533 | p->chan->cid.cid_num = ast_strdup(p->owner->cid.cid_num); | ||
3503 | 534 | p->chan->cid.cid_name = ast_strdup(p->owner->cid.cid_name); | ||
3504 | 535 | p->chan->cid.cid_rdnis = ast_strdup(p->owner->cid.cid_rdnis); | ||
3505 | 536 | p->chan->cid.cid_ani = ast_strdup(p->owner->cid.cid_ani); | ||
3506 | 537 | p->chan->cid.cid_pres = p->owner->cid.cid_pres; | ||
3507 | 538 | p->chan->cid.cid_ani2 = p->owner->cid.cid_ani2; | ||
3508 | 539 | p->chan->cid.cid_ton = p->owner->cid.cid_ton; | ||
3509 | 540 | p->chan->cid.cid_tns = p->owner->cid.cid_tns; | ||
3510 | 541 | ast_string_field_set(p->chan, language, p->owner->language); | ||
3511 | 542 | ast_string_field_set(p->chan, accountcode, p->owner->accountcode); | ||
3512 | 543 | ast_string_field_set(p->chan, musicclass, p->owner->musicclass); | ||
3513 | 544 | ast_cdr_update(p->chan); | ||
3514 | 545 | p->chan->cdrflags = p->owner->cdrflags; | ||
3515 | 546 | |||
3516 | 547 | if (!ast_exists_extension(NULL, p->chan->context, p->chan->exten, 1, p->owner->cid.cid_num)) { | ||
3517 | 548 | ast_log(LOG_NOTICE, "No such extension/context %s@%s while calling Local channel\n", p->chan->exten, p->chan->context); | ||
3518 | 549 | ast_mutex_unlock(&p->lock); | ||
3519 | 550 | return -1; | ||
3520 | 551 | } | ||
3521 | 552 | |||
3522 | 553 | /* Make sure we inherit the ANSWERED_ELSEWHERE flag if it's set on the queue/dial call request in the dialplan */ | ||
3523 | 554 | if (ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) { | ||
3524 | 555 | ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE); | ||
3525 | 556 | } | ||
3526 | 557 | |||
3527 | 558 | /* copy the channel variables from the incoming channel to the outgoing channel */ | ||
3528 | 559 | /* Note that due to certain assumptions, they MUST be in the same order */ | ||
3529 | 560 | AST_LIST_TRAVERSE(&p->owner->varshead, varptr, entries) { | ||
3530 | 561 | namelen = strlen(varptr->name); | ||
3531 | 562 | len = sizeof(struct ast_var_t) + namelen + strlen(varptr->value) + 2; | ||
3532 | 563 | if ((new = ast_calloc(1, len))) { | ||
3533 | 564 | memcpy(new, varptr, len); | ||
3534 | 565 | new->value = &(new->name[0]) + namelen + 1; | ||
3535 | 566 | AST_LIST_INSERT_TAIL(&p->chan->varshead, new, entries); | ||
3536 | 567 | } | ||
3537 | 568 | } | ||
3538 | 569 | ast_channel_datastore_inherit(p->owner, p->chan); | ||
3539 | 570 | |||
3540 | 571 | /* Start switch on sub channel */ | ||
3541 | 572 | if (!(res = ast_pbx_start(p->chan))) | ||
3542 | 573 | ast_set_flag(p, LOCAL_LAUNCHED_PBX); | ||
3543 | 574 | |||
3544 | 575 | ast_mutex_unlock(&p->lock); | ||
3545 | 576 | return res; | ||
3546 | 577 | } | ||
3547 | 578 | |||
3548 | 579 | /*! \brief Hangup a call through the local proxy channel */ | ||
3549 | 580 | static int local_hangup(struct ast_channel *ast) | ||
3550 | 581 | { | ||
3551 | 582 | struct local_pvt *p = ast->tech_pvt; | ||
3552 | 583 | int isoutbound; | ||
3553 | 584 | struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_HANGUP, .data.uint32 = ast->hangupcause }; | ||
3554 | 585 | struct ast_channel *ochan = NULL; | ||
3555 | 586 | int glaredetect = 0, res = 0; | ||
3556 | 587 | |||
3557 | 588 | if (!p) | ||
3558 | 589 | return -1; | ||
3559 | 590 | |||
3560 | 591 | ast_mutex_lock(&p->lock); | ||
3561 | 592 | |||
3562 | 593 | isoutbound = IS_OUTBOUND(ast, p); | ||
3563 | 594 | |||
3564 | 595 | if (p->chan && ast_test_flag(ast, AST_FLAG_ANSWERED_ELSEWHERE)) { | ||
3565 | 596 | ast_set_flag(p->chan, AST_FLAG_ANSWERED_ELSEWHERE); | ||
3566 | 597 | ast_debug(2, "This local call has the ANSWERED_ELSEWHERE flag set.\n"); | ||
3567 | 598 | } | ||
3568 | 599 | |||
3569 | 600 | if (isoutbound) { | ||
3570 | 601 | const char *status = pbx_builtin_getvar_helper(p->chan, "DIALSTATUS"); | ||
3571 | 602 | if ((status) && (p->owner)) { | ||
3572 | 603 | /* Deadlock avoidance */ | ||
3573 | 604 | while (p->owner && ast_channel_trylock(p->owner)) { | ||
3574 | 605 | ast_mutex_unlock(&p->lock); | ||
3575 | 606 | if (ast) { | ||
3576 | 607 | ast_channel_unlock(ast); | ||
3577 | 608 | } | ||
3578 | 609 | usleep(1); | ||
3579 | 610 | if (ast) { | ||
3580 | 611 | ast_channel_lock(ast); | ||
3581 | 612 | } | ||
3582 | 613 | ast_mutex_lock(&p->lock); | ||
3583 | 614 | } | ||
3584 | 615 | if (p->owner) { | ||
3585 | 616 | pbx_builtin_setvar_helper(p->owner, "CHANLOCALSTATUS", status); | ||
3586 | 617 | ast_channel_unlock(p->owner); | ||
3587 | 618 | } | ||
3588 | 619 | } | ||
3589 | 620 | p->chan = NULL; | ||
3590 | 621 | ast_clear_flag(p, LOCAL_LAUNCHED_PBX); | ||
3591 | 622 | ast_module_user_remove(p->u_chan); | ||
3592 | 623 | } else { | ||
3593 | 624 | ast_module_user_remove(p->u_owner); | ||
3594 | 625 | while (p->chan && ast_channel_trylock(p->chan)) { | ||
3595 | 626 | DEADLOCK_AVOIDANCE(&p->lock); | ||
3596 | 627 | } | ||
3597 | 628 | p->owner = NULL; | ||
3598 | 629 | if (p->chan) { | ||
3599 | 630 | ast_queue_hangup(p->chan); | ||
3600 | 631 | ast_channel_unlock(p->chan); | ||
3601 | 632 | } | ||
3602 | 633 | } | ||
3603 | 634 | |||
3604 | 635 | ast->tech_pvt = NULL; | ||
3605 | 636 | |||
3606 | 637 | if (!p->owner && !p->chan) { | ||
3607 | 638 | /* Okay, done with the private part now, too. */ | ||
3608 | 639 | glaredetect = ast_test_flag(p, LOCAL_GLARE_DETECT); | ||
3609 | 640 | /* If we have a queue holding, don't actually destroy p yet, but | ||
3610 | 641 | let local_queue do it. */ | ||
3611 | 642 | if (glaredetect) | ||
3612 | 643 | ast_set_flag(p, LOCAL_CANCEL_QUEUE); | ||
3613 | 644 | /* Remove from list */ | ||
3614 | 645 | AST_LIST_LOCK(&locals); | ||
3615 | 646 | AST_LIST_REMOVE(&locals, p, list); | ||
3616 | 647 | AST_LIST_UNLOCK(&locals); | ||
3617 | 648 | ast_mutex_unlock(&p->lock); | ||
3618 | 649 | /* And destroy */ | ||
3619 | 650 | if (!glaredetect) { | ||
3620 | 651 | p = local_pvt_destroy(p); | ||
3621 | 652 | } | ||
3622 | 653 | return 0; | ||
3623 | 654 | } | ||
3624 | 655 | if (p->chan && !ast_test_flag(p, LOCAL_LAUNCHED_PBX)) | ||
3625 | 656 | /* Need to actually hangup since there is no PBX */ | ||
3626 | 657 | ochan = p->chan; | ||
3627 | 658 | else | ||
3628 | 659 | res = local_queue_frame(p, isoutbound, &f, NULL, 1); | ||
3629 | 660 | if (!res) | ||
3630 | 661 | ast_mutex_unlock(&p->lock); | ||
3631 | 662 | if (ochan) | ||
3632 | 663 | ast_hangup(ochan); | ||
3633 | 664 | return 0; | ||
3634 | 665 | } | ||
3635 | 666 | |||
3636 | 667 | /*! \brief Create a call structure */ | ||
3637 | 668 | static struct local_pvt *local_alloc(const char *data, int format) | ||
3638 | 669 | { | ||
3639 | 670 | struct local_pvt *tmp = NULL; | ||
3640 | 671 | char *c = NULL, *opts = NULL; | ||
3641 | 672 | |||
3642 | 673 | if (!(tmp = ast_calloc(1, sizeof(*tmp)))) | ||
3643 | 674 | return NULL; | ||
3644 | 675 | |||
3645 | 676 | /* Initialize private structure information */ | ||
3646 | 677 | ast_mutex_init(&tmp->lock); | ||
3647 | 678 | ast_copy_string(tmp->exten, data, sizeof(tmp->exten)); | ||
3648 | 679 | |||
3649 | 680 | memcpy(&tmp->jb_conf, &g_jb_conf, sizeof(tmp->jb_conf)); | ||
3650 | 681 | |||
3651 | 682 | /* Look for options */ | ||
3652 | 683 | if ((opts = strchr(tmp->exten, '/'))) { | ||
3653 | 684 | *opts++ = '\0'; | ||
3654 | 685 | if (strchr(opts, 'n')) | ||
3655 | 686 | ast_set_flag(tmp, LOCAL_NO_OPTIMIZATION); | ||
3656 | 687 | if (strchr(opts, 'j')) { | ||
3657 | 688 | if (ast_test_flag(tmp, LOCAL_NO_OPTIMIZATION)) | ||
3658 | 689 | ast_set_flag(&tmp->jb_conf, AST_JB_ENABLED); | ||
3659 | 690 | else { | ||
3660 | 691 | ast_log(LOG_ERROR, "You must use the 'n' option for chan_local " | ||
3661 | 692 | "to use the 'j' option to enable the jitterbuffer\n"); | ||
3662 | 693 | } | ||
3663 | 694 | } | ||
3664 | 695 | if (strchr(opts, 'b')) { | ||
3665 | 696 | ast_set_flag(tmp, LOCAL_BRIDGE); | ||
3666 | 697 | } | ||
3667 | 698 | if (strchr(opts, 'm')) { | ||
3668 | 699 | ast_set_flag(tmp, LOCAL_MOH_PASSTHRU); | ||
3669 | 700 | } | ||
3670 | 701 | } | ||
3671 | 702 | |||
3672 | 703 | /* Look for a context */ | ||
3673 | 704 | if ((c = strchr(tmp->exten, '@'))) | ||
3674 | 705 | *c++ = '\0'; | ||
3675 | 706 | |||
3676 | 707 | ast_copy_string(tmp->context, c ? c : "default", sizeof(tmp->context)); | ||
3677 | 708 | |||
3678 | 709 | tmp->reqformat = format; | ||
3679 | 710 | |||
3680 | 711 | #if 0 | ||
3681 | 712 | /* We can't do this check here, because we don't know the CallerID yet, and | ||
3682 | 713 | * the CallerID could potentially affect what step is actually taken (or | ||
3683 | 714 | * even if that step exists). */ | ||
3684 | 715 | if (!ast_exists_extension(NULL, tmp->context, tmp->exten, 1, NULL)) { | ||
3685 | 716 | ast_log(LOG_NOTICE, "No such extension/context %s@%s creating local channel\n", tmp->exten, tmp->context); | ||
3686 | 717 | tmp = local_pvt_destroy(tmp); | ||
3687 | 718 | } else { | ||
3688 | 719 | #endif | ||
3689 | 720 | /* Add to list */ | ||
3690 | 721 | AST_LIST_LOCK(&locals); | ||
3691 | 722 | AST_LIST_INSERT_HEAD(&locals, tmp, list); | ||
3692 | 723 | AST_LIST_UNLOCK(&locals); | ||
3693 | 724 | #if 0 | ||
3694 | 725 | } | ||
3695 | 726 | #endif | ||
3696 | 727 | |||
3697 | 728 | return tmp; | ||
3698 | 729 | } | ||
3699 | 730 | |||
3700 | 731 | /*! \brief Start new local channel */ | ||
3701 | 732 | static struct ast_channel *local_new(struct local_pvt *p, int state) | ||
3702 | 733 | { | ||
3703 | 734 | struct ast_channel *tmp = NULL, *tmp2 = NULL; | ||
3704 | 735 | int randnum = ast_random() & 0xffff, fmt = 0; | ||
3705 | 736 | const char *t; | ||
3706 | 737 | int ama; | ||
3707 | 738 | |||
3708 | 739 | /* Allocate two new Asterisk channels */ | ||
3709 | 740 | /* safe accountcode */ | ||
3710 | 741 | if (p->owner && p->owner->accountcode) | ||
3711 | 742 | t = p->owner->accountcode; | ||
3712 | 743 | else | ||
3713 | 744 | t = ""; | ||
3714 | 745 | |||
3715 | 746 | if (p->owner) | ||
3716 | 747 | ama = p->owner->amaflags; | ||
3717 | 748 | else | ||
3718 | 749 | ama = 0; | ||
3719 | 750 | if (!(tmp = ast_channel_alloc(1, state, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x;1", p->exten, p->context, randnum)) | ||
3720 | 751 | || !(tmp2 = ast_channel_alloc(1, AST_STATE_RING, 0, 0, t, p->exten, p->context, ama, "Local/%s@%s-%04x;2", p->exten, p->context, randnum))) { | ||
3721 | 752 | if (tmp) | ||
3722 | 753 | ast_channel_free(tmp); | ||
3723 | 754 | if (tmp2) | ||
3724 | 755 | ast_channel_free(tmp2); | ||
3725 | 756 | ast_log(LOG_WARNING, "Unable to allocate channel structure(s)\n"); | ||
3726 | 757 | return NULL; | ||
3727 | 758 | } | ||
3728 | 759 | |||
3729 | 760 | tmp2->tech = tmp->tech = &local_tech; | ||
3730 | 761 | |||
3731 | 762 | tmp->nativeformats = p->reqformat; | ||
3732 | 763 | tmp2->nativeformats = p->reqformat; | ||
3733 | 764 | |||
3734 | 765 | /* Determine our read/write format and set it on each channel */ | ||
3735 | 766 | fmt = ast_best_codec(p->reqformat); | ||
3736 | 767 | tmp->writeformat = fmt; | ||
3737 | 768 | tmp2->writeformat = fmt; | ||
3738 | 769 | tmp->rawwriteformat = fmt; | ||
3739 | 770 | tmp2->rawwriteformat = fmt; | ||
3740 | 771 | tmp->readformat = fmt; | ||
3741 | 772 | tmp2->readformat = fmt; | ||
3742 | 773 | tmp->rawreadformat = fmt; | ||
3743 | 774 | tmp2->rawreadformat = fmt; | ||
3744 | 775 | |||
3745 | 776 | tmp->tech_pvt = p; | ||
3746 | 777 | tmp2->tech_pvt = p; | ||
3747 | 778 | |||
3748 | 779 | p->owner = tmp; | ||
3749 | 780 | p->chan = tmp2; | ||
3750 | 781 | p->u_owner = ast_module_user_add(p->owner); | ||
3751 | 782 | p->u_chan = ast_module_user_add(p->chan); | ||
3752 | 783 | |||
3753 | 784 | ast_copy_string(tmp->context, p->context, sizeof(tmp->context)); | ||
3754 | 785 | ast_copy_string(tmp2->context, p->context, sizeof(tmp2->context)); | ||
3755 | 786 | ast_copy_string(tmp2->exten, p->exten, sizeof(tmp->exten)); | ||
3756 | 787 | tmp->priority = 1; | ||
3757 | 788 | tmp2->priority = 1; | ||
3758 | 789 | |||
3759 | 790 | ast_jb_configure(tmp, &p->jb_conf); | ||
3760 | 791 | |||
3761 | 792 | return tmp; | ||
3762 | 793 | } | ||
3763 | 794 | |||
3764 | 795 | /*! \brief Part of PBX interface */ | ||
3765 | 796 | static struct ast_channel *local_request(const char *type, int format, void *data, int *cause) | ||
3766 | 797 | { | ||
3767 | 798 | struct local_pvt *p = NULL; | ||
3768 | 799 | struct ast_channel *chan = NULL; | ||
3769 | 800 | |||
3770 | 801 | /* Allocate a new private structure and then Asterisk channel */ | ||
3771 | 802 | if ((p = local_alloc(data, format))) { | ||
3772 | 803 | if (!(chan = local_new(p, AST_STATE_DOWN))) { | ||
3773 | 804 | AST_LIST_LOCK(&locals); | ||
3774 | 805 | AST_LIST_REMOVE(&locals, p, list); | ||
3775 | 806 | AST_LIST_UNLOCK(&locals); | ||
3776 | 807 | p = local_pvt_destroy(p); | ||
3777 | 808 | } | ||
3778 | 809 | } | ||
3779 | 810 | |||
3780 | 811 | return chan; | ||
3781 | 812 | } | ||
3782 | 813 | |||
3783 | 814 | /*! \brief CLI command "local show channels" */ | ||
3784 | 815 | static char *locals_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||
3785 | 816 | { | ||
3786 | 817 | struct local_pvt *p = NULL; | ||
3787 | 818 | |||
3788 | 819 | switch (cmd) { | ||
3789 | 820 | case CLI_INIT: | ||
3790 | 821 | e->command = "local show channels"; | ||
3791 | 822 | e->usage = | ||
3792 | 823 | "Usage: local show channels\n" | ||
3793 | 824 | " Provides summary information on active local proxy channels.\n"; | ||
3794 | 825 | return NULL; | ||
3795 | 826 | case CLI_GENERATE: | ||
3796 | 827 | return NULL; | ||
3797 | 828 | } | ||
3798 | 829 | |||
3799 | 830 | if (a->argc != 3) | ||
3800 | 831 | return CLI_SHOWUSAGE; | ||
3801 | 832 | |||
3802 | 833 | AST_LIST_LOCK(&locals); | ||
3803 | 834 | if (!AST_LIST_EMPTY(&locals)) { | ||
3804 | 835 | AST_LIST_TRAVERSE(&locals, p, list) { | ||
3805 | 836 | ast_mutex_lock(&p->lock); | ||
3806 | 837 | ast_cli(a->fd, "%s -- %s@%s\n", p->owner ? p->owner->name : "<unowned>", p->exten, p->context); | ||
3807 | 838 | ast_mutex_unlock(&p->lock); | ||
3808 | 839 | } | ||
3809 | 840 | } else | ||
3810 | 841 | ast_cli(a->fd, "No local channels in use\n"); | ||
3811 | 842 | AST_LIST_UNLOCK(&locals); | ||
3812 | 843 | |||
3813 | 844 | return CLI_SUCCESS; | ||
3814 | 845 | } | ||
3815 | 846 | |||
3816 | 847 | static struct ast_cli_entry cli_local[] = { | ||
3817 | 848 | AST_CLI_DEFINE(locals_show, "List status of local channels"), | ||
3818 | 849 | }; | ||
3819 | 850 | |||
3820 | 851 | /*! \brief Load module into PBX, register channel */ | ||
3821 | 852 | static int load_module(void) | ||
3822 | 853 | { | ||
3823 | 854 | /* Make sure we can register our channel type */ | ||
3824 | 855 | if (ast_channel_register(&local_tech)) { | ||
3825 | 856 | ast_log(LOG_ERROR, "Unable to register channel class 'Local'\n"); | ||
3826 | 857 | return AST_MODULE_LOAD_FAILURE; | ||
3827 | 858 | } | ||
3828 | 859 | ast_cli_register_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry)); | ||
3829 | 860 | return AST_MODULE_LOAD_SUCCESS; | ||
3830 | 861 | } | ||
3831 | 862 | |||
3832 | 863 | /*! \brief Unload the local proxy channel from Asterisk */ | ||
3833 | 864 | static int unload_module(void) | ||
3834 | 865 | { | ||
3835 | 866 | struct local_pvt *p = NULL; | ||
3836 | 867 | |||
3837 | 868 | /* First, take us out of the channel loop */ | ||
3838 | 869 | ast_cli_unregister_multiple(cli_local, sizeof(cli_local) / sizeof(struct ast_cli_entry)); | ||
3839 | 870 | ast_channel_unregister(&local_tech); | ||
3840 | 871 | if (!AST_LIST_LOCK(&locals)) { | ||
3841 | 872 | /* Hangup all interfaces if they have an owner */ | ||
3842 | 873 | AST_LIST_TRAVERSE(&locals, p, list) { | ||
3843 | 874 | if (p->owner) | ||
3844 | 875 | ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD); | ||
3845 | 876 | } | ||
3846 | 877 | AST_LIST_UNLOCK(&locals); | ||
3847 | 878 | } else { | ||
3848 | 879 | ast_log(LOG_WARNING, "Unable to lock the monitor\n"); | ||
3849 | 880 | return -1; | ||
3850 | 881 | } | ||
3851 | 882 | return 0; | ||
3852 | 883 | } | ||
3853 | 884 | |||
3854 | 885 | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Local Proxy Channel (Note: used internally by other modules)"); | ||
3855 | 0 | 886 | ||
3856 | === modified file 'channels/chan_local.c' | |||
3857 | --- channels/chan_local.c 2010-02-16 14:08:54 +0000 | |||
3858 | +++ channels/chan_local.c 2011-01-20 23:43:55 +0000 | |||
3859 | @@ -251,9 +251,10 @@ | |||
3860 | 251 | } | 251 | } |
3861 | 252 | 252 | ||
3862 | 253 | if (other) { | 253 | if (other) { |
3866 | 254 | if (other->pbx || other->_bridge || !ast_strlen_zero(other->appl)) { | 254 | if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_RINGING) { |
3867 | 255 | ast_queue_frame(other, f); | 255 | ast_setstate(other, AST_STATE_RINGING); |
3868 | 256 | } /* else the frame won't go anywhere */ | 256 | } |
3869 | 257 | ast_queue_frame(other, f); | ||
3870 | 257 | ast_channel_unlock(other); | 258 | ast_channel_unlock(other); |
3871 | 258 | } | 259 | } |
3872 | 259 | 260 | ||
3873 | 260 | 261 | ||
3874 | === modified file 'debian/changelog' | |||
3875 | --- debian/changelog 2010-04-13 16:27:27 +0000 | |||
3876 | +++ debian/changelog 2011-01-20 23:43:55 +0000 | |||
3877 | @@ -1,3 +1,30 @@ | |||
3878 | 1 | asterisk (1:1.6.2.5-0ubuntu1.3) lucid-security; urgency=low | ||
3879 | 2 | |||
3880 | 3 | * SECURITY UPDATE: Stack buffer overflow in SIP channel driver. (LP: #705014) | ||
3881 | 4 | - debian/patches/AST-2011-001-1.6.2: The size of the output buffer passed | ||
3882 | 5 | to the ast_uri_encode function is now properly respected in main/utils.c. | ||
3883 | 6 | Patch courtesy of upstream. | ||
3884 | 7 | - CVE-2011-0495 | ||
3885 | 8 | |||
3886 | 9 | -- Dave Walker (Daviey) <DaveWalker@ubuntu.com> Thu, 20 Jan 2011 23:31:55 +0000 | ||
3887 | 10 | |||
3888 | 11 | asterisk (1:1.6.2.5-0ubuntu1.2) lucid-proposed; urgency=low | ||
3889 | 12 | |||
3890 | 13 | * debian/patches/unattended_fix: Fix attended transfer call in 1.2.6.5 | ||
3891 | 14 | Patch based on Asterisk project's upstream patch (between 1.2.6.5 and | ||
3892 | 15 | 1.2.6.6 where issue is declared to be fixed see issue 16816 on Asterisk | ||
3893 | 16 | bug tracker). (LP: #686625) | ||
3894 | 17 | |||
3895 | 18 | -- Lionel Porcheron <lionel@alveonet.org> Mon, 06 Dec 2010 16:56:12 +0100 | ||
3896 | 19 | |||
3897 | 20 | asterisk (1:1.6.2.5-0ubuntu1.1) lucid-proposed; urgency=low | ||
3898 | 21 | |||
3899 | 22 | * debian/patches/dnsmgr-A-SRV-handling: Resolve handling of A and SRV | ||
3900 | 23 | record changes and problem with multiple A/SRV records returned. | ||
3901 | 24 | Patch based on Asterisk project's upstream patch. (LP: #605358) | ||
3902 | 25 | |||
3903 | 26 | -- Dave Walker (Daviey) <DaveWalker@ubuntu.com> Wed, 14 Jul 2010 11:40:55 +0100 | ||
3904 | 27 | |||
3905 | 1 | asterisk (1:1.6.2.5-0ubuntu1) lucid; urgency=low | 28 | asterisk (1:1.6.2.5-0ubuntu1) lucid; urgency=low |
3906 | 2 | 29 | ||
3907 | 3 | * New upstream bugfix release (1.6.2.5) | 30 | * New upstream bugfix release (1.6.2.5) |
3908 | 4 | 31 | ||
3909 | === added file 'debian/patches/AST-2011-001-1.6.2' | |||
3910 | --- debian/patches/AST-2011-001-1.6.2 1970-01-01 00:00:00 +0000 | |||
3911 | +++ debian/patches/AST-2011-001-1.6.2 2011-01-20 23:43:55 +0000 | |||
3912 | @@ -0,0 +1,52 @@ | |||
3913 | 1 | Description: Stack buffer overflow in SIP channel driver. | ||
3914 | 2 | Prevent buffer overflows in ast_uri_encode() | ||
3915 | 3 | Origin: upstream, http://downloads.asterisk.org/pub/security/AST-2011-001.html | ||
3916 | 4 | Bug-Ubuntu: https://launchpad.net/bugs/705014 | ||
3917 | 5 | Bug-Debian: http://bugs.debian.org/610487 | ||
3918 | 6 | Applied-Upstream: http://svnview.digium.com/svn/asterisk?view=revision&revision=301307 | ||
3919 | 7 | Last-Update: 2011-01-20 | ||
3920 | 8 | |||
3921 | 9 | --- a/main/utils.c | ||
3922 | 10 | +++ b/main/utils.c | ||
3923 | 11 | @@ -385,28 +385,27 @@ | ||
3924 | 12 | char *reserved = ";/?:@&=+$,# "; /* Reserved chars */ | ||
3925 | 13 | |||
3926 | 14 | const char *ptr = string; /* Start with the string */ | ||
3927 | 15 | - char *out = NULL; | ||
3928 | 16 | - char *buf = NULL; | ||
3929 | 17 | + char *out = outbuf; | ||
3930 | 18 | |||
3931 | 19 | - ast_copy_string(outbuf, string, buflen); | ||
3932 | 20 | - | ||
3933 | 21 | - /* If there's no characters to convert, just go through and don't do anything */ | ||
3934 | 22 | - while (*ptr) { | ||
3935 | 23 | + /* If there's no characters to convert, just go through and copy the string */ | ||
3936 | 24 | + while (*ptr && out - outbuf < buflen - 1) { | ||
3937 | 25 | if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) { | ||
3938 | 26 | - /* Oops, we need to start working here */ | ||
3939 | 27 | - if (!buf) { | ||
3940 | 28 | - buf = outbuf; | ||
3941 | 29 | - out = buf + (ptr - string) ; /* Set output ptr */ | ||
3942 | 30 | + if (out - outbuf >= buflen - 3) { | ||
3943 | 31 | + break; | ||
3944 | 32 | } | ||
3945 | 33 | + | ||
3946 | 34 | out += sprintf(out, "%%%02x", (unsigned char) *ptr); | ||
3947 | 35 | - } else if (buf) { | ||
3948 | 36 | - *out = *ptr; /* Continue copying the string */ | ||
3949 | 37 | + } else { | ||
3950 | 38 | + *out = *ptr; /* copy the character */ | ||
3951 | 39 | out++; | ||
3952 | 40 | - } | ||
3953 | 41 | + } | ||
3954 | 42 | ptr++; | ||
3955 | 43 | } | ||
3956 | 44 | - if (buf) | ||
3957 | 45 | + | ||
3958 | 46 | + if (buflen) { | ||
3959 | 47 | *out = '\0'; | ||
3960 | 48 | + } | ||
3961 | 49 | + | ||
3962 | 50 | return outbuf; | ||
3963 | 51 | } | ||
3964 | 52 | |||
3965 | 0 | 53 | ||
3966 | === added file 'debian/patches/dnsmgr-A-SRV-handling' | |||
3967 | --- debian/patches/dnsmgr-A-SRV-handling 1970-01-01 00:00:00 +0000 | |||
3968 | +++ debian/patches/dnsmgr-A-SRV-handling 2011-01-20 23:43:55 +0000 | |||
3969 | @@ -0,0 +1,132 @@ | |||
3970 | 1 | Description: Resolve handling of A and SRV record changes and problem with multiple A/SRV records returned. | ||
3971 | 2 | Origin: upstream, https://issues.asterisk.org/file_download.php?file_id=25360&type=bug | ||
3972 | 3 | Bug: https://issues.asterisk.org/view.php?id=15827 | ||
3973 | 4 | Bug-Ubuntu: https://launchpad.net/bugs/605358 | ||
3974 | 5 | Last-Update: 2010-07-14 | ||
3975 | 6 | --- a/include/asterisk/dnsmgr.h | ||
3976 | 7 | +++ b/include/asterisk/dnsmgr.h | ||
3977 | 8 | @@ -42,12 +42,12 @@ | ||
3978 | 9 | * | ||
3979 | 10 | * \param name the hostname | ||
3980 | 11 | * \param result where the DNS manager should store the IP address as it refreshes it. | ||
3981 | 12 | - * it. | ||
3982 | 13 | + * \param service SRV prefix (optional, set to NULL to disable SRV) | ||
3983 | 14 | * | ||
3984 | 15 | * This function allocates a new DNS manager entry object, and fills it with the | ||
3985 | 16 | * provided hostname and IP address. This function does not force an initial lookup | ||
3986 | 17 | - * of the IP address. So, generally, this should be used when the initial address | ||
3987 | 18 | - * is already known. | ||
3988 | 19 | + * of the IP address. So, this should be used when the initial address | ||
3989 | 20 | + * is already known and stored in result. | ||
3990 | 21 | * | ||
3991 | 22 | * \return a DNS manager entry | ||
3992 | 23 | * \version 1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number | ||
3993 | 24 | @@ -69,6 +69,7 @@ | ||
3994 | 25 | * \param name the hostname | ||
3995 | 26 | * \param result where to store the IP address as the DNS manager refreshes it | ||
3996 | 27 | * \param dnsmgr Where to store the allocate DNS manager entry | ||
3997 | 28 | + * \param service SRV prefix (optional, set to NULL to disable SRV) | ||
3998 | 29 | * | ||
3999 | 30 | * This function allocates a new DNS manager entry object, and fills it with | ||
4000 | 31 | * the provided hostname and IP address. This function _does_ force an initial | ||
4001 | 32 | --- a/main/acl.c | ||
4002 | 33 | +++ b/main/acl.c | ||
4003 | 34 | @@ -387,6 +387,7 @@ | ||
4004 | 35 | } | ||
4005 | 36 | } | ||
4006 | 37 | if ((hp = ast_gethostbyname(value, &ahp))) { | ||
4007 | 38 | + sin->sin_family = hp->h_addrtype; | ||
4008 | 39 | memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr)); | ||
4009 | 40 | } else { | ||
4010 | 41 | ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value); | ||
4011 | 42 | --- a/main/dnsmgr.c | ||
4012 | 43 | +++ b/main/dnsmgr.c | ||
4013 | 44 | @@ -52,8 +52,6 @@ | ||
4014 | 45 | struct ast_dnsmgr_entry { | ||
4015 | 46 | /*! where we will store the resulting IP address and port number */ | ||
4016 | 47 | struct sockaddr_in *result; | ||
4017 | 48 | - /*! the last result, used to check if address/port has changed */ | ||
4018 | 49 | - struct sockaddr_in last; | ||
4019 | 50 | /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */ | ||
4020 | 51 | char *service; | ||
4021 | 52 | /*! Set to 1 if the entry changes */ | ||
4022 | 53 | @@ -85,6 +83,9 @@ | ||
4023 | 54 | .verbose = 0, | ||
4024 | 55 | }; | ||
4025 | 56 | |||
4026 | 57 | +/* | ||
4027 | 58 | + * Allocate a new DNS manager entry | ||
4028 | 59 | + */ | ||
4029 | 60 | struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service) | ||
4030 | 61 | { | ||
4031 | 62 | struct ast_dnsmgr_entry *entry; | ||
4032 | 63 | @@ -96,7 +97,6 @@ | ||
4033 | 64 | entry->result = result; | ||
4034 | 65 | ast_mutex_init(&entry->lock); | ||
4035 | 66 | strcpy(entry->name, name); | ||
4036 | 67 | - memcpy(&entry->last, result, sizeof(entry->last)); | ||
4037 | 68 | if (service) { | ||
4038 | 69 | entry->service = ((char *) entry) + sizeof(*entry) + strlen(name); | ||
4039 | 70 | strcpy(entry->service, service); | ||
4040 | 71 | @@ -109,6 +109,9 @@ | ||
4041 | 72 | return entry; | ||
4042 | 73 | } | ||
4043 | 74 | |||
4044 | 75 | +/* | ||
4045 | 76 | + * Free a DNS manager entry | ||
4046 | 77 | + */ | ||
4047 | 78 | void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry) | ||
4048 | 79 | { | ||
4049 | 80 | if (!entry) | ||
4050 | 81 | @@ -123,6 +126,9 @@ | ||
4051 | 82 | ast_free(entry); | ||
4052 | 83 | } | ||
4053 | 84 | |||
4054 | 85 | +/* | ||
4055 | 86 | + * Allocate a new DNS manager entry and perform the initial lookup before returning | ||
4056 | 87 | + */ | ||
4057 | 88 | int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service) | ||
4058 | 89 | { | ||
4059 | 90 | if (ast_strlen_zero(name) || !result || !dnsmgr) | ||
4060 | 91 | @@ -133,8 +139,10 @@ | ||
4061 | 92 | |||
4062 | 93 | /* if it's actually an IP address and not a name, | ||
4063 | 94 | there's no need for a managed lookup */ | ||
4064 | 95 | - if (inet_aton(name, &result->sin_addr)) | ||
4065 | 96 | + if (inet_aton(name, &result->sin_addr)) { | ||
4066 | 97 | + result->sin_family = AF_INET; | ||
4067 | 98 | return 0; | ||
4068 | 99 | + } | ||
4069 | 100 | |||
4070 | 101 | ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name); | ||
4071 | 102 | |||
4072 | 103 | @@ -151,7 +159,7 @@ | ||
4073 | 104 | } | ||
4074 | 105 | |||
4075 | 106 | /* | ||
4076 | 107 | - * Refresh a dnsmgr entry | ||
4077 | 108 | + * Force a refresh of a dnsmgr entry | ||
4078 | 109 | */ | ||
4079 | 110 | static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose) | ||
4080 | 111 | { | ||
4081 | 112 | @@ -164,15 +172,15 @@ | ||
4082 | 113 | if (verbose) | ||
4083 | 114 | ast_verb(3, "refreshing '%s'\n", entry->name); | ||
4084 | 115 | |||
4085 | 116 | - tmp.sin_port = entry->last.sin_port; | ||
4086 | 117 | + memset(&tmp, 0, sizeof(tmp)); | ||
4087 | 118 | + tmp.sin_port = entry->result->sin_port; | ||
4088 | 119 | |||
4089 | 120 | - if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, &entry->last)) { | ||
4090 | 121 | - ast_copy_string(iabuf, ast_inet_ntoa(entry->last.sin_addr), sizeof(iabuf)); | ||
4091 | 122 | + if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, entry->result)) { | ||
4092 | 123 | + ast_copy_string(iabuf, ast_inet_ntoa(entry->result->sin_addr), sizeof(iabuf)); | ||
4093 | 124 | ast_copy_string(iabuf2, ast_inet_ntoa(tmp.sin_addr), sizeof(iabuf2)); | ||
4094 | 125 | ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s:%d to %s:%d\n", | ||
4095 | 126 | - entry->name, iabuf, ntohs(entry->last.sin_port), iabuf2, ntohs(tmp.sin_port)); | ||
4096 | 127 | + entry->name, iabuf, ntohs(entry->result->sin_port), iabuf2, ntohs(tmp.sin_port)); | ||
4097 | 128 | *entry->result = tmp; | ||
4098 | 129 | - entry->last = tmp; | ||
4099 | 130 | changed = entry->changed = 1; | ||
4100 | 131 | } | ||
4101 | 132 | |||
4102 | 0 | 133 | ||
4103 | === modified file 'debian/patches/series' | |||
4104 | --- debian/patches/series 2010-02-16 14:08:54 +0000 | |||
4105 | +++ debian/patches/series 2011-01-20 23:43:55 +0000 | |||
4106 | @@ -17,3 +17,6 @@ | |||
4107 | 17 | dahdi_ptmp_nt | 17 | dahdi_ptmp_nt |
4108 | 18 | dahdi_pri_debug_spannums | 18 | dahdi_pri_debug_spannums |
4109 | 19 | sound_files | 19 | sound_files |
4110 | 20 | dnsmgr-A-SRV-handling | ||
4111 | 21 | unattended_fix | ||
4112 | 22 | AST-2011-001-1.6.2 | ||
4113 | 20 | 23 | ||
4114 | === added file 'debian/patches/unattended_fix' | |||
4115 | --- debian/patches/unattended_fix 1970-01-01 00:00:00 +0000 | |||
4116 | +++ debian/patches/unattended_fix 2011-01-20 23:43:55 +0000 | |||
4117 | @@ -0,0 +1,18 @@ | |||
4118 | 1 | Index: asterisk-1.6.2.5/channels/chan_local.c | ||
4119 | 2 | =================================================================== | ||
4120 | 3 | --- asterisk-1.6.2.5.orig/channels/chan_local.c 2010-12-06 16:37:54.000000000 +0100 | ||
4121 | 4 | +++ asterisk-1.6.2.5/channels/chan_local.c 2010-12-06 16:54:56.000000000 +0100 | ||
4122 | 5 | @@ -251,9 +251,10 @@ | ||
4123 | 6 | } | ||
4124 | 7 | |||
4125 | 8 | if (other) { | ||
4126 | 9 | - if (other->pbx || other->_bridge || !ast_strlen_zero(other->appl)) { | ||
4127 | 10 | - ast_queue_frame(other, f); | ||
4128 | 11 | - } /* else the frame won't go anywhere */ | ||
4129 | 12 | + if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_RINGING) { | ||
4130 | 13 | + ast_setstate(other, AST_STATE_RINGING); | ||
4131 | 14 | + } | ||
4132 | 15 | + ast_queue_frame(other, f); | ||
4133 | 16 | ast_channel_unlock(other); | ||
4134 | 17 | } | ||
4135 | 18 | |||
4136 | 0 | 19 | ||
4137 | === modified file 'include/asterisk/dnsmgr.h' | |||
4138 | --- include/asterisk/dnsmgr.h 2009-03-29 22:21:47 +0000 | |||
4139 | +++ include/asterisk/dnsmgr.h 2011-01-20 23:43:55 +0000 | |||
4140 | @@ -42,12 +42,12 @@ | |||
4141 | 42 | * | 42 | * |
4142 | 43 | * \param name the hostname | 43 | * \param name the hostname |
4143 | 44 | * \param result where the DNS manager should store the IP address as it refreshes it. | 44 | * \param result where the DNS manager should store the IP address as it refreshes it. |
4145 | 45 | * it. | 45 | * \param service SRV prefix (optional, set to NULL to disable SRV) |
4146 | 46 | * | 46 | * |
4147 | 47 | * This function allocates a new DNS manager entry object, and fills it with the | 47 | * This function allocates a new DNS manager entry object, and fills it with the |
4148 | 48 | * provided hostname and IP address. This function does not force an initial lookup | 48 | * provided hostname and IP address. This function does not force an initial lookup |
4151 | 49 | * of the IP address. So, generally, this should be used when the initial address | 49 | * of the IP address. So, this should be used when the initial address |
4152 | 50 | * is already known. | 50 | * is already known and stored in result. |
4153 | 51 | * | 51 | * |
4154 | 52 | * \return a DNS manager entry | 52 | * \return a DNS manager entry |
4155 | 53 | * \version 1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number | 53 | * \version 1.6.1 result changed from struct in_addr to struct sockaddr_in to store port number |
4156 | @@ -69,6 +69,7 @@ | |||
4157 | 69 | * \param name the hostname | 69 | * \param name the hostname |
4158 | 70 | * \param result where to store the IP address as the DNS manager refreshes it | 70 | * \param result where to store the IP address as the DNS manager refreshes it |
4159 | 71 | * \param dnsmgr Where to store the allocate DNS manager entry | 71 | * \param dnsmgr Where to store the allocate DNS manager entry |
4160 | 72 | * \param service SRV prefix (optional, set to NULL to disable SRV) | ||
4161 | 72 | * | 73 | * |
4162 | 73 | * This function allocates a new DNS manager entry object, and fills it with | 74 | * This function allocates a new DNS manager entry object, and fills it with |
4163 | 74 | * the provided hostname and IP address. This function _does_ force an initial | 75 | * the provided hostname and IP address. This function _does_ force an initial |
4164 | 75 | 76 | ||
4165 | === modified file 'main/acl.c' | |||
4166 | --- main/acl.c 2010-04-13 16:27:27 +0000 | |||
4167 | +++ main/acl.c 2011-01-20 23:43:55 +0000 | |||
4168 | @@ -387,6 +387,7 @@ | |||
4169 | 387 | } | 387 | } |
4170 | 388 | } | 388 | } |
4171 | 389 | if ((hp = ast_gethostbyname(value, &ahp))) { | 389 | if ((hp = ast_gethostbyname(value, &ahp))) { |
4172 | 390 | sin->sin_family = hp->h_addrtype; | ||
4173 | 390 | memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr)); | 391 | memcpy(&sin->sin_addr, hp->h_addr, sizeof(sin->sin_addr)); |
4174 | 391 | } else { | 392 | } else { |
4175 | 392 | ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value); | 393 | ast_log(LOG_WARNING, "Unable to lookup '%s'\n", value); |
4176 | 393 | 394 | ||
4177 | === modified file 'main/dnsmgr.c' | |||
4178 | --- main/dnsmgr.c 2010-02-16 14:08:54 +0000 | |||
4179 | +++ main/dnsmgr.c 2011-01-20 23:43:55 +0000 | |||
4180 | @@ -52,8 +52,6 @@ | |||
4181 | 52 | struct ast_dnsmgr_entry { | 52 | struct ast_dnsmgr_entry { |
4182 | 53 | /*! where we will store the resulting IP address and port number */ | 53 | /*! where we will store the resulting IP address and port number */ |
4183 | 54 | struct sockaddr_in *result; | 54 | struct sockaddr_in *result; |
4184 | 55 | /*! the last result, used to check if address/port has changed */ | ||
4185 | 56 | struct sockaddr_in last; | ||
4186 | 57 | /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */ | 55 | /*! SRV record to lookup, if provided. Composed of service, protocol, and domain name: _Service._Proto.Name */ |
4187 | 58 | char *service; | 56 | char *service; |
4188 | 59 | /*! Set to 1 if the entry changes */ | 57 | /*! Set to 1 if the entry changes */ |
4189 | @@ -85,6 +83,9 @@ | |||
4190 | 85 | .verbose = 0, | 83 | .verbose = 0, |
4191 | 86 | }; | 84 | }; |
4192 | 87 | 85 | ||
4193 | 86 | /* | ||
4194 | 87 | * Allocate a new DNS manager entry | ||
4195 | 88 | */ | ||
4196 | 88 | struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service) | 89 | struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct sockaddr_in *result, const char *service) |
4197 | 89 | { | 90 | { |
4198 | 90 | struct ast_dnsmgr_entry *entry; | 91 | struct ast_dnsmgr_entry *entry; |
4199 | @@ -96,7 +97,6 @@ | |||
4200 | 96 | entry->result = result; | 97 | entry->result = result; |
4201 | 97 | ast_mutex_init(&entry->lock); | 98 | ast_mutex_init(&entry->lock); |
4202 | 98 | strcpy(entry->name, name); | 99 | strcpy(entry->name, name); |
4203 | 99 | memcpy(&entry->last, result, sizeof(entry->last)); | ||
4204 | 100 | if (service) { | 100 | if (service) { |
4205 | 101 | entry->service = ((char *) entry) + sizeof(*entry) + strlen(name); | 101 | entry->service = ((char *) entry) + sizeof(*entry) + strlen(name); |
4206 | 102 | strcpy(entry->service, service); | 102 | strcpy(entry->service, service); |
4207 | @@ -109,6 +109,9 @@ | |||
4208 | 109 | return entry; | 109 | return entry; |
4209 | 110 | } | 110 | } |
4210 | 111 | 111 | ||
4211 | 112 | /* | ||
4212 | 113 | * Free a DNS manager entry | ||
4213 | 114 | */ | ||
4214 | 112 | void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry) | 115 | void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry) |
4215 | 113 | { | 116 | { |
4216 | 114 | if (!entry) | 117 | if (!entry) |
4217 | @@ -123,6 +126,9 @@ | |||
4218 | 123 | ast_free(entry); | 126 | ast_free(entry); |
4219 | 124 | } | 127 | } |
4220 | 125 | 128 | ||
4221 | 129 | /* | ||
4222 | 130 | * Allocate a new DNS manager entry and perform the initial lookup before returning | ||
4223 | 131 | */ | ||
4224 | 126 | int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service) | 132 | int ast_dnsmgr_lookup(const char *name, struct sockaddr_in *result, struct ast_dnsmgr_entry **dnsmgr, const char *service) |
4225 | 127 | { | 133 | { |
4226 | 128 | if (ast_strlen_zero(name) || !result || !dnsmgr) | 134 | if (ast_strlen_zero(name) || !result || !dnsmgr) |
4227 | @@ -133,8 +139,10 @@ | |||
4228 | 133 | 139 | ||
4229 | 134 | /* if it's actually an IP address and not a name, | 140 | /* if it's actually an IP address and not a name, |
4230 | 135 | there's no need for a managed lookup */ | 141 | there's no need for a managed lookup */ |
4232 | 136 | if (inet_aton(name, &result->sin_addr)) | 142 | if (inet_aton(name, &result->sin_addr)) { |
4233 | 143 | result->sin_family = AF_INET; | ||
4234 | 137 | return 0; | 144 | return 0; |
4235 | 145 | } | ||
4236 | 138 | 146 | ||
4237 | 139 | ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name); | 147 | ast_verb(4, "doing dnsmgr_lookup for '%s'\n", name); |
4238 | 140 | 148 | ||
4239 | @@ -151,7 +159,7 @@ | |||
4240 | 151 | } | 159 | } |
4241 | 152 | 160 | ||
4242 | 153 | /* | 161 | /* |
4244 | 154 | * Refresh a dnsmgr entry | 162 | * Force a refresh of a dnsmgr entry |
4245 | 155 | */ | 163 | */ |
4246 | 156 | static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose) | 164 | static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose) |
4247 | 157 | { | 165 | { |
4248 | @@ -164,15 +172,15 @@ | |||
4249 | 164 | if (verbose) | 172 | if (verbose) |
4250 | 165 | ast_verb(3, "refreshing '%s'\n", entry->name); | 173 | ast_verb(3, "refreshing '%s'\n", entry->name); |
4251 | 166 | 174 | ||
4253 | 167 | tmp.sin_port = entry->last.sin_port; | 175 | memset(&tmp, 0, sizeof(tmp)); |
4254 | 176 | tmp.sin_port = entry->result->sin_port; | ||
4255 | 168 | 177 | ||
4258 | 169 | if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, &entry->last)) { | 178 | if (!ast_get_ip_or_srv(&tmp, entry->name, entry->service) && inaddrcmp(&tmp, entry->result)) { |
4259 | 170 | ast_copy_string(iabuf, ast_inet_ntoa(entry->last.sin_addr), sizeof(iabuf)); | 179 | ast_copy_string(iabuf, ast_inet_ntoa(entry->result->sin_addr), sizeof(iabuf)); |
4260 | 171 | ast_copy_string(iabuf2, ast_inet_ntoa(tmp.sin_addr), sizeof(iabuf2)); | 180 | ast_copy_string(iabuf2, ast_inet_ntoa(tmp.sin_addr), sizeof(iabuf2)); |
4261 | 172 | ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s:%d to %s:%d\n", | 181 | ast_log(LOG_NOTICE, "dnssrv: host '%s' changed from %s:%d to %s:%d\n", |
4263 | 173 | entry->name, iabuf, ntohs(entry->last.sin_port), iabuf2, ntohs(tmp.sin_port)); | 182 | entry->name, iabuf, ntohs(entry->result->sin_port), iabuf2, ntohs(tmp.sin_port)); |
4264 | 174 | *entry->result = tmp; | 183 | *entry->result = tmp; |
4265 | 175 | entry->last = tmp; | ||
4266 | 176 | changed = entry->changed = 1; | 184 | changed = entry->changed = 1; |
4267 | 177 | } | 185 | } |
4268 | 178 | 186 | ||
4269 | 179 | 187 | ||
4270 | === modified file 'main/utils.c' | |||
4271 | --- main/utils.c 2010-02-16 14:08:54 +0000 | |||
4272 | +++ main/utils.c 2011-01-20 23:43:55 +0000 | |||
4273 | @@ -385,28 +385,27 @@ | |||
4274 | 385 | char *reserved = ";/?:@&=+$,# "; /* Reserved chars */ | 385 | char *reserved = ";/?:@&=+$,# "; /* Reserved chars */ |
4275 | 386 | 386 | ||
4276 | 387 | const char *ptr = string; /* Start with the string */ | 387 | const char *ptr = string; /* Start with the string */ |
4284 | 388 | char *out = NULL; | 388 | char *out = outbuf; |
4285 | 389 | char *buf = NULL; | 389 | |
4286 | 390 | 390 | /* If there's no characters to convert, just go through and copy the string */ | |
4287 | 391 | ast_copy_string(outbuf, string, buflen); | 391 | while (*ptr && out - outbuf < buflen - 1) { |
4281 | 392 | |||
4282 | 393 | /* If there's no characters to convert, just go through and don't do anything */ | ||
4283 | 394 | while (*ptr) { | ||
4288 | 395 | if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) { | 392 | if ((*ptr < 32) || (doreserved && strchr(reserved, *ptr))) { |
4293 | 396 | /* Oops, we need to start working here */ | 393 | if (out - outbuf >= buflen - 3) { |
4294 | 397 | if (!buf) { | 394 | break; |
4291 | 398 | buf = outbuf; | ||
4292 | 399 | out = buf + (ptr - string) ; /* Set output ptr */ | ||
4295 | 400 | } | 395 | } |
4296 | 396 | |||
4297 | 401 | out += sprintf(out, "%%%02x", (unsigned char) *ptr); | 397 | out += sprintf(out, "%%%02x", (unsigned char) *ptr); |
4300 | 402 | } else if (buf) { | 398 | } else { |
4301 | 403 | *out = *ptr; /* Continue copying the string */ | 399 | *out = *ptr; /* copy the character */ |
4302 | 404 | out++; | 400 | out++; |
4304 | 405 | } | 401 | } |
4305 | 406 | ptr++; | 402 | ptr++; |
4306 | 407 | } | 403 | } |
4308 | 408 | if (buf) | 404 | |
4309 | 405 | if (buflen) { | ||
4310 | 409 | *out = '\0'; | 406 | *out = '\0'; |
4311 | 407 | } | ||
4312 | 408 | |||
4313 | 410 | return outbuf; | 409 | return outbuf; |
4314 | 411 | } | 410 | } |
4315 | 412 | 411 |