Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So basically an IPV4-address and netmask are 32 bit unsigned integer displayed in pairs of 8 bit with a dot between them. </p> <p>So if you have "a.b.c.d" you could get the integer value by doing </p> <pre><code>(a&lt;&lt;24) + (b&lt;&lt;16) + (c&lt;&lt;8) + d </code></pre> <p>If you're not used to the &lt;&lt; it's shifts the bits in the number to the left the number of times as the right argument.. Thus <code>a&lt;&lt;b</code> is the same as <code>a*2^b</code></p> <p>Now in a CIDR the number after the slash tells you how many bits (of 32) that are the network part of the number before the slash. eg. 10.10.10.1/28 has 28 bits network and 32-28=4 bits host part. That means that if you take the ip and translate it to a number (=0x0A0A0A01) and do a logical <code>&amp;</code> with 0xFFFFFFFF&lt;&lt;4 (= 0xFFFFFFF0) you get 0x0A0A0A00. If you translate that back to a ip with the following:</p> <pre><code>sprintf("%d.%d.%d.%d", (n&gt;&gt;24)&amp;0xff, (n&gt;&gt;16)&amp;0xff, (n&gt;&gt;8)&amp;0xff, n&amp;0xff) </code></pre> <p>You get 10.10.10.0. Also if you translate 0xFFFFFFF0 to a ip with the same algorithm you get 255.255.255.248 which is the netmask that corresponds to mask length 28.</p> <p>IPv6 is exactly the same, except it's 128 bits divided in 8 16 bit clusters displayed in hex with commas between them with some fancy shortcut for multiple runs of 0 (::)..</p> <p>eg. </p> <pre><code>fe80::52e5:49ff:ffc9:1889/64 == ;;convert the mask 0xffffffffffffffffffffffffffffffff &lt;&lt; (128-64) = 0xffffffffffffffff0000000000000000 ;;convert the ip: fe80::52e5:49ff:ffc9:1889 == fe80:0000:0000:0000:52e5:49ff:ffc9:1889 == 0xfe8000000000000052e549ffffc91889 0xfe8000000000000052e549ffffc91889 &amp; 0xffffffffffffffff0000000000000000 = 0xfe800000000000000000000000000000 ;;convert first address in range back to string: fe80:0000:0000:0000:0000:0000:0000:0000 = fe80:: </code></pre> <p>I guess that on the basis of this you can make a function that does what you want. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload