1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.pcap;
17
18 import io.netty.buffer.ByteBuf;
19
20 final class TCPPacket {
21
22
23
24
25 private static final short OFFSET = 0x5000;
26
27 private TCPPacket() {
28
29 }
30
31
32
33
34
35
36
37
38
39 static void writePacket(ByteBuf byteBuf, ByteBuf payload, int segmentNumber, int ackNumber, int srcPort,
40 int dstPort, TCPFlag... tcpFlags) {
41
42 byteBuf.writeShort(srcPort);
43 byteBuf.writeShort(dstPort);
44 byteBuf.writeInt(segmentNumber);
45 byteBuf.writeInt(ackNumber);
46 byteBuf.writeShort(OFFSET | TCPFlag.getFlag(tcpFlags));
47 byteBuf.writeShort(65535);
48 byteBuf.writeShort(0x0001);
49 byteBuf.writeShort(0);
50
51 if (payload != null) {
52 byteBuf.writeBytes(payload);
53 }
54 }
55
56 enum TCPFlag {
57 FIN(1),
58 SYN(1 << 1),
59 RST(1 << 2),
60 PSH(1 << 3),
61 ACK(1 << 4),
62 URG(1 << 5),
63 ECE(1 << 6),
64 CWR(1 << 7);
65
66 private final int value;
67
68 TCPFlag(int value) {
69 this.value = value;
70 }
71
72 static int getFlag(TCPFlag... tcpFlags) {
73 int flags = 0;
74
75 for (TCPFlag tcpFlag : tcpFlags) {
76 flags |= tcpFlag.value;
77 }
78
79 return flags;
80 }
81 }
82 }