/go-conntrack

c-binding free API for golang to communicate with the conntrack subsystem

Primary LanguageGoMIT LicenseMIT

go-conntrack Build Status GoDoc

This is go-conntrack and it is written in golang. It provides a C-binding free API to the conntrack subsystem of the Linux kernel.

Example

package main
import (
    "fmt"

    ct "github.com/florianl/go-conntrack"
)

func main() {
    // Opens the socket for the communication with the subsystem
    nfct, err := ct.Open()
    if err != nil {
        fmt.Println("Could not open socket:", err)
        return
    }
    defer nfct.Close()

    // Get all IPv4 sessions
    sessions, err := nfct.Dump(ct.Ct, ct.CtIPv4)
    if err != nil {
        fmt.Println("Could not dump sessions:", err)
        return
    }

    for _, x := range sessions {
        oSrcIP, _ := x.OrigSrcIP()
        oDstIP, _ := x.OrigDstIP()
        // Print source and destination for each IPv4 session
        fmt.Printf("src: %s\tdst: %s \n", oSrcIP, oDstIP)
    }
}

For documentation and more examples please take a look at GoDoc