gethostbyname -> getaddrinfo
Prefer reentrant DNS API
This commit is contained in:
committed by
Andrew Johnson
parent
ff1fb82ef2
commit
5ae9cc6536
@@ -41,6 +41,13 @@
|
||||
# define SOCK_CLOEXEC (0)
|
||||
#endif
|
||||
|
||||
#if defined(AI_PASSIVE) && !defined(__rtems__)
|
||||
# define USE_INFO
|
||||
#else
|
||||
# define USE_BY
|
||||
#endif
|
||||
|
||||
#ifdef USE_BY
|
||||
/*
|
||||
* Protect some routines which are not thread-safe
|
||||
*/
|
||||
@@ -60,7 +67,7 @@ static void unlockInfo (void)
|
||||
{
|
||||
epicsMutexUnlock (infoMutex);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
static size_t nAttached;
|
||||
|
||||
@@ -164,6 +171,7 @@ LIBCOM_API void epicsStdCall epicsSocketDestroy ( SOCKET s )
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_BY
|
||||
/*
|
||||
* ipAddrToHostName
|
||||
* On many systems, gethostbyaddr must be protected by a
|
||||
@@ -214,6 +222,53 @@ LIBCOM_API int epicsStdCall hostToIPAddr
|
||||
unlockInfo ();
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_INFO
|
||||
|
||||
unsigned epicsStdCall ipAddrToHostName(const struct in_addr *pAddr, char *pBuf, unsigned bufSize)
|
||||
{
|
||||
osiSockAddr query;
|
||||
|
||||
if(!bufSize)
|
||||
return 0; // non-sense
|
||||
|
||||
memset(&query, 0, sizeof(query));
|
||||
query.ia.sin_family = AF_INET;
|
||||
query.ia.sin_addr = *pAddr;
|
||||
|
||||
int ret = getnameinfo(&query.sa, sizeof(query), pBuf, bufSize, NULL, 0, NI_NAMEREQD);
|
||||
if(ret==0) {
|
||||
ret = strlen (pBuf);
|
||||
|
||||
} else { // lookup fails
|
||||
ret = 0; // indicate failure to caller
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int epicsStdCall hostToIPAddr(const char *pHostName, struct in_addr *pIPA)
|
||||
{
|
||||
struct addrinfo hint, *result = NULL;
|
||||
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
hint.ai_family = AF_INET;
|
||||
|
||||
int ret = getaddrinfo(pHostName, NULL, &hint, &result);
|
||||
if(ret==0) {
|
||||
const struct addrinfo *ai;
|
||||
ret = -1;
|
||||
for(ai = result; ai; ai = ai->ai_next) {
|
||||
assert(ai->ai_family==AF_INET); // ensured by hint
|
||||
const struct sockaddr_in *answer = (const struct sockaddr_in*)ai->ai_addr;
|
||||
*pIPA = answer->sin_addr;
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(result) {
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user