From 1a97ed578777b9ff19a41db5533498850f7dba35 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Mon, 27 Aug 2012 10:44:54 +0200 Subject: [PATCH] Add port command. Fixes bug 0000187. --- daemon/Makefile.am | 2 + daemon/commands/port.cc | 136 ++++++++++++++++++++++++++++++++++++++++ daemon/commands/port.h | 17 +++++ daemon/daemon.cc | 2 + 4 files changed, 157 insertions(+) create mode 100644 daemon/commands/port.cc create mode 100644 daemon/commands/port.h diff --git a/daemon/Makefile.am b/daemon/Makefile.am index e1a114e7a..d483e9a59 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -20,6 +20,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/media-encryption.cc \ commands/msfilter-add-fmtp.cc \ commands/pop-event.cc \ + commands/port.cc \ commands/ptime.cc \ commands/register.cc \ commands/register-status.cc \ @@ -44,6 +45,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/media-encryption.h \ commands/msfilter-add-fmtp.h \ commands/pop-event.h \ + commands/port.h \ commands/ptime.h \ commands/register.h \ commands/register-status.h \ diff --git a/daemon/commands/port.cc b/daemon/commands/port.cc new file mode 100644 index 000000000..bd708aa84 --- /dev/null +++ b/daemon/commands/port.cc @@ -0,0 +1,136 @@ +#include "port.h" + +using namespace std; + +enum PortType { + SIPPort, + AudioRTPPort, + VideoRTPPort +}; + +enum Protocol { + UDPProtocol, + TCPProtocol, + TLSProtocol +}; + +class PortCommandPrivate { +public: + void outputPort(Daemon *app, ostringstream &ost, PortType type); + void outputPorts(Daemon *app, ostringstream &ost); +}; + +void PortCommandPrivate::outputPort(Daemon* app, ostringstream& ost, PortType type) { + switch (type) { + case SIPPort: + LCSipTransports transports; + linphone_core_get_sip_transports(app->getCore(), &transports); + ost << "SIP: "; + if (transports.udp_port > 0) { + ost << transports.udp_port << " UDP\n"; + } else if (transports.tcp_port > 0) { + ost << transports.tcp_port << " TCP\n"; + } else { + ost << transports.tls_port << " TLS\n"; + } + break; + case AudioRTPPort: + ost << "Audio RTP: " << linphone_core_get_audio_port(app->getCore()) << "\n"; + break; + case VideoRTPPort: + ost << "Video RTP: " << linphone_core_get_video_port(app->getCore()) << "\n"; + break; + } +} + +void PortCommandPrivate::outputPorts(Daemon* app, ostringstream& ost) { + outputPort(app, ost, SIPPort); + outputPort(app, ost, AudioRTPPort); + outputPort(app, ost, VideoRTPPort); +} + +PortCommand::PortCommand() : + DaemonCommand("port", "port [] [] []", + "Set the port to use for type if port is set, otherwise return the port used for type if specified or all the used ports if no type is specified.\n" + " must be one of these values: sip, audio, video.\n" + " should be defined only for sip port and have one of these values: udp, tcp, tls."), + d(new PortCommandPrivate()) { +} + +PortCommand::~PortCommand() { + delete d; +} + +void PortCommand::exec(Daemon *app, const char *args) { + string type; + int port; + istringstream ist(args); + ostringstream ost; + ist >> type; + if (ist.eof() && (type.length() == 0)) { + d->outputPorts(app, ost); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (ist.fail()) { + app->sendResponse(Response("Incorrect type parameter.", Response::Error)); + } else { + ist >> port; + if (ist.fail()) { + if (type.compare("sip") == 0) { + d->outputPort(app, ost, SIPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (type.compare("audio") == 0) { + d->outputPort(app, ost, AudioRTPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (type.compare("video") == 0) { + d->outputPort(app, ost, VideoRTPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else { + app->sendResponse(Response("Incorrect type parameter.", Response::Error)); + } + } else { + if (type.compare("sip") == 0) { + Protocol protocol = UDPProtocol; + string protocol_str; + ist >> protocol_str; + if (!ist.fail()) { + if (protocol_str.compare("udp") == 0) { + protocol = UDPProtocol; + } else if (protocol_str.compare("tcp") == 0) { + protocol = TCPProtocol; + } else if (protocol_str.compare("tls") == 0) { + protocol = TLSProtocol; + } else { + app->sendResponse(Response("Incorrect protocol parameter.", Response::Error)); + return; + } + } + LCSipTransports transports; + memset(&transports, 0, sizeof(transports)); + switch (protocol) { + case UDPProtocol: + transports.udp_port = port; + break; + case TCPProtocol: + transports.tcp_port = port; + break; + case TLSProtocol: + transports.tls_port = port; + break; + } + linphone_core_set_sip_transports(app->getCore(), &transports); + d->outputPort(app, ost, SIPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (type.compare("audio") == 0) { + linphone_core_set_audio_port(app->getCore(), port); + d->outputPort(app, ost, AudioRTPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (type.compare("video") == 0) { + linphone_core_set_video_port(app->getCore(), port); + d->outputPort(app, ost, VideoRTPPort); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else { + app->sendResponse(Response("Incorrect type parameter.", Response::Error)); + } + } + } +} diff --git a/daemon/commands/port.h b/daemon/commands/port.h new file mode 100644 index 000000000..59679e63d --- /dev/null +++ b/daemon/commands/port.h @@ -0,0 +1,17 @@ +#ifndef COMMAND_PORT_H_ +#define COMMAND_PORT_H_ + +#include "../daemon.h" + +class PortCommandPrivate; + +class PortCommand: public DaemonCommand { +public: + PortCommand(); + ~PortCommand(); + virtual void exec(Daemon *app, const char *args); +private: + PortCommandPrivate *d; +}; + +#endif //COMMAND_PORT_H_ diff --git a/daemon/daemon.cc b/daemon/daemon.cc index 974b28a5a..217349c3a 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -27,6 +27,7 @@ #include "commands/media-encryption.h" #include "commands/msfilter-add-fmtp.h" #include "commands/pop-event.h" +#include "commands/port.h" #include "commands/ptime.h" #include "commands/register.h" #include "commands/register-status.h" @@ -306,6 +307,7 @@ void Daemon::initCommands() { mCommands.push_back(new IPv6Command()); mCommands.push_back(new FirewallPolicyCommand()); mCommands.push_back(new MediaEncryptionCommand()); + mCommands.push_back(new PortCommand()); mCommands.push_back(new QuitCommand()); mCommands.push_back(new HelpCommand());