19 #include "networkgatewayparser.h" 28 log_error() <<
"No type specified in network config.";
32 e.m_defaultTarget = IPTableEntry::Target::DROP;
34 if (
"INCOMING" == chain) {
36 }
else if (
"OUTGOING" == chain) {
39 log_error() << e.m_type <<
" is not a valid type ('INCOMING' or 'OUTGOING')";
43 const json_t *rules = json_object_get(element,
"allow");
45 if (rules ==
nullptr) {
46 log_error() <<
"No rules specified";
50 if (!json_is_array(rules)) {
51 log_error() <<
"Rules not specified as an array";
57 json_array_foreach(rules, ix, val) {
58 if (json_is_object(val)) {
59 if (!parseRule(val, e.m_rules)) {
60 log_error() <<
"Could not parse rule config";
64 log_error() <<
"formatting of rules array is incorrect.";
73 bool NetworkGatewayParser::parseRule(
const json_t *element,
74 std::vector<IPTableEntry::Rule> &rules)
78 r.target = IPTableEntry::Target::ACCEPT;
81 log_error() <<
"Host not specified in the network config.";
86 json_t *ports = json_object_get(element,
"ports");
87 if (ports !=
nullptr) {
88 parsePort(ports, r.ports);
92 json_t *protocols = json_object_get(element,
"protocols");
93 if (protocols !=
nullptr) {
94 parseProtocol(protocols, r.protocols);
104 if (json_is_integer(element)) {
105 auto port = json_integer_value(element);
107 ports.multiport =
false;
108 ports.ports = std::to_string(port);
110 }
else if (json_is_string(element)) {
111 auto portRange = json_string_value(element);
113 ports.multiport =
true;
114 ports.ports = portRange;
116 }
else if (json_is_array(element)) {
119 std::string portList =
"";
121 json_array_foreach(element, ix, val) {
122 if (!json_is_integer(val)) {
123 log_error() <<
"Entry in port array is not an integer.";
127 int port = json_integer_value(val);
128 portList = portList + std::to_string(port) +
",";
132 ports.multiport =
true;
133 ports.ports = portList;
135 log_error() <<
"Rules specified in an invalid format";
141 bool NetworkGatewayParser::isProtocolValid(std::string protocol) {
142 if ((protocol ==
"tcp") || (protocol ==
"udp") || (protocol ==
"icmp")) {
145 log_error() << protocol
146 <<
" is not valid value. Only tcp, udp and icmp protocols are allowed";
151 bool NetworkGatewayParser::parseProtocol(
const json_t *element,
152 std::vector<std::string> &proto)
155 if (json_is_string(element)) {
156 std::string protocol = json_string_value(element);
157 if (!isProtocolValid(protocol)) {
160 proto.push_back(protocol);
162 }
else if (json_is_array(element)) {
166 json_array_foreach(element, ix, val) {
167 if (!json_is_string(val)) {
168 log_error() <<
"Listed protocol is not valid";
172 std::string protocol = json_string_value(val);
174 if (!isProtocolValid(protocol)) {
178 proto.push_back(protocol);
181 log_error() <<
"Protocols specified in an invalid format";
Definition of a 'Rule' used to handle network traffic.
bool parseNetworkGatewayConfiguration(const json_t *element, IPTableEntry &e)
Parses NetworkGateway configuration into IPTableEntry.
container for port filtering options.
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.
A rules entry for the treatment of packets.