⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conversation.java

📁 The WLAN Traffic Visualizer is a platform independent Java program providing accurate measurement of
💻 JAVA
字号:
package model;//Copyright (C) 2008 Harald Unander, Wang Wenjuan////    This file is part of WlanTV.////    WlanTV is free software: you can redistribute it and/or modify//    it under the terms of the GNU General Public License as published by//    the Free Software Foundation, either version 3 of the License, or//    (at your option) any later version.////    WlanTV is distributed in the hope that it will be useful,//    but WITHOUT ANY WARRANTY; without even the implied warranty of//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the//    GNU General Public License for more details.////    You should have received a copy of the GNU General Public License//    along with WlanTV.  If not, see <http://www.gnu.org/licenses/>.import java.io.IOException;import java.io.Serializable;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import view.View;import main.Main;import model.Transaction.OneTransaction;public class Conversation {	private Map<Long,AP> apList= new HashMap<Long,AP>();	private Map<Long,STA> staList= new HashMap<Long,STA>();	private Packet ackP, uniP;	private String captureFile;	private Capture capture;	private Set<Long> foundApSet = new HashSet<Long>();	private boolean collectDelay = false;	public Conversation(boolean collectDelay) {		this.collectDelay = collectDelay;	}	public void updateUnicastStaList(OneTransaction one) {		uniP = one.unicastPacket;		if (one.packetList.size()>1 && uniP == one.packetList.get(one.packetList.size()-2))			ackP = one.packetList.get(one.packetList.size()-1);		else			ackP = null;		long staMac=0;		if (uniP.wlanToDs==1 && uniP.wlanFromDs==0)			staMac = uniP.wlanSa;		else if (uniP.wlanToDs==0 && uniP.wlanFromDs==1)			staMac = uniP.wlanDa;		if (staMac!=0) {			STA sta;			if (!staList.containsKey(staMac)) {				sta = new STA();				sta.ap = uniP.wlanBssid;				sta.sta = staMac;				sta.rssi = uniP.rssi;				if (uniP.wlanToDs==1) {					sta.unicastUplinkCount = 1;					if (collectDelay) {						sta.durToDs.calc(uniP.wlanDuration);						if (ackP!=null)							sta.ackDelayToDs.calc((int)(ackP.frameTimeRel-uniP.frameTimeRel));						//							sta.diffToDs.calc((int)(uniP.wlanDuration-(ackP.frameTimeRel-uniP.frameTimeRel)));					}				}				else if (uniP.wlanFromDs==1) {					sta.unicastDownlinkCount = 1;					if (collectDelay) {						sta.durFromDs.calc(uniP.wlanDuration);						if (ackP!=null)							sta.ackDelayFromDs.calc((int)(ackP.frameTimeRel-uniP.frameTimeRel));						//							sta.diffFromDs.calc((int)(uniP.wlanDuration-(ackP.frameTimeRel-uniP.frameTimeRel)));					}				}			}			else {				sta = staList.get(staMac);				if (uniP.wlanToDs==1) {					sta.unicastUplinkCount++;					if (collectDelay) {						sta.durToDs.calc(uniP.wlanDuration);						if (ackP!=null)							sta.ackDelayToDs.calc((int)(ackP.frameTimeRel-uniP.frameTimeRel));						//							sta.diffToDs.calc((int)(uniP.wlanDuration-(ackP.frameTimeRel-uniP.frameTimeRel)));					}				}				else if (uniP.wlanFromDs==1) {					sta.unicastDownlinkCount++;					if (collectDelay) {						sta.durFromDs.calc(uniP.wlanDuration);						if (ackP!=null)							sta.ackDelayFromDs.calc((int)(ackP.frameTimeRel-uniP.frameTimeRel));						//							sta.diffFromDs.calc((int)(uniP.wlanDuration-(ackP.frameTimeRel-uniP.frameTimeRel)));					}				}			}			staList.put(staMac,sta);			if (apList.containsKey(uniP.wlanBssid)) {				AP ap = apList.get(uniP.wlanBssid);				ap.STA.add(staMac);				apList.put(uniP.wlanBssid,ap);			}		}	}	public void updateBeaconApList(Packet r) {		if (!apList.containsKey(r.wlanBssid)) {			AP ap = new AP();			ap.mgmtShortSlotTime = r.mgmtShortSlotTime;			ap.mgmtPreamble = r.mgmtPreamble;			ap.rssi = r.rssi;			ap.bssid = r.wlanBssid;			apList.put(r.wlanBssid,ap);		}	}	public class AP implements Serializable {		private static final long serialVersionUID = 1L;		public int mgmtPreamble;		public int rssi;		public transient Collection<Long> STA = new HashSet<Long>();		public int mgmtShortSlotTime;		public int channel;		public String ssid;		public String rates;		public String erp;		public String xRates;		public String sChannel;		public long bssid;	}	public class STA implements Comparable<STA> {		public int unicastDownlinkCount;		public int unicastUplinkCount;		public int rssi;		public long ap;		public long sta;		public Stat ackDelayToDs = new Stat();		public Stat ackDelayFromDs = new Stat();		public Stat durToDs = new Stat();		public Stat durFromDs = new Stat();		//		public Stat diffToDs = new Stat();		//		public Stat diffFromDs = new Stat();		public int compareTo(STA arg0) {			return this.unicastUplinkCount+this.unicastDownlinkCount - arg0.unicastUplinkCount-arg0.unicastDownlinkCount;		}	}	public Map<Long,AP> getApList() {		return apList;	}	public Map<Long,STA> getStaList() {		return staList;	}	public ArrayList<String> getApListStrings() {		ArrayList<String> list = new ArrayList<String>();		for (long l : apList.keySet()) 			list.add(Packet.longToMac(l));//+" ("+apList.get(l).STA.size()+")");		return list;	}	public ArrayList<String> getStaListStrings() {		ArrayList<String> list = new ArrayList<String>();		List<STA> ls = sortMap(staList);		for (STA sta : ls) { 			list.add(Packet.longToMac(sta.sta)+" ("+sta.unicastDownlinkCount+"/"+sta.unicastUplinkCount+")");		}		return list;	}	private List<STA> sortMap(Map<Long,STA> map) {		List<STA> ls = new ArrayList<STA>(map.values());		Collections.sort(ls);		Collections.reverse(ls);		return ls;	}	public String getInfo() {		StringBuffer sb = new StringBuffer();		for (long ap : apList.keySet()) {			sb.append("AP  "+Packet.longToMac(ap)+"\n");			sb.append("  "+apList.get(ap).ssid+"\n");			sb.append("  "+apList.get(ap).sChannel+"\n");			sb.append("  "+apList.get(ap).erp+"\n");			sb.append("  "+apList.get(ap).rates+"\n");			sb.append("  X"+apList.get(ap).xRates+"\n");			sb.append("  RSSI: "+apList.get(ap).rssi+"\n");			sb.append("\n");			List<STA> ls = new ArrayList<STA>();			for (Long sta : apList.get(ap).STA) ls.add(staList.get(sta));			Collections.sort(ls);			Collections.reverse(ls);			for (STA sta : ls) {				sb.append("  STA  "+Packet.longToMac(sta.sta)+"\n");				sb.append("     RSSI         \t"+sta.rssi+"\n");				sb.append("     Downlink data\t"+sta.unicastDownlinkCount+"\n");				sb.append("     Uplink data  \t"+sta.unicastUplinkCount+"\n");				if (collectDelay) {					sb.append("     FromDs-NAV   \t"+sta.durFromDs.get()+"\n");					sb.append("     FromDs-Delay \t"+sta.ackDelayFromDs.get()+"\n");					//				sb.append("       FromDs-Diff  "+sta.diffFromDs.get()+"\n");					sb.append("     ToDs-NAV     \t"+sta.durToDs.get()+"\n");					sb.append("     ToDs-Delay   \t"+sta.ackDelayToDs.get()+"\n");					//				sb.append("       ToDs-Diff  "+sta.diffToDs.get()+"\n");					checkDelay(sta);				}				sb.append("\n");			}		}		return sb.toString();	}	public void checkDelay(STA sta) {		//		System.out.println(sta.durFromDs.sdMean+" "+sta.ackDelayFromDs.sdMean+" "+sta.durFromDs.sdMean*0.20d);		//		System.out.println(Math.abs(sta.durFromDs.sdMean-sta.ackDelayFromDs.sdMean)+" "+sta.durFromDs.sdMean*0.20d);				double diffFromDs = sta.durFromDs.sdMean-sta.ackDelayFromDs.sdMean;		if (Math.abs(diffFromDs) > sta.durFromDs.sdMean*0.3) {			Main.myLogging.addWarning(this,"Suspicious FromDS ACK delay vs NAV for "+					Packet.longToMac(sta.sta)+"-> "+(int)sta.ackDelayFromDs.sdMean+"/"+(int)sta.durFromDs.sdMean);		}		double diffToDs = sta.durToDs.sdMean-sta.ackDelayToDs.sdMean;		if (Math.abs(diffToDs) > sta.durToDs.sdMean*0.3) {			Main.myLogging.addWarning(this,"Suspicious ToDS ACK delay vs NAV for "+					Packet.longToMac(sta.sta)+"-> "+(int)sta.ackDelayToDs.sdMean+"/"+(int)sta.durToDs.sdMean);		}	}	public AP getAp(long ap) {		return apList.get(ap);	}	public void scanAp(final String captureFile) throws Exception {		if (!captureFile.endsWith(".wtvcap")) {			this.captureFile = View.addQuotesForWindows(captureFile);			foundApSet.addAll(apList.keySet());			if (!foundApSet.isEmpty()) {				capture = new Capture();				capture.run();			}		}	}	class Capture extends Command implements Runnable {		@Override		public void newLine(String line) throws InterruptedException {			String bssid = line.substring(line.length()-17).replaceAll(":","");			long foundAp = Long.parseLong(bssid,16);			if (foundApSet.contains(foundAp)) {				System.out.println(line);				AP ap = apList.get(foundAp);				ap.ssid = find(line,"SSID=",' ');				ap.rates = find(line,"Supported rates: ",'\"'); 				ap.sChannel = find(line,"Current Channel: ",'\"');				ap.channel = Integer.parseInt(ap.sChannel.split(" ")[2]);				ap.erp = find(line,"ERP info: ",'\"');				ap.xRates = findLast(line,"Supported rates: ",'\"');				foundApSet.remove(foundAp);			}			if (foundApSet.isEmpty()) {				capture.abortCapture();			}		}		public String find(String line,String pattern,Character endChar) {			int pos1 = line.indexOf(pattern);			int pos2 = line.indexOf(endChar, pos1);			if (pos1 != -1 && pos2 != -1)				return line.substring(pos1,pos2);			else				return pattern + "null";		}		public String findLast(String line,String pattern,Character endChar) {			int pos1 = line.lastIndexOf(pattern);			int pos2 = line.indexOf(endChar, pos1);			if (pos1 != -1 && pos2 != -1)				return line.substring(pos1,pos2);			else				return pattern + "null";		}		public void run() {			String cmd = Main.tsharkExe+" -l -r"+captureFile+ " -Rwlan.fc.type_subtype==0x08" +			" -zproto,colinfo,wlan.bssid,wlan.bssid -zproto,colinfo,wlan_mgt.tag.interpretation,wlan_mgt.tag.interpretation";			try {				execute(cmd);			} catch (IOException e) {				//				e.printStackTrace();			} catch (LowMemException e) {				//				e.printStackTrace();			} catch (Exception e) {				e.printStackTrace();			}		}	}	public void setAdvanced(boolean on) {		collectDelay = on;	}	public void removeUnusedAPs() {		Set<Long> set = new HashSet<Long>();		set.addAll(apList.keySet());		for (long ap : set) {			if (apList.get(ap).STA.size() == 0)				apList.remove(ap);		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -