added ipAddrToHostName
This commit is contained in:
@@ -55,48 +55,24 @@ void bsdSockRelease()
|
||||
}
|
||||
|
||||
/*
|
||||
* ipAddrToA()
|
||||
* (convert IP address to ASCII host name)
|
||||
* ipAddrToHostName
|
||||
*/
|
||||
void epicsShareAPI ipAddrToA
|
||||
(const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize)
|
||||
epicsShareFunc unsigned epicsShareAPI ipAddrToHostName
|
||||
(const struct in_addr *pAddr, char *pBuf, unsigned bufSize)
|
||||
{
|
||||
char *pString;
|
||||
struct hostent *ent;
|
||||
# define maxPortDigits 5u
|
||||
|
||||
if (bufSize<1) {
|
||||
return;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (paddr->sin_family!=AF_INET) {
|
||||
strncpy(pBuf, "<Ukn Addr Type>", bufSize-1);
|
||||
/*
|
||||
* force null termination
|
||||
*/
|
||||
pBuf[bufSize-1] = '\0';
|
||||
}
|
||||
else {
|
||||
ent = gethostbyaddr((char *) &paddr->sin_addr,
|
||||
sizeof(paddr->sin_addr), AF_INET);
|
||||
if(ent){
|
||||
pString = ent->h_name;
|
||||
}
|
||||
else{
|
||||
pString = inet_ntoa (paddr->sin_addr);
|
||||
}
|
||||
|
||||
/*
|
||||
* allow space for the port number
|
||||
*/
|
||||
if (bufSize>maxPortDigits+strlen(pString)) {
|
||||
sprintf (pBuf, "%.*s:%hu", (int) (bufSize-maxPortDigits-1),
|
||||
pString, ntohs(paddr->sin_port));
|
||||
}
|
||||
else {
|
||||
sprintf (pBuf, "%.*s", (int) (bufSize-1), pString);
|
||||
}
|
||||
ent = gethostbyaddr((char *) pAddr, sizeof (*pAddr), AF_INET);
|
||||
if (ent) {
|
||||
strncpy (pBuf, ent->h_name, bufSize);
|
||||
pBuf[bufSize-1] = '\0';
|
||||
return strlen (pBuf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -21,13 +21,54 @@ void bsdSockRelease()
|
||||
{
|
||||
}
|
||||
|
||||
/*
|
||||
* ipAddrToHostName
|
||||
*/
|
||||
epicsShareFunc unsigned epicsShareAPI ipAddrToHostName
|
||||
(const struct in_addr *pAddr, char *pBuf, unsigned bufSize)
|
||||
{
|
||||
int status;
|
||||
int errnoCopy = errno;
|
||||
unsigned len;
|
||||
|
||||
if (bufSize<1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bufSize>MAXHOSTNAMELEN) {
|
||||
status = hostGetByAddr ((int)pAddr->s_addr, pBuf);
|
||||
if (status==OK) {
|
||||
pBuf[MAXHOSTNAMELEN] = '\0';
|
||||
len = strlen (pBuf);
|
||||
}
|
||||
else {
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
char name[MAXHOSTNAMELEN+1];
|
||||
status = hostGetByAddr (pAddr->s_addr, name);
|
||||
if (status==OK) {
|
||||
strncpy (pBuf, name, bufSize);
|
||||
pBuf[bufSize-1] = '\0';
|
||||
len = strlen (pBuf);
|
||||
}
|
||||
else {
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
errno = errnoCopy;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/*
|
||||
* ipAddrToA()
|
||||
* (convert IP address to ASCII host name)
|
||||
* sockAddrToA()
|
||||
* (convert socket address to ASCII host name)
|
||||
*/
|
||||
void epicsShareAPI ipAddrToA
|
||||
(const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize)
|
||||
void epicsShareAPI sockAddrToA
|
||||
(const struct sockaddr *paddr, char *pBuf, unsigned bufSize)
|
||||
{
|
||||
const int maxPortDigits = 5u;
|
||||
char name[max(INET_ADDR_LEN,MAXHOSTNAMELEN)+1];
|
||||
@@ -38,7 +79,7 @@ void epicsShareAPI ipAddrToA
|
||||
return;
|
||||
}
|
||||
|
||||
if (paddr->sin_family!=AF_INET) {
|
||||
if (paddr->sa_family!=AF_INET) {
|
||||
strncpy(pBuf, "<Host with Unknown Address Type>", bufSize-1);
|
||||
/*
|
||||
* force null termination
|
||||
@@ -46,9 +87,10 @@ void epicsShareAPI ipAddrToA
|
||||
pBuf[bufSize-1] = '\0';
|
||||
}
|
||||
else {
|
||||
status = hostGetByAddr ((int)paddr->sin_addr.s_addr, name);
|
||||
struct sockaddr_in *paddr_in = (struct sockaddr_in *) paddr;
|
||||
status = hostGetByAddr ((int)paddr_in->sin_addr.s_addr, name);
|
||||
if (status!=OK) {
|
||||
inet_ntoa_b (paddr->sin_addr, name);
|
||||
inet_ntoa_b (paddr_in->sin_addr, name);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -56,7 +98,7 @@ void epicsShareAPI ipAddrToA
|
||||
*/
|
||||
if (bufSize>maxPortDigits+strlen(name)) {
|
||||
sprintf (pBuf, "%.*s:%u", ((int)bufSize)-maxPortDigits-1,
|
||||
name, ntohs(paddr->sin_port));
|
||||
name, ntohs(paddr_in->sin_port));
|
||||
}
|
||||
else {
|
||||
sprintf (pBuf, "%.*s", ((int)bufSize)-1, name);
|
||||
@@ -88,4 +130,4 @@ epicsShareFunc int epicsShareAPI hostToIPAddr
|
||||
* success
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user