当设备注册进来时,我们需要记录下设备的IP和端口,以便后续与其进行通信,其实好多地方都可以获取到,但是不一定是准确的,尤其是经过映射的地址更是不好难以捕捉。
今天写一下怎么样获取设备的IP和端口,对映射与非映射同样有效。先构造获answer,然后再answer的Via header中取,这时answer中含有映射地址的信息:
if (MSG_IS_REGISTER(je->request))
{
int i = eXosip_message_build_answer (m_Ctx, je->tid, 200, &asw_register);
if(i != 0)
return;
char addr[20];
int port;
memset(addr, '\0', strlen(addr));
osip_via_t* via = nullptr;
osip_message_get_via(asw_register, 0, &via);
if(!via || !via->host)
{
eXosip_lock (m_Ctx);
eXosip_message_send_answer (m_Ctx, je->tid, 400, NULL);
eXosip_unlock (m_Ctx);
return;
}
osip_generic_param_t* br = nullptr;
osip_via_param_get_byname (via, "received", &br);
if (br != NULL && br->gvalue != NULL)
strcpy(addr, br->gvalue);
else
strcpy(addr, via->host);
osip_via_param_get_byname(via, "rport", &br);
if(!br || !br->gvalue)
{
eXosip_message_send_answer (m_Ctx, je->tid, 400, NULL);
return;
}
port = atoi(br->gvalue);
eXosip_lock (m_Ctx);
eXosip_message_send_answer (m_Ctx, je->tid, 200, asw_register);
eXosip_unlock (m_Ctx);
}