读书人

哪位高手有pdu编码解码的列子

发布时间: 2012-03-05 11:54:01 作者: rapoo

谁有pdu编码解码的列子?
谢谢

[解决办法]
RadiusPDU::RadiusPDU()
:
code( 0 ),
id( 0 )
{
//memset(authenticator,0,sizeof(authenticator));
}

RadiusPDU::RadiusPDU(
const RadiusPDU& pdu
)
:
code( pdu.code ),
id( pdu.id )
{
memcpy(authenticator,pdu.authenticator,sizeof(authenticator));
attributes = pdu.attributes;
attributes.MakeUnique();
}

RadiusPDU::RadiusPDU(
unsigned char packetCode, /// code - see #Codes enum#
unsigned char packetId /// packet id (sequence number)
)
:
code( packetCode ),
id( packetId )
{
//memset(authenticator,0,sizeof(authenticator));
}

RadiusPDU::RadiusPDU(
unsigned char packetCode, /// code - see #Codes enum#
const RadiusAttr::List& attrs, /// attributes
unsigned char packetId /// packet id (sequence number)
)
:
code( packetCode ),
id( packetId )
{
attributes = attrs;
attributes.MakeUnique();
}

RadiusPDU::RadiusPDU(
const void* rawData, /// raw data buffer
PINDEX rawLength /// raw data length
)
:
code( 0 ),
id( 0 )
{
if( !Read(rawData,rawLength) ) {
attributes.RemoveAll();
code = id = 0;
}
}

RadiusPDU::~RadiusPDU()
{
}

PINDEX RadiusPDU::GetLength() const
{
PINDEX len = FixedHeaderLength;
const PINDEX numAttrs = attributes.GetSize();

for( PINDEX i = 0; i < numAttrs; i++ ) {
const RadiusAttr* const attr = (RadiusAttr*)(attributes.GetAt(i));
if( attr && attr-> IsValid() )
len += attr-> GetLength();
else
return 0;
}

return len;
}

void RadiusPDU::PrintOn(
ostream& strm /// Stream to print the object into.
) const
{
const int indent = strm.precision() + 2;

strm < < ((!IsValid())? "(Invalid) {\n ": "{\n ");

#if PTRACING
strm < < setw(indent+7) < < "code = " < < (unsigned)code
< < " ( " < < PMAP_CODE_TO_NAME(code) < < ")\n ";
#else
strm < < setw(indent+7) < < "code = " < < (unsigned)code < < '\n ';
#endif
strm < < setw(indent+5) < < "id = " < < (unsigned)id < < '\n ';
strm < < setw(indent+9) < < "length = " < < GetLength() < < " octets\n ";

const _Ios_Fmtflags flags = strm.flags();
const PBYTEArray value( (const BYTE*)authenticator, AuthenticatorLength, FALSE );

strm < < setw(indent+28) < < "authenticator = 16 octets {\n ";
strm < < hex < < setfill( '0 ') < < resetiosflags(ios::floatfield)
< < setprecision(indent+2) < < setw(16);
strm < < value < < '\n ';
strm < < dec < < setfill( ' ') < < setprecision(indent);
strm.flags(flags);
strm < < setw(indent+2) < < "}\n ";

if( attributes.GetSize() == 0 )
strm < < setw(indent+22) < < "attributes = < <null> > \n ";
else {
strm < < setw(indent+13) < < "attributes = " < < attributes.GetSize() < < " elements {\n ";

const int aindent = indent + 2;

for( PINDEX i = 0; i < attributes.GetSize(); i++ ) {


const RadiusAttr* const attr = (RadiusAttr*)(attributes.GetAt(i));
if( attr )
strm < < setw(aindent+1) < < "[ " < < i < < "]= "
< < setprecision(aindent) < < *attr
< < setprecision(indent);
}
strm < < setw(aindent) < < "}\n ";
}

strm < < setw(indent-1) < < "}\n " < < setprecision(indent-2);
}

BOOL RadiusPDU::IsValid() const
{
if( code == Invalid )
return FALSE;

PINDEX len = FixedHeaderLength;
const PINDEX numAttrs = attributes.GetSize();

for( PINDEX i = 0; i < numAttrs; i++ ) {
const RadiusAttr* const attr = (RadiusAttr*)(attributes.GetAt(i));
if( !(attr && attr-> IsValid()) )
return FALSE;
len += attr-> GetLength();
}

if( (len < MinPduLength) || (len > MaxPduLength) )
return FALSE;

return TRUE;
}

void RadiusPDU::GetAuthenticator( PBYTEArray& vector, PINDEX offset ) const
{
memcpy(vector.GetPointer(offset+AuthenticatorLength)+offset,
authenticator, AuthenticatorLength
);
}

BOOL RadiusPDU::SetAuthenticator( const PBYTEArray& vector, PINDEX offset )
{
PINDEX len = vector.GetSize();

if( offset > = len )
return FALSE;

len -= offset;

if( len > 0 )
memcpy( authenticator, ((const BYTE*)vector)+offset,
((len <AuthenticatorLength)?len:AuthenticatorLength)
);

return TRUE;
}

BOOL RadiusPDU::SetAuthenticator( const void* data )
{
#ifdef _DEBUG
PAssertNULL(data);
#endif
if( data == NULL )
return FALSE;

memcpy(authenticator,data,AuthenticatorLength);
return TRUE;
}

[解决办法]
http://faq.csdn.net/read/212510.html

读书人网 >C++ Builder

热点推荐