From 4710f52e256f8313e4cd5700dede00716c2a68fb Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Mon, 1 May 2000 16:31:11 +0000 Subject: [PATCH] simplify code by starting out with unsigned --- src/libCom/misc/aToIPAddr.c | 159 +++++++++++++++++------------------- 1 file changed, 74 insertions(+), 85 deletions(-) diff --git a/src/libCom/misc/aToIPAddr.c b/src/libCom/misc/aToIPAddr.c index 13d6fa7f5..9bddf54d5 100644 --- a/src/libCom/misc/aToIPAddr.c +++ b/src/libCom/misc/aToIPAddr.c @@ -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; i0xff) { + 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; }