Files
sics/site_ansto/anstoutil.c
2014-05-16 17:23:44 +10:00

220 lines
5.9 KiB
C

#include <stdlib.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "sics.h"
#include "anstoutil.h"
#define MAXNUMCHAR 32
/** \brief Get configuration parameter
* \param *pCon (r) connection object.
* \param *pTcl (r) Tcl interpreter
* \param *params Tcl array of configuration parameters
* \param *parName name of parameter to get from the array
* \param mustHave indicates optional or mandatory parameters\n
* possible values
* - _REQUIRED
* - _OPTIONAL
* \return (r) a reference to a string representation of the parameter value
*/
char *getParam(SConnection *pCon, Tcl_Interp *pTcl, char *params, char *parName, int mustHave ) {
char *pPtr=NULL;
char pError[ERRLEN];
pPtr = Tcl_GetVar2(pTcl,params,parName,TCL_GLOBAL_ONLY);
if((mustHave == _REQUIRED) && !pPtr){
snprintf(pError, ERRLEN,"ERROR: You must supply an '%s' parameter", parName);
SCWrite(pCon,pError, eError);
}
return pPtr;
}
/** \brief Lookup named host in /etc/hosts
* \param *pCon (r) connection object.
* \param *hostName (r) name of host to look up
* \return
* - host address on success
* - 0 on failure
*/
const char* getHostNam(SConnection *pCon, char *hostName) {
struct hostent *hp=NULL;
char pError[ERRLEN];
static char hostaddr[132];
/* allow specification of dotted decimal address */
do {
struct in_addr in;
if (inet_aton(hostName, &in))
return hostName;
} while (0);
/* allow redirection in file sics_test_hosts */
do {
FILE *fp = NULL;
char line[202], *cp;
fp = fopen("../sics_test_hosts", "r");
if (fp == NULL)
fp = fopen("sics_test_hosts", "r");
if (fp != NULL) {
while ((cp = fgets(line, sizeof(line)-1, fp)) != NULL) {
int len = strlen(cp);
while (len > 0 && cp[len-1] == '\n')
cp[--len] = '\0';
if (*cp != '#' && *cp != '\0') {
char addr[132], *ap;
ap = addr;
while (*cp && !isblank(*cp)) {
*ap++ = *cp++;
*ap = '\0';
}
while (*cp) {
char name[132], *np;
np = name;
while (*cp && isblank(*cp)) {
++cp;
}
while (*cp && !isblank(*cp)) {
*np++ = *cp++;
*np = '\0';
}
if (strcasecmp(hostName, name) == 0) {
strncpy(hostaddr, addr, sizeof(hostaddr)-1);
return hostaddr;
}
}
}
}
fclose(fp);
}
} while (0);
hp = gethostbyname(hostName);
if (hp == NULL) {
snprintf(pError, ERRLEN,"ERROR: '%s' hostname not found", hostName);
SCWrite(pCon,pError, eError);
return 0;
}
strncpy(hostaddr, inet_ntoa(*(struct in_addr *)hp->h_addr), sizeof(hostaddr)-1);
return hostaddr;
}
int hostNamCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
const char* hn;
//int iMacro;
char hostaddr[132];
assert(pCon != NULL);
assert(pInter != NULL);
switch (argc) {
case 2:
hn = getHostNam(pCon, argv[1]);
snprintf(hostaddr, sizeof(hostaddr)-1, "%s", hn);
break;
default:
SCWrite(pCon,"Insufficient arguments to hostNamCmd",eError);
return 0;
}
//iMacro = SCinMacro(pCon);
//SCsetMacro(pCon,0);
SCWrite(pCon,hostaddr,eValue);
//SCsetMacro(pCon,iMacro);
return 1;
}
/** \brief Lookup named port in /etc/services
* \param *pCon (r) connection object.
* \param *portName (r) name of port to look up
* \return
* - port number on success
* - 0 on failure
*/
int getPortNum(SConnection *pCon, char *portName) {
struct servent *sp=NULL;
char pError[ERRLEN];
/* allow specification of decimal port number */
do {
char *cp;
long port = strtol(portName, &cp, 10);
if (cp == portName) {
/* not a valid number */
} else {
return (int) port;
}
} while (0);
/* TODO allow redirection in file sics_test_services */
do {
FILE *fp = NULL;
char line[202], *cp;
fp = fopen("../sics_test_services", "r");
if (fp == NULL)
fp = fopen("sics_test_services", "r");
if (fp != NULL) {
int udp_port = 0;
while ((cp = fgets(line, sizeof(line)-1, fp)) != NULL) {
if (*cp != '#' && *cp != '\0') {
char name[132], *np;
np = name;
while (*cp && !isblank(*cp)) {
*np++ = *cp++;
*np = '\0';
}
if (strcasecmp(portName, name) != 0)
continue;
while (*cp && isblank(*cp))
++cp;
if (*cp && isdigit(*cp)) {
long port;
port = strtol(cp, &np, 10);
if (strncasecmp("/tcp", np, 4) == 0)
return (int) port;
if (strncasecmp("/udp", np, 4) == 0)
udp_port = (int) port;
}
}
}
fclose(fp);
if (udp_port > 0)
return udp_port;
}
} while (0);
sp = getservbyname(portName, NULL);
if (sp == NULL) {
snprintf(pError, ERRLEN,"ERROR: '%s' service not found", portName);
SCWrite(pCon,pError, eError);
return 0;
}
return ntohs(sp->s_port);
}
int portNumCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
int pn;
//int iMacro;
char portNum[MAXNUMCHAR];
assert(pCon != NULL);
assert(pInter != NULL);
switch (argc) {
case 2:
pn = getPortNum(pCon, argv[1]);
snprintf(portNum, MAXNUMCHAR, "%d", pn);
break;
default:
SCWrite(pCon,"Insufficient arguments to portNumCmd",eError);
return 0;
}
//iMacro = SCinMacro(pCon);
//SCsetMacro(pCon,0);
SCWrite(pCon,portNum,eValue);
//SCsetMacro(pCon,iMacro);
return 1;
}
int AbortBatch(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]) {
SCSetInterrupt(pCon,eAbortBatch);
return TCL_ERROR;
}