1 /**
2 * Copyright (c) 2023-2024 Marcin Niestroj
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * netdb.h related code common to Zephyr (top: nsos_sockets.c) and Linux
7 * (bottom: nsos_adapt.c).
8 *
9 * It is needed by both sides to share the same macro definitions/values
10 * (prefixed with NSOS_MID_), which is not possible to achieve with two separate
11 * standard libc libraries, since they use different values for the same
12 * symbols.
13 */
14
15 #include "nsos_netdb.h"
16
17 #ifdef __ZEPHYR__
18
19 #include <zephyr/net/socket.h>
20 #define ERR(_name) \
21 { DNS_ ## _name, NSOS_MID_ ## _name }
22
23 #else
24
25 #include <netdb.h>
26 #define ERR(_name) \
27 { _name, NSOS_MID_ ## _name }
28
29 #endif
30
31 #ifndef ARRAY_SIZE
32 #define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
33 #endif
34
35 struct nsos_eai_map {
36 int err;
37 int mid_err;
38 };
39
40 static const struct nsos_eai_map map[] = {
41 ERR(EAI_BADFLAGS),
42 ERR(EAI_NONAME),
43 ERR(EAI_AGAIN),
44 ERR(EAI_FAIL),
45 ERR(EAI_FAMILY),
46 ERR(EAI_SOCKTYPE),
47 ERR(EAI_SERVICE),
48 ERR(EAI_MEMORY),
49 ERR(EAI_SYSTEM),
50 ERR(EAI_OVERFLOW),
51 };
52
eai_to_nsos_mid(int err)53 int eai_to_nsos_mid(int err)
54 {
55 for (int i = 0; i < ARRAY_SIZE(map); i++) {
56 if (map[i].err == err) {
57 return map[i].mid_err;
58 }
59 }
60
61 return err;
62 }
63
eai_from_nsos_mid(int err)64 int eai_from_nsos_mid(int err)
65 {
66 for (int i = 0; i < ARRAY_SIZE(map); i++) {
67 if (map[i].mid_err == err) {
68 return map[i].err;
69 }
70 }
71
72 return err;
73 }
74