package ZClient; import java.io.ByteArrayInputStream; import java.util.EventListener; import java.util.Scanner; import javax.swing.event.EventListenerList; import jpcap.PacketReceiver; import jpcap.packet.Packet; import ZClient.EAPSender.EAPNameExcetion; /** * A 802.1x packet process class * @author Lingle<p> * Email: mea08@126.com<p> */ public class PacketProc implements PacketReceiver{ private final static byte EAP_SUCCESS=3;//data[4] private final static byte EAP_FAILURE=4; private final static byte EAP_REQUEST=1;//data[4],data[8] private byte authCode;//Extensible Authentication Protocol Code: 1 Request private byte authType;//Extensible Authentication Protocol Type: 1 Identity private byte id; private EAPSender eapSender =null; private static String messsage =null; private byte[] attachkey=new byte[16]; private EventListenerList listeners =null; public interface EAPListener extends EventListener { public void pktRecivedAction(String messsage); public void successEventAction(); public void failureEventAction(); } public void addEapListener(EAPListener listener) { if (listeners==null) { listeners=new EventListenerList(); } listeners.add(EAPListener.class, listener); } private void notifyEapRecived(String messsage) { for (EAPListener listener: listeners.getListeners(EAPListener.class)) { listener.pktRecivedAction(messsage); } } private void notifyEapSuccess() { for (EAPListener listener: listeners.getListeners(EAPListener.class)) { listener.successEventAction(); } } private void notifyEapFailure() { for (EAPListener listener: listeners.getListeners(EAPListener.class)) { listener.failureEventAction(); } } public PacketProc(EAPSender sender) { this.eapSender=sender; } @Override public void receivePacket(Packet p) { id=p.data[5]; authCode=p.data[4]; authType=p.data[8]; System.arraycopy(p.data, 10, attachkey, 0, attachkey.length); if (authCode==EAP_SUCCESS){ messsage="EAP_SUCCESS"; eapSender.sendEapOnline(); this.notifyEapSuccess(); } if (authCode==EAP_FAILURE) { if(p.data[12]!=0){ byte[] msg=new byte[p.data[12]]; System.arraycopy(p.data, 13, msg, 0, msg.length); messsage=new Scanner(new ByteArrayInputStream(msg)).nextLine(); this.notifyEapFailure(); } } if (authCode == EAP_REQUEST) { if (authType == EAP_REQUEST) { try { eapSender.sendEapName(id); messsage="Send EAP_USERNAME"; } catch (EAPNameExcetion e) { messsage=e.getMessage(); } } else { try { eapSender.sendEapPassWord(id, attachkey); messsage="Send EAP_PASSWORD"; } catch (EAPNameExcetion e) { messsage=e.getMessage(); } } } this.notifyEapRecived(messsage); } }