[asterisk-commits] kpfleming: branch kpfleming/SRV-priority-handling r86980 - in /team/kpfleming...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Wed Oct 24 12:39:39 CDT 2007
Author: kpfleming
Date: Wed Oct 24 12:39:38 2007
New Revision: 86980
URL: http://svn.digium.com/view/asterisk?view=rev&rev=86980
Log:
add priority field sorting for DNS SRV lookups
Added:
team/kpfleming/SRV-priority-handling/
- copied from r86979, branches/1.4/
Modified:
team/kpfleming/SRV-priority-handling/include/asterisk/srv.h
team/kpfleming/SRV-priority-handling/main/srv.c
Modified: team/kpfleming/SRV-priority-handling/include/asterisk/srv.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/SRV-priority-handling/include/asterisk/srv.h?view=diff&rev=86980&r1=86979&r2=86980
==============================================================================
--- team/kpfleming/SRV-priority-handling/include/asterisk/srv.h (original)
+++ team/kpfleming/SRV-priority-handling/include/asterisk/srv.h Wed Oct 24 12:39:38 2007
@@ -26,9 +26,10 @@
/*!
\file srv.h
\brief Support for DNS SRV records, used in to locate SIP services.
- \note Note: The Asterisk DNS SRV record support is broken, it only
- supports the first DNS SRV record and will give no load
- balancing or failover support.
+ \note Note: This SRV record support is very minimal; it will only
+ return the first (lowest priority) answer that is received, and
+ has no provisions for the 'weight' elements of the records or
+ retrying if the first returned result fails.
*/
/*! Lookup entry in SRV records Returns 1 if found, 0 if not found, -1 on hangup
Modified: team/kpfleming/SRV-priority-handling/main/srv.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/SRV-priority-handling/main/srv.c?view=diff&rev=86980&r1=86979&r2=86980
==============================================================================
--- team/kpfleming/SRV-priority-handling/main/srv.c (original)
+++ team/kpfleming/SRV-priority-handling/main/srv.c Wed Oct 24 12:39:38 2007
@@ -50,78 +50,94 @@
#include "asterisk/dns.h"
#include "asterisk/options.h"
#include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
#ifdef __APPLE__
#undef T_SRV
#define T_SRV 33
#endif
-struct srv {
+struct srv_entry {
unsigned short priority;
unsigned short weight;
- unsigned short portnum;
-} __attribute__ ((__packed__));
+ unsigned short port;
+ AST_LIST_ENTRY(srv_entry) list;
+ char host[1];
+};
-static int parse_srv(char *host, int hostlen, int *portno, unsigned char *answer, int len, unsigned char *msg)
+AST_LIST_HEAD_NOLOCK(srv_entries, srv_entry);
+
+static int parse_srv(unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result)
{
+ struct srv {
+ unsigned short priority;
+ unsigned short weight;
+ unsigned short port;
+ } __attribute__ ((__packed__)) *srv = (struct srv *) answer;
+
int res = 0;
- struct srv *srv = (struct srv *)answer;
char repl[256] = "";
+ struct srv_entry *entry;
- if (len < sizeof(struct srv)) {
- printf("Length too short\n");
+ if (len < sizeof(*srv))
return -1;
- }
- answer += sizeof(struct srv);
- len -= sizeof(struct srv);
- if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) < 0) {
+ answer += sizeof(*srv);
+ len -= sizeof(*srv);
+
+ if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) {
ast_log(LOG_WARNING, "Failed to expand hostname\n");
return -1;
}
- if (res && strcmp(repl, ".")) {
- if (option_verbose > 3)
- ast_verbose( VERBOSE_PREFIX_3 "parse_srv: SRV mapped to host %s, port %d\n", repl, ntohs(srv->portnum));
- if (host) {
- ast_copy_string(host, repl, hostlen);
- host[hostlen-1] = '\0';
- }
- if (portno)
- *portno = ntohs(srv->portnum);
- return 0;
- }
- return -1;
+
+ if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl))))
+ return -1;
+
+ entry->priority = ntohs(srv->priority);
+ entry->port = ntohs(srv->port);
+ strcpy(entry->host, repl);
+
+ *result = entry;
+
+ return 0;
}
-
-struct srv_context {
- char *host;
- int hostlen;
- int *port;
-};
static int srv_callback(void *context, unsigned char *answer, int len, unsigned char *fullanswer)
{
- struct srv_context *c = (struct srv_context *)context;
+ struct srv_entries *c = (struct srv_entries *) context;
+ struct srv_entry *entry = NULL;
+ struct srv_entry *current;
- if (parse_srv(c->host, c->hostlen, c->port, answer, len, fullanswer)) {
+ if (parse_srv(answer, len, fullanswer, &entry)) {
ast_log(LOG_WARNING, "Failed to parse srv\n");
return -1;
}
- if (!ast_strlen_zero(c->host))
- return 1;
+ AST_LIST_TRAVERSE_SAFE_BEGIN(c, current, list) {
+ /* insert this entry just before the first existing
+ entry with a higher priority */
+ if (current->priority <= entry->priority)
+ continue;
+
+ AST_LIST_INSERT_BEFORE_CURRENT(c, entry, list);
+ entry = NULL;
+ break;
+ }
+ AST_LIST_TRAVERSE_SAFE_END;
+
+ /* 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, entry, list);
return 0;
}
int ast_get_srv(struct ast_channel *chan, char *host, int hostlen, int *port, const char *service)
{
- struct srv_context context;
+ struct srv_entries context = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
+ struct srv_entry *current;
int ret;
-
- context.host = host;
- context.hostlen = hostlen;
- context.port = port;
if (chan && ast_autoservice_start(chan) < 0)
return -1;
@@ -131,10 +147,27 @@
if (chan)
ret |= ast_autoservice_stop(chan);
- if (ret <= 0) {
+ /* 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, list))) {
+ ast_copy_string(host, current->host, hostlen);
+ *port = current->port;
+ ast_free(current);
+ if (option_verbose > 3) {
+ ast_verbose(VERBOSE_PREFIX_3 "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n",
+ service, host, *port);
+ }
+ } else {
host[0] = '\0';
*port = -1;
- return ret;
}
+
+ while ((current = AST_LIST_REMOVE_HEAD(&context, list)))
+ ast_free(current);
+
return ret;
}
More information about the asterisk-commits
mailing list