-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARP_Engine.java
68 lines (54 loc) · 2.52 KB
/
ARP_Engine.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
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Arrays;
public class ARP_Engine implements EventRegistration {
private static HashMap<InetAddress, MACAddress> arpCache;
private final EtherPort[] ports;
public ARP_Engine(EtherPort[] ports) {
arpCache = new HashMap<InetAddress, MACAddress>();
this.ports = ports;
//this is gonna be harder to make a thread than not right now
}
@Override
public void frameReceived(byte[] frameData, int jack) {
System.out.println("ARP_Engine: received frame data on jack " + jack);
System.out.println( Arrays.toString(frameData) );
ARPPacket toProcess = new ARPPacket(frameData);
System.out.println( Arrays.toString( toProcess.toByteArray() ) );
System.out.println(toProcess.getOper() == 1);
//We received a request
if( toProcess.getOper() == (short)1 ) {
InetAddress tpa = toProcess.getTPA();
if( arpCache.containsKey(tpa) )
respond( arpCache.get(tpa), toProcess.getTPA(),
toProcess.getSHA(), toProcess.getSPA(),jack );
System.out.println("MISSION SUCCESSFUL");
}
//We received a response, so store the sender's MAC and IP
else if( toProcess.getOper() == (short)2 ){
arpCache.put( toProcess.getSPA(), toProcess.getSHA() );
}
}
public void requestMAC( MACAddress sha, InetAddress spa,
InetAddress tpa, int jack) {
ARPPacket request = new ARPPacket(sha, spa, tpa);
EtherPort eth = ports[jack];
eth.enqueueFrame(new MACAddress(),(short)0x0806,request.toByteArray());
System.out.println(Arrays.toString(request.toByteArray()));
}
//Mystery router's MAC/IP are sha/spa.
//The computer we're responding to (who made the original
//request) is tha/tpa.
public void respond( MACAddress sha, InetAddress spa,
MACAddress tha, InetAddress tpa, int jack) {
//make sure we learn the requestor and requestee's info
arpCache.put(tpa, tha);
arpCache.put(spa, sha);
ARPPacket response = new ARPPacket(sha,spa,tha,tpa);
EtherPort eth = ports[jack];
eth.enqueueFrame(new MACAddress(),(short)0x0806,response.toByteArray());
}
public void addToCache(InetAddress localIP, int localVirtualPort) {
arpCache.put( localIP, ports[localVirtualPort].getMAC() );
}
}