-
Notifications
You must be signed in to change notification settings - Fork 8
/
connection.go
316 lines (275 loc) · 6.97 KB
/
connection.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
package gowl
import (
"encoding/binary"
"bytes"
"fmt"
"io"
"bufio"
"syscall"
"os"
"net"
)
type Connection struct {
unixconn *net.UnixConn
addr *net.UnixAddr
reader *bufio.Reader
writer *bufio.Writer
alive bool
}
type message struct {
obj Object
opcode int16
buf *bytes.Buffer
fd uintptr
}
var conn Connection
func connect_to_socket() error {
// Connect to socket
addr := fmt.Sprintf("%s/%s", os.Getenv("XDG_RUNTIME_DIR"), "wayland-0")
c,err := net.DialUnix("unix", conn.addr, &net.UnixAddr{addr, "unix"})
if err != nil {
return err
}
conn.reader = bufio.NewReader(c)
conn.writer = bufio.NewWriter(c)
conn.unixconn = c
conn.alive = true
return nil
}
func getmsg() (msgs []message, err error) {
// Get all messages in buffer
b := make([]byte, 1024)
oob := make([]byte, 1024)
n, oobn, flags,_, err := conn.unixconn.ReadMsgUnix(b, oob)
fd := uintptr(0)
if err != nil {
return
}
bbuf := bytes.NewBuffer(b)
oobbuf := bytes.NewBuffer(oob)
if oobn != 0 {
fmt.Println("Out-of-band recv: Len",oobn,oobbuf.Bytes()[:oobn], flags)
readInt32(oobbuf)
readInt32(oobbuf)
readInt32(oobbuf)
fd,err = readUintptr(oobbuf)
fmt.Println("Fd is",fd)
dup,_ := syscall.Dup(int(fd))
fd = uintptr(dup)
fmt.Println("Fd is",fd)
if err != nil {
fmt.Println("Had an error:", err)
return
}
}
read := 0
for read < n {
var msg message
id,err := readInt32(bbuf)
if err != nil {
break
}
msg.obj = getObject(id)
msg.opcode, err = readInt16(bbuf)
if err != nil {
break
}
size, err := readInt16(bbuf)
if err != nil {
break
}
msg.buf = bytes.NewBuffer(bbuf.Next(int(size-8)))
if err != nil {
break
}
read += int(size)
if fd != 0 {
msg.fd = fd
}
msgs = append(msgs, msg)
fmt.Printf("%s@%d.%d(%d)\n", msg.obj.Name(), msg.obj.Id(), msg.opcode, msg.buf.Bytes())
}
return
}
func sendmsg(msg *message) {
size := len(msg.buf.Bytes())
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, msg.obj.Id())
binary.Write(buf, binary.LittleEndian, msg.opcode)
binary.Write(buf, binary.LittleEndian, int16(size+8))
binary.Write(buf, binary.LittleEndian, msg.buf.Bytes())
var cmsgbytes []byte
if msg.fd != 0 {
cmsgbytes = syscall.UnixRights(int(msg.fd))
}
_,_,err := conn.unixconn.WriteMsgUnix(buf.Bytes(), cmsgbytes, nil)
if err != nil {
fmt.Println("sendmsg",err)
return
}
syscall.Close(int(msg.fd))
}
func sendrequest(obj Object, req string, args ...interface{}) {
if conn.alive != true {
return
}
data := new(bytes.Buffer)
dbg := new(bytes.Buffer)
size := 8
fd := uintptr(0)
signature := signatures[req]
opcode := signature.opcode
sig := signature.signature
for i,arg := range args {
switch sig[i] {
case 'i', 'f':
size += 4
binary.Write(data, binary.LittleEndian, arg.(int32))
fmt.Fprintf(dbg, "%d ", arg)
case 'u':
size += 4
binary.Write(data, binary.LittleEndian, arg.(uint32))
fmt.Fprintf(dbg, "%d ", arg)
case 'h':
fd,_,_ = syscall.Syscall(syscall.SYS_FCNTL, arg.(uintptr), syscall.F_DUPFD_CLOEXEC, 0)
case 'o':
size += 4
binary.Write(data, binary.LittleEndian, arg.(Object).Id())
fmt.Fprintf(dbg, "%d ", arg.(Object).Id())
case 'n':
size += 4
nobj := arg.(Object)
appendObject(nobj)
binary.Write(data, binary.LittleEndian, nobj.Id())
fmt.Fprintf(dbg, "new_id %d ", nobj.Id())
case 's':
// First get padding
str := arg.(string)
pad := 4-(len(str) % 4)
binary.Write(data, binary.LittleEndian, int32(len(str)+pad))
binary.Write(data, binary.LittleEndian, []byte(str))
for i := 0 ; i < pad ; i++ {
binary.Write(data, binary.LittleEndian, []byte{0})
}
size += len(str)+pad+4
fmt.Fprintf(dbg, "%s ", str)
}
}
fmt.Printf(" -> %s@%d ( %s)\n",req,obj.Id(), dbg)
// Send message
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, obj.Id())
binary.Write(buf, binary.LittleEndian, opcode)
binary.Write(buf, binary.LittleEndian, int16(size))
binary.Write(buf, binary.LittleEndian, data.Bytes())
var cmsgbytes []byte
if fd != 0 {
cmsgbytes = syscall.UnixRights(int(fd))
}
_,_,err := conn.unixconn.WriteMsgUnix(buf.Bytes(), cmsgbytes, nil)
if err != nil {
fmt.Println("sendrequest",err)
os.Exit(1)
return
}
syscall.Close(int(fd))
}
func readUintptr(buf io.Reader) (uintptr, error) {
var val uint32
err := binary.Read(buf, binary.LittleEndian, &val)
if err != nil {
return uintptr(val), err
}
return uintptr(val), nil
}
func readUint32(buf io.Reader) (uint32, error) {
var val uint32
err := binary.Read(buf, binary.LittleEndian, &val)
if err != nil {
return val, err
}
return val, nil
}
func readInt32(c io.Reader) (int32, error) {
var val int32
err := binary.Read(c, binary.LittleEndian, &val)
if err != nil {
return val, err
}
return val, nil
}
func readFixed(c io.Reader) (int32, error) {
var val int32
err := binary.Read(c, binary.LittleEndian, &val)
if err != nil {
return val, err
}
return val/256, nil
}
func readUint16(c io.Reader) (uint16, error) {
var val uint16
err := binary.Read(c, binary.LittleEndian, &val)
if err != nil {
return val, err
}
return val, nil
}
func readInt16(c io.Reader) (int16, error) {
var val int16
err := binary.Read(c, binary.LittleEndian, &val)
if err != nil {
return val, err
}
return val, nil
}
func readString(c io.Reader) (string, error) {
// First get string length
strlen, err := readUint32(c)
if err != nil {
return "", err
}
// Now get string
str := make([]byte, strlen-1)
_,err = c.Read(str)
if err != nil {
return "", err
}
pad := 4-(strlen % 4)
if pad == 4 {
pad = 0
}
for i := uint32(0) ; i <= pad ; i++ {
binary.Read(c, binary.LittleEndian, []byte{0})
}
return string(str), nil
}
func readArray(c io.Reader) ([]interface{}, error) {
return nil, nil
}
func writeInteger(msg *message, val interface{}) {
err := binary.Write(msg.buf, binary.LittleEndian, val)
if err != nil {
fmt.Println(err)
}
}
func writeString(msg *message, val []byte) {
// First get padding
pad := 4-(len(val) % 4)
writeInteger(msg, int32(len(val)+pad))
binary.Write(msg.buf, binary.LittleEndian, val)
for i := 0 ; i < pad ; i++ {
binary.Write(msg.buf, binary.LittleEndian, []byte{0})
}
}
func writeFd(msg *message, val uintptr) {
newfd,_,_ := syscall.Syscall(syscall.SYS_FCNTL, val, syscall.F_DUPFD_CLOEXEC, 0)
msg.fd = newfd
}
func newMessage(obj Object, opcode int16) *message {
return &message{
obj: obj,
opcode: opcode,
buf: new(bytes.Buffer),
fd: 0,
}
}