[svn-commits] trunk r32817 - in /trunk: ./ channels/ include/asterisk/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Wed Jun 7 10:44:36 MST 2006


Author: russell
Date: Wed Jun  7 12:44:36 2006
New Revision: 32817

URL: http://svn.digium.com/view/asterisk?rev=32817&view=rev
Log:
add an updated patch that adds dnsmgr support for outgoing iax2 registrations
(issue #6305, by ivanfm, with mods)

Modified:
    trunk/channels/chan_iax2.c
    trunk/dnsmgr.c
    trunk/include/asterisk/dnsmgr.h

Modified: trunk/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_iax2.c?rev=32817&r1=32816&r2=32817&view=diff
==============================================================================
--- trunk/channels/chan_iax2.c (original)
+++ trunk/channels/chan_iax2.c Wed Jun  7 12:44:36 2006
@@ -416,6 +416,7 @@
 	int callno;				/*!< Associated call number if applicable */
 	struct sockaddr_in us;			/*!< Who the server thinks we are */
 	struct iax2_registry *next;
+	struct ast_dnsmgr_entry *dnsmgr;	/*!< DNS refresh manager */
 };
 
 static struct iax2_registry *registrations;
@@ -4304,8 +4305,8 @@
 
 static int iax2_show_registry(int fd, int argc, char *argv[])
 {
-#define FORMAT2 "%-20.20s  %-10.10s  %-20.20s %8.8s  %s\n"
-#define FORMAT "%-20.20s  %-10.10s  %-20.20s %8d  %s\n"
+#define FORMAT2 "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8.8s  %s\n"
+#define FORMAT  "%-20.20s  %-6.6s  %-10.10s  %-20.20s %8d  %s\n"
 	struct iax2_registry *reg = NULL;
 
 	char host[80];
@@ -4314,7 +4315,7 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	AST_LIST_LOCK(&peers);
-	ast_cli(fd, FORMAT2, "Host", "Username", "Perceived", "Refresh", "State");
+	ast_cli(fd, FORMAT2, "Host", "dnsmgr", "Username", "Perceived", "Refresh", "State");
 	for (reg = registrations;reg;reg = reg->next) {
 		snprintf(host, sizeof(host), "%s:%d", ast_inet_ntoa(iabuf, sizeof(iabuf), reg->addr.sin_addr), ntohs(reg->addr.sin_port));
 		if (reg->us.sin_addr.s_addr) 
@@ -4322,6 +4323,7 @@
 		else
 			ast_copy_string(perceived, "<Unregistered>", sizeof(perceived));
 		ast_cli(fd, FORMAT, host, 
+					(reg->dnsmgr) ? "Y" : "N", 
 					reg->username, perceived, reg->refresh, regstate2str(reg->regstate));
 	}
 	AST_LIST_UNLOCK(&peers);
@@ -5447,7 +5449,6 @@
 	char *porta;
 	char *stringp=NULL;
 	
-	struct ast_hostent ahp; struct hostent *hp;
 	if (!value)
 		return -1;
 	ast_copy_string(copy, value, sizeof(copy));
@@ -5469,20 +5470,18 @@
 		ast_log(LOG_WARNING, "%s is not a valid port number at line %d\n", porta, lineno);
 		return -1;
 	}
-	hp = ast_gethostbyname(hostname, &ahp);
-	if (!hp) {
-		ast_log(LOG_WARNING, "Host '%s' not found at line %d\n", hostname, lineno);
-		return -1;
-	}
 	if (!(reg = ast_calloc(1, sizeof(*reg))))
 		return -1;
+	if (ast_dnsmgr_lookup(hostname, &reg->addr.sin_addr, &reg->dnsmgr) < 0) {
+		free(reg);
+		return -1;
+	}
 	ast_copy_string(reg->username, username, sizeof(reg->username));
 	if (secret)
 		ast_copy_string(reg->secret, secret, sizeof(reg->secret));
 	reg->expire = -1;
 	reg->refresh = IAX_DEFAULT_REG_EXPIRE;
 	reg->addr.sin_family = AF_INET;
-	memcpy(&reg->addr.sin_addr, hp->h_addr, sizeof(&reg->addr.sin_addr));
 	reg->addr.sin_port = porta ? htons(atoi(porta)) : htons(IAX_DEFAULT_PORTNO);
 	reg->next = registrations;
 	reg->callno = 0;
@@ -7759,6 +7758,31 @@
 	struct iax_ie_data ied;
 	if (option_debug && iaxdebug)
 		ast_log(LOG_DEBUG, "Sending registration request for '%s'\n", reg->username);
+
+	if (reg->dnsmgr && 
+	    ((reg->regstate == REG_STATE_TIMEOUT) || !reg->addr.sin_addr.s_addr)) {
+		/* Maybe the IP has changed, force DNS refresh */
+		ast_dnsmgr_refresh(reg->dnsmgr);
+	}
+	
+	/*
+	 * if IP has Changed, free allocated call to create a new one with new IP
+	 * call has the pointer to IP and must be updated to the new one
+	 */
+	if (reg->dnsmgr && ast_dnsmgr_changed(reg->dnsmgr) && (reg->callno > 0)) {
+		iax2_destroy(reg->callno);
+		reg->callno = 0;
+	}
+	if (!reg->addr.sin_addr.s_addr) {
+		if (option_debug && iaxdebug)
+			ast_log(LOG_DEBUG, "Unable to send registration request for '%s' without IP address\n", reg->username);
+		/* Setup the next registration attempt */
+		if (reg->expire > -1)
+			ast_sched_del(sched, reg->expire);
+		reg->expire  = ast_sched_add(sched, (5 * reg->refresh / 6) * 1000, iax2_do_register_s, reg);
+		return -1;
+	}
+
 	if (!reg->callno) {
 		if (option_debug)
 			ast_log(LOG_DEBUG, "Allocate call number\n");
@@ -8651,6 +8675,8 @@
 			}
 			ast_mutex_unlock(&iaxsl[regl->callno]);
 		}
+		if (regl->dnsmgr)
+			ast_dnsmgr_release(regl->dnsmgr);
 		free(regl);
 	}
 	registrations = NULL;

Modified: trunk/dnsmgr.c
URL: http://svn.digium.com/view/asterisk/trunk/dnsmgr.c?rev=32817&r1=32816&r2=32817&view=diff
==============================================================================
--- trunk/dnsmgr.c (original)
+++ trunk/dnsmgr.c Wed Jun  7 12:44:36 2006
@@ -53,8 +53,15 @@
 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 @@
 		return NULL;
 
 	entry->result = result;
+	ast_mutex_init(&entry->lock);
 	strcpy(entry->name, name);
 
 	AST_LIST_LOCK(&entry_list);
@@ -104,6 +112,10 @@
 	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 @@
 		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,10 +146,66 @@
 		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)
@@ -155,8 +223,6 @@
 {
 	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 @@
 		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);
 

Modified: trunk/include/asterisk/dnsmgr.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/dnsmgr.h?rev=32817&r1=32816&r2=32817&view=diff
==============================================================================
--- trunk/include/asterisk/dnsmgr.h (original)
+++ trunk/include/asterisk/dnsmgr.h Wed Jun  7 12:44:36 2006
@@ -37,6 +37,24 @@
 
 int ast_dnsmgr_lookup(const char *name, struct in_addr *result, struct ast_dnsmgr_entry **dnsmgr);
 
+/*!
+ * \brief Force a refresh of a dnsmgr entry
+ *
+ * \retval non-zero if the result is different than the previous result
+ * \retval zero if the result is the same as the previous result 
+ */
+int ast_dnsmgr_refresh(struct ast_dnsmgr_entry *entry);
+
+/*!
+ * \brief Check is see if a dnsmgr entry has changed
+ *
+ * \retval non-zero if the dnsmgr entry has changed since the last call to
+ *                  this function
+ * \retval zero     if the dnsmgr entry has not changed since the last call to
+ *                  this function
+ */
+int ast_dnsmgr_changed(struct ast_dnsmgr_entry *entry);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif /* c_plusplus */



More information about the svn-commits mailing list