Commit a9b306e7 authored by Adrien Béraud's avatar Adrien Béraud

dns: fallback from pjsip_endpt_resolve to getaddrinfo

Refs #77355

Change-Id: Idb5c98945fb2eac8cdcfe4c07a81739ada8daba5
parent 635efcad
......@@ -963,11 +963,11 @@ SIPAccount::sendRegister()
throw VoipLinkException("UserAgent: Unable to create regc structure.");
std::string srvUri(getServerUri());
pj_str_t pjSrv = pj_str((char*) srvUri.c_str());
pj_str_t pjSrv {(char*) srvUri.data(), (pj_ssize_t) srvUri.size()};
// Generate the FROM header
std::string from(getFromUri());
pj_str_t pjFrom = pj_str((char*) from.c_str());
pj_str_t pjFrom {(char*) from.data(), (pj_ssize_t) from.size()};
// Get the received header
std::string received(getReceivedParameter());
......@@ -1004,14 +1004,14 @@ SIPAccount::sendRegister()
pjsip_hdr hdr_list;
pj_list_init(&hdr_list);
std::string useragent(getUserAgentName());
pj_str_t pJuseragent = pj_str((char*) useragent.c_str());
pj_str_t pJuseragent {(char*) useragent.data(), (pj_ssize_t) useragent.size()};
const pj_str_t STR_USER_AGENT = CONST_PJ_STR("User-Agent");
pjsip_generic_string_hdr *h = pjsip_generic_string_hdr_create(link_->getPool(), &STR_USER_AGENT, &pJuseragent);
pj_list_push_back(&hdr_list, (pjsip_hdr*) h);
pjsip_regc_add_headers(regc, &hdr_list);
pjsip_tx_data *tdata;
pjsip_tx_data *tdata;
if (pjsip_regc_register(regc, PJ_TRUE, &tdata) != PJ_SUCCESS)
throw VoipLinkException("Unable to initialize transaction data for account registration");
......@@ -1019,6 +1019,16 @@ SIPAccount::sendRegister()
if (pjsip_regc_set_transport(regc, &tp_sel) != PJ_SUCCESS)
throw VoipLinkException("Unable to set transport");
if (hostIp_) {
auto ai = &tdata->dest_info;
ai->name = pj_strdup3(tdata->pool, hostname_.c_str());
ai->addr.count = 1;
ai->addr.entry[0].type = (pjsip_transport_type_e)tp_sel.u.transport->key.type;
pj_memcpy(&ai->addr.entry[0].addr, hostIp_.pjPtr(), sizeof(pj_sockaddr));
ai->addr.entry[0].addr_len = hostIp_.getLength();
ai->cur_addr = 0;
}
// pjsip_regc_send increment the transport ref count by one,
if ((status = pjsip_regc_send(regc, tdata)) != PJ_SUCCESS) {
sip_utils::sip_strerror(status);
......
......@@ -1242,6 +1242,15 @@ resolver_callback(pj_status_t status, void *token, const struct pjsip_server_add
getResolveCallbackMap().process((uintptr_t)token, status, addr);
}
template<typename Callback>
static void
runOnMainThread(Callback&& cb){
Manager::instance().addTask([=](){
cb();
return false;
});
}
void
SIPVoIPLink::resolveSrvName(const std::string &name, pjsip_transport_type_e type, SrvResolveCallback cb)
{
......@@ -1272,11 +1281,14 @@ SIPVoIPLink::resolveSrvName(const std::string &name, pjsip_transport_type_e type
const auto token = std::hash<std::string>()(name + to_string(type));
getResolveCallbackMap().registerCallback(token,
[cb](pj_status_t s, const pjsip_server_addresses* r) {
[=](pj_status_t s, const pjsip_server_addresses* r) {
try {
if (s != PJ_SUCCESS || !r) {
sip_utils::sip_strerror(s);
throw std::runtime_error("Can't resolve address");
RING_WARN("Can't resolve \"%s\" using pjsip_endpt_resolve, trying getaddrinfo.", name.c_str());
std::thread([=](){
auto ips = ip_utils::getAddrList(name.c_str());
runOnMainThread(std::bind(cb, ips.empty() ? std::vector<IpAddr>{} : std::move(ips)));
}).detach();
} else {
std::vector<IpAddr> ips;
ips.reserve(r->count);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment