#include "asterisk.h"
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include "asterisk/channel.h"
#include "asterisk/srv.h"
#include "asterisk/dns.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
Go to the source code of this file.
Data Structures | |
struct | srv_context |
struct | srv_context::srv_entries |
struct | srv_entry |
Functions | |
int | ast_get_srv (struct ast_channel *chan, char *host, int hostlen, int *port, const char *service) |
static int | parse_srv (unsigned char *answer, int len, unsigned char *msg, struct srv_entry **result) |
static void | process_weights (struct srv_context *context) |
static int | srv_callback (void *context, unsigned char *answer, int len, unsigned char *fullanswer) |
Definition in file srv.c.
int ast_get_srv | ( | struct ast_channel * | chan, | |
char * | host, | |||
int | hostlen, | |||
int * | port, | |||
const char * | service | |||
) |
chan | Ast channel | |
host | host name (return value) | |
hostlen | Length of string "host" | |
port | Port number (return value) | |
service | Service tag for SRV lookup (like "_sip._udp" or "_stun._udp" |
Definition at line 200 of file srv.c.
References ast_autoservice_start(), ast_autoservice_stop(), ast_copy_string(), ast_free, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_REMOVE_HEAD, ast_search_dns(), ast_verb, chan, context, srv_entry::host, srv_entry::list, srv_entry::port, process_weights(), and srv_callback().
Referenced by ast_get_ip_or_srv(), and create_addr().
00201 { 00202 struct srv_context context = { .entries = AST_LIST_HEAD_NOLOCK_INIT_VALUE }; 00203 struct srv_entry *current; 00204 int ret; 00205 00206 if (chan && ast_autoservice_start(chan) < 0) 00207 return -1; 00208 00209 ret = ast_search_dns(&context, service, C_IN, T_SRV, srv_callback); 00210 00211 if (context.have_weights) 00212 process_weights(&context); 00213 00214 if (chan) 00215 ret |= ast_autoservice_stop(chan); 00216 00217 /* TODO: there could be a "." entry in the returned list of 00218 answers... if so, this requires special handling */ 00219 00220 /* the list of entries will be sorted in the proper selection order 00221 already, so we just need the first one (if any) */ 00222 00223 if ((ret > 0) && (current = AST_LIST_REMOVE_HEAD(&context.entries, list))) { 00224 ast_copy_string(host, current->host, hostlen); 00225 *port = current->port; 00226 ast_free(current); 00227 ast_verb(3, "ast_get_srv: SRV lookup for '%s' mapped to host %s, port %d\n", 00228 service, host, *port); 00229 } else { 00230 host[0] = '\0'; 00231 *port = -1; 00232 } 00233 00234 while ((current = AST_LIST_REMOVE_HEAD(&context.entries, list))) 00235 ast_free(current); 00236 00237 return ret; 00238 }
static int parse_srv | ( | unsigned char * | answer, | |
int | len, | |||
unsigned char * | msg, | |||
struct srv_entry ** | result | |||
) | [static] |
Definition at line 70 of file srv.c.
References ast_calloc, ast_log(), LOG_WARNING, srv_entry::port, srv_entry::priority, and srv_entry::weight.
Referenced by srv_callback().
00071 { 00072 struct srv { 00073 unsigned short priority; 00074 unsigned short weight; 00075 unsigned short port; 00076 } __attribute__((__packed__)) *srv = (struct srv *) answer; 00077 00078 int res = 0; 00079 char repl[256] = ""; 00080 struct srv_entry *entry; 00081 00082 if (len < sizeof(*srv)) 00083 return -1; 00084 00085 answer += sizeof(*srv); 00086 len -= sizeof(*srv); 00087 00088 if ((res = dn_expand(msg, answer + len, answer, repl, sizeof(repl) - 1)) <= 0) { 00089 ast_log(LOG_WARNING, "Failed to expand hostname\n"); 00090 return -1; 00091 } 00092 00093 /* the magic value "." for the target domain means that this service 00094 is *NOT* available at the domain we searched */ 00095 if (!strcmp(repl, ".")) 00096 return -1; 00097 00098 if (!(entry = ast_calloc(1, sizeof(*entry) + strlen(repl)))) 00099 return -1; 00100 00101 entry->priority = ntohs(srv->priority); 00102 entry->weight = ntohs(srv->weight); 00103 entry->port = ntohs(srv->port); 00104 strcpy(entry->host, repl); 00105 00106 *result = entry; 00107 00108 return 0; 00109 }
static void process_weights | ( | struct srv_context * | context | ) | [static] |
Definition at line 148 of file srv.c.
References AST_LIST_APPEND_LIST, AST_LIST_FIRST, AST_LIST_HEAD_NOLOCK_INIT_VALUE, AST_LIST_MOVE_CURRENT, AST_LIST_TRAVERSE, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_random(), context, srv_entry::list, srv_entry::priority, srv_entry::weight, and srv_entry::weight_sum.
Referenced by ast_get_srv().
00149 { 00150 struct srv_entry *current; 00151 struct srv_entries newlist = AST_LIST_HEAD_NOLOCK_INIT_VALUE; 00152 00153 while (AST_LIST_FIRST(&context->entries)) { 00154 unsigned int random_weight; 00155 unsigned int weight_sum; 00156 unsigned short cur_priority = AST_LIST_FIRST(&context->entries)->priority; 00157 struct srv_entries temp_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE; 00158 weight_sum = 0; 00159 00160 AST_LIST_TRAVERSE_SAFE_BEGIN(&context->entries, current, list) { 00161 if (current->priority != cur_priority) 00162 break; 00163 00164 AST_LIST_MOVE_CURRENT(&temp_list, list); 00165 } 00166 AST_LIST_TRAVERSE_SAFE_END; 00167 00168 while (AST_LIST_FIRST(&temp_list)) { 00169 weight_sum = 0; 00170 AST_LIST_TRAVERSE(&temp_list, current, list) 00171 current->weight_sum = weight_sum += current->weight; 00172 00173 /* if all the remaining entries have weight == 0, 00174 then just append them to the result list and quit */ 00175 if (weight_sum == 0) { 00176 AST_LIST_APPEND_LIST(&newlist, &temp_list, list); 00177 break; 00178 } 00179 00180 random_weight = 1 + (unsigned int) ((float) weight_sum * (ast_random() / ((float) RAND_MAX + 1.0))); 00181 00182 AST_LIST_TRAVERSE_SAFE_BEGIN(&temp_list, current, list) { 00183 if (current->weight < random_weight) 00184 continue; 00185 00186 AST_LIST_MOVE_CURRENT(&newlist, list); 00187 break; 00188 } 00189 AST_LIST_TRAVERSE_SAFE_END; 00190 } 00191 00192 } 00193 00194 /* now that the new list has been ordered, 00195 put it in place */ 00196 00197 AST_LIST_APPEND_LIST(&context->entries, &newlist, list); 00198 }
static int srv_callback | ( | void * | context, | |
unsigned char * | answer, | |||
int | len, | |||
unsigned char * | fullanswer | |||
) | [static] |
Definition at line 111 of file srv.c.
References AST_LIST_INSERT_BEFORE_CURRENT, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, srv_context::entries, srv_context::have_weights, srv_entry::list, parse_srv(), srv_entry::priority, and srv_entry::weight.
Referenced by ast_get_srv().
00112 { 00113 struct srv_context *c = (struct srv_context *) context; 00114 struct srv_entry *entry = NULL; 00115 struct srv_entry *current; 00116 00117 if (parse_srv(answer, len, fullanswer, &entry)) 00118 return -1; 00119 00120 if (entry->weight) 00121 c->have_weights = 1; 00122 00123 AST_LIST_TRAVERSE_SAFE_BEGIN(&c->entries, current, list) { 00124 /* insert this entry just before the first existing 00125 entry with a higher priority */ 00126 if (current->priority <= entry->priority) 00127 continue; 00128 00129 AST_LIST_INSERT_BEFORE_CURRENT(entry, list); 00130 entry = NULL; 00131 break; 00132 } 00133 AST_LIST_TRAVERSE_SAFE_END; 00134 00135 /* if we didn't find a place to insert the entry before an existing 00136 entry, then just add it to the end */ 00137 if (entry) 00138 AST_LIST_INSERT_TAIL(&c->entries, entry, list); 00139 00140 return 0; 00141 }