Arithmetic with IPv6 addresses (large integers)

不羁的心 提交于 2019-12-05 01:18:29

问题


I'm working with IPv6 addresses in the form:

FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF

Internally, I store them in an array:

TIp6Bytes = array [0..15] of Byte;

I need to manipulate the IPv6 addresses in a number of ways including adding, dividing, multiplying etc. Can anyone suggest a good way to do this?

I guess I should have mentioned that I'm working with Delphi 2009


回答1:


Jes Klinke wrote a bignum unit for Pascal here.

Disclaimer : I have not used this library personally.




回答2:


After trying many of the suggestions I could not find a library that fulfilled all my needs and were bug free. I searched a little harder and found a relatively new library by Alex Ciobanu which does BigIntegers (and Big Cardinals) seamlessly allowing you to manipulate them in much the same way as you manipulate normal Integers, Cardinals etc.

As well as BigIntegers, the library also provides a number of very useful features. From the readme:

  • A set of generic collections classes (List, Dictionary, HashSet, etc).
  • Date/Time functionality all combined in a few structures (somehow equivalent to .NET's DateTime structure)
  • Type Support concept that defines a set of default "support classes" for each built-in Delphi types (used as defaults in collections). Custom "type support" classes can be registered for your custom data types.
  • BigCardinal and BigInteger data types.
  • Smart pointers in Delphi

The library is being actively developed. In fact, the author fixed a small bug I found within a day.

You can read more about the library on Alex's blog and download DeHL from Google code.




回答3:


I once wrote a IPv4 and IPv6 conversion unit including a custom variant type for both types of IP addresses.

For instance, with these Variant types, the following example arithmetics and conversions are possible:

procedure TForm1.Log(const S: String);
begin
  Memo.Lines.Add(S);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I4: TIPv4;
  I6: TIPv6;
  V1, V2, V3, V4: Variant;
begin
  I4 := StrToIPv4('192.0.2.128');
  I6 := IPv4ToIPv6(I4);
  V1 := VarIPv6Create('2001:db8:85a3:0:0:8a2e:0370:7334');
  V2 := IPv6ToVar(I6);
  V3 := V1 - V2;
  V4 := V1 or V2;
  if V3 < V4 then
    Log(V3 + ' is smaller than ' + V4);
  if V2.Equals('::ffff:192.0.2.128') or V2.IsZero then
    Log('OK');
  Log('V1 = ' + V1.AsStringOutwritten);
  Log('V2 = ' + V2.AsURL);
  Log('V3 = ' + V3.AsStringCompressed);
  V4.Follow;
end;

Custom variant types really are quite powerfull.




回答4:


I would say that if you can add, you can then use it to subtract, multiply and divide using addition. Should I assume overflows will be simply ignored?

I seem to recall a method of adding bit-oriented variables using XOR. I am looking for that answer now.

Hopefully, this will point you in the right direction. If I can find that XOR code, I will post it for you.

Here it is: Bitwise operation Exclusive disjunction is often used for bitwise operations. Examples: 1 xor 1 = 0 1 xor 0 = 1 1110 xor 1001 = 0111 (this is equivalent to addition without carry)

And the reference is: http://www.absoluteastronomy.com/topics/Exclusive_disjunction



来源:https://stackoverflow.com/questions/697731/arithmetic-with-ipv6-addresses-large-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!