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
|
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <wchar.h>
#include <dlfcn.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <getopt.h>
#include <pwd.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "box64stack.h"
#include "x64emu.h"
#include "debug.h"
#include "wrapper32.h"
#include "bridge.h"
#include "callback.h"
#include "librarian.h"
#include "emu/x64emu_private.h"
#include "box32context.h"
#include "myalign32.h"
#include "fileutils.h"
#include "globalsymbols.h"
#include "box32.h"
#include "converter32.h"
EXPORT ssize_t my32_recvmsg(x64emu_t* emu, int socket, struct i386_msghdr* msg, int flags)
{
struct iovec iov[msg->msg_iovlen];
struct msghdr m;
uint8_t buff[msg->msg_controllen+256];
AlignMsgHdr_32(&m, iov, buff, msg, 0);
ssize_t ret = recvmsg(socket, &m, flags);
UnalignMsgHdr_32(msg, &m);
return ret;
}
EXPORT ssize_t my32_sendmsg(x64emu_t* emu, int socket, struct i386_msghdr* msg, int flags)
{
struct iovec iov[msg->msg_iovlen];
struct msghdr m;
uint8_t buff[msg->msg_controllen+256];
AlignMsgHdr_32(&m, iov, buff, msg, 1);
ssize_t ret = sendmsg(socket, &m, flags);
UnalignMsgHdr_32(msg, &m);
return ret;
}
EXPORT void* my32___cmsg_nxthdr(struct i386_msghdr* mhdr, struct i386_cmsghdr* cmsg)
{
// simpler to redo, also, will be used internaly
if(cmsg->cmsg_len < sizeof(struct i386_cmsghdr))
return NULL;
// compute next
cmsg = (struct i386_cmsghdr*)(((uintptr_t)cmsg) + ((cmsg->cmsg_len+3)&~3));
// check it still inside limits
if((uintptr_t)(cmsg+1) > mhdr->msg_control+mhdr->msg_controllen)
return NULL;
if((uintptr_t)(cmsg)+cmsg->cmsg_len > mhdr->msg_control+mhdr->msg_controllen)
return NULL;
return cmsg;
}
EXPORT int my32_getaddrinfo(x64emu_t* emu, void* node, void* service, struct i386_addrinfo* hints, ptr_t* res)
{
struct addrinfo* hints_ = (struct addrinfo*)hints; // only first part is used, wich is identical
struct addrinfo* p = {0};
int ret = getaddrinfo(node, service, hints_, &p);
if(!ret && p) {
// counting the number of "next"
struct addrinfo* p2 = p;
int idx = 0;
while(p2) {++idx; p2 = p2->ai_next;}
// doing the malloc!
void* r = actual_malloc(idx*sizeof(struct i386_addrinfo)+sizeof(void*));
ptr_t p3 = to_ptrv(r);
*res = p3;
p2 = p;
for(int i=0; i<idx; ++i) {
struct i386_addrinfo* dest = (struct i386_addrinfo*)from_ptrv(p3);
p3+=sizeof(struct i386_addrinfo);
if(!i) {
*(void**)from_ptrv(p3) = p;
p3+=sizeof(void*);
}
dest->ai_flags = p2->ai_flags;
dest->ai_family = p2->ai_family;
dest->ai_socktype = p2->ai_socktype;
dest->ai_protocol = p2->ai_protocol;
dest->ai_addrlen = p2->ai_addrlen;
dest->ai_addr = to_ptrv(p2->ai_addr);
dest->ai_canonname = to_cstring(p2->ai_canonname);
p2 = p2->ai_next;
dest->ai_next = p2?p3:0;
}
} else
*res = 0;
return ret;
}
EXPORT void my32_freeaddrinfo(x64emu_t* emu, void* a) {
if(!a) return;
void* orig = *(void**)(a+sizeof(struct i386_addrinfo));
freeaddrinfo(orig);
actual_free(a);
}
EXPORT void* my32_gethostbyname(x64emu_t* emu, const char* a)
{
static struct i386_hostent ret = {0};
static ptr_t strings[128] = {0};
struct hostent* h = gethostbyname(a);
if(!h) return NULL;
// convert...
ret.h_name = to_cstring(h->h_name);
ret.h_addrtype = h->h_addrtype;
ret.h_length = h->h_length;
ptr_t s = to_ptrv(&strings);
int idx = 0;
ret.h_aliases = h->h_aliases?s:0;
if(h->h_aliases) {
char** p = h->h_aliases;
while(*p) {
strings[idx++] = to_cstring(*p);
++p;
}
strings[idx++] = 0;
}
ret.h_addr_list = h->h_addr_list?to_ptrv(&strings[idx]):0;
if(h->h_addr_list) {
char** p = h->h_addr_list;
while(*p) {
strings[idx++] = to_ptrv(*p);
++p;
}
strings[idx++] = 0;
}
// done
emu->libc_herr = h_errno;
return &ret;
}
EXPORT int my32_gethostbyname_r(x64emu_t* emu, void* name, struct i386_hostent* ret, void* buff, size_t buflen, ptr_t* result, int* h_err)
{
struct hostent ret_l = {0};
struct hostent *result_l = NULL;
int r = gethostbyname_r(name, &ret_l, buff, buflen, &result_l, h_err);
if(!result_l)
*result = 0;
else
*result = to_ptrv(ret);
// convert result, all memory allocated should be in program space
if(result_l) {
ret->h_name = to_cstring(result_l->h_name);
ret->h_addrtype = result_l->h_addrtype;
ret->h_length = result_l->h_length;
int idx = 0;
ret->h_aliases = to_ptrv(result_l->h_aliases);
if(result_l->h_aliases) {
char** p = result_l->h_aliases;
ptr_t* strings = from_ptrv(ret->h_aliases);
while(*p) {
strings[idx++] = to_cstring(*p);
++p;
}
strings[idx++] = 0;
}
ret->h_addr_list = to_ptrv(result_l->h_addr_list);
if(result_l->h_addr_list) {
char** p = result_l->h_addr_list;
ptr_t* strings = from_ptrv(ret->h_addr_list);
while(*p) {
strings[idx++] = to_ptrv(*p);
++p;
}
strings[idx++] = 0;
}
}
return r;
}
EXPORT int my32_gethostbyaddr_r(x64emu_t* emu, void* addr, uint32_t len, int type, struct i386_hostent* ret, void* buff, size_t buflen, ptr_t* result, int* h_err)
{
struct hostent ret_l = {0};
struct hostent *result_l = NULL;
int r = gethostbyaddr_r(addr, len, type, &ret_l, buff, buflen, &result_l, h_err);
if(!result_l)
*result = 0;
else
*result = to_ptrv(ret);
// convert result, all memory allocated should be in program space
if(result_l) {
ret->h_name = to_cstring(result_l->h_name);
ret->h_addrtype = result_l->h_addrtype;
ret->h_length = result_l->h_length;
int idx = 0;
ret->h_aliases = to_ptrv(result_l->h_aliases);
if(result_l->h_aliases) {
char** p = result_l->h_aliases;
ptr_t* strings = from_ptrv(ret->h_aliases);
while(*p) {
strings[idx++] = to_cstring(*p);
++p;
}
strings[idx++] = 0;
}
ret->h_addr_list = to_ptrv(result_l->h_addr_list);
if(result_l->h_addr_list) {
char** p = result_l->h_addr_list;
ptr_t* strings = from_ptrv(ret->h_addr_list);
while(*p) {
strings[idx++] = to_ptrv(*p);
++p;
}
strings[idx++] = 0;
}
}
return r;
}
struct i386_ifaddrs
{
ptr_t ifa_next; // struct ifaddrs *
ptr_t ifa_name; // char *
uint32_t ifa_flags;
ptr_t ifa_addr; // struct sockaddr *
ptr_t ifa_netmask;// struct sockaddr *
ptr_t ifa_ifu; // union of struct sockaddr
ptr_t ifa_data; // void *
};
EXPORT int my32_getifaddrs(x64emu_t* emu, void** res)
{
int ret = getifaddrs((void*)res);
if(!ret) {
// convert the chained list of ifaddrs to i386 (narrowed) in place
struct ifaddrs* p = *res;
while(p) {
struct i386_ifaddrs *i386 = (struct i386_ifaddrs*)p;
struct ifaddrs* next = p->ifa_next;
i386->ifa_next = to_ptrv(p->ifa_next);
i386->ifa_name = to_cstring(p->ifa_name);
i386->ifa_flags = p->ifa_flags;
i386->ifa_addr = to_ptrv(p->ifa_addr);
i386->ifa_netmask = to_ptrv(p->ifa_netmask);
i386->ifa_ifu = (i386->ifa_flags&IFF_BROADCAST)?to_ptrv(p->ifa_broadaddr):to_ptrv(p->ifa_dstaddr);
i386->ifa_data = to_ptrv(p->ifa_data);
p = next;
}
}
}
EXPORT void* my32___h_errno_location(x64emu_t* emu)
{
// TODO: Find a better way to do this
// cannot use __thread as it makes the address not 32bits
emu->libc_herr = h_errno;
return &emu->libc_herr;
}
struct protoent_32
{
ptr_t p_name; //char*
ptr_t p_aliases;// char**
int p_proto;
};
EXPORT void* my32_getprotobyname(x64emu_t* emu, void* name)
{
static struct protoent_32 my_protoent = {0};
static ptr_t strings[256];
struct protoent *ret = getprotobyname(name);
if(!ret)
return NULL;
my_protoent.p_name = to_cstring(ret->p_name);
my_protoent.p_proto = ret->p_proto;
if(ret->p_aliases) {
my_protoent.p_aliases = to_ptrv(&strings);
int i = 0;
while(ret->p_aliases[i]) {
strings[i] = to_cstring(ret->p_aliases[i]);
++i;
}
strings[i] = 0;
} else
my_protoent.p_aliases = 0;
return &my_protoent;
}
|