SOL9 2.0 Class: IPv4AddressArray

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * IPv4AddressArray.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9 2.0

// 2011/1/1 

#pragma once
#include <sol/dns/DnsObject.h>
#include <sol/StringT.h>
#include <sol/net/IPv4Address.h>

/*
typedef DWORD IP4_ADDRESS;
typedef struct in_addr {
  union {
    struct {
      u_char s_b1,s_b2,s_b3,s_b4;
    } S_un_b;
    struct {
      u_short s_w1,s_w2;
    } S_un_w;
    u_long S_addr;
  } S_un;
} IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;

typedef struct _IP4_ARRAY 
{ 
DWORD AddrCount; 
#ifdef MIDL_PASS 
[size_is( AddrCount )] IP4_ADDRESS AddrArray[]; 
#else 
IP4_ADDRESS AddrArray[1]; 
#endif 
} IP4_ARRAY, *PIP4_ARRAY; 

*/

namespace SOL {

class IPv4AddressArray :public Object {
private:
    size_t      count;

private:
    IP4_ARRAY* ipv4Array;

public:

    IPv4AddressArray()
        :count(0),
        ipv4Array(NULL)
    {
    }

public:
    ~IPv4AddressArray()
    {
        clear();
    }


public:
    void allocArray(size_t c)
    {
        if (c > 0) {
            count = c;
            int allocSize = sizeof(DWORD) + sizeof(IP4_ADDRESS)*count;
            ipv4Array = (IP4_ARRAY*)new char[allocSize];
            
            memset(ipv4Array, 0, allocSize);
            ipv4Array->AddrCount = count;

        } else {
            throw -1;
        }
    }

public:
    void clear()
    {
        if (ipv4Array) {
            delete [] (char*)ipv4Array;
            ipv4Array = NULL;
            count = 0;
        }
    }

public:
    void setNth(int n, in_addr addr)
    {
        if (n>= 0 && n<count) {
            if (ipv4Array) {
                ipv4Array->AddrArray[n] = addr.s_addr;
                //printf("setNth(%s)\n", inet_ntoa(addr));
            } else {
                throw -1;
            }
        } else {
            throw -1;
        }
    }

public:
    IP4_ARRAY* getArray() 
    {
        return ipv4Array;
    }

public:
    operator const IP4_ARRAY*() const
    {
        return ipv4Array;
    }


public:
    size_t getCount()
    {
        return count;
    }

public:
    void display()
    {
        if (ipv4Array && count>0) {
            _tprintf(_T("DnsServer: IPv4AddressArray(count=%d)\n"), count);
            for (int i=0; i<count; i++) {
                IPv4Address ipv4address;
                StringT<TCHAR> string;
                ipv4address.toString(ipv4Array->AddrArray[i], string);
                _tprintf(_T("\tIP4_ADDRESS[%d]=%s\n"), i, (const TCHAR*)string);
            }
        }
    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.