Browse code

core: new function to get IP address from su structure

- suip2a(...) returns zero-terminated string representation of IPv4/IPv6
address

Daniel-Constantin Mierla authored on 18/07/2011 20:34:47
Showing 1 changed files
... ...
@@ -722,7 +722,7 @@ static inline char* su2a(union sockaddr_union* su, int su_len)
722 722
 {
723 723
 	static char buf[SU2A_MAX_STR_SIZE];
724 724
 	int offs;
725
-	
725
+
726 726
 #ifdef USE_IPV6
727 727
 	if (unlikely(su->s.sa_family==AF_INET6)){
728 728
 		if (unlikely(su_len<sizeof(su->sin6)))
... ...
@@ -744,6 +744,34 @@ static inline char* su2a(union sockaddr_union* su, int su_len)
744 744
 	return buf;
745 745
 }
746 746
 
747
+#define SUIP2A_MAX_STR_SIZE  (IP6_MAX_STR_SIZE + 2 /* [] */ + 1 /* \0 */)
748
+/* returns an asciiz string containing the ip
749
+ *  (<ipv4_addr> or [<ipv6_addr>])
750
+ */
751
+static inline char* suip2a(union sockaddr_union* su, int su_len)
752
+{
753
+	static char buf[SUIP2A_MAX_STR_SIZE];
754
+	int offs;
755
+
756
+#ifdef USE_IPV6
757
+	if (unlikely(su->s.sa_family==AF_INET6)){
758
+		if (unlikely(su_len<sizeof(su->sin6)))
759
+			return "<addr. error>";
760
+		buf[0]='[';
761
+		offs=1+ip6tosbuf((unsigned char*)su->sin6.sin6_addr.s6_addr, &buf[1],
762
+							sizeof(buf)-4);
763
+		buf[offs]=']';
764
+		offs++;
765
+	}else
766
+#endif /* USE_IPV6*/
767
+	if (unlikely(su_len<sizeof(su->sin)))
768
+		return "<addr. error>";
769
+	else
770
+		offs=ip4tosbuf((unsigned char*)&su->sin.sin_addr, buf, sizeof(buf)-2);
771
+	buf[offs]=0;
772
+	return buf;
773
+}
774
+
747 775
 
748 776
 
749 777
 /* converts an ip_addr structure to a hostent, returns pointer to internal