• 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 <arpa/inet.h>
17 #include <dirent.h>
18 #include <js_native_api.h>
19 #include <netinet/in.h>
20 #include <node_api.h>
21 #include <syslog.h>
22 
23 #define SUCCESS 1
24 #define FAIL (-1)
25 #define FALSE (-1)
26 #define RETURN_0 0
27 #define FAILD (-1)
28 #define PARAM_0 0
29 #define PARAM_1 1
30 #define PARAM_0x1234 0x1234
31 #define PARAM_0x3412 0x3412
32 #define PARAM_0X7F000001 0x7f000001
33 #define PARAM_0X12345678 0x12345678
34 
Ntohl(napi_env env,napi_callback_info info)35 static napi_value Ntohl(napi_env env, napi_callback_info info)
36 {
37     size_t argc = PARAM_1;
38     napi_value args[1] = {nullptr};
39     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
40     int value0;
41     napi_get_value_int32(env, args[0], &value0);
42     uint32_t hl = PARAM_0X12345678;
43     napi_value result = nullptr;
44     uint32_t ret = ntohl(hl);
45     napi_create_uint32(env, ret, &result);
46     return result;
47 }
48 
Ntohs(napi_env env,napi_callback_info info)49 static napi_value Ntohs(napi_env env, napi_callback_info info)
50 {
51     size_t argc = PARAM_1;
52     napi_value args[1] = {nullptr};
53     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
54     int value0;
55     napi_get_value_int32(env, args[0], &value0);
56     uint16_t hs = PARAM_0x1234;
57     napi_value result = nullptr;
58     uint16_t ret = ntohs(hs);
59     napi_create_uint32(env, ret, &result);
60     return result;
61 }
Htonl(napi_env env,napi_callback_info info)62 static napi_value Htonl(napi_env env, napi_callback_info info)
63 {
64     const uint32_t IP = PARAM_0X7F000001;
65     uint32_t ret = htonl(IP);
66     int addr2 = inet_addr("127.0.0.1");
67     napi_value result;
68     if (ret == addr2) {
69         napi_create_int32(env, SUCCESS, &result);
70     } else {
71         napi_create_int32(env, FALSE, &result);
72     }
73     return result;
74 }
75 static const uint16_t LE = PARAM_0x1234;
76 static const uint16_t BE = PARAM_0x3412;
Htons(napi_env env,napi_callback_info info)77 static napi_value Htons(napi_env env, napi_callback_info info)
78 {
79     uint32_t ret = htons(LE);
80     napi_value result;
81     if (ret == BE) {
82         napi_create_int32(env, SUCCESS, &result);
83     } else {
84         napi_create_int32(env, FALSE, &result);
85     }
86     return result;
87 }
88 
In6addrAny(napi_env env,napi_callback_info info)89 static napi_value In6addrAny(napi_env env, napi_callback_info info)
90 {
91     int resultValue = FAILD;
92     const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
93     if (sizeof(in6addr_any) >= PARAM_0) {
94         resultValue = RETURN_0;
95     }
96     napi_value result = nullptr;
97     napi_create_int32(env, resultValue, &result);
98 
99     return result;
100 }
101 
In6addrLoopback(napi_env env,napi_callback_info info)102 static napi_value In6addrLoopback(napi_env env, napi_callback_info info)
103 {
104     int resultValue = FAILD;
105     const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
106     if (sizeof(in6addr_loopback) >= PARAM_0) {
107         resultValue = RETURN_0;
108     }
109     napi_value result = nullptr;
110     napi_create_int32(env, resultValue, &result);
111 
112     return result;
113 }
114 
115 EXTERN_C_START
Init(napi_env env,napi_value exports)116 static napi_value Init(napi_env env, napi_value exports)
117 {
118     napi_property_descriptor desc[] = {
119         {"ntohl", nullptr, Ntohl, nullptr, nullptr, nullptr, napi_default, nullptr},
120         {"ntohs", nullptr, Ntohs, nullptr, nullptr, nullptr, napi_default, nullptr},
121         {"htonl", nullptr, Htonl, nullptr, nullptr, nullptr, napi_default, nullptr},
122         {"htons", nullptr, Htons, nullptr, nullptr, nullptr, napi_default, nullptr},
123         {"in6addrAny", nullptr, In6addrAny, nullptr, nullptr, nullptr, napi_default, nullptr},
124         {"in6addrLoopback", nullptr, In6addrLoopback, nullptr, nullptr, nullptr, napi_default, nullptr},
125     };
126     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
127     return exports;
128 }
129 EXTERN_C_END
130 
131 static napi_module demoModule = {
132     .nm_version = 1,
133     .nm_flags = 0,
134     .nm_filename = nullptr,
135     .nm_register_func = Init,
136     .nm_modname = "in",
137     .nm_priv = ((void *)0),
138     .reserved = {0},
139 };
140 
RegisterEntryModule(void)141 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
142