aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/ipv4.go
blob: bdc83f5eefa35cf2d37823d24418eff051e11e23 (plain) (blame)
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
package proto

import (
	"encoding/binary"
	"net/netip"
)

// IPv4 represents IPv4 address as uint32 number.
//
// Not using netip.Addr because uint32 is 5 times faster,
// consumes 6 times less memory and better represents IPv4.
//
// Use ToIP helper for convenience.
type IPv4 uint32

func (v IPv4) String() string {
	return v.ToIP().String()
}

// ToIP represents IPv4 as netaddr.IP.
func (v IPv4) ToIP() netip.Addr {
	var buf [4]byte
	binary.BigEndian.PutUint32(buf[:], uint32(v))
	return netip.AddrFrom4(buf)
}

// ToIPv4 represents ip as IPv4. Panics if ip is not ipv4.
func ToIPv4(ip netip.Addr) IPv4 {
	b := ip.As4()
	return IPv4(binary.BigEndian.Uint32(b[:]))
}