about summary refs log tree commit diff stats
path: root/src/libtools/libc_net32.c
blob: 533104d2ceac06eea028d66930e5ce927151f9b4 (plain) (blame)
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
#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 = box_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);
    box_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
    return &ret;
}

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)
{
    struct ifaddrs* addrs;
    int ret = getifaddrs(&addrs);
    if(!ret) {
        // convert the chained list of ifaddrs to i386 (narrowed) in place
        struct ifaddrs* p = addrs;
        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;
}