-
Notifications
You must be signed in to change notification settings - Fork 22
/
ipcbridge.py
211 lines (190 loc) · 5.59 KB
/
ipcbridge.py
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
from select import select
from socket import *
from struct import pack, unpack
from threading import Thread
import sys
from util import *
PORT = 31337
buffers = []
nullbuf = '\0' * (1024 * 1024)
def start(ctu):
Thread(target=threadstart, args=(ctu, )).start()
def threadstart(ctu):
def waitSock(sock):
while True:
rl, wl, el = select([sock], [], [], .1)
if ctu.exiting:
sys.exit()
elif len(rl):
break
server = socket(AF_INET, SOCK_STREAM)
server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
server.bind(('', PORT))
server.listen(5)
while True:
waitSock(server)
sock, _ = server.accept()
while not ctu.threads.startupCompleted:
time.sleep(0.1)
for i in xrange(24):
addr = (i + 1) * (1 << 20) + (1 << 28)
ctu.map(addr, 1024 * 1024)
buffers.append(addr)
def readint(default=None):
waitSock(sock)
data = sock.recv(8)
if len(data) != 8:
return default
return unpack('<Q', data)[0]
def readdata(default=None):
waitSock(sock)
size = readint()
if size is None:
return default
odata = ''
while len(odata) != size:
data = sock.recv(size - len(odata))
if len(data) == 0:
return None
odata += data
return odata
def writeint(v):
try:
sock.send(pack('<Q', v))
except:
pass
def writedata(v):
try:
writeint(len(v))
sock.send(v)
except:
pass
openHandles = []
while True:
waitSock(sock)
cmd = readint()
if cmd is None: # Connection dropped
break
elif cmd == 0: # Open service
name = readdata()
if name is None:
break
print 'Open service', name
port = servicePorts[name]
port.acquire()
cp, sp = Pipe.new()
cp.accept()
port.push(sp)
port.release()
sp.waitForAccept()
handle = ctu.newHandle(cp)
openHandles.append(handle)
writeint(handle)
elif cmd == 1: # Close handle
handle = readint()
if handle is None:
break
print 'Close handle', handle
openHandles = [x for x in openHandles if x != handle]
ctu.closeHandle(handle)
elif cmd == 2: # IPC Message
request_type = readint()
data = [readint() for i in xrange(readint(0))]
pid = readint()
copy = [readint() for i in xrange(readint(0))]
move = [readint() for i in xrange(readint(0))]
a = [(readdata(), readint()) for i in xrange(readint(0))]
b = [(readdata(), readint()) for i in xrange(readint(0))]
c = [readdata() for i in xrange(readint(0))]
x = [(readdata(), readint()) for i in xrange(readint(0))]
handle = readint()
if handle is None:
break
msg = IPCMessage(data[0])
msg.setType(request_type)
msg.data(*data[1:])
if pid != 0xFFFFFFFFFFFFFFFF:
msg.hasPID(pid)
map(msg.copyHandle, copy)
map(msg.moveHandle, move)
bufI = 0
for data, perms in a:
if len(data) == 0:
addr = 0
else:
addr = buffers[bufI]
ctu.writemem(addr, nullbuf, check=False)
ctu.writemem(addr, data)
print 'A descriptor at %x' % addr
msg.aDescriptor(addr, len(data), perms)
bufI += 1
for data, perms in b:
if len(data) == 0:
addr = 0
else:
addr = buffers[bufI]
ctu.writemem(addr, nullbuf, check=False)
ctu.writemem(addr, data)
print 'B descriptor at %x' % addr
msg.bDescriptor(addr, len(data), perms)
bufI += 1
for data in c:
if len(data) == 0:
addr = 0xf00ba1
else:
addr = buffers[bufI]
ctu.writemem(addr, nullbuf)
ctu.writemem(addr, data)
print 'C descriptor at %x' % addr
msg.cDescriptor(addr, len(data))
bufI += 1
for data, counter in x:
if len(data) == 0:
addr = 0xf00ba3
else:
addr = buffers[bufI]
ctu.writemem(addr, nullbuf)
ctu.writemem(addr, data)
print 'X descriptor at %x' % addr
msg.xDescriptor(addr, len(data), counter)
bufI += 1
msg.request = True
data = msg.pack()
data = pack('<' + 'I' * len(data), *[(x if x is not None else 0) & 0xFFFFFFFF for x in data])
obj = ctu.handles[handle]
print 'IPC message to', obj, obj.other, msg
if obj.closed:
print 'But pipe is closed!'
writeint(0xf601)
continue
obj.push(data)
resp = obj.pop()
if resp is None:
print 'IPC port closed!'
openHandles = [x for x in openHandles if x != handle]
writeint(0xf601)
else:
writeint(0)
resp = IPCMessage().unpack(unpack('<' + 'I' * (len(resp) / 4), resp), request=False)
writeint(len(resp.dataBuffer) + 1)
map(writeint, [resp.cmdId] + resp.dataBuffer)
writeint(len(resp.copiedHandles))
map(writeint, resp.copiedHandles)
writeint(len(resp.movedHandles))
map(writeint, resp.movedHandles)
writeint(len(msg.aDescriptors))
[(writedata(ctu.readmem(addr, size)), writeint(perms)) for addr, size, perms in msg.aDescriptors]
writeint(len(msg.bDescriptors))
[(writedata(ctu.readmem(addr, size)), writeint(perms)) for addr, size, perms in msg.bDescriptors]
writeint(len(msg.cDescriptors))
[writedata(ctu.readmem(addr, size)) for addr, size in msg.cDescriptors]
writeint(len(msg.xDescriptors))
[(writedata(ctu.readmem(addr, size)), writeint(counter)) for addr, size, counter in msg.xDescriptors]
writeint(resp.type)
openHandles += resp.movedHandles
for handle in openHandles:
print 'IPC bridge closing handle %x' % handle
ctu.closeHandle(handle)
servicePorts = {}
def register(name, port):
servicePorts[name] = port