org.pcap4j.packet.TcpPacket Java Examples
The following examples show how to use
org.pcap4j.packet.TcpPacket.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PacketUpdater.java From trex-stateless-gui with Apache License 2.0 | 7 votes |
private Packet fixL3L4Checksums(Packet packet) { Packet.Builder builder = packet.getBuilder(); builder.get(IpV4Packet.Builder.class).correctChecksumAtBuild(true); TcpPacket.Builder tcpBuilder = builder.get(TcpPacket.Builder.class); if (tcpBuilder != null) { tcpBuilder.srcAddr(packet.get(IpV4Packet.class).getHeader().getSrcAddr()); tcpBuilder.dstAddr(packet.get(IpV4Packet.class).getHeader().getDstAddr()); tcpBuilder.correctChecksumAtBuild(true); } UdpPacket.Builder udpBuilder = builder.get(UdpPacket.Builder.class); if (udpBuilder != null) { udpBuilder.srcAddr(packet.get(IpV4Packet.class).getHeader().getSrcAddr()); udpBuilder.dstAddr(packet.get(IpV4Packet.class).getHeader().getDstAddr()); udpBuilder.correctChecksumAtBuild(true); } return builder.build(); }
Example #2
Source File: TCPTun.java From NSS with Apache License 2.0 | 6 votes |
void onReceiveDataPacket(TcpPacket tcpPacket, TcpHeader tcpHeader, IpV4Header ipV4Header ){ if(System.currentTimeMillis()-lastSendAckTime>1000){ int rs=tcpHeader.getSequenceNumber()+tcpPacket.getPayload().getRawData().length; if(rs>remoteSequence_max){ remoteSequence_max=rs; } Packet ackPacket=PacketUtils.createAck( capEnv.local_mac, capEnv.gateway_mac, localAddress,(short)localPort, ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(), remoteSequence_max, localSequence,getIdent()); try { sendHandle.sendPacket(ackPacket); } catch (Exception e) { e.printStackTrace(); } lastSendAckTime=System.currentTimeMillis(); lastReceiveDataTime=System.currentTimeMillis(); } }
Example #3
Source File: TCPTun.java From finalspeed with GNU General Public License v2.0 | 6 votes |
void onReceiveDataPacket(TcpPacket tcpPacket, TcpHeader tcpHeader, IpV4Header ipV4Header) { if (System.currentTimeMillis() - lastSendAckTime > 1000) { int rs = tcpHeader.getSequenceNumber() + tcpPacket.getPayload().getRawData().length; if (rs > remoteSequence_max) { remoteSequence_max = rs; } Packet ackPacket = PacketUtils.createAck( capEnv.local_mac, capEnv.gateway_mac, localAddress, localPort, ipV4Header.getSrcAddr(), tcpHeader.getSrcPort().value(), remoteSequence_max, localSequence, getIdent()); try { sendHandle.sendPacket(ackPacket); } catch (Exception e) { e.printStackTrace(); } lastSendAckTime = System.currentTimeMillis(); lastReceiveDataTime = System.currentTimeMillis(); } }
Example #4
Source File: TCPTun.java From finalspeed-91yun with GNU General Public License v2.0 | 6 votes |
void onReceiveDataPacket(TcpPacket tcpPacket,TcpHeader tcpHeader,IpV4Header ipV4Header ){ if(System.currentTimeMillis()-lastSendAckTime>1000){ int rs=tcpHeader.getSequenceNumber()+tcpPacket.getPayload().getRawData().length; if(rs>remoteSequence_max){ remoteSequence_max=rs; } Packet ackPacket=PacketUtils.createAck( capEnv.local_mac, capEnv.gateway_mac, localAddress,(short)localPort, ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(), remoteSequence_max, localSequence,getIdent()); try { sendHandle.sendPacket(ackPacket); } catch (Exception e) { e.printStackTrace(); } lastSendAckTime=System.currentTimeMillis(); lastReceiveDataTime=System.currentTimeMillis(); } }
Example #5
Source File: PacketUpdater.java From trex-stateless-gui with Apache License 2.0 | 5 votes |
/** * Return source port * * @param packet * @return */ private String getSrcPort(Packet packet) { if (protocolType.equals("TCP")) { return packet.get(TcpPacket.class).getHeader().getSrcPort().toString(); } else if (protocolType.equals("UDP")) { return packet.get(UdpPacket.class).getHeader().getSrcPort().toString(); } return "80"; }
Example #6
Source File: PacketUpdater.java From trex-stateless-gui with Apache License 2.0 | 5 votes |
/** * Return destination port * * @param packet * @return */ private String getDstPort(Packet packet) { if (protocolType.equals("TCP")) { return packet.get(TcpPacket.class).getHeader().getDstPort().toString(); } else if (protocolType.equals("UDP")) { return packet.get(UdpPacket.class).getHeader().getDstPort().toString(); } return "80"; }
Example #7
Source File: PacketParser.java From trex-stateless-gui with Apache License 2.0 | 4 votes |
/** * Extract packet info * * @param packet * @return */ private PacketInfo extractPacketInfo(Packet packet) { if (packet != null) { packetInfo.setPacketHex(formatHex(DatatypeConverter.printHexBinary(packet.getRawData()))); packetInfo.setPacketRawData(formatPayLoad(new String(packet.getRawData()))); } // defien whether packet has vlan or not if(packet.get(Dot1qVlanTagPacket.class) != null){ packetInfo.setVlanPacket(true); } // If the packet has Ethernet if (packet.get(EthernetPacket.class) != null) { packetInfo.setEthernetHex(getHeaderOffset(packet.get(EthernetPacket.class).getHeader().toHexString().toUpperCase())); packetInfo.setEthernetRawData(new String()); packetInfo.setDestMac(packet.get(EthernetPacket.class).getHeader().getDstAddr().toString()); packetInfo.setSrcMac(packet.get(EthernetPacket.class).getHeader().getSrcAddr().toString()); } // if the packet has IPV4 if (packet.get(IpV4Packet.class) != null) { packetInfo.setIpv4Hex(getHeaderOffset(packet.get(IpV4Packet.class).getHeader().toHexString().toUpperCase())); packetInfo.setIpv4RawData(new String()); packetInfo.setDestIpv4(packet.get(IpV4Packet.class).getHeader().getDstAddr().getHostAddress()); packetInfo.setSrcIpv4(packet.get(IpV4Packet.class).getHeader().getSrcAddr().getHostAddress()); } // if the packet has TCP if (packet.get(TcpPacket.class) != null) { packetInfo.setL4Name("TCP"); packetInfo.setL4Hex(getHeaderOffset(packet.get(TcpPacket.class).getHeader().toHexString().toUpperCase())); packetInfo.setL4RawData(new String()); if (packet.get(TcpPacket.class).getPayload() != null) { packetInfo.setPacketPayLoad(getHeaderOffset(spaceHex(Hex.encodeHexString(packet.get(TcpPacket.class).getPayload().getRawData())))); } else { packetInfo.setPacketPayLoad(null); } } // if the packet has UDP if (packet.get(UdpPacket.class) != null) { packetInfo.setL4Name("UDP"); packetInfo.setL4Hex(getHeaderOffset(packet.get(UdpPacket.class).getHeader().toHexString().toUpperCase())); packetInfo.setL4RawData(new String()); if (packet.get(UdpPacket.class).getPayload() != null) { packetInfo.setPacketPayLoad(getHeaderOffset(spaceHex(Hex.encodeHexString(packet.get(UdpPacket.class).getPayload().getRawData())))); } else { packetInfo.setPacketPayLoad(null); } } return packetInfo; }
Example #8
Source File: TCPTun.java From finalspeed-91yun with GNU General Public License v2.0 | 4 votes |
public void process_server(final Packet packet,EthernetHeader ethernetHeader,IpV4Header ipV4Header,TcpPacket tcpPacket,boolean client){ TcpHeader tcpHeader=tcpPacket.getHeader(); if(!preDataReady){ if(!connectReady){ //第一次握手 dstMacaAddress=ethernetHeader.getSrcAddr(); if(tcpHeader.getSyn()&&!tcpHeader.getAck()){ remoteStartSequence=tcpHeader.getSequenceNumber(); remoteSequence=remoteStartSequence+1; remoteSequence_max=remoteSequence; MLog.println("接收第一次握手 "+remoteAddress.getHostAddress()+":"+remotePort+"->"+localAddress.getHostAddress()+":"+localPort+" ident "+ipV4Header.getIdentification()); MLog.println(""+packet); Packet responePacket=PacketUtils.createSyncAck( capEnv.local_mac, capEnv.gateway_mac, localAddress,(short)localPort, ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(), tcpHeader.getSequenceNumber()+1,localStartSequence,(short)0 ); try { sendHandle.sendPacket(responePacket); } catch (Exception e) { e.printStackTrace(); } localSequence=localStartSequence+1; MLog.println("发送第二次握手 "+capEnv.local_mac+"->"+capEnv.gateway_mac+" "+localAddress+"->"+" ident "+0); MLog.println(""+responePacket); } if(!tcpHeader.getSyn()&&tcpHeader.getAck()){ if(tcpPacket.getPayload()==null){ //第三次握手,客户端确认 if(tcpHeader.getAcknowledgmentNumber()==localSequence){ MLog.println("接收第三次握手 "+" ident "+ipV4Header.getIdentification()); MLog.println(packet+""); Thread t1=new Thread(){ public void run(){ //startSend(basePacket_server,syc_sequence_client+1); } }; //t1.start(); connectReady=true; } } //MLog.println("客户端响应preview\n "+packet); //MLog.println("request "+tcp.ack()); sendedTable_server.remove(tcpHeader.getAcknowledgmentNumber()); boolean selfAck=selfAckTable.contains(ipV4Header.getIdentification()); //MLog.println("客户端确认 "+"selfack "+selfAck+" id "+ipV4Header.getIdentification()+" ack_sequence "+tcpHeader.getAcknowledgmentNumberAsLong()+" "+sendedTable_server.size()+"ppppppp "+tcpHeader); } }else { if(tcpPacket.getPayload()!=null){ preDataReady=true; onReceiveDataPacket( tcpPacket, tcpHeader, ipV4Header ); byte[] sim=getSimResponeHead(); sendData(sim); } } }else { if(tcpPacket.getPayload()!=null){ onReceiveDataPacket( tcpPacket, tcpHeader, ipV4Header ); TunData td=new TunData(); td.tun=this; td.data=tcpPacket.getPayload().getRawData(); capEnv.vDatagramSocket.onReceinveFromTun(td); } } if(tcpHeader.getRst()){ MLog.println("reset packet "+ipV4Header.getIdentification()+" "+tcpHeader.getSequenceNumber()+" "+remoteAddress.getHostAddress()+":"+remotePort+"->"+localAddress.getHostAddress()+":"+localPort+" "+" ident "+ipV4Header.getIdentification()); } }
Example #9
Source File: TrexTcpPacket.java From trex-stateless-gui with Apache License 2.0 | 4 votes |
/** * Build packet */ public void buildPacket() { int calculatedLength = getPacketLength() - 14; String payloadString = payload.getPayloadType().getPadPayloadString(payload.getPayloadPattern(), calculatedLength * 2); UnknownPacket.Builder unknownb = null; if (calculatedLength != 0) { unknownb = new UnknownPacket.Builder(); unknownb.rawData(payload.getPayloadPad(payloadString, calculatedLength)); } ArrayList<TcpOption> options = new ArrayList<TcpOption>(); options.add(TcpEndOfOptionList.getInstance()); tcpBuilder = new TcpPacket.Builder(); tcpBuilder.dstPort(dstPort) .srcPort(srcPort) .sequenceNumber(sequenceNumber) .acknowledgmentNumber(acknowledgmentNumber) .dataOffset(dataOffset) .reserved(reserved) .urg(urg) .ack(ack) .psh(psh) .rst(rst) .syn(syn) .fin(fin) .window(window) .checksum(checksum) .urgentPointer(urgentPointer) .options(options) .padding(new byte[]{(byte) 0xaa}) .correctChecksumAtBuild(false) .correctLengthAtBuild(false) .paddingAtBuild(false); if (calculatedLength != 0) { tcpBuilder.payloadBuilder(unknownb); } this.packet = tcpBuilder.build(); }
Example #10
Source File: PacketUtils.java From finalspeed-91yun with GNU General Public License v2.0 | 4 votes |
static Packet createDataPacket( MacAddress srcAddress_mac, MacAddress dstAddrress_mac, Inet4Address srcAddress,short srcPort, Inet4Address dstAddress,short dstPort, int sequence,int ack, byte[] data,short ident){ Packet p=null; TcpPacket.Builder builder_tcp=new TcpPacket.Builder(); builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(data)); builder_tcp.correctChecksumAtBuild(true); builder_tcp.correctLengthAtBuild(true); builder_tcp.paddingAtBuild(true); builder_tcp.ack(true); builder_tcp.acknowledgmentNumber(ack); //builder_tcp.checksum(tcpHeader.getChecksum()); //builder_tcp.dataOffset((byte)8); builder_tcp.dstAddr(dstAddress); builder_tcp.dstPort(new TcpPort( dstPort,"")); builder_tcp.fin(false); //builder_tcp.options(tcpHeader.getOptions()); //builder_tcp.padding(tcpHeader.getPadding()); builder_tcp.psh(false); builder_tcp.reserved((byte) 0); builder_tcp.rst(false); builder_tcp.sequenceNumber(sequence); builder_tcp.srcAddr(srcAddress); builder_tcp.srcPort(new TcpPort( srcPort,"")); builder_tcp.syn(false); builder_tcp.urg(false); //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer()); builder_tcp.window( window); IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder(); builder_ipv4.correctChecksumAtBuild(true); builder_ipv4.correctLengthAtBuild(true); builder_ipv4.dontFragmentFlag(true); builder_ipv4.paddingAtBuild(true); builder_ipv4.dstAddr(dstAddress); builder_ipv4.fragmentOffset( (short)0); //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum()); //short identification= Math.abs(random.nextInt(Short.MAX_VALUE)); //identification=ident; builder_ipv4.identification(ident); builder_ipv4.ihl((byte) 5); builder_ipv4.moreFragmentFlag(false); //builder_ipv4.options(ipV4Header.getOptions()); //builder_ipv4.padding(ipV4Header.getPadding()); builder_ipv4.protocol(IpNumber.TCP); //builder_ipv4.reservedFlag(ipV4Header.getReservedFlag()); builder_ipv4.srcAddr(srcAddress); builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0)); //builder_ipv4.totalLength( 52); builder_ipv4.ttl(ttl); builder_ipv4.version(IpVersion.IPV4); builder_ipv4.payloadBuilder(builder_tcp); p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4); return p; }
Example #11
Source File: PacketUtils.java From finalspeed-91yun with GNU General Public License v2.0 | 4 votes |
static Packet createAck( MacAddress srcAddress_mac, MacAddress dstAddrress_mac, Inet4Address srcAddress,short srcPort, Inet4Address dstAddress,short dstPort, int ack_sequence,int sequence,short ident){ TcpPacket.Builder builder_tcp=new TcpPacket.Builder(); //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0])); builder_tcp.correctChecksumAtBuild(true); builder_tcp.correctLengthAtBuild(true); builder_tcp.paddingAtBuild(true); builder_tcp.ack(true); builder_tcp.acknowledgmentNumber(ack_sequence); //builder_tcp.checksum(tcpHeader.getChecksum()); //builder_tcp.dataOffset((byte) 8); builder_tcp.dstAddr(dstAddress); builder_tcp.dstPort(new TcpPort( dstPort,"")); //builder_tcp.fin(tcpHeader.getFin()); builder_tcp.psh(false); builder_tcp.reserved((byte) 0); builder_tcp.rst(false); builder_tcp.sequenceNumber(sequence); builder_tcp.srcAddr(srcAddress); builder_tcp.srcPort(new TcpPort( srcPort,"")); builder_tcp.syn(false); builder_tcp.urg(false); //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer()); builder_tcp.window( window); IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder(); builder_ipv4.correctChecksumAtBuild(true); builder_ipv4.correctLengthAtBuild(true); builder_ipv4.paddingAtBuild(true); builder_ipv4.dstAddr(dstAddress); builder_ipv4.dontFragmentFlag(true); builder_ipv4.fragmentOffset( (short) 0); //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum()); //short identification= Math.abs(random.nextInt(Short.MAX_VALUE)); builder_ipv4.identification(ident); builder_ipv4.ihl((byte) 5); //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag()); //builder_ipv4.options(ipV4Header.getOptions()); //builder_ipv4.padding(ipV4Header.getPadding()); builder_ipv4.protocol(IpNumber.TCP); // builder_ipv4.reservedFlag(ipV4Header.getReservedFlag()); builder_ipv4.srcAddr(srcAddress); builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0)); //builder_ipv4.totalLength( 52); builder_ipv4.ttl(ttl); builder_ipv4.version(IpVersion.IPV4); builder_ipv4.payloadBuilder(builder_tcp); // Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4); //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader()); return p; }
Example #12
Source File: PacketUtils.java From finalspeed-91yun with GNU General Public License v2.0 | 4 votes |
static Packet createSyncAck( MacAddress srcAddress_mac, MacAddress dstAddrress_mac, Inet4Address srcAddress,short srcPort, Inet4Address dstAddress,short dstPort, int ack_sequence,int sequence,short ident){ TcpPacket.Builder builder_tcp=new TcpPacket.Builder(); //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0])); builder_tcp.correctChecksumAtBuild(true); builder_tcp.correctLengthAtBuild(true); builder_tcp.paddingAtBuild(true); builder_tcp.ack(true); builder_tcp.acknowledgmentNumber(ack_sequence); //builder_tcp.checksum(tcpHeader.getChecksum()); //builder_tcp.dataOffset((byte) 8); builder_tcp.dstAddr(dstAddress); builder_tcp.dstPort(new TcpPort(dstPort,"")); //builder_tcp.fin(tcpHeader.getFin()); ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>(); TcpNoOperationOption nop=TcpNoOperationOption.getInstance(); TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build(); tcp_options.add(seg_option); tcp_options.add(nop); tcp_options.add(nop); TcpSackPermittedOption sack_permit_option=TcpSackPermittedOption.getInstance(); tcp_options.add(sack_permit_option); tcp_options.add(nop); TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount(shiftCount).correctLengthAtBuild(true).build(); tcp_options.add(win_option); builder_tcp.options(tcp_options); //builder_tcp.padding(tcpHeader.getPadding()); builder_tcp.psh(false); builder_tcp.reserved((byte) 0); builder_tcp.rst(false); builder_tcp.sequenceNumber(sequence); builder_tcp.srcAddr(srcAddress); builder_tcp.srcPort(new TcpPort(srcPort,"")); builder_tcp.syn(true); builder_tcp.urg(false); //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer()); builder_tcp.window( window); IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder(); builder_ipv4.correctChecksumAtBuild(true); builder_ipv4.correctLengthAtBuild(true); builder_ipv4.paddingAtBuild(true); builder_ipv4.dstAddr(dstAddress); builder_ipv4.dontFragmentFlag(true); builder_ipv4.fragmentOffset((short)0); //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum()); // short identification= Math.abs(random.nextInt(Short.MAX_VALUE)); builder_ipv4.identification(ident); builder_ipv4.ihl((byte) 5); //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag()); //builder_ipv4.options(ipV4Header.getOptions()); //builder_ipv4.padding(ipV4Header.getPadding()); builder_ipv4.protocol(IpNumber.TCP); // builder_ipv4.reservedFlag(ipV4Header.getReservedFlag()); builder_ipv4.srcAddr(srcAddress); builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0)); //builder_ipv4.totalLength( 52); builder_ipv4.ttl(ttl); builder_ipv4.version(IpVersion.IPV4); builder_ipv4.payloadBuilder(builder_tcp); // Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4); //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader()); return p; }
Example #13
Source File: PacketUtils.java From finalspeed-91yun with GNU General Public License v2.0 | 4 votes |
static Packet createSync( MacAddress srcAddress_mac, MacAddress dstAddrress_mac, Inet4Address srcAddress,short srcPort, Inet4Address dstAddress,short dstPort, int sequence,short ident){ TcpPacket.Builder builder_tcp=new TcpPacket.Builder(); //builder_tcp.payloadBuilder(new UnknownPacket.Builder().rawData(new byte[0])); builder_tcp.correctChecksumAtBuild(true); builder_tcp.correctLengthAtBuild(true); builder_tcp.paddingAtBuild(true); //builder_tcp.ack(true); //builder_tcp.acknowledgmentNumber(ack_sequence); //builder_tcp.checksum(tcpHeader.getChecksum()); //builder_tcp.dataOffset((byte) 8); builder_tcp.dstAddr(dstAddress); builder_tcp.dstPort(new TcpPort( dstPort,"")); //builder_tcp.fin(tcpHeader.getFin()); TcpNoOperationOption nop=TcpNoOperationOption.getInstance(); ArrayList<TcpOption> tcp_options=new ArrayList<TcpOption>(); TcpMaximumSegmentSizeOption seg_option=new TcpMaximumSegmentSizeOption.Builder().maxSegSize(mtu).correctLengthAtBuild(true).build(); tcp_options.add(seg_option); tcp_options.add(nop); TcpWindowScaleOption win_option=new TcpWindowScaleOption.Builder().shiftCount((byte)6).correctLengthAtBuild(true).build(); tcp_options.add(win_option); tcp_options.add(nop); tcp_options.add(nop); TcpSackPermittedOption sack_permit_option=TcpSackPermittedOption.getInstance(); tcp_options.add(sack_permit_option); builder_tcp.options(tcp_options); //builder_tcp.padding(tcpHeader.getPadding()); builder_tcp.psh(false); builder_tcp.reserved((byte) 0); builder_tcp.rst(false); builder_tcp.sequenceNumber(sequence); builder_tcp.srcAddr(srcAddress); builder_tcp.srcPort(new TcpPort( srcPort,"")); builder_tcp.syn(true); builder_tcp.urg(false); //builder_tcp.urgentPointer(tcpHeader.getUrgentPointer()); builder_tcp.window( window); IpV4Packet.Builder builder_ipv4=new IpV4Packet.Builder(); builder_ipv4.correctChecksumAtBuild(true); builder_ipv4.correctLengthAtBuild(true); builder_ipv4.paddingAtBuild(true); builder_ipv4.dstAddr(dstAddress); builder_ipv4.dontFragmentFlag(true); builder_ipv4.fragmentOffset((short)0); //builder_ipv4.headerChecksum(ipV4Header.getHeaderChecksum()); //short identification= Math.abs(random.nextInt(Short.MAX_VALUE)); builder_ipv4.identification(ident); builder_ipv4.ihl((byte) 5); //builder_ipv4.moreFragmentFlag(ipV4Header.getMoreFragmentFlag()); //builder_ipv4.options(ipV4Header.getOptions()); //builder_ipv4.padding(ipV4Header.getPadding()); builder_ipv4.protocol(IpNumber.TCP); // builder_ipv4.reservedFlag(ipV4Header.getReservedFlag()); builder_ipv4.srcAddr(srcAddress); builder_ipv4.tos(IpV4Rfc1349Tos.newInstance((byte) 0)); //builder_ipv4.totalLength( 52); builder_ipv4.ttl(ttl); builder_ipv4.version(IpVersion.IPV4); builder_ipv4.payloadBuilder(builder_tcp); // Packet p = buildIpV4(srcAddress_mac,dstAddrress_mac,builder_ipv4); // IpV4Packet p4=builder_ipv4.build(); // TcpPacket tcpPacket=builder_tcp.build(); //selfAckTable.add(identification); //System.out.println("自定义确认 "+" identification "+identification+" ack_sequence "+ack_sequence+" # "+tcpPacket.getHeader()); return p; }
Example #14
Source File: TCPTun.java From finalspeed with GNU General Public License v2.0 | 4 votes |
public void process_server(final Packet packet, EthernetHeader ethernetHeader, IpV4Header ipV4Header, TcpPacket tcpPacket, boolean client) { TcpHeader tcpHeader = tcpPacket.getHeader(); if (!preDataReady) { if (!connectReady) { //第一次握手 dstMacaAddress = ethernetHeader.getSrcAddr(); if (tcpHeader.getSyn() && !tcpHeader.getAck()) { remoteStartSequence = tcpHeader.getSequenceNumber(); remoteSequence = remoteStartSequence + 1; remoteSequence_max = remoteSequence; MLog.println("接收第一次握手 " + remoteAddress.getHostAddress() + ":" + remotePort + "->" + localAddress .getHostAddress() + ":" + localPort + " ident " + ipV4Header.getIdentification()); MLog.println("" + packet); Packet responePacket = PacketUtils.createSyncAck( capEnv.local_mac, capEnv.gateway_mac, localAddress, localPort, ipV4Header.getSrcAddr(), tcpHeader.getSrcPort().value(), tcpHeader.getSequenceNumber() + 1, localStartSequence, (short) 0 ); try { sendHandle.sendPacket(responePacket); } catch (Exception e) { e.printStackTrace(); } localSequence = localStartSequence + 1; MLog.println("发送第二次握手 " + capEnv.local_mac + "->" + capEnv.gateway_mac + " " + localAddress + "->" + " ident " + 0); MLog.println("" + responePacket); } if (!tcpHeader.getSyn() && tcpHeader.getAck()) { if (tcpPacket.getPayload() == null) { //第三次握手,客户端确认 if (tcpHeader.getAcknowledgmentNumber() == localSequence) { MLog.println("接收第三次握手 " + " ident " + ipV4Header.getIdentification()); MLog.println(packet + ""); Thread t1 = new Thread() { public void run() { //startSend(basePacket_server,syc_sequence_client+1); } }; //t1.start(); connectReady = true; } } //MLog.println("客户端响应preview\n "+packet); //MLog.println("request "+tcp.ack()); sendedTable_server.remove(tcpHeader.getAcknowledgmentNumber()); boolean selfAck = selfAckTable.contains(ipV4Header.getIdentification()); //MLog.println("客户端确认 "+"selfack "+selfAck+" id "+ipV4Header.getIdentification()+" ack_sequence // "+tcpHeader.getAcknowledgmentNumberAsLong()+" "+sendedTable_server.size()+"ppppppp "+tcpHeader); } } else { if (tcpPacket.getPayload() != null) { preDataReady = true; onReceiveDataPacket(tcpPacket, tcpHeader, ipV4Header); byte[] sim = getSimResponeHead(); sendData(sim); } } } else { if (tcpPacket.getPayload() != null) { onReceiveDataPacket(tcpPacket, tcpHeader, ipV4Header); TunData td = new TunData(); td.tun = this; td.data = tcpPacket.getPayload().getRawData(); capEnv.vDatagramSocket.onReceinveFromTun(td); } } if (tcpHeader.getRst()) { MLog.println("reset packet " + ipV4Header.getIdentification() + " " + tcpHeader.getSequenceNumber() + " " + remoteAddress.getHostAddress() + ":" + remotePort + "->" + localAddress.getHostAddress() + ":" + localPort + " " + " ident " + ipV4Header.getIdentification()); } }
Example #15
Source File: TCPTun.java From finalspeed with GNU General Public License v2.0 | 4 votes |
public void process_client(CapEnv capEnv, final Packet packet, EthernetHeader ethernetHeader, IpV4Header ipV4Header, TcpPacket tcpPacket, boolean client) { TcpHeader tcpHeader = tcpPacket.getHeader(); if (!preDataReady) { if (!connectReady) { if (tcpHeader.getAck() && tcpHeader.getSyn()) { if (tcpHeader.getAcknowledgmentNumber() == (localStartSequence + 1)) { MLog.println("接收第二次握手 " + " ident " + ipV4Header.getIdentification()); MLog.println("" + packet); remoteStartSequence = tcpHeader.getSequenceNumber(); remoteSequence = remoteStartSequence + 1; remoteSequence_max = remoteSequence; Packet p3 = PacketUtils.createAck(capEnv.local_mac, capEnv.gateway_mac, capEnv.local_ipv4, localPort, remoteAddress, remotePort, remoteSequence, localSequence, getIdent()); try { sendHandle.sendPacket(p3); MLog.println("发送第三次握手 " + " ident " + localIdent); MLog.println("" + p3); connectReady = true; byte[] sim = getSimRequestHead(remotePort); sendData(sim); MLog.println("发送请求 " + " ident " + localIdent); } catch (PcapNativeException | NotOpenException e) { e.printStackTrace(); } } } } else { if (tcpPacket.getPayload() != null) { preDataReady = true; onReceiveDataPacket(tcpPacket, tcpHeader, ipV4Header); MLog.println("接收响应 " + " ident " + ipV4Header.getIdentification()); } } } else { if (tcpPacket.getPayload() != null) { //MLog.println("客户端正式接收数据 "+capClientEnv.vDatagramSocket); onReceiveDataPacket(tcpPacket, tcpHeader, ipV4Header); TunData td = new TunData(); td.tun = this; td.data = tcpPacket.getPayload().getRawData(); capEnv.vDatagramSocket. onReceinveFromTun(td); } } if (tcpHeader.getRst()) { MLog.println("reset packet " + ipV4Header.getIdentification() + " " + tcpHeader.getSequenceNumber() + " " + remoteAddress.getHostAddress() + ":" + remotePort + "->" + localAddress.getHostAddress() + ":" + localPort); } }
Example #16
Source File: TCPTun.java From NSS with Apache License 2.0 | 4 votes |
public void process_server(final Packet packet, EthernetHeader ethernetHeader, IpV4Header ipV4Header, TcpPacket tcpPacket, boolean client){ TcpHeader tcpHeader=tcpPacket.getHeader(); if(!preDataReady){ if(!connectReady){ //第一次握手 dstMacaAddress=ethernetHeader.getSrcAddr(); if(tcpHeader.getSyn()&&!tcpHeader.getAck()){ remoteStartSequence=tcpHeader.getSequenceNumber(); remoteSequence=remoteStartSequence+1; remoteSequence_max=remoteSequence; MLog.println("接收第一次握手 "+remoteAddress.getHostAddress()+":"+remotePort+"->"+localAddress.getHostAddress()+":"+localPort+" ident "+ipV4Header.getIdentification()); MLog.println(""+packet); Packet responePacket=PacketUtils.createSyncAck( capEnv.local_mac, capEnv.gateway_mac, localAddress,(short)localPort, ipV4Header.getSrcAddr(),tcpHeader.getSrcPort().value(), tcpHeader.getSequenceNumber()+1,localStartSequence,(short)0 ); try { sendHandle.sendPacket(responePacket); } catch (Exception e) { e.printStackTrace(); } localSequence=localStartSequence+1; MLog.println("发送第二次握手 "+capEnv.local_mac+"->"+capEnv.gateway_mac+" "+localAddress+"->"+" ident "+0); MLog.println(""+responePacket); } if(!tcpHeader.getSyn()&&tcpHeader.getAck()){ if(tcpPacket.getPayload()==null){ //第三次握手,客户端确认 if(tcpHeader.getAcknowledgmentNumber()==localSequence){ MLog.println("接收第三次握手 "+" ident "+ipV4Header.getIdentification()); MLog.println(packet+""); Thread t1=new Thread(){ public void run(){ //startSend(basePacket_server,syc_sequence_client+1); } }; //t1.start(); connectReady=true; } } //MLog.println("客户端响应preview\n "+packet); //MLog.println("request "+tcp.ack()); sendedTable_server.remove(tcpHeader.getAcknowledgmentNumber()); boolean selfAck=selfAckTable.contains(ipV4Header.getIdentification()); //MLog.println("客户端确认 "+"selfack "+selfAck+" id "+ipV4Header.getIdentification()+" ack_sequence "+tcpHeader.getAcknowledgmentNumberAsLong()+" "+sendedTable_server.size()+"ppppppp "+tcpHeader); } }else { if(tcpPacket.getPayload()!=null){ preDataReady=true; onReceiveDataPacket( tcpPacket, tcpHeader, ipV4Header ); byte[] sim=getSimResponeHead(); sendData(sim); } } }else { if(tcpPacket.getPayload()!=null){ onReceiveDataPacket( tcpPacket, tcpHeader, ipV4Header ); TunData td=new TunData(); td.tun=this; td.data=tcpPacket.getPayload().getRawData(); capEnv.vDatagramSocket.onReceinveFromTun(td); } } if(tcpHeader.getRst()){ MLog.println("reset packet "+ipV4Header.getIdentification()+" "+tcpHeader.getSequenceNumber()+" "+remoteAddress.getHostAddress()+":"+remotePort+"->"+localAddress.getHostAddress()+":"+localPort+" "+" ident "+ipV4Header.getIdentification()); } }
Example #17
Source File: TrexTcpPacket.java From trex-stateless-gui with Apache License 2.0 | 2 votes |
/** * * @return */ public TcpPacket.Builder getTcpBuilder() { return tcpBuilder; }
Example #18
Source File: TrexTcpPacket.java From trex-stateless-gui with Apache License 2.0 | 2 votes |
/** * * @return */ public TcpPacket.Builder getBuilder() { return tcpBuilder; }