📄 dns_test.c
字号:
/* $Id: dns_test.c 1036 2007-03-03 02:15:41Z 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 "test.h"
#include <pjsip.h>
#include <pjlib.h>
#include <pjlib-util.h>
/* For logging purpose. */
#define THIS_FILE "dns_test.c"
struct result
{
pj_status_t status;
pjsip_server_addresses servers;
};
static void cb(pj_status_t status,
void *token,
const struct pjsip_server_addresses *addr)
{
struct result *result = token;
result->status = status;
if (status == PJ_SUCCESS)
pj_memcpy(&result->servers, addr, sizeof(*addr));
}
static void add_dns_entries(pj_dns_resolver *resv)
{
/* Inject DNS SRV entry */
pj_dns_parsed_packet pkt;
pj_dns_parsed_rr ans[4];
pj_dns_parsed_rr ar[5];
pj_str_t tmp;
unsigned i;
/*
* This is answer to SRV query to "example.com" domain, and
* the answer contains full reference to the A records of
* the server. The full DNS records is :
_sip._udp.example.com 3600 IN SRV 0 0 5060 sip01.example.com.
_sip._udp.example.com 3600 IN SRV 0 20 5060 sip02.example.com.
_sip._udp.example.com 3600 IN SRV 0 10 5060 sip03.example.com.
_sip._udp.example.com 3600 IN SRV 1 0 5060 sip04.example.com.
sip01.example.com. 3600 IN A 1.1.1.1
sip02.example.com. 3600 IN A 2.2.2.2
sip03.example.com. 3600 IN A 3.3.3.3
sip04.example.com. 3600 IN A 4.4.4.4
; Additionally, add A record for "example.com"
example.com. 3600 IN A 5.5.5.5
*/
pj_bzero(&pkt, sizeof(pkt));
pj_bzero(ans, sizeof(ans));
pj_bzero(ar, sizeof(ar));
pkt.hdr.flags = PJ_DNS_SET_QR(1);
pkt.hdr.anscount = PJ_ARRAY_SIZE(ans);
pkt.hdr.arcount = 0;
pkt.ans = ans;
pkt.arr = ar;
ans[0].name = pj_str("_sip._udp.example.com");
ans[0].type = PJ_DNS_TYPE_SRV;
ans[0].dnsclass = PJ_DNS_CLASS_IN;
ans[0].ttl = 3600;
ans[0].rdata.srv.prio = 0;
ans[0].rdata.srv.weight = 0;
ans[0].rdata.srv.port = 5060;
ans[0].rdata.srv.target = pj_str("sip01.example.com");
ans[1].name = pj_str("_sip._udp.example.com");
ans[1].type = PJ_DNS_TYPE_SRV;
ans[1].dnsclass = PJ_DNS_CLASS_IN;
ans[1].ttl = 3600;
ans[1].rdata.srv.prio = 0;
ans[1].rdata.srv.weight = 20;
ans[1].rdata.srv.port = 5060;
ans[1].rdata.srv.target = pj_str("sip02.example.com");
ans[2].name = pj_str("_sip._udp.example.com");
ans[2].type = PJ_DNS_TYPE_SRV;
ans[2].dnsclass = PJ_DNS_CLASS_IN;
ans[2].ttl = 3600;
ans[2].rdata.srv.prio = 0;
ans[2].rdata.srv.weight = 10;
ans[2].rdata.srv.port = 5060;
ans[2].rdata.srv.target = pj_str("sip03.example.com");
ans[3].name = pj_str("_sip._udp.example.com");
ans[3].type = PJ_DNS_TYPE_SRV;
ans[3].dnsclass = PJ_DNS_CLASS_IN;
ans[3].ttl = 3600;
ans[3].rdata.srv.prio = 1;
ans[3].rdata.srv.weight = 0;
ans[3].rdata.srv.port = 5060;
ans[3].rdata.srv.target = pj_str("sip04.example.com");
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
ar[0].name = pj_str("sip01.example.com");
ar[0].type = PJ_DNS_TYPE_A;
ar[0].dnsclass = PJ_DNS_CLASS_IN;
ar[0].ttl = 3600;
ar[0].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "1.1.1.1"));
ar[1].name = pj_str("sip02.example.com");
ar[1].type = PJ_DNS_TYPE_A;
ar[1].dnsclass = PJ_DNS_CLASS_IN;
ar[1].ttl = 3600;
ar[1].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "2.2.2.2"));
ar[2].name = pj_str("sip03.example.com");
ar[2].type = PJ_DNS_TYPE_A;
ar[2].dnsclass = PJ_DNS_CLASS_IN;
ar[2].ttl = 3600;
ar[2].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "3.3.3.3"));
ar[3].name = pj_str("sip04.example.com");
ar[3].type = PJ_DNS_TYPE_A;
ar[3].dnsclass = PJ_DNS_CLASS_IN;
ar[3].ttl = 3600;
ar[3].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "4.4.4.4"));
ar[4].name = pj_str("example.com");
ar[4].type = PJ_DNS_TYPE_A;
ar[4].dnsclass = PJ_DNS_CLASS_IN;
ar[4].ttl = 3600;
ar[4].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "5.5.5.5"));
/*
* Create individual A records for all hosts in "example.com" domain.
*/
for (i=0; i<PJ_ARRAY_SIZE(ar); ++i) {
pj_bzero(&pkt, sizeof(pkt));
pkt.hdr.anscount = 1;
pkt.hdr.flags = PJ_DNS_SET_QR(1);
pkt.ans = &ar[i];
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
}
/*
* Simulate DNS error response by creating these answers.
* Sample of invalid SRV records: _sip._udp.sip01.example.com.
*/
for (i=0; i<PJ_ARRAY_SIZE(ans); ++i) {
pj_dns_parsed_query q;
char buf[128];
char *services[] = { "_sip._udp.", "_sip._tcp.", "_sips._tcp."};
unsigned j;
for (j=0; j<PJ_ARRAY_SIZE(services); ++j) {
q.dnsclass = PJ_DNS_CLASS_IN;
q.type = PJ_DNS_TYPE_SRV;
q.name.ptr = buf;
pj_bzero(buf, sizeof(buf));
pj_strcpy2(&q.name, services[j]);
pj_strcat(&q.name, &ans[i].rdata.srv.target);
pj_bzero(&pkt, sizeof(pkt));
pkt.hdr.qdcount = 1;
pkt.hdr.flags = PJ_DNS_SET_QR(1) |
PJ_DNS_SET_RCODE(PJ_DNS_RCODE_NXDOMAIN);
pkt.q = &q;
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
}
}
/*
* ANOTHER DOMAIN.
*
* This time we let SRV and A get answered in different DNS
* query.
*/
/* The "domain.com" DNS records (note the different the port):
_sip._tcp.domain.com 3600 IN SRV 1 0 50060 sip06.domain.com.
_sip._tcp.domain.com 3600 IN SRV 2 0 50060 sip07.domain.com.
sip06.domain.com. 3600 IN A 6.6.6.6
sip07.domain.com. 3600 IN A 7.7.7.7
*/
pj_bzero(&pkt, sizeof(pkt));
pj_bzero(&ans, sizeof(ans));
pkt.hdr.flags = PJ_DNS_SET_QR(1);
pkt.hdr.anscount = 2;
pkt.ans = ans;
/* Add the SRV records, with reverse priority (to test that sorting
* works.
*/
ans[0].name = pj_str("_sip._tcp.domain.com");
ans[0].type = PJ_DNS_TYPE_SRV;
ans[0].dnsclass = PJ_DNS_CLASS_IN;
ans[0].ttl = 3600;
ans[0].rdata.srv.prio = 2;
ans[0].rdata.srv.weight = 0;
ans[0].rdata.srv.port = 50060;
ans[0].rdata.srv.target = pj_str("SIP07.DOMAIN.COM");
ans[1].name = pj_str("_sip._tcp.domain.com");
ans[1].type = PJ_DNS_TYPE_SRV;
ans[1].dnsclass = PJ_DNS_CLASS_IN;
ans[1].ttl = 3600;
ans[1].rdata.srv.prio = 1;
ans[1].rdata.srv.weight = 0;
ans[1].rdata.srv.port = 50060;
ans[1].rdata.srv.target = pj_str("SIP06.DOMAIN.COM");
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
/* From herein there is only one answer */
pkt.hdr.anscount = 1;
/* Add a single SRV for UDP */
ans[0].name = pj_str("_sip._udp.domain.com");
ans[0].type = PJ_DNS_TYPE_SRV;
ans[0].dnsclass = PJ_DNS_CLASS_IN;
ans[0].ttl = 3600;
ans[0].rdata.srv.prio = 0;
ans[0].rdata.srv.weight = 0;
ans[0].rdata.srv.port = 50060;
ans[0].rdata.srv.target = pj_str("SIP06.DOMAIN.COM");
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
/* Add the A record for sip06.domain.com */
ans[0].name = pj_str("sip06.domain.com");
ans[0].type = PJ_DNS_TYPE_A;
ans[0].dnsclass = PJ_DNS_CLASS_IN;
ans[0].ttl = 3600;
ans[0].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "6.6.6.6"));
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
/* Add the A record for sip07.domain.com */
ans[0].name = pj_str("sip07.domain.com");
ans[0].type = PJ_DNS_TYPE_A;
ans[0].dnsclass = PJ_DNS_CLASS_IN;
ans[0].ttl = 3600;
ans[0].rdata.a.ip_addr = pj_inet_addr(pj_cstr(&tmp, "7.7.7.7"));
pj_dns_resolver_add_entry( resv, &pkt, PJ_FALSE);
}
/*
* Perform server resolution where the results are expected to
* come in strict order.
*/
static int test_resolve(const char *title,
pj_pool_t *pool,
pjsip_transport_type_e type,
char *name,
int port,
pjsip_server_addresses *ref)
{
pjsip_host_info dest;
struct result result;
PJ_LOG(3,(THIS_FILE, " test_resolve(): %s", title));
dest.type = type;
dest.flag = pjsip_transport_get_flag_from_type(type);
dest.addr.host = pj_str(name);
dest.addr.port = port;
result.status = 0x12345678;
pjsip_endpt_resolve(endpt, pool, &dest, &result, &cb);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -