| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Funding provided by nic.at | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief DNS SRV Record Lookup Support for Asterisk | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-11-19 16:47:25 +01:00
										 |  |  |  * \arg See also \ref ast_extension_states "AstENUM" | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \note Funding provided by nic.at | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-15 16:20:16 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | #include <netinet/in.h>
 | 
					
						
							|  |  |  | #include <arpa/nameser.h>
 | 
					
						
							| 
									
										
										
										
											2007-02-18 15:03:42 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							| 
									
										
										
										
											2003-10-27 20:00:41 +00:00
										 |  |  | #include <arpa/nameser_compat.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | #include <resolv.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/srv.h"
 | 
					
						
							|  |  |  | #include "asterisk/dns.h"
 | 
					
						
							|  |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | #include "asterisk/linkedlists.h"
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-10-26 18:50:49 +00:00
										 |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | #undef T_SRV
 | 
					
						
							|  |  |  | #define T_SRV 33
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | struct srv_entry { | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 	unsigned short priority; | 
					
						
							|  |  |  | 	unsigned short weight; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	unsigned short port; | 
					
						
							|  |  |  | 	unsigned int weight_sum; | 
					
						
							|  |  |  | 	AST_LIST_ENTRY(srv_entry) list; | 
					
						
							|  |  |  | 	char host[1]; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | struct srv_context { | 
					
						
							|  |  |  | 	unsigned int have_weights:1; | 
					
						
							| 
									
										
										
										
											2010-01-19 00:28:49 +00:00
										 |  |  | 	struct srv_entry *prev; | 
					
						
							| 
									
										
										
										
											2010-04-09 14:37:50 +00:00
										 |  |  | 	unsigned int num_records; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry) entries; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result) | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	struct srv { | 
					
						
							|  |  |  | 		unsigned short priority; | 
					
						
							|  |  |  | 		unsigned short weight; | 
					
						
							|  |  |  | 		unsigned short port; | 
					
						
							| 
									
										
										
										
											2008-11-29 17:57:39 +00:00
										 |  |  | 	} __attribute__((__packed__)) *srv = (struct srv *) answer; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	char repl[256] = ""; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	struct srv_entry *entry; | 
					
						
							| 
									
										
										
										
											2003-08-13 23:56:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	if (len < sizeof(*srv)) | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	answer += sizeof(*srv); | 
					
						
							|  |  |  | 	len -= sizeof(*srv); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) { | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Failed to expand hostname\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	/* the magic value "." for the target domain means that this service
 | 
					
						
							|  |  |  | 	   is *NOT* available at the domain we searched */ | 
					
						
							|  |  |  | 	if (!strcmp(repl, ".")) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl)))) | 
					
						
							|  |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	entry->priority = ntohs(srv->priority); | 
					
						
							|  |  |  | 	entry->weight = ntohs(srv->weight); | 
					
						
							|  |  |  | 	entry->port = ntohs(srv->port); | 
					
						
							|  |  |  | 	strcpy(entry->host, repl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*result = entry; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2003-09-27 00:22:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-07 15:44:39 +00:00
										 |  |  | static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer) | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	struct srv_context *c = (struct srv_context *) context; | 
					
						
							|  |  |  | 	struct srv_entry *entry = NULL; | 
					
						
							|  |  |  | 	struct srv_entry *current; | 
					
						
							| 
									
										
										
										
											2003-08-13 23:56:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	if (parse_srv(answer, len, fullanswer, &entry)) | 
					
						
							| 
									
										
										
										
											2004-03-03 03:12:59 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (entry->weight) | 
					
						
							|  |  |  | 		c->have_weights = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) { | 
					
						
							|  |  |  | 		/* insert this entry just before the first existing
 | 
					
						
							|  |  |  | 		   entry with a higher priority */ | 
					
						
							|  |  |  | 		if (current->priority <= entry->priority) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-08 05:28:47 +00:00
										 |  |  | 		AST_LIST_INSERT_BEFORE_CURRENT(entry, list); | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 		entry = NULL; | 
					
						
							|  |  |  | 		break; | 
					
						
							| 
									
										
										
										
											2004-03-03 03:12:59 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							| 
									
										
										
										
											2003-08-13 23:56:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	/* if we didn't find a place to insert the entry before an existing
 | 
					
						
							|  |  |  | 	   entry, then just add it to the end */ | 
					
						
							|  |  |  | 	if (entry) | 
					
						
							|  |  |  | 		AST_LIST_INSERT_TAIL(&c->entries, entry, list); | 
					
						
							| 
									
										
										
										
											2003-08-13 23:56:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-08 21:00:01 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | /* Do the bizarre SRV record weight-handling algorithm
 | 
					
						
							|  |  |  |    involving sorting and random number generation... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    See RFC 2782 if you want know why this code does this | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | static void process_weights(struct srv_context *context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct srv_entry *current; | 
					
						
							|  |  |  | 	struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (AST_LIST_FIRST(&context->entries)) { | 
					
						
							|  |  |  | 		unsigned int random_weight; | 
					
						
							|  |  |  | 		unsigned int weight_sum; | 
					
						
							|  |  |  | 		unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority; | 
					
						
							|  |  |  | 		struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE; | 
					
						
							|  |  |  | 		weight_sum = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) { | 
					
						
							|  |  |  | 			if (current->priority != cur_priority) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-08 05:28:47 +00:00
										 |  |  | 			AST_LIST_MOVE_CURRENT(&temp_list, list); | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		while (AST_LIST_FIRST(&temp_list)) { | 
					
						
							|  |  |  | 			weight_sum = 0; | 
					
						
							|  |  |  | 			AST_LIST_TRAVERSE(&temp_list, current, list) | 
					
						
							|  |  |  | 				current->weight_sum = weight_sum += current->weight; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* if all the remaining entries have weight == 0,
 | 
					
						
							|  |  |  | 			   then just append them to the result list and quit */ | 
					
						
							|  |  |  | 			if (weight_sum == 0) { | 
					
						
							|  |  |  | 				AST_LIST_APPEND_LIST(&newlist, &temp_list, list); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) { | 
					
						
							|  |  |  | 				if (current->weight < random_weight) | 
					
						
							|  |  |  | 					continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-08 05:28:47 +00:00
										 |  |  | 				AST_LIST_MOVE_CURRENT(&newlist, list); | 
					
						
							| 
									
										
										
										
											2007-11-08 05:36:28 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			AST_LIST_TRAVERSE_SAFE_END; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* now that the new list has been ordered,
 | 
					
						
							|  |  |  | 	   put it in place */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_APPEND_LIST(&context->entries, &newlist, list); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 00:28:49 +00:00
										 |  |  | int ast_srv_lookup(struct srv_context **context, const char *service, const char **host, unsigned short *port) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct srv_entry *cur; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (*context == NULL) { | 
					
						
							|  |  |  | 		if (!(*context = ast_calloc(1, sizeof(struct srv_context)))) { | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		AST_LIST_HEAD_INIT_NOLOCK(&(*context)->entries); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 18:01:03 +00:00
										 |  |  | 		if (((ast_search_dns(*context, service, C_IN, T_SRV, srv_callback)) < 1) || | 
					
						
							|  |  |  | 			AST_LIST_EMPTY(&(*context)->entries)) { | 
					
						
							| 
									
										
										
										
											2010-01-19 00:28:49 +00:00
										 |  |  | 			ast_free(*context); | 
					
						
							|  |  |  | 			*context = NULL; | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((*context)->have_weights) { | 
					
						
							|  |  |  | 			process_weights(*context); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		(*context)->prev = AST_LIST_FIRST(&(*context)->entries); | 
					
						
							|  |  |  | 		*host = (*context)->prev->host; | 
					
						
							|  |  |  | 		*port = (*context)->prev->port; | 
					
						
							| 
									
										
										
										
											2010-04-09 14:37:50 +00:00
										 |  |  | 		AST_LIST_TRAVERSE(&(*context)->entries, cur, list) { | 
					
						
							|  |  |  | 			++((*context)->num_records); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-01-19 00:28:49 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (((*context)->prev = AST_LIST_NEXT((*context)->prev, list))) { | 
					
						
							|  |  |  | 		/* Retrieve next item in result */ | 
					
						
							|  |  |  | 		*host = (*context)->prev->host; | 
					
						
							|  |  |  | 		*port = (*context)->prev->port; | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/* No more results */ | 
					
						
							|  |  |  | 		while ((cur = AST_LIST_REMOVE_HEAD(&(*context)->entries, list))) { | 
					
						
							|  |  |  | 			ast_free(cur); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ast_free(*context); | 
					
						
							|  |  |  | 		*context = NULL; | 
					
						
							|  |  |  | 		return 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ast_srv_cleanup(struct srv_context **context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *host; | 
					
						
							|  |  |  | 	unsigned short port; | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-27 23:46:07 +00:00
										 |  |  | 	if (*context) { | 
					
						
							|  |  |  | 		/* We have a context to clean up. */ | 
					
						
							|  |  |  | 		while (!(ast_srv_lookup(context, NULL, &host, &port))) { | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-19 00:28:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE }; | 
					
						
							|  |  |  | 	struct srv_entry *current; | 
					
						
							| 
									
										
										
										
											2003-09-27 00:22:46 +00:00
										 |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	if (chan && ast_autoservice_start(chan) < 0) { | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-09-27 00:22:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	if (context.have_weights) { | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 		process_weights(&context); | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	if (chan) { | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 		ret |= ast_autoservice_stop(chan); | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-09-27 00:22:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 	/* TODO: there could be a "." entry in the returned list of
 | 
					
						
							|  |  |  | 	   answers... if so, this requires special handling */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* the list of entries will be sorted in the proper selection order
 | 
					
						
							|  |  |  | 	   already, so we just need the first one (if any) */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) { | 
					
						
							|  |  |  | 		ast_copy_string(host, current->host, hostlen); | 
					
						
							|  |  |  | 		*port = current->port; | 
					
						
							|  |  |  | 		ast_free(current); | 
					
						
							| 
									
										
										
										
											2012-04-12 20:08:26 +00:00
										 |  |  | 		ast_debug(4, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n", | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 				    service, host, *port); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2004-07-14 07:44:19 +00:00
										 |  |  | 		host[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2004-06-22 20:11:15 +00:00
										 |  |  | 		*port = -1; | 
					
						
							| 
									
										
										
										
											2003-10-04 21:58:16 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) { | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 		ast_free(current); | 
					
						
							| 
									
										
										
										
											2010-07-16 13:32:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-05 20:44:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-06-12 22:14:03 +00:00
										 |  |  | 	return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-04-09 14:37:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | unsigned int ast_srv_get_record_count(struct srv_context *context) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return context->num_records; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_srv_get_nth_record(struct srv_context *context, int record_num, const char **host, | 
					
						
							|  |  |  | 		unsigned short *port, unsigned short *priority, unsigned short *weight) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i = 1; | 
					
						
							|  |  |  | 	int res = -1; | 
					
						
							|  |  |  | 	struct srv_entry *entry; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (record_num < 1 || record_num > context->num_records) { | 
					
						
							|  |  |  | 		return res; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&context->entries, entry, list) { | 
					
						
							|  |  |  | 		if (i == record_num) { | 
					
						
							|  |  |  | 			*host = entry->host; | 
					
						
							|  |  |  | 			*port = entry->port; | 
					
						
							|  |  |  | 			*priority = entry->priority; | 
					
						
							|  |  |  | 			*weight = entry->weight; | 
					
						
							|  |  |  | 			res = 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		++i; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } |