Skip to content

Commit 17a56ac

Browse files
author
Jordan Holland
authored
Merge pull request #10 from nprint/index
Index
2 parents 0598bce + 5e3acbb commit 17a56ac

File tree

124 files changed

+431
-439
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+431
-439
lines changed

Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ AUTOMAKE_OPTIONS = subdir-objects
22

33
bin_PROGRAMS = pcapml
44

5-
pcapml_SOURCES = include/dir/dir.hpp include/label/label.hpp include/label/labeler.hpp \
5+
pcapml_SOURCES = include/label/dir.hpp include/label/label.hpp include/label/labeler.hpp \
6+
include/label/pcap_labeler.hpp \
67
include/pcap/reader_pcap.hpp include/pcap/writer_pcap.hpp \
78
include/pcapng/block_pcapng.hpp include/pcapng/reader_pcapng.hpp \
89
include/pcapng/sorter_pcapng.hpp include/pcapng/splitter_pcapng.hpp \
910
include/pcapng/writer_pcapng.hpp include/pcapng/stripper_pcapng.hpp \
1011
include/sample/sample.hpp include/sample/sampler.hpp \
1112
include/util.hpp \
12-
src/dir/dir.cpp src/label/label.cpp src/label/labeler.cpp \
13+
src/label/dir.cpp src/label/label.cpp src/label/labeler.cpp \
14+
src/label/pcap_labeler.cpp \
1315
src/pcap/reader_pcap.cpp src/pcap/writer_pcap.cpp \
1416
src/pcapng/block_pcapng.cpp src/pcapng/reader_pcapng.cpp \
1517
src/pcapng/sorter_pcapng.cpp src/pcapng/splitter_pcapng.cpp \

include/dir/dir.hpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

include/label/dir.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 nPrint
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
* use this file except in compliance with the License. You may obtain a copy
5+
* of the License at https://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
8+
#ifndef INCLUDE_LABEL_DIR_HPP_
9+
#define INCLUDE_LABEL_DIR_HPP_
10+
11+
#define MAX_FILE_LEN 2056
12+
13+
#include <stdio.h>
14+
#include <dirent.h>
15+
#include <string.h>
16+
#include <sys/stat.h>
17+
18+
#include <map>
19+
#include <string>
20+
21+
#include "labeler.hpp"
22+
23+
class DirLabeler : public Labeler {
24+
public:
25+
void print_stats();
26+
uint32_t process_packet(PcapPacketInfo *pi);
27+
uint32_t label_dir(std::string dir, std::string label_file,
28+
std::string outfile, bool stats_out);
29+
private:
30+
uint64_t files_processed = 0;
31+
uint64_t files_skipped = 0;
32+
Label *active_file_label;
33+
int16_t cur_linktype = 999;
34+
std::map<std::string, Label *> file_labels;
35+
36+
void build_file_label_map();
37+
uint32_t label_file(Label *l);
38+
uint32_t process_directory(std::string dir);
39+
};
40+
41+
#endif // INCLUDE_LABEL_DIR_HPP_

include/label/label.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818
class Label {
1919
public:
2020
void print();
21-
bool set_info(std::string label, std::string bpf_filter = "",
21+
uint32_t set_info(std::string label, std::string bpf_filter = "",
22+
std::string file = "", std::string hash_key = "",
2223
uint64_t ts_start = 0, uint64_t ts_end = UINT64_MAX,
2324
pcap_t *handle = NULL);
25+
std::string get_file();
2426
std::string get_label();
2527
std::string get_sample_id();
2628
std::string get_comment_string();
2729
std::string get_unhashed_sample_id();
28-
bool match_packet(pcap_packet_info *pi);
30+
bool match_packet(PcapPacketInfo *pi);
2931
private:
3032
bool info_set = false;
33+
uint64_t ts_start, ts_end;
3134
std::string bpf_string_filter;
3235
struct bpf_program *bpf_pcap_filter = NULL;
33-
uint64_t ts_start, ts_end;
34-
std::string sample_id, unhashed_sample_id, label, comment_str;
36+
std::string sample_id, unhashed_sample_id, label, comment_str, file, hash_key;
3537
};
3638

3739
#endif // INCLUDE_LABEL_LABEL_HPP_

include/label/labeler.hpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,39 @@
1010

1111
#include <signal.h>
1212

13+
static volatile int stop = 0;
14+
15+
#include <string>
1316
#include <vector>
1417
#include <fstream>
1518

16-
static volatile int stop = 0;
17-
1819
#include "util.hpp"
1920
#include "label.hpp"
2021
#include "reader_pcap.hpp"
2122
#include "writer_pcapng.hpp"
2223

23-
#define LABEL_FILE_LOC 0
24-
#define FILTER_LOC 1
25-
#define TS_START 2
26-
#define TS_END 3
24+
#define TRAFFIC_LOC 0
25+
#define METADATA_LOC 1
26+
#define HASHKEY_LOC 2
2727

28-
class PcapMLLabeler {
28+
class Labeler {
2929
public:
30-
void print_stats(FILE *stream);
31-
bool label_pcap(char *label_file, char *pcap, char *outfile,
32-
bool infile_is_device, bool print_stats);
33-
private:
30+
virtual void print_stats() = 0;
31+
virtual uint32_t process_packet(PcapPacketInfo *pi) = 0;
32+
33+
int process_traffic(PcapReader r);
34+
int load_labels(std::string label_file, pcap_t *handle = NULL);
35+
protected:
36+
PcapNGWriter w;
3437
std::vector<Label *> labels;
35-
uint64_t packets_matched = 0;
38+
uint64_t packets_matched = 0;
3639
uint64_t packets_received = 0;
37-
38-
bool load_labels(char *label_file, pcap_t *handle = NULL);
40+
private:
41+
Label *process_label_line(std::string line, pcap_t *handle = NULL);
42+
Label *process_traffic_filter(std::string traffic_filter,
43+
std::string hash_key,
44+
std::string metadata,
45+
pcap_t *handle = NULL);
3946
};
4047

4148
#endif // INCLUDE_LABEL_LABELER_HPP_

include/label/pcap_labeler.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2020 nPrint
3+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
4+
* use this file except in compliance with the License. You may obtain a copy
5+
* of the License at https://www.apache.org/licenses/LICENSE-2.0
6+
*/
7+
8+
#ifndef INCLUDE_LABEL_PCAP_LABELER_HPP_
9+
#define INCLUDE_LABEL_PCAP_LABELER_HPP_
10+
11+
#include "labeler.hpp"
12+
13+
class PcapLabeler : public Labeler {
14+
public:
15+
void print_stats();
16+
uint32_t process_packet(PcapPacketInfo *pi);
17+
uint32_t label_pcap(char *label_file, char *pcap, char *outfile,
18+
bool infile_is_device, bool print_stats);
19+
};
20+
21+
#endif // INCLUDE_LABEL_PCAP_LABELER_HPP_

include/pcap/reader_pcap.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
class PcapReader {
2727
public:
2828
void close_file();
29+
void print_stats();
2930
pcap_t *get_pcap_t();
3031
uint16_t get_linktype();
3132
int open_live(char *devce);
3233
int open_file(char *infile);
33-
void print_stats(FILE *stream);
34-
pcap_packet_info *get_next_packet();
34+
PcapPacketInfo *get_next_packet();
3535
private:
3636
pcap_t *f = NULL;
3737
};

include/pcapng/writer_pcapng.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class PcapNGWriter {
4242
void close_file();
4343
int write_section_block();
4444
int write_interface_block(uint16_t link_type, uint32_t snap_len);
45-
int write_epb_from_pcap_pkt(pcap_packet_info *pi, std::string comment);
45+
int write_epb_from_pcap_pkt(PcapPacketInfo *pi, std::string comment);
4646
private:
4747
FILE *pcapng;
4848

include/util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#include "block_pcapng.hpp"
2121

22-
struct pcap_packet_info {
22+
struct PcapPacketInfo {
2323
struct pcap_pkthdr *hdr;
2424
const uint8_t *buf;
2525
int32_t pcap_next_rv;

src/dir/dir.cpp

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)