2006-06-18 13:28:25 +00:00
|
|
|
/**
|
|
|
|
* yiaxchan.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* IAX channel
|
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
|
|
|
* Copyright (C) 2004-2006 Null Team
|
|
|
|
* Author: Marian Podgoreanu
|
|
|
|
*
|
|
|
|
* 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 <yatephone.h>
|
|
|
|
#include <yateversn.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <yateiax.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
|
|
|
namespace { // anonymous
|
|
|
|
|
2006-10-31 15:18:08 +00:00
|
|
|
TokenDict dict_payloads[] = {
|
|
|
|
{"gsm", IAXFormat::GSM},
|
|
|
|
{"ilbc30", IAXFormat::ILBC},
|
|
|
|
{"speex", IAXFormat::SPEEX},
|
|
|
|
{"lpc10", IAXFormat::LPC10},
|
|
|
|
{"mulaw", IAXFormat::ULAW},
|
|
|
|
{"alaw", IAXFormat::ALAW},
|
|
|
|
{"g723", IAXFormat::G723_1},
|
|
|
|
{"g729", IAXFormat::G729A},
|
|
|
|
{"adpcm", IAXFormat::ADPCM},
|
|
|
|
{"mp3", IAXFormat::MP3},
|
|
|
|
{"slin", IAXFormat::SLIN},
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2006-06-26 05:19:52 +00:00
|
|
|
static TokenDict dict_tos[] = {
|
|
|
|
{ "lowdelay", Socket::LowDelay },
|
|
|
|
{ "throughput", Socket::MaxThroughput },
|
|
|
|
{ "reliability", Socket::MaxReliability },
|
|
|
|
{ "mincost", Socket::MinCost },
|
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
class YIAXLineContainer;
|
|
|
|
class YIAXEngine;
|
|
|
|
class IAXURI;
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* Keep a single registration line
|
|
|
|
*/
|
2006-06-18 13:28:25 +00:00
|
|
|
class YIAXLine : public String
|
|
|
|
{
|
|
|
|
friend class YIAXLineContainer;
|
2006-06-22 11:24:19 +00:00
|
|
|
friend class YIAXEngine;
|
2006-06-18 13:28:25 +00:00
|
|
|
public:
|
|
|
|
enum State {
|
|
|
|
Idle,
|
|
|
|
Registering,
|
|
|
|
Unregistering,
|
|
|
|
};
|
|
|
|
YIAXLine(const String& name);
|
|
|
|
virtual ~YIAXLine();
|
|
|
|
inline State state() const
|
|
|
|
{ return m_state; }
|
2006-06-22 11:24:19 +00:00
|
|
|
inline bool registered() const
|
|
|
|
{ return m_registered; }
|
2006-06-18 13:28:25 +00:00
|
|
|
inline const String& username() const
|
|
|
|
{ return m_username; }
|
|
|
|
inline const String& password() const
|
|
|
|
{ return m_password; }
|
|
|
|
inline const String& callingNo() const
|
|
|
|
{ return m_callingNo; }
|
|
|
|
inline const String& callingName() const
|
|
|
|
{ return m_callingName; }
|
2006-06-22 11:24:19 +00:00
|
|
|
inline u_int16_t expire() const
|
2006-06-18 13:28:25 +00:00
|
|
|
{ return m_expire; }
|
|
|
|
inline const String& localAddr() const
|
|
|
|
{ return m_localAddr; }
|
|
|
|
inline const String& remoteAddr() const
|
|
|
|
{ return m_remoteAddr; }
|
|
|
|
inline int localPort() const
|
|
|
|
{ return m_localPort; }
|
|
|
|
inline int remotePort() const
|
|
|
|
{ return m_remotePort; }
|
|
|
|
private:
|
2006-08-12 16:49:02 +00:00
|
|
|
void setRegistered(bool registered, const char* reason = 0);
|
2006-06-18 13:28:25 +00:00
|
|
|
State m_state;
|
2006-07-02 20:11:46 +00:00
|
|
|
String m_username; // Username
|
|
|
|
String m_password; // Password
|
|
|
|
String m_callingNo; // Calling number
|
|
|
|
String m_callingName; // Calling name
|
|
|
|
u_int16_t m_expire; // Expire time
|
2006-06-18 13:28:25 +00:00
|
|
|
String m_localAddr;
|
|
|
|
String m_remoteAddr;
|
|
|
|
int m_localPort;
|
|
|
|
int m_remotePort;
|
2006-07-02 20:11:46 +00:00
|
|
|
u_int32_t m_nextReg; // Time to next registration
|
|
|
|
u_int32_t m_nextKeepAlive; // Time to next keep alive signal
|
|
|
|
bool m_registered; // Registered flag. If true the line is registered
|
|
|
|
bool m_register; // Operation flag: True - register
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* m_transaction;
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* Line container: Add/Delete/Update/Register/Unregister lines
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
|
|
|
class YIAXLineContainer : public Mutex
|
|
|
|
{
|
|
|
|
public:
|
2009-05-05 14:06:39 +00:00
|
|
|
inline YIAXLineContainer() : Mutex(true,"YIAXLineContainer") {}
|
2006-06-18 13:28:25 +00:00
|
|
|
inline ~YIAXLineContainer() {}
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-26 05:19:52 +00:00
|
|
|
* Logout and remove all lines
|
2006-08-19 10:18:25 +00:00
|
|
|
* @param forced Forcedly remove lines synchronously
|
2006-06-18 21:59:23 +00:00
|
|
|
*/
|
2006-08-19 10:18:25 +00:00
|
|
|
void clear(bool forced = false);
|
2006-06-18 21:59:23 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Update a line from a message
|
|
|
|
* This method is thread safe
|
|
|
|
* @param msg Received message
|
|
|
|
* @return True if the successfully updated
|
|
|
|
*/
|
|
|
|
bool updateLine(Message &msg);
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-22 11:24:19 +00:00
|
|
|
* Event handler for a registration.
|
|
|
|
* @param event The event.
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
void handleEvent(IAXEvent* event);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-22 11:24:19 +00:00
|
|
|
* Terminate notification of a Register/Unregister operation
|
2006-06-18 13:28:25 +00:00
|
|
|
* This method is thread safe
|
2006-06-22 11:24:19 +00:00
|
|
|
* @param event The event (result)
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
void regTerminate(IAXEvent* event);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2007-01-10 18:56:27 +00:00
|
|
|
/*
|
|
|
|
* Process an authentication request for a Register/Unregister operation
|
|
|
|
* This method is thread safe
|
|
|
|
* @param event The event (AuthReq)
|
|
|
|
*/
|
|
|
|
void regAuthReq(IAXEvent* event);
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-22 11:24:19 +00:00
|
|
|
* Timer notification
|
2006-06-18 13:28:25 +00:00
|
|
|
* This method is thread safe
|
2006-06-22 11:24:19 +00:00
|
|
|
* @param time Time
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
void evTimer(Time& time);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-22 11:24:19 +00:00
|
|
|
* Fill a named list from a line
|
2006-06-18 13:28:25 +00:00
|
|
|
* This method is thread safe
|
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
bool fillList(String& name, NamedList& dest, SocketAddr& addr, bool& registered);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-22 11:24:19 +00:00
|
|
|
* Check if a line exists
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
inline bool hasLine(const String& line)
|
|
|
|
{ Lock lock(this); return findLine(line) != 0; }
|
2006-06-18 13:28:25 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool updateLine(YIAXLine* line, Message &msg);
|
|
|
|
bool addLine(Message &msg);
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXLine* findLine(const String& name);
|
|
|
|
YIAXLine* findLine(YIAXLine* line);
|
2006-06-18 13:28:25 +00:00
|
|
|
void startRegisterLine(YIAXLine* line);
|
|
|
|
void startUnregisterLine(YIAXLine* line);
|
|
|
|
private:
|
|
|
|
ObjList m_lines;
|
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Thread class for reading data from socket for the specified IAX engine
|
|
|
|
*/
|
|
|
|
class YIAX_API YIAXListener : public Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline YIAXListener(YIAXEngine* engine, const char* name = 0, Priority prio = Normal)
|
|
|
|
: Thread(name,prio), m_engine(engine)
|
|
|
|
{}
|
|
|
|
virtual void run();
|
|
|
|
protected:
|
|
|
|
YIAXEngine* m_engine;
|
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Thread class for reading events for the specified IAX engine
|
|
|
|
*/
|
|
|
|
class YIAX_API YIAXGetEvent : public Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline YIAXGetEvent(YIAXEngine* engine, const char* name = 0, Priority prio = Normal)
|
|
|
|
: Thread(name,prio), m_engine(engine)
|
|
|
|
{}
|
|
|
|
virtual void run();
|
|
|
|
protected:
|
|
|
|
YIAXEngine* m_engine;
|
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* Thread class for sending trunked mini frames for the specified IAX engine
|
|
|
|
*/
|
|
|
|
class YIAX_API YIAXTrunking : public Thread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline YIAXTrunking(YIAXEngine* engine, const char* name = 0, Priority prio = Normal)
|
|
|
|
: Thread(name,prio), m_engine(engine)
|
|
|
|
{}
|
|
|
|
virtual void run();
|
|
|
|
protected:
|
|
|
|
YIAXEngine* m_engine;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The IAX engine for this driver
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
|
|
|
class YIAXEngine : public IAXEngine
|
|
|
|
{
|
|
|
|
public:
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-26 05:19:52 +00:00
|
|
|
* Constructor
|
2006-08-08 10:04:30 +00:00
|
|
|
* @param iface Interface address to use
|
2006-06-26 05:19:52 +00:00
|
|
|
* @param port UDP port to use
|
|
|
|
* @param transListCount Number of entries in the transaction hash table
|
|
|
|
* @param retransCount Retransmission counter for each transaction belonging to this engine
|
|
|
|
* @param retransInterval Retransmission interval default value in miliseconds
|
|
|
|
* @param authTimeout Timeout (in seconds) of acknoledged auth frames sent
|
|
|
|
* @param transTimeout Timeout (in seconds) on remote request of transactions belonging to this engine
|
|
|
|
* @param maxFullFrameDataLen Max full frame IE list (buffer) length
|
2006-07-02 20:11:46 +00:00
|
|
|
* @param trunkSendInterval Send trunk meta frame interval
|
2006-10-14 10:51:52 +00:00
|
|
|
* @param authRequired Automatically challenge all clients for authentication
|
2006-06-26 05:19:52 +00:00
|
|
|
*/
|
2006-08-08 10:04:30 +00:00
|
|
|
YIAXEngine(const char* iface, int port, u_int16_t transListCount, u_int16_t retransCount, u_int16_t retransInterval,
|
2006-07-02 20:11:46 +00:00
|
|
|
u_int16_t authTimeout, u_int16_t transTimeout,
|
2006-10-14 10:51:52 +00:00
|
|
|
u_int16_t maxFullFrameDataLen, u_int32_t trunkSendInterval, bool authRequired);
|
2006-06-22 11:24:19 +00:00
|
|
|
|
|
|
|
virtual ~YIAXEngine()
|
2006-06-18 13:28:25 +00:00
|
|
|
{}
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Process media from remote peer.
|
|
|
|
* @param transaction IAXTransaction that owns the call leg
|
|
|
|
* @param data Media data.
|
|
|
|
* @param tStamp Media timestamp.
|
|
|
|
*/
|
|
|
|
virtual void processMedia(IAXTransaction* transaction, DataBlock& data, u_int32_t tStamp);
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Initiate an outgoing registration (release) request.
|
|
|
|
* @param line YIAXLine pointer to use for registration.
|
|
|
|
* @param regreq Registration request flag. If false a registration release will take place.
|
|
|
|
* @return IAXTransaction pointer on success.
|
|
|
|
*/
|
|
|
|
IAXTransaction* reg(YIAXLine* line, bool regreq = true);
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Initiate an aoutgoing call.
|
2006-06-22 11:24:19 +00:00
|
|
|
* @param addr Address to poke.
|
|
|
|
* @param params Call parameters.
|
2006-06-18 13:28:25 +00:00
|
|
|
* @return IAXTransaction pointer on success.
|
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* call(SocketAddr& addr, NamedList& params);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Initiate a test of existence of a remote IAX peer.
|
2006-06-22 11:24:19 +00:00
|
|
|
* @param addr Address to poke.
|
2006-06-18 13:28:25 +00:00
|
|
|
* @return IAXTransaction pointer on success.
|
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* poke(SocketAddr& addr);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Start thread members
|
2006-06-26 05:19:52 +00:00
|
|
|
* @param listenThreadCount Reading socket thread count.
|
|
|
|
* @param eventThreadCount Reading event thread count.
|
2006-07-02 20:11:46 +00:00
|
|
|
* @param trunkingThreadCount Trunking thread count.
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
2006-07-02 20:11:46 +00:00
|
|
|
void start(u_int16_t listenThreadCount, u_int16_t eventThreadCount,u_int16_t trunkingThreadCount);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Event handler for transaction with a connection.
|
|
|
|
*/
|
|
|
|
virtual void processEvent(IAXEvent* event);
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-26 05:19:52 +00:00
|
|
|
* Event handler for incoming registration transactions.
|
2006-10-14 10:51:52 +00:00
|
|
|
* @param event The event.
|
|
|
|
* @param first True if this is the first request.
|
|
|
|
* False if it is a response to an authentication request.
|
2006-06-26 05:19:52 +00:00
|
|
|
*/
|
2006-10-14 10:51:52 +00:00
|
|
|
void processRemoteReg(IAXEvent* event,bool first);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Send Register/Unregister messages to Engine
|
|
|
|
*/
|
2006-06-26 05:49:06 +00:00
|
|
|
bool userreg(IAXTransaction* tr, bool regrel = true);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
|
|
|
private:
|
2006-07-02 20:11:46 +00:00
|
|
|
bool m_threadsCreated; // True if reading and get events threads were created
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* YIAXRegDataHandler
|
|
|
|
*/
|
|
|
|
class YIAXRegDataHandler : public MessageHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
YIAXRegDataHandler()
|
|
|
|
: MessageHandler("user.login",150)
|
|
|
|
{ }
|
|
|
|
virtual bool received(Message &msg);
|
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* YIAXDriver
|
|
|
|
*/
|
|
|
|
class YIAXDriver : public Driver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
YIAXDriver();
|
|
|
|
virtual ~YIAXDriver();
|
|
|
|
virtual void initialize();
|
2008-07-16 09:48:49 +00:00
|
|
|
virtual bool hasLine(const String& line) const;
|
2006-06-18 13:28:25 +00:00
|
|
|
virtual bool msgExecute(Message& msg, String& dest);
|
2006-06-22 11:24:19 +00:00
|
|
|
virtual bool msgRoute(Message& msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
virtual bool received(Message& msg, int id);
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Default codec map - should have only one bit set
|
2006-06-18 13:28:25 +00:00
|
|
|
inline u_int32_t defaultCodec() const
|
|
|
|
{ return m_defaultCodec; }
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Supported codec map
|
2006-06-18 13:28:25 +00:00
|
|
|
inline u_int32_t codecs() const
|
|
|
|
{ return m_codecs; }
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// UDP port we are using, also used as default
|
2006-06-18 13:28:25 +00:00
|
|
|
inline int port() const
|
|
|
|
{ return m_port; }
|
|
|
|
|
2010-06-11 11:25:10 +00:00
|
|
|
// Retrieve the IAX engine
|
2006-06-18 13:28:25 +00:00
|
|
|
inline YIAXEngine* getEngine() const
|
|
|
|
{ return m_iaxEngine; }
|
|
|
|
|
2006-09-25 14:03:14 +00:00
|
|
|
// Update codecs from 'formats' parameter of a message
|
|
|
|
// @param codecs Codec list to update
|
|
|
|
// @param formats The 'formats' parameter of a message
|
|
|
|
// @return False if formtas is not 0 and the result is 0 (no intersection)
|
|
|
|
bool updateCodecsFromRoute(u_int32_t& codecs, const char* formats);
|
|
|
|
|
2006-11-02 08:03:06 +00:00
|
|
|
// Create a format list from codecs
|
|
|
|
void createFormatList(String& dest, u_int32_t codecs);
|
|
|
|
|
2006-10-14 10:51:52 +00:00
|
|
|
// Dispatch user.auth
|
|
|
|
// @tr The IAX transaction
|
|
|
|
// @param response True if it is a response.
|
|
|
|
// @param requestAuth True on exit: the caller should request authentication
|
|
|
|
// @param invalidAuth True on exit: authentication response is incorrect
|
|
|
|
// @return False if not authenticated
|
|
|
|
bool userAuth(IAXTransaction* tr, bool response, bool& requestAuth,
|
|
|
|
bool& invalidAuth);
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
protected:
|
|
|
|
YIAXEngine* m_iaxEngine;
|
2006-07-02 20:11:46 +00:00
|
|
|
u_int32_t m_defaultCodec;
|
|
|
|
u_int32_t m_codecs;
|
2006-08-08 10:04:30 +00:00
|
|
|
int m_port;
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class YIAXConnection;
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* Connection's data consumer
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
|
|
|
class YIAXConsumer : public DataConsumer
|
|
|
|
{
|
|
|
|
public:
|
2006-06-26 05:19:52 +00:00
|
|
|
YIAXConsumer(YIAXConnection* conn, u_int32_t format, const char* formatText);
|
2006-06-18 13:28:25 +00:00
|
|
|
~YIAXConsumer();
|
2009-07-02 09:24:33 +00:00
|
|
|
virtual unsigned long Consume(const DataBlock &data, unsigned long tStamp, unsigned long flags);
|
2006-06-18 13:28:25 +00:00
|
|
|
private:
|
|
|
|
YIAXConnection* m_connection;
|
|
|
|
unsigned m_total;
|
2006-08-08 10:04:30 +00:00
|
|
|
u_int32_t m_format; // in IAX coding
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* Connection's data source
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
|
|
|
class YIAXSource : public DataSource
|
|
|
|
{
|
|
|
|
public:
|
2006-06-26 05:19:52 +00:00
|
|
|
YIAXSource(YIAXConnection* conn, u_int32_t format, const char* formatText);
|
2006-06-18 13:28:25 +00:00
|
|
|
~YIAXSource();
|
|
|
|
void Forward(const DataBlock &data, unsigned long tStamp = 0);
|
|
|
|
private:
|
|
|
|
YIAXConnection* m_connection;
|
|
|
|
unsigned m_total;
|
2006-08-08 10:04:30 +00:00
|
|
|
u_int32_t m_format; // in IAX coding
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
|
|
|
* The connection
|
2006-06-18 13:28:25 +00:00
|
|
|
*/
|
|
|
|
class YIAXConnection : public Channel
|
|
|
|
{
|
|
|
|
public:
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXConnection(YIAXEngine* iaxEngine, IAXTransaction* transaction, Message* msg = 0);
|
2006-06-18 13:28:25 +00:00
|
|
|
virtual ~YIAXConnection();
|
|
|
|
virtual void callAccept(Message& msg);
|
|
|
|
virtual void callRejected(const char* error, const char* reason = 0, const Message* msg = 0);
|
|
|
|
virtual bool callPrerouted(Message& msg, bool handled);
|
|
|
|
virtual bool callRouted(Message& msg);
|
2007-10-11 08:24:39 +00:00
|
|
|
virtual bool msgProgress(Message& msg);
|
2006-06-18 20:16:56 +00:00
|
|
|
virtual bool msgRinging(Message& msg);
|
|
|
|
virtual bool msgAnswered(Message& msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
virtual bool msgTone(Message& msg, const char* tone);
|
|
|
|
virtual bool msgText(Message& msg, const char* text);
|
|
|
|
virtual void disconnected(bool final, const char* reason);
|
|
|
|
|
2007-04-19 08:43:14 +00:00
|
|
|
inline bool disconnect()
|
|
|
|
{ return Channel::disconnect(m_reason); }
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
inline IAXTransaction* transaction() const
|
|
|
|
{ return m_transaction; }
|
|
|
|
|
|
|
|
inline bool mutedIn() const
|
|
|
|
{ return m_mutedIn; }
|
|
|
|
|
|
|
|
inline bool mutedOut() const
|
|
|
|
{ return m_mutedOut; }
|
|
|
|
|
|
|
|
void handleEvent(IAXEvent* event);
|
|
|
|
|
2006-06-22 11:24:19 +00:00
|
|
|
bool route(bool authenticated = false);
|
2006-06-18 13:28:25 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void hangup(const char* reason = 0, bool reject = false);
|
|
|
|
inline void hangup(IAXEvent* event, const char* reason = 0, bool reject = false) {
|
|
|
|
event->setFinal();
|
|
|
|
hangup(reason,reject);
|
|
|
|
}
|
|
|
|
void startAudioIn();
|
|
|
|
void startAudioOut();
|
|
|
|
void evAuthRep(IAXEvent* event);
|
2007-01-10 18:56:27 +00:00
|
|
|
void evAuthReq(IAXEvent* event);
|
2006-07-02 20:11:46 +00:00
|
|
|
// Safe deref the connection if the reference counter was increased during registration
|
2006-06-18 13:28:25 +00:00
|
|
|
void safeDeref();
|
2006-06-26 05:19:52 +00:00
|
|
|
bool safeRefIncrease();
|
2006-06-18 13:28:25 +00:00
|
|
|
private:
|
2006-06-26 05:19:52 +00:00
|
|
|
YIAXEngine* m_iaxEngine; // IAX engine owning the transaction
|
|
|
|
IAXTransaction* m_transaction; // IAX transaction
|
|
|
|
String m_password; // Password for client authentication
|
|
|
|
bool m_mutedIn; // No remote media accepted
|
|
|
|
bool m_mutedOut; // No local media accepted
|
|
|
|
String m_reason; // Call end reason text
|
|
|
|
bool m_hangup; // Need to send chan.hangup message
|
2006-07-02 20:11:46 +00:00
|
|
|
Mutex m_mutexTrans; // Safe m_transaction operations
|
2006-06-26 05:19:52 +00:00
|
|
|
Mutex m_mutexRefIncreased; // Safe ref/deref connection
|
|
|
|
bool m_refIncreased; // If true, the reference counter was increased
|
2006-06-18 13:28:25 +00:00
|
|
|
};
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-08-08 10:04:30 +00:00
|
|
|
* An IAX URI parser
|
2006-06-18 13:28:25 +00:00
|
|
|
* [iax[2]:][username@]host[:port][/called_number[@called_context]]
|
|
|
|
*/
|
|
|
|
class IAXURI : public String
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline IAXURI(String& s) : String(s), m_port(0), m_parsed(false) {}
|
|
|
|
inline IAXURI(const char* s) : String(s), m_port(0), m_parsed(false) {}
|
|
|
|
IAXURI(const char* user, const char* host, const char* calledNo, const char* calledContext, int port = 4569);
|
|
|
|
inline ~IAXURI() {}
|
|
|
|
void parse();
|
2006-06-22 11:24:19 +00:00
|
|
|
bool fillList(NamedList& dest);
|
|
|
|
bool setAddr(SocketAddr& dest);
|
2006-06-18 13:28:25 +00:00
|
|
|
inline const String& username() const
|
|
|
|
{ return m_username; }
|
|
|
|
inline const String& host() const
|
|
|
|
{ return m_host; }
|
|
|
|
inline int port() const
|
|
|
|
{ return m_port; }
|
|
|
|
inline const String& calledNo() const
|
|
|
|
{ return m_calledNo; }
|
|
|
|
inline const String& calledContext() const
|
|
|
|
{ return m_calledContext; }
|
|
|
|
protected:
|
|
|
|
inline IAXURI() {}
|
|
|
|
private:
|
|
|
|
String m_username;
|
|
|
|
String m_host;
|
|
|
|
int m_port;
|
|
|
|
String m_calledNo;
|
|
|
|
String m_calledContext;
|
|
|
|
bool m_parsed;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Local data
|
|
|
|
*/
|
2006-07-02 20:11:46 +00:00
|
|
|
static Configuration s_cfg; // Configuration file
|
|
|
|
static YIAXLineContainer s_lines; // Lines
|
2007-05-23 14:15:22 +00:00
|
|
|
static Thread::Priority s_priority = Thread::Normal; // Threads priority
|
2009-07-22 12:57:14 +00:00
|
|
|
static YIAXDriver iplugin; // Init the driver
|
2007-05-23 14:15:22 +00:00
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
/*
|
2006-06-18 13:28:25 +00:00
|
|
|
* Class definitions
|
|
|
|
*/
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create an idle line
|
2006-06-18 13:28:25 +00:00
|
|
|
YIAXLine::YIAXLine(const String& name)
|
2006-06-18 21:59:23 +00:00
|
|
|
: String(name),
|
|
|
|
m_state(Idle), m_expire(60), m_localPort(4569), m_remotePort(4569),
|
2006-06-22 11:24:19 +00:00
|
|
|
m_nextReg(Time::secNow() + 40),
|
|
|
|
m_registered(false),
|
|
|
|
m_register(true)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXLine::~YIAXLine()
|
|
|
|
{
|
2006-08-12 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the registered status, emits user.notify messages if necessary
|
|
|
|
void YIAXLine::setRegistered(bool registered, const char* reason)
|
|
|
|
{
|
|
|
|
if ((m_registered == registered) && !reason)
|
|
|
|
return;
|
|
|
|
m_registered = registered;
|
|
|
|
if (m_username) {
|
|
|
|
Message* m = new Message("user.notify");
|
|
|
|
m->addParam("account",*this);
|
|
|
|
m->addParam("protocol","iax");
|
|
|
|
m->addParam("username",m_username);
|
|
|
|
m->addParam("registered",String::boolText(registered));
|
|
|
|
if (reason)
|
|
|
|
m->addParam("reason",reason);
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Find and update a line with parameters from message, create if needed
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXLineContainer::updateLine(Message& msg)
|
|
|
|
{
|
|
|
|
Lock lock(this);
|
|
|
|
String name = msg.getValue("account");
|
|
|
|
YIAXLine* line = findLine(name);
|
|
|
|
if (line)
|
|
|
|
return updateLine(line,msg);
|
|
|
|
return addLine(msg);
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Handle registration related transaction terminations
|
2006-06-22 11:24:19 +00:00
|
|
|
void YIAXLineContainer::regTerminate(IAXEvent* event)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
Lock lock(this);
|
2006-06-22 11:24:19 +00:00
|
|
|
if (!event)
|
2006-06-18 13:28:25 +00:00
|
|
|
return;
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* trans = event->getTransaction();
|
|
|
|
if (!trans)
|
2006-06-18 13:28:25 +00:00
|
|
|
return;
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXLine* line = findLine(static_cast<YIAXLine*>(trans->getUserData()));
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!line)
|
|
|
|
return;
|
2006-06-22 11:24:19 +00:00
|
|
|
switch (event->type()) {
|
|
|
|
case IAXEvent::Accept:
|
|
|
|
// re-register at 75% of the expire time
|
|
|
|
line->m_nextReg = Time::secNow() + (line->expire() * 3 / 4);
|
|
|
|
line->m_callingNo = trans->callingNo();
|
|
|
|
line->m_callingName = trans->callingName();
|
2006-07-04 19:34:46 +00:00
|
|
|
Debug(&iplugin,DebugAll,"YIAXLineContainer::regTerminate[%s] - ACK for '%s'. Next: %u",
|
2006-06-26 05:19:52 +00:00
|
|
|
line->c_str(),line->state() == YIAXLine::Registering?"Register":"Unregister",line->m_nextReg);
|
2006-08-12 16:49:02 +00:00
|
|
|
line->setRegistered(true);
|
2006-06-22 11:24:19 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Reject:
|
|
|
|
// retry at 25% of the expire time
|
2006-06-26 05:19:52 +00:00
|
|
|
line->m_nextReg = Time::secNow() + (line->expire() / 2);
|
2006-07-04 19:34:46 +00:00
|
|
|
Debug(&iplugin,DebugAll,"YIAXLineContainer::regTerminate[%s] - REJECT for '%s'. Next: %u",
|
2006-06-26 05:19:52 +00:00
|
|
|
line->c_str(),line->state() == YIAXLine::Registering?"Register":"Unregister",line->m_nextReg);
|
2006-08-12 16:49:02 +00:00
|
|
|
line->setRegistered(false,"rejected");
|
2006-06-22 11:24:19 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Timeout:
|
|
|
|
// retry at 50% of the expire time
|
|
|
|
line->m_nextReg = Time::secNow() + (line->expire() / 2);
|
2006-06-26 05:19:52 +00:00
|
|
|
Debug(&iplugin,DebugAll,"YIAXLineContainer::regTerminate[%s] - Timeout for '%s'. Next: %u",
|
|
|
|
line->c_str(),line->state() == YIAXLine::Registering?"Register":"Unregister",line->m_nextReg);
|
2006-08-12 16:49:02 +00:00
|
|
|
line->setRegistered(false,"timeout");
|
2006-06-22 11:24:19 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line->m_transaction = 0;
|
|
|
|
// Unregister operation. Remove line
|
2006-06-18 13:28:25 +00:00
|
|
|
if (line->state() == YIAXLine::Unregistering) {
|
2006-08-19 10:18:25 +00:00
|
|
|
line->setRegistered(false);
|
2006-06-18 13:28:25 +00:00
|
|
|
m_lines.remove(line,true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line->m_state = YIAXLine::Idle;
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:27 +00:00
|
|
|
// Handle registration related authentication
|
|
|
|
void YIAXLineContainer::regAuthReq(IAXEvent* event)
|
|
|
|
{
|
|
|
|
Lock lock(this);
|
|
|
|
if (!event || event->type() != IAXEvent::AuthReq)
|
|
|
|
return;
|
|
|
|
IAXTransaction* trans = event->getTransaction();
|
|
|
|
if (!trans)
|
|
|
|
return;
|
|
|
|
YIAXLine* line = findLine(static_cast<YIAXLine*>(trans->getUserData()));
|
|
|
|
if (!line) {
|
|
|
|
Debug(&iplugin,DebugAll,"Lines: NO LINE");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Send auth reply
|
|
|
|
String response;
|
|
|
|
if (!iplugin.getEngine())
|
|
|
|
return;
|
|
|
|
iplugin.getEngine()->getMD5FromChallenge(response,trans->challenge(),line->password());
|
|
|
|
trans->sendAuthReply(response);
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Handle registration related events
|
2006-06-22 11:24:19 +00:00
|
|
|
void YIAXLineContainer::handleEvent(IAXEvent* event)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
switch (event->type()) {
|
|
|
|
case IAXEvent::Accept:
|
|
|
|
case IAXEvent::Reject:
|
|
|
|
case IAXEvent::Timeout:
|
|
|
|
regTerminate(event);
|
|
|
|
break;
|
2007-01-10 18:56:27 +00:00
|
|
|
case IAXEvent::AuthReq:
|
|
|
|
regAuthReq(event);
|
|
|
|
break;
|
2006-06-22 11:24:19 +00:00
|
|
|
default:
|
2006-06-26 05:19:52 +00:00
|
|
|
iplugin.getEngine()->defaultEventHandler(event);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Tick the timer for all lines, send keepalives and reregister
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXLineContainer::evTimer(Time& time)
|
|
|
|
{
|
2006-06-18 21:59:23 +00:00
|
|
|
u_int32_t sec = time.sec();
|
2006-06-18 13:28:25 +00:00
|
|
|
Lock lock(this);
|
|
|
|
for (ObjList* l = m_lines.skipNull(); l; l = l->next()) {
|
|
|
|
YIAXLine* line = static_cast<YIAXLine*>(l->get());
|
2006-07-02 20:11:46 +00:00
|
|
|
// Line exists and is idle ?
|
2006-06-22 11:24:19 +00:00
|
|
|
if (!line || line->state() != YIAXLine::Idle)
|
2006-06-18 13:28:25 +00:00
|
|
|
continue;
|
2006-07-02 20:11:46 +00:00
|
|
|
// Time to keep alive
|
2006-06-18 21:59:23 +00:00
|
|
|
if (sec > line->m_nextKeepAlive) {
|
|
|
|
line->m_nextKeepAlive = sec + 25;
|
2006-06-18 13:28:25 +00:00
|
|
|
SocketAddr addr(AF_INET);
|
|
|
|
addr.host(line->remoteAddr());
|
|
|
|
addr.port(line->remotePort());
|
|
|
|
iplugin.getEngine()->keepAlive(addr);
|
|
|
|
}
|
2006-07-02 20:11:46 +00:00
|
|
|
// Time to reg/unreg
|
2006-06-18 21:59:23 +00:00
|
|
|
if (sec > line->m_nextReg) {
|
|
|
|
line->m_nextReg += line->expire();
|
2006-06-22 11:24:19 +00:00
|
|
|
if (line->m_register)
|
|
|
|
startRegisterLine(line);
|
|
|
|
else
|
|
|
|
startUnregisterLine(line);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Fill parameters with information taken from line
|
2006-06-22 11:24:19 +00:00
|
|
|
bool YIAXLineContainer::fillList(String& name, NamedList& dest, SocketAddr& addr, bool& registered)
|
|
|
|
{
|
|
|
|
Lock lock(this);
|
|
|
|
registered = false;
|
|
|
|
YIAXLine* line = findLine(name);
|
|
|
|
if (!line)
|
|
|
|
return false;
|
|
|
|
dest.setParam("username",line->username());
|
|
|
|
dest.setParam("password",line->password());
|
|
|
|
dest.setParam("caller",line->callingNo());
|
|
|
|
dest.setParam("callername",line->callingName());
|
|
|
|
addr.host(line->remoteAddr());
|
|
|
|
addr.port(line->remotePort());
|
|
|
|
registered = line->registered();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Update a line with data from message, create or delete line if needed
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXLineContainer::updateLine(YIAXLine* line, Message& msg)
|
|
|
|
{
|
|
|
|
Debug(&iplugin,DebugAll,"YIAXLineContainer - updateLine: %s",line->c_str());
|
|
|
|
String op = msg.getValue("operation");
|
|
|
|
if (op == "logout") {
|
2006-06-22 11:24:19 +00:00
|
|
|
if (line->state() != YIAXLine::Unregistering)
|
|
|
|
return true;
|
|
|
|
if (line->state() != YIAXLine::Idle && line->m_transaction)
|
|
|
|
line->m_transaction->abortReg();
|
2006-06-18 13:28:25 +00:00
|
|
|
startUnregisterLine(line);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool change = false;
|
|
|
|
if (line->m_remoteAddr != msg.getValue("server")) {
|
|
|
|
line->m_remoteAddr = msg.getValue("server");
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
if (line->m_username != msg.getValue("username")) {
|
|
|
|
line->m_username = msg.getValue("username");
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
if (line->m_password != msg.getValue("password")) {
|
|
|
|
line->m_password = msg.getValue("password");
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
if (line->m_expire != String(msg.getValue("interval")).toInteger()) {
|
|
|
|
line->m_expire = String(msg.getValue("interval")).toInteger();
|
|
|
|
change = true;
|
|
|
|
}
|
2006-06-18 21:59:23 +00:00
|
|
|
line->m_nextReg = Time::secNow() + (line->m_expire * 3 / 4);
|
2006-06-18 13:28:25 +00:00
|
|
|
line->m_nextKeepAlive = Time::secNow() + 25;
|
|
|
|
if (change || op == "login")
|
|
|
|
startRegisterLine(line);
|
|
|
|
return change;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Add a new line and start logging in to it if applicable
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXLineContainer::addLine(Message& msg)
|
|
|
|
{
|
|
|
|
Debug(&iplugin,DebugAll,"YIAXLineContainer - addLine: %s",msg.getValue("account"));
|
|
|
|
YIAXLine* line = new YIAXLine(msg.getValue("account"));
|
|
|
|
m_lines.append(line);
|
|
|
|
line->m_remoteAddr = msg.getValue("server");
|
|
|
|
line->m_username = msg.getValue("username");
|
|
|
|
line->m_password = msg.getValue("password");
|
2006-06-18 21:59:23 +00:00
|
|
|
line->m_expire = msg.getIntValue("interval",60);
|
|
|
|
line->m_nextReg = Time::secNow() + line->m_expire;
|
2006-06-18 13:28:25 +00:00
|
|
|
String op = msg.getValue("operation");
|
2006-06-18 21:59:23 +00:00
|
|
|
if (op.null() || (op == "login"))
|
2006-06-18 13:28:25 +00:00
|
|
|
startRegisterLine(line);
|
|
|
|
else
|
|
|
|
if (op == "logout")
|
|
|
|
startUnregisterLine(line);
|
2006-06-22 11:24:19 +00:00
|
|
|
else
|
|
|
|
line->m_register = true;
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Find a line by name
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXLine* YIAXLineContainer::findLine(const String& name)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
for (ObjList* l = m_lines.skipNull(); l; l = l->next()) {
|
|
|
|
YIAXLine* line = static_cast<YIAXLine*>(l->get());
|
|
|
|
if (line && *line == name)
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Find a line by address, return same if found
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXLine* YIAXLineContainer::findLine(YIAXLine* line)
|
|
|
|
{
|
|
|
|
if (!line)
|
|
|
|
return 0;
|
|
|
|
for (ObjList* l = m_lines.skipNull(); l; l = l->next()) {
|
|
|
|
YIAXLine* ln = static_cast<YIAXLine*>(l->get());
|
|
|
|
if (ln && ln == line)
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Initiate registration of line, only if idle
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXLineContainer::startRegisterLine(YIAXLine* line)
|
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
Lock lock(this);
|
|
|
|
line->m_register = true;
|
|
|
|
if (line->state() != YIAXLine::Idle)
|
|
|
|
return;
|
2006-06-18 13:28:25 +00:00
|
|
|
if (iplugin.getEngine()->reg(line,true))
|
|
|
|
line->m_state = YIAXLine::Registering;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Initiate deregistration of line, only if idle
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXLineContainer::startUnregisterLine(YIAXLine* line)
|
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
Lock lock(this);
|
|
|
|
line->m_register = false;
|
|
|
|
if (line->state() != YIAXLine::Idle)
|
|
|
|
return;
|
2006-06-18 13:28:25 +00:00
|
|
|
if (iplugin.getEngine()->reg(line,false))
|
|
|
|
line->m_state = YIAXLine::Unregistering;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Unregister all lines
|
2006-08-19 10:18:25 +00:00
|
|
|
void YIAXLineContainer::clear(bool forced)
|
2006-06-18 21:59:23 +00:00
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
Lock lock(this);
|
2006-08-19 10:18:25 +00:00
|
|
|
if (forced) {
|
|
|
|
m_lines.clear();
|
|
|
|
return;
|
|
|
|
}
|
2006-06-18 21:59:23 +00:00
|
|
|
for (ObjList* l = m_lines.skipNull(); l; l = l->next()) {
|
|
|
|
YIAXLine* line = static_cast<YIAXLine*>(l->get());
|
|
|
|
if (line)
|
|
|
|
startUnregisterLine(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Run the socket listening thread
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXListener::run()
|
|
|
|
{
|
2006-09-25 14:03:14 +00:00
|
|
|
DDebug(m_engine,DebugAll,"%s started",currentName());
|
2006-06-18 13:28:25 +00:00
|
|
|
SocketAddr addr;
|
|
|
|
m_engine->readSocket(addr);
|
|
|
|
}
|
|
|
|
|
2007-05-23 14:15:22 +00:00
|
|
|
// Run the event retreiving thread
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXGetEvent::run()
|
|
|
|
{
|
2006-09-25 14:03:14 +00:00
|
|
|
DDebug(m_engine,DebugAll,"%s started",currentName());
|
2006-06-18 13:28:25 +00:00
|
|
|
m_engine->runGetEvents();
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Run the trunk sending thread
|
2006-07-02 20:11:46 +00:00
|
|
|
void YIAXTrunking::run()
|
|
|
|
{
|
2006-09-25 14:03:14 +00:00
|
|
|
DDebug(m_engine,DebugAll,"%s started",currentName());
|
2006-07-02 20:11:46 +00:00
|
|
|
m_engine->runProcessTrunkFrames();
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
/**
|
|
|
|
* YIAXEngine
|
|
|
|
*/
|
2006-10-14 10:51:52 +00:00
|
|
|
YIAXEngine::YIAXEngine(const char* iface, int port, u_int16_t transListCount,
|
|
|
|
u_int16_t retransCount, u_int16_t retransInterval, u_int16_t authTimeout,
|
|
|
|
u_int16_t transTimeout, u_int16_t maxFullFrameDataLen,
|
|
|
|
u_int32_t trunkSendInterval, bool authRequired)
|
|
|
|
: IAXEngine(iface,port,transListCount,retransCount,retransInterval,authTimeout,
|
|
|
|
transTimeout,maxFullFrameDataLen,iplugin.defaultCodec(),iplugin.codecs(),
|
|
|
|
trunkSendInterval,authRequired),
|
2006-06-26 05:19:52 +00:00
|
|
|
m_threadsCreated(false)
|
2006-06-22 11:24:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Handle received voice data, forward it to connection's source
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXEngine::processMedia(IAXTransaction* transaction, DataBlock& data, u_int32_t tStamp)
|
|
|
|
{
|
|
|
|
if (transaction)
|
|
|
|
if (transaction->getUserData())
|
|
|
|
if ((static_cast<YIAXConnection*>(transaction->getUserData()))->getSource())
|
2006-10-26 14:14:47 +00:00
|
|
|
// Multiply tStamp with 8 to keep the consumer satisfied
|
|
|
|
(static_cast<YIAXSource*>((static_cast<YIAXConnection*>(transaction->getUserData()))->getSource()))->Forward(data,tStamp * 8);
|
2006-06-26 05:19:52 +00:00
|
|
|
else {
|
2007-04-17 09:01:48 +00:00
|
|
|
XDebug(this,DebugAll,"processMedia. No media source");
|
2006-06-26 05:19:52 +00:00
|
|
|
}
|
2006-06-18 13:28:25 +00:00
|
|
|
else
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,"processMedia. Transaction doesn't have a connection");
|
2006-06-18 13:28:25 +00:00
|
|
|
else
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,"processMedia. No transaction");
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create a new registration transaction for a line
|
2006-06-18 13:28:25 +00:00
|
|
|
IAXTransaction* YIAXEngine::reg(YIAXLine* line, bool regreq)
|
|
|
|
{
|
|
|
|
if (!line)
|
|
|
|
return 0;
|
|
|
|
SocketAddr addr(AF_INET);
|
|
|
|
addr.host(line->remoteAddr());
|
|
|
|
addr.port(line->remotePort());
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,
|
2010-02-15 21:07:17 +00:00
|
|
|
"Outgoing Registration[%s]:\r\nUsername: %s\r\nHost: %s\r\nPort: %d\r\nTime(sec): %u",
|
2006-07-04 19:34:46 +00:00
|
|
|
line->c_str(),line->username().c_str(),addr.host().c_str(),addr.port(),Time::secNow());
|
2006-06-26 05:19:52 +00:00
|
|
|
// Create IE list
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXIEList ieList;
|
|
|
|
ieList.appendString(IAXInfoElement::USERNAME,line->username());
|
|
|
|
ieList.appendString(IAXInfoElement::PASSWORD,line->password());
|
|
|
|
ieList.appendString(IAXInfoElement::CALLING_NUMBER,line->callingNo());
|
|
|
|
ieList.appendString(IAXInfoElement::CALLING_NAME,line->callingName());
|
|
|
|
ieList.appendNumeric(IAXInfoElement::REFRESH,line->expire(),2);
|
2006-06-26 05:19:52 +00:00
|
|
|
// Make it !
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* tr = startLocalTransaction(regreq ? IAXTransaction::RegReq : IAXTransaction::RegRel,addr,ieList);
|
2006-06-18 13:28:25 +00:00
|
|
|
if (tr)
|
2006-06-22 11:24:19 +00:00
|
|
|
tr->setUserData(line);
|
|
|
|
line->m_transaction = tr;
|
2006-06-18 13:28:25 +00:00
|
|
|
return tr;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create a new call transaction from target address and message params
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* YIAXEngine::call(SocketAddr& addr, NamedList& params)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,
|
2010-02-15 21:07:17 +00:00
|
|
|
"Outgoing Call:\r\nUsername: %s\r\nHost: %s\r\nPort: %d\r\nCalled number: %s\r\nCalled context: %s",
|
2007-04-17 09:01:48 +00:00
|
|
|
params.getValue("username"),addr.host().c_str(),addr.port(),
|
|
|
|
params.getValue("called"),params.getValue("calledname"));
|
2006-06-26 05:19:52 +00:00
|
|
|
// Create IE list
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXIEList ieList;
|
|
|
|
ieList.appendString(IAXInfoElement::USERNAME,params.getValue("username"));
|
|
|
|
ieList.appendString(IAXInfoElement::PASSWORD,params.getValue("password"));
|
|
|
|
ieList.appendString(IAXInfoElement::CALLING_NUMBER,params.getValue("caller"));
|
|
|
|
ieList.appendString(IAXInfoElement::CALLING_NAME,params.getValue("callername"));
|
|
|
|
ieList.appendString(IAXInfoElement::CALLED_NUMBER,params.getValue("called"));
|
2006-09-12 16:54:29 +00:00
|
|
|
ieList.appendString(IAXInfoElement::CALLED_CONTEXT,params.getValue("iaxcontext"));
|
2006-10-26 14:14:47 +00:00
|
|
|
// Set format and capabilities
|
2006-09-25 14:03:14 +00:00
|
|
|
u_int32_t codecs = iplugin.codecs();
|
|
|
|
if (!iplugin.updateCodecsFromRoute(codecs,params.getValue("formats"))) {
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"Outgoing call failed: No codecs");
|
2006-09-25 14:03:14 +00:00
|
|
|
params.setParam("error","nomedia");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-26 14:14:47 +00:00
|
|
|
u_int32_t format = iplugin.defaultCodec();
|
|
|
|
if (!(format & codecs)) {
|
|
|
|
for (format = 1; format < 0x10000; format = format << 1)
|
|
|
|
if (format & codecs)
|
|
|
|
break;
|
|
|
|
if (format >= 0x10000) {
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"Outgoing call failed: No preffered format");
|
2006-10-26 14:14:47 +00:00
|
|
|
params.setParam("error","nomedia");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ieList.appendNumeric(IAXInfoElement::FORMAT,format,4);
|
2006-09-25 14:03:14 +00:00
|
|
|
ieList.appendNumeric(IAXInfoElement::CAPABILITY,codecs,4);
|
2006-06-22 11:24:19 +00:00
|
|
|
return startLocalTransaction(IAXTransaction::New,addr,ieList);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create a POKE transaction
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* YIAXEngine::poke(SocketAddr& addr)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
Debug(this,DebugAll,"Outgoing POKE: Host: %s Port: %d",addr.host().c_str(),addr.port());
|
|
|
|
IAXIEList ieList;
|
|
|
|
return startLocalTransaction(IAXTransaction::Poke,addr,ieList);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
void YIAXEngine::start(u_int16_t listenThreadCount, u_int16_t eventThreadCount, u_int16_t trunkThreadCount)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
if (m_threadsCreated)
|
|
|
|
return;
|
|
|
|
if (!listenThreadCount)
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugWarn,"YIAXEngine. No reading socket threads(s)!");
|
2006-06-26 05:19:52 +00:00
|
|
|
if (!eventThreadCount)
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugWarn,"YIAXEngine. No reading event threads(s)!");
|
2006-07-02 20:11:46 +00:00
|
|
|
if (!trunkThreadCount)
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugWarn,"YIAXEngine. No trunking threads(s)!");
|
2006-06-18 13:28:25 +00:00
|
|
|
for (; listenThreadCount; listenThreadCount--)
|
2009-06-22 14:48:26 +00:00
|
|
|
(new YIAXListener(this,"YIAX Listener",s_priority))->startup();
|
2006-06-26 05:19:52 +00:00
|
|
|
for (; eventThreadCount; eventThreadCount--)
|
2009-06-22 14:48:26 +00:00
|
|
|
(new YIAXGetEvent(this,"YIAX GetEvent"))->startup();
|
2006-07-02 20:11:46 +00:00
|
|
|
for (; trunkThreadCount; trunkThreadCount--)
|
2009-06-22 14:48:26 +00:00
|
|
|
(new YIAXTrunking(this,"YIAX Trunking",s_priority))->startup();
|
2006-06-22 11:24:19 +00:00
|
|
|
m_threadsCreated = true;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Process all IAX events
|
2006-06-22 11:24:19 +00:00
|
|
|
void YIAXEngine::processEvent(IAXEvent* event)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXConnection* connection = 0;
|
|
|
|
switch (event->getTransaction()->type()) {
|
|
|
|
case IAXTransaction::New:
|
|
|
|
connection = static_cast<YIAXConnection*>(event->getTransaction()->getUserData());
|
|
|
|
if (connection) {
|
|
|
|
// We already have a channel for this call
|
|
|
|
connection->handleEvent(event);
|
|
|
|
if (event->final()) {
|
2006-06-26 05:19:52 +00:00
|
|
|
// Final event: disconnect
|
2007-04-19 08:43:14 +00:00
|
|
|
Debug(this,DebugAll,"processEvent. Disconnecting (%p): '%s'",
|
|
|
|
connection,connection->id().c_str());
|
2006-06-22 11:24:19 +00:00
|
|
|
connection->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (event->type() == IAXEvent::New) {
|
2006-06-26 05:19:52 +00:00
|
|
|
// Incoming request for a new call
|
2006-06-22 11:24:19 +00:00
|
|
|
connection = new YIAXConnection(this,event->getTransaction());
|
2010-01-26 10:31:32 +00:00
|
|
|
connection->initChan();
|
2006-06-22 11:24:19 +00:00
|
|
|
event->getTransaction()->setUserData(connection);
|
|
|
|
if (!connection->route())
|
|
|
|
event->getTransaction()->setUserData(0);
|
|
|
|
}
|
|
|
|
}
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
2006-06-22 11:24:19 +00:00
|
|
|
case IAXTransaction::RegReq:
|
|
|
|
case IAXTransaction::RegRel:
|
2006-10-14 10:51:52 +00:00
|
|
|
if (event->type() == IAXEvent::New || event->type() == IAXEvent::AuthRep)
|
|
|
|
processRemoteReg(event,(event->type() == IAXEvent::New));
|
2006-06-26 05:19:52 +00:00
|
|
|
else
|
|
|
|
if (event->getTransaction()->getUserData())
|
|
|
|
s_lines.handleEvent(event);
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
2006-06-22 11:24:19 +00:00
|
|
|
default: ;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
2006-06-22 11:24:19 +00:00
|
|
|
delete event;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Process events for remote users registering to us
|
2006-10-14 10:51:52 +00:00
|
|
|
void YIAXEngine::processRemoteReg(IAXEvent* event, bool first)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* tr = event->getTransaction();
|
|
|
|
Debug(this,DebugAll,"processRemoteReg: %s username: '%s'",
|
|
|
|
tr->type() == IAXTransaction::RegReq?"Register":"Unregister",tr->username().c_str());
|
2006-10-14 10:51:52 +00:00
|
|
|
// Check for automatomatically authentication request if it's the first request
|
|
|
|
if (first && iplugin.getEngine()->authRequired()) {
|
|
|
|
Debug(this,DebugAll,"processRemoteReg. Request authentication");
|
|
|
|
tr->sendAuth();
|
2006-06-18 13:28:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-10-14 10:51:52 +00:00
|
|
|
// Authenticated: register/unregister
|
|
|
|
bool requestAuth = false, invalidAuth = false;
|
|
|
|
if (iplugin.userAuth(tr,!first,requestAuth,invalidAuth)) {
|
|
|
|
// Authenticated. Try to (un)register
|
2006-06-26 05:49:06 +00:00
|
|
|
if (userreg(tr,event->subclass() == IAXControl::RegRel)) {
|
2006-06-26 05:19:52 +00:00
|
|
|
Debug(this,DebugAll,"processRemoteReg. Authenticated and (un)registered. Ack");
|
2006-06-22 11:24:19 +00:00
|
|
|
tr->sendAccept();
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-06-26 05:19:52 +00:00
|
|
|
Debug(this,DebugAll,"processRemoteReg. Authenticated but not (un)registered. Reject");
|
2006-10-14 10:51:52 +00:00
|
|
|
tr->sendReject("not registered");
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2006-10-14 10:51:52 +00:00
|
|
|
// First request: check if we should request auth
|
|
|
|
const char* reason = 0;
|
|
|
|
if (first && requestAuth) {
|
|
|
|
Debug(this,DebugAll,"processRemoteReg. Request authentication");
|
|
|
|
tr->sendAuth();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (invalidAuth)
|
|
|
|
reason = IAXTransaction::s_iax_modInvalidAuth;
|
|
|
|
if (!reason)
|
|
|
|
reason = "not authenticated";
|
|
|
|
Debug(this,DebugAll,"processRemoteReg. Not authenticated ('%s'). Reject",reason);
|
|
|
|
tr->sendReject(reason);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Build and dispatch the user.(un)register message
|
2006-06-26 05:49:06 +00:00
|
|
|
bool YIAXEngine::userreg(IAXTransaction* tr, bool regrel)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-26 05:49:06 +00:00
|
|
|
Debug(this,DebugAll,"YIAXEngine - userreg. %s username: '%s'",
|
|
|
|
regrel ? "Unregistering":"Registering",tr->username().c_str());
|
2006-06-22 11:24:19 +00:00
|
|
|
Message msg(regrel ? "user.unregister" : "user.register");
|
2006-06-26 05:49:06 +00:00
|
|
|
msg.addParam("username",tr->username());
|
|
|
|
msg.addParam("driver","iax");
|
|
|
|
if (!regrel) {
|
|
|
|
String data = "iax/iax2:";
|
|
|
|
data << tr->username() << "@";
|
|
|
|
data << tr->remoteAddr().host() << ":" << tr->remoteAddr().port();
|
2008-03-28 16:06:21 +00:00
|
|
|
const char* called = tr->calledNo();
|
|
|
|
const char* context = tr->calledContext();
|
|
|
|
if (called || context) {
|
|
|
|
data << "/" << called;
|
|
|
|
if (context)
|
|
|
|
data << "@" << context;
|
|
|
|
}
|
2006-06-26 05:49:06 +00:00
|
|
|
msg.addParam("data",data);
|
2006-07-02 20:38:51 +00:00
|
|
|
msg.addParam("expires",String((unsigned int)tr->expire()));
|
2006-06-26 05:49:06 +00:00
|
|
|
}
|
|
|
|
msg.addParam("ip_host",tr->remoteAddr().host());
|
|
|
|
msg.addParam("ip_port",String(tr->remoteAddr().port()));
|
2006-06-22 11:24:19 +00:00
|
|
|
return Engine::dispatch(msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Handler for outgoing registration messages
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXRegDataHandler::received(Message& msg)
|
|
|
|
{
|
|
|
|
String tmp(msg.getValue("protocol"));
|
|
|
|
if (tmp != "iax")
|
|
|
|
return false;
|
|
|
|
tmp = msg.getValue("account");
|
|
|
|
if (tmp.null())
|
|
|
|
return false;
|
2006-06-18 21:59:23 +00:00
|
|
|
s_lines.updateLine(msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXDriver::YIAXDriver()
|
|
|
|
: Driver("iax","varchans"), m_iaxEngine(0), m_port(4569)
|
|
|
|
{
|
|
|
|
Output("Loaded module YIAX");
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXDriver::~YIAXDriver()
|
|
|
|
{
|
|
|
|
Output("Unloading module YIAX");
|
|
|
|
lock();
|
|
|
|
channels().clear();
|
2006-08-19 10:18:25 +00:00
|
|
|
s_lines.clear(true);
|
2006-06-18 13:28:25 +00:00
|
|
|
unlock();
|
|
|
|
delete m_iaxEngine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void YIAXDriver::initialize()
|
|
|
|
{
|
|
|
|
Output("Initializing module YIAX");
|
|
|
|
lock();
|
2006-06-26 05:19:52 +00:00
|
|
|
// Load configuration
|
2006-06-18 13:28:25 +00:00
|
|
|
s_cfg = Engine::configFile("yiaxchan");
|
|
|
|
s_cfg.load();
|
2006-06-26 05:19:52 +00:00
|
|
|
// Codec capabilities
|
2006-06-18 13:28:25 +00:00
|
|
|
m_defaultCodec = 0;
|
|
|
|
m_codecs = 0;
|
|
|
|
u_int32_t fallback = 0;
|
|
|
|
String preferred = s_cfg.getValue("formats","preferred");
|
|
|
|
bool def = s_cfg.getBoolValue("formats","default",true);
|
2006-10-31 15:18:08 +00:00
|
|
|
for (int i = 0; dict_payloads[i].token; i++) {
|
|
|
|
if (s_cfg.getBoolValue("formats",dict_payloads[i].token,
|
|
|
|
def && DataTranslator::canConvert(dict_payloads[i].token))) {
|
2006-09-25 14:03:14 +00:00
|
|
|
XDebug(this,DebugAll,"Adding supported codec %u: '%s'.",
|
2006-10-31 15:18:08 +00:00
|
|
|
dict_payloads[i].value,dict_payloads[i].token);
|
|
|
|
m_codecs |= dict_payloads[i].value;
|
|
|
|
fallback = dict_payloads[i].value;
|
2006-06-26 05:19:52 +00:00
|
|
|
// Set default (desired) codec
|
2006-10-31 15:18:08 +00:00
|
|
|
if (preferred == dict_payloads[i].token)
|
2006-06-18 13:28:25 +00:00
|
|
|
m_defaultCodec = fallback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!m_codecs)
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugWarn,"No audio format(s) available.");
|
2006-06-26 05:19:52 +00:00
|
|
|
// If desired codec is disabled fall back to last in list
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!m_defaultCodec)
|
|
|
|
m_defaultCodec = fallback;
|
|
|
|
unlock();
|
2007-10-11 08:24:39 +00:00
|
|
|
// Setup driver if this is the first call
|
|
|
|
if (m_iaxEngine)
|
|
|
|
return;
|
2006-06-18 13:28:25 +00:00
|
|
|
setup();
|
|
|
|
installRelay(Halt);
|
2006-06-22 11:24:19 +00:00
|
|
|
installRelay(Route);
|
2007-10-11 08:24:39 +00:00
|
|
|
installRelay(Progress);
|
|
|
|
Engine::install(new YIAXRegDataHandler);
|
2006-06-26 05:19:52 +00:00
|
|
|
// Init IAX engine
|
2006-07-02 20:11:46 +00:00
|
|
|
u_int16_t transListCount = 64;
|
2006-06-26 05:19:52 +00:00
|
|
|
u_int16_t retransCount = 5;
|
|
|
|
u_int16_t retransInterval = 500;
|
|
|
|
u_int16_t authTimeout = 30;
|
|
|
|
u_int16_t transTimeout = 10;
|
|
|
|
u_int16_t maxFullFrameDataLen = 1400;
|
2006-07-02 20:11:46 +00:00
|
|
|
u_int32_t trunkSendInterval = 10;
|
2007-10-11 08:24:39 +00:00
|
|
|
m_port = s_cfg.getIntValue("general","port",4569);
|
|
|
|
String iface = s_cfg.getValue("general","addr");
|
|
|
|
bool authReq = s_cfg.getBoolValue("registrar","auth_required",true);
|
|
|
|
m_iaxEngine = new YIAXEngine(iface,m_port,transListCount,retransCount,retransInterval,authTimeout,
|
|
|
|
transTimeout,maxFullFrameDataLen,trunkSendInterval,authReq);
|
|
|
|
m_iaxEngine->debugChain(this);
|
|
|
|
int tos = s_cfg.getIntValue("general","tos",dict_tos,0);
|
|
|
|
if (tos && !m_iaxEngine->socket().setTOS(tos))
|
|
|
|
Debug(this,DebugWarn,"Could not set IP TOS to 0x%02x",tos);
|
|
|
|
s_priority = Thread::priority(s_cfg.getValue("general","thread"),s_priority);
|
|
|
|
DDebug(this,DebugInfo,"Default thread priority set to '%s'",Thread::priority(s_priority));
|
2007-04-02 12:51:23 +00:00
|
|
|
int readThreadCount = s_cfg.getIntValue("general","read_threads",Engine::clientMode() ? 1 : 3);
|
|
|
|
if (readThreadCount < 1)
|
|
|
|
readThreadCount = 1;
|
|
|
|
int eventThreadCount = s_cfg.getIntValue("general","event_threads",Engine::clientMode() ? 1 : 3);
|
|
|
|
if (eventThreadCount < 1)
|
|
|
|
eventThreadCount = 1;
|
|
|
|
int trunkingThreadCount = s_cfg.getIntValue("general","trunk_threads",1);
|
|
|
|
if (trunkingThreadCount < 1)
|
|
|
|
trunkingThreadCount = 1;
|
2006-07-02 20:11:46 +00:00
|
|
|
m_iaxEngine->start(readThreadCount,eventThreadCount,trunkingThreadCount);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2008-07-16 09:48:49 +00:00
|
|
|
// Check if we have a line
|
|
|
|
bool YIAXDriver::hasLine(const String& line) const
|
|
|
|
{
|
|
|
|
return line && s_lines.hasLine(line);
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Route calls that use a line owned by this driver
|
2006-06-22 11:24:19 +00:00
|
|
|
bool YIAXDriver::msgRoute(Message& msg)
|
|
|
|
{
|
2008-07-16 09:48:49 +00:00
|
|
|
if (!isE164(msg.getValue("called")))
|
2006-06-22 11:24:19 +00:00
|
|
|
return false;
|
2008-07-16 09:48:49 +00:00
|
|
|
return Driver::msgRoute(msg);
|
2006-06-22 11:24:19 +00:00
|
|
|
}
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXDriver::msgExecute(Message& msg, String& dest)
|
|
|
|
{
|
|
|
|
if (!msg.userData()) {
|
2006-06-18 21:59:23 +00:00
|
|
|
Debug(this,DebugAll,"No data channel for this IAX call!");
|
2006-06-18 13:28:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-06-22 11:24:19 +00:00
|
|
|
SocketAddr addr(AF_INET);
|
|
|
|
if (isE164(dest)) {
|
|
|
|
// dest is called number. Find line
|
|
|
|
String name(msg.getValue("line"));
|
|
|
|
bool lineReg;
|
|
|
|
if(!s_lines.fillList(name,msg,addr,lineReg)) {
|
|
|
|
Debug(this,DebugNote,"No line ['%s'] for this IAX call!",name.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!lineReg) {
|
|
|
|
Debug(this,DebugNote,"Line ['%s'] is not registered!",name.c_str());
|
|
|
|
msg.setParam("error","offline");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
msg.setParam("called",dest);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// dest should be an URI
|
|
|
|
IAXURI uri(dest);
|
|
|
|
uri.parse();
|
|
|
|
uri.fillList(msg);
|
|
|
|
uri.setAddr(addr);
|
|
|
|
}
|
|
|
|
if (!addr.host().length()) {
|
|
|
|
Debug(this,DebugAll,"Missing host name in this IAX call");
|
2006-06-18 21:59:23 +00:00
|
|
|
return false;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
2006-06-22 11:24:19 +00:00
|
|
|
IAXTransaction* tr = m_iaxEngine->call(addr,msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!tr)
|
|
|
|
return false;
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXConnection* conn = new YIAXConnection(m_iaxEngine,tr,&msg);
|
2010-01-26 10:31:32 +00:00
|
|
|
conn->initChan();
|
2006-06-18 13:28:25 +00:00
|
|
|
tr->setUserData(conn);
|
|
|
|
Channel* ch = static_cast<Channel*>(msg.userData());
|
|
|
|
if (ch && conn->connect(ch,msg.getValue("reason"))) {
|
2006-09-08 09:50:19 +00:00
|
|
|
conn->callConnect(msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
msg.setParam("peerid",conn->id());
|
|
|
|
msg.setParam("targetid",conn->id());
|
2006-08-08 10:04:30 +00:00
|
|
|
// Enable trunking if trunkout parameter is enabled
|
|
|
|
if (msg.getBoolValue("trunkout"))
|
2006-07-04 19:34:46 +00:00
|
|
|
m_iaxEngine->enableTrunking(tr);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
tr->setUserData(0);
|
|
|
|
conn->deref();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXDriver::received(Message& msg, int id)
|
|
|
|
{
|
|
|
|
if (id == Timer)
|
2006-06-18 21:59:23 +00:00
|
|
|
s_lines.evTimer(msg.msgTime());
|
2006-06-18 13:28:25 +00:00
|
|
|
else
|
|
|
|
if (id == Halt) {
|
|
|
|
dropAll(msg);
|
|
|
|
channels().clear();
|
2006-06-18 21:59:23 +00:00
|
|
|
s_lines.clear();
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
return Driver::received(msg,id);
|
|
|
|
}
|
|
|
|
|
2006-09-25 14:03:14 +00:00
|
|
|
bool YIAXDriver::updateCodecsFromRoute(u_int32_t& codecs, const char* formats)
|
|
|
|
{
|
|
|
|
// Extract individual codecs from 'formats'
|
|
|
|
// Check if IAXFormat contains it
|
|
|
|
// Before exiting: update 'codecs'
|
|
|
|
if (!formats)
|
|
|
|
return true;
|
|
|
|
u_int32_t codec_formats = 0;
|
|
|
|
for (u_int32_t i = 0; formats[i];) {
|
|
|
|
// Skip separator(s)
|
|
|
|
for (; formats[i] && formats[i] == ','; i++) ;
|
|
|
|
// Find first separator
|
|
|
|
u_int32_t start = i;
|
|
|
|
for (; formats[i] && formats[i] != ','; i++) ;
|
|
|
|
// Get format
|
|
|
|
if (start != i) {
|
|
|
|
// Get string
|
|
|
|
String tmp(formats + start,i - start);
|
|
|
|
// Get format from IAXFormat::audioData
|
|
|
|
u_int32_t format = 0;
|
2006-10-31 15:18:08 +00:00
|
|
|
for (u_int32_t j = 0; dict_payloads[j].value; j++)
|
|
|
|
if (tmp == dict_payloads[j].token) {
|
|
|
|
format = dict_payloads[j].value;
|
2006-09-25 14:03:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (format)
|
|
|
|
codec_formats |= format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set intersection
|
|
|
|
codecs &= codec_formats;
|
|
|
|
return codecs != 0;
|
|
|
|
}
|
|
|
|
|
2006-11-02 08:03:06 +00:00
|
|
|
void YIAXDriver::createFormatList(String& dest, u_int32_t codecs)
|
|
|
|
{
|
|
|
|
for (u_int32_t i = 0; dict_payloads[i].token; i++) {
|
|
|
|
if (!(codecs & dict_payloads[i].value))
|
|
|
|
continue;
|
2006-11-10 17:13:12 +00:00
|
|
|
dest.append(dict_payloads[i].token,",");
|
2006-11-02 08:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-14 10:51:52 +00:00
|
|
|
bool YIAXDriver::userAuth(IAXTransaction* tr, bool response, bool& requestAuth,
|
|
|
|
bool& invalidAuth)
|
|
|
|
{
|
|
|
|
requestAuth = invalidAuth = false;
|
|
|
|
// Create and dispatch user.auth
|
|
|
|
Message msg("user.auth");
|
|
|
|
msg.addParam("protocol","iax");
|
|
|
|
msg.addParam("username",tr->username());
|
|
|
|
msg.addParam("called",tr->calledNo());
|
|
|
|
msg.addParam("caller",tr->callingNo());
|
|
|
|
msg.addParam("callername",tr->callingName());
|
|
|
|
msg.addParam("ip_host",tr->remoteAddr().host());
|
|
|
|
msg.addParam("ip_port",String(tr->remoteAddr().port()));
|
|
|
|
if (response) {
|
|
|
|
msg.addParam("nonce",tr->challenge());
|
|
|
|
msg.addParam("response",tr->authdata());
|
|
|
|
}
|
|
|
|
if (!Engine::dispatch(msg))
|
|
|
|
return false;
|
|
|
|
String pwd = msg.retValue();
|
|
|
|
// We have a password
|
|
|
|
if (pwd) {
|
|
|
|
// Not a response: request authentication
|
|
|
|
if (!response) {
|
|
|
|
requestAuth = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check response
|
|
|
|
if (!IAXEngine::isMD5ChallengeCorrect(tr->authdata(),tr->challenge(),pwd)) {
|
|
|
|
invalidAuth = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
/**
|
|
|
|
* IAXConsumer
|
|
|
|
*/
|
2006-06-26 05:19:52 +00:00
|
|
|
YIAXConsumer::YIAXConsumer(YIAXConnection* conn, u_int32_t format, const char* formatText)
|
|
|
|
: DataConsumer(formatText), m_connection(conn), m_total(0), m_format(format)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXConsumer::~YIAXConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-07-02 09:24:33 +00:00
|
|
|
unsigned long YIAXConsumer::Consume(const DataBlock& data, unsigned long tStamp, unsigned long flags)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
if (m_connection && !m_connection->mutedOut()) {
|
|
|
|
m_total += data.length();
|
2009-07-02 09:24:33 +00:00
|
|
|
if (m_connection->transaction()) {
|
2006-06-26 05:19:52 +00:00
|
|
|
m_connection->transaction()->sendMedia(data,m_format);
|
2009-07-02 09:24:33 +00:00
|
|
|
return invalidStamp();
|
|
|
|
}
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
2009-07-02 09:24:33 +00:00
|
|
|
return 0;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YIAXSource
|
|
|
|
*/
|
2006-06-26 05:19:52 +00:00
|
|
|
YIAXSource::YIAXSource(YIAXConnection* conn, u_int32_t format, const char* formatText)
|
|
|
|
: DataSource(formatText), m_connection(conn), m_total(0), m_format(format)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXSource::~YIAXSource()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void YIAXSource::Forward(const DataBlock& data, unsigned long tStamp)
|
|
|
|
{
|
|
|
|
if (m_connection && m_connection->mutedIn())
|
|
|
|
return;
|
|
|
|
m_total += data.length();
|
|
|
|
DataSource::Forward(data,tStamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* YIAXConnection
|
|
|
|
*/
|
2006-06-22 11:24:19 +00:00
|
|
|
YIAXConnection::YIAXConnection(YIAXEngine* iaxEngine, IAXTransaction* transaction, Message* msg)
|
2006-06-18 13:28:25 +00:00
|
|
|
: Channel(&iplugin,0,transaction->outgoing()),
|
|
|
|
m_iaxEngine(iaxEngine), m_transaction(transaction), m_mutedIn(false), m_mutedOut(false),
|
2009-05-05 14:06:39 +00:00
|
|
|
m_hangup(true),
|
|
|
|
m_mutexTrans(true,"YIAXConnection::trans"),
|
|
|
|
m_mutexRefIncreased(true,"YIAXConnection::refIncreased"),
|
|
|
|
m_refIncreased(false)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,"Created %s [%p]",isOutgoing()?"outgoing":"incoming",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
setMaxcall(msg);
|
2007-01-23 00:17:11 +00:00
|
|
|
Message* m = message("chan.startup",msg);
|
2006-06-18 13:28:25 +00:00
|
|
|
m->setParam("direction",status());
|
2006-06-26 05:49:06 +00:00
|
|
|
if (transaction)
|
|
|
|
m_address << transaction->remoteAddr().host() << ":" << transaction->remoteAddr().port();
|
2006-06-18 13:28:25 +00:00
|
|
|
if (msg) {
|
2006-06-18 20:16:56 +00:00
|
|
|
m_targetid = msg->getValue("id");
|
2006-06-26 05:19:52 +00:00
|
|
|
m_password = msg->getValue("password");
|
2009-11-25 18:34:54 +00:00
|
|
|
m->copyParams(*msg,"caller,callername,called,billid,callto,username");
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
YIAXConnection::~YIAXConnection()
|
|
|
|
{
|
|
|
|
status("destroyed");
|
|
|
|
setConsumer();
|
|
|
|
setSource();
|
|
|
|
hangup();
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugAll,"Destroyed with reason '%s' [%p]",m_reason.safe(),this);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Incoming call accepted, possibly set trunking on this connection
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXConnection::callAccept(Message& msg)
|
|
|
|
{
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"callAccept [%p]",this);
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.lock();
|
2006-07-04 19:34:46 +00:00
|
|
|
if (m_transaction) {
|
2006-06-22 11:24:19 +00:00
|
|
|
m_transaction->sendAccept();
|
2006-08-08 10:04:30 +00:00
|
|
|
// Enable trunking if trunkin parameter is enabled
|
|
|
|
if (msg.getBoolValue("trunkin"))
|
2006-07-04 19:34:46 +00:00
|
|
|
m_iaxEngine->enableTrunking(m_transaction);
|
|
|
|
}
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.unlock();
|
2006-06-18 13:28:25 +00:00
|
|
|
Channel::callAccept(msg);
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Call rejected, check if we have to authenticate caller
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXConnection::callRejected(const char* error, const char* reason, const Message* msg)
|
|
|
|
{
|
|
|
|
Channel::callRejected(error,reason,msg);
|
2007-04-17 09:01:48 +00:00
|
|
|
if (!error)
|
|
|
|
error = reason;
|
2006-06-18 13:28:25 +00:00
|
|
|
String s(error);
|
2006-10-14 10:51:52 +00:00
|
|
|
Lock lock(m_mutexTrans);
|
2009-01-20 12:27:53 +00:00
|
|
|
if (m_transaction && s == "noauth") {
|
2007-04-17 09:01:48 +00:00
|
|
|
if (safeRefIncrease()) {
|
|
|
|
Debug(this,DebugInfo,"callRejected. Requesting authentication [%p]",this);
|
|
|
|
m_transaction->sendAuth();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error = "temporary-failure";
|
2009-01-20 12:27:53 +00:00
|
|
|
}
|
2006-10-14 10:51:52 +00:00
|
|
|
lock.drop();
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugCall,"callRejected. Reason: '%s' [%p]",error,this);
|
2006-06-18 13:28:25 +00:00
|
|
|
hangup(reason,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXConnection::callRouted(Message& msg)
|
|
|
|
{
|
2006-08-08 10:04:30 +00:00
|
|
|
// check if the caller did abort the call while routing
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!m_transaction) {
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugMild,"callRouted. No transaction: ABORT [%p]",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-06-18 21:59:23 +00:00
|
|
|
DDebug(this,DebugAll,"callRouted [%p]",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-11 08:24:39 +00:00
|
|
|
bool YIAXConnection::msgProgress(Message& msg)
|
|
|
|
{
|
|
|
|
Lock lock(&m_mutexTrans);
|
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->sendProgress();
|
|
|
|
// only start audio output for early media
|
|
|
|
startAudioOut();
|
|
|
|
return Channel::msgProgress(msg);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-06-18 20:16:56 +00:00
|
|
|
bool YIAXConnection::msgRinging(Message& msg)
|
|
|
|
{
|
2006-07-02 20:11:46 +00:00
|
|
|
Lock lock(&m_mutexTrans);
|
2006-06-18 20:16:56 +00:00
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->sendRinging();
|
2006-08-08 10:04:30 +00:00
|
|
|
// only start audio output for early media
|
2006-06-18 20:16:56 +00:00
|
|
|
startAudioOut();
|
|
|
|
return Channel::msgRinging(msg);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXConnection::msgAnswered(Message& msg)
|
|
|
|
{
|
2006-07-02 20:11:46 +00:00
|
|
|
Lock lock(&m_mutexTrans);
|
2006-06-18 20:16:56 +00:00
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->sendAnswer();
|
2006-08-08 10:04:30 +00:00
|
|
|
// fully start audio
|
2006-06-18 20:16:56 +00:00
|
|
|
startAudioIn();
|
|
|
|
startAudioOut();
|
|
|
|
return Channel::msgAnswered(msg);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
bool YIAXConnection::msgTone(Message& msg, const char* tone)
|
|
|
|
{
|
2006-07-02 20:11:46 +00:00
|
|
|
Lock lock(&m_mutexTrans);
|
2006-06-18 20:16:56 +00:00
|
|
|
if (m_transaction) {
|
|
|
|
while (tone && *tone)
|
|
|
|
m_transaction->sendDtmf(*tone++);
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXConnection::msgText(Message& msg, const char* text)
|
|
|
|
{
|
2006-07-02 20:11:46 +00:00
|
|
|
Lock lock(&m_mutexTrans);
|
2006-06-18 20:16:56 +00:00
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->sendText(text);
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void YIAXConnection::disconnected(bool final, const char* reason)
|
|
|
|
{
|
2007-04-19 08:43:14 +00:00
|
|
|
DDebug(this,DebugAll,"Disconnected. Final: %s . Reason: '%s' [%p]",
|
|
|
|
String::boolText(final),reason,this);
|
2007-04-17 09:01:48 +00:00
|
|
|
hangup(reason);
|
2006-06-18 13:28:25 +00:00
|
|
|
Channel::disconnected(final,reason);
|
|
|
|
safeDeref();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXConnection::callPrerouted(Message& msg, bool handled)
|
|
|
|
{
|
2006-08-08 10:04:30 +00:00
|
|
|
// check if the caller did abort the call while prerouting
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!m_transaction) {
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugMild,"callPrerouted. No transaction: ABORT [%p]",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-06-18 21:59:23 +00:00
|
|
|
DDebug(this,DebugAll,"callPrerouted [%p]",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void YIAXConnection::handleEvent(IAXEvent* event)
|
|
|
|
{
|
|
|
|
switch(event->type()) {
|
2006-06-22 11:24:19 +00:00
|
|
|
case IAXEvent::Text: {
|
|
|
|
String text;
|
|
|
|
event->getList().getString(IAXInfoElement::textframe,text);
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugInfo,"TEXT: '%s' [%p]",text.safe(),this);
|
2006-06-22 11:24:19 +00:00
|
|
|
Message* m = message("chan.text");
|
|
|
|
m->addParam("text",text);
|
|
|
|
Engine::enqueue(m);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IAXEvent::Dtmf: {
|
|
|
|
String dtmf((char)event->subclass());
|
2006-06-26 05:19:52 +00:00
|
|
|
dtmf.toUpper();
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"DTMF: %s [%p]",dtmf.safe(),this);
|
2006-06-22 11:24:19 +00:00
|
|
|
Message* m = message("chan.dtmf");
|
|
|
|
m->addParam("text",dtmf);
|
2008-04-23 22:50:20 +00:00
|
|
|
m->addParam("detected","iax-event");
|
2008-04-25 13:11:49 +00:00
|
|
|
dtmfEnqueue(m);
|
2006-06-22 11:24:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IAXEvent::Noise:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugInfo,"NOISE: %u [%p]",event->subclass(),this);
|
2006-06-22 11:24:19 +00:00
|
|
|
break;
|
2006-06-18 13:28:25 +00:00
|
|
|
case IAXEvent::Progressing:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugInfo,"CALL PROGRESSING [%p]",this);
|
2007-10-11 08:24:39 +00:00
|
|
|
Engine::enqueue(message("call.progress",false,true));
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Accept:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"ACCEPT [%p]",this);
|
2006-06-22 11:24:19 +00:00
|
|
|
startAudioIn();
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
2006-06-22 11:24:19 +00:00
|
|
|
case IAXEvent::Answer:
|
|
|
|
if (isAnswered())
|
|
|
|
break;
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"ANSWER [%p]",this);
|
2006-06-22 11:24:19 +00:00
|
|
|
status("answered");
|
|
|
|
startAudioIn();
|
|
|
|
startAudioOut();
|
|
|
|
Engine::enqueue(message("call.answered",false,true));
|
|
|
|
break;
|
2006-06-18 13:28:25 +00:00
|
|
|
case IAXEvent::Quelch:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"QUELCH [%p]",this);
|
2006-06-22 11:24:19 +00:00
|
|
|
m_mutedOut = true;
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Unquelch:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"UNQUELCH [%p]",this);
|
2006-06-22 11:24:19 +00:00
|
|
|
m_mutedOut = false;
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Ringing:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugInfo,"RINGING [%p]",this);
|
2006-06-22 11:24:19 +00:00
|
|
|
startAudioIn();
|
|
|
|
Engine::enqueue(message("call.ringing",false,true));
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Hangup:
|
|
|
|
case IAXEvent::Reject:
|
2007-04-17 10:14:25 +00:00
|
|
|
if (m_reason.null()) {
|
|
|
|
event->getList().getString(IAXInfoElement::CAUSE,m_reason);
|
|
|
|
DDebug(this,DebugCall,"REJECT/HANGUP: '%s' [%p]",m_reason.c_str(),this);
|
|
|
|
}
|
2009-06-19 11:58:33 +00:00
|
|
|
#ifdef DEBUG
|
2007-04-17 10:14:25 +00:00
|
|
|
else
|
2009-06-19 11:58:33 +00:00
|
|
|
Debug(this,DebugCall,"REJECT/HANGUP [%p]",this);
|
|
|
|
#endif
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Timeout:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugNote,"TIMEOUT. Transaction: %u,%u, Frame: %u,%u [%p]",
|
2006-09-25 14:03:14 +00:00
|
|
|
event->getTransaction()->localCallNo(),event->getTransaction()->remoteCallNo(),
|
2007-04-17 09:01:48 +00:00
|
|
|
event->frameType(),event->subclass(),this);
|
|
|
|
if (event->final())
|
|
|
|
m_reason = "offline";
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::Busy:
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"BUSY [%p]",this);
|
|
|
|
if (event->final())
|
|
|
|
m_reason = "busy";
|
2006-06-18 13:28:25 +00:00
|
|
|
break;
|
|
|
|
case IAXEvent::AuthRep:
|
|
|
|
evAuthRep(event);
|
|
|
|
break;
|
2007-01-10 18:56:27 +00:00
|
|
|
case IAXEvent::AuthReq:
|
|
|
|
evAuthReq(event);
|
|
|
|
break;
|
2006-06-18 13:28:25 +00:00
|
|
|
default:
|
2006-06-26 05:19:52 +00:00
|
|
|
iplugin.getEngine()->defaultEventHandler(event);
|
2006-06-18 13:28:25 +00:00
|
|
|
if (!m_transaction)
|
|
|
|
event->setFinal();
|
|
|
|
}
|
|
|
|
if (event->final()) {
|
|
|
|
safeDeref();
|
|
|
|
m_transaction = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-02 20:11:46 +00:00
|
|
|
void YIAXConnection::hangup(const char* reason, bool reject)
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
|
|
|
if (!m_hangup)
|
|
|
|
return;
|
|
|
|
m_hangup = false;
|
2007-04-17 09:01:48 +00:00
|
|
|
if (m_reason.null())
|
|
|
|
m_reason = reason;
|
|
|
|
if (m_reason.null())
|
|
|
|
m_reason = Engine::exiting() ? "shutdown" : "unknown";
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.lock();
|
2006-06-18 13:28:25 +00:00
|
|
|
if (m_transaction) {
|
|
|
|
m_transaction->setUserData(0);
|
|
|
|
if (reject)
|
2007-04-17 09:01:48 +00:00
|
|
|
m_transaction->sendReject(m_reason);
|
2006-06-18 13:28:25 +00:00
|
|
|
else
|
2007-04-17 09:01:48 +00:00
|
|
|
m_transaction->sendHangup(m_reason);
|
2006-06-18 13:28:25 +00:00
|
|
|
m_transaction = 0;
|
|
|
|
}
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.unlock();
|
2006-06-18 13:28:25 +00:00
|
|
|
Message* m = message("chan.hangup",true);
|
|
|
|
m->setParam("status","hangup");
|
2007-04-17 09:01:48 +00:00
|
|
|
m->setParam("reason",m_reason);
|
2006-06-18 13:28:25 +00:00
|
|
|
Engine::enqueue(m);
|
2007-04-17 09:01:48 +00:00
|
|
|
Debug(this,DebugCall,"Hangup. Reason: %s [%p]",m_reason.safe(),this);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool YIAXConnection::route(bool authenticated)
|
|
|
|
{
|
2006-06-22 11:24:19 +00:00
|
|
|
if (!m_transaction)
|
|
|
|
return false;
|
2006-06-18 13:28:25 +00:00
|
|
|
Message* m = message("call.preroute",false,true);
|
2006-07-02 20:11:46 +00:00
|
|
|
Lock lock(&m_mutexTrans);
|
2006-06-18 13:28:25 +00:00
|
|
|
if (authenticated) {
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"Route pass 2: Password accepted [%p]",this);
|
2006-06-18 13:28:25 +00:00
|
|
|
m_refIncreased = false;
|
2006-06-22 11:24:19 +00:00
|
|
|
m->addParam("username",m_transaction->username());
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
2006-06-18 18:08:01 +00:00
|
|
|
else {
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"Route pass 1: No username [%p]",this);
|
2006-06-26 05:19:52 +00:00
|
|
|
if (!iplugin.getEngine()->acceptFormatAndCapability(m_transaction)) {
|
|
|
|
hangup(IAXTransaction::s_iax_modNoMediaFormat,true);
|
|
|
|
return false;
|
|
|
|
}
|
2006-09-25 14:03:14 +00:00
|
|
|
// Advertise the not yet authenticated username
|
2006-06-22 11:24:19 +00:00
|
|
|
if (m_transaction->username())
|
|
|
|
m->addParam("authname",m_transaction->username());
|
2006-09-25 14:03:14 +00:00
|
|
|
// Set 'formats' parameter
|
|
|
|
String formats;
|
2006-11-02 08:03:06 +00:00
|
|
|
iplugin.createFormatList(formats,m_transaction->capability());
|
2006-09-25 14:03:14 +00:00
|
|
|
m->addParam("formats",formats);
|
2006-06-18 18:08:01 +00:00
|
|
|
}
|
2006-06-22 11:24:19 +00:00
|
|
|
m->addParam("called",m_transaction->calledNo());
|
2006-09-25 14:03:14 +00:00
|
|
|
m->addParam("caller",m_transaction->callingNo());
|
2006-06-22 11:24:19 +00:00
|
|
|
m->addParam("callername",m_transaction->callingName());
|
2006-06-26 05:49:06 +00:00
|
|
|
m->addParam("ip_host",m_transaction->remoteAddr().host());
|
|
|
|
m->addParam("ip_port",String(m_transaction->remoteAddr().port()));
|
2006-06-18 13:28:25 +00:00
|
|
|
return startRouter(m);
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create audio source with the proper format
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXConnection::startAudioIn()
|
|
|
|
{
|
|
|
|
if (getSource())
|
|
|
|
return;
|
2006-06-26 05:19:52 +00:00
|
|
|
u_int32_t format = 0;
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.lock();
|
2006-06-26 05:19:52 +00:00
|
|
|
if (m_transaction)
|
|
|
|
format = m_transaction->formatIn();
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.unlock();
|
2006-11-02 08:03:06 +00:00
|
|
|
const char* formatText = lookup(format,dict_payloads);
|
2006-06-26 05:19:52 +00:00
|
|
|
setSource(new YIAXSource(this,format,formatText));
|
2006-06-18 13:28:25 +00:00
|
|
|
getSource()->deref();
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"startAudioIn. Format %u: '%s' [%p]",format,formatText,this);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Create audio consumer with the proper format
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXConnection::startAudioOut()
|
|
|
|
{
|
|
|
|
if (getConsumer())
|
|
|
|
return;
|
2006-06-26 05:19:52 +00:00
|
|
|
u_int32_t format = 0;
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.lock();
|
2006-06-26 05:19:52 +00:00
|
|
|
if (m_transaction)
|
|
|
|
format = m_transaction->formatOut();
|
2006-07-02 20:11:46 +00:00
|
|
|
m_mutexTrans.unlock();
|
2006-11-02 08:03:06 +00:00
|
|
|
const char* formatText = lookup(format,dict_payloads);
|
2006-06-26 05:19:52 +00:00
|
|
|
setConsumer(new YIAXConsumer(this,format,formatText));
|
2006-06-18 13:28:25 +00:00
|
|
|
getConsumer()->deref();
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"startAudioOut. Format %u: '%s' [%p]",format,formatText,this);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void YIAXConnection::evAuthRep(IAXEvent* event)
|
|
|
|
{
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"AUTHREP [%p]",this);
|
2006-10-14 10:51:52 +00:00
|
|
|
bool requestAuth, invalidAuth;
|
|
|
|
if (iplugin.userAuth(event->getTransaction(),true,requestAuth,invalidAuth)) {
|
|
|
|
// Authenticated. Route the user.
|
|
|
|
route(true);
|
2006-06-18 13:28:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-01-10 18:56:27 +00:00
|
|
|
const char* reason = IAXTransaction::s_iax_modInvalidAuth.c_str();
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugCall,"Not authenticated. Rejecting [%p]",this);
|
2006-10-14 10:51:52 +00:00
|
|
|
hangup(event,reason,true);
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:27 +00:00
|
|
|
void YIAXConnection::evAuthReq(IAXEvent* event)
|
|
|
|
{
|
2007-04-17 09:01:48 +00:00
|
|
|
DDebug(this,DebugAll,"AUTHREQ [%p]",this);
|
2007-01-10 18:56:27 +00:00
|
|
|
String response;
|
|
|
|
if (m_iaxEngine && m_transaction) {
|
|
|
|
m_iaxEngine->getMD5FromChallenge(response,m_transaction->challenge(),m_password);
|
|
|
|
m_transaction->sendAuthReply(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Get rid of the extra reference
|
2006-06-18 13:28:25 +00:00
|
|
|
void YIAXConnection::safeDeref()
|
|
|
|
{
|
|
|
|
m_mutexRefIncreased.lock();
|
|
|
|
bool bref = m_refIncreased;
|
|
|
|
m_refIncreased = false;
|
|
|
|
m_mutexRefIncreased.unlock();
|
|
|
|
if (bref)
|
|
|
|
deref();
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Keep an extra reference to prevent destroying the connection
|
2006-06-26 05:19:52 +00:00
|
|
|
bool YIAXConnection::safeRefIncrease()
|
2006-06-18 13:28:25 +00:00
|
|
|
{
|
2006-06-26 05:19:52 +00:00
|
|
|
bool ok = false;
|
|
|
|
m_mutexRefIncreased.lock();
|
|
|
|
if (!m_refIncreased && ref())
|
|
|
|
m_refIncreased = ok = true;
|
|
|
|
m_mutexRefIncreased.unlock();
|
|
|
|
return ok;
|
2006-06-18 13:28:25 +00:00
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// IAX URI constructor from components
|
2006-06-18 13:28:25 +00:00
|
|
|
IAXURI::IAXURI(const char* user, const char* host, const char* calledNo, const char* calledContext, int port)
|
2006-06-26 05:19:52 +00:00
|
|
|
: m_username(user),
|
|
|
|
m_host(host),
|
|
|
|
m_port(port),
|
|
|
|
m_calledNo(calledNo),
|
|
|
|
m_calledContext(calledContext),
|
|
|
|
m_parsed(true)
|
2006-06-18 13:28:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
*this << "iax:";
|
2006-06-26 05:19:52 +00:00
|
|
|
if (m_username)
|
2006-06-18 13:28:25 +00:00
|
|
|
*this << m_username << "@";
|
|
|
|
*this << m_host;
|
|
|
|
if (m_port)
|
|
|
|
*this << ":" << m_port;
|
2006-06-26 05:19:52 +00:00
|
|
|
if (m_calledNo) {
|
2006-06-18 13:28:25 +00:00
|
|
|
*this << "/" << m_calledNo;
|
2006-06-26 05:19:52 +00:00
|
|
|
if (m_calledContext)
|
2006-06-18 13:28:25 +00:00
|
|
|
*this << "@" << m_calledContext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IAXURI::parse()
|
|
|
|
{
|
2006-08-08 10:04:30 +00:00
|
|
|
/*
|
|
|
|
proto: user@ host :port /calledno @context
|
|
|
|
proto: user@ host :port /calledno ?context
|
|
|
|
*/
|
2006-06-18 13:28:25 +00:00
|
|
|
if (m_parsed)
|
|
|
|
return;
|
|
|
|
String tmp(*this), _port;
|
2010-06-17 11:38:46 +00:00
|
|
|
static const Regexp r("^\\([Ii][Aa][Xx]2\\+:\\)\\?\\([^[:space:][:cntrl:]@]\\+@\\)\\?\\([[:alnum:]._-]\\+\\)\\(:[0-9]\\+\\)\\?\\(/[[:alnum:]]*\\)\\?\\([@?][^@?:/]*\\)\\?$");
|
2006-06-18 13:28:25 +00:00
|
|
|
if (tmp.matches(r))
|
|
|
|
{
|
|
|
|
m_username = tmp.matchString(2);
|
|
|
|
m_username = m_username.substr(0,m_username.length() -1);
|
|
|
|
m_host = tmp.matchString(3).toLower();
|
|
|
|
_port = tmp.matchString(4);
|
|
|
|
m_port = _port.substr(1,_port.length()).toInteger();
|
|
|
|
m_calledNo = tmp.matchString(5);
|
|
|
|
m_calledNo = m_calledNo.substr(1,m_calledNo.length());
|
|
|
|
m_calledContext = tmp.matchString(6);
|
|
|
|
m_calledContext = m_calledContext.substr(1,m_calledContext.length());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_username = "";
|
|
|
|
m_host = "";
|
|
|
|
m_port = 0;
|
|
|
|
m_calledNo = "";
|
|
|
|
m_calledContext = "";
|
|
|
|
}
|
|
|
|
m_parsed = true;
|
|
|
|
}
|
|
|
|
|
2006-08-08 10:04:30 +00:00
|
|
|
// Pick URI parameters from a message or setting
|
2006-06-22 11:24:19 +00:00
|
|
|
bool IAXURI::fillList(NamedList& dest)
|
|
|
|
{
|
|
|
|
if (!m_parsed)
|
|
|
|
return false;
|
|
|
|
if (m_username.length())
|
|
|
|
dest.setParam("username",m_username);
|
|
|
|
if (m_calledNo.length())
|
|
|
|
dest.setParam("called",m_calledNo);
|
|
|
|
if (m_calledContext.length())
|
2006-09-12 16:54:29 +00:00
|
|
|
dest.setParam("iaxcontext",m_calledContext);
|
2006-06-22 11:24:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IAXURI::setAddr(SocketAddr& dest)
|
|
|
|
{
|
|
|
|
parse();
|
|
|
|
if (!m_host.length())
|
|
|
|
return false;
|
|
|
|
dest.host(m_host);
|
2006-09-25 14:03:14 +00:00
|
|
|
dest.port(m_port ? m_port : 0);
|
2006-06-22 11:24:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:28:25 +00:00
|
|
|
}; // anonymous namespace
|
|
|
|
|
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|