?? srv_resolver.c
字號:
/* $Id: srv_resolver.c 1239 2007-05-01 12:25:01Z bennylp $ */
/*
* Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <pjlib-util/srv_resolver.h>
#include <pjlib-util/errno.h>
#include <pj/array.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/pool.h>
#include <pj/rand.h>
#include <pj/string.h>
#define THIS_FILE "srv_resolver.c"
#define ADDR_MAX_COUNT 8
struct srv_target
{
pj_str_t target_name;
char target_buf[PJ_MAX_HOSTNAME];
pj_str_t cname;
char cname_buf[PJ_MAX_HOSTNAME];
unsigned port;
unsigned priority;
unsigned weight;
unsigned sum;
unsigned addr_cnt;
pj_in_addr addr[ADDR_MAX_COUNT];
};
typedef struct pj_dns_srv_resolver_job
{
char *objname;
pj_dns_resolver *resolver; /**< Resolver SIP instance. */
pj_dns_type dns_state; /**< DNS type being resolved. */
void *token;
pj_dns_srv_resolver_cb *cb;
pj_dns_async_query *qobject;
pj_status_t last_error;
/* Original request: */
pj_bool_t fallback_a;
pj_str_t full_name;
pj_str_t domain_part;
pj_uint16_t def_port;
/* SRV records and their resolved IP addresses: */
unsigned srv_cnt;
struct srv_target srv[PJ_DNS_SRV_MAX_ADDR];
/* Number of hosts in SRV records that the IP address has been resolved */
unsigned host_resolved;
} pj_dns_srv_resolver_job;
/* Async resolver callback, forward decl. */
static void dns_callback(void *user_data,
pj_status_t status,
pj_dns_parsed_packet *pkt);
/*
* The public API to invoke DNS SRV resolution.
*/
PJ_DEF(pj_status_t) pj_dns_srv_resolve( const pj_str_t *domain_name,
const pj_str_t *res_name,
unsigned def_port,
pj_pool_t *pool,
pj_dns_resolver *resolver,
pj_bool_t fallback_a,
void *token,
pj_dns_srv_resolver_cb *cb)
{
int len;
pj_str_t target_name;
pj_dns_srv_resolver_job *query_job;
pj_status_t status;
PJ_ASSERT_RETURN(domain_name && domain_name->slen &&
res_name && res_name->slen &&
pool && resolver && cb, PJ_EINVAL);
/* Build full name */
len = domain_name->slen + res_name->slen + 2;
target_name.ptr = (char*) pj_pool_alloc(pool, len);
pj_strcpy(&target_name, res_name);
if (res_name->ptr[res_name->slen-1] != '.')
pj_strcat2(&target_name, ".");
len = target_name.slen;
pj_strcat(&target_name, domain_name);
target_name.ptr[target_name.slen] = '\0';
/* Build the query_job state */
query_job = PJ_POOL_ZALLOC_T(pool, pj_dns_srv_resolver_job);
query_job->objname = target_name.ptr;
query_job->resolver = resolver;
query_job->token = token;
query_job->cb = cb;
query_job->fallback_a = fallback_a;
query_job->full_name = target_name;
query_job->domain_part.ptr = target_name.ptr + len;
query_job->domain_part.slen = target_name.slen - len;
query_job->def_port = (pj_uint16_t)def_port;
/* Start the asynchronous query_job */
query_job->dns_state = PJ_DNS_TYPE_SRV;
PJ_LOG(5, (query_job->objname,
"Starting async DNS %s query_job: target=%.*",
pj_dns_get_type_name(query_job->dns_state),
(int)target_name.slen, target_name.ptr));
status = pj_dns_resolver_start_query(resolver, &target_name,
query_job->dns_state, 0,
&dns_callback,
query_job, &query_job->qobject);
return status;
}
#define SWAP(type,ptr1,ptr2) if (ptr1 != ptr2) { \
type tmp; \
pj_memcpy(&tmp, ptr1, sizeof(type)); \
pj_memcpy(ptr1, ptr2, sizeof(type)); \
(ptr1)->target_name.ptr = (ptr1)->target_buf;\
pj_memcpy(ptr2, &tmp, sizeof(type)); \
(ptr2)->target_name.ptr = (ptr2)->target_buf;\
} else {}
/* Build server entries in the query_job based on received SRV response */
static void build_server_entries(pj_dns_srv_resolver_job *query_job,
pj_dns_parsed_packet *response)
{
unsigned i;
/* Save the Resource Records in DNS answer into SRV targets. */
query_job->srv_cnt = 0;
for (i=0; i<response->hdr.anscount &&
query_job->srv_cnt < PJ_DNS_SRV_MAX_ADDR; ++i)
{
pj_dns_parsed_rr *rr = &response->ans[i];
struct srv_target *srv = &query_job->srv[query_job->srv_cnt];
if (rr->type != PJ_DNS_TYPE_SRV) {
PJ_LOG(4,(query_job->objname,
"Received non SRV answer for SRV query_job!"));
continue;
}
if (rr->rdata.srv.target.slen > PJ_MAX_HOSTNAME) {
PJ_LOG(4,(query_job->objname, "Hostname is too long!"));
continue;
}
/* Build the SRV entry for RR */
pj_bzero(srv, sizeof(*srv));
srv->target_name.ptr = srv->target_buf;
pj_strncpy(&srv->target_name, &rr->rdata.srv.target,
sizeof(srv->target_buf));
srv->port = rr->rdata.srv.port;
srv->priority = rr->rdata.srv.prio;
srv->weight = rr->rdata.srv.weight;
++query_job->srv_cnt;
}
/* First pass:
* order the entries based on priority.
*/
for (i=0; i<query_job->srv_cnt-1; ++i) {
unsigned min = i, j;
for (j=i+1; j<query_job->srv_cnt; ++j) {
if (query_job->srv[j].priority < query_job->srv[min].priority)
min = j;
}
SWAP(struct srv_target, &query_job->srv[i], &query_job->srv[min]);
}
/* Second pass:
* pick one host among hosts with the same priority, according
* to its weight. The idea is when one server fails, client should
* contact the next server with higher priority rather than contacting
* server with the same priority as the failed one.
*
* The algorithm for selecting server among servers with the same
* priority is described in RFC 2782.
*/
for (i=0; i<query_job->srv_cnt; ++i) {
unsigned j, count=1, sum;
/* Calculate running sum for servers with the same priority */
sum = query_job->srv[i].sum = query_job->srv[i].weight;
for (j=i+1; j<query_job->srv_cnt &&
query_job->srv[j].priority == query_job->srv[i].priority; ++j)
{
sum += query_job->srv[j].weight;
query_job->srv[j].sum = sum;
++count;
}
if (count > 1) {
unsigned r;
/* Elect one random number between zero and the total sum of
* weight (inclusive).
*/
r = pj_rand() % (sum + 1);
/* Select the first server which running sum is greater than or
* equal to the random number.
*/
for (j=i; j<i+count; ++j) {
if (query_job->srv[j].sum >= r)
break;
}
/* Must have selected one! */
pj_assert(j != i+count);
/* Put this entry in front (of entries with same priority) */
SWAP(struct srv_target, &query_job->srv[i], &query_job->srv[j]);
/* Remove all other entries (of the same priority) */
while (count > 1) {
pj_array_erase(query_job->srv, sizeof(struct srv_target),
query_job->srv_cnt, i+1);
--count;
--query_job->srv_cnt;
}
}
}
/* Since we've been moving around SRV entries, update the pointers
* in target_name.
*/
for (i=0; i<query_job->srv_cnt; ++i) {
query_job->srv[i].target_name.ptr = query_job->srv[i].target_buf;
}
/* Check for Additional Info section if A records are available, and
* fill in the IP address (so that we won't need to resolve the A
* record with another DNS query_job).
*/
for (i=0; i<response->hdr.arcount; ++i) {
pj_dns_parsed_rr *rr = &response->arr[i];
unsigned j;
if (rr->type != PJ_DNS_TYPE_A)
continue;
/* Yippeaiyee!! There is an "A" record!
* Update the IP address of the corresponding SRV record.
*/
for (j=0; j<query_job->srv_cnt; ++j) {
if (pj_stricmp(&rr->name, &query_job->srv[j].target_name)==0) {
unsigned cnt = query_job->srv[j].addr_cnt;
query_job->srv[j].addr[cnt].s_addr = rr->rdata.a.ip_addr.s_addr;
++query_job->srv[j].addr_cnt;
++query_job->host_resolved;
break;
}
}
/* Not valid message; SRV entry might have been deleted in
* server selection process.
*/
/*
if (j == query_job->srv_cnt) {
PJ_LOG(4,(query_job->objname,
"Received DNS SRV answer with A record, but "
"couldn't find matching name (name=%.*s)",
(int)rr->name.slen,
rr->name.ptr));
}
*/
}
/* Rescan again the name specified in the SRV record to see if IP
* address is specified as the target name (unlikely, but well, who
* knows..).
*/
for (i=0; i<query_job->srv_cnt; ++i) {
pj_in_addr addr;
if (query_job->srv[i].addr_cnt != 0) {
/* IP address already resolved */
continue;
}
if (pj_inet_aton(&query_job->srv[i].target_name, &addr) != 0) {
query_job->srv[i].addr[query_job->srv[i].addr_cnt++] = addr;
++query_job->host_resolved;
}
}
/* Print resolved entries to the log */
PJ_LOG(5,(query_job->objname,
"SRV query_job for %.*s completed, "
"%d of %d total entries selected%c",
(int)query_job->full_name.slen,
query_job->full_name.ptr,
query_job->srv_cnt,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -