允许 linux 防火墙接收我的代码的数据包

Allowing linux firewall to receive packets for my code

我为 ping 命令编写了一个 c 代码,但出于某种原因它没有收到任何回复。我花了几天时间试图找出代码的问题,但后来我检查了使用 wireshark 发送的数据包。我发现回复是发给我的(目的地是不可达的,因为它应该是我输入的目的地IP)。在网上我发现我的防火墙可能会导致这个问题,但我没有找到任何解决方案。所以请帮助我,我怎样才能为我的防火墙添加某种特定代码的异常?谢谢。

已编辑

iptables 输出:

Chain INPUT (policy ACCEPT) target prot opt source destination

Chain FORWARD (policy ACCEPT) target prot opt source destination

Chain OUTPUT (policy ACCEPT) target prot opt source destination

Wireshark 输出:

17 0.641636029 192.168.0.134 192.168.0.1 ICMP 120 Destination unreachable (Port unreachable)

另外我也有可能弄错了,问题可能不在接收,而是在发送数据包

尝试使用以下命令检查防火墙iptables -L -n
并提供输出

考虑使用像 liboping or libping

这样的库

请注意 ping 使用 ICMP (see icmp(7)). Your program is likely to require root privilege. So you need to run it as root, or use setuid 技巧。

系统地检查您正在使用的每个系统调用是否失败(参见syscalls(2) for a list). Read errno(3) and use perror(3)(或strerror(errno))。

在您的程序上也使用 strace(1) 来了解完成了哪些系统调用(以及哪个失败)。