• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi/native_api.h"
17 #include <arpa/inet.h>
18 #include <cstdlib>
19 #include <cstring>
20 #include <ifaddrs.h>
21 #include <js_native_api_types.h>
22 #include <net/if.h>
23 #include <netdb.h>
24 #include <sys/inotify.h>
25 #include <utmp.h>
26 
27 #define STRLENGTH 64
28 #define FALSE 0
29 #define TRUE 1
30 #define ERROR (-1)
31 #define PORT 9000
32 #define DEFAULT_VALUE 0
33 #define ONE 1
34 #define PARAM_0 0
35 #define PARAM_1 1
36 #define PARAM_2 2
InetAddr(napi_env env,napi_callback_info info)37 static napi_value InetAddr(napi_env env, napi_callback_info info)
38 {
39     size_t argc = ONE;
40     napi_value args[1] = {nullptr};
41     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
42     int valueZero;
43     napi_get_value_int32(env, args[0], &valueZero);
44     in_addr_t ret = PARAM_0;
45     if (valueZero == PARAM_0) {
46         ret = inet_addr("10.1.59.80");
47     }
48     if (valueZero == PARAM_1) {
49         ret = inet_addr("192.168.1.100");
50     }
51     if (valueZero == PARAM_2) {
52         ret = inet_addr("192.168.100.1");
53     }
54     napi_value result = nullptr;
55     napi_create_int32(env, ret, &result);
56     return result;
57 }
InetAton(napi_env env,napi_callback_info info)58 static napi_value InetAton(napi_env env, napi_callback_info info)
59 {
60     size_t argc = ONE;
61     napi_value args[1] = {nullptr};
62     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
63     int valueZero;
64     napi_get_value_int32(env, args[0], &valueZero);
65     int ret = FALSE;
66     struct sockaddr_in adr_inet;
67     memset(&adr_inet, FALSE, sizeof(adr_inet));
68     adr_inet.sin_family = AF_INET;
69     uint16_t port = PORT;
70     adr_inet.sin_port = htons(port);
71     if (valueZero == PARAM_0) {
72         ret = inet_aton("192.168.100.1", &adr_inet.sin_addr);
73     }
74     if (valueZero == PARAM_1) {
75         ret = inet_aton("192.168.1.100", &adr_inet.sin_addr);
76     }
77     if (valueZero == PARAM_2) {
78         ret = inet_aton("10.1.59.80", &adr_inet.sin_addr);
79     }
80     napi_value result = nullptr;
81     napi_create_int32(env, ret, &result);
82     return result;
83 }
InetLnaof(napi_env env,napi_callback_info info)84 static napi_value InetLnaof(napi_env env, napi_callback_info info)
85 {
86     size_t argc = ONE;
87     napi_value args[1] = {nullptr};
88     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
89 
90     int valueZero;
91     napi_get_value_int32(env, args[0], &valueZero);
92     struct in_addr addr;
93     addr.s_addr = valueZero;
94 
95     in_addr_t ret = inet_lnaof(addr);
96     napi_value result = nullptr;
97     napi_create_int32(env, ret, &result);
98     return result;
99 }
InetMakeaddr(napi_env env,napi_callback_info info)100 static napi_value InetMakeaddr(napi_env env, napi_callback_info info)
101 {
102     size_t argc = ONE;
103     napi_value args[1] = {nullptr};
104     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
105     int valueZero;
106     napi_get_value_int32(env, args[0], &valueZero);
107     struct in_addr inp;
108     inp.s_addr = valueZero;
109     struct in_addr new_inp = inet_makeaddr(inet_netof(inp), inet_lnaof(inp));
110     napi_value result = nullptr;
111     napi_create_int32(env, new_inp.s_addr, &result);
112     return result;
113 }
114 
InetNetof(napi_env env,napi_callback_info info)115 static napi_value InetNetof(napi_env env, napi_callback_info info)
116 {
117     size_t argc = ONE;
118     napi_value args[1] = {nullptr};
119     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
120     int valueZero;
121     napi_get_value_int32(env, args[0], &valueZero);
122     struct in_addr inp;
123     inp.s_addr = valueZero;
124     in_addr_t addr = inet_netof(inp);
125     napi_value result = nullptr;
126     napi_create_int32(env, addr, &result);
127     return result;
128 }
InetNetwork(napi_env env,napi_callback_info info)129 static napi_value InetNetwork(napi_env env, napi_callback_info info)
130 {
131     size_t argc = ONE;
132     napi_value args[1] = {nullptr};
133     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
134     int valueZero;
135     napi_get_value_int32(env, args[0], &valueZero);
136     in_addr_t addr = FALSE;
137     if (valueZero == PARAM_0) {
138         addr = inet_network("10.1.59.80");
139     }
140     if (valueZero == PARAM_1) {
141         addr = inet_network("192.168.100.1");
142     }
143     if (valueZero == PARAM_2) {
144         addr = inet_network("192.168.1.100");
145     }
146     napi_value result = nullptr;
147     napi_create_int32(env, addr, &result);
148     return result;
149 }
InetNtoa(napi_env env,napi_callback_info info)150 static napi_value InetNtoa(napi_env env, napi_callback_info info)
151 {
152     size_t argc = ONE;
153     napi_value args[1] = {nullptr};
154     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
155     int valueZero;
156     napi_get_value_int32(env, args[0], &valueZero);
157     struct in_addr inp;
158     inp.s_addr = valueZero;
159     char *ret = inet_ntoa(inp);
160     napi_value result = nullptr;
161     napi_create_string_utf8(env, ret, NAPI_AUTO_LENGTH, &result);
162     return result;
163 }
164 
InetNtop(napi_env env,napi_callback_info info)165 static napi_value InetNtop(napi_env env, napi_callback_info info)
166 {
167     size_t argc = ONE;
168     napi_value args[1] = {nullptr};
169     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
170     int valueZero;
171     napi_get_value_int32(env, args[0], &valueZero);
172     struct in_addr inp;
173     inp.s_addr = valueZero;
174     char buf[20] = {DEFAULT_VALUE};
175     const char *ret = inet_ntop(AF_INET, &inp, buf, sizeof(buf));
176     int flag = ERROR;
177     if (ret != nullptr) {
178         flag = TRUE;
179     }
180     napi_value result = nullptr;
181     napi_create_int32(env, flag, &result);
182     return result;
183 }
184 
InetPton(napi_env env,napi_callback_info info)185 static napi_value InetPton(napi_env env, napi_callback_info info)
186 {
187     size_t argc = ONE;
188     napi_value args[1] = {nullptr};
189     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
190     int valueZero;
191     napi_get_value_int32(env, args[0], &valueZero);
192     struct in_addr inp;
193     inp.s_addr = valueZero;
194     char buf[20] = {DEFAULT_VALUE};
195     int ret = inet_pton(AF_INET, buf, &inp);
196     napi_value result = nullptr;
197     napi_create_int32(env, ret, &result);
198     return result;
199 }
200 
201 EXTERN_C_START
Init(napi_env env,napi_value exports)202 static napi_value Init(napi_env env, napi_value exports)
203 {
204     napi_property_descriptor desc[] = {
205         {"inetAddr", nullptr, InetAddr, nullptr, nullptr, nullptr, napi_default, nullptr},
206         {"inetAton", nullptr, InetAton, nullptr, nullptr, nullptr, napi_default, nullptr},
207         {"inetLnaof", nullptr, InetLnaof, nullptr, nullptr, nullptr, napi_default, nullptr},
208         {"inetMakeaddr", nullptr, InetMakeaddr, nullptr, nullptr, nullptr, napi_default, nullptr},
209         {"inetNetof", nullptr, InetNetof, nullptr, nullptr, nullptr, napi_default, nullptr},
210         {"inetNetwork", nullptr, InetNetwork, nullptr, nullptr, nullptr, napi_default, nullptr},
211         {"inetNtoa", nullptr, InetNtoa, nullptr, nullptr, nullptr, napi_default, nullptr},
212         {"inetNtop", nullptr, InetNtop, nullptr, nullptr, nullptr, napi_default, nullptr},
213         {"inetPton", nullptr, InetPton, nullptr, nullptr, nullptr, napi_default, nullptr},
214 
215     };
216     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
217     return exports;
218 }
219 EXTERN_C_END
220 
221 static napi_module demoModule = {
222     .nm_version = 1,
223     .nm_flags = 0,
224     .nm_filename = nullptr,
225     .nm_register_func = Init,
226     .nm_modname = "libinet",
227     .nm_priv = ((void *)0),
228     .reserved = {0},
229 };
230 
RegisterModule(void)231 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
232