2004-12-24 18:15:34 +00:00
|
|
|
/**
|
|
|
|
* engine.cpp
|
|
|
|
* Yet Another SIP Stack
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
|
|
|
* Copyright (C) 2004 Null Team
|
|
|
|
*
|
|
|
|
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <telengine.h>
|
2004-12-28 05:15:11 +00:00
|
|
|
#include <yateversn.h>
|
2004-12-24 18:15:34 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <ysip.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
|
2004-12-29 04:02:55 +00:00
|
|
|
URI::URI()
|
|
|
|
: m_parsed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
URI::URI(const String& uri)
|
|
|
|
: String(uri), m_parsed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
URI::URI(const URI& uri)
|
|
|
|
: String(uri), m_parsed(false)
|
|
|
|
{
|
|
|
|
m_proto = uri.getProtocol();
|
|
|
|
m_user = uri.getUser();
|
|
|
|
m_host = uri.getHost();
|
|
|
|
m_port = uri.getPort();
|
|
|
|
m_parsed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI::URI(const char* proto, const char* user, const char* host, int port)
|
|
|
|
: m_proto(proto), m_user(user), m_host(host), m_port(port)
|
|
|
|
{
|
|
|
|
*this << m_proto << ":" << m_user << "@" << m_host;
|
|
|
|
if (m_port > 0)
|
|
|
|
*this << ":" << m_port;
|
|
|
|
m_parsed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URI::changed()
|
|
|
|
{
|
|
|
|
m_parsed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void URI::parse() const
|
|
|
|
{
|
|
|
|
if (m_parsed)
|
|
|
|
return;
|
|
|
|
m_port = 0;
|
2004-12-29 17:01:39 +00:00
|
|
|
|
|
|
|
Regexp r("<\\([^>]\\+\\)>");
|
|
|
|
if (const_cast<URI*>(this)->matches(r)) {
|
|
|
|
*const_cast<URI*>(this) = matchString(1);
|
|
|
|
DDebug("URI",DebugAll,"new value='%s'",c_str());
|
|
|
|
}
|
|
|
|
|
2004-12-29 04:02:55 +00:00
|
|
|
// proto:[user[:passwd]@]hostname[:port][/path][?param=value[¶m=value...]]
|
|
|
|
// proto:user@hostname[:port][/path][?params][¶ms]
|
2004-12-29 17:01:39 +00:00
|
|
|
r = "^\\([[:alpha:]]\\+\\):\\([[:alnum:]._-]\\+\\)@\\([[:alnum:]._-]\\+\\)\\(:[0-9]\\+\\)\\?";
|
2004-12-29 04:02:55 +00:00
|
|
|
if (const_cast<URI*>(this)->matches(r)) {
|
|
|
|
m_proto = matchString(1).toLower();
|
|
|
|
m_user = matchString(2);
|
|
|
|
m_host = matchString(3);
|
|
|
|
String tmp = matchString(4);
|
|
|
|
tmp >> ":" >> m_port;
|
2004-12-29 17:01:39 +00:00
|
|
|
DDebug("URI",DebugAll,"proto='%s' user='%s' host='%s' port=%d",
|
2004-12-29 04:02:55 +00:00
|
|
|
m_proto.c_str(), m_user.c_str(), m_host.c_str(), m_port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_proto.clear();
|
|
|
|
m_user.clear();
|
|
|
|
m_host.clear();
|
|
|
|
}
|
|
|
|
m_parsed = true;
|
|
|
|
}
|
|
|
|
|
2004-12-24 18:15:34 +00:00
|
|
|
SIPParty::SIPParty()
|
2004-12-28 05:15:11 +00:00
|
|
|
: m_reliable(false)
|
2004-12-24 18:15:34 +00:00
|
|
|
{
|
|
|
|
Debug(DebugAll,"SIPParty::SIPParty() [%p]",this);
|
|
|
|
}
|
|
|
|
|
2004-12-28 05:15:11 +00:00
|
|
|
SIPParty::SIPParty(bool reliable)
|
|
|
|
: m_reliable(reliable)
|
|
|
|
{
|
|
|
|
Debug(DebugAll,"SIPParty::SIPParty(%d) [%p]",reliable,this);
|
|
|
|
}
|
|
|
|
|
2004-12-24 18:15:34 +00:00
|
|
|
SIPParty::~SIPParty()
|
|
|
|
{
|
|
|
|
Debug(DebugAll,"SIPParty::~SIPParty() [%p]",this);
|
|
|
|
}
|
|
|
|
|
|
|
|
SIPEvent::SIPEvent(SIPMessage* message, SIPTransaction* transaction)
|
|
|
|
: m_message(message), m_transaction(transaction),
|
|
|
|
m_state(SIPTransaction::Invalid)
|
|
|
|
{
|
|
|
|
Debug(DebugAll,"SIPEvent::SIPEvent(%p,%p) [%p]",message,transaction,this);
|
|
|
|
if (m_message)
|
|
|
|
m_message->ref();
|
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->ref();
|
|
|
|
m_state = m_transaction->getState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SIPEvent::~SIPEvent()
|
|
|
|
{
|
|
|
|
Debugger debug(DebugAll,"SIPEvent::~SIPEvent"," [%p]",this);
|
|
|
|
if (m_transaction)
|
|
|
|
m_transaction->deref();
|
|
|
|
if (m_message)
|
|
|
|
m_message->deref();
|
|
|
|
}
|
|
|
|
|
2004-12-28 05:15:11 +00:00
|
|
|
SIPEngine::SIPEngine(const char* userAgent)
|
2004-12-28 14:50:10 +00:00
|
|
|
: m_t1(500000), m_t4(5000000), m_maxForwards(70),
|
|
|
|
m_cseq(0), m_userAgent(userAgent)
|
2004-12-24 18:15:34 +00:00
|
|
|
{
|
|
|
|
Debug(DebugInfo,"SIPEngine::SIPEngine() [%p]",this);
|
2004-12-28 05:15:11 +00:00
|
|
|
if (m_userAgent.null())
|
|
|
|
m_userAgent << "YATE/" << YATE_VERSION;
|
2004-12-29 17:01:39 +00:00
|
|
|
m_allowed = "INVITE,ACK,CANCEL,BYE";
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SIPEngine::~SIPEngine()
|
|
|
|
{
|
|
|
|
Debug(DebugInfo,"SIPEngine::~SIPEngine() [%p]",this);
|
|
|
|
}
|
|
|
|
|
2004-12-28 05:15:11 +00:00
|
|
|
SIPTransaction* SIPEngine::addMessage(SIPParty* ep, const char *buf, int len)
|
2004-12-24 18:15:34 +00:00
|
|
|
{
|
|
|
|
Debug("SIPEngine",DebugInfo,"addMessage(%p,%d) [%p]",buf,len,this);
|
|
|
|
SIPMessage* msg = SIPMessage::fromParsing(ep,buf,len);
|
|
|
|
if (ep)
|
|
|
|
ep->deref();
|
|
|
|
if (msg) {
|
2004-12-28 05:15:11 +00:00
|
|
|
SIPTransaction* tr = addMessage(msg);
|
2004-12-24 18:15:34 +00:00
|
|
|
msg->deref();
|
2004-12-28 05:15:11 +00:00
|
|
|
return tr;
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
2004-12-28 05:15:11 +00:00
|
|
|
return 0;
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
|
2004-12-28 05:15:11 +00:00
|
|
|
SIPTransaction* SIPEngine::addMessage(SIPMessage* message)
|
2004-12-24 18:15:34 +00:00
|
|
|
{
|
|
|
|
Debug("SIPEngine",DebugInfo,"addMessage(%p) [%p]",message,this);
|
2004-12-28 05:15:11 +00:00
|
|
|
if (!message)
|
|
|
|
return 0;
|
2004-12-29 04:02:55 +00:00
|
|
|
// make sure outgoing messages are well formed
|
|
|
|
if (message->isOutgoing())
|
|
|
|
message->complete(this);
|
|
|
|
// locate the branch parameter of last Via header - added by the UA
|
|
|
|
const HeaderLine* hl = message->getLastHeader("Via");
|
|
|
|
const NamedString* br = hl ? hl->getParam("branch") : 0;
|
2004-12-28 05:15:11 +00:00
|
|
|
String branch(br ? *br : 0);
|
|
|
|
if (!branch.startsWith("z9hG4bK"))
|
|
|
|
branch.clear();
|
2004-12-24 18:15:34 +00:00
|
|
|
Lock lock(m_mutex);
|
|
|
|
ObjList* l = &TransList;
|
|
|
|
for (; l; l = l->next()) {
|
|
|
|
SIPTransaction* t = static_cast<SIPTransaction*>(l->get());
|
2004-12-28 05:15:11 +00:00
|
|
|
if (t && t->processMessage(message,branch))
|
|
|
|
return t;
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
if (message->isAnswer()) {
|
|
|
|
Debug("SIPEngine",DebugInfo,"Message %p was an unhandled answer [%p]",message,this);
|
2004-12-28 05:15:11 +00:00
|
|
|
return 0;
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
2004-12-29 04:02:55 +00:00
|
|
|
message->complete(this);
|
2004-12-28 05:15:11 +00:00
|
|
|
return new SIPTransaction(message,this,false);
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SIPEngine::process()
|
|
|
|
{
|
|
|
|
SIPEvent* e = getEvent();
|
|
|
|
if (!e)
|
|
|
|
return false;
|
|
|
|
Debug("SIPEngine",DebugInfo,"process() got event %p",e);
|
|
|
|
processEvent(e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SIPEvent* SIPEngine::getEvent()
|
|
|
|
{
|
|
|
|
Lock lock(m_mutex);
|
|
|
|
ObjList* l = &TransList;
|
|
|
|
for (; l; l = l->next()) {
|
|
|
|
SIPTransaction* t = static_cast<SIPTransaction*>(l->get());
|
|
|
|
if (t) {
|
|
|
|
SIPEvent* e = t->getEvent();
|
2004-12-28 05:15:11 +00:00
|
|
|
if (e) {
|
2004-12-29 17:01:39 +00:00
|
|
|
Debug("SIPEngine",DebugInfo,"Got event %p (state %s) from transaction %p [%p]",
|
|
|
|
e,SIPTransaction::stateName(e->getState()),t,this);
|
2004-12-24 18:15:34 +00:00
|
|
|
return e;
|
2004-12-28 05:15:11 +00:00
|
|
|
}
|
2004-12-24 18:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SIPEngine::processEvent(SIPEvent *event)
|
|
|
|
{
|
|
|
|
Lock lock(m_mutex);
|
|
|
|
if (event) {
|
|
|
|
if (event->isOutgoing() && event->getParty())
|
|
|
|
event->getParty()->transmit(event);
|
|
|
|
delete event;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-28 05:15:11 +00:00
|
|
|
unsigned long long SIPEngine::getTimer(char which, bool reliable) const
|
|
|
|
{
|
|
|
|
switch (which) {
|
|
|
|
case '1':
|
|
|
|
// T1: RTT Estimate 500ms default
|
|
|
|
return m_t1;
|
|
|
|
case '2':
|
|
|
|
// T2: Maximum retransmit interval
|
|
|
|
// for non-INVITE requests and INVITE responses
|
|
|
|
return 4000000;
|
|
|
|
case '4':
|
|
|
|
// T4: Maximum duration a message will remain in the network
|
|
|
|
return m_t4;
|
|
|
|
case 'A':
|
|
|
|
// A: INVITE request retransmit interval, for UDP only
|
|
|
|
return m_t1;
|
|
|
|
case 'B':
|
|
|
|
// B: INVITE transaction timeout timer
|
|
|
|
return 64*m_t1;
|
|
|
|
case 'C':
|
|
|
|
// C: proxy INVITE transaction timeout
|
|
|
|
return 180000000;
|
|
|
|
case 'D':
|
|
|
|
// D: Wait time for response retransmits
|
|
|
|
return reliable ? 0 : 32000000;
|
|
|
|
case 'E':
|
|
|
|
// E: non-INVITE request retransmit interval, UDP only
|
|
|
|
return m_t1;
|
|
|
|
case 'F':
|
|
|
|
// F: non-INVITE transaction timeout timer
|
|
|
|
return 64*m_t1;
|
|
|
|
case 'G':
|
|
|
|
// G: INVITE response retransmit interval
|
|
|
|
return m_t1;
|
|
|
|
case 'H':
|
|
|
|
// H: Wait time for ACK receipt
|
|
|
|
return 64*m_t1;
|
|
|
|
case 'I':
|
|
|
|
// I: Wait time for ACK retransmits
|
|
|
|
return reliable ? 0 : m_t4;
|
|
|
|
case 'J':
|
|
|
|
// J: Wait time for non-INVITE request retransmits
|
|
|
|
return reliable ? 0 : 64*m_t1;
|
|
|
|
case 'K':
|
|
|
|
// K: Wait time for response retransmits
|
|
|
|
return reliable ? 0 : m_t4;
|
|
|
|
}
|
|
|
|
Debug("SIPEngine",DebugInfo,"Requested invalid timer '%c' [%p]",which,this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-12-29 17:01:39 +00:00
|
|
|
bool SIPEngine::isAllowed(const char* method) const
|
|
|
|
{
|
|
|
|
int pos = m_allowed.find(method);
|
|
|
|
if (pos < 0)
|
|
|
|
return false;
|
|
|
|
if ((pos > 0) && (m_allowed[pos-1] != ','))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SIPEngine::addAllowed(const char* method)
|
|
|
|
{
|
|
|
|
if (method && *method && !isAllowed(method))
|
|
|
|
m_allowed << "," << method;
|
|
|
|
}
|
|
|
|
|
2004-12-24 18:15:34 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|