This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
forked from discord/discord-rpc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnection_win.cpp
294 lines (256 loc) · 5.67 KB
/
connection_win.cpp
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
#include "connection.h"
#include <stdio.h>
#include <string.h>
#pragma region
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
#pragma endregion wine-specific header thingy
#pragma region
_declspec(naked) unsigned int getpid() {
__asm {
mov eax, 0x14
int 0x80
ret
}
}
_declspec(naked) int close(int fd) {
__asm {
push ebx
mov eax, 0x06
mov ebx, [esp + 4 + 4]
int 0x80
pop ebx
ret
}
}
_declspec(naked) int socketcall(int call, void* args) {
__asm {
push ebx
mov eax, 0x66
mov ebx, [esp + 4 + 4]
mov ecx, [esp + 4 + 8]
int 0x80
pop ebx
ret
}
}
_declspec(naked) int fcntl(unsigned int fd, unsigned int cmd, unsigned long arg) {
__asm {
push ebx
mov eax, 0x37
mov ebx, [esp + 4 + 4]
mov ecx, [esp + 4 + 8]
mov edx, [esp + 4 + 12]
int 0x80
pop ebx
ret
}
}
_declspec(naked) int open(const char* filename, int flags, int mode) {
__asm {
push ebx
mov eax, 0x05
mov ebx, [esp + 4 + 4]
mov ecx, [esp + 4 + 8]
mov edx, [esp + 4 + 12]
int 0x80
pop ebx
ret
}
}
_declspec(naked) int write(unsigned int fd, const char* buf, unsigned int count) {
__asm {
push ebx
mov eax, 0x04
mov ebx, [esp + 4 + 4]
mov ecx, [esp + 4 + 8]
mov edx, [esp + 4 + 12]
int 0x80
pop ebx
ret
}
}
_declspec(naked) int read(unsigned int fd, char* buf, unsigned int count) {
__asm {
push ebx
mov eax, 0x03
mov ebx, [esp + 4 + 4]
mov ecx, [esp + 4 + 8]
mov edx, [esp + 4 + 12]
int 0x80
pop ebx
ret
}
}
#pragma endregion syscall wrappers
#pragma region
int 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 socketcall(1, args);
}
int 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 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 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 socketcall(10, args);
}
#pragma endregion socketcall wrappers
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 = open("/proc/self/environ", 0, 0);
if (fd < 0) {
return 0;
}
n = read((unsigned int)fd, buf, (unsigned int)sizeof(buf));
if (n < 0) {
return 0;
}
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;
}
int GetProcessId()
{
return (int)getpid();
}
struct BaseConnectionUnix : public BaseConnection {
int sock{ -1 };
};
static BaseConnectionUnix Connection;
static sockaddr_un PipeAddr{};
#ifdef MSG_NOSIGNAL
static int MsgFlags = MSG_NOSIGNAL;
#else
static int MsgFlags = 0;
#endif
static const char* GetTempPath()
{
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*/ BaseConnection* BaseConnection::Create()
{
PipeAddr.sun_family = AF_UNIX;
return &Connection;
}
/*static*/ void BaseConnection::Destroy(BaseConnection*& c)
{
auto self = reinterpret_cast<BaseConnectionUnix*>(c);
self->Close();
c = nullptr;
}
bool BaseConnection::Open()
{
const char* tempPath = GetTempPath();
auto self = reinterpret_cast<BaseConnectionUnix*>(this);
self->sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (self->sock == -1) {
return false;
}
int fcres = fcntl((unsigned int)self->sock, (unsigned int)F_SETFL, (unsigned int)O_NONBLOCK);
if (fcres < 0) {
return false;
}
for (int pipeNum = 0; pipeNum < 10; ++pipeNum) {
snprintf(
PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum);
int err = connect(self->sock, (const sockaddr*)&PipeAddr, sizeof(PipeAddr));
if (err == 0) {
self->isOpen = true;
return true;
}
}
self->Close();
return false;
}
bool BaseConnection::Close()
{
auto self = reinterpret_cast<BaseConnectionUnix*>(this);
if (self->sock == -1) {
return false;
}
close(self->sock);
self->sock = -1;
self->isOpen = false;
return true;
}
bool BaseConnection::Write(const void* data, unsigned int length)
{
auto self = reinterpret_cast<BaseConnectionUnix*>(this);
if (self->sock == -1) {
return false;
}
int sentBytes = send(self->sock, data, length, MsgFlags);
if (sentBytes < 0) {
Close();
}
return sentBytes == (int)length;
}
bool BaseConnection::Read(void* data, unsigned int length)
{
auto self = reinterpret_cast<BaseConnectionUnix*>(this);
if (self->sock == -1) {
return false;
}
int res = (int)recv(self->sock, data, length, MsgFlags);
if (res < 0) {
if (res == -11) { // EAGAIN
return false;
}
Close();
}
return res == (int)length;
}