-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcmsg.go
375 lines (317 loc) · 9.17 KB
/
ipcmsg.go
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
/*
* Copyright (c) 2021 Gilles Chehade <gilles@poolp.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ipcmsg
import (
"bytes"
"encoding/binary"
"encoding/gob"
"log"
"os"
"reflect"
"sync"
"syscall"
"github.com/google/uuid"
)
type Channel struct {
name string
w chan *IPCMessage
r chan *IPCMessage
mu sync.Mutex
muQueries sync.Mutex
queries map[uuid.UUID]chan *IPCMessage
muHandlers sync.Mutex
handlers map[IPCMsgType]func(*IPCMessage)
}
const IPCMSG_HEADER_SIZE = 31
type IPCMsgType uint32
type ipcMsgHdr struct {
Id uuid.UUID
Type IPCMsgType
Size uint16
HasFd uint8
Peerid uint32
Pid uint32
}
type IPCMessage struct {
channel *Channel
hdr ipcMsgHdr
fd int
data []byte
}
var msgTypes = make(map[IPCMsgType]string)
func NewIPCMsgType(msgObject interface{}) IPCMsgType {
msgType := IPCMsgType(len(msgTypes))
msgTypes[msgType] = reflect.TypeOf(msgObject).Name()
gob.Register(msgObject)
return msgType
}
func NewChannel(name string, peerid int, fd int) *Channel {
channel := &Channel{}
pid := os.Getpid()
channel.name = name
channel.queries = make(map[uuid.UUID]chan *IPCMessage)
channel.handlers = make(map[IPCMsgType]func(*IPCMessage))
channel.w = make(chan *IPCMessage)
channel.r = make(chan *IPCMessage)
// read message from write channel and send to peer fd
go func() {
for msg := range channel.w {
msg.hdr.Peerid = uint32(peerid)
msg.hdr.Pid = uint32(pid)
// pack msg header and msg data into output buf
obuf := make([]byte, 0)
var packed bytes.Buffer
if err := binary.Write(&packed, binary.BigEndian, &msg.hdr); err != nil {
log.Fatal("NewChannel: binary.Write: ", err)
}
obuf = append(obuf, packed.Bytes()...)
obuf = append(obuf, msg.data...)
// if msg has no FD attached, send as is
if msg.hdr.HasFd == 0 {
err := syscall.Sendmsg(fd, obuf, nil, nil, 0)
if err != nil {
log.Fatal("NewChannel: syscall.Sendmsg (#1): ", err)
}
// annnnnnnd... we're done for this msg
continue
}
// an FD is attached, we need to craft a UnixRights control message
channel.mu.Lock()
err := syscall.Sendmsg(fd, obuf, syscall.UnixRights(msg.fd), nil, 0)
if err != nil {
log.Fatal("NewChannel: syscall.Sendmsg (#2): ", err)
}
err = syscall.Close(msg.fd)
if err != nil {
log.Fatal("NewChannel: syscall.Close: ", err)
}
channel.mu.Unlock()
}
}()
// read message from peer fd and write to read channel
go func() {
// oh gosh... the fun begins
for {
// a buffer to hold the data
buf := make([]byte, 64*1024)
// a cmsgbuf for control message, we only expect 1 fd (4 bytes)
cmsgbuf := make([]byte, syscall.CmsgSpace(1*4))
// read a msg, for now only expects blocking IO
n, oobn, _, _, err := syscall.Recvmsg(fd, buf, cmsgbuf, 0)
if err != nil {
log.Fatal("NewChannel: syscall.Recvmsg:", err)
}
if n == 0 {
break
}
buf = buf[:n]
// sometimes we have an FD, sometimes we don't
// assume there's a control message and try parsing it,
// if it fails then we assume there's no FD
// caller can detect this is IPCMsgHdr.HasFlag is 1 and IpcMsg.Fd == -1
channel.mu.Lock()
cmsg := true
scms, err := syscall.ParseSocketControlMessage(cmsgbuf[:oobn])
if err != nil {
if err != syscall.EINVAL {
log.Fatal("NewChannel: syscall.ParseSocketControlMessage:", err)
}
cmsg = false
}
pfd := -1
if cmsg {
// we have a control message ...
// we're only supposed to have one
if len(scms) != 1 {
log.Fatal("received more than one control message")
}
fds, err := syscall.ParseUnixRights(&scms[0])
if err != nil {
log.Fatal("NewChannel: syscall.ParseUnixRights:", err)
}
// we're only supposed to have one FD
if len(fds) != 1 {
log.Fatal("received more than one FD")
}
pfd = fds[0]
if npfd, err := syscall.Dup(pfd); err != nil {
log.Fatal("NewChannel: syscall.Dup:", err)
} else {
if err := syscall.Close(pfd); err != nil {
log.Fatal("NewChannel: syscall.Close:", err)
}
pfd = npfd
}
}
channel.mu.Unlock()
// we may have multiple messages crammed in our input buffer
// process them sequentially, parsing header and extracting data
for {
// first, decode a header
var hdr_bin bytes.Buffer
var hdr ipcMsgHdr
hdr_bin.Write(buf[:IPCMSG_HEADER_SIZE])
err = binary.Read(&hdr_bin, binary.BigEndian, &hdr)
if err != nil {
log.Fatal("NewChannel: binary.Read:", err)
}
// unsure if this can happen, sanity check
if len(buf) < IPCMSG_HEADER_SIZE+int(hdr.Size) {
log.Fatal("packet too small")
}
// now that we have a header, reset peerid and pid
// extract the right amount of data from input buffer
// and if a FD is supposed to be attached, use the one
// we extracted from control message
msg := &IPCMessage{}
msg.channel = channel
msg.hdr = hdr
msg.hdr.Peerid = uint32(peerid)
msg.hdr.Pid = uint32(pid)
msg.data = buf[IPCMSG_HEADER_SIZE : IPCMSG_HEADER_SIZE+int(msg.hdr.Size)]
msg.fd = -1
if msg.hdr.HasFd != 0 {
if pfd == -1 {
// FD exhaustion on receiving end most-likely
}
msg.fd = pfd
pfd = -1
}
// discard consumed data from input buffer
buf = buf[IPCMSG_HEADER_SIZE+int(msg.hdr.Size):]
// message is ready for caller
channel.r <- msg
// input buffer is empty, go back to read loop
if len(buf) == 0 {
break
}
// not sure if short reads can happen,
// if so they'll be caught by the earlier log.Fatal()
// and I'll move the input buffer out of the goroutine
// into the Channel
}
}
}()
return channel
}
func (channel *Channel) Dispatch() <-chan bool {
done := make(chan bool)
go func() {
for msg := range channel.r {
channel.muQueries.Lock()
callbackChannel, exists := channel.queries[msg.hdr.Id]
delete(channel.queries, msg.hdr.Id)
channel.muQueries.Unlock()
if exists {
callbackChannel <- msg
continue
}
handler, exists := channel.handlers[msg.hdr.Type]
if !exists {
log.Fatalf("channel %s: received unexpected message type %d", channel.name, msg.hdr.Type)
}
handler(msg)
}
done <- true
}()
return done
}
func (channel *Channel) Handler(msgtype IPCMsgType, handler func(*IPCMessage)) {
channel.muHandlers.Lock()
defer channel.muHandlers.Unlock()
channel.handlers[msgtype] = handler
}
func createMessage(msgtype IPCMsgType, data interface{}, fd int) *IPCMessage {
if reflecType, exists := msgTypes[msgtype]; !exists {
panic("unregistered IPC message type")
} else {
if reflect.TypeOf(data).Name() != reflecType {
panic("creating IPC message with invalid data type")
}
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(data)
if err != nil {
panic(err)
}
msg := &IPCMessage{}
msg.hdr = ipcMsgHdr{}
msg.hdr.Id, _ = uuid.NewRandom()
msg.hdr.Type = msgtype
msg.hdr.Size = uint16(len(buf.Bytes()))
if fd == -1 {
msg.hdr.HasFd = 0
} else {
msg.hdr.HasFd = 1
}
msg.data = buf.Bytes()
msg.fd = fd
return msg
}
func createReply(msg IPCMessage, msgtype IPCMsgType, data interface{}, fd int) *IPCMessage {
reply := createMessage(msgtype, data, fd)
reply.hdr.Id = msg.hdr.Id
return reply
}
func (channel *Channel) Message(msgtype IPCMsgType, data interface{}, fd int) {
channel.w <- createMessage(msgtype, data, fd)
}
func (channel *Channel) Query(msgtype IPCMsgType, data interface{}, fd int) *IPCMessage {
wait := make(chan *IPCMessage)
msg := createMessage(msgtype, data, fd)
channel.muQueries.Lock()
channel.queries[msg.hdr.Id] = wait
channel.muQueries.Unlock()
channel.w <- msg
return <-wait
}
func (channel *Channel) ChannelIn() <-chan *IPCMessage {
return channel.r
}
func (channel *Channel) ChannelOut() chan<- *IPCMessage {
return channel.w
}
func (msg *IPCMessage) Type() IPCMsgType {
return msg.hdr.Type
}
func (msg *IPCMessage) TryUnmarshal(v interface{}) error {
dec := gob.NewDecoder(bytes.NewBuffer(msg.data))
return dec.Decode(v)
}
func (msg *IPCMessage) Unmarshal(v interface{}) {
err := msg.TryUnmarshal(v)
if err != nil {
panic(err)
}
}
func (msg *IPCMessage) HasFd() bool {
return msg.hdr.HasFd == 1
}
func (msg *IPCMessage) Fd() int {
return msg.fd
}
func (msg *IPCMessage) Reply(msgtype IPCMsgType, data interface{}, fd int) {
msg.channel.w <- createReply(*msg, msgtype, data, fd)
}
func (msg *IPCMessage) OneOf(msgtypes ...IPCMsgType) *IPCMessage {
for _, msgtype := range msgtypes {
if msg.Type() == msgtype {
return msg
}
}
panic("IPCMessage type assertion failed")
}