英文:
Raw Socket Sniffing in Golang
问题
我有一个关于使用Golang进行TCP数据包嗅探的问题。
我们编写了一个小工具,用于捕获来自光纤Tap的所有传入TCP数据包。
当前的实现在Linux下使用了一个libpcap的封装。我们需要将这个工具移植到Windows上。当然,目前还不可能。
所以我的问题是,是否有一个跨平台的解决方案来嗅探数据包?我们只需要TCP数据包、IP头部,不需要以太网数据和libpcap的所有功能。
如果没有跨平台的解决方案,两种代码实现也可以。我知道在Linux下可以使用原始套接字(在Windows下有一些限制)。Golang是否支持原始套接字,并且是否有一个使用套接字嗅探数据包的示例实现?
谢谢!
英文:
I have a question concerning tcp packet sniffing with golang.
We have written a small tool which captures all incoming TCP packets comming from a fibre Tap.
The current implementation uses a libpcap wrapper under Linux. We need to port this tool to Windows. Of course, it is not possible at the moment.
So my question is, is there a cross platform solution for sniffing packets? We only need TCP packets, IP headers , no Ethernet Data and not all features of libpcap.
If there is no cross platform solution, two Code implementation would be ok, too. I know one can use raw sockets under Linux (and with some limitations under Windows). Does Golang support raw sockets and is there an example implementation for sniffing packets with sockets?
Tanks!!
答案1
得分: 7
你可以使用 ipv4 package 来自 go.net。
ipv4 包实现了用于 Internet Protocol version 4 的 IP 级别的套接字选项。
ipv4.RawConn 类型及其相关方法应该可以在跨平台上工作。
RawConn 表示使用 IPv4 传输的数据包网络端点。它用于控制几个 IP 级别的套接字选项,包括 IPv4 标头操作。它还提供了特定于 IPv4 和处理 IPv4 数据报的更高层协议(如 OSPF、GRE)的数据报网络 I/O 方法。
还有一个等效的 ipv6 包。
英文:
You should be able to use the ipv4 package from go.net.
>Package ipv4 implements IP-level socket options for the Internet Protocol version 4.
The ipv4.RawConn type and it's associated methods should work cross-platform.
>A RawConn represents a packet network endpoint that uses the IPv4 transport. It is used to control several IP-level socket options including IPv4 header manipulation. It also provides datagram based network I/O methods specific to the IPv4 and higher layer protocols that handle IPv4 datagram directly such as OSPF, GRE.
There is also an equivalent package for ipv6.
答案2
得分: 6
请查看 https://github.com/google/gopacket,因为它支持 pcap(需要 cgo),并且可以解码多种协议,包括 TCP/IP。
英文:
Take a look at https://github.com/google/gopacket since it supports pcap (requires cgo for this) and can decode a number of protocols including tcp/ip.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论