diff --git a/daemon/Makefile.am b/daemon/Makefile.am index 30c417681..1fe661506 100644 --- a/daemon/Makefile.am +++ b/daemon/Makefile.am @@ -14,6 +14,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/call.cc \ commands/call-stats.cc \ commands/call-status.cc \ + commands/firewall-policy.cc \ commands/help.cc \ commands/msfilter-add-fmtp.cc \ commands/pop-event.cc \ @@ -35,6 +36,7 @@ linphone_daemon_SOURCES=daemon.cc \ commands/call.h \ commands/call-stats.h \ commands/call-status.h \ + commands/firewall-policy.h \ commands/help.h \ commands/msfilter-add-fmtp.h \ commands/pop-event.h \ diff --git a/daemon/commands/firewall-policy.cc b/daemon/commands/firewall-policy.cc new file mode 100644 index 000000000..e7c374733 --- /dev/null +++ b/daemon/commands/firewall-policy.cc @@ -0,0 +1,92 @@ +#include "firewall-policy.h" + +using namespace std; + +class FirewallPolicyCommandPrivate { +public: + void outputFirewallPolicy(Daemon *app, ostringstream &ost); +}; + +void FirewallPolicyCommandPrivate::outputFirewallPolicy(Daemon* app, ostringstream& ost) { + LinphoneFirewallPolicy policy = linphone_core_get_firewall_policy(app->getCore()); + ost << "Type: "; + switch (policy) { + case LinphonePolicyNoFirewall: + ost << "none\n"; + break; + case LinphonePolicyUseNatAddress: + ost << "nat\n"; + ost << "Address: " << linphone_core_get_nat_address(app->getCore()) << "\n"; + break; + case LinphonePolicyUseStun: + ost << "stun\n"; + ost << "Address: " << linphone_core_get_stun_server(app->getCore()) << "\n"; + break; + case LinphonePolicyUseIce: + ost << "ice\n"; + ost << "Address: " << linphone_core_get_stun_server(app->getCore()) << "\n"; + break; + } +} + +FirewallPolicyCommand::FirewallPolicyCommand() : + DaemonCommand("firewall-policy", "firewall-policy [
]", + "Set the firewall policy if type is set, otherwise return the used firewall policy.\n" + " must be one of these values: none, nat, stun, ice.\n" + "
must be specified for the 'nat' and 'stun' types. " + "It represents the public address of the gateway for the 'nat' type and the STUN server address for the 'stun' and 'ice' types."), + d(new FirewallPolicyCommandPrivate()) { +} + +FirewallPolicyCommand::~FirewallPolicyCommand() { + delete d; +} + +void FirewallPolicyCommand::exec(Daemon *app, const char *args) { + string type; + string address; + istringstream ist(args); + ist >> type; + if (ist.eof() && (type.length() == 0)) { + ostringstream ost; + d->outputFirewallPolicy(app, ost); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } else if (ist.fail()) { + app->sendResponse(Response("Incorrect type parameter.", Response::Error)); + } else { + bool get_address; + LinphoneFirewallPolicy policy; + if (type.compare("none") == 0) { + policy = LinphonePolicyNoFirewall; + get_address = false; + } else if (type.compare("nat") == 0) { + policy = LinphonePolicyUseNatAddress; + get_address = true; + } else if (type.compare("stun") == 0) { + policy = LinphonePolicyUseStun; + get_address = true; + } else if (type.compare("ice") == 0) { + policy = LinphonePolicyUseIce; + get_address = true; + } else { + app->sendResponse(Response("Incorrect type parameter.", Response::Error)); + return; + } + if (get_address) { + ist >> address; + if (ist.fail()) { + app->sendResponse(Response("Missing/Incorrect address parameter.", Response::Error)); + return; + } + } + linphone_core_set_firewall_policy(app->getCore(), policy); + if (policy == LinphonePolicyUseNatAddress) { + linphone_core_set_nat_address(app->getCore(), address.c_str()); + } else if ((policy == LinphonePolicyUseStun) || (policy == LinphonePolicyUseIce)) { + linphone_core_set_stun_server(app->getCore(), address.c_str()); + } + ostringstream ost; + d->outputFirewallPolicy(app, ost); + app->sendResponse(Response(ost.str().c_str(), Response::Ok)); + } +} diff --git a/daemon/commands/firewall-policy.h b/daemon/commands/firewall-policy.h new file mode 100644 index 000000000..ac927c1b1 --- /dev/null +++ b/daemon/commands/firewall-policy.h @@ -0,0 +1,17 @@ +#ifndef COMMAND_FIREWALL_POLICY_H_ +#define COMMAND_FIREWALL_POLICY_H_ + +#include "../daemon.h" + +class FirewallPolicyCommandPrivate; + +class FirewallPolicyCommand: public DaemonCommand { +public: + FirewallPolicyCommand(); + ~FirewallPolicyCommand(); + virtual void exec(Daemon *app, const char *args); +private: + FirewallPolicyCommandPrivate *d; +}; + +#endif //COMMAND_FIREWALL_POLICY_H_ diff --git a/daemon/daemon.cc b/daemon/daemon.cc index cfdc3a114..a6ebf8971 100644 --- a/daemon/daemon.cc +++ b/daemon/daemon.cc @@ -21,6 +21,7 @@ #include "commands/call.h" #include "commands/call-stats.h" #include "commands/call-status.h" +#include "commands/firewall-policy.h" #include "commands/help.h" #include "commands/msfilter-add-fmtp.h" #include "commands/pop-event.h" @@ -300,6 +301,7 @@ void Daemon::initCommands() { mCommands.push_back(new AudioStreamStopCommand()); mCommands.push_back(new MSFilterAddFmtpCommand()); mCommands.push_back(new PtimeCommand()); + mCommands.push_back(new FirewallPolicyCommand()); mCommands.push_back(new QuitCommand()); mCommands.push_back(new HelpCommand());