Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Convert IP addresses to 32 bit integers and make a column for the address and the mask. You can use <code>INET_NTOA()</code> and <code>INET_ATON()</code> functions. Your 10.0.%.1 range can be expressed as network 10.0.0.1 with a mask of 255.255.0.255. To see if a given IP address matches this network, you apply the mask to the IP address, and compare it to the network address. If they match, the address is in the network. For example, if you want to test 10.0.4.1, you apply the mask to it using bitwise AND:</p> <pre><code>10.0.4.1 &amp; 255.255.0.255 = 10.0.0.1 </code></pre> <p>This matches your network address, so this IP is in the network.</p> <p>You can store this network in your table like this (ip and mask are integers):</p> <pre><code>CREATE TABLE networks (id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, ip INT, mask INT); INSERT INTO networks (ip, mask) VALUES (INET_ATON('10.0.0.1'), INET_ATON('255.255.0.255')); </code></pre> <p>You can find if a given IP (10.0.23.1) matches like this:</p> <pre><code>SELECT * FROM networks WHERE ip &amp; mask = INET_ATON('10.0.23.1') &amp; mask </code></pre> <p>This will select the IPs as integers though, if you want them human readable:</p> <pre><code>SELECT id, INET_NTOA(ip) AS ipstring, INET_NTOA(mask) AS maskstring FROM networks WHERE ip &amp; mask = INET_ATON('10.0.23.1') &amp; mask </code></pre> <p>This allows you to test if a single given IP address matches a network. If you want to test a range of IPs, you'd have to apply the mask to each one in the range and test each one as a above. You can do it in a single query, it'll just have to list every IP address you're testing.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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