|
1 | 1 | "use strict";
|
2 | 2 |
|
3 |
| -const getSqlStatement = family => { |
4 |
| - let sql = |
5 |
| - "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT'"; |
6 |
| - |
7 |
| - if (family === "v4") { |
8 |
| - sql += " and CONNECTION_TYPE='IPV4'"; |
9 |
| - } else { |
10 |
| - sql += " and CONNECTION_TYPE='IPV6'"; |
11 |
| - } |
12 |
| - |
13 |
| - return sql; |
| 3 | +const queries = { |
| 4 | + v4: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV4'", |
| 5 | + v6: "select NEXT_HOP, LOCAL_BINDING_INTERFACE from QSYS2.NETSTAT_ROUTE_INFO where ROUTE_TYPE='DFTROUTE' and NEXT_HOP!='*DIRECT' and CONNECTION_TYPE='IPV6'", |
14 | 6 | };
|
15 | 7 |
|
16 |
| -const getGatewayInformationAsync = async family => { |
| 8 | +const get = family => { |
17 | 9 | return new Promise((resolve, reject) => {
|
18 | 10 | try {
|
19 | 11 | const idbConnector = require("idb-connector");
|
20 | 12 |
|
21 | 13 | const dbconn = new idbConnector.dbconn();
|
22 | 14 | dbconn.conn("*LOCAL");
|
23 | 15 |
|
24 |
| - const sql = getSqlStatement(family); |
25 |
| - const stmt = new idbConnector.dbstmt(dbconn); |
26 |
| - |
27 |
| - stmt.exec(sql, async results => { |
| 16 | + const dbstmt = new idbConnector.dbstmt(dbconn); |
| 17 | + dbstmt.exec(queries[family], results => { |
28 | 18 | try {
|
29 |
| - stmt.close(); |
| 19 | + dbstmt.close(); |
30 | 20 | dbconn.disconn();
|
31 | 21 | dbconn.close();
|
32 | 22 | } catch (err) {
|
33 |
| - reject(new Error("Unable to determine default gateway")); |
34 |
| - return; |
| 23 | + return reject(err); |
35 | 24 | }
|
36 | 25 |
|
37 | 26 | if (results && results[0] && results[0].NEXT_HOP) {
|
38 | 27 | resolve({
|
39 | 28 | gateway: results[0].NEXT_HOP,
|
40 |
| - interface: results[0].LOCAL_BINDING_INTERFACE || "" |
| 29 | + interface: results[0].LOCAL_BINDING_INTERFACE || null, |
41 | 30 | });
|
42 | 31 | } else {
|
43 |
| - reject(new Error("Unable to determine default gateway")); |
| 32 | + return reject(new Error("Unable to determine default gateway")); |
44 | 33 | }
|
45 | 34 | });
|
46 | 35 | } catch (err) {
|
47 |
| - reject(new Error("Unable to determine default gateway")); |
48 |
| - return; |
| 36 | + return reject(err); |
49 | 37 | }
|
50 | 38 | });
|
51 | 39 | };
|
52 | 40 |
|
53 |
| -const getGatewayInformationSync = family => { |
54 |
| - let results; |
55 |
| - try { |
56 |
| - const idbConnector = require("idb-connector"); |
| 41 | +const getSync = family => { |
| 42 | + const idbConnector = require("idb-connector"); |
57 | 43 |
|
58 |
| - const dbconn = new idbConnector.dbconn(); |
59 |
| - dbconn.conn("*LOCAL"); |
| 44 | + const dbconn = new idbConnector.dbconn(); |
| 45 | + dbconn.conn("*LOCAL"); |
60 | 46 |
|
61 |
| - const sql = getSqlStatement(family); |
62 |
| - const stmt = new idbConnector.dbstmt(dbconn); |
| 47 | + const dbstmt = new idbConnector.dbstmt(dbconn); |
| 48 | + const results = dbstmt.execSync(queries[family]); |
63 | 49 |
|
64 |
| - results = stmt.execSync(sql); |
65 |
| - |
66 |
| - stmt.close(); |
67 |
| - dbconn.disconn(); |
68 |
| - dbconn.close(); |
69 |
| - } catch (err) { |
70 |
| - throw new Error("Unable to determine default gateway"); |
71 |
| - } |
| 50 | + dbstmt.close(); |
| 51 | + dbconn.disconn(); |
| 52 | + dbconn.close(); |
72 | 53 |
|
73 | 54 | if (results && results[0] && results[0].NEXT_HOP) {
|
74 | 55 | return {
|
75 | 56 | gateway: results[0].NEXT_HOP,
|
76 |
| - interface: results[0].LOCAL_BINDING_INTERFACE || "" |
| 57 | + interface: results[0].LOCAL_BINDING_INTERFACE || null, |
77 | 58 | };
|
78 | 59 | } else {
|
79 | 60 | throw new Error("Unable to determine default gateway");
|
80 | 61 | }
|
81 | 62 | };
|
82 | 63 |
|
83 |
| -const promise = family => { |
84 |
| - return getGatewayInformationAsync(family); |
85 |
| -}; |
86 |
| - |
87 |
| -const sync = family => { |
88 |
| - return getGatewayInformationSync(family); |
89 |
| -}; |
| 64 | +const promise = family => get(family); |
| 65 | +const sync = family => getSync(family); |
90 | 66 |
|
91 | 67 | module.exports.v4 = () => promise("v4");
|
92 | 68 | module.exports.v6 = () => promise("v6");
|
|
0 commit comments