i have tis following code
* DHCPServer.java
* DHCPClient.java
* DHCPMessage.java
* DHCPOptions.java
i hav complied in my command promt using javac
it has no error
i run the code
bt i dont know how the code works
to be more specific i dont know how to run it
DHCPServer.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Arrays;
public class DHCPServer {
private static final int MAX_BUFFER_SIZE = 1024;
private int listenPort = 67;
public DHCPServer(int servePort) {
listenPort = servePort;
new DHCPServer();
}
public DHCPServer() {
DatagramSocket socket = null;
try {
socket = new DatagramSocket(listenPort);
byte[] payload = new byte[MAX_BUFFER_SIZE];
int length = 6;
DatagramPacket p = new DatagramPacket(payload, length);
System.out.println("Listening on port " + listenPort + "...");
boolean listening = true;
while (listening) {
socket.receive(p);
System.out.println("Connection established from " + p.getAddress());
System.out.println("Data Received: " + Arrays.toString(p.getData()));
}
} catch (SocketException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DHCPServer server;
if (args.length >= 1) {
server = new DHCPServer(Integer.parseInt(args[0]));
} else {
server = new DHCPServer();
}
}
}
DHCPClient.java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
public class DHCPClient {
private static final int MAX_BUFFER_SIZE = 1024;
private int listenPort = 68;
private String serverIP = "127.0.0.1";
private int serverPort = 67;
public DHCPClient() {
System.out.println("Connecting to DHCPServer at " + serverIP + " on port " + serverPort + "...");
DatagramSocket socket = null;
try {
socket = new DatagramSocket(listenPort);
byte[] payload = new byte[MAX_BUFFER_SIZE];
int length = 6;
payload[0] = 'h';
payload[1] = '3';
payload[2] = 'l';
payload[3] = 'l';
payload[4] = 'o';
payload[5] = '!';
DatagramPacket p = new DatagramPacket(payload, length, InetAddress.getByName(serverIP), serverPort);
socket.send(p);
socket.send(p);
System.out.println("Connection Established Successfully!");
System.out.println("Sending data: " + Arrays.toString(p.getData()));
} catch (SocketException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DHCPClient client;
client = new DHCPClient();
printMacAddress();
}
public static byte[] getMacAddress() {
byte[] mac = null;
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
mac = ni.getHardwareAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
assert(mac != null);
return mac;
}
public static void printMacAddress() {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
byte[] mac = ni.getHardwareAddress();
for (int i = 0; i < mac.length; i++) {
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-"
: "");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
}
}
DHCPMessage.java
import java.net.Inet4Address;
import java.net.InetAddress;
public class DHCPMessage {
private static final int BOOTREQUEST = 1;
private static final int BOOTREPLY = 2;
private static final int DHCPREQUEST = 1;
private static final int DHCPREPLY = 2;
private static final int ETHERNET10MB = 1;
private byte op;
private byte hType;
private byte hLen;
private byte hops;
private int xid;
private short secs;
private short flags;
private byte[] cIAddr;
private byte[] yIAddr;
private byte[] sIAddr;
private byte[] gIAddr;
private byte[] cHAddr;
private byte[] sName;
private byte[] file;
private DHCPOptions options;
public DHCPMessage() {
cIAddr = new byte[4];
yIAddr = new byte[4];
sIAddr = new byte[4];
gIAddr = new byte[4];
cHAddr = new byte[16];
sName = new byte[64];
file = new byte[128];
options = new DHCPOptions();
this.printMessage();
}
public byte[] discoverMsg(byte[] cMacAddress) {
op = DHCPREQUEST;
hType = ETHERNET10MB;
hLen = 6;
hops = 0;
xid = 556223005;
secs = 0;
flags = 0;
cIAddr = new byte[] { 0, 0, 0, 0 };
yIAddr = new byte[] { 0, 0, 0, 0 };
sIAddr = new byte[] { 0, 0, 0, 0 };
gIAddr = new byte[] { 0, 0, 0, 0 };
cHAddr = cMacAddress;
sName = new byte[sName.length];
file = new byte[file.length];
return this.externalize();
}
public byte[] externalize() {
int staticSize = 236;
byte[] options = this.options.externalize();
int size = staticSize + options.length;
byte[] msg = new byte[size];
msg[0] = this.op;
msg[1] = this.hType;
msg[2] = this.hLen;
msg[3] = this.hops;
for (int i=0; i < 4; i++) msg[4+i] = inttobytes(xid)[i];
for (int i=0; i < 2; i++) msg[8+i] = shorttobytes(secs)[i];
for (int i=0; i < 2; i++) msg[10+i] = shorttobytes(flags)[i];
for (int i=0; i < 4; i++) msg[12+i] = cIAddr[i];
for (int i=0; i < 4; i++) msg[16+i] = yIAddr[i];
for (int i=0; i < 4; i++) msg[20+i] = sIAddr[i];
for (int i=0; i < 4; i++) msg[24+i] = gIAddr[i];
for (int i=0; i < 16; i++) msg[28+i] = cHAddr[i];
for (int i=0; i < 64; i++) msg[44+i] = sName[i];
for (int i=0; i < 128; i++) msg[108+i] = file[i];
for (int i=0; i < options.length; i++) msg[staticSize+i] = options[i];
return msg;
}
public byte getOp() {
return op;
}
public void setOp(byte op) {
this.op = op;
}
public byte getHType() {
return hType;
}
public void setHType(byte type) {
hType = type;
}
public byte getHLen() {
return hLen;
}
public void setHLen(byte len) {
hLen = len;
}
public byte getHops() {
return hops;
}
public void setHops(byte hops) {
this.hops = hops;
}
public int getXid() {
return xid;
}
public void setXid(int xid) {
this.xid = xid;
}
public short getSecs() {
return secs;
}
public void setSecs(short secs) {
this.secs = secs;
}
public short getFlags() {
return flags;
}
public void setFlags(short flags) {
this.flags = flags;
}
public byte[] getCIAddr() {
return cIAddr;
}
public void setCIAddr(byte[] addr) {
cIAddr = addr;
}
public byte[] getYIAddr() {
return yIAddr;
}
public void setYIAddr(byte[] addr) {
yIAddr = addr;
}
public byte[] getSIAddr() {
return sIAddr;
}
public void setSIAddr(byte[] addr) {
sIAddr = addr;
}
public byte[] getGIAddr() {
return gIAddr;
}
public void setGIAddr(byte[] addr) {
gIAddr = addr;
}
public byte[] getCHAddr() {
return cHAddr;
}
public void setCHAddr(byte[] addr) {
cHAddr = addr;
}
public byte[] getSName() {
return sName;
}
public void setSName(byte[] name) {
sName = name;
}
public byte[] getFile() {
return file;
}
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getOptions() {
return options.externalize();
}
public void printMessage() {
System.out.println(this.toString());
}
@Override
public String toString() {
String msg = new String();
msg += "Operation Code: " + this.op + "\n";
msg += "Hardware Type: " + this.hType + "\n";
msg += "Hardware Length: " + this.hLen + "\n";
msg += "Hops: " + this.hops + "\n";
msg += Integer.toString(xid) + "\n";
msg += Short.toString(secs) + "\n";
msg += Short.toString(flags) + "\n";
msg += cIAddr.toString() + "\n";
msg += yIAddr.toString() + "\n";
msg += sIAddr.toString() + "\n";
msg += gIAddr.toString() + "\n";
msg += cHAddr.toString() + "\n";
msg += sName.toString() + "\n";
msg += file.toString() + "\n";
msg += options.toString() + "\n";
assert(file != null);
assert (options != null);
return msg;
}
private byte[] inttobytes(int i){
byte[] dword = new byte[4];
dword[0] = (byte) ((i >> 24) & 0x000000FF);
dword[1] = (byte) ((i >> 16) & 0x000000FF);
dword[2] = (byte) ((i >> 8) & 0x000000FF);
dword[3] = (byte) (i & 0x00FF);
return dword;
}
private byte[] shorttobytes(short i){
byte[] b = new byte[2];
b[0] = (byte) ((i >> 8) & 0x000000FF);
b[1] = (byte) (i & 0x00FF);
return b;
}
}
DHCPOptions.java
import java.util.Hashtable;
import java.util.LinkedList;
public class DHCPOptions {
public static final int DHCPDISCOVER = 1;
public static final int DHCPOFFER = 2;
public static final int DHCPREQUEST = 3;
public static final int DHCPDECLINE = 4;
public static final int DHCPACK = 5;
public static final int DHCPNAK = 6;
public static final int DHCPRELEASE = 7;
public static final int DHCPMESSAGETYPE = 53;
private Hashtable<Integer,byte[]> options;
public DHCPOptions() {
options = new Hashtable<Integer, byte[]>();
}
public byte[] getOption(int optionID) {
return options.get(optionID);
}
public void setOption(int optionID, byte[] option) {
options.put(optionID, option);
}
public byte[] getOptionData(int optionID) {
byte[] option = options.get(optionID);
byte[] optionData = new byte[option.length-2];
for (int i=0; i < optionData.length; i++) optionData[i] = option[2+i];
return optionData;
}
public void setOptionData(int optionID, byte[] optionData) {
byte[] option = new byte[2+optionData.length];
option[0] = (byte) optionID;
option[1] = (byte) optionData.length;
for (int i=0; i < optionData.length; i++) option[2+i] = optionData[i];
options.put(optionID, option);
}
public void printOption (int optionID) {
String output = new String("");
if (options.get(optionID) != null) {
byte[] option = options.get(optionID);
for (int i=0; i < option.length; i++) {
output += option[i] +
(i == option.length-1 ? "" : ",");
}
} else {
output = "<Empty>";
}
System.out.println(output);
}
public void printOptions () {
for (byte[] option : options.values()) {
printOption(option[0]);
}
}
public static void main (String[] args) {
DHCPOptions test = new DHCPOptions();
}
public byte[] externalize() {
int totalBytes = 0;
for (byte[] option : this.options.values()) {
totalBytes += option.length;
}
byte[] options = new byte[totalBytes];
int bytes = 0;
for (byte[] option : this.options.values()) {
for (int i=0; i < option.length; i++) {
options[bytes+i] = option[i];
}
bytes += option.length;
}
return options;
}
}