As IPV6 address has 16 bytes and their is no data type to store it in c++, 开发者_开发问答i wanted to store the IPV6 address, and do some comparisions between to IPv6 address, please let me know how to do it
You can store those as plain arrays of unsigned char
s as you would do in C. Like unsigned char ipv6[16];
for example. You could then compare them using any array comparison algorithm out there. Using char*
algorithms wouldn't be an option as some bytes of your ipv6 addresses could be 0x00
and thus interpreted as a string ending character by string-based algorithms.
You do not specify what platform or IP stack you are using. On windows the IPV6 address is stored in a structure call in6_addr. In that struct you have u_char Byte[16] for the address. And using std::memcmp() you could compare two structures.
On linux the proposed standard also call the struckt in6_addr and can be use in the same way as above. More info here.
Create wrapper class of BYTE array to store the ipv6 address and overload [] operator to access individual byte and you can overload operators for comparisons.
struct IPV6Address
{
unsigned char address[16];
unsigned char operator [] (int i) ; //
bool operator == (const IPV6Address &ipv6) { //write you own logic }
bool operator < (const IPV6Address &ipv6) { //write you own logic }
};
Another option is define your own structure which can override operators like == != [] etc. and inside it can be implemented like array of 16 chars or array of x ints which can be defined according to building architecture because you don't know size of int. There can be defined also operations for getting mask etc. This method allow easy usage of bit operators.
EDIT:
Are you using
std::list<MyIPStruct> iplist
ok?
When you are iterating through the list you can compare
iplist[i] < iplist[i+1]
and If I understood your question you you don't know how to override > operator?
struct MyIPStruct {
usigned char[16] bytes; // one option
// unsigned int[16 / sizeof(int)] bytes; // another option
/* other methods... */
bool operator > ( const MyIPStruct & ip2 ) { /* here is your code */}
};
Store your IPV6-array in std::vector
. STL vector already contains operator <
and ==
.
Store them as std::strings. You can then use the string comparison operators, as these won't be foxed by included null characters, at least with regards for tests for equality/inequality. For the relational tests ( <, > etc.) you will probably want to write your own functions, as the std::string ones probably won't do what you want.
for comparisions defining own structure is better, and one more thing i am storing the ip in Std list were i need to define overloaded operator < == functions.so how to define the < function any clue please
Recently had to deal with similiar questions with fairly busy code. Far from ideal the basic solution I used was to create a union with several different datatypes:
typedef union myip
{
unsigned char ip8[16];
unsigned int ip32[4];
unsigned long long ip64[2];
};
Its a little quirky but worked out well. To compare two IPs just need two compares on the 64-bit integer type ip.ip64[0]==ip.ip64[0] && ip.ip64[1]=ip.ip64[1] then just add some basic functions/macros to cover needed compares.
To copy a IPv6 from an external structure directly you can use memcpy on the ip8 member or cast the structure as a pointer. ip32 was sometimes useful for IPv4 interop manipulations (IPv4 mapped IPv6 addresses) ..etc.
If you do anything other than equality remember to convert to host byte order first as the IPv6 arrays are always stored in network byte order.
精彩评论