summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/core/CIDRIP.java
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2013-09-18 14:37:34 +0200
committerArne Schwabe <arne@rfc2549.org>2013-09-18 14:37:34 +0200
commit2fdfb2b7f3d28667de2a20f68eb3ec46aebfec12 (patch)
tree479c1f92fd279357e95e914e7067616b7e4ec66e /src/de/blinkt/openvpn/core/CIDRIP.java
parente86a9a41d289e107bf235baaac766c109f23a5c6 (diff)
Refactor logging messages
Diffstat (limited to 'src/de/blinkt/openvpn/core/CIDRIP.java')
-rw-r--r--src/de/blinkt/openvpn/core/CIDRIP.java126
1 files changed, 65 insertions, 61 deletions
diff --git a/src/de/blinkt/openvpn/core/CIDRIP.java b/src/de/blinkt/openvpn/core/CIDRIP.java
index 27ce414c..960e7d11 100644
--- a/src/de/blinkt/openvpn/core/CIDRIP.java
+++ b/src/de/blinkt/openvpn/core/CIDRIP.java
@@ -2,65 +2,69 @@ package de.blinkt.openvpn.core;
import java.util.Locale;
-class CIDRIP{
- String mIp;
- int len;
-
-
- public CIDRIP(String ip, String mask){
- mIp=ip;
- long netmask=getInt(mask);
-
- // Add 33. bit to ensure the loop terminates
- netmask += 1l << 32;
-
- int lenZeros = 0;
- while((netmask & 0x1) == 0) {
- lenZeros++;
- netmask = netmask >> 1;
- }
- // Check if rest of netmask is only 1s
- if(netmask != (0x1ffffffffl >> lenZeros)) {
- // Asume no CIDR, set /32
- len=32;
- } else {
- len =32 -lenZeros;
- }
-
- }
- public CIDRIP(String address, int prefix_length) {
- len = prefix_length;
- mIp = address;
- }
- @Override
- public String toString() {
- return String.format(Locale.ENGLISH,"%s/%d",mIp,len);
- }
-
- public boolean normalise(){
- long ip=getInt(mIp);
-
- long newip = ip & (0xffffffffl << (32 -len));
- if (newip != ip){
- mIp = String.format("%d.%d.%d.%d", (newip & 0xff000000) >> 24,(newip & 0xff0000) >> 16, (newip & 0xff00) >> 8 ,newip & 0xff);
- return true;
- } else {
- return false;
- }
- }
- static long getInt(String ipaddr) {
- String[] ipt = ipaddr.split("\\.");
- long ip=0;
-
- ip += Long.parseLong(ipt[0])<< 24;
- ip += Integer.parseInt(ipt[1])<< 16;
- ip += Integer.parseInt(ipt[2])<< 8;
- ip += Integer.parseInt(ipt[3]);
-
- return ip;
- }
- public long getInt() {
- return getInt(mIp);
- }
-
+class CIDRIP {
+ String mIp;
+ int len;
+
+
+ public CIDRIP(String ip, String mask) {
+ mIp = ip;
+ long netmask = getInt(mask);
+
+ // Add 33. bit to ensure the loop terminates
+ netmask += 1l << 32;
+
+ int lenZeros = 0;
+ while ((netmask & 0x1) == 0) {
+ lenZeros++;
+ netmask = netmask >> 1;
+ }
+ // Check if rest of netmask is only 1s
+ if (netmask != (0x1ffffffffl >> lenZeros)) {
+ // Asume no CIDR, set /32
+ len = 32;
+ } else {
+ len = 32 - lenZeros;
+ }
+
+ }
+
+ public CIDRIP(String address, int prefix_length) {
+ len = prefix_length;
+ mIp = address;
+ }
+
+ @Override
+ public String toString() {
+ return String.format(Locale.ENGLISH, "%s/%d", mIp, len);
+ }
+
+ public boolean normalise() {
+ long ip = getInt(mIp);
+
+ long newip = ip & (0xffffffffl << (32 - len));
+ if (newip != ip) {
+ mIp = String.format("%d.%d.%d.%d", (newip & 0xff000000) >> 24, (newip & 0xff0000) >> 16, (newip & 0xff00) >> 8, newip & 0xff);
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ static long getInt(String ipaddr) {
+ String[] ipt = ipaddr.split("\\.");
+ long ip = 0;
+
+ ip += Long.parseLong(ipt[0]) << 24;
+ ip += Integer.parseInt(ipt[1]) << 16;
+ ip += Integer.parseInt(ipt[2]) << 8;
+ ip += Integer.parseInt(ipt[3]);
+
+ return ip;
+ }
+
+ public long getInt() {
+ return getInt(mIp);
+ }
+
} \ No newline at end of file