-2

i am using pcapdotnet DLL's in my application and one of the option i have added is to change the packet ip address before sending the pcap file (send to my function 2 ip's: old ip address and the new ip address i want to change).

now i want to add another option that can change range of ip address.

for example:

old ip addreee is 70.1.2.3 and the range is 10.0.0.1 until 212.0.0.0

so the ip address 70.1.2.3 will change to 10.0.0.1 and than 10.0.0.2 up to 70.1.2.3 and i am looking the best way to do it.

so far i only see this post who did not help me: Is there easy way of calculating number of IPs from 2 given IP addresses?

Community
  • 1
  • 1
user1269592
  • 691
  • 3
  • 12
  • 24
  • 1
    [What have you tried](http://whathaveyoutried.com)? What kind of "best" are you looking for? Performance? Readability? Terseness? Something else? – Oded Nov 08 '12 at 11:10

2 Answers2

1
  1. An IPv4 address consists of 4 bytes, and combining these 4 bytes gives you a plain 32-bit number. You can easily get this number using the method described in the thread you linked (I just noticed @DarkSquirrel42 already posted the same answer while I was meddling around). The only thing I would change in that answer is to return an unsigned integer (uint):

    public static uint IPToInt(string IP)
    {
         return (uint)IPAddress.NetworkToHostOrder(
             BitConverter.ToInt32(IPAddress.Parse(IP).GetAddressBytes(), 0));
    }
    
  2. Once you have it, it's a matter of simple math:

    Map a range of IP numbers to a different range of IP numbers

    Note that 10.0.0.1 to 212.0.0.0 is a very large range. If you map 10.0.0.1 to 70.1.2.3, you will run out of address space before you get to 212.0.0.0.

Community
  • 1
  • 1
vgru
  • 49,838
  • 16
  • 120
  • 201
  • i think the best i have found is this: http://www.dreamincode.net/code/snippet3877.htm but after create the class object and than run GetIPRange i dont know why my code didnt get into this class – user1269592 Nov 08 '12 at 14:23
  • What do you mean by "my code didnt get into the class"? Can you post some code in your question? – vgru Nov 08 '12 at 14:33
  • RangeFinder range = new RangeFinder(); and range.GetIPRange(oldIP, newIP); did not lead me into the class, nothing happen – user1269592 Nov 08 '12 at 14:41
  • Sorry, I still don't understand. What do you think was supposed to happen? I don't understand what *"lead into the class"* means? – vgru Nov 08 '12 at 14:42
  • i am checking with the debugger and after the class object creation range.GetIPRange(oldIP, newIP); did not doing nothing, it is supposed to get into GetIPRange function but it did not – user1269592 Nov 08 '12 at 14:48
  • Oh, ok, that's because it returns an `IEnumerable`, which is [lazy-evaluated](http://blogs.msdn.com/b/pedram/archive/2007/06/02/lazy-evaluation-in-c.aspx). Use a `foreach` loop to iterate over `GetIPRange` and it will step inside when you request the first item. – vgru Nov 08 '12 at 15:02
0

the post that "did not help" has all you need:

it shows you how to transform an IPv4 in three-dotted-form into an Int32 (that works because an IPv4 address is exactly 32 bits in size)

from there you can simply start counting

what you want to do looks like:

transform your IP range into 2 32-Bit integers

do a for loop from one to the other

transform the loop variable back into three-dotted-form

optionally check for IPs that should not be used and skip them (in case of classless IP ranges you might want to exclude IPs that would be network or broadcast addresses in classfull ranges...)

DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31
  • i try it and you right, BTW after get the number of IP's what is the best way to ensure that my ip will remain correct ? (after 192.168.0.255 will come 192.168.1.0 and not 192.168.0.256) – user1269592 Nov 08 '12 at 14:34
  • @user1269592: DarkSquirrel said that you need to transform IPs into integers, then loop **integers** (increase these numbers, not IPs), and then transform the integer variable back into the three-dotted form. – vgru Nov 08 '12 at 14:40