I am declaring following variables
unsigned long dstAddr;
unsigned long gateWay;
unsigned long mask;
These variables contains ipaddresses in network byte order. So when I am trying to print the dot notation using inet_ntoa function for mask variable sometimes it is printing strange values. The below code is in a while loop .. which loops for n times.
printf("%s\t%s\t%s\t",inet_ntoa(dstAddr),inet_ntoa(gateWay),inet_ntoa(mask));
192.168.122.0 0.0.0.0 0.255.255.255
but it should be
192.168.122.0 0.0.0.0 255.255.255.0
I printed the HEX values of the variables and it shows ..
007aa8c0 00000000 ffffff00
So is this because of inet_ntoa ??
Actually I am trying to get the values of the declared variables from 254 routing table in kernel via NETLINKS. I guess I should still use inet_ntoa function to convert the value into dot notation .. ??
The only thing that makes sense is that your assumption regarding all the addresses being in network byte order is incorrect.
Well, given that it works for your non-mask values (including the first which also has the high bit set), I'd be looking at what mask actually contains.
What is it when you print it out as a normal unsigned long? My bet is that mask is actually not the correct value:
printf ("%08x\t%08x\t%08x\n", dstAddr, gateWay, mask);
(assuming you have four-byte longs).
For example, this little program (compiled under Cygwin):
#include <stdio.h>
int main (void) {
unsigned long dstAddr, gateWay, mask;
dstAddr = 0x007aa8c0;
gateWay = 0x00000000;
mask = 0x00ffffff;
printf("%-15s %-15s %-15s\n",
inet_ntoa (dstAddr),
inet_ntoa (gateWay),
inet_ntoa (mask));
printf("%-15s ", inet_ntoa (dstAddr));
printf("%-15s ", inet_ntoa (gateWay));
printf("%-15s\n", inet_ntoa (mask));
printf ("%08x%8s%08x%8s%08x\n",
dstAddr, "",
gateWay, "",
mask);
return 0;
}
outputs:
192.168.122.0 192.168.122.0 192.168.122.0
192.168.122.0 0.0.0.0 255.255.255.0
007aa8c0 00000000 00ffffff
Note that I had to separate my calls to inet_ntoa as it appears to use a static buffer. When I was doing it all within a single printf, it overwrote the contents of that buffer before any of them were printed, hence I only got the last one processed. I don't think that's happening in your case since you're getting different values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With