Skip to content

Commit da9adde

Browse files
committed
Added packetcapture class
Added filters class, globalheader class and Packet class.
1 parent 5ca0fd6 commit da9adde

7 files changed

Lines changed: 645 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
#pragma once
19+
20+
21+
#include "../base/FiltersBase.h"
22+
23+
24+
struct filters_table {
25+
bool network_filter_src_flag;
26+
uint32_t network_filter_src;
27+
uint32_t netmask_filter_src;
28+
bool network_filter_dst_flag;
29+
uint32_t network_filter_dst;
30+
uint32_t netmask_filter_dst;
31+
bool src_port_flag;
32+
uint16_t src_port_filter;
33+
bool dst_port_flag;
34+
uint16_t dst_port_filter;
35+
bool l4proto_flag;
36+
int l4proto_filter; /* if l4proto_filter = 1 is TCP only else if l4proto_filter = 2 is UDP only */
37+
uint32_t snaplen;
38+
};
39+
40+
class Packetcapture;
41+
42+
using namespace polycube::service::model;
43+
44+
class Filters : public FiltersBase {
45+
46+
47+
bool set_srcIp, set_dstIp, set_srcPort, set_dstPort, set_l4proto, set_snaplen;
48+
49+
bool bootstrap = true;
50+
std::string srcIp = "0.0.0.0/24";
51+
std::string dstIp = "0.0.0.0/24";
52+
uint32_t networkSrc = 0;
53+
uint32_t networkDst = 0;
54+
uint32_t netmaskSrc = 0;
55+
uint32_t netmaskDst = 0;
56+
uint16_t srcPort = 0;
57+
uint16_t dstPort = 0;
58+
std::string l4proto = "";
59+
uint32_t snaplen = 262144; /* 65535 for no sliced packets */
60+
61+
public:
62+
Filters(Packetcapture &parent, const FiltersJsonObject &conf);
63+
virtual ~Filters();
64+
65+
/// <summary>
66+
/// Snapshot length
67+
/// </summary>
68+
uint32_t getSnaplen() override;
69+
void setSnaplen(const uint32_t &value) override;
70+
71+
/// <summary>
72+
/// IP source filter
73+
/// </summary>
74+
std::string getSrc() override;
75+
void setSrc(const std::string &value) override;
76+
bool srcIp_is_set(){ return set_srcIp; };
77+
78+
/// <summary>
79+
/// IP destination filter
80+
/// </summary>
81+
std::string getDst() override;
82+
void setDst(const std::string &value) override;
83+
bool dstIp_is_set(){ return set_dstIp; };
84+
85+
/// <summary>
86+
/// Level 4 protocol filter
87+
/// </summary>
88+
std::string getL4proto() override;
89+
void setL4proto(const std::string &value) override;
90+
bool l4proto_is_set(){ return set_l4proto; };
91+
92+
/// <summary>
93+
/// Source port filter
94+
/// </summary>
95+
uint16_t getSport() override;
96+
void setSport(const uint16_t &value) override;
97+
bool srcPort_is_set(){ return set_srcPort; };
98+
99+
/// <summary>
100+
/// Destination port filter
101+
/// </summary>
102+
uint16_t getDport() override;
103+
void setDport(const uint16_t &value) override;
104+
bool dstPort_is_set(){ return set_dstPort; };
105+
106+
uint32_t getNetworkFilterSrc();
107+
uint32_t getNetworkFilterDst();
108+
uint32_t getNetmaskFilterSrc();
109+
uint32_t getNetmaskFilterDst();
110+
};
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 "Globalheader.h"
19+
#include "Packetcapture.h"
20+
#define LINKTYPE_ETHERNET 1
21+
#define MAGICNUMBER 0xa1b2c3d4
22+
#define MAJOR_VERSION 2
23+
#define MINOR_VERSION 4
24+
25+
26+
27+
Globalheader::Globalheader(Packetcapture &parent, const GlobalheaderJsonObject &conf)
28+
: GlobalheaderBase(parent) {
29+
if (conf.magicIsSet()) {
30+
setMagic(conf.getMagic());
31+
}
32+
33+
if (conf.versionMajorIsSet()) {
34+
setVersionMajor(conf.getVersionMajor());
35+
}
36+
37+
if (conf.versionMinorIsSet()) {
38+
setVersionMinor(conf.getVersionMinor());
39+
}
40+
41+
if (conf.thiszoneIsSet()) {
42+
setThiszone(conf.getThiszone());
43+
}
44+
45+
if (conf.sigfigsIsSet()) {
46+
setSigfigs(conf.getSigfigs());
47+
}
48+
49+
if (conf.snaplenIsSet()) {
50+
setSnaplen(conf.getSnaplen());
51+
}
52+
53+
if (conf.linktypeIsSet()) {
54+
setLinktype(conf.getLinktype());
55+
}
56+
57+
}
58+
59+
Globalheader::~Globalheader() {}
60+
61+
uint32_t Globalheader::getMagic() {
62+
return MAGICNUMBER;
63+
}
64+
65+
void Globalheader::setMagic(const uint32_t &value) {
66+
throw std::runtime_error("Globalheader::setMagic: Method not implemented");
67+
}
68+
69+
uint16_t Globalheader::getVersionMajor() {
70+
return MAJOR_VERSION;
71+
}
72+
73+
void Globalheader::setVersionMajor(const uint16_t &value) {
74+
throw std::runtime_error("Globalheader::setVersionMajor: Method not implemented");
75+
}
76+
77+
uint16_t Globalheader::getVersionMinor() {
78+
return MINOR_VERSION;
79+
}
80+
81+
void Globalheader::setVersionMinor(const uint16_t &value) {
82+
throw std::runtime_error("Globalheader::setVersionMinor: Method not implemented");
83+
}
84+
85+
int32_t Globalheader::getThiszone() {
86+
return 0x0;
87+
}
88+
89+
void Globalheader::setThiszone(const int32_t &value) {
90+
throw std::runtime_error("Globalheader::setThiszone: Method not implemented");
91+
}
92+
93+
uint32_t Globalheader::getSigfigs() {
94+
return 0x0;
95+
}
96+
97+
void Globalheader::setSigfigs(const uint32_t &value) {
98+
throw std::runtime_error("Globalheader::setSigfigs: Method not implemented");
99+
}
100+
101+
uint32_t Globalheader::getSnaplen() {
102+
return snaplen;
103+
}
104+
105+
void Globalheader::setSnaplen(const uint32_t &value) {
106+
snaplen = value;
107+
}
108+
109+
uint32_t Globalheader::getLinktype() {
110+
return LINKTYPE_ETHERNET;
111+
}
112+
113+
void Globalheader::setLinktype(const uint32_t &value) {
114+
throw std::runtime_error("Globalheader::setLinktype: Method not implemented");
115+
}
116+
117+

0 commit comments

Comments
 (0)