I have written a simple inet_ntop function which appears to work for the majority of the calls but there are some where I believe it's returning an incorrect IP address...
I have accounted for endianness. What am I doing wrong? The reason I did this is because this function does not exist on Windows...
Code:
#include "netstats.h"
int inet_ntop(int family, const void * src, char * dst, size_t size)
{
switch(family)
{
case AF_INET:
#ifdef BIGENDIAN
snprintf(dst, size, "%d.%d.%d.%d",
((* (int *)src) >> 0) & 0x000000ff,
((* (int *)src) >> 8) & 0x000000ff,
((* (int *)src) >> 16) & 0x000000ff,
((* (int *)src) >> 24) & 0x000000ff
);
#else
snprintf(dst, size, "%d.%d.%d.%d",
((* (int *)src) >> 24) & 0x000000ff,
((* (int *)src) >> 16) & 0x000000ff,
((* (int *)src) >> 8) & 0x000000ff,
((* (int *)src) >> 0) & 0x000000ff
);
#endif
return 1;
break;
case AF_INET6:
fprintf(stderr, "inet_ntop: IPv6 is not supported\n");
return 0;
break;
default:
fprintf(stderr, "Invalid address family\n");
return 0;
break;
}
return 0;
}