--- /home/cupotka/radiusclient/lib/ip_util.c 2007-07-11 20:29:29.000000000 +0300 +++ lib/ip_util.c 2010-03-08 19:48:23.058806817 +0200 @@ -18,12 +18,18 @@ #include #include +#define HOSTBUF_SIZE 1024 + #if !defined(SA_LEN) #define SA_LEN(sa) \ (((sa)->sa_family == AF_INET) ? \ sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6)) #endif + +static __thread size_t hostbuflen=HOSTBUF_SIZE; +static __thread char *tmphostbuf=NULL; + /* * Function: rc_gethostbyname * @@ -38,13 +44,10 @@ #ifdef GETHOSTBYNAME_R #if defined (GETHOSTBYNAMERSTYLE_SYSV) || defined (GETHOSTBYNAMERSTYLE_GNU) struct hostent hostbuf; - size_t hostbuflen; - char *tmphostbuf; int res; int herr; - hostbuflen = 1024; - tmphostbuf = malloc(hostbuflen); + if(!tmphostbuf) tmphostbuf = realloc(tmphostbuf,hostbuflen); #endif #endif @@ -56,10 +59,9 @@ hostbuflen *= 2; tmphostbuf = realloc(tmphostbuf, hostbuflen); } - free(tmphostbuf); + if(res) return NULL; #elif defined (GETHOSTBYNAMERSTYLE_SYSV) hp = gethostbyname_r(hostname, &hostbuf, tmphostbuf, hostbuflen, &herr); - free(tmphostbuf); #else hp = gethostbyname(hostname); #endif @@ -87,13 +89,10 @@ #ifdef GETHOSTBYADDR_R #if defined (GETHOSTBYADDRRSTYLE_SYSV) || defined (GETHOSTBYADDRRSTYLE_GNU) struct hostent hostbuf; - size_t hostbuflen; - char *tmphostbuf; int res; int herr; - hostbuflen = 1024; - tmphostbuf = malloc(hostbuflen); + if(!tmphostbuf) tmphostbuf = realloc(tmphostbuf,hostbuflen); #endif #endif @@ -106,10 +105,9 @@ hostbuflen *= 2; tmphostbuf = realloc(tmphostbuf, hostbuflen); } - free(tmphostbuf); + if(res) return NULL; #elif GETHOSTBYADDRSTYLE_SYSV hp = gethostbyaddr_r(addr, length, format, &hostbuf, tmphostbuf, hostbuflen, &herr); - free(tmphostbuf); #else hp = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET); #endif