Add [hostnam xxx] and [testlog] commands
r3758 | dcl | 2012-10-04 14:36:30 +1000 (Thu, 04 Oct 2012) | 1 line
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sics.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "sics.h"
|
||||
#include "anstoutil.h"
|
||||
|
||||
#define MAXNUMCHAR 32
|
||||
@@ -28,6 +30,98 @@ char *getParam(SConnection *pCon, Tcl_Interp *pTcl, char *params, char *parName,
|
||||
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
|
||||
@@ -39,6 +133,49 @@ 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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
} while (0);
|
||||
sp = getservbyname(portName, NULL);
|
||||
if (sp == NULL) {
|
||||
snprintf(pError, ERRLEN,"ERROR: '%s' service not found", portName);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define FAILURE 0
|
||||
#define SUCCESS 1
|
||||
char *getParam(SConnection *pCon, Tcl_Interp *pTcl, char *params, char *parName, int mustHave );
|
||||
int hostNamCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]);
|
||||
int portNumCmd(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]);
|
||||
int getPortNum(SConnection *pCon, char *portName);
|
||||
int AbortBatch(SConnection *pCon, SicsInterp *pInter, void *pData, int argc, char *argv[]);
|
||||
|
||||
@@ -68,6 +68,7 @@ extern void AddRFAmpProtocol();
|
||||
extern void AddTCPMBProtocol ();
|
||||
extern void AddLFGenProtocol();
|
||||
extern int ANSTO_MakeHistMemory(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]);
|
||||
extern int testLogCmd(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]);
|
||||
|
||||
int SICS_Site(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[])
|
||||
{
|
||||
@@ -139,8 +140,10 @@ static void AddCommands(SicsInterp *pInter)
|
||||
LS340InitProtocol(pInter);
|
||||
AddCommand(pInter,"InstallProtocolHandler", InstallProtocol,NULL,NULL);
|
||||
AddCommand(pInter,"MakeTCPSelector",VelSelTcpFactory,NULL,NULL);
|
||||
AddCommand(pInter,"hostnam",hostNamCmd,NULL,NULL);
|
||||
AddCommand(pInter,"portnum",portNumCmd,NULL,NULL);
|
||||
AddCommand(pInter,"abortbatch",AbortBatch,NULL,NULL);
|
||||
AddCommand(pInter,"testlog",testLogCmd,NULL,NULL);
|
||||
AddCommand(pInter,"MakeHMControl_ANSTO",MakeHMControl_ANSTO,NULL,NULL);
|
||||
// AddCommand(pInter,"MakeAsyncProtocol",AsyncProtocolFactory,NULL,NULL);
|
||||
// AddCommand(pInter,"MakeAsyncQueue",AsyncQueueFactory,NULL,NULL);
|
||||
|
||||
Reference in New Issue
Block a user