Skip to content

Commit

Permalink
[ui] Add ip-address module, force tiny net ip assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
lts-rad committed Jun 28, 2023
1 parent 5534520 commit 17f70b0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"chart.js": "^3.7.1",
"chartjs-adapter-moment": "^1.0.0",
"chroma-js": "^2.4.2",
"ip-address": "^8.1.0",
"jest": "^28.1.0",
"metro-react-native-babel-preset": "^0.72.2",
"miragejs": "^0.1.43",
Expand Down
29 changes: 29 additions & 0 deletions frontend/src/components/Devices/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {
useColorModeValue
} from 'native-base'

import { Address4 } from 'ip-address';

const GroupItem = React.memo(({ name }) => {
let groupIcons = {
wan: faCircleNodes,
Expand Down Expand Up @@ -138,7 +140,34 @@ const Device = ({ device, edit, notifyChange, ...props }) => {
setEditing(name != device.Name)
}

function toLong(ipAddress) {
return ipAddress.parsedAddress.reduce((accumulator, octet) => (accumulator << 8) + Number(octet), 0);
}

function fromLong(long) {
return [(long >>> 24) & 0xff, (long >>> 16) & 0xff, (long >>> 8) & 0xff, long & 0xff].join('.');
}

function makeTinyAddress(ipAddress) {
let subnet
let address
try {
address = new Address4(ipAddress);
subnet = new Address4(address.startAddress().address + '/30');
} catch {
return ipAddress
}

return fromLong(toLong(subnet.startAddress())+2)
}

const handleIP = (ip) => {
//transform ip into a tinynet address , and notify
let new_ip = makeTinyAddress(ip)
if (ip != new_ip) {
context.info("SPR Micro-Segmentation Uses /30 network IP assignments, forcing IP to device IP")
ip = new_ip
}
setIP(ip)
setEditing(ip != device.RecentIP)
}
Expand Down
18 changes: 18 additions & 0 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7616,6 +7616,14 @@ invariant@*, invariant@^2.2.4:
dependencies:
loose-envify "^1.0.0"

ip-address@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/ip-address/-/ip-address-8.1.0.tgz#1fe9b4509b51ff7d2fbbef4d3d26994d9915a459"
integrity sha512-Wz91gZKpNKoXtqvY8ScarKYwhXoK4r/b5QuT+uywe/azv0/nUCo7Bh0IRRI7F9DHR06kJNWtzMGLIbXavngbKA==
dependencies:
jsbn "1.1.0"
sprintf-js "1.1.2"

ip@^1.1.5:
version "1.1.8"
resolved "https://registry.npmjs.org/ip/-/ip-1.1.8.tgz"
Expand Down Expand Up @@ -8951,6 +8959,11 @@ js-yaml@^4.1.0:
dependencies:
argparse "^2.0.1"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-1.1.0.tgz#b01307cb29b618a1ed26ec79e911f803c4da0040"
integrity sha512-4bYVV3aAMtDTTu4+xsDYa6sy9GyJ69/amsu9sYF2zqjiEoZA5xJi3BrfX3uY+/IekIu7MwdObdbDWpoZdBv3/A==

jsc-android@^250230.2.1:
version "250230.2.1"
resolved "https://registry.npmjs.org/jsc-android/-/jsc-android-250230.2.1.tgz"
Expand Down Expand Up @@ -12716,6 +12729,11 @@ split-string@^3.0.1, split-string@^3.0.2:
dependencies:
extend-shallow "^3.0.0"

[email protected]:
version "1.1.2"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673"
integrity sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==

sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz"
Expand Down

0 comments on commit 17f70b0

Please sign in to comment.