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 #include "napi/native_api.h"
16 #include <cerrno>
17 #include <cstdio>
18 #include <js_native_api.h>
19 #include <malloc.h>
20 #include <net/if.h>
21 #include <node_api.h>
22
23 #define NO_ERR 0
24 #define SUCCESS 1
25 #define FAIL (-1)
26
IfFreeNameIndex(napi_env env, napi_callback_info info)27 static napi_value IfFreeNameIndex(napi_env env, napi_callback_info info)
28 {
29 struct if_nameindex *ni;
30 ni = if_nameindex();
31 napi_value result = nullptr;
32 if_freenameindex(ni);
33 napi_create_int32(env, NO_ERR, &result);
34 return result;
35 }
36
IfNameIndex(napi_env env, napi_callback_info info)37 static napi_value IfNameIndex(napi_env env, napi_callback_info info)
38 {
39 struct if_nameindex *ni;
40 ni = if_nameindex();
41 napi_value result = nullptr;
42 if (ni != nullptr) {
43 napi_create_int32(env, NO_ERR, &result);
44 } else {
45 napi_create_int32(env, FAIL, &result);
46 }
47 if_freenameindex(ni);
48 return result;
49 }
50
IfIndexToName(napi_env env, napi_callback_info info)51 static napi_value IfIndexToName(napi_env env, napi_callback_info info)
52 {
53 char *name = nullptr;
54 char index[BUFSIZ] = "Ethernet HWaddr 00:21:5A:44:C8:02";
55 errno = NO_ERR;
56 unsigned int i = if_nametoindex(index);
57 name = if_indextoname(i, index);
58 napi_value result = nullptr;
59 if (name != nullptr) {
60 napi_create_int32(env, NO_ERR, &result);
61 } else {
62 napi_create_int32(env, FAIL, &result);
63 }
64 return result;
65 }
66
IfNameToIndex(napi_env env, napi_callback_info info)67 static napi_value IfNameToIndex(napi_env env, napi_callback_info info)
68 {
69 unsigned int i = if_nametoindex("lan0");
70 napi_value result = nullptr;
71 if (i != NO_ERR) {
72 napi_create_int32(env, NO_ERR, &result);
73 } else {
74 napi_create_int32(env, FAIL, &result);
75 }
76 return result;
77 }
78
79 EXTERN_C_START
Init(napi_env env, napi_value exports)80 static napi_value Init(napi_env env, napi_value exports)
81 {
82 napi_property_descriptor desc[] = {
83 {"ifFreeNameIndex", nullptr, IfFreeNameIndex, nullptr, nullptr, nullptr, napi_default, nullptr},
84 {"ifNameIndex", nullptr, IfNameIndex, nullptr, nullptr, nullptr, napi_default, nullptr},
85 {"ifIndexToName", nullptr, IfIndexToName, nullptr, nullptr, nullptr, napi_default, nullptr},
86 {"ifNameToIndex", nullptr, IfNameToIndex, nullptr, nullptr, nullptr, napi_default, nullptr},
87 };
88 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
89 return exports;
90 }
91 EXTERN_C_END
92
93 static napi_module demoModule = {
94 .nm_version = 1,
95 .nm_flags = 0,
96 .nm_filename = nullptr,
97 .nm_register_func = Init,
98 .nm_modname = "if",
99 .nm_priv = ((void *)0),
100 .reserved = {0},
101 };
102
RegisterEntryModule(void)103 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
104