-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPpacket.java
More file actions
134 lines (105 loc) · 3.25 KB
/
TCPpacket.java
File metadata and controls
134 lines (105 loc) · 3.25 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class TCPpacket{
public static final int headerSize = 24;
public int seqNum, ackNum;
public long time;
public Boolean[] flags = new Boolean[3];
public byte[] payloadData;
short checksum;
// Create packet from variables
public TCPpacket(int seqNum, int ackNum, long time, Boolean[] flags, byte[] payloadData){
this.seqNum = seqNum;
this.ackNum = ackNum;
this.time = time;
this.flags = flags;
this.payloadData = payloadData;
this.checksum = 0;
}
public TCPpacket(){}
public boolean isChecksumValid(byte[] data){
this.deserialize(data);
short oldCheckSum = this.checksum;
this.checksum = 0;
this.serialize();
return oldCheckSum == this.checksum;
}
short getChecksum(byte[] data){
//TODO: complete checksum
return (short)0;
}
public boolean isSyn(){
return flags[0];
}
public boolean isFin(){
return flags[1];
}
public boolean isAck(){
return flags[2];
}
public int getReturnAck(){
return this.seqNum + this.payloadData.length;
}
public byte[] serialize() {
int payloadLength = 0;
if(payloadData != null) {
payloadLength = this.payloadData.length;
}
int totalLength = headerSize + payloadLength;
byte[] data = new byte[totalLength];
ByteBuffer bb = ByteBuffer.wrap(data);
bb.putInt(this.seqNum);
bb.putInt(this.ackNum);
bb.putLong(this.time);
int sizeWithFlags = payloadLength;
for (int i=0; i<3; i++) {
sizeWithFlags = sizeWithFlags << 1;
sizeWithFlags += flags[i] ? 1 : 0;
}
bb.putInt(sizeWithFlags);
bb.putShort((short)0);
this.checksum = 0;
bb.putShort(this.checksum);
if(payloadLength != 0) {
bb.put(this.payloadData);
}
// compute checksum if needed
if (this.checksum == 0) {
bb.rewind();
int accumulation = 0;
// TODO: check if changing to headerSize is correct ... was headerLength before
for (int i = 0; i < this.headerSize / 2; i++) {
accumulation += 0xffff & bb.getShort();
}
accumulation = ((accumulation >> 16) & 0xffff)
+ (accumulation & 0xffff);
this.checksum = (short) (~accumulation & 0xffff);
bb.putShort(22, this.checksum);
}
return data;
}
public TCPpacket deserialize(byte[] data) {
ByteBuffer bb = ByteBuffer.wrap(data);
this.seqNum = bb.getInt();
this.ackNum = bb.getInt();
this.time = bb.getLong();
int sizeWithFlags = bb.getInt();
int payloadLength = sizeWithFlags >> 3;
if(payloadLength > data.length - headerSize){
System.out.println("Incorrect data length");
return null;
}
for (int i = 0; i<3; i++) {
flags[2-i] = sizeWithFlags % 2 == 1;
sizeWithFlags = sizeWithFlags >> 1;
}
bb.getShort();
this.checksum = bb.getShort();
this.payloadData = new byte[payloadLength];
bb.get(payloadData);
return this;
}
}