简体   繁体   中英

Comparison operations on ipv6 address using C++

由于IPV6地址有16个字节且它们没有数据类型来存储在c ++中,我想存储IPV6地址,并对IPv6地址进行一些比较,请告诉我怎么做

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.

用于比较定义自己的结构更好,还有一件事我将ip存储在Std列表中我需要定义重载的operator <== functions.so如何定义<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM