少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
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
/**
 * \file
 * Modern posix networking code
 *
 * Author:
 *    Rodrigo Kumpera (kumpera@gmail.com)
 *
 * (C) 2015 Xamarin
 */
 
#include <config.h>
#include <glib.h>
 
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETIFADDRS
#include <ifaddrs.h>
#endif
 
#include <mono/utils/networking.h>
#include <mono/utils/mono-threads-coop.h>
 
static void*
get_address_from_sockaddr (struct sockaddr *sa)
{
    switch (sa->sa_family) {
    case AF_INET:
        return &((struct sockaddr_in*)sa)->sin_addr;
#ifdef HAVE_STRUCT_SOCKADDR_IN6
    case AF_INET6:
        return &((struct sockaddr_in6*)sa)->sin6_addr;
#endif
    }
    return NULL;
}
 
#ifdef HAVE_GETADDRINFO
 
int
mono_get_address_info (const char *hostname, int port, int flags, MonoAddressInfo **result)
{
    char service_name [16];
    struct addrinfo hints, *res = NULL, *info;
    MonoAddressEntry *cur = NULL, *prev = NULL;
    MonoAddressInfo *addr_info;
    int ret;
 
    memset (&hints, 0, sizeof (struct addrinfo));
    *result = NULL;
 
    hints.ai_family = PF_UNSPEC;
    if (flags & MONO_HINT_IPV4)
        hints.ai_family = PF_INET;
    else if (flags & MONO_HINT_IPV6)
        hints.ai_family = PF_INET6;
 
    hints.ai_socktype = SOCK_STREAM;
 
    if (flags & MONO_HINT_CANONICAL_NAME)
        hints.ai_flags = AI_CANONNAME;
 
/* Some ancient libc don't define AI_ADDRCONFIG */
#ifdef AI_ADDRCONFIG
    if (flags & MONO_HINT_CONFIGURED_ONLY)
        hints.ai_flags = AI_ADDRCONFIG;
#endif
    sprintf (service_name, "%d", port);
 
    MONO_ENTER_GC_SAFE;
    ret = getaddrinfo (hostname, service_name, &hints, &info);
    MONO_EXIT_GC_SAFE;
 
    if (ret)
        return 1; /* FIXME propagate the error */
 
    res = info;
    *result = addr_info = g_new0 (MonoAddressInfo, 1);
 
    while (res) {
        cur = g_new0 (MonoAddressEntry, 1);
        cur->family = res->ai_family;
        cur->socktype = res->ai_socktype;
        cur->protocol = res->ai_protocol;
        if (cur->family == PF_INET) {
            cur->address_len = sizeof (struct in_addr);
            cur->address.v4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr;
#ifdef HAVE_STRUCT_SOCKADDR_IN6            
        } else if (cur->family == PF_INET6) {
            cur->address_len = sizeof (struct in6_addr);
            cur->address.v6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr;
#endif
        } else {
            g_warning ("Cannot handle address family %d", cur->family);
            res = res->ai_next;
            g_free (cur);
            continue;
        }
 
        if (res->ai_canonname)
            cur->canonical_name = g_strdup (res->ai_canonname);
 
        if (prev)
            prev->next = cur;
        else
            addr_info->entries = cur;
            
        prev = cur;
        res = res->ai_next;
    }
 
    freeaddrinfo (info);
    return 0;
}
 
#endif
 
#ifdef HAVE_GETPROTOBYNAME
 
static int
fetch_protocol (const char *proto_name, int *cache, int *proto, int default_val)
{
    if (!*cache) {
        struct protoent *pent;
 
        pent = getprotobyname (proto_name);
        *proto = pent ? pent->p_proto : default_val;
        *cache = 1;
    }
    return *proto;
}
 
int
mono_networking_get_tcp_protocol (void)
{
    static int cache, proto;
    return fetch_protocol ("tcp", &cache, &proto, 6); //6 is SOL_TCP on linux
}
 
int
mono_networking_get_ip_protocol (void)
{
    static int cache, proto;
    return fetch_protocol ("ip", &cache, &proto, 0); //0 is SOL_IP on linux
}
 
int
mono_networking_get_ipv6_protocol (void)
{
    static int cache, proto;
    return fetch_protocol ("ipv6", &cache, &proto, 41); //41 is SOL_IPV6 on linux
}
 
#endif
 
#if defined (HAVE_SIOCGIFCONF)
 
#define IFCONF_BUFF_SIZE 1024
#ifndef _SIZEOF_ADDR_IFREQ
#define _SIZEOF_ADDR_IFREQ(ifr) (sizeof (struct ifreq))
#endif
 
#define FOREACH_IFR(IFR, IFC) \
    for (IFR = (IFC).ifc_req;    \
    ifr < (struct ifreq*)((char*)(IFC).ifc_req + (IFC).ifc_len); \
    ifr = (struct ifreq*)((char*)(IFR) + _SIZEOF_ADDR_IFREQ (*(IFR))))
 
void *
mono_get_local_interfaces (int family, int *interface_count)
{
    int fd;
    struct ifconf ifc;
    struct ifreq *ifr;
    int if_count = 0;
    gboolean ignore_loopback = FALSE;
    void *result = NULL;
    char *result_ptr;
 
    *interface_count = 0;
 
    if (!mono_address_size_for_family (family))
        return NULL;
 
    fd = socket (family, SOCK_STREAM, 0);
    if (fd == -1)
        return NULL;
 
    memset (&ifc, 0, sizeof (ifc));
    ifc.ifc_len = IFCONF_BUFF_SIZE;
    ifc.ifc_buf = (char *)g_malloc (IFCONF_BUFF_SIZE); /* We can't have such huge buffers on the stack. */
    if (ioctl (fd, SIOCGIFCONF, &ifc) < 0)
        goto done;
 
    FOREACH_IFR (ifr, ifc) {
        struct ifreq iflags;
 
        //only return addresses of the same type as @family
        if (ifr->ifr_addr.sa_family != family) {
            ifr->ifr_name [0] = '\0';
            continue;
        }
 
        strcpy (iflags.ifr_name, ifr->ifr_name);
 
        //ignore interfaces we can't get props for
        if (ioctl (fd, SIOCGIFFLAGS, &iflags) < 0) {
            ifr->ifr_name [0] = '\0';
            continue;
        }
 
        //ignore interfaces that are down
        if ((iflags.ifr_flags & IFF_UP) == 0) {
            ifr->ifr_name [0] = '\0';
            continue;
        }
 
        //If we have a non-loopback iface, don't return any loopback
        if ((iflags.ifr_flags & IFF_LOOPBACK) == 0) {
            ignore_loopback = TRUE;
            ifr->ifr_name [0] = 1;//1 means non-loopback
        } else {
            ifr->ifr_name [0] = 2; //2 means loopback
        }
        ++if_count;
    }
 
    result = (char *)g_malloc (if_count * mono_address_size_for_family (family));
    result_ptr = (char *)result;
    FOREACH_IFR (ifr, ifc) {
        if (ifr->ifr_name [0] == '\0')
            continue;
 
        if (ignore_loopback && ifr->ifr_name [0] == 2) {
            --if_count;
            continue;
        }
 
        memcpy (result_ptr, get_address_from_sockaddr (&ifr->ifr_addr), mono_address_size_for_family (family));
        result_ptr += mono_address_size_for_family (family);
    }
    g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
 
done:
    *interface_count = if_count;
    g_free (ifc.ifc_buf);
    close (fd);
    return result;
}
 
#elif defined(HAVE_GETIFADDRS)
 
void *
mono_get_local_interfaces (int family, int *interface_count)
{
    struct ifaddrs *ifap = NULL, *cur;
    int if_count = 0;
    gboolean ignore_loopback = FALSE;
    void *result;
    char *result_ptr;
 
    *interface_count = 0;
 
    if (!mono_address_size_for_family (family))
        return NULL;
 
    if (getifaddrs (&ifap))
        return NULL;
 
    for (cur = ifap; cur; cur = cur->ifa_next) {
        //ignore interfaces with no address assigned
        if (!cur->ifa_addr)
            continue;
 
        //ignore interfaces that don't belong to @family
        if (cur->ifa_addr->sa_family != family)
            continue;
 
        //ignore interfaces that are down
        if ((cur->ifa_flags & IFF_UP) == 0)
            continue;
 
        //If we have a non-loopback iface, don't return any loopback
        if ((cur->ifa_flags & IFF_LOOPBACK) == 0)
            ignore_loopback = TRUE;
 
        if_count++;
    }
 
    result_ptr = result = g_malloc (if_count * mono_address_size_for_family (family));
    for (cur = ifap; cur; cur = cur->ifa_next) {
        if (!cur->ifa_addr)
            continue;
        if (cur->ifa_addr->sa_family != family)
            continue;
        if ((cur->ifa_flags & IFF_UP) == 0)
            continue;
 
        //we decrement if_count because it did not on the previous loop.
        if (ignore_loopback && (cur->ifa_flags & IFF_LOOPBACK)) {
            --if_count;
            continue;
        }
 
        memcpy (result_ptr, get_address_from_sockaddr (cur->ifa_addr), mono_address_size_for_family (family));
        result_ptr += mono_address_size_for_family (family);
    }
    g_assert (result_ptr <= (char*)result + if_count * mono_address_size_for_family (family));
 
    freeifaddrs (ifap);
    *interface_count = if_count;
    return result;
}
 
#endif
 
#ifdef HAVE_GETNAMEINFO
 
gboolean
mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
{
    MonoSocketAddress saddr;
    socklen_t len;
    mono_socket_address_init (&saddr, &len, address->family, &address->addr, 0);
 
    return getnameinfo (&saddr.addr, len, buffer, buflen, NULL, 0, NI_NUMERICHOST) == 0;
}
 
#elif HAVE_INET_NTOP
 
gboolean
mono_networking_addr_to_str (MonoAddress *address, char *buffer, socklen_t buflen)
{
    return inet_ntop (address->family, &address->addr, buffer, buflen) != NULL;
}
 
#endif
 
#ifndef _WIN32
// These are already defined in networking-windows.c for Windows
void
mono_networking_init (void)
{
    //nothing really
}
 
void
mono_networking_shutdown (void)
{
    //nothing really
}
#endif