-
Notifications
You must be signed in to change notification settings - Fork 22
/
inet.go
188 lines (169 loc) · 4.05 KB
/
inet.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
//nolint:revive
package schema
import (
"encoding"
"encoding/json"
"fmt"
"net"
"strings"
)
type InetTransformer interface {
TransformInet(*Inet) any
}
// workaround this Golang bug: https://github.com/golang/go/issues/35727
type inetWrapper struct {
IPNet *net.IPNet
Status Status
}
// Inet represents both inet and cidr PostgreSQL types.
type Inet struct {
IPNet *net.IPNet
Status Status
}
func (*Inet) Type() ValueType {
return TypeInet
}
func (dst *Inet) Size() int {
return len(dst.IPNet.IP) + len(dst.IPNet.Mask)
}
func (dst *Inet) GetStatus() Status {
return dst.Status
}
func (dst *Inet) Equal(src CQType) bool {
if src == nil {
return false
}
s, ok := src.(*Inet)
if !ok {
return false
}
return dst.Status == s.Status && dst.IPNet.String() == s.IPNet.String()
}
func (dst *Inet) String() string {
if dst.Status == Present {
return dst.IPNet.String()
} else {
return ""
}
}
func (dst *Inet) Set(src any) error {
if src == nil {
*dst = Inet{Status: Null}
return nil
}
if value, ok := src.(interface{ Get() any }); ok {
value2 := value.Get()
if value2 != value {
return dst.Set(value2)
}
}
switch value := src.(type) {
case net.IPNet:
*dst = Inet{IPNet: &value, Status: Present}
case net.IP:
if len(value) == 0 {
*dst = Inet{Status: Null}
} else {
bitCount := len(value) * 8
mask := net.CIDRMask(bitCount, bitCount)
*dst = Inet{IPNet: &net.IPNet{Mask: mask, IP: value}, Status: Present}
}
case string:
ip, ipnet, err := net.ParseCIDR(value)
if err != nil {
ip := net.ParseIP(value)
if ip == nil {
return fmt.Errorf("unable to parse inet address: %s", value)
}
if ipv4 := maybeGetIPv4(value, ip); ipv4 != nil {
ipnet = &net.IPNet{IP: ipv4, Mask: net.CIDRMask(32, 32)}
} else {
ipnet = &net.IPNet{IP: ip, Mask: net.CIDRMask(128, 128)}
}
} else {
ipnet.IP = ip
if ipv4 := maybeGetIPv4(value, ipnet.IP); ipv4 != nil {
ipnet.IP = ipv4
if len(ipnet.Mask) == 16 {
ipnet.Mask = ipnet.Mask[12:] // Not sure this is ever needed.
}
}
}
*dst = Inet{IPNet: ipnet, Status: Present}
case *net.IPNet:
if value == nil {
*dst = Inet{Status: Null}
} else {
return dst.Set(*value)
}
case *net.IP:
if value == nil {
*dst = Inet{Status: Null}
} else {
return dst.Set(*value)
}
case *string:
if value == nil {
*dst = Inet{Status: Null}
} else {
return dst.Set(*value)
}
default:
if tv, ok := src.(encoding.TextMarshaler); ok {
text, err := tv.MarshalText()
if err != nil {
return fmt.Errorf("cannot marshal %v: %w", value, err)
}
return dst.Set(string(text))
}
if sv, ok := src.(fmt.Stringer); ok {
return dst.Set(sv.String())
}
if originalSrc, ok := underlyingPtrType(src); ok {
return dst.Set(originalSrc)
}
return fmt.Errorf("cannot convert %v to Inet", value)
}
return nil
}
// Convert the net.IP to IPv4, if appropriate.
//
// When parsing a string to a net.IP using net.ParseIP() and the like, we get a
// 16 byte slice for IPv4 addresses as well as IPv6 addresses. This function
// calls To4() to convert them to a 4 byte slice. This is useful as it allows
// users of the net.IP check for IPv4 addresses based on the length and makes
// it clear we are handling IPv4 as opposed to IPv6 or IPv4-mapped IPv6
// addresses.
func maybeGetIPv4(input string, ip net.IP) net.IP {
// Do not do this if the provided input looks like IPv6. This is because
// To4() on IPv4-mapped IPv6 addresses converts them to IPv4, which behave
// different in some cases.
if strings.Contains(input, ":") {
return nil
}
return ip.To4()
}
// workaround this Golang bug: https://github.com/golang/go/issues/35727
func (dst *Inet) UnmarshalJSON(b []byte) error {
tmp := inetWrapper{}
if err := json.Unmarshal(b, &tmp); err != nil {
return err
}
*dst = Inet{Status: tmp.Status}
if dst.Status == Present {
if err := dst.Set(tmp.IPNet.String()); err != nil {
return err
}
}
return nil
}
func (dst Inet) Get() any {
switch dst.Status {
case Present:
return dst.IPNet
case Null:
return nil
default:
return dst.Status
}
}