Created Linux IPC libraries to be used by the AuthToken components.
At this point there is still work to do on them.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
#######################################################################
|
||||
#
|
||||
# Copyright (C) 2006 Novell, Inc.
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public
|
||||
# License along with this program; if not, write to the Free
|
||||
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
#
|
||||
# Author: Juan Carlos Luciani <jluciani@novell.com>
|
||||
#
|
||||
#######################################################################
|
||||
|
||||
SUBDIRS =
|
||||
|
||||
DIST_SUBDIRS =
|
||||
|
||||
CFILES =
|
||||
|
||||
EXTRA_DIST = $(CFILES) *.h
|
||||
|
||||
.PHONY: package package-clean package-install package-uninstall
|
||||
package package-clean package-install package-uninstall:
|
||||
$(MAKE) -C $(TARGET_OS) $@
|
||||
|
||||
maintainer-clean-local:
|
||||
rm -f Makefile.in
|
||||
|
||||
@@ -0,0 +1,388 @@
|
||||
/***********************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, Novell, Inc.
|
||||
*
|
||||
* To contact Novell about this file by physical or electronic mail,
|
||||
* you may find current contact information at www.novell.com.
|
||||
*
|
||||
* Author: Juan Carlos Luciani <jluciani@novell.com>
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
//===[ Include files ]=====================================================
|
||||
|
||||
#include "ipcint.h"
|
||||
|
||||
//===[ External data ]=====================================================
|
||||
|
||||
//===[ External prototypes ]===============================================
|
||||
|
||||
//===[ Manifest constants ]================================================
|
||||
|
||||
//===[ Type definitions ]==================================================
|
||||
|
||||
//===[ Function prototypes ]===============================================
|
||||
|
||||
//===[ Global variables ]==================================================
|
||||
|
||||
// Channel Packet Types
|
||||
string DataCarrierTypeTemplate = "TypeXX";
|
||||
string ReqDataCarrierType = "Type01";
|
||||
string ReqErrorCarrierType = "Type02";
|
||||
|
||||
// Channel Packet Headers
|
||||
string ReqIdHdr = "ReqIdHdr =";
|
||||
string PayloadLengthHdr = "PayloadLength =";
|
||||
|
||||
// Req Data Pkt Hdr Template
|
||||
string ReqDataPktHdrTemplate = "Type01\r\nReqIdHdr =XXXXXXXX\r\nPayloadLength =XXXXXXXX\r\n\r\n";
|
||||
|
||||
// Req Error Pkt Hdr Template
|
||||
string ReqErrorPktHdrTemplate = "Type02\r\nReqIdHdr =XXXXXXXX\r\nPayloadLength =XXXXXXXX\r\n\r\n";
|
||||
|
||||
|
||||
//++=======================================================================
|
||||
int
|
||||
ChannelProto::buildReqDataPktHdr(
|
||||
uint32_t reqId,
|
||||
int32_t payloadLength,
|
||||
char *pPktHdr)
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// Abstract:
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// L2
|
||||
//=======================================================================--
|
||||
{
|
||||
int retStatus = -1;
|
||||
|
||||
DbgTrace(1, "ChannelProto::buildReqDataPktHdr- Start\n", 0);
|
||||
|
||||
try {
|
||||
// - Req Data Packet Header Format -
|
||||
//
|
||||
// ReqDataCarrierType
|
||||
// ReqIdHdr value (value format=%08X)
|
||||
// PayloadLengthHdr value (value format=%08X)
|
||||
//
|
||||
|
||||
// Setup the necessary value strings
|
||||
char wrkBuffer[10];
|
||||
|
||||
sprintf(wrkBuffer, "%08X", reqId);
|
||||
string reqIdValue = wrkBuffer;
|
||||
sprintf(wrkBuffer, "%08X", payloadLength);
|
||||
string payloadLengthValue = wrkBuffer;
|
||||
|
||||
// Format the header.
|
||||
char* pCurr = pPktHdr;
|
||||
|
||||
memcpy(pCurr, ReqDataCarrierType.c_str(), ReqDataCarrierType.length());
|
||||
pCurr += ReqDataCarrierType.length();
|
||||
memcpy(pCurr, "\r\n", 2);
|
||||
pCurr += 2;
|
||||
|
||||
memcpy(pCurr, ReqIdHdr.c_str(), ReqIdHdr.length());
|
||||
pCurr += ReqIdHdr.length();
|
||||
memcpy(pCurr, reqIdValue.c_str(), reqIdValue.length());
|
||||
pCurr += reqIdValue.length();
|
||||
memcpy(pCurr, "\r\n", 2);
|
||||
pCurr += 2;
|
||||
|
||||
memcpy(pCurr, PayloadLengthHdr.c_str(), PayloadLengthHdr.length());
|
||||
pCurr += PayloadLengthHdr.length();
|
||||
memcpy(pCurr, payloadLengthValue.c_str(), payloadLengthValue.length());
|
||||
pCurr += payloadLengthValue.length();
|
||||
memcpy(pCurr, "\r\n\r\n", 4);
|
||||
|
||||
// Success
|
||||
retStatus = 0;
|
||||
}
|
||||
catch (...) {
|
||||
DbgTrace(0, "ChannelProto::buildReqDataPktHdr- Exception caught while creating header\n", 0);
|
||||
}
|
||||
|
||||
DbgTrace(1, "ChannelProto::buildReqDataPktHdr- End, retStatus = %08X\n", retStatus);
|
||||
|
||||
return retStatus;
|
||||
|
||||
} /*-- ChannelProto::buildReqDataPktHdr() --*/
|
||||
|
||||
|
||||
//++=======================================================================
|
||||
int
|
||||
ChannelProto::buildReqErrorPktHdr(
|
||||
uint32_t reqId,
|
||||
int32_t payloadLength,
|
||||
char *pPktHdr)
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// Abstract:
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// L2
|
||||
//=======================================================================--
|
||||
{
|
||||
int retStatus = -1;
|
||||
|
||||
DbgTrace(1, "ChannelProto::buildReqErrorPktHdr- Start\n", 0);
|
||||
|
||||
try {
|
||||
// - Req Error Packet Header Format -
|
||||
//
|
||||
// ReqErrorCarrierType
|
||||
// ReqIdHdr value (value format=%08X)
|
||||
// PayloadLengthHdr value (value format=%08X)
|
||||
//
|
||||
|
||||
// Setup the necessary value strings
|
||||
char wrkBuffer[10];
|
||||
|
||||
sprintf(wrkBuffer, "%08X", reqId);
|
||||
string reqIdValue = wrkBuffer;
|
||||
sprintf(wrkBuffer, "%08X", payloadLength);
|
||||
string payloadLengthValue = wrkBuffer;
|
||||
|
||||
// Format the header.
|
||||
char* pCurr = pPktHdr;
|
||||
|
||||
memcpy(pCurr, ReqErrorCarrierType.c_str(), ReqErrorCarrierType.length());
|
||||
pCurr += ReqErrorCarrierType.length();
|
||||
memcpy(pCurr, "\r\n", 2);
|
||||
pCurr += 2;
|
||||
|
||||
memcpy(pCurr, ReqIdHdr.c_str(), ReqIdHdr.length());
|
||||
pCurr += ReqIdHdr.length();
|
||||
memcpy(pCurr, reqIdValue.c_str(), reqIdValue.length());
|
||||
pCurr += reqIdValue.length();
|
||||
memcpy(pCurr, "\r\n", 2);
|
||||
pCurr += 2;
|
||||
|
||||
memcpy(pCurr, PayloadLengthHdr.c_str(), PayloadLengthHdr.length());
|
||||
pCurr += PayloadLengthHdr.length();
|
||||
memcpy(pCurr, payloadLengthValue.c_str(), payloadLengthValue.length());
|
||||
pCurr += payloadLengthValue.length();
|
||||
memcpy(pCurr, "\r\n\r\n", 4);
|
||||
|
||||
// Success
|
||||
retStatus = 0;
|
||||
}
|
||||
catch (...) {
|
||||
DbgTrace(0, "ChannelProto::buildReqErrorPktHdr- Exception caught while creating header\n", 0);
|
||||
}
|
||||
|
||||
DbgTrace(1, "ChannelProto::buildReqErrorPktHdr- End, retStatus = %08X\n", retStatus);
|
||||
|
||||
return retStatus;
|
||||
|
||||
} /*-- ChannelProto::buildReqErrorPktHdr() --*/
|
||||
|
||||
|
||||
//++=======================================================================
|
||||
ChannelProto::PacketTypes
|
||||
ChannelProto::getPktType(
|
||||
char &buff)
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// Abstract:
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// L2
|
||||
//=======================================================================--
|
||||
{
|
||||
PacketTypes packetType = UnknownPacketType;
|
||||
|
||||
DbgTrace(1, "ChannelProto::getPktType- Start\n", 0);
|
||||
|
||||
// Find the end of the Channel Packet Type
|
||||
char *pCurr = &buff;
|
||||
while (*pCurr != '\r')
|
||||
pCurr ++;
|
||||
|
||||
// Found the end of the Channel Packet Type, now
|
||||
// calculate its length.
|
||||
int channelPktTypeLength = pCurr - &buff;
|
||||
|
||||
// Now start comparing
|
||||
if (channelPktTypeLength == ReqDataCarrierType.length()
|
||||
&& !memcmp(&buff, ReqDataCarrierType.c_str(), channelPktTypeLength))
|
||||
{
|
||||
// The type is Channel Req Data Carrier
|
||||
packetType = ReqDataCarrierPacketType;
|
||||
}
|
||||
else if (channelPktTypeLength == ReqErrorCarrierType.length()
|
||||
&& !memcmp(&buff, ReqErrorCarrierType.c_str(), channelPktTypeLength))
|
||||
{
|
||||
// The type is Channel Req Error Carrier
|
||||
packetType = ReqErrorCarrierPacketType;
|
||||
}
|
||||
else
|
||||
{
|
||||
DbgTrace(0, "ChannelProto::getPktType- No match found\n", 0);
|
||||
}
|
||||
|
||||
DbgTrace(1, "ChannelProto::getPktType- End, type = %d\n", packetType);
|
||||
|
||||
return packetType;
|
||||
|
||||
} /*-- ChannelProto::getPktType() --*/
|
||||
|
||||
|
||||
//++=======================================================================
|
||||
bool
|
||||
ChannelProto::getReqIdAndPayloadLength(
|
||||
char *pBuff,
|
||||
int hdrLength,
|
||||
uint32_t *pReqId,
|
||||
int32_t *pPayloadLength)
|
||||
//
|
||||
// Arguments:
|
||||
//
|
||||
// Returns:
|
||||
//
|
||||
// Abstract:
|
||||
//
|
||||
// Notes:
|
||||
//
|
||||
// L2
|
||||
//=======================================================================--
|
||||
{
|
||||
bool reqIdObtained = false;
|
||||
bool payloadLengthObtained = false;
|
||||
|
||||
DbgTrace(1, "ChannelProto::getReqIdAndPayloadLength- Start\n", 0);
|
||||
|
||||
char *pCurr = pBuff;
|
||||
char *pChannelHdr = NULL;
|
||||
int bytesLeft = hdrLength;
|
||||
|
||||
// Skip the Channel Packet Type
|
||||
while (bytesLeft >= 2)
|
||||
{
|
||||
if (*pCurr == '\r'
|
||||
&& *(pCurr+1) == '\n')
|
||||
{
|
||||
// Found the end of the channel packet type
|
||||
pCurr += 2;
|
||||
bytesLeft -= 2;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
pCurr ++;
|
||||
bytesLeft --;
|
||||
}
|
||||
}
|
||||
|
||||
// Start processing Channel Packet Headers
|
||||
pChannelHdr = pCurr;
|
||||
while (bytesLeft >= 2
|
||||
&& (!reqIdObtained || !payloadLengthObtained))
|
||||
{
|
||||
if (*pCurr == '\r'
|
||||
&& *(pCurr+1) == '\n')
|
||||
{
|
||||
// Found the end of the current channel header
|
||||
pCurr += 2;
|
||||
bytesLeft -= 2;
|
||||
|
||||
// Check if the line is empty or if it contained a
|
||||
// channel header.
|
||||
if ((pCurr - pChannelHdr) == 2)
|
||||
{
|
||||
// This was an empty line, which means that
|
||||
// we reached the end of the channel packet header.
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Check if the header is the Req Id Hdr
|
||||
if (!reqIdObtained && (pCurr - pChannelHdr) > ReqIdHdr.length()
|
||||
&& !memcmp(pChannelHdr, ReqIdHdr.c_str(), ReqIdHdr.length()))
|
||||
{
|
||||
// We found the Req Id Hdr, get the value.
|
||||
char *pValue = pChannelHdr + ReqIdHdr.length();
|
||||
|
||||
// Temporarily NULL terminate the value
|
||||
*(pCurr-2) = '\0';
|
||||
|
||||
// Convert the value to hex
|
||||
*pReqId = strtoul(pValue, NULL, 16);
|
||||
|
||||
// Undo the damage that we did
|
||||
*(pCurr-2) = '\r';
|
||||
|
||||
// Remember that the Req Id was obtained
|
||||
reqIdObtained = true;
|
||||
}
|
||||
// Check if the header is the Payload Length Hdr
|
||||
else if ((pCurr - pChannelHdr) > PayloadLengthHdr.length()
|
||||
&& !memcmp(pChannelHdr, PayloadLengthHdr.c_str(), PayloadLengthHdr.length()))
|
||||
{
|
||||
// We found the Payload Length Hdr, get the value.
|
||||
char *pValue = pChannelHdr + PayloadLengthHdr.length();
|
||||
|
||||
// Temporarily NULL terminate the value
|
||||
*(pCurr-2) = '\0';
|
||||
|
||||
// Convert the value to hex
|
||||
*pPayloadLength = strtoul(pValue, NULL, 16);
|
||||
|
||||
// Undo the damage that we did
|
||||
*(pCurr-2) = '\r';
|
||||
|
||||
// Remember that the Payload Lenght was obtained
|
||||
payloadLengthObtained = true;
|
||||
}
|
||||
|
||||
// Get set to process the next header
|
||||
pChannelHdr = pCurr;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pCurr ++;
|
||||
bytesLeft --;
|
||||
}
|
||||
}
|
||||
|
||||
DbgTrace(1,
|
||||
"ChannelProto::getReqIdAndPayloadLength- End, retStatus = %08X\n",
|
||||
reqIdObtained && payloadLengthObtained);
|
||||
|
||||
return reqIdObtained && payloadLengthObtained;
|
||||
|
||||
} /*-- ChannelProto::getReqIdAndPayloadLength() --*/
|
||||
|
||||
|
||||
//=========================================================================
|
||||
//=========================================================================
|
||||
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
/***********************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, Novell, Inc.
|
||||
*
|
||||
* To contact Novell about this file by physical or electronic mail,
|
||||
* you may find current contact information at www.novell.com.
|
||||
*
|
||||
* Author: Juan Carlos Luciani <jluciani@novell.com>
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
#ifndef _CHANNELPROTO_
|
||||
#define _CHANNELPROTO_
|
||||
|
||||
//===[ Include files ]=====================================================
|
||||
|
||||
//===[ External data ]=====================================================
|
||||
|
||||
extern string ReqDataPktHdrTemplate;
|
||||
extern string ReqErrorPktHdrTemplate;
|
||||
|
||||
//===[ External prototypes ]===============================================
|
||||
|
||||
//===[ Manifest constants ]================================================
|
||||
|
||||
//===[ Type definitions ]==================================================
|
||||
|
||||
//===[ Function prototypes ]===============================================
|
||||
|
||||
//===[ Global variables ]==================================================
|
||||
|
||||
//===[ Type definitions ]==================================================
|
||||
|
||||
//
|
||||
// ChannelProto Class Definition
|
||||
//
|
||||
class ChannelProto
|
||||
{
|
||||
public:
|
||||
|
||||
// Packet Types
|
||||
enum PacketTypes
|
||||
{
|
||||
ReqDataCarrierPacketType = 1,
|
||||
ReqErrorCarrierPacketType,
|
||||
UnknownPacketType
|
||||
};
|
||||
|
||||
//
|
||||
// Build Req Data Packet Header routine
|
||||
//
|
||||
// Parameters:
|
||||
// reqId (input) -
|
||||
// Req id.
|
||||
//
|
||||
// payloadLength (input) -
|
||||
// Length of the payload being carried by the packet.
|
||||
//
|
||||
// pPktHdt (input/output) -
|
||||
// Pointer to buffer that will receive the header.
|
||||
// Note, this buffer needs to be big eneough to
|
||||
// contain the ReqDataPktHdrTemplate string.
|
||||
//
|
||||
// Abstract: Returns Req Data Pkt Hdr for the specified
|
||||
// parameters.
|
||||
//
|
||||
// Returns: 0 if successful.
|
||||
//
|
||||
static int buildReqDataPktHdr(uint32_t reqId,
|
||||
int32_t payloadLength,
|
||||
char *pPktHdr);
|
||||
|
||||
//
|
||||
// Build Req Error Packet Header routine
|
||||
//
|
||||
// Parameters:
|
||||
// reqId (input) -
|
||||
// Req id.
|
||||
//
|
||||
// payloadLength (input) -
|
||||
// Length of the payload being carried by the packet.
|
||||
//
|
||||
// pPktHdt (input/output) -
|
||||
// Pointer to buffer that will receive the header.
|
||||
// Note, this buffer needs to be big eneough to
|
||||
// contain the ReqErrorPktHdrTemplate string.
|
||||
//
|
||||
// Abstract: Returns Req Error Pkt Hdr for the specified
|
||||
// parameters.
|
||||
//
|
||||
// Returns: 0 if successful.
|
||||
//
|
||||
static int buildReqErrorPktHdr(uint32_t reqId,
|
||||
int32_t payloadLength,
|
||||
char *pPktHdr);
|
||||
|
||||
//
|
||||
// Get Channel Packet Type routine
|
||||
//
|
||||
// Parameters:
|
||||
// buff (input) -
|
||||
// Reference to buffer containing the packet data.
|
||||
//
|
||||
// Abstract: Returns the type of the specified channel packet.
|
||||
//
|
||||
// Returns: Channel packet type.
|
||||
//
|
||||
static PacketTypes getPktType(char &buff);
|
||||
|
||||
//
|
||||
// Get Req Id and Payload Length Values routine
|
||||
//
|
||||
// Parameters:
|
||||
// buff (input) -
|
||||
// Reference to buffer containing the packet data.
|
||||
//
|
||||
// hdrLength (input) -
|
||||
// Length of the channel header.
|
||||
//
|
||||
// pReqId (input/output) -
|
||||
// Pointer to variable that will receive the req id.
|
||||
//
|
||||
// pPayloadLength (input/output) -
|
||||
// Pointer to variable that will receive the payload length.
|
||||
//
|
||||
// Abstract: Returns the values of the ReqId and PayloadLength headers
|
||||
// present in the channel packet header.
|
||||
//
|
||||
// Returns: True if successful.
|
||||
//
|
||||
static bool getReqIdAndPayloadLength(char *pBuff,
|
||||
int hdrLength,
|
||||
uint32_t *pReqId,
|
||||
int32_t *pPayloadLength);
|
||||
};
|
||||
|
||||
|
||||
//===[ Function prototypes ]===============================================
|
||||
|
||||
|
||||
#endif // _CHANNELPROTO_
|
||||
|
||||
//=========================================================================
|
||||
//=========================================================================
|
||||
149
CASA-auth-token/non-java/utilities/IpcLibs/linux/common/ipcint.h
Normal file
149
CASA-auth-token/non-java/utilities/IpcLibs/linux/common/ipcint.h
Normal file
@@ -0,0 +1,149 @@
|
||||
/**********************\*************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, Novell, Inc.
|
||||
*
|
||||
* To contact Novell about this file by physical or electronic mail,
|
||||
* you may find current contact information at www.novell.com.
|
||||
*
|
||||
* Author: Juan Carlos Luciani <jluciani@novell.com>
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
#ifndef _IPCINT_
|
||||
#define _IPCINT_
|
||||
|
||||
//===[ Include files ]=====================================================
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <syslog.h>
|
||||
#include <signal.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/file.h>
|
||||
#include <assert.h> // Ensure that NDEBUG is defined for release builds!
|
||||
#include <sys/ipc.h>
|
||||
}
|
||||
|
||||
//===[ External data ]=====================================================
|
||||
|
||||
extern int DebugLevel;
|
||||
extern bool UseSyslog;
|
||||
extern char *pAppName;
|
||||
extern pthread_mutex_t interlockedMutex;
|
||||
|
||||
//===[ Macro definitions ]=================================================
|
||||
|
||||
//
|
||||
// DbgTrace macro define
|
||||
//
|
||||
#define MAX_FORMAT_STRING_LEN 1024
|
||||
#define DbgTrace(LEVEL, X, Y) { \
|
||||
if (LEVEL == 0) { \
|
||||
char *pFormatString = new char[MAX_FORMAT_STRING_LEN]; \
|
||||
if (pFormatString) { \
|
||||
snprintf(pFormatString, MAX_FORMAT_STRING_LEN, X, Y); \
|
||||
if (UseSyslog) \
|
||||
syslog(LOG_USER | LOG_INFO, "%s -%s", pAppName, pFormatString); \
|
||||
else \
|
||||
fprintf(stderr, "%s -%s", pAppName, pFormatString); \
|
||||
delete[] pFormatString; \
|
||||
} \
|
||||
} else if (DebugLevel >= LEVEL) { \
|
||||
char *pFormatString = new char[MAX_FORMAT_STRING_LEN]; \
|
||||
if (pFormatString) { \
|
||||
snprintf(pFormatString, MAX_FORMAT_STRING_LEN, X, Y); \
|
||||
if (UseSyslog) \
|
||||
syslog(LOG_USER | LOG_DEBUG, "%s -%s", pAppName, pFormatString); \
|
||||
else \
|
||||
fprintf(stderr, "%s -%s", pAppName, pFormatString); \
|
||||
delete[] pFormatString; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
//
|
||||
// Interlocked Increment and Decrement macros
|
||||
//
|
||||
// Well, kind of interlocked :-).
|
||||
//
|
||||
__inline static unsigned long
|
||||
InterlockedIncrement(unsigned long *pValue)
|
||||
{
|
||||
pthread_mutex_lock(&interlockedMutex);
|
||||
*pValue ++;
|
||||
pthread_mutex_unlock(&interlockedMutex);
|
||||
}
|
||||
|
||||
__inline static unsigned long
|
||||
InterlockedDecrement(unsigned long *pValue)
|
||||
{
|
||||
pthread_mutex_lock(&interlockedMutex);
|
||||
*pValue --;
|
||||
pthread_mutex_unlock(&interlockedMutex);
|
||||
}
|
||||
|
||||
__inline static uint32_t
|
||||
InterlockedIncrement(uint32_t *pValue)
|
||||
{
|
||||
pthread_mutex_lock(&interlockedMutex);
|
||||
*pValue ++;
|
||||
pthread_mutex_unlock(&interlockedMutex);
|
||||
}
|
||||
|
||||
__inline static uint32_t
|
||||
InterlockedDecrement(uint32_t *pValue)
|
||||
{
|
||||
pthread_mutex_lock(&interlockedMutex);
|
||||
*pValue --;
|
||||
pthread_mutex_unlock(&interlockedMutex);
|
||||
}
|
||||
|
||||
//===[ Include files ]=====================================================
|
||||
|
||||
#include "smartptr.h"
|
||||
#include "channelproto.h"
|
||||
|
||||
//===[ External prototypes ]===============================================
|
||||
|
||||
//===[ Manifest constants ]================================================
|
||||
|
||||
//===[ Type definitions ]==================================================
|
||||
|
||||
//===[ Function prototypes ]===============================================
|
||||
|
||||
|
||||
#endif // _IPCINT_
|
||||
|
||||
//=========================================================================
|
||||
//=========================================================================
|
||||
@@ -0,0 +1,280 @@
|
||||
/***********************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Novell, Inc. All Rights Reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, Novell, Inc.
|
||||
*
|
||||
* To contact Novell about this file by physical or electronic mail,
|
||||
* you may find current contact information at www.novell.com.
|
||||
*
|
||||
* Author: Juan Carlos Luciani <jluciani@novell.com>
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
#ifndef SMARTPTR_H
|
||||
#define SMARTPTR_H
|
||||
|
||||
/*******************************************************************************
|
||||
* Include Files
|
||||
*******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
*******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Types and Classes
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Reference Object
|
||||
//
|
||||
// In order to use the SmartPtr<> class, the object type used to instantiate
|
||||
// the SmartPtr template must inheirit from the ObjRef class.
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
class ObjRef
|
||||
{
|
||||
//---------------------------------------------------------------------------
|
||||
// Public interface
|
||||
//
|
||||
public:
|
||||
|
||||
ObjRef() : m_Count(0) {}
|
||||
|
||||
void IncRefCount(void)
|
||||
{
|
||||
InterlockedIncrement((unsigned long*)&m_Count);
|
||||
}
|
||||
|
||||
bool DecRefCount(void)
|
||||
{
|
||||
if ((m_Count > 0) && (InterlockedDecrement((unsigned long*)&m_Count) == 0))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int GetRefCount(void) const
|
||||
{
|
||||
return m_Count;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Private data
|
||||
//
|
||||
private:
|
||||
|
||||
// BUGBUG!! - Need to put a lock in here so the count can be updated atomically.
|
||||
// or use an interlocked inc/dec if one exists.
|
||||
mutable unsigned int m_Count;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// SmartPtr Object
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
template<class T>
|
||||
class SmartPtr
|
||||
{
|
||||
//---------------------------------------------------------------------------
|
||||
// Public interface
|
||||
//
|
||||
public:
|
||||
|
||||
SmartPtr();
|
||||
SmartPtr(T* ptr);
|
||||
SmartPtr(const SmartPtr<T>& ref);
|
||||
|
||||
virtual ~SmartPtr();
|
||||
|
||||
operator bool (void) const;
|
||||
bool operator! (void) const;
|
||||
bool operator== (SmartPtr<T>& ref) const;
|
||||
bool operator!= (SmartPtr<T>& ref) const;
|
||||
|
||||
SmartPtr<T>& operator= (const SmartPtr<T>& ref);
|
||||
SmartPtr<T>& operator= (T* ptr);
|
||||
|
||||
T& operator* (void) const;
|
||||
T* operator-> (void) const;
|
||||
operator T* (void) const;
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Private interface
|
||||
//
|
||||
private:
|
||||
|
||||
void deleteObject(void);
|
||||
void resetPtr(T* newPtr);
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
// Private data
|
||||
//
|
||||
private:
|
||||
|
||||
T* m_Ptr;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::SmartPtr() :
|
||||
m_Ptr(0)
|
||||
{
|
||||
} // End of SmartPtr::SmartPtr()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::SmartPtr(T* ptr) :
|
||||
m_Ptr(0)
|
||||
{
|
||||
resetPtr(ptr);
|
||||
|
||||
} // End of SmartPtr::SmartPtr()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::SmartPtr(const SmartPtr<T>& ref) :
|
||||
m_Ptr(0)
|
||||
{
|
||||
resetPtr(ref.m_Ptr);
|
||||
|
||||
} // End of SmartPtr::SmartPtr()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::~SmartPtr()
|
||||
{
|
||||
deleteObject();
|
||||
|
||||
} // End of SmartPtr::~SmartPtr()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::operator bool (void) const
|
||||
{
|
||||
return m_Ptr != 0;
|
||||
|
||||
} // End of SmartPtr::operator bool()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline bool SmartPtr<T>::operator! (void) const
|
||||
{
|
||||
return m_Ptr == 0;
|
||||
|
||||
} // End of SmartPtr::operator!()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline bool SmartPtr<T>::operator== (SmartPtr<T>& ref) const
|
||||
{
|
||||
return m_Ptr == ref.m_Ptr;
|
||||
|
||||
} // End of SmartPtr::operator==()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline bool SmartPtr<T>::operator!= (SmartPtr<T>& ref) const
|
||||
{
|
||||
return m_Ptr != ref.m_Ptr;
|
||||
|
||||
} // End of SmartPtr::operator==()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>& SmartPtr<T>::operator= (const SmartPtr<T>& ref)
|
||||
{
|
||||
resetPtr(ref.m_Ptr);
|
||||
return *this;
|
||||
|
||||
} // End of SmartPtr::operator=()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>& SmartPtr<T>::operator= (T* ptr)
|
||||
{
|
||||
resetPtr(ptr);
|
||||
return *this;
|
||||
|
||||
} // End of SmartPtr::operator=()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T& SmartPtr<T>::operator* (void) const
|
||||
{
|
||||
return *m_Ptr;
|
||||
|
||||
} // End of SmartPtr::operator*()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline T* SmartPtr<T>::operator-> (void) const
|
||||
{
|
||||
return m_Ptr;
|
||||
|
||||
} // End of SmartPtr::operator->()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline SmartPtr<T>::operator T* (void) const
|
||||
{
|
||||
return m_Ptr;
|
||||
|
||||
} // End of SmartPtr::operator T*()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void SmartPtr<T>::deleteObject(void)
|
||||
{
|
||||
if (m_Ptr && m_Ptr->DecRefCount())
|
||||
{
|
||||
delete m_Ptr;
|
||||
m_Ptr = 0;
|
||||
}
|
||||
|
||||
} // End of SmartPtr::deleteObject()
|
||||
|
||||
|
||||
template<class T>
|
||||
inline void SmartPtr<T>::resetPtr(T* newPtr)
|
||||
{
|
||||
if (m_Ptr != newPtr)
|
||||
{
|
||||
deleteObject();
|
||||
m_Ptr = newPtr;
|
||||
|
||||
if (m_Ptr)
|
||||
{
|
||||
// New object reference.
|
||||
m_Ptr->IncRefCount();
|
||||
}
|
||||
}
|
||||
|
||||
} // End of SmartPtr::resetPtr()
|
||||
|
||||
|
||||
#endif // SMARTPTR_H
|
||||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
Reference in New Issue
Block a user