forked from 0e4ef622/wine-discord-ipc-bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
418 lines (351 loc) · 11.7 KB
/
main.c
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <tchar.h>
#include <strsafe.h>
struct sockaddr_un {
unsigned short sun_family; /* AF_UNIX */
char sun_path[108]; /* pathname */
};
/* struct sockaddr { */
/* unsigned short sa_family; // address family, AF_xxx */
/* char sa_data[14]; // 14 bytes of protocol address */
/* }; */
#define AF_UNIX 1
#define SOCK_STREAM 1
#define F_SETFL 4
#define O_RDONLY 00000000
#define O_WRONLY 00000001
#define O_CREAT 00000100
#define O_APPEND 00002000
#define O_NONBLOCK 00004000
#define BUFSIZE 2048 // size of read/write buffers
__declspec(naked) unsigned int l_getpid() {
__asm__ (
"mov eax, 0x14\n\t"
"int 0x80\n\t"
"ret"
);
}
__declspec(naked) int l_close(int fd) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x06\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_socketcall(int call, void* args) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x66\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"mov ecx, [esp + 4 + 8]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x37\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"mov ecx, [esp + 4 + 8]\n\t"
"mov edx, [esp + 4 + 12]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_open(const char* filename, int flags, int mode) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x05\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"mov ecx, [esp + 4 + 8]\n\t"
"mov edx, [esp + 4 + 12]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_write(unsigned int fd, const char* buf, unsigned int count) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x04\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"mov ecx, [esp + 4 + 8]\n\t"
"mov edx, [esp + 4 + 12]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
__declspec(naked) int l_read(unsigned int fd, char* buf, unsigned int count) {
__asm__ (
"push ebx\n\t"
"mov eax, 0x03\n\t"
"mov ebx, [esp + 4 + 4]\n\t"
"mov ecx, [esp + 4 + 8]\n\t"
"mov edx, [esp + 4 + 12]\n\t"
"int 0x80\n\t"
"pop ebx\n\t"
"ret"
);
}
int l_socket(int domain, int type, int protocol) {
void* args[3];
args[0] = (void*)(int*)domain;
args[1] = (void*)(int*)type;
args[2] = (void*)(int*)protocol;
return l_socketcall(1, args);
}
int l_connect(int sockfd, const struct sockaddr *addr, unsigned int addrlen) {
void* args[3];
args[0] = (void*)(int*)sockfd;
args[1] = (void*)addr;
args[2] = (void*)(int*)addrlen;
return l_socketcall(3, args);
}
/* int send(int sockfd, const void* buf, unsigned int len, int flags) { */
/* void* args[4]; */
/* args[0] = (void*)(int*)sockfd; */
/* args[1] = (void*)buf; */
/* args[2] = (void*)(unsigned int*)len; */
/* args[3] = (void*)(int*)flags; */
/* return l_socketcall(9, args); */
/* } */
/* int recv(int fd, void* buf, unsigned int len, int flags) { */
/* void* args[4]; */
/* args[0] = (void*)(int*)fd; */
/* args[1] = (void*)buf; */
/* args[2] = (void*)(unsigned int*)len; */
/* args[3] = (void*)(int*)flags; */
/* return l_socketcall(10, args); */
/* } */
char* getenv_(char* name) // written by https://github.com/Francesco149
{
static char buf[1024 * 1024];
static char* end = 0;
unsigned int namelen;
char* p;
if (!end) {
int fd, n;
fd = l_open("/proc/self/environ", 0, 0);
if (fd < 0) {
return 0;
}
n = l_read((unsigned int)fd, buf, (unsigned int)sizeof(buf));
if (n < 0) {
return 0;
}
l_close(fd);
end = buf + n;
}
namelen = strlen(name);
for (p = buf; p < end;) {
if (!strncmp(p, name, namelen)) {
return p + namelen + 1; /* skip name and the = */
}
for (; *p && p < end; ++p); /* skip to next entry */
++p;
}
return 0;
}
static const char* get_temp_path()
{
const char* temp = getenv_("XDG_RUNTIME_DIR");
temp = temp ? temp : getenv_("TMPDIR");
temp = temp ? temp : getenv_("TMP");
temp = temp ? temp : getenv_("TEMP");
temp = temp ? temp : "/tmp";
return temp;
}
static HANDLE hPipe = INVALID_HANDLE_VALUE;
static int sock_fd;
DWORD WINAPI winwrite_thread(LPVOID lpvParam);
// koukuno: This implemented because if no client ever connects but there are no more user
// wine processes running, this bridge can just exit gracefully.
static HANDLE conn_evt = INVALID_HANDLE_VALUE;
static BOOL fConnected = FALSE;
static HANDLE make_wine_system_process()
{
HMODULE ntdll_mod;
FARPROC proc;
if ((ntdll_mod = GetModuleHandleW(L"NTDLL.DLL")) == NULL) {
printf("Cannot find NTDLL.DLL in process map");
return NULL;
}
if ((proc = GetProcAddress(ntdll_mod, "__wine_make_process_system")) == NULL) {
printf("Not a wine installation?");
return NULL;
}
return ((HANDLE (CDECL *)(void))proc)();
}
DWORD WINAPI wait_for_client(LPVOID param)
{
(void)param;
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
SetEvent(conn_evt);
return 0;
}
int main(void)
{
DWORD dwThreadId = 0;
HANDLE hThread = NULL;
HANDLE wine_evt = NULL;
if ((wine_evt = make_wine_system_process()) == NULL) {
return 1;
}
// The main loop creates an instance of the named pipe and
// then waits for a client to connect to it. When the client
// connects, a thread is created to handle communications
// with that client, and this loop is free to wait for the
// next client connect request. It is an infinite loop.
printf("Opening discord-ipc-0 Windows pipe\n");
hPipe = CreateNamedPipeW(
L"\\\\.\\pipe\\discord-ipc-0", // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_BYTE | // message type pipe
PIPE_READMODE_BYTE | // message-read mode
PIPE_WAIT, // blocking mode
1, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
printf("CreateNamedPipe failed, GLE=%lu.\n", GetLastError());
return -1;
}
conn_evt = CreateEventW(NULL, FALSE, FALSE, NULL);
CloseHandle(CreateThread(NULL, 0, wait_for_client, NULL, 0, NULL));
for (;;) {
HANDLE events[] = { wine_evt, conn_evt };
DWORD result = WaitForMultipleObjectsEx(2, events, FALSE, INFINITE, FALSE);
if (result == WAIT_TIMEOUT)
continue;
if (result == 0) {
printf("closing bridge since wine is exiting\n");
}
break;
}
if (fConnected)
{
printf("Client connected\n");
printf("Creating socket\n");
if ((sock_fd = l_socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
printf("Failed to create socket\n");
return 1;
}
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
const char *const temp_path = get_temp_path();
char connected = 0;
for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/discord-ipc-%d", temp_path, pipeNum);
printf("Attempting to connect to %s\n", addr.sun_path);
if (l_connect(sock_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
printf("Failed to connect\n");
} else {
connected = 1;
break;
}
}
if (!connected) {
printf("Could not connect to discord client\n");
return 1;
}
printf("Connected successfully\n");
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
winwrite_thread, // thread proc
(LPVOID) NULL, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
printf("CreateThread failed, GLE=%lu.\n", GetLastError());
return 1;
}
for (;;) {
char buf[BUFSIZE];
DWORD bytes_read = 0;
BOOL fSuccess = ReadFile(
hPipe, // handle to pipe
buf, // buffer to receive data
BUFSIZE, // size of buffer
&bytes_read, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess) {
if (GetLastError() == ERROR_BROKEN_PIPE) {
printf("winread EOF\n");
return 0;
} else {
printf("Failed to read from pipe\n");
return 1;
}
}
printf("%ld bytes w->l\n", bytes_read);
/* uncomment to dump the actual data being passed from the pipe to the socket */
/* for(int i=0;i<bytes_read;i++)putchar(buf[i]); */
/* printf("\n"); */
int total_written = 0, written = 0;
while (total_written < bytes_read) {
written = l_write(sock_fd, buf + total_written, bytes_read - total_written);
if (written < 0) {
printf("Failed to write to socket\n");
return 1;
}
total_written += written;
written = 0;
}
}
}
else
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
CloseHandle(wine_evt);
CloseHandle(conn_evt);
return 0;
}
DWORD WINAPI winwrite_thread(LPVOID lpvParam) {
for (;;) {
char buf[BUFSIZE];
int bytes_read = l_read(sock_fd, buf, BUFSIZE);
if (bytes_read < 0) {
printf("Failed to read from socket\n");
l_close(sock_fd);
return 1;
} else if (bytes_read == 0) {
printf("EOF\n");
break;
}
printf("%d bytes l->w\n", bytes_read);
/* uncomment to dump the actual data being passed from the socket to the pipe */
/* for(int i=0;i<bytes_read;i++)putchar(buf[i]); */
/* printf("\n"); */
DWORD total_written = 0, cbWritten = 0;
while (total_written < bytes_read) {
BOOL fSuccess = WriteFile(
hPipe, // handle to pipe
buf + total_written, // buffer to write from
bytes_read - total_written, // number of bytes to write
&cbWritten, // number of bytes written
NULL); // not overlapped I/O
if (!fSuccess) {
printf("Failed to write to pipe\n");
return 1;
}
total_written += cbWritten;
cbWritten = 0;
}
}
return 1;
}