gtest_inject_tcp_rst creates a working directory under the current path

This commit is contained in:
luwenpeng
2024-05-17 19:50:51 +08:00
parent 31a4267e23
commit 5678cf645e
4 changed files with 46 additions and 22 deletions

View File

@@ -19,6 +19,7 @@
struct dumpfile_io
{
uint16_t nr_threads;
char work_dir[256];
char directory[256];
pcap_t *pcap;
@@ -68,7 +69,7 @@ struct pcap_file_hdr DEFAULT_PCAP_FILE_HDR =
* Private API
******************************************************************************/
static void save_packet(struct packet *pkt, uint64_t idx)
static void save_packet(const char *work_dir, struct packet *pkt, uint64_t idx)
{
int len = 0;
FILE *fp = NULL;
@@ -94,7 +95,7 @@ static void save_packet(struct packet *pkt, uint64_t idx)
inet_ntop(AF_INET6, &tuple.src_addr.v6, src_addr, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &tuple.dst_addr.v6, dst_addr, INET6_ADDRSTRLEN);
}
snprintf(file, sizeof(file), "/tmp/inject-%s:%u-%s:%u-%lu.pcap", src_addr, ntohs(tuple.src_port), dst_addr, ntohs(tuple.dst_port), idx);
snprintf(file, sizeof(file), "%s/inject-%s:%u-%s:%u-%lu.pcap", work_dir, src_addr, ntohs(tuple.src_port), dst_addr, ntohs(tuple.dst_port), idx);
fp = fopen(file, "w+");
if (fp)
@@ -297,6 +298,11 @@ struct dumpfile_io *dumpfile_io_new(const char *directory, uint16_t nr_threads)
PACKET_IO_LOG_ERROR("unable to allocate memory for dumpfile_io");
return NULL;
}
if (getcwd(handle->work_dir, sizeof(handle->work_dir)) == NULL)
{
PACKET_IO_LOG_ERROR("unable to get current work directory");
goto error_out;
}
handle->nr_threads = nr_threads;
strncpy(handle->directory, directory, MIN(strlen(directory), sizeof(handle->directory)));
@@ -468,7 +474,7 @@ int dumpfile_io_inject(struct dumpfile_io *handle, uint16_t thr_idx, struct pack
stat->dev_tx_pkts++;
stat->dev_tx_bytes += len;
save_packet(pkt, stat->inject_pkts);
save_packet(handle->work_dir, pkt, stat->inject_pkts);
packet_free(pkt);
}