Search code examples
cnetwork-programmingnetmask

Check is netmask correct


Hello i have such netmask, f.e. 255.128.0.0, it is correct netmask, i need to check it.

int s; 
struct in_addr ipvalue;
s = inet_pton(AF_INET, argv[1], &ipvalue);

and if i print s i see 33023 witch in binary form is 00000000.00000000.10000000.11111111, but this is not equals to my input netmask, so how i can check is my netmask is correct or not? Now i am cheking it by comparing netmask in decimal form and for (int i = 31; i >=0; i--) sm |= (1 << i); Thank you.


Solution

  • if i print s i see 33023 witch in binary form is 00000000.00000000.10000000.11111111

    Your problem looks like an endianness issue.

    Try the following:

    int s; 
    struct in_addr ipvalue;
    s = inet_pton(AF_INET, argv[1], &ipvalue);
    s = ntohl(s);
    

    That should fix the byte order issue.