File: route_options.go

package info (click to toggle)
golang-github-jsimonetti-rtnetlink 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,264 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 932 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package rtnl

import (
	"net"

	"github.com/jsimonetti/rtnetlink/v2"
)

// RouteOptions is the functional options struct
type RouteOptions struct {
	Src   *net.IPNet
	Attrs rtnetlink.RouteAttributes
}

// RouteOption is the functional options func
type RouteOption func(*RouteOptions)

// DefaultRouteOptions defines the default route options.
func DefaultRouteOptions(ifc *net.Interface, dst net.IPNet, gw net.IP) *RouteOptions {
	ro := &RouteOptions{
		Src: nil,
		Attrs: rtnetlink.RouteAttributes{
			Dst:      dst.IP,
			OutIface: uint32(ifc.Index),
		},
	}

	if gw != nil {
		ro.Attrs.Gateway = gw
	}

	return ro
}

// WithRouteSrc sets the src address.
func WithRouteSrc(src *net.IPNet) RouteOption {
	return func(opts *RouteOptions) {
		opts.Src = src
	}
}

// WithRouteAttrs sets the attributes.
func WithRouteAttrs(attrs rtnetlink.RouteAttributes) RouteOption {
	return func(opts *RouteOptions) {
		opts.Attrs = attrs
	}
}