|
| 1 | +/* |
| 2 | + * Copyright 2018 The Polycube Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#include "Filters.h" |
| 19 | +#include "Packetcapture.h" |
| 20 | +#include "Packetcapture_dp_ingress.h" |
| 21 | +#include "Packetcapture_dp_egress.h" |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +Filters::Filters(Packetcapture &parent, const FiltersJsonObject &conf) |
| 26 | + : FiltersBase(parent), set_dstIp(false), set_srcIp(false), set_dstPort(false), set_srcPort(false), set_l4proto(false) { |
| 27 | + |
| 28 | + if (conf.snaplenIsSet()) { |
| 29 | + setSnaplen(conf.getSnaplen()); |
| 30 | + } |
| 31 | + |
| 32 | + if (conf.srcIsSet()) { |
| 33 | + setSrc(conf.getSrc()); |
| 34 | + } |
| 35 | + |
| 36 | + if (conf.dstIsSet()) { |
| 37 | + setDst(conf.getDst()); |
| 38 | + } |
| 39 | + |
| 40 | + if (conf.l4protoIsSet()) { |
| 41 | + setL4proto(conf.getL4proto()); |
| 42 | + } |
| 43 | + |
| 44 | + if (conf.sportIsSet()) { |
| 45 | + setSport(conf.getSport()); |
| 46 | + } |
| 47 | + |
| 48 | + if (conf.dportIsSet()) { |
| 49 | + setDport(conf.getDport()); |
| 50 | + } |
| 51 | + bootstrap = false; |
| 52 | + snaplen = 262144; |
| 53 | +} |
| 54 | + |
| 55 | +Filters::~Filters() {} |
| 56 | + |
| 57 | +uint32_t Filters::getSnaplen() { |
| 58 | + return snaplen; |
| 59 | +} |
| 60 | + |
| 61 | +void Filters::setSnaplen(const uint32_t &value) { |
| 62 | + snaplen = value; |
| 63 | + set_snaplen = true; |
| 64 | + if(!bootstrap) |
| 65 | + parent_.updateFiltersMaps(); |
| 66 | +} |
| 67 | + |
| 68 | +std::string Filters::getSrc() { |
| 69 | + return srcIp; |
| 70 | +} |
| 71 | + |
| 72 | +void Filters::setSrc(const std::string &value) { |
| 73 | + srcIp = value; |
| 74 | + |
| 75 | + uint32_t ip_src_filter = 0; |
| 76 | + netmaskSrc = (0xFFFFFFFF << (32 - std::stoi(value.substr(value.find("/")+1)))) & 0xFFFFFFFF; |
| 77 | + std::string source_ip = value.substr(0, value.find("/")); |
| 78 | + inet_pton(AF_INET, source_ip.data(), &ip_src_filter); |
| 79 | + ip_src_filter = ntohl(ip_src_filter); |
| 80 | + networkSrc = ip_src_filter & netmaskSrc; |
| 81 | + //network_packet = pkt_values.srcIp & netmask_filter; //TODO: do it in the fast path |
| 82 | + |
| 83 | + set_srcIp = true; |
| 84 | + if(!bootstrap) |
| 85 | + parent_.updateFiltersMaps(); |
| 86 | +} |
| 87 | + |
| 88 | +std::string Filters::getDst() { |
| 89 | + return dstIp; |
| 90 | +} |
| 91 | + |
| 92 | +void Filters::setDst(const std::string &value) { |
| 93 | + dstIp = value; |
| 94 | + |
| 95 | + uint32_t ip_dst_filter = 0; |
| 96 | + netmaskDst = (0xFFFFFFFF << (32 - std::stoi(value.substr(value.find("/")+1)))) & 0xFFFFFFFF; |
| 97 | + std::string source_ip = value.substr(0, value.find("/")); |
| 98 | + inet_pton(AF_INET, source_ip.data(), &ip_dst_filter); |
| 99 | + ip_dst_filter = ntohl(ip_dst_filter); |
| 100 | + networkDst = ip_dst_filter & netmaskDst; |
| 101 | + //network_packet = pkt_values.dstIp & netmask_filter; //TODO: do it in the fast path |
| 102 | + |
| 103 | + set_dstIp = true; |
| 104 | + if(!bootstrap) |
| 105 | + parent_.updateFiltersMaps(); |
| 106 | +} |
| 107 | + |
| 108 | +std::string Filters::getL4proto() { |
| 109 | + return l4proto; |
| 110 | +} |
| 111 | + |
| 112 | +void Filters::setL4proto(const std::string &value) { |
| 113 | + if((value.compare(std::string("tcp")) == 0) || (value.compare(std::string("udp")) == 0)){ |
| 114 | + l4proto = value; |
| 115 | + set_l4proto = true; |
| 116 | + if(!bootstrap) |
| 117 | + parent_.updateFiltersMaps(); |
| 118 | + }else |
| 119 | + throw std::runtime_error("Bad value at setL4proto. Please enter 'tcp' or 'udp'"); |
| 120 | +} |
| 121 | + |
| 122 | +uint16_t Filters::getSport() { |
| 123 | + return this->srcPort; |
| 124 | +} |
| 125 | + |
| 126 | +void Filters::setSport(const uint16_t &value) { |
| 127 | + srcPort = value; |
| 128 | + set_srcPort = true; |
| 129 | + if(!bootstrap) |
| 130 | + parent_.updateFiltersMaps(); |
| 131 | +} |
| 132 | + |
| 133 | +uint16_t Filters::getDport() { |
| 134 | + return dstPort; |
| 135 | +} |
| 136 | + |
| 137 | +void Filters::setDport(const uint16_t &value) { |
| 138 | + dstPort = value; |
| 139 | + set_dstPort = true; |
| 140 | + if(!bootstrap) |
| 141 | + parent_.updateFiltersMaps(); |
| 142 | +} |
| 143 | + |
| 144 | +uint32_t Filters::getNetworkFilterSrc(){ |
| 145 | + return networkSrc; |
| 146 | +} |
| 147 | + |
| 148 | +uint32_t Filters::getNetworkFilterDst(){ |
| 149 | + return networkDst; |
| 150 | +} |
| 151 | + |
| 152 | +uint32_t Filters::getNetmaskFilterSrc(){ |
| 153 | + return netmaskSrc; |
| 154 | +} |
| 155 | + |
| 156 | +uint32_t Filters::getNetmaskFilterDst(){ |
| 157 | + return netmaskDst; |
| 158 | +} |
0 commit comments