winroute

package module
v0.0.0-...-9106bec Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

README

win-route

Manipulate Windows routing table via system calls

Example

package main

import (
  "flag"
  "net"

  log "github.com/Sirupsen/logrus"
)

func main() {
  gateway := flag.String("gateway", "", "interface (IPv4) address serving as a gateway")
  debug := flag.Bool("debug", false, "enable debug logging")
  flag.Parse()

  var gatewayAddress net.IP
  if *gateway != "" {
    gatewayAddress = net.ParseIP(*gateway)
    if gatewayAddress == nil {
      log.WithField("address", *gateway).Warn("Invalid gateway address specified")
    }
  }

  if *debug {
    log.SetLevel(log.DebugLevel)
  }

  intf := MustResolveInterface(net.ParseIP(*gateway))

  r := NewNetRoute()
  defer r.Close()

  // Make system call to get interface metric
  i, err := r.GetInterfaceByIndex(uint32(intf.Index))
  if err != nil {
    log.Fatal(err)
  }

  printRoutes(r)

  r1 := &IPForwardRow{
    ForwardDest:    Inet_aton("192.168.1.0", false),
    ForwardMask:    Inet_aton("255.255.255.0", false),
    ForwardNextHop: Inet_aton("172.22.101.121", false),
    ForwardIfIndex: i.InterfaceIndex,
    ForwardType:    3,
    ForwardProto:   3,
    ForwardMetric1: i.Metric, // route metric is 0 (+ interface metric)
  }

  if err = r.AddRoute(r1); err == nil {
    log.Info("Added route")
    printRoutes(r)

    if err = r.DeleteRoute(r1); err == nil {
      log.Info("Deleted route")
      printRoutes(r)
    } else {
      log.Warn("Error deleting route")
    }
  } else {
    log.Warn("Error adding route")
  }
}

func printRoutes(r *NetRoute) {
  routes, err := r.GetRoutes()
  if err != nil {
    log.Error("Error getting routes")
  }
  for _, route := range routes {
    dest := Inet_ntoa(route.ForwardDest, false)
    mask := Inet_ntoa(route.ForwardMask, false)
    gate := Inet_ntoa(route.ForwardNextHop, false)
    log.WithFields(log.Fields{
      "dest":    dest,
      "mask":    mask,
      "gate":    gate,
      "metric":  route.ForwardMetric1,
      "ifIndex": route.ForwardIfIndex,
    }).Info("Route")
  }
}

Credits

Special thanks to github.com/yijunjun/route-table

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inet_aton

func Inet_aton(ip string, isBig bool) uint32

func Inet_ntoa

func Inet_ntoa(ipnr uint32, isBig bool) string

func MustResolveInterface

func MustResolveInterface(gatewayAddress net.IP) net.Interface

func ResolveInterface

func ResolveInterface(gatewayAddress net.IP) (net.Interface, error)

Types

type DynamicMemory

type DynamicMemory struct {
	// contains filtered or unexported fields
}

func (*DynamicMemory) Address

func (d *DynamicMemory) Address() uintptr

func (*DynamicMemory) Len

func (d *DynamicMemory) Len() uint32

type IPForwardRow

type IPForwardRow struct {
	ForwardDest      uint32
	ForwardMask      uint32
	ForwardPolicy    uint32
	ForwardNextHop   uint32
	ForwardIfIndex   uint32
	ForwardType      uint32
	ForwardProto     uint32
	ForwardAge       uint32
	ForwardNextHopAS uint32
	ForwardMetric1   uint32
	ForwardMetric2   uint32
	ForwardMetric3   uint32
	ForwardMetric4   uint32
	ForwardMetric5   uint32
}

func (IPForwardRow) Equals

func (t IPForwardRow) Equals(o IPForwardRow) bool

type IPInterfaceEntry

type IPInterfaceEntry struct {
	Family                               uint32
	InterfaceLuid                        uint64
	InterfaceIndex                       uint32
	MaxReassemblySize                    uint32
	InterfaceIdentifier                  uint64
	MinRouterAdvertisementInterval       uint32
	MaxRouterAdvertisementInterval       uint32
	AdvertisingEnabled                   bool
	ForwardingEnabled                    bool
	WeakHostSend                         bool
	WeakHostReceive                      bool
	UseAutomaticMetric                   bool
	UseNeighborUnreachabilityDetection   bool
	ManagedAddressConfigurationSupported bool
	OtherStatefulConfigurationSupported  bool
	AdvertiseDefaultRoute                bool
	RouterDiscoveryBehavior              uint32
	DadTransmits                         uint32
	BaseReachableTime                    uint32
	RetransmitTime                       uint32
	PathMtuDiscoveryTimeout              uint32
	LinkLocalAddressBehavior             uint32
	LinkLocalAddressTimeout              uint32
	ZoneIndices                          [16]uint32
	SitePrefixLength                     uint32
	Metric                               uint32
	NlMtu                                uint32
	Connected                            bool
	SupportsWakeUpPatterns               bool
	SupportsNeighborDiscovery            bool
	SupportsRouterDiscovery              bool
	ReachableTime                        uint32
	TransmitOffload                      InterfaceOffloadRod
	ReceiveOffload                       InterfaceOffloadRod
	DisableDefaultRoutes                 bool
}

type InterfaceOffloadRod

type InterfaceOffloadRod struct {
	ChecksumSupported          bool
	OptionsSupported           bool
	DatagramChecksumSupported  bool
	StreamChecksumSupported    bool
	StreamOptionsSupported     bool
	StreamFastPathCompatible   bool
	DatagramFastPathCompatible bool
	LargeSendOffloadSupported  bool
	GiantSendOffloadSupported  bool
}

type NetRoute

type NetRoute struct {
	// contains filtered or unexported fields
}

func NewNetRoute

func NewNetRoute() *NetRoute

func (*NetRoute) AddRoute

func (r *NetRoute) AddRoute(route *IPForwardRow) error

AddRoute adds an IPForwardRow to the routing table.

func (*NetRoute) Close

func (r *NetRoute) Close()

func (*NetRoute) DeleteRoute

func (r *NetRoute) DeleteRoute(route *IPForwardRow) error

func (*NetRoute) GetInterfaceByIndex

func (r *NetRoute) GetInterfaceByIndex(index uint32) (*IPInterfaceEntry, error)

func (*NetRoute) GetRoutes

func (r *NetRoute) GetRoutes() ([]IPForwardRow, error)

func (*NetRoute) UpdateRoute

func (r *NetRoute) UpdateRoute(route *IPForwardRow) error

type SliceHeader

type SliceHeader struct {
	Addr uintptr
	Len  int
	Cap  int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL