Esp32 error check settings from asyncwebserver

I am using asyncwebserver to change settings in preferences.

I can enter text and click submit to change settings in the nvram.
I want to set ip, subnet mask and gateway but there is nothing to stop the entering of illegal information ie 192.168.10.1 is legal but a.b.c.d should be illegal entry.
255.255.255.0 is legal but 0.255.255.255 is illegal.

Are there any examples of running rule checks for these?

You need to make code to check for valid data. Try to opening a connection using the given data. If it fails, report "Not a valid address."

I can now validate the ip subnet and gateway but only as far as numbers between 0 an 255 using the following but was hoping someone already created a library for doing the entire task.

void handleIPAddress(AsyncWebServerRequest *request) {
  String ipAddress = request->getParam("ip_address")->value();

  // Validate the IP address
  if (!validateIPAddress(ipAddress)) {
    request->send(400, "Bad Request", "Invalid IP address");
    return;
  }

  // Process the IP address
  Serial.println("IP address: " + ipAddress);

  request->send(200, "OK", "IP address accepted");
}

bool validateIPAddress(String ipAddress) {
  // Split the IP address into four sections
  String sections[4];
  ipAddress.split(".", sections, 4);

  // Check if the IP address has four sections
  if (sections.length != 4) {
    return false;
  }

  // Check if each section is a valid number
  for (int i = 0; i < 4; i++) {
    int section = sections[i].toInt();
    if (section < 0 || section > 255) {
      return false;
    }
  }

  return true;
}

void setup() {
  Serial.begin(115200);
  server.on("/ip_address", handleIPAddress);
  server.begin();
}

Not me. Prepare for making that code while waiting.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.