Skip to content

Commit

Permalink
feat: Added matching between ipv4 and DHCP range (#5576)
Browse files Browse the repository at this point in the history
* feat: Added matching between ipv4 and DHCP range

Signed-off-by: MMaiero <[email protected]>

* feat: Improved range checking

Signed-off-by: MMaiero <[email protected]>

---------

Signed-off-by: MMaiero <[email protected]>
  • Loading branch information
MMaiero authored Nov 22, 2024
1 parent ad967d6 commit 11550f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ public boolean isDirty() {

@Override
public boolean isValid() {
checkSubnetMatch();
return !(this.groupRouter.getValidationState().equals(ValidationState.ERROR)
|| this.groupBegin.getValidationState().equals(ValidationState.ERROR)
|| this.groupEnd.getValidationState().equals(ValidationState.ERROR)
Expand Down Expand Up @@ -455,7 +456,7 @@ private void initDHCPEndAddress() {
this.end.addMouseOutHandler(event -> resetHelp());
this.end.addValueChangeHandler(event -> {
setDirty(true);
setDHCPEndAddressValidation();
setDHCPRangeValidation();
});
}

Expand All @@ -472,7 +473,7 @@ private void initDHCPBeginAddress() {
this.begin.addMouseOutHandler(event -> resetHelp());
this.begin.addValueChangeHandler(event -> {
setDirty(true);
setDHCPBeginAddressValidation();
setDHCPRangeValidation();
});
}

Expand Down Expand Up @@ -532,17 +533,6 @@ private void resetHelp() {
this.helpText.add(new Span(MSGS.netHelpDefaultHint()));
}

private boolean isPositiveInteger(String value) {
try {
if (Integer.parseInt(value) > 0) {
return true;
}
} catch (NumberFormatException e) {
return false;
}
return false;
}

private void checkDhcpRangeValidity() {
if (!isDhcpRangeValid()) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
Expand Down Expand Up @@ -575,8 +565,7 @@ private long addressToLong(String address) {

private void setValidations() {
setDHCPSubnetMaskValidation();
setDHCPEndAddressValidation();
setDHCPBeginAddressValidation();
setDHCPRangeValidation();
setMaxLeaseTimeValidation();
setDefaultLeaseTimeValidation();
}
Expand All @@ -585,7 +574,7 @@ private void setDHCPSubnetMaskValidation() {
GwtConfigParameter param = new GwtConfigParameter();
param.setRequired(true);
param.setType(GwtConfigParameterType.STRING);
if (!validateTextBox(param, groupSubnet).isEmpty()) {
if (!validateTextBox(param, this.groupSubnet).isEmpty()) {
TabDhcp4NatUi.this.groupSubnet.setValidationState(ValidationState.ERROR);
}
if (!TabDhcp4NatUi.this.subnet.getText().matches(REGEX_IPV4)) {
Expand All @@ -595,31 +584,55 @@ private void setDHCPSubnetMaskValidation() {
}
}

private void setDHCPEndAddressValidation() {
private void setDHCPRangeValidation() {
GwtConfigParameter param = new GwtConfigParameter();
param.setRequired(true);
param.setType(GwtConfigParameterType.STRING);
if (!validateTextBox(param, groupEnd).isEmpty()) {
if (!validateTextBox(param, this.groupBegin).isEmpty()) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
}
if (!validateTextBox(param, this.groupEnd).isEmpty()) {
TabDhcp4NatUi.this.groupEnd.setValidationState(ValidationState.ERROR);
}
if (!TabDhcp4NatUi.this.end.getText().matches(REGEX_IPV4)) {
TabDhcp4NatUi.this.groupEnd.setValidationState(ValidationState.ERROR);
} else {
return;
}
if (!TabDhcp4NatUi.this.begin.getText().matches(REGEX_IPV4)) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
return;
}

checkSubnetMatch();

if (TabDhcp4NatUi.this.groupBegin.getValidationState().equals(ValidationState.NONE)) {
checkDhcpRangeValidity();
}

}

private void setDHCPBeginAddressValidation() {
GwtConfigParameter param = new GwtConfigParameter();
param.setRequired(true);
param.setType(GwtConfigParameterType.STRING);
if (!validateTextBox(param, groupBegin).isEmpty()) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
private void checkSubnetMatch() {
if (ROUTER_OFF_MESSAGE.equals(this.router.getSelectedValue())) {
return;
}
if (!TabDhcp4NatUi.this.begin.getText().matches(REGEX_IPV4)) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
boolean isValid = false;

long interfaceIP = addressToLong(this.tcpTab.ip.getText().trim());
long rangeStart = addressToLong(TabDhcp4NatUi.this.begin.getText().trim());
long rangeEnd = addressToLong(TabDhcp4NatUi.this.end.getText().trim());
long subnetMask = addressToLong(this.tcpTab.subnet.getText().trim());

if ((interfaceIP & subnetMask) == (rangeStart & subnetMask)
&& (interfaceIP & subnetMask) == (rangeEnd & subnetMask)) {
isValid = true;
}

if (isValid) {
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.NONE);
TabDhcp4NatUi.this.groupEnd.setValidationState(ValidationState.NONE);
} else {
checkDhcpRangeValidity();
TabDhcp4NatUi.this.groupBegin.setValidationState(ValidationState.ERROR);
TabDhcp4NatUi.this.groupEnd.setValidationState(ValidationState.ERROR);
}
}

Expand All @@ -628,7 +641,7 @@ private void setMaxLeaseTimeValidation() {
param.setRequired(true);
param.setType(GwtConfigParameterType.INTEGER);
param.setMin("1");
if (!validateTextBox(param, groupMaxLease).isEmpty()) {
if (!validateTextBox(param, this.groupMaxLease).isEmpty()) {
TabDhcp4NatUi.this.groupMaxLease.setValidationState(ValidationState.ERROR);
} else {
TabDhcp4NatUi.this.groupMaxLease.setValidationState(ValidationState.NONE);
Expand All @@ -640,7 +653,7 @@ private void setDefaultLeaseTimeValidation() {
param.setRequired(true);
param.setType(GwtConfigParameterType.INTEGER);
param.setMin("1");
if (!validateTextBox(param, groupDefaultLease).isEmpty()) {
if (!validateTextBox(param, this.groupDefaultLease).isEmpty()) {
TabDhcp4NatUi.this.groupDefaultLease.setValidationState(ValidationState.ERROR);
} else {
TabDhcp4NatUi.this.groupDefaultLease.setValidationState(ValidationState.NONE);
Expand All @@ -663,7 +676,8 @@ public void onFailure(Throwable ex) {

@Override
public void onSuccess(GwtXSRFToken token) {
TabDhcp4NatUi.this.gwtNetworkService.getDhcpLeases(token, selectedNetIfConfig.getInterfaceName(),
TabDhcp4NatUi.this.gwtNetworkService.getDhcpLeases(token,
TabDhcp4NatUi.this.selectedNetIfConfig.getInterfaceName(),
new AsyncCallback<List<String>>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2011, 2024 Eurotech and/or its affiliates and others
*
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Eurotech
* Areti
Expand Down Expand Up @@ -577,6 +577,9 @@ private void initIpAddressField() {
TabIp4Ui.this.groupIp.setValidationState(ValidationState.NONE);
TabIp4Ui.this.helpIp.setText("");
}
if (this.tabs.dhcp4NatTab != null) {
this.tabs.dhcp4NatTab.isValid();
}
});
}

Expand Down

0 comments on commit 11550f4

Please sign in to comment.