| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.utils; |
| 2 | |
| 3 | import java.net.Inet4Address; |
| 4 | import java.net.InetAddress; |
| 5 | import java.net.UnknownHostException; |
| 6 | |
| 7 | /** |
| 8 | * User: hanl |
| 9 | * Date: 9/13/13 |
| 10 | * Time: 2:25 PM |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 11 | * |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 12 | * currently only supports IPv4! |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 13 | * |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 14 | */ |
| 15 | // todo: integrate to gerrit |
| 16 | public class IPNetMask { |
| 17 | |
| 18 | private final Inet4Address i4addr; |
| 19 | private final byte maskCtr; |
| 20 | |
| 21 | private final int addrInt; |
| 22 | private final int maskInt; |
| 23 | |
| 24 | private static final int default_mask = 16; |
| 25 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 26 | |
| 27 | private IPNetMask (Inet4Address i4addr, byte mask) { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 28 | this.i4addr = i4addr; |
| 29 | this.maskCtr = mask; |
| 30 | |
| 31 | this.addrInt = addrToInt(i4addr); |
| 32 | this.maskInt = ~((1 << (32 - maskCtr)) - 1); |
| 33 | } |
| 34 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 35 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 36 | /** |
| 37 | * IPNetMask factory method. |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 38 | * |
| 39 | * @param addrSlashMask |
| 40 | * IP/Mask String in format "nnn.nnn.nnn.nnn/mask". If |
| 41 | * the "/mask" is omitted, "/32" (just the single |
| 42 | * address) is assumed. |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 43 | * @return a new IPNetMask |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | * @throws UnknownHostException |
| 45 | * if address part cannot be parsed by |
| 46 | * InetAddress |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 47 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 48 | public static IPNetMask getIPMask (String addrSlashMask) |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 49 | throws UnknownHostException { |
| 50 | int pos = addrSlashMask.indexOf('/'); |
| 51 | String addr; |
| 52 | byte maskCtr; |
| 53 | if (pos == -1) { |
| 54 | addr = addrSlashMask; |
| 55 | maskCtr = default_mask; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 56 | } |
| 57 | else { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 58 | addr = addrSlashMask.substring(0, pos); |
| 59 | maskCtr = Byte.parseByte(addrSlashMask.substring(pos + 1)); |
| 60 | } |
| 61 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 62 | return new IPNetMask((Inet4Address) InetAddress.getByName(addr), |
| 63 | maskCtr); |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 66 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 67 | /** |
| 68 | * Test given IPv4 address against this IPNetMask object. |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 69 | * |
| 70 | * @param testAddr |
| 71 | * address to isSystem. |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 72 | * @return true if address is in the IP Mask range, false if not. |
| 73 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 74 | public boolean matches (Inet4Address testAddr) { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 75 | int testAddrInt = addrToInt(testAddr); |
| 76 | return ((addrInt & maskInt) == (testAddrInt & maskInt)); |
| 77 | } |
| 78 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 79 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 80 | /** |
| 81 | * Convenience method that converts String host to IPv4 address. |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 82 | * |
| 83 | * @param addr |
| 84 | * IP address to match in nnn.nnn.nnn.nnn format or |
| 85 | * hostname. |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 86 | * @return true if address is in the IP Mask range, false if not. |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 87 | * @throws UnknownHostException |
| 88 | * if the string cannot be decoded. |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 89 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 90 | public boolean matches (String addr) throws UnknownHostException { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 91 | return matches((Inet4Address) InetAddress.getByName(addr)); |
| 92 | } |
| 93 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 94 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 95 | /** |
| 96 | * Converts IPv4 address to integer representation. |
| 97 | */ |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 98 | private int addrToInt (Inet4Address i4addr) { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 99 | byte[] ba = i4addr.getAddress(); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 100 | return (ba[0] << 24) | ((ba[1] & 0xFF) << 16) | ((ba[2] & 0xFF) << 8) |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 101 | | (ba[3] & 0xFF); |
| 102 | } |
| 103 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 104 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 105 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 106 | public String toString () { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 107 | return i4addr.getHostAddress() + "/" + maskCtr; |
| 108 | } |
| 109 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 110 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 111 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 112 | public boolean equals (Object obj) { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 113 | if (obj == null) |
| 114 | return false; |
| 115 | if (getClass() != obj.getClass()) |
| 116 | return false; |
| 117 | final IPNetMask that = (IPNetMask) obj; |
| 118 | return (this.addrInt == that.addrInt && this.maskInt == that.maskInt); |
| 119 | } |
| 120 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 121 | |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 122 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 123 | public int hashCode () { |
| Michael Hanl | c31d09f | 2015-09-25 12:40:59 +0200 | [diff] [blame] | 124 | return this.maskInt + this.addrInt; |
| 125 | } |
| 126 | |
| 127 | } |