20 #include "iptableentry.h" 21 #include "gateway/gateway.h" 28 for (
auto rule : m_rules) {
29 if (rule.protocols.size()) {
31 for (
auto proto : rule.protocols) {
33 std::cerr <<
"Couldn't apply the rule " << rule.target << std::endl;
39 std::cerr <<
"Couldn't apply the rule " << rule.target << std::endl;
45 std::cerr <<
"Unable to set policy " << convertTarget(m_defaultTarget)
46 <<
" for " << m_type << std::endl;
53 std::string IPTableEntry::convertTarget (
Target& t)
63 case Target::INVALID_TARGET:
71 std::string iptableCommand =
"iptables -A " + m_type;
73 if (!rule.host.empty()) {
74 if (rule.host !=
"*") {
75 if (
"INPUT" == m_type) {
76 iptableCommand = iptableCommand +
" -s ";
78 iptableCommand = iptableCommand +
" -d ";
80 iptableCommand = iptableCommand + rule.host ;
85 if (rule.ports.multiport) {
86 iptableCommand = iptableCommand +
" -p " + protocol +
" --match multiport ";
88 if (
"INPUT" == m_type) {
89 iptableCommand = iptableCommand +
"--sports " + rule.ports.ports;
91 iptableCommand = iptableCommand +
"--dports " + rule.ports.ports;
94 iptableCommand = iptableCommand +
" -p " + protocol +
" ";
95 if (
"INPUT" == m_type) {
96 iptableCommand = iptableCommand +
"--sport " + rule.ports.ports;
98 iptableCommand = iptableCommand +
"--dport " + rule.ports.ports;
102 iptableCommand = iptableCommand +
" -p " + protocol;
105 iptableCommand = iptableCommand +
" -j " + convertTarget(rule.target);
107 return iptableCommand;
112 std::string iptableCommand =
"iptables -A " + m_type;
114 if (!rule.host.empty()) {
115 if (rule.host !=
"*") {
116 if (
"INPUT" == m_type) {
117 iptableCommand = iptableCommand +
" -s ";
119 iptableCommand = iptableCommand +
" -d ";
121 iptableCommand = iptableCommand + rule.host ;
125 if (rule.ports.any) {
126 if (rule.ports.multiport) {
127 iptableCommand = iptableCommand +
" -p all --match multiport ";
129 if (
"INPUT" == m_type) {
130 iptableCommand = iptableCommand +
"--sports " + rule.ports.ports;
132 iptableCommand = iptableCommand +
"--dports " + rule.ports.ports;
135 iptableCommand = iptableCommand +
" -p tcp ";
136 if (
"INPUT" == m_type) {
137 iptableCommand = iptableCommand +
"--sport " + rule.ports.ports;
139 iptableCommand = iptableCommand +
"--dport " + rule.ports.ports;
144 iptableCommand = iptableCommand +
" -j " + convertTarget(rule.target);
146 return iptableCommand;
151 if (m_defaultTarget != Target::ACCEPT && m_defaultTarget != Target::DROP) {
152 std::cerr <<
"Wrong default target : " << convertTarget(m_defaultTarget) << std::endl;
155 std::string iptableCommand =
"iptables -P " + m_type +
" " + convertTarget(m_defaultTarget);
157 return iptableCommand;
161 bool IPTableEntry::insertCommand(std::string command)
163 std::cout <<
"Add network rule : " << command << std::endl;
166 Glib::spawn_command_line_sync(command);
168 }
catch (Glib::SpawnError e) {
169 std::cerr <<
"Failed to spawn " << command <<
": code " << e.code()
170 <<
" msg: " << e.what() << std::endl;
173 }
catch (Glib::ShellError e) {
174 std::cerr <<
"Failed to call " << command <<
": code " << e.code()
175 <<
" msg: " << e.what() << std::endl;
184 return "IPTableEntry type: " + m_type;
Definition of a 'Rule' used to handle network traffic.
std::string interpretRuleWithProtocol(Rule rule, const std::string &protocol)
Interprets a rule with protocol information to iptables applicable string.
std::string interpretRule(Rule rule)
Interprets a rule to iptables applicable string.
bool applyRules()
Applies all rules to iptables.
std::string interpretPolicy(void)
This function Interprets defaultTarget rule to iptables applicable policy string. ...
std::string toString()
Creates a string with information about the entry.
Developers guide to adding a config item: