"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/inet_ntop.c" (19 Feb 2021, 1364 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "inet_ntop.c" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * private implementaiton if inet_ntop for systems that don't have it.
    3  * Functionally, correct, this version doesn't do condensing of IPv6 addresses,
    4  * and is kind of slow.
    5  * 
    6  * This is included if the OS does not have inet_ntop.
    7  *
    8  * PUBLIC DOMAIN.
    9  * Simson L. Garfinkel, Jan 20, 2013
   10  */
   11 
   12 static const char *inet_ntop4(const struct in_addr *addr, char *buf, socklen_t buflen)
   13 {
   14     const uint8_t *a = (uint8_t *)addr;
   15     snprintf(buf,buflen,"%03d.%03d.%03d.%03d", a[0], a[1], a[2], a[3]);
   16     return buf;
   17 }
   18 
   19 static const char *inet_ntop6(const struct private_in6_addr *addr, char *buf, socklen_t buflen)
   20 {
   21     const char *obuf=buf;
   22     const uint8_t *a = (uint8_t *)addr;
   23     for(size_t i=0;i<16;i++){
   24         if(buflen<2) return 0;        /* can't convert */
   25         snprintf(buf,buflen,"%02x",a[i]);
   26         buf+=2;
   27         buflen-=2;
   28         if(i>0 && i<15 && i%2==1){
   29             if(buflen<1) return 0;
   30             buf[0] = ':';
   31             buf++;
   32             buflen--;
   33         }
   34     }
   35     if(buflen<1) return 0;
   36     buf[0] = 0;
   37     return obuf;
   38 }
   39 
   40 const char *
   41 inet_ntop(int af, const void *addr, char *buf, socklen_t len)
   42 {
   43     switch(af){
   44     case AF_INET:
   45         return inet_ntop4((const struct in_addr *)addr, buf, len);
   46     case AF_INET6:
   47         return inet_ntop6((const struct private_in6_addr *)addr, buf, len);
   48     }
   49     return NULL;
   50 }