bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gnulib] inet_r, xinet


From: Bruno Haible
Subject: Re: [Bug-gnulib] inet_r, xinet
Date: Fri, 17 Sep 2004 13:59:16 +0200
User-agent: KMail/1.5

Simon Josefsson wrote:
> +char *inet_ntoa_r (struct in_addr in, char *buf);
> +char *xinet_ntoa (struct in_addr in);

Hi,

I don't think we should, at this time, introduce new API that works only
for IPv4 and doesn't work for other protocols like IPv6.

Can you rework this in such a way that the API becomes protocol independent
(like the gethostbyaddr function is) and that the implementation supports
IPv6?

You can use the appended autoconf macros for detecting IPv4 and IPv6 support.
Maybe you can also use the appended code from GNU clisp.

Bruno


AC_MSG_CHECKING(for IPv4 sockets)
AC_CACHE_VAL(gl_cv_socket_ipv4,
  [AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>],
[int x = AF_INET; struct in_addr y; struct sockaddr_in z;],
     gl_cv_socket_ipv4=yes, gl_cv_socket_ipv4=no)])
AC_MSG_RESULT($gl_cv_socket_ipv4)
if test $gl_cv_socket_ipv4 = yes; then
  AC_DEFINE(HAVE_IPV4, 1, [<sys/socket.h> defines AF_INET])
fi

AC_MSG_CHECKING(for IPv6 sockets)
AC_CACHE_VAL(gl_cv_socket_ipv6,
  [AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>],
[int x = AF_INET6; struct in6_addr y; struct sockaddr_in6 z;],
     gl_cv_socket_ipv6=yes, gl_cv_socket_ipv6=no)])
AC_MSG_RESULT($gl_cv_socket_ipv6)
if test $gl_cv_socket_ipv6 = yes; then
  AC_DEFINE(HAVE_IPV6, 1, [<sys/socket.h> defines AF_INET6])
fi



/* Converts an AF_INET address to a printable, presentable format.
 ipv4_ntop(buffer,addr);
 > struct in_addr addr: IPv4 address
 < char[] buffer: printable address
 buffer should have at least 15+1 characters. */
#if HAVE_IPV4
  #if HAVE_INET_NTOP
    #define ipv4_ntop(buffer,addr) \
      inet_ntop (AF_INET, &addr, buffer, 15+1)
  #else
    #define ipv4_ntop(buffer,addr) \
      strcpy (buffer, inet_ntoa (addr))
  #endif
#endif

/* Converts an AF_INET6 address to a printable, presentable format.
 ipv6_ntop(buffer,addr);
 > struct in_addr6 addr: IPv6 address
 < char[] buffer: printable address
 buffer should have at least 45+1 characters. */
#if HAVE_IPV6
  #ifndef s6_addr16
    #define s6_addr16 in6_u.u6_addr16
  #endif
  #if defined(_WIN32) || defined(__CYGWIN32__)
    #define S6_ADDR16(addr)   ((u_short*)((addr).s6_addr))
  #else
    #define S6_ADDR16(addr)   (addr).s6_addr16
  #endif
  #if HAVE_INET_NTOP
    #define ipv6_ntop(buffer,addr) \
      inet_ntop (AF_INET6, &addr, buffer, 45+1)
  #else
    #define ipv6_ntop(buffer,addr) \
     (sprintf (buffer, "%x:%x:%x:%x:%x:%x:%x:%x",       \
               ntohs (S6_ADDR16(addr)[0]),              \
               ntohs (S6_ADDR16(addr)[1]),              \
               ntohs (S6_ADDR16(addr)[2]),              \
               ntohs (S6_ADDR16(addr)[3]),              \
               ntohs (S6_ADDR16(addr)[4]),              \
               ntohs (S6_ADDR16(addr)[5]),              \
               ntohs (S6_ADDR16(addr)[6]),              \
               ntohs (S6_ADDR16(addr)[7])), buffer)
  #endif
#endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]