cdev-1.7.2n

This commit is contained in:
2022-12-13 12:44:04 +01:00
commit b3b88fc333
1357 changed files with 338883 additions and 0 deletions
+417
View File
@@ -0,0 +1,417 @@
#ifndef ADDRESS_INDEX_H_
#define ADDRESS_INDEX_H_
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// *****************************************************************************
// * union AddressReference :
// * This union contains all of the components that are necessary to locate
// * and validate a memory address from within an AddressIndex object.
// *
// * The index entry contains the location of the pointer within the address
// * array.
// *
// * The checksum entry contains the checksum value that should be located
// * at the specified index within the checksum array.
// *
// *****************************************************************************
typedef union
{
unsigned rawData;
struct {
unsigned index : 24;
unsigned checksum : 8;
} value;
} AddressReference;
// *****************************************************************************
// * class AddressIndex:
// * This class is used to allow an array to be dereferenced using an integer
// * code that contains an array index and a checksum value. Both of these
// * values are validated before the user is allowed to access the data.
// *****************************************************************************
class AddressIndex
{
friend class AddressIndexIterator;
private:
enum {START_ARRAY_SIZE=1024};
// *********************************************************************
// * population : this integer identifies the exact number of data items
// * that currently exist in the list.
// *********************************************************************
int population;
// *********************************************************************
// * maxEntries : this integer identifies the maximum number of entries
// * that this object can contain.
// *********************************************************************
int maxEntries;
// *********************************************************************
// * curEntry : this integer identifies the currently selected entry.
// *********************************************************************
int curEntry;
// *********************************************************************
// * checksum : this is an array of bytes that are used to identify the
// * checksum for a specific address. This checksum is used to validate
// * that the address has not been changed externally.
// *********************************************************************
unsigned char * checksum;
// *********************************************************************
// * address : this is the array of addresses that will be stored and
// * retrieved using this object.
// *********************************************************************
void ** address;
public:
inline AddressIndex ( void );
inline ~AddressIndex ( void );
inline unsigned insert( void * ptr );
inline void remove( void * ptr );
inline void remove( unsigned idx );
inline unsigned find ( void * ptr );
inline void * find ( unsigned idx );
};
class AddressIndexIterator
{
private:
AddressIndex * index;
int current;
public:
inline AddressIndexIterator (AddressIndex * Index);
inline ~AddressIndexIterator( void );
inline void * first ( void );
inline void * operator ++ ( void );
inline void * operator ++ ( int x );
inline unsigned key ( void );
inline void * data ( void );
};
// *****************************************************************************
// * AddressIndex::AddressIndex :
// * This is the constructor for the AddressIndex class. It is allocates a
// * default number of entries in the checksum and address arrays and then
// * initializes the data values to 0.
// *
// * Note that these arrays must be allocated using malloc because we will
// * use realloc later to expand there size if necessary.
// *****************************************************************************
inline AddressIndex::AddressIndex ( void )
: population(0),maxEntries(0),curEntry(0),checksum(NULL),address(NULL)
{
maxEntries = START_ARRAY_SIZE;
checksum = (unsigned char *)malloc(sizeof(unsigned char)*maxEntries);
address = (void **) malloc(sizeof(void *) *maxEntries);
memset(checksum, 1, sizeof(unsigned char)*maxEntries);
memset(address, 0, sizeof(void *) *maxEntries);
}
// *****************************************************************************
// * AddressIndex::~AddressIndex :
// * This is the destructor for the object. It will deallocate any memory
// * that has been assigned to the checksum or address objects.
// *
// * Note that these arrays must be deallocated using the free library call
// * because they were allocated using either malloc or realloc.
// *****************************************************************************
inline AddressIndex::~AddressIndex ( void )
{
if(checksum!=NULL) free(checksum);
if(address!=NULL) free(address);
}
// *****************************************************************************
// * AddressIndex::insert :
// * This method is used to insert a void * pointer into the AddressIndex
// * object. It returns an unsigned integer result which is the rawData
// * component of an AddressReference object.
// *****************************************************************************
inline unsigned AddressIndex::insert ( void * ptr )
{
AddressReference result;
// *********************************************************************
// * Initial all entries within the AddressReference object before
// * starting.
// *********************************************************************
result.rawData = 0U;
// *********************************************************************
// * If the pointer provided by the user is NULL, then it cannot be
// * inserted into the list... Otherwise,
// *
// * From the current position in the array, we are going to walk
// * forward until we find an empty slot. An empty slot is indicated
// * by the value NULL in its address.
// *
// * If the end of the list is reached before an empty slot can be found
// * then special considerations will be made as described below.
// *********************************************************************
if(ptr!=NULL)
{
while(curEntry<maxEntries && address[curEntry]!=NULL) curEntry++;
// *************************************************************
// * If curEntry>=maxEntries then an empty slot was not located.
// * The following options should be considered...
// *************************************************************
// *************************************************************
// * 1) If the population of the table is less than half of the
// * maximum number of entries... go back to the beginning
// * and search the table for an empty slot.
// *************************************************************
if(curEntry>=maxEntries && population<(maxEntries/2))
{
curEntry = 0;
while(curEntry<maxEntries && address[curEntry]!=NULL)
{
curEntry++;
}
}
// *************************************************************
// * 2) If the population of the table is more than half of the
// * maximum number of entries... double the size of the
// * table and continue from the current cursor position.
// * Note: the newly allocated data at the end of the buffer
// * must be initialized to 0.
// *************************************************************
else if(curEntry>=maxEntries)
{
curEntry = maxEntries;
maxEntries*=2;
checksum = (unsigned char *)realloc(checksum, sizeof(char *)*maxEntries);
address = (void **) realloc(address, sizeof(void *)*maxEntries);
memset(&checksum[curEntry], 0, sizeof(char *)*curEntry);
memset(&address [curEntry], 0, sizeof(void *)*curEntry);
}
// *************************************************************
// * At this time the curEntry index points to the insertion
// * point that should be used. Place the index and checksum
// * information into the result object and update the
// * population count.
// *************************************************************
checksum[curEntry] = (checksum[curEntry]==255?1:(checksum[curEntry]+1));
address [curEntry] = ptr;
result.value.index = curEntry;
result.value.checksum = checksum[curEntry];
population++;
}
// *********************************************************************
// * Return the rawData portion of the AddressReference to the caller
// * as an unsigned integer.
// *********************************************************************
return result.rawData;
}
// *****************************************************************************
// * AddressIndex::remove :
// * This function will locate an entry by searching the array for a
// * specific pointer. If the entry is found it will be deleted. Direct
// * searching is one of the most time consuming approaches to locating
// * data, therefore the remove function that uses an unsigned integer
// * identifier should be used whenever possible.
// *****************************************************************************
inline void AddressIndex::remove ( void * ptr)
{
if(ptr!=NULL)
{
for(int idx=0; idx<maxEntries && address[idx]!=ptr; idx++);
if(idx<maxEntries)
{
address[idx]=NULL;
population--;
}
}
}
// *****************************************************************************
// * AddressIndex::remove :
// * This function will locate a specific entry based on the long integer
// * identifier. If the entry is valid and has a matching checksum, then
// * it will be removed from the list.
// *****************************************************************************
inline void AddressIndex::remove ( unsigned idx )
{
AddressReference ref;
ref.rawData = idx;
if(ref.value.index<maxEntries &&
ref.value.checksum==checksum[ref.value.index])
{
address[ref.value.index] = NULL;
population--;
}
}
// *****************************************************************************
// * AddressIndex::find :
// * This function will locate an entry within the list using a pointer to
// * its memory address and will return an unsigned integer indicating its
// * location. If the returned bvalue is 0, then the ptr was not found...
// *****************************************************************************
inline unsigned AddressIndex::find ( void * ptr )
{
AddressReference result;
int idx;
result.rawData = 0U;
for(idx=0; idx<maxEntries && address[idx]!=ptr; idx++);
if(idx<maxEntries)
{
result.value.index =idx;
result.value.checksum = checksum[idx];
}
return result.rawData;
}
// *****************************************************************************
// * AddressIndex::find :
// * This function will obtain a pointer to the data item using the user
// * provided unsigned index. A value of NULL will be returned if the
// * data item could not be located or if the unsigned index was invalid.
// *****************************************************************************
inline void * AddressIndex::find ( unsigned idx )
{
void * result = NULL;
AddressReference ref;
ref.rawData = idx;
if(ref.value.index<maxEntries &&
ref.value.checksum==checksum[ref.value.index])
{
result = address[ref.value.index];
}
return result;
}
// *****************************************************************************
// * AddressIndexIterator::AddressIndexIterator:
// * Simple constructor for the AddressIndexIterator class.
// *****************************************************************************
inline AddressIndexIterator::AddressIndexIterator (AddressIndex * Index)
: index(Index), current (0)
{}
// *****************************************************************************
// * AddressIndexIterator::~AddressIndexIterator :
// * Destructor for the AddressIndexIterator... does nothing.
// *****************************************************************************
inline AddressIndexIterator::~AddressIndexIterator( void ) {}
// *****************************************************************************
// * AddressIndexIterator::first :
// * Repositions to the first entry in the AddressIndex.
// *****************************************************************************
inline void * AddressIndexIterator::first( void )
{
current = 0;
while(index->population &&
current<index->maxEntries &&
index->address[current]==NULL)
{
current++;
}
return (index->population && current<index->maxEntries)?index->address[current]:NULL;
}
// *****************************************************************************
// * AddressIndexIterator::operator ++ :
// * Repositions to the next entry in the AddressIndex.
// *****************************************************************************
inline void * AddressIndexIterator::operator ++ ( void )
{
current++;
while(index->population &&
current<index->maxEntries &&
index->address[current]==NULL)
{
current++;
}
return (index->population && current<index->maxEntries)?index->address[current]:NULL;
}
// *****************************************************************************
// * AddressIndexIterator::operator ++ :
// * Repositions to the next entry in the AddressIndex.
// *****************************************************************************
inline void * AddressIndexIterator::operator ++ ( int x )
{
current++;
while(x==0 &&
index->population &&
current<index->maxEntries &&
index->address[current]==NULL)
{
current++;
}
return (x==0 && index->population && current<index->maxEntries)?index->address[current]:NULL;
}
// *****************************************************************************
// * AddressIndexIterator::key :
// * This method will return the AddressIndex key value of the current entry.
// *****************************************************************************
inline unsigned AddressIndexIterator::key ( void )
{
AddressReference result;
result.rawData = 0;
if(current<index->maxEntries && index->address[current]!=NULL)
{
result.value.index = current;
result.value.checksum = index->checksum[current];
}
return result.rawData;
}
// *****************************************************************************
// * AddressIndexIterator::data :
// * This method will return the data value of the current entry.
// *****************************************************************************
inline void * AddressIndexIterator::data ( void )
{
return(current<index->maxEntries)?index->address[current]:NULL;
}
#endif /* ADDRESS_INDEX_H_ */
+355
View File
@@ -0,0 +1,355 @@
#include <cdevPlatforms.h>
#include <cdevData.h>
#include <time.h>
class ClientInfoStruct
{
private:
char unknown[8];
char * username;
char * group;
unsigned uid;
unsigned gid;
unsigned pid;
char * program;
char * commandline;
time_t starttime;
time_t connecttime;
char * host;
char * os;
char * osrelease;
char * osversion;
char * machine;
char * shell;
private:
void updateDataString ( cdevData & data, char * tag, char * &item )
{
size_t nelems = 0;
if(data.getElems(tag, &nelems)==CDEV_SUCCESS &&
nelems==1 && data.getType(tag)==CDEV_STRING)
{
if(item!=NULL) delete item;
item = NULL;
data.get(tag, &item);
}
}
void updateDataInt ( cdevData & data, char * tag, unsigned &item )
{
size_t nelems = 0;
if(data.getElems(tag, &nelems)==CDEV_SUCCESS && nelems==1)
{
data.get(tag, &item);
}
}
public:
ClientInfoStruct ( void )
: username(NULL), group(NULL), uid(0), gid(0), pid(0),
program(NULL), commandline(NULL), starttime(0),
connecttime(0), host(NULL), os(NULL), osrelease(NULL),
osversion(NULL), machine(NULL), shell(NULL)
{
strcpy(unknown, "UNKNOWN");
}
~ClientInfoStruct ( void )
{
if(username) delete username;
if(group) delete group;
if(program) delete program;
if(commandline) delete commandline;
if(host) delete host;
if(os) delete os;
if(osrelease) delete osrelease;
if(osversion) delete osversion;
if(machine) delete machine;
if(shell) delete shell;
}
void updateClientInfo ( cdevData & data )
{
updateDataString(data, "username", username);
updateDataString(data, "group", group);
updateDataInt (data, "uid", uid);
updateDataInt (data, "gid", gid);
updateDataInt (data, "pid", pid);
updateDataString(data, "program", program);
updateDataString(data, "commandline", commandline);
updateDataInt (data, "starttime", (unsigned &)starttime);
updateDataInt (data, "connecttime", (unsigned &)connecttime);
updateDataString(data, "host", host);
updateDataString(data, "os", os);
updateDataString(data, "osrelease", osrelease);
updateDataString(data, "osversion", osversion);
updateDataString(data, "machine", machine);
updateDataString(data, "shell", shell);
connecttime = time(NULL);
}
char * getUsername ( void ) { return username?username:unknown; }
char * getGroup ( void ) { return group?group:unknown; }
unsigned getUid ( void ) { return uid; }
unsigned getGid ( void ) { return gid; }
unsigned getPid ( void ) { return pid; }
char * getProgram ( void ) { return program?program:unknown; }
char * getCommandLine ( void ) { return commandline?commandline:unknown; }
time_t getStartTime ( void ) { return starttime; }
time_t getConnectTime ( void ) { return connecttime; }
char * getHost ( void ) { return host?host:unknown; }
char * getOs ( void ) { return os?os:unknown; }
char * getOsRelease ( void ) { return osrelease?osrelease:unknown; }
char * getOsVersion ( void ) { return osversion?osversion:unknown; }
char * getMachine ( void ) { return machine?machine:unknown; }
char * getShell ( void ) { return shell?shell:unknown; }
void asciiDump ( FILE * fp = stdout )
{
fprintf(fp, "--------------------------------------------------------\n");
fprintf(fp, " Printing Client Information\n");
fprintf(fp, "--------------------------------------------------------\n");
fprintf(fp, "=> USERNAME : %s\n", username?username:"UNDEFINED");
fprintf(fp, "=> GROUP : %s\n", group?group:"UNDEFINED");
fprintf(fp, "=> USER ID : %s\n", uid?ltoa(uid):"UNDEFINED");
fprintf(fp, "=> GROUP ID : %s\n", gid?ltoa(gid):"UNDEFINED");
fprintf(fp, "=> PROCESS ID : %s\n", pid?ltoa(pid):"UNDEFINED");
fprintf(fp, "=> APPLICATION : %s\n", program?program:"UNDEFINED");
fprintf(fp, "=> COMMAND LINE : %s\n", commandline?commandline:"UNDEFINED");
fprintf(fp, "=> START TIME : %s", starttime?ctime(&starttime):"UNKNOWN\n");
fprintf(fp, "=> CONNECT TIME : %s", connecttime?ctime(&connecttime):"UNKNOWN\n");
fprintf(fp, "=> HOST NAME : %s\n", host?host:"UNKNOWN");
fprintf(fp, "=> OS : %s\n", os?os:"UNKNOWN");
fprintf(fp, "=> OS VERSION : %s\n", osversion?osversion:"UNKNOWN");
fprintf(fp, "=> OS RELEASE : %s\n", osrelease?osrelease:"UNKNOWN");
fprintf(fp, "=> HARDWARE ID : %s\n", machine?machine:"UNKNOWN");
fprintf(fp, "--------------------------------------------------------\n");
}
};
class ClientInfo
{
private:
cdevData data;
public:
ClientInfo ( void )
{
cdevData::addTag("username");
cdevData::addTag("group");
cdevData::addTag("uid");
cdevData::addTag("gid");
cdevData::addTag("pid");
cdevData::addTag("program");
cdevData::addTag("commandline");
cdevData::addTag("starttime");
cdevData::addTag("connecttime");
cdevData::addTag("host");
cdevData::addTag("os");
cdevData::addTag("osrelease");
cdevData::addTag("osversion");
cdevData::addTag("machine");
cdevData::addTag("shell");
cdevData::addTag("sendPktCnt");
cdevData::addTag("recvPktCnt");
cdevData::addTag("socket");
#ifndef _WIN32
struct utsname hostinfo;
struct group * grp;
char * shell = getenv("SHELL");
uname(&hostinfo);
grp = getgrgid(getgid());
data.insert("username", getlogin());
data.insert("group", grp?grp->gr_name:(char *)"UNDEFINED");
data.insert("uid", (unsigned)getuid());
data.insert("gid", (unsigned)getgid());
data.insert("pid", (unsigned)getpid());
data.insert("host", hostinfo.nodename);
data.insert("os", hostinfo.sysname);
data.insert("osrelease", hostinfo.release);
data.insert("osversion", hostinfo.version);
data.insert("machine", hostinfo.machine);
data.insert("shell", shell?shell:(char *)"UNDEFINED");
#if defined(__hpux)
struct pst_status pstatData;
pstat_getproc(&pstatData, sizeof(pstatData), 0, (int)getpid());
data.insert("program", pstatData.pst_ucomm);
data.insert("commandline", pstatData.pst_cmd);
data.insert("starttime", pstatData.pst_start);
#endif
#else
char userNameBuf[UNLEN+1];
unsigned long userNameLen = UNLEN;
char hostNameBuf[MAXHOSTNAMELEN+1];
unsigned long hostNameLen = MAXHOSTNAMELEN;
*userNameBuf = 0;
*hostNameBuf = 0;
data.insert("pid", GetCurrentProcessId());
if(GetUserName (userNameBuf, &userNameLen)) data.insert("username", userNameBuf);
if(GetComputerName(hostNameBuf, &hostNameLen)) data.insert("host", hostNameBuf);
if(userNameBuf && hostNameBuf)
{
char grpNameBuf[GNLEN+1];
unsigned long grpNameLen = GNLEN;
char sidBuf[256];
unsigned long sidLen = 256;
SID_NAME_USE sidType;
*grpNameBuf = 0;
if(LookupAccountName(hostNameBuf, userNameBuf,
sidBuf, &sidLen, grpNameBuf,
&grpNameLen, &sidType))
{
data.insert("group", grpNameBuf);
}
}
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx (&osv))
{
char osVersionBuf[64];
switch(osv.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
data.insert("os", "WINDOWS");
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(osv.dwMinorVersion == 0)
data.insert("os", "WINDOWS 95");
else data.insert("os", "WINDOWS 98");
break;
case VER_PLATFORM_WIN32_NT:
data.insert("os", "WINDOWS NT");
break;
default:
data.insert("os", "WIN32");
break;
}
sprintf(osVersionBuf, "%d.%d", osv.dwMajorVersion, osv.dwMinorVersion);
data.insert("osversion", osVersionBuf);
data.insert("osrelease", ltoa(osv.dwBuildNumber));
}
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
switch(sysinfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_INTEL:
data.insert("machine", "PROCESSOR_ARCHITECTURE_INTEL");
break;
case PROCESSOR_ARCHITECTURE_MIPS:
data.insert("machine", "PROCESSOR_ARCHITECTURE_MIPS");
break;
case PROCESSOR_ARCHITECTURE_ALPHA:
data.insert("machine", "PROCESSOR_ARCHITECTURE_ALPHA");
break;
case PROCESSOR_ARCHITECTURE_PPC:
data.insert("machine", "PROCESSOR_ARCHITECTURE_PPC");
break;
case PROCESSOR_ARCHITECTURE_UNKNOWN:
default:
data.insert("machine", "PROCESSOR_ARCHITECTURE_UNKNOWN");
break;
}
if(__argc>0)
{
int i;
char * cmdLine = NULL;
size_t cmdLineLen = 0;
data.insert("program", __argv[0]);
for(i=0; i<__argc; i++) cmdLineLen+=strlen(__argv[i])+1;
cmdLine = new char [cmdLineLen];
*cmdLine = 0;
for(i=0; i<__argc; i++)
{
strcat(cmdLine, __argv[i]);
if(i+1<__argc) strcat(cmdLine, " ");
}
data.insert("commandline", cmdLine);
}
FILETIME startTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
if(GetProcessTimes(GetCurrentProcess(), &startTime, &exitTime, &kernelTime, &userTime))
{
struct timeval tv;
FileTimeToTimeVal(&startTime, &tv);
data.insert("starttime", tv.tv_sec);
}
#endif
}
cdevData & getClientData ( void ) { return data; }
void asciiDump ( FILE * fp = stdout )
{
fprintf(fp, "--------------------------------------------------------\n");
fprintf(fp, " Printing Client Information\n");
fprintf(fp, "--------------------------------------------------------\n");
char * username;
char * group;
unsigned pid;
unsigned gid;
unsigned uid;
char * host;
char * os;
char * osrelease;
char * osversion;
char * machine;
char * shell;
char * program;
char * commandline;
long starttime;
if(data.find("username", (void * &)username)!=CDEV_SUCCESS) username = NULL;
if(data.find("group", (void * &)group)!=CDEV_SUCCESS) group = NULL;
if(data.find("host", (void * &)host)!=CDEV_SUCCESS) host = NULL;
if(data.find("os", (void * &)os)!=CDEV_SUCCESS) os = NULL;
if(data.find("osrelease", (void * &)osrelease)!=CDEV_SUCCESS) osrelease = NULL;
if(data.find("osversion", (void * &)osversion)!=CDEV_SUCCESS) osversion = NULL;
if(data.find("machine", (void * &)machine)!=CDEV_SUCCESS) machine = NULL;
if(data.find("shell", (void * &)shell)!=CDEV_SUCCESS) shell=NULL;
if(data.find("program", (void * &)program)!=CDEV_SUCCESS) program=NULL;
if(data.find("commandline", (void * &)commandline)!=CDEV_SUCCESS) commandline=NULL;
if(data.get("starttime", &starttime)!=CDEV_SUCCESS) starttime = 0;
if(data.get("pid", &pid)!=CDEV_SUCCESS) pid = 0;
if(data.get("gid", &gid)!=CDEV_SUCCESS) gid = 0;
if(data.get("uid", &uid)!=CDEV_SUCCESS) uid = 0;
fprintf(fp, "=> USERNAME : %s\n", username?username:"UNDEFINED");
fprintf(fp, "=> GROUP : %s\n", group?group:"UNDEFINED");
fprintf(fp, "=> USER ID : %s\n", uid?ltoa(uid):"UNDEFINED");
fprintf(fp, "=> GROUP ID : %s\n", gid?ltoa(gid):"UNDEFINED");
fprintf(fp, "=> PROCESS ID : %s\n", pid?ltoa(pid):"UNDEFINED");
fprintf(fp, "=> APPLICATION : %s\n", program?program:"UNDEFINED");
fprintf(fp, "=> COMMAND LINE : %s\n", commandline?commandline:"UNDEFINED");
fprintf(fp, "=> START TIME : %s", starttime?ctime(&starttime):"UNKNOWN\n");
fprintf(fp, "=> HOST NAME : %s\n", host?host:"UNKNOWN");
fprintf(fp, "=> OS : %s\n", os?os:"UNKNOWN");
fprintf(fp, "=> OS VERSION : %s\n", osversion?osversion:"UNKNOWN");
fprintf(fp, "=> OS RELEASE : %s\n", osrelease?osrelease:"UNKNOWN");
fprintf(fp, "=> HARDWARE ID : %s\n", machine?machine:"UNKNOWN");
fprintf(fp, "--------------------------------------------------------\n");
}
};
+19
View File
@@ -0,0 +1,19 @@
#include <ClientInfo.h>
#include <ServerInfo.h>
ClientInfo info;
ServerInfo sInfo("MODEL", "MODEL1", 32526);
ClientInfoStruct infoStruct;
int main()
{
sInfo.asciiDump();
info.asciiDump();
infoStruct.updateClientInfo(info.getClientData());
infoStruct.updateClientInfo(info.getClientData());
infoStruct.updateClientInfo(info.getClientData());
infoStruct.updateClientInfo(info.getClientData());
infoStruct.asciiDump();
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
#ifndef _ERROR_REPORTER_H_
#define _ERROR_REPORTER_H_ 1
#include <cdevPlatforms.h>
#ifndef _NO_CDEV_SYSTEM_
#include <cdevSystem.h>
#include <cdevErrCode.h>
#else
#include <stdarg.h>
#ifndef CDEV_SEVERITY_INFO /* cdevError class severity codes */
#define CDEV_SEVERITY_INFO 0 /* informative message */
#define CDEV_SEVERITY_WARN 1 /* warning message */
#define CDEV_SEVERITY_ERROR 2 /* error message */
#define CDEV_SEVERITY_SEVERE 3 /* severe or fatal error message */
#endif
#endif
class GENERIC_SERVER_API ErrorReporter
{
public:
ErrorReporter ( void ) {}
virtual ~ErrorReporter ( void ) {}
virtual int outputError ( int severity, char *name, char *formatString, ... )
{
int result = 0;
va_list argp;
va_start (argp, formatString);
#ifndef _NO_CDEV_SYSTEM_
result = cdevSystem::defaultSystem().vreportError(severity, name, NULL, formatString, argp);
#else
fprintf(stdout,"%s %s: ",
name,
(severity==CDEV_SEVERITY_INFO?"Information":
(severity==CDEV_SEVERITY_WARN?"Warning":
(severity==CDEV_SEVERITY_ERROR?"Error":
(severity==CDEV_SEVERITY_SEVERE?"Severe Error":
"Event")))));
vfprintf(stdout, formatString, argp);
fprintf (stdout, "\n");
#endif
va_end(argp);
return result;
}
};
#endif /* _ERROR_REPORTER_H_ */
+192
View File
@@ -0,0 +1,192 @@
#ifndef _FD_TRIGGER_H_
#define _FD_TRIGGER_H_ 1
#include <cdevPlatforms.h>
// *****************************************************************************
// * FD_Trigger :
// * This class provides a method for triggering events based on file
// * descriptor activity.
// *****************************************************************************
class FD_Trigger
{
private:
enum {READ=0, WRITE=1};
int sockPair[2];
#ifndef _WIN32
char cbuf[100];
#endif
public:
FD_Trigger ( void );
~FD_Trigger ( void );
int insertEvent ( int numEvents = 1 );
int removeEvent ( int numEvents = 1 );
void purge ( void );
int writefd ( void ) const;
int readfd ( void ) const;
};
// *****************************************************************************
// * FD_Trigger::FD_Trigger :
// * Constructor for the FD_Trigger class.
// *****************************************************************************
inline FD_Trigger::FD_Trigger ( void )
{
if(pipe(sockPair)!=0)
{
sockPair[READ] = -1;
sockPair[WRITE] = -1;
}
#ifndef _WIN32
else
{
int val;
val = ::fcntl(sockPair[READ], F_GETFL, 0);
if(val>0) ::fcntl(sockPair[READ], F_SETFL, val|O_NONBLOCK);
val = ::fcntl(sockPair[WRITE], F_GETFL, 0);
if(val>0) ::fcntl(sockPair[WRITE], F_SETFL, val|O_NONBLOCK);
memset(cbuf, '1', 100);
}
#endif
}
// *****************************************************************************
// * FD_Trigger::~FD_Trigger :
// * Destructor for the FD_Trigger class.
// *****************************************************************************
inline FD_Trigger::~FD_Trigger ( void )
{
if(sockPair[READ] != -1) close(sockPair[READ]);
if(sockPair[WRITE] != -1) close(sockPair[WRITE]);
}
// *****************************************************************************
// * FD_Trigger::insertEvent :
// * Adds one or more bytes (indicating events) to the pipe
// *****************************************************************************
inline int FD_Trigger::insertEvent ( int numEvents )
{
if(sockPair[WRITE]>0)
{
int count = 0;
#ifdef _WIN32
char cptr = 1;
while(count++<numEvents) send(sockPair[WRITE], &cptr, 1, 0);
#else
count = numEvents;
while(count>0)
{
write(sockPair[WRITE], cbuf, min(100, count));
count-=100;
}
#endif
}
return (sockPair[WRITE]>0)?0:-1;
}
// *****************************************************************************
// * FD_Trigger::removeEvent :
// * Removes one or more bytes (indicating events) from the pipe.
// *****************************************************************************
inline int FD_Trigger::removeEvent ( int numEvents )
{
int retval = 0;
if(sockPair[READ]>0)
{
int count = 0;
#ifdef _WIN32
char cptr = 0;
while(count<numEvents &&
(recv(sockPair[READ], &cptr, 1, 0)>0 ||
WSAGetLastError()==WSAEMSGSIZE))
{
count++;
}
if(count==0) retval = -1;
#else
if(ioctl(sockPair[READ], FIONREAD, &count)<0 || count==0)
{
retval = -1;
}
else
{
if(numEvents>count) numEvents = count;
while(numEvents>0)
{
read(sockPair[READ], cbuf, min(100, numEvents));
numEvents-=100;
}
}
#endif
}
else retval = -1;
return retval;
}
// *****************************************************************************
// * FD_Trigger::purge :
// * This function removes all bytes from the pipe.
// *****************************************************************************
inline void FD_Trigger::purge ( void )
{
if(sockPair[READ]>0)
{
#ifdef _WIN32
char cptr = 0;
while(recv(sockPair[READ], &cptr, 1, 0)>0 || WSAGetLastError()==WSAEMSGSIZE);
#else
int count = 0;
ioctl(sockPair[READ], FIONREAD, &count);
while(count>0)
{
read(sockPair[READ], cbuf, min(100, count));
count-=100;
}
#endif
}
}
// *****************************************************************************
// * FD_Trigger::writefd
// * Returns the write file descriptor associated with the pipe.
// *****************************************************************************
inline int FD_Trigger::writefd ( void ) const
{
return sockPair[WRITE];
}
// *****************************************************************************
// * FD_Trigger::readfd
// * Returns the read file descriptor associated with the pipe.
// *****************************************************************************
inline int FD_Trigger::readfd ( void ) const
{
return sockPair[READ];
}
#endif /* _FD_TRIGGER_H_ */
@@ -0,0 +1,104 @@
#include <FD_Trigger.h>
char * resultString[] =
{
"SUCCEEDED",
"FAILED"
};
int main ()
{
#ifdef _WIN32
InitializeNetwork();
#endif
FD_Trigger trigger;
int i;
int cnt;
int singleInsertResult = 0;
int multiInsertResult = 0;
int singleExtractResult = 0;
int multiExtractResult = 0;
int purgeResult = 0;
fprintf(stdout, "Inserting Twenty Single Events\n");
for(i=0, cnt=0; i<20; i++)
{
if(trigger.insertEvent()!=0)
{
fprintf(stdout, "Error inserting event %i\n", i);
singleInsertResult = 1;
}
else cnt++;
}
fprintf(stdout, "%i Events Successfully Inserted - %i Failed\n", cnt, 20-cnt);
fprintf(stdout, "Extracting Each Event\n");
for(cnt=0; trigger.removeEvent()==0 && cnt<500; cnt++);
if(cnt!=20) singleExtractResult = 1;
fprintf(stdout, "Extracted %i Events\n", cnt);
fprintf(stdout, "Inserting Twenty Quad Events\n");
for(i=0, cnt=0; i<20; i++)
{
if(trigger.insertEvent(4)!=0)
{
fprintf(stdout, "Error inserting event %i\n", i);
multiInsertResult = 1;
}
else cnt++;
}
fprintf(stdout, "%i Quad Events Successfully Inserted - %i Failed\n", cnt, 20-cnt);
fprintf(stdout, "Extracting Each Event\n");
for(cnt=0; trigger.removeEvent()==0 && cnt<500; cnt++);
if(cnt!=80) singleExtractResult = 1;
fprintf(stdout, "Extracted %i Events\n", cnt);
fprintf(stdout, "Inserting Twenty Single Events\n");
for(i=0, cnt=0; i<20; i++)
{
if(trigger.insertEvent()!=0)
{
fprintf(stdout, "Error inserting event %i\n", i);
singleInsertResult = 1;
}
else cnt++;
}
fprintf(stdout, "%i Events Successfully Inserted - %i Failed\n", cnt, 20-cnt);
fprintf(stdout, "Extracting Each Quad Event\n");
for(cnt=0; trigger.removeEvent(4)==0 && cnt<500; cnt++);
if(cnt!=5) multiExtractResult = 1;
fprintf(stdout, "Extracted %i Quad Events\n", cnt);
fprintf(stdout, "Inserting Twenty Single Events\n");
for(i=0, cnt=0; i<20; i++)
{
if(trigger.insertEvent()!=0)
{
fprintf(stdout, "Error inserting event %i\n", i);
}
else cnt++;
}
fprintf(stdout, "%i Events Successfully Inserted - %i Failed\n", cnt, 20-cnt);
fprintf(stdout, "Purging Trigger\n");
trigger.purge();
fprintf(stdout, "Extracting Each Remaining Event\n");
for(cnt=0; trigger.removeEvent()==0 && cnt<500; cnt++);
if(cnt!=0) purgeResult = 1;
fprintf(stdout, "Extracted %i Events\n\n", cnt);
fprintf(stdout, "RESULTS: \n");
fprintf(stdout, "\tInsertion of single events %s\n", resultString[singleInsertResult]);
fprintf(stdout, "\tInsertion of multiple events %s\n", resultString[multiInsertResult]);
fprintf(stdout, "\tExtraction of single events %s\n", resultString[singleExtractResult]);
fprintf(stdout, "\tExtraction of multiple events %s\n", resultString[multiExtractResult]);
fprintf(stdout, "\tPurging of multiple events %s\n", resultString[purgeResult]);
#ifdef _WIN32
TerminateNetwork();
#endif
return 0;
}
@@ -0,0 +1,13 @@
#include <IntHash.h>
IntHashNode::IntHashNode ( int HashInt, void * HashData)
: next(NULL)
{
hashInt = HashInt;
hashData = HashData;
}
IntHashNode::~IntHashNode ( void )
{
}
+204
View File
@@ -0,0 +1,204 @@
#ifndef _INT_HASH_H_
#define _INT_HASH_H_ 1
#include <cdevPlatforms.h>
class GENERIC_SERVER_API IntHashNode
{
friend class IntHash;
friend class IntHashIterator;
private:
int hashInt;
void * hashData;
IntHashNode * next;
IntHashNode ( int HashInt, void * HashData);
~IntHashNode ( void );
};
class GENERIC_SERVER_API IntHash
{
friend class IntHashIterator;
private:
enum { HASH_CNT=256, HASH_NO=256 };
IntHashNode * nodes[HASH_CNT];
public:
IntHash ( void );
~IntHash ( void );
inline unsigned char hash ( int hashInt );
inline void insert ( int hashInt, void * hashData );
inline void remove ( int hashInt );
inline void * find ( int hashInt );
};
class GENERIC_SERVER_API IntHashIterator
{
private:
IntHash * hashTbl;
IntHashNode * node;
int idx;
public:
IntHashIterator( IntHash * HashTbl );
~IntHashIterator( void );
inline void * first ( void );
inline void * operator ++ ( void );
inline void * operator ++ ( int x );
inline int key ( void );
inline void * data ( void );
};
inline IntHash::IntHash ( void )
{
memset(nodes, 0, sizeof(nodes));
}
inline IntHash::~IntHash ( void )
{
for(int i=0; i<HASH_CNT; i++)
{
while(nodes[i]!=NULL)
{
IntHashNode * node = nodes[i];
nodes[i] = node->next;
delete node;
}
}
}
inline unsigned char IntHash::hash ( int hashInt )
{
return (hashInt%HASH_NO);
}
inline void IntHash::insert (int hashInt, void * hashData )
{
unsigned char idx = hash(hashInt);
IntHashNode * prev = NULL, * node = nodes[idx];
IntHashNode * newNode = new IntHashNode(hashInt, hashData);
while(node!=NULL && node->hashInt!=hashInt)
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
newNode->next = node->next;
delete node;
}
if(prev!=NULL) prev->next = newNode;
else nodes[idx] = newNode;
}
inline void IntHash::remove ( int hashInt )
{
unsigned char idx = hash(hashInt);
IntHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && node->hashInt!=hashInt)
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
if(prev!=NULL) prev->next = node->next;
else nodes[idx] = node->next;
delete node;
}
}
inline void * IntHash::find ( int hashInt )
{
unsigned char idx = hash(hashInt);
IntHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && node->hashInt!=hashInt)
{
prev = node;
node = prev->next;
}
return node!=NULL?node->hashData:NULL;
}
inline IntHashIterator::IntHashIterator(IntHash * HashTbl)
: hashTbl(HashTbl), idx(0), node(NULL)
{
}
inline IntHashIterator::~IntHashIterator( void )
{
}
inline void * IntHashIterator::first ( void )
{
if(hashTbl!=NULL)
{
for(idx = 0; idx<IntHash::HASH_CNT &&
(node = hashTbl->nodes[idx])==NULL; idx++);
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * IntHashIterator::operator ++ ( void )
{
if(hashTbl!=NULL)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<IntHash::HASH_CNT &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * IntHashIterator::operator ++ ( int x )
{
if(hashTbl!=NULL && x==0)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<IntHash::HASH_CNT &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline int IntHashIterator::key ( void )
{
return (node!=NULL)?node->hashInt:0;
}
inline void * IntHashIterator::data ( void )
{
return (node!=NULL)?node->hashData:NULL;
}
#endif /* _INT_HASH_H_ */
File diff suppressed because it is too large Load Diff
+15
View File
@@ -0,0 +1,15 @@
ARCH = OS
SHOBJ = YES
include ../include/makeinclude/Makefile.$(ARCH)
APPNAME = Signal Manager Source
CXXINCLUDES = -I./
LIBS = $(CDEVLIBS) $(OSLIBS)
TARGETS = $(BASEBIN)/SignalManagerTest
targets: $(TARGETS)
$(BASEBIN)/SignalManagerTest: $(OBJDIR)/SignalManager.o $(OBJDIR)/SignalManagerTest.o $(OBJDIR)/ErrorReporter.obj
$(LINK.cc) $^ -o $@ $(LIBS)
+192
View File
@@ -0,0 +1,192 @@
#ifndef __MULTIQUEUE_H_
#define __MULTIQUEUE_H_ 1
#include <stdlib.h>
#include <string.h>
class MultiQueueBase
{
private:
int nodeCount;
public:
MultiQueueBase ( void ) { nodeCount = 0; }
virtual void setTail ( void *tail ) = 0;
virtual int getCount ( void ) { return nodeCount; }
virtual int setCount ( int cnt ) { return (nodeCount = (cnt>=0?cnt:0)); }
virtual int incCount ( void ) { return (++nodeCount); }
virtual int decCount ( void ) { return (nodeCount=(nodeCount>0?nodeCount-1:0)); }
};
template <unsigned queueCount> class MultiQueueNode
{
private:
char * binary;
unsigned binaryLen;
public:
class MultiQueueBase *queue[queueCount];
class MultiQueueNode<queueCount> *prev [queueCount];
class MultiQueueNode<queueCount> *next [queueCount];
MultiQueueNode(char * Binary=NULL, size_t BinaryLen=0)
: binary(Binary), binaryLen(BinaryLen)
{
memset(queue, 0, sizeof(class MultiQueueBase *)*queueCount);
memset(prev, 0, sizeof(class MultiQueueNode<queueCount> *)*queueCount);
memset(next, 0, sizeof(class MultiQueueNode<queueCount> *)*queueCount);
}
virtual ~MultiQueueNode(void)
{
binary = NULL;
binaryLen = 0;
clear();
}
virtual void set ( char * Binary, size_t BinaryLen )
{
binary = Binary;
binaryLen = BinaryLen;
}
virtual void get ( char ** Binary, size_t * BinaryLen )
{
*Binary = binary;
*BinaryLen = binaryLen;
}
virtual void clear ( void )
{
for(int i=0; i<queueCount; i++)
{
if(prev[i]) prev [i]->next[i] = next[i];
if(next[i]) next [i]->prev[i] = prev[i];
else if(queue[i]) queue[i]->setTail(prev[i]);
if(queue[i]) queue[i]->decCount();
queue[i]=NULL;
prev[i] =NULL;
next[i] =NULL;
}
}
};
template <unsigned queueIndex, unsigned queueCount> class MultiQueue
: public MultiQueueBase
{
private:
MultiQueueNode<queueCount> head;
MultiQueueNode<queueCount> * tail;
public:
MultiQueue ( void )
{
tail = NULL;
}
virtual ~MultiQueue ( void )
{
MultiQueueNode<queueCount> * node;
while((node=dequeue())!=NULL) delete node;
}
virtual void setTail ( void * Tail )
{
tail = (MultiQueueNode<queueCount> *)Tail;
}
virtual int empty ( void ) { return (head.next[queueIndex]==NULL?1:0); }
virtual void enqueue ( MultiQueueNode<queueCount> * node )
{
if(node!=NULL)
{
if(head.next[queueIndex]==NULL)
{
head.next[queueIndex] = node;
node->prev[queueIndex] = &head;
}
else
{
tail->next[queueIndex] = node;
node->prev[queueIndex] = tail;
}
tail = node;
node->queue[queueIndex] = this;
node->queue[queueIndex]->incCount();
}
}
virtual MultiQueueNode<queueCount> * peek ( void )
{
return head.next[queueIndex];
}
virtual MultiQueueNode<queueCount> * dequeue ( void )
{
MultiQueueNode<queueCount> * node = head.next[queueIndex];
if(node!=NULL) node->clear();
return node;
}
virtual int peek ( char ** binary, size_t * binaryLen )
{
int result = -1;
MultiQueueNode<queueCount> * node;
if((node = peek())!=NULL)
{
node->get(binary, binaryLen);
if(*binary!=NULL && *binaryLen!=0) result = 0;
}
return result;
}
virtual int dequeue ( char ** binary, size_t * binaryLen )
{
int result = -1;
MultiQueueNode<queueCount> * node;
*binary = NULL;
*binaryLen = 0;
if((node = dequeue())!=NULL)
{
node->get(binary, binaryLen);
if(*binary!=NULL && *binaryLen!=0) result = 0;
delete node;
}
return result;
}
virtual MultiQueueNode<queueCount> * localDequeue ( void )
{
MultiQueueNode<queueCount> * node = head.next[queueIndex];
if(node!=NULL)
{
if(node->prev[queueIndex]!=NULL)
node->prev[queueIndex]->next[queueIndex] = node->next[queueIndex];
if(node->next[queueIndex]!=NULL)
node->next[queueIndex]->prev[queueIndex] = node->prev[queueIndex];
else if(node->queue[queueIndex]!=NULL)
{
node->queue[queueIndex]->setTail(node->prev);
node->queue[queueIndex]->decCount();
}
node->queue[queueIndex]=NULL;
node->next [queueIndex]=NULL;
node->prev [queueIndex]=NULL;
}
return node;
}
};
#endif /* __MULTIQUEUE_H_ */
@@ -0,0 +1,42 @@
.SUFFIXES: .cc .obj
APPNAME = Common Source
ARCH = WINNT-4.0
BINARIES = $(BASEBIN)\SignalManagerTest.exe $(BASEBIN)\ClientInfoTest.exe $(BASEBIN)\FD_TriggerTest.exe $(BASEBIN)\includeAllTest.exe
include ..\include\makeinclude\Makefile.WINNT-4.0
CXXEXTRA = /D "GENERIC_SERVER_API= "
CXXINCLUDES = /I .\\
TARGETS = $(BASEBIN)\SignalManagerTest.exe $(BASEBIN)\ClientInfoTest.exe $(BASEBIN)\FD_TriggerTest.exe $(BASEBIN)\includeAllTest.exe
targets : $(TARGETS)
$(BASEBIN)\SignalManagerTest.exe : .exec\$(TARGETDIR)\SignalManager.obj .exec\$(TARGETDIR)\SignalManagerTest.obj
-@if exist $@ erase $@
@echo ^ ^ ^ ^ ^ ^ =^> Linking $(@F)
$(LINK) $(CDEVLIB)\cdev.lib\
$(LINK_EXE_FLAGS) /out:$@ $?
@echo ^ ^ ^ ^ ^ ^ ^ ^ ^ Done...
$(BASEBIN)\ClientInfoTest.exe : .exec\$(TARGETDIR)\ClientInfoTest.obj
-@if exist $@ erase $@
@echo ^ ^ ^ ^ ^ ^ =^> Linking $(@F)
$(LINK) $(CDEVLIB)\cdev.lib\
$(LINK_EXE_FLAGS) /out:$@ $?
@echo ^ ^ ^ ^ ^ ^ ^ ^ ^ Done...
$(BASEBIN)\FD_TriggerTest.exe : .exec\$(TARGETDIR)\FD_TriggerTest.obj
-@if exist $@ erase $@
@echo ^ ^ ^ ^ ^ ^ =^> Linking $(@F)
$(LINK) $(CDEVLIB)\cdev.lib\
$(LINK_EXE_FLAGS) /out:$@ $?
@echo ^ ^ ^ ^ ^ ^ ^ ^ ^ Done...
$(BASEBIN)\includeAllTest.exe : .exec\$(TARGETDIR)\includeAll.obj
-@if exist $@ erase $@
@echo ^ ^ ^ ^ ^ ^ =^> Linking $(@F)
$(LINK) $(CDEVLIB)\cdev.lib\
$(LINK_EXE_FLAGS) /out:$@ $?
@echo ^ ^ ^ ^ ^ ^ ^ ^ ^ Done...
+280
View File
@@ -0,0 +1,280 @@
#include <cdevPlatforms.h>
#include <cdevData.h>
class ServerInfo
{
friend class cdevServer;
private:
cdevData data;
size_t sendPktCnt;
size_t recvPktCnt;
size_t clientCnt;
long pageSize;
public:
ServerInfo ( char * domain, char * server, unsigned short port )
: sendPktCnt(0), recvPktCnt(0), clientCnt(0), pageSize(0)
{
cdevData::addTag("username");
cdevData::addTag("group");
cdevData::addTag("uid");
cdevData::addTag("gid");
cdevData::addTag("pid");
cdevData::addTag("program");
cdevData::addTag("commandline");
cdevData::addTag("starttime");
cdevData::addTag("host");
cdevData::addTag("server");
cdevData::addTag("domain");
cdevData::addTag("port");
cdevData::addTag("os");
cdevData::addTag("osrelease");
cdevData::addTag("osversion");
cdevData::addTag("machine");
cdevData::addTag("shell");
cdevData::addTag("sendPktCnt");
cdevData::addTag("recvPktCnt");
cdevData::addTag("clientCnt");
cdevData::addTag("pctcpu");
cdevData::addTag("datasize");
cdevData::addTag("socket");
data.insert("server", server);
data.insert("domain", domain);
data.insert("port", port);
#ifndef _WIN32
struct utsname hostinfo;
struct group * grp;
char * shell = getenv("SHELL");
uname(&hostinfo);
grp = getgrgid(getgid());
data.insert("username", getlogin());
data.insert("group", grp?grp->gr_name:(char *)"UNDEFINED");
data.insert("uid", (unsigned)getuid());
data.insert("gid", (unsigned)getgid());
data.insert("pid", (unsigned)getpid());
data.insert("host", hostinfo.nodename);
data.insert("os", hostinfo.sysname);
data.insert("osrelease", hostinfo.release);
data.insert("osversion", hostinfo.version);
data.insert("machine", hostinfo.machine);
data.insert("shell", shell?shell:(char *)"UNDEFINED");
#if defined(__hpux)
struct pst_status pstatData;
pstat_getproc(&pstatData, sizeof(pstatData), 0, (int)getpid());
data.insert("program", pstatData.pst_ucomm);
data.insert("commandline", pstatData.pst_cmd);
data.insert("starttime", pstatData.pst_start);
struct pst_static pstatStatic;
pstat_getstatic(&pstatStatic, sizeof(pstatStatic), 1, 0);
pageSize = pstatStatic.page_size;
#endif
#else
char userNameBuf[UNLEN+1];
unsigned long userNameLen = UNLEN;
char hostNameBuf[MAXHOSTNAMELEN+1];
unsigned long hostNameLen = MAXHOSTNAMELEN;
*userNameBuf = 0;
*hostNameBuf = 0;
data.insert("pid", getpid());
if(GetUserName (userNameBuf, &userNameLen)) data.insert("username", userNameBuf);
if(GetComputerName(hostNameBuf, &hostNameLen)) data.insert("host", hostNameBuf);
if(userNameBuf && hostNameBuf)
{
char grpNameBuf[GNLEN+1];
unsigned long grpNameLen = GNLEN;
char sidBuf[256];
unsigned long sidLen = 256;
SID_NAME_USE sidType;
*grpNameBuf = 0;
if(LookupAccountName(hostNameBuf, userNameBuf,
sidBuf, &sidLen, grpNameBuf,
&grpNameLen, &sidType))
{
data.insert("group", grpNameBuf);
}
}
OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(GetVersionEx (&osv))
{
char osVersionBuf[64];
switch(osv.dwPlatformId)
{
case VER_PLATFORM_WIN32s:
data.insert("os", "WINDOWS");
break;
case VER_PLATFORM_WIN32_WINDOWS:
if(osv.dwMinorVersion == 0)
data.insert("os", "WINDOWS 95");
else data.insert("os", "WINDOWS 98");
break;
case VER_PLATFORM_WIN32_NT:
data.insert("os", "WINDOWS NT");
break;
default:
data.insert("os", "WIN32");
break;
}
sprintf(osVersionBuf, "%d.%d", osv.dwMajorVersion, osv.dwMinorVersion);
data.insert("osversion", osVersionBuf);
data.insert("osrelease", ltoa(osv.dwBuildNumber));
}
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
switch(sysinfo.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_INTEL:
data.insert("machine", "PROCESSOR_ARCHITECTURE_INTEL");
break;
case PROCESSOR_ARCHITECTURE_MIPS:
data.insert("machine", "PROCESSOR_ARCHITECTURE_MIPS");
break;
case PROCESSOR_ARCHITECTURE_ALPHA:
data.insert("machine", "PROCESSOR_ARCHITECTURE_ALPHA");
break;
case PROCESSOR_ARCHITECTURE_PPC:
data.insert("machine", "PROCESSOR_ARCHITECTURE_PPC");
break;
case PROCESSOR_ARCHITECTURE_UNKNOWN:
default:
data.insert("machine", "PROCESSOR_ARCHITECTURE_UNKNOWN");
break;
}
if(__argc>0)
{
int i;
char * cmdLine = NULL;
size_t cmdLineLen = 0;
data.insert("program", __argv[0]);
for(i=0; i<__argc; i++) cmdLineLen+=strlen(__argv[i])+1;
cmdLine = new char [cmdLineLen];
*cmdLine = 0;
for(i=0; i<__argc; i++)
{
strcat(cmdLine, __argv[i]);
if(i+1<__argc) strcat(cmdLine, " ");
}
data.insert("commandline", cmdLine);
}
FILETIME startTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
if(GetProcessTimes(GetCurrentProcess(), &startTime, &exitTime, &kernelTime, &userTime))
{
struct timeval tv;
FileTimeToTimeVal(&startTime, &tv);
data.insert("starttime", tv.tv_sec);
}
#endif
}
cdevData & getServerData ( void )
{
#if defined(__hpux)
struct pst_status pstatData;
pstat_getproc(&pstatData, sizeof(pstatData), 0, (int)getpid());
data.insert("pctcpu", pstatData.pst_pctcpu);
data.insert("datasize", pageSize*(pstatData.pst_dsize+pstatData.pst_tsize+pstatData.pst_ssize));
data.insert("sendPktCnt", sendPktCnt);
data.insert("recvPktCnt", recvPktCnt);
#endif
return data;
}
void asciiDump ( FILE * fp = stdout )
{
fprintf(fp, "--------------------------------------------------------\n");
fprintf(fp, " Printing Server Information\n");
fprintf(fp, "--------------------------------------------------------\n");
char * server;
char * domain;
unsigned short port;
char * username;
char * group;
unsigned pid;
unsigned gid;
unsigned uid;
char * host;
char * os;
char * osrelease;
char * osversion;
char * machine;
char * shell;
char * program;
char * commandline;
long starttime;
size_t datasize;
float pctcpu;
getServerData();
if(data.find("domain", (void *&)domain)!=CDEV_SUCCESS) host = NULL;
if(data.find("server", (void *&)server)!=CDEV_SUCCESS) server = NULL;
if(data.find("username", (void * &)username)!=CDEV_SUCCESS) username = NULL;
if(data.find("group", (void * &)group)!=CDEV_SUCCESS) group = NULL;
if(data.find("host", (void * &)host)!=CDEV_SUCCESS) host = NULL;
if(data.find("os", (void * &)os)!=CDEV_SUCCESS) os = NULL;
if(data.find("osrelease", (void * &)osrelease)!=CDEV_SUCCESS) osrelease = NULL;
if(data.find("osversion", (void * &)osversion)!=CDEV_SUCCESS) osversion = NULL;
if(data.find("machine", (void * &)machine)!=CDEV_SUCCESS) machine = NULL;
if(data.find("shell", (void * &)shell)!=CDEV_SUCCESS) shell=NULL;
if(data.find("program", (void * &)program)!=CDEV_SUCCESS) program=NULL;
if(data.find("commandline", (void * &)commandline)!=CDEV_SUCCESS) commandline=NULL;
if(data.get("port", &port)!=CDEV_SUCCESS) port = 0;
if(data.get("starttime", &starttime)!=CDEV_SUCCESS) starttime = 0;
if(data.get("pid", &pid)!=CDEV_SUCCESS) pid = 0;
if(data.get("gid", &gid)!=CDEV_SUCCESS) gid = 0;
if(data.get("uid", &uid)!=CDEV_SUCCESS) uid = 0;
if(data.get("datasize", &datasize)!=CDEV_SUCCESS) datasize = 0;
if(data.get("pctcpu", &pctcpu)!=CDEV_SUCCESS) pctcpu = (float)0.0;
fprintf(fp, "=> DOMAIN : %s\n", domain?domain:"UNDEFINED");
fprintf(fp, "=> SERVER : %s\n", server?server:"UNDEFINED");
fprintf(fp, "=> LISTENING PORT : %s\n", port?ltoa(port):"UNDEFINED");
fprintf(fp, "=> USERNAME : %s\n", username?username:"UNDEFINED");
fprintf(fp, "=> GROUP : %s\n", group?group:"UNDEFINED");
fprintf(fp, "=> USER ID : %s\n", uid?ltoa(uid):"UNDEFINED");
fprintf(fp, "=> GROUP ID : %s\n", gid?ltoa(gid):"UNDEFINED");
fprintf(fp, "=> PROCESS ID : %s\n", pid?ltoa(pid):"UNDEFINED");
fprintf(fp, "=> APPLICATION : %s\n", program?program:"UNDEFINED");
fprintf(fp, "=> COMMAND LINE : %s\n", commandline?commandline:"UNDEFINED");
fprintf(fp, "=> START TIME : %s", starttime?ctime(&starttime):"UNKNOWN\n");
fprintf(fp, "=> HOST NAME : %s\n", host?host:"UNKNOWN");
fprintf(fp, "=> OS : %s\n", os?os:"UNKNOWN");
fprintf(fp, "=> OS VERSION : %s\n", osversion?osversion:"UNKNOWN");
fprintf(fp, "=> OS RELEASE : %s\n", osrelease?osrelease:"UNKNOWN");
fprintf(fp, "=> HARDWARE ID : %s\n", machine?machine:"UNKNOWN");
fprintf(fp, "=> PERCENT CPU : %f\n", pctcpu);
fprintf(fp, "=> RESIDENT SIZE : %i\n", datasize);
fprintf(fp, "=> PACKETS SENT : %i\n", sendPktCnt);
fprintf(fp, "=> PACKETS RECV : %i\n", recvPktCnt);
fprintf(fp, "=> ACTIVE CLIENTS : %i\n", clientCnt);
fprintf(fp, "--------------------------------------------------------\n");
}
};
+257
View File
@@ -0,0 +1,257 @@
#include <SignalManager.h>
#ifdef __linux
#ifndef SIGSYS
#define SIGSYS SIGUNUSED
#endif
#endif
// *****************************************************************************
// * SignalManager::reporter :
// * This is the declaration for the error reporter.
// *****************************************************************************
ErrorReporter SignalManager::reporter;
// *****************************************************************************
// * SignalHandler::SignalHandler :
// * This is the initializer for a SignalHandler class. It holds the
// * information needed to handle a single signal number.
// *****************************************************************************
_SignalHandler_::_SignalHandler_ ( void )
: signo(0), handler(NULL), oldHandler(NULL)
{
}
// *****************************************************************************
// * SignalHandler::~SignalHandler :
// * This is the destructor for the SignlaHandler class. It will call the
// * uninstall method to remove the handler that was imposed for the signal.
// *****************************************************************************
_SignalHandler_::~_SignalHandler_ ( void )
{
uninstall ();
}
// *****************************************************************************
// * SignalHandler::install :
// * This method will install the specified signal handler function on the
// * signal number specified by signo. The old handler will be preserved
// * for later restoration.
// *****************************************************************************
void _SignalHandler_::install ( int newSigno, SignalHandlerFunc newHandler )
{
// *********************************************************************
// * If a local signal handler has already been installed on the
// * specified signal number, uninstall it before proceeding.
// *********************************************************************
if(handler!=NULL && signo==newSigno) uninstall();
// *********************************************************************
// * Setup the control variables.
// *********************************************************************
signo = newSigno;
handler = newHandler;
// *********************************************************************
// * Install the new signal handler, preserving the old handler in the
// * oact sigaction structure.
// *********************************************************************
oldHandler = signal(signo, handler);
}
// *****************************************************************************
// * SignalHandler::uninstall :
// * This method will remove a user specified signal handler and will restore
// * the previously installed handler.
// *****************************************************************************
void _SignalHandler_::uninstall ( void )
{
// *********************************************************************
// * Check to see if the signal handler has been installed. If so,
// * call sigaction to confirm that the current signal handler is the
// * one that was installed.
// *********************************************************************
if(signo!=0 && handler!=NULL)
{
SignalHandlerFunc current = signal(signo, SIG_DFL);
if(current==handler) signal(signo, oldHandler);
else signal(signo, current);
}
signo = 0;
handler = NULL;
}
// *****************************************************************************
// * SignalManager::installHandler :
// * This method will install the specified signal handler.
// *****************************************************************************
void SignalManager::installHandler (int signo, SignalHandlerFunc handler )
{
if(signo>0 && signo<=MAXSIGNAL) signals[signo-1].install(signo, handler);
}
// *****************************************************************************
// * SignalManager::uninstallHandler :
// * This method will remove a signal handler that was previously installed.
// *****************************************************************************
void SignalManager::uninstallHandler ( int signo )
{
if(signo>0 && signo<=MAXSIGNAL) signals[signo-1].uninstall();
}
// *****************************************************************************
// * SignalManager::installDefaults :
// * This method will install a default collection of signal handlers.
// *****************************************************************************
void SignalManager::installDefaults ( void )
{
static int sigList[] =
{
#if !defined (_WIN32)
SIGFPE, // Erroneous arithmetic operation.
SIGHUP, // Hangup.
SIGILL, // Illegal instruction.
SIGINT, // Terminal interrupt signal.
SIGPIPE, // Write on a socket with no one to read
SIGQUIT, // Terminal quit signal.
SIGSEGV, // Invalid memory reference.
SIGTERM, // Termination signal.
SIGBUS, // Bus error.
SIGSYS, // Bad system call.
#else
SIGABRT, // Process abort signal.
SIGFPE, // Erroneous arithmetic operation.
SIGILL, // Illegal instruction.
SIGINT, // Terminal interrupt signal.
SIGSEGV, // Invalid memory reference.
SIGTERM, // Termination signal.
#endif
0
};
for(int i=0; sigList[i]!=0; i++) installHandler(sigList[i], defaultHandler);
}
// *****************************************************************************
// * SignalManager::defaultHandler :
// * This is the default signal handler method.
// *****************************************************************************
void SignalManager::defaultHandler( int signo )
{
switch(signo)
{
// *************************************************************
// * Process the interrupt signal by reporting it and exiting.
// *************************************************************
case SIGINT:
reporter.outputError (
CDEV_SEVERITY_INFO,
"Signal Manager",
"[SIGINT] Terminating at user's request");
exit(0);
// *************************************************************
// * Process the terminate signal by reporting it and exiting.
// *************************************************************
case SIGTERM:
reporter.outputError (
CDEV_SEVERITY_INFO,
"Signal Manager",
"[SIGTERM] Terminating normally");
exit(0);
// *************************************************************
// * Process abort signal by reporting the error and exiting.
// *************************************************************
case SIGABRT:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGABRT] Application terminating due to abort signal");
abort();
// *************************************************************
// * Process erroneous arithmetic operations by reporting them
// * and exiting.
// *************************************************************
case SIGFPE:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGFPE] Floating point error - terminating\n");
abort();
// *************************************************************
// * Process illegal instructions by reporting them and exiting.
// *************************************************************
case SIGILL:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGILL] Illegal instruction");
abort();
// *************************************************************
// * Process the segmentation fault signal by reporting the
// * error and terminating.
// *************************************************************
case SIGSEGV:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGSEGV] Segmentation fault");
abort();
#if !defined(_WIN32)
// *************************************************************
// * Report pipe errors, however, do not terminate.
// *************************************************************
case SIGPIPE:
reporter.outputError (
CDEV_SEVERITY_ERROR,
"Signal Manager",
"[SIGPIPE] Write on a pipe with no one to read it");
errno = SIGPIPE;
break;
// *************************************************************
// * Process a bus error by reporting it and exiting.
// *************************************************************
case SIGBUS:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGBUS] Bus error");
abort();
// *************************************************************
// * Process a bad system call by reporting it and exiting.
// *************************************************************
case SIGSYS:
reporter.outputError (
CDEV_SEVERITY_SEVERE,
"Signal Manager",
"[SIGSYS] Bad system call");
abort();
// *************************************************************
// * Ignore the following signals:
// * SIGHUP: hangup signal.
// * SIGQUIT: terminal quit signal
// * default: All other signals
// *************************************************************
case SIGHUP:
case SIGQUIT:
#endif
default:
break;
}
signal(signo, SignalManager::defaultHandler);
}
+41
View File
@@ -0,0 +1,41 @@
#ifndef _SIGNAL_MANAGER_H_
#define _SIGNAL_MANAGER_H_
#include <signal.h>
#include <stdlib.h>
#include <cdevPlatforms.h>
#include <ErrorReporter.h>
typedef void (*SignalHandlerFunc)( int );
class GENERIC_SERVER_API _SignalHandler_
{
public:
_SignalHandler_ ( void );
~_SignalHandler_ ( void );
void install ( int, SignalHandlerFunc);
void uninstall ( void );
private:
int signo;
SignalHandlerFunc handler;
SignalHandlerFunc oldHandler;
};
class GENERIC_SERVER_API SignalManager
{
public:
static ErrorReporter reporter;
void installHandler ( int, SignalHandlerFunc );
void uninstallHandler ( int );
void installDefaults ( void );
static void defaultHandler ( int );
private:
enum { MAXSIGNAL = 34 };
_SignalHandler_ signals[MAXSIGNAL];
};
#endif /* _SIGNAL_MANAGER_H_ */
+25
View File
@@ -0,0 +1,25 @@
#include <SignalManager.h>
int x = 1;
void SIGINT_Handler ( int signo )
{
SignalManager::reporter.outputError (
CDEV_SEVERITY_INFO,
"Signal Manager",
"[SIGINT] Ignoring interrupt signal");
signal(signo, SIGINT_Handler);
x++;
}
int main ()
{
SignalManager manager;
manager.installDefaults();
manager.installHandler (SIGINT, SIGINT_Handler);
while(1)
{
if(x%5==0) raise(SIGABRT);
}
return 0;
}
+545
View File
@@ -0,0 +1,545 @@
#if !defined (_SOCKET_UTIL_H)
#define _SOCKET_UTIL_H
#include <cdevPlatforms.h>
#include <ErrorReporter.h>
#include <cdevHandleSet.h>
class SocketReader : virtual public ErrorReporter
{
public:
enum { SHUTDOWN_CODE = -128};
enum { MAX_RETRIES = 1000 };
enum { READBUF_INITIAL_SIZE = 56000 };
virtual int getHandle ( void ) const = 0;
SocketReader ( long MagicNumber = 0L );
~SocketReader ( void );
int read ( char ** buf, int * len );
virtual int readNextPacket ( char ** buf, int * len );
virtual int reading ( void );
virtual int readReset ( void );
private:
char * readBuf;
char * readNextPktPtr;
int readBufMax;
int readPktCnt;
int readPktXfrCnt;
int readBufLen;
int readXfrLen;
int readPktXfrLen;
int readRetries;
// ***************************
// * This section added to
// * optionally support magic
// * number for buffer
// * validation.
// ***************************
const long readMagicNumber;
int readMagicLen;
long readMagicVal;
};
class SocketWriter : virtual public ErrorReporter
{
public:
enum { WRITEBUF_INITIAL_SIZE = 56000 };
enum { WRITEBUF_PAD_SIZE = 32 };
virtual int getHandle ( void ) const = 0;
SocketWriter ( long MagicNumber = 0L );
~SocketWriter ( void );
int write ( char * buf, int buflen);
int writeEnqueue ( char * buf, int buflen);
virtual int writing ( void );
virtual int writeContinue ( void );
virtual int writeReset ( void );
virtual int writeGoodbye ( void );
private:
char * writeBuf;
char * writeNextPktPtr;
int writeBufMax;
int writePktCnt;
int writeBufLen;
int writeXfrLen;
int writePktXfrLen;
// ***************************
// * This section added to
// * optionally support magic
// * number for buffer
// * validation.
// ***************************
const long writeMagicNumber;
int writeMagicLen;
};
inline SocketReader::SocketReader ( long MagicNumber )
: readBuf(NULL), readNextPktPtr(NULL), readBufMax(READBUF_INITIAL_SIZE),
readPktCnt(0), readPktXfrCnt(0), readBufLen(0), readXfrLen(0),
readPktXfrLen(0), readRetries(0),
// *************************
// * Magic number support.
// *************************
readMagicNumber(MagicNumber), readMagicLen(0), readMagicVal(0L)
{
readBuf = (char *)malloc(readBufMax);
readNextPktPtr = readBuf+RNDUP(sizeof(long));
}
inline SocketReader::~SocketReader ( void )
{
if(readBuf!=NULL) free(readBuf);
}
inline int SocketReader::reading ( void )
{
return (readPktCnt>readPktXfrCnt)?1:0;
}
inline int SocketReader::readReset ( void )
{
readNextPktPtr = readBuf+RNDUP(sizeof(long));
readBufLen = 0;
readXfrLen = 0;
readPktCnt = 0;
readPktXfrCnt = 0;
readPktXfrLen = 0;
readMagicLen = 0;
readMagicVal = 0L;
return 0;
}
// *****************************************************************************
// * This method causes the next available packet to be dequeued from the
// * already read buffer.
// *****************************************************************************
inline int SocketReader::readNextPacket (char ** buf, int *buflen)
{
if(readPktCnt>readPktXfrCnt)
{
*buflen = (int)ntohl(*(long *)readNextPktPtr);
readNextPktPtr+=RNDUP(sizeof(long));
*buf = readNextPktPtr;
readNextPktPtr+=RNDUP(*buflen);
readPktXfrCnt++;
}
else {
*buf = NULL;
*buflen = 0;
}
if(readPktXfrCnt>=readPktCnt) readReset();
return *buflen;
}
// *****************************************************************************
// * This method will get the next available packet from the socket. The caller
// * is responsible for transfering this data to a new location immediately.
// * The caller should not delete the pointer provided by this method, or rely
// * on it to remain valid between calls.
// *****************************************************************************
inline int SocketReader::read(char ** buf, int * buflen)
{
int handle = getHandle();
int result = 0;
int shutdown = 0;
int error = 0;
*buf = NULL;
*buflen = 0;
// *********************************************************************
// * First test to ensure that the socket is allocated and that the
// * device descriptor is valid.
// *********************************************************************
if(handle<=0) result = -1;
// *********************************************************************
// * If the handle is valid attempt to read the next packet of a
// * multi-packet set from an already existing buffer.
// *********************************************************************
else if(reading()) result = readNextPacket(buf, buflen);
// *********************************************************************
// * If all packets in the most recent multi-packet set have already
// * been read... Then attempt to read a new block from the socket.
// *********************************************************************
else {
int amntread = 0;
char *readPtr = NULL;
// *************************************************************
// * Optionally read the magic number from the socket.
// *************************************************************
readPtr = (char *)&readMagicVal;
while(readMagicNumber && readMagicLen<sizeof(long) &&
(amntread = recv(handle,
readPtr+readMagicLen,
sizeof(long)-readMagicLen, 0))>0)
{
// *****************************************************
// * Anytime a read is successful, set the readRetries
// * variable to 0.
// *****************************************************
readRetries = 0;
// *****************************************************
// * Once an entire long integer is read from the socket
// * validate that against the Magic Number that is
// * expected.
// *****************************************************
if((readMagicLen += amntread)>=sizeof(long))
{
readMagicVal = ntohl(readMagicVal);
// *********************************************
// * If the Magic Number received is not the
// * same as the Magic Number expected, set the
// * shutdown flag and kill the connection.
// *********************************************
if(readMagicVal!=readMagicNumber)
{
outputError (CDEV_SEVERITY_ERROR, "SocketReader",
"Invalid magic number read from socket\n\t=> Expected %lX - received %lX",
readMagicNumber, readMagicVal);
shutdown = 1;
}
}
}
// *************************************************************
// * Read the size of the packet from the socket... note, this
// * code will not be executed until the Magic Number has been
// * successfully read.
// *************************************************************
readPtr = (char *)&readBufLen;
while(!shutdown && readPktXfrLen<sizeof(long) &&
(!readMagicNumber || readMagicLen>=sizeof(long)) &&
(amntread = recv(handle,
readPtr+readPktXfrLen,
sizeof(long)-readPktXfrLen, 0))>0)
{
// *****************************************************
// * Anytime a read is successful, set the readRetries
// * variable to 0.
// *****************************************************
readRetries = 0;
// *****************************************************
// * Once an entire long integer is read from the socket
// * use that variable as the expected packet length,
// * and allocate a buffer of sufficient size to hold
// * the incoming packet.
// *****************************************************
if((readPktXfrLen += amntread)>=sizeof(long))
{
readBufLen = ntohl(readBufLen);
// *********************************************
// * A length of -1 indicates that the socket
// * should be shutdown.
// *********************************************
if(readBufLen == -1) shutdown = 1;
if(readBufLen <= 0) readReset();
else {
if(readBufLen>readBufMax)
{
readBufMax = readBufLen;
readBuf = (char *)realloc(readBuf, readBufMax);
readNextPktPtr = readBuf+RNDUP(sizeof(long));
}
readXfrLen = 0;
}
}
}
// *************************************************************
// * Continue reading from the socket into the new buffer until
// * the amount of data specified by the readBufLen variable
// * has been obtained, or no further data is available.
// *************************************************************
while(!shutdown &&
readPktXfrLen>=sizeof(long) &&
readXfrLen < readBufLen &&
(amntread = recv(handle, readBuf+readXfrLen, readBufLen-readXfrLen, 0))>0)
{
// *****************************************************
// * Anytime a read is successful, set the readRetries
// * variable to 0.
// *****************************************************
readRetries = 0;
// *****************************************************
// * Once a complete buffer of data has been read from
// * the socket, use the readNextPacket method to set
// * the user pointer to the appropriate position within
// * the data buffer.
// *****************************************************
if((readXfrLen+=amntread)>=readBufLen)
{
readPktCnt = (int)ntohl(*(long *)readBuf);
result = readNextPacket(buf, buflen);
}
}
// *************************************************************
// * If an error occurred, or the function failed to read
// * data from the socket, then this section of code will be
// * executed.
// *************************************************************
if(!shutdown && amntread<=0)
{
int errCode = GetSocketErrno();
// *****************************************************
// * Increment the readRetries to count the number of
// * empty receives. Once this variable reaches the
// * MAX_RETRIES value, a -1 will be returned to delete
// * the socket.
// *****************************************************
readRetries++;
// *****************************************************
// * If the amntread is 0 or -1 (and any error was
// * caused by blocking), and the maximum number of
// * retries has not been reached, do the following
// *****************************************************
if(readRetries < MAX_RETRIES &&
((amntread==0 && readPktXfrLen>0) ||
(amntread==-1 && (errCode == EWOULDBLOCK || errCode == EAGAIN))))
{
result = 0;
}
// *****************************************************
// * Otherwise, if the maximum number of retries have
// * been reached, do the following
// *****************************************************
else if(readRetries >= MAX_RETRIES)
{
outputError (CDEV_SEVERITY_WARN, "SocketReader",
"Have exceeded maximum retries on socket");
result = -1;
}
// *****************************************************
// * Otherwise, if the error was not due to blocking,
// * do the following
// ******************************************************
else if(amntread==-1)
{
outputError (CDEV_SEVERITY_ERROR, "SocketReader",
"Error number %i while reading from socket",
errCode);
result = -1;
}
}
}
if(shutdown) result = SHUTDOWN_CODE;
if(result==-1) readReset();
return result;
}
inline SocketWriter::SocketWriter ( long MagicNumber )
: writeBuf(NULL), writeNextPktPtr(NULL), writeBufMax(WRITEBUF_INITIAL_SIZE),
writePktCnt(0), writeBufLen(RNDUP(sizeof(long))), writeXfrLen(0), writePktXfrLen(0),
// *************************
// * Magic number support.
// *************************
writeMagicNumber(MagicNumber), writeMagicLen(0)
{
writeBuf = (char *)malloc(writeBufMax);
writeNextPktPtr = writeBuf+writeBufLen;
}
inline SocketWriter::~SocketWriter ( void )
{
if(writeBuf!=NULL) free(writeBuf);
}
inline int SocketWriter::writing ( void )
{
return (writePktCnt>0)?1:0;
}
inline int SocketWriter::writeReset ( void )
{
writeBufLen = RNDUP(sizeof(long));
writeNextPktPtr = writeBuf+writeBufLen;
writePktCnt = 0;
writeXfrLen = 0;
writePktXfrLen = 0;
// *************************
// * Magic number support.
// *************************
writeMagicLen = 0;
return 0;
}
inline int SocketWriter::writeContinue ( void )
{
int handle = getHandle();
int result = 0;
// *********************************************************************
// * The following variable has been added to allow the SocketWriter
// * to poll the file descriptor for validity prior to writing to it.
// *********************************************************************
#ifdef SYSV
struct pollfd fds;
fds.fd = handle;
fds.events = POLLERR|POLLNVAL|POLLHUP;
fds.revents = 0;
// *********************************************************************
// * First test to ensure that the socket is allocated and that the
// * device descriptor is valid.
// *********************************************************************
if( handle<=0 ) result = -1;
// *********************************************************************
// * Execute poll to ensure that the handle is still valid and writable.
// *********************************************************************
else if(poll(&fds, 1, 0)>0 && (fds.revents&(POLLERR|POLLNVAL|POLLHUP))!=0)
{
result = -1;
}
#else
cdevHandleSet readfd;
struct timeval tv;
readfd.set_bit(handle);
tv.tv_sec = 0;
tv.tv_usec = 0;
if (handle<=0) result = -1;
else if (cdevSelect (handle+1,readfd,0,0,&tv)<0) result = -1;
#endif
// *********************************************************************
// * If all is well, continue writing data.
// *********************************************************************
else if( writing() )
{
int amntsent = 0;
char *sendPtr = NULL;
long magicNumber = htonl(writeMagicNumber);
long packetSize = htonl(writeBufLen);
sendPtr = (char *)&magicNumber;
while(writeMagicNumber && writeMagicLen<sizeof(long) &&
(amntsent = send(handle, sendPtr+writeMagicLen, sizeof(long)-writeMagicLen, 0))>0)
{
writeMagicLen += amntsent;
}
sendPtr = (char *)&packetSize;
while((!writeMagicNumber || writeMagicLen>=sizeof(long)) && writePktXfrLen<sizeof(long) &&
(amntsent = send(handle, sendPtr+writePktXfrLen, sizeof(long)-writePktXfrLen, 0))>0)
{
writePktXfrLen += amntsent;
}
while(writePktXfrLen>=sizeof(long) &&
writeXfrLen < writeBufLen &&
(amntsent = send(handle, writeBuf+writeXfrLen, writeBufLen-writeXfrLen, 0))>0)
{
if((writeXfrLen+=amntsent)>=writeBufLen)
{
result = writeBufLen;
writeReset();
}
}
if(amntsent<=0)
{
int errCode = GetSocketErrno();
if((amntsent==0 && writePktXfrLen>0) ||
(amntsent==-1 && (errCode == EWOULDBLOCK || errCode == EAGAIN)))
{
// *********************************************
// * Do Nothing
// *********************************************
}
else if(amntsent==-1)
{
outputError (CDEV_SEVERITY_ERROR, "SocketWriter",
"Error number %i while writing to socket",
errCode);
result = -1;
}
}
}
if(result==-1) writeReset();
return result;
}
inline int SocketWriter::writeGoodbye ( void )
{
int handle = getHandle();
int result = 0;
if( handle<=0 ) result = -1;
else
{
char val = -1;
result=(send(handle, &val, 1, MSG_OOB)==1)?0:-1;
}
if(result==-1) writeReset();
return result;
}
inline int SocketWriter::writeEnqueue ( char * buf, int buflen )
{
int result = 0;
// *********************************************************************
// * If data has already been transmitted, then it is impossible to
// * add more data to the outbound buffer because it would invalidate
// * the packet length and packet count variables.
// *********************************************************************
if(writePktXfrLen>0) result = -1;
else
{
if(writePktCnt==0 &&
(RNDUP(sizeof(long))+RNDUP(buflen)+RNDUP(sizeof(long))) > writeBufMax)
{
writeBufMax = (RNDUP(sizeof(long))+RNDUP(buflen)+RNDUP(sizeof(long))+WRITEBUF_PAD_SIZE);
writeBuf = (char *)realloc(writeBuf, writeBufMax);
writeBufLen = RNDUP(sizeof(long));
writeNextPktPtr = writeBuf+writeBufLen;
}
if(writeBufLen+RNDUP(buflen)+RNDUP(sizeof(long)) < writeBufMax)
{
writePktCnt++;
long tpktCnt = htonl(writePktCnt);
memcpy(writeBuf, &tpktCnt, sizeof(long));
long tbuflen = htonl(buflen);
memcpy(writeNextPktPtr, &tbuflen, sizeof(long));
writeNextPktPtr+=RNDUP(sizeof(long));
memcpy(writeNextPktPtr, buf, buflen);
writeNextPktPtr+=RNDUP(buflen);
writeBufLen = (int)(writeNextPktPtr-writeBuf);
result = 0;
}
else result = -1;
}
return result;
}
inline int SocketWriter::write(char * buf, int buflen)
{
writeEnqueue(buf, buflen);
return writeContinue();
}
#endif /* _SOCKET_UTIL_H */
+235
View File
@@ -0,0 +1,235 @@
#ifndef _VAR_STRING_HASH_H_
#define _VAR_STRING_HASH_H_ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class StringHashNode
{
friend class StringHash;
friend class StringHashIterator;
private:
int copyKeyFlag;
char * hashString;
void * hashData;
StringHashNode * next;
StringHashNode ( char * HashString, void * HashData, int copyKey=1);
~StringHashNode ( void );
};
class StringHash
{
friend class StringHashIterator;
private:
int tableSize;
int copyKeyFlag;
StringHashNode ** nodes;
public:
StringHash (int copyKey = 1, int TableSize = 255 );
~StringHash ( void );
inline unsigned int stringHash ( char * hashString );
inline void insert ( char * hashString, void * hashData );
inline void remove ( char * hashString );
inline void * find ( char * hashString );
};
class StringHashIterator
{
private:
StringHash * hashTbl;
StringHashNode * node;
int idx;
public:
StringHashIterator( StringHash * HashTbl );
~StringHashIterator( void );
inline void * first ( void );
inline void * operator ++ ( void );
inline void * operator ++ ( int x );
inline char * key ( void );
inline void * data ( void );
};
inline StringHashNode::StringHashNode ( char * HashString, void * HashData, int copyKey )
: next(NULL), copyKeyFlag(copyKey)
{
if(copyKeyFlag) hashString = strdup(HashString);
else hashString = HashString;
hashData = HashData;
}
inline StringHashNode::~StringHashNode ( void )
{
if(copyKeyFlag) delete hashString;
}
inline StringHash::StringHash ( int copyKey, int TableSize )
: copyKeyFlag(copyKey), tableSize (TableSize)
{
nodes = new StringHashNode * [tableSize];
memset(nodes, 0, sizeof(StringHashNode *) * tableSize );
}
inline StringHash::~StringHash ( void )
{
for(int i=0; i<tableSize; i++)
{
while(nodes[i]!=NULL)
{
StringHashNode * node = nodes[i];
nodes[i] = node->next;
delete node;
}
}
delete nodes;
}
inline unsigned int StringHash::stringHash ( char * hashString )
{
unsigned int hash = 0, g;
for (int i = 0; hashString[i] != '\0'; i++)
{
hash = (hash << 4) + hashString[i];
if (g = hash & 0xf0000000)
{
hash ^= g >> 24;
hash ^= g;
}
}
return (hash % tableSize);
}
inline void StringHash::insert (char * hashString, void * hashData )
{
unsigned int idx = stringHash(hashString);
StringHashNode * prev = NULL, * node = nodes[idx];
StringHashNode * newNode = new StringHashNode(hashString, hashData, copyKeyFlag);
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
newNode->next = node->next;
delete node;
}
if(prev!=NULL) prev->next = newNode;
else nodes[idx] = newNode;
}
inline void StringHash::remove ( char * hashString )
{
unsigned int idx = stringHash(hashString);
StringHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
if(prev!=NULL) prev->next = node->next;
else nodes[idx] = node->next;
delete node;
}
}
inline void * StringHash::find ( char * hashString )
{
unsigned int idx = stringHash(hashString);
StringHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
return node!=NULL?node->hashData:NULL;
}
inline StringHashIterator::StringHashIterator(StringHash * HashTbl)
: hashTbl(HashTbl), idx(0), node(NULL)
{
}
inline StringHashIterator::~StringHashIterator( void )
{
}
inline void * StringHashIterator::first ( void )
{
if(hashTbl!=NULL)
{
for(idx = 0; idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL; idx++);
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * StringHashIterator::operator ++ ( void )
{
if(hashTbl!=NULL)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * StringHashIterator::operator ++ ( int x )
{
if(hashTbl!=NULL && x==0)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline char * StringHashIterator::key ( void )
{
return (node!=NULL)?node->hashString:(char *)NULL;
}
inline void * StringHashIterator::data ( void )
{
return (node!=NULL)?node->hashData:NULL;
}
#endif /* _VAR_STRING_HASH_H_ */
+235
View File
@@ -0,0 +1,235 @@
#ifndef _VAR_STRING_HASH_H_
#define _VAR_STRING_HASH_H_ 1
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class VarStrHashNode
{
friend class VarStrHash;
friend class VarStrHashIterator;
private:
int copyKeyFlag;
char * hashString;
void * hashData;
VarStrHashNode * next;
VarStrHashNode ( char * HashString, void * HashData, int copyKey=1);
~VarStrHashNode ( void );
};
class VarStrHash
{
friend class VarStrHashIterator;
private:
int tableSize;
int copyKeyFlag;
VarStrHashNode ** nodes;
public:
VarStrHash (int copyKey = 1, int TableSize = 255 );
~VarStrHash ( void );
inline unsigned char stringHash ( char * hashString );
inline void insert ( char * hashString, void * hashData );
inline void remove ( char * hashString );
inline void * find ( char * hashString );
};
class VarStrHashIterator
{
private:
VarStrHash * hashTbl;
VarStrHashNode * node;
int idx;
public:
VarStrHashIterator( VarStrHash * HashTbl );
~VarStrHashIterator( void );
inline void * first ( void );
inline void * operator ++ ( void );
inline void * operator ++ ( int x );
inline char * key ( void );
inline void * data ( void );
};
inline VarStrHashNode::VarStrHashNode ( char * HashString, void * HashData, int copyKey )
: next(NULL), copyKeyFlag(copyKey)
{
if(copyKeyFlag) hashString = strdup(HashString);
else hashString = HashString;
hashData = HashData;
}
inline VarStrHashNode::~VarStrHashNode ( void )
{
if(copyKeyFlag) delete hashString;
}
inline VarStrHash::VarStrHash ( int copyKey, int TableSize )
: copyKeyFlag(copyKey), tableSize (TableSize)
{
nodes = new VarStrHashNode * [tableSize];
memset(nodes, 0, sizeof(VarStrHashNode *) * tableSize );
}
inline VarStrHash::~VarStrHash ( void )
{
for(int i=0; i<tableSize; i++)
{
while(nodes[i]!=NULL)
{
VarStrHashNode * node = nodes[i];
nodes[i] = node->next;
delete node;
}
}
}
inline unsigned char VarStrHash::stringHash ( char * hashString )
{
unsigned char idx=0;
unsigned int hash = 0, g;
for (int i = 0; hashString[i] != '\0'; i++)
{
hash = (hash << 4) + hashString[i];
if (g = hash & 0xf0000000)
{
hash ^= g >> 24;
hash ^= g;
}
}
return (hash % tableSize);
}
inline void VarStrHash::insert (char * hashString, void * hashData )
{
unsigned char idx = stringHash(hashString);
VarStrHashNode * prev = NULL, * node = nodes[idx];
VarStrHashNode * newNode = new VarStrHashNode(hashString, hashData, copyKeyFlag);
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
newNode->next = node->next;
delete node;
}
if(prev!=NULL) prev->next = newNode;
else nodes[idx] = newNode;
}
inline void VarStrHash::remove ( char * hashString )
{
unsigned char idx = stringHash(hashString);
VarStrHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
if(node!=NULL)
{
if(prev!=NULL) prev->next = node->next;
else nodes[idx] = node->next;
delete node;
}
}
inline void * VarStrHash::find ( char * hashString )
{
unsigned char idx = stringHash(hashString);
VarStrHashNode * prev = NULL, * node = nodes[idx];
while(node!=NULL && strcmp(node->hashString, hashString))
{
prev = node;
node = prev->next;
}
return node!=NULL?node->hashData:NULL;
}
inline VarStrHashIterator::VarStrHashIterator(VarStrHash * HashTbl)
: hashTbl(HashTbl), idx(0), node(NULL)
{
}
inline VarStrHashIterator::~VarStrHashIterator( void )
{
}
inline void * VarStrHashIterator::first ( void )
{
if(hashTbl!=NULL)
{
for(idx = 0; idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL; idx++);
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * VarStrHashIterator::operator ++ ( void )
{
if(hashTbl!=NULL)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline void * VarStrHashIterator::operator ++ ( int x )
{
if(hashTbl!=NULL && x==0)
{
if(node==NULL) first();
else if(node->next!=NULL) node = node->next;
else
{
node = NULL;
do {
idx++;
} while(idx<hashTbl->tableSize &&
(node = hashTbl->nodes[idx])==NULL);
}
}
else node = NULL;
return (node!=NULL)?node->hashData:NULL;
}
inline char * VarStrHashIterator::key ( void )
{
return (node!=NULL)?node->hashString:NULL;
}
inline void * VarStrHashIterator::data ( void )
{
return (node!=NULL)?node->hashData:NULL;
}
#endif /* _VAR_STRING_HASH_H_ */
@@ -0,0 +1,66 @@
static char * cdevGenericServerTags[] =
{
"client",
"clientCnt",
"commandline",
"connecttime",
"datasize",
"domain",
"gid",
"group",
"host",
"key",
"keyExp",
"keyType",
"machine",
"name",
"os",
"osrelease",
"osversion",
"owner",
"pctcpu",
"pid",
"ping",
"port",
"program",
"queryMsg",
"recvPktCnt",
"resultCode",
"sendPktCnt",
"server",
"shell",
"socket",
"starttime",
"table",
"time",
"uid",
"username",
NULL
};
class cdevGenericServerTagDef
{
public:
enum {GENERIC_BASE_TAG=900};
cdevGenericServerTagDef ( void )
{
installTags();
}
void installTags ( void )
{
for(int i=0; cdevGenericServerTags[i]!=NULL; i++)
{
int tagVal;
cdevData::insertTag(GENERIC_BASE_TAG+i, cdevGenericServerTags[i]);
if(cdevData::tagC2I(cdevGenericServerTags[i], &tagVal)!=CDEV_SUCCESS)
{
cdevData::addTag(cdevGenericServerTags[i]);
}
}
}
};
+334
View File
@@ -0,0 +1,334 @@
#ifndef _CDEV_PLATFORMS_H_
#define _CDEV_PLATFORMS_H_
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#ifdef _WIN32
#include <time.h>
#ifndef CDEV_REACTOR_API
// *********************************************************************
// * _CDEV_REACTOR_EXPORTS :
// * This definition allows cdevReactor DLL components to be imported
// * and exported by other applications and libraries.
// *********************************************************************
#ifdef _CDEV_REACTOR_EXPORTS_
#define CDEV_REACTOR_API __declspec(dllexport)
#else
#define CDEV_REACTOR_API __declspec(dllimport)
#endif
#endif
#ifndef GENERIC_SERVER_API
// *********************************************************************
// * _GENERIC_SERVER_EXPORTS :
// * This definition allows Generic Server DLL components to be imported
// * and exported by other applications and libraries.
// *********************************************************************
#ifdef _GENERIC_SERVER_EXPORTS_
#define GENERIC_SERVER_API __declspec(dllexport)
#else
#define GENERIC_SERVER_API __declspec(dllimport)
#endif
#endif
#include <WinSock2.h>
#include <io.h>
#include <fcntl.h>
#include <WinBase.h>
#include <lmcons.h>
#include <process.h>
#include <sys/types.h>
#include <xdr.h>
typedef signed long ssize_t;
#define MAXHOSTNAMELEN 128
#define EWOULDBLOCK WSAEWOULDBLOCK
#define EINPROGRESS WSAEINPROGRESS
#define EISCONN WSAEISCONN
#define SELECT_PARM fd_set *
#ifndef O_NONBLOCK
#define O_NONBLOCK 1
#endif
// *********************************************************************
// * SOCKOPT_SIZE_PARM :
// * This is the datatype of the parameter that is sent to getsockopt,
// * setsockopt, and getsockname.
// *********************************************************************
typedef int SOCKOPT_SIZE_PARM;
inline int GetSocketErrno ( void )
{
return WSAGetLastError();
}
inline int GetSelectErrno ( void )
{
return WSAGetLastError();
}
inline int InitializeNetwork ( void )
{
WORD version_requested = MAKEWORD (2, 0);
WSADATA wsa_data;
return WSAStartup(version_requested, &wsa_data);
}
inline int TerminateNetwork ( void )
{
return WSACleanup();
}
inline char * ultoa ( unsigned long val )
{
static char buf[64];
return _ultoa(val, buf, 10);
}
inline char * ltoa ( long val )
{
static char buf[64];
return _ltoa(val, buf, 10);
}
inline int usleep ( unsigned int useconds )
{
int retval = 0;
if(useconds<100000) Sleep(useconds/1000);
else retval = -1;
return retval;
}
inline unsigned int sleep ( unsigned int seconds )
{
Sleep(seconds*1000);
return seconds;
}
// *********************************************************************
// * This version of pipe is provided because the file descriptors
// * generated by the Windows NT version of pipe cannot be used with the
// * select system call.
// *********************************************************************
inline int pipe ( int fd[2] )
{
int retval = 0;
sockaddr_in addr[2];
hostent * hostptr;
char hostname[MAXHOSTNAMELEN];
unsigned long ipAddr;
unsigned long blockFlag = 1;
gethostname(hostname, MAXHOSTNAMELEN);
hostptr = ::gethostbyname(hostname);
ipAddr = *(unsigned long *)hostptr->h_addr;
for(int i=0; i<2; i++)
{
addr[i].sin_family = AF_INET;
addr[i].sin_port = htons(0);
*(unsigned long *)&addr[i].sin_addr = ipAddr;
if((fd[i] = socket(PF_INET, SOCK_DGRAM,0))>0 &&
bind(fd[i], (sockaddr *)&addr[i], sizeof(addr[i]))==0)
{
int addrLen = sizeof(addr[i]);
getsockname(fd[i], (sockaddr *)&addr[i], &addrLen);
ioctlsocket(fd[i], FIONBIO, &blockFlag);
}
else fd[i] = -1;
}
if(fd[0]>0 && fd[1]>0)
{
::connect(fd[0], (sockaddr *)&addr[1], sizeof(addr[1]));
::connect(fd[1], (sockaddr *)&addr[0], sizeof(addr[0]));
}
else
{
if(fd[0]>0) close(fd[0]);
if(fd[1]>0) close(fd[1]);
fd[0] = (fd[1] = (retval = -1));
}
return retval;
}
inline int FileTimeToTimeVal ( FILETIME * tfile, struct timeval * tv )
{
if(tfile && tv)
{
ULARGE_INTEGER _100ns = {tfile->dwLowDateTime,
tfile->dwHighDateTime};
_100ns.QuadPart -= 0x19db1ded53e8000i64;
tv->tv_sec = long (_100ns.QuadPart / (10000 * 1000));
tv->tv_usec = long ((_100ns.QuadPart % (10000 * 1000)) / 10);
return 0;
}
return -1;
}
inline int gettimeofday (struct timeval *tv)
{
FILETIME tfile;
GetSystemTimeAsFileTime(&tfile);
return FileTimeToTimeVal(&tfile, tv);
}
inline int cdevSelect (
int size,
fd_set * read_set,
fd_set * write_set,
fd_set * except_set,
struct timeval * timeout )
{
int result = select(size, read_set, write_set, except_set, timeout);
if(result<0 && GetSelectErrno()==WSAEINVAL)
{
if(timeout)
{
unsigned long millisec = timeout->tv_sec*1000+timeout->tv_usec/1000;
Sleep(millisec);
}
result = 0;
}
else if (result<0 && GetSelectErrno()==WSAEINPROGRESS)
{
result = 0;
}
return result;
}
#else
#include <sys/time.h>
#define CDEV_REACTOR_API
#define GENERIC_SERVER_API
#ifdef __VMS
#ifdef _TGV_MULTINET
// *****************************************************
// * Under TGV multinet and VMS, if you want sys/types.h
// * you need to have types.h already pulled in because
// * sys/types.h makes it look like types.h is loaded.
// * Then when types.h does get loaded, it is ignored
// * because it looks like it is already loaded --
// * Mr. Danial Van Olst
// *****************************************************
#include <types.h>
// *****************************************************
// * Under TGV Multinet and VMS, the file sys/param.h
// * does not define NOFILE (max number of open files
// * per process). FD_SETSIZE seems to be the correct
// * value for NOFILE.
// * See Multinet's sys/types.h file for more info on
// * FD_SETSIZE. - Daniel Van Olst
// *****************************************************
#ifndef NOFILE
#define NOFILE FD_SETSIZE
#endif
#endif
#endif
#ifdef SYSV
#include <poll.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <grp.h>
#include <rpc/rpc.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#if defined (__hpux)
#include <sys/pstat.h>
#endif
#if defined (AIX)
#include <sys/select.h>
#endif
#if defined(solaris) || defined(SunOS)
#include <sys/filio.h>
extern "C" { int gethostname(char *name, int namelen); }
#endif
#ifdef _SELECT_USES_INT_
#define SELECT_PARM int *
#else
#define SELECT_PARM fd_set *
#endif
#ifndef min
#define min(a,b) ((a<b)?a:b)
#endif
#ifndef max
#define max(a,b) ((a>b)?a:b)
#endif
// *********************************************************************
// * SOCKOPT_SIZE_PARM :
// * This is the datatype of the parameter that is sent to getsockopt,
// * setsockopt, and getsockname.
// *********************************************************************
#if defined(AIX) || defined(__linux)
typedef unsigned int SOCKOPT_SIZE_PARM;
#else
typedef int SOCKOPT_SIZE_PARM;
#endif
inline int GetSocketErrno ( void )
{
return errno;
}
inline int GetSelectErrno ( void )
{
return errno;
}
inline int InitializeNetwork ( void )
{
return 0;
}
inline int TerminateNetwork ( void )
{
return 0;
}
inline int gettimeofday (struct timeval * tv)
{
struct timezone tz;
return ::gettimeofday(tv, &tz);
}
inline int cdevSelect (int nfds, fd_set *r, fd_set *w, fd_set *e, struct timeval *t)
{
return ::select(nfds, (SELECT_PARM)r, (SELECT_PARM)w, (SELECT_PARM)e, t);
}
#endif
#endif /* _CDEV_PLATFORMS_H_ */
+8
View File
@@ -0,0 +1,8 @@
// *****************************************************************************
// * CLIP_MAGIC_NUMBER :
// * The magic number was added to the CLIP Protocol beginning with version
// * 1.6 of CDEV. This magic number will preceed the buffer length in
// * a clip buffer and will be used to validate that the client is using the
// * same protocol as the server.
// *****************************************************************************
#define CLIP_MAGIC_NUMBER 0x434C4950
@@ -0,0 +1,2 @@
#define _FIFO_QUEUE_MASTER_ 1
#include <fifo.h>
+315
View File
@@ -0,0 +1,315 @@
/*-----------------------------------------------------------------------------
* Copyright (c) 1991,1992 Southeastern Universities Research Association,
* Continuous Electron Beam Accelerator Facility
*
* This software was developed under a United States Government license
* described in the NOTICE file included as part of this distribution.
*
*-----------------------------------------------------------------------------
*
* Description:
*
* Author: Walt Akers
*
* Revision History:
* fifo.h,v
* Revision 1.4 1997/12/22 15:15:45 akers
* Ongoing development with new cdevReactor
*
* Revision 1.3 1996/09/13 18:47:18 akers
* Installed free list for node objects
*
* Revision 1.2 1996/07/16 15:48:55 akers
* Ongoing Modifications
*
*-----------------------------------------------------------------------------
*/
#ifndef __FIFO_H_
#define __FIFO_H_ 1
#include <cdevPlatforms.h>
class GENERIC_SERVER_API QueueBase
{
public:
class node
{
friend class QueueBase;
private:
node ( void ) : binary(NULL), binaryLen(0), next(NULL) { }
~node ( void ) { }
public:
void * binary;
size_t binaryLen;
class node * next;
};
protected:
node * head;
node * tail;
private:
enum { NEWCNT = 32 };
static node * freeList_;
public:
QueueBase ( void )
: head(NULL), tail(NULL)
{
}
virtual ~QueueBase ( void )
{
}
static node * newNode ( void )
{
node * result;
if(freeList_==NULL)
{
freeList_ = new node[NEWCNT];
for(int i=0; i<NEWCNT; i++)
{
freeList_[i].binary = NULL;
freeList_[i].binaryLen = 0;
freeList_[i].next = (i<(NEWCNT-1))?&freeList_[i+1]:(node *)NULL;
}
}
result = &freeList_[0];
freeList_ = result->next;
result->binary = NULL;
result->binaryLen = 0;
result->next = NULL;
return result;
}
static void deleteNode ( node * ptr)
{
if(ptr->next!=NULL) deleteNode(ptr->next);
ptr->next = freeList_;
freeList_ = ptr;
}
};
class GENERIC_SERVER_API FifoQueue : public QueueBase
{
private:
int nodeCnt;
public:
FifoQueue ( void ) : QueueBase(), nodeCnt(0) {}
virtual ~FifoQueue ( void )
{
while(dequeue()!=NULL);
}
int empty ( void )
{
return head==NULL?1:0;
}
void enqueue ( void * ptr )
{
if(ptr != NULL)
{
if(tail==NULL) tail = newNode();
else
{
tail->next = newNode();
tail = tail->next;
}
tail->binary = ptr;
if(head==NULL) head = tail;
nodeCnt++;
}
}
void enqueue ( char * ptr, size_t len )
{
if(ptr != NULL && len > 0 )
{
if(tail==NULL) tail = newNode();
else {
tail->next = newNode();
tail = tail->next;
}
tail->binary = ptr;
tail->binaryLen = len;
if(head==NULL) head = tail;
nodeCnt++;
}
}
void * dequeue ( void )
{
void * result = NULL;
if(head!=NULL)
{
node * next = head->next;
result = head->binary;
head->next = NULL;
deleteNode(head);
head = next;
if(head==NULL) tail = NULL;
nodeCnt--;
}
return result;
}
int dequeue ( char ** ptr, size_t * len )
{
int result = -1;
*ptr = NULL;
*len = 0;
if(head!=NULL)
{
node * next = head->next;
result = 0;
*ptr = (char *)head->binary;
*len = head->binaryLen;
head->next = NULL;
deleteNode(head);
head = next;
if(head==NULL) tail = NULL;
nodeCnt--;
}
return result;
}
int peek ( char ** ptr, size_t *len )
{
int result = -1;
if(head!=NULL)
{
*ptr = (char *)head->binary;
*len = head->binaryLen;
result = 0;
}
return result;
}
void undequeue ( void * ptr )
{
if(ptr != NULL)
{
if(tail==NULL) enqueue(ptr);
else {
node * headnode = newNode();
headnode->binary = ptr;
headnode->next = head;
head = headnode;
nodeCnt++;
}
}
}
void undequeue ( char *ptr, size_t len)
{
if(ptr != NULL && len > 0)
{
if(tail==NULL) enqueue(ptr, len);
else {
node * headnode = newNode();
headnode->binary = ptr;
headnode->binaryLen = len;
headnode->next = head;
head = headnode;
nodeCnt++;
}
}
}
int getCount ( void ) { return nodeCnt; }
};
class GENERIC_SERVER_API ListQueue : public QueueBase
{
protected:
node * prev;
node * bookmark;
public:
ListQueue ( void ) : QueueBase ()
{
prev = NULL;
bookmark = NULL;
}
virtual ~ListQueue ( void )
{
while(first()==0) remove();
}
int first ( void )
{
prev = NULL;
bookmark = head;
return (bookmark==NULL?-1:0);
}
int next ( void )
{
if(bookmark!=NULL)
{
prev = bookmark;
bookmark = bookmark->next;
}
return (bookmark==NULL?-1:0);
}
void * dequeue ( void )
{
return (bookmark==NULL?NULL:bookmark->binary);
}
void enqueue ( void * ptr )
{
if(ptr != NULL)
{
prev = tail;
if(tail==NULL) tail = newNode();
else
{
tail->next = newNode();
tail = tail->next;
}
tail->binary = ptr;
bookmark = tail;
if(head==NULL) head = tail;
}
}
int remove ( void )
{
int retval = -1;
if(bookmark != NULL)
{
if(prev!=NULL) prev->next = bookmark->next;
else head = bookmark->next;
bookmark->next = NULL;
deleteNode(bookmark);
if(prev!=NULL) {
bookmark = prev->next;
}
else if(head!=NULL && head->next!=NULL) {
bookmark = head->next;
}
else bookmark = head;
for( tail=head;
tail!=NULL && tail->next!=NULL;
tail=tail->next);
retval = 0;
}
else if(head!=NULL) first();
else tail = NULL;
return retval;
}
};
#ifdef _FIFO_QUEUE_MASTER_
QueueBase::node * QueueBase::freeList_ = NULL;
#endif
#endif // __FIFO_H_
@@ -0,0 +1,21 @@
#include <AddressIndex.h>
#include <ClientInfo.h>
#include <ErrorReporter.h>
#include <FD_Trigger.h>
#include <IntHash.h>
#include <LinkList.h>
#include <ServerInfo.h>
#include <SignalManager.h>
#include <SocketUtil.h>
#include <StringHash.h>
#include <VarStrHash.h>
#include <cdevGenericServerTags.h>
#include <cdevPlatforms.h>
#include <clipMagicNumber.h>
#include <fifo.h>
int main()
{
return 0;
}