-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpPacket.java
121 lines (97 loc) · 3.02 KB
/
IpPacket.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
package com.company;
import java.lang.*;
import static java.lang.Math.abs;
public class IpPacket {
private String srcIp;
private String dstIp;
private String srcPort;
private String dstPort;
private int ipHeaderLen;
private int ipTotalLen;
private int transHeaderLen;
private int transLayerType;
private int dataLen;
public int getDataLen() {
return dataLen;
}
public void setDataLen(int dataLen) {
this.dataLen = dataLen;
}
public String getDstPort() {
return dstPort;
}
public String getSrcPort() {
return srcPort;
}
public void setDstPort(String dstPort) {
this.dstPort = dstPort;
}
public void setSrcPort(String srcPort) {
this.srcPort = srcPort;
}
public int getTransLayerType() {
return transLayerType;
}
public void setTransLayerType(int transLayerType) {
this.transLayerType = transLayerType;
}
public void setSrcIp(String srcIp) {
this.srcIp = srcIp;
}
public String getSrcIp() {
return srcIp;
}
public String getDstIp() {
return dstIp;
}
public void setDstIp(String dstIp) {
this.dstIp = dstIp;
}
public int getIpHeaderLen() {
return ipHeaderLen;
}
public void setIpHeaderLen(int ipHeaderLen) {
this.ipHeaderLen = ipHeaderLen;
}
public int getIpTotalLen() {
return ipTotalLen;
}
public void setIpTotalLen(int ipTotalLen) {
this.ipTotalLen = ipTotalLen;
}
public int getTransHeaderLen() {
return transHeaderLen;
}
public void setTransHeaderLen(int transHeaderLen) {
this.transHeaderLen = transHeaderLen;
}
public void parserIpTransLayer(byte[] packetBody, int flag) {
//取出IP头长度
ipHeaderLen = 4*(packetBody[14]& 0x0f);
//System.out.println("=ipHeaderLength==="+ipHeaderLen);
//取出IP包总长度
ipTotalLen = DataUtils.byteArrayToInt2(DataUtils.cutBytes(packetBody, 16, 18));
//System.out.println("======ipTotalLength====="+ipTotalLen);
if(flag==0){
transHeaderLen = 4*(((int)(packetBody[46] >> 4)) & 0xf);
//System.out.println("========packet======="+(int)((packetBody[46]>>4) & 0xff));
}else {
transHeaderLen = 8;
}
this.transLayerType = ((int)(packetBody[23]))&0xff;
//System.out.println("=======translayertype========"+transLayerType);
this.setTransHeaderLen(transHeaderLen);
//System.out.println("=========transHeaderLen========"+transHeaderLen);
srcPort = DataUtils.byteArrayToInt2(DataUtils.cutBytes(packetBody,34,36))+"";
dstPort = DataUtils.byteArrayToInt2(DataUtils.cutBytes(packetBody,36,38))+"";
}
public String getIpAddr(byte[] ipaddr) {
String ipStr = "";
for(int i=0; i<3; i++){
ipStr += (int)(ipaddr[i]&0xff);
ipStr += ".";
}
ipStr += (int)(ipaddr[3]&0xff);
return ipStr;
}
}