Skip to content

florianl/go-diag

 
 

Repository files navigation

diag PkgGoDev Go

This is a work in progress version of diag. It provides a C-binding free API to the netlink based socket statistics system.

Example

package main

import (
	"fmt"
	"os"

	"github.com/florianl/go-diag"
)

func main() {
	// open a netlink socket
	nl, err := diag.Open(&diag.Config{})
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not open netlink socket: %v\n", err)
		return
	}
	defer nl.Close()

	// Dump all TCP sockets
	tcpSockets, err := nl.TCPDump()
	if err != nil {
		fmt.Fprintf(os.Stderr, "could not dump data: %v\n", err)
		return
	}

    // Loop over tcpSockets and print out source- and destination IP with
    // the respective port information.
	for _, socket := range tcpSockets {
		src := diag.ToNetipAddr(socket.ID.Src)
		srcPort := diag.Ntohs(socket.ID.SPort)
		dst := diag.ToNetipAddr(socket.ID.Dst)
		dstPort := diag.Ntohs(socket.ID.DPort)
		fmt.Printf("%v:%d -> %v:%d\n", src, srcPort, dst, dstPort)
	}
}

Requirements

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages