Merge lp:~davewalker/ubuntu/lucid/asterisk/lp_705014 into lp:ubuntu/lucid/asterisk

Proposed by Dave Walker
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
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.

Description of the change

Waiting on 1:1.6.2.5-0ubuntu1.2 to clear -proposed, which has verification-done.

To post a comment you must log in.
Revision history for this message
Jamie Strandboge (jdstrand) :
review: Disapprove

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

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

Subscribers

People subscribed via source and target branches

to all changes: