Module: sip-router
Branch: master
Commit: fabaa667795e5162fe17afd3e4a073bc99057144
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=fabaa66…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: Mon Jul 18 22:34:47 2011 +0200
core: new function to get IP address from su structure
- suip2a(...) returns zero-terminated string representation of IPv4/IPv6
address
---
ip_addr.h | 30 +++++++++++++++++++++++++++++-
1 files changed, 29 insertions(+), 1 deletions(-)
diff --git a/ip_addr.h b/ip_addr.h
index 2f04693..d9cbf02 100644
--- a/ip_addr.h
+++ b/ip_addr.h
@@ -722,7 +722,7 @@ static inline char* su2a(union sockaddr_union* su, int su_len)
{
static char buf[SU2A_MAX_STR_SIZE];
int offs;
-
+
#ifdef USE_IPV6
if (unlikely(su->s.sa_family==AF_INET6)){
if (unlikely(su_len<sizeof(su->sin6)))
@@ -744,6 +744,34 @@ static inline char* su2a(union sockaddr_union* su, int su_len)
return buf;
}
+#define SUIP2A_MAX_STR_SIZE (IP6_MAX_STR_SIZE + 2 /* [] */ + 1 /* \0 */)
+/* returns an asciiz string containing the ip
+ * (<ipv4_addr> or [<ipv6_addr>])
+ */
+static inline char* suip2a(union sockaddr_union* su, int su_len)
+{
+ static char buf[SUIP2A_MAX_STR_SIZE];
+ int offs;
+
+#ifdef USE_IPV6
+ if (unlikely(su->s.sa_family==AF_INET6)){
+ if (unlikely(su_len<sizeof(su->sin6)))
+ return "<addr. error>";
+ buf[0]='[';
+ offs=1+ip6tosbuf((unsigned char*)su->sin6.sin6_addr.s6_addr, &buf[1],
+ sizeof(buf)-4);
+ buf[offs]=']';
+ offs++;
+ }else
+#endif /* USE_IPV6*/
+ if (unlikely(su_len<sizeof(su->sin)))
+ return "<addr. error>";
+ else
+ offs=ip4tosbuf((unsigned char*)&su->sin.sin_addr, buf, sizeof(buf)-2);
+ buf[offs]=0;
+ return buf;
+}
+
/* converts an ip_addr structure to a hostent, returns pointer to internal