1
/* $Id: sip_resolve.c 4108 2012-04-27 01:32:12Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjsip/sip_resolve.h>
21
#include <pjsip/sip_transport.h>
22
#include <pjsip/sip_errno.h>
23
#include <pjlib-util/errno.h>
24
#include <pjlib-util/srv_resolver.h>
25
#include <pj/addr_resolv.h>
27
#include <pj/assert.h>
32
#include <pj/string.h>
35
#define THIS_FILE "sip_resolve.c"
37
#define ADDR_MAX_COUNT 8
41
pj_str_t res_type; /**< e.g. "_sip._udp" */
42
pj_str_t name; /**< Domain name. */
43
pjsip_transport_type_e type; /**< Transport type. */
44
unsigned order; /**< Order */
45
unsigned pref; /**< Preference. */
52
pj_dns_type query_type;
54
pjsip_resolver_callback *cb;
55
pj_dns_async_query *object;
56
pj_status_t last_error;
58
/* Original request: */
60
pjsip_host_info target;
66
struct naptr_target naptr[8];
70
struct pjsip_resolver_t
76
static void srv_resolver_cb(void *user_data,
78
const pj_dns_srv_record *rec);
79
static void dns_a_callback(void *user_data,
81
pj_dns_parsed_packet *response);
85
* Public API to create the resolver.
87
PJ_DEF(pj_status_t) pjsip_resolver_create( pj_pool_t *pool,
88
pjsip_resolver_t **p_res)
90
pjsip_resolver_t *resolver;
92
PJ_ASSERT_RETURN(pool && p_res, PJ_EINVAL);
93
resolver = PJ_POOL_ZALLOC_T(pool, pjsip_resolver_t);
101
* Public API to set the DNS resolver instance for the SIP resolver.
103
PJ_DEF(pj_status_t) pjsip_resolver_set_resolver(pjsip_resolver_t *res,
104
pj_dns_resolver *dns_res)
106
#if PJSIP_HAS_RESOLVER
111
PJ_UNUSED_ARG(dns_res);
112
pj_assert(!"Resolver is disabled (PJSIP_HAS_RESOLVER==0)");
113
return PJ_EINVALIDOP;
119
* Public API to get the internal DNS resolver.
121
PJ_DEF(pj_dns_resolver*) pjsip_resolver_get_resolver(pjsip_resolver_t *res)
128
* Public API to create destroy the resolver
130
PJ_DEF(void) pjsip_resolver_destroy(pjsip_resolver_t *resolver)
133
#if PJSIP_HAS_RESOLVER
134
pj_dns_resolver_destroy(resolver->res, PJ_FALSE);
136
resolver->res = NULL;
142
* determine if an address is a valid IP address, and if it is,
143
* return the IP version (4 or 6).
145
static int get_ip_addr_ver(const pj_str_t *host)
150
/* First check with inet_aton() */
151
if (pj_inet_aton(host, &dummy) > 0)
154
/* Then check if this is an IPv6 address */
155
if (pj_inet_pton(pj_AF_INET6(), host, &dummy6) == PJ_SUCCESS)
158
/* Not an IP address */
164
* This is the main function for performing server resolution.
166
PJ_DEF(void) pjsip_resolve( pjsip_resolver_t *resolver,
168
const pjsip_host_info *target,
170
pjsip_resolver_callback *cb)
172
pjsip_server_addresses svr_addr;
173
pj_status_t status = PJ_SUCCESS;
176
pjsip_transport_type_e type = target->type;
178
/* Is it IP address or hostname? And if it's an IP, which version? */
179
ip_addr_ver = get_ip_addr_ver(&target->addr.host);
181
/* Set the transport type if not explicitly specified.
182
* RFC 3263 section 4.1 specify rules to set up this.
184
if (type == PJSIP_TRANSPORT_UNSPECIFIED) {
185
if (ip_addr_ver || (target->addr.port != 0)) {
187
if (target->flag & PJSIP_TRANSPORT_SECURE)
189
type = PJSIP_TRANSPORT_TLS;
190
} else if (target->flag & PJSIP_TRANSPORT_RELIABLE)
192
type = PJSIP_TRANSPORT_TCP;
196
type = PJSIP_TRANSPORT_UDP;
199
/* No type or explicit port is specified, and the address is
201
* In this case, full NAPTR resolution must be performed.
202
* But we don't support it (yet).
205
if (target->flag & PJSIP_TRANSPORT_SECURE)
207
type = PJSIP_TRANSPORT_TLS;
208
} else if (target->flag & PJSIP_TRANSPORT_RELIABLE)
210
type = PJSIP_TRANSPORT_TCP;
214
type = PJSIP_TRANSPORT_UDP;
218
/* Add IPv6 flag for IPv6 address */
219
if (ip_addr_ver == 6)
220
type = (pjsip_transport_type_e)((int)type + PJSIP_TRANSPORT_IPV6);
224
/* If target is an IP address, or if resolver is not configured,
225
* we can just finish the resolution now using pj_gethostbyname()
227
if (ip_addr_ver || resolver->res == NULL) {
228
char addr_str[PJ_INET6_ADDRSTRLEN+10];
229
pj_uint16_t srv_port;
231
if (ip_addr_ver != 0) {
232
/* Target is an IP address, no need to resolve */
233
if (ip_addr_ver == 4) {
234
pj_sockaddr_init(pj_AF_INET(), &svr_addr.entry[0].addr,
236
pj_inet_aton(&target->addr.host,
237
&svr_addr.entry[0].addr.ipv4.sin_addr);
239
pj_sockaddr_init(pj_AF_INET6(), &svr_addr.entry[0].addr,
241
pj_inet_pton(pj_AF_INET6(), &target->addr.host,
242
&svr_addr.entry[0].addr.ipv6.sin6_addr);
250
"DNS resolver not available, target '%.*s:%d' type=%s "
251
"will be resolved with getaddrinfo()",
252
target->addr.host.slen,
253
target->addr.host.ptr,
255
pjsip_transport_get_type_name(target->type)));
257
if (type & PJSIP_TRANSPORT_IPV6) {
265
status = pj_getaddrinfo(af, &target->addr.host, &count, &ai);
266
if (status != PJ_SUCCESS) {
267
/* "Normalize" error to PJ_ERESOLVE. This is a special error
268
* because it will be translated to SIP status 502 by
271
status = PJ_ERESOLVE;
275
svr_addr.entry[0].addr.addr.sa_family = (pj_uint16_t)af;
276
pj_memcpy(&svr_addr.entry[0].addr, &ai.ai_addr,
277
sizeof(pj_sockaddr));
280
/* Set the port number */
281
if (target->addr.port == 0) {
282
srv_port = (pj_uint16_t)
283
pjsip_transport_get_default_port_for_type(type);
285
srv_port = (pj_uint16_t)target->addr.port;
287
pj_sockaddr_set_port(&svr_addr.entry[0].addr, srv_port);
289
/* Call the callback. */
291
"Target '%.*s:%d' type=%s resolved to "
293
(int)target->addr.host.slen,
294
target->addr.host.ptr,
296
pjsip_transport_get_type_name(target->type),
297
pj_sockaddr_print(&svr_addr.entry[0].addr, addr_str,
298
sizeof(addr_str), 3),
299
pjsip_transport_get_type_name(type),
300
pjsip_transport_get_type_desc(type)));
302
svr_addr.entry[0].priority = 0;
303
svr_addr.entry[0].weight = 0;
304
svr_addr.entry[0].type = type;
305
svr_addr.entry[0].addr_len = pj_sockaddr_get_len(&svr_addr.entry[0].addr);
306
(*cb)(status, token, &svr_addr);
312
/* Target is not an IP address so we need to resolve it. */
313
#if PJSIP_HAS_RESOLVER
315
/* Build the query state */
316
query = PJ_POOL_ZALLOC_T(pool, struct query);
317
query->objname = THIS_FILE;
318
query->token = token;
320
query->req.target = *target;
321
pj_strdup(pool, &query->req.target.addr.host, &target->addr.host);
323
/* If port is not specified, start with SRV resolution
324
* (should be with NAPTR, but we'll do that later)
326
PJ_TODO(SUPPORT_DNS_NAPTR);
328
/* Build dummy NAPTR entry */
329
query->naptr_cnt = 1;
330
pj_bzero(&query->naptr[0], sizeof(query->naptr[0]));
331
query->naptr[0].order = 0;
332
query->naptr[0].pref = 0;
333
query->naptr[0].type = type;
334
pj_strdup(pool, &query->naptr[0].name, &target->addr.host);
337
/* Start DNS SRV or A resolution, depending on whether port is specified */
338
if (target->addr.port == 0) {
339
query->query_type = PJ_DNS_TYPE_SRV;
341
query->req.def_port = 5060;
343
if (type == PJSIP_TRANSPORT_TLS) {
344
query->naptr[0].res_type = pj_str("_sips._tcp.");
345
query->req.def_port = 5061;
346
} else if (type == PJSIP_TRANSPORT_TCP)
347
query->naptr[0].res_type = pj_str("_sip._tcp.");
348
else if (type == PJSIP_TRANSPORT_UDP)
349
query->naptr[0].res_type = pj_str("_sip._udp.");
351
pj_assert(!"Unknown transport type");
352
query->naptr[0].res_type = pj_str("_sip._udp.");
357
/* Otherwise if port is specified, start with A (or AAAA) host
360
query->query_type = PJ_DNS_TYPE_A;
361
query->naptr[0].res_type.slen = 0;
362
query->req.def_port = target->addr.port;
365
/* Start the asynchronous query */
366
PJ_LOG(5, (query->objname,
367
"Starting async DNS %s query: target=%.*s%.*s, transport=%s, "
369
pj_dns_get_type_name(query->query_type),
370
(int)query->naptr[0].res_type.slen,
371
query->naptr[0].res_type.ptr,
372
(int)query->naptr[0].name.slen, query->naptr[0].name.ptr,
373
pjsip_transport_get_type_name(target->type),
376
if (query->query_type == PJ_DNS_TYPE_SRV) {
378
status = pj_dns_srv_resolve(&query->naptr[0].name,
379
&query->naptr[0].res_type,
380
query->req.def_port, pool, resolver->res,
381
PJ_TRUE, query, &srv_resolver_cb, NULL);
383
} else if (query->query_type == PJ_DNS_TYPE_A) {
385
status = pj_dns_resolver_start_query(resolver->res,
386
&query->naptr[0].name,
389
query, &query->object);
392
pj_assert(!"Unexpected");
396
if (status != PJ_SUCCESS)
401
#else /* PJSIP_HAS_RESOLVER */
403
PJ_UNUSED_ARG(query);
404
PJ_UNUSED_ARG(srv_name);
405
#endif /* PJSIP_HAS_RESOLVER */
408
if (status != PJ_SUCCESS) {
409
char errmsg[PJ_ERR_MSG_SIZE];
410
PJ_LOG(4,(THIS_FILE, "Failed to resolve '%.*s'. Err=%d (%s)",
411
(int)target->addr.host.slen,
412
target->addr.host.ptr,
414
pj_strerror(status,errmsg,sizeof(errmsg)).ptr));
415
(*cb)(status, token, NULL);
420
#if PJSIP_HAS_RESOLVER
423
* This callback is called when target is resolved with DNS A query.
425
static void dns_a_callback(void *user_data,
427
pj_dns_parsed_packet *pkt)
429
struct query *query = (struct query*) user_data;
430
pjsip_server_addresses srv;
436
/* Parse the response */
437
if (status == PJ_SUCCESS) {
438
status = pj_dns_parse_a_response(pkt, &rec);
441
if (status != PJ_SUCCESS) {
442
char errmsg[PJ_ERR_MSG_SIZE];
445
pj_strerror(status, errmsg, sizeof(errmsg));
446
PJ_LOG(4,(query->objname, "DNS A record resolution failed: %s",
449
/* Call the callback */
450
(*query->cb)(status, query->token, NULL);
454
/* Build server addresses and call callback */
456
for (i=0; i<rec.addr_count; ++i) {
457
srv.entry[srv.count].type = query->naptr[0].type;
458
srv.entry[srv.count].priority = 0;
459
srv.entry[srv.count].weight = 0;
460
srv.entry[srv.count].addr_len = sizeof(pj_sockaddr_in);
461
pj_sockaddr_in_init(&srv.entry[srv.count].addr.ipv4,
462
0, (pj_uint16_t)query->req.def_port);
463
srv.entry[srv.count].addr.ipv4.sin_addr.s_addr =
469
/* Call the callback */
470
(*query->cb)(PJ_SUCCESS, query->token, &srv);
474
/* Callback to be called by DNS SRV resolution */
475
static void srv_resolver_cb(void *user_data,
477
const pj_dns_srv_record *rec)
479
struct query *query = (struct query*) user_data;
480
pjsip_server_addresses srv;
483
if (status != PJ_SUCCESS) {
484
char errmsg[PJ_ERR_MSG_SIZE];
487
pj_strerror(status, errmsg, sizeof(errmsg));
488
PJ_LOG(4,(query->objname, "DNS A record resolution failed: %s",
491
/* Call the callback */
492
(*query->cb)(status, query->token, NULL);
496
/* Build server addresses and call callback */
498
for (i=0; i<rec->count; ++i) {
501
for (j=0; j<rec->entry[i].server.addr_count; ++j) {
502
srv.entry[srv.count].type = query->naptr[0].type;
503
srv.entry[srv.count].priority = rec->entry[i].priority;
504
srv.entry[srv.count].weight = rec->entry[i].weight;
505
srv.entry[srv.count].addr_len = sizeof(pj_sockaddr_in);
506
pj_sockaddr_in_init(&srv.entry[srv.count].addr.ipv4,
507
0, (pj_uint16_t)rec->entry[i].port);
508
srv.entry[srv.count].addr.ipv4.sin_addr.s_addr =
509
rec->entry[i].server.addr[j].s_addr;
515
/* Call the callback */
516
(*query->cb)(PJ_SUCCESS, query->token, &srv);
519
#endif /* PJSIP_HAS_RESOLVER */