use I2n::tmpfstream to write pcap dump files
[pingcheck] / src / icmp / icmppacketfactory.cpp
index 762ce2e..2eed296 100644 (file)
@@ -33,6 +33,7 @@
 #include <boost/scoped_array.hpp>
 
 #include <logfunc.hpp>
+#include <tmpfstream.hpp>
 
 #include "boost_assert_handler.h"
 #include "icmp/icmpdata.h"
@@ -194,75 +195,52 @@ IcmpPacketItem IcmpPacketFactory::create_icmp_packet_echo_request(
 }
 
 
-/**
- * @brief helper function for dump_packet methods, do not use elsewhere!
- *
- * creates a file descriptor and expects calling function dump_packet to deal
- *   with it (i.e. test and close); also logs a dump_packet-related text
- *   
- * @returns file descriptor from mkstemps
- */
-int IcmpPacketFactory::create_dump_file(const time_t &capture_time)
-{
-    std::stringstream temp_name;
-    temp_name << DumpFilePrefix;
-    temp_name << capture_time;
-    temp_name << "_XXXXXX.pcap";
-    std::string temp_str = temp_name.str();
-    std::size_t temp_size = temp_str.size();
-    boost::scoped_array<char> secure_filename( new char[temp_size + 1] );
-    std::copy(temp_str.begin(), temp_str.end(), secure_filename.get());
-    secure_filename[temp_size] = '\0';
-    int fd = mkstemps(secure_filename.get(), 5);   // 5 = ".pcap".length
-    if (fd == -1)
-        GlobalLogger.warning() << "Failed to create temp file "
-            << secure_filename.get() << ": " << strerror(errno) << "!" << endl;
-        // maybe create containing directory if errno == ENOENT?
-    else
-        GlobalLogger.debug() << "Dumping a copy of the data into "
-                             << secure_filename.get() << endl;
-    return fd;
-}
-
-
 void IcmpPacketFactory::dump_packet(const std::string &data)
 {
     // create unique file name
     time_t capture_time = time(0);
-    int fd = create_dump_file(capture_time);
-    if (fd == -1)
-        return;
+    std::stringstream temp_name;
+    temp_name << DumpFilePrefix << capture_time << ".pcap.XXXXXX";
 
-    // create file pointer for file descriptor
-    FILE *fp = fdopen(fd, "w");
+    // open file
+    I2n::tmpfstream temp_stream;
+    if ( !temp_stream.open(temp_name.str()) )
+    {
+        GlobalLogger.warning() << "Failed to create temp file "
+            << temp_name.str() << ": " << strerror(errno) << "!" << endl;
+        return;
+    }
 
-    // dump data
-    write_pcap_packet_data(data, fp, capture_time);
+    // write
+    write_pcap_packet_data(data, temp_stream, capture_time);
 
-    // clean up
-    fclose(fp);
-    close(fd);
+    // close
+    GlobalLogger.debug() << "Dumped a copy of the ICMP data into "
+                         << temp_stream.get_tmp_filename() << endl;
+    temp_stream.close();
 }
 
 void IcmpPacketFactory::dump_packet(const IcmpPacket &packet)
 {
     // create unique file name
     time_t capture_time = time(0);
-    int fd = create_dump_file(capture_time);
-    if (fd == -1)
-        return;
-
-    // create file pointer for file descriptor
-    FILE *fp = fdopen(fd, "w");
+    std::stringstream temp_name;
+    temp_name << DumpFilePrefix << capture_time << ".pcap.XXXXXX";
 
-    // create string with packet contents
-    std::stringstream data;
-    packet.dump(data);
+    // open file
+    I2n::tmpfstream temp_stream;
+    if ( !temp_stream.open(temp_name.str()) )
+    {
+        GlobalLogger.warning() << "Failed to create temp file "
+            << temp_name.str() << ": " << strerror(errno) << "!" << endl;
+        return;
+    }
 
     // dump data
-    write_pcap_packet_data(data.str(), fp, capture_time);
+    packet.dump(temp_stream);
 
-    // clean up
-    fclose(fp);
-    close(fd);
+    // close
+    GlobalLogger.debug() << "Dumped a copy of the packet into "
+                         << temp_stream.get_tmp_filename() << endl;
+    temp_stream.close();
 }