cleaned up a few of the packer functions
This commit is contained in:
parent
ce512ece34
commit
8fb732e3b8
|
@ -1,6 +1,8 @@
|
||||||
package packer
|
package packer
|
||||||
|
|
||||||
import "bytes"
|
import (
|
||||||
|
"bytes"
|
||||||
|
)
|
||||||
|
|
||||||
//packerData -- Contains packer data
|
//packerData -- Contains packer data
|
||||||
type packerData struct {
|
type packerData struct {
|
||||||
|
@ -14,18 +16,15 @@ func newPackerData() (p *packerData) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerData) AppendString(dataString string) (n int, err error) {
|
func (p *packerData) AppendString(dataString string) (int, error) {
|
||||||
n, err = p.buffer.WriteString(dataString)
|
return p.buffer.WriteString(dataString)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerData) AppendBytes(dataBytes []byte) (n int, err error) {
|
func (p *packerData) AppendBytes(dataBytes []byte) (int, error) {
|
||||||
n, err = p.buffer.Write(dataBytes)
|
return p.buffer.Write(dataBytes)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Data --
|
//Data --
|
||||||
func (p *packerData) Data() (b []byte) {
|
func (p *packerData) Data() []byte {
|
||||||
b = p.buffer.Bytes()
|
return p.buffer.Bytes()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package packer
|
package packer
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
import "fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
type addressFamily int
|
type addressFamily int
|
||||||
|
|
||||||
|
@ -37,45 +39,36 @@ func newPackerHeader() (p *packerHeader) {
|
||||||
//SetAddress -- Set Address. which sets address family automatically
|
//SetAddress -- Set Address. which sets address family automatically
|
||||||
func (p *packerHeader) SetAddress(addr string) {
|
func (p *packerHeader) SetAddress(addr string) {
|
||||||
p.address = net.ParseIP(addr)
|
p.address = net.ParseIP(addr)
|
||||||
err := p.address.To4()
|
|
||||||
|
|
||||||
if err != nil {
|
if p.address.To4() != nil {
|
||||||
p.family = FamilyIPv4
|
p.family = FamilyIPv4
|
||||||
} else {
|
} else if p.address.To16() != nil {
|
||||||
err := p.address.To16()
|
|
||||||
if err != nil {
|
|
||||||
p.family = FamilyIPv6
|
p.family = FamilyIPv6
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Sprintf("setAddress does not support %s", addr))
|
panic(fmt.Sprintf("setAddress does not support %q", addr))
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) AddressBytes() (b []byte) {
|
func (p *packerHeader) AddressBytes() []byte {
|
||||||
b = make([]byte, 16)
|
if ip4 := p.address.To4(); ip4 != nil {
|
||||||
|
p.address = ip4
|
||||||
switch {
|
|
||||||
case p.address.To4() != nil:
|
|
||||||
b = make([]byte, 4)
|
|
||||||
for pos := range b {
|
|
||||||
b[pos] = p.address[pos+12]
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) Address() (address net.IP) {
|
return []byte(p.address)
|
||||||
address = p.address
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) Family() (family addressFamily) {
|
func (p *packerHeader) AddressString() string {
|
||||||
family = p.family
|
return p.address.String()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) FamilyText() (familyText string) {
|
func (p *packerHeader) Address() net.IP {
|
||||||
familyText = addressFamilyText[p.family]
|
return p.address
|
||||||
return
|
}
|
||||||
|
|
||||||
|
func (p *packerHeader) Family() addressFamily {
|
||||||
|
return p.family
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *packerHeader) FamilyText() string {
|
||||||
|
return addressFamilyText[p.family]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package packer
|
package packer
|
||||||
|
|
||||||
import "log"
|
import (
|
||||||
import "os"
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
loginfo *log.Logger
|
loginfo *log.Logger
|
||||||
|
|
Loading…
Reference in New Issue