/** Make a clone of the given object. */ protected static Object makeClone(Object obj) { Object clone_obj = obj; if (obj instanceof String) { String string = (String) obj; clone_obj = (Object) new String(string); } else if (obj instanceof Integer) { clone_obj = new Integer(((Integer) obj).intValue()); } else if (obj instanceof Float) { clone_obj = new Float(((Float) obj).floatValue()); } else if (obj instanceof Double) { clone_obj = new Double(((Double) obj).doubleValue()); } else if (obj instanceof Long) { clone_obj = new Long(((Long) obj).longValue()); } else { // If a clone method exists for the object, then // invoke it try { Class cl = obj.getClass(); Method meth = cl.getMethod("clone", null); clone_obj = meth.invoke(obj, null); } catch (SecurityException ex) { clone_obj = obj; } catch (IllegalArgumentException ex) { InternalErrorHandler.handleException(ex); } catch (IllegalAccessException ex) { clone_obj = obj; } catch (InvocationTargetException ex) { clone_obj = obj; } catch (NoSuchMethodException ex) { clone_obj = obj; } } return clone_obj; }
public static Class getClassFromName(String className) { try { return Class.forName(className); } catch (Exception ex) { InternalErrorHandler.handleException(ex); return null; } }
public static boolean isMySubclass(Class other) { try { return Class.forName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(other); } catch (Exception ex) { InternalErrorHandler.handleException(ex); } return false; }
/** * Do a recursive find and replace of objects pointed to by this object. * * @since v1.0 * @param objectText is the canonical string representation of the object that we want to replace. * @param replacement is the object that we want to replace it with. * @param matchSubstring a boolean which tells if we should match a substring of the target object * A replacement will occur if a portion of the structure is found with matching encoded text * (a substring if matchSubstring is true) as objectText and with the same class as * replacement. */ public void replace(String objectText, GenericObject replacement, boolean matchSubstring) throws IllegalArgumentException { if (objectText == null || replacement == null) { throw new IllegalArgumentException("null argument!"); } Class replacementClass = replacement.getClass(); Class myclass = getClass(); Field[] fields = myclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Class fieldType = f.getType(); if (!getClassFromName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType) && !getClassFromName(SIP_PACKAGE + ".GenericObjectList").isAssignableFrom(fieldType)) { continue; } else if ((f.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE) { continue; } try { if (fieldType.equals(replacementClass)) { if (GenericObject.isMySubclass(replacementClass)) { GenericObject obj = (GenericObject) f.get(this); if (!matchSubstring) { if (objectText.compareTo(obj.encode()) == 0) { f.set(this, replacement); } } else { // Substring match is specified if (obj.encode().indexOf(objectText) >= 0) { f.set(this, replacement); } } } } else if (GenericObjectList.isMySubclass(replacementClass)) { GenericObjectList obj = (GenericObjectList) f.get(this); if (!matchSubstring) { if (objectText.compareTo(obj.encode()) == 0) { f.set(this, replacement); } } else { if (obj.encode().indexOf(objectText) >= 0) { f.set(this, replacement); } } } else if (getClassFromName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType)) { GenericObject g = (GenericObject) f.get(this); g.replace(objectText, replacement, matchSubstring); } else if (getClassFromName(SIP_PACKAGE + ".GenericObjectList") .isAssignableFrom(fieldType)) { GenericObjectList g = (GenericObjectList) f.get(this); g.replace(objectText, replacement, matchSubstring); } } catch (IllegalAccessException ex) { InternalErrorHandler.handleException(ex); } } }
/** * Clone - do a deep copy. * * @return Object AcceptContactHeader */ public Object clone() { try { AcceptContactHeader retval = (AcceptContactHeader) this.getClass().newInstance(); if (this.parameters != null) retval.parameters = (NameValueList) parameters.clone(); return retval; } catch (Exception ex) { InternalErrorHandler.handleException(ex); return null; } }
/** Make a clone of this object. */ public Object clone() { Class myclass = this.getClass(); Object newObject = null; try { newObject = myclass.newInstance(); } catch (Exception ex) { InternalErrorHandler.handleException(ex); } GenericObject gobj = (GenericObject) newObject; if (this.inputText != null) gobj.inputText = new String(this.inputText); return newObject; }
/** * Send a message to a specified receiver address. * * @param msg string to send. * @param peerAddress Address of the place to send it to. * @param peerPort the port to send it to. * @throws IOException If there is trouble sending this message. */ protected void sendMessage(byte[] msg, InetAddress peerAddress, int peerPort, boolean reConnect) throws IOException { // Via is not included in the request so silently drop the reply. if (sipStack.isLoggingEnabled() && this.sipStack.logStackTraceOnMessageSend) { this.sipStack.logWriter.logStackTrace(LogWriter.TRACE_MESSAGES); } if (peerPort == -1) { if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug(getClass().getName() + ":sendMessage: Dropping reply!"); } throw new IOException("Receiver port not set "); } else { if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug( getClass().getName() + ":sendMessage " + peerAddress.getHostAddress() + "/" + peerPort + "\n" + new String(msg)); this.sipStack.logWriter.logDebug("*******************\n"); } } DatagramPacket reply = new DatagramPacket(msg, msg.length, peerAddress, peerPort); try { DatagramSocket sock; boolean created = false; if (sipStack.udpFlag) { // Use the socket from the message processor (for firewall // support use the same socket as the message processor // socket -- feature request # 18 from java.net). This also // makes the whole thing run faster! sock = ((UDPMessageProcessor) messageProcessor).sock; // Bind the socket to the stack address in case there // are multiple interfaces on the machine (feature reqeust // by Will Scullin) 0 binds to an ephemeral port. // sock = new DatagramSocket(0,sipStack.stackInetAddress); } else { // bind to any interface and port. sock = new DatagramSocket(); created = true; } sock.send(reply); if (created) sock.close(); } catch (IOException ex) { throw ex; } catch (Exception ex) { InternalErrorHandler.handleException(ex); } }
/** * An introspection based equality predicate for GenericObjects. * * @other the other object to test against. */ public boolean equals(Object that) { if (!this.getClass().equals(that.getClass())) return false; Class myclass = this.getClass(); Field[] fields = myclass.getDeclaredFields(); Class hisclass = that.getClass(); Field[] hisfields = hisclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Field g = hisfields[i]; // Only print protected and public members. int modifier = f.getModifiers(); if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue; Class fieldType = f.getType(); String fieldName = f.getName(); if (fieldName.compareTo("stringRepresentation") == 0) { continue; } if (fieldName.compareTo("indentation") == 0) { continue; } if (fieldName.compareTo("inputText") == 0) { continue; } try { // Primitive fields are printed with type: value if (fieldType.isPrimitive()) { String fname = fieldType.toString(); if (fname.compareTo("int") == 0) { if (f.getInt(this) != g.getInt(that)) return false; } else if (fname.compareTo("short") == 0) { if (f.getShort(this) != g.getShort(that)) return false; } else if (fname.compareTo("char") == 0) { if (f.getChar(this) != g.getChar(that)) return false; } else if (fname.compareTo("long") == 0) { if (f.getLong(this) != g.getLong(that)) return false; } else if (fname.compareTo("boolean") == 0) { if (f.getBoolean(this) != g.getBoolean(that)) return false; } else if (fname.compareTo("double") == 0) { if (f.getDouble(this) != g.getDouble(that)) return false; } else if (fname.compareTo("float") == 0) { if (f.getFloat(this) != g.getFloat(that)) return false; } } else if (g.get(that) == f.get(this)) return true; else if (f.get(this) == null) return false; else if (g.get(that) == null) return false; else return f.get(this).equals(g.get(that)); } catch (IllegalAccessException ex1) { InternalErrorHandler.handleException(ex1); } } return false; }
/** * Encode this into a byte array. This is used when the body has been set as a binary array and * you want to encode the body as a byte array for transmission. * * @return a byte array containing the SIPRequest encoded as a byte array. */ public byte[] encodeAsBytes(String transport) { if (this.isNullRequest()) { // Encoding a null message for keepalive. return "\r\n\r\n".getBytes(); } else if (this.requestLine == null) { return new byte[0]; } byte[] rlbytes = null; if (requestLine != null) { try { rlbytes = requestLine.encode().getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { InternalErrorHandler.handleException(ex); } } byte[] superbytes = super.encodeAsBytes(transport); byte[] retval = new byte[rlbytes.length + superbytes.length]; System.arraycopy(rlbytes, 0, retval, 0, rlbytes.length); System.arraycopy(superbytes, 0, retval, rlbytes.length, superbytes.length); return retval; }
/** An assertion checking utility. */ protected void Assert(boolean condition, String msg) { if (!condition) InternalErrorHandler.handleException(msg); }
/** * An introspection based predicate matching using a template object. Allows for partial match of * two protocl Objects. * * @other the match pattern to test against. The match object has to be of the same type (class). * Primitive types and non-sip fields that are non null are matched for equality. Null in any * field matches anything. Some book-keeping fields are ignored when making the comparison. */ public boolean match(Object other) { if (other == null) return true; if (!this.getClass().equals(other.getClass())) return false; GenericObject that = (GenericObject) other; Class myclass = this.getClass(); Field[] fields = myclass.getDeclaredFields(); Class hisclass = other.getClass(); Field[] hisfields = hisclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Field g = hisfields[i]; // Only print protected and public members. int modifier = f.getModifiers(); if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue; Class fieldType = f.getType(); String fieldName = f.getName(); if (fieldName.compareTo("stringRepresentation") == 0) { continue; } if (fieldName.compareTo("indentation") == 0) { continue; } if (fieldName.compareTo("inputText") == 0) { continue; } try { // Primitive fields are printed with type: value if (fieldType.isPrimitive()) { String fname = fieldType.toString(); if (fname.compareTo("int") == 0) { if (f.getInt(this) != g.getInt(that)) return false; } else if (fname.compareTo("short") == 0) { if (f.getShort(this) != g.getShort(that)) return false; } else if (fname.compareTo("char") == 0) { if (f.getChar(this) != g.getChar(that)) return false; } else if (fname.compareTo("long") == 0) { if (f.getLong(this) != g.getLong(that)) return false; } else if (fname.compareTo("boolean") == 0) { if (f.getBoolean(this) != g.getBoolean(that)) return false; } else if (fname.compareTo("double") == 0) { if (f.getDouble(this) != g.getDouble(that)) return false; } else if (fname.compareTo("float") == 0) { if (f.getFloat(this) != g.getFloat(that)) return false; } } else { Object myObj = f.get(this); Object hisObj = g.get(that); if (hisObj == myObj) return true; else if (hisObj != null && myObj == null) return false; else if (GenericObject.isMySubclass(myObj.getClass()) && !((GenericObject) myObj).match(hisObj)) return false; else if (hisObj instanceof java.lang.String && myObj instanceof java.lang.String) { if (((String) myObj).compareToIgnoreCase((String) hisObj) != 0) return false; } else if (GenericObjectList.isMySubclass(myObj.getClass()) && !((GenericObjectList) myObj).match(hisObj)) return false; } } catch (IllegalAccessException ex1) { InternalErrorHandler.handleException(ex1); } } return true; }
/** * This gets invoked when thread.start is called from the constructor. Implements a message loop - * reading the tcp connection and processing messages until we are done or the other end has * closed. */ public void run() { String message; Pipeline hispipe = null; // Create a pipeline to connect to our message parser. hispipe = new Pipeline(myClientInputStream, stack.readTimeout, ((SIPTransactionStack) stack).timer); // Create a pipelined message parser to read and parse // messages that we write out to him. myParser = new PipelinedMsgParser(this, hispipe, this.stack.getMaxMessageSize()); // Start running the parser thread. myParser.processInput(); // bug fix by Emmanuel Proulx int bufferSize = 4096; this.tlsMessageProcessor.useCount++; this.isRunning = true; try { while (true) { try { byte[] msg = new byte[bufferSize]; int nbytes = myClientInputStream.read(msg, 0, bufferSize); // no more bytes to read... if (nbytes == -1) { hispipe.write("\r\n\r\n".getBytes("UTF-8")); try { if (stack.maxConnections != -1) { synchronized (tlsMessageProcessor) { tlsMessageProcessor.nConnections--; tlsMessageProcessor.notify(); } } hispipe.close(); mySock.close(); } catch (IOException ioex) { } return; } hispipe.write(msg, 0, nbytes); } catch (IOException ex) { // Terminate the message. try { hispipe.write("\r\n\r\n".getBytes("UTF-8")); } catch (Exception e) { // InternalErrorHandler.handleException(e); } try { if (LogWriter.needsLogging) stack.logWriter.logMessage("IOException closing sock " + ex); try { if (stack.maxConnections != -1) { synchronized (tlsMessageProcessor) { tlsMessageProcessor.nConnections--; tlsMessageProcessor.notify(); } } mySock.close(); hispipe.close(); } catch (IOException ioex) { } } catch (Exception ex1) { // Do nothing. } return; } catch (Exception ex) { InternalErrorHandler.handleException(ex); } } } finally { this.isRunning = false; this.tlsMessageProcessor.remove(this); this.tlsMessageProcessor.useCount--; } }
/** * Gets invoked by the parser as a callback on successful message parsing (i.e. no parser errors). * * @param sipMessage Mesage to process (this calls the application for processing the message). */ public void processMessage(SIPMessage sipMessage) throws Exception { try { if (sipMessage.getFrom() == null || // sipMessage.getFrom().getTag() == null || sipMessage.getTo() == null || sipMessage.getCallId() == null || sipMessage.getCSeq() == null || sipMessage.getViaHeaders() == null) { String badmsg = sipMessage.encode(); if (LogWriter.needsLogging) { stack.logWriter.logMessage("bad message " + badmsg); stack.logWriter.logMessage(">>> Dropped Bad Msg"); } stack.logBadMessage(badmsg); return; } ViaList viaList = sipMessage.getViaHeaders(); // For a request // first via header tells where the message is coming from. // For response, this has already been recorded in the outgoing // message. if (sipMessage instanceof SIPRequest) { Via v = (Via) viaList.first(); if (v.hasPort()) { this.peerPort = v.getPort(); } else this.peerPort = 5061; this.peerProtocol = v.getTransport(); try { this.peerAddress = mySock.getInetAddress(); // Check to see if the received parameter matches // the peer address and tag it appropriately. // Bug fix by [email protected] // Should record host address not host name // bug fix by Joost Yervante Damand if (!v.getSentBy().getInetAddress().equals(this.peerAddress)) { v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress()); // @@@ hagai v.setParameter(Via.RPORT, new Integer(this.peerPort).toString()); } } catch (java.net.UnknownHostException ex) { // Could not resolve the sender address. if (LogWriter.needsLogging) { stack.logWriter.logMessage("Rejecting message -- could not resolve Via Address"); } return; } catch (java.text.ParseException ex) { InternalErrorHandler.handleException(ex); } // Use this for outgoing messages as well. if (!this.isCached) { ((TLSMessageProcessor) this.messageProcessor).cacheMessageChannel(this); this.isCached = true; String key = IOHandler.makeKey(mySock.getInetAddress(), this.peerPort); stack.ioHandler.putSocket(key, mySock); } } // Foreach part of the request header, fetch it and process it long receptionTime = System.currentTimeMillis(); // if (sipMessage instanceof SIPRequest) { // This is a request - process the request. SIPRequest sipRequest = (SIPRequest) sipMessage; // Create a new sever side request processor for this // message and let it handle the rest. if (LogWriter.needsLogging) { stack.logWriter.logMessage("----Processing Message---"); } // Check for reasonable size - reject message // if it is too long. if (stack.getMaxMessageSize() > 0 && sipRequest.getSize() + (sipRequest.getContentLength() == null ? 0 : sipRequest.getContentLength().getContentLength()) > stack.getMaxMessageSize()) { SIPResponse sipResponse = sipRequest.createResponse(SIPResponse.MESSAGE_TOO_LARGE); byte[] resp = sipResponse.encodeAsBytes(); this.sendMessage(resp, false); throw new Exception("Message size exceeded"); } ServerRequestInterface sipServerRequest = stack.newSIPServerRequest(sipRequest, this); sipServerRequest.processRequest(sipRequest, this); if (this.stack.serverLog.needsLogging(ServerLog.TRACE_MESSAGES)) { if (sipServerRequest.getProcessingInfo() == null) { stack.serverLog.logMessage( sipMessage, sipRequest.getViaHost() + ":" + sipRequest.getViaPort(), stack.getHostAddress() + ":" + stack.getPort(this.getTransport()), false, receptionTime); } else { this.stack.serverLog.logMessage( sipMessage, sipRequest.getViaHost() + ":" + sipRequest.getViaPort(), stack.getHostAddress() + ":" + stack.getPort(this.getTransport()), sipServerRequest.getProcessingInfo(), false, receptionTime); } } } else { SIPResponse sipResponse = (SIPResponse) sipMessage; // This is a response message - process it. // Check the size of the response. // If it is too large dump it silently. if (stack.getMaxMessageSize() > 0 && sipResponse.getSize() + (sipResponse.getContentLength() == null ? 0 : sipResponse.getContentLength().getContentLength()) > stack.getMaxMessageSize()) { if (LogWriter.needsLogging) this.stack.logWriter.logMessage("Message size exceeded"); return; } ServerResponseInterface sipServerResponse = stack.newSIPServerResponse(sipResponse, this); sipServerResponse.processResponse(sipResponse, this); } } finally { } }
/** * Return addresses for default proxy to forward the request to. The list is organized in the * following priority. If the requestURI refers directly to a host, the host and port information * are extracted from it and made the next hop on the list. If the default route has been * specified, then it is used to construct the next element of the list. <code> * RouteHeader firstRoute = (RouteHeader) req.getHeader( RouteHeader.NAME ); * if (firstRoute!=null) { * URI uri = firstRoute.getAddress().getURI(); * if (uri.isSIPUri()) { * SipURI nextHop = (SipURI) uri; * if ( nextHop.hasLrParam() ) { * // OK, use it * } else { * nextHop = fixStrictRouting( req ); <--- Here, make the modifications as per RFC3261 * } * } else { * // error: non-SIP URI not allowed in Route headers * throw new SipException( "Request has Route header with non-SIP URI" ); * } * } else if (outboundProxy!=null) { * // use outbound proxy for nextHop * } else if ( req.getRequestURI().isSipURI() ) { * // use request URI for nextHop * } * * </code> * * @param request is the sip request to route. */ public Hop getNextHop(Request request) throws SipException { SIPRequest sipRequest = (SIPRequest) request; RequestLine requestLine = sipRequest.getRequestLine(); if (requestLine == null) { return defaultRoute; } javax.sip.address.URI requestURI = requestLine.getUri(); if (requestURI == null) throw new IllegalArgumentException("Bad message: Null requestURI"); RouteList routes = sipRequest.getRouteHeaders(); /* * In case the topmost Route header contains no 'lr' parameter (which * means the next hop is a strict router), the implementation will * perform 'Route Information Postprocessing' as described in RFC3261 * section 16.6 step 6 (also known as "Route header popping"). That is, * the following modifications will be made to the request: * * The implementation places the Request-URI into the Route header field * as the last value. * * The implementation then places the first Route header field value * into the Request-URI and removes that value from the Route header * field. * * Subsequently, the request URI will be used as next hop target */ if (routes != null) { // to send the request through a specified hop the application is // supposed to prepend the appropriate Route header which. Route route = (Route) routes.getFirst(); URI uri = route.getAddress().getURI(); if (uri.isSipURI()) { SipURI sipUri = (SipURI) uri; if (!sipUri.hasLrParam()) { fixStrictRouting(sipRequest); if (sipStack.isLoggingEnabled()) sipStack.getStackLogger().logDebug("Route post processing fixed strict routing"); } Hop hop = createHop(sipUri, request); if (sipStack.isLoggingEnabled()) sipStack.getStackLogger().logDebug("NextHop based on Route:" + hop); return hop; } else { throw new SipException("First Route not a SIP URI"); } } else if (requestURI.isSipURI() && ((SipURI) requestURI).getMAddrParam() != null) { Hop hop = createHop((SipURI) requestURI, request); if (sipStack.isLoggingEnabled()) sipStack .getStackLogger() .logDebug("Using request URI maddr to route the request = " + hop.toString()); // JvB: don't remove it! // ((SipURI) requestURI).removeParameter("maddr"); return hop; } else if (defaultRoute != null) { if (sipStack.isLoggingEnabled()) sipStack .getStackLogger() .logDebug("Using outbound proxy to route the request = " + defaultRoute.toString()); return defaultRoute; } else if (requestURI.isSipURI()) { Hop hop = createHop((SipURI) requestURI, request); if (hop != null && sipStack.isLoggingEnabled()) sipStack.getStackLogger().logDebug("Used request-URI for nextHop = " + hop.toString()); else if (sipStack.isLoggingEnabled()) { sipStack.getStackLogger().logDebug("returning null hop -- loop detected"); } return hop; } else { // The internal router should never be consulted for non-sip URIs. InternalErrorHandler.handleException( "Unexpected non-sip URI", this.sipStack.getStackLogger()); return null; } }
/** * Send a message to a specified receiver address. * * @param msg message string to send. * @param peerAddress Address of the place to send it to. * @param peerPort the port to send it to. * @param peerProtocol protocol to use to send. * @throws IOException If there is trouble sending this message. */ protected void sendMessage( byte[] msg, InetAddress peerAddress, int peerPort, String peerProtocol, boolean retry) throws IOException { // Via is not included in the request so silently drop the reply. if (peerPort == -1) { if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug(getClass().getName() + ":sendMessage: Dropping reply!"); } throw new IOException("Receiver port not set "); } else { if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug( getClass().getName() + ":sendMessage " + peerAddress.getHostAddress() + "/" + peerPort + "\n" + new String(msg)); this.sipStack.logWriter.logDebug("*******************\n"); } } if (peerProtocol.compareToIgnoreCase("UDP") == 0) { DatagramPacket reply = new DatagramPacket(msg, msg.length, peerAddress, peerPort); try { DatagramSocket sock; if (sipStack.udpFlag) { sock = ((UDPMessageProcessor) messageProcessor).sock; } else { // bind to any interface and port. sock = sipStack.getNetworkLayer().createDatagramSocket(); } if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug( "sendMessage " + peerAddress.getHostAddress() + "/" + peerPort + "\n" + new String(msg)); } sock.send(reply); if (!sipStack.udpFlag) sock.close(); } catch (IOException ex) { throw ex; } catch (Exception ex) { InternalErrorHandler.handleException(ex); } } else { // Use TCP to talk back to the sender. Socket outputSocket = sipStack.ioHandler.sendBytes( this.messageProcessor.getIpAddress(), peerAddress, peerPort, "tcp", msg, retry); OutputStream myOutputStream = outputSocket.getOutputStream(); myOutputStream.write(msg, 0, msg.length); myOutputStream.flush(); // The socket is cached (dont close it!); } }
/** * Generic print formatting function: Does depth-first descent of the structure and recursively * prints all non-private objects pointed to by this object. <bf> Warning - the following generic * string routine will bomb (go into infinite loop) if there are any circularly linked structures * so if you have these, they had better be private! </bf> We dont have to worry about such things * for our structures (we never use circular linked structures). */ public String toString() { stringRepresentation = ""; Class myclass = getClass(); sprint(myclass.getName()); sprint("{"); sprint("inputText:"); sprint(inputText); Field[] fields = myclass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; // Only print protected and public members. int modifier = f.getModifiers(); if ((modifier & Modifier.PRIVATE) == Modifier.PRIVATE) continue; Class fieldType = f.getType(); String fieldName = f.getName(); if (fieldName.compareTo("stringRepresentation") == 0) { // avoid nasty recursions... continue; } if (fieldName.compareTo("indentation") == 0) { // formatting stuff - not relevant here. continue; } sprint(fieldName + ":"); try { // Primitive fields are printed with type: value if (fieldType.isPrimitive()) { String fname = fieldType.toString(); sprint(fname + ":"); if (fname.compareTo("int") == 0) { int intfield = f.getInt(this); sprint(intfield); } else if (fname.compareTo("short") == 0) { short shortField = f.getShort(this); sprint(shortField); } else if (fname.compareTo("char") == 0) { char charField = f.getChar(this); sprint(charField); } else if (fname.compareTo("long") == 0) { long longField = f.getLong(this); sprint(longField); } else if (fname.compareTo("boolean") == 0) { boolean booleanField = f.getBoolean(this); sprint(booleanField); } else if (fname.compareTo("double") == 0) { double doubleField = f.getDouble(this); sprint(doubleField); } else if (fname.compareTo("float") == 0) { float floatField = f.getFloat(this); sprint(floatField); } } else if (Class.forName(SIP_PACKAGE + ".GenericObject").isAssignableFrom(fieldType)) { if (f.get(this) != null) { sprint(((GenericObject) f.get(this)).toString(indentation + 1)); } else { sprint("<null>"); } } else if (Class.forName(SIP_PACKAGE + ".GenericObjectList").isAssignableFrom(fieldType)) { if (f.get(this) != null) { sprint(((GenericObjectList) f.get(this)).toString(indentation + 1)); } else { sprint("<null>"); } } else { // Dont do recursion on things that are not // of our header type... if (f.get(this) != null) { sprint(f.get(this).getClass().getName() + ":"); } else { sprint(fieldType.getName() + ":"); } sprint("{"); if (f.get(this) != null) { sprint(f.get(this).toString()); } else { sprint("<null>"); } sprint("}"); } } catch (IllegalAccessException ex1) { continue; // we are accessing a private field... } catch (Exception ex) { InternalErrorHandler.handleException(ex); } } sprint("}"); return stringRepresentation; }
/** * This is input reading thread for the pipelined parser. You feed it input through the input * stream (see the constructor) and it calls back an event listener interface for message * processing or error. It cleans up the input - dealing with things like line continuation */ public void run() { Pipeline inputStream = this.rawInputStream; // inputStream = new MyFilterInputStream(this.rawInputStream); // I cannot use buffered reader here because we may need to switch // encodings to read the message body. try { while (true) { this.sizeCounter = this.maxMessageSize; // this.messageSize = 0; StringBuffer inputBuffer = new StringBuffer(); if (Debug.parserDebug) Debug.println("Starting parse!"); String line1; String line2 = null; while (true) { try { line1 = readLine(inputStream); // ignore blank lines. if (line1.equals("\n")) { if (Debug.parserDebug) Debug.println("Discarding " + line1); continue; } else break; } catch (IOException ex) { Debug.printStackTrace(ex); this.rawInputStream.stopTimer(); return; } } inputBuffer.append(line1); // Guard against bad guys. this.rawInputStream.startTimer(); while (true) { try { line2 = readLine(inputStream); inputBuffer.append(line2); if (line2.trim().equals("")) break; } catch (IOException ex) { this.rawInputStream.stopTimer(); Debug.printStackTrace(ex); return; } } // Stop the timer that will kill the read. this.rawInputStream.stopTimer(); inputBuffer.append(line2); StringMsgParser smp = new StringMsgParser(sipMessageListener); smp.readBody = false; SIPMessage sipMessage = null; try { sipMessage = smp.parseSIPMessage(inputBuffer.toString()); if (sipMessage == null) { this.rawInputStream.stopTimer(); continue; } } catch (ParseException ex) { // Just ignore the parse exception. continue; } if (Debug.parserDebug) Debug.println("Completed parsing message"); ContentLength cl = (ContentLength) sipMessage.getContentLength(); int contentLength = 0; if (cl != null) { contentLength = cl.getContentLength(); } else { contentLength = 0; } if (Debug.parserDebug) { Debug.println("contentLength " + contentLength); Debug.println("sizeCounter " + this.sizeCounter); Debug.println("maxMessageSize " + this.maxMessageSize); } if (contentLength == 0) { sipMessage.removeContent(); } else if (maxMessageSize == 0 || contentLength < this.sizeCounter) { byte[] message_body = new byte[contentLength]; int nread = 0; while (nread < contentLength) { // Start my starvation timer. // This ensures that the other end // writes at least some data in // or we will close the pipe from // him. This prevents DOS attack // that takes up all our connections. this.rawInputStream.startTimer(); try { int readlength = inputStream.read(message_body, nread, contentLength - nread); if (readlength > 0) { nread += readlength; } else { break; } } catch (IOException ex) { ex.printStackTrace(); break; } finally { // Stop my starvation timer. this.rawInputStream.stopTimer(); } } sipMessage.setMessageContent(message_body); } // Content length too large - process the message and // return error from there. if (sipMessageListener != null) { try { sipMessageListener.processMessage(sipMessage); } catch (Exception ex) { // fatal error in processing - close the // connection. break; } } } } finally { try { inputStream.close(); } catch (IOException e) { InternalErrorHandler.handleException(e); } } }
/** * Process an incoming datagram * * @param packet is the incoming datagram packet. */ private void processIncomingDataPacket(DatagramPacket packet) throws Exception { this.peerAddress = packet.getAddress(); int packetLength = packet.getLength(); // Read bytes and put it in a eueue. byte[] bytes = packet.getData(); byte[] msgBytes = new byte[packetLength]; System.arraycopy(bytes, 0, msgBytes, 0, packetLength); // Do debug logging. if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug( "UDPMessageChannel: processIncomingDataPacket : peerAddress = " + peerAddress.getHostAddress() + "/" + packet.getPort() + " Length = " + packetLength); } SIPMessage sipMessage = null; try { this.receptionTime = System.currentTimeMillis(); sipMessage = myParser.parseSIPMessage(msgBytes); myParser = null; } catch (ParseException ex) { myParser = null; // let go of the parser reference. if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug("Rejecting message ! " + new String(msgBytes)); this.sipStack.logWriter.logDebug("error message " + ex.getMessage()); this.sipStack.logWriter.logException(ex); } // JvB: send a 400 response for requests (except ACK) // Currently only UDP, @todo also other transports String msgString = new String(msgBytes, 0, packetLength); if (!msgString.startsWith("SIP/") && !msgString.startsWith("ACK ")) { String badReqRes = createBadReqRes(msgString, ex); if (badReqRes != null) { if (sipStack.isLoggingEnabled()) { sipStack.getLogWriter().logDebug("Sending automatic 400 Bad Request:"); sipStack.getLogWriter().logDebug(badReqRes); } try { this.sendMessage(badReqRes.getBytes(), peerAddress, packet.getPort(), "UDP", false); } catch (IOException e) { this.sipStack.logWriter.logException(e); } } else { if (sipStack.isLoggingEnabled()) { sipStack.getLogWriter().logDebug("Could not formulate automatic 400 Bad Request"); } } } return; } // No parse exception but null message - reject it and // march on (or return). // exit this message processor if the message did not parse. if (sipMessage == null) { if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logDebug("Rejecting message ! + Null message parsed."); } return; } ViaList viaList = sipMessage.getViaHeaders(); // Check for the required headers. if (sipMessage.getFrom() == null || sipMessage.getTo() == null || sipMessage.getCallId() == null || sipMessage.getCSeq() == null || sipMessage.getViaHeaders() == null) { String badmsg = new String(msgBytes); if (sipStack.isLoggingEnabled()) { this.sipStack.logWriter.logError("bad message " + badmsg); this.sipStack.logWriter.logError( ">>> Dropped Bad Msg " + "From = " + sipMessage.getFrom() + "To = " + sipMessage.getTo() + "CallId = " + sipMessage.getCallId() + "CSeq = " + sipMessage.getCSeq() + "Via = " + sipMessage.getViaHeaders()); } sipStack.logWriter.logError("BAD MESSAGE!"); return; } // For a request first via header tells where the message // is coming from. // For response, just get the port from the packet. if (sipMessage instanceof SIPRequest) { Via v = (Via) viaList.getFirst(); Hop hop = sipStack.addressResolver.resolveAddress(v.getHop()); this.peerPort = hop.getPort(); this.peerProtocol = v.getTransport(); this.peerPacketSourceAddress = packet.getAddress(); this.peerPacketSourcePort = packet.getPort(); try { this.peerAddress = packet.getAddress(); // Check to see if the received parameter matches // the peer address and tag it appropriately. boolean hasRPort = v.hasParameter(Via.RPORT); if (hasRPort || !hop.getHost().equals(this.peerAddress.getHostAddress())) { v.setParameter(Via.RECEIVED, this.peerAddress.getHostAddress()); } if (hasRPort) { v.setParameter(Via.RPORT, Integer.toString(this.peerPacketSourcePort)); } } catch (java.text.ParseException ex1) { InternalErrorHandler.handleException(ex1); } } else { this.peerPacketSourceAddress = packet.getAddress(); this.peerPacketSourcePort = packet.getPort(); this.peerAddress = packet.getAddress(); this.peerPort = packet.getPort(); this.peerProtocol = ((Via) viaList.getFirst()).getTransport(); } this.processMessage(sipMessage); }