forked from fiorix/go-diameter
/
ipv4.go
49 lines (40 loc) · 978 Bytes
/
ipv4.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
// Copyright 2013-2015 go-diameter authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package datatype
import (
"fmt"
"net"
)
// IPv4 data type for Framed-IP-Address AVP.
type IPv4 net.IP
// DecodeIPv4 decodes an IPv4 data type from byte array.
func DecodeIPv4(b []byte) (Type, error) {
if len(b) != 4 {
return IPv4{0, 0, 0, 0}, nil
}
return IPv4(b), nil
}
// Serialize implements the Type interface.
func (ip IPv4) Serialize() []byte {
if ip4 := net.IP(ip).To4(); ip4 != nil {
return ip4
}
return ip
}
// Len implements the Type interface.
func (ip IPv4) Len() int {
return 4
}
// Padding implements the Type interface.
func (ip IPv4) Padding() int {
return 0
}
// Type implements the Type interface.
func (ip IPv4) Type() TypeID {
return IPv4Type
}
// String implements the Type interface.
func (ip IPv4) String() string {
return fmt.Sprintf("IPv4{%s}", net.IP(ip))
}