cleaned up a few of the packer functions

This commit is contained in:
tigerbot 2017-03-22 12:30:56 -06:00
parent ce512ece34
commit 8fb732e3b8
3 changed files with 36 additions and 42 deletions

View File

@ -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
} }

View File

@ -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 []byte(p.address)
return
} }
func (p *packerHeader) Address() (address net.IP) { func (p *packerHeader) AddressString() string {
address = p.address return p.address.String()
return
} }
func (p *packerHeader) Family() (family addressFamily) { func (p *packerHeader) Address() net.IP {
family = p.family return p.address
return
} }
func (p *packerHeader) FamilyText() (familyText string) { func (p *packerHeader) Family() addressFamily {
familyText = addressFamilyText[p.family] return p.family
return }
func (p *packerHeader) FamilyText() string {
return addressFamilyText[p.family]
} }

View File

@ -1,7 +1,9 @@
package packer package packer
import "log" import (
import "os" "log"
"os"
)
var ( var (
loginfo *log.Logger loginfo *log.Logger