More types.

This commit is contained in:
2020-12-23 23:21:47 +01:00
parent a1261c1b8f
commit 7ecbae27c2
10 changed files with 138 additions and 23 deletions

20
main.go
View File

@@ -1,8 +1,10 @@
package main
import (
"bytes"
"context"
"fmt"
"io"
"ippserver/packages/ipp"
"ippserver/packages/mdnsserver"
"net/http"
@@ -40,10 +42,24 @@ func handle(w http.ResponseWriter, r *http.Request) {
// log.Infof("Body %x", body)
request := ipp.NewRequest()
request.UnMarshal(r.Body)
rdata := make([]byte, r.ContentLength)
io.ReadFull(r.Body, rdata)
//log.Printf("Data %x", rdata)
fmt.Printf("data % #0x", rdata)
buf := bytes.NewBuffer(rdata)
request.UnMarshal(buf)
fmt.Printf("%v", request)
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
a := ipp.NewCharSetValue("attributes-charset", "utf-8")
var a ipp.Attribute
a = ipp.NewCharSetValue("attributes-charset", "utf-8")
response.AddOperatonAttribute(a)
a = ipp.NewNaturalLanguage("attributes-natural-language", "en")
response.AddOperatonAttribute(a)
a = ipp.NewUriValue("printer-uri", "ipp://drpork:1234/ipp/print")
response.AddOperatonAttribute(a)
a = ipp.NewtextWithoutLanguage("printer-make-and-model", "ChroBro 001")
response.AddOperatonAttribute(a)
a = ipp.NewEnum("printer-state", uint16(3))
response.AddOperatonAttribute(a)
data := response.Marshal()
log.Infof("% x", data)

53
packages/ipp/enum.go Normal file
View File

@@ -0,0 +1,53 @@
package ipp
import (
"encoding/binary"
"io"
)
type enum struct {
name string
value uint16
}
func NewEnum(name string, value uint16) *enum {
e := new(enum)
e.name = name
e.value = value
return e
}
func (e enum) String() string {
return e.name + ":" + string(e.value)
}
func (e *enum) valueTag() tag {
return enumValueTag
}
func (e *enum) unmarshal(byteStream io.Reader) {
//e.name, e.value = unmarshalSingleValue(byteStream)
}
func (e *enum) marshal() []byte {
l := 3 + len(e.name) + 6
b := make([]byte, l, l)
p := 0
b[0] = byte(enumValueTag)
p += 1
binary.BigEndian.PutUint16(b[p:p+2], uint16(len(e.name)))
p += 2
copy(b[p:], []byte(e.name))
p += len(e.name)
binary.BigEndian.PutUint16(b[p:p+2], uint16(4))
p += 2
binary.BigEndian.PutUint32(b[p:p+4], uint32(e.value))
return b
}
func (e *enum) size() int {
l := 1 + 4 // The attribute tag + 2 lengths
l += len(e.name)
l += 4
return l
}

View File

@@ -9,7 +9,7 @@ type keyWord struct {
values []string
}
func newKeyWord() *keyWord {
func NewKeyWord() *keyWord {
k := new(keyWord)
k.values = make([]string, 0)
return k
@@ -69,7 +69,7 @@ func (k *keyWord) marshal() []byte {
return []byte{}
}
func (k *keyWord) addValue(v string) {
func (k *keyWord) AddValue(v string) {
k.values = append(k.values, v)
}

View File

@@ -43,15 +43,15 @@ const (
endCollectionValueTag tag = 0x37
// Character string values
textWithoutLanguagageValueTag tag = 0x41
nameWithoutLanguagageValueTag tag = 0x42
keyWordValueTag tag = 0x44
uriValueTag tag = 0x45
uriSchemeValueTag tag = 0x46
charsetValueTag tag = 0x47
naturalLanguagageValueTag tag = 0x48
mimeMediaTypeValueTag tag = 0x49
memberAttrNameValueTag tag = 0x4a
textWithoutLanguageValueTag tag = 0x41
nameWithoutLanguageValueTag tag = 0x42
keyWordValueTag tag = 0x44
uriValueTag tag = 0x45
uriSchemeValueTag tag = 0x46
charsetValueTag tag = 0x47
naturalLanguageValueTag tag = 0x48
mimeMediaTypeValueTag tag = 0x49
memberAttrNameValueTag tag = 0x4a
)
// Operation-id, defined in rfc8011

View File

@@ -99,23 +99,23 @@ func (r *Request) UnMarshal(body io.Reader) {
r.operationAttributes[c.name] = c
log.Infof("%v %v", c.name, c.value)
case uriValueTag:
u := newUriValue("", "")
u := NewUriValue("", "")
u.unmarshal(body)
r.operationAttributes[u.name] = u
log.Infof("%v %v", u.name, u.value)
case naturalLanguagageValueTag:
n := newNaturalLanguagage("", "")
case naturalLanguageValueTag:
n := NewNaturalLanguage("", "")
n.unmarshal(body)
r.operationAttributes[n.name] = n
log.Infof("%v %v", n.name, n.value)
case keyWordValueTag:
name, value := unmarshalSingleValue(body)
if name == "" {
lastKeyword.addValue(value)
lastKeyword.AddValue(value)
} else {
k := newKeyWord()
k := NewKeyWord()
k.name = name
k.addValue(value)
k.AddValue(value)
r.operationAttributes[k.name] = k
lastKeyword = k
}

View File

@@ -52,6 +52,10 @@ func NewResponse(code statusCode, requestId uint32) *Response {
// }
func (r Response) String() string {
return ""
}
func (r *Response) Marshal() []byte {
a := make([]byte, 0, 20)
a = append(a, r.header.marshal()...)

View File

@@ -28,13 +28,13 @@ func _() {
_ = x[textWithLanguageValueTag-53]
_ = x[nameWithLanguageValueTag-54]
_ = x[endCollectionValueTag-55]
_ = x[textWithoutLanguagageValueTag-65]
_ = x[nameWithoutLanguagageValueTag-66]
_ = x[textWithoutLanguageValueTag-65]
_ = x[nameWithoutLanguageValueTag-66]
_ = x[keyWordValueTag-68]
_ = x[uriValueTag-69]
_ = x[uriSchemeValueTag-70]
_ = x[charsetValueTag-71]
_ = x[naturalLanguagageValueTag-72]
_ = x[naturalLanguageValueTag-72]
_ = x[mimeMediaTypeValueTag-73]
_ = x[memberAttrNameValueTag-74]
}

View File

@@ -0,0 +1,41 @@
package ipp
import "io"
type textWithoutLanguage struct {
name string
value string
}
func NewtextWithoutLanguage(name, value string) *textWithoutLanguage {
c := new(textWithoutLanguage)
c.name = name
c.value = value
return c
}
func (c textWithoutLanguage) String() string {
return c.name + ":" + c.value
}
func (c *textWithoutLanguage) valueTag() tag {
return textWithoutLanguageValueTag
}
func (c *textWithoutLanguage) unmarshal(byteStream io.Reader) {
c.name, c.value = unmarshalSingleValue(byteStream)
}
func (c *textWithoutLanguage) marshal() []byte {
l := 5 + len(c.name) + len(c.value)
b := make([]byte, l, l)
b[0] = byte(textWithoutLanguageValueTag)
marshalNameValue(c.name, c.value, b[1:])
return b
}
func (c *textWithoutLanguage) size() int {
l := 1 + 4 // The attribute tag + 2 lengths
l += len(c.name)
l += len(c.value)
return l
}

View File

@@ -9,7 +9,7 @@ type uriValue struct {
value string
}
func newUriValue(name, value string) *uriValue {
func NewUriValue(name, value string) *uriValue {
u := new(uriValue)
u.name = name
u.value = value

View File

@@ -39,6 +39,7 @@ func Run(ctx context.Context) {
txt = append(txt, []byte("product=coola-skrivaren"))
txt = append(txt, []byte("Color=T"))
txt = append(txt, []byte("rp=ipp/print"))
txt = append(txt, []byte("ty=ChroBro 001"))
err = eg.AddService(avahi.InterfaceUnspec, avahi.ProtoUnspec, 0, hostname, "_ipp._tcp", "local", fqdn, 1234, txt)
if err != nil {
log.Fatalf("AddService() failed: %v", err)