added new dotted IP addr ascii conversions

This commit is contained in:
Jeff Hill
2000-08-25 01:34:20 +00:00
parent ad17787317
commit ce193d5c57
2 changed files with 114 additions and 63 deletions
+90 -53
View File
@@ -40,29 +40,18 @@
#define nDigitsDottedIP 4u
#define maxChunkDigit 3u
#define maxDottedIPDigit ((nDigitsDottedIP-1) + nDigitsDottedIP*maxChunkDigit)
#define maxDottedIPDigit ( ( nDigitsDottedIP - 1 ) + nDigitsDottedIP*maxChunkDigit )
#define chunkSize 8u
#define maxPortDigits 5u
#define makeMask(NBITS) ((1u<<((unsigned)NBITS))-1u)
/*
* ipAddrToA()
* (convert IP address to ASCII host name)
*/
void epicsShareAPI ipAddrToA
(const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize)
{
sockAddrToA ( (struct sockaddr *)paddr, pBuf, bufSize);
}
#define makeMask(NBITS) ( ( 1u << ( (unsigned) NBITS) ) - 1u )
/*
* sockAddrToA()
* (convert socket address to ASCII host name)
*/
void epicsShareAPI sockAddrToA
(const struct sockaddr *paddr, char *pBuf, unsigned bufSize)
( const struct sockaddr *paddr, char *pBuf, unsigned bufSize )
{
if (bufSize<1) {
return;
@@ -76,45 +65,93 @@ void epicsShareAPI sockAddrToA
pBuf[bufSize-1] = '\0';
}
else {
struct sockaddr_in *paddr_in = (struct sockaddr_in *) paddr;
unsigned len;
len = ipAddrToHostName (&paddr_in->sin_addr, pBuf, bufSize);
if (len==0) {
if (bufSize>maxDottedIPDigit) {
unsigned chunk[nDigitsDottedIP];
unsigned addr = ntohl (paddr_in->sin_addr.s_addr);
unsigned i;
for (i=0; i<nDigitsDottedIP; i++) {
chunk[i] = addr & makeMask(chunkSize);
addr >>= chunkSize;
}
/*
* inet_ntoa() isnt used because it isnt thread safe
* (and the replacements are not standardized)
*/
len = (unsigned) sprintf (pBuf, "%u.%u.%u.%u",
chunk[3], chunk[2], chunk[1], chunk[0]);
}
else {
strncpy (pBuf, "<IP addr>", bufSize);
pBuf[bufSize-1] = '\0';
len = strlen (pBuf);
}
}
assert (len<bufSize);
bufSize -= len;
/*
* allow space for the port number
*/
if (bufSize>maxPortDigits+1) {
sprintf (&pBuf[len], ":%hu", ntohs(paddr_in->sin_port));
}
const struct sockaddr_in *paddr_in = ( const struct sockaddr_in * ) paddr;
ipAddrToA ( paddr_in, pBuf, bufSize );
}
}
/*
* ipAddrToA()
* (convert IP address to ASCII host name)
*/
void epicsShareAPI ipAddrToA
( const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize )
{
unsigned len;
len = ipAddrToHostName ( &paddr->sin_addr, pBuf, bufSize );
if ( len == 0 ) {
ipAddrToDottedA ( paddr, pBuf, bufSize );
}
else {
assert ( len < bufSize );
bufSize -= len;
/*
* allow space for the port number
*/
if ( bufSize > maxPortDigits + 1 ) {
sprintf ( &pBuf[len], ":%hu", ntohs (paddr->sin_port) );
}
}
}
/*
* sockAddrToDottedA ()
*/
void epicsShareAPI sockAddrToDottedA
( const struct sockaddr *paddr, char *pBuf, unsigned bufSize )
{
if ( paddr->sa_family != AF_INET ) {
strncpy ( pBuf, "<Ukn Addr Type>", bufSize - 1 );
/*
* force null termination
*/
pBuf[bufSize-1] = '\0';
}
else {
const struct sockaddr_in *paddr_in = ( const struct sockaddr_in * ) paddr;
ipAddrToDottedA ( paddr_in, pBuf, bufSize );
}
}
/*
* ipAddrToDottedA ()
*/
void epicsShareAPI ipAddrToDottedA
( const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize )
{
if ( bufSize > maxDottedIPDigit ) {
unsigned chunk[nDigitsDottedIP];
unsigned addr = ntohl ( paddr->sin_addr.s_addr );
unsigned i;
unsigned len;
for ( i = 0; i < nDigitsDottedIP; i++ ) {
chunk[i] = addr & makeMask ( chunkSize );
addr >>= chunkSize;
}
/*
* inet_ntoa() isnt used because it isnt thread safe
* (and the replacements are not standardized)
*/
len = (unsigned) sprintf ( pBuf, "%u.%u.%u.%u",
chunk[3], chunk[2], chunk[1], chunk[0] );
assert ( len < bufSize );
bufSize -= len;
/*
* allow space for the port number
*/
if ( bufSize > maxPortDigits + 1 ) {
sprintf ( &pBuf[len], ":%hu", ntohs ( paddr->sin_port ) );
}
}
else {
strncpy ( pBuf, "<IPA>", bufSize );
pBuf[bufSize-1] = '\0';
}
}
+24 -10
View File
@@ -44,30 +44,44 @@ struct sockaddr;
struct sockaddr_in;
struct in_addr;
/*
* convert IP address to ASCII in this order
* 1) look for matching host name
* 2) convert to raw dotted IP address with trailing port
*/
epicsShareFunc void epicsShareAPI ipAddrToA
(const struct sockaddr_in *pInetAddr, char *pBuf, unsigned bufSize);
/*
* convert socket address to ASCII in this order
* 1) look for matching host name
* 1) look for matching host name and typically add trailing IP port
* 2) convert to raw ascii address (typically this is a
* dotted IP address with trailing port)
*/
epicsShareFunc void epicsShareAPI sockAddrToA
(const struct sockaddr *paddr, char *pBuf, unsigned bufSize);
/*
* convert IP address to ASCII in this order
* 1) look for matching host name and add trailing port
* 2) convert to raw dotted IP address with trailing port
*/
epicsShareFunc void epicsShareAPI ipAddrToA
(const struct sockaddr_in *pInetAddr, char *pBuf, unsigned bufSize);
/*
* sockAddrToDottedA ()
* typically convert to raw dotted IP address with trailing port
*/
epicsShareFunc void epicsShareAPI sockAddrToDottedA
( const struct sockaddr *paddr, char *pBuf, unsigned bufSize );
/*
* ipAddrToDottedA ()
* convert to raw dotted IP address with trailing port
*/
epicsShareFunc void epicsShareAPI ipAddrToDottedA
( const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize );
/*
* convert inet address to a host name string
*
* returns the number of bytes stored in buffer not counting the terminating
* null character, or zero on failure
*
* OS specific
* there are many OS specific implementation stubs for this routine
*/
epicsShareFunc unsigned epicsShareAPI ipAddrToHostName
(const struct in_addr *pAddr, char *pBuf, unsigned bufSize);