org.pcap4j.packet.IpV4Packet.IpV4Header Java Examples

The following examples show how to use org.pcap4j.packet.IpV4Packet.IpV4Header. 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: TCPTun.java    From NSS with Apache License 2.0 6 votes vote down vote up
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 #2
Source File: TCPTun.java    From finalspeed-91yun with GNU General Public License v2.0 6 votes vote down vote up
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 vote down vote up
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 NSS with Apache License 2.0 4 votes vote down vote up
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 #5
Source File: TCPTun.java    From finalspeed-91yun with GNU General Public License v2.0 4 votes vote down vote up
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 #6
Source File: TCPTun.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
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 #7
Source File: TCPTun.java    From finalspeed with GNU General Public License v2.0 4 votes vote down vote up
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);
    }

}