Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
sudn0
qtcpp_demo
提交
7a278f81
qtcpp_demo
项目概览
sudn0
/
qtcpp_demo
与 Fork 源项目一致
Fork自
colorEagleStdio / qtcpp_demo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
qtcpp_demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7a278f81
编写于
8月 12, 2022
作者:
D
dev@dev.com
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
帮助劲犇同步更新pcap的例子
上级
b228bd0c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
172 addition
and
149 deletion
+172
-149
README
README
+4
-0
pcapio/main.cpp
pcapio/main.cpp
+72
-56
pcapio/pcapio.cpp
pcapio/pcapio.cpp
+96
-93
未找到文件。
README
浏览文件 @
7a278f81
...
@@ -47,6 +47,10 @@ omp_threadpool
...
@@ -47,6 +47,10 @@ omp_threadpool
C++与OMP配合的最简线程池
C++与OMP配合的最简线程池
https://goldenhawking.blog.csdn.net/article/details/51824243
https://goldenhawking.blog.csdn.net/article/details/51824243
pcapio
UDP丢包替代:用PCAP实现C/C++以太网SDR吞吐
https://goldenhawking.blog.csdn.net/article/details/126292692
primer_calc
primer_calc
C++ OpenMP有限资源快速连续素数求取
C++ OpenMP有限资源快速连续素数求取
https://goldenhawking.blog.csdn.net/article/details/109043815
https://goldenhawking.blog.csdn.net/article/details/109043815
...
...
pcapio/main.cpp
浏览文件 @
7a278f81
#include <iostream>
#include <iostream>
#include <vector>
#include "pcapio.h"
#include "pcapio.h"
#include <pcap.h>
#include <pcap.h>
using
namespace
std
;
using
namespace
std
;
int
main
()
int
main
()
{
{
std
::
map
<
std
::
string
,
std
::
string
>
devmap
;
std
::
map
<
std
::
string
,
std
::
string
>
devmap
;
std
::
string
errstring
=
PCAPIO
::
pcapio_interfaces
(
devmap
);
std
::
vector
<
std
::
string
>
names
;
for
(
auto
&
p
:
devmap
)
std
::
string
errstring
=
PCAPIO
::
pcapio_interfaces
(
devmap
);
{
if
(
errstring
.
size
())
cout
<<
p
.
second
;
{
cout
<<
"
\n
"
;
cout
<<
"Error:
\n
"
<<
errstring
<<
"
\n
"
;
}
return
0
;
}
for
(
auto
&
p
:
devmap
)
{
names
.
push_back
(
p
.
first
);
cout
<<
"==========
\n
"
;
cout
<<
"Device "
<<
names
.
size
()
<<
":
\n
"
;
cout
<<
p
.
second
;
}
cout
<<
"Input device name:"
;
cout
<<
"Input src device id 1~"
<<
names
.
size
()
<<
":"
;
unsigned
int
nID
=
0
;
cin
>>
nID
;
if
(
!
nID
||
nID
>
devmap
.
size
())
{
cout
<<
"Invalid ID out of range
\n
"
;
return
0
;
}
std
::
string
strDev
=
names
[
nID
-
1
];
std
::
string
strDev
;
pcap_t
*
handle
=
NULL
;
cin
>>
strDev
;
char
errbuf
[
PCAP_ERRBUF_SIZE
];
handle
=
pcap_open_live
(
strDev
.
c_str
(),
65535
,
1
,
10
,
errbuf
);
if
(
handle
==
NULL
)
{
printf
(
"pcap_open_live return err,errbuf:%s...
\n
"
,
errbuf
);
return
-
1
;
}
struct
bpf_program
filter
;
char
filter_app
[]
=
"ip"
;
bpf_u_int32
net
=
0
;
int
ret32
=
pcap_compile
(
handle
,
&
filter
,
filter_app
,
0
,
net
);
if
(
ret32
<
0
)
{
printf
(
"pcap_compile return %d, errbuf:%s
\n
"
,
ret32
,
errbuf
);
return
-
1
;
}
ret32
=
pcap_setfilter
(
handle
,
&
filter
);
if
(
ret32
<
0
)
{
printf
(
"pcap_setfilter return %d, errbuf:%s
\n
"
,
ret32
,
errbuf
);
return
-
1
;
}
pcap_t
*
handle
=
NULL
;
const
u_char
*
packet
;
char
errbuf
[
PCAP_ERRBUF_SIZE
];
struct
pcap_pkthdr
header
;
handle
=
pcap_open_live
(
strDev
.
c_str
(),
65535
,
1
,
10
,
errbuf
);
//注意,要设置较大的缓存
if
(
handle
==
NULL
)
pcap_set_buffer_size
(
handle
,
256
*
1024
*
1024
);
{
printf
(
"pcap_open_live return err,errbuf:%s...
\n
"
,
errbuf
);
return
-
1
;
}
struct
bpf_program
filter
;
char
filter_app
[]
=
"ip"
;
bpf_u_int32
net
=
0
;
int
ret32
=
pcap_compile
(
handle
,
&
filter
,
filter_app
,
0
,
net
);
if
(
ret32
<
0
)
{
printf
(
"pcap_compile return %d, errbuf:%s
\n
"
,
ret32
,
errbuf
);
return
-
1
;
}
ret32
=
pcap_setfilter
(
handle
,
&
filter
);
if
(
ret32
<
0
)
{
printf
(
"pcap_setfilter return %d, errbuf:%s
\n
"
,
ret32
,
errbuf
);
return
-
1
;
}
const
u_char
*
packet
;
struct
pcap_pkthdr
header
;
pcap_set_buffer_size
(
handle
,
128
*
1024
*
1024
);
while
(
1
)
{
packet
=
pcap_next
(
handle
,
&
header
);
while
(
1
)
if
(
packet
)
{
{
packet
=
pcap_next
(
handle
,
&
header
);
printf
(
"LEN=%d:"
,
header
.
len
);
if
(
packet
)
for
(
unsigned
int
i
=
0
;
i
<
header
.
len
;
++
i
)
{
{
printf
(
"LEN=%d:"
,
header
.
len
);
if
((
i
>=
14
&&
i
<
32
)
||
i
+
4
>=
header
.
len
)
for
(
int
i
=
0
;
i
<
header
.
len
;
++
i
)
printf
(
"%02X"
,
packet
[
i
]);
{
else
if
(
i
==
32
)
if
((
i
>=
14
&&
i
<
32
)
||
i
+
4
>=
header
.
len
)
printf
(
"..."
);
printf
(
"%02X"
,
packet
[
i
]);
}
else
if
(
i
==
32
)
printf
(
"
\n
"
);
printf
(
"..."
);
//如果需要转发,则要打开另一个handle。
}
//pcap_sendpacket(handle_dst,packet,header.len);
printf
(
"
\n
"
);
}
}
}
}
pcap_close
(
handle
);
pcap_close
(
handle
);
return
0
;
return
0
;
}
}
pcapio/pcapio.cpp
浏览文件 @
7a278f81
...
@@ -2,102 +2,105 @@
...
@@ -2,102 +2,105 @@
#include <pcap.h>
#include <pcap.h>
#include <string.h>
#include <string.h>
namespace
PCAPIO
{
namespace
PCAPIO
{
std
::
string
ifaddresses
(
pcap_if_t
*
d
);
std
::
string
ifaddresses
(
pcap_if_t
*
d
);
std
::
string
pcapio_interfaces
(
std
::
map
<
std
::
string
,
std
::
string
>
&
devmap
)
std
::
string
pcapio_interfaces
(
std
::
map
<
std
::
string
,
std
::
string
>
&
devmap
)
{
{
std
::
string
res
;
std
::
string
res
;
pcap_if_t
*
alldevs
;
pcap_if_t
*
alldevs
;
int
count
=
0
;
char
errbuf
[
PCAP_ERRBUF_SIZE
];
char
errbuf
[
PCAP_ERRBUF_SIZE
];
if
(
pcap_findalldevs
(
&
alldevs
,
errbuf
)
==
-
1
){
if
(
pcap_findalldevs
(
&
alldevs
,
errbuf
)
==
-
1
){
res
=
errbuf
;
res
=
errbuf
;
return
res
;
return
res
;
}
}
for
(
auto
d
=
alldevs
;
d
!=
NULL
;
d
=
d
->
next
){
for
(
auto
d
=
alldevs
;
d
!=
NULL
;
d
=
d
->
next
){
std
::
string
d_name
=
d
->
name
;
std
::
string
d_name
=
d
->
name
;
std
::
string
d_des
=
ifaddresses
(
d
);
std
::
string
d_des
=
ifaddresses
(
d
);
devmap
[
d_name
]
=
d_des
;
devmap
[
d_name
]
=
d_des
;
}
}
pcap_freealldevs
(
alldevs
);
pcap_freealldevs
(
alldevs
);
return
res
;
return
res
;
}
}
std
::
string
address_print
(
unsigned
char
*
v
)
std
::
string
address_print
(
unsigned
char
*
v
)
{
{
std
::
string
res
;
std
::
string
res
;
char
buf
[
1024
];
char
buf
[
1024
];
int
lbegin
=
0
,
rbegin
=
13
;
int
lbegin
=
0
,
rbegin
=
13
;
while
(
!
v
[
lbegin
]
)
while
(
!
v
[
lbegin
]
)
{
{
++
lbegin
;
++
lbegin
;
if
(
lbegin
>=
rbegin
)
if
(
lbegin
>=
rbegin
)
break
;
break
;
}
}
while
(
!
v
[
rbegin
]
)
while
(
!
v
[
rbegin
]
)
{
{
--
rbegin
;
--
rbegin
;
if
(
lbegin
>=
rbegin
)
if
(
lbegin
>=
rbegin
)
break
;
break
;
}
}
res
+=
"HEX("
;
res
+=
"HEX("
;
for
(
int
i
=
lbegin
;
i
<=
rbegin
;
++
i
)
for
(
int
i
=
lbegin
;
i
<=
rbegin
;
++
i
)
{
{
snprintf
(
buf
,
1024
,
"%02X"
,
(
unsigned
int
)
v
[
i
]);
if
(
i
>
lbegin
)
res
+=
buf
;
res
+=
":"
;
}
snprintf
(
buf
,
1024
,
"%02X"
,
(
unsigned
int
)
v
[
i
]);
res
+=
")"
;
res
+=
buf
;
res
+=
"DEC("
;
}
for
(
int
i
=
lbegin
;
i
<=
rbegin
;
++
i
)
res
+=
")"
;
{
res
+=
"DEC("
;
if
(
i
>
lbegin
)
for
(
int
i
=
lbegin
;
i
<=
rbegin
;
++
i
)
res
+=
"."
;
{
snprintf
(
buf
,
1024
,
"%u"
,
(
unsigned
int
)
v
[
i
]
);
if
(
i
>
lbegin
)
res
+=
buf
;
res
+=
"."
;
}
snprintf
(
buf
,
1024
,
"%u"
,
(
unsigned
int
)
v
[
i
]
);
res
+=
")"
;
res
+=
buf
;
}
res
+=
")"
;
return
res
;
return
res
;
}
}
/* Print all the available information on the given interface */
/* Print all the available information on the given interface */
std
::
string
ifaddresses
(
pcap_if_t
*
d
)
std
::
string
ifaddresses
(
pcap_if_t
*
d
)
{
{
char
buf
[
1024
];
char
buf
[
1024
];
pcap_addr_t
*
a
;
pcap_addr_t
*
a
;
std
::
string
res
=
d
->
name
;
std
::
string
res
=
"NAME="
;
res
+=
"
\n
"
;
res
+=
d
->
name
;
if
(
d
->
description
)
res
+=
"
\n
"
;
{
if
(
d
->
description
)
res
+=
d
->
description
;
{
res
+=
"
\n
"
;
res
+=
"Description="
;
}
res
+=
d
->
description
;
res
+=
"
\n
"
;
}
for
(
a
=
d
->
addresses
;
a
;
a
=
a
->
next
)
{
for
(
a
=
d
->
addresses
;
a
;
a
=
a
->
next
)
{
snprintf
(
buf
,
1024
,
"
\t
AF_0x%02X:"
,(
unsigned
int
)
a
->
addr
->
sa_family
);
snprintf
(
buf
,
1024
,
"
AF_0x%02X:"
,(
unsigned
int
)
a
->
addr
->
sa_family
);
res
+=
buf
;
res
+=
buf
;
if
(
a
->
addr
)
if
(
a
->
addr
)
{
{
snprintf
(
buf
,
1024
,
"
\n\t\t
Addr:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
snprintf
(
buf
,
1024
,
"
\n
Addr:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
res
+=
buf
;
res
+=
buf
;
}
}
if
(
a
->
netmask
)
if
(
a
->
netmask
)
{
{
snprintf
(
buf
,
1024
,
"
\n\t\t
Mask:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
snprintf
(
buf
,
1024
,
"
\n
Mask:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
res
+=
buf
;
res
+=
buf
;
}
}
if
(
a
->
broadaddr
)
if
(
a
->
broadaddr
)
{
{
snprintf
(
buf
,
1024
,
"
\n\t\t
Cast:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
snprintf
(
buf
,
1024
,
"
\n
Cast:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
res
+=
buf
;
res
+=
buf
;
}
}
if
(
a
->
dstaddr
)
if
(
a
->
dstaddr
)
{
{
snprintf
(
buf
,
1024
,
"
\n\t\t
Dest:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
snprintf
(
buf
,
1024
,
"
\n
Dest:%s "
,
address_print
((
unsigned
char
*
)
a
->
addr
->
sa_data
).
c_str
());
res
+=
buf
;
res
+=
buf
;
}
}
res
+=
"
\n
"
;
res
+=
"
\n
"
;
}
}
return
res
;
return
res
;
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录