2015-09-23 16:25:22 +08:00
|
|
|
package iplist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2018-02-11 12:11:26 +08:00
|
|
|
"fmt"
|
2015-09-23 16:25:22 +08:00
|
|
|
"io"
|
|
|
|
"net"
|
2016-04-04 14:23:05 +08:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/edsrzf/mmap-go"
|
2015-09-23 16:25:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// The packed format is an 8 byte integer of the number of ranges. Then 20
|
|
|
|
// bytes per range, consisting of 4 byte packed IP being the lower bound IP of
|
|
|
|
// the range, then 4 bytes of the upper, inclusive bound, 8 bytes for the
|
|
|
|
// offset of the description from the end of the packed ranges, and 4 bytes
|
|
|
|
// for the length of the description. After these packed ranges, are the
|
|
|
|
// concatenated descriptions.
|
|
|
|
|
|
|
|
const (
|
|
|
|
packedRangesOffset = 8
|
2018-02-11 12:11:26 +08:00
|
|
|
packedRangeLen = 44
|
2015-09-23 16:25:22 +08:00
|
|
|
)
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ipl *IPList) WritePacked(w io.Writer) (err error) {
|
|
|
|
descOffsets := make(map[string]int64, len(ipl.ranges))
|
|
|
|
descs := make([]string, 0, len(ipl.ranges))
|
2015-09-23 16:25:22 +08:00
|
|
|
var nextOffset int64
|
|
|
|
// This is a little monadic, no?
|
|
|
|
write := func(b []byte, expectedLen int) {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var n int
|
|
|
|
n, err = w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if n != expectedLen {
|
|
|
|
panic(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var b [8]byte
|
2016-04-19 12:11:11 +08:00
|
|
|
binary.LittleEndian.PutUint64(b[:], uint64(len(ipl.ranges)))
|
2015-09-23 16:25:22 +08:00
|
|
|
write(b[:], 8)
|
2016-04-19 12:11:11 +08:00
|
|
|
for _, r := range ipl.ranges {
|
2018-02-11 12:11:26 +08:00
|
|
|
write(r.First.To16(), 16)
|
|
|
|
write(r.Last.To16(), 16)
|
2015-09-23 16:25:22 +08:00
|
|
|
descOff, ok := descOffsets[r.Description]
|
|
|
|
if !ok {
|
|
|
|
descOff = nextOffset
|
|
|
|
descOffsets[r.Description] = descOff
|
|
|
|
descs = append(descs, r.Description)
|
|
|
|
nextOffset += int64(len(r.Description))
|
|
|
|
}
|
|
|
|
binary.LittleEndian.PutUint64(b[:], uint64(descOff))
|
|
|
|
write(b[:], 8)
|
|
|
|
binary.LittleEndian.PutUint32(b[:], uint32(len(r.Description)))
|
|
|
|
write(b[:4], 4)
|
|
|
|
}
|
|
|
|
for _, d := range descs {
|
|
|
|
write([]byte(d), len(d))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFromPacked(b []byte) PackedIPList {
|
2018-02-11 12:11:26 +08:00
|
|
|
ret := PackedIPList(b)
|
|
|
|
minLen := packedRangesOffset + ret.len()*packedRangeLen
|
|
|
|
if len(b) < minLen {
|
|
|
|
panic(fmt.Sprintf("packed len %d < %d", len(b), minLen))
|
|
|
|
}
|
|
|
|
return ret
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type PackedIPList []byte
|
|
|
|
|
|
|
|
var _ Ranger = PackedIPList{}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (pil PackedIPList) len() int {
|
|
|
|
return int(binary.LittleEndian.Uint64(pil[:8]))
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (pil PackedIPList) NumRanges() int {
|
|
|
|
return pil.len()
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (pil PackedIPList) getFirst(i int) net.IP {
|
2015-10-18 21:00:26 +08:00
|
|
|
off := packedRangesOffset + packedRangeLen*i
|
2018-02-11 12:11:26 +08:00
|
|
|
return net.IP(pil[off : off+16])
|
2015-10-18 21:00:26 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (pil PackedIPList) getRange(i int) (ret Range) {
|
2015-09-23 16:25:22 +08:00
|
|
|
rOff := packedRangesOffset + packedRangeLen*i
|
2018-02-11 12:11:26 +08:00
|
|
|
last := pil[rOff+16 : rOff+32]
|
|
|
|
descOff := int(binary.LittleEndian.Uint64(pil[rOff+32:]))
|
|
|
|
descLen := int(binary.LittleEndian.Uint32(pil[rOff+40:]))
|
2016-04-19 12:11:11 +08:00
|
|
|
descOff += packedRangesOffset + packedRangeLen*pil.len()
|
2015-10-18 21:00:26 +08:00
|
|
|
ret = Range{
|
2016-04-19 12:11:11 +08:00
|
|
|
pil.getFirst(i),
|
2015-10-18 21:00:26 +08:00
|
|
|
net.IP(last),
|
2016-04-19 12:11:11 +08:00
|
|
|
string(pil[descOff : descOff+descLen]),
|
2015-10-18 21:00:26 +08:00
|
|
|
}
|
2015-09-23 16:25:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (pil PackedIPList) Lookup(ip net.IP) (r Range, ok bool) {
|
2018-02-11 12:11:26 +08:00
|
|
|
ip16 := ip.To16()
|
|
|
|
if ip16 == nil {
|
|
|
|
panic(ip)
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
2018-02-11 12:11:26 +08:00
|
|
|
return lookup(pil.getFirst, pil.getRange, pil.len(), ip16)
|
2015-09-23 16:25:22 +08:00
|
|
|
}
|
2016-04-04 14:23:05 +08:00
|
|
|
|
2018-02-08 20:57:35 +08:00
|
|
|
type closerFunc func() error
|
|
|
|
|
|
|
|
func (me closerFunc) Close() error {
|
|
|
|
return me()
|
|
|
|
}
|
|
|
|
|
|
|
|
func MMapPackedFile(filename string) (
|
|
|
|
ret interface {
|
|
|
|
Ranger
|
|
|
|
io.Closer
|
|
|
|
},
|
|
|
|
err error,
|
|
|
|
) {
|
2016-04-04 14:23:05 +08:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
mm, err := mmap.Map(f, mmap.RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-02-08 20:57:35 +08:00
|
|
|
ret = struct {
|
|
|
|
Ranger
|
|
|
|
io.Closer
|
|
|
|
}{NewFromPacked(mm), closerFunc(mm.Unmap)}
|
2016-04-04 14:23:05 +08:00
|
|
|
return
|
|
|
|
}
|