001: package tools.sniffer;
002:
003: import java.util.*;
004: import java.text.*;
005: import gov.nist.javax.sip.parser.*;
006: import gov.nist.javax.sip.message.*;
007:
008: /**
009: * A parser for Sniff files. This is the main
010: * workhorse that reads a Ethereal Sniff file and
011: * converts it to a fromat that can be used by the trace viewer application
012: * Acknowledgement:
013: * This code was contributed by Tim Bardzil <bardzil@colorado.edu>.
014: * This code was completed as part of a class project in TLEN 5843
015: * Singaling Protocols, taught by Professor Douglas C. Sicker, Ph.D. at
016: * the University of Colorado, Boulder.
017: *
018: *@author Tim Bardzil <bardzil@colorado.edu> (original)
019: *@author M. Ranganathan (ported to 1.2)
020: *Jeff Adams submitted a patch for this file.
021: *
022: */
023:
024: public class SniffMessage implements ParseExceptionListener {
025: String time;
026: String sourceIP;
027: String destIP;
028: SIPMessage sipMessage;
029:
030: public SniffMessage() {
031: }
032:
033: public SniffMessage(ArrayList sniffMsgList) throws ParseException {
034: getTime(sniffMsgList);
035: getIPAddresses(sniffMsgList);
036: getSipMessage(sniffMsgList);
037: }
038:
039: private void getTime(ArrayList sniffMsgList) {
040: Iterator i = sniffMsgList.iterator();
041: //Date d = new Date(System.currentTimeMillis());
042: while (i.hasNext()) {
043: String line = (String) i.next();
044: if (line.startsWith("Arrival Time")) {
045: time = line.substring(line.indexOf(":") + 1).trim();
046: time = time.substring(0, time.length() - 6);
047: SimpleDateFormat formatter = new SimpleDateFormat(
048: "MMM dd',' yyyy hh:mm:ss.SSS");
049: ParsePosition pos = new ParsePosition(0);
050: Date d = formatter.parse(time, pos);
051: time = String.valueOf(d.getTime());
052: break;
053: }
054: }
055: }
056:
057: private void getIPAddresses(ArrayList sniffMsgList) {
058: Iterator i = sniffMsgList.iterator();
059: while (i.hasNext()) {
060: String line = (String) i.next();
061: if (line.startsWith("Internet Protocol")) {
062: StringTokenizer st = new StringTokenizer(line, ",");
063: while (st.hasMoreTokens()) {
064: String temp = st.nextToken().trim();
065: if (temp.startsWith("Src Addr:")) {
066: StringTokenizer st2 = new StringTokenizer(temp,
067: ":");
068: st2.nextToken(); //skip Src Addr:
069: sourceIP = st2.nextToken().trim();
070: }
071: if (temp.startsWith("Dst Addr:")) {
072: StringTokenizer st2 = new StringTokenizer(temp,
073: ":");
074: st2.nextToken(); //skip Src Addr:
075: destIP = st2.nextToken().trim();
076: }
077: }
078: break;
079: }
080: }
081: }
082:
083: private int indexOfSDP(ArrayList sniffMsgList) {
084: Iterator i = sniffMsgList.iterator();
085: while (i.hasNext()) {
086: String line = (String) i.next();
087: if (line.startsWith("Session Description Protocol")) {
088: return sniffMsgList.indexOf(line);
089: }
090: }
091: return sniffMsgList.size();
092: }
093:
094: private int indexOfSIP(ArrayList sniffMsgList) {
095: Iterator i = sniffMsgList.iterator();
096: while (i.hasNext()) {
097: String line = (String) i.next();
098: if (line.startsWith("Session Initiation Protocol")) {
099: return sniffMsgList.indexOf(line);
100: }
101: }
102: return sniffMsgList.size();
103: }
104:
105: private void getSipMessage(ArrayList sniffMsgList)
106: throws ParseException {
107: int sipIndex = indexOfSIP(sniffMsgList);
108: int sdpIndex = indexOfSDP(sniffMsgList);
109: String msgBuffer = new String();
110:
111: //get SIP message
112: for (int i = sipIndex + 1; i < sdpIndex; i++) {
113: String line = (String) sniffMsgList.get(i);
114: if (line.startsWith("Request-Line")
115: || line.startsWith("Status-Line")) {
116: msgBuffer = msgBuffer
117: + line.substring(line.indexOf(":") + 1).trim()
118: + "\r\n";
119: } else if (line.startsWith("Message Header")) {
120: //do nothing
121: } else if (line.startsWith("Message body")) {
122: //do nothing (start of SDP)
123: } else {
124: msgBuffer = msgBuffer + line.trim() + "\r\n";
125: }
126: }
127:
128: msgBuffer = msgBuffer + "\r\n";
129:
130: //get SDP if it exsits
131: for (int j = sdpIndex; j < sniffMsgList.size(); j++) {
132: String line = (String) sniffMsgList.get(j);
133: if (line.indexOf("(") > 0 && line.indexOf(")") > 0) {
134: msgBuffer = msgBuffer
135: + line.charAt(line.indexOf(":") - 2) + "="
136: + line.substring(line.indexOf(":") + 1).trim()
137: + "\r\n";
138: }
139: }
140:
141: //parse SIP message
142: StringMsgParser parser = new StringMsgParser();
143: parser.setParseExceptionListener(new SniffMessage());
144: sipMessage = parser.parseSIPMessage(msgBuffer);
145: }
146:
147: public String getCallID() {
148:
149: return sipMessage.getCallId().getCallId();
150: }
151:
152: public String toXML() {
153: String xmlMessage = new String();
154:
155: String message = sipMessage.encode();
156: String statusMessage = "";
157: String firstLine = new String();
158: if (sipMessage.getClass().isInstance(new SIPRequest())) {
159: SIPRequest sipReq = (SIPRequest) sipMessage;
160: firstLine = sipReq.getRequestLine().encode().trim();
161: } else if (sipMessage.getClass().isInstance(new SIPResponse())) {
162: SIPResponse sipRes = (SIPResponse) sipMessage;
163: firstLine = sipRes.getStatusLine().encode().trim();
164: }
165: xmlMessage += "<message from=\"" + sourceIP + "\" to=\""
166: + destIP + "\" time=\"" + time + "\" isSender=\""
167: + true + "\" callId=\""
168: + sipMessage.getCallId().getCallId()
169: + "\" statusMessage=\"" + statusMessage
170: + "\" transactionId=\"" + sipMessage.getTransactionId()
171: + "\" firstLine=\"" + firstLine + "\">\n";
172: xmlMessage += "<![CDATA[";
173: xmlMessage += message;
174: xmlMessage += "]]>\n";
175: xmlMessage += "</message>\n";
176:
177: return xmlMessage;
178: }
179:
180: public void handleException(ParseException ex,
181: SIPMessage sipMessage, Class headerClass,
182: String headerText, String messageText)
183: throws ParseException {
184: System.out.println("Error line = " + headerText);
185:
186: try {
187: if (headerClass.equals(Class
188: .forName("gov.nist.javax.sip.header.From"))
189: || headerClass.equals(Class
190: .forName("gov.nist.javax.sip.header.To"))
191: || headerClass
192: .equals(Class
193: .forName("gov.nist.javax.sip.header.ViaList"))
194: || headerClass.equals(Class
195: .forName("gov.nist.javax.sip.header.CSeq"))
196: || headerClass
197: .equals(Class
198: .forName("gov.nist.javax.sip.header.CallId")))
199: throw ex;
200: } catch (ClassNotFoundException e) {
201: System.out
202: .println("could not find class -- internal error");
203: e.printStackTrace();
204: System.exit(0);
205: }
206:
207: }
208: }
|