-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTPDatagram.java
208 lines (159 loc) · 6.58 KB
/
RTPDatagram.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import java.io.*;
import java.util.zip.CRC32;
import java.lang.*;
public class RTPDatagram implements Comparable<RTPDatagram>{
// In practice, the PSH, URG, and the urgent data pointer are not used.
// REMOVED FROM TCP
// public static final int URG = 0b100000;
public static final int ACK = 8;
// REMOVED FROM TCP
// public static final int PSH = 0b001000;
public static final int RST = 4;
public static final int SYN = 2;
public static final int FIN = 1;
public int sourcePort;
public int destPort;
public long sequenceNumber;
public long ackNumber;
// Reserved 4 bits for future purposes (possible flags)
public int reserved;
public int flags;
public int receiveWindowSize;
public byte[] receiveWindow;
// CRC32
public long checksum;
// REMOVED FROM TCP
// public int urgentDataPointer;
public byte[] data;
public RTPDatagram(int src, int dest, int flags, byte[] receiveWindow, byte[] data){
this.sourcePort = src;
this.destPort = dest;
this.flags = flags;
this.reserved = 0;
this.receiveWindow = receiveWindow;
receiveWindowSize = receiveWindow.length;
this.data = data;
}
public RTPDatagram(byte[] rawData){
// RTPUtil.debug("Creating RTPDatagram from raw bytes");
// RTPUtil.debug("-----------------------------------------------------------------------------");
int rawDataPointer = 0;
this.sourcePort = ((RTPUtil.toInt(rawData[rawDataPointer]) << 8) + (RTPUtil.toInt(rawData[rawDataPointer + 1]))) & 0x0000FFFF;
rawDataPointer += 2;
// RTPUtil.debug("sourcePort:" + this.sourcePort);
this.destPort = ((RTPUtil.toInt(rawData[rawDataPointer]) << 8) + (RTPUtil.toInt(rawData[rawDataPointer + 1]))) & 0x0000FFFF;
rawDataPointer += 2;
// RTPUtil.debug("destPort:" + this.destPort);
this.sequenceNumber = ((RTPUtil.toLong(rawData[rawDataPointer]) << 24) +
(RTPUtil.toLong(rawData[rawDataPointer + 1]) << 16) +
(RTPUtil.toLong(rawData[rawDataPointer + 2]) << 8) +
(RTPUtil.toLong(rawData[rawDataPointer + 3])));
rawDataPointer += 4;
// RTPUtil.debug("sequenceNumber:" + this.sequenceNumber);
this.ackNumber = ((RTPUtil.toLong(rawData[rawDataPointer]) << 24) +
(RTPUtil.toLong(rawData[rawDataPointer + 1]) << 16) +
(RTPUtil.toLong(rawData[rawDataPointer + 2]) << 8) +
(RTPUtil.toLong(rawData[rawDataPointer + 3])));
rawDataPointer += 4;
// RTPUtil.debug("ackNumber:" + this.ackNumber);
this.reserved = rawData[rawDataPointer] & 0xF;
this.flags = (rawData[rawDataPointer] >> 4) & 0xF;
rawDataPointer += 1;
// RTPUtil.debug("reserved:" + this.reserved);
// RTPUtil.debug("flags:" + this.flags);
this.receiveWindowSize = ((int)rawData[rawDataPointer]) & 0x000000FF;
rawDataPointer += 1;
// RTPUtil.debug("receiveWindowSize:" + this.receiveWindowSize);
this.receiveWindow = new byte[this.receiveWindowSize];
for (int i = 0; i < this.receiveWindowSize; i++){
this.receiveWindow[i] = rawData[rawDataPointer + i];
}
rawDataPointer += this.receiveWindowSize;
this.checksum = ((RTPUtil.toLong(rawData[rawDataPointer]) << 24) +
(RTPUtil.toLong(rawData[rawDataPointer + 1]) << 16) +
(RTPUtil.toLong(rawData[rawDataPointer + 2]) << 8) +
(RTPUtil.toLong(rawData[rawDataPointer + 3]))) & 0x00000000FFFFFFFFL;
rawDataPointer += 4;
// RTPUtil.debug("checksum:" + this.checksum);
data = new byte[rawData.length - rawDataPointer];
for (int i = 0; i < data.length; i++){
data[i] = rawData[rawDataPointer + i];
}
// RTPUtil.debug("-----------------------------------------------------------------------------");
}
public ByteArrayOutputStream getHeaderByteArrayNoChecksum(){
ByteArrayOutputStream bb = new ByteArrayOutputStream();
RTPUtil.writeByteArrayToByteArrayOutputStream(RTPUtil.toShortBytes(sourcePort), bb);
RTPUtil.writeByteArrayToByteArrayOutputStream(RTPUtil.toShortBytes(destPort), bb);
RTPUtil.writeByteArrayToByteArrayOutputStream(RTPUtil.toIntBytes(sequenceNumber), bb);
RTPUtil.writeByteArrayToByteArrayOutputStream(RTPUtil.toIntBytes(ackNumber), bb);
int reservedAndFlagsByte = (reserved & 0xF) | ((flags & 0xF) << 4);
bb.write((reservedAndFlagsByte & 0xFF));
bb.write((receiveWindowSize & 0xFF));
RTPUtil.writeByteArrayToByteArrayOutputStream(receiveWindow, bb);
return bb;
}
public boolean checkChecksum(){
ByteArrayOutputStream bb = getHeaderByteArrayNoChecksum();
CRC32 crc = new CRC32();
crc.update(bb.toByteArray());
crc.update(data);
// RTPUtil.debug("checking " + this.checksum + " against " + crc.getValue());
return new Long(this.checksum).equals(new Long(crc.getValue()));
}
public void updateChecksum(){
ByteArrayOutputStream bb = getHeaderByteArrayNoChecksum();
CRC32 crc = new CRC32();
crc.update(bb.toByteArray());
crc.update(data);
this.checksum = crc.getValue();
}
public byte[] getByteArray(){
ByteArrayOutputStream bb = getHeaderByteArrayNoChecksum();
RTPUtil.writeByteArrayToByteArrayOutputStream(RTPUtil.toIntBytes(this.checksum), bb);
RTPUtil.writeByteArrayToByteArrayOutputStream(data, bb);
return bb.toByteArray();
}
public String toString(){
String returnString = "--------------------Printing RTPDatagram-----------------------";
returnString +=
"\nSource port:" + sourcePort +
"\nDestination port:" + destPort +
"\nSequence number:" + sequenceNumber +
"\nAck number:" + ackNumber +
"\nflags:" + flags +
"\nchecksum:" + checksum +
"\nData:" + new String(data);
returnString += "\n----------------------------------------------------------------";
return returnString;
}
@Override public int compareTo(RTPDatagram datagram) {
if (this.sourcePort != datagram.sourcePort){
return new Integer(this.sourcePort).compareTo(new Integer(datagram.sourcePort));
} else if (this.destPort != datagram.destPort) {
return new Integer(this.destPort).compareTo(new Integer(datagram.destPort));
} else if (this.sequenceNumber != datagram.sequenceNumber){
return new Long(this.sequenceNumber).compareTo(new Long(datagram.sequenceNumber));
} else {
return new Long(this.ackNumber).compareTo(new Long(datagram.ackNumber));
}
}
@Override public boolean equals(Object obj) {
if (!(obj instanceof RTPDatagram))
return false;
if (obj == this)
return true;
RTPDatagram datagram = (RTPDatagram) obj;
if (this.sourcePort != datagram.sourcePort){
return false;
} else if (this.destPort != datagram.destPort) {
return false;
} else if (this.sequenceNumber != datagram.sequenceNumber){
return false;
} else if (this.ackNumber != datagram.ackNumber) {
return false;
} else {
return true;
}
}
}