English 中文(简体)
Comparison operations on ipv6 address using C++
原标题:
  • 时间:2009-12-30 11:03:49
  •  标签:
  • c++
  • ipv6

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 chars 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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签