Thread: inet_ntop implementation incorrect some of the time

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266

    inet_ntop implementation incorrect some of the time

    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;
    }

  2. #2
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    The address pointed to by src should already be in network byte order. No conversion is necessary.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The reason I did this is because this function does not exist on Windows...
    Yes inet_ntop() doesn't exist for Windows, but InetNtop() is the Windows equivalent.

    Jim

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Syscal View Post
    appears to work for the majority of the calls but there are some where I believe it's returning an incorrect IP address...
    If you provide examples of which inputs it's wrong for - and the expected vs. actual output - it'd be a lot easier to troubleshoot...
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. raw time program time incorrect
    By ratmo in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2012, 05:16 AM
  2. compiler warning with sprintf and inet_ntop
    By qualia in forum C Programming
    Replies: 2
    Last Post: 08-19-2010, 03:42 AM
  3. Every time I use %d I get an incorrect number
    By Demipimp in forum C Programming
    Replies: 3
    Last Post: 12-04-2008, 12:36 AM
  4. inet_ntop reentrant?
    By stevfletchcom in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-17-2008, 09:58 AM
  5. Does C++ have any standard implementation for time/date?
    By indigo0086 in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2007, 01:05 PM