/
inet_convert.go
361 lines (294 loc) · 9.39 KB
/
inet_convert.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package function
import (
"encoding/binary"
"fmt"
"net"
"reflect"
"strings"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/go-mysql-server/sql"
)
type InetAton struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*InetAton)(nil)
var _ sql.CollationCoercible = (*InetAton)(nil)
func NewInetAton(val sql.Expression) sql.Expression {
return &InetAton{expression.UnaryExpression{Child: val}}
}
// FunctionName implements sql.FunctionExpression
func (i *InetAton) FunctionName() string {
return "inet_aton"
}
// Description implements sql.FunctionExpression
func (i *InetAton) Description() string {
return "returns the numeric value of an IP address."
}
func (i *InetAton) String() string {
return fmt.Sprintf("%s(%s)", i.FunctionName(), i.Child.String())
}
func (i *InetAton) Type() sql.Type {
return types.Uint32
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*InetAton) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
func (i *InetAton) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewInetAton(children[0]), nil
}
func (i *InetAton) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Evaluate value
val, err := i.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
// Return null if given null
if val == nil {
return nil, nil
}
// Expect to receive an IP address, so convert val into string
ipstr, err := types.ConvertToString(val, types.LongText)
if err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(val).String())
}
// Parse IP address
ip := net.ParseIP(ipstr)
if ip == nil {
// Failed to Parse IP correctly
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%s'' for function %s", ipstr, i.FunctionName()))
return nil, nil
}
// Expect an IPv4 address
ipv4 := ip.To4()
if ipv4 == nil {
// Received invalid IPv4 address (IPv6 address are invalid)
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%s'' for function %s", ipstr, i.FunctionName()))
return nil, nil
}
// Return IPv4 address as uint32
ipv4int := binary.BigEndian.Uint32(ipv4)
return ipv4int, nil
}
type Inet6Aton struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*Inet6Aton)(nil)
var _ sql.CollationCoercible = (*Inet6Aton)(nil)
func NewInet6Aton(val sql.Expression) sql.Expression {
return &Inet6Aton{expression.UnaryExpression{Child: val}}
}
// FunctionName implements sql.FunctionExpression
func (i *Inet6Aton) FunctionName() string {
return "inet6_aton"
}
// Description implements sql.FunctionExpression
func (i *Inet6Aton) Description() string {
return "returns the numeric value of an IPv6 address."
}
func (i *Inet6Aton) String() string {
return fmt.Sprintf("%s(%s)", i.FunctionName(), i.Child.String())
}
func (i *Inet6Aton) Type() sql.Type {
return types.LongBlob
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*Inet6Aton) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 4
}
func (i *Inet6Aton) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewInet6Aton(children[0]), nil
}
func (i *Inet6Aton) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Evaluate value
val, err := i.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
// Return null if given null
if val == nil {
return nil, nil
}
// Parse IP address
ipstr := val.(string)
ip := net.ParseIP(ipstr)
if ip == nil {
// Failed to Parse IP correctly
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%s'' for function %s", ipstr, i.FunctionName()))
return nil, nil
}
// if it doesn't contain colons, treat it as ipv4
if strings.Count(val.(string), ":") < 2 {
ipv4 := ip.To4()
return []byte(ipv4), nil
}
// Received IPv6 address
ipv6 := ip.To16()
if ipv6 == nil {
// Invalid IPv6 address
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%s'' for function %s", ipstr, i.FunctionName()))
return nil, nil
}
// Return as []byte
return []byte(ipv6), nil
}
type InetNtoa struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*InetNtoa)(nil)
var _ sql.CollationCoercible = (*InetNtoa)(nil)
func NewInetNtoa(val sql.Expression) sql.Expression {
return &InetNtoa{expression.UnaryExpression{Child: val}}
}
// FunctionName implements sql.FunctionExpression
func (i *InetNtoa) FunctionName() string {
return "inet_ntoa"
}
// Description implements sql.FunctionExpression
func (i *InetNtoa) Description() string {
return "returns the IP address from a numeric value."
}
func (i *InetNtoa) String() string {
return fmt.Sprintf("%s(%s)", i.FunctionName(), i.Child.String())
}
func (i *InetNtoa) Type() sql.Type {
return types.LongText
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*InetNtoa) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return ctx.GetCollation(), 4
}
func (i *InetNtoa) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewInetNtoa(children[0]), nil
}
func (i *InetNtoa) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Evaluate value
val, err := i.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
// Return null if given null
if val == nil {
return nil, nil
}
// Convert val into int
ipv4int, _, err := types.Int32.Convert(val)
if ipv4int != nil && err != nil {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(val).String())
}
// Received a hex string instead of int
if ipv4int == nil {
// Create new IPv4
var ipv4 net.IP = []byte{0, 0, 0, 0}
return ipv4.String(), nil
}
// Create new IPv4, and fill with val
ipv4 := make(net.IP, 4)
binary.BigEndian.PutUint32(ipv4, uint32(ipv4int.(int32)))
return ipv4.String(), nil
}
type Inet6Ntoa struct {
expression.UnaryExpression
}
var _ sql.FunctionExpression = (*Inet6Ntoa)(nil)
var _ sql.CollationCoercible = (*Inet6Ntoa)(nil)
func NewInet6Ntoa(val sql.Expression) sql.Expression {
return &Inet6Ntoa{expression.UnaryExpression{Child: val}}
}
// FunctionName implements sql.FunctionExpression
func (i *Inet6Ntoa) FunctionName() string {
return "inet6_ntoa"
}
// Description implements sql.FunctionExpression
func (i *Inet6Ntoa) Description() string {
return "returns the IPv6 address from a numeric value."
}
func (i *Inet6Ntoa) String() string {
return fmt.Sprintf("%s(%s)", i.FunctionName(), i.Child.String())
}
func (i *Inet6Ntoa) Type() sql.Type {
return types.LongText
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*Inet6Ntoa) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return ctx.GetCollation(), 4
}
func (i *Inet6Ntoa) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 1)
}
return NewInet6Ntoa(children[0]), nil
}
func (i *Inet6Ntoa) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Evaluate value
val, err := i.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
// Return null if given null
if val == nil {
return nil, nil
}
// Only convert if received string as input
switch val.(type) {
case []byte:
ipbytes := val.([]byte)
// Exactly 4 bytes, treat as IPv4 address
if len(ipbytes) == 4 {
var ipv4 net.IP = ipbytes
return ipv4.String(), nil
}
// There must be exactly 4 or 16 bytes (len == 4 satisfied above)
if len(ipbytes) != 16 {
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%s'' for function %s", string(val.([]byte)), i.FunctionName()))
return nil, nil
}
// Check to see if it should be printed as IPv6; non-zero within first 10 bytes
for _, b := range ipbytes[:10] {
if b != 0 {
// Create new IPv6
var ipv6 net.IP = ipbytes
return ipv6.String(), nil
}
}
// IPv4-compatible (12 bytes of 0x00)
if ipbytes[10] == 0 && ipbytes[11] == 0 && (ipbytes[12] != 0 || ipbytes[13] != 0) {
var ipv4 net.IP = ipbytes[12:]
return "::" + ipv4.String(), nil
}
// IPv4-mapped (10 bytes of 0x00 followed by 2 bytes of 0xFF)
if ipbytes[10] == 0xFF && ipbytes[11] == 0xFF {
var ipv4 net.IP = ipbytes[12:]
return "::ffff:" + ipv4.String(), nil
}
// Print as IPv6 by default
var ipv6 net.IP = ipbytes
return ipv6.String(), nil
default:
ctx.Warn(1411, fmt.Sprintf("Incorrect string value: ''%v'' for function %s", val, i.FunctionName()))
return nil, nil
}
}