From 9fb2ddb0bedad21e0080d7e18d6917b8555c0f1b Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Fri, 14 Mar 2008 16:23:16 +0200 Subject: [PATCH] Added silc_net_bit2addr, silc_htonl, silc_ntohl, silc_htons, silc_ntohs --- lib/silcutil/silcnet.c | 80 ++++++++++++++++++++++++++++++++++++++++++ lib/silcutil/silcnet.h | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/lib/silcutil/silcnet.c b/lib/silcutil/silcnet.c index 5c6dadb0..47ca9fce 100644 --- a/lib/silcutil/silcnet.c +++ b/lib/silcutil/silcnet.c @@ -630,3 +630,83 @@ char *silc_net_localip(void) return silc_strdup(ip_addr); } + +/* Convert network byte order IP to string */ + +SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len, + char *addr, SilcUInt32 addr_size) +{ + if (!addr || !addr_size) + return TRUE; + + if (bin_len == 16) { +#ifdef HAVE_IPV6 + struct sockaddr_in6 ipv6; + memset(&ipv6, 0, sizeof(ipv6)); + ipv6.sin6_family = AF_INET6; + memcpy(&ipv6.sin6_addr, bin, sizeof(ipv6.sin6_addr)); + if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6), + addr, addr_size, NULL, 0, NI_NUMERICHOST)) + return TRUE; +#else + return FALSE; +#endif /* HAVE_IPV6 */ + } else if (bin_len == 4) { + struct in_addr ipv4; + char *a; + + memcpy(&ipv4.s_addr, bin, 4); + a = inet_ntoa(ipv4); + if (!a) + return FALSE; + + silc_snprintf(addr, addr_size, a); + return TRUE; + } + + return FALSE; +} + +/* Host to network byte order */ + +SilcUInt32 silc_htonl(SilcUInt32 host) +{ +#ifdef WORDS_BIGENDIAN + return host; +#else + return SILC_SWAB_32(host); +#endif /* WORDS_BIGENDIAN */ +} + +/* Network to host byte order */ + +SilcUInt32 silc_ntohl(SilcUInt32 net) +{ +#ifdef WORDS_BIGENDIAN + return net; +#else + return SILC_SWAB_32(net); +#endif /* WORDS_BIGENDIAN */ +} + +/* Host to network byte order */ + +SilcUInt16 silc_htons(SilcUInt16 host) +{ +#ifdef WORDS_BIGENDIAN + return net; +#else + return SILC_SWAB_16(host); +#endif /* WORDS_BIGENDIAN */ +} + +/* Network to host byte order */ + +SilcUInt16 silc_ntohs(SilcUInt16 net) +{ +#ifdef WORDS_BIGENDIAN + return net; +#else + return SILC_SWAB_16(net); +#endif /* WORDS_BIGENDIAN */ +} diff --git a/lib/silcutil/silcnet.h b/lib/silcutil/silcnet.h index 7172338d..e7edcda4 100644 --- a/lib/silcutil/silcnet.h +++ b/lib/silcutil/silcnet.h @@ -501,6 +501,22 @@ SilcBool silc_net_is_ip(const char *addr); ***/ SilcBool silc_net_addr2bin(const char *addr, void *bin, SilcUInt32 bin_len); +/****f* silcutil/silc_net_bin2addr + * + * SYNOPSIS + * + * SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len, + * char *addr, SilcUInt32 addr_size); + * + * DESCRIPTION + * + * Converts network byte ordered IP address into a numbers-and-dots + * string notation. The `bin' address can be either IPv4 or IPv6 address. + * + ***/ +SilcBool silc_net_bin2addr(const void *bin, SilcUInt32 bin_len, + char *addr, SilcUInt32 addr_size); + /****f* silcutil/SilcNetResolveCallback * * SYNOPSIS @@ -695,6 +711,58 @@ char *silc_net_localhost(void); ***/ char *silc_net_localip(void); +/****f* silcutil/silc_htonl + * + * SYNOPSIS + * + * SilcUInt32 silc_htonl(SilcUInt32 host); + * + * DESCRIPTION + * + * Converts integer `host' from host byte order to network byte order. + * + ***/ +SilcUInt32 silc_htonl(SilcUInt32 host); + +/****f* silcutil/silc_ntohl + * + * SYNOPSIS + * + * SilcUInt32 silc_ntohl(SilcUInt32 net); + * + * DESCRIPTION + * + * Converts integer `net' from network byte order to host byte order. + * + ***/ +SilcUInt32 silc_ntohl(SilcUInt32 net); + +/****f* silcutil/silc_htonl + * + * SYNOPSIS + * + * SilcUInt16 silc_htons(SilcUInt16 host); + * + * DESCRIPTION + * + * Converts integer `host' from host byte order to network byte order. + * + ***/ +SilcUInt16 silc_htons(SilcUInt16 host); + +/****f* silcutil/silc_ntohl + * + * SYNOPSIS + * + * SilcUInt16 silc_ntohs(SilcUInt16 net); + * + * DESCRIPTION + * + * Converts integer `net' from network byte order to host byte order. + * + ***/ +SilcUInt16 silc_ntohs(SilcUInt16 net); + #include "silcnet_i.h" #endif /* SILCNET_H */ -- 2.24.0