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
/
anycast_ip_address_row.go
117 lines (88 loc) · 2.75 KB
/
anycast_ip_address_row.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
/* SPDX-License-Identifier: MIT
*
* Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
*/
package winipcfg
import (
"fmt"
"net"
)
// Corresponds to MIB_ANYCASTIPADDRESS_ROW defined in netioapi.h
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/ns-netioapi-_mib_anycastipaddress_row).
type AnycastIpAddressRow struct {
//
// Key Structure.
//
Address SockaddrInet
InterfaceLuid uint64
InterfaceIndex uint32
//
// Read-Only Fields.
//
ScopeId uint32
}
// Returns all anycast IP addresses from the system. GetAnycastIpAddressTable function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getanycastipaddresstable).
func GetAnycastIpAddressRows(family AddressFamily) ([]*AnycastIpAddressRow, error) {
rows, err := getWtMibAnycastipaddressRows(family)
if err != nil {
return nil, err
}
length := len(rows)
addresses := make([]*AnycastIpAddressRow, length, length)
for idx, row := range rows {
address, err := row.toAnycastIpAddressRow()
if err != nil {
return nil, err
}
addresses[idx] = address
}
return addresses, nil
}
// Returns anycast IP address specified by the input criteria. Corresponds to GetAnycastIpAddressEntry function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-getanycastipaddressentry).
func GetAnycastIpAddressRow(interfaceLuid uint64, ip *net.IP) (*AnycastIpAddressRow, error) {
row, err := getWtMibAnycastipaddressRowAlt(interfaceLuid, ip)
if err != nil {
return nil, err
}
return row.toAnycastIpAddressRow()
}
// Adds new anycast IP address to system. Corresponds to CreateAnycastIpAddressEntry function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-createanycastipaddressentry)
func (aia *AnycastIpAddressRow) Add() error {
wtsainet, err := aia.Address.toWtSockaddrInet()
if err != nil {
return err
}
row := wtMibAnycastipaddressRow{
Address: *wtsainet,
InterfaceLuid: aia.InterfaceLuid,
InterfaceIndex: aia.InterfaceIndex,
}
return row.add()
}
// Deletes anycast IP address from the system. Corresponds to DeleteAnycastIpAddressEntry function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteanycastipaddressentry).
func (aia *AnycastIpAddressRow) Delete() error {
wtsainet, err := aia.Address.toWtSockaddrInet()
if err != nil {
return err
}
row, err := getWtMibAnycastipaddressRow(aia.InterfaceLuid, wtsainet)
if err == nil {
return row.delete()
} else {
return err
}
}
func (aia *AnycastIpAddressRow) String() string {
if aia == nil {
return "nil"
} else {
return fmt.Sprintf(`Address: %s
InterfaceLuid: %d
InterfaceIndex: %d
ScopeId: %d`, aia.Address.String(), aia.InterfaceLuid, aia.InterfaceIndex, aia.ScopeId)
}
}