7 Commits

Author SHA1 Message Date
48aa81238d Add collection type, use buffered bytestream.
Fixed some more attribute types.
2021-02-09 21:31:27 +01:00
0805cec129 Fix lint warnings. 2021-01-09 12:10:43 +01:00
f449b535db Add rangeOfInteger attribute. 2020-12-29 21:45:00 +01:00
1eff8711d3 Add integer attribute type.
Merges some common code between integer and enum.
Enum can now bw a set.
Add valuer must take a interface{} in addValue since it can be different types.
2020-12-29 20:53:01 +01:00
e0a0c88d95 Start client implementation. 2020-12-28 20:16:59 +01:00
53aaaf5521 Merge attribute handling from request and response to commen code. 2020-12-28 20:05:01 +01:00
04a4b4157f 2-add-keyword-support (#6)
More development.

More types.
Fixed attribute groups in requests.
Started on client.
Saving data to file.

More types. Printing from chromeos works a little bit.

More types.

Spelling corrections.

WIP: Fix keyword handling

Move request to a separate file and add test.

Co-authored-by: Henrik Sölver <henrik.solver@gmail.com>
Reviewed-on: #6
Co-Authored-By: henrik <henrik.solver@gmail.com>
Co-Committed-By: henrik <henrik.solver@gmail.com>
2020-12-27 09:16:32 +01:00
32 changed files with 1378 additions and 291 deletions

39
client/main.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import (
"bytes"
"fmt"
"ippserver/packages/ipp"
"net/http"
log "github.com/sirupsen/logrus"
)
//application/ipp
// brn30055cb5e3ae.local:631/ipp/print
func main() {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
log.SetLevel(log.DebugLevel)
const printerUri = "brn30055cb5e3ae.local:631/ipp/print"
request := ipp.NewRequest(ipp.GetPrinterAttributes, 10)
request.AddOperatonAttribute(ipp.NewCharSetValue("attributes-charset", "utf-8"))
request.AddOperatonAttribute(ipp.NewNaturalLanguage("attributes-natural-language", "en"))
request.AddOperatonAttribute(ipp.NewURIValue("printer-uri", "ipp://"+printerUri))
r := request.Marshal()
b := bytes.NewBuffer(r)
httpResponse, err := http.Post("http://"+"brn30055cb5e3ae.local:631/ipp/print", "application/ipp", b)
if err != nil {
fmt.Print(err)
return
}
rb := ipp.NewResponse(0, 0)
rb.UnMarshal(httpResponse.Body)
fmt.Print(r)
}

59
packages/ipp/boolean.go Normal file
View File

@@ -0,0 +1,59 @@
package ipp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
log "github.com/sirupsen/logrus"
)
type boolean struct {
name string
value bool
}
func NewBoolean(name string, value bool) *boolean {
b := new(boolean)
b.name = name
b.value = value
return b
}
func (b boolean) Name() string {
return b.name
}
func (b boolean) String() string {
return b.name + ":" + fmt.Sprint(b.value)
}
func (b *boolean) valueTag() tag {
return booleanValueTag
}
func (b *boolean) unmarshal(byteStream io.Reader) {
log.Warn("Unmarshal of boolean is not implemented yet")
}
func (b *boolean) marshal() []byte {
l := 5 + len(b.name)
ba := make([]byte, 0, l)
buf := bytes.NewBuffer(ba)
buf.WriteByte(byte(booleanValueTag))
binary.Write(buf, binary.BigEndian, uint16(len(b.name)))
buf.WriteString(b.name)
binary.Write(buf, binary.BigEndian, uint16(1))
if b.value {
buf.WriteByte(byte(1))
} else {
buf.WriteByte(byte(0))
}
return buf.Bytes()
}
func (b *boolean) size() int {
l := 5 + len(b.name)
return l
}

View File

@@ -16,9 +16,14 @@ func NewCharSetValue(name string, value string) *charSetValue {
return c
}
func (c charSetValue) Name() string {
return c.name
}
func (c charSetValue) String() string {
return c.name + ":" + c.value
}
func (c *charSetValue) valueTag() tag {
return charsetValueTag
}
@@ -29,7 +34,7 @@ func (c *charSetValue) unmarshal(byteStream io.Reader) {
func (c *charSetValue) marshal() []byte {
l := 5 + len(c.name) + len(c.value)
b := make([]byte, l, l)
b := make([]byte, l)
b[0] = byte(charsetValueTag)
marshalNameValue(c.name, c.value, b[1:])
return b

View File

@@ -0,0 +1,74 @@
package ipp
import (
"bufio"
"encoding/binary"
log "github.com/sirupsen/logrus"
)
// Currently the collection data is just consumed and dropped from the bytestream
func consumeCollection(byteStream *bufio.Reader) {
// RFC8010 Section 3.1.6
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
collectionName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, collectionName)
}
log.Info("collection name " + string(collectionName))
err := binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if err != nil {
log.Fatal("error ", err.Error())
}
if length != 0 {
log.Fatal("Should be zero")
}
// Member attributes
done:
for {
var t tag
binary.Read(byteStream, binary.BigEndian, &t)
log.Debug("Collection tag ", t)
switch t {
case endCollectionValueTag:
binary.Read(byteStream, binary.BigEndian, &length)
if length != 0 {
log.Fatal("Should be zero")
}
binary.Read(byteStream, binary.BigEndian, &length)
if length != 0 {
log.Fatal("Should be zero")
}
case memberAttrNameValueTag:
// RFC8010 Section 3.7.1
binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if length != 0 {
log.Fatal("Should be zero")
}
binary.Read(byteStream, binary.BigEndian, &length) // Value length
memberName := make([]byte, length)
binary.Read(byteStream, binary.BigEndian, memberName)
log.Debugf("Member name: %v", string(memberName))
var memberValueTag tag
binary.Read(byteStream, binary.BigEndian, &memberValueTag)
log.Debug("Member value tag: ", memberValueTag)
binary.Read(byteStream, binary.BigEndian, &length) //Always zero ??
if length != 0 {
log.Fatal("Should be zero")
}
var memberValueLength uint16
binary.Read(byteStream, binary.BigEndian, &memberValueLength)
memberValue := make([]byte, memberValueLength)
binary.Read(byteStream, binary.BigEndian, memberValue)
log.Debugf("Member Value: % x", memberValue)
default:
// Next tag is one that can not be handled in the collection
// Put it back in the byte stream and return to main loop
byteStream.UnreadByte()
break done
}
}
log.Debug("Collection done")
}

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

@@ -0,0 +1,41 @@
package ipp
import (
"fmt"
)
type enum struct {
name string
values []int32
}
func NewEnum(name string, values ...int32) *enum {
e := new(enum)
e.name = name
e.values = values
return e
}
func (e enum) Name() string {
return e.name
}
func (e enum) String() string {
return e.name + ":" + fmt.Sprint(e.values)
}
func (e *enum) valueTag() tag {
return enumValueTag
}
func (e *enum) size() int {
return 9 + len(e.name)
}
func (e *enum) addValue(v interface{}) {
e.values = append(e.values, v.(int32))
}
func (e *enum) marshal() []byte {
return marshalInteger(enumValueTag, e.name, e.values)
}

67
packages/ipp/integer.go Normal file
View File

@@ -0,0 +1,67 @@
package ipp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type integer struct {
name string
values []int32
}
func NewInteger(name string, values ...int32) *integer {
e := new(integer)
e.name = name
e.values = values
return e
}
func (i integer) Name() string {
return i.name
}
func (i integer) String() string {
return i.name + ":" + fmt.Sprint(i.values)
}
func (i *integer) valueTag() tag {
return integerValueTag
}
func (i *integer) size() int {
return 9 + len(i.name) // The attribute tag + 2 lengths
}
func (i *integer) addValue(v interface{}) {
i.values = append(i.values, v.(int32))
}
func (i *integer) marshal() []byte {
return marshalInteger(integerValueTag, i.name, i.values)
}
func marshalInteger(t tag, name string, values []int32) []byte {
l := 9 + len(name)
b := make([]byte, 0, l)
buf := bytes.NewBuffer(b)
buf.WriteByte(byte(integerValueTag))
binary.Write(buf, binary.BigEndian, uint16(len(name)))
buf.WriteString(name)
binary.Write(buf, binary.BigEndian, uint16(4))
binary.Write(buf, binary.BigEndian, values[0])
for _, v := range values[1:] {
buf.WriteByte(byte(integerValueTag))
binary.Write(buf, binary.BigEndian, uint16(0))
binary.Write(buf, binary.BigEndian, uint16(4))
binary.Write(buf, binary.BigEndian, v)
}
return buf.Bytes()
}
func unmarshalSingleInteger(byteStream io.Reader) (string, int32) {
name, data := unmarshalSingleAttribute(byteStream)
return name, int32(binary.BigEndian.Uint32(data))
}

View File

@@ -0,0 +1,38 @@
package ipp
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnMarshalSinglePositiveInteger(T *testing.T) {
testdata := []byte{
0x00, 0x04,
0x66, 0x6c, 0x6f, 0x70, //flop
0x00, 0x04,
0x00, 0x0, 0x0, 0x4,
}
buf := bytes.NewBuffer(testdata)
n, v := unmarshalSingleInteger(buf)
assert.Equal(T, "flop", n, "Should be equal")
assert.Equal(T, int32(4), v, "Should be equal")
}
func TestUnMarshalSingleNegativeInteger(T *testing.T) {
testdata := []byte{
0x00, 0x04,
0x66, 0x6c, 0x6f, 0x70, //flop
0x00, 0x04,
0xff, 0xff, 0xff, 0xfc,
}
buf := bytes.NewBuffer(testdata)
n, v := unmarshalSingleInteger(buf)
assert.Equal(T, "flop", n, "Should be equal")
assert.Equal(T, int32(-4), v, "Should be equal")
}

View File

@@ -1,45 +1,37 @@
package ipp
import "io"
type keyWord struct {
name string
values []string
sos *setOfStrings
}
func newKeyWord() *keyWord {
func NewKeyWord(name string, values ...string) *keyWord {
k := new(keyWord)
k.sos = NewSetOfStrings(name, keyWordValueTag, values)
return k
}
func (k *keyWord) string() string {
return "a uriValue"
}
func (k *keyWord) valueTag() tag {
return keyWordValueTag
func (k keyWord) Name() string {
return k.sos.name
}
func (k *keyWord) unmarshal(byteStream io.Reader) {
}
func (k *keyWord) marshal() []byte {
return []byte{}
}
func (k *keyWord) addValue(v string) {
k.values = append(k.values, v)
func (k keyWord) String() string {
return k.sos.String()
}
func (k *keyWord) size() int {
l := 1 + 2 // The value tag (0x44) + name-length field (2 bytes)
l += len(k.name)
l += 2 // value-length field (2 bytes)
l += len(k.values[0])
// Add all additional values
for _, v := range k.values[1:] {
l += 1 + 4 // The value tag (0x44) + 2 length fields (2 bytes)
l += len(v)
}
return l
return k.sos.size()
}
func (k *keyWord) valueTag() tag {
return k.sos.valueTag()
}
func (k *keyWord) marshal() []byte {
return k.sos.marshal()
}
func (k *keyWord) addValue(v interface{}) {
k.sos.AddValue(v.(string))
}

View File

@@ -0,0 +1,36 @@
package ipp
import (
"testing"
"github.com/stretchr/testify/assert"
)
// func TestUnmarshalSimpleKeyword(T *testing.T) {
// testdata := []byte{
// 0x00, 0x14,
// 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x2d, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73,
// 0x00, 0x16,
// 0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x6d, 0x61, 0x6b, 0x65, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x6c,
// }
// buf := bytes.NewBuffer(testdata)
// var k keyWord
// k.unmarshal(buf)
// assert.Equal(T, k.name, "requested-attributes")
// assert.Equal(T, k.values[0], "printer-make-and-model")
// }
func TestMarshalSimpleKeyword(T *testing.T) {
testdata := []byte{
0x44, 0x00, 0x14,
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x2d, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73,
0x00, 0x16,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x6d, 0x61, 0x6b, 0x65, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x6c,
}
k := NewKeyWord("requested-attributes")
k.addValue("printer-make-and-model")
m := k.marshal()
assert.Equal(T, testdata, m, "Should be equal")
}

View File

@@ -1,6 +1,8 @@
//Package ipp provides functonality to handle ipp messages
package ipp
import (
"bufio"
"encoding/binary"
"fmt"
"io"
@@ -45,41 +47,51 @@ 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
type operationId uint16
// OperationID is defined in rfc8011
type OperationID uint16
const (
PrintJob operationId = 0x0002
PrintURI operationId = 0x0003
ValidateJob operationId = 0x0004
CreateJob operationId = 0x0005
SendDocument operationId = 0x0006
SendURI operationId = 0x0007
CancelJob operationId = 0x0008
GetJobAttributes operationId = 0x0009
GetJobs operationId = 0x000a
GetPrinterAttributes operationId = 0x000b
HoldJob operationId = 0x000c
ReleaseJob operationId = 0x000d
RestartJob operationId = 0x000e
PausePrinter operationId = 0x0010
ResumePrinter operationId = 0x0011
PurgeJobs operationId = 0x0012
PrintJob OperationID = 0x0002
PrintURI OperationID = 0x0003
ValidateJob OperationID = 0x0004
CreateJob OperationID = 0x0005
SendDocument OperationID = 0x0006
SendURI OperationID = 0x0007
CancelJob OperationID = 0x0008
GetJobAttributes OperationID = 0x0009
GetJobs OperationID = 0x000a
GetPrinterAttributes OperationID = 0x000b
HoldJob OperationID = 0x000c
ReleaseJob OperationID = 0x000d
RestartJob OperationID = 0x000e
PausePrinter OperationID = 0x0010
ResumePrinter OperationID = 0x0011
PurgeJobs OperationID = 0x0012
)
type printerState int32
// printerstate defenitions
const (
Idle printerState = 3
Processing printerState = 4
Stopped printerState = 5
)
type statusCode uint16
// status code defenitions
const (
SuccessfulOk statusCode = 0x0000
ClientErrorBadRequest statusCode = 0x0400
@@ -92,151 +104,29 @@ func (v versionNumber) String() string {
return fmt.Sprintf("%x.%x", vn&0xff00>>8, vn&0x00ff)
}
type ippRequestHeader struct {
versionNumber versionNumber
operationId operationId
requestId uint32
}
func (h *ippRequestHeader) marshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.operationId)
binary.Read(byteStream, binary.BigEndian, &h.requestId)
}
func (h ippRequestHeader) String() string {
return fmt.Sprintf("Version number: %v Operation Id: %v Request Id: %v", h.versionNumber, h.operationId, h.requestId)
}
type Attribute interface {
valueTag() tag
unmarshal(io.Reader)
marshal() []byte
size() int
}
type Request struct {
header ippRequestHeader
operationAttributes map[string]Attribute
jobAttributes map[string]Attribute
printerAttributes map[string]Attribute
}
func NewRequest() *Request {
r := new(Request)
r.operationAttributes = make(map[string]Attribute)
r.jobAttributes = make(map[string]Attribute)
r.printerAttributes = make(map[string]Attribute)
return r
}
func (r Request) String() string {
s := r.header.String() + "\n" + " OperationAttributes" + "\n"
for _, a := range r.operationAttributes {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
func unmarshalSingleAttribute(byteStream io.Reader) (string, []byte) {
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
attributeName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, attributeName)
}
return s
}
// log.Info(r.Header)
// log.Info(r.ContentLength)
//body := make([]byte, r.ContentLength)
//body.Read(body)
func (r *Request) UnMarshal(body io.Reader) {
r.header.marshal(body)
log.Infof("Header %v", r.header)
var tag tag
err := binary.Read(body, binary.BigEndian, &tag)
if err != nil {
log.Error(err.Error())
}
log.Infof("got tag - %v", tag)
if tag == operationAttributes {
nextoperationattr:
for tag != endOfAttributes {
var lastKeyword *keyWord
err = binary.Read(body, binary.BigEndian, &tag)
if err != nil {
log.Error(err.Error())
}
log.Infof("Value tag - %v", tag)
switch tag {
case endOfAttributes:
err = binary.Read(body, binary.BigEndian, &tag)
if err == io.EOF {
// No more data
return
}
if err != nil {
log.Error(err.Error())
}
log.Infof("got tag - %v", tag)
err = binary.Read(body, binary.BigEndian, &tag)
if err != nil {
log.Error(err.Error())
}
log.Infof("got tag - %v", tag)
break nextoperationattr
case charsetValueTag:
c := NewCharSetValue("", "")
c.unmarshal(body)
r.operationAttributes[c.name] = c
log.Infof("%v %v", c.name, c.value)
case uriValueTag:
u := newUriValue("", "")
u.unmarshal(body)
r.operationAttributes[u.name] = u
log.Infof("%v %v", u.name, u.value)
case naturalLanguagageValueTag:
n := newNaturalLanguagage("", "")
n.unmarshal(body)
r.operationAttributes[n.name] = n
log.Infof("%v %v", n.name, n.value)
case keyWordValueTag:
k := newKeyWord()
k.unmarshal(body)
r.operationAttributes[k.name] = k
if k.name == "" {
lastKeyword.addValue(k.values[0])
} else {
lastKeyword = k
}
default:
log.Errorf("Unsupported tag %v", tag)
}
}
log.Infof("Value tag %v", tag)
} else {
log.Error("unexpected tag")
// TODO Return something sensible here
}
}
func (r *Request) RequestId() uint32 {
return r.header.requestId
binary.Read(byteStream, binary.BigEndian, &length)
attributeValue := make([]byte, length)
binary.Read(byteStream, binary.BigEndian, attributeValue)
return string(attributeName), attributeValue
}
func unmarshalSingleValue(byteStream io.Reader) (string, string) {
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
//log.Infof("Length %v", length)
attributeName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, attributeName)
//log.Infof("Valuename %v", string(attributeName))
}
binary.Read(byteStream, binary.BigEndian, &length)
//log.Infof("Length %v", length)
attributeValue := make([]byte, length)
binary.Read(byteStream, binary.BigEndian, attributeValue)
//log.Infof("Value %v", string(attributeValue))
return string(attributeName), string(attributeValue)
}
@@ -250,3 +140,172 @@ func marshalNameValue(name, value string, b []byte) {
p += 2
copy(b[p:], []byte(value))
}
type Attribute interface {
Name() string
valueTag() tag
marshal() []byte
//size() int
}
type attributes struct {
operation []Attribute
printer []Attribute
job []Attribute
}
func (a *attributes) String() string {
s := " OperationAttributes" + "\n"
for _, a := range a.operation {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
s = s + " PrinterAttributes" + "\n"
for _, a := range a.printer {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
s = s + " JobAttributes" + "\n"
for _, a := range a.job {
s = s + fmt.Sprintf(" %v (%v)\n", a, a.valueTag())
}
return s
}
func (a *attributes) addAttribute(group tag, attr Attribute) {
switch group {
case operationAttributes:
a.operation = append(a.operation, attr)
case jobAttributes:
a.job = append(a.job, attr)
case printerAttributes:
a.printer = append(a.printer, attr)
default:
log.Error("Unknown attribute group")
}
}
func UnMarshalAttributes(bytestream *bufio.Reader) *attributes {
a := new(attributes)
var t tag
err := binary.Read(bytestream, binary.BigEndian, &t)
if err != nil {
log.Error(err.Error())
}
log.Debugf("got tag - %v", t)
if t != operationAttributes && t != jobAttributes && t != printerAttributes {
log.Errorf("Unknown attribute group tag %v", t)
return nil
}
currentAttributeGroup := t
var lastAddValuer AddValuer
for {
err = binary.Read(bytestream, binary.BigEndian, &t)
if err != nil {
log.Fatal("End of input before end of attributes tag (%v)", err.Error())
}
log.Debugf("Value tag - %v", t)
switch t {
case endOfAttributes:
return a
case charsetValueTag:
c := NewCharSetValue("", "")
c.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, c)
log.Debugf("%v %v", c.name, c.value)
case booleanValueTag:
na := NewBoolean("", false)
na.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, na)
case uriValueTag:
u := NewURIValue("", "")
u.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, u)
log.Debugf("%v %v", u.name, u.value)
case naturalLanguageValueTag:
n := NewNaturalLanguage("", "")
n.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, n)
log.Debugf("%v %v", n.name, n.value)
case keyWordValueTag:
name, value := unmarshalSingleValue(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
k := NewKeyWord(name, value)
a.addAttribute(currentAttributeGroup, k)
lastAddValuer = k
}
log.Debugf("%v : %v", name, value)
case nameWithoutLanguageValueTag:
n := NewNameWithoutLanguage("", "")
n.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, n)
log.Debugf("%v %v", n.name, n.value)
case textWithoutLanguageValueTag:
attr := NewtextWithoutLanguage("", "")
attr.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, attr)
log.Debugf("%v %v", attr.name, attr.value)
case mimeMediaTypeValueTag:
name, value := unmarshalSingleValue(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
m := NewMimeMediaType(name, value)
a.addAttribute(currentAttributeGroup, m)
lastAddValuer = m
}
log.Debugf("%v : %v", name, value)
case integerValueTag:
name, value := unmarshalSingleInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
i := NewInteger(name, value)
a.addAttribute(currentAttributeGroup, i)
lastAddValuer = i
}
log.Debugf("%v : %v", name, value)
case rangeOfIntegerValueTag:
name, value := unmarshalSingleRangeOfInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
r := NewRangeOfInteger(name, value)
a.addAttribute(currentAttributeGroup, r)
lastAddValuer = r
}
log.Debugf("%v : %v", name, value)
case enumValueTag:
name, value := unmarshalSingleInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
e := NewEnum(name, value)
a.addAttribute(currentAttributeGroup, e)
lastAddValuer = e
}
log.Debugf("%v : %v", name, value)
case begCollectionValueTag:
// For now just consume the collection
consumeCollection(bytestream)
case jobAttributes:
log.Debug("Start job attributes")
currentAttributeGroup = jobAttributes
case printerAttributes:
log.Debug("Start printer attributes")
currentAttributeGroup = printerAttributes
case operationAttributes:
log.Debug("Start operation attributes")
currentAttributeGroup = operationAttributes
case resolutionValueTag:
res := NewResolution("", 0, 0)
res.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, res)
log.Debugf("Resolution %v", res)
default:
log.Errorf("Unsupported tag %v (%x)", t, uint8(t))
}
}
}

View File

@@ -0,0 +1,36 @@
package ipp
type mimeMediaType struct {
sos *setOfStrings
}
func NewMimeMediaType(name string, values ...string) *mimeMediaType {
m := new(mimeMediaType)
m.sos = NewSetOfStrings(name, mimeMediaTypeValueTag, values)
return m
}
func (m mimeMediaType) Name() string {
return m.sos.name
}
func (m mimeMediaType) String() string {
return m.sos.String()
}
func (m *mimeMediaType) size() int {
return m.sos.size()
}
func (m *mimeMediaType) valueTag() tag {
return m.sos.valueTag()
}
func (m *mimeMediaType) marshal() []byte {
return m.sos.marshal()
}
func (m *mimeMediaType) addValue(v interface{}) {
m.sos.AddValue(v.(string))
}

View File

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

View File

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

View File

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

View File

@@ -36,7 +36,7 @@ var (
_operationId_index_1 = [...]uint8{0, 12, 25, 34}
)
func (i operationId) String() string {
func (i OperationID) String() string {
switch {
case 2 <= i && i <= 14:
i -= 2

View File

@@ -0,0 +1,26 @@
// Code generated by "stringer -type printerState ."; DO NOT EDIT.
package ipp
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[Idle-3]
_ = x[Processing-4]
_ = x[Stopped-5]
}
const _printerState_name = "IdlePreocessingStopped"
var _printerState_index = [...]uint8{0, 4, 15, 22}
func (i printerState) String() string {
i -= 3
if i < 0 || i >= printerState(len(_printerState_index)-1) {
return "printerState(" + strconv.FormatInt(int64(i+3), 10) + ")"
}
return _printerState_name[_printerState_index[i]:_printerState_index[i+1]]
}

View File

@@ -0,0 +1,55 @@
package ipp
import (
"encoding/binary"
"fmt"
"io"
log "github.com/sirupsen/logrus"
)
type IRange struct {
lower int32
upper int32
}
type rangeOfInteger struct {
name string
values []IRange
}
func NewRangeOfInteger(name string, values ...IRange) *rangeOfInteger {
r := new(rangeOfInteger)
r.name = name
r.values = values
return r
}
func (r *rangeOfInteger) Name() string {
return r.name
}
func (r rangeOfInteger) String() string {
return r.name + ":" + fmt.Sprint(r.values)
}
func (r *rangeOfInteger) valueTag() tag {
return rangeOfIntegerValueTag
}
func (r *rangeOfInteger) marshal() []byte {
log.Error("marshal rangeOfInteger is not implemented yet")
return []byte{}
}
func (r *rangeOfInteger) addValue(v interface{}) {
r.values = append(r.values, v.(IRange))
}
func unmarshalSingleRangeOfInteger(byteStream io.Reader) (string, IRange) {
name, data := unmarshalSingleAttribute(byteStream)
var r IRange
r.lower = int32(binary.BigEndian.Uint32(data[0:4]))
r.upper = int32(binary.BigEndian.Uint32(data[4:8]))
return name, r
}

View File

@@ -0,0 +1,24 @@
package ipp
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnMarshalSingleRange(T *testing.T) {
testdata := []byte{
0x00, 0x04,
0x66, 0x6c, 0x6f, 0x70, //flop
0x00, 0x08,
0x00, 0x0, 0x0, 0x4, 0x00, 0x0, 0x0, 0x5,
}
buf := bytes.NewBuffer(testdata)
n, v := unmarshalSingleRangeOfInteger(buf)
assert.Equal(T, "flop", n, "Should be equal")
assert.Equal(T, int32(4), v.lower, "Should be equal")
assert.Equal(T, int32(5), v.upper, "Should be equal")
}

118
packages/ipp/request.go Normal file
View File

@@ -0,0 +1,118 @@
package ipp
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
)
type ippMessageHeader struct {
versionNumber versionNumber
operationID OperationID
requestID uint32
}
func (h *ippMessageHeader) unmarshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.operationID)
binary.Read(byteStream, binary.BigEndian, &h.requestID)
}
func (h *ippMessageHeader) marshal() []byte {
b := make([]byte, 0, 8)
buf := bytes.NewBuffer(b)
binary.Write(buf, binary.BigEndian, h.versionNumber)
binary.Write(buf, binary.BigEndian, h.operationID)
binary.Write(buf, binary.BigEndian, h.requestID)
return buf.Bytes()
}
func (h ippMessageHeader) String() string {
return fmt.Sprintf("Version number: %v Operation Id: %v Request Id: %v", h.versionNumber, h.operationID, h.requestID)
}
type AddValuer interface {
addValue(interface{})
}
type Request struct {
a *attributes
header ippMessageHeader
}
func NewRequest(op OperationID, requestID uint32) *Request {
r := new(Request)
r.header.operationID = op
r.header.requestID = requestID
r.header.versionNumber = 0x0200
r.a = new(attributes)
return r
}
func (r Request) String() string {
return r.header.String() + "\n" + r.a.String()
}
func (r *Request) UnMarshal(body io.Reader) {
buffbody := bufio.NewReader(body)
r.header.unmarshal(buffbody)
//log.Printf("Header %v", r.header)
r.a = UnMarshalAttributes(buffbody)
}
func (r *Request) RequestID() uint32 {
return r.header.requestID
}
func (r *Request) Operation() OperationID {
return r.header.operationID
}
func (r *Request) GetAttribute(name string) Attribute {
for _, a := range r.a.operation {
if a.Name() == name {
return a
}
}
return nil
}
func (r *Request) Marshal() []byte {
var buf bytes.Buffer
buf.Write(r.header.marshal())
if len(r.a.operation) > 0 {
buf.WriteByte(byte(operationAttributes))
for _, e := range r.a.operation {
buf.Write(e.marshal())
}
}
if len(r.a.job) > 0 {
buf.WriteByte(byte(jobAttributes))
for _, e := range r.a.job {
buf.Write(e.marshal())
}
}
if len(r.a.printer) > 0 {
buf.WriteByte(byte(printerAttributes))
for _, e := range r.a.printer {
buf.Write(e.marshal())
}
}
buf.WriteByte(byte(endOfAttributes))
return buf.Bytes()
}
func (r *Request) AddPrinterAttribute(a Attribute) {
r.a.addAttribute(printerAttributes, a)
}
func (r *Request) AddOperatonAttribute(a Attribute) {
r.a.addAttribute(operationAttributes, a)
}
func (r *Request) AddJobAttribute(a Attribute) {
r.a.addAttribute(jobAttributes, a)
}

View File

@@ -0,0 +1,79 @@
package ipp
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var testRequest = []byte{0x01, 0x01, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x11,
0x01,
0x47,
0x00, 0x12,
0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x2d, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65, 0x74,
0x00, 0x05,
0x75, 0x74, 0x66, 0x2d, 0x38,
0x48,
0x00, 0x1b,
0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x2d, 0x6e, 0x61, 0x74, 0x75, 0x72, 0x61, 0x6c, 0x2d, 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,
0x00, 0x05,
0x65, 0x6e, 0x2d, 0x75, 0x73,
0x45, // uriValueTag
0x00, 0x0b,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x75, 0x72, 0x69,
0x00, 0x20,
0x69, 0x70, 0x70, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38, 0x2e, 0x30, 0x2e, 0x32, 0x3a, 0x31, 0x32, 0x33, 0x34, 0x2f, 0x69, 0x70, 0x70, 0x2f, 0x70, 0x72, 0x69, 0x6e, 0x74,
0x44, // keywordValueTag
0x00, 0x14,
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x2d, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73,
0x00, 0x16,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x6d, 0x61, 0x6b, 0x65, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x6c,
0x44, // keywordValueTag
0x00, 0x00,
0x00, 0x16,
0x69, 0x70, 0x70, 0x2d, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x2d, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64,
0x44, // keywordValueTag
0x00, 0x00,
0x00, 0x16,
0x69, 0x70, 0x70, 0x2d, 0x66, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x2d, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64,
0x44, // keywordValueTag
0x00, 0x00,
0x00, 0x19,
0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x2d, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x2d, 0x73, 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64,
0x44,
0x00, 0x00,
0x00, 0x0d,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x73, 0x74, 0x61, 0x74, 0x65,
0x44,
0x00, 0x00,
0x00, 0x15,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x73, 0x74, 0x61, 0x74, 0x65, 0x2d, 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x73,
0x44,
0x00, 0x00,
0x00, 0x15,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x73, 0x74, 0x61, 0x74, 0x65, 0x2d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x03}
func TestUnmarshalRequestPrinterAttributes(T *testing.T) {
buf := bytes.NewBuffer(testRequest)
req := NewRequest(GetPrinterAttributes, 17)
req.UnMarshal(buf)
fmt.Print(req)
assert.Equal(T, versionNumber(0x0101), req.header.versionNumber, "Wrong version number")
assert.Equal(T, GetPrinterAttributes, req.header.operationID, "Wrong Operation")
assert.Equal(T, uint32(17), req.header.requestID, "Wrong request id")
assert.Len(T, req.a.operation, 4)
v := req.GetAttribute("requested-attributes").(*keyWord).sos.values
assert.Len(T, v, 7)
assert.Contains(T, v, "printer-make-and-model")
assert.Contains(T, v, "ipp-versions-supported")
assert.Contains(T, v, "ipp-features-supported")
assert.Contains(T, v, "document-format-supported")
assert.Contains(T, v, "printer-state-reasons")
assert.Contains(T, v, "printer-state-message")
assert.Contains(T, v, "printer-state")
}

View File

@@ -0,0 +1,67 @@
package ipp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type resolution struct {
name string
crossFeedResolution int32
feedResolution int32
units int8
}
func NewResolution(name string, xfres int32, fres int32) *resolution {
r := new(resolution)
r.name = name
r.crossFeedResolution = xfres
r.feedResolution = fres
r.units = 3 // 3 seems to mean dpi (rfc3805)
return r
}
func (r *resolution) unmarshal(byteStream io.Reader) {
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
attributeName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, attributeName)
}
r.name = string(attributeName)
binary.Read(byteStream, binary.BigEndian, &length)
if length != 9 {
panic("Wrong length in resolution")
}
binary.Read(byteStream, binary.BigEndian, &r.crossFeedResolution)
binary.Read(byteStream, binary.BigEndian, &r.feedResolution)
binary.Read(byteStream, binary.BigEndian, &r.units)
}
func (r resolution) Name() string {
return r.name
}
func (r resolution) String() string {
return fmt.Sprintf("%v:%v,%v,%v", r.name, r.crossFeedResolution, r.feedResolution, r.units)
}
func (r *resolution) marshal() []byte {
b := make([]byte, 0, 14+len(r.name))
buf := bytes.NewBuffer(b)
buf.WriteByte(byte(resolutionValueTag))
binary.Write(buf, binary.BigEndian, uint16(len(r.name)))
buf.WriteString(r.name)
binary.Write(buf, binary.BigEndian, uint16(9))
binary.Write(buf, binary.BigEndian, int32(r.crossFeedResolution))
binary.Write(buf, binary.BigEndian, int32(r.feedResolution))
buf.WriteByte(byte(r.units))
return buf.Bytes()
}
func (r *resolution) valueTag() tag {
return resolutionValueTag
}

View File

@@ -1,91 +1,95 @@
package ipp
import "encoding/binary"
import (
"bufio"
"encoding/binary"
"fmt"
"io"
)
type ippResponseHeader struct {
versionNumber versionNumber
statusCode statusCode
requestId uint32
requestID uint32
}
func (h ippResponseHeader) String() string {
return fmt.Sprintf("Version number: %v Status code: %v Request Id: %v", h.versionNumber, h.statusCode, h.requestID)
}
func (h *ippResponseHeader) marshal() []byte {
a := make([]byte, 8, 8)
a := make([]byte, 8)
binary.BigEndian.PutUint16(a[0:2], uint16(h.versionNumber))
binary.BigEndian.PutUint16(a[2:4], uint16(h.statusCode))
binary.BigEndian.PutUint32(a[4:8], h.requestId)
binary.BigEndian.PutUint32(a[4:8], h.requestID)
return a
}
type Response struct {
header ippResponseHeader
operationAttributes []Attribute
jobAttributes []Attribute
printerAttributes []Attribute
func (h *ippResponseHeader) unmarshal(byteStream io.Reader) {
binary.Read(byteStream, binary.BigEndian, &h.versionNumber)
binary.Read(byteStream, binary.BigEndian, &h.statusCode)
binary.Read(byteStream, binary.BigEndian, &h.requestID)
}
func NewResponse(code statusCode, requestId uint32) *Response {
type Response struct {
a *attributes
header ippResponseHeader
}
func NewResponse(code statusCode, requestID uint32) *Response {
r := new(Response)
r.a = new(attributes)
r.header.versionNumber = 0x0101
r.header.requestId = requestId
r.header.requestID = requestID
r.header.statusCode = code
r.operationAttributes = make([]Attribute, 0)
r.printerAttributes = make([]Attribute, 0)
r.jobAttributes = make([]Attribute, 0)
return r
}
// func (r *Response) length() int {
// l := 8 + 1
// if len(r.jobAttributes) > 0 {
// l += 1 //
// for _, e := range r.jobAttributes {
// l += e.length()
// }
// }
// for _, e := range r.operationAttributes {
// l += e.length()
// }
// for _, e := range r.printerAttributes {
// l += e.length()
// }
// }
func (r Response) String() string {
return r.header.String() + "\n" + r.a.String()
}
func (r *Response) Marshal() []byte {
a := make([]byte, 0, 20)
a = append(a, r.header.marshal()...)
if len(r.operationAttributes) > 0 {
if len(r.a.operation) > 0 {
a = append(a, byte(operationAttributes))
for _, e := range r.operationAttributes {
for _, e := range r.a.operation {
a = append(a, e.marshal()...)
}
}
if len(r.jobAttributes) > 0 {
if len(r.a.job) > 0 {
a = append(a, byte(jobAttributes))
for _, e := range r.jobAttributes {
for _, e := range r.a.job {
a = append(a, e.marshal()...)
}
}
if len(r.printerAttributes) > 0 {
if len(r.a.printer) > 0 {
a = append(a, byte(printerAttributes))
for _, e := range r.printerAttributes {
for _, e := range r.a.printer {
a = append(a, e.marshal()...)
}
}
a = append(a, byte(endOfAttributes))
return a
}
func (r *Response) UnMarshal(body io.Reader) {
buffbody := bufio.NewReader(body)
r.header.unmarshal(buffbody)
//log.Printf("Header %v", r.header)
r.a = UnMarshalAttributes(buffbody)
}
func (r *Response) AddPrinterAttribute(a Attribute) {
r.printerAttributes = append(r.printerAttributes, a)
r.a.addAttribute(printerAttributes, a)
}
func (r *Response) AddOperatonAttribute(a Attribute) {
r.operationAttributes = append(r.operationAttributes, a)
r.a.addAttribute(operationAttributes, a)
}
func (r *Response) AddJobAttribute(a Attribute) {
r.jobAttributes = append(r.jobAttributes, a)
r.a.addAttribute(jobAttributes, a)
}

View File

@@ -10,7 +10,7 @@ func TestMarshalResponseHeader(T *testing.T) {
h.versionNumber = 0x0101
h.statusCode = SuccessfulOk
h.requestId = 0xdeadbeef
h.requestID = 0xdeadbeef
b := h.marshal()
fmt.Printf("% x\n", b)

View File

@@ -0,0 +1,88 @@
package ipp
import "encoding/binary"
type setOfStrings struct {
name string
values []string
vTag tag
}
func NewSetOfStrings(name string, t tag, values []string) *setOfStrings {
s := new(setOfStrings)
s.name = name
s.vTag = t
s.values = values //make([]string, 0)
return s
}
func (s setOfStrings) String() string {
r := s.name + " :"
for _, v := range s.values {
r = r + " " + v
}
return r
}
func (s *setOfStrings) valueTag() tag {
return s.vTag
}
// func (k *keyWord) unmarshal(byteStream io.Reader) {
// if len(k.values) == 0 {
// var v string
// k.name, v = unmarshalSingleValue(byteStream)
// k.values = append(k.values, v)
// } else {
// var v string
// _, v = unmarshalSingleValue(byteStream)
// k.values = append(k.values, v)
// }
// }
func (s *setOfStrings) marshal() []byte {
l := 5 + len(s.name) + len(s.values[0])
for i := range s.values[1:] {
l += 5 + len(s.values[i+1])
}
res := make([]byte, l)
p := 0
res[p] = byte(s.vTag)
p += 1
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.name)))
p += 2
copy(res[p:], []byte(s.name))
p += len(s.name)
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.values[0])))
p += 2
copy(res[p:], []byte(s.values[0]))
p += len(s.values[0])
for i := range s.values[1:] {
res[p] = byte(s.vTag)
p += 1
binary.BigEndian.PutUint16(res[p:p+2], uint16(0))
p = p + 2
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.values[i+1])))
p = p + 2
copy(res[p:], []byte(s.values[i+1]))
p += len(s.values[i+1])
}
return res
}
func (s *setOfStrings) AddValue(v string) {
s.values = append(s.values, v)
}
func (s *setOfStrings) size() int {
l := 1 + 2 // The value tag (0x44) + name-length field (2 bytes)
l += len(s.name)
l += 2 // value-length field (2 bytes)
l += len(s.values[0])
// Add all additional values
for _, v := range s.values[1:] {
l += 1 + 4 // The value tag (0x44) + 2 length fields (2 bytes)
l += len(v)
}
return l
}

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,46 @@
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) Name() string {
return c.name
}
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)
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,13 +9,17 @@ 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
return u
}
func (u uriValue) Name() string {
return u.name
}
func (u uriValue) String() string {
return u.name + ":" + u.value
}

View File

@@ -24,7 +24,7 @@ func Run(ctx context.Context) {
log.Fatalf("EntryGroupNew() failed: %v", err)
}
hostname, err := a.GetHostName()
//hostname, err := a.GetHostName()
if err != nil {
log.Fatalf("GetHostName() failed: %v", err)
}
@@ -39,7 +39,8 @@ 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"))
err = eg.AddService(avahi.InterfaceUnspec, avahi.ProtoUnspec, 0, hostname, "_ipp._tcp", "local", fqdn, 1234, txt)
txt = append(txt, []byte("ty=ChroBro 001"))
err = eg.AddService(avahi.InterfaceUnspec, avahi.ProtoUnspec, 0, "ChroBro 1000", "_ipp._tcp", "local", fqdn, 1234, txt)
if err != nil {
log.Fatalf("AddService() failed: %v", err)
}

8
server/handlegetjobs.go Normal file
View File

@@ -0,0 +1,8 @@
package main
import "ippserver/packages/ipp"
func handleGetJobs(r *ipp.Request) *ipp.Response {
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
return response
}

View File

@@ -0,0 +1,28 @@
package main
import "ippserver/packages/ipp"
func handleGetPrinterAttributes(r *ipp.Request) *ipp.Response {
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
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", int32(ipp.Idle))
response.AddOperatonAttribute(a)
a = ipp.NewKeyWord("ipp-versions-supported", "1.0", "1.1", "2.0")
response.AddOperatonAttribute(a)
response.AddOperatonAttribute(ipp.NewKeyWord("ipp-features-supported", "wfds-print-1.0"))
response.AddOperatonAttribute(ipp.NewMimeMediaType("document-format-supported", "image/pwg-raster"))
response.AddOperatonAttribute(ipp.NewKeyWord("media-supported", "iso_a4_210x297mm"))
response.AddOperatonAttribute(ipp.NewKeyWord("sides-supported", "one-sided", "two-sided-long-edge", "two-sided-short-edge"))
response.AddOperatonAttribute(ipp.NewKeyWord("print-color-mode-supported", "auto", "color", "monochrome"))
response.AddOperatonAttribute(ipp.NewResolution("printer-resolution-default", 600, 600))
response.AddOperatonAttribute(ipp.NewBoolean("printer-is-accepting-jobs", true))
return response
}

22
server/handleprintjob.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"io"
"ippserver/packages/ipp"
"os"
)
func handlePrintJob(r *ipp.Request, byteStream io.Reader) *ipp.Response {
a := r.GetAttribute("job-name")
//a.(nameWithoutLanguage).Value
f, err := os.Create(a.(*ipp.NameWithoutLanguage).Value())
if err != nil {
panic("fail")
}
defer f.Close()
io.Copy(f, byteStream)
response := ipp.NewResponse(ipp.SuccessfulOk, r.RequestID())
return response
}

View File

@@ -2,7 +2,6 @@ package main
import (
"context"
"fmt"
"ippserver/packages/ipp"
"ippserver/packages/mdnsserver"
"net/http"
@@ -15,6 +14,7 @@ func main() {
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
log.SetLevel(log.InfoLevel)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
@@ -35,18 +35,35 @@ func handle(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
}
// body := make([]byte, r.ContentLength)
// io.ReadFull(r.Body, body)
// log.Infof("Body %x", body)
log.Info(r.Header)
//body := make([]byte, r.ContentLength)
//io.ReadFull(r.Body, body)
//log.Infof("Body %x", body)
request := ipp.NewRequest()
request := ipp.NewRequest(0, 0)
//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(r.Body)
fmt.Printf("%v", request)
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
a := ipp.NewCharSetValue("attributes-charset", "utf-8")
response.AddOperatonAttribute(a)
log.Infof("Request: \n%v\n", request)
var response *ipp.Response
switch request.Operation() {
case ipp.GetPrinterAttributes:
response = handleGetPrinterAttributes(request)
case ipp.PrintJob:
response = handlePrintJob(request, r.Body)
case ipp.GetJobs:
response = handleGetJobs(request)
default:
response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestID())
}
log.Infof("Response:\n%v\n", response)
data := response.Marshal()
log.Infof("% x", data)
//log.Debugf("% x", data)
w.Write(data)
}