-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaq_example1.cpp
More file actions
112 lines (98 loc) · 3.35 KB
/
daq_example1.cpp
File metadata and controls
112 lines (98 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <stdio.h>
#include <string.h>
#include <cstdlib>
// 외부의 C 함수를 C++에 사용하기 위한 설정
extern "C" {
#include <daq.h>
#include <daq_api.h>
}
// 패킷 처리 콜백 함수 (DAQ_Analysis_Func_t 형식)
static DAQ_Verdict packet_callback(void *user, const DAQ_PktHdr_t *hdr, const uint8_t *data)
{
printf("패킷 수신: 길이=%u\n", hdr->pktlen);
return DAQ_VERDICT_PASS;
}
int main(int argc, char *argv[])
{
const DAQ_Module_t *module = NULL;
void *handle = NULL;
const char *filter_string = "tcp port 80";
char errbuf[DAQ_ERRBUF_SIZE];
int ret;
// DAQ 모듈 디렉토리 설정
const char *module_dirs[] = {"/usr/local/lib/daq", NULL};
ret = daq_load_modules(module_dirs);
if (ret != DAQ_SUCCESS) {
fprintf(stderr, "DAQ 모듈 로드 실패: %s\n", daq_get_error(NULL, NULL));
return 1;
}
// 모듈 찾기
module = daq_find_module("pcap");
if (module == NULL) {
fprintf(stderr, "DAQ 모듈 찾기 실패: %s\n", errbuf);
daq_unload_modules();
return 1;
}
// 설정 추가
DAQ_Config_t config;
memset(&config, 0, sizeof(config));
config.mode = DAQ_MODE_PASSIVE; // 패시브 모드
config.snaplen = 1518; // 최대 패킷 길이
config.timeout = 1000; // 타임아웃 (ms)
char iface[] = "eth0";
config.name = iface; // 문자 배열 사용
// 설정 초기화(initialize)
ret = module->initialize(&config, &handle, errbuf, sizeof(errbuf));
if (ret != DAQ_SUCCESS) {
fprintf(stderr, "DAQ 초기화 실패: %s\n", errbuf);
daq_unload_modules();
return 1;
}
// 필터 설정
ret = daq_set_filter(module, handle, filter_string);
if (ret != DAQ_SUCCESS) {
fprintf(stderr, "필터 설정 실패: %s\n", daq_get_error(module, handle));
module->shutdown(handle);
daq_unload_modules();
return 1;
}
printf("BPF 필터 설정 성공: %s\n", filter_string);
// 패킷 캡처 시작
ret = daq_start(module, handle);
if (ret != DAQ_SUCCESS) {
fprintf(stderr, "패킷 캡처 시작 실패: %s\n", daq_get_error(module, handle));
module->shutdown(handle);
daq_unload_modules();
return 1;
}
printf("패킷 캡처 시작 (필터: %s)\n", filter_string);
// 패킷 처리 루프 (예: 100개 패킷 획득)
const unsigned int max_packets = 100;
unsigned int packet_count = 0;
DAQ_PktHdr_t pkt_hdr;
const uint8_t *pkt_data;
while (packet_count < max_packets) {
// 패킷 획득 (타임아웃 1000ms)
ret = daq_acquire(module, handle, 1000, packet_callback, NULL);
if (ret == DAQ_SUCCESS) {
// 패킷 데이터 처리
printf("패킷 #%u: 길이=%u\n", packet_count, pkt_hdr.pktlen);
packet_count++;
} else if (ret == DAQ_ERROR_AGAIN) {
printf("타임아웃, 재시도...\n");
continue;
} else {
fprintf(stderr, "패킷 획득 실패: %d\n", ret);
break;
}
}
// 패킷 캡처 중지
ret = daq_stop(module, handle);
if (ret != DAQ_SUCCESS) {
fprintf(stderr, "패킷 캡처 중지 실패: %s\n", daq_get_error(module, handle));
}
// 정리
module->shutdown(handle);
daq_unload_modules();
return 0;
}