Commit 81f26e77 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Fix routingPrefix handling in network-ip-config

This also adds a constant to the mojom defining an unset value.

Bug: 1000213
Change-Id: Idd3e7239d0142db6b55695ac886f51e562d4e042
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1783242Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693198}
parent 1305ec3d
...@@ -316,6 +316,9 @@ struct FoundNetworkProperties { ...@@ -316,6 +316,9 @@ struct FoundNetworkProperties {
string? long_name; string? long_name;
}; };
// A routing prefix can not be 0. Use 0 to indicate an unset value.
const int32 kNoRoutingPrefix = 0;
struct IPConfigProperties { struct IPConfigProperties {
string? gateway; string? gateway;
// The IP address represented as a string, as provided by the configuration // The IP address represented as a string, as provided by the configuration
...@@ -326,7 +329,7 @@ struct IPConfigProperties { ...@@ -326,7 +329,7 @@ struct IPConfigProperties {
array<string>? included_routes; array<string>? included_routes;
array<string>? name_servers; array<string>? name_servers;
array<string>? search_domains; array<string>? search_domains;
int32 routing_prefix = 0; int32 routing_prefix = kNoRoutingPrefix;
string? type; string? type;
string? web_proxy_auto_discovery_url; string? web_proxy_auto_discovery_url;
}; };
......
...@@ -9,6 +9,85 @@ ...@@ -9,6 +9,85 @@
(function() { (function() {
'use strict'; 'use strict';
/**
* Returns the routing prefix as a string for a given prefix length. If
* |prefixLength| is invalid, returns undefined.
* @param {number} prefixLength The ONC routing prefix length.
* @return {string|undefined}
*/
const getRoutingPrefixAsNetmask = function(prefixLength) {
'use strict';
// Return the empty string for invalid inputs.
if (prefixLength <= 0 || prefixLength > 32) {
return undefined;
}
let netmask = '';
for (let i = 0; i < 4; ++i) {
let remainder = 8;
if (prefixLength >= 8) {
prefixLength -= 8;
} else {
remainder = prefixLength;
prefixLength = 0;
}
if (i > 0) {
netmask += '.';
}
let value = 0;
if (remainder != 0) {
value = ((2 << (remainder - 1)) - 1) << (8 - remainder);
}
netmask += value.toString();
}
return netmask;
};
/**
* Returns the routing prefix length as a number from the netmask string.
* @param {string} netmask The netmask string, e.g. 255.255.255.0.
* @return {number} The corresponding netmask or kNoRoutingPrefix if invalid.
*/
const getRoutingPrefixAsLength = function(netmask) {
'use strict';
let prefixLength = 0;
const tokens = netmask.split('.');
if (tokens.length != 4) {
return -1;
}
for (let i = 0; i < tokens.length; ++i) {
const token = tokens[i];
// If we already found the last mask and the current one is not
// '0' then the netmask is invalid. For example, 255.224.255.0
if (prefixLength / 8 != i) {
if (token != '0') {
return chromeos.networkConfig.mojom.kNoRoutingPrefix;
}
} else if (token == '255') {
prefixLength += 8;
} else if (token == '254') {
prefixLength += 7;
} else if (token == '252') {
prefixLength += 6;
} else if (token == '248') {
prefixLength += 5;
} else if (token == '240') {
prefixLength += 4;
} else if (token == '224') {
prefixLength += 3;
} else if (token == '192') {
prefixLength += 2;
} else if (token == '128') {
prefixLength += 1;
} else if (token == '0') {
prefixLength += 0;
} else {
// mask is not a valid number.
return chromeos.networkConfig.mojom.kNoRoutingPrefix;
}
}
return prefixLength;
};
Polymer({ Polymer({
is: 'network-ip-config', is: 'network-ip-config',
...@@ -156,7 +235,10 @@ Polymer({ ...@@ -156,7 +235,10 @@ Polymer({
for (const key in ipconfig) { for (const key in ipconfig) {
const value = ipconfig[key]; const value = ipconfig[key];
if (key == 'routingPrefix') { if (key == 'routingPrefix') {
result.routingPrefix = CrOnc.getRoutingPrefixAsNetmask(value); const netmask = getRoutingPrefixAsNetmask(value);
if (netmask !== undefined) {
result.routingPrefix = netmask;
}
} else { } else {
result[key] = value; result[key] = value;
} }
...@@ -176,7 +258,10 @@ Polymer({ ...@@ -176,7 +258,10 @@ Polymer({
for (const key in ipconfig) { for (const key in ipconfig) {
const value = ipconfig[key]; const value = ipconfig[key];
if (key == 'routingPrefix') { if (key == 'routingPrefix') {
result.routingPrefix = CrOnc.getRoutingPrefixAsLength(value); const routingPrefix = getRoutingPrefixAsLength(value);
if (routingPrefix != chromeos.networkConfig.mojom.kNoRoutingPrefix) {
result.routingPrefix = routingPrefix;
}
} else { } else {
result[key] = value; result[key] = value;
} }
......
...@@ -486,84 +486,6 @@ CrOnc.setTypeProperty = function(properties, key, value) { ...@@ -486,84 +486,6 @@ CrOnc.setTypeProperty = function(properties, key, value) {
CrOnc.setProperty(properties, typeKey, value); CrOnc.setProperty(properties, typeKey, value);
}; };
/**
* Returns the routing prefix as a string for a given prefix length.
* @param {number} prefixLength The ONC routing prefix length.
* @return {string} The corresponding netmask.
*/
CrOnc.getRoutingPrefixAsNetmask = function(prefixLength) {
'use strict';
// Return the empty string for invalid inputs.
if (prefixLength < 0 || prefixLength > 32) {
return '';
}
let netmask = '';
for (let i = 0; i < 4; ++i) {
let remainder = 8;
if (prefixLength >= 8) {
prefixLength -= 8;
} else {
remainder = prefixLength;
prefixLength = 0;
}
if (i > 0) {
netmask += '.';
}
let value = 0;
if (remainder != 0) {
value = ((2 << (remainder - 1)) - 1) << (8 - remainder);
}
netmask += value.toString();
}
return netmask;
};
/**
* Returns the routing prefix length as a number from the netmask string.
* @param {string} netmask The netmask string, e.g. 255.255.255.0.
* @return {number} The corresponding netmask or -1 if invalid.
*/
CrOnc.getRoutingPrefixAsLength = function(netmask) {
'use strict';
let prefixLength = 0;
const tokens = netmask.split('.');
if (tokens.length != 4) {
return -1;
}
for (let i = 0; i < tokens.length; ++i) {
const token = tokens[i];
// If we already found the last mask and the current one is not
// '0' then the netmask is invalid. For example, 255.224.255.0
if (prefixLength / 8 != i) {
if (token != '0') {
return -1;
}
} else if (token == '255') {
prefixLength += 8;
} else if (token == '254') {
prefixLength += 7;
} else if (token == '252') {
prefixLength += 6;
} else if (token == '248') {
prefixLength += 5;
} else if (token == '240') {
prefixLength += 4;
} else if (token == '224') {
prefixLength += 3;
} else if (token == '192') {
prefixLength += 2;
} else if (token == '128') {
prefixLength += 1;
} else if (token == '0') {
prefixLength += 0;
} else {
// mask is not a valid number.
return -1;
}
}
return prefixLength;
};
/** /**
* Returns a valid CrOnc.Type, or undefined. * Returns a valid CrOnc.Type, or undefined.
* @param {string} typeStr * @param {string} typeStr
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment