simplify code by starting out with unsigned

This commit is contained in:
Jeff Hill
2000-05-01 16:31:11 +00:00
parent 82d441411b
commit 4710f52e25

View File

@@ -19,86 +19,24 @@
#define NELEMENTS(A) (sizeof(A)/sizeof(A[0]))
#endif /*NELEMENTS*/
LOCAL int initIPAddr (struct in_addr ipAddr, unsigned short port, struct sockaddr_in *pIP);
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, struct in_addr *pIpAddr);
/*
* rational replacement for inet_addr()
* which allows the limited broadcast address
* 255.255.255.255, allows the user
* to specify a port number, and allows also a
* named host to be specified.
*
* Sets the port number to "defaultPort" only if
* "pAddrString" does not contain an addres of the form
* "n.n.n.n:p"
* addrArrayToUL ()
*/
epicsShareFunc int epicsShareAPI
aToIPAddr(const char *pAddrString, unsigned short defaultPort, struct sockaddr_in *pIP)
LOCAL int addrArrayToUL (const unsigned short *pAddr, unsigned nElements, struct in_addr *pIpAddr)
{
int status;
long addr[4];
char hostName[512]; /* !! change n elements here requires change in format below !! */
int port;
struct in_addr ina;
/*
* traditional dotted ip addres
*/
status = sscanf (pAddrString, "%li.%li.%li.%li:%i",
addr, addr+1u, addr+2u, addr+3u, &port);
if (status>=4) {
if (addrArrayToUL (addr, NELEMENTS(addr), &ina)<0) {
return -1;
}
if (status==4) {
port = defaultPort;
}
if (port<0 || port>USHRT_MAX) {
return -1;
}
return initIPAddr (ina, (unsigned short) port, pIP);
}
unsigned i;
unsigned long addr = 0ul;
/*
* IP address as a raw number
*/
status = sscanf (pAddrString, "%li:%i", addr, &port);
if (status>=1) {
if (*addr<0x0 && *addr>0xffffffff) {
for ( i=0u; i < nElements; i++ ) {
if ( pAddr[i] > 0xff ) {
return -1;
}
if (status==1) {
port = defaultPort;
}
if (port<0 || port>USHRT_MAX) {
return -1;
}
ina.s_addr = htonl ( ((unsigned long)*addr) );
return initIPAddr (ina, (unsigned short)port, pIP);
addr <<= 8;
addr |= pAddr[i];
}
/*
* check for a valid host name before giving up
*/
status = sscanf (pAddrString, "%511s:%i", hostName, &port);
if (status>=1) {
if (status==1) {
port = defaultPort;
}
if (port<0 || port>USHRT_MAX) {
return -1;
}
status = hostToIPAddr (hostName, &ina);
if (status==0) {
return initIPAddr (ina, (unsigned short)port, pIP);
}
}
/*
* none of the above - return indicating failure
*/
return -1;
pIpAddr->s_addr = htonl ( addr );
return 0;
}
/*
@@ -116,21 +54,72 @@ LOCAL int initIPAddr (struct in_addr ipAddr, unsigned short port, struct sockadd
}
/*
* addrArrayToUL()
* rational replacement for inet_addr()
* which allows the limited broadcast address
* 255.255.255.255, allows the user
* to specify a port number, and allows also a
* named host to be specified.
*
* Sets the port number to "defaultPort" only if
* "pAddrString" does not contain an addres of the form
* "n.n.n.n:p"
*/
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, struct in_addr *pIpAddr)
epicsShareFunc int epicsShareAPI
aToIPAddr(const char *pAddrString, unsigned short defaultPort, struct sockaddr_in *pIP)
{
unsigned i;
unsigned long addr = 0ul;
for (i=0u; i<nElements; i++) {
if (pAddr[i]<0x0 || pAddr[i]>0xff) {
int status;
unsigned short addr[4];
unsigned long rawAddr;
char hostName[512]; /* !! change n elements here requires change in format below !! */
unsigned short port;
struct in_addr ina;
/*
* traditional dotted ip addres
*/
status = sscanf (pAddrString, "%hu.%hu.%hu.%hu:%hu",
addr, addr+1u, addr+2u, addr+3u, &port);
if (status>=4) {
if ( addrArrayToUL ( addr, NELEMENTS ( addr ), &ina ) < 0 ) {
return -1;
}
addr <<= 8;
addr |= (unsigned long) pAddr[i];
if (status==4) {
port = defaultPort;
}
return initIPAddr (ina, port, pIP);
}
pIpAddr->s_addr = htonl (addr);
return 0;
/*
* IP address as a raw number
*/
status = sscanf ( pAddrString, "%lu:%hu", &rawAddr, &port );
if (status>=1) {
if ( rawAddr > 0xffffffff ) {
return -1;
}
if ( status == 1 ) {
port = defaultPort;
}
ina.s_addr = htonl ( rawAddr );
return initIPAddr ( ina, port, pIP );
}
/*
* check for a valid host name before giving up
*/
status = sscanf (pAddrString, "%511s:%hu", hostName, &port);
if (status>=1) {
if (status==1) {
port = defaultPort;
}
status = hostToIPAddr (hostName, &ina);
if (status==0) {
return initIPAddr (ina, port, pIP);
}
}
/*
* none of the above - return indicating failure
*/
return -1;
}