Add firewall-policy command.

Fixes bugs 0000181 and 0000182.
This commit is contained in:
Ghislain MARY 2012-08-24 15:13:23 +02:00
parent 445bd2c495
commit bad9235398
4 changed files with 113 additions and 0 deletions

View file

@ -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 \

View file

@ -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 <type> [<address>]",
"Set the firewall policy if type is set, otherwise return the used firewall policy.\n"
"<type> must be one of these values: none, nat, stun, ice.\n"
"<address> 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));
}
}

View file

@ -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_

View file

@ -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());