mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-11-03 20:38:59 +00:00 
			
		
		
		
	add an updated patch that adds dnsmgr support for outgoing iax2 registrations
(issue #6305, by ivanfm, with mods) git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@32817 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
		
							
								
								
									
										82
									
								
								dnsmgr.c
									
									
									
									
									
								
							
							
						
						
									
										82
									
								
								dnsmgr.c
									
									
									
									
									
								
							@@ -53,8 +53,15 @@ static int refresh_sched = -1;
 | 
			
		||||
static pthread_t refresh_thread = AST_PTHREADT_NULL;
 | 
			
		||||
 | 
			
		||||
struct ast_dnsmgr_entry {
 | 
			
		||||
	/*! where we will store the resulting address */
 | 
			
		||||
	struct in_addr *result;
 | 
			
		||||
	/*! the last result, used to check if address has changed */
 | 
			
		||||
	struct in_addr last;
 | 
			
		||||
	/*! Set to 1 if the entry changes */
 | 
			
		||||
	int changed:1;
 | 
			
		||||
	ast_mutex_t lock;
 | 
			
		||||
	AST_LIST_ENTRY(ast_dnsmgr_entry) list;
 | 
			
		||||
	/*! just 1 here, but we use calloc to allocate the correct size */
 | 
			
		||||
	char name[1];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -87,6 +94,7 @@ struct ast_dnsmgr_entry *ast_dnsmgr_get(const char *name, struct in_addr *result
 | 
			
		||||
		return NULL;
 | 
			
		||||
 | 
			
		||||
	entry->result = result;
 | 
			
		||||
	ast_mutex_init(&entry->lock);
 | 
			
		||||
	strcpy(entry->name, name);
 | 
			
		||||
 | 
			
		||||
	AST_LIST_LOCK(&entry_list);
 | 
			
		||||
@@ -104,6 +112,10 @@ void ast_dnsmgr_release(struct ast_dnsmgr_entry *entry)
 | 
			
		||||
	AST_LIST_LOCK(&entry_list);
 | 
			
		||||
	AST_LIST_REMOVE(&entry_list, entry, list);
 | 
			
		||||
	AST_LIST_UNLOCK(&entry_list);
 | 
			
		||||
	if (option_verbose > 3)
 | 
			
		||||
		ast_verbose(VERBOSE_PREFIX_4 "removing dns manager for '%s'\n", entry->name);
 | 
			
		||||
 | 
			
		||||
	ast_mutex_destroy(&entry->lock);
 | 
			
		||||
	free(entry);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -116,7 +128,7 @@ int ast_dnsmgr_lookup(const char *name, struct in_addr *result, struct ast_dnsmg
 | 
			
		||||
		return 0;
 | 
			
		||||
 | 
			
		||||
	if (option_verbose > 3)
 | 
			
		||||
		ast_verbose(VERBOSE_PREFIX_3 "doing lookup for '%s'\n", name);
 | 
			
		||||
		ast_verbose(VERBOSE_PREFIX_4 "doing dnsmgr_lookup for '%s'\n", name);
 | 
			
		||||
 | 
			
		||||
	/* if it's actually an IP address and not a name,
 | 
			
		||||
	   there's no need for a managed lookup */
 | 
			
		||||
@@ -134,12 +146,68 @@ int ast_dnsmgr_lookup(const char *name, struct in_addr *result, struct ast_dnsmg
 | 
			
		||||
		return 0;
 | 
			
		||||
	} else {
 | 
			
		||||
		if (option_verbose > 2)
 | 
			
		||||
			ast_verbose(VERBOSE_PREFIX_2 "adding manager for '%s'\n", name);
 | 
			
		||||
			ast_verbose(VERBOSE_PREFIX_2 "adding dns manager for '%s'\n", name);
 | 
			
		||||
		*dnsmgr = ast_dnsmgr_get(name, result);
 | 
			
		||||
		return !*dnsmgr;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Refresh a dnsmgr entry
 | 
			
		||||
 */
 | 
			
		||||
static int dnsmgr_refresh(struct ast_dnsmgr_entry *entry, int verbose)
 | 
			
		||||
{
 | 
			
		||||
	struct ast_hostent ahp;
 | 
			
		||||
	struct hostent *hp;
 | 
			
		||||
	char iabuf[INET_ADDRSTRLEN];
 | 
			
		||||
	char iabuf2[INET_ADDRSTRLEN];
 | 
			
		||||
	struct in_addr tmp;
 | 
			
		||||
	int changed = 0;
 | 
			
		||||
        
 | 
			
		||||
	ast_mutex_lock(&entry->lock);
 | 
			
		||||
	if (verbose && (option_verbose > 2))
 | 
			
		||||
		ast_verbose(VERBOSE_PREFIX_2 "refreshing '%s'\n", entry->name);
 | 
			
		||||
 | 
			
		||||
	if ((hp = ast_gethostbyname(entry->name, &ahp))) {
 | 
			
		||||
		/* check to see if it has changed, do callback if requested (where de callback is defined ????) */
 | 
			
		||||
		memcpy(&tmp, hp->h_addr, sizeof(tmp));
 | 
			
		||||
		if (tmp.s_addr != entry->last.s_addr) {
 | 
			
		||||
			ast_log(LOG_NOTICE, "host '%s' changed from %s to %s\n", 
 | 
			
		||||
				entry->name,
 | 
			
		||||
				ast_inet_ntoa(iabuf, sizeof(iabuf), entry->last),
 | 
			
		||||
				ast_inet_ntoa(iabuf2, sizeof(iabuf2), tmp));
 | 
			
		||||
			memcpy(entry->result, hp->h_addr, sizeof(entry->result));
 | 
			
		||||
			memcpy(&entry->last, hp->h_addr, sizeof(entry->last));
 | 
			
		||||
			changed = entry->changed = 1;
 | 
			
		||||
		} 
 | 
			
		||||
		
 | 
			
		||||
	}
 | 
			
		||||
	ast_mutex_unlock(&entry->lock);
 | 
			
		||||
	return changed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry)
 | 
			
		||||
{
 | 
			
		||||
	return dnsmgr_refresh(entry, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Check if dnsmgr entry has changed from since last call to this function
 | 
			
		||||
 */
 | 
			
		||||
int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry) 
 | 
			
		||||
{
 | 
			
		||||
	int changed;
 | 
			
		||||
 | 
			
		||||
	ast_mutex_lock(&entry->lock);
 | 
			
		||||
 | 
			
		||||
	changed = entry->changed;
 | 
			
		||||
	entry->changed = 0;
 | 
			
		||||
 | 
			
		||||
	ast_mutex_unlock(&entry->lock);
 | 
			
		||||
	
 | 
			
		||||
	return changed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *do_refresh(void *data)
 | 
			
		||||
{
 | 
			
		||||
	for (;;) {
 | 
			
		||||
@@ -155,8 +223,6 @@ static int refresh_list(void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct refresh_info *info = data;
 | 
			
		||||
	struct ast_dnsmgr_entry *entry;
 | 
			
		||||
	struct ast_hostent ahp;
 | 
			
		||||
	struct hostent *hp;
 | 
			
		||||
 | 
			
		||||
	/* if a refresh or reload is already in progress, exit now */
 | 
			
		||||
	if (ast_mutex_trylock(&refresh_lock)) {
 | 
			
		||||
@@ -172,13 +238,7 @@ static int refresh_list(void *data)
 | 
			
		||||
		if (info->regex_present && regexec(&info->filter, entry->name, 0, NULL, 0))
 | 
			
		||||
		    continue;
 | 
			
		||||
 | 
			
		||||
		if (info->verbose && (option_verbose > 2))
 | 
			
		||||
			ast_verbose(VERBOSE_PREFIX_2 "refreshing '%s'\n", entry->name);
 | 
			
		||||
 | 
			
		||||
		if ((hp = ast_gethostbyname(entry->name, &ahp))) {
 | 
			
		||||
			/* check to see if it has changed, do callback if requested */
 | 
			
		||||
			memcpy(entry->result, hp->h_addr, sizeof(entry->result));
 | 
			
		||||
		}
 | 
			
		||||
		dnsmgr_refresh(entry, info->verbose);
 | 
			
		||||
	}
 | 
			
		||||
	AST_LIST_UNLOCK(info->entries);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user