This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wt_in6_addr.go
94 lines (74 loc) · 1.65 KB
/
wt_in6_addr.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
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
*/
package winipcfg
import (
"fmt"
"net"
)
// https://docs.microsoft.com/en-us/windows/desktop/api/in6addr/ns-in6addr-in6_addr
// IN6_ADDR defined in in6addr.h
type wtIn6Addr struct {
Byte [16]uint8 // Windows type [16]UCHAR
}
func (addr *wtIn6Addr) toNetIp() net.IP {
if addr == nil {
return nil
}
return net.IP{
byte(addr.Byte[0]),
byte(addr.Byte[1]),
byte(addr.Byte[2]),
byte(addr.Byte[3]),
byte(addr.Byte[4]),
byte(addr.Byte[5]),
byte(addr.Byte[6]),
byte(addr.Byte[7]),
byte(addr.Byte[8]),
byte(addr.Byte[9]),
byte(addr.Byte[10]),
byte(addr.Byte[11]),
byte(addr.Byte[12]),
byte(addr.Byte[13]),
byte(addr.Byte[14]),
byte(addr.Byte[15]),
}
}
// Compares two wtIn6Addr structs for equality. Note that the function will return false if either of structs is nil,
// even if the other is also nil.
func (addr *wtIn6Addr) equal(other *wtIn6Addr) bool {
if addr == nil || other == nil {
return false
}
for i := 0; i < 16; i++ {
if addr.Byte[i] != other.Byte[i] {
return false
}
}
return true
}
func (addr *wtIn6Addr) matches(ip *net.IP) bool {
if addr == nil {
return false
}
if len(*ip) != net.IPv6len || ip.To4() != nil {
return false
}
for i := 0; i < 16; i++ {
if addr.Byte[i] != (*ip)[i] {
return false
}
}
return true
}
func netIpToWtIn6Addr(ip net.IP) (*wtIn6Addr, error) {
if len(ip) != net.IPv6len || ip.To4() != nil {
return nil, fmt.Errorf("netIpToWtIn6Addr() requires IPv6 addresses")
}
in6_addr := wtIn6Addr{}
for i := 0; i < 16; i++ {
in6_addr.Byte[i] = ip[i]
}
return &in6_addr, nil
}