Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/netif.zig
1067 views
1
// Network interfaces
2
3
const c = @import("c.zig");
4
const node = @import("node.zig");
5
const net_if = @cImport({
6
@cDefine("struct__OSUnalignedU16", "uint16_t");
7
@cDefine("struct__OSUnalignedU32", "uint32_t");
8
@cDefine("struct__OSUnalignedU64", "uint64_t");
9
@cInclude("net/if.h");
10
});
11
12
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
13
// if_indextoname
14
// if_nameindex
15
// if_nametoindex
16
17
try node.registerFunction(env, exports, "if_indextoname", if_indextoname);
18
try node.registerFunction(env, exports, "if_nametoindex", if_nametoindex);
19
try node.registerFunction(env, exports, "if_nameindex", if_nameindex);
20
}
21
22
// char *if_indextoname(unsigned int ifindex, char *ifname);
23
fn if_indextoname(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
24
var ifname: [net_if.IFNAMSIZ:0]u8 = undefined;
25
const argv = node.getArgv(env, info, 1) catch return null;
26
const ifindex = node.u32FromValue(env, argv[0], "ifindex") catch return null;
27
if (net_if.if_indextoname(ifindex, &ifname) == 0) {
28
node.throwError(env, "invalid index");
29
return null;
30
}
31
return node.createStringFromPtr(env, &ifname, "name") catch return null;
32
}
33
34
// unsigned int if_nametoindex(const char *ifname);
35
fn if_nametoindex(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
36
var ifname: [net_if.IFNAMSIZ]u8 = undefined;
37
const argv = node.getArgv(env, info, 1) catch return null;
38
node.stringFromValue(env, argv[0], "ifname", net_if.IFNAMSIZ, &ifname) catch return null;
39
const ifindex = net_if.if_nametoindex(&ifname);
40
if (ifindex == 0) {
41
node.throwError(env, "interface ifname does not exist");
42
return null;
43
}
44
return node.create_u32(env, ifindex, "ifindex") catch return null;
45
}
46
47
// struct if_nameindex *if_nameindex(void);
48
fn if_nameindex(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
49
_ = info;
50
var nameindex = net_if.if_nameindex();
51
var i: usize = 0;
52
while (nameindex[i].if_index != 0) : (i += 1) {}
53
54
const array = node.createArray(env, @intCast(u32, i), "if_nameindex output array") catch return null;
55
i -= 1;
56
while (i >= 0) : (i -= 1) {
57
const entry = node.createArray(env, 2, "if_nameindex element") catch return null;
58
const index = node.create_u32(env, nameindex[i].if_index, "index") catch return null;
59
node.setElement(env, entry, 0, index, "setting index of entry in if_nameindex") catch return null;
60
const name = node.createStringFromPtr(env, nameindex[i].if_name, "name") catch return null;
61
node.setElement(env, entry, 1, name, "setting name of entry in if_nameindex") catch return null;
62
node.setElement(env, array, @intCast(u32, i), entry, "setting entry of if_nameindex array") catch return null;
63
if (i == 0) break; // unsigned so avoid wrap around
64
}
65
return array;
66
}
67
68