Added support for building a packer buffer.

This commit is contained in:
Henry Camacho 2017-02-13 18:47:16 -06:00
parent 82b9e7be34
commit 604f1d0b03
4 changed files with 76 additions and 14 deletions

View File

@ -1,5 +1,10 @@
package packer package packer
import (
"bytes"
"fmt"
)
//Packer -- contains both header and data //Packer -- contains both header and data
type Packer struct { type Packer struct {
Header *packerHeader Header *packerHeader
@ -13,3 +18,37 @@ func NewPacker() (p *Packer) {
p.Data = newPackerData() p.Data = newPackerData()
return return
} }
//PackV1 -- Outputs version 1 of packer
func (p *Packer) PackV1() (b bytes.Buffer) {
version := byte(1)
var headerBuf bytes.Buffer
headerBuf.WriteString(p.Header.FamilyText())
headerBuf.WriteString(",")
headerBuf.Write([]byte(p.Header.Address().String()))
headerBuf.WriteString(",")
headerBuf.WriteString(fmt.Sprintf("%d", p.Header.Port))
headerBuf.WriteString(",")
headerBuf.WriteString(fmt.Sprintf("%d", p.Data.buffer.Len()))
headerBuf.WriteString(",")
headerBuf.WriteString(p.Header.Service)
var metaBuf bytes.Buffer
metaBuf.WriteByte(byte(255) - version)
metaBuf.WriteByte(byte(headerBuf.Len()))
var buf bytes.Buffer
buf.Write(metaBuf.Bytes())
buf.Write(headerBuf.Bytes())
buf.Write(p.Data.buffer.Bytes())
fmt.Println("header: ", headerBuf.String())
fmt.Println("meta: ", metaBuf)
fmt.Println("Data: ", p.Data.buffer)
fmt.Println("Buffer: ", buf.Bytes())
b = buf
return
}

View File

@ -4,11 +4,16 @@ import "bytes"
//packerData -- Contains packer data //packerData -- Contains packer data
type packerData struct { type packerData struct {
Buffer *bytes.Buffer buffer *bytes.Buffer
} }
func newPackerData() (p *packerData) { func newPackerData() (p *packerData) {
p = new(packerData) p = new(packerData)
p.Buffer = new(bytes.Buffer) p.buffer = new(bytes.Buffer)
return
}
func (p packerData) AppendString(dataString string) (n int, err error) {
n, err = p.buffer.WriteString(dataString)
return return
} }

View File

@ -3,6 +3,8 @@ package packer
import "net" import "net"
import "fmt" import "fmt"
type addressFamily int
// packerHeader structure to hold our header information. // packerHeader structure to hold our header information.
type packerHeader struct { type packerHeader struct {
family addressFamily family addressFamily
@ -11,15 +13,17 @@ type packerHeader struct {
Service string Service string
} }
type addressFamily int
type addressFamilyString string
//Family -- ENUM for Address Family //Family -- ENUM for Address Family
const ( const (
FamilyIPv4 addressFamily = iota FamilyIPv4 addressFamily = iota
FamilyIPv6 FamilyIPv6
) )
var addressFamilyText = [...]string{
"ipv4",
"ipv6",
}
func newPackerHeader() (p *packerHeader) { func newPackerHeader() (p *packerHeader) {
p = new(packerHeader) p = new(packerHeader)
p.SetAddress("127.0.0.1") p.SetAddress("127.0.0.1")
@ -45,6 +49,20 @@ func (p *packerHeader) SetAddress(addr string) {
} }
} }
func (p *packerHeader) AddressBytes() (b []byte) {
b = make([]byte, 16)
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) { func (p *packerHeader) Address() (address net.IP) {
address = p.address address = p.address
return return
@ -54,3 +72,8 @@ func (p *packerHeader) Family() (family addressFamily) {
family = p.family family = p.family
return return
} }
func (p *packerHeader) FamilyText() (familyText string) {
familyText = addressFamilyText[p.family]
return
}

View File

@ -43,17 +43,12 @@ func Run() {
loginfo.Println("startup") loginfo.Println("startup")
p := packer.NewPacker() p := packer.NewPacker()
fmt.Println(*p.Header)
p.Header.SetAddress("127.0.0.2") p.Header.SetAddress("127.0.0.2")
fmt.Println(*p.Header) p.Header.Port = 32768
p.Data.AppendString("A test message")
p.PackV1()
p.Header.SetAddress("2001:db8::1") fmt.Println("-=-=-=-=-=-=-=-=-=-=")
fmt.Println(*p.Header)
fmt.Println(p.Header.Address())
loginfo.Println(p)
wssMapping = xlate.NewwssMapping() wssMapping = xlate.NewwssMapping()
go wssMapping.Run() go wssMapping.Run()