yate/contrib/ysip/engine.cpp

303 lines
7.6 KiB
C++
Raw Normal View History

/**
* 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>
#include <yateversn.h>
#include <string.h>
#include <stdlib.h>
#include <ysip.h>
using namespace TelEngine;
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;
Regexp r("<\\([^>]\\+\\)>");
if (const_cast<URI*>(this)->matches(r)) {
*const_cast<URI*>(this) = matchString(1);
DDebug("URI",DebugAll,"new value='%s'",c_str());
}
// proto:[user[:passwd]@]hostname[:port][/path][?param=value[&param=value...]]
// proto:user@hostname[:port][/path][?params][&params]
r = "^\\([[:alpha:]]\\+\\):\\([[:alnum:]._-]\\+\\)@\\([[:alnum:]._-]\\+\\)\\(:[0-9]\\+\\)\\?";
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;
DDebug("URI",DebugAll,"proto='%s' user='%s' host='%s' port=%d",
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;
}
SIPParty::SIPParty()
: m_reliable(false)
{
Debug(DebugAll,"SIPParty::SIPParty() [%p]",this);
}
SIPParty::SIPParty(bool reliable)
: m_reliable(reliable)
{
Debug(DebugAll,"SIPParty::SIPParty(%d) [%p]",reliable,this);
}
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();
}
SIPEngine::SIPEngine(const char* userAgent)
: m_t1(500000), m_t4(5000000), m_maxForwards(70),
m_cseq(0), m_userAgent(userAgent)
{
Debug(DebugInfo,"SIPEngine::SIPEngine() [%p]",this);
if (m_userAgent.null())
m_userAgent << "YATE/" << YATE_VERSION;
m_allowed = "INVITE,ACK,CANCEL,BYE";
}
SIPEngine::~SIPEngine()
{
Debug(DebugInfo,"SIPEngine::~SIPEngine() [%p]",this);
}
SIPTransaction* SIPEngine::addMessage(SIPParty* ep, const char *buf, int len)
{
Debug("SIPEngine",DebugInfo,"addMessage(%p,%d) [%p]",buf,len,this);
SIPMessage* msg = SIPMessage::fromParsing(ep,buf,len);
if (ep)
ep->deref();
if (msg) {
SIPTransaction* tr = addMessage(msg);
msg->deref();
return tr;
}
return 0;
}
SIPTransaction* SIPEngine::addMessage(SIPMessage* message)
{
Debug("SIPEngine",DebugInfo,"addMessage(%p) [%p]",message,this);
if (!message)
return 0;
// 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;
String branch(br ? *br : 0);
if (!branch.startsWith("z9hG4bK"))
branch.clear();
Lock lock(m_mutex);
ObjList* l = &TransList;
for (; l; l = l->next()) {
SIPTransaction* t = static_cast<SIPTransaction*>(l->get());
if (t && t->processMessage(message,branch))
return t;
}
if (message->isAnswer()) {
Debug("SIPEngine",DebugInfo,"Message %p was an unhandled answer [%p]",message,this);
return 0;
}
message->complete(this);
return new SIPTransaction(message,this,false);
}
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();
if (e) {
Debug("SIPEngine",DebugInfo,"Got event %p (state %s) from transaction %p [%p]",
e,SIPTransaction::stateName(e->getState()),t,this);
return e;
}
}
}
return 0;
}
void SIPEngine::processEvent(SIPEvent *event)
{
Lock lock(m_mutex);
if (event) {
if (event->isOutgoing() && event->getParty())
event->getParty()->transmit(event);
delete event;
}
}
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;
}
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;
}
/* vi: set ts=8 sw=4 sts=4 noet: */