/
inet.go
200 lines (157 loc) · 4.28 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
189
190
191
192
193
194
195
196
197
198
199
200
package pgtype
import (
"bytes"
"database/sql/driver"
"errors"
"fmt"
"net/netip"
)
// Network address family is dependent on server socket.h value for AF_INET.
// In practice, all platforms appear to have the same value. See
// src/include/utils/inet.h for more information.
const (
defaultAFInet = 2
defaultAFInet6 = 3
)
type NetipPrefixScanner interface {
ScanNetipPrefix(v netip.Prefix) error
}
type NetipPrefixValuer interface {
NetipPrefixValue() (netip.Prefix, error)
}
// InetCodec handles both inet and cidr PostgreSQL types. The preferred Go types are netip.Prefix and netip.Addr. If
// IsValid() is false then they are treated as SQL NULL.
type InetCodec struct{}
func (InetCodec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (InetCodec) PreferredFormat() int16 {
return BinaryFormatCode
}
func (InetCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan {
if _, ok := value.(NetipPrefixValuer); !ok {
return nil
}
switch format {
case BinaryFormatCode:
return encodePlanInetCodecBinary{}
case TextFormatCode:
return encodePlanInetCodecText{}
}
return nil
}
type encodePlanInetCodecBinary struct{}
func (encodePlanInetCodecBinary) Encode(value any, buf []byte) (newBuf []byte, err error) {
prefix, err := value.(NetipPrefixValuer).NetipPrefixValue()
if err != nil {
return nil, err
}
if !prefix.IsValid() {
return nil, nil
}
var family byte
if prefix.Addr().Is4() {
family = defaultAFInet
} else {
family = defaultAFInet6
}
buf = append(buf, family)
ones := prefix.Bits()
buf = append(buf, byte(ones))
// is_cidr is ignored on server
buf = append(buf, 0)
if family == defaultAFInet {
buf = append(buf, byte(4))
b := prefix.Addr().As4()
buf = append(buf, b[:]...)
} else {
buf = append(buf, byte(16))
b := prefix.Addr().As16()
buf = append(buf, b[:]...)
}
return buf, nil
}
type encodePlanInetCodecText struct{}
func (encodePlanInetCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
prefix, err := value.(NetipPrefixValuer).NetipPrefixValue()
if err != nil {
return nil, err
}
if !prefix.IsValid() {
return nil, nil
}
return append(buf, prefix.String()...), nil
}
func (InetCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
switch format {
case BinaryFormatCode:
switch target.(type) {
case NetipPrefixScanner:
return scanPlanBinaryInetToNetipPrefixScanner{}
}
case TextFormatCode:
switch target.(type) {
case NetipPrefixScanner:
return scanPlanTextAnyToNetipPrefixScanner{}
}
}
return nil
}
func (c InetCodec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) {
return codecDecodeToTextFormat(c, m, oid, format, src)
}
func (c InetCodec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) {
if src == nil {
return nil, nil
}
var prefix netip.Prefix
err := codecScan(c, m, oid, format, src, (*netipPrefixWrapper)(&prefix))
if err != nil {
return nil, err
}
if !prefix.IsValid() {
return nil, nil
}
return prefix, nil
}
type scanPlanBinaryInetToNetipPrefixScanner struct{}
func (scanPlanBinaryInetToNetipPrefixScanner) Scan(src []byte, dst any) error {
scanner := (dst).(NetipPrefixScanner)
if src == nil {
return scanner.ScanNetipPrefix(netip.Prefix{})
}
if len(src) != 8 && len(src) != 20 {
return fmt.Errorf("Received an invalid size for an inet: %d", len(src))
}
// ignore family
bits := src[1]
// ignore is_cidr
// ignore addressLength - implicit in length of message
addr, ok := netip.AddrFromSlice(src[4:])
if !ok {
return errors.New("netip.AddrFromSlice failed")
}
return scanner.ScanNetipPrefix(netip.PrefixFrom(addr, int(bits)))
}
type scanPlanTextAnyToNetipPrefixScanner struct{}
func (scanPlanTextAnyToNetipPrefixScanner) Scan(src []byte, dst any) error {
scanner := (dst).(NetipPrefixScanner)
if src == nil {
return scanner.ScanNetipPrefix(netip.Prefix{})
}
var prefix netip.Prefix
if bytes.IndexByte(src, '/') == -1 {
addr, err := netip.ParseAddr(string(src))
if err != nil {
return err
}
prefix = netip.PrefixFrom(addr, addr.BitLen())
} else {
var err error
prefix, err = netip.ParsePrefix(string(src))
if err != nil {
return err
}
}
return scanner.ScanNetipPrefix(prefix)
}