I use inet_pton to verify that the input IP address is correct and not all zeros (0.0.0.0 or 00.00.0.0).
inet_pton(int af, const char *src, void *dst)
If the ip (src) input address is 0.0.0.0 inet_pton, set dst to 0. If src is 00.00.00.00, dst is not 0, but I get a random value for each trail. Why inet_pton converts 0.00.00.00 to 0
#include <string.h> #include <arpa/inet.h> void main( int argc, char *argv[]) { int s; struct in_addr ipvalue; printf("converting %s to network address \n", argv[1]); s = inet_pton(AF_INET, argv[1], &ipvalue); if(s < 0) printf("inet_pton conversion error \n"); printf("converted value = %x \n", ipvalue.s_addr); }
Run Examples
Valid Values:
./a.out 10.1.2.3 converting 10.1.2.3 to network address converted value = 302010a
./a.out 0.0.0.0 converting 0.0.0.0 to network address converted value = 0
Incorrect results:
./a.out 00.00.00.0 converting 00.00.00.0 to network address converted value = **a58396a0**
./a.out 00.0.0.0 converting 00.0.0.0 to network address converted value = **919e2c30**
source share