r3139 - trunk/libmnetutil/source
erik at minisip.org
erik at minisip.org
Mon Jan 22 17:06:23 CET 2007
Author: erik
Date: 2007-01-22 17:06:22 +0100 (Mon, 22 Jan 2007)
New Revision: 3139
Modified:
trunk/libmnetutil/source/IP4Address.cxx
trunk/libmnetutil/source/IP6Address.cxx
Log:
* Make IPAddress constructors reentrant by moving
from gethostbyname2 to gethostbyname2_r
Modified: trunk/libmnetutil/source/IP4Address.cxx
===================================================================
--- trunk/libmnetutil/source/IP4Address.cxx 2007-01-22 13:45:17 UTC (rev 3138)
+++ trunk/libmnetutil/source/IP4Address.cxx 2007-01-22 16:06:22 UTC (rev 3139)
@@ -89,6 +89,7 @@
setAddressFamily(AF_INET);
setProtocolFamily(PF_INET);
struct in_addr ip_data;
+ char *auxbuf=NULL;
if (inet_aton(addr.c_str(),&ip_data)){
numIp = ntoh32(ip_data.s_addr);
}else{
@@ -96,7 +97,35 @@
//unsigned char *ip;
#ifndef WIN32
- struct hostent *hp= gethostbyname2(ipaddr.c_str(), AF_INET);
+//Non-reentrant version:
+// struct hostent *hp= gethostbyname2(ipaddr.c_str(), AF_INET);
+
+ struct hostent *hp;
+ struct hostent hbuf;
+ size_t buflen=1024;
+
+ auxbuf = (char*)malloc(1024);
+
+ int herr;
+ int res;
+ while ((res=gethostbyname2_r(ipaddr.c_str(),
+ AF_INET,
+ &hbuf,
+ auxbuf,
+ buflen,
+ &hp,
+ &herr
+ ))==ERANGE){
+ buflen*=2;
+ auxbuf=(char*)realloc(auxbuf,buflen);
+ }
+
+ if (res!=0 || hp==NULL){
+ free(auxbuf);
+ throw ResolvError( herr );
+ }
+
+
#else
struct hostent *hp= gethostbyname(ipaddr.c_str());
// struct hostent *hp= gethostbyaddr(ipaddr.c_str(), 4, AF_INET);
@@ -124,6 +153,9 @@
sockaddress->sin_family=AF_INET;
sockaddress->sin_addr.s_addr = hton32(numIp);
sockaddress->sin_port=0;
+
+ if (auxbuf)
+ free(auxbuf);
}
IP4Address::IP4Address(const IP4Address& other){
Modified: trunk/libmnetutil/source/IP6Address.cxx
===================================================================
--- trunk/libmnetutil/source/IP6Address.cxx 2007-01-22 13:45:17 UTC (rev 3138)
+++ trunk/libmnetutil/source/IP6Address.cxx 2007-01-22 16:06:22 UTC (rev 3139)
@@ -114,7 +114,32 @@
setAddressFamily(AF_INET6);
setProtocolFamily(PF_INET6);
#ifndef WIN32
- hostent *hp= gethostbyname2(addr.c_str(), AF_INET6);
+// hostent *hp= gethostbyname2(addr.c_str(), AF_INET6);
+ struct hostent *hp;
+ struct hostent hbuf;
+ size_t buflen=1024;
+
+ char *auxbuf = (char*)malloc(1024);
+
+ int herr;
+ int res;
+ while ((res=gethostbyname2_r(addr.c_str(),
+ AF_INET,
+ &hbuf,
+ auxbuf,
+ buflen,
+ &hp,
+ &herr
+ ))==ERANGE){
+ buflen*=2;
+ auxbuf=(char*)realloc(auxbuf,buflen);
+ }
+
+ if (res!=0 || hp==NULL){
+ free(auxbuf);
+ throw ResolvError( herr );
+ }
+
#else
hostent *hp= gethostbyname(addr.c_str());
#endif
@@ -137,6 +162,10 @@
memcpy(&sockaddress->sin6_addr,hp->h_addr, hp->h_length);
ipaddr = buildAddressString((struct sockaddr*)sockaddress, sizeof(struct sockaddr_in6));
+
+#ifndef WIN32
+ free(auxbuf);
+#endif
}
IP6Address::IP6Address(struct sockaddr_in6 * addr){
More information about the Minisip-devel
mailing list