-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEthernet.java
77 lines (66 loc) · 1.8 KB
/
Ethernet.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
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Ethernet {
private byte[] packet;
private byte[] destinationMac;
private byte[] sourceMac;
private byte[] ethertype;
public Ethernet(byte[] packet_) {
this.packet = packet_;
setDestinationMac(Arrays.copyOfRange(packet_, 0, 6));
setSourceMac(Arrays.copyOfRange(packet_, 6, 12));
setEthertype(Arrays.copyOfRange(packet_, 12, 14));
}
public void setDestinationMac(byte[] dest) {
destinationMac = dest;
}
public byte[] getDestinationMac() {
return destinationMac;
}
public void setSourceMac(byte[] src) {
sourceMac = src;
}
public byte[] getSourceMac() {
return sourceMac;
}
public void setEthertype(byte[] eth) {
ethertype = eth;
}
public byte[] getEthertype() {
return ethertype;
}
public String resolveEthertype() {
String etype = "";
if (ethertype[0] == 8 && ethertype[1] == 0) {
etype = "ip";
} else if (ethertype[0] == 8 && ethertype[1] == 6) {
etype = "arp";
} else {
etype = "N/A";
}
return etype;
}
public String toString() {
try {
String src = bytesToHex(sourceMac);
String dest = bytesToHex(destinationMac);
String eth = bytesToHex(ethertype);
String output = "Ethernet:\n" + "Destination address in bytes: " + dest + "\nSource Address in bytes: "
+ src + "\nEthertype: " + resolveEthertype();
return output;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public static String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}