From ab122cc3b3cce0cf861dfddbea324455735a1b60 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Mon, 27 Aug 2012 09:30:48 +0200 Subject: [PATCH] Add media-encryption command. Fixes bug 0000186. --- daemon/Makefile.am | 2 + daemon/commands/media-encryption.cc | 63 +++++++++++++++++++++++++++++ daemon/commands/media-encryption.h | 17 ++++++++ daemon/daemon.cc | 2 + 4 files changed, 84 insertions(+) create mode 100644 daemon/commands/media-encryption.cc create mode 100644 daemon/commands/media-encryption.h diff --git a/daemon/Makefile.am b/daemon/Makefile.am index b824ccaed..e1a114e7a 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -17,6 +17,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/firewall-policy.cc \ commands/help.cc \ commands/ipv6.cc \ + commands/media-encryption.cc \ commands/msfilter-add-fmtp.cc \ commands/pop-event.cc \ commands/ptime.cc \ @@ -40,6 +41,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/firewall-policy.h \ commands/help.h \ commands/ipv6.h \ + commands/media-encryption.h \ commands/msfilter-add-fmtp.h \ commands/pop-event.h \ commands/ptime.h \ diff --git a/daemon/commands/media-encryption.cc b/daemon/commands/media-encryption.cc new file mode 100644 index 000000000..c28b3d7c8 --- /dev/null +++ b/daemon/commands/media-encryption.cc @@ -0,0 +1,63 @@ +#include "media-encryption.h" + +using namespace std; + +class MediaEncryptionCommandPrivate { +public: + void outputMediaEncryption(Daemon *app, ostringstream &ost); +}; + +void MediaEncryptionCommandPrivate::outputMediaEncryption(Daemon* app, ostringstream& ost) { + LinphoneMediaEncryption encryption = linphone_core_get_media_encryption(app->getCore()); + ost << "Encryption: "; + switch (encryption) { + case LinphoneMediaEncryptionNone: + ost << "none\n"; + break; + case LinphoneMediaEncryptionSRTP: + ost << "srtp\n"; + break; + case LinphoneMediaEncryptionZRTP: + ost << "zrtp\n"; + break; + } +} + +MediaEncryptionCommand::MediaEncryptionCommand() : + DaemonCommand("media-encryption", "media-encryption [none|srtp|zrtp]", + "Set the media encryption policy if a parameter is given, otherwise return the media encrytion in use."), + d(new MediaEncryptionCommandPrivate()) { +} + +MediaEncryptionCommand::~MediaEncryptionCommand() { + delete d; +} + +void MediaEncryptionCommand::exec(Daemon *app, const char *args) { + string encryption_str; + istringstream ist(args); + ist >> encryption_str; + if (ist.eof() && (encryption_str.length() == 0)) { + ostringstream ost; + d->outputMediaEncryption(app, ost); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (ist.fail()) { + app->sendResponse(Response("Incorrect parameter.", Response::Error)); + } else { + LinphoneMediaEncryption encryption; + if (encryption_str.compare("none") == 0) { + encryption = LinphoneMediaEncryptionNone; + } else if (encryption_str.compare("srtp") == 0) { + encryption = LinphoneMediaEncryptionSRTP; + } else if (encryption_str.compare("zrtp") == 0) { + encryption = LinphoneMediaEncryptionZRTP; + } else { + app->sendResponse(Response("Incorrect parameter.", Response::Error)); + return; + } + linphone_core_set_media_encryption(app->getCore(), encryption); + ostringstream ost; + d->outputMediaEncryption(app, ost); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } +} diff --git a/daemon/commands/media-encryption.h b/daemon/commands/media-encryption.h new file mode 100644 index 000000000..8e519a351 --- /dev/null +++ b/daemon/commands/media-encryption.h @@ -0,0 +1,17 @@ +#ifndef COMMAND_MEDIA_ENCRYPTION_H_ +#define COMMAND_MEDIA_ENCRYPTION_H_ + +#include "../daemon.h" + +class MediaEncryptionCommandPrivate; + +class MediaEncryptionCommand: public DaemonCommand { +public: + MediaEncryptionCommand(); + ~MediaEncryptionCommand(); + virtual void exec(Daemon *app, const char *args); +private: + MediaEncryptionCommandPrivate *d; +}; + +#endif //COMMAND_MEDIA_ENCRYPTION_H_ diff --git a/daemon/daemon.cc b/daemon/daemon.cc index f5230bf48..974b28a5a 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -24,6 +24,7 @@ #include "commands/firewall-policy.h" #include "commands/help.h" #include "commands/ipv6.h" +#include "commands/media-encryption.h" #include "commands/msfilter-add-fmtp.h" #include "commands/pop-event.h" #include "commands/ptime.h" @@ -304,6 +305,7 @@ void Daemon::initCommands() { mCommands.push_back(new PtimeCommand()); mCommands.push_back(new IPv6Command()); mCommands.push_back(new FirewallPolicyCommand()); + mCommands.push_back(new MediaEncryptionCommand()); mCommands.push_back(new QuitCommand()); mCommands.push_back(new HelpCommand());