Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package tools.sniffer;
  
  import java.util.*;
  import java.text.*;
A parser for Sniff files. This is the main workhorse that reads a Ethereal Sniff file and converts it to a fromat that can be used by the trace viewer application Acknowledgement: This code was contributed by Tim Bardzil <bardzil@colorado.edu>. This code was completed as part of a class project in TLEN 5843 Singaling Protocols, taught by Professor Douglas C. Sicker, Ph.D. at the University of Colorado, Boulder.

Author(s):
Tim Bardzil <bardzil@colorado.edu> (original)
M. Ranganathan (ported to 1.2) Jeff Adams submitted a patch for this file.
 
 
 public class SniffMessage implements ParseExceptionListener {
     String time;
     String sourceIP;
     String destIP;
 
     public SniffMessage() {
     }
 
     public SniffMessage(ArrayList sniffMsgListthrows ParseException {
         getTime(sniffMsgList);
         getIPAddresses(sniffMsgList);
         getSipMessage(sniffMsgList);
     }
 
     private void getTime(ArrayList sniffMsgList) {
         Iterator i = sniffMsgList.iterator();
         //Date d = new Date(System.currentTimeMillis());
         while (i.hasNext()) {
             String line = (Stringi.next();
             if (line.startsWith("Arrival Time")) {
                  = line.substring(line.indexOf(":") + 1).trim();
                  = .substring(0, .length() - 6);
                 SimpleDateFormat formatter =
                     new SimpleDateFormat("MMM dd',' yyyy hh:mm:ss.SSS");
                 ParsePosition pos = new ParsePosition(0);
                 Date d = formatter.parse(pos);
                  = String.valueOf(d.getTime());
                 break;
             }
         }
     }
 
     private void getIPAddresses(ArrayList sniffMsgList) {
         Iterator i = sniffMsgList.iterator();
         while (i.hasNext()) {
             String line = (Stringi.next();
             if (line.startsWith("Internet Protocol")) {
                 StringTokenizer st = new StringTokenizer(line",");
                 while (st.hasMoreTokens()) {
                     String temp = st.nextToken().trim();
                     if (temp.startsWith("Src Addr:")) {
                         StringTokenizer st2 = new StringTokenizer(temp":");
                         st2.nextToken(); //skip Src Addr:
                          = st2.nextToken().trim();
                     }
                     if (temp.startsWith("Dst Addr:")) {
                         StringTokenizer st2 = new StringTokenizer(temp":");
                         st2.nextToken(); //skip Src Addr:
                          = st2.nextToken().trim();
                     }
                 }
                 break;
             }
         }
     }
 
     private int indexOfSDP(ArrayList sniffMsgList) {
         Iterator i = sniffMsgList.iterator();
         while (i.hasNext()) {
             String line = (Stringi.next();
             if (line.startsWith("Session Description Protocol")) {
                 return sniffMsgList.indexOf(line);
             }
         }
         return sniffMsgList.size();
     }
 
     private int indexOfSIP(ArrayList sniffMsgList) {
         Iterator i = sniffMsgList.iterator();
         while (i.hasNext()) {
             String line = (Stringi.next();
             if (line.startsWith("Session Initiation Protocol")) {
                 return sniffMsgList.indexOf(line);
             }
         }
        return sniffMsgList.size();
    }
    private void getSipMessage(ArrayList sniffMsgListthrows ParseException {
        int sipIndex = indexOfSIP(sniffMsgList);
        int sdpIndex = indexOfSDP(sniffMsgList);
        String msgBuffer = new String();
        //get SIP message
        for (int i = sipIndex + 1; i < sdpIndexi++) {
            String line = (StringsniffMsgList.get(i);
            if (line.startsWith("Request-Line")
                || line.startsWith("Status-Line")) {
                msgBuffer =
                    msgBuffer
                        + line.substring(line.indexOf(":") + 1).trim()
                        + "\r\n";
            } else if (line.startsWith("Message Header")) {
                //do nothing
            } else if (line.startsWith("Message body")) {
                //do nothing (start of SDP)
            } else {
                msgBuffer = msgBuffer + line.trim() + "\r\n";
            }
        }
        msgBuffer = msgBuffer + "\r\n";
        //get SDP if it exsits
        for (int j = sdpIndexj < sniffMsgList.size(); j++) {
            String line = (StringsniffMsgList.get(j);
            if (line.indexOf("(") > 0 && line.indexOf(")") > 0) {
                msgBuffer =
                    msgBuffer
                        + line.charAt(line.indexOf(":") - 2)
                        + "="
                        + line.substring(line.indexOf(":") + 1).trim()
                        + "\r\n";
            }
        }
        //parse SIP message
        StringMsgParser parser = new StringMsgParser();
         = parser.parseSIPMessage(msgBuffer.getBytes(), truefalsenew SniffMessage());
    }
    public String getCallID() {
        return .getCallId().getCallId();
    }
    public String toXML() {
        String xmlMessage = new String();
        String message = .encode();
        String statusMessage = "";
        String firstLine = new String();
        if (.getClass().isInstance(new SIPRequest())) {
            SIPRequest sipReq = (SIPRequest;
            firstLine = sipReq.getRequestLine().encode().trim();
        } else if (.getClass().isInstance(new SIPResponse())) {
            SIPResponse sipRes = (SIPResponse;
            firstLine = sipRes.getStatusLine().encode().trim();
        }
        xmlMessage += "<message from=\""
            + 
            + "\" to=\""
            + 
            + "\" time=\""
            + 
            + "\" isSender=\""
            + true
            + "\" callId=\""
            + .getCallId().getCallId()
            + "\" statusMessage=\""
            + statusMessage
            + "\" transactionId=\""
            + .getTransactionId()
            + "\" firstLine=\""
            + firstLine
            + "\">\n";
        xmlMessage += "<![CDATA[";
        xmlMessage += message;
        xmlMessage += "]]>\n";
        xmlMessage += "</message>\n";
        return xmlMessage;
    }
    public void handleException(
        ParseException ex,
        SIPMessage sipMessage,
        Class headerClass,
        String headerText,
        String messageText)
        throws ParseException {
        ..println("Error line = " + headerText);
        try {
            if (headerClass
                .equals(Class.forName("gov.nist.javax.sip.header.From"))
                || headerClass.equals(
                    Class.forName("gov.nist.javax.sip.header.To"))
                || headerClass.equals(
                    Class.forName("gov.nist.javax.sip.header.ViaList"))
                || headerClass.equals(
                    Class.forName("gov.nist.javax.sip.header.CSeq"))
                || headerClass.equals(
                    Class.forName("gov.nist.javax.sip.header.CallId")))
                throw ex;
        } catch (ClassNotFoundException e) {
            ..println("could not find class -- internal error");
            e.printStackTrace();
            System.exit(0);
        }
    }
New to GrepCode? Check out our FAQ X