A function that checks if a string subnet mask is valid

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I've written a small helper function that receives a string and checks if it translates to a valid subnet mask value (255.255.255.0 for example).



I would appreciate some peer review!



static bool isValidSubnetMask(IN char *subNetMask)

char *str = NULL;
char *endptr;
int counter = 0;
long int nChainInstance = -1;

/*Check if string is valid*/
if (!subNetMask






share|improve this question

















  • 3




    Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
    – Wingblade
    Mar 20 at 15:25










  • You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
    – Toby Speight
    Mar 20 at 16:06










  • Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
    – Toby Speight
    Mar 20 at 16:08










  • @wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
    – dear_tzvi
    Mar 20 at 18:18
















up vote
4
down vote

favorite












I've written a small helper function that receives a string and checks if it translates to a valid subnet mask value (255.255.255.0 for example).



I would appreciate some peer review!



static bool isValidSubnetMask(IN char *subNetMask)

char *str = NULL;
char *endptr;
int counter = 0;
long int nChainInstance = -1;

/*Check if string is valid*/
if (!subNetMask






share|improve this question

















  • 3




    Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
    – Wingblade
    Mar 20 at 15:25










  • You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
    – Toby Speight
    Mar 20 at 16:06










  • Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
    – Toby Speight
    Mar 20 at 16:08










  • @wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
    – dear_tzvi
    Mar 20 at 18:18












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I've written a small helper function that receives a string and checks if it translates to a valid subnet mask value (255.255.255.0 for example).



I would appreciate some peer review!



static bool isValidSubnetMask(IN char *subNetMask)

char *str = NULL;
char *endptr;
int counter = 0;
long int nChainInstance = -1;

/*Check if string is valid*/
if (!subNetMask






share|improve this question













I've written a small helper function that receives a string and checks if it translates to a valid subnet mask value (255.255.255.0 for example).



I would appreciate some peer review!



static bool isValidSubnetMask(IN char *subNetMask)

char *str = NULL;
char *endptr;
int counter = 0;
long int nChainInstance = -1;

/*Check if string is valid*/
if (!subNetMask








share|improve this question












share|improve this question




share|improve this question








edited Mar 20 at 14:53









rolfl♦

90.2k13186390




90.2k13186390









asked Mar 20 at 14:06









dear_tzvi

1485




1485







  • 3




    Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
    – Wingblade
    Mar 20 at 15:25










  • You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
    – Toby Speight
    Mar 20 at 16:06










  • Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
    – Toby Speight
    Mar 20 at 16:08










  • @wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
    – dear_tzvi
    Mar 20 at 18:18












  • 3




    Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
    – Wingblade
    Mar 20 at 15:25










  • You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
    – Toby Speight
    Mar 20 at 16:06










  • Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
    – Toby Speight
    Mar 20 at 16:08










  • @wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
    – dear_tzvi
    Mar 20 at 18:18







3




3




Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
– Wingblade
Mar 20 at 15:25




Your function considers any IPv4 addresses as valid, not just subnet masks. When visualized as 32-bit binary, a valid subnet mask has its n most significant bits set to 1, and all other bits to 0. Your function also accepts "holey" and therefore invalid subnet masks. See the RFC 1878 memo for more details, including a table of all 32 possible subnet masks.
– Wingblade
Mar 20 at 15:25












You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
– Toby Speight
Mar 20 at 16:06




You'll receive better reviews if you show a complete example. For example, I recommend that you edit to show the necessary #include lines, and a main() that shows how to call your function. It can really help reviewers if they are able to compile and run your program. You seem to be missing definitions for IN and is_numeric(), and possibly others - including them may help you get a more meaningful review.
– Toby Speight
Mar 20 at 16:06












Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
– Toby Speight
Mar 20 at 16:08




Ideally, you would provide a main() that includes your test suite of good and bad netmask strings.
– Toby Speight
Mar 20 at 16:08












@wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
– dear_tzvi
Mar 20 at 18:18




@wingblade - you are right about "holey". I have a different function checking if the ip is valid or not
– dear_tzvi
Mar 20 at 18:18










1 Answer
1






active

oldest

votes

















up vote
2
down vote













Missing includes



#include <stdbool.h>
#include <string.h>


Other test may be needed



@Wingblade



Non-standard function



is_numeric() is not standard C library nor did I find it in Linux.



Questionable signature



I'd expect a function that checks a string's validity to cope with a const char * subNetMask. So using strtok() on the supplied subNetMask is not possible. Code needs to make a copy or otherwise assess the string.



// isValidSubnetMask(IN char *subNetMask)
isValidSubnetMask(IN const char *subNetMask)


Weak functionality



Below are 7 test cases that failed OP's code. Sample working code provided. sscanf() is a bit ponderous, yet useful as a test competitor.



bool is_numeric() // Added undefined Op's missing function.
return true;


static bool isValidSubnetMask2(const char *subNetMask) subNetMask[n])
return false;

int i[4];
sscanf(subNetMask, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
return i[0] <= 255 && i[1] <= 255 && i[2] <= 255 && i[3] <= 255;


void test(bool expect, const char *s)
char buff[99];
strcpy(buff, s);
bool y = isValidSubnetMask(buff);
if (y != expect)
printf("Failed %d <%s>n", expect, s);

y = isValidSubnetMask2(s);
if (y != expect)
printf("Oops %d <%s>n", expect, s);



int main()
test(1, "255.255.255.0");
test(1, "255.255.255.123");
test(1, "0.0.0.0");
test(0, "455.255.255.0");
test(0, "255.255.255.-0");
test(0, "255.255.255. 0");
test(0, "2.2.5.2.3");
test(0, "2.2.3");
test(0, "+2.5.2.3");
test(0, "A.5.2.3");
test(0, "2.2.5.2.");
test(0, "2..2.5.2");
return 0;



Output



Failed 0 <255.255.255.-0>
Failed 0 <255.255.255. 0>
Failed 0 <2.2.5.2.3>
Failed 0 <2.2.3>
Failed 0 <+2.5.2.3>
Failed 0 <2.2.5.2.>
Failed 0 <2..2.5.2>





share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190034%2fa-function-that-checks-if-a-string-subnet-mask-is-valid%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Missing includes



    #include <stdbool.h>
    #include <string.h>


    Other test may be needed



    @Wingblade



    Non-standard function



    is_numeric() is not standard C library nor did I find it in Linux.



    Questionable signature



    I'd expect a function that checks a string's validity to cope with a const char * subNetMask. So using strtok() on the supplied subNetMask is not possible. Code needs to make a copy or otherwise assess the string.



    // isValidSubnetMask(IN char *subNetMask)
    isValidSubnetMask(IN const char *subNetMask)


    Weak functionality



    Below are 7 test cases that failed OP's code. Sample working code provided. sscanf() is a bit ponderous, yet useful as a test competitor.



    bool is_numeric() // Added undefined Op's missing function.
    return true;


    static bool isValidSubnetMask2(const char *subNetMask) subNetMask[n])
    return false;

    int i[4];
    sscanf(subNetMask, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
    return i[0] <= 255 && i[1] <= 255 && i[2] <= 255 && i[3] <= 255;


    void test(bool expect, const char *s)
    char buff[99];
    strcpy(buff, s);
    bool y = isValidSubnetMask(buff);
    if (y != expect)
    printf("Failed %d <%s>n", expect, s);

    y = isValidSubnetMask2(s);
    if (y != expect)
    printf("Oops %d <%s>n", expect, s);



    int main()
    test(1, "255.255.255.0");
    test(1, "255.255.255.123");
    test(1, "0.0.0.0");
    test(0, "455.255.255.0");
    test(0, "255.255.255.-0");
    test(0, "255.255.255. 0");
    test(0, "2.2.5.2.3");
    test(0, "2.2.3");
    test(0, "+2.5.2.3");
    test(0, "A.5.2.3");
    test(0, "2.2.5.2.");
    test(0, "2..2.5.2");
    return 0;



    Output



    Failed 0 <255.255.255.-0>
    Failed 0 <255.255.255. 0>
    Failed 0 <2.2.5.2.3>
    Failed 0 <2.2.3>
    Failed 0 <+2.5.2.3>
    Failed 0 <2.2.5.2.>
    Failed 0 <2..2.5.2>





    share|improve this answer



























      up vote
      2
      down vote













      Missing includes



      #include <stdbool.h>
      #include <string.h>


      Other test may be needed



      @Wingblade



      Non-standard function



      is_numeric() is not standard C library nor did I find it in Linux.



      Questionable signature



      I'd expect a function that checks a string's validity to cope with a const char * subNetMask. So using strtok() on the supplied subNetMask is not possible. Code needs to make a copy or otherwise assess the string.



      // isValidSubnetMask(IN char *subNetMask)
      isValidSubnetMask(IN const char *subNetMask)


      Weak functionality



      Below are 7 test cases that failed OP's code. Sample working code provided. sscanf() is a bit ponderous, yet useful as a test competitor.



      bool is_numeric() // Added undefined Op's missing function.
      return true;


      static bool isValidSubnetMask2(const char *subNetMask) subNetMask[n])
      return false;

      int i[4];
      sscanf(subNetMask, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
      return i[0] <= 255 && i[1] <= 255 && i[2] <= 255 && i[3] <= 255;


      void test(bool expect, const char *s)
      char buff[99];
      strcpy(buff, s);
      bool y = isValidSubnetMask(buff);
      if (y != expect)
      printf("Failed %d <%s>n", expect, s);

      y = isValidSubnetMask2(s);
      if (y != expect)
      printf("Oops %d <%s>n", expect, s);



      int main()
      test(1, "255.255.255.0");
      test(1, "255.255.255.123");
      test(1, "0.0.0.0");
      test(0, "455.255.255.0");
      test(0, "255.255.255.-0");
      test(0, "255.255.255. 0");
      test(0, "2.2.5.2.3");
      test(0, "2.2.3");
      test(0, "+2.5.2.3");
      test(0, "A.5.2.3");
      test(0, "2.2.5.2.");
      test(0, "2..2.5.2");
      return 0;



      Output



      Failed 0 <255.255.255.-0>
      Failed 0 <255.255.255. 0>
      Failed 0 <2.2.5.2.3>
      Failed 0 <2.2.3>
      Failed 0 <+2.5.2.3>
      Failed 0 <2.2.5.2.>
      Failed 0 <2..2.5.2>





      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        Missing includes



        #include <stdbool.h>
        #include <string.h>


        Other test may be needed



        @Wingblade



        Non-standard function



        is_numeric() is not standard C library nor did I find it in Linux.



        Questionable signature



        I'd expect a function that checks a string's validity to cope with a const char * subNetMask. So using strtok() on the supplied subNetMask is not possible. Code needs to make a copy or otherwise assess the string.



        // isValidSubnetMask(IN char *subNetMask)
        isValidSubnetMask(IN const char *subNetMask)


        Weak functionality



        Below are 7 test cases that failed OP's code. Sample working code provided. sscanf() is a bit ponderous, yet useful as a test competitor.



        bool is_numeric() // Added undefined Op's missing function.
        return true;


        static bool isValidSubnetMask2(const char *subNetMask) subNetMask[n])
        return false;

        int i[4];
        sscanf(subNetMask, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
        return i[0] <= 255 && i[1] <= 255 && i[2] <= 255 && i[3] <= 255;


        void test(bool expect, const char *s)
        char buff[99];
        strcpy(buff, s);
        bool y = isValidSubnetMask(buff);
        if (y != expect)
        printf("Failed %d <%s>n", expect, s);

        y = isValidSubnetMask2(s);
        if (y != expect)
        printf("Oops %d <%s>n", expect, s);



        int main()
        test(1, "255.255.255.0");
        test(1, "255.255.255.123");
        test(1, "0.0.0.0");
        test(0, "455.255.255.0");
        test(0, "255.255.255.-0");
        test(0, "255.255.255. 0");
        test(0, "2.2.5.2.3");
        test(0, "2.2.3");
        test(0, "+2.5.2.3");
        test(0, "A.5.2.3");
        test(0, "2.2.5.2.");
        test(0, "2..2.5.2");
        return 0;



        Output



        Failed 0 <255.255.255.-0>
        Failed 0 <255.255.255. 0>
        Failed 0 <2.2.5.2.3>
        Failed 0 <2.2.3>
        Failed 0 <+2.5.2.3>
        Failed 0 <2.2.5.2.>
        Failed 0 <2..2.5.2>





        share|improve this answer















        Missing includes



        #include <stdbool.h>
        #include <string.h>


        Other test may be needed



        @Wingblade



        Non-standard function



        is_numeric() is not standard C library nor did I find it in Linux.



        Questionable signature



        I'd expect a function that checks a string's validity to cope with a const char * subNetMask. So using strtok() on the supplied subNetMask is not possible. Code needs to make a copy or otherwise assess the string.



        // isValidSubnetMask(IN char *subNetMask)
        isValidSubnetMask(IN const char *subNetMask)


        Weak functionality



        Below are 7 test cases that failed OP's code. Sample working code provided. sscanf() is a bit ponderous, yet useful as a test competitor.



        bool is_numeric() // Added undefined Op's missing function.
        return true;


        static bool isValidSubnetMask2(const char *subNetMask) subNetMask[n])
        return false;

        int i[4];
        sscanf(subNetMask, "%d.%d.%d.%d", &i[0], &i[1], &i[2], &i[3]);
        return i[0] <= 255 && i[1] <= 255 && i[2] <= 255 && i[3] <= 255;


        void test(bool expect, const char *s)
        char buff[99];
        strcpy(buff, s);
        bool y = isValidSubnetMask(buff);
        if (y != expect)
        printf("Failed %d <%s>n", expect, s);

        y = isValidSubnetMask2(s);
        if (y != expect)
        printf("Oops %d <%s>n", expect, s);



        int main()
        test(1, "255.255.255.0");
        test(1, "255.255.255.123");
        test(1, "0.0.0.0");
        test(0, "455.255.255.0");
        test(0, "255.255.255.-0");
        test(0, "255.255.255. 0");
        test(0, "2.2.5.2.3");
        test(0, "2.2.3");
        test(0, "+2.5.2.3");
        test(0, "A.5.2.3");
        test(0, "2.2.5.2.");
        test(0, "2..2.5.2");
        return 0;



        Output



        Failed 0 <255.255.255.-0>
        Failed 0 <255.255.255. 0>
        Failed 0 <2.2.5.2.3>
        Failed 0 <2.2.3>
        Failed 0 <+2.5.2.3>
        Failed 0 <2.2.5.2.>
        Failed 0 <2..2.5.2>






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Mar 25 at 8:32


























        answered Mar 24 at 2:06









        chux

        11.4k11238




        11.4k11238






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190034%2fa-function-that-checks-if-a-string-subnet-mask-is-valid%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation