r2909 - in trunk/libmnetutil: . include/libmnetutil source

mikma at minisip.org mikma at minisip.org
Thu Nov 16 21:22:51 CET 2006


Author: mikma
Date: 2006-11-16 21:22:50 +0100 (Thu, 16 Nov 2006)
New Revision: 2909

Modified:
   trunk/libmnetutil/configure.ac
   trunk/libmnetutil/include/libmnetutil/IP6Address.h
   trunk/libmnetutil/source/IP6Address.cxx
Log:
IP6Address:
* operator<<, declare address argument const.
* Convert sockaddr_in6 to a string using getnameinfo.
* Fix IP6Address::IP6Address(string addr), zero the whole sockaddress
  not only the first 4 or 8 bytes.
* Fix copy constructor. Copy the whole num_ip not only the first 4 or 8
  bytes.
* Remove unnecessary call to gethostbyname in connect, address is
  already resolved in constructor.


Modified: trunk/libmnetutil/configure.ac
===================================================================
--- trunk/libmnetutil/configure.ac	2006-11-16 17:44:34 UTC (rev 2908)
+++ trunk/libmnetutil/configure.ac	2006-11-16 20:22:50 UTC (rev 2909)
@@ -24,7 +24,7 @@
 dnl networking headers
 AC_CHECK_HEADERS([netinet/in.h ws2tcpip.h])
 AC_CHECK_HEADERS([ifaddrs.h netdb.h sys/socket.h])
-AC_CHECK_FUNCS([getifaddrs])
+AC_CHECK_FUNCS([getifaddrs getnameinfo])
 AM_MINISIP_CHECK_IPV6
 
 AC_ARG_ENABLE(srp,

Modified: trunk/libmnetutil/include/libmnetutil/IP6Address.h
===================================================================
--- trunk/libmnetutil/include/libmnetutil/IP6Address.h	2006-11-16 17:44:34 UTC (rev 2908)
+++ trunk/libmnetutil/include/libmnetutil/IP6Address.h	2006-11-16 20:22:50 UTC (rev 2909)
@@ -42,7 +42,7 @@
 		virtual int32_t getPort() const;
 		virtual std::string getString() const;
 		virtual void connect(Socket &socket, int32_t port);
-		friend std::ostream& operator<<(std::ostream&, IP6Address &a);
+		friend std::ostream& operator<<(std::ostream&, const IP6Address &a);
 
 		virtual struct sockaddr *getSockaddrptr(int32_t port=0) const;
 		virtual int32_t getSockaddrLength() const;

Modified: trunk/libmnetutil/source/IP6Address.cxx
===================================================================
--- trunk/libmnetutil/source/IP6Address.cxx	2006-11-16 17:44:34 UTC (rev 2908)
+++ trunk/libmnetutil/source/IP6Address.cxx	2006-11-16 20:22:50 UTC (rev 2909)
@@ -52,6 +52,26 @@
 
 using namespace std;
 
+#ifdef HAVE_GETNAMEINFO
+string buildAddressString(const struct sockaddr *sa, socklen_t salen)
+{
+	char buf[INET6_ADDRSTRLEN+1] = "";
+
+	memset(buf, 0, sizeof(buf));
+	int res = getnameinfo( sa, salen, buf, sizeof(buf),
+			       NULL, 0, NI_NUMERICHOST );
+
+	if( res < 0 ){
+		throw ResolvError( res );
+	}
+	else {
+		return buf;
+	}
+}
+#else
+#error getnameinfo required when enabling ipv6
+#endif	// HAVE_GETNAMEINFO
+
 /*
 **
 ** Alg:	1. Convert address to binary format
@@ -61,20 +81,19 @@
 IP6Address::IP6Address(string addr){
 	sockaddress = new sockaddr_in6;
 	type = IP_ADDRESS_TYPE_V6;
-	ipaddr = addr;
 
 	setAddressFamily(AF_INET6);
 	setProtocolFamily(PF_INET6);
 #ifndef WIN32
-	hostent *hp= gethostbyname2(ipaddr.c_str(), AF_INET6);	
+	hostent *hp= gethostbyname2(addr.c_str(), AF_INET6);	
 #else
-	hostent *hp= gethostbyname(ipaddr.c_str());	
+	hostent *hp= gethostbyname(addr.c_str());	
 #endif
 	if (!hp){ //throw host not found exception here
 #ifdef DEBUG_OUTPUT
-		cerr << "ERROR:(in IP6Address) Unknown host: <" << ipaddr.c_str() <<">"<< endl;
+		cerr << "ERROR:(in IP6Address) Unknown host: <" << addr.c_str() <<">"<< endl;
 #endif
-		throw HostNotFound( ipaddr );
+		throw HostNotFound( addr );
 
 	}
 	unsigned short *ip = (unsigned short *)hp->h_addr;
@@ -82,11 +101,13 @@
 		num_ip[i] = ip[i];
 
 	//bzero((char*)&sockaddress, sizeof(sockaddress));
-	memset(sockaddress, '\0', sizeof(sockaddress));
+	memset(sockaddress, '\0', sizeof(*sockaddress));
 	sockaddress->sin6_family=PF_INET6;
 	sockaddress->sin6_port=0;
 	//bcopy(hp->h_addr,(char *)&sockaddress.sin6_addr, hp->h_length);
 	memcpy(&sockaddress->sin6_addr,hp->h_addr, hp->h_length);
+
+ 	ipaddr = buildAddressString((struct sockaddr*)sockaddress, sizeof(struct sockaddr_in6));
 }
 
 IP6Address::IP6Address(struct sockaddr_in6 * addr){
@@ -111,6 +132,7 @@
 #endif
 #endif
 
+	ipaddr = buildAddressString((struct sockaddr*)addr, sizeof(struct sockaddr_in6));
 }
 
 
@@ -119,7 +141,7 @@
 	setAddressFamily(AF_INET6);
 	setProtocolFamily(PF_INET6);
 	ipaddr = other.ipaddr;
-	memcpy( num_ip, other.num_ip, 8 );
+	memcpy( num_ip, other.num_ip, sizeof(num_ip) );
 	sockaddress = new sockaddr_in6;
 	memcpy(sockaddress, other.sockaddress, sizeof(sockaddr_in6));
 }
@@ -148,27 +170,10 @@
 }
 
 void IP6Address::connect(Socket &socket, int32_t port){
-#ifndef WIN32
-	struct hostent *hp = gethostbyname2(ipaddr.c_str(), AF_INET6);
-#else
-	struct hostent *hp = gethostbyname(ipaddr.c_str());
-#endif
-	if (!hp){ //throw host not found exception here
-#ifdef DEBUG_OUTPUT
-		cerr << "ERROR:(in IP6Address::connect) Unknown host: " << ipaddr << endl;
-#endif
-
-		throw HostNotFound( ipaddr );
-	}
-	
 	struct sockaddr_in6 sin;
-	//bzero((char*)&sin, sizeof(sin));
-	memset(&sin,'\0', sizeof(sin));
-	sin.sin6_family = AF_INET6;
-	//bcopy(hp->h_addr, (char *)&sin.sin6_addr, hp->h_length);
-	memcpy( &sin.sin6_addr,hp->h_addr, hp->h_length);
+	memcpy(&sin, sockaddress, sizeof(sin));
 	sin.sin6_port = htons( (unsigned short)port );
-	
+
 	if (::connect(socket.getFd(), (struct sockaddr *)&sin, sizeof(sin)) < 0){
 		merror("(in IP6Address::connect()): connect");
 		socket.close();
@@ -177,7 +182,7 @@
 
 }
 
-ostream& operator<<(ostream& out, IP6Address &a){
+ostream& operator<<(ostream& out, const IP6Address &a){
 	out << a.ipaddr;
 	
 	unsigned short *ip = (unsigned short*)&a.num_ip;



More information about the Minisip-devel mailing list