ARP 패킷 클래스 정의하기
ARP는 주소를 결정하는 하나의 프로토콜
IP주소로 MAC주소를 결정하는 프로토콜
상대방이 IP는 알고 MAC주소를 모를 때 사용하는 기법
ARP request는 보내는사람의 MAC주소와 IP주소 그리고 상대방의 IP주소를 이용해서 실행할 수 있음
ARP 패킷은 총 42바이트로 이루어져 있음
package model;
import java.util.Arrays;
public class ARP {
private byte[] destinationMAC = new byte[6];
private byte[] sourceMAC = new byte[6];
private byte[] ethernetType = {0x08, 0x06}; //ARP
private byte[] hardwareType = {0x00, 0x01}; //Ethernet
private byte[] protocolType = {0x08, 0x00}; //IPv4
private byte hardwareSize = 0x06; //MAC Size
private byte protocolSize = 0x04; //IP Size
private byte[] opcode = new byte[2];
private byte[] senderMAC = new byte[6];
private byte[] senderIP = new byte[4];
private byte[] targetMAC = new byte[6];
private byte[] targetIP = new byte[4];
public void makeARPRequest(byte[] sourceMAC, byte[] senderIP, byte[] targetIP) {
Arrays.fill(destinationMAC, (byte) 0xff); //Broadcast
System.arraycopy(sourceMAC, 0, this.sourceMAC, 0, 6);
opcode[0] = 0x00; opcode[1] = 0x01; //Request
System.arraycopy(sourceMAC, 0, this.senderMAC, 0, 6);
System.arraycopy(senderIP, 0, this.senderIP, 0, 4);
Arrays.fill(targetMAC, (byte) 0x00); //Broadcast
System.arraycopy(targetIP, 0, targetIP, 0, 4);
}
public void makeARPReply(byte[] destinationMAC, byte[] sourceMAC, byte[] senderMAC,
byte[] senderIP, byte[] targetMAC, byte[] targetIP) {
System.arraycopy(destinationMAC, 0, this.destinationMAC, 0, 6);
System.arraycopy(sourceMAC, 0, this.sourceMAC, 0, 6);
opcode[0] = 0x00; opcode[1] = 0x02; //Reply
System.arraycopy(senderMAC, 0, this.senderMAC, 0, 6);
System.arraycopy(senderIP, 0, this.senderIP, 0, 4);
System.arraycopy(targetMAC, 0, this.targetMAC, 0, 6);
System.arraycopy(targetIP, 0, this.targetIP, 0, 4);
}
public byte[] getPacket() {
byte[] bytes = new byte[42];
System.arraycopy(destinationMAC, 0, bytes, 0, destinationMAC.length);
System.arraycopy(sourceMAC, 0, bytes, 6, sourceMAC.length);
System.arraycopy(ethernetType, 0, bytes, 12, ethernetType.length);
System.arraycopy(hardwareType, 0, bytes, 14, hardwareType.length);
System.arraycopy(protocolType, 0, bytes, 16, protocolType.length);
bytes[18] = hardwareSize;
bytes[19] = protocolSize;
System.arraycopy(opcode, 0, bytes, 20, opcode.length);
System.arraycopy(senderMAC, 0, bytes, 22, senderMAC.length);
System.arraycopy(senderIP, 0, bytes, 28, senderIP.length);
System.arraycopy(targetMAC, 0, bytes, 32, targetMAC.length);
System.arraycopy(targetIP, 0, bytes, 38, targetIP.length);
return bytes;
}
}