本文章借鉴:
尊重原作者创作,我只是学习。基于GBT28181:SIP协议组件开发-----------第五篇SIP注册流程eXosip2实现(二) - 程序人生qq1269122125 - 博客园 (cnblogs.com)https://www.cnblogs.com/qq1269122125/p/3966794.htmlRMG类 .h
class RMg
{
public:
RMg();
~RMg();
//RMg(string m_UAIP,string m_UACODE,string m_UAPORT,string m_NAME,string m_PWD);
int ServerInit();
int SendRegister(int& registerId,CSipFromToHeader& from, CSipFromToHeader& request_line, CContracHeader& contact,
const string& username, const string& pwd, const int expires, int iType);
static void Register(string m_UAIP, string m_UACODE, string m_UAPORT, string m_NAME, string m_PWD);
static void RefreshRegister();
static void UnRegister(string m_UAIP, string m_UACODE, string m_UAPORT, string m_NAME, string m_PWD);
void parserRegisterInfo(osip_message_t* request, int iReqId, sipRegisterInfo& regInfo);
void sendRegisterAnswer(sipRegisterInfo& info);
void OnRegister(eXosip_event_t* osipEvent);
static void BeginServer();
private:
};
RMG.cpp
RMg::RMg()
{
}
RMg::~RMg()
{
}
int RMg::ServerInit()
{
int result = OSIP_SUCCESS;
if (OSIP_SUCCESS != (result = eXosip_init()))
{
return -1;
}
if (OSIP_SUCCESS != eXosip_listen_addr(IPPROTO_UDP, NULL, UACPORTINT, AF_INET, 0))
{
eXosip_quit();
return -1;
}
if (OSIP_SUCCESS != eXosip_set_option(EXOSIP_OPT_SET_IPV4_FOR_GATEWAY, LISTEN_ADDR))
{
return -1;
}
return OSIP_SUCCESS;
}
int RMg::SendRegister(int& registerId,CSipFromToHeader& from, CSipFromToHeader& request_line, CContracHeader& contact,
const string& username, const string& pwd, const int expires, int iType)
{
cout << "registerId :" << registerId << endl;
cout << "request_line :" << request_line.GetFormatHeader() << endl;
cout << "from :" << from.GetFormatHeader() << endl;
cout << "contact :" << contact.GetConFormatHeader()<< endl;
cout << "userName :" << username << endl;
cout << "pwd :" << pwd << endl;
cout << "expires :" << expires << endl;
static osip_message_t* regMsg = 0;
int ret = 0;
eXosip_add_authentication_info(username.c_str(), username.c_str(), pwd.c_str(), "MD5", NULL);
eXosip_lock();
cout << "in success" << endl;
if (0 == registerId)
{
registerId = eXosip_register_build_initial_register(from.GetFormatHeader().c_str(), request_line.GetFormatHeader().c_str(), contact.
GetConFormatHeader().c_str(), expires, ®Msg);
if (registerId <= 0)
{
cout << "initial field" << endl;
return -1;
}
}
else
{
ret = eXosip_register_build_register(registerId, expires, ®Msg);
if (ret != OSIP_SUCCESS)
{
cout << "build field" << endl;
return ret;
}
if (expires == 0)
{
osip_contact_t* scontact = NULL;
char tmp[128];
osip_message_get_contact(regMsg, 0, &scontact);
osip_list_remove(®Msg->contacts, 0);
osip_message_set_contact(regMsg, tmp);
osip_message_set_header(regMsg, "logout-reason", "logout");
}
}
ret = eXosip_register_send_register(registerId, regMsg);
if (ret != OSIP_SUCCESS)
{
cout << "send field" << endl;
registerId = 0;
return -2;
}
cout << "send success" << endl;
eXosip_unlock();
return ret;
}
void RMg::Register(string m_UAIP, string m_UACODE, string m_UAPORT, string m_NAME, string m_PWD)
{
RMg test1;
if (iCurrentStatus == 1)
{
return;
}
CSipFromToHeader request_line;
request_line.SetHeader(m_UACODE, m_UAIP, m_UAPORT);
CSipFromToHeader stfrom;
stfrom.SetHeader(UACCODE, LISTEN_ADDR, UACPORT);
CContracHeader stContact;
stContact.SetContracHeader(UACCODE, LISTEN_ADDR, UACPORT);
int registerId = 0;
if (0 > test1.SendRegister(registerId,stfrom, request_line, stContact, m_NAME, m_PWD, 3000, 0))
{
cout << "Send field" << endl;
return;
}
cout << "Send success" << endl;
iCurrentStatus = 1;
iHandle = registerId;
eXosip_event_t* osipEventPtr = NULL;
while (true)
{
osipEventPtr = ::eXosip_event_wait(0, 200);
{
eXosip_lock();
//一般处理401/407采用库默认处理
eXosip_default_action(osipEventPtr);
eXosip_unlock();
}
if (NULL == osipEventPtr)
{
continue;
}
else
{
break;
}
}
}
void RMg::RefreshRegister()
{
RMg test1;
if (iCurrentStatus == 0)
{
return;
}
CSipFromToHeader request_line;
request_line.SetHeader(UASCODE, UAS_ADDR, UAS_PORT);
CSipFromToHeader stfrom;
stfrom.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
CContracHeader stContact;
stContact.SetContracHeader(UACCODE, LISTEN_ADDR, UACPORT);
if (0 > test1.SendRegister(iHandle,stfrom, request_line, stContact, UACCODE, UACPWD, 3000, 1))
{
return;
}
}
void RMg::UnRegister(string m_UAIP, string m_UACODE, string m_UAPORT, string m_NAME, string m_PWD)
{
RMg test1;
if (iCurrentStatus == 1)
{
return;
}
CSipFromToHeader request_line;
request_line.SetHeader(m_UACODE, m_UAIP, m_UAPORT);
CSipFromToHeader stfrom;
stfrom.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
CContracHeader stContact;
stContact.SetContracHeader(UACCODE, LISTEN_ADDR, UACPORT);
int registerId = 0;
if (0 > test1.SendRegister(registerId,stfrom, request_line, stContact, m_NAME, m_PWD, 0, 2))
{
return;
}
iCurrentStatus = 0;
iHandle = -1;
eXosip_event_t* osipEventPtr = NULL;
while (true)
{
osipEventPtr = ::eXosip_event_wait(0, 200);
{
eXosip_lock();
//一般处理401/407采用库默认处理
eXosip_default_action(osipEventPtr);
eXosip_unlock();
}
if (NULL == osipEventPtr)
{
continue;
}
else
{
break;
}
}
}
void RMg::parserRegisterInfo(osip_message_t* request, int iReqId, sipRegisterInfo& regInfo)
{
std::stringstream stream;
regInfo.baseInfo.method = request->sip_method;
regInfo.baseInfo.from.SetHeader(request->from->url->username,request->from->url->host, request->from->url->port);
regInfo.baseInfo.proxy.SetHeader(request->to->url->username,request->to->url->host, request->to->url->port);
//获取expires
osip_header_t * header = NULL;
{
osip_message_header_get_byname(request, "expires",0, &header);
if (NULL != header && NULL != header->hvalue)
{
regInfo.baseInfo.expires = atoi(header->hvalue);
}
}
//contact 字段
osip_contact_t * contact = NULL;
osip_message_get_contact(request, 0, &contact);
if (NULL != contact)
{
regInfo.baseInfo.contact.SetContacHeader(contact->url->username,contact->url->host, contact->url->port,
regInfo.baseInfo.expires);
}
//注册返回 由发送方维护的请求ID 接收方接收后原样返回即可
regInfo.baseInfo.sipRequestId = iReqId;
//CALL_ID
{
stream.str("");
stream << request->call_id->number;
regInfo.baseInfo.callId = stream.str();
}
//解析content消息
osip_body_t * body = NULL;
osip_message_get_body(request, 0, &body);
if (body != NULL)
{
stream.str("");
stream << body->body;
regInfo.baseInfo.content = stream.str();
}
//鉴权信息
osip_authorization_t * authentication = NULL;
{
osip_message_get_authorization(request, 0, &authentication);
if (NULL == authentication)
{
regInfo.isAuthNull = true;
}
else
{
regInfo.isAuthNull = false;
stream.str("");
stream << authentication->username;
regInfo.authInfo.userName = stream.str();
stream.str("");
stream << authentication->algorithm;
regInfo.authInfo.algorithm = stream.str();
stream.str("");
stream << authentication->realm;
regInfo.authInfo.digestRealm = stream.str();
stream.str("");
stream << authentication->nonce;
regInfo.authInfo.nonce = stream.str();
stream.str("");
stream << authentication->response;
regInfo.authInfo.response = stream.str();
stream.str("");
stream << authentication->uri;
regInfo.authInfo.uri = stream.str();
}
}
authentication = NULL;
}
void RMg::sendRegisterAnswer(sipRegisterInfo& info)
{
osip_message_t* answer = NULL;
int iStatus;
if (info.isAuthNull)
{
iStatus = 401;
}
else
{
iStatus = 200;
}eXosip_lock();
{
int result = eXosip_message_build_answer(info.baseInfo.sipRequestId,
iStatus, &answer);
if (iStatus == 401)
{
//由SIP库生成认证方法和认证参数发送客户端
std::stringstream stream;
string nonce = NONCE;
string algorithm = ALGORITHTHM;
stream << "Digest realm=\"" << info.baseInfo.from.GetRealName()
<< "\",nonce=\"" << nonce
<< "\",algorithm=" << algorithm;
osip_message_set_header(answer, "WWW-Authenticate",stream.str().c_str());
cout << "=======================================================""=========" << endl;
cout << "发送401报文" << endl;
cout << "========================================================""========" << endl;
}
else if (iStatus == 200)
{
osip_message_set_header(answer, "Contact",info.baseInfo.contact.GetContacFormatHeader(true).c_str());
cout << "=========================================================""=======" << endl;
cout << "发送200报文" << endl;
cout << "==========================================================""======" << endl;
//string_t b = "<sip: 100110000101000000@192.168.31.18:5061>;expires=600";
//osip_message_set_header(answer, "Contact", b.c_str());
}
else
{
//Do nothing
}
if (OSIP_SUCCESS != result)
{
eXosip_message_send_answer(info.baseInfo.sipRequestId, 400, NULL);
}
else
{
//发送消息体
eXosip_message_send_answer(info.baseInfo.sipRequestId, iStatus,
answer);
}
if (0 == info.baseInfo.expires)
{
eXosip_register_remove(info.baseInfo.sipRequestId);
}
}eXosip_unlock();
}
void RMg::OnRegister(eXosip_event_t* osipEvent)
{
sipRegisterInfo regInfo;
parserRegisterInfo(osipEvent->request, osipEvent->tid, regInfo);
//发送应答报文
sendRegisterAnswer(regInfo);
}
void RMg::BeginServer()
{
RMg svr;
eXosip_event_t* osipEventPtr = NULL;
while (true)
{
osipEventPtr = eXosip_event_wait(0, 200);
if (NULL == osipEventPtr)
{
cout << "svr begin" << endl;
continue;
}
switch (osipEventPtr->type)
{
//需要继续验证REGISTER是什么类型
case EXOSIP_REGISTRATION_NEW:
svr.OnRegister(osipEventPtr);
break;
case EXOSIP_MESSAGE_NEW:
{
if (!strncmp(osipEventPtr->request->sip_method, "REGISTER", strlen("REGISTER")))
{
svr.OnRegister(osipEventPtr);
}
else if (!strncmp(osipEventPtr->request->sip_method, "MESSAGE", strlen("MESSAGE")))
{
//
}
break;
default:
break;
}
eXosip_event_free(osipEventPtr);
osipEventPtr = NULL;
}
}
}