如何在L3网络层上收听?

我正在创建聊天应用程序后端,并希望考虑可伸缩性。</ p>

我想创建一个负载均衡器,但不是在HTTP所在的L7层上,而是在IP网络所在的L3层上创建一个负载平衡器,以将连接定向到特定的服务器,然后我就可以创建 TCP </ code >。</ p>

net.ListenIP </ code > 用于侦听IP层上数据包的正确功能? </ p>

与更高的 Listen( 例如“ tcp”)</ code> ? 是实现负载均衡器所需的正确方法吗?</ p>

是否有关于数据包结构的参考,因此我可以从中获取要转发的源IP和目标IP </ p>

如果不告诉我在L3网络层上使用哪个函数来侦听以平衡其他服务器的负载。</ p>
</ div>

展开原文

原文

I am creating a chat application backend and want to take into consideration the scalability.

I wanted to create a load balancer but not on the L7 layer where HTTP is located, but on the L3 layer where IP network is located to direct connections to the specific servers where I can then make TCP.

Is net.ListenIP the correct function to use to listen to the packets on the IP layer?

Is it the same as the higher Listen("tcp") for example? Is it the right method that I need to implement the load balancer?

Is there a reference to how the packet is structured so I am able get out from it the source and destination IPs to forward them?

If not tell me which function to use to listen on the L3 network layer to balance the loads to other servers.

2个回答



在 阅读文档,是的,此功能将帮助您接收IP数据包。</ p>


ListenIP的作用类似于IP网络的ListenPacket。</ p>
</ blockquote>

ListenIP </ code> 相似 到 ListenPacket(“ tcp”)</ code> ,但用于IP数据包。 </ p>

关于IP数据包的结构,并使用它们, net </ code>软件包似乎没有。</ p>

还有另一个看起来像它的软件包 gopacket </ code> </ p>

gopacket </ code>中,有一个 Packet </ code> 类型,可用于网络层。 </ p>

Packet.NetworkLayer()。LayerContent()</ code>和 Packet.NetworkLayer()。LayerPayload()</ code>都将返回 字节[] </ code>,您可以通过IP数据包的预期结构来解释</ a >。</ p>


注意</ strong>:现在,我已经写完了这本书,我不得不想像那里有人在写 一个很好的覆盖层/包装器,可以使此操作更容易。 这只是我谷歌搜索10分钟的结果。 也许其他人会用更好的工具/方法来回答</ em> </ p>
</ div>

展开原文

原文

After reading the Docs, yes this function will help you receive IP Packets.

ListenIP acts like ListenPacket for IP networks.

ListenIP is similar to ListenPacket("tcp") but for IP packets.

As for the structure of IP packets, and working with them, the net package doesn't seem to have that.

There's another package gopacket which looks like it will be able to help you read and modify packets from any layer.

In gopacket there is a Packet type, which allows working with the network layer.

Packet.NetworkLayer().LayerContent() and Packet.NetworkLayer().LayerPayload() will each return byte[] which you can interpret by the expected structure of an IP packet.


Note: Now that I've written this whole thing I have to imagine somebody out there has written a nice overlay/wrapper to make this easier. This is just the result of me Googling for 10 minutes. Maybe somebody else will answer with a better tool/method

doutenglou6588
doutenglou6588 所以我看到并监听了很多数据包,然后将它们转发到服务器以处理它们并打开tcp,对吗? 平衡器充当桥梁?
大约 3 年之前 回复
dongshou2017
dongshou2017 我读了您的笔记,但您的答案一定是最好的,因为我想自己实现负载均衡器
大约 3 年之前 回复

Personally, I use gopacket in order to capture multiple network layers, and this library is very impressive.

When you're using gopacket, you are able to capture multiple network layers by specifying them, for example Ipv4, TCP, Ethernet... For more information, see layers packet.

Then, you will be able to analyze your layers by using packet.Data(), which is a set of bytes that make up this entire packet, and then switch on the packet type to perform some actions.

For example, capturing multiple network layers on eth0 :

package main

import (
    "fmt"
    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
    "github.com/google/gopacket/pcap"
    "time"
)

//Layers we want to decode
var (
    ip4 layers.IPv4
    eth layers.Ethernet
    tcp layers.TCP
)

func main() {

    //Array to store decoded layers
    decodedLayers := []gopacket.LayerType{}

    //Create parser
    parser := gopacket.NewDecodingLayerParser(layers.LayerTypeEthernet, &eth, &ip4, &tcp)

    //Here we use pcap to capture packet on eth0 interface, we can also use afpacket for example, which is more efficient
    handle, err := pcap.OpenLive("eth0", 65536, true, pcap.BlockForever)
    if err != nil {
        panic("Error opening pcap: " + err.Error())
    }

    datasource := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet)

    //packets will be a channel of packets
    packets := datasource.Packets()

    for {
        select {
        case packet := <-packets:
            //We are decoding layers, and switching on the layer type
            err := parser.DecodeLayers(packet.Data(), &decodedLayers)
            for _, typ := range decodedLayers {
                switch typ {
                case layers.LayerTypeIPv4:
                    fmt.Printf("Source ip = %s - Destination ip = %s 
", ip4.SrcIP.String(), ip4.DstIP.String())
                case layers.LayerTypeTCP:
                    //Here, we can access tcp packet properties
                    fmt.Println("Capture tcp traffic")
                }
                //etc ....
            }
            if len(decodedLayers) == 0 {
                fmt.Println("Packet truncated")
            }

            //If the DecodeLayers is unable to decode the next layer type
            if err != nil {
                //fmt.Printf("Layer not found : %s", err)
            }
        }
    }

}
duanqiaoren9975
duanqiaoren9975 您的回答也很有帮助。 谢谢
大约 3 年之前 回复
douqian2957
douqian2957 所以我看到并监听了很多数据包,然后将它们转发到服务器以处理它们并打开tcp,对吗? 平衡器充当桥梁?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐