forked from cSploit/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIP4Address.java
More file actions
136 lines (113 loc) · 3.7 KB
/
Copy pathIP4Address.java
File metadata and controls
136 lines (113 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* This file is part of the dSploit.
*
* Copyleft of Simone Margaritelli aka evilsocket <evilsocket@gmail.com>
*
* dSploit is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dSploit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dSploit. If not, see <http://www.gnu.org/licenses/>.
*/
package org.csploit.android.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteOrder;
public class IP4Address
{
private byte[] mByteArray = null;
private String mString = "";
private int mInteger = 0;
private InetAddress mAddress = null;
public static int ntohl(int n){
return ((n >> 24) & 0xFF) + ((n >> 16) & 0xFF) + ((n >> 8) & 0xFF) + (n & 0xFF);
}
public static IP4Address next(IP4Address address) throws UnknownHostException{
byte[] addr = address.toByteArray();
int i = addr.length - 1;
while(i >= 0 && addr[i] == (byte) 0xff){
addr[i] = 0;
i--;
}
if(i >= 0){
addr[i]++;
return new IP4Address(addr);
} else
return null;
}
public IP4Address(int address) throws UnknownHostException{
mByteArray = new byte[4];
mInteger = address;
if(ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN){
mByteArray[0] = (byte) (address & 0xFF);
mByteArray[1] = (byte) (0xFF & address >> 8);
mByteArray[2] = (byte) (0xFF & address >> 16);
mByteArray[3] = (byte) (0xFF & address >> 24);
} else{
mByteArray[0] = (byte) (0xFF & address >> 24);
mByteArray[1] = (byte) (0xFF & address >> 16);
mByteArray[2] = (byte) (0xFF & address >> 8);
mByteArray[3] = (byte) (address & 0xFF);
}
mAddress = InetAddress.getByAddress(mByteArray);
mString = mAddress.getHostAddress();
}
public IP4Address(String address) throws UnknownHostException{
mString = address;
mAddress = InetAddress.getByName(address);
mByteArray = mAddress.getAddress();
mInteger = ((mByteArray[0] & 0xFF) << 24) +
((mByteArray[1] & 0xFF) << 16) +
((mByteArray[2] & 0xFF) << 8) +
(mByteArray[3] & 0xFF);
}
public IP4Address(byte[] address) throws UnknownHostException{
mByteArray = address;
mAddress = InetAddress.getByAddress(mByteArray);
mString = mAddress.getHostAddress();
mInteger = ((mByteArray[0] & 0xFF) << 24) +
((mByteArray[1] & 0xFF) << 16) +
((mByteArray[2] & 0xFF) << 8) +
(mByteArray[3] & 0xFF);
}
public IP4Address(InetAddress address){
mAddress = address;
mByteArray = address.getAddress();
mString = mAddress.getHostAddress();
mInteger = ((mByteArray[0] & 0xFF) << 24) +
((mByteArray[1] & 0xFF) << 16) +
((mByteArray[2] & 0xFF) << 8) +
(mByteArray[3] & 0xFF);
}
public byte[] toByteArray(){
return mByteArray;
}
public String toString(){
return mString;
}
public int toInteger(){
return mInteger;
}
public InetAddress toInetAddress(){
return mAddress;
}
public boolean equals(IP4Address address){
return mInteger == address.toInteger();
}
public boolean equals(InetAddress address){
return mAddress.equals(address);
}
public int getPrefixLength(){
int bits, i, n = mInteger;
// WTF is this?
for(i = 0, bits = (n & 1); i < 32; i++, n >>>= 1, bits += n & 1) ;
return bits;
}
}