Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
base/net/web-net-script.js@147:ea6f647725a1, 2021-01-10 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Jan 10 11:53:12 2021 +0000
- Revision:
- 147:ea6f647725a1
- Parent:
- 146:6bc151bd7063
- Child:
- 148:3d685db9fdd3
Added ability for the user to resolve a name.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 132:5b2df69a4f17 | 1 | //Net script |
| andrewboyson | 132:5b2df69a4f17 | 2 | 'use strict'; |
| andrewboyson | 132:5b2df69a4f17 | 3 | |
| andrewboyson | 132:5b2df69a4f17 | 4 | let mac = ''; |
| andrewboyson | 147:ea6f647725a1 | 5 | let nr = ''; |
| andrewboyson | 132:5b2df69a4f17 | 6 | let tcp = ''; |
| andrewboyson | 143:cc2e148cb96a | 7 | let dns = ''; |
| andrewboyson | 146:6bc151bd7063 | 8 | let a4s = null; |
| andrewboyson | 146:6bc151bd7063 | 9 | let a6s = null; |
| andrewboyson | 132:5b2df69a4f17 | 10 | |
| andrewboyson | 132:5b2df69a4f17 | 11 | function parseTcpLine(line) |
| andrewboyson | 132:5b2df69a4f17 | 12 | { |
| andrewboyson | 132:5b2df69a4f17 | 13 | if (line.length == 0) return; |
| andrewboyson | 132:5b2df69a4f17 | 14 | let fields = line.split('\t'); |
| andrewboyson | 133:98c6bf14bc37 | 15 | let state = parseInt(fields[0], 16); |
| andrewboyson | 133:98c6bf14bc37 | 16 | let idleMs = parseInt(fields[1], 16); |
| andrewboyson | 133:98c6bf14bc37 | 17 | let ipType = parseInt(fields[2], 16); |
| andrewboyson | 133:98c6bf14bc37 | 18 | let arIndex = parseInt(fields[3], 16); |
| andrewboyson | 133:98c6bf14bc37 | 19 | let locPort = parseInt(fields[4], 16); |
| andrewboyson | 133:98c6bf14bc37 | 20 | let remPort = parseInt(fields[5], 16); |
| andrewboyson | 133:98c6bf14bc37 | 21 | let bytesRcvd = parseInt(fields[6], 16); |
| andrewboyson | 133:98c6bf14bc37 | 22 | let bytesSent = parseInt(fields[7], 16); |
| andrewboyson | 132:5b2df69a4f17 | 23 | |
| andrewboyson | 133:98c6bf14bc37 | 24 | switch (state) |
| andrewboyson | 133:98c6bf14bc37 | 25 | { |
| andrewboyson | 133:98c6bf14bc37 | 26 | case 1: tcp += " Syn"; break; |
| andrewboyson | 133:98c6bf14bc37 | 27 | case 2: tcp += " Est"; break; |
| andrewboyson | 133:98c6bf14bc37 | 28 | case 3: tcp += " Wait"; break; |
| andrewboyson | 133:98c6bf14bc37 | 29 | default: tcp += state.toString().padStart(5, ' '); break; |
| andrewboyson | 133:98c6bf14bc37 | 30 | } |
| andrewboyson | 132:5b2df69a4f17 | 31 | |
| andrewboyson | 133:98c6bf14bc37 | 32 | let idleMinutes = Math.floor(idleMs / 1000 / 60); |
| andrewboyson | 133:98c6bf14bc37 | 33 | tcp += idleMinutes.toString().padStart(4, ' '); |
| andrewboyson | 133:98c6bf14bc37 | 34 | tcp += " "; |
| andrewboyson | 132:5b2df69a4f17 | 35 | |
| andrewboyson | 133:98c6bf14bc37 | 36 | if (ipType == 0x0800) tcp += "IPv4"; |
| andrewboyson | 132:5b2df69a4f17 | 37 | else if (ipType == 0x86DD) tcp += "IPv6"; |
| andrewboyson | 132:5b2df69a4f17 | 38 | else tcp += fields[1]; |
| andrewboyson | 132:5b2df69a4f17 | 39 | |
| andrewboyson | 133:98c6bf14bc37 | 40 | tcp += arIndex.toString().padStart(4, ' '); |
| andrewboyson | 133:98c6bf14bc37 | 41 | tcp += locPort.toString().padStart(9, ' '); |
| andrewboyson | 133:98c6bf14bc37 | 42 | tcp += remPort.toString().padStart(9, ' '); |
| andrewboyson | 133:98c6bf14bc37 | 43 | tcp += bytesRcvd.toString().padStart(6, ' '); |
| andrewboyson | 133:98c6bf14bc37 | 44 | tcp += bytesSent.toString().padStart(6, ' '); |
| andrewboyson | 132:5b2df69a4f17 | 45 | tcp += '\r\n'; |
| andrewboyson | 132:5b2df69a4f17 | 46 | } |
| andrewboyson | 143:cc2e148cb96a | 47 | function parseDnsLine(line) |
| andrewboyson | 143:cc2e148cb96a | 48 | { |
| andrewboyson | 143:cc2e148cb96a | 49 | if (line.length == 0) return; |
| andrewboyson | 143:cc2e148cb96a | 50 | let fields = line.split('\t'); |
| andrewboyson | 143:cc2e148cb96a | 51 | |
| andrewboyson | 143:cc2e148cb96a | 52 | dns += parseInt(fields[0], 16).toString().padStart(2, ' '); |
| andrewboyson | 143:cc2e148cb96a | 53 | let minutes = parseInt(fields[1], 16) / 1000 / 60; |
| andrewboyson | 143:cc2e148cb96a | 54 | dns += Math.floor(minutes).toString().padStart(4, ' '); |
| andrewboyson | 143:cc2e148cb96a | 55 | dns += ' '; |
| andrewboyson | 147:ea6f647725a1 | 56 | let addrType = fields[2]; |
| andrewboyson | 147:ea6f647725a1 | 57 | switch (addrType) |
| andrewboyson | 147:ea6f647725a1 | 58 | { |
| andrewboyson | 147:ea6f647725a1 | 59 | case '4': dns += ' A'; break; |
| andrewboyson | 147:ea6f647725a1 | 60 | case '6': dns += 'AAAA'; break; |
| andrewboyson | 147:ea6f647725a1 | 61 | default: dns += addrType; break; |
| andrewboyson | 147:ea6f647725a1 | 62 | } |
| andrewboyson | 147:ea6f647725a1 | 63 | dns += ' '; |
| andrewboyson | 146:6bc151bd7063 | 64 | let addr = ''; |
| andrewboyson | 146:6bc151bd7063 | 65 | let mac = ''; |
| andrewboyson | 147:ea6f647725a1 | 66 | switch (addrType) |
| andrewboyson | 145:d2bd78be00b2 | 67 | { |
| andrewboyson | 147:ea6f647725a1 | 68 | case '4': |
| andrewboyson | 147:ea6f647725a1 | 69 | addr = Net.makeIp4(fields[3]); |
| andrewboyson | 147:ea6f647725a1 | 70 | if (a4s.has(addr)) mac = a4s.get(addr); |
| andrewboyson | 147:ea6f647725a1 | 71 | break; |
| andrewboyson | 147:ea6f647725a1 | 72 | case '6': |
| andrewboyson | 147:ea6f647725a1 | 73 | addr = Net.makeIp6(fields[3]); |
| andrewboyson | 147:ea6f647725a1 | 74 | if (a6s.has(addr)) mac = a6s.get(addr); |
| andrewboyson | 147:ea6f647725a1 | 75 | break; |
| andrewboyson | 147:ea6f647725a1 | 76 | } |
| andrewboyson | 147:ea6f647725a1 | 77 | /* |
| andrewboyson | 147:ea6f647725a1 | 78 | if (fields[3].substr( 0, 2) === '00') //Is empty or IP4 |
| andrewboyson | 147:ea6f647725a1 | 79 | { |
| andrewboyson | 147:ea6f647725a1 | 80 | if (fields[3].substr(24, 2) === '00') //Is empty |
| andrewboyson | 145:d2bd78be00b2 | 81 | { |
| andrewboyson | 146:6bc151bd7063 | 82 | addr = ''; |
| andrewboyson | 145:d2bd78be00b2 | 83 | } |
| andrewboyson | 145:d2bd78be00b2 | 84 | else //Is Ip4 |
| andrewboyson | 145:d2bd78be00b2 | 85 | { |
| andrewboyson | 147:ea6f647725a1 | 86 | addr = Net.makeIp4Rev(fields[3].substr(24, 8)); |
| andrewboyson | 146:6bc151bd7063 | 87 | if (a4s.has(addr)) mac = a4s.get(addr); |
| andrewboyson | 145:d2bd78be00b2 | 88 | } |
| andrewboyson | 145:d2bd78be00b2 | 89 | } |
| andrewboyson | 145:d2bd78be00b2 | 90 | else //Is IP6 |
| andrewboyson | 145:d2bd78be00b2 | 91 | { |
| andrewboyson | 147:ea6f647725a1 | 92 | addr = Net.makeIp6(fields[3]); |
| andrewboyson | 146:6bc151bd7063 | 93 | if (a6s.has(addr)) mac = a6s.get(addr) |
| andrewboyson | 145:d2bd78be00b2 | 94 | } |
| andrewboyson | 147:ea6f647725a1 | 95 | */ |
| andrewboyson | 146:6bc151bd7063 | 96 | dns += addr.padEnd(40, ' '); |
| andrewboyson | 146:6bc151bd7063 | 97 | |
| andrewboyson | 146:6bc151bd7063 | 98 | dns += ' '; |
| andrewboyson | 147:ea6f647725a1 | 99 | dns += fields[4]; //DNS protocol used |
| andrewboyson | 143:cc2e148cb96a | 100 | dns += ' '; |
| andrewboyson | 147:ea6f647725a1 | 101 | dns += fields[5].padEnd(32, ' '); //Resolved name |
| andrewboyson | 146:6bc151bd7063 | 102 | |
| andrewboyson | 146:6bc151bd7063 | 103 | if (mac != '') |
| andrewboyson | 146:6bc151bd7063 | 104 | { |
| andrewboyson | 146:6bc151bd7063 | 105 | dns += ' '; |
| andrewboyson | 146:6bc151bd7063 | 106 | dns += mac; |
| andrewboyson | 146:6bc151bd7063 | 107 | dns += ' '; |
| andrewboyson | 146:6bc151bd7063 | 108 | |
| andrewboyson | 146:6bc151bd7063 | 109 | let vendor = Net.getVendorFromLocalStorage(mac); |
| andrewboyson | 146:6bc151bd7063 | 110 | if (vendor) |
| andrewboyson | 146:6bc151bd7063 | 111 | { |
| andrewboyson | 146:6bc151bd7063 | 112 | dns += ' '; |
| andrewboyson | 146:6bc151bd7063 | 113 | dns += vendor; |
| andrewboyson | 146:6bc151bd7063 | 114 | } |
| andrewboyson | 146:6bc151bd7063 | 115 | else |
| andrewboyson | 146:6bc151bd7063 | 116 | { |
| andrewboyson | 146:6bc151bd7063 | 117 | Net.addVendorToLocalStorage(mac); |
| andrewboyson | 146:6bc151bd7063 | 118 | } |
| andrewboyson | 146:6bc151bd7063 | 119 | } |
| andrewboyson | 143:cc2e148cb96a | 120 | dns += '\r\n'; |
| andrewboyson | 143:cc2e148cb96a | 121 | } |
| andrewboyson | 146:6bc151bd7063 | 122 | function parseAr4Line(line) |
| andrewboyson | 146:6bc151bd7063 | 123 | { |
| andrewboyson | 146:6bc151bd7063 | 124 | if (line.length == 0) return; |
| andrewboyson | 146:6bc151bd7063 | 125 | let fields = line.split('\t'); |
| andrewboyson | 146:6bc151bd7063 | 126 | |
| andrewboyson | 146:6bc151bd7063 | 127 | let index = parseInt(fields[0], 16); |
| andrewboyson | 146:6bc151bd7063 | 128 | let ms = parseInt(fields[1], 16); |
| andrewboyson | 146:6bc151bd7063 | 129 | let addr = Net.makeIp4(fields[2]); |
| andrewboyson | 146:6bc151bd7063 | 130 | let mac = Net.makeMac(fields[3]); |
| andrewboyson | 146:6bc151bd7063 | 131 | |
| andrewboyson | 146:6bc151bd7063 | 132 | a4s.set(addr, mac); |
| andrewboyson | 146:6bc151bd7063 | 133 | } |
| andrewboyson | 146:6bc151bd7063 | 134 | function parseAr6Line(line) |
| andrewboyson | 146:6bc151bd7063 | 135 | { |
| andrewboyson | 146:6bc151bd7063 | 136 | if (line.length == 0) return; |
| andrewboyson | 146:6bc151bd7063 | 137 | let fields = line.split('\t'); |
| andrewboyson | 146:6bc151bd7063 | 138 | |
| andrewboyson | 146:6bc151bd7063 | 139 | let index = parseInt(fields[0], 16); |
| andrewboyson | 146:6bc151bd7063 | 140 | let ms = parseInt(fields[1], 16); |
| andrewboyson | 146:6bc151bd7063 | 141 | let addr = Net.makeIp6(fields[2]); |
| andrewboyson | 146:6bc151bd7063 | 142 | let mac = Net.makeMac(fields[3]); |
| andrewboyson | 146:6bc151bd7063 | 143 | |
| andrewboyson | 146:6bc151bd7063 | 144 | a6s.set(addr, mac); |
| andrewboyson | 146:6bc151bd7063 | 145 | } |
| andrewboyson | 146:6bc151bd7063 | 146 | function parseGenLines(text) |
| andrewboyson | 146:6bc151bd7063 | 147 | { |
| andrewboyson | 146:6bc151bd7063 | 148 | let lines = text.split('\n'); |
| andrewboyson | 146:6bc151bd7063 | 149 | mac = Net.makeMac(lines[ 0]); |
| andrewboyson | 147:ea6f647725a1 | 150 | nr = lines[ 1] ; |
| andrewboyson | 146:6bc151bd7063 | 151 | } |
| andrewboyson | 146:6bc151bd7063 | 152 | function parseTcpLines(text) |
| andrewboyson | 146:6bc151bd7063 | 153 | { |
| andrewboyson | 146:6bc151bd7063 | 154 | tcp = "State Idle Protocol ARI Port-Loc Port-Rem Rcvd Sent\n"; |
| andrewboyson | 146:6bc151bd7063 | 155 | text.split('\n').forEach(parseTcpLine); |
| andrewboyson | 146:6bc151bd7063 | 156 | } |
| andrewboyson | 144:7106252b7abf | 157 | function parseDnsLines(text) |
| andrewboyson | 144:7106252b7abf | 158 | { |
| andrewboyson | 144:7106252b7abf | 159 | dns = ''; |
| andrewboyson | 144:7106252b7abf | 160 | text.split('\n').forEach(parseDnsLine); |
| andrewboyson | 144:7106252b7abf | 161 | } |
| andrewboyson | 146:6bc151bd7063 | 162 | function parseAr4Lines(text) |
| andrewboyson | 132:5b2df69a4f17 | 163 | { |
| andrewboyson | 146:6bc151bd7063 | 164 | a4s = new Map(); |
| andrewboyson | 146:6bc151bd7063 | 165 | text.split('\n').forEach(parseAr4Line); |
| andrewboyson | 146:6bc151bd7063 | 166 | } |
| andrewboyson | 146:6bc151bd7063 | 167 | function parseAr6Lines(text) |
| andrewboyson | 146:6bc151bd7063 | 168 | { |
| andrewboyson | 146:6bc151bd7063 | 169 | a6s = new Map(); |
| andrewboyson | 146:6bc151bd7063 | 170 | text.split('\n').forEach(parseAr6Line); |
| andrewboyson | 132:5b2df69a4f17 | 171 | } |
| andrewboyson | 132:5b2df69a4f17 | 172 | function parse() |
| andrewboyson | 132:5b2df69a4f17 | 173 | { |
| andrewboyson | 132:5b2df69a4f17 | 174 | let topics = Ajax.response.split('\f'); |
| andrewboyson | 132:5b2df69a4f17 | 175 | parseGenLines(topics[0]); |
| andrewboyson | 132:5b2df69a4f17 | 176 | parseTcpLines(topics[1]); |
| andrewboyson | 146:6bc151bd7063 | 177 | parseAr4Lines(topics[3]); |
| andrewboyson | 146:6bc151bd7063 | 178 | parseAr6Lines(topics[4]); |
| andrewboyson | 143:cc2e148cb96a | 179 | parseDnsLines(topics[2]); |
| andrewboyson | 132:5b2df69a4f17 | 180 | } |
| andrewboyson | 132:5b2df69a4f17 | 181 | function display() |
| andrewboyson | 132:5b2df69a4f17 | 182 | { |
| andrewboyson | 132:5b2df69a4f17 | 183 | let elem; |
| andrewboyson | 132:5b2df69a4f17 | 184 | |
| andrewboyson | 132:5b2df69a4f17 | 185 | elem = Ajax.getElementOrNull('ajax-tcp' ); if (elem) elem.textContent = tcp; |
| andrewboyson | 132:5b2df69a4f17 | 186 | elem = Ajax.getElementOrNull('ajax-mac' ); if (elem) elem.textContent = mac; |
| andrewboyson | 147:ea6f647725a1 | 187 | elem = Ajax.getElementOrNull('ajax-nr' ); if (elem) elem.value = nr; |
| andrewboyson | 143:cc2e148cb96a | 188 | elem = Ajax.getElementOrNull('ajax-dns' ); if (elem) elem.textContent = dns; |
| andrewboyson | 132:5b2df69a4f17 | 189 | } |
| andrewboyson | 132:5b2df69a4f17 | 190 | |
| andrewboyson | 132:5b2df69a4f17 | 191 | Ajax.server = '/net-ajax'; |
| andrewboyson | 132:5b2df69a4f17 | 192 | Ajax.onResponse = function() { parse(); display(); }; |
| andrewboyson | 132:5b2df69a4f17 | 193 | Ajax.init(); |