Files
sics/site_ansto/anstoutil.c
Ferdi Franceschini 4ba40fd8bd added getPortNum
r1142 | ffr | 2006-10-13 15:44:20 +1000 (Fri, 13 Oct 2006) | 2 lines
2012-11-15 12:48:16 +11:00

48 lines
1.4 KiB
C

#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sics.h>
#include "anstoutil.h"
/** \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 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];
sp = getservbyname(portName, "tcp");
if (sp == NULL) {
snprintf(pError, ERRLEN,"ERROR: '%s' service not found", portName);
SCWrite(pCon,pError, eError);
return 0;
}
return ntohs(sp->s_port);
}