< Previous by Date | Date Index | Next by Date > |
Thread Index | Next in Thread > |
bool UserAgent::addUDPPort(int port, bool enableSTUN)
{
bool state = true;
try
{
mUDPTransport = (resip::UdpTransport*)mStack.addTransport(UDP, port, V4, StunEnabled);
}
catch(const Transport::Exception& e)
{
state = false;
}
return state;
}
void UserAgent::sendStunTest()
{
if (!mUDPTransport)
{
return;
}
hostent* h = gethostbyname(mStunServer.c_str());
if (!h)
{
return;
}
in_addr sin_addr = *(struct in_addr*)h->h_addr;
resip::Tuple tStunDest(sin_addr, 3478, UDP, Data::Empty);
mUDPTransport->stunSendTest(tStunDest);
mLastStunTestTime = GetTickCount();
}
resip::Tuple UserAgent::getStunAddress()
{
resip::Tuple mappedAddress;
mappedAddress.setPort(0);
if (!mUDPTransport)
{
return mappedAddress;
}
if (!mUDPTransport->stunResult(mappedAddress))
{
// no valid result available, send another request
sendStunTest();
}
else if ((GetTickCount() - mLastStunTestTime) > 1000 * 60 * 3)
{
// don't use a STUN response that is older than 3 minutes
sendStunTest();
}
DWORD tmpLastStunTestTime = mLastStunTestTime;
while ((GetTickCount() - tmpLastStunTestTime) < 5 * 1000) // wait 5s for result
{
if (mUDPTransport->stunResult(mappedAddress))
{
break;
}
Sleep(200);
}
mLastStunTestTime = 0;
return mappedAddress;
}
void UserAgent::stunTest(const Data & stunServer, int stunPort)
{
mStunServer = stunServer;
mStunPort = stunPort;
if (stunServer.empty() == true)
{
return;
}
string ip;
int port;
Utility utility;
string emptyIP = "0.0.0.0";
for (int i=0; i<3; ++i)
{
Tuple mapAddress = getStunAddress();
ip = Tuple::inet_ntop(mapAddress).c_str();
port = mapAddress.getPort();
if (utility.compareStringNoCase(ip, emptyIP) != 0)
{
break;
}
}
if (utility.compareStringNoCase(ip, emptyIP) == 0)
{
return;
}
Data newUri = "sip:";
newUri += ip.c_str();
newUri += ":";
Data temPort(port);
newUri += temPort;
Uri stunUri(newUri);
mMasterProfile->setOverrideHostAndPort(stunUri);
StunMessageDecorator * stunMessageDec = new StunMessageDecorator;
stunMessageDec->setStunResult(ip, port);
SharedPtr<MessageDecorator> messageDec((MessageDecorator *)stunMessageDec);
mMasterProfile->setOutboundDecorator(messageDec);
}
void StunMessageDecorator::decorateMessage(SipMessage &msg, const Tuple &source, const Tuple &destination)
{
if (mStunPort!=0 && mStunIP.empty()==false)
{
Utility utility;
ParserContainer<Via>::iterator viaIter = msg.header(h_Vias).begin();
for (; viaIter!=msg.header(h_Vias).end(); ++viaIter)
{
string srcIP = Tuple::inet_ntop(source).c_str();
string viaIP =
msg.header(h_Vias).front().sentHost().c_str();
if (utility.compareStringNoCase(srcIP, viaIP) == 0)
{
msg.header(h_Vias).front().sentPort() = mStunPort;
msg.header(h_Vias).front().sentHost() = mStunIP.c_str();
}
}
}
}
REGISTER sip:sip.testserver.net:8000 SIP/2.0 Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-da5493664d12662c-1---d8754z-;rport Max-Forwards: 70 Contact: <sip:testuser1@xxxxxxxxxxxxxx:1138;rinstance=9a75af02c548a628> To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000> From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 1 REGISTER Expires: 90 Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, UPDATE, NOTIFY, REFER, REGISTER, SUBSCRIBE, MESSAGE, INFO User-Agent: reSIProcate Content-Length: 0 SIP/2.0 100 Trying Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-da5493664d12662c-1---d8754z-;rport=1138 To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000> From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 1 REGISTER Server: OpenSer (1.1.1-notls (i386/linux)) Content-Length: 0 Warning: 392 sip.testserver.net:8000 "Noisy feedback tells: pid=3442 req_src_ip=61.247.101.218 req_src_port=1138 in_uri=sip:sip.testserver.net:8000 out_uri=sip:sip.testserver.net:8000 via_cnt==1" SIP/2.0 401 Unauthorized Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-da5493664d12662c-1---d8754z-;rport=1138 To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=87625ddffbb24ccca3705354d9b0edc4.352c From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 1 REGISTER WWW-Authenticate: Digest realm="sip.testserver.net", nonce="4789de2cbaf2b9dfcf186360e0bc15b3bf3dad7b", qop="auth" Server: OpenSer (1.1.1-notls (i386/linux)) Content-Length: 0 Warning: 392 sip.testserver.net:8000 "Noisy feedback tells: pid=3442 req_src_ip=61.247.101.218 req_src_port=1138 in_uri=sip:sip.testserver.net:8000 out_uri=sip:sip.testserver.net:8000 via_cnt==1" REGISTER sip:sip.testserver.net:8000 SIP/2.0 Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-6e7d5852f9629b74-1---d8754z-;rport Max-Forwards: 70 Contact: <sip:testuser1@xxxxxxxxxxxxxx:1138;rinstance=9a75af02c548a628> To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000> From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 2 REGISTER Expires: 90 Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, UPDATE, NOTIFY, REFER, REGISTER, SUBSCRIBE, MESSAGE, INFO User-Agent: reSIProcate Authorization: Digest username="testuser1",realm="sip.testserver.net",nonce="4789de2cbaf2b9dfcf186360e0bc15b3bf3dad7b",uri="sip:sip.testserver.net:8000",response="4c7d21f7ba2e8cb6c3da55676302ee30",cnonce="3d609924372eba77",nc=00000001,qop=auth,algorithm=MD5 Content-Length: 0 SIP/2.0 100 Trying Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-6e7d5852f9629b74-1---d8754z-;rport=1138 To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000> From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 2 REGISTER Server: OpenSer (1.1.1-notls (i386/linux)) Content-Length: 0 Warning: 392 sip.testserver.net:8000 "Noisy feedback tells: pid=3459 req_src_ip=61.247.101.218 req_src_port=1138 in_uri=sip:sip.testserver.net:8000 out_uri=sip:sip.testserver.net:8000 via_cnt==1" SIP/2.0 200 OK Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-6e7d5852f9629b74-1---d8754z-;rport=1138 To: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=87625ddffbb24ccca3705354d9b0edc4.5e42 From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=a4124c3d Call-ID: YmViYWI2MDYwZGU2NjU4M2E0ZTJlMjg5Yzc4ZDVmY2Q. CSeq: 2 REGISTER Contact: <sip:testuser1@xxxxxxxxxxxxxx:1135;rinstance=8b08b700b132830d>;expires=28, <sip:testuser1@xxxxxxxxxxxxxx:1138;rinstance=9a75af02c548a628>;expires=90 Server: OpenSer (1.1.1-notls (i386/linux)) Content-Length: 0 Warning: 392 sip.testserver.net:8000 "Noisy feedback tells: pid=3459 req_src_ip=61.247.101.218 req_src_port=1138 in_uri=sip:sip.testserver.net:8000 out_uri=sip:sip.testserver.net:8000 via_cnt==1" INVITE sip:testuser2@xxxxxxxxxxxxxxxxxx:8000 SIP/2.0 Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-65046f5567508158-1---d8754z-;rport Max-Forwards: 70 Contact: <sip:testuser1@xxxxxxxxxxxxxx:1138> To: <sip:testuser2@xxxxxxxxxxxxxxxxxx:8000> From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=1160344d Call-ID: MWYyNzY1YTNiZDg0YWUwYTBkYmY3ODEzYjIxOTI4ZGM. CSeq: 1 INVITE Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, UPDATE, NOTIFY, REFER, REGISTER, SUBSCRIBE, MESSAGE, INFO Content-Type: application/sdp User-Agent: reSIProcate Content-Length: 291 v=0 o=- 24641749 24641749 IN IP4 61.247.101.218 s=http://www.test.com c=IN IP4 61.247.101.218 t=0 0 m=audio 1140 RTP/AVP 18 3 0 101 a=fmtp:18 annexb=no a=fmtp:101 0-15 a=ptime:20 a=rtpmap:18 G729/8000 a=rtpmap:3 GSM/8000 a=rtpmap:0 PCMU/8000 a=rtpmap:101 telephone-event/8000 SIP/2.0 407 Proxy Authentication Required Via: SIP/2.0/UDP 61.247.101.218:1138;branch=z9hG4bK-d8754z-65046f5567508158-1---d8754z-;rport=1138 To: <sip:testuser2@xxxxxxxxxxxxxxxxxx:8000>;tag=87625ddffbb24ccca3705354d9b0edc4.96d1 From: <sip:testuser1@xxxxxxxxxxxxxxxxxx:8000>;tag=1160344d Call-ID: MWYyNzY1YTNiZDg0YWUwYTBkYmY3ODEzYjIxOTI4ZGM. CSeq: 1 INVITE Proxy-Authenticate: Digest realm="sip.testserver.net", nonce="4789de3875bc325f513d0b70584a624819e842e3", qop="auth" Server: OpenSer (1.1.1-notls (i386/linux)) Content-Length: 0 Warning: 392 sip.testserver.net:8000 "Noisy feedback tells: pid=3444 req_src_ip=61.247.101.218 req_src_port=1138 in_uri=sip:testuser2@xxxxxxxxxxxxxxxxxx:8000 out_uri=sip:testuser2@xxxxxxxxxxxxxxxxxx:8000 via_cnt==1"