在Golang的原始插座嗅探 [英] Raw Socket Sniffing in Golang

查看:277
本文介绍了在Golang的原始插座嗅探的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于golang的TCP数据包嗅探的问题。
我们写了一个小工具来捕获所有来自光纤Tap的传入TCP数据包。

当前的实现在Linux下使用libpcap包装器。我们需要将此工具移植到Windows。当然,目前这是不可能的。

所以我的问题是,是否有跨平台解决方案来侦听数据包?我们只需要TCP数据包,IP头,不需要以太网数据,也不需要libpcap的所有功能。

如果没有跨平台解决方案,那么两个代码实现也可以。我知道可以在Linux下使用原始套接字(并且在Windows下有一些限制)。 Golang是否支持原始套接字,并且是否存在一个用套接字嗅探数据包的示例实现?

坦克!! :-)

解决方案

您应该可以使用 go.net的google.com/p/go.net/ipv4\">ipv4软件包
$ b


软件包ipv4为Internet协议版本4实现了IP级套接字选项。


ipv4.RawConn <


RawConn表示使用IPv4传输的分组网络端点。它用于控制多个IP级别的套接字选项,包括IPv4标头操作。它还提供特定于IPv4和更高层协议的基于数据报的网络I / O方法,可直接处理IPv4数据报,如OSPF,GRE。 p>还有一个用于 ipv6 的等效包。


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!! :-)

解决方案

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.

这篇关于在Golang的原始插座嗅探的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆