-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTCPPacket.java
86 lines (71 loc) · 2.47 KB
/
TCPPacket.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
78
79
80
81
82
83
84
85
86
import java.nio.ByteBuffer;
public class TCPPacket {
private Long sequenceNumber;
private Long acknowledgementNumber;
private Boolean ACK;
private Boolean SYN;
private int dataLength;
private byte[] data;
public static final int dataOffset = Long.SIZE + Long.SIZE + Byte.SIZE + Byte.SIZE + Integer.SIZE;
public static final int dataOffsetByBytes = dataOffset / Byte.SIZE + (dataOffset % Byte.SIZE != 0 ? 1 : 0);
public int getSize() {
return dataOffset + this.getDataLength() * Byte.SIZE;
}
public int getBytesNumber() {
return dataOffsetByBytes + this.getDataLength();
}
public TCPPacket(byte[] UDPData) {
ByteBuffer buffer = ByteBuffer.allocate(UDPData.length);
buffer.put(UDPData);
buffer.flip();
this.sequenceNumber = buffer.getLong();
this.acknowledgementNumber = buffer.getLong();
this.ACK = buffer.get() != 0;
this.SYN = buffer.get() != 0;
this.dataLength = buffer.getInt();
this.data = new byte[this.dataLength];
buffer.get(this.data, 0, this.dataLength);
}
public TCPPacket(Long sequenceNumber, Long acknowledgementNumber, Boolean ACK, Boolean SYN, byte[] data) {
this.sequenceNumber = sequenceNumber;
this.acknowledgementNumber = acknowledgementNumber;
this.ACK = ACK;
this.SYN = SYN;
this.data = data != null ? data : new byte[0];
this.dataLength = this.data.length;
}
public TCPPacket(Long sequenceNumber, byte[] data) {
this(sequenceNumber, (long) (0), false, false, data);
}
public Long getSequenceNumber() {
return sequenceNumber;
}
public Long getAcknowledgementNumber() {
return acknowledgementNumber;
}
public Boolean getACK() {
return ACK;
}
public Boolean getSYN() {
return SYN;
}
public byte[] getData() {
return data;
}
public int getDataLength() {
return dataLength;
}
public long getExpectedAcknowledgementNumber() {
return this.getSequenceNumber() + this.getDataLength() + 1;
}
public byte[] toUDPData() {
ByteBuffer res = ByteBuffer.allocate(this.getBytesNumber());
res.putLong(this.sequenceNumber);
res.putLong(this.acknowledgementNumber);
res.put((byte) (ACK ? 1 : 0));
res.put((byte) (SYN ? 1 : 0));
res.putInt(this.dataLength);
res.put(this.data);
return res.array();
}
}