1 package ZClient;
 2 import java.io.ByteArrayInputStream;
 3 import java.util.EventListener;
 4 import java.util.Scanner;
 5 import javax.swing.event.EventListenerList;
 6 import jpcap.PacketReceiver;
 7 import jpcap.packet.Packet;
 8 import ZClient.EAPSender.EAPNameExcetion;
 9 
10 /**
11  * A 802.1x packet process class
12  * @author Lingle<p>
13  * Email: mea08@126.com<p>
14  */
15 public class PacketProc implements PacketReceiver{
16 
17     private final static byte EAP_SUCCESS=3;//data[4]
18     private final static byte EAP_FAILURE=4;
19     private final static byte EAP_REQUEST=1;//data[4],data[8]
20     private byte authCode;//Extensible Authentication Protocol Code: 1 Request
21     private byte authType;//Extensible Authentication Protocol Type: 1 Identity
22     private byte id;
23     private EAPSender eapSender =null;
24     private static String messsage =null;
25     private byte[] attachkey=new byte[16];
26     private EventListenerList listeners =null;
27     
28     public interface EAPListener extends EventListener {
29         public void pktRecivedAction(String messsage);
30         public void successEventAction();
31         public void failureEventAction();
32     }
33     public void addEapListener(EAPListener listener) {
34         if (listeners==null) {
35             listeners=new EventListenerList();
36         }
37         listeners.add(EAPListener.class, listener);
38     }
39     private void notifyEapRecived(String messsage) {
40         for (EAPListener listener: listeners.getListeners(EAPListener.class)) {
41             listener.pktRecivedAction(messsage);
42         }
43     }
44     private void notifyEapSuccess() {
45         for (EAPListener listener: listeners.getListeners(EAPListener.class)) {
46             listener.successEventAction();
47         }
48     }
49     private void notifyEapFailure() {
50         for (EAPListener listener: listeners.getListeners(EAPListener.class)) {
51             listener.failureEventAction();
52         }
53     }
54     
55     public PacketProc(EAPSender sender) {
56         this.eapSender=sender;
57     }
58 
59 @Override
60     public void receivePacket(Packet p) {
61         id=p.data[5];
62         authCode=p.data[4];
63         authType=p.data[8];
64         System.arraycopy(p.data, 10, attachkey, 0, attachkey.length);
65         if (authCode==EAP_SUCCESS){
66             messsage="EAP_SUCCESS";
67             eapSender.sendEapOnline();
68             this.notifyEapSuccess();
69         }
70         if (authCode==EAP_FAILURE) {
71             if(p.data[12]!=0){
72                 byte[] msg=new byte[p.data[12]];
73                 System.arraycopy(p.data, 13, msg, 0, msg.length);
74                 messsage=new Scanner(new ByteArrayInputStream(msg)).nextLine();
75                 this.notifyEapFailure();
76             }
77         }
78         if (authCode == EAP_REQUEST) {
79             if (authType == EAP_REQUEST) {
80                 try {
81                     eapSender.sendEapName(id);
82                     messsage="Send EAP_USERNAME";
83                 } catch (EAPNameExcetion e) {
84                     messsage=e.getMessage();
85                 }
86             } else {
87                 try {
88                     eapSender.sendEapPassWord(id, attachkey);
89                     messsage="Send EAP_PASSWORD";
90                 } catch (EAPNameExcetion e) {
91                     messsage=e.getMessage();
92                 }
93             }
94         }
95         this.notifyEapRecived(messsage);
96     }
97 }
posted on 2013-02-01 12:03  linnil  阅读(266)  评论(0)    收藏  举报