我在运行在 macOS 上的 Docker 中使用 Rust 编写了一个简单的数据包捕获程序。
然而,libc::recv 函数没有返回任何结果。
代码文件为 src/main.rs。
use std::io;
fn main() -> io::Result<()> {
let sock = unsafe {
match libc::socket(
libc::AF_PACKET,
libc::SOCK_RAW,
libc::ETH_P_ALL.to_be() as _,
) {
-1 => Err(io::Error::last_os_error()),
fd => Ok(fd),
}
}?;
println!("sock: {}", sock);
let mut buf = [0u8; 1024];
loop {
let len = unsafe {
libc::recv(
sock,
(&mut buf[..]).as_mut_ptr() as *mut libc::c_void,
buf.len(),
0,
)
};
println!("len: {}", len);
}
}
Dockerfile
RUN apt-get update && apt-get install -y tcpdump
WORKDIR /app
COPY . .
ENTRYPOINT ["cargo", "run"]
运行命令
$ docker build . -t rust_cap && docker run -p 127.0.0.1:15006:15006/udp -it --rm --name="rust_cap" rust_cap
我尝试使用tcpdump检查是否有任何数据包发送到容器中,并使用strace检查系统调用,它们似乎是正确的。
其次,我使用C语言编写了与上述代码相同的代码,如下所示:
main.c
#include <stdio.h>
#include <sys/socket.h>
#include <net/ethernet.h>
int main(int argc, char **argv) {
int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock < 0) {
perror("socket");
exit(1);
}
printf("sock: %d\n", sock);
u_char buf[1024];
while (1) {
int len = recv(sock, buf, sizeof(buf), 0);
printf("len: %d\n", len);
}
}
Dockerfile
FROM ubuntu:20.04
RUN apt-get update && apt-get install -y build-essential
COPY . .
RUN cc -o main main.c
ENTRYPOINT ["./main"]
运行命令
$ docker build . -t c_cap && docker run -p 127.0.0.1:15007:15007/udp -it --rm --name="c_cap" c_cap
这段 C 代码可以正确运行。(我能在标准输出中看到我发送的每条消息的长度len: xxx
)
为什么 Rust 代码中的 recv
没有返回呢?我漏了什么吗?
参考 strace 输出
Rust 代码中:
socket(AF_PACKET, SOCK_RAW, htons(0 /* ETH_P_??? */)) = 3
write(1, "sock: 3\n", 8sock: 3
) = 8
recvfrom(3,
C语言中(接收缓冲区的部分被省略)
socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)) = 3
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(0x88, 0), ...}) = 0
brk(NULL) = 0x5616f5ab4000
brk(0x5616f5ad5000) = 0x5616f5ad5000
write(1, "sock: 3\n", 8sock: 3
) = 8
recvfrom(3,
我通过以下方式运行了strace。
$ docker run -it --cap-add sys_ptrace --entrypoint="/bin/bash" $IMAGE
$ cargo build && strace /app/target/debug/xxx
# or strace /main
strace
的输出?如果它是“正确的”,那么你应该看到在Rust程序中发出并完成了recv
。如果不是这样的话,你看到了什么?Rust和C程序的strace
输出应该几乎相同。如果它们不同,你的问题就在于这种差异。如有疑问,请编辑问题以包括两个程序的相关部分strace
输出。 - user4815162342recv
处阻塞了两个。 - tharaETH_P_ALL
是一个类型为c_int
的值 3,即3i32
。你需要将它转换为大端序,这样你就会得到类似于 0x03000000 的东西。htons
代表主机到网络的 _short_(即 u16),所以你最终应该得到 0x0300,这是一个完全不同的数字。要模拟htons(ETH_P_ALL)
,你应该做类似于(ETH_P_ALL as u16).to_be() as _
的操作。 - user4815162342