Added support for building a packer buffer.
This commit is contained in:
parent
82b9e7be34
commit
604f1d0b03
|
@ -1,5 +1,10 @@
|
|||
package packer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//Packer -- contains both header and data
|
||||
type Packer struct {
|
||||
Header *packerHeader
|
||||
|
@ -13,3 +18,37 @@ func NewPacker() (p *Packer) {
|
|||
p.Data = newPackerData()
|
||||
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
|
||||
}
|
||||
|
|
|
@ -4,11 +4,16 @@ import "bytes"
|
|||
|
||||
//packerData -- Contains packer data
|
||||
type packerData struct {
|
||||
Buffer *bytes.Buffer
|
||||
buffer *bytes.Buffer
|
||||
}
|
||||
|
||||
func newPackerData() (p *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
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package packer
|
|||
import "net"
|
||||
import "fmt"
|
||||
|
||||
type addressFamily int
|
||||
|
||||
// packerHeader structure to hold our header information.
|
||||
type packerHeader struct {
|
||||
family addressFamily
|
||||
|
@ -11,15 +13,17 @@ type packerHeader struct {
|
|||
Service string
|
||||
}
|
||||
|
||||
type addressFamily int
|
||||
type addressFamilyString string
|
||||
|
||||
//Family -- ENUM for Address Family
|
||||
const (
|
||||
FamilyIPv4 addressFamily = iota
|
||||
FamilyIPv6
|
||||
)
|
||||
|
||||
var addressFamilyText = [...]string{
|
||||
"ipv4",
|
||||
"ipv6",
|
||||
}
|
||||
|
||||
func newPackerHeader() (p *packerHeader) {
|
||||
p = new(packerHeader)
|
||||
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) {
|
||||
address = p.address
|
||||
return
|
||||
|
@ -54,3 +72,8 @@ func (p *packerHeader) Family() (family addressFamily) {
|
|||
family = p.family
|
||||
return
|
||||
}
|
||||
|
||||
func (p *packerHeader) FamilyText() (familyText string) {
|
||||
familyText = addressFamilyText[p.family]
|
||||
return
|
||||
}
|
||||
|
|
|
@ -43,17 +43,12 @@ func Run() {
|
|||
loginfo.Println("startup")
|
||||
|
||||
p := packer.NewPacker()
|
||||
fmt.Println(*p.Header)
|
||||
|
||||
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(*p.Header)
|
||||
|
||||
fmt.Println(p.Header.Address())
|
||||
|
||||
loginfo.Println(p)
|
||||
fmt.Println("-=-=-=-=-=-=-=-=-=-=")
|
||||
|
||||
wssMapping = xlate.NewwssMapping()
|
||||
go wssMapping.Run()
|
||||
|
|
Loading…
Reference in New Issue