-
Notifications
You must be signed in to change notification settings - Fork 0
/
ARPPacket.java
85 lines (77 loc) · 2.43 KB
/
ARPPacket.java
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
import java.net.InetAddress;
import java.nio.ByteBuffer;
public class ARPPacket{
private final static short REQUEST = 1, RESPONSE = 2;
private short htype = 1;
private short ptype = 0x0800;
private byte hlen = 6;
private byte plen = 4;
private short oper;
private MACAddress sha;
private InetAddress spa;
private MACAddress tha;
private InetAddress tpa;
public ARPPacket(byte[] frameBytes) {
ByteBuffer buf = ByteBuffer.wrap(frameBytes);
htype = buf.getShort();
ptype = buf.getShort();
hlen = buf.get();
plen = buf.get();
oper = buf.getShort();
byte[] shabyt = new byte[6];
byte[] spabyt = new byte[4];
byte[] thabyt = new byte[6];
byte[] tpabyt = new byte[4];
buf.get(shabyt,0,6);
buf.get(spabyt,0,4);
buf.get(thabyt,0,6);
buf.get(tpabyt,0,4);
sha = new MACAddress(shabyt);
try {
spa = InetAddress.getByAddress(spabyt);
} catch(Exception e) {}
tha = new MACAddress(thabyt);
try {
tpa = InetAddress.getByAddress(tpabyt);
} catch(Exception e) {}
}
public ARPPacket(MACAddress sha, InetAddress spa, InetAddress tpa){
this.sha = sha;
this.spa = spa;
this.tha = new MACAddress(0L);
this.tpa = tpa;
oper = REQUEST;
}
public ARPPacket(MACAddress sha, InetAddress spa, MACAddress tha,
InetAddress tpa ){
this.sha = sha;
this.spa = spa;
this.tha = tha;
this.tpa = tpa;
oper = RESPONSE;
}
public byte[] toByteArray() {
ByteBuffer buf = ByteBuffer.allocate(28);
byte[] bytes = new byte[28];
buf.putShort(htype);
buf.putShort(ptype);
buf.put(hlen);
buf.put(plen);
buf.putShort(oper);
buf.put(sha.getByteAddress());
buf.put(spa.getAddress());
buf.put(tha.getByteAddress());
byte[] tpaBytes;
if( tpa != null )
tpaBytes = tpa.getAddress();
else
tpaBytes = new byte[]{ 0, 0, 0, 0 };
buf.put(tpaBytes);
return buf.array();
}
public short getOper(){return oper;};
public MACAddress getSHA(){return sha;}
public InetAddress getSPA(){return spa;}
public MACAddress getTHA(){return tha;}
public InetAddress getTPA(){return tpa;}
}