Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,
I was wondering cause i don't know how to start on it. A point in the right direction would be much appreciated :) .

Say i have two ip's in the following format,
Ipv4-192.168.168.100/24
IPv6-2620: 0:860:2::/64

I want to get he minimum and maximum ip range of the above via c#. It should be compatible with both the ip versions.please help me. I don't know how to start on it..

thank you very much.
Posted
Updated 16-Aug-12 19:20pm
v3
Comments
Sergey Alexandrovich Kryukov 17-Aug-12 1:22am    
Not clear. A maximum or minimum in what set of addresses? And what is the ordering rule (a notion of maximum or minimum requires definition of the "greater or equal" relation). Or is this a question about network classes?
--SA
dhl_hh 17-Aug-12 1:27am    
for instance when you get the Ipv4 address i have posted, the ip range for that would be from 192.168.168.1 to 192.168.168.254..
and for the ipv6 address the range would be from 2620:0:860:: to 2620:0:860:ffff:ffff:ffff:ffff:ffff.
i want to know how to get those two ranges from addresses that use the formats i've given. :)
Sergey Alexandrovich Kryukov 17-Aug-12 1:32am    
So, this is related to the class, as you did not touch first three integers... so, this is maximum and minimum in this network class...
--SA
dhl_hh 17-Aug-12 1:37am    
yeaa..i want to know how to derive them via c#. Any clue, a link perhaps :) would be much greateful.
Philip Stuyck 17-Aug-12 1:55am    
classes are not used anymore, we have classless IP routing nowadays what it is all about is bitmasking. The /24 identifies which part of the ip address refers to the network id and 32-24=8 refers to the host id. There are 2 special cases of host ids that cannot be used, namely 0 and all 1.

1 solution

C#
uint getMinHostRange(int maskLength){
  return 1; //0 is not allowed
}

uint getMaxHostRange(int maskLength){
  uint mask = 1;
  while(i=1;i<masklength;++i){>
    mask = mask<<1+1;
  }
  return mask-1;
}


The ip address is not relevant because as I said IP uses classles routing today.
 
Share this answer
 
Comments
dhl_hh 17-Aug-12 4:15am    
hey thank you :) ... do you know anyway of converting ipv6 ranges to cidr notation via c#? or do i have to post a new question on it??
Philip Stuyck 17-Aug-12 5:15am    
don't know, I did not do much in ipv6 yet
Sergey Alexandrovich Kryukov 17-Aug-12 10:41am    
I voted 4. You demonstrated how to iterate through the masked integer value set and get integer maximum and minimum. As I say, I suspect that OP needs the iteration itself (enumeration), not maximum/minimum, to do something with the whole subset of IP addresses. You also avoided to work with IP. This is quite reasonable, but I don't think you managed to explain it properly, but you actually confirmed my comments to the question.

The problem is not that the IP address sets are classless. (How can it be a problem? The system was introduced for a good reason :-) The problem is that OP did not fully formulate the problem: the order of IP address is not defined. In fact, it is not probably needed (it does not make technical sense), and OP simply needs a way to get a whole IP address set elements, based on some subset (say, mask).

I hope my comments can give OP the better idea...

Thank you,
--SA
dhl_hh 19-Aug-12 23:03pm    
Thank you, your posts clarified things much better.. thank you for your time and effort :) much appreciated :)
Sergey Alexandrovich Kryukov 19-Aug-12 23:38pm    
It's my pleasure to help, but I'm not sure that my posts are more clear; it really depends in the case and also on the time I feel I can spend. On some questions, the help appears as a result of some virtual team work.
Your decision to accept this answer formally was correct.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900