Commit 98f897e7 authored by Tristan Matthews's avatar Tristan Matthews
parents cc120af6 db7e48f0
......@@ -389,13 +389,13 @@ void ConfigurationManager::setAddressbookList(
std::map<std::string, std::string> ConfigurationManager::getHookSettings()
{
return Manager::instance().getHookSettings();
return Manager::instance().hookPreference.toMap();
}
void ConfigurationManager::setHookSettings(const std::map<std::string,
std::string>& settings)
{
Manager::instance().setHookSettings(settings);
Manager::instance().hookPreference = HookPreference(settings);
}
void ConfigurationManager::setAccountsOrder(const std::string& order)
......
......@@ -191,9 +191,7 @@ bool ManagerImpl::outgoingCall(const std::string& account_id,
std::string current_call_id(getCurrentCallId());
std::string prefix;
if (hookPreference.getNumberEnabled())
prefix = hookPreference.getNumberAddPrefix();
std::string prefix(hookPreference.getNumberAddPrefix());
std::string to_cleaned(NumberCleaner::clean(to, prefix));
......@@ -2748,30 +2746,6 @@ std::vector<std::string> ManagerImpl::getAddressbookList() const
return unserialize(addressbookPreference.getList());
}
std::map<std::string, std::string> ManagerImpl::getHookSettings() const
{
std::map<std::string, std::string> settings;
settings["URLHOOK_IAX2_ENABLED"] = hookPreference.getIax2Enabled() ? "true" : "false";
settings["PHONE_NUMBER_HOOK_ADD_PREFIX"] = hookPreference.getNumberAddPrefix();
settings["PHONE_NUMBER_HOOK_ENABLED"] = hookPreference.getNumberEnabled() ? "true" : "false";
settings["URLHOOK_SIP_ENABLED"] = hookPreference.getSipEnabled() ? "true" : "false";
settings["URLHOOK_COMMAND"] = hookPreference.getUrlCommand();
settings["URLHOOK_SIP_FIELD"] = hookPreference.getUrlSipField();
return settings;
}
void ManagerImpl::setHookSettings(const std::map<std::string, std::string>& settings)
{
hookPreference.setIax2Enabled(settings.find("URLHOOK_IAX2_ENABLED")->second == "true");
hookPreference.setNumberAddPrefix(settings.find("PHONE_NUMBER_HOOK_ADD_PREFIX")->second);
hookPreference.setNumberEnabled(settings.find("PHONE_NUMBER_HOOK_ENABLED")->second == "true");
hookPreference.setSipEnabled(settings.find("URLHOOK_SIP_ENABLED")->second == "true");
hookPreference.setUrlCommand(settings.find("URLHOOK_COMMAND")->second);
hookPreference.setUrlSipField(settings.find("URLHOOK_SIP_FIELD")->second);
}
void ManagerImpl::setIPToIPForCall(const std::string& callID, bool IPToIP)
{
if (not isIPToIP(callID)) // no IPToIP calls with the same ID
......
......@@ -691,17 +691,6 @@ class ManagerImpl {
*/
std::vector <std::string> getAddressbookList() const;
/**
* Hook configuration
*/
std::map<std::string, std::string> getHookSettings() const;
/**
* Hook configuration
*/
void setHookSettings(const std::map<std::string, std::string>& settings);
/**
* Get the audio manager
* @return int The audio manager
......
......@@ -33,6 +33,7 @@
#include "audio/pulseaudio/pulselayer.h"
#include "config/yamlemitter.h"
#include "config/yamlnode.h"
#include "hooks/urlhook.h"
#include <sstream>
#include "global.h"
#include <cassert>
......@@ -216,6 +217,28 @@ HookPreference::HookPreference() : iax2Enabled_(false)
, urlSipField_("X-sflphone-url")
{}
HookPreference::HookPreference(const std::map<std::string, std::string> &settings) :
iax2Enabled_(settings.find("URLHOOK_IAX2_ENABLED")->second == "true")
, numberAddPrefix_(settings.find("PHONE_NUMBER_HOOK_ADD_PREFIX")->second)
, numberEnabled_(settings.find("PHONE_NUMBER_HOOK_ENABLED")->second == "true")
, sipEnabled_(settings.find("URLHOOK_SIP_ENABLED")->second == "true")
, urlCommand_(settings.find("URLHOOK_COMMAND")->second)
, urlSipField_(settings.find("URLHOOK_SIP_FIELD")->second)
{}
std::map<std::string, std::string> HookPreference::toMap() const
{
std::map<std::string, std::string> settings;
settings["URLHOOK_IAX2_ENABLED"] = iax2Enabled_ ? "true" : "false";
settings["PHONE_NUMBER_HOOK_ADD_PREFIX"] = numberAddPrefix_;
settings["PHONE_NUMBER_HOOK_ENABLED"] = numberEnabled_ ? "true" : "false";
settings["URLHOOK_SIP_ENABLED"] = sipEnabled_ ? "true" : "false";
settings["URLHOOK_COMMAND"] = urlCommand_;
settings["URLHOOK_SIP_FIELD"] = urlSipField_;
return settings;
}
void HookPreference::serialize(Conf::YamlEmitter *emitter)
{
Conf::MappingNode preferencemap(NULL);
......@@ -252,6 +275,11 @@ void HookPreference::unserialize(const Conf::MappingNode *map)
map->getValue(urlSipFieldKey, &urlSipField_);
}
void HookPreference::run(const std::string &header)
{
UrlHook::runAction(urlCommand_, header);
}
AudioPreference::AudioPreference() :
audioApi_(PULSEAUDIO_API_STR)
, cardin_(atoi(ALSA_DFT_CARD)) // ALSA_DFT_CARD
......
......@@ -335,56 +335,23 @@ class AddressbookPreference : public Serializable {
class HookPreference : public Serializable {
public:
HookPreference();
HookPreference(const std::map<std::string, std::string> &settings);
virtual void serialize(Conf::YamlEmitter *emitter);
virtual void unserialize(const Conf::MappingNode *map);
bool getIax2Enabled() const {
return iax2Enabled_;
}
void setIax2Enabled(bool i) {
iax2Enabled_ = i;
}
std::string getNumberAddPrefix() const {
return numberAddPrefix_;
}
void setNumberAddPrefix(const std::string &n) {
numberAddPrefix_ = n;
}
bool getNumberEnabled() const {
return numberEnabled_;
}
void setNumberEnabled(bool n) {
numberEnabled_ = n;
if (numberEnabled_)
return numberAddPrefix_;
else
return "";
}
bool getSipEnabled() const {
return sipEnabled_;
}
void setSipEnabled(bool s) {
sipEnabled_ = s;
}
std::string getUrlCommand() const {
return urlCommand_;
}
void setUrlCommand(const std::string &u) {
urlCommand_ = u;
}
std::string getUrlSipField() const {
return urlSipField_;
}
void setUrlSipField(const std::string &u) {
urlSipField_ = u;
}
std::map<std::string, std::string> toMap() const;
bool getSipEnabled() const { return sipEnabled_; }
std::string getUrlSipField() const { return urlSipField_; }
void run(const std::string &header);
private:
bool iax2Enabled_;
......@@ -393,7 +360,6 @@ class HookPreference : public Serializable {
bool sipEnabled_;
std::string urlCommand_;
std::string urlSipField_;
};
class AudioPreference : public Serializable {
......
......@@ -48,7 +48,6 @@
#include "dbus/dbusmanager.h"
#include "dbus/callmanager.h"
#include "hooks/urlhook.h"
#include "im/instant_messaging.h"
#include "audio/audiolayer.h"
......@@ -309,7 +308,7 @@ pj_bool_t transaction_request_cb(pjsip_rx_data *rdata)
if (Manager::instance().hookPreference.getSipEnabled()) {
std::string header_value(fetchHeaderValue(rdata->msg_info.msg, Manager::instance().hookPreference.getUrlSipField()));
UrlHook::runAction(Manager::instance().hookPreference.getUrlCommand(), header_value);
Manager::instance().hookPreference.run(header_value);
}
SIPCall* call = new SIPCall(Manager::instance().getNewCallID(), Call::INCOMING, cp_);
......@@ -1240,6 +1239,31 @@ bool SIPVoIPLink::SIPNewIpToIpCall(const std::string& id, const std::string& to)
pj_bool_t stun_sock_on_status_cb(pj_stun_sock *stun_sock UNUSED, pj_stun_sock_op op UNUSED, pj_status_t status)
{
switch(op) {
case PJ_STUN_SOCK_DNS_OP:
DEBUG("UserAgent: Stun operation dns resolution");
break;
case PJ_STUN_SOCK_BINDING_OP:
DEBUG("UserAgent: Stun operation binding");
break;
case PJ_STUN_SOCK_KEEP_ALIVE_OP:
DEBUG("UserAgent: Stun operation keep alive");
break;
case PJ_STUN_SOCK_MAPPED_ADDR_CHANGE:
DEBUG("UserAgent: Stun operation address mapping change");
break;
default:
DEBUG("UserAgent: Stun unknown operation");
break;
}
if(status == PJ_SUCCESS) {
DEBUG("UserAgent: Stun operation success");
}
else {
ERROR("UserAgent: Stun operation failure");
}
return status == PJ_SUCCESS;
}
......@@ -1249,7 +1273,7 @@ pj_bool_t stun_sock_on_rx_data_cb(pj_stun_sock *stun_sock UNUSED, void *pkt UNUS
}
pj_status_t SIPVoIPLink::stunServerResolve(SIPAccount *account)
pj_status_t SIPVoIPLink::stunServerResolve(pj_str_t serverName, pj_uint16_t port)
{
pj_stun_config stunCfg;
pj_stun_config_init(&stunCfg, &cp_->factory, 0, pjsip_endpt_get_ioqueue(endpt_), pjsip_endpt_get_timer_heap(endpt_));
......@@ -1263,21 +1287,19 @@ pj_status_t SIPVoIPLink::stunServerResolve(SIPAccount *account)
pj_stun_sock *stun_sock;
pj_status_t status = pj_stun_sock_create(&stunCfg, "stunresolve", pj_AF_INET(), &stun_sock_cb, NULL, NULL, &stun_sock);
pj_str_t stunServer = account->getStunServerName();
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
ERROR("Error creating STUN socket for %.*s: %s", (int) stunServer.slen, stunServer.ptr, errmsg);
ERROR("UserAgent: Error creating STUN socket for %.*s: %s", (int) serverName.slen, serverName.ptr, errmsg);
return status;
}
status = pj_stun_sock_start(stun_sock, &stunServer, account->getStunPort(), NULL);
status = pj_stun_sock_start(stun_sock, &serverName, port, NULL);
if (status != PJ_SUCCESS) {
char errmsg[PJ_ERR_MSG_SIZE];
pj_strerror(status, errmsg, sizeof(errmsg));
DEBUG("Error starting STUN socket for %.*s: %s", (int) stunServer.slen, stunServer.ptr, errmsg);
DEBUG("UserAgent: Error starting STUN socket for %.*s: %s", (int) serverName.slen, serverName.ptr, errmsg);
pj_stun_sock_destroy(stun_sock);
}
......@@ -1354,13 +1376,13 @@ void SIPVoIPLink::createTlsTransport(SIPAccount *account, std::string remoteAddr
void SIPVoIPLink::createSipTransport(SIPAccount *account)
{
shutdownSipTransport(account);
if (account == NULL) {
ERROR("Account is NULL while creating sip transport");
ERROR("UserAgent: Account is NULL while creating sip transport");
return;
}
shutdownSipTransport(account);
if (account->isTlsEnabled()) {
std::string remoteSipUri(account->getServerUri());
static const char SIPS_PREFIX[] = "<sips:";
......@@ -1369,8 +1391,10 @@ void SIPVoIPLink::createSipTransport(SIPAccount *account)
std::string remoteAddr(remoteSipUri.substr(sips, trns-sips));
createTlsTransport(account, remoteAddr);
} else if (account->isStunEnabled())
createStunTransport(account);
} else if (account->isStunEnabled()) {
pjsip_transport *transport = createStunTransport(account->getStunServerName(), account->getStunPort());
account->transport_ = transport;
}
else {
pjsip_transport *transport = createUdpTransport(account->getLocalInterface(), account->getLocalPort());
account->transport_ = transport;
......@@ -1448,39 +1472,43 @@ pjsip_tpselector *SIPVoIPLink::initTransportSelector(pjsip_transport *transport,
return tp;
}
void SIPVoIPLink::createStunTransport(SIPAccount *account)
pjsip_transport *SIPVoIPLink::createStunTransport(pj_str_t serverName, pj_uint16_t port)
{
pj_str_t stunServer = account->getStunServerName();
pj_uint16_t stunPort = PJ_STUN_PORT; // account->getStunPort();
DEBUG("UserAgent: Create stun transport server name: %s, port: %d", account->getStunServerName(), stunPort);// account->getStunPort());
if (stunServerResolve(account) != PJ_SUCCESS) {
ERROR("Can't resolve STUN server");
return;
// pj_str_t stunServer = account->getStunServerName();
// pj_uint16_t stunPort = PJ_STUN_PORT; // account->getStunPort();
pjsip_transport *transport;
DEBUG("UserAgent: Create stun transport server name: %s, port: %d", serverName, port);// account->getStunPort());
if (stunServerResolve(serverName, port) != PJ_SUCCESS) {
ERROR("UserAgent: Can't resolve STUN server");
// Signal client
return NULL;
}
pj_sock_t sock = PJ_INVALID_SOCKET;
pj_sockaddr_in boundAddr;
if (pj_sockaddr_in_init(&boundAddr, &stunServer, 0) != PJ_SUCCESS) {
ERROR("Can't initialize IPv4 socket on %*s:%i", stunServer.slen, stunServer.ptr, stunPort);
return;
if (pj_sockaddr_in_init(&boundAddr, &serverName, 0) != PJ_SUCCESS) {
ERROR("UserAgent: Can't initialize IPv4 socket on %*s:%i", serverName.slen, serverName.ptr, port);
// Signal client
return NULL;
}
if (pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock) != PJ_SUCCESS) {
ERROR("Can't create or bind socket");
return;
ERROR("UserAgent: Can't create or bind socket");
// Signal client
return NULL;
}
// Query the mapped IP address and port on the 'outside' of the NAT
pj_sockaddr_in pub_addr;
if (pjstun_get_mapped_addr(&cp_->factory, 1, &sock, &stunServer, stunPort, &stunServer, stunPort, &pub_addr) != PJ_SUCCESS) {
ERROR("Can't contact STUN server");
if (pjstun_get_mapped_addr(&cp_->factory, 1, &sock, &serverName, port, &serverName, port, &pub_addr) != PJ_SUCCESS) {
ERROR("UserAgent: Can't contact STUN server");
pj_sock_close(sock);
return;
// signal client
return NULL;
}
pjsip_host_port a_name = {
......@@ -1488,15 +1516,17 @@ void SIPVoIPLink::createStunTransport(SIPAccount *account)
pj_ntohs(pub_addr.sin_port)
};
std::string listeningAddress = std::string(a_name.host.ptr, a_name.host.slen);
// std::string listeningAddress = std::string(a_name.host.ptr, a_name.host.slen);
account->setPublishedAddress(listeningAddress);
account->setPublishedPort(a_name.port);
// account->setPublishedAddress(listeningAddress);
// account->setPublishedPort(a_name.port);
pjsip_udp_transport_attach2(endpt_, PJSIP_TRANSPORT_UDP, sock, &a_name, 1,
&account->transport_);
&transport);
pjsip_tpmgr_dump_transports(pjsip_endpt_get_tpmgr(endpt_));
return transport;
}
void SIPVoIPLink::shutdownSipTransport(SIPAccount *account)
......@@ -1544,7 +1574,6 @@ void SIPVoIPLink::findLocalAddressFromTransport(pjsip_transport *transport, pjsi
int i_port = 0;
// Find the local address and port for this transport
DEBUG("transportType: %d\n", transportType);
if (pjsip_tpmgr_find_local_addr(tpmgr, pool_, transportType, tp_sel, &localAddress, &i_port) != PJ_SUCCESS) {
WARN("SIPVoIPLink: Could not retreive local address and port from transport, using %s:%s", addr.c_str(), port.c_str());
return;
......@@ -2124,14 +2153,14 @@ std::string fetchHeaderValue(pjsip_msg *msg, const std::string &field)
if (!hdr)
return "";
std::string value(std::string(hdr->hvalue.ptr, hdr->hvalue.slen));
std::string value(hdr->hvalue.ptr, hdr->hvalue.slen);
size_t pos = value.find("\n");
if (pos == std::string::npos)
if (pos != std::string::npos)
return value.substr(0, pos);
else
return "";
return value.substr(0, pos);
}
} // end anonymous namespace
......
......@@ -301,7 +301,7 @@ class SIPVoIPLink : public VoIPLink {
/**
* Resolve public address for this account
*/
pj_status_t stunServerResolve(SIPAccount *);
pj_status_t stunServerResolve(pj_str_t serverName, pj_uint16_t port);
/**
* Create the default TLS listener.
......@@ -331,7 +331,7 @@ class SIPVoIPLink : public VoIPLink {
* Create a UDP transport using stun server to resove public address
* @param account The account for which a transport must be created.
*/
void createStunTransport(SIPAccount *account);
pjsip_transport *createStunTransport(pj_str_t serverName, pj_uint16_t port);
/**
* UDP Transports are stored in this map in order to retreive them in case
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment