2017-02-12 04:13:29 +00:00
|
|
|
package packer
|
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
)
|
2017-02-12 04:13:29 +00:00
|
|
|
|
2017-02-14 00:47:16 +00:00
|
|
|
type addressFamily int
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
// packerHeader structure to hold our header information.
|
|
|
|
type packerHeader struct {
|
2017-03-22 21:03:58 +00:00
|
|
|
family addressFamily
|
|
|
|
address net.IP
|
|
|
|
Port int
|
|
|
|
Service string
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Family -- ENUM for Address Family
|
|
|
|
const (
|
|
|
|
FamilyIPv4 addressFamily = iota
|
|
|
|
FamilyIPv6
|
|
|
|
)
|
|
|
|
|
2017-02-14 00:47:16 +00:00
|
|
|
var addressFamilyText = [...]string{
|
2017-02-18 21:21:36 +00:00
|
|
|
"IPv4",
|
|
|
|
"IPv6",
|
2017-02-14 00:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 04:13:29 +00:00
|
|
|
func newPackerHeader() (p *packerHeader) {
|
|
|
|
p = new(packerHeader)
|
|
|
|
p.SetAddress("127.0.0.1")
|
|
|
|
p.Port = 65535
|
|
|
|
p.Service = "na"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//SetAddress -- Set Address. which sets address family automatically
|
|
|
|
func (p *packerHeader) SetAddress(addr string) {
|
|
|
|
p.address = net.ParseIP(addr)
|
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
if p.address.To4() != nil {
|
2017-02-12 04:13:29 +00:00
|
|
|
p.family = FamilyIPv4
|
2017-03-22 18:30:56 +00:00
|
|
|
} else if p.address.To16() != nil {
|
|
|
|
p.family = FamilyIPv6
|
2017-02-12 04:13:29 +00:00
|
|
|
} else {
|
2017-03-22 18:30:56 +00:00
|
|
|
panic(fmt.Sprintf("setAddress does not support %q", addr))
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
func (p *packerHeader) AddressBytes() []byte {
|
|
|
|
if ip4 := p.address.To4(); ip4 != nil {
|
|
|
|
p.address = ip4
|
2017-02-14 00:47:16 +00:00
|
|
|
}
|
2017-03-22 18:30:56 +00:00
|
|
|
|
|
|
|
return []byte(p.address)
|
2017-02-14 00:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
func (p *packerHeader) AddressString() string {
|
|
|
|
return p.address.String()
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
func (p *packerHeader) Address() net.IP {
|
|
|
|
return p.address
|
2017-02-12 04:13:29 +00:00
|
|
|
}
|
2017-02-14 00:47:16 +00:00
|
|
|
|
2017-03-22 18:30:56 +00:00
|
|
|
func (p *packerHeader) Family() addressFamily {
|
|
|
|
return p.family
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *packerHeader) FamilyText() string {
|
|
|
|
return addressFamilyText[p.family]
|
2017-02-14 00:47:16 +00:00
|
|
|
}
|