Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/kernel/src/wasm/posix/netif.ts
1068 views
1
/*
2
Functions from net/if.h
3
4
NOTE: node.js has require('os').networkInterfaces(), but it is not equivalent
5
to the system calls in net/if.h. E.g., on my mac if_indextoname(2) is "gif0",
6
but "gif0" isn't in require('os').networkInterfaces(). This is because
7
networkInterface "Returns an object containing network interfaces that
8
have been assigned a network address." and gif0 hasn't been.
9
*/
10
11
import { notImplemented } from "./util";
12
import constants from "./constants";
13
14
export default function netif({ posix, recv, send, callFunction }) {
15
return {
16
// char *if_indextoname(unsigned int ifindex, char *ifname);
17
if_indextoname: (ifindex: number, ifnamePtr: number): number => {
18
const { if_indextoname } = posix;
19
if (if_indextoname == null) {
20
notImplemented("if_indextoname");
21
}
22
let ifname;
23
try {
24
ifname = if_indextoname(ifindex);
25
} catch (_err) {
26
return 0;
27
}
28
send.string(ifname, {
29
ptr: ifnamePtr,
30
len: constants.IFNAMSIZ,
31
});
32
return ifnamePtr;
33
},
34
35
// unsigned int if_nametoindex(const char *ifname);
36
if_nametoindex: (ifnamePtr: number): number => {
37
const { if_nametoindex } = posix;
38
if (if_nametoindex == null) {
39
notImplemented("if_nametoindex");
40
}
41
const ifname = recv.string(ifnamePtr);
42
try {
43
return if_nametoindex(ifname);
44
} catch (_err) {
45
return 0;
46
}
47
},
48
49
if_nameindex: (): number => {
50
const { if_nameindex } = posix;
51
try {
52
if (if_nameindex == null) {
53
const ptr = callFunction("createNameIndexArray", 0);
54
if (ptr == 0) {
55
throw Error("out of memory");
56
}
57
return ptr;
58
}
59
const ni = if_nameindex();
60
const ptr = callFunction("createNameIndexArray", ni.length);
61
if (ptr == 0) {
62
throw Error("out of memory");
63
}
64
for (let i = 0; i < ni.length; i++) {
65
callFunction(
66
"setNameIndexElement",
67
ptr,
68
i,
69
ni[i][0],
70
send.string(ni[i][1])
71
);
72
}
73
return ptr;
74
} catch (err) {
75
// ret = 0 since pointer and null pointer indicates error.
76
err.ret = 0;
77
throw err;
78
}
79
},
80
81
if_freenameindex: (ptr): void => {
82
callFunction("freeNameIndexArray", ptr);
83
},
84
};
85
}
86
87