Back

// PSLIPv6.cpp : Implementation of CPSLIPv6

#include "stdafx.h"
#include "PSLIPv6.h"

CPSLIPv6::CPSLIPv6()
{
   ::memset(m_uAddr, 0, sizeof(m_uAddr));
   m_uPort = 0;
   m_uPrefix = 0;
   m_Format = ip6Default;
}

HRESULT CPSLIPv6::FinalConstruct()
{
   return S_OK;
}

void CPSLIPv6::FinalRelease()
{
}

/*
Formats;

with prefix: x:x:x:x:x:x:x:x/prefix
with port: [x:x:x:x:x:x:x:x]:port
with time zone: x:x:x:x:x:x:x:x%zone

with everything: [x:x:x:x:x:x:x:x%zone/prefix]:port
which is invalid, by the way :)

*/
_bstr_t CPSLIPv6::GetIPName()
{
   int iCompressIdx = -1;  // Index of the largest zero gap;
   int iMaxGapSize = 0; // Size of the largest zero gap;

   if(m_Format & ip6Compressed) // If zero-compression is required;
   {
      int iCurrentGapSize = 0;
      for(int i = 0;i < 8;i ++)
      {
         if(m_uAddr[i])
            iCurrentGapSize = 0;
         else
            iCurrentGapSize ++;
         if(iCurrentGapSize > iMaxGapSize)
         {
            iMaxGapSize = iCurrentGapSize;
            iCompressIdx = i - iMaxGapSize + 1;
         }
      }
      // Now we have gap from index iCompressIdx of size iMaxGapSize,
      // or no zero gap at all, if iMaxGapSize is 0 or iCompressIdx is -1;
   }

   TCHAR separator = ':';
   if(m_Format & ip6WebAddress)
      separator = '-';

   // further parsing...

   return _bstr_t("NOT IMPLEMENTED");
}

/*
An empty string is a valid zero address
when passed during object initialization,
i.e. bCreating = true;
*/
bool CPSLIPv6::Initialize(LPCTSTR sIP, bool bCreating)
{
   tstring s(sIP);
   remove_chars(s, SPACES);
   size_t size = s.length();
   if(!size)
   {
      if(bCreating)
      {
         // It is Ok to pass an empty string
         // to create a zero-address object;
         Clear(VARIANT_FALSE);
         return true;
      }
      return false;
   }

   if(size < 2) // 2 is the smallest size of a valid IP address "::";
      return false; // Invalid IP address;

   make_upper(s);

   // Now parse the address here
   // ...

   // NOT IMPLEMENTED YET!

   return true;
}

////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////

STDMETHODIMP CPSLIPv6::get_A1(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[0];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A1(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[0] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A2(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[1];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A2(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[1] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A3(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[2];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A3(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[2] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A4(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[3];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A4(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[3] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A5(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[4];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A5(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[4] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A6(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[5];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A6(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[5] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A7(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[6];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A7(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[6] = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_A8(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uAddr[7];

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_A8(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uAddr[7] = (USHORT)newValue;

   PSL_END
}


STDMETHODIMP CPSLIPv6::get_Port(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uPort;

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_Port(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uPort = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_Prefix(long * pValue)
{
   PSL_BEGIN

   *pValue = m_uPrefix;

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_Prefix(long newValue)
{
   PSL_BEGIN

   if(newValue < 0 || newValue > 0xFFFF)
      return MakeException(EX_INVALIDPARAMETER);

   m_uPrefix = (USHORT)newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_Text(BSTR * pValue)
{
   PSL_BEGIN

   *pValue = GetIPName().copy();

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_Text(BSTR newValue)
{
   PSL_BEGIN

   if(!newValue)
      return MakeException(EX_INVALIDPARAMETER);

   if(!Initialize(_bstr_t(newValue), false))
      SetException(EX_INVALIDPARAMETER);

   PSL_END
}

STDMETHODIMP CPSLIPv6::get_Format(PSLIPv6Format * pValue)
{
   PSL_BEGIN

   *pValue = m_Format;

   PSL_END
}

STDMETHODIMP CPSLIPv6::put_Format(PSLIPv6Format newValue)
{
   PSL_BEGIN

   m_Format = newValue;

   PSL_END
}

STDMETHODIMP CPSLIPv6::Clear(VARIANT_BOOL bClearFormat)
{
   PSL_BEGIN

   ::memset(m_uAddr, 0, sizeof(m_uAddr));
   m_uPort = 0;
   m_uPrefix = 0;
   if(bClearFormat)
      m_Format = ip6Default;

   PSL_END
}

Top