This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
libnetutils.js
146 lines (134 loc) · 6.41 KB
/
libnetutils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
"use strict";
let libnetutils = (function () {
let library = ctypes.open("libnetutils.so");
let cutils = ctypes.open("libcutils.so");
let cbuf = ctypes.char.array(4096)();
let c_property_get = cutils.declare("property_get", ctypes.default_abi,
ctypes.int, // return value: length
ctypes.char.ptr, // key
ctypes.char.ptr, // value
ctypes.char.ptr); // default
let property_get = function (key, defaultValue) {
if (defaultValue === undefined) {
defaultValue = null;
}
c_property_get(key, cbuf, defaultValue);
return cbuf.readString();
}
let sdkVersion = parseInt(property_get("ro.build.version.sdk"));
let iface = {
ifc_enable: library.declare("ifc_enable", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_disable: library.declare("ifc_disable", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_add_host_route: library.declare("ifc_add_host_route", ctypes.default_abi, ctypes.int, ctypes.char.ptr, ctypes.int),
ifc_remove_host_routes: library.declare("ifc_remove_host_routes", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_set_default_route: library.declare("ifc_set_default_route", ctypes.default_abi, ctypes.int, ctypes.char.ptr, ctypes.int),
ifc_get_default_route: library.declare("ifc_get_default_route", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_remove_default_route: library.declare("ifc_remove_default_route", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_reset_connections: library.declare("ifc_reset_connections", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
ifc_configure: library.declare("ifc_configure", ctypes.default_abi, ctypes.int, ctypes.char.ptr,
ctypes.int, ctypes.int, ctypes.int, ctypes.int, ctypes.int),
dhcp_stop: library.declare("dhcp_stop", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
dhcp_release_lease: library.declare("dhcp_release_lease", ctypes.default_abi, ctypes.int, ctypes.char.ptr),
dhcp_get_errmsg: library.declare("dhcp_get_errmsg", ctypes.default_abi, ctypes.char.ptr),
};
if (sdkVersion >= 15) {
let ipaddrbuf = ctypes.char.array(4096)();
let gatewaybuf = ctypes.char.array(4096)();
let prefixLen = ctypes.int();
let dns1buf = ctypes.char.array(4096)();
let dns2buf = ctypes.char.array(4096)();
let serverbuf = ctypes.char.array(4096)();
let lease = ctypes.int();
let c_dhcp_do_request =
library.declare("dhcp_do_request", ctypes.default_abi,
ctypes.int, // [return]
ctypes.char.ptr, // ifname
ctypes.char.ptr, // ipaddr
ctypes.char.ptr, // gateway
ctypes.int.ptr, // prefixlen
ctypes.char.ptr, // dns1
ctypes.char.ptr, // dns2
ctypes.char.ptr, // server
ctypes.int.ptr); // lease
let stringToIp = function (s) {
if (!s) return 0;
let comps = s.split(".");
return ((parseInt(comps[0]) & 0xff) << 0 |
(parseInt(comps[1]) & 0xff) << 8 |
(parseInt(comps[2]) & 0xff) << 16 |
(parseInt(comps[3]) & 0xff) << 24);
}
let makeMask = function (len) {
let mask = 0;
for (let i = 0; i < len; ++i)
mask |= (1 << i);
return mask;
}
iface.dhcp_do_request = function (ifname) {
let ret = c_dhcp_do_request(ifname,
ipaddrbuf,
gatewaybuf,
prefixLen.address(),
dns1buf,
dns2buf,
serverbuf,
lease.address());
return {
ret: ret |0,
ipaddr: stringToIp(ipaddrbuf.readString()),
gateway: stringToIp(gatewaybuf.readString()),
mask: makeMask(prefixLen),
dns1: stringToIp(dns1buf.readString()),
dns2: stringToIp(dns2buf.readString()),
server: stringToIp(serverbuf.readString()),
lease: lease |0
};
};
// dhcp_do_request_renew() went away in newer libnetutils.
iface.dhcp_do_request_renew = iface.dhcp_do_request;
} else {
let ints = ctypes.int.array(8)();
let c_dhcp_do_request =
library.declare("dhcp_do_request", ctypes.default_abi,
ctypes.int, // [return]
ctypes.char.ptr, // ifname
ctypes.int.ptr, // ipaddr
ctypes.int.ptr, // gateway
ctypes.int.ptr, // mask
ctypes.int.ptr, // dns1
ctypes.int.ptr, // dns2
ctypes.int.ptr, // server
ctypes.int.ptr); // lease
let c_dhcp_do_request_renew =
library.declare("dhcp_do_request_renew", ctypes.default_abi,
ctypes.int, // [return]
ctypes.char.ptr, // ifname
ctypes.int.ptr, // ipaddr
ctypes.int.ptr, // gateway
ctypes.int.ptr, // mask
ctypes.int.ptr, // dns1
ctypes.int.ptr, // dns2
ctypes.int.ptr, // server
ctypes.int.ptr); // lease
let makeRequestWrapper = function (c_fn) {
return function (ifname) {
let ret = c_fn(ifname,
ints.addressOfElement(0),
ints.addressOfElement(1),
ints.addressOfElement(2),
ints.addressOfElement(3),
ints.addressOfElement(4),
ints.addressOfElement(5),
ints.addressOfElement(6));
return { ret: ret |0, ipaddr: ints[0] |0, gateway: ints[1] |0,
mask: ints[2] |0, dns1: ints[3] |0, dns2: ints[4] |0,
server: ints[5] |0, lease: ints[6] |0 };
};
}
iface.dhcp_do_request = makeRequestWrapper(c_dhcp_do_request);
iface.dhcp_do_request_renew = makeRequestWrapper(c_dhcp_do_request_renew);
}
return iface;
})();