More types.
This commit is contained in:
20
main.go
20
main.go
@@ -1,8 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"ippserver/packages/ipp"
|
"ippserver/packages/ipp"
|
||||||
"ippserver/packages/mdnsserver"
|
"ippserver/packages/mdnsserver"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -40,10 +42,24 @@ func handle(w http.ResponseWriter, r *http.Request) {
|
|||||||
// log.Infof("Body %x", body)
|
// log.Infof("Body %x", body)
|
||||||
|
|
||||||
request := ipp.NewRequest()
|
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)
|
fmt.Printf("%v", request)
|
||||||
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
|
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)
|
response.AddOperatonAttribute(a)
|
||||||
data := response.Marshal()
|
data := response.Marshal()
|
||||||
log.Infof("% x", data)
|
log.Infof("% x", data)
|
||||||
|
|||||||
53
packages/ipp/enum.go
Normal file
53
packages/ipp/enum.go
Normal 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
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ type keyWord struct {
|
|||||||
values []string
|
values []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newKeyWord() *keyWord {
|
func NewKeyWord() *keyWord {
|
||||||
k := new(keyWord)
|
k := new(keyWord)
|
||||||
k.values = make([]string, 0)
|
k.values = make([]string, 0)
|
||||||
return k
|
return k
|
||||||
@@ -69,7 +69,7 @@ func (k *keyWord) marshal() []byte {
|
|||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *keyWord) addValue(v string) {
|
func (k *keyWord) AddValue(v string) {
|
||||||
k.values = append(k.values, v)
|
k.values = append(k.values, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,15 +43,15 @@ const (
|
|||||||
endCollectionValueTag tag = 0x37
|
endCollectionValueTag tag = 0x37
|
||||||
|
|
||||||
// Character string values
|
// Character string values
|
||||||
textWithoutLanguagageValueTag tag = 0x41
|
textWithoutLanguageValueTag tag = 0x41
|
||||||
nameWithoutLanguagageValueTag tag = 0x42
|
nameWithoutLanguageValueTag tag = 0x42
|
||||||
keyWordValueTag tag = 0x44
|
keyWordValueTag tag = 0x44
|
||||||
uriValueTag tag = 0x45
|
uriValueTag tag = 0x45
|
||||||
uriSchemeValueTag tag = 0x46
|
uriSchemeValueTag tag = 0x46
|
||||||
charsetValueTag tag = 0x47
|
charsetValueTag tag = 0x47
|
||||||
naturalLanguagageValueTag tag = 0x48
|
naturalLanguageValueTag tag = 0x48
|
||||||
mimeMediaTypeValueTag tag = 0x49
|
mimeMediaTypeValueTag tag = 0x49
|
||||||
memberAttrNameValueTag tag = 0x4a
|
memberAttrNameValueTag tag = 0x4a
|
||||||
)
|
)
|
||||||
|
|
||||||
// Operation-id, defined in rfc8011
|
// Operation-id, defined in rfc8011
|
||||||
|
|||||||
@@ -99,23 +99,23 @@ func (r *Request) UnMarshal(body io.Reader) {
|
|||||||
r.operationAttributes[c.name] = c
|
r.operationAttributes[c.name] = c
|
||||||
log.Infof("%v %v", c.name, c.value)
|
log.Infof("%v %v", c.name, c.value)
|
||||||
case uriValueTag:
|
case uriValueTag:
|
||||||
u := newUriValue("", "")
|
u := NewUriValue("", "")
|
||||||
u.unmarshal(body)
|
u.unmarshal(body)
|
||||||
r.operationAttributes[u.name] = u
|
r.operationAttributes[u.name] = u
|
||||||
log.Infof("%v %v", u.name, u.value)
|
log.Infof("%v %v", u.name, u.value)
|
||||||
case naturalLanguagageValueTag:
|
case naturalLanguageValueTag:
|
||||||
n := newNaturalLanguagage("", "")
|
n := NewNaturalLanguage("", "")
|
||||||
n.unmarshal(body)
|
n.unmarshal(body)
|
||||||
r.operationAttributes[n.name] = n
|
r.operationAttributes[n.name] = n
|
||||||
log.Infof("%v %v", n.name, n.value)
|
log.Infof("%v %v", n.name, n.value)
|
||||||
case keyWordValueTag:
|
case keyWordValueTag:
|
||||||
name, value := unmarshalSingleValue(body)
|
name, value := unmarshalSingleValue(body)
|
||||||
if name == "" {
|
if name == "" {
|
||||||
lastKeyword.addValue(value)
|
lastKeyword.AddValue(value)
|
||||||
} else {
|
} else {
|
||||||
k := newKeyWord()
|
k := NewKeyWord()
|
||||||
k.name = name
|
k.name = name
|
||||||
k.addValue(value)
|
k.AddValue(value)
|
||||||
r.operationAttributes[k.name] = k
|
r.operationAttributes[k.name] = k
|
||||||
lastKeyword = k
|
lastKeyword = k
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,6 +52,10 @@ func NewResponse(code statusCode, requestId uint32) *Response {
|
|||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func (r Response) String() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) Marshal() []byte {
|
func (r *Response) Marshal() []byte {
|
||||||
a := make([]byte, 0, 20)
|
a := make([]byte, 0, 20)
|
||||||
a = append(a, r.header.marshal()...)
|
a = append(a, r.header.marshal()...)
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ func _() {
|
|||||||
_ = x[textWithLanguageValueTag-53]
|
_ = x[textWithLanguageValueTag-53]
|
||||||
_ = x[nameWithLanguageValueTag-54]
|
_ = x[nameWithLanguageValueTag-54]
|
||||||
_ = x[endCollectionValueTag-55]
|
_ = x[endCollectionValueTag-55]
|
||||||
_ = x[textWithoutLanguagageValueTag-65]
|
_ = x[textWithoutLanguageValueTag-65]
|
||||||
_ = x[nameWithoutLanguagageValueTag-66]
|
_ = x[nameWithoutLanguageValueTag-66]
|
||||||
_ = x[keyWordValueTag-68]
|
_ = x[keyWordValueTag-68]
|
||||||
_ = x[uriValueTag-69]
|
_ = x[uriValueTag-69]
|
||||||
_ = x[uriSchemeValueTag-70]
|
_ = x[uriSchemeValueTag-70]
|
||||||
_ = x[charsetValueTag-71]
|
_ = x[charsetValueTag-71]
|
||||||
_ = x[naturalLanguagageValueTag-72]
|
_ = x[naturalLanguageValueTag-72]
|
||||||
_ = x[mimeMediaTypeValueTag-73]
|
_ = x[mimeMediaTypeValueTag-73]
|
||||||
_ = x[memberAttrNameValueTag-74]
|
_ = x[memberAttrNameValueTag-74]
|
||||||
}
|
}
|
||||||
|
|||||||
41
packages/ipp/textWithoutLanguage.go
Normal file
41
packages/ipp/textWithoutLanguage.go
Normal 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
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ type uriValue struct {
|
|||||||
value string
|
value string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newUriValue(name, value string) *uriValue {
|
func NewUriValue(name, value string) *uriValue {
|
||||||
u := new(uriValue)
|
u := new(uriValue)
|
||||||
u.name = name
|
u.name = name
|
||||||
u.value = value
|
u.value = value
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ func Run(ctx context.Context) {
|
|||||||
txt = append(txt, []byte("product=coola-skrivaren"))
|
txt = append(txt, []byte("product=coola-skrivaren"))
|
||||||
txt = append(txt, []byte("Color=T"))
|
txt = append(txt, []byte("Color=T"))
|
||||||
txt = append(txt, []byte("rp=ipp/print"))
|
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)
|
err = eg.AddService(avahi.InterfaceUnspec, avahi.ProtoUnspec, 0, hostname, "_ipp._tcp", "local", fqdn, 1234, txt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("AddService() failed: %v", err)
|
log.Fatalf("AddService() failed: %v", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user