如何在L3网络层上收听?

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.

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

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)
            }
        }
    }

}