check for a valid host name after trying a dotted ip addr
This commit is contained in:
+53
-28
@@ -8,22 +8,24 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "osiSock.h"
|
||||
#define LOCAL
|
||||
|
||||
#define epicsExportSharedSymbols
|
||||
#include "ipAddrToA.h"
|
||||
#include "bsdSocketResource.h"
|
||||
|
||||
#ifndef NELEMENTS
|
||||
#define NELEMENTS(A) (sizeof(A)/sizeof(A[0]))
|
||||
#endif /*NELEMENTS*/
|
||||
|
||||
static int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr);
|
||||
static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP);
|
||||
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr);
|
||||
LOCAL int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP);
|
||||
|
||||
/*
|
||||
* rational replacement for inet_addr()
|
||||
* which allows the limited broadcast address
|
||||
* 255.255.255.255 and also allows the user
|
||||
* to specify a port number
|
||||
* 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
|
||||
@@ -34,53 +36,76 @@ epicsShareFunc int epicsShareAPI
|
||||
{
|
||||
int status;
|
||||
long addr[4];
|
||||
char hostName[512]; /* !! change n elements here requires change in format below !! */
|
||||
int port;
|
||||
|
||||
unsigned long ipAddr;
|
||||
|
||||
/*
|
||||
* traditional dotted ip addres
|
||||
*/
|
||||
status = sscanf (pAddrString, "%li.%li.%li.%li:%i",
|
||||
addr, addr+1u, addr+2u, addr+3u, &port);
|
||||
if (status==5) {
|
||||
status = addrArrayToUL (addr, NELEMENTS(addr), &ipAddr);
|
||||
if (status<0) {
|
||||
if (status>=4) {
|
||||
if (addrArrayToUL (addr, NELEMENTS(addr), &ipAddr)<0) {
|
||||
return -1;
|
||||
}
|
||||
if (status==4) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr (ipAddr, (unsigned short) port, pIP);
|
||||
}
|
||||
|
||||
status = sscanf (pAddrString, "%li.%li.%li.%li",
|
||||
addr, addr+1u, addr+2u, addr+3u);
|
||||
if (status==4) {
|
||||
status = addrArrayToUL (addr, NELEMENTS(addr), &ipAddr);
|
||||
if (status<0) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr (ipAddr, defaultPort, pIP);
|
||||
}
|
||||
|
||||
status = sscanf (pAddrString, "%li", addr);
|
||||
if (status==1) {
|
||||
/*
|
||||
* IP address as a raw number
|
||||
*/
|
||||
status = sscanf (pAddrString, "%li:%i", addr, &port);
|
||||
if (status>=1) {
|
||||
if (*addr<0x0 && *addr>0xffffffff) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr ((unsigned long)*addr, defaultPort, pIP);
|
||||
if (status==1) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr ((unsigned long)*addr, (unsigned short)port, pIP);
|
||||
}
|
||||
|
||||
/*
|
||||
* check for a valid host name before giving up
|
||||
*/
|
||||
status = sscanf (pAddrString, "%511s:%i", hostName, &port);
|
||||
if (status>=1) {
|
||||
struct in_addr ina;
|
||||
|
||||
if (status==1) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
status = hostToIPAddr (hostName, &ina);
|
||||
if (status==0) {
|
||||
return initIPAddr (ina.s_addr, (unsigned short)port, pIP);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* none of the above - return indicating failure
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* initIPAddr()
|
||||
*/
|
||||
static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP)
|
||||
LOCAL int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP)
|
||||
{
|
||||
if (port<=IPPORT_USERRESERVED) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset (pIP, '\0', sizeof(*pIP));
|
||||
pIP->sin_family = AF_INET;
|
||||
pIP->sin_port = htons(port);
|
||||
@@ -91,7 +116,7 @@ static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockadd
|
||||
/*
|
||||
* addrArrayToUL()
|
||||
*/
|
||||
static int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr)
|
||||
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
|
||||
+53
-28
@@ -8,22 +8,24 @@
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "osiSock.h"
|
||||
#define LOCAL
|
||||
|
||||
#define epicsExportSharedSymbols
|
||||
#include "ipAddrToA.h"
|
||||
#include "bsdSocketResource.h"
|
||||
|
||||
#ifndef NELEMENTS
|
||||
#define NELEMENTS(A) (sizeof(A)/sizeof(A[0]))
|
||||
#endif /*NELEMENTS*/
|
||||
|
||||
static int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr);
|
||||
static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP);
|
||||
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr);
|
||||
LOCAL int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP);
|
||||
|
||||
/*
|
||||
* rational replacement for inet_addr()
|
||||
* which allows the limited broadcast address
|
||||
* 255.255.255.255 and also allows the user
|
||||
* to specify a port number
|
||||
* 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
|
||||
@@ -34,53 +36,76 @@ epicsShareFunc int epicsShareAPI
|
||||
{
|
||||
int status;
|
||||
long addr[4];
|
||||
char hostName[512]; /* !! change n elements here requires change in format below !! */
|
||||
int port;
|
||||
|
||||
unsigned long ipAddr;
|
||||
|
||||
/*
|
||||
* traditional dotted ip addres
|
||||
*/
|
||||
status = sscanf (pAddrString, "%li.%li.%li.%li:%i",
|
||||
addr, addr+1u, addr+2u, addr+3u, &port);
|
||||
if (status==5) {
|
||||
status = addrArrayToUL (addr, NELEMENTS(addr), &ipAddr);
|
||||
if (status<0) {
|
||||
if (status>=4) {
|
||||
if (addrArrayToUL (addr, NELEMENTS(addr), &ipAddr)<0) {
|
||||
return -1;
|
||||
}
|
||||
if (status==4) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr (ipAddr, (unsigned short) port, pIP);
|
||||
}
|
||||
|
||||
status = sscanf (pAddrString, "%li.%li.%li.%li",
|
||||
addr, addr+1u, addr+2u, addr+3u);
|
||||
if (status==4) {
|
||||
status = addrArrayToUL (addr, NELEMENTS(addr), &ipAddr);
|
||||
if (status<0) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr (ipAddr, defaultPort, pIP);
|
||||
}
|
||||
|
||||
status = sscanf (pAddrString, "%li", addr);
|
||||
if (status==1) {
|
||||
/*
|
||||
* IP address as a raw number
|
||||
*/
|
||||
status = sscanf (pAddrString, "%li:%i", addr, &port);
|
||||
if (status>=1) {
|
||||
if (*addr<0x0 && *addr>0xffffffff) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr ((unsigned long)*addr, defaultPort, pIP);
|
||||
if (status==1) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return initIPAddr ((unsigned long)*addr, (unsigned short)port, pIP);
|
||||
}
|
||||
|
||||
/*
|
||||
* check for a valid host name before giving up
|
||||
*/
|
||||
status = sscanf (pAddrString, "%511s:%i", hostName, &port);
|
||||
if (status>=1) {
|
||||
struct in_addr ina;
|
||||
|
||||
if (status==1) {
|
||||
port = defaultPort;
|
||||
}
|
||||
if (port<0 || port>USHRT_MAX) {
|
||||
return -1;
|
||||
}
|
||||
status = hostToIPAddr (hostName, &ina);
|
||||
if (status==0) {
|
||||
return initIPAddr (ina.s_addr, (unsigned short)port, pIP);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* none of the above - return indicating failure
|
||||
*/
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* initIPAddr()
|
||||
*/
|
||||
static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP)
|
||||
LOCAL int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockaddr_in *pIP)
|
||||
{
|
||||
if (port<=IPPORT_USERRESERVED) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset (pIP, '\0', sizeof(*pIP));
|
||||
pIP->sin_family = AF_INET;
|
||||
pIP->sin_port = htons(port);
|
||||
@@ -91,7 +116,7 @@ static int initIPAddr (unsigned long ipAddr, unsigned short port, struct sockadd
|
||||
/*
|
||||
* addrArrayToUL()
|
||||
*/
|
||||
static int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr)
|
||||
LOCAL int addrArrayToUL (const long *pAddr, unsigned nElements, unsigned long *pIpAddr)
|
||||
{
|
||||
unsigned i;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user