File: message.go

package info (click to toggle)
golang-go-dbus 1~bzr20150122-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: makefile: 13
file content (379 lines) | stat: -rw-r--r-- 10,519 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package dbus

import (
	"encoding/binary"
	"errors"
	"io"
)

// See the D-Bus tutorial for information about message types.
//		http://dbus.freedesktop.org/doc/dbus-tutorial.html#messages
type MessageType uint8

const (
	TypeInvalid MessageType = iota
	TypeMethodCall
	TypeMethodReturn
	TypeError
	TypeSignal
)

var messageTypeString = map[MessageType]string{
	TypeInvalid:      "invalid",
	TypeMethodCall:   "method_call",
	TypeMethodReturn: "method_return",
	TypeSignal:       "signal",
	TypeError:        "error",
}

func (t MessageType) String() string { return messageTypeString[t] }

type MessageFlag uint8

const (
	// When applied to method call messages, indicates that no
	// method return or error message is expected.
	FlagNoReplyExpected MessageFlag = 1 << iota
	// Indicates that the message should not autostart the service
	// if the destination service is not currently running.
	FlagNoAutoStart
)

// Message represents a D-Bus message.
//
// It is used to both construct messages for sending on the bus, and
// to represent messages received from the bus.
//
// There type does not use locks to protect its internal members.
// Instead, it is expected that users either (a) only modify a message
// from a single thread (usually the case when constructing a message
// to send), or (b) treat the message as read only (usually the case
// when processing a received message).
type Message struct {
	order    binary.ByteOrder
	Type     MessageType
	Flags    MessageFlag
	Protocol uint8
	serial   uint32
	// header fields
	Path        ObjectPath
	Interface   string
	Member      string
	ErrorName   string
	replySerial uint32
	Dest        string
	Sender      string
	sig         Signature
	body        []byte
}

// Create a new message with Flags == 0 and Protocol == 1.
func newMessage() *Message {
	msg := new(Message)

	msg.order = binary.LittleEndian
	msg.serial = 0
	msg.replySerial = 0
	msg.Flags = 0
	msg.Protocol = 1

	msg.body = []byte{}

	return msg
}

// NewMethodCallMessage creates a method call message.
//
// Method arguments can be appended to the message via AppendArgs.
func NewMethodCallMessage(destination string, path ObjectPath, iface string, member string) *Message {
	msg := newMessage()
	msg.Type = TypeMethodCall
	msg.Dest = destination
	msg.Path = path
	msg.Interface = iface
	msg.Member = member
	return msg
}

// NewMethodReturnMessage creates a method return message.
//
// This message type represents a successful reply to the method call
// message passed as an argument.
//
// Return arguments should be appended to the message via AppendArgs.
func NewMethodReturnMessage(methodCall *Message) *Message {
	if methodCall.serial == 0 {
		panic("methodCall.serial == 0")
	}
	if methodCall.Type != TypeMethodCall {
		panic("replies should be sent in response to method calls")
	}
	msg := newMessage()
	msg.Type = TypeMethodReturn
	msg.replySerial = methodCall.serial
	msg.Dest = methodCall.Sender
	return msg
}

// NewSignalMessage creates a signal message.
//
// Signal messages are used to broadcast messages to interested
// listeners.
//
// Arguments can be appended to the signal with AppendArgs.
func NewSignalMessage(path ObjectPath, iface string, member string) *Message {
	msg := newMessage()
	msg.Type = TypeSignal
	msg.Path = path
	msg.Interface = iface
	msg.Member = member
	return msg
}

// NewErrorMessage creates an error message.
//
// This message type should be sent in response to a method call
// message in the case of a failure.
func NewErrorMessage(methodCall *Message, errorName string, message string) *Message {
	if methodCall.serial == 0 {
		panic("methodCall.serial == 0")
	}
	if methodCall.Type != TypeMethodCall {
		panic("errors should be sent in response to method calls")
	}
	msg := newMessage()
	msg.Type = TypeError
	msg.replySerial = methodCall.serial
	msg.Dest = methodCall.Sender
	msg.ErrorName = errorName
	if err := msg.AppendArgs(message); err != nil {
		panic(err)
	}
	return msg
}

func (p *Message) setSerial(serial uint32) {
	if p.serial != 0 {
		panic("Message already has a serial number")
	}
	p.serial = serial
}

// AppendArgs appends arguments to a message.
//
// Native Go types are converted to equivalent D-Bus types:
//  - uint8 represents a byte.
//  - bool represents a boolean value.
//  - int16, uint16, int32, uint32, int64 and uint64 represent the
//    equivalent integer types.
//  - string represents a string.
//  - The dbus.ObjectPath type or any type conforming to the
//    dbus.ObjectPather interface represents an object path.
//  - arrays and slices represent arrays of the element type.
//  - maps represent equivalent D-Bus dictionaries.
//  - structures represent a structure comprising the public members.
//  - the dbus.Variant type represents a variant.
//
// If an argument can not be serialised in the message, an error is
// returned.  When multiple arguments are being appended, it is
// possible for some arguments to be successfully appended before the
// error is generated.
func (p *Message) AppendArgs(args ...interface{}) error {
	enc := newEncoder(p.sig, p.body, p.order)
	if err := enc.Append(args...); err != nil {
		return err
	}
	p.sig = enc.signature
	p.body = enc.data.Bytes()
	return nil
}

// Args decodes one or more arguments from the message.
//
// The arguments should be pointers to variables used to hold the
// arguments.  If the type of the argument does not match the
// corresponding argument in the message, then an error will be
// raised.
//
// As a special case, arguments may be decoded into a blank interface
// value.  This may result in a less useful decoded version though
// (e.g. an "ai" message argument would be decoded as []interface{}
// instead of []int32).
func (p *Message) Args(args ...interface{}) error {
	dec := newDecoder(p.sig, p.body, p.order)
	return dec.Decode(args...)
}

// AllArgs returns all arguments in the message.
//
// This method is equivalent to calling Args and passing pointers
// to blank interface values for each message argument.
func (p *Message) AllArgs() []interface{} {
	dec := newDecoder(p.sig, p.body, p.order)
	args := make([]interface{}, 0)
	for dec.HasMore() {
		var arg interface{}
		if err := dec.Decode(&arg); err != nil {
			panic(err)
		}
		args = append(args, arg)
	}
	return args
}

// AsError creates a Go error value corresponding to a message.
//
// This method should only be called on messages of the error type.
func (p *Message) AsError() error {
	if p.Type != TypeError {
		panic("Only messages of type 'error' can be converted to an error")
	}
	var errorMessage string
	if err := p.Args(&errorMessage); err != nil {
		// Ignore error
		errorMessage = ""
	}
	return &Error{p.ErrorName, errorMessage}
}

type headerField struct {
	Code  byte
	Value Variant
}

func readMessage(r io.Reader) (*Message, error) {
	header := make([]byte, 16)
	if n, err := r.Read(header); n < len(header) {
		if err == nil {
			err = errors.New("Could not read message header")
		}
		return nil, err
	}

	msg := newMessage()
	switch header[0] {
	case 'l':
		msg.order = binary.LittleEndian
	case 'B':
		msg.order = binary.BigEndian
	default:
		return nil, errors.New("Unknown message endianness: " + string(header[0]))
	}
	dec := newDecoder("yyyyuuu", header, msg.order)
	var msgOrder byte
	var msgBodyLength, headerFieldsLength uint32
	if err := dec.Decode(&msgOrder, &msg.Type, &msg.Flags, &msg.Protocol, &msgBodyLength, &msg.serial, &headerFieldsLength); err != nil {
		return nil, err
	}

	// Read out and decode the header fields, plus the padding to
	// 8 bytes.
	padding := -(len(header) + int(headerFieldsLength)) % 8
	if padding < 0 {
		padding += 8
	}
	headerFields := make([]byte, 16+int(headerFieldsLength)+padding)
	copy(headerFields[:16], header)
	if n, err := r.Read(headerFields[16:]); n < len(headerFields)-16 {
		if err == nil {
			err = errors.New("Could not read message header fields")
		}
		return nil, err
	}
	dec = newDecoder("a(yv)", headerFields, msg.order)
	dec.dataOffset += 12
	fields := make([]headerField, 0, 10)
	if err := dec.Decode(&fields); err != nil {
		return nil, err
	}
	for _, field := range fields {
		switch field.Code {
		case 1:
			msg.Path = field.Value.Value.(ObjectPath)
		case 2:
			msg.Interface = field.Value.Value.(string)
		case 3:
			msg.Member = field.Value.Value.(string)
		case 4:
			msg.ErrorName = field.Value.Value.(string)
		case 5:
			msg.replySerial = field.Value.Value.(uint32)
		case 6:
			msg.Dest = field.Value.Value.(string)
		case 7:
			msg.Sender = field.Value.Value.(string)
		case 8:
			msg.sig = field.Value.Value.(Signature)
		}
	}

	msg.body = make([]byte, msgBodyLength)
	if n, err := r.Read(msg.body); n < len(msg.body) {
		if err == nil {
			err = errors.New("Could not read message body")
		}
		return nil, err
	}
	return msg, nil
}

// WriteTo serialises the message and writes it to the given writer. Not atomic!
func (p *Message) WriteTo(w io.Writer) (int64, error) {
	fields := make([]headerField, 0, 10)
	if p.Path != "" {
		fields = append(fields, headerField{1, Variant{p.Path}})
	}
	if p.Interface != "" {
		fields = append(fields, headerField{2, Variant{p.Interface}})
	}
	if p.Member != "" {
		fields = append(fields, headerField{3, Variant{p.Member}})
	}
	if p.ErrorName != "" {
		fields = append(fields, headerField{4, Variant{p.ErrorName}})
	}
	if p.replySerial != 0 {
		fields = append(fields, headerField{5, Variant{p.replySerial}})
	}
	if p.Dest != "" {
		fields = append(fields, headerField{6, Variant{p.Dest}})
	}
	if p.Sender != "" {
		fields = append(fields, headerField{7, Variant{p.Sender}})
	}
	if p.sig != "" {
		fields = append(fields, headerField{8, Variant{p.sig}})
	}

	var orderTag byte
	switch p.order {
	case binary.LittleEndian:
		orderTag = 'l'
	case binary.BigEndian:
		orderTag = 'B'
	default:
		return 0, errors.New("Unknown byte order: " + p.order.String())
	}

	header := newEncoder("", nil, p.order)
	if err := header.Append(orderTag, byte(p.Type), byte(p.Flags), byte(p.Protocol), uint32(len(p.body)), p.serial, fields); err != nil {
		return 0, err
	}

	// Add alignment bytes for body
	header.align(8)
	m, err := w.Write(header.data.Bytes())
	if err != nil {
		return int64(m), err
	} else if m != header.data.Len() {
		return int64(m), errors.New("Failed to write complete message header")
	}

	n, err := w.Write(p.body)
	if err != nil {
		return int64(m + n), err
	} else if n != len(p.body) {
		return int64(m + n), errors.New("Failed to write complete message body")
	}
	return int64(m + n), nil
}