Back to index

glibc  2.9
Defines | Functions
inet_net_ntop.c File Reference
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

Go to the source code of this file.

Defines

#define SPRINTF(x)   ((size_t)sprintf x)

Functions

static char * inet_net_ntop_ipv4 (const u_char *src, int bits, char *dst, size_t size) __THROW
char * inet_net_ntop (int af, const void *src, int bits, char *dst, size_t size)
static char * inet_net_ntop_ipv4 (u_char *src, int bits, char *dst, size_t size) const

Define Documentation

#define SPRINTF (   x)    ((size_t)sprintf x)

Definition at line 35 of file inet_net_ntop.c.


Function Documentation

char* inet_net_ntop ( int  af,
const void *  src,
int  bits,
char *  dst,
size_t  size 
)

Definition at line 52 of file inet_net_ntop.c.

{
       switch (af) {
       case AF_INET:
              return (inet_net_ntop_ipv4(src, bits, dst, size));
       default:
              __set_errno (EAFNOSUPPORT);
              return (NULL);
       }
}

Here is the call graph for this function:

static char* inet_net_ntop_ipv4 ( const u_char src,
int  bits,
char *  dst,
size_t  size 
) [static]

Here is the caller graph for this function:

static char* inet_net_ntop_ipv4 ( u_char src,
int  bits,
char *  dst,
size_t  size 
) const [static]

Definition at line 82 of file inet_net_ntop.c.

{
       char *odst = dst;
       char *t;
       u_int m;
       int b;

       if (bits < 0 || bits > 32) {
              __set_errno (EINVAL);
              return (NULL);
       }
       if (bits == 0) {
              if (size < sizeof "0")
                     goto emsgsize;
              *dst++ = '0';
              size--;
              *dst = '\0';
       }

       /* Format whole octets. */
       for (b = bits / 8; b > 0; b--) {
              if (size < sizeof "255.")
                     goto emsgsize;
              t = dst;
              dst += SPRINTF((dst, "%u", *src++));
              if (b > 1) {
                     *dst++ = '.';
                     *dst = '\0';
              }
              size -= (size_t)(dst - t);
       }

       /* Format partial octet. */
       b = bits % 8;
       if (b > 0) {
              if (size < sizeof ".255")
                     goto emsgsize;
              t = dst;
              if (dst != odst)
                     *dst++ = '.';
              m = ((1 << b) - 1) << (8 - b);
              dst += SPRINTF((dst, "%u", *src & m));
              size -= (size_t)(dst - t);
       }

       /* Format CIDR /width. */
       if (size < sizeof "/32")
              goto emsgsize;
       dst += SPRINTF((dst, "/%u", bits));
       return (odst);

 emsgsize:
       __set_errno (EMSGSIZE);
       return (NULL);
}