-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRARPServer.java
More file actions
30 lines (26 loc) · 1.18 KB
/
RARPServer.java
File metadata and controls
30 lines (26 loc) · 1.18 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
import java.net.*;
import java.util.HashMap;
public class RARPServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(9092);
HashMap<String, String> rarpTable = new HashMap<>();
// Sample RARP Table: MAC -> IP
rarpTable.put("00:11:22:33:44:55", "192.168.1.1");
rarpTable.put("AA:BB:CC:DD:EE:FF", "192.168.1.2");
rarpTable.put("11:22:33:44:55:66", "192.168.1.3");
System.out.println("RARP Server running...");
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String request = new String(dp.getData(), 0, dp.getLength());
if (request.startsWith("REQUEST:")) {
String mac = request.substring(8);
String ip = rarpTable.getOrDefault(mac, "Not Found");
byte[] reply = ("REPLY: IP for " + mac + " is " + ip).getBytes();
ds.send(new DatagramPacket(reply, reply.length, dp.getAddress(), dp.getPort()));
System.out.println("Sent reply for " + mac + ": " + ip);
}
}
}
}