-
Notifications
You must be signed in to change notification settings - Fork 78
/
op_ips.go
164 lines (132 loc) · 3.37 KB
/
op_ips.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package spruce
import (
"fmt"
"net"
"github.com/ziutek/utils/netaddr"
"github.com/starkandwayne/goutils/tree"
. "github.com/geofffranks/spruce/log"
)
// IpOperator...
type IpsOperator struct{}
// Setup ...
func (IpsOperator) Setup() error {
return nil
}
// Phase ...
func (IpsOperator) Phase() OperatorPhase {
return EvalPhase
}
// Dependencies ...
func (IpsOperator) Dependencies(_ *Evaluator, args []*Expr, locs []*tree.Cursor, auto []*tree.Cursor) []*tree.Cursor {
l := []*tree.Cursor{}
for _, arg := range args {
if arg.Type != Reference {
continue
}
for _, other := range locs {
if other.Under(arg.Reference) {
l = append(l, other)
}
}
}
//append autogenerated dependencies (operator reference-type arguments)
for _, dep := range auto {
l = append(l, dep)
}
return l
}
func makeInt(val interface{}) int {
var num int;
num, ok := val.(int)
if !ok {
num = int(val.(int64))
}
return num
}
func netSize(ipnet *net.IPNet) int {
ones, bits := ipnet.Mask.Size()
return 1<<uint(bits-ones)
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
// Run ...
func (IpsOperator) Run(ev *Evaluator, args []*Expr) (*Response, error) {
DEBUG("running (( ips ... )) operation at $.%s", ev.Here)
defer DEBUG("done with (( ips ... )) operation at $%s\n", ev.Here)
if (len(args) < 2) {
return nil, fmt.Errorf("ips requires at least two arguments: 1) An IP or a CIDR and 2) an index")
}
var vals []interface{}
for i, arg := range args {
v, err := arg.Resolve(ev.Tree)
if err != nil {
DEBUG(" [%d]: resolution failed\n error: %s", i, err)
return nil, err
}
switch v.Type {
case Literal:
DEBUG(" arg[%d]: found string literal '%s'", i, v.Literal)
vals = append(vals, v.Literal)
case Reference:
DEBUG(" arg[%d]: trying to resolve reference $.%s", i, v.Reference)
s, err := v.Reference.Resolve(ev.Tree)
if err != nil {
DEBUG(" [%d]: resolution failed\n error: %s", i, err)
return nil, fmt.Errorf("Unable to resolve `%s`: %s", v.Reference, err)
}
DEBUG(" [%d]: resolved to a value (could be a map, a list or a scalar); appending", i)
vals = append(vals, s)
default:
DEBUG(" arg[%d]: I don't know what to do with '%v'", i, arg)
return nil, fmt.Errorf("ips operator only accepts literals and key reference arguments")
}
DEBUG("")
}
ip, ipnet, err := net.ParseCIDR(vals[0].(string))
if err != nil {
ip = net.ParseIP(vals[0].(string))
if ip == nil {
DEBUG(" [n]: failed to parse IP or CIDR \"%s\": %s", vals[0], err)
return nil, err
}
}
start := makeInt(vals[1])
if ipnet != nil {
ip = ip.Mask(ipnet.Mask)
netsize := netSize(ipnet)
if abs(start) > netsize {
return nil, fmt.Errorf("Start index %d exceeds size of subnet %s", start, vals[0])
}
if start < 0 {
start += netsize
}
}
if len(args) == 2 {
return &Response{
Type: Replace,
Value: netaddr.IPAdd(ip, start).String(),
}, nil
} else {
count := makeInt(vals[2])
if ipnet != nil {
if start + count > netSize(ipnet) {
return nil, fmt.Errorf("Start index %d and count %d would exceed size of subnet %s", start, count, vals[0])
}
}
lst := []interface{}{}
for i := start; i < start + count; i++ {
lst = append(lst, netaddr.IPAdd(ip, i).String())
}
return &Response{
Type: Replace,
Value: lst,
}, nil
}
}
func init() {
RegisterOp("ips", IpsOperator{})
}