-
Notifications
You must be signed in to change notification settings - Fork 1
/
cidrpicker.go
202 lines (177 loc) · 4.52 KB
/
cidrpicker.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"log"
"net"
)
func getVPCCidrBlock(sess *session.Session, vpcId string) (string, error) {
svc := ec2.New(sess)
input := &ec2.DescribeVpcsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcId),
},
},
},
}
result, err := svc.DescribeVpcs(input)
if err != nil {
return "", err
}
return *result.Vpcs[0].CidrBlock, nil
}
func getVPCSubnetCidrBlocks(sess *session.Session, vpcId string) ([]string, error) {
svc := ec2.New(sess)
input := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{
aws.String(vpcId),
},
},
},
}
result, err := svc.DescribeSubnets(input)
if err != nil {
return nil, err
}
blocks := make([]string, len(result.Subnets))
for i, subnet := range result.Subnets {
blocks[i] = *subnet.CidrBlock
}
return blocks, nil
}
func handleAwsErr(err error) {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
log.Fatal(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
log.Fatal(err.Error())
}
return
}
func bisectSubnet(subnet net.IPNet) (subnetA net.IPNet, subnetB net.IPNet) {
mask_size, total_size := subnet.Mask.Size()
var bit_to_switch byte = byte(total_size - mask_size - 1)
subnetA.Mask = make([]byte, 4)
copy(subnetA.Mask, subnet.Mask)
subnetA.Mask[3-(bit_to_switch/8)] |= (1 << (bit_to_switch % 8))
subnetA.IP = make([]byte, 4)
copy(subnetA.IP, subnet.IP)
subnetB.Mask = make([]byte, 4)
copy(subnetB.Mask, subnet.Mask)
subnetB.Mask[3-(bit_to_switch/8)] |= (1 << (bit_to_switch % 8))
subnetB.IP = make([]byte, 4)
copy(subnetB.IP, subnet.IP)
subnetB.IP[3-(bit_to_switch/8)] |= (1 << (bit_to_switch % 8))
return
}
func cidrSize(net net.IPNet) int {
size, _ := net.Mask.Size()
return size
}
func netInList(net *net.IPNet, list *[]net.IPNet) bool {
found := false
//log.Printf("Checking against %v\n", net.String())
for _, test := range *list {
//log.Printf("Testing %v\n", test.String())
if net.String() == test.String() {
found = true
}
}
return found
}
func netContainsNets(net *net.IPNet, list *[]net.IPNet) bool {
contains := false
for _, test := range *list {
if net.Contains(test.IP) {
contains = true
break
}
}
return contains
}
func FindBlock(vpc net.IPNet, occupied *[]net.IPNet, size int) (result net.IPNet) {
blocks := []net.IPNet{vpc}
//log.Printf("Looking for a block of size %v\n", size)
for len(blocks) > 0 {
//pop the first element off the queue
//log.Printf("blocks are set to %v\n", blocks)
block := blocks[0]
blocks = blocks[1:]
//block := blocks[len(blocks)-1]
//blocks = blocks[:len(blocks)-1]
if netInList(&block, occupied) {
//log.Printf("Found block %v in occupied list :-(", block.String())
continue
}
//log.Printf("block is size %v\n", cidrSize(block))
if cidrSize(block) == size {
//log.Println("Block is the right size")
if netContainsNets(&block, occupied) {
//log.Printf("Found one of the occupied nets within %v :-(", block.String())
continue
} else {
result = block
break
}
} else if cidrSize(block) < size {
left, right := bisectSubnet(block)
blocks = append(blocks, left)
blocks = append(blocks, right)
continue
}
}
return
}
func main() {
vpcId := flag.String("vpcid", "", "VPC ID to use")
size := flag.Int("size", 24, "Size of CIDR block to find")
flag.Parse()
if *vpcId == "" {
log.Fatal("Must specify a VPC ID")
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
vpcCidrBlock, err := getVPCCidrBlock(sess, *vpcId)
if err != nil {
handleAwsErr(err)
return
}
subnetCidrs, err := getVPCSubnetCidrBlocks(sess, *vpcId)
if err != nil {
handleAwsErr(err)
return
}
_, vpc, err := net.ParseCIDR(vpcCidrBlock)
if err != nil {
log.Fatal("error, %v\n", err)
} else {
subnets := make([]net.IPNet, len(subnetCidrs))
for i, cidr := range subnetCidrs {
_, sn, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal("error, %v\n", err)
}
subnets[i] = *sn
}
block := FindBlock(*vpc, &subnets, *size)
//fmt.Printf("VPC: %v\n", vpc)
//fmt.Printf("subnets: %v\n", subnetCidrs)
//fmt.Printf("found this block: %v\n", block.String())
fmt.Println(block.String())
}
}