gluster / glusterfs

Gluster Filesystem : Build your distributed storage in minutes
https://www.gluster.org
GNU General Public License v2.0
4.51k stars 1.07k forks source link

cli: Fix Hostname validation #4310

Closed aravindavk closed 3 months ago

aravindavk commented 3 months ago

Gluster volume create command fails to use the hostnames that starts with 0.

Please provide a valid hostname/ip other than localhost, 127.0.0.1 or
loopback address (0.0.0.0 to 0.255.255.255).

For example:

gluster volume create vol1 0.s1.dev:/data/gfs/vol1/b1 force

Fixes: #4309

gluster-ant commented 3 months ago

CLANG-FORMAT FAILURE: Before merging the patch, this diff needs to be considered for passing clang-format

index 3e66b0553..ecba8790c 100644
--- a/cli/src/cli-cmd-parser.c
+++ b/cli/src/cli-cmd-parser.c
@@ -131,7 +131,8 @@ cli_cmd_ta_brick_parse(const char **words, int wordcount, char **ta_brick)

     ip_len = strlen(host_name);
     if (!(strcmp(host_name, "localhost") && strcmp(host_name, "127.0.0.1")) ||
-        (valid_ipv4_address(host_name, ip_len, 0) && !strncmp(host_name, "0.", 2))) {
+        (valid_ipv4_address(host_name, ip_len, 0) &&
+         !strncmp(host_name, "0.", 2))) {
         cli_err(
             "Please provide a valid hostname/ip other "
             "than localhost, 127.0.0.1 or loopback "
@@ -232,8 +233,10 @@ cli_cmd_bricks_parse(const char **words, int wordcount, int brick_index,
         }

         ip_len = strlen(host_name);
-        if (!(strcmp(host_name, "localhost") && strcmp(host_name, "127.0.0.1")) ||
-            (valid_ipv4_address(host_name, ip_len, 0) && !strncmp(host_name, "0.", 2))) {
+        if (!(strcmp(host_name, "localhost") &&
+              strcmp(host_name, "127.0.0.1")) ||
+            (valid_ipv4_address(host_name, ip_len, 0) &&
+             !strncmp(host_name, "0.", 2))) {
             cli_err(
                 "Please provide a valid hostname/ip other "
                 "than localhost, 127.0.0.1 or loopback "
aravindavk commented 3 months ago

/run regression

aravindavk commented 3 months ago

I would prefer to move the repeated code into a function to avoid code duplication.

Updated.

aravindavk commented 3 months ago

/run regression