Skip to content

Commit d8be778

Browse files
committed
capture stored in the temp folder
1 parent a558706 commit d8be778

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

Documentation/services/pcn-packetcapture/Packetcapture.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ Filters can be viewed using the command **polycubectl mysniffer filters show**
114114

115115
Get the capture dump
116116
--------------------
117-
When the service is not set in *networkmode*, the dump is automatically written in a resilient way in the current working directory.
117+
When the service is not set in *networkmode*, the dump is automatically written in a resilient way in the temporary user folder.
118118

119119
The path of the capture file can be shown using the command: **polycubectl mysniffer show dump**
120120

src/services/pcn-packetcapture/src/Packetcapture.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Packetcapture::Packetcapture(const std::string name, const PacketcaptureJsonObje
6767
* 'timeval struct ts' rapresents the system start time in epoch calculated as
6868
* actual time - system uptime
6969
*
70-
* See line 141 or 307 of this file for more details
70+
* See line 161 or 329 of this file for more details
7171
*/
7272

7373

@@ -119,9 +119,28 @@ void Packetcapture::writeDump(const std::vector<uint8_t> &packet){
119119
std::stringstream tmstmp;
120120
tmstmp << ltm->tm_mday << "-" << ltm->tm_mon << "-" << (ltm->tm_year + 1900) << "-" << ltm->tm_hour << ":" << ltm->tm_min << ":" << ltm->tm_sec;
121121
dt = tmstmp.str();
122+
123+
//getting temp folder
124+
//std::filesystem::temp_directory_path only in c++17
125+
//according to ISO/IEC 9945 (POSIX)
126+
char const *folder = getenv("TMPDIR");
127+
if (folder == 0){
128+
folder = getenv("TMP");
129+
if (folder == 0){
130+
folder = getenv("TEMP");
131+
if (folder == 0){
132+
folder = getenv("TEMPDIR");
133+
if (folder == 0){
134+
folder = "/tmp/";
135+
}
136+
}
137+
}
138+
}
139+
temp_folder = std::string(folder);
140+
random_number = std::to_string(rand()%1000); //to avoid two files that using the same name
122141
}
123142

124-
myFile.open("capture_"+ dt +".pcap", std::ios::binary | std::ios::app);
143+
myFile.open(temp_folder + "capture_" + dt + "_" + random_number + ".pcap", std::ios::binary | std::ios::app);
125144

126145
if (writeHeader == true) {
127146
struct pcap_file_header *pcap_header = new struct pcap_file_header;
@@ -245,10 +264,12 @@ std::string Packetcapture::getDump() {
245264

246265
if (network_mode_flag) {
247266
dump << "the service is running in network mode";
248-
} else {
267+
} else if (temp_folder != ""){
249268
char *cwdr_ptr = get_current_dir_name();
250269
std::string wdr(cwdr_ptr);
251-
dump << "capture dump in " << wdr << "/capture_"+ dt +".pcap" << std::endl;
270+
dump << "capture dump in " << temp_folder << "capture_" + dt + "_" + random_number + ".pcap" << std::endl;
271+
} else{
272+
dump << "no packets captured";
252273
}
253274

254275
return dump.str();

src/services/pcn-packetcapture/src/Packetcapture.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Packetcapture : public PacketcaptureBase {
4141
bool writeHeader;
4242
std::ofstream myFile;
4343
std::string dt;
44+
std::string temp_folder;
45+
std::string random_number;
4446
std::chrono::system_clock::time_point timeP;
4547
std::chrono::nanoseconds temp_offset;
4648

tests/transparent_services/test_packetcapture.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ polycubectl attach packetcapture_service br1:toveth1
3333

3434
sudo ip netns exec ns1 ping 10.0.0.2 -c 1
3535

36-
rm -f $(polycubectl packetcapture_service show dump | cut -d ' ' -f 4-)
36+
sudo rm -f $(polycubectl packetcapture_service show dump | cut -d ' ' -f 4-)
3737

3838

3939
polycubectl detach packetcapture_service br1:toveth1

0 commit comments

Comments
 (0)