-
Notifications
You must be signed in to change notification settings - Fork 4
/
iputil.go
126 lines (105 loc) · 2.94 KB
/
iputil.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
package vpnlib
import (
"fmt"
"net"
)
const (
ipv4HeaderLengthBytes = 20
ipv6HeaderLengthBytes = 40
)
func cidrToUsableIPs(cidr string) ([]string, uint8, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, 0, fmt.Errorf("invalid cidr range: %v", err)
}
subnetSize, _ := ipnet.Mask.Size()
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); incIp(ip) {
ips = append(ips, ip.String())
}
// remove network and broadcast addresses
if len(ips) > 2 {
ips = ips[1 : len(ips)-1]
}
return ips, uint8(subnetSize), nil
}
func incIp(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func parseIpFromPacketHeader(packet []byte) (net.IP, net.IP) {
return net.IP(packet[12:16]), net.IP(packet[16:20])
}
func validateIPv4(packet []byte) error {
if len(packet) < ipv4HeaderLengthBytes {
return fmt.Errorf("packet too short")
}
ipVersion := (packet[0] & 0xF0) >> 4
if ipVersion != 4 {
return fmt.Errorf("packet header advertises non IPv4, version: %d", ipVersion)
}
return nil
}
func validateIPv6(packet []byte) error {
if len(packet) < ipv6HeaderLengthBytes {
return fmt.Errorf("packet too short for IPv6")
}
ipVersion := (packet[0] & 0xF0) >> 4
if ipVersion != 6 {
return fmt.Errorf("packet header advertises non IPv6, version: %d", ipVersion)
}
return nil
}
// Returns a list of interfaces on which this IP network was found
func GetLocalInterfacesForIp(ipAddress string) ([]string, error) {
//create a list of network interfaces, so we can return those if a match is found
networkInterfaces := []string{}
targetIP := net.ParseIP(ipAddress)
if targetIP == nil {
return networkInterfaces, fmt.Errorf("invalid IP address")
}
interfaces, err := net.Interfaces()
if err != nil {
return networkInterfaces, fmt.Errorf("failed to get local network interfaces: %v", err)
}
for _, iface := range interfaces {
// Only consider interfaces that are up
if iface.Flags&net.FlagUp == 0 {
continue
}
addresses, err := iface.Addrs()
if err != nil {
return networkInterfaces, fmt.Errorf("failed to get addresses for interface %v: %v", iface.Name, err)
}
for _, address := range addresses {
_, ipNet, err := net.ParseCIDR(address.String())
if err != nil {
continue
}
if ipNet.Contains(targetIP) {
// append iface.Name to list of interfaces
networkInterfaces = append(networkInterfaces, iface.Name)
}
}
}
return networkInterfaces, nil
}
// IsIPInCIDR checks if an IP address is in a CIDR range.
func IsIPInCIDR(ipStr, cidrStr string) (bool, error) {
// Parse the IP address.
ip := net.ParseIP(ipStr)
if ip == nil {
return false, fmt.Errorf("invalid IP address: %s", ipStr)
}
// Parse the CIDR prefix.
_, cidr, err := net.ParseCIDR(cidrStr)
if err != nil {
return false, fmt.Errorf("invalid CIDR notation: %s", cidrStr)
}
// Use the Contains method to check if the CIDR contains the IP.
return cidr.Contains(ip), nil
}